xref: /petsc/src/dm/interface/dm.c (revision 90b157c44842f659fe30172b3ce8b86a412dec46)
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;
1001de53e9aSMatthew G. Knepley   PetscInt       dim;
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   }
13638221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
13738221697SMatthew G. Knepley   if (coords) {
13838221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
13938221697SMatthew G. Knepley   } else {
14038221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
14138221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
14238221697SMatthew G. Knepley   }
143*90b157c4SStefano Zampini   {
144*90b157c4SStefano Zampini     PetscBool             isper;
145c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1465dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
147*90b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
148*90b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
149c6b900c6SMatthew G. Knepley   }
15038221697SMatthew G. Knepley   PetscFunctionReturn(0);
15138221697SMatthew G. Knepley }
15238221697SMatthew G. Knepley 
1539a42bb27SBarry Smith /*@C
154564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1559a42bb27SBarry Smith 
1568472ad0fSDave May    Logically Collective on DM
1579a42bb27SBarry Smith 
1589a42bb27SBarry Smith    Input Parameter:
1599a42bb27SBarry Smith +  da - initial distributed array
1608154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1619a42bb27SBarry Smith 
1629a42bb27SBarry Smith    Options Database:
163dd85299cSBarry Smith .   -dm_vec_type ctype
1649a42bb27SBarry Smith 
1659a42bb27SBarry Smith    Level: intermediate
1669a42bb27SBarry Smith 
1678472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1689a42bb27SBarry Smith @*/
16919fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1709a42bb27SBarry Smith {
1719a42bb27SBarry Smith   PetscErrorCode ierr;
1729a42bb27SBarry Smith 
1739a42bb27SBarry Smith   PetscFunctionBegin;
1749a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1759a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
17619fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1779a42bb27SBarry Smith   PetscFunctionReturn(0);
1789a42bb27SBarry Smith }
1799a42bb27SBarry Smith 
180c0dedaeaSBarry Smith /*@C
181c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
182c0dedaeaSBarry Smith 
1838472ad0fSDave May    Logically Collective on DM
184c0dedaeaSBarry Smith 
185c0dedaeaSBarry Smith    Input Parameter:
186c0dedaeaSBarry Smith .  da - initial distributed array
187c0dedaeaSBarry Smith 
188c0dedaeaSBarry Smith    Output Parameter:
189c0dedaeaSBarry Smith .  ctype - the vector type
190c0dedaeaSBarry Smith 
191c0dedaeaSBarry Smith    Level: intermediate
192c0dedaeaSBarry Smith 
1938472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
194c0dedaeaSBarry Smith @*/
195c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
196c0dedaeaSBarry Smith {
197c0dedaeaSBarry Smith   PetscFunctionBegin;
198c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
199c0dedaeaSBarry Smith   *ctype = da->vectype;
200c0dedaeaSBarry Smith   PetscFunctionReturn(0);
201c0dedaeaSBarry Smith }
202c0dedaeaSBarry Smith 
2035f1ad066SMatthew G Knepley /*@
20434f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2055f1ad066SMatthew G Knepley 
2065f1ad066SMatthew G Knepley   Not collective
2075f1ad066SMatthew G Knepley 
2085f1ad066SMatthew G Knepley   Input Parameter:
2095f1ad066SMatthew G Knepley . v - The Vec
2105f1ad066SMatthew G Knepley 
2115f1ad066SMatthew G Knepley   Output Parameter:
2125f1ad066SMatthew G Knepley . dm - The DM
2135f1ad066SMatthew G Knepley 
2145f1ad066SMatthew G Knepley   Level: intermediate
2155f1ad066SMatthew G Knepley 
2165f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2175f1ad066SMatthew G Knepley @*/
2185f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2195f1ad066SMatthew G Knepley {
2205f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2215f1ad066SMatthew G Knepley 
2225f1ad066SMatthew G Knepley   PetscFunctionBegin;
2235f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2245f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2255f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2265f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2275f1ad066SMatthew G Knepley }
2285f1ad066SMatthew G Knepley 
2295f1ad066SMatthew G Knepley /*@
230d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2315f1ad066SMatthew G Knepley 
2325f1ad066SMatthew G Knepley   Not collective
2335f1ad066SMatthew G Knepley 
2345f1ad066SMatthew G Knepley   Input Parameters:
2355f1ad066SMatthew G Knepley + v - The Vec
2365f1ad066SMatthew G Knepley - dm - The DM
2375f1ad066SMatthew G Knepley 
238d9805387SMatthew 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.
239d9805387SMatthew G. Knepley 
2405f1ad066SMatthew G Knepley   Level: intermediate
2415f1ad066SMatthew G Knepley 
2425f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2435f1ad066SMatthew G Knepley @*/
2445f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2455f1ad066SMatthew G Knepley {
2465f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2475f1ad066SMatthew G Knepley 
2485f1ad066SMatthew G Knepley   PetscFunctionBegin;
2495f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
250d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2515f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2525f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2535f1ad066SMatthew G Knepley }
2545f1ad066SMatthew G Knepley 
255521d9a4cSLisandro Dalcin /*@C
256521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
257521d9a4cSLisandro Dalcin 
258521d9a4cSLisandro Dalcin    Logically Collective on DM
259521d9a4cSLisandro Dalcin 
260521d9a4cSLisandro Dalcin    Input Parameter:
261521d9a4cSLisandro Dalcin +  dm - the DM context
262a2b5a043SBarry Smith -  ctype - the matrix type
263521d9a4cSLisandro Dalcin 
264521d9a4cSLisandro Dalcin    Options Database:
265521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
266521d9a4cSLisandro Dalcin 
267521d9a4cSLisandro Dalcin    Level: intermediate
268521d9a4cSLisandro Dalcin 
269c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
270521d9a4cSLisandro Dalcin @*/
27119fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
272521d9a4cSLisandro Dalcin {
273521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
27488f0584fSBarry Smith 
275521d9a4cSLisandro Dalcin   PetscFunctionBegin;
276521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
277521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
27819fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
279521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
280521d9a4cSLisandro Dalcin }
281521d9a4cSLisandro Dalcin 
282c0dedaeaSBarry Smith /*@C
283c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
284c0dedaeaSBarry Smith 
285c0dedaeaSBarry Smith    Logically Collective on DM
286c0dedaeaSBarry Smith 
287c0dedaeaSBarry Smith    Input Parameter:
288c0dedaeaSBarry Smith .  dm - the DM context
289c0dedaeaSBarry Smith 
290c0dedaeaSBarry Smith    Output Parameter:
291c0dedaeaSBarry Smith .  ctype - the matrix type
292c0dedaeaSBarry Smith 
293c0dedaeaSBarry Smith    Options Database:
294c0dedaeaSBarry Smith .   -dm_mat_type ctype
295c0dedaeaSBarry Smith 
296c0dedaeaSBarry Smith    Level: intermediate
297c0dedaeaSBarry Smith 
298c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
299c0dedaeaSBarry Smith @*/
300c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
301c0dedaeaSBarry Smith {
302c0dedaeaSBarry Smith   PetscFunctionBegin;
303c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
304c0dedaeaSBarry Smith   *ctype = dm->mattype;
305c0dedaeaSBarry Smith   PetscFunctionReturn(0);
306c0dedaeaSBarry Smith }
307c0dedaeaSBarry Smith 
308c688c046SMatthew G Knepley /*@
30934f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
310c688c046SMatthew G Knepley 
311c688c046SMatthew G Knepley   Not collective
312c688c046SMatthew G Knepley 
313c688c046SMatthew G Knepley   Input Parameter:
314c688c046SMatthew G Knepley . A - The Mat
315c688c046SMatthew G Knepley 
316c688c046SMatthew G Knepley   Output Parameter:
317c688c046SMatthew G Knepley . dm - The DM
318c688c046SMatthew G Knepley 
319c688c046SMatthew G Knepley   Level: intermediate
320c688c046SMatthew G Knepley 
321c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
322c688c046SMatthew G Knepley @*/
323c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
324c688c046SMatthew G Knepley {
325c688c046SMatthew G Knepley   PetscErrorCode ierr;
326c688c046SMatthew G Knepley 
327c688c046SMatthew G Knepley   PetscFunctionBegin;
328c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
329c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
330c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
331c688c046SMatthew G Knepley   PetscFunctionReturn(0);
332c688c046SMatthew G Knepley }
333c688c046SMatthew G Knepley 
334c688c046SMatthew G Knepley /*@
335c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
336c688c046SMatthew G Knepley 
337c688c046SMatthew G Knepley   Not collective
338c688c046SMatthew G Knepley 
339c688c046SMatthew G Knepley   Input Parameters:
340c688c046SMatthew G Knepley + A - The Mat
341c688c046SMatthew G Knepley - dm - The DM
342c688c046SMatthew G Knepley 
343c688c046SMatthew G Knepley   Level: intermediate
344c688c046SMatthew G Knepley 
345c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
346c688c046SMatthew G Knepley @*/
347c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
348c688c046SMatthew G Knepley {
349c688c046SMatthew G Knepley   PetscErrorCode ierr;
350c688c046SMatthew G Knepley 
351c688c046SMatthew G Knepley   PetscFunctionBegin;
352c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3538865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
354c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
355c688c046SMatthew G Knepley   PetscFunctionReturn(0);
356c688c046SMatthew G Knepley }
357c688c046SMatthew G Knepley 
3589a42bb27SBarry Smith /*@C
3599a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3606757b960SDave May    DM options in the database.
3619a42bb27SBarry Smith 
3628353ddbbSDave May    Logically Collective on DM
3639a42bb27SBarry Smith 
3649a42bb27SBarry Smith    Input Parameter:
3658353ddbbSDave May +  da - the DM context
3669a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3679a42bb27SBarry Smith 
3689a42bb27SBarry Smith    Notes:
3699a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3709a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3719a42bb27SBarry Smith 
3729a42bb27SBarry Smith    Level: advanced
3739a42bb27SBarry Smith 
3748353ddbbSDave May .keywords: DM, set, options, prefix, database
3759a42bb27SBarry Smith 
3769a42bb27SBarry Smith .seealso: DMSetFromOptions()
3779a42bb27SBarry Smith @*/
3787087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3799a42bb27SBarry Smith {
3809a42bb27SBarry Smith   PetscErrorCode ierr;
3819a42bb27SBarry Smith 
3829a42bb27SBarry Smith   PetscFunctionBegin;
3839a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3849a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
385691be533SLawrence Mitchell   if (dm->sf) {
386691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
387691be533SLawrence Mitchell   }
388691be533SLawrence Mitchell   if (dm->defaultSF) {
389691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
390691be533SLawrence Mitchell   }
3919a42bb27SBarry Smith   PetscFunctionReturn(0);
3929a42bb27SBarry Smith }
3939a42bb27SBarry Smith 
39431697293SDave May /*@C
39531697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
39631697293SDave May    DM options in the database.
39731697293SDave May 
39831697293SDave May    Logically Collective on DM
39931697293SDave May 
40031697293SDave May    Input Parameters:
40131697293SDave May +  dm - the DM context
40231697293SDave May -  prefix - the prefix string to prepend to all DM option requests
40331697293SDave May 
40431697293SDave May    Notes:
40531697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
40631697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
40731697293SDave May 
40831697293SDave May    Level: advanced
40931697293SDave May 
41031697293SDave May .keywords: DM, append, options, prefix, database
41131697293SDave May 
41231697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
41331697293SDave May @*/
41431697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
41531697293SDave May {
41631697293SDave May   PetscErrorCode ierr;
41731697293SDave May 
41831697293SDave May   PetscFunctionBegin;
41931697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42031697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
42131697293SDave May   PetscFunctionReturn(0);
42231697293SDave May }
42331697293SDave May 
42431697293SDave May /*@C
42531697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
42631697293SDave May    DM options in the database.
42731697293SDave May 
42831697293SDave May    Not Collective
42931697293SDave May 
43031697293SDave May    Input Parameters:
43131697293SDave May .  dm - the DM context
43231697293SDave May 
43331697293SDave May    Output Parameters:
43431697293SDave May .  prefix - pointer to the prefix string used is returned
43531697293SDave May 
43631697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
43731697293SDave May    sufficient length to hold the prefix.
43831697293SDave May 
43931697293SDave May    Level: advanced
44031697293SDave May 
44131697293SDave May .keywords: DM, set, options, prefix, database
44231697293SDave May 
44331697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
44431697293SDave May @*/
44531697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
44631697293SDave May {
44731697293SDave May   PetscErrorCode ierr;
44831697293SDave May 
44931697293SDave May   PetscFunctionBegin;
45031697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45131697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
45231697293SDave May   PetscFunctionReturn(0);
45331697293SDave May }
45431697293SDave May 
45588bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
45688bdff64SToby Isaac {
45788bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
45888bdff64SToby Isaac   DMNamedVecLink nlink;
45988bdff64SToby Isaac   PetscErrorCode ierr;
46088bdff64SToby Isaac 
46188bdff64SToby Isaac   PetscFunctionBegin;
46288bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
46388bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
46488bdff64SToby Isaac     if (dm->localin[i])  refct--;
46588bdff64SToby Isaac     if (dm->globalin[i]) refct--;
46688bdff64SToby Isaac   }
46788bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
46888bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
46988bdff64SToby Isaac   if (dm->x) {
47088bdff64SToby Isaac     DM obj;
47188bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
47288bdff64SToby Isaac     if (obj == dm) refct--;
47388bdff64SToby Isaac   }
47488bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
47588bdff64SToby Isaac     refct--;
47688bdff64SToby Isaac     if (recurseCoarse) {
47788bdff64SToby Isaac       PetscInt coarseCount;
47888bdff64SToby Isaac 
47988bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
48088bdff64SToby Isaac       refct += coarseCount;
48188bdff64SToby Isaac     }
48288bdff64SToby Isaac   }
48388bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
48488bdff64SToby Isaac     refct--;
48588bdff64SToby Isaac     if (recurseFine) {
48688bdff64SToby Isaac       PetscInt fineCount;
48788bdff64SToby Isaac 
48888bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
48988bdff64SToby Isaac       refct += fineCount;
49088bdff64SToby Isaac     }
49188bdff64SToby Isaac   }
49288bdff64SToby Isaac   *ncrefct = refct;
49388bdff64SToby Isaac   PetscFunctionReturn(0);
49488bdff64SToby Isaac }
49588bdff64SToby Isaac 
496354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
497354557abSToby Isaac {
498354557abSToby Isaac   PetscErrorCode ierr;
499354557abSToby Isaac 
500354557abSToby Isaac   PetscFunctionBegin;
501354557abSToby Isaac   if (!--(dm->labels->refct)) {
502354557abSToby Isaac     DMLabelLink next = dm->labels->next;
503354557abSToby Isaac 
504354557abSToby Isaac     /* destroy the labels */
505354557abSToby Isaac     while (next) {
506354557abSToby Isaac       DMLabelLink tmp = next->next;
507354557abSToby Isaac 
508354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
509354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
510354557abSToby Isaac       next = tmp;
511354557abSToby Isaac     }
512354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
513354557abSToby Isaac   }
514354557abSToby Isaac   PetscFunctionReturn(0);
515354557abSToby Isaac }
516354557abSToby Isaac 
51747c6ae99SBarry Smith /*@
5188472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
51947c6ae99SBarry Smith 
52047c6ae99SBarry Smith     Collective on DM
52147c6ae99SBarry Smith 
52247c6ae99SBarry Smith     Input Parameter:
52347c6ae99SBarry Smith .   dm - the DM object to destroy
52447c6ae99SBarry Smith 
52547c6ae99SBarry Smith     Level: developer
52647c6ae99SBarry Smith 
527e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
52847c6ae99SBarry Smith 
52947c6ae99SBarry Smith @*/
530fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
53147c6ae99SBarry Smith {
53288bdff64SToby Isaac   PetscInt       i, cnt;
533dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
53447c6ae99SBarry Smith   PetscErrorCode ierr;
53547c6ae99SBarry Smith 
53647c6ae99SBarry Smith   PetscFunctionBegin;
5376bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5386bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
53987e657c6SBarry Smith 
54088bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
54188bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
54288bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
54388bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
544732e2eb9SMatthew G Knepley   /*
545732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
546732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
547732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
548732e2eb9SMatthew G Knepley   */
5496bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5506bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
551732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5526bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5536bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
554732e2eb9SMatthew G Knepley   }
555f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5560298fd71SBarry Smith   (*dm)->namedglobal = NULL;
557f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5582348bcf4SPeter Brune     nnext = nlink->next;
5592348bcf4SPeter 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);
5602348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5612348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5622348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5632348bcf4SPeter Brune   }
564f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5650298fd71SBarry Smith   (*dm)->namedlocal = NULL;
566f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
567f490541aSPeter Brune     nnext = nlink->next;
568f490541aSPeter 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);
569f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
570f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
571f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
572f490541aSPeter Brune   }
5732348bcf4SPeter Brune 
574b17ce1afSJed Brown   /* Destroy the list of hooks */
575c833c3b5SJed Brown   {
576c833c3b5SJed Brown     DMCoarsenHookLink link,next;
577b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
578b17ce1afSJed Brown       next = link->next;
579b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
580b17ce1afSJed Brown     }
5810298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
582c833c3b5SJed Brown   }
583c833c3b5SJed Brown   {
584c833c3b5SJed Brown     DMRefineHookLink link,next;
585c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
586c833c3b5SJed Brown       next = link->next;
587c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
588c833c3b5SJed Brown     }
5890298fd71SBarry Smith     (*dm)->refinehook = NULL;
590c833c3b5SJed Brown   }
591be081cd6SPeter Brune   {
592be081cd6SPeter Brune     DMSubDomainHookLink link,next;
593be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
594be081cd6SPeter Brune       next = link->next;
595be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
596be081cd6SPeter Brune     }
5970298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
598be081cd6SPeter Brune   }
599baf369e7SPeter Brune   {
600baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
601baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
602baf369e7SPeter Brune       next = link->next;
603baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
604baf369e7SPeter Brune     }
6050298fd71SBarry Smith     (*dm)->gtolhook = NULL;
606baf369e7SPeter Brune   }
607d4d07f1eSToby Isaac   {
608d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
609d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
610d4d07f1eSToby Isaac       next = link->next;
611d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
612d4d07f1eSToby Isaac     }
613d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
614d4d07f1eSToby Isaac   }
615aa1993deSMatthew G Knepley   /* Destroy the work arrays */
616aa1993deSMatthew G Knepley   {
617aa1993deSMatthew G Knepley     DMWorkLink link,next;
618aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
619aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
620aa1993deSMatthew G Knepley       next = link->next;
621aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
622aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
623aa1993deSMatthew G Knepley     }
6240298fd71SBarry Smith     (*dm)->workin = NULL;
625aa1993deSMatthew G Knepley   }
626c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
627c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
628c58f1c22SToby Isaac 
629c58f1c22SToby Isaac     /* destroy the labels */
630c58f1c22SToby Isaac     while (next) {
631c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
632c58f1c22SToby Isaac 
633c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
634c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
635c58f1c22SToby Isaac       next = tmp;
636c58f1c22SToby Isaac     }
637c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
638c58f1c22SToby Isaac   }
639e6f8dbb6SToby Isaac   {
640e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
641e6f8dbb6SToby Isaac     while (next) {
642e6f8dbb6SToby Isaac       DMBoundary b = next;
643e6f8dbb6SToby Isaac 
644e6f8dbb6SToby Isaac       next = b->next;
645e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
646e6f8dbb6SToby Isaac     }
647e6f8dbb6SToby Isaac   }
648b17ce1afSJed Brown 
64952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
65052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
65152536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
65252536dc3SBarry Smith 
6531a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
6541a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
6551a266240SBarry Smith   }
65687e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
65771cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
6584dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6596bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
6606bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
661073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
66288ed4aceSMatthew G Knepley 
66388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
66488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6658b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
666fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
667fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
66888ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
66988ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
6708e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
671af122d2aSMatthew G Knepley 
67288bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
67388bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
67488bdff64SToby Isaac   }
675a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
67688bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
67788bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
67888bdff64SToby Isaac   }
67988bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
6806636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
6816636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
6826636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
6835dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
6846636e97aSMatthew G Knepley 
6852764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
68614f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
687e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
688e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
689732e2eb9SMatthew G Knepley 
6906bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
691435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
692732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
69347c6ae99SBarry Smith   PetscFunctionReturn(0);
69447c6ae99SBarry Smith }
69547c6ae99SBarry Smith 
696d7bf68aeSBarry Smith /*@
697d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
698d7bf68aeSBarry Smith 
699d7bf68aeSBarry Smith     Collective on DM
700d7bf68aeSBarry Smith 
701d7bf68aeSBarry Smith     Input Parameter:
702d7bf68aeSBarry Smith .   dm - the DM object to setup
703d7bf68aeSBarry Smith 
704d7bf68aeSBarry Smith     Level: developer
705d7bf68aeSBarry Smith 
706e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
707d7bf68aeSBarry Smith 
708d7bf68aeSBarry Smith @*/
7097087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
710d7bf68aeSBarry Smith {
711d7bf68aeSBarry Smith   PetscErrorCode ierr;
712d7bf68aeSBarry Smith 
713d7bf68aeSBarry Smith   PetscFunctionBegin;
714171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7158387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
716d7bf68aeSBarry Smith   if (dm->ops->setup) {
717d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
718d7bf68aeSBarry Smith   }
7198387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
720d7bf68aeSBarry Smith   PetscFunctionReturn(0);
721d7bf68aeSBarry Smith }
722d7bf68aeSBarry Smith 
723d7bf68aeSBarry Smith /*@
724d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
725d7bf68aeSBarry Smith 
726d7bf68aeSBarry Smith     Collective on DM
727d7bf68aeSBarry Smith 
728d7bf68aeSBarry Smith     Input Parameter:
729d7bf68aeSBarry Smith .   dm - the DM object to set options for
730d7bf68aeSBarry Smith 
731732e2eb9SMatthew G Knepley     Options Database:
7324164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7334164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7344164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
7354164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
736732e2eb9SMatthew G Knepley 
737d7bf68aeSBarry Smith     Level: developer
738d7bf68aeSBarry Smith 
739e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
740d7bf68aeSBarry Smith 
741d7bf68aeSBarry Smith @*/
7427087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
743d7bf68aeSBarry Smith {
7447781c08eSBarry Smith   char           typeName[256];
745ca266f36SBarry Smith   PetscBool      flg;
746d7bf68aeSBarry Smith   PetscErrorCode ierr;
747d7bf68aeSBarry Smith 
748d7bf68aeSBarry Smith   PetscFunctionBegin;
749171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
750f1fd5e65SToby Isaac   if (dm->prob) {
751f1fd5e65SToby Isaac     ierr = PetscDSSetFromOptions(dm->prob);CHKERRQ(ierr);
752f1fd5e65SToby Isaac   }
753691be533SLawrence Mitchell   if (dm->sf) {
754691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
755691be533SLawrence Mitchell   }
756691be533SLawrence Mitchell   if (dm->defaultSF) {
757691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
758691be533SLawrence Mitchell   }
7593194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
7600298fd71SBarry 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);
761a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
762f9ba7244SBarry Smith   if (flg) {
763f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
764f9ba7244SBarry Smith   }
765a264d7a6SBarry 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);
766073dac72SJed Brown   if (flg) {
767521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
768073dac72SJed Brown   }
7690298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
770f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
771e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
772f9ba7244SBarry Smith   }
773f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
7740633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
77582fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
776d7bf68aeSBarry Smith   PetscFunctionReturn(0);
777d7bf68aeSBarry Smith }
778d7bf68aeSBarry Smith 
779fc9bc008SSatish Balay /*@C
780224748a4SBarry Smith     DMView - Views a DM
78147c6ae99SBarry Smith 
78247c6ae99SBarry Smith     Collective on DM
78347c6ae99SBarry Smith 
78447c6ae99SBarry Smith     Input Parameter:
78547c6ae99SBarry Smith +   dm - the DM object to view
78647c6ae99SBarry Smith -   v - the viewer
78747c6ae99SBarry Smith 
788224748a4SBarry Smith     Level: beginner
78947c6ae99SBarry Smith 
790e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
79147c6ae99SBarry Smith 
79247c6ae99SBarry Smith @*/
7937087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
79447c6ae99SBarry Smith {
79547c6ae99SBarry Smith   PetscErrorCode ierr;
79632c0f0efSBarry Smith   PetscBool      isbinary;
79747c6ae99SBarry Smith 
79847c6ae99SBarry Smith   PetscFunctionBegin;
799171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8003014e516SBarry Smith   if (!v) {
801ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8023014e516SBarry Smith   }
80398c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
80432c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
80532c0f0efSBarry Smith   if (isbinary) {
80655849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
80732c0f0efSBarry Smith     char     type[256];
80832c0f0efSBarry Smith 
80932c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
81032c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
81132c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
81232c0f0efSBarry Smith   }
8130c010503SBarry Smith   if (dm->ops->view) {
8140c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
81547c6ae99SBarry Smith   }
81647c6ae99SBarry Smith   PetscFunctionReturn(0);
81747c6ae99SBarry Smith }
81847c6ae99SBarry Smith 
81947c6ae99SBarry Smith /*@
8208472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
82147c6ae99SBarry Smith 
82247c6ae99SBarry Smith     Collective on DM
82347c6ae99SBarry Smith 
82447c6ae99SBarry Smith     Input Parameter:
82547c6ae99SBarry Smith .   dm - the DM object
82647c6ae99SBarry Smith 
82747c6ae99SBarry Smith     Output Parameter:
82847c6ae99SBarry Smith .   vec - the global vector
82947c6ae99SBarry Smith 
830073dac72SJed Brown     Level: beginner
83147c6ae99SBarry Smith 
832e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
83347c6ae99SBarry Smith 
83447c6ae99SBarry Smith @*/
8357087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
83647c6ae99SBarry Smith {
83747c6ae99SBarry Smith   PetscErrorCode ierr;
83847c6ae99SBarry Smith 
83947c6ae99SBarry Smith   PetscFunctionBegin;
840171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84147c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
84247c6ae99SBarry Smith   PetscFunctionReturn(0);
84347c6ae99SBarry Smith }
84447c6ae99SBarry Smith 
84547c6ae99SBarry Smith /*@
8468472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
84747c6ae99SBarry Smith 
84847c6ae99SBarry Smith     Not Collective
84947c6ae99SBarry Smith 
85047c6ae99SBarry Smith     Input Parameter:
85147c6ae99SBarry Smith .   dm - the DM object
85247c6ae99SBarry Smith 
85347c6ae99SBarry Smith     Output Parameter:
85447c6ae99SBarry Smith .   vec - the local vector
85547c6ae99SBarry Smith 
856073dac72SJed Brown     Level: beginner
85747c6ae99SBarry Smith 
858e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
85947c6ae99SBarry Smith 
86047c6ae99SBarry Smith @*/
8617087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
86247c6ae99SBarry Smith {
86347c6ae99SBarry Smith   PetscErrorCode ierr;
86447c6ae99SBarry Smith 
86547c6ae99SBarry Smith   PetscFunctionBegin;
866171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
86747c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
86847c6ae99SBarry Smith   PetscFunctionReturn(0);
86947c6ae99SBarry Smith }
87047c6ae99SBarry Smith 
8711411c6eeSJed Brown /*@
8721411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8731411c6eeSJed Brown 
8741411c6eeSJed Brown    Collective on DM
8751411c6eeSJed Brown 
8761411c6eeSJed Brown    Input Parameter:
8771411c6eeSJed Brown .  dm - the DM that provides the mapping
8781411c6eeSJed Brown 
8791411c6eeSJed Brown    Output Parameter:
8801411c6eeSJed Brown .  ltog - the mapping
8811411c6eeSJed Brown 
8821411c6eeSJed Brown    Level: intermediate
8831411c6eeSJed Brown 
8841411c6eeSJed Brown    Notes:
8851411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
8861411c6eeSJed Brown    MatSetLocalToGlobalMapping().
8871411c6eeSJed Brown 
888fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
8891411c6eeSJed Brown @*/
8907087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
8911411c6eeSJed Brown {
892bff27382SMatthew G. Knepley   PetscInt       bs = -1, bsLocal, bsMin, bsMax;
8931411c6eeSJed Brown   PetscErrorCode ierr;
8941411c6eeSJed Brown 
8951411c6eeSJed Brown   PetscFunctionBegin;
8961411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8971411c6eeSJed Brown   PetscValidPointer(ltog,2);
8981411c6eeSJed Brown   if (!dm->ltogmap) {
89937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
90037d0c07bSMatthew G Knepley 
90137d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
90237d0c07bSMatthew G Knepley     if (section) {
903a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
90437d0c07bSMatthew G Knepley       PetscInt       *ltog;
90537d0c07bSMatthew G Knepley       PetscInt        pStart, pEnd, size, p, l;
90637d0c07bSMatthew G Knepley 
90737d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
90837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
90937d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
910785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
91137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
912a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
91337d0c07bSMatthew G Knepley 
91437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
91537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
916a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
917a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
91837d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
9191a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
9201a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
9211a7dc684SMatthew G. Knepley         if (dof) {
922b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
923b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
9241a7dc684SMatthew G. Knepley         }
92537d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
926a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
927a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
92837d0c07bSMatthew G Knepley         }
92937d0c07bSMatthew G Knepley       }
930bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
931bff27382SMatthew G. Knepley       bsLocal = bs;
932bff27382SMatthew G. Knepley       ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
933bff27382SMatthew G. Knepley       bsLocal = bs < 0 ? bsMax : bs;
934bff27382SMatthew G. Knepley       ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
935bff27382SMatthew G. Knepley       if (bsMin != bsMax) {bs = 1;}
936bff27382SMatthew G. Knepley       else                {bs = bsMax;}
9377591dbb2SMatthew G. Knepley       bs   = bs < 0 ? 1 : bs;
9387591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
9397591dbb2SMatthew G. Knepley       if (bs > 1) for (l = 0; l < size; ++l) ltog[l] /= bs;
9407591dbb2SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9413bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
94237d0c07bSMatthew G Knepley     } else {
943184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
944184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9451411c6eeSJed Brown     }
94637d0c07bSMatthew G Knepley   }
9471411c6eeSJed Brown   *ltog = dm->ltogmap;
9481411c6eeSJed Brown   PetscFunctionReturn(0);
9491411c6eeSJed Brown }
9501411c6eeSJed Brown 
9511411c6eeSJed Brown /*@
9521411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9531411c6eeSJed Brown 
9541411c6eeSJed Brown    Not Collective
9551411c6eeSJed Brown 
9561411c6eeSJed Brown    Input Parameter:
9571411c6eeSJed Brown .  dm - the DM with block structure
9581411c6eeSJed Brown 
9591411c6eeSJed Brown    Output Parameter:
9601411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
9611411c6eeSJed Brown 
9621411c6eeSJed Brown    Level: intermediate
9631411c6eeSJed Brown 
964fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
9651411c6eeSJed Brown @*/
9667087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
9671411c6eeSJed Brown {
9681411c6eeSJed Brown   PetscFunctionBegin;
9691411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9701411c6eeSJed Brown   PetscValidPointer(bs,2);
9711411c6eeSJed 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");
9721411c6eeSJed Brown   *bs = dm->bs;
9731411c6eeSJed Brown   PetscFunctionReturn(0);
9741411c6eeSJed Brown }
9751411c6eeSJed Brown 
97647c6ae99SBarry Smith /*@
9778472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
97847c6ae99SBarry Smith 
97947c6ae99SBarry Smith     Collective on DM
98047c6ae99SBarry Smith 
98147c6ae99SBarry Smith     Input Parameter:
98247c6ae99SBarry Smith +   dm1 - the DM object
98347c6ae99SBarry Smith -   dm2 - the second, finer DM object
98447c6ae99SBarry Smith 
98547c6ae99SBarry Smith     Output Parameter:
98647c6ae99SBarry Smith +  mat - the interpolation
98747c6ae99SBarry Smith -  vec - the scaling (optional)
98847c6ae99SBarry Smith 
98947c6ae99SBarry Smith     Level: developer
99047c6ae99SBarry Smith 
99185afcc9aSBarry 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
99285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
993d52bd9f3SBarry Smith 
9941f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
995d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
99685afcc9aSBarry Smith 
99785afcc9aSBarry Smith 
9983ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith @*/
1001e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
100247c6ae99SBarry Smith {
100347c6ae99SBarry Smith   PetscErrorCode ierr;
100447c6ae99SBarry Smith 
100547c6ae99SBarry Smith   PetscFunctionBegin;
1006171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1007171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1008cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
100925296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1010cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
101147c6ae99SBarry Smith   PetscFunctionReturn(0);
101247c6ae99SBarry Smith }
101347c6ae99SBarry Smith 
10143ad4599aSBarry Smith /*@
10153ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
10163ad4599aSBarry Smith 
10173ad4599aSBarry Smith     Collective on DM
10183ad4599aSBarry Smith 
10193ad4599aSBarry Smith     Input Parameter:
10203ad4599aSBarry Smith +   dm1 - the DM object
10213ad4599aSBarry Smith -   dm2 - the second, finer DM object
10223ad4599aSBarry Smith 
10233ad4599aSBarry Smith     Output Parameter:
10243ad4599aSBarry Smith .  mat - the restriction
10253ad4599aSBarry Smith 
10263ad4599aSBarry Smith 
10273ad4599aSBarry Smith     Level: developer
10283ad4599aSBarry Smith 
10293ad4599aSBarry 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
10303ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
10313ad4599aSBarry Smith 
10323ad4599aSBarry Smith 
10333ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
10343ad4599aSBarry Smith 
10353ad4599aSBarry Smith @*/
10363ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
10373ad4599aSBarry Smith {
10383ad4599aSBarry Smith   PetscErrorCode ierr;
10393ad4599aSBarry Smith 
10403ad4599aSBarry Smith   PetscFunctionBegin;
10413ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
10423ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10433ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10447294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
10453ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
10463ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10473ad4599aSBarry Smith   PetscFunctionReturn(0);
10483ad4599aSBarry Smith }
10493ad4599aSBarry Smith 
105047c6ae99SBarry Smith /*@
10518472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
105247c6ae99SBarry Smith 
105347c6ae99SBarry Smith     Collective on DM
105447c6ae99SBarry Smith 
105547c6ae99SBarry Smith     Input Parameter:
105647c6ae99SBarry Smith +   dm1 - the DM object
105747c6ae99SBarry Smith -   dm2 - the second, finer DM object
105847c6ae99SBarry Smith 
105947c6ae99SBarry Smith     Output Parameter:
10606dbf9973SLawrence Mitchell .   mat - the injection
106147c6ae99SBarry Smith 
106247c6ae99SBarry Smith     Level: developer
106347c6ae99SBarry Smith 
106485afcc9aSBarry 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
106585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
106685afcc9aSBarry Smith 
1067e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
106847c6ae99SBarry Smith 
106947c6ae99SBarry Smith @*/
10706dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
107147c6ae99SBarry Smith {
107247c6ae99SBarry Smith   PetscErrorCode ierr;
107347c6ae99SBarry Smith 
107447c6ae99SBarry Smith   PetscFunctionBegin;
1075171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1076171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10770c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
10786dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
107947c6ae99SBarry Smith   PetscFunctionReturn(0);
108047c6ae99SBarry Smith }
108147c6ae99SBarry Smith 
1082b412c318SBarry Smith /*@
1083b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
108447c6ae99SBarry Smith 
108547c6ae99SBarry Smith     Collective on DM
108647c6ae99SBarry Smith 
108747c6ae99SBarry Smith     Input Parameter:
108847c6ae99SBarry Smith +   dm - the DM object
10895bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
109047c6ae99SBarry Smith 
109147c6ae99SBarry Smith     Output Parameter:
109247c6ae99SBarry Smith .   coloring - the coloring
109347c6ae99SBarry Smith 
109447c6ae99SBarry Smith     Level: developer
109547c6ae99SBarry Smith 
1096b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
109747c6ae99SBarry Smith 
1098aab9d709SJed Brown @*/
1099b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
110047c6ae99SBarry Smith {
110147c6ae99SBarry Smith   PetscErrorCode ierr;
110247c6ae99SBarry Smith 
110347c6ae99SBarry Smith   PetscFunctionBegin;
1104171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1105ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1106b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
110747c6ae99SBarry Smith   PetscFunctionReturn(0);
110847c6ae99SBarry Smith }
110947c6ae99SBarry Smith 
1110b412c318SBarry Smith /*@
11118472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
111247c6ae99SBarry Smith 
111347c6ae99SBarry Smith     Collective on DM
111447c6ae99SBarry Smith 
111547c6ae99SBarry Smith     Input Parameter:
1116b412c318SBarry Smith .   dm - the DM object
111747c6ae99SBarry Smith 
111847c6ae99SBarry Smith     Output Parameter:
111947c6ae99SBarry Smith .   mat - the empty Jacobian
112047c6ae99SBarry Smith 
1121073dac72SJed Brown     Level: beginner
112247c6ae99SBarry Smith 
112394013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
112494013140SBarry Smith        do not need to do it yourself.
112594013140SBarry Smith 
112694013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
11277889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
112894013140SBarry Smith 
112994013140SBarry 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
113094013140SBarry Smith        internally by PETSc.
113194013140SBarry Smith 
113294013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1133aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
113494013140SBarry Smith 
1135b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
113647c6ae99SBarry Smith 
1137aab9d709SJed Brown @*/
1138b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
113947c6ae99SBarry Smith {
114047c6ae99SBarry Smith   PetscErrorCode ierr;
114147c6ae99SBarry Smith 
114247c6ae99SBarry Smith   PetscFunctionBegin;
1143171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1144607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1145c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1146c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1147b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
114847c6ae99SBarry Smith   PetscFunctionReturn(0);
114947c6ae99SBarry Smith }
115047c6ae99SBarry Smith 
1151732e2eb9SMatthew G Knepley /*@
1152950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1153732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1154732e2eb9SMatthew G Knepley 
11558472ad0fSDave May   Logically Collective on DM
1156732e2eb9SMatthew G Knepley 
1157732e2eb9SMatthew G Knepley   Input Parameter:
1158732e2eb9SMatthew G Knepley + dm - the DM
1159732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1160732e2eb9SMatthew G Knepley 
1161732e2eb9SMatthew G Knepley   Level: developer
1162950540a4SJed Brown .seealso DMCreateMatrix()
1163732e2eb9SMatthew G Knepley @*/
1164732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1165732e2eb9SMatthew G Knepley {
1166732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1167732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1168732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1169732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1170732e2eb9SMatthew G Knepley }
1171732e2eb9SMatthew G Knepley 
1172a89ea682SMatthew G Knepley /*@C
1173aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1174a89ea682SMatthew G Knepley 
1175a89ea682SMatthew G Knepley   Not Collective
1176a89ea682SMatthew G Knepley 
1177a89ea682SMatthew G Knepley   Input Parameters:
1178a89ea682SMatthew G Knepley + dm - the DM object
1179aa1993deSMatthew G Knepley . count - The minium size
1180aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1181a89ea682SMatthew G Knepley 
1182a89ea682SMatthew G Knepley   Output Parameter:
1183a89ea682SMatthew G Knepley . array - the work array
1184a89ea682SMatthew G Knepley 
1185a89ea682SMatthew G Knepley   Level: developer
1186a89ea682SMatthew G Knepley 
1187a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1188a89ea682SMatthew G Knepley @*/
1189aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1190a89ea682SMatthew G Knepley {
1191a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1192aa1993deSMatthew G Knepley   DMWorkLink     link;
1193854ce69bSBarry Smith   size_t         dsize;
1194a89ea682SMatthew G Knepley 
1195a89ea682SMatthew G Knepley   PetscFunctionBegin;
1196a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1197aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1198aa1993deSMatthew G Knepley   if (dm->workin) {
1199aa1993deSMatthew G Knepley     link       = dm->workin;
1200aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1201aa1993deSMatthew G Knepley   } else {
1202b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1203a89ea682SMatthew G Knepley   }
1204854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1205854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1206aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1207854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1208854ce69bSBarry Smith     link->bytes = dsize*count;
1209aa1993deSMatthew G Knepley   }
1210aa1993deSMatthew G Knepley   link->next   = dm->workout;
1211aa1993deSMatthew G Knepley   dm->workout  = link;
1212aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1213a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1214a89ea682SMatthew G Knepley }
1215a89ea682SMatthew G Knepley 
1216aa1993deSMatthew G Knepley /*@C
1217aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1218aa1993deSMatthew G Knepley 
1219aa1993deSMatthew G Knepley   Not Collective
1220aa1993deSMatthew G Knepley 
1221aa1993deSMatthew G Knepley   Input Parameters:
1222aa1993deSMatthew G Knepley + dm - the DM object
1223aa1993deSMatthew G Knepley . count - The minium size
1224aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1225aa1993deSMatthew G Knepley 
1226aa1993deSMatthew G Knepley   Output Parameter:
1227aa1993deSMatthew G Knepley . array - the work array
1228aa1993deSMatthew G Knepley 
1229aa1993deSMatthew G Knepley   Level: developer
1230aa1993deSMatthew G Knepley 
1231aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1232aa1993deSMatthew G Knepley @*/
1233aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1234aa1993deSMatthew G Knepley {
1235aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1236aa1993deSMatthew G Knepley 
1237aa1993deSMatthew G Knepley   PetscFunctionBegin;
1238aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1239aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1240aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1241aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1242aa1993deSMatthew G Knepley       *p           = link->next;
1243aa1993deSMatthew G Knepley       link->next   = dm->workin;
1244aa1993deSMatthew G Knepley       dm->workin   = link;
12450298fd71SBarry Smith       *(void**)mem = NULL;
1246aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1247aa1993deSMatthew G Knepley     }
1248aa1993deSMatthew G Knepley   }
1249aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1250aa1993deSMatthew G Knepley }
1251e7c4fc90SDmitry Karpeev 
1252435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1253435a35e8SMatthew G Knepley {
1254435a35e8SMatthew G Knepley   PetscFunctionBegin;
1255435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
125682f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1257435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1258435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1259435a35e8SMatthew G Knepley }
1260435a35e8SMatthew G Knepley 
12614f3b5142SJed Brown /*@C
12624d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
12634d343eeaSMatthew G Knepley 
12644d343eeaSMatthew G Knepley   Not collective
12654d343eeaSMatthew G Knepley 
12664d343eeaSMatthew G Knepley   Input Parameter:
12674d343eeaSMatthew G Knepley . dm - the DM object
12684d343eeaSMatthew G Knepley 
12694d343eeaSMatthew G Knepley   Output Parameters:
12700298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
12710298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
12720298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
12734d343eeaSMatthew G Knepley 
12744d343eeaSMatthew G Knepley   Level: intermediate
12754d343eeaSMatthew G Knepley 
127621c9b008SJed Brown   Notes:
127721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
127821c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
127921c9b008SJed Brown   PetscFree().
128021c9b008SJed Brown 
12814d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
12824d343eeaSMatthew G Knepley @*/
128337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
12844d343eeaSMatthew G Knepley {
128537d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
12864d343eeaSMatthew G Knepley   PetscErrorCode ierr;
12874d343eeaSMatthew G Knepley 
12884d343eeaSMatthew G Knepley   PetscFunctionBegin;
12894d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
129069ca1f37SDmitry Karpeev   if (numFields) {
129169ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
129269ca1f37SDmitry Karpeev     *numFields = 0;
129369ca1f37SDmitry Karpeev   }
129437d0c07bSMatthew G Knepley   if (fieldNames) {
129537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
12960298fd71SBarry Smith     *fieldNames = NULL;
129769ca1f37SDmitry Karpeev   }
129869ca1f37SDmitry Karpeev   if (fields) {
129969ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
13000298fd71SBarry Smith     *fields = NULL;
130169ca1f37SDmitry Karpeev   }
130237d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
130337d0c07bSMatthew G Knepley   if (section) {
130437d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
130537d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
130637d0c07bSMatthew G Knepley 
130737d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
130837d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1309dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
131037d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
131137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
131237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
131337d0c07bSMatthew G Knepley     }
131437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
131537d0c07bSMatthew G Knepley       PetscInt gdof;
131637d0c07bSMatthew G Knepley 
131737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
131837d0c07bSMatthew G Knepley       if (gdof > 0) {
131937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
132037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
132137d0c07bSMatthew G Knepley 
132237d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
132337d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
132437d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
132537d0c07bSMatthew G Knepley         }
132637d0c07bSMatthew G Knepley       }
132737d0c07bSMatthew G Knepley     }
132837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1329785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
133037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
133137d0c07bSMatthew G Knepley     }
133237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
133337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
133437d0c07bSMatthew G Knepley 
133537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
133637d0c07bSMatthew G Knepley       if (gdof > 0) {
133737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
133837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
133937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
134037d0c07bSMatthew G Knepley 
134137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
134237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
134337d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
134437d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
134537d0c07bSMatthew G Knepley           }
134637d0c07bSMatthew G Knepley         }
134737d0c07bSMatthew G Knepley       }
134837d0c07bSMatthew G Knepley     }
13498865f1eaSKarl Rupp     if (numFields) *numFields = nF;
135037d0c07bSMatthew G Knepley     if (fieldNames) {
1351785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
135237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
135337d0c07bSMatthew G Knepley         const char *fieldName;
135437d0c07bSMatthew G Knepley 
135537d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
135637d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
135737d0c07bSMatthew G Knepley       }
135837d0c07bSMatthew G Knepley     }
135937d0c07bSMatthew G Knepley     if (fields) {
1360785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
136137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
136282f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
136337d0c07bSMatthew G Knepley       }
136437d0c07bSMatthew G Knepley     }
136537d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
13668865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
13678865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
136869ca1f37SDmitry Karpeev   }
13694d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
13704d343eeaSMatthew G Knepley }
13714d343eeaSMatthew G Knepley 
137216621825SDmitry Karpeev 
137316621825SDmitry Karpeev /*@C
137416621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
137516621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
137616621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1377e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1378e7c4fc90SDmitry Karpeev 
1379e7c4fc90SDmitry Karpeev   Not collective
1380e7c4fc90SDmitry Karpeev 
1381e7c4fc90SDmitry Karpeev   Input Parameter:
1382e7c4fc90SDmitry Karpeev . dm - the DM object
1383e7c4fc90SDmitry Karpeev 
1384e7c4fc90SDmitry Karpeev   Output Parameters:
13850298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
13860298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
13870298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
13880298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1389e7c4fc90SDmitry Karpeev 
1390e7c4fc90SDmitry Karpeev   Level: intermediate
1391e7c4fc90SDmitry Karpeev 
1392e7c4fc90SDmitry Karpeev   Notes:
1393e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1394e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1395e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1396e7c4fc90SDmitry Karpeev 
1397e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1398e7c4fc90SDmitry Karpeev @*/
139916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1400e7c4fc90SDmitry Karpeev {
1401e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1402e7c4fc90SDmitry Karpeev 
1403e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1404e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14058865f1eaSKarl Rupp   if (len) {
14068865f1eaSKarl Rupp     PetscValidPointer(len,2);
14078865f1eaSKarl Rupp     *len = 0;
14088865f1eaSKarl Rupp   }
14098865f1eaSKarl Rupp   if (namelist) {
14108865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
14118865f1eaSKarl Rupp     *namelist = 0;
14128865f1eaSKarl Rupp   }
14138865f1eaSKarl Rupp   if (islist) {
14148865f1eaSKarl Rupp     PetscValidPointer(islist,4);
14158865f1eaSKarl Rupp     *islist = 0;
14168865f1eaSKarl Rupp   }
14178865f1eaSKarl Rupp   if (dmlist) {
14188865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
14198865f1eaSKarl Rupp     *dmlist = 0;
14208865f1eaSKarl Rupp   }
1421f3f0edfdSDmitry Karpeev   /*
1422f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1423f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1424f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1425f3f0edfdSDmitry Karpeev    */
1426ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
142716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1428435a35e8SMatthew G Knepley     PetscSection section;
1429435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1430435a35e8SMatthew G Knepley 
1431435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1432435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1433435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1434f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
143503dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
143603dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
143703dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1438435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1439435a35e8SMatthew G Knepley         const char *fieldName;
1440435a35e8SMatthew G Knepley 
144103dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
144203dc3394SMatthew G. Knepley         if (namelist) {
1443435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1444435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1445435a35e8SMatthew G Knepley         }
144603dc3394SMatthew G. Knepley       }
1447435a35e8SMatthew G Knepley     } else {
144869ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1449e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
14500298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1451e7c4fc90SDmitry Karpeev     }
14528865f1eaSKarl Rupp   } else {
145316621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
145416621825SDmitry Karpeev   }
145516621825SDmitry Karpeev   PetscFunctionReturn(0);
145616621825SDmitry Karpeev }
145716621825SDmitry Karpeev 
1458564cec59SMatthew G. Knepley /*@
1459435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1460435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1461435a35e8SMatthew G Knepley 
1462435a35e8SMatthew G Knepley   Not collective
1463435a35e8SMatthew G Knepley 
1464435a35e8SMatthew G Knepley   Input Parameters:
1465435a35e8SMatthew G Knepley + dm - the DM object
1466435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
14670298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1468435a35e8SMatthew G Knepley 
1469435a35e8SMatthew G Knepley   Output Parameters:
1470435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1471435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1472435a35e8SMatthew G Knepley 
1473435a35e8SMatthew G Knepley   Level: intermediate
1474435a35e8SMatthew G Knepley 
1475435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1476435a35e8SMatthew G Knepley @*/
1477435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1478435a35e8SMatthew G Knepley {
1479435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1480435a35e8SMatthew G Knepley 
1481435a35e8SMatthew G Knepley   PetscFunctionBegin;
1482435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1483435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
14848865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
14858865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1486435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1487435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
148882f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1489435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1490435a35e8SMatthew G Knepley }
1491435a35e8SMatthew G Knepley 
149216621825SDmitry Karpeev 
149316621825SDmitry Karpeev /*@C
14948d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
14958d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
14968d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
14978d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
14988d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
149916621825SDmitry Karpeev 
150016621825SDmitry Karpeev   Not collective
150116621825SDmitry Karpeev 
150216621825SDmitry Karpeev   Input Parameter:
150316621825SDmitry Karpeev . dm - the DM object
150416621825SDmitry Karpeev 
150516621825SDmitry Karpeev   Output Parameters:
15060298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
15070298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
15080298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
15090298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
15100298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
151116621825SDmitry Karpeev 
151216621825SDmitry Karpeev   Level: intermediate
151316621825SDmitry Karpeev 
151416621825SDmitry Karpeev   Notes:
151516621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
151616621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
151716621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
151816621825SDmitry Karpeev 
15198d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
152016621825SDmitry Karpeev @*/
15218d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
152216621825SDmitry Karpeev {
152316621825SDmitry Karpeev   PetscErrorCode      ierr;
1524be081cd6SPeter Brune   DMSubDomainHookLink link;
1525be081cd6SPeter Brune   PetscInt            i,l;
152616621825SDmitry Karpeev 
152716621825SDmitry Karpeev   PetscFunctionBegin;
152816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
152914a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
15300298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
15310298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
15320298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
15330298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1534f3f0edfdSDmitry Karpeev   /*
1535f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1536f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1537f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1538f3f0edfdSDmitry Karpeev    */
1539ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
154016621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1541be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
154214a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1543f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1544be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1545be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1546be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1547be081cd6SPeter Brune         }
1548648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1549e7c4fc90SDmitry Karpeev       }
155014a18fd3SPeter Brune     }
155114a18fd3SPeter Brune     if (len) *len = l;
155214a18fd3SPeter Brune   }
1553e30e807fSPeter Brune   PetscFunctionReturn(0);
1554e30e807fSPeter Brune }
1555e30e807fSPeter Brune 
1556e30e807fSPeter Brune 
1557e30e807fSPeter Brune /*@C
1558e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1559e30e807fSPeter Brune 
1560e30e807fSPeter Brune   Not collective
1561e30e807fSPeter Brune 
1562e30e807fSPeter Brune   Input Parameters:
1563e30e807fSPeter Brune + dm - the DM object
1564e30e807fSPeter Brune . n  - the number of subdomain scatters
1565e30e807fSPeter Brune - subdms - the local subdomains
1566e30e807fSPeter Brune 
1567e30e807fSPeter Brune   Output Parameters:
1568e30e807fSPeter Brune + n     - the number of scatters returned
1569e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1570e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1571e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1572e30e807fSPeter Brune 
1573e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1574e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1575e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1576e30e807fSPeter Brune   solution and residual data.
1577e30e807fSPeter Brune 
1578e30e807fSPeter Brune   Level: developer
1579e30e807fSPeter Brune 
1580e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1581e30e807fSPeter Brune @*/
1582e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1583e30e807fSPeter Brune {
1584e30e807fSPeter Brune   PetscErrorCode ierr;
1585e30e807fSPeter Brune 
1586e30e807fSPeter Brune   PetscFunctionBegin;
1587e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1588e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1589e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1590e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
15916668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1592e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1593e7c4fc90SDmitry Karpeev }
1594e7c4fc90SDmitry Karpeev 
159547c6ae99SBarry Smith /*@
159647c6ae99SBarry Smith   DMRefine - Refines a DM object
159747c6ae99SBarry Smith 
159847c6ae99SBarry Smith   Collective on DM
159947c6ae99SBarry Smith 
160047c6ae99SBarry Smith   Input Parameter:
160147c6ae99SBarry Smith + dm   - the DM object
160291d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
160347c6ae99SBarry Smith 
160447c6ae99SBarry Smith   Output Parameter:
16050298fd71SBarry Smith . dmf - the refined DM, or NULL
1606ae0a1c52SMatthew G Knepley 
16070298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
160847c6ae99SBarry Smith 
160947c6ae99SBarry Smith   Level: developer
161047c6ae99SBarry Smith 
1611e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
161247c6ae99SBarry Smith @*/
16137087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
161447c6ae99SBarry Smith {
161547c6ae99SBarry Smith   PetscErrorCode   ierr;
1616c833c3b5SJed Brown   DMRefineHookLink link;
161747c6ae99SBarry Smith 
161847c6ae99SBarry Smith   PetscFunctionBegin;
1619732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16201ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1621fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
162247c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
16234057135bSMatthew G Knepley   if (*dmf) {
162443842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
16258865f1eaSKarl Rupp 
16268cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
16278865f1eaSKarl Rupp 
1628644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
16290598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1630656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
16318865f1eaSKarl Rupp 
1632e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1633c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
16348865f1eaSKarl Rupp       if (link->refinehook) {
16358865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
16368865f1eaSKarl Rupp       }
1637c833c3b5SJed Brown     }
1638c833c3b5SJed Brown   }
16391ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1640c833c3b5SJed Brown   PetscFunctionReturn(0);
1641c833c3b5SJed Brown }
1642c833c3b5SJed Brown 
1643bb9467b5SJed Brown /*@C
1644c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1645c833c3b5SJed Brown 
1646c833c3b5SJed Brown    Logically Collective
1647c833c3b5SJed Brown 
1648c833c3b5SJed Brown    Input Arguments:
1649c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1650c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1651c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
16520298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1653c833c3b5SJed Brown 
1654c833c3b5SJed Brown    Calling sequence of refinehook:
1655c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1656c833c3b5SJed Brown 
1657c833c3b5SJed Brown +  coarse - coarse level DM
1658c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1659c833c3b5SJed Brown -  ctx - optional user-defined function context
1660c833c3b5SJed Brown 
1661c833c3b5SJed Brown    Calling sequence for interphook:
1662c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1663c833c3b5SJed Brown 
1664c833c3b5SJed Brown +  coarse - coarse level DM
1665c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1666c833c3b5SJed Brown .  fine - fine level DM to update
1667c833c3b5SJed Brown -  ctx - optional user-defined function context
1668c833c3b5SJed Brown 
1669c833c3b5SJed Brown    Level: advanced
1670c833c3b5SJed Brown 
1671c833c3b5SJed Brown    Notes:
1672c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1673c833c3b5SJed Brown 
1674c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1675c833c3b5SJed Brown 
1676bb9467b5SJed Brown    This function is currently not available from Fortran.
1677bb9467b5SJed Brown 
1678c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1679c833c3b5SJed Brown @*/
1680c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1681c833c3b5SJed Brown {
1682c833c3b5SJed Brown   PetscErrorCode   ierr;
1683c833c3b5SJed Brown   DMRefineHookLink link,*p;
1684c833c3b5SJed Brown 
1685c833c3b5SJed Brown   PetscFunctionBegin;
1686c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1687c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
168895dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1689c833c3b5SJed Brown   link->refinehook = refinehook;
1690c833c3b5SJed Brown   link->interphook = interphook;
1691c833c3b5SJed Brown   link->ctx        = ctx;
16920298fd71SBarry Smith   link->next       = NULL;
1693c833c3b5SJed Brown   *p               = link;
1694c833c3b5SJed Brown   PetscFunctionReturn(0);
1695c833c3b5SJed Brown }
1696c833c3b5SJed Brown 
1697c833c3b5SJed Brown /*@
1698c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1699c833c3b5SJed Brown 
1700c833c3b5SJed Brown    Collective if any hooks are
1701c833c3b5SJed Brown 
1702c833c3b5SJed Brown    Input Arguments:
1703c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1704c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1705c833c3b5SJed Brown -  fine - finer DM to update
1706c833c3b5SJed Brown 
1707c833c3b5SJed Brown    Level: developer
1708c833c3b5SJed Brown 
1709c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1710c833c3b5SJed Brown @*/
1711c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1712c833c3b5SJed Brown {
1713c833c3b5SJed Brown   PetscErrorCode   ierr;
1714c833c3b5SJed Brown   DMRefineHookLink link;
1715c833c3b5SJed Brown 
1716c833c3b5SJed Brown   PetscFunctionBegin;
1717c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
17188865f1eaSKarl Rupp     if (link->interphook) {
17198865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
17208865f1eaSKarl Rupp     }
17214057135bSMatthew G Knepley   }
172247c6ae99SBarry Smith   PetscFunctionReturn(0);
172347c6ae99SBarry Smith }
172447c6ae99SBarry Smith 
1725eb3f98d2SBarry Smith /*@
1726eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1727eb3f98d2SBarry Smith 
1728eb3f98d2SBarry Smith     Not Collective
1729eb3f98d2SBarry Smith 
1730eb3f98d2SBarry Smith     Input Parameter:
1731eb3f98d2SBarry Smith .   dm - the DM object
1732eb3f98d2SBarry Smith 
1733eb3f98d2SBarry Smith     Output Parameter:
1734eb3f98d2SBarry Smith .   level - number of refinements
1735eb3f98d2SBarry Smith 
1736eb3f98d2SBarry Smith     Level: developer
1737eb3f98d2SBarry Smith 
17386a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1739eb3f98d2SBarry Smith 
1740eb3f98d2SBarry Smith @*/
1741eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1742eb3f98d2SBarry Smith {
1743eb3f98d2SBarry Smith   PetscFunctionBegin;
1744eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1745eb3f98d2SBarry Smith   *level = dm->levelup;
1746eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1747eb3f98d2SBarry Smith }
1748eb3f98d2SBarry Smith 
1749fef3a512SBarry Smith /*@
1750fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
1751fef3a512SBarry Smith 
1752fef3a512SBarry Smith     Not Collective
1753fef3a512SBarry Smith 
1754fef3a512SBarry Smith     Input Parameter:
1755fef3a512SBarry Smith +   dm - the DM object
1756fef3a512SBarry Smith -   level - number of refinements
1757fef3a512SBarry Smith 
1758fef3a512SBarry Smith     Level: advanced
1759fef3a512SBarry Smith 
1760fef3a512SBarry Smith     Notes: This value is used by PCMG to determine how many multigrid levels to use
1761fef3a512SBarry Smith 
1762fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1763fef3a512SBarry Smith 
1764fef3a512SBarry Smith @*/
1765fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
1766fef3a512SBarry Smith {
1767fef3a512SBarry Smith   PetscFunctionBegin;
1768fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1769fef3a512SBarry Smith   dm->levelup = level;
1770fef3a512SBarry Smith   PetscFunctionReturn(0);
1771fef3a512SBarry Smith }
1772fef3a512SBarry Smith 
1773bb9467b5SJed Brown /*@C
1774baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1775baf369e7SPeter Brune 
1776baf369e7SPeter Brune    Logically Collective
1777baf369e7SPeter Brune 
1778baf369e7SPeter Brune    Input Arguments:
1779baf369e7SPeter Brune +  dm - the DM
1780baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1781baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
17820298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1783baf369e7SPeter Brune 
1784baf369e7SPeter Brune    Calling sequence for beginhook:
1785baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1786baf369e7SPeter Brune 
1787baf369e7SPeter Brune +  dm - global DM
1788baf369e7SPeter Brune .  g - global vector
1789baf369e7SPeter Brune .  mode - mode
1790baf369e7SPeter Brune .  l - local vector
1791baf369e7SPeter Brune -  ctx - optional user-defined function context
1792baf369e7SPeter Brune 
1793baf369e7SPeter Brune 
1794baf369e7SPeter Brune    Calling sequence for endhook:
1795ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1796baf369e7SPeter Brune 
1797baf369e7SPeter Brune +  global - global DM
1798baf369e7SPeter Brune -  ctx - optional user-defined function context
1799baf369e7SPeter Brune 
1800baf369e7SPeter Brune    Level: advanced
1801baf369e7SPeter Brune 
1802baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1803baf369e7SPeter Brune @*/
1804baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1805baf369e7SPeter Brune {
1806baf369e7SPeter Brune   PetscErrorCode          ierr;
1807baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1808baf369e7SPeter Brune 
1809baf369e7SPeter Brune   PetscFunctionBegin;
1810baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1811baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
181295dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
1813baf369e7SPeter Brune   link->beginhook = beginhook;
1814baf369e7SPeter Brune   link->endhook   = endhook;
1815baf369e7SPeter Brune   link->ctx       = ctx;
18160298fd71SBarry Smith   link->next      = NULL;
1817baf369e7SPeter Brune   *p              = link;
1818baf369e7SPeter Brune   PetscFunctionReturn(0);
1819baf369e7SPeter Brune }
1820baf369e7SPeter Brune 
18214c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
18224c274da1SToby Isaac {
18234c274da1SToby Isaac   Mat cMat;
18244c274da1SToby Isaac   Vec cVec;
18254c274da1SToby Isaac   PetscSection section, cSec;
18264c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
18274c274da1SToby Isaac   PetscErrorCode ierr;
18284c274da1SToby Isaac 
18294c274da1SToby Isaac   PetscFunctionBegin;
18304c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18314c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
18324c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
18335db9a05bSToby Isaac     PetscInt nRows;
18345db9a05bSToby Isaac 
18355db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
18365db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
18374c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
18387711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
18394c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
18404c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
18414c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
18424c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
18434c274da1SToby Isaac       if (dof) {
18444c274da1SToby Isaac         PetscScalar *vals;
18454c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
18464c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
18474c274da1SToby Isaac       }
18484c274da1SToby Isaac     }
18494c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
18504c274da1SToby Isaac   }
18514c274da1SToby Isaac   PetscFunctionReturn(0);
18524c274da1SToby Isaac }
18534c274da1SToby Isaac 
185447c6ae99SBarry Smith /*@
185547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
185647c6ae99SBarry Smith 
185747c6ae99SBarry Smith     Neighbor-wise Collective on DM
185847c6ae99SBarry Smith 
185947c6ae99SBarry Smith     Input Parameters:
186047c6ae99SBarry Smith +   dm - the DM object
186147c6ae99SBarry Smith .   g - the global vector
186247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
186347c6ae99SBarry Smith -   l - the local vector
186447c6ae99SBarry Smith 
186547c6ae99SBarry Smith 
186647c6ae99SBarry Smith     Level: beginner
186747c6ae99SBarry Smith 
1868e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
186947c6ae99SBarry Smith 
187047c6ae99SBarry Smith @*/
18717087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
187247c6ae99SBarry Smith {
18737128ae9fSMatthew G Knepley   PetscSF                 sf;
187447c6ae99SBarry Smith   PetscErrorCode          ierr;
1875baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
187647c6ae99SBarry Smith 
187747c6ae99SBarry Smith   PetscFunctionBegin;
1878171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1879baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
18808865f1eaSKarl Rupp     if (link->beginhook) {
18818865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
18828865f1eaSKarl Rupp     }
1883baf369e7SPeter Brune   }
18847128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18857128ae9fSMatthew G Knepley   if (sf) {
1886ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1887ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
18887128ae9fSMatthew G Knepley 
188982f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18907128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1891ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
18927128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
18937128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1894ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
18957128ae9fSMatthew G Knepley   } else {
1896843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18977128ae9fSMatthew G Knepley   }
189847c6ae99SBarry Smith   PetscFunctionReturn(0);
189947c6ae99SBarry Smith }
190047c6ae99SBarry Smith 
190147c6ae99SBarry Smith /*@
190247c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
190347c6ae99SBarry Smith 
190447c6ae99SBarry Smith     Neighbor-wise Collective on DM
190547c6ae99SBarry Smith 
190647c6ae99SBarry Smith     Input Parameters:
190747c6ae99SBarry Smith +   dm - the DM object
190847c6ae99SBarry Smith .   g - the global vector
190947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
191047c6ae99SBarry Smith -   l - the local vector
191147c6ae99SBarry Smith 
191247c6ae99SBarry Smith 
191347c6ae99SBarry Smith     Level: beginner
191447c6ae99SBarry Smith 
1915e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
191647c6ae99SBarry Smith 
191747c6ae99SBarry Smith @*/
19187087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
191947c6ae99SBarry Smith {
19207128ae9fSMatthew G Knepley   PetscSF                 sf;
192147c6ae99SBarry Smith   PetscErrorCode          ierr;
1922ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1923ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1924baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
192547c6ae99SBarry Smith 
192647c6ae99SBarry Smith   PetscFunctionBegin;
1927171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19287128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
19297128ae9fSMatthew G Knepley   if (sf) {
193082f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19317128ae9fSMatthew G Knepley 
19327128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1933ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
19347128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
19357128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1936ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
19377128ae9fSMatthew G Knepley   } else {
1938843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
19397128ae9fSMatthew G Knepley   }
19404c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1941baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1942baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1943baf369e7SPeter Brune   }
194447c6ae99SBarry Smith   PetscFunctionReturn(0);
194547c6ae99SBarry Smith }
194647c6ae99SBarry Smith 
1947d4d07f1eSToby Isaac /*@C
1948d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1949d4d07f1eSToby Isaac 
1950d4d07f1eSToby Isaac    Logically Collective
1951d4d07f1eSToby Isaac 
1952d4d07f1eSToby Isaac    Input Arguments:
1953d4d07f1eSToby Isaac +  dm - the DM
1954d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1955d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1956d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1957d4d07f1eSToby Isaac 
1958d4d07f1eSToby Isaac    Calling sequence for beginhook:
1959d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1960d4d07f1eSToby Isaac 
1961d4d07f1eSToby Isaac +  dm - global DM
1962d4d07f1eSToby Isaac .  l - local vector
1963d4d07f1eSToby Isaac .  mode - mode
1964d4d07f1eSToby Isaac .  g - global vector
1965d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1966d4d07f1eSToby Isaac 
1967d4d07f1eSToby Isaac 
1968d4d07f1eSToby Isaac    Calling sequence for endhook:
1969d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1970d4d07f1eSToby Isaac 
1971d4d07f1eSToby Isaac +  global - global DM
1972d4d07f1eSToby Isaac .  l - local vector
1973d4d07f1eSToby Isaac .  mode - mode
1974d4d07f1eSToby Isaac .  g - global vector
1975d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1976d4d07f1eSToby Isaac 
1977d4d07f1eSToby Isaac    Level: advanced
1978d4d07f1eSToby Isaac 
1979d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1980d4d07f1eSToby Isaac @*/
1981d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1982d4d07f1eSToby Isaac {
1983d4d07f1eSToby Isaac   PetscErrorCode          ierr;
1984d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
1985d4d07f1eSToby Isaac 
1986d4d07f1eSToby Isaac   PetscFunctionBegin;
1987d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1988d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
198995dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
1990d4d07f1eSToby Isaac   link->beginhook = beginhook;
1991d4d07f1eSToby Isaac   link->endhook   = endhook;
1992d4d07f1eSToby Isaac   link->ctx       = ctx;
1993d4d07f1eSToby Isaac   link->next      = NULL;
1994d4d07f1eSToby Isaac   *p              = link;
1995d4d07f1eSToby Isaac   PetscFunctionReturn(0);
1996d4d07f1eSToby Isaac }
1997d4d07f1eSToby Isaac 
19984c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
19994c274da1SToby Isaac {
20004c274da1SToby Isaac   Mat cMat;
20014c274da1SToby Isaac   Vec cVec;
20024c274da1SToby Isaac   PetscSection section, cSec;
20034c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
20044c274da1SToby Isaac   PetscErrorCode ierr;
20054c274da1SToby Isaac 
20064c274da1SToby Isaac   PetscFunctionBegin;
20074c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20084c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
20094c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
20105db9a05bSToby Isaac     PetscInt nRows;
20115db9a05bSToby Isaac 
20125db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
20135db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
20144c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
20157711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
20164c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
20174c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
20184c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
20194c274da1SToby Isaac       if (dof) {
20204c274da1SToby Isaac         PetscInt d;
20214c274da1SToby Isaac         PetscScalar *vals;
20224c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
20234c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
20244c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
20254c274da1SToby Isaac          * we just extracted */
20264c274da1SToby Isaac         for (d = 0; d < dof; d++) {
20274c274da1SToby Isaac           vals[d] = 0.;
20284c274da1SToby Isaac         }
20294c274da1SToby Isaac       }
20304c274da1SToby Isaac     }
20314c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
20324c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
20334c274da1SToby Isaac   }
20344c274da1SToby Isaac   PetscFunctionReturn(0);
20354c274da1SToby Isaac }
20364c274da1SToby Isaac 
203747c6ae99SBarry Smith /*@
20389a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
20399a42bb27SBarry Smith 
20409a42bb27SBarry Smith     Neighbor-wise Collective on DM
20419a42bb27SBarry Smith 
20429a42bb27SBarry Smith     Input Parameters:
20439a42bb27SBarry Smith +   dm - the DM object
2044f6813fd5SJed Brown .   l - the local vector
20451eb28f2eSBarry 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.
20461eb28f2eSBarry Smith -   g - the global vector
20479a42bb27SBarry Smith 
2048d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
204984330215SMatthew 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.
20509a42bb27SBarry Smith 
20519a42bb27SBarry Smith     Level: beginner
20529a42bb27SBarry Smith 
2053e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
20549a42bb27SBarry Smith 
20559a42bb27SBarry Smith @*/
20567087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
20579a42bb27SBarry Smith {
20587128ae9fSMatthew G Knepley   PetscSF                 sf;
205984330215SMatthew G. Knepley   PetscSection            s, gs;
2060d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2061ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2062ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
206384330215SMatthew G. Knepley   PetscBool               isInsert;
206484330215SMatthew G. Knepley   PetscErrorCode          ierr;
20659a42bb27SBarry Smith 
20669a42bb27SBarry Smith   PetscFunctionBegin;
2067171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2068d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2069d4d07f1eSToby Isaac     if (link->beginhook) {
2070d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2071d4d07f1eSToby Isaac     }
2072d4d07f1eSToby Isaac   }
20734c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
20747128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
207584330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
20767128ae9fSMatthew G Knepley   switch (mode) {
20777128ae9fSMatthew G Knepley   case INSERT_VALUES:
20787128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2079304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
208084330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
20817128ae9fSMatthew G Knepley   case ADD_VALUES:
20827128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2083304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
208484330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
20857128ae9fSMatthew G Knepley   default:
208682f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20877128ae9fSMatthew G Knepley   }
208884330215SMatthew G. Knepley   if (sf && !isInsert) {
2089ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
20907128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2091a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2092ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
209384330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
209484330215SMatthew G. Knepley   } else if (s && isInsert) {
209584330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
209684330215SMatthew G. Knepley 
209784330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
209884330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
209984330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2100ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
210184330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
210284330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2103b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
210484330215SMatthew G. Knepley 
210584330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
210603442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
210784330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2108b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
210984330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
211084330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2111b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
211203442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2113b3b16f48SMatthew 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);
2114b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2115b3b16f48SMatthew G. Knepley       if (!gcdof) {
211684330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2117b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2118b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
211984330215SMatthew G. Knepley         const PetscInt *cdofs;
212084330215SMatthew G. Knepley         PetscInt        cind = 0;
212184330215SMatthew G. Knepley 
212284330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2123b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
212484330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2125b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
212684330215SMatthew G. Knepley         }
2127b3b16f48SMatthew 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);
212884330215SMatthew G. Knepley     }
2129ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
21307128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
21317128ae9fSMatthew G Knepley   } else {
2132843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
21337128ae9fSMatthew G Knepley   }
21349a42bb27SBarry Smith   PetscFunctionReturn(0);
21359a42bb27SBarry Smith }
21369a42bb27SBarry Smith 
21379a42bb27SBarry Smith /*@
21389a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
213947c6ae99SBarry Smith 
214047c6ae99SBarry Smith     Neighbor-wise Collective on DM
214147c6ae99SBarry Smith 
214247c6ae99SBarry Smith     Input Parameters:
214347c6ae99SBarry Smith +   dm - the DM object
2144f6813fd5SJed Brown .   l - the local vector
214547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2146f6813fd5SJed Brown -   g - the global vector
214747c6ae99SBarry Smith 
214847c6ae99SBarry Smith 
214947c6ae99SBarry Smith     Level: beginner
215047c6ae99SBarry Smith 
2151e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
215247c6ae99SBarry Smith 
215347c6ae99SBarry Smith @*/
21547087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
215547c6ae99SBarry Smith {
21567128ae9fSMatthew G Knepley   PetscSF                 sf;
215784330215SMatthew G. Knepley   PetscSection            s;
2158d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
215984330215SMatthew G. Knepley   PetscBool               isInsert;
216084330215SMatthew G. Knepley   PetscErrorCode          ierr;
216147c6ae99SBarry Smith 
216247c6ae99SBarry Smith   PetscFunctionBegin;
2163171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21647128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
216584330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
21667128ae9fSMatthew G Knepley   switch (mode) {
21677128ae9fSMatthew G Knepley   case INSERT_VALUES:
21687128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
216984330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
21707128ae9fSMatthew G Knepley   case ADD_VALUES:
21717128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
217284330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
21737128ae9fSMatthew G Knepley   default:
217482f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21757128ae9fSMatthew G Knepley   }
217684330215SMatthew G. Knepley   if (sf && !isInsert) {
2177ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2178ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
217984330215SMatthew G. Knepley 
2180ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
21817128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2182a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2183ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
21847128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
218584330215SMatthew G. Knepley   } else if (s && isInsert) {
21867128ae9fSMatthew G Knepley   } else {
2187843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
21887128ae9fSMatthew G Knepley   }
2189d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2190d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2191d4d07f1eSToby Isaac   }
219247c6ae99SBarry Smith   PetscFunctionReturn(0);
219347c6ae99SBarry Smith }
219447c6ae99SBarry Smith 
2195f089877aSRichard Tran Mills /*@
2196bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2197bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2198d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2199f089877aSRichard Tran Mills 
2200bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2201f089877aSRichard Tran Mills 
2202f089877aSRichard Tran Mills    Input Parameters:
2203f089877aSRichard Tran Mills +  dm - the DM object
2204bc0a1609SRichard Tran Mills .  g - the original local vector
2205bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2206f089877aSRichard Tran Mills 
2207bc0a1609SRichard Tran Mills    Output Parameter:
2208bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2209f089877aSRichard Tran Mills 
2210f089877aSRichard Tran Mills    Level: intermediate
2211f089877aSRichard Tran Mills 
2212bc0a1609SRichard Tran Mills    Notes:
2213bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2214bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2215bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2216bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2217bc0a1609SRichard Tran Mills 
2218bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2219bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2220f089877aSRichard Tran Mills 
2221f089877aSRichard Tran Mills @*/
2222f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2223f089877aSRichard Tran Mills {
2224f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2225f089877aSRichard Tran Mills 
2226f089877aSRichard Tran Mills   PetscFunctionBegin;
2227f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2228f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2229f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2230f089877aSRichard Tran Mills }
2231f089877aSRichard Tran Mills 
2232f089877aSRichard Tran Mills /*@
2233bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2234bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2235d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2236f089877aSRichard Tran Mills 
2237bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2238f089877aSRichard Tran Mills 
2239f089877aSRichard Tran Mills    Input Parameters:
2240bc0a1609SRichard Tran Mills +  da - the DM object
2241bc0a1609SRichard Tran Mills .  g - the original local vector
2242bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2243f089877aSRichard Tran Mills 
2244bc0a1609SRichard Tran Mills    Output Parameter:
2245bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2246f089877aSRichard Tran Mills 
2247f089877aSRichard Tran Mills    Level: intermediate
2248f089877aSRichard Tran Mills 
2249bc0a1609SRichard Tran Mills    Notes:
2250bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2251bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2252bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2253bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2254bc0a1609SRichard Tran Mills 
2255bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2256bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2257f089877aSRichard Tran Mills 
2258f089877aSRichard Tran Mills @*/
2259f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2260f089877aSRichard Tran Mills {
2261f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2262f089877aSRichard Tran Mills 
2263f089877aSRichard Tran Mills   PetscFunctionBegin;
2264f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2265f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2266f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2267f089877aSRichard Tran Mills }
2268f089877aSRichard Tran Mills 
2269f089877aSRichard Tran Mills 
227047c6ae99SBarry Smith /*@
227147c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
227247c6ae99SBarry Smith 
227347c6ae99SBarry Smith     Collective on DM
227447c6ae99SBarry Smith 
227547c6ae99SBarry Smith     Input Parameter:
227647c6ae99SBarry Smith +   dm - the DM object
227791d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
227847c6ae99SBarry Smith 
227947c6ae99SBarry Smith     Output Parameter:
228047c6ae99SBarry Smith .   dmc - the coarsened DM
228147c6ae99SBarry Smith 
228247c6ae99SBarry Smith     Level: developer
228347c6ae99SBarry Smith 
2284e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
228547c6ae99SBarry Smith 
228647c6ae99SBarry Smith @*/
22877087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
228847c6ae99SBarry Smith {
228947c6ae99SBarry Smith   PetscErrorCode    ierr;
2290b17ce1afSJed Brown   DMCoarsenHookLink link;
229147c6ae99SBarry Smith 
229247c6ae99SBarry Smith   PetscFunctionBegin;
2293171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2294fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
229547a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
229647c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
22978892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2298a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
229943842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
23008cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2301644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
23020598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2303656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2304e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2305b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2306b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2307b17ce1afSJed Brown   }
230847a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2309b17ce1afSJed Brown   PetscFunctionReturn(0);
2310b17ce1afSJed Brown }
2311b17ce1afSJed Brown 
2312bb9467b5SJed Brown /*@C
2313b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2314b17ce1afSJed Brown 
2315b17ce1afSJed Brown    Logically Collective
2316b17ce1afSJed Brown 
2317b17ce1afSJed Brown    Input Arguments:
2318b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2319b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2320b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
23210298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2322b17ce1afSJed Brown 
2323b17ce1afSJed Brown    Calling sequence of coarsenhook:
2324b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2325b17ce1afSJed Brown 
2326b17ce1afSJed Brown +  fine - fine level DM
2327b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2328b17ce1afSJed Brown -  ctx - optional user-defined function context
2329b17ce1afSJed Brown 
2330b17ce1afSJed Brown    Calling sequence for restricthook:
2331c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2332b17ce1afSJed Brown 
2333b17ce1afSJed Brown +  fine - fine level DM
2334b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2335c833c3b5SJed Brown .  rscale - scaling vector for restriction
2336c833c3b5SJed Brown .  inject - matrix restricting by injection
2337b17ce1afSJed Brown .  coarse - coarse level DM to update
2338b17ce1afSJed Brown -  ctx - optional user-defined function context
2339b17ce1afSJed Brown 
2340b17ce1afSJed Brown    Level: advanced
2341b17ce1afSJed Brown 
2342b17ce1afSJed Brown    Notes:
2343b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2344b17ce1afSJed Brown 
2345b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2346b17ce1afSJed Brown 
2347b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2348b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2349b17ce1afSJed Brown 
2350bb9467b5SJed Brown    This function is currently not available from Fortran.
2351bb9467b5SJed Brown 
2352c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2353b17ce1afSJed Brown @*/
2354b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2355b17ce1afSJed Brown {
2356b17ce1afSJed Brown   PetscErrorCode    ierr;
2357b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2358b17ce1afSJed Brown 
2359b17ce1afSJed Brown   PetscFunctionBegin;
2360b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
23616bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
236295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2363b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2364b17ce1afSJed Brown   link->restricthook = restricthook;
2365b17ce1afSJed Brown   link->ctx          = ctx;
23660298fd71SBarry Smith   link->next         = NULL;
2367b17ce1afSJed Brown   *p                 = link;
2368b17ce1afSJed Brown   PetscFunctionReturn(0);
2369b17ce1afSJed Brown }
2370b17ce1afSJed Brown 
2371b17ce1afSJed Brown /*@
2372b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2373b17ce1afSJed Brown 
2374b17ce1afSJed Brown    Collective if any hooks are
2375b17ce1afSJed Brown 
2376b17ce1afSJed Brown    Input Arguments:
2377b17ce1afSJed Brown +  fine - finer DM to use as a base
2378b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2379b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2380b17ce1afSJed Brown -  coarse - coarer DM to update
2381b17ce1afSJed Brown 
2382b17ce1afSJed Brown    Level: developer
2383b17ce1afSJed Brown 
2384b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2385b17ce1afSJed Brown @*/
2386b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2387b17ce1afSJed Brown {
2388b17ce1afSJed Brown   PetscErrorCode    ierr;
2389b17ce1afSJed Brown   DMCoarsenHookLink link;
2390b17ce1afSJed Brown 
2391b17ce1afSJed Brown   PetscFunctionBegin;
2392b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
23938865f1eaSKarl Rupp     if (link->restricthook) {
23948865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
23958865f1eaSKarl Rupp     }
2396b17ce1afSJed Brown   }
239747c6ae99SBarry Smith   PetscFunctionReturn(0);
239847c6ae99SBarry Smith }
239947c6ae99SBarry Smith 
2400bb9467b5SJed Brown /*@C
2401be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
24025dbd56e3SPeter Brune 
24035dbd56e3SPeter Brune    Logically Collective
24045dbd56e3SPeter Brune 
24055dbd56e3SPeter Brune    Input Arguments:
24065dbd56e3SPeter Brune +  global - global DM
2407ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
24085dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
24090298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
24105dbd56e3SPeter Brune 
2411ec4806b8SPeter Brune 
2412ec4806b8SPeter Brune    Calling sequence for ddhook:
2413ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2414ec4806b8SPeter Brune 
2415ec4806b8SPeter Brune +  global - global DM
2416ec4806b8SPeter Brune .  block  - block DM
2417ec4806b8SPeter Brune -  ctx - optional user-defined function context
2418ec4806b8SPeter Brune 
24195dbd56e3SPeter Brune    Calling sequence for restricthook:
2420ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
24215dbd56e3SPeter Brune 
24225dbd56e3SPeter Brune +  global - global DM
24235dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
24245dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2425ec4806b8SPeter Brune .  block  - block DM
24265dbd56e3SPeter Brune -  ctx - optional user-defined function context
24275dbd56e3SPeter Brune 
24285dbd56e3SPeter Brune    Level: advanced
24295dbd56e3SPeter Brune 
24305dbd56e3SPeter Brune    Notes:
2431ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
24325dbd56e3SPeter Brune 
24335dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
24345dbd56e3SPeter Brune 
24355dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2436ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
24375dbd56e3SPeter Brune 
2438bb9467b5SJed Brown    This function is currently not available from Fortran.
2439bb9467b5SJed Brown 
24405dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
24415dbd56e3SPeter Brune @*/
2442be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
24435dbd56e3SPeter Brune {
24445dbd56e3SPeter Brune   PetscErrorCode      ierr;
2445be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
24465dbd56e3SPeter Brune 
24475dbd56e3SPeter Brune   PetscFunctionBegin;
24485dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2449be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
245095dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
24515dbd56e3SPeter Brune   link->restricthook = restricthook;
2452be081cd6SPeter Brune   link->ddhook       = ddhook;
24535dbd56e3SPeter Brune   link->ctx          = ctx;
24540298fd71SBarry Smith   link->next         = NULL;
24555dbd56e3SPeter Brune   *p                 = link;
24565dbd56e3SPeter Brune   PetscFunctionReturn(0);
24575dbd56e3SPeter Brune }
24585dbd56e3SPeter Brune 
24595dbd56e3SPeter Brune /*@
2460be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
24615dbd56e3SPeter Brune 
24625dbd56e3SPeter Brune    Collective if any hooks are
24635dbd56e3SPeter Brune 
24645dbd56e3SPeter Brune    Input Arguments:
24655dbd56e3SPeter Brune +  fine - finer DM to use as a base
2466be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2467be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
24685dbd56e3SPeter Brune -  coarse - coarer DM to update
24695dbd56e3SPeter Brune 
24705dbd56e3SPeter Brune    Level: developer
24715dbd56e3SPeter Brune 
24725dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
24735dbd56e3SPeter Brune @*/
2474be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
24755dbd56e3SPeter Brune {
24765dbd56e3SPeter Brune   PetscErrorCode      ierr;
2477be081cd6SPeter Brune   DMSubDomainHookLink link;
24785dbd56e3SPeter Brune 
24795dbd56e3SPeter Brune   PetscFunctionBegin;
2480be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
24818865f1eaSKarl Rupp     if (link->restricthook) {
24828865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
24838865f1eaSKarl Rupp     }
24845dbd56e3SPeter Brune   }
24855dbd56e3SPeter Brune   PetscFunctionReturn(0);
24865dbd56e3SPeter Brune }
24875dbd56e3SPeter Brune 
24885fe1f584SPeter Brune /*@
24896a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
24905fe1f584SPeter Brune 
24915fe1f584SPeter Brune     Not Collective
24925fe1f584SPeter Brune 
24935fe1f584SPeter Brune     Input Parameter:
24945fe1f584SPeter Brune .   dm - the DM object
24955fe1f584SPeter Brune 
24965fe1f584SPeter Brune     Output Parameter:
24976a7d9d85SPeter Brune .   level - number of coarsenings
24985fe1f584SPeter Brune 
24995fe1f584SPeter Brune     Level: developer
25005fe1f584SPeter Brune 
25016a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
25025fe1f584SPeter Brune 
25035fe1f584SPeter Brune @*/
25045fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
25055fe1f584SPeter Brune {
25065fe1f584SPeter Brune   PetscFunctionBegin;
25075fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25085fe1f584SPeter Brune   *level = dm->leveldown;
25095fe1f584SPeter Brune   PetscFunctionReturn(0);
25105fe1f584SPeter Brune }
25115fe1f584SPeter Brune 
25125fe1f584SPeter Brune 
25135fe1f584SPeter Brune 
251447c6ae99SBarry Smith /*@C
251547c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
251647c6ae99SBarry Smith 
251747c6ae99SBarry Smith     Collective on DM
251847c6ae99SBarry Smith 
251947c6ae99SBarry Smith     Input Parameter:
252047c6ae99SBarry Smith +   dm - the DM object
252147c6ae99SBarry Smith -   nlevels - the number of levels of refinement
252247c6ae99SBarry Smith 
252347c6ae99SBarry Smith     Output Parameter:
252447c6ae99SBarry Smith .   dmf - the refined DM hierarchy
252547c6ae99SBarry Smith 
252647c6ae99SBarry Smith     Level: developer
252747c6ae99SBarry Smith 
2528e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
252947c6ae99SBarry Smith 
253047c6ae99SBarry Smith @*/
25317087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
253247c6ae99SBarry Smith {
253347c6ae99SBarry Smith   PetscErrorCode ierr;
253447c6ae99SBarry Smith 
253547c6ae99SBarry Smith   PetscFunctionBegin;
2536171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2537ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
253847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
253947c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
254047c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
254147c6ae99SBarry Smith   } else if (dm->ops->refine) {
254247c6ae99SBarry Smith     PetscInt i;
254347c6ae99SBarry Smith 
2544ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
254547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2546ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
254747c6ae99SBarry Smith     }
2548ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
254947c6ae99SBarry Smith   PetscFunctionReturn(0);
255047c6ae99SBarry Smith }
255147c6ae99SBarry Smith 
255247c6ae99SBarry Smith /*@C
255347c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
255447c6ae99SBarry Smith 
255547c6ae99SBarry Smith     Collective on DM
255647c6ae99SBarry Smith 
255747c6ae99SBarry Smith     Input Parameter:
255847c6ae99SBarry Smith +   dm - the DM object
255947c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
256047c6ae99SBarry Smith 
256147c6ae99SBarry Smith     Output Parameter:
256247c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
256347c6ae99SBarry Smith 
256447c6ae99SBarry Smith     Level: developer
256547c6ae99SBarry Smith 
2566e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
256747c6ae99SBarry Smith 
256847c6ae99SBarry Smith @*/
25697087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
257047c6ae99SBarry Smith {
257147c6ae99SBarry Smith   PetscErrorCode ierr;
257247c6ae99SBarry Smith 
257347c6ae99SBarry Smith   PetscFunctionBegin;
2574171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2575ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
257647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
257747c6ae99SBarry Smith   PetscValidPointer(dmc,3);
257847c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
257947c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
258047c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
258147c6ae99SBarry Smith     PetscInt i;
258247c6ae99SBarry Smith 
2583ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
258447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2585ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
258647c6ae99SBarry Smith     }
2587ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
258847c6ae99SBarry Smith   PetscFunctionReturn(0);
258947c6ae99SBarry Smith }
259047c6ae99SBarry Smith 
259147c6ae99SBarry Smith /*@
2592e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
259347c6ae99SBarry Smith    grids associated with two DMs.
259447c6ae99SBarry Smith 
259547c6ae99SBarry Smith    Collective on DM
259647c6ae99SBarry Smith 
259747c6ae99SBarry Smith    Input Parameters:
259847c6ae99SBarry Smith +  dmc - the coarse grid DM
259947c6ae99SBarry Smith -  dmf - the fine grid DM
260047c6ae99SBarry Smith 
260147c6ae99SBarry Smith    Output Parameters:
260247c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
260347c6ae99SBarry Smith 
260447c6ae99SBarry Smith    Level: intermediate
260547c6ae99SBarry Smith 
260647c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
260747c6ae99SBarry Smith 
2608e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
260947c6ae99SBarry Smith @*/
2610e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
261147c6ae99SBarry Smith {
261247c6ae99SBarry Smith   PetscErrorCode ierr;
261347c6ae99SBarry Smith 
261447c6ae99SBarry Smith   PetscFunctionBegin;
2615171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2616171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
261747c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
261847c6ae99SBarry Smith   PetscFunctionReturn(0);
261947c6ae99SBarry Smith }
262047c6ae99SBarry Smith 
26211a266240SBarry Smith /*@C
26221a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
26231a266240SBarry Smith 
26241a266240SBarry Smith     Not Collective
26251a266240SBarry Smith 
26261a266240SBarry Smith     Input Parameters:
26271a266240SBarry Smith +   dm - the DM object
26281a266240SBarry Smith -   destroy - the destroy function
26291a266240SBarry Smith 
26301a266240SBarry Smith     Level: intermediate
26311a266240SBarry Smith 
2632e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
26331a266240SBarry Smith 
2634f07f9ceaSJed Brown @*/
26351a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
26361a266240SBarry Smith {
26371a266240SBarry Smith   PetscFunctionBegin;
2638171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26391a266240SBarry Smith   dm->ctxdestroy = destroy;
26401a266240SBarry Smith   PetscFunctionReturn(0);
26411a266240SBarry Smith }
26421a266240SBarry Smith 
2643b07ff414SBarry Smith /*@
26441b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
264547c6ae99SBarry Smith 
264647c6ae99SBarry Smith     Not Collective
264747c6ae99SBarry Smith 
264847c6ae99SBarry Smith     Input Parameters:
264947c6ae99SBarry Smith +   dm - the DM object
265047c6ae99SBarry Smith -   ctx - the user context
265147c6ae99SBarry Smith 
265247c6ae99SBarry Smith     Level: intermediate
265347c6ae99SBarry Smith 
2654e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
265547c6ae99SBarry Smith 
265647c6ae99SBarry Smith @*/
26571b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
265847c6ae99SBarry Smith {
265947c6ae99SBarry Smith   PetscFunctionBegin;
2660171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
266147c6ae99SBarry Smith   dm->ctx = ctx;
266247c6ae99SBarry Smith   PetscFunctionReturn(0);
266347c6ae99SBarry Smith }
266447c6ae99SBarry Smith 
266547c6ae99SBarry Smith /*@
26661b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
266747c6ae99SBarry Smith 
266847c6ae99SBarry Smith     Not Collective
266947c6ae99SBarry Smith 
267047c6ae99SBarry Smith     Input Parameter:
267147c6ae99SBarry Smith .   dm - the DM object
267247c6ae99SBarry Smith 
267347c6ae99SBarry Smith     Output Parameter:
267447c6ae99SBarry Smith .   ctx - the user context
267547c6ae99SBarry Smith 
267647c6ae99SBarry Smith     Level: intermediate
267747c6ae99SBarry Smith 
2678e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
267947c6ae99SBarry Smith 
268047c6ae99SBarry Smith @*/
26811b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
268247c6ae99SBarry Smith {
268347c6ae99SBarry Smith   PetscFunctionBegin;
2684171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26851b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
268647c6ae99SBarry Smith   PetscFunctionReturn(0);
268747c6ae99SBarry Smith }
268847c6ae99SBarry Smith 
268908da532bSDmitry Karpeev /*@C
2690df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
269108da532bSDmitry Karpeev 
269208da532bSDmitry Karpeev     Logically Collective on DM
269308da532bSDmitry Karpeev 
269408da532bSDmitry Karpeev     Input Parameter:
269508da532bSDmitry Karpeev +   dm - the DM object
26960298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
269708da532bSDmitry Karpeev 
269808da532bSDmitry Karpeev     Level: intermediate
269908da532bSDmitry Karpeev 
2700835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
270108da532bSDmitry Karpeev          DMSetJacobian()
270208da532bSDmitry Karpeev 
270308da532bSDmitry Karpeev @*/
270408da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
270508da532bSDmitry Karpeev {
270608da532bSDmitry Karpeev   PetscFunctionBegin;
270708da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
270808da532bSDmitry Karpeev   PetscFunctionReturn(0);
270908da532bSDmitry Karpeev }
271008da532bSDmitry Karpeev 
271108da532bSDmitry Karpeev /*@
271208da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
271308da532bSDmitry Karpeev 
271408da532bSDmitry Karpeev     Not Collective
271508da532bSDmitry Karpeev 
271608da532bSDmitry Karpeev     Input Parameter:
271708da532bSDmitry Karpeev .   dm - the DM object to destroy
271808da532bSDmitry Karpeev 
271908da532bSDmitry Karpeev     Output Parameter:
272008da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
272108da532bSDmitry Karpeev 
272208da532bSDmitry Karpeev     Level: developer
272308da532bSDmitry Karpeev 
272474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
272508da532bSDmitry Karpeev 
272608da532bSDmitry Karpeev @*/
272708da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
272808da532bSDmitry Karpeev {
272908da532bSDmitry Karpeev   PetscFunctionBegin;
273008da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
273108da532bSDmitry Karpeev   PetscFunctionReturn(0);
273208da532bSDmitry Karpeev }
273308da532bSDmitry Karpeev 
273408da532bSDmitry Karpeev /*@C
273508da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
273608da532bSDmitry Karpeev 
273708da532bSDmitry Karpeev     Logically Collective on DM
273808da532bSDmitry Karpeev 
273908da532bSDmitry Karpeev     Input Parameters:
2740907376e6SBarry Smith .   dm - the DM object
274108da532bSDmitry Karpeev 
274208da532bSDmitry Karpeev     Output parameters:
274308da532bSDmitry Karpeev +   xl - lower bound
274408da532bSDmitry Karpeev -   xu - upper bound
274508da532bSDmitry Karpeev 
2746907376e6SBarry Smith     Level: advanced
2747907376e6SBarry Smith 
2748907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
274908da532bSDmitry Karpeev 
275074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
275108da532bSDmitry Karpeev 
275208da532bSDmitry Karpeev @*/
275308da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
275408da532bSDmitry Karpeev {
275508da532bSDmitry Karpeev   PetscErrorCode ierr;
27565fd66863SKarl Rupp 
275708da532bSDmitry Karpeev   PetscFunctionBegin;
275808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
275908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
276008da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
276108da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
27628865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
276308da532bSDmitry Karpeev   PetscFunctionReturn(0);
276408da532bSDmitry Karpeev }
276508da532bSDmitry Karpeev 
2766b0ae01b7SPeter Brune /*@
2767b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2768b0ae01b7SPeter Brune 
2769b0ae01b7SPeter Brune     Not Collective
2770b0ae01b7SPeter Brune 
2771b0ae01b7SPeter Brune     Input Parameter:
2772b0ae01b7SPeter Brune .   dm - the DM object
2773b0ae01b7SPeter Brune 
2774b0ae01b7SPeter Brune     Output Parameter:
2775b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2776b0ae01b7SPeter Brune 
2777b0ae01b7SPeter Brune     Level: developer
2778b0ae01b7SPeter Brune 
2779b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2780b0ae01b7SPeter Brune 
2781b0ae01b7SPeter Brune @*/
2782b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2783b0ae01b7SPeter Brune {
2784b0ae01b7SPeter Brune   PetscFunctionBegin;
2785b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2786b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2787b0ae01b7SPeter Brune }
2788b0ae01b7SPeter Brune 
27893ad4599aSBarry Smith /*@
27903ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
27913ad4599aSBarry Smith 
27923ad4599aSBarry Smith     Not Collective
27933ad4599aSBarry Smith 
27943ad4599aSBarry Smith     Input Parameter:
27953ad4599aSBarry Smith .   dm - the DM object
27963ad4599aSBarry Smith 
27973ad4599aSBarry Smith     Output Parameter:
27983ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
27993ad4599aSBarry Smith 
28003ad4599aSBarry Smith     Level: developer
28013ad4599aSBarry Smith 
28023ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
28033ad4599aSBarry Smith 
28043ad4599aSBarry Smith @*/
28053ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
28063ad4599aSBarry Smith {
28073ad4599aSBarry Smith   PetscFunctionBegin;
28083ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
28093ad4599aSBarry Smith   PetscFunctionReturn(0);
28103ad4599aSBarry Smith }
28113ad4599aSBarry Smith 
2812748fac09SDmitry Karpeev /*@C
281308da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
281408da532bSDmitry Karpeev 
281508da532bSDmitry Karpeev     Collective on DM
281608da532bSDmitry Karpeev 
281708da532bSDmitry Karpeev     Input Parameter:
281808da532bSDmitry Karpeev +   dm - the DM object
28190298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
282008da532bSDmitry Karpeev 
282108da532bSDmitry Karpeev     Level: developer
282208da532bSDmitry Karpeev 
282374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
282408da532bSDmitry Karpeev 
282508da532bSDmitry Karpeev @*/
282608da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
282708da532bSDmitry Karpeev {
282808da532bSDmitry Karpeev   PetscErrorCode ierr;
28295fd66863SKarl Rupp 
283008da532bSDmitry Karpeev   PetscFunctionBegin;
283108da532bSDmitry Karpeev   if (x) {
283208da532bSDmitry Karpeev     if (!dm->x) {
283308da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
283408da532bSDmitry Karpeev     }
283508da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
28368865f1eaSKarl Rupp   } else if (dm->x) {
283708da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
283808da532bSDmitry Karpeev   }
283908da532bSDmitry Karpeev   PetscFunctionReturn(0);
284008da532bSDmitry Karpeev }
284108da532bSDmitry Karpeev 
28420298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2843264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2844264ace61SBarry Smith 
2845264ace61SBarry Smith /*@C
2846264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2847264ace61SBarry Smith 
2848264ace61SBarry Smith   Collective on DM
2849264ace61SBarry Smith 
2850264ace61SBarry Smith   Input Parameters:
2851264ace61SBarry Smith + dm     - The DM object
2852264ace61SBarry Smith - method - The name of the DM type
2853264ace61SBarry Smith 
2854264ace61SBarry Smith   Options Database Key:
2855264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2856264ace61SBarry Smith 
2857264ace61SBarry Smith   Notes:
2858e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2859264ace61SBarry Smith 
2860264ace61SBarry Smith   Level: intermediate
2861264ace61SBarry Smith 
2862264ace61SBarry Smith .keywords: DM, set, type
2863264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2864264ace61SBarry Smith @*/
286519fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2866264ace61SBarry Smith {
2867264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2868264ace61SBarry Smith   PetscBool      match;
2869264ace61SBarry Smith   PetscErrorCode ierr;
2870264ace61SBarry Smith 
2871264ace61SBarry Smith   PetscFunctionBegin;
2872264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2873251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2874264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2875264ace61SBarry Smith 
28760f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
28771c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2878ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2879264ace61SBarry Smith 
2880264ace61SBarry Smith   if (dm->ops->destroy) {
2881264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
28820298fd71SBarry Smith     dm->ops->destroy = NULL;
2883264ace61SBarry Smith   }
2884264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2885264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2886264ace61SBarry Smith   PetscFunctionReturn(0);
2887264ace61SBarry Smith }
2888264ace61SBarry Smith 
2889264ace61SBarry Smith /*@C
2890264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2891264ace61SBarry Smith 
2892264ace61SBarry Smith   Not Collective
2893264ace61SBarry Smith 
2894264ace61SBarry Smith   Input Parameter:
2895264ace61SBarry Smith . dm  - The DM
2896264ace61SBarry Smith 
2897264ace61SBarry Smith   Output Parameter:
2898264ace61SBarry Smith . type - The DM type name
2899264ace61SBarry Smith 
2900264ace61SBarry Smith   Level: intermediate
2901264ace61SBarry Smith 
2902264ace61SBarry Smith .keywords: DM, get, type, name
2903264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2904264ace61SBarry Smith @*/
290519fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2906264ace61SBarry Smith {
2907264ace61SBarry Smith   PetscErrorCode ierr;
2908264ace61SBarry Smith 
2909264ace61SBarry Smith   PetscFunctionBegin;
2910264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2911c959eef4SJed Brown   PetscValidPointer(type,2);
2912607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
2913264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2914264ace61SBarry Smith   PetscFunctionReturn(0);
2915264ace61SBarry Smith }
2916264ace61SBarry Smith 
291767a56275SMatthew G Knepley /*@C
291867a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
291967a56275SMatthew G Knepley 
292067a56275SMatthew G Knepley   Collective on DM
292167a56275SMatthew G Knepley 
292267a56275SMatthew G Knepley   Input Parameters:
292367a56275SMatthew G Knepley + dm - the DM
292467a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
292567a56275SMatthew G Knepley 
292667a56275SMatthew G Knepley   Output Parameter:
292767a56275SMatthew G Knepley . M - pointer to new DM
292867a56275SMatthew G Knepley 
292967a56275SMatthew G Knepley   Notes:
293067a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
293167a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
293267a56275SMatthew G Knepley   of the input DM.
293367a56275SMatthew G Knepley 
293467a56275SMatthew G Knepley   Level: intermediate
293567a56275SMatthew G Knepley 
293667a56275SMatthew G Knepley .seealso: DMCreate()
293767a56275SMatthew G Knepley @*/
293819fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
293967a56275SMatthew G Knepley {
294067a56275SMatthew G Knepley   DM             B;
294167a56275SMatthew G Knepley   char           convname[256];
2942c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
294367a56275SMatthew G Knepley   PetscErrorCode ierr;
294467a56275SMatthew G Knepley 
294567a56275SMatthew G Knepley   PetscFunctionBegin;
294667a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
294767a56275SMatthew G Knepley   PetscValidType(dm,1);
294867a56275SMatthew G Knepley   PetscValidPointer(M,3);
2949251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
2950c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
2951c067b6caSMatthew G. Knepley   if (sametype) {
2952c067b6caSMatthew G. Knepley     *M   = dm;
2953c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
2954c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
2955c067b6caSMatthew G. Knepley   } else {
29560298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
295767a56275SMatthew G Knepley 
295867a56275SMatthew G Knepley     /*
295967a56275SMatthew G Knepley        Order of precedence:
296067a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
296167a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
296267a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
296367a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
296467a56275SMatthew G Knepley        5) Use a really basic converter.
296567a56275SMatthew G Knepley     */
296667a56275SMatthew G Knepley 
296767a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
296867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
296967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
297067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
297167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
297267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
29730005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
297467a56275SMatthew G Knepley     if (conv) goto foundconv;
297567a56275SMatthew G Knepley 
297667a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
297782f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
297867a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
297967a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
298067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
298167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
298267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
298367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
29840005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
298567a56275SMatthew G Knepley     if (conv) {
2986fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
298767a56275SMatthew G Knepley       goto foundconv;
298867a56275SMatthew G Knepley     }
298967a56275SMatthew G Knepley 
299067a56275SMatthew G Knepley #if 0
299167a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
299267a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2993fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
299467a56275SMatthew G Knepley     if (conv) goto foundconv;
299567a56275SMatthew G Knepley 
299667a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
299767a56275SMatthew G Knepley     if (dm->ops->convert) {
299867a56275SMatthew G Knepley       conv = dm->ops->convert;
299967a56275SMatthew G Knepley     }
300067a56275SMatthew G Knepley     if (conv) goto foundconv;
300167a56275SMatthew G Knepley #endif
300267a56275SMatthew G Knepley 
300367a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
300482f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
300567a56275SMatthew G Knepley 
300667a56275SMatthew G Knepley foundconv:
300767a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
300867a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
300912fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
3010*90b157c4SStefano Zampini     {
3011*90b157c4SStefano Zampini       PetscBool             isper;
301212fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
301312fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
3014*90b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
3015*90b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
301612fa691eSMatthew G. Knepley     }
301767a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
301867a56275SMatthew G Knepley   }
301967a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
302067a56275SMatthew G Knepley   PetscFunctionReturn(0);
302167a56275SMatthew G Knepley }
3022264ace61SBarry Smith 
3023264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3024264ace61SBarry Smith 
3025264ace61SBarry Smith /*@C
30261c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
30271c84c290SBarry Smith 
30281c84c290SBarry Smith   Not Collective
30291c84c290SBarry Smith 
30301c84c290SBarry Smith   Input Parameters:
30311c84c290SBarry Smith + name        - The name of a new user-defined creation routine
30321c84c290SBarry Smith - create_func - The creation routine itself
30331c84c290SBarry Smith 
30341c84c290SBarry Smith   Notes:
30351c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
30361c84c290SBarry Smith 
30371c84c290SBarry Smith 
30381c84c290SBarry Smith   Sample usage:
30391c84c290SBarry Smith .vb
3040bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
30411c84c290SBarry Smith .ve
30421c84c290SBarry Smith 
30431c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
30441c84c290SBarry Smith .vb
30451c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
30461c84c290SBarry Smith     DMSetType(DM,"my_da");
30471c84c290SBarry Smith .ve
30481c84c290SBarry Smith    or at runtime via the option
30491c84c290SBarry Smith .vb
30501c84c290SBarry Smith     -da_type my_da
30511c84c290SBarry Smith .ve
3052264ace61SBarry Smith 
3053264ace61SBarry Smith   Level: advanced
30541c84c290SBarry Smith 
30551c84c290SBarry Smith .keywords: DM, register
3056bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
30571c84c290SBarry Smith 
3058264ace61SBarry Smith @*/
3059bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3060264ace61SBarry Smith {
3061264ace61SBarry Smith   PetscErrorCode ierr;
3062264ace61SBarry Smith 
3063264ace61SBarry Smith   PetscFunctionBegin;
3064a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3065264ace61SBarry Smith   PetscFunctionReturn(0);
3066264ace61SBarry Smith }
3067264ace61SBarry Smith 
3068b859378eSBarry Smith /*@C
306955849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3070b859378eSBarry Smith 
3071b859378eSBarry Smith   Collective on PetscViewer
3072b859378eSBarry Smith 
3073b859378eSBarry Smith   Input Parameters:
3074b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3075b859378eSBarry Smith            some related function before a call to DMLoad().
3076b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3077b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3078b859378eSBarry Smith 
3079b859378eSBarry Smith    Level: intermediate
3080b859378eSBarry Smith 
3081b859378eSBarry Smith   Notes:
308255849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3083b859378eSBarry Smith 
3084b859378eSBarry Smith   Notes for advanced users:
3085b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3086b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3087b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3088b859378eSBarry Smith   format is
3089b859378eSBarry Smith .vb
3090b859378eSBarry Smith      has not yet been determined
3091b859378eSBarry Smith .ve
3092b859378eSBarry Smith 
3093b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3094b859378eSBarry Smith @*/
3095b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3096b859378eSBarry Smith {
30979331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3098b859378eSBarry Smith   PetscErrorCode ierr;
3099b859378eSBarry Smith 
3100b859378eSBarry Smith   PetscFunctionBegin;
3101b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3102b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
310332c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
31049331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
31059331c7a4SMatthew G. Knepley   if (isbinary) {
31069331c7a4SMatthew G. Knepley     PetscInt classid;
31079331c7a4SMatthew G. Knepley     char     type[256];
3108b859378eSBarry Smith 
3109060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
31109200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3111060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
311232c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
31139331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
31149331c7a4SMatthew G. Knepley   } else if (ishdf5) {
31159331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
31169331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3117b859378eSBarry Smith   PetscFunctionReturn(0);
3118b859378eSBarry Smith }
3119b859378eSBarry Smith 
31207da65231SMatthew G Knepley /******************************** FEM Support **********************************/
31217da65231SMatthew G Knepley 
3122a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3123a6dfd86eSKarl Rupp {
31241d47ebbbSSatish Balay   PetscInt       f;
31251b30c384SMatthew G Knepley   PetscErrorCode ierr;
31261b30c384SMatthew G Knepley 
31277da65231SMatthew G Knepley   PetscFunctionBegin;
312874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
31291d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
313057622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
31317da65231SMatthew G Knepley   }
31327da65231SMatthew G Knepley   PetscFunctionReturn(0);
31337da65231SMatthew G Knepley }
31347da65231SMatthew G Knepley 
3135a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3136a6dfd86eSKarl Rupp {
31371b30c384SMatthew G Knepley   PetscInt       f, g;
31387da65231SMatthew G Knepley   PetscErrorCode ierr;
31397da65231SMatthew G Knepley 
31407da65231SMatthew G Knepley   PetscFunctionBegin;
314174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
31421d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
314374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
31441d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3145e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
31467da65231SMatthew G Knepley     }
314774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
31487da65231SMatthew G Knepley   }
31497da65231SMatthew G Knepley   PetscFunctionReturn(0);
31507da65231SMatthew G Knepley }
3151e7c4fc90SDmitry Karpeev 
31526113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3153e759306cSMatthew G. Knepley {
31549852e123SBarry Smith   PetscMPIInt    rank, size;
3155e759306cSMatthew G. Knepley   PetscInt       p;
3156e759306cSMatthew G. Knepley   PetscErrorCode ierr;
3157e759306cSMatthew G. Knepley 
3158e759306cSMatthew G. Knepley   PetscFunctionBegin;
3159e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
31609852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &size);CHKERRQ(ierr);
3161e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
31629852e123SBarry Smith   for (p = 0; p < size; ++p) {
3163e759306cSMatthew G. Knepley     if (p == rank) {
3164e759306cSMatthew G. Knepley       Vec x;
3165e759306cSMatthew G. Knepley 
3166e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3167e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
31686113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
3169e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3170e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3171e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3172e759306cSMatthew G. Knepley     }
3173e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3174e759306cSMatthew G. Knepley   }
3175e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3176e759306cSMatthew G. Knepley }
3177e759306cSMatthew G. Knepley 
317888ed4aceSMatthew G Knepley /*@
317988ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
318088ed4aceSMatthew G Knepley 
318188ed4aceSMatthew G Knepley   Input Parameter:
318288ed4aceSMatthew G Knepley . dm - The DM
318388ed4aceSMatthew G Knepley 
318488ed4aceSMatthew G Knepley   Output Parameter:
318588ed4aceSMatthew G Knepley . section - The PetscSection
318688ed4aceSMatthew G Knepley 
318788ed4aceSMatthew G Knepley   Level: intermediate
318888ed4aceSMatthew G Knepley 
318988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
319088ed4aceSMatthew G Knepley 
319188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
319288ed4aceSMatthew G Knepley @*/
31930adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
31940adebc6cSBarry Smith {
3195fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3196fd59a867SMatthew G. Knepley 
319788ed4aceSMatthew G Knepley   PetscFunctionBegin;
319888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
319988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
32002f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
32012f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3202ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
32032f0f8703SMatthew G. Knepley   }
320488ed4aceSMatthew G Knepley   *section = dm->defaultSection;
320588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
320688ed4aceSMatthew G Knepley }
320788ed4aceSMatthew G Knepley 
320888ed4aceSMatthew G Knepley /*@
320988ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
321088ed4aceSMatthew G Knepley 
321188ed4aceSMatthew G Knepley   Input Parameters:
321288ed4aceSMatthew G Knepley + dm - The DM
321388ed4aceSMatthew G Knepley - section - The PetscSection
321488ed4aceSMatthew G Knepley 
321588ed4aceSMatthew G Knepley   Level: intermediate
321688ed4aceSMatthew G Knepley 
321788ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
321888ed4aceSMatthew G Knepley 
321988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
322088ed4aceSMatthew G Knepley @*/
32210adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
32220adebc6cSBarry Smith {
3223c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3224af122d2aSMatthew G Knepley   PetscInt       f;
322588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
322688ed4aceSMatthew G Knepley 
322788ed4aceSMatthew G Knepley   PetscFunctionBegin;
322888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3229c473ab19SMatthew G. Knepley   if (section) {
32301d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
32311d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3232c473ab19SMatthew G. Knepley   }
323388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
323488ed4aceSMatthew G Knepley   dm->defaultSection = section;
3235c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3236af122d2aSMatthew G Knepley   if (numFields) {
3237af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3238af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
32390f21e855SMatthew G. Knepley       PetscObject disc;
3240af122d2aSMatthew G Knepley       const char *name;
3241af122d2aSMatthew G Knepley 
3242af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
32430f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
32440f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3245af122d2aSMatthew G Knepley     }
3246af122d2aSMatthew G Knepley   }
32471d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
32481d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
324988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
325088ed4aceSMatthew G Knepley }
325188ed4aceSMatthew G Knepley 
32529435951eSToby Isaac /*@
32539435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
32549435951eSToby Isaac 
3255e228b242SToby Isaac   not collective
3256e228b242SToby Isaac 
32579435951eSToby Isaac   Input Parameter:
32589435951eSToby Isaac . dm - The DM
32599435951eSToby Isaac 
32609435951eSToby Isaac   Output Parameter:
32619435951eSToby 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.
32629435951eSToby 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.
32639435951eSToby Isaac 
32649435951eSToby Isaac   Level: advanced
32659435951eSToby Isaac 
32669435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
32679435951eSToby Isaac 
32689435951eSToby Isaac .seealso: DMSetDefaultConstraints()
32699435951eSToby Isaac @*/
32709435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
32719435951eSToby Isaac {
32729435951eSToby Isaac   PetscErrorCode ierr;
32739435951eSToby Isaac 
32749435951eSToby Isaac   PetscFunctionBegin;
32759435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32769435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
327745a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
327845a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
32799435951eSToby Isaac   PetscFunctionReturn(0);
32809435951eSToby Isaac }
32819435951eSToby Isaac 
32829435951eSToby Isaac /*@
32839435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
32849435951eSToby Isaac 
32859435951eSToby 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().
32869435951eSToby Isaac 
32879435951eSToby 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.
32889435951eSToby Isaac 
3289e228b242SToby Isaac   collective on dm
3290e228b242SToby Isaac 
32919435951eSToby Isaac   Input Parameters:
32929435951eSToby Isaac + dm - The DM
3293e228b242SToby 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).
3294e228b242SToby 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).
32959435951eSToby Isaac 
32969435951eSToby Isaac   Level: advanced
32979435951eSToby Isaac 
32989435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
32999435951eSToby Isaac 
33009435951eSToby Isaac .seealso: DMGetDefaultConstraints()
33019435951eSToby Isaac @*/
33029435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
33039435951eSToby Isaac {
3304e228b242SToby Isaac   PetscMPIInt result;
33059435951eSToby Isaac   PetscErrorCode ierr;
33069435951eSToby Isaac 
33079435951eSToby Isaac   PetscFunctionBegin;
33089435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3309e228b242SToby Isaac   if (section) {
3310e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3311e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3312f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3313e228b242SToby Isaac   }
3314e228b242SToby Isaac   if (mat) {
3315e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3316e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3317f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3318e228b242SToby Isaac   }
33199435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
33209435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
33219435951eSToby Isaac   dm->defaultConstraintSection = section;
33229435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
33239435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
33249435951eSToby Isaac   dm->defaultConstraintMat = mat;
33259435951eSToby Isaac   PetscFunctionReturn(0);
33269435951eSToby Isaac }
33279435951eSToby Isaac 
3328f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3329507e4973SMatthew G. Knepley /*
3330507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3331507e4973SMatthew G. Knepley 
3332507e4973SMatthew G. Knepley   Input Parameters:
3333507e4973SMatthew G. Knepley + dm - The DM
3334507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3335507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3336507e4973SMatthew G. Knepley 
3337507e4973SMatthew G. Knepley   Level: intermediate
3338507e4973SMatthew G. Knepley 
3339507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3340507e4973SMatthew G. Knepley */
3341f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3342507e4973SMatthew G. Knepley {
3343507e4973SMatthew G. Knepley   MPI_Comm        comm;
3344507e4973SMatthew G. Knepley   PetscLayout     layout;
3345507e4973SMatthew G. Knepley   const PetscInt *ranges;
3346507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3347507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3348507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3349507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3350507e4973SMatthew G. Knepley 
3351507e4973SMatthew G. Knepley   PetscFunctionBegin;
3352507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3353507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3354507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3355507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3356507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3357507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3358507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3359507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3360507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3361507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3362507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3363507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3364f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3365507e4973SMatthew G. Knepley 
3366507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3367507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3368507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3369507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3370507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3371507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3372507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3373507e4973SMatthew 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;}
3374507e4973SMatthew 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;}
3375507e4973SMatthew G. Knepley     if (gdof < 0) {
3376507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3377507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3378507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3379507e4973SMatthew G. Knepley 
3380507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3381507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3382507e4973SMatthew 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;}
3383507e4973SMatthew G. Knepley       }
3384507e4973SMatthew G. Knepley     }
3385507e4973SMatthew G. Knepley   }
3386507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3387507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3388b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3389507e4973SMatthew G. Knepley   if (!gvalid) {
3390507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3391507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3392507e4973SMatthew G. Knepley   }
3393507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3394507e4973SMatthew G. Knepley }
3395f741bcd2SMatthew G. Knepley #endif
3396507e4973SMatthew G. Knepley 
339788ed4aceSMatthew G Knepley /*@
339888ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
339988ed4aceSMatthew G Knepley 
34008b1ab98fSJed Brown   Collective on DM
34018b1ab98fSJed Brown 
340288ed4aceSMatthew G Knepley   Input Parameter:
340388ed4aceSMatthew G Knepley . dm - The DM
340488ed4aceSMatthew G Knepley 
340588ed4aceSMatthew G Knepley   Output Parameter:
340688ed4aceSMatthew G Knepley . section - The PetscSection
340788ed4aceSMatthew G Knepley 
340888ed4aceSMatthew G Knepley   Level: intermediate
340988ed4aceSMatthew G Knepley 
341088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
341188ed4aceSMatthew G Knepley 
341288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
341388ed4aceSMatthew G Knepley @*/
34140adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
34150adebc6cSBarry Smith {
341688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
341788ed4aceSMatthew G Knepley 
341888ed4aceSMatthew G Knepley   PetscFunctionBegin;
341988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
342088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
342188ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3422fd59a867SMatthew G. Knepley     PetscSection s;
3423fd59a867SMatthew G. Knepley 
3424fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3425fd59a867SMatthew 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");
3426fd59a867SMatthew 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");
342715b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3428cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3429ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3430685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
343188ed4aceSMatthew G Knepley   }
343288ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
343388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
343488ed4aceSMatthew G Knepley }
343588ed4aceSMatthew G Knepley 
3436b21d0597SMatthew G Knepley /*@
3437b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3438b21d0597SMatthew G Knepley 
3439b21d0597SMatthew G Knepley   Input Parameters:
3440b21d0597SMatthew G Knepley + dm - The DM
34415080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3442b21d0597SMatthew G Knepley 
3443b21d0597SMatthew G Knepley   Level: intermediate
3444b21d0597SMatthew G Knepley 
3445b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3446b21d0597SMatthew G Knepley 
3447b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3448b21d0597SMatthew G Knepley @*/
34490adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
34500adebc6cSBarry Smith {
3451b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3452b21d0597SMatthew G Knepley 
3453b21d0597SMatthew G Knepley   PetscFunctionBegin;
3454b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34555080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
34561d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3457b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3458b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3459507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3460f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3461507e4973SMatthew G. Knepley #endif
3462b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3463b21d0597SMatthew G Knepley }
3464b21d0597SMatthew G Knepley 
346588ed4aceSMatthew G Knepley /*@
346688ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
346788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
346888ed4aceSMatthew G Knepley 
346988ed4aceSMatthew G Knepley   Input Parameter:
347088ed4aceSMatthew G Knepley . dm - The DM
347188ed4aceSMatthew G Knepley 
347288ed4aceSMatthew G Knepley   Output Parameter:
347388ed4aceSMatthew G Knepley . sf - The PetscSF
347488ed4aceSMatthew G Knepley 
347588ed4aceSMatthew G Knepley   Level: intermediate
347688ed4aceSMatthew G Knepley 
347788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
347888ed4aceSMatthew G Knepley 
347988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
348088ed4aceSMatthew G Knepley @*/
34810adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
34820adebc6cSBarry Smith {
348388ed4aceSMatthew G Knepley   PetscInt       nroots;
348488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
348588ed4aceSMatthew G Knepley 
348688ed4aceSMatthew G Knepley   PetscFunctionBegin;
348788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
348888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
34890298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
349088ed4aceSMatthew G Knepley   if (nroots < 0) {
349188ed4aceSMatthew G Knepley     PetscSection section, gSection;
349288ed4aceSMatthew G Knepley 
349388ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
349431ea6d37SMatthew G Knepley     if (section) {
349588ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
349688ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
349731ea6d37SMatthew G Knepley     } else {
34980298fd71SBarry Smith       *sf = NULL;
349931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
350031ea6d37SMatthew G Knepley     }
350188ed4aceSMatthew G Knepley   }
350288ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
350388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
350488ed4aceSMatthew G Knepley }
350588ed4aceSMatthew G Knepley 
350688ed4aceSMatthew G Knepley /*@
350788ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
350888ed4aceSMatthew G Knepley 
350988ed4aceSMatthew G Knepley   Input Parameters:
351088ed4aceSMatthew G Knepley + dm - The DM
351188ed4aceSMatthew G Knepley - sf - The PetscSF
351288ed4aceSMatthew G Knepley 
351388ed4aceSMatthew G Knepley   Level: intermediate
351488ed4aceSMatthew G Knepley 
351588ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
351688ed4aceSMatthew G Knepley 
351788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
351888ed4aceSMatthew G Knepley @*/
35190adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
35200adebc6cSBarry Smith {
352188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
352288ed4aceSMatthew G Knepley 
352388ed4aceSMatthew G Knepley   PetscFunctionBegin;
352488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
352588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
352688ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
352788ed4aceSMatthew G Knepley   dm->defaultSF = sf;
352888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
352988ed4aceSMatthew G Knepley }
353088ed4aceSMatthew G Knepley 
353188ed4aceSMatthew G Knepley /*@C
353288ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
353388ed4aceSMatthew G Knepley   describing the data layout.
353488ed4aceSMatthew G Knepley 
353588ed4aceSMatthew G Knepley   Input Parameters:
353688ed4aceSMatthew G Knepley + dm - The DM
353788ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
353888ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
353988ed4aceSMatthew G Knepley 
354088ed4aceSMatthew G Knepley   Level: intermediate
354188ed4aceSMatthew G Knepley 
354288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
354388ed4aceSMatthew G Knepley @*/
354488ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
354588ed4aceSMatthew G Knepley {
354682f516ccSBarry Smith   MPI_Comm       comm;
354788ed4aceSMatthew G Knepley   PetscLayout    layout;
354888ed4aceSMatthew G Knepley   const PetscInt *ranges;
354988ed4aceSMatthew G Knepley   PetscInt       *local;
355088ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3551ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
355288ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
355388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
355488ed4aceSMatthew G Knepley 
355588ed4aceSMatthew G Knepley   PetscFunctionBegin;
355682f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
355788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
355888ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
355988ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
356088ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
356188ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
356288ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
356388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
356488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
356588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
356688ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3567ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
35686636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
356988ed4aceSMatthew G Knepley 
35706636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35716636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3572235fbf56SMatthew 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));
35736636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
357488ed4aceSMatthew G Knepley   }
3575785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3576785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
357788ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
35781f588964SMatthew G Knepley     const PetscInt *cind;
35796636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
358088ed4aceSMatthew G Knepley 
358188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
358288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
358388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
358488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
358588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35866636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
358788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
35886636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
35896636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
35906636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3591057b4bcdSMatthew 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);
35926636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
35936636e97aSMatthew G Knepley     }
359488ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
359588ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
359688ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
359788ed4aceSMatthew G Knepley     }
359888ed4aceSMatthew G Knepley     if (gdof < 0) {
35996636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
360088ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
360188ed4aceSMatthew G Knepley 
360205376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
360331d3f06eSJed Brown         if (r < 0) r = -(r+2);
360405376888SMatthew 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);
360588ed4aceSMatthew G Knepley         remote[l].rank  = r;
360688ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
360788ed4aceSMatthew G Knepley       }
360888ed4aceSMatthew G Knepley     } else {
36096636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
361088ed4aceSMatthew G Knepley         remote[l].rank  = rank;
361188ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
361288ed4aceSMatthew G Knepley       }
361388ed4aceSMatthew G Knepley     }
361488ed4aceSMatthew G Knepley   }
36156636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
361688ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
361788ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
361888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
361988ed4aceSMatthew G Knepley }
3620af122d2aSMatthew G Knepley 
3621b21d0597SMatthew G Knepley /*@
3622b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3623b21d0597SMatthew G Knepley 
3624b21d0597SMatthew G Knepley   Input Parameter:
3625b21d0597SMatthew G Knepley . dm - The DM
3626b21d0597SMatthew G Knepley 
3627b21d0597SMatthew G Knepley   Output Parameter:
3628b21d0597SMatthew G Knepley . sf - The PetscSF
3629b21d0597SMatthew G Knepley 
3630b21d0597SMatthew G Knepley   Level: intermediate
3631b21d0597SMatthew G Knepley 
3632b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3633b21d0597SMatthew G Knepley 
3634057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3635b21d0597SMatthew G Knepley @*/
36360adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
36370adebc6cSBarry Smith {
3638b21d0597SMatthew G Knepley   PetscFunctionBegin;
3639b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3640b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3641b21d0597SMatthew G Knepley   *sf = dm->sf;
3642b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3643b21d0597SMatthew G Knepley }
3644b21d0597SMatthew G Knepley 
3645057b4bcdSMatthew G Knepley /*@
3646057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3647057b4bcdSMatthew G Knepley 
3648057b4bcdSMatthew G Knepley   Input Parameters:
3649057b4bcdSMatthew G Knepley + dm - The DM
3650057b4bcdSMatthew G Knepley - sf - The PetscSF
3651057b4bcdSMatthew G Knepley 
3652057b4bcdSMatthew G Knepley   Level: intermediate
3653057b4bcdSMatthew G Knepley 
3654057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3655057b4bcdSMatthew G Knepley @*/
36560adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
36570adebc6cSBarry Smith {
3658057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3659057b4bcdSMatthew G Knepley 
3660057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3661057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3662057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3663057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3664057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3665057b4bcdSMatthew G Knepley   dm->sf = sf;
3666057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3667057b4bcdSMatthew G Knepley }
3668057b4bcdSMatthew G Knepley 
36692764a2aaSMatthew G. Knepley /*@
36702764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
36712764a2aaSMatthew G. Knepley 
36722764a2aaSMatthew G. Knepley   Input Parameter:
36732764a2aaSMatthew G. Knepley . dm - The DM
36742764a2aaSMatthew G. Knepley 
36752764a2aaSMatthew G. Knepley   Output Parameter:
36762764a2aaSMatthew G. Knepley . prob - The PetscDS
36772764a2aaSMatthew G. Knepley 
36782764a2aaSMatthew G. Knepley   Level: developer
36792764a2aaSMatthew G. Knepley 
36802764a2aaSMatthew G. Knepley .seealso: DMSetDS()
36812764a2aaSMatthew G. Knepley @*/
36822764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3683af122d2aSMatthew G Knepley {
3684af122d2aSMatthew G Knepley   PetscFunctionBegin;
3685af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36860f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
36870f21e855SMatthew G. Knepley   *prob = dm->prob;
36880f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36890f21e855SMatthew G. Knepley }
36900f21e855SMatthew G. Knepley 
36912764a2aaSMatthew G. Knepley /*@
36922764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
36932764a2aaSMatthew G. Knepley 
36942764a2aaSMatthew G. Knepley   Input Parameters:
36952764a2aaSMatthew G. Knepley + dm - The DM
36962764a2aaSMatthew G. Knepley - prob - The PetscDS
36972764a2aaSMatthew G. Knepley 
36982764a2aaSMatthew G. Knepley   Level: developer
36992764a2aaSMatthew G. Knepley 
37002764a2aaSMatthew G. Knepley .seealso: DMGetDS()
37012764a2aaSMatthew G. Knepley @*/
37022764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
37030f21e855SMatthew G. Knepley {
37040f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37050f21e855SMatthew G. Knepley 
37060f21e855SMatthew G. Knepley   PetscFunctionBegin;
37070f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37082764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
370937316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
37102764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
37110f21e855SMatthew G. Knepley   dm->prob = prob;
37120f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
37130f21e855SMatthew G. Knepley }
37140f21e855SMatthew G. Knepley 
37150f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
37160f21e855SMatthew G. Knepley {
37170f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37180f21e855SMatthew G. Knepley 
37190f21e855SMatthew G. Knepley   PetscFunctionBegin;
37200f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37212764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3722af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3723af122d2aSMatthew G Knepley }
3724af122d2aSMatthew G Knepley 
3725af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3726af122d2aSMatthew G Knepley {
37270f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3728af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3729af122d2aSMatthew G Knepley 
3730af122d2aSMatthew G Knepley   PetscFunctionBegin;
3731af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37322764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
37330f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
37340f21e855SMatthew G. Knepley     PetscContainer obj;
37350f21e855SMatthew G. Knepley 
37360f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
37372764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
37380f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3739af122d2aSMatthew G Knepley   }
3740af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3741af122d2aSMatthew G Knepley }
3742af122d2aSMatthew G Knepley 
3743c1929be8SMatthew G. Knepley /*@
3744c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3745c1929be8SMatthew G. Knepley 
3746c1929be8SMatthew G. Knepley   Not collective
3747c1929be8SMatthew G. Knepley 
3748c1929be8SMatthew G. Knepley   Input Parameters:
3749c1929be8SMatthew G. Knepley + dm - The DM
3750c1929be8SMatthew G. Knepley - f  - The field number
3751c1929be8SMatthew G. Knepley 
3752c1929be8SMatthew G. Knepley   Output Parameter:
3753c1929be8SMatthew G. Knepley . field - The discretization object
3754c1929be8SMatthew G. Knepley 
3755c1929be8SMatthew G. Knepley   Level: developer
3756c1929be8SMatthew G. Knepley 
3757c1929be8SMatthew G. Knepley .seealso: DMSetField()
3758c1929be8SMatthew G. Knepley @*/
3759af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3760af122d2aSMatthew G Knepley {
37610f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37620f21e855SMatthew G. Knepley 
3763af122d2aSMatthew G Knepley   PetscFunctionBegin;
3764af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37652764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3766decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3767decb47aaSMatthew G. Knepley }
3768decb47aaSMatthew G. Knepley 
3769c1929be8SMatthew G. Knepley /*@
3770c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3771c1929be8SMatthew G. Knepley 
3772c1929be8SMatthew G. Knepley   Logically collective on DM
3773c1929be8SMatthew G. Knepley 
3774c1929be8SMatthew G. Knepley   Input Parameters:
3775c1929be8SMatthew G. Knepley + dm - The DM
3776c1929be8SMatthew G. Knepley . f  - The field number
3777c1929be8SMatthew G. Knepley - field - The discretization object
3778c1929be8SMatthew G. Knepley 
3779c1929be8SMatthew G. Knepley   Level: developer
3780c1929be8SMatthew G. Knepley 
3781c1929be8SMatthew G. Knepley .seealso: DMGetField()
3782c1929be8SMatthew G. Knepley @*/
3783decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3784decb47aaSMatthew G. Knepley {
3785decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3786decb47aaSMatthew G. Knepley 
3787decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3788decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37892764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3790af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3791af122d2aSMatthew G Knepley }
37926636e97aSMatthew G Knepley 
3793b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3794b64e0483SPeter Brune {
3795b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3796b64e0483SPeter Brune   PetscErrorCode ierr;
3797b64e0483SPeter Brune   Vec coords,ccoords;
37986dbf9973SLawrence Mitchell   Mat inject;
3799b64e0483SPeter Brune   PetscFunctionBegin;
3800b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3801b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3802b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3803b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3804b64e0483SPeter Brune   if (coords && !ccoords) {
3805b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
38066668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
38076dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
38082adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
38096dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3810b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3811b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3812b64e0483SPeter Brune   }
3813b64e0483SPeter Brune   PetscFunctionReturn(0);
3814b64e0483SPeter Brune }
3815b64e0483SPeter Brune 
381603dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
381703dadc2fSPeter Brune {
381803dadc2fSPeter Brune   DM dm_coord,subdm_coord;
381903dadc2fSPeter Brune   PetscErrorCode ierr;
382003dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
382103dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
382203dadc2fSPeter Brune   PetscFunctionBegin;
382303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
382403dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
382503dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
382603dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
382703dadc2fSPeter Brune   if (coords && !ccoords) {
382803dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
38296668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
383003dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
383124640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
383203dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
383303dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
383403dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
383503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
383603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
383703dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
383803dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
383903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
384003dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
384103dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
384203dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
384303dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
384403dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
384503dadc2fSPeter Brune   }
384603dadc2fSPeter Brune   PetscFunctionReturn(0);
384703dadc2fSPeter Brune }
384803dadc2fSPeter Brune 
3849c73cfb54SMatthew G. Knepley /*@
3850c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3851c73cfb54SMatthew G. Knepley 
3852c73cfb54SMatthew G. Knepley   Not collective
3853c73cfb54SMatthew G. Knepley 
3854c73cfb54SMatthew G. Knepley   Input Parameter:
3855c73cfb54SMatthew G. Knepley . dm - The DM
3856c73cfb54SMatthew G. Knepley 
3857c73cfb54SMatthew G. Knepley   Output Parameter:
3858c73cfb54SMatthew G. Knepley . dim - The topological dimension
3859c73cfb54SMatthew G. Knepley 
3860c73cfb54SMatthew G. Knepley   Level: beginner
3861c73cfb54SMatthew G. Knepley 
3862c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3863c73cfb54SMatthew G. Knepley @*/
3864c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3865c73cfb54SMatthew G. Knepley {
3866c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3867c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3868c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3869c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3870c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3871c73cfb54SMatthew G. Knepley }
3872c73cfb54SMatthew G. Knepley 
3873c73cfb54SMatthew G. Knepley /*@
3874c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3875c73cfb54SMatthew G. Knepley 
3876c73cfb54SMatthew G. Knepley   Collective on dm
3877c73cfb54SMatthew G. Knepley 
3878c73cfb54SMatthew G. Knepley   Input Parameters:
3879c73cfb54SMatthew G. Knepley + dm - The DM
3880c73cfb54SMatthew G. Knepley - dim - The topological dimension
3881c73cfb54SMatthew G. Knepley 
3882c73cfb54SMatthew G. Knepley   Level: beginner
3883c73cfb54SMatthew G. Knepley 
3884c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3885c73cfb54SMatthew G. Knepley @*/
3886c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3887c73cfb54SMatthew G. Knepley {
3888c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3889c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3890c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3891c73cfb54SMatthew G. Knepley   dm->dim = dim;
3892c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3893c73cfb54SMatthew G. Knepley }
3894c73cfb54SMatthew G. Knepley 
3895793f3fe5SMatthew G. Knepley /*@
3896793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3897793f3fe5SMatthew G. Knepley 
3898793f3fe5SMatthew G. Knepley   Collective on DM
3899793f3fe5SMatthew G. Knepley 
3900793f3fe5SMatthew G. Knepley   Input Parameters:
3901793f3fe5SMatthew G. Knepley + dm - the DM
3902793f3fe5SMatthew G. Knepley - dim - the dimension
3903793f3fe5SMatthew G. Knepley 
3904793f3fe5SMatthew G. Knepley   Output Parameters:
3905793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3906793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3907793f3fe5SMatthew G. Knepley 
3908793f3fe5SMatthew G. Knepley   Note:
3909793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3910793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3911793f3fe5SMatthew G. Knepley   then the interval is empty.
3912793f3fe5SMatthew G. Knepley 
3913793f3fe5SMatthew G. Knepley   Level: intermediate
3914793f3fe5SMatthew G. Knepley 
3915793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3916793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3917793f3fe5SMatthew G. Knepley @*/
3918793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3919793f3fe5SMatthew G. Knepley {
3920793f3fe5SMatthew G. Knepley   PetscInt       d;
3921793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3922793f3fe5SMatthew G. Knepley 
3923793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3924793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3925793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3926793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3927793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3928793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3929793f3fe5SMatthew G. Knepley }
3930793f3fe5SMatthew G. Knepley 
39316636e97aSMatthew G Knepley /*@
39326636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
39336636e97aSMatthew G Knepley 
39346636e97aSMatthew G Knepley   Collective on DM
39356636e97aSMatthew G Knepley 
39366636e97aSMatthew G Knepley   Input Parameters:
39376636e97aSMatthew G Knepley + dm - the DM
39386636e97aSMatthew G Knepley - c - coordinate vector
39396636e97aSMatthew G Knepley 
39406636e97aSMatthew G Knepley   Note:
39416636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
39426636e97aSMatthew G Knepley 
39436636e97aSMatthew G Knepley   Level: intermediate
39446636e97aSMatthew G Knepley 
39456636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39466636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
39476636e97aSMatthew G Knepley @*/
39486636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
39496636e97aSMatthew G Knepley {
39506636e97aSMatthew G Knepley   PetscErrorCode ierr;
39516636e97aSMatthew G Knepley 
39526636e97aSMatthew G Knepley   PetscFunctionBegin;
39536636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39546636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39556636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39566636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39576636e97aSMatthew G Knepley   dm->coordinates = c;
39586636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3959b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
396003dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
39616636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39626636e97aSMatthew G Knepley }
39636636e97aSMatthew G Knepley 
39646636e97aSMatthew G Knepley /*@
39656636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
39666636e97aSMatthew G Knepley 
39676636e97aSMatthew G Knepley   Collective on DM
39686636e97aSMatthew G Knepley 
39696636e97aSMatthew G Knepley    Input Parameters:
39706636e97aSMatthew G Knepley +  dm - the DM
39716636e97aSMatthew G Knepley -  c - coordinate vector
39726636e97aSMatthew G Knepley 
39736636e97aSMatthew G Knepley   Note:
39746636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
39756636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
39766636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
39776636e97aSMatthew G Knepley 
39786636e97aSMatthew G Knepley   Level: intermediate
39796636e97aSMatthew G Knepley 
39806636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39816636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
39826636e97aSMatthew G Knepley @*/
39836636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
39846636e97aSMatthew G Knepley {
39856636e97aSMatthew G Knepley   PetscErrorCode ierr;
39866636e97aSMatthew G Knepley 
39876636e97aSMatthew G Knepley   PetscFunctionBegin;
39886636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39896636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39906636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39916636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
39928865f1eaSKarl Rupp 
39936636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
39948865f1eaSKarl Rupp 
39956636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39966636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39976636e97aSMatthew G Knepley }
39986636e97aSMatthew G Knepley 
39996636e97aSMatthew G Knepley /*@
40006636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
40016636e97aSMatthew G Knepley 
40026636e97aSMatthew G Knepley   Not Collective
40036636e97aSMatthew G Knepley 
40046636e97aSMatthew G Knepley   Input Parameter:
40056636e97aSMatthew G Knepley . dm - the DM
40066636e97aSMatthew G Knepley 
40076636e97aSMatthew G Knepley   Output Parameter:
40086636e97aSMatthew G Knepley . c - global coordinate vector
40096636e97aSMatthew G Knepley 
40106636e97aSMatthew G Knepley   Note:
40116636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40126636e97aSMatthew G Knepley 
40136636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
40146636e97aSMatthew G Knepley 
40156636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40166636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40176636e97aSMatthew G Knepley 
40186636e97aSMatthew G Knepley   Level: intermediate
40196636e97aSMatthew G Knepley 
40206636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40216636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
40226636e97aSMatthew G Knepley @*/
40236636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
40246636e97aSMatthew G Knepley {
40256636e97aSMatthew G Knepley   PetscErrorCode ierr;
40266636e97aSMatthew G Knepley 
40276636e97aSMatthew G Knepley   PetscFunctionBegin;
40286636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40296636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40301f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
40310298fd71SBarry Smith     DM cdm = NULL;
40326636e97aSMatthew G Knepley 
40336636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40346636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
40356636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
40366636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40376636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40386636e97aSMatthew G Knepley   }
40396636e97aSMatthew G Knepley   *c = dm->coordinates;
40406636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40416636e97aSMatthew G Knepley }
40426636e97aSMatthew G Knepley 
40436636e97aSMatthew G Knepley /*@
40446636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
40456636e97aSMatthew G Knepley 
40466636e97aSMatthew G Knepley   Collective on DM
40476636e97aSMatthew G Knepley 
40486636e97aSMatthew G Knepley   Input Parameter:
40496636e97aSMatthew G Knepley . dm - the DM
40506636e97aSMatthew G Knepley 
40516636e97aSMatthew G Knepley   Output Parameter:
40526636e97aSMatthew G Knepley . c - coordinate vector
40536636e97aSMatthew G Knepley 
40546636e97aSMatthew G Knepley   Note:
40556636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40566636e97aSMatthew G Knepley 
40576636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
40586636e97aSMatthew G Knepley 
40596636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40606636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40616636e97aSMatthew G Knepley 
40626636e97aSMatthew G Knepley   Level: intermediate
40636636e97aSMatthew G Knepley 
40646636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40656636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
40666636e97aSMatthew G Knepley @*/
40676636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
40686636e97aSMatthew G Knepley {
40696636e97aSMatthew G Knepley   PetscErrorCode ierr;
40706636e97aSMatthew G Knepley 
40716636e97aSMatthew G Knepley   PetscFunctionBegin;
40726636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40736636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40741f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
40750298fd71SBarry Smith     DM cdm = NULL;
40766636e97aSMatthew G Knepley 
40776636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40786636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
40796636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
40806636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
40816636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
40826636e97aSMatthew G Knepley   }
40836636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
40846636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40856636e97aSMatthew G Knepley }
40866636e97aSMatthew G Knepley 
40876636e97aSMatthew G Knepley /*@
40881cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
40896636e97aSMatthew G Knepley 
40906636e97aSMatthew G Knepley   Collective on DM
40916636e97aSMatthew G Knepley 
40926636e97aSMatthew G Knepley   Input Parameter:
40936636e97aSMatthew G Knepley . dm - the DM
40946636e97aSMatthew G Knepley 
40956636e97aSMatthew G Knepley   Output Parameter:
40966636e97aSMatthew G Knepley . cdm - coordinate DM
40976636e97aSMatthew G Knepley 
40986636e97aSMatthew G Knepley   Level: intermediate
40996636e97aSMatthew G Knepley 
41006636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41011cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
41026636e97aSMatthew G Knepley @*/
41036636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
41046636e97aSMatthew G Knepley {
41056636e97aSMatthew G Knepley   PetscErrorCode ierr;
41066636e97aSMatthew G Knepley 
41076636e97aSMatthew G Knepley   PetscFunctionBegin;
41086636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41096636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
41106636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
411182f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
41126636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
41136636e97aSMatthew G Knepley   }
41146636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
41156636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41166636e97aSMatthew G Knepley }
4117e87bb0d3SMatthew G Knepley 
41181cfe2091SMatthew G. Knepley /*@
41191cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
41201cfe2091SMatthew G. Knepley 
41211cfe2091SMatthew G. Knepley   Logically Collective on DM
41221cfe2091SMatthew G. Knepley 
41231cfe2091SMatthew G. Knepley   Input Parameters:
41241cfe2091SMatthew G. Knepley + dm - the DM
41251cfe2091SMatthew G. Knepley - cdm - coordinate DM
41261cfe2091SMatthew G. Knepley 
41271cfe2091SMatthew G. Knepley   Level: intermediate
41281cfe2091SMatthew G. Knepley 
41291cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41301cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
41311cfe2091SMatthew G. Knepley @*/
41321cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
41331cfe2091SMatthew G. Knepley {
41341cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
41351cfe2091SMatthew G. Knepley 
41361cfe2091SMatthew G. Knepley   PetscFunctionBegin;
41371cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41381cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4139f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
41401cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
41411cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
41421cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
41431cfe2091SMatthew G. Knepley }
41441cfe2091SMatthew G. Knepley 
414546e270d4SMatthew G. Knepley /*@
414646e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
414746e270d4SMatthew G. Knepley 
414846e270d4SMatthew G. Knepley   Not Collective
414946e270d4SMatthew G. Knepley 
415046e270d4SMatthew G. Knepley   Input Parameter:
415146e270d4SMatthew G. Knepley . dm - The DM object
415246e270d4SMatthew G. Knepley 
415346e270d4SMatthew G. Knepley   Output Parameter:
415446e270d4SMatthew G. Knepley . dim - The embedding dimension
415546e270d4SMatthew G. Knepley 
415646e270d4SMatthew G. Knepley   Level: intermediate
415746e270d4SMatthew G. Knepley 
415846e270d4SMatthew G. Knepley .keywords: mesh, coordinates
415946e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
416046e270d4SMatthew G. Knepley @*/
416146e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
416246e270d4SMatthew G. Knepley {
416346e270d4SMatthew G. Knepley   PetscFunctionBegin;
416446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
416546e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
41669a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
41679a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
41689a9a41abSToby Isaac   }
416946e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
417046e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
417146e270d4SMatthew G. Knepley }
417246e270d4SMatthew G. Knepley 
417346e270d4SMatthew G. Knepley /*@
417446e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
417546e270d4SMatthew G. Knepley 
417646e270d4SMatthew G. Knepley   Not Collective
417746e270d4SMatthew G. Knepley 
417846e270d4SMatthew G. Knepley   Input Parameters:
417946e270d4SMatthew G. Knepley + dm  - The DM object
418046e270d4SMatthew G. Knepley - dim - The embedding dimension
418146e270d4SMatthew G. Knepley 
418246e270d4SMatthew G. Knepley   Level: intermediate
418346e270d4SMatthew G. Knepley 
418446e270d4SMatthew G. Knepley .keywords: mesh, coordinates
418546e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
418646e270d4SMatthew G. Knepley @*/
418746e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
418846e270d4SMatthew G. Knepley {
418946e270d4SMatthew G. Knepley   PetscFunctionBegin;
419046e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
419146e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
419246e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
419346e270d4SMatthew G. Knepley }
419446e270d4SMatthew G. Knepley 
4195e8abe2deSMatthew G. Knepley /*@
4196e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4197e8abe2deSMatthew G. Knepley 
4198e8abe2deSMatthew G. Knepley   Not Collective
4199e8abe2deSMatthew G. Knepley 
4200e8abe2deSMatthew G. Knepley   Input Parameter:
4201e8abe2deSMatthew G. Knepley . dm - The DM object
4202e8abe2deSMatthew G. Knepley 
4203e8abe2deSMatthew G. Knepley   Output Parameter:
4204e8abe2deSMatthew G. Knepley . section - The PetscSection object
4205e8abe2deSMatthew G. Knepley 
4206e8abe2deSMatthew G. Knepley   Level: intermediate
4207e8abe2deSMatthew G. Knepley 
4208e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4209e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4210e8abe2deSMatthew G. Knepley @*/
4211e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4212e8abe2deSMatthew G. Knepley {
4213e8abe2deSMatthew G. Knepley   DM             cdm;
4214e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4215e8abe2deSMatthew G. Knepley 
4216e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4217e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4218e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4219e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4220e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4221e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4222e8abe2deSMatthew G. Knepley }
4223e8abe2deSMatthew G. Knepley 
4224e8abe2deSMatthew G. Knepley /*@
4225e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4226e8abe2deSMatthew G. Knepley 
4227e8abe2deSMatthew G. Knepley   Not Collective
4228e8abe2deSMatthew G. Knepley 
4229e8abe2deSMatthew G. Knepley   Input Parameters:
4230e8abe2deSMatthew G. Knepley + dm      - The DM object
423146e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4232e8abe2deSMatthew G. Knepley - section - The PetscSection object
4233e8abe2deSMatthew G. Knepley 
4234e8abe2deSMatthew G. Knepley   Level: intermediate
4235e8abe2deSMatthew G. Knepley 
4236e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4237e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4238e8abe2deSMatthew G. Knepley @*/
423946e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4240e8abe2deSMatthew G. Knepley {
4241e8abe2deSMatthew G. Knepley   DM             cdm;
4242e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4243e8abe2deSMatthew G. Knepley 
4244e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4245e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
424646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4247e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4248e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
424946e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
425046e270d4SMatthew G. Knepley     PetscInt d = dim;
425146e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
425246e270d4SMatthew G. Knepley 
425346e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
425446e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
425546e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
425646e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
425746e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
425846e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
425946e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
426046e270d4SMatthew G. Knepley     }
42616be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
426246e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
426346e270d4SMatthew G. Knepley   }
4264e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4265e8abe2deSMatthew G. Knepley }
4266e8abe2deSMatthew G. Knepley 
42675dc8c3f7SMatthew G. Knepley /*@C
4268*90b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
42695dc8c3f7SMatthew G. Knepley 
42705dc8c3f7SMatthew G. Knepley   Input Parameters:
4271*90b157c4SStefano Zampini . dm      - The DM object
4272*90b157c4SStefano Zampini 
4273*90b157c4SStefano Zampini   Output Parameters:
4274*90b157c4SStefano Zampini + per     - Whether the DM is periodic or not
42755dc8c3f7SMatthew 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
42765dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
42775dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
42785dc8c3f7SMatthew G. Knepley 
42795dc8c3f7SMatthew G. Knepley   Level: developer
42805dc8c3f7SMatthew G. Knepley 
42815dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
42825dc8c3f7SMatthew G. Knepley @*/
4283*90b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4284c6b900c6SMatthew G. Knepley {
4285c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4286c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4287*90b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4288c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4289c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
42905dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4291c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4292c6b900c6SMatthew G. Knepley }
4293c6b900c6SMatthew G. Knepley 
42945dc8c3f7SMatthew G. Knepley /*@C
42955dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
42965dc8c3f7SMatthew G. Knepley 
42975dc8c3f7SMatthew G. Knepley   Input Parameters:
42985dc8c3f7SMatthew G. Knepley + dm      - The DM object
4299*90b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
43005dc8c3f7SMatthew 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
43015dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
43025dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
43035dc8c3f7SMatthew G. Knepley 
43045dc8c3f7SMatthew G. Knepley   Level: developer
43055dc8c3f7SMatthew G. Knepley 
43065dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
43075dc8c3f7SMatthew G. Knepley @*/
4308*90b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4309c6b900c6SMatthew G. Knepley {
4310c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4311*90b157c4SStefano Zampini   PetscBool      useMaxCell = maxCell ? PETSC_TRUE : PETSC_FALSE;
4312c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4313c6b900c6SMatthew G. Knepley 
4314c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4315c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4316*90b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
4317*90b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,useMaxCell,3);
4318*90b157c4SStefano Zampini   if (maxCell) {
4319*90b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
4320*90b157c4SStefano Zampini     PetscValidPointer(L,4);
4321*90b157c4SStefano Zampini     PetscValidPointer(bd,5);
4322*90b157c4SStefano Zampini   }
43235dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
43245dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4325*90b157c4SStefano Zampini   if (maxCell) {
43265dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
43275dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4328*90b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
4329*90b157c4SStefano Zampini   } else {
4330*90b157c4SStefano Zampini     dm->periodic = per;
4331*90b157c4SStefano Zampini   }
4332c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4333c6b900c6SMatthew G. Knepley }
4334c6b900c6SMatthew G. Knepley 
43352e17dfb7SMatthew G. Knepley /*@
43362e17dfb7SMatthew 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.
43372e17dfb7SMatthew G. Knepley 
43382e17dfb7SMatthew G. Knepley   Input Parameters:
43392e17dfb7SMatthew G. Knepley + dm     - The DM
43402e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
43412e17dfb7SMatthew G. Knepley 
43422e17dfb7SMatthew G. Knepley   Output Parameter:
43432e17dfb7SMatthew G. Knepley . out - The localized coordinate point
43442e17dfb7SMatthew G. Knepley 
43452e17dfb7SMatthew G. Knepley   Level: developer
43462e17dfb7SMatthew G. Knepley 
43472e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
43482e17dfb7SMatthew G. Knepley @*/
43492e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscScalar out[])
43502e17dfb7SMatthew G. Knepley {
43512e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
43522e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
43532e17dfb7SMatthew G. Knepley 
43542e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
43552e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
43562e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
43572e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
43582e17dfb7SMatthew G. Knepley   } else {
43592e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
43602e17dfb7SMatthew G. Knepley       out[d] = in[d] - dm->L[d]*floor(PetscRealPart(in[d])/dm->L[d]);
43612e17dfb7SMatthew G. Knepley     }
43622e17dfb7SMatthew G. Knepley   }
43632e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
43642e17dfb7SMatthew G. Knepley }
43652e17dfb7SMatthew G. Knepley 
43662e17dfb7SMatthew G. Knepley /*
43672e17dfb7SMatthew 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.
43682e17dfb7SMatthew G. Knepley 
43692e17dfb7SMatthew G. Knepley   Input Parameters:
43702e17dfb7SMatthew G. Knepley + dm     - The DM
43712e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
43722e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
43732e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
43742e17dfb7SMatthew G. Knepley 
43752e17dfb7SMatthew G. Knepley   Output Parameter:
43762e17dfb7SMatthew G. Knepley . out - The localized coordinate point
43772e17dfb7SMatthew G. Knepley 
43782e17dfb7SMatthew G. Knepley   Level: developer
43792e17dfb7SMatthew G. Knepley 
43802e17dfb7SMatthew 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
43812e17dfb7SMatthew G. Knepley 
43822e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
43832e17dfb7SMatthew G. Knepley */
43842e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
43852e17dfb7SMatthew G. Knepley {
43862e17dfb7SMatthew G. Knepley   PetscInt d;
43872e17dfb7SMatthew G. Knepley 
43882e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
43892e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
43902e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
43912e17dfb7SMatthew G. Knepley   } else {
43922e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
43932e17dfb7SMatthew G. Knepley       if (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d]) {
43942e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
43952e17dfb7SMatthew G. Knepley       } else {
43962e17dfb7SMatthew G. Knepley         out[d] = in[d];
43972e17dfb7SMatthew G. Knepley       }
43982e17dfb7SMatthew G. Knepley     }
43992e17dfb7SMatthew G. Knepley   }
44002e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
44012e17dfb7SMatthew G. Knepley }
44022e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
44032e17dfb7SMatthew G. Knepley {
44042e17dfb7SMatthew G. Knepley   PetscInt d;
44052e17dfb7SMatthew G. Knepley 
44062e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
44072e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
44082e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
44092e17dfb7SMatthew G. Knepley   } else {
44102e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
44112e17dfb7SMatthew G. Knepley       if (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d]) {
44122e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
44132e17dfb7SMatthew G. Knepley       } else {
44142e17dfb7SMatthew G. Knepley         out[d] = in[d];
44152e17dfb7SMatthew G. Knepley       }
44162e17dfb7SMatthew G. Knepley     }
44172e17dfb7SMatthew G. Knepley   }
44182e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
44192e17dfb7SMatthew G. Knepley }
44202e17dfb7SMatthew G. Knepley 
44212e17dfb7SMatthew G. Knepley /*
44222e17dfb7SMatthew 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.
44232e17dfb7SMatthew G. Knepley 
44242e17dfb7SMatthew G. Knepley   Input Parameters:
44252e17dfb7SMatthew G. Knepley + dm     - The DM
44262e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
44272e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
44282e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
44292e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
44302e17dfb7SMatthew G. Knepley 
44312e17dfb7SMatthew G. Knepley   Output Parameter:
44322e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
44332e17dfb7SMatthew G. Knepley 
44342e17dfb7SMatthew G. Knepley   Level: developer
44352e17dfb7SMatthew G. Knepley 
44362e17dfb7SMatthew 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
44372e17dfb7SMatthew G. Knepley 
44382e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
44392e17dfb7SMatthew G. Knepley */
44402e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
44412e17dfb7SMatthew G. Knepley {
44422e17dfb7SMatthew G. Knepley   PetscInt d;
44432e17dfb7SMatthew G. Knepley 
44442e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
44452e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
44462e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
44472e17dfb7SMatthew G. Knepley   } else {
44482e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
44492e17dfb7SMatthew G. Knepley       if (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d]) {
44502e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
44512e17dfb7SMatthew G. Knepley       } else {
44522e17dfb7SMatthew G. Knepley         out[d] += in[d];
44532e17dfb7SMatthew G. Knepley       }
44542e17dfb7SMatthew G. Knepley     }
44552e17dfb7SMatthew G. Knepley   }
44562e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
44572e17dfb7SMatthew G. Knepley }
44582e17dfb7SMatthew G. Knepley 
44592e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexGetDepthStratum(DM, PetscInt, PetscInt *, PetscInt *);
44602e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexGetHeightStratum(DM, PetscInt, PetscInt *, PetscInt *);
44612e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexVecGetClosure(DM, PetscSection, Vec, PetscInt, PetscInt *, PetscScalar *[]);
44622e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexVecRestoreClosure(DM, PetscSection, Vec, PetscInt, PetscInt *, PetscScalar *[]);
44632e17dfb7SMatthew G. Knepley 
446436447a5eSToby Isaac /*@
446536447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
446636447a5eSToby Isaac 
446736447a5eSToby Isaac   Input Parameter:
446836447a5eSToby Isaac . dm - The DM
446936447a5eSToby Isaac 
447036447a5eSToby Isaac   Output Parameter:
447136447a5eSToby Isaac   areLocalized - True if localized
447236447a5eSToby Isaac 
447336447a5eSToby Isaac   Level: developer
447436447a5eSToby Isaac 
447536447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
447636447a5eSToby Isaac @*/
447736447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
447836447a5eSToby Isaac {
447936447a5eSToby Isaac   DM             cdm;
448036447a5eSToby Isaac   PetscSection   coordSection;
448136447a5eSToby Isaac   PetscInt       cStart, cEnd, c, sStart, sEnd, dof;
448236447a5eSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
448336447a5eSToby Isaac   PetscErrorCode ierr;
448436447a5eSToby Isaac 
448536447a5eSToby Isaac   PetscFunctionBegin;
448636447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44878b09590cSToby Isaac   if (!dm->maxCell) {
44888b09590cSToby Isaac     *areLocalized = PETSC_FALSE;
44898b09590cSToby Isaac     PetscFunctionReturn(0);
44908b09590cSToby Isaac   }
449136447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
449236447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
449336447a5eSToby Isaac   {
449436447a5eSToby Isaac     PetscBool isplex;
449536447a5eSToby Isaac 
449636447a5eSToby Isaac     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
449736447a5eSToby Isaac     if (isplex) {
449836447a5eSToby Isaac       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
449936447a5eSToby Isaac     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
450036447a5eSToby Isaac   }
450136447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
450236447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
45035a42012cSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_FALSE;
450436447a5eSToby Isaac   for (c = cStart; c < cEnd; ++c) {
450536447a5eSToby Isaac     if (c < sStart || c >= sEnd) {
450636447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
450736447a5eSToby Isaac       break;
450836447a5eSToby Isaac     }
450936447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
45105a42012cSToby Isaac     if (dof) {
45115a42012cSToby Isaac       alreadyLocalized = PETSC_TRUE;
451236447a5eSToby Isaac       break;
451336447a5eSToby Isaac     }
451436447a5eSToby Isaac   }
45155a42012cSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
451636447a5eSToby Isaac   *areLocalized = alreadyLocalizedGlobal;
451736447a5eSToby Isaac   PetscFunctionReturn(0);
451836447a5eSToby Isaac }
451936447a5eSToby Isaac 
452036447a5eSToby Isaac 
45212e17dfb7SMatthew G. Knepley /*@
45222e17dfb7SMatthew G. Knepley   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for each cell
45232e17dfb7SMatthew G. Knepley 
45242e17dfb7SMatthew G. Knepley   Input Parameter:
45252e17dfb7SMatthew G. Knepley . dm - The DM
45262e17dfb7SMatthew G. Knepley 
45272e17dfb7SMatthew G. Knepley   Level: developer
45282e17dfb7SMatthew G. Knepley 
45292e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
45302e17dfb7SMatthew G. Knepley @*/
45312e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
45322e17dfb7SMatthew G. Knepley {
45332e17dfb7SMatthew G. Knepley   DM             cdm;
45342e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
45352e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
45363e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
45373e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4538e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
45393e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
45403e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
45412e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
45422e17dfb7SMatthew G. Knepley 
45432e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45442e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45452e17dfb7SMatthew G. Knepley   if (!dm->maxCell) PetscFunctionReturn(0);
45462e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
45472e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
45482e17dfb7SMatthew G. Knepley   {
45492e17dfb7SMatthew G. Knepley     PetscBool isplex;
45502e17dfb7SMatthew G. Knepley 
45512e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
45522e17dfb7SMatthew G. Knepley     if (isplex) {
45532e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
45543e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
45553e922f36SToby Isaac       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
45563e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
45573e922f36SToby Isaac       newStart = vStart;
45583e922f36SToby Isaac       newEnd   = vEnd;
45593e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
45603e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
45613e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
45623e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
45633e922f36SToby Isaac       }
45642e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
45652e17dfb7SMatthew G. Knepley   }
45662e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
45672e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
45683e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
4569e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
45703e922f36SToby Isaac 
45712e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
45722e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
45732e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
45742e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
45753e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
45763e922f36SToby Isaac 
45773e922f36SToby Isaac   ierr = DMGetWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
45783e922f36SToby Isaac   localized = &anchor[bs];
45793e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
45803e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
45813e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
45823e922f36SToby Isaac 
45833e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
45843e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
45853e922f36SToby Isaac       PetscInt     b;
45863e922f36SToby Isaac 
45873e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
45883e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
45893e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
45903e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
45913e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
45923e922f36SToby Isaac         for (b = 0; b < bs; b++) {
45933e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
45943e922f36SToby Isaac         }
45953e922f36SToby Isaac         if (b < bs) break;
45963e922f36SToby Isaac       }
45973e922f36SToby Isaac       if (d < dof/bs) {
45983e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
45993e922f36SToby Isaac           PetscInt cdof;
46003e922f36SToby Isaac 
46013e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
46023e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
46033e922f36SToby Isaac         }
46043e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
46053e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
46063e922f36SToby Isaac       }
46073e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
46083e922f36SToby Isaac     }
46093e922f36SToby Isaac   }
46103e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
46113e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
46123e922f36SToby Isaac     ierr = DMRestoreWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
46133e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
46143e922f36SToby Isaac     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
46153e922f36SToby Isaac     PetscFunctionReturn(0);
46163e922f36SToby Isaac   }
46172e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
46182e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
46192e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
46202e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
46212e17dfb7SMatthew G. Knepley   }
46222e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
46232e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
4624c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
46252e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
46262e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
46272e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
46282e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
4629c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
46302e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
46312e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
46322e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
46332e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
46342e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
46352e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
46362e17dfb7SMatthew G. Knepley   }
46373e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
46383e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
46393e922f36SToby Isaac 
46402e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
46412e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
46423e922f36SToby Isaac       PetscInt     b, cdof;
46432e17dfb7SMatthew G. Knepley 
46443e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
46453e922f36SToby Isaac       if (!cdof) continue;
46462e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
46472e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
46482e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
46492e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
46502e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
46512e17dfb7SMatthew G. Knepley     }
46523e922f36SToby Isaac   }
46533e922f36SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
46543e922f36SToby Isaac   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
4655c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
46562e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
46572e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
46582e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
46592e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
46602e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
46612e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46622e17dfb7SMatthew G. Knepley }
46632e17dfb7SMatthew G. Knepley 
4664e87bb0d3SMatthew G Knepley /*@
46653a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
4666e87bb0d3SMatthew G Knepley 
46673a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
4668e87bb0d3SMatthew G Knepley 
4669e87bb0d3SMatthew G Knepley   Input Parameters:
4670e87bb0d3SMatthew G Knepley + dm - The DM
46713a93e3b7SToby Isaac . v - The Vec of points
467262a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
46733a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
4674e87bb0d3SMatthew G Knepley 
467561e3bb9bSMatthew G Knepley   Output Parameter:
467662a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
467762a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
46783a93e3b7SToby Isaac 
4679e87bb0d3SMatthew G Knepley 
4680e87bb0d3SMatthew G Knepley   Level: developer
468161e3bb9bSMatthew G Knepley 
468262a38674SMatthew G. Knepley   Notes:
46833a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
468462a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
46853a93e3b7SToby Isaac 
46863a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
468762a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
46883a93e3b7SToby Isaac 
46893a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
46903a93e3b7SToby Isaac 
469162a38674SMatthew G. Knepley $    const PetscSFNode *cells;
469262a38674SMatthew G. Knepley $    PetscInt           nFound;
469362a38674SMatthew G. Knepley $    const PetscSFNode *found;
469462a38674SMatthew G. Knepley $
469562a38674SMatthew G. Knepley $    PetscSFGetGraph(cells,NULL,&nFound,&found,&cells);
46963a93e3b7SToby Isaac 
46973a93e3b7SToby 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
46983a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
46993a93e3b7SToby Isaac 
470061e3bb9bSMatthew G Knepley .keywords: point location, mesh
470162a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
470261e3bb9bSMatthew G Knepley @*/
470362a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
4704e87bb0d3SMatthew G Knepley {
4705735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4706735aa83eSMatthew G Knepley 
4707e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4708e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4709e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4710e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
47113a93e3b7SToby Isaac   if (*cellSF) {
47123a93e3b7SToby Isaac     PetscMPIInt result;
47133a93e3b7SToby Isaac 
4714e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
47153a93e3b7SToby Isaac     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)cellSF),&result);CHKERRQ(ierr);
47163a93e3b7SToby 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");
4717e0fc9d1bSMatthew G. Knepley   } else {
47183a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
47193a93e3b7SToby Isaac   }
472047a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4721735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
472262a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
472382f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
472447a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4725e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4726e87bb0d3SMatthew G Knepley }
472714f150ffSMatthew G. Knepley 
4728f4d763aaSMatthew G. Knepley /*@
4729f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4730f4d763aaSMatthew G. Knepley 
4731f4d763aaSMatthew G. Knepley   Input Parameter:
4732f4d763aaSMatthew G. Knepley . dm - The original DM
4733f4d763aaSMatthew G. Knepley 
4734f4d763aaSMatthew G. Knepley   Output Parameter:
4735f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4736f4d763aaSMatthew G. Knepley 
4737f4d763aaSMatthew G. Knepley   Level: intermediate
4738f4d763aaSMatthew G. Knepley 
4739f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4740f4d763aaSMatthew G. Knepley @*/
474114f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
474214f150ffSMatthew G. Knepley {
4743c26acbdeSMatthew G. Knepley   PetscSection   section;
47442d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
474514f150ffSMatthew G. Knepley   PetscErrorCode ierr;
474614f150ffSMatthew G. Knepley 
474714f150ffSMatthew G. Knepley   PetscFunctionBegin;
474814f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
474914f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4750c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4751c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4752127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
47532d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
4754c26acbdeSMatthew G. Knepley     *odm = dm;
4755c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4756c26acbdeSMatthew G. Knepley   }
475714f150ffSMatthew G. Knepley   if (!dm->dmBC) {
47580305aff6SMatthew G. Knepley     PetscDS      ds;
4759c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
476014f150ffSMatthew G. Knepley     PetscSF      sf;
476114f150ffSMatthew G. Knepley 
476214f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
47630305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
47640305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
476514f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
476614f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
476714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
476814f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
476915b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
477014f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
477114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
477214f150ffSMatthew G. Knepley   }
477314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
477414f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
477514f150ffSMatthew G. Knepley }
4776f4d763aaSMatthew G. Knepley 
4777f4d763aaSMatthew G. Knepley /*@
4778cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4779f4d763aaSMatthew G. Knepley 
4780f4d763aaSMatthew G. Knepley   Input Parameter:
4781f4d763aaSMatthew G. Knepley . dm - The original DM
4782f4d763aaSMatthew G. Knepley 
4783cdb7a50dSMatthew G. Knepley   Output Parameters:
4784cdb7a50dSMatthew G. Knepley + num - The output sequence number
4785cdb7a50dSMatthew G. Knepley - val - The output sequence value
4786f4d763aaSMatthew G. Knepley 
4787f4d763aaSMatthew G. Knepley   Level: intermediate
4788f4d763aaSMatthew G. Knepley 
4789f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4790f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4791f4d763aaSMatthew G. Knepley 
4792f4d763aaSMatthew G. Knepley .seealso: VecView()
4793f4d763aaSMatthew G. Knepley @*/
4794cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4795f4d763aaSMatthew G. Knepley {
4796f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4797f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4798cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4799cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4800f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4801f4d763aaSMatthew G. Knepley }
4802f4d763aaSMatthew G. Knepley 
4803f4d763aaSMatthew G. Knepley /*@
4804cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4805f4d763aaSMatthew G. Knepley 
4806f4d763aaSMatthew G. Knepley   Input Parameters:
4807f4d763aaSMatthew G. Knepley + dm - The original DM
4808cdb7a50dSMatthew G. Knepley . num - The output sequence number
4809cdb7a50dSMatthew G. Knepley - val - The output sequence value
4810f4d763aaSMatthew G. Knepley 
4811f4d763aaSMatthew G. Knepley   Level: intermediate
4812f4d763aaSMatthew G. Knepley 
4813f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4814f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4815f4d763aaSMatthew G. Knepley 
4816f4d763aaSMatthew G. Knepley .seealso: VecView()
4817f4d763aaSMatthew G. Knepley @*/
4818cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4819f4d763aaSMatthew G. Knepley {
4820f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4821f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4822f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4823cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4824cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4825cdb7a50dSMatthew G. Knepley }
4826cdb7a50dSMatthew G. Knepley 
4827cdb7a50dSMatthew G. Knepley /*@C
4828cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4829cdb7a50dSMatthew G. Knepley 
4830cdb7a50dSMatthew G. Knepley   Input Parameters:
4831cdb7a50dSMatthew G. Knepley + dm   - The original DM
4832cdb7a50dSMatthew G. Knepley . name - The sequence name
4833cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4834cdb7a50dSMatthew G. Knepley 
4835cdb7a50dSMatthew G. Knepley   Output Parameter:
4836cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4837cdb7a50dSMatthew G. Knepley 
4838cdb7a50dSMatthew G. Knepley   Level: intermediate
4839cdb7a50dSMatthew G. Knepley 
4840cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4841cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4842cdb7a50dSMatthew G. Knepley 
4843cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4844cdb7a50dSMatthew G. Knepley @*/
4845cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4846cdb7a50dSMatthew G. Knepley {
4847cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4848cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4849cdb7a50dSMatthew G. Knepley 
4850cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4851cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4852cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4853cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4854cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4855cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4856cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4857cdb7a50dSMatthew G. Knepley     PetscScalar value;
4858cdb7a50dSMatthew G. Knepley 
485939d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
48604aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4861cdb7a50dSMatthew G. Knepley #endif
4862cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4863f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4864f4d763aaSMatthew G. Knepley }
48658e4ac7eaSMatthew G. Knepley 
48668e4ac7eaSMatthew G. Knepley /*@
48678e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
48688e4ac7eaSMatthew G. Knepley 
48698e4ac7eaSMatthew G. Knepley   Not collective
48708e4ac7eaSMatthew G. Knepley 
48718e4ac7eaSMatthew G. Knepley   Input Parameter:
48728e4ac7eaSMatthew G. Knepley . dm - The DM
48738e4ac7eaSMatthew G. Knepley 
48748e4ac7eaSMatthew G. Knepley   Output Parameter:
48758e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
48768e4ac7eaSMatthew G. Knepley 
48778e4ac7eaSMatthew G. Knepley   Level: beginner
48788e4ac7eaSMatthew G. Knepley 
48798e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
48808e4ac7eaSMatthew G. Knepley @*/
48818e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
48828e4ac7eaSMatthew G. Knepley {
48838e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
48848e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48858e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
48868e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
48878e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
48888e4ac7eaSMatthew G. Knepley }
48898e4ac7eaSMatthew G. Knepley 
48908e4ac7eaSMatthew G. Knepley /*@
48918e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
48928e4ac7eaSMatthew G. Knepley 
48938e4ac7eaSMatthew G. Knepley   Collective on dm
48948e4ac7eaSMatthew G. Knepley 
48958e4ac7eaSMatthew G. Knepley   Input Parameters:
48968e4ac7eaSMatthew G. Knepley + dm - The DM
48978e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
48988e4ac7eaSMatthew G. Knepley 
48998e4ac7eaSMatthew G. Knepley   Level: beginner
49008e4ac7eaSMatthew G. Knepley 
49018e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
49028e4ac7eaSMatthew G. Knepley @*/
49038e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
49048e4ac7eaSMatthew G. Knepley {
49058e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
49068e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49078e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
49088e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
49098e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
49108e4ac7eaSMatthew G. Knepley }
4911c58f1c22SToby Isaac 
4912c58f1c22SToby Isaac 
4913c58f1c22SToby Isaac /*@C
4914c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
4915c58f1c22SToby Isaac 
4916c58f1c22SToby Isaac   Not Collective
4917c58f1c22SToby Isaac 
4918c58f1c22SToby Isaac   Input Parameters:
4919c58f1c22SToby Isaac + dm   - The DM object
4920c58f1c22SToby Isaac - name - The label name
4921c58f1c22SToby Isaac 
4922c58f1c22SToby Isaac   Level: intermediate
4923c58f1c22SToby Isaac 
4924c58f1c22SToby Isaac .keywords: mesh
4925c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
4926c58f1c22SToby Isaac @*/
4927c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
4928c58f1c22SToby Isaac {
4929c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
4930c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
4931c58f1c22SToby Isaac   PetscErrorCode ierr;
4932c58f1c22SToby Isaac 
4933c58f1c22SToby Isaac   PetscFunctionBegin;
4934c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4935c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4936c58f1c22SToby Isaac   while (next) {
4937c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
4938c58f1c22SToby Isaac     if (flg) break;
4939c58f1c22SToby Isaac     next = next->next;
4940c58f1c22SToby Isaac   }
4941c58f1c22SToby Isaac   if (!flg) {
4942c58f1c22SToby Isaac     DMLabelLink tmpLabel;
4943c58f1c22SToby Isaac 
4944c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
4945c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
4946c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
4947c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
4948c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
4949c58f1c22SToby Isaac   }
4950c58f1c22SToby Isaac   PetscFunctionReturn(0);
4951c58f1c22SToby Isaac }
4952c58f1c22SToby Isaac 
4953c58f1c22SToby Isaac /*@C
4954c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
4955c58f1c22SToby Isaac 
4956c58f1c22SToby Isaac   Not Collective
4957c58f1c22SToby Isaac 
4958c58f1c22SToby Isaac   Input Parameters:
4959c58f1c22SToby Isaac + dm   - The DM object
4960c58f1c22SToby Isaac . name - The label name
4961c58f1c22SToby Isaac - point - The mesh point
4962c58f1c22SToby Isaac 
4963c58f1c22SToby Isaac   Output Parameter:
4964c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
4965c58f1c22SToby Isaac 
4966c58f1c22SToby Isaac   Level: beginner
4967c58f1c22SToby Isaac 
4968c58f1c22SToby Isaac .keywords: mesh
4969c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
4970c58f1c22SToby Isaac @*/
4971c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
4972c58f1c22SToby Isaac {
4973c58f1c22SToby Isaac   DMLabel        label;
4974c58f1c22SToby Isaac   PetscErrorCode ierr;
4975c58f1c22SToby Isaac 
4976c58f1c22SToby Isaac   PetscFunctionBegin;
4977c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4978c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
4979c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
4980c58f1c22SToby Isaac   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);CHKERRQ(ierr);
4981c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
4982c58f1c22SToby Isaac   PetscFunctionReturn(0);
4983c58f1c22SToby Isaac }
4984c58f1c22SToby Isaac 
4985c58f1c22SToby Isaac /*@C
4986c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
4987c58f1c22SToby Isaac 
4988c58f1c22SToby Isaac   Not Collective
4989c58f1c22SToby Isaac 
4990c58f1c22SToby Isaac   Input Parameters:
4991c58f1c22SToby Isaac + dm   - The DM object
4992c58f1c22SToby Isaac . name - The label name
4993c58f1c22SToby Isaac . point - The mesh point
4994c58f1c22SToby Isaac - value - The label value for this point
4995c58f1c22SToby Isaac 
4996c58f1c22SToby Isaac   Output Parameter:
4997c58f1c22SToby Isaac 
4998c58f1c22SToby Isaac   Level: beginner
4999c58f1c22SToby Isaac 
5000c58f1c22SToby Isaac .keywords: mesh
5001c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5002c58f1c22SToby Isaac @*/
5003c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5004c58f1c22SToby Isaac {
5005c58f1c22SToby Isaac   DMLabel        label;
5006c58f1c22SToby Isaac   PetscErrorCode ierr;
5007c58f1c22SToby Isaac 
5008c58f1c22SToby Isaac   PetscFunctionBegin;
5009c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5010c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5011c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5012c58f1c22SToby Isaac   if (!label) {
5013c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5014c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5015c58f1c22SToby Isaac   }
5016c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5017c58f1c22SToby Isaac   PetscFunctionReturn(0);
5018c58f1c22SToby Isaac }
5019c58f1c22SToby Isaac 
5020c58f1c22SToby Isaac /*@C
5021c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5022c58f1c22SToby Isaac 
5023c58f1c22SToby Isaac   Not Collective
5024c58f1c22SToby Isaac 
5025c58f1c22SToby Isaac   Input Parameters:
5026c58f1c22SToby Isaac + dm   - The DM object
5027c58f1c22SToby Isaac . name - The label name
5028c58f1c22SToby Isaac . point - The mesh point
5029c58f1c22SToby Isaac - value - The label value for this point
5030c58f1c22SToby Isaac 
5031c58f1c22SToby Isaac   Output Parameter:
5032c58f1c22SToby Isaac 
5033c58f1c22SToby Isaac   Level: beginner
5034c58f1c22SToby Isaac 
5035c58f1c22SToby Isaac .keywords: mesh
5036c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5037c58f1c22SToby Isaac @*/
5038c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5039c58f1c22SToby Isaac {
5040c58f1c22SToby Isaac   DMLabel        label;
5041c58f1c22SToby Isaac   PetscErrorCode ierr;
5042c58f1c22SToby Isaac 
5043c58f1c22SToby Isaac   PetscFunctionBegin;
5044c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5045c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5046c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5047c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5048c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5049c58f1c22SToby Isaac   PetscFunctionReturn(0);
5050c58f1c22SToby Isaac }
5051c58f1c22SToby Isaac 
5052c58f1c22SToby Isaac /*@C
5053c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5054c58f1c22SToby Isaac 
5055c58f1c22SToby Isaac   Not Collective
5056c58f1c22SToby Isaac 
5057c58f1c22SToby Isaac   Input Parameters:
5058c58f1c22SToby Isaac + dm   - The DM object
5059c58f1c22SToby Isaac - name - The label name
5060c58f1c22SToby Isaac 
5061c58f1c22SToby Isaac   Output Parameter:
5062c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5063c58f1c22SToby Isaac 
5064c58f1c22SToby Isaac   Level: beginner
5065c58f1c22SToby Isaac 
5066c58f1c22SToby Isaac .keywords: mesh
5067c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5068c58f1c22SToby Isaac @*/
5069c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5070c58f1c22SToby Isaac {
5071c58f1c22SToby Isaac   DMLabel        label;
5072c58f1c22SToby Isaac   PetscErrorCode ierr;
5073c58f1c22SToby Isaac 
5074c58f1c22SToby Isaac   PetscFunctionBegin;
5075c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5076c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5077c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5078c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5079c58f1c22SToby Isaac   *size = 0;
5080c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5081c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5082c58f1c22SToby Isaac   PetscFunctionReturn(0);
5083c58f1c22SToby Isaac }
5084c58f1c22SToby Isaac 
5085c58f1c22SToby Isaac /*@C
5086c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5087c58f1c22SToby Isaac 
5088c58f1c22SToby Isaac   Not Collective
5089c58f1c22SToby Isaac 
5090c58f1c22SToby Isaac   Input Parameters:
5091c58f1c22SToby Isaac + mesh - The DM object
5092c58f1c22SToby Isaac - name - The label name
5093c58f1c22SToby Isaac 
5094c58f1c22SToby Isaac   Output Parameter:
5095c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5096c58f1c22SToby Isaac 
5097c58f1c22SToby Isaac   Level: beginner
5098c58f1c22SToby Isaac 
5099c58f1c22SToby Isaac .keywords: mesh
5100c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5101c58f1c22SToby Isaac @*/
5102c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5103c58f1c22SToby Isaac {
5104c58f1c22SToby Isaac   DMLabel        label;
5105c58f1c22SToby Isaac   PetscErrorCode ierr;
5106c58f1c22SToby Isaac 
5107c58f1c22SToby Isaac   PetscFunctionBegin;
5108c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5109c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5110c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5111c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5112c58f1c22SToby Isaac   *ids = NULL;
5113c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5114c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5115c58f1c22SToby Isaac   PetscFunctionReturn(0);
5116c58f1c22SToby Isaac }
5117c58f1c22SToby Isaac 
5118c58f1c22SToby Isaac /*@C
5119c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5120c58f1c22SToby Isaac 
5121c58f1c22SToby Isaac   Not Collective
5122c58f1c22SToby Isaac 
5123c58f1c22SToby Isaac   Input Parameters:
5124c58f1c22SToby Isaac + dm - The DM object
5125c58f1c22SToby Isaac . name - The label name
5126c58f1c22SToby Isaac - value - The stratum value
5127c58f1c22SToby Isaac 
5128c58f1c22SToby Isaac   Output Parameter:
5129c58f1c22SToby Isaac . size - The stratum size
5130c58f1c22SToby Isaac 
5131c58f1c22SToby Isaac   Level: beginner
5132c58f1c22SToby Isaac 
5133c58f1c22SToby Isaac .keywords: mesh
5134c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5135c58f1c22SToby Isaac @*/
5136c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5137c58f1c22SToby Isaac {
5138c58f1c22SToby Isaac   DMLabel        label;
5139c58f1c22SToby Isaac   PetscErrorCode ierr;
5140c58f1c22SToby Isaac 
5141c58f1c22SToby Isaac   PetscFunctionBegin;
5142c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5143c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5144c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5145c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5146c58f1c22SToby Isaac   *size = 0;
5147c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5148c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5149c58f1c22SToby Isaac   PetscFunctionReturn(0);
5150c58f1c22SToby Isaac }
5151c58f1c22SToby Isaac 
5152c58f1c22SToby Isaac /*@C
5153c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5154c58f1c22SToby Isaac 
5155c58f1c22SToby Isaac   Not Collective
5156c58f1c22SToby Isaac 
5157c58f1c22SToby Isaac   Input Parameters:
5158c58f1c22SToby Isaac + dm - The DM object
5159c58f1c22SToby Isaac . name - The label name
5160c58f1c22SToby Isaac - value - The stratum value
5161c58f1c22SToby Isaac 
5162c58f1c22SToby Isaac   Output Parameter:
5163c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5164c58f1c22SToby Isaac 
5165c58f1c22SToby Isaac   Level: beginner
5166c58f1c22SToby Isaac 
5167c58f1c22SToby Isaac .keywords: mesh
5168c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5169c58f1c22SToby Isaac @*/
5170c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5171c58f1c22SToby Isaac {
5172c58f1c22SToby Isaac   DMLabel        label;
5173c58f1c22SToby Isaac   PetscErrorCode ierr;
5174c58f1c22SToby Isaac 
5175c58f1c22SToby Isaac   PetscFunctionBegin;
5176c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5177c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5178c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5179c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5180c58f1c22SToby Isaac   *points = NULL;
5181c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5182c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5183c58f1c22SToby Isaac   PetscFunctionReturn(0);
5184c58f1c22SToby Isaac }
5185c58f1c22SToby Isaac 
51864de306b1SToby Isaac /*@C
51874de306b1SToby Isaac   DMGetStratumIS - Set the points in a label stratum
51884de306b1SToby Isaac 
51894de306b1SToby Isaac   Not Collective
51904de306b1SToby Isaac 
51914de306b1SToby Isaac   Input Parameters:
51924de306b1SToby Isaac + dm - The DM object
51934de306b1SToby Isaac . name - The label name
51944de306b1SToby Isaac . value - The stratum value
51954de306b1SToby Isaac - points - The stratum points
51964de306b1SToby Isaac 
51974de306b1SToby Isaac   Level: beginner
51984de306b1SToby Isaac 
51994de306b1SToby Isaac .keywords: mesh
52004de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
52014de306b1SToby Isaac @*/
52024de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
52034de306b1SToby Isaac {
52044de306b1SToby Isaac   DMLabel        label;
52054de306b1SToby Isaac   PetscErrorCode ierr;
52064de306b1SToby Isaac 
52074de306b1SToby Isaac   PetscFunctionBegin;
52084de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52094de306b1SToby Isaac   PetscValidCharPointer(name, 2);
52104de306b1SToby Isaac   PetscValidPointer(points, 4);
52114de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
52124de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
52134de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
52144de306b1SToby Isaac   PetscFunctionReturn(0);
52154de306b1SToby Isaac }
52164de306b1SToby Isaac 
5217c58f1c22SToby Isaac /*@C
5218c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5219c58f1c22SToby Isaac 
5220c58f1c22SToby Isaac   Not Collective
5221c58f1c22SToby Isaac 
5222c58f1c22SToby Isaac   Input Parameters:
5223c58f1c22SToby Isaac + dm   - The DM object
5224c58f1c22SToby Isaac . name - The label name
5225c58f1c22SToby Isaac - value - The label value for this point
5226c58f1c22SToby Isaac 
5227c58f1c22SToby Isaac   Output Parameter:
5228c58f1c22SToby Isaac 
5229c58f1c22SToby Isaac   Level: beginner
5230c58f1c22SToby Isaac 
5231c58f1c22SToby Isaac .keywords: mesh
5232c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5233c58f1c22SToby Isaac @*/
5234c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5235c58f1c22SToby Isaac {
5236c58f1c22SToby Isaac   DMLabel        label;
5237c58f1c22SToby Isaac   PetscErrorCode ierr;
5238c58f1c22SToby Isaac 
5239c58f1c22SToby Isaac   PetscFunctionBegin;
5240c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5241c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5242c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5243c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5244c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5245c58f1c22SToby Isaac   PetscFunctionReturn(0);
5246c58f1c22SToby Isaac }
5247c58f1c22SToby Isaac 
5248c58f1c22SToby Isaac /*@
5249c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5250c58f1c22SToby Isaac 
5251c58f1c22SToby Isaac   Not Collective
5252c58f1c22SToby Isaac 
5253c58f1c22SToby Isaac   Input Parameter:
5254c58f1c22SToby Isaac . dm   - The DM object
5255c58f1c22SToby Isaac 
5256c58f1c22SToby Isaac   Output Parameter:
5257c58f1c22SToby Isaac . numLabels - the number of Labels
5258c58f1c22SToby Isaac 
5259c58f1c22SToby Isaac   Level: intermediate
5260c58f1c22SToby Isaac 
5261c58f1c22SToby Isaac .keywords: mesh
5262c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5263c58f1c22SToby Isaac @*/
5264c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5265c58f1c22SToby Isaac {
5266c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5267c58f1c22SToby Isaac   PetscInt  n    = 0;
5268c58f1c22SToby Isaac 
5269c58f1c22SToby Isaac   PetscFunctionBegin;
5270c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5271c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5272c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5273c58f1c22SToby Isaac   *numLabels = n;
5274c58f1c22SToby Isaac   PetscFunctionReturn(0);
5275c58f1c22SToby Isaac }
5276c58f1c22SToby Isaac 
5277c58f1c22SToby Isaac /*@C
5278c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5279c58f1c22SToby Isaac 
5280c58f1c22SToby Isaac   Not Collective
5281c58f1c22SToby Isaac 
5282c58f1c22SToby Isaac   Input Parameters:
5283c58f1c22SToby Isaac + dm - The DM object
5284c58f1c22SToby Isaac - n  - the label number
5285c58f1c22SToby Isaac 
5286c58f1c22SToby Isaac   Output Parameter:
5287c58f1c22SToby Isaac . name - the label name
5288c58f1c22SToby Isaac 
5289c58f1c22SToby Isaac   Level: intermediate
5290c58f1c22SToby Isaac 
5291c58f1c22SToby Isaac .keywords: mesh
5292c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5293c58f1c22SToby Isaac @*/
5294c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5295c58f1c22SToby Isaac {
5296c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5297c58f1c22SToby Isaac   PetscInt  l    = 0;
5298c58f1c22SToby Isaac 
5299c58f1c22SToby Isaac   PetscFunctionBegin;
5300c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5301c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5302c58f1c22SToby Isaac   while (next) {
5303c58f1c22SToby Isaac     if (l == n) {
5304c58f1c22SToby Isaac       *name = next->label->name;
5305c58f1c22SToby Isaac       PetscFunctionReturn(0);
5306c58f1c22SToby Isaac     }
5307c58f1c22SToby Isaac     ++l;
5308c58f1c22SToby Isaac     next = next->next;
5309c58f1c22SToby Isaac   }
5310c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5311c58f1c22SToby Isaac }
5312c58f1c22SToby Isaac 
5313c58f1c22SToby Isaac /*@C
5314c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5315c58f1c22SToby Isaac 
5316c58f1c22SToby Isaac   Not Collective
5317c58f1c22SToby Isaac 
5318c58f1c22SToby Isaac   Input Parameters:
5319c58f1c22SToby Isaac + dm   - The DM object
5320c58f1c22SToby Isaac - name - The label name
5321c58f1c22SToby Isaac 
5322c58f1c22SToby Isaac   Output Parameter:
5323c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5324c58f1c22SToby Isaac 
5325c58f1c22SToby Isaac   Level: intermediate
5326c58f1c22SToby Isaac 
5327c58f1c22SToby Isaac .keywords: mesh
5328c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5329c58f1c22SToby Isaac @*/
5330c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5331c58f1c22SToby Isaac {
5332c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5333c58f1c22SToby Isaac   PetscErrorCode ierr;
5334c58f1c22SToby Isaac 
5335c58f1c22SToby Isaac   PetscFunctionBegin;
5336c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5337c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5338c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5339c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5340c58f1c22SToby Isaac   while (next) {
5341c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5342c58f1c22SToby Isaac     if (*hasLabel) break;
5343c58f1c22SToby Isaac     next = next->next;
5344c58f1c22SToby Isaac   }
5345c58f1c22SToby Isaac   PetscFunctionReturn(0);
5346c58f1c22SToby Isaac }
5347c58f1c22SToby Isaac 
5348c58f1c22SToby Isaac /*@C
5349c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5350c58f1c22SToby Isaac 
5351c58f1c22SToby Isaac   Not Collective
5352c58f1c22SToby Isaac 
5353c58f1c22SToby Isaac   Input Parameters:
5354c58f1c22SToby Isaac + dm   - The DM object
5355c58f1c22SToby Isaac - name - The label name
5356c58f1c22SToby Isaac 
5357c58f1c22SToby Isaac   Output Parameter:
5358c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5359c58f1c22SToby Isaac 
5360c58f1c22SToby Isaac   Level: intermediate
5361c58f1c22SToby Isaac 
5362c58f1c22SToby Isaac .keywords: mesh
5363c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5364c58f1c22SToby Isaac @*/
5365c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5366c58f1c22SToby Isaac {
5367c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5368c58f1c22SToby Isaac   PetscBool      hasLabel;
5369c58f1c22SToby Isaac   PetscErrorCode ierr;
5370c58f1c22SToby Isaac 
5371c58f1c22SToby Isaac   PetscFunctionBegin;
5372c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5373c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5374c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5375c58f1c22SToby Isaac   *label = NULL;
5376c58f1c22SToby Isaac   while (next) {
5377c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5378c58f1c22SToby Isaac     if (hasLabel) {
5379c58f1c22SToby Isaac       *label = next->label;
5380c58f1c22SToby Isaac       break;
5381c58f1c22SToby Isaac     }
5382c58f1c22SToby Isaac     next = next->next;
5383c58f1c22SToby Isaac   }
5384c58f1c22SToby Isaac   PetscFunctionReturn(0);
5385c58f1c22SToby Isaac }
5386c58f1c22SToby Isaac 
5387c58f1c22SToby Isaac /*@C
5388c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5389c58f1c22SToby Isaac 
5390c58f1c22SToby Isaac   Not Collective
5391c58f1c22SToby Isaac 
5392c58f1c22SToby Isaac   Input Parameters:
5393c58f1c22SToby Isaac + dm - The DM object
5394c58f1c22SToby Isaac - n  - the label number
5395c58f1c22SToby Isaac 
5396c58f1c22SToby Isaac   Output Parameter:
5397c58f1c22SToby Isaac . label - the label
5398c58f1c22SToby Isaac 
5399c58f1c22SToby Isaac   Level: intermediate
5400c58f1c22SToby Isaac 
5401c58f1c22SToby Isaac .keywords: mesh
5402c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5403c58f1c22SToby Isaac @*/
5404c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5405c58f1c22SToby Isaac {
5406c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5407c58f1c22SToby Isaac   PetscInt    l    = 0;
5408c58f1c22SToby Isaac 
5409c58f1c22SToby Isaac   PetscFunctionBegin;
5410c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5411c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5412c58f1c22SToby Isaac   while (next) {
5413c58f1c22SToby Isaac     if (l == n) {
5414c58f1c22SToby Isaac       *label = next->label;
5415c58f1c22SToby Isaac       PetscFunctionReturn(0);
5416c58f1c22SToby Isaac     }
5417c58f1c22SToby Isaac     ++l;
5418c58f1c22SToby Isaac     next = next->next;
5419c58f1c22SToby Isaac   }
5420c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5421c58f1c22SToby Isaac }
5422c58f1c22SToby Isaac 
5423c58f1c22SToby Isaac /*@C
5424c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5425c58f1c22SToby Isaac 
5426c58f1c22SToby Isaac   Not Collective
5427c58f1c22SToby Isaac 
5428c58f1c22SToby Isaac   Input Parameters:
5429c58f1c22SToby Isaac + dm   - The DM object
5430c58f1c22SToby Isaac - label - The DMLabel
5431c58f1c22SToby Isaac 
5432c58f1c22SToby Isaac   Level: developer
5433c58f1c22SToby Isaac 
5434c58f1c22SToby Isaac .keywords: mesh
5435c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5436c58f1c22SToby Isaac @*/
5437c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5438c58f1c22SToby Isaac {
5439c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5440c58f1c22SToby Isaac   PetscBool      hasLabel;
5441c58f1c22SToby Isaac   PetscErrorCode ierr;
5442c58f1c22SToby Isaac 
5443c58f1c22SToby Isaac   PetscFunctionBegin;
5444c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5445c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5446c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5447c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5448c58f1c22SToby Isaac   tmpLabel->label  = label;
5449c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5450c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5451c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5452c58f1c22SToby Isaac   PetscFunctionReturn(0);
5453c58f1c22SToby Isaac }
5454c58f1c22SToby Isaac 
5455c58f1c22SToby Isaac /*@C
5456c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5457c58f1c22SToby Isaac 
5458c58f1c22SToby Isaac   Not Collective
5459c58f1c22SToby Isaac 
5460c58f1c22SToby Isaac   Input Parameters:
5461c58f1c22SToby Isaac + dm   - The DM object
5462c58f1c22SToby Isaac - name - The label name
5463c58f1c22SToby Isaac 
5464c58f1c22SToby Isaac   Output Parameter:
5465c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5466c58f1c22SToby Isaac 
5467c58f1c22SToby Isaac   Level: developer
5468c58f1c22SToby Isaac 
5469c58f1c22SToby Isaac .keywords: mesh
5470c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5471c58f1c22SToby Isaac @*/
5472c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5473c58f1c22SToby Isaac {
5474c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5475c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5476c58f1c22SToby Isaac   PetscBool      hasLabel;
5477c58f1c22SToby Isaac   PetscErrorCode ierr;
5478c58f1c22SToby Isaac 
5479c58f1c22SToby Isaac   PetscFunctionBegin;
5480c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5481c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5482c58f1c22SToby Isaac   *label = NULL;
5483c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5484c58f1c22SToby Isaac   while (next) {
5485c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5486c58f1c22SToby Isaac     if (hasLabel) {
5487c58f1c22SToby Isaac       if (last) last->next       = next->next;
5488c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5489c58f1c22SToby Isaac       next->next = NULL;
5490c58f1c22SToby Isaac       *label     = next->label;
5491c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5492c58f1c22SToby Isaac       if (hasLabel) {
5493c58f1c22SToby Isaac         dm->depthLabel = NULL;
5494c58f1c22SToby Isaac       }
5495c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5496c58f1c22SToby Isaac       break;
5497c58f1c22SToby Isaac     }
5498c58f1c22SToby Isaac     last = next;
5499c58f1c22SToby Isaac     next = next->next;
5500c58f1c22SToby Isaac   }
5501c58f1c22SToby Isaac   PetscFunctionReturn(0);
5502c58f1c22SToby Isaac }
5503c58f1c22SToby Isaac 
5504c58f1c22SToby Isaac /*@C
5505c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5506c58f1c22SToby Isaac 
5507c58f1c22SToby Isaac   Not Collective
5508c58f1c22SToby Isaac 
5509c58f1c22SToby Isaac   Input Parameters:
5510c58f1c22SToby Isaac + dm   - The DM object
5511c58f1c22SToby Isaac - name - The label name
5512c58f1c22SToby Isaac 
5513c58f1c22SToby Isaac   Output Parameter:
5514c58f1c22SToby Isaac . output - The flag for output
5515c58f1c22SToby Isaac 
5516c58f1c22SToby Isaac   Level: developer
5517c58f1c22SToby Isaac 
5518c58f1c22SToby Isaac .keywords: mesh
5519c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5520c58f1c22SToby Isaac @*/
5521c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5522c58f1c22SToby Isaac {
5523c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5524c58f1c22SToby Isaac   PetscErrorCode ierr;
5525c58f1c22SToby Isaac 
5526c58f1c22SToby Isaac   PetscFunctionBegin;
5527c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5528c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5529c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5530c58f1c22SToby Isaac   while (next) {
5531c58f1c22SToby Isaac     PetscBool flg;
5532c58f1c22SToby Isaac 
5533c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5534c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5535c58f1c22SToby Isaac     next = next->next;
5536c58f1c22SToby Isaac   }
5537c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5538c58f1c22SToby Isaac }
5539c58f1c22SToby Isaac 
5540c58f1c22SToby Isaac /*@C
5541c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5542c58f1c22SToby Isaac 
5543c58f1c22SToby Isaac   Not Collective
5544c58f1c22SToby Isaac 
5545c58f1c22SToby Isaac   Input Parameters:
5546c58f1c22SToby Isaac + dm     - The DM object
5547c58f1c22SToby Isaac . name   - The label name
5548c58f1c22SToby Isaac - output - The flag for output
5549c58f1c22SToby Isaac 
5550c58f1c22SToby Isaac   Level: developer
5551c58f1c22SToby Isaac 
5552c58f1c22SToby Isaac .keywords: mesh
5553c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5554c58f1c22SToby Isaac @*/
5555c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5556c58f1c22SToby Isaac {
5557c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5558c58f1c22SToby Isaac   PetscErrorCode ierr;
5559c58f1c22SToby Isaac 
5560c58f1c22SToby Isaac   PetscFunctionBegin;
5561c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5562c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5563c58f1c22SToby Isaac   while (next) {
5564c58f1c22SToby Isaac     PetscBool flg;
5565c58f1c22SToby Isaac 
5566c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5567c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5568c58f1c22SToby Isaac     next = next->next;
5569c58f1c22SToby Isaac   }
5570c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5571c58f1c22SToby Isaac }
5572c58f1c22SToby Isaac 
5573c58f1c22SToby Isaac 
5574c58f1c22SToby Isaac /*@
5575c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5576c58f1c22SToby Isaac 
5577c58f1c22SToby Isaac   Collective on DM
5578c58f1c22SToby Isaac 
5579c58f1c22SToby Isaac   Input Parameter:
55802e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5581c58f1c22SToby Isaac 
5582c58f1c22SToby Isaac   Output Parameter:
55832e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5584c58f1c22SToby Isaac 
5585c58f1c22SToby Isaac   Level: intermediate
5586c58f1c22SToby Isaac 
5587c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5588c58f1c22SToby Isaac 
5589c58f1c22SToby Isaac .keywords: mesh
5590c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5591c58f1c22SToby Isaac @*/
5592c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5593c58f1c22SToby Isaac {
5594c58f1c22SToby Isaac   PetscInt       numLabels, l;
5595c58f1c22SToby Isaac   PetscErrorCode ierr;
5596c58f1c22SToby Isaac 
5597c58f1c22SToby Isaac   PetscFunctionBegin;
5598c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5599c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5600c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5601c58f1c22SToby Isaac     DMLabel     label, labelNew;
5602c58f1c22SToby Isaac     const char *name;
5603c58f1c22SToby Isaac     PetscBool   flg;
5604c58f1c22SToby Isaac 
5605c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5606c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5607c58f1c22SToby Isaac     if (flg) continue;
5608c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5609c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5610c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5611c58f1c22SToby Isaac   }
5612c58f1c22SToby Isaac   PetscFunctionReturn(0);
5613c58f1c22SToby Isaac }
5614a8fb8f29SToby Isaac 
5615a8fb8f29SToby Isaac /*@
5616a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5617a8fb8f29SToby Isaac 
5618a8fb8f29SToby Isaac   Input Parameter:
5619a8fb8f29SToby Isaac . dm - The DM object
5620a8fb8f29SToby Isaac 
5621a8fb8f29SToby Isaac   Output Parameter:
5622a8fb8f29SToby Isaac . cdm - The coarse DM
5623a8fb8f29SToby Isaac 
5624a8fb8f29SToby Isaac   Level: intermediate
5625a8fb8f29SToby Isaac 
5626a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5627a8fb8f29SToby Isaac @*/
5628a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5629a8fb8f29SToby Isaac {
5630a8fb8f29SToby Isaac   PetscFunctionBegin;
5631a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5632a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5633a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5634a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5635a8fb8f29SToby Isaac }
5636a8fb8f29SToby Isaac 
5637a8fb8f29SToby Isaac /*@
5638a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5639a8fb8f29SToby Isaac 
5640a8fb8f29SToby Isaac   Input Parameters:
5641a8fb8f29SToby Isaac + dm - The DM object
5642a8fb8f29SToby Isaac - cdm - The coarse DM
5643a8fb8f29SToby Isaac 
5644a8fb8f29SToby Isaac   Level: intermediate
5645a8fb8f29SToby Isaac 
5646a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5647a8fb8f29SToby Isaac @*/
5648a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5649a8fb8f29SToby Isaac {
5650a8fb8f29SToby Isaac   PetscErrorCode ierr;
5651a8fb8f29SToby Isaac 
5652a8fb8f29SToby Isaac   PetscFunctionBegin;
5653a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5654a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5655a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5656a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5657a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5658a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5659a8fb8f29SToby Isaac }
5660a8fb8f29SToby Isaac 
566188bdff64SToby Isaac /*@
566288bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
566388bdff64SToby Isaac 
566488bdff64SToby Isaac   Input Parameter:
566588bdff64SToby Isaac . dm - The DM object
566688bdff64SToby Isaac 
566788bdff64SToby Isaac   Output Parameter:
566888bdff64SToby Isaac . fdm - The fine DM
566988bdff64SToby Isaac 
567088bdff64SToby Isaac   Level: intermediate
567188bdff64SToby Isaac 
567288bdff64SToby Isaac .seealso: DMSetFineDM()
567388bdff64SToby Isaac @*/
567488bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
567588bdff64SToby Isaac {
567688bdff64SToby Isaac   PetscFunctionBegin;
567788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
567888bdff64SToby Isaac   PetscValidPointer(fdm, 2);
567988bdff64SToby Isaac   *fdm = dm->fineMesh;
568088bdff64SToby Isaac   PetscFunctionReturn(0);
568188bdff64SToby Isaac }
568288bdff64SToby Isaac 
568388bdff64SToby Isaac /*@
568488bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
568588bdff64SToby Isaac 
568688bdff64SToby Isaac   Input Parameters:
568788bdff64SToby Isaac + dm - The DM object
568888bdff64SToby Isaac - fdm - The fine DM
568988bdff64SToby Isaac 
569088bdff64SToby Isaac   Level: intermediate
569188bdff64SToby Isaac 
569288bdff64SToby Isaac .seealso: DMGetFineDM()
569388bdff64SToby Isaac @*/
569488bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
569588bdff64SToby Isaac {
569688bdff64SToby Isaac   PetscErrorCode ierr;
569788bdff64SToby Isaac 
569888bdff64SToby Isaac   PetscFunctionBegin;
569988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
570088bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
570188bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
570288bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
570388bdff64SToby Isaac   dm->fineMesh = fdm;
570488bdff64SToby Isaac   PetscFunctionReturn(0);
570588bdff64SToby Isaac }
570688bdff64SToby Isaac 
5707a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5708a6ba4734SToby Isaac 
5709a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5710a6ba4734SToby Isaac {
5711a6ba4734SToby Isaac   PetscErrorCode ierr;
5712a6ba4734SToby Isaac 
5713a6ba4734SToby Isaac   PetscFunctionBegin;
5714e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
5715a6ba4734SToby Isaac   PetscFunctionReturn(0);
5716a6ba4734SToby Isaac }
5717a6ba4734SToby Isaac 
5718a6ba4734SToby Isaac /*@C
5719a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
5720a6ba4734SToby Isaac 
5721a6ba4734SToby Isaac   Input Parameters:
57224c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
5723f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
5724a6ba4734SToby Isaac . name        - The BC name
5725a6ba4734SToby Isaac . labelname   - The label defining constrained points
5726a6ba4734SToby Isaac . field       - The field to constrain
5727a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5728a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5729a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5730a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5731a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5732a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5733a6ba4734SToby Isaac 
5734a6ba4734SToby Isaac   Options Database Keys:
5735a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5736a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5737a6ba4734SToby Isaac 
5738a6ba4734SToby Isaac   Level: developer
5739a6ba4734SToby Isaac 
5740a6ba4734SToby Isaac .seealso: DMGetBoundary()
5741a6ba4734SToby Isaac @*/
5742f971fd6bSMatthew G. Knepley PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], const char labelname[], PetscInt field, PetscInt numcomps, const PetscInt *comps, void (*bcFunc)(), PetscInt numids, const PetscInt *ids, void *ctx)
5743a6ba4734SToby Isaac {
5744a6ba4734SToby Isaac   PetscErrorCode ierr;
5745a6ba4734SToby Isaac 
5746a6ba4734SToby Isaac   PetscFunctionBegin;
5747a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5748f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
5749a6ba4734SToby Isaac   PetscFunctionReturn(0);
5750a6ba4734SToby Isaac }
5751a6ba4734SToby Isaac 
5752a6ba4734SToby Isaac /*@
5753a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
5754a6ba4734SToby Isaac 
5755a6ba4734SToby Isaac   Input Parameters:
5756a6ba4734SToby Isaac . dm - The mesh object
5757a6ba4734SToby Isaac 
5758a6ba4734SToby Isaac   Output Parameters:
5759a6ba4734SToby Isaac . numBd - The number of BC
5760a6ba4734SToby Isaac 
5761a6ba4734SToby Isaac   Level: intermediate
5762a6ba4734SToby Isaac 
5763a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
5764a6ba4734SToby Isaac @*/
5765a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
5766a6ba4734SToby Isaac {
576758ebd649SToby Isaac   PetscErrorCode ierr;
5768a6ba4734SToby Isaac 
5769a6ba4734SToby Isaac   PetscFunctionBegin;
5770a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
577158ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
5772a6ba4734SToby Isaac   PetscFunctionReturn(0);
5773a6ba4734SToby Isaac }
5774a6ba4734SToby Isaac 
5775a6ba4734SToby Isaac /*@C
5776a6ba4734SToby Isaac   DMGetBoundary - Add a boundary condition to the model
5777a6ba4734SToby Isaac 
5778a6ba4734SToby Isaac   Input Parameters:
5779a6ba4734SToby Isaac + dm          - The mesh object
5780a6ba4734SToby Isaac - bd          - The BC number
5781a6ba4734SToby Isaac 
5782a6ba4734SToby Isaac   Output Parameters:
5783f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
5784a6ba4734SToby Isaac . name        - The BC name
5785a6ba4734SToby Isaac . labelname   - The label defining constrained points
5786a6ba4734SToby Isaac . field       - The field to constrain
5787a6ba4734SToby Isaac . numcomps    - The number of constrained field components
5788a6ba4734SToby Isaac . comps       - An array of constrained component numbers
5789a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
5790a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
5791a6ba4734SToby Isaac . ids         - An array of ids for constrained points
5792a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
5793a6ba4734SToby Isaac 
5794a6ba4734SToby Isaac   Options Database Keys:
5795a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
5796a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
5797a6ba4734SToby Isaac 
5798a6ba4734SToby Isaac   Level: developer
5799a6ba4734SToby Isaac 
5800a6ba4734SToby Isaac .seealso: DMAddBoundary()
5801a6ba4734SToby Isaac @*/
5802f971fd6bSMatthew G. Knepley PetscErrorCode DMGetBoundary(DM dm, PetscInt bd, DMBoundaryConditionType *type, const char **name, const char **labelname, PetscInt *field, PetscInt *numcomps, const PetscInt **comps, void (**func)(), PetscInt *numids, const PetscInt **ids, void **ctx)
5803a6ba4734SToby Isaac {
580458ebd649SToby Isaac   PetscErrorCode ierr;
5805a6ba4734SToby Isaac 
5806a6ba4734SToby Isaac   PetscFunctionBegin;
5807a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5808f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
5809a6ba4734SToby Isaac   PetscFunctionReturn(0);
5810a6ba4734SToby Isaac }
5811a6ba4734SToby Isaac 
5812e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
5813e6f8dbb6SToby Isaac {
5814dff059c6SToby Isaac   DMBoundary *lastnext;
5815e6f8dbb6SToby Isaac   DSBoundary dsbound;
5816e6f8dbb6SToby Isaac   PetscErrorCode ierr;
5817e6f8dbb6SToby Isaac 
5818e6f8dbb6SToby Isaac   PetscFunctionBegin;
5819e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
582047a1f5adSToby Isaac   if (dm->boundary) {
582147a1f5adSToby Isaac     DMBoundary next = dm->boundary;
582247a1f5adSToby Isaac 
582347a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
582447a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
582547a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
582647a1f5adSToby Isaac     while (next) {
582747a1f5adSToby Isaac       DMBoundary b = next;
582847a1f5adSToby Isaac 
582947a1f5adSToby Isaac       next = b->next;
583047a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
5831a6ba4734SToby Isaac     }
583247a1f5adSToby Isaac     dm->boundary = NULL;
5833a6ba4734SToby Isaac   }
583447a1f5adSToby Isaac 
5835dff059c6SToby Isaac   lastnext = &(dm->boundary);
5836e6f8dbb6SToby Isaac   while (dsbound) {
5837e6f8dbb6SToby Isaac     DMBoundary dmbound;
5838e6f8dbb6SToby Isaac 
5839e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
5840e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
5841e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
5842e6f8dbb6SToby Isaac     if (!dmbound->label) PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);
584347a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
5844dff059c6SToby Isaac     *lastnext = dmbound;
5845dff059c6SToby Isaac     lastnext = &(dmbound->next);
5846dff059c6SToby Isaac     dsbound = dsbound->next;
5847a6ba4734SToby Isaac   }
5848a6ba4734SToby Isaac   PetscFunctionReturn(0);
5849a6ba4734SToby Isaac }
5850a6ba4734SToby Isaac 
5851a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
5852a6ba4734SToby Isaac {
5853b95f2879SToby Isaac   DMBoundary     b;
5854a6ba4734SToby Isaac   PetscErrorCode ierr;
5855a6ba4734SToby Isaac 
5856a6ba4734SToby Isaac   PetscFunctionBegin;
5857a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5858a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
5859a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
5860e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
5861b95f2879SToby Isaac   b = dm->boundary;
5862a6ba4734SToby Isaac   while (b && !(*isBd)) {
5863e6f8dbb6SToby Isaac     DMLabel    label = b->label;
5864e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
58653424c85cSToby Isaac 
58663424c85cSToby Isaac     if (label) {
5867a6ba4734SToby Isaac       PetscInt i;
5868a6ba4734SToby Isaac 
5869e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
5870e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
5871a6ba4734SToby Isaac       }
5872a6ba4734SToby Isaac     }
5873a6ba4734SToby Isaac     b = b->next;
5874a6ba4734SToby Isaac   }
5875a6ba4734SToby Isaac   PetscFunctionReturn(0);
5876a6ba4734SToby Isaac }
58774d6f44ffSToby Isaac 
58784d6f44ffSToby Isaac /*@C
58794d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
58804d6f44ffSToby Isaac 
58814d6f44ffSToby Isaac   Input Parameters:
58824d6f44ffSToby Isaac + dm      - The DM
58830709b2feSToby Isaac . time    - The time
58844d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
58854d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
58864d6f44ffSToby Isaac - mode    - The insertion mode for values
58874d6f44ffSToby Isaac 
58884d6f44ffSToby Isaac   Output Parameter:
58894d6f44ffSToby Isaac . X - vector
58904d6f44ffSToby Isaac 
58914d6f44ffSToby Isaac    Calling sequence of func:
58920709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
58934d6f44ffSToby Isaac 
58944d6f44ffSToby Isaac +  dim - The spatial dimension
58954d6f44ffSToby Isaac .  x   - The coordinates
58964d6f44ffSToby Isaac .  Nf  - The number of fields
58974d6f44ffSToby Isaac .  u   - The output field values
58984d6f44ffSToby Isaac -  ctx - optional user-defined function context
58994d6f44ffSToby Isaac 
59004d6f44ffSToby Isaac   Level: developer
59014d6f44ffSToby Isaac 
59022716604bSToby Isaac .seealso: DMComputeL2Diff()
59034d6f44ffSToby Isaac @*/
59040709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
59054d6f44ffSToby Isaac {
59064d6f44ffSToby Isaac   Vec            localX;
59074d6f44ffSToby Isaac   PetscErrorCode ierr;
59084d6f44ffSToby Isaac 
59094d6f44ffSToby Isaac   PetscFunctionBegin;
59104d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59114d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
59120709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
59134d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
59144d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
59154d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
59164d6f44ffSToby Isaac   PetscFunctionReturn(0);
59174d6f44ffSToby Isaac }
59184d6f44ffSToby Isaac 
59190709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
59204d6f44ffSToby Isaac {
59214d6f44ffSToby Isaac   PetscErrorCode ierr;
59224d6f44ffSToby Isaac 
59234d6f44ffSToby Isaac   PetscFunctionBegin;
59244d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59254d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
59264d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLocal",((PetscObject)dm)->type_name);
59270709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
59284d6f44ffSToby Isaac   PetscFunctionReturn(0);
59294d6f44ffSToby Isaac }
59304d6f44ffSToby Isaac 
59310709b2feSToby Isaac PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
59324d6f44ffSToby Isaac {
59334d6f44ffSToby Isaac   PetscErrorCode ierr;
59344d6f44ffSToby Isaac 
59354d6f44ffSToby Isaac   PetscFunctionBegin;
59364d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59374d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
59384d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
59390709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, funcs, ctxs, mode, localX);CHKERRQ(ierr);
59404d6f44ffSToby Isaac   PetscFunctionReturn(0);
59414d6f44ffSToby Isaac }
59422716604bSToby Isaac 
59438c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
59448c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
59458c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
59468c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
59478c6c5593SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscScalar[]),
59488c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
59498c6c5593SMatthew G. Knepley {
59508c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
59518c6c5593SMatthew G. Knepley 
59528c6c5593SMatthew G. Knepley   PetscFunctionBegin;
59538c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59548c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
59558c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
59568c6c5593SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFieldLocal",((PetscObject)dm)->type_name);
59578c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
59588c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
59598c6c5593SMatthew G. Knepley }
59608c6c5593SMatthew G. Knepley 
59618c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], Vec localU,
59628c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
59638c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
59648c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
59658c6c5593SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscScalar[]),
59668c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
59678c6c5593SMatthew G. Knepley {
59688c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
59698c6c5593SMatthew G. Knepley 
59708c6c5593SMatthew G. Knepley   PetscFunctionBegin;
59718c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59728c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
59738c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
59748c6c5593SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFieldLocal",((PetscObject)dm)->type_name);
59758c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, localU, funcs, mode, localX);CHKERRQ(ierr);
59768c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
59778c6c5593SMatthew G. Knepley }
59788c6c5593SMatthew G. Knepley 
59792716604bSToby Isaac /*@C
59802716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
59812716604bSToby Isaac 
59822716604bSToby Isaac   Input Parameters:
59832716604bSToby Isaac + dm    - The DM
59840709b2feSToby Isaac . time  - The time
59852716604bSToby Isaac . funcs - The functions to evaluate for each field component
59862716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
59872716604bSToby Isaac - X     - The coefficient vector u_h
59882716604bSToby Isaac 
59892716604bSToby Isaac   Output Parameter:
59902716604bSToby Isaac . diff - The diff ||u - u_h||_2
59912716604bSToby Isaac 
59922716604bSToby Isaac   Level: developer
59932716604bSToby Isaac 
59941189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
59952716604bSToby Isaac @*/
59960709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
59972716604bSToby Isaac {
59982716604bSToby Isaac   PetscErrorCode ierr;
59992716604bSToby Isaac 
60002716604bSToby Isaac   PetscFunctionBegin;
60012716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6002b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
60032716604bSToby Isaac   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2Diff",((PetscObject)dm)->type_name);
60040709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
60052716604bSToby Isaac   PetscFunctionReturn(0);
60062716604bSToby Isaac }
6007b698f381SToby Isaac 
6008b698f381SToby Isaac /*@C
6009b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6010b698f381SToby Isaac 
6011b698f381SToby Isaac   Input Parameters:
6012b698f381SToby Isaac + dm    - The DM
6013b698f381SToby Isaac , time  - The time
6014b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6015b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6016b698f381SToby Isaac . X     - The coefficient vector u_h
6017b698f381SToby Isaac - n     - The vector to project along
6018b698f381SToby Isaac 
6019b698f381SToby Isaac   Output Parameter:
6020b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6021b698f381SToby Isaac 
6022b698f381SToby Isaac   Level: developer
6023b698f381SToby Isaac 
6024b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6025b698f381SToby Isaac @*/
6026b698f381SToby 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)
6027b698f381SToby Isaac {
6028b698f381SToby Isaac   PetscErrorCode ierr;
6029b698f381SToby Isaac 
6030b698f381SToby Isaac   PetscFunctionBegin;
6031b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6032b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6033b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6034b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6035b698f381SToby Isaac   PetscFunctionReturn(0);
6036b698f381SToby Isaac }
6037b698f381SToby Isaac 
60382a16baeaSToby Isaac /*@C
60392a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
60402a16baeaSToby Isaac 
60412a16baeaSToby Isaac   Input Parameters:
60422a16baeaSToby Isaac + dm    - The DM
60432a16baeaSToby Isaac . time  - The time
60442a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
60452a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
60462a16baeaSToby Isaac - X     - The coefficient vector u_h
60472a16baeaSToby Isaac 
60482a16baeaSToby Isaac   Output Parameter:
60492a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
60502a16baeaSToby Isaac 
60512a16baeaSToby Isaac   Level: developer
60522a16baeaSToby Isaac 
60531189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
60542a16baeaSToby Isaac @*/
60551189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
60562a16baeaSToby Isaac {
60572a16baeaSToby Isaac   PetscErrorCode ierr;
60582a16baeaSToby Isaac 
60592a16baeaSToby Isaac   PetscFunctionBegin;
60602a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60612a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
60622a16baeaSToby Isaac   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
60632a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
60642a16baeaSToby Isaac   PetscFunctionReturn(0);
60652a16baeaSToby Isaac }
60662a16baeaSToby Isaac 
6067df0b854cSToby Isaac /*@C
6068df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6069cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6070df0b854cSToby Isaac 
6071df0b854cSToby Isaac   Collective on dm
6072df0b854cSToby Isaac 
6073df0b854cSToby Isaac   Input parameters:
6074df0b854cSToby Isaac + dm - the pre-adaptation DM object
6075a1b0c543SToby Isaac - label - label with the flags
6076df0b854cSToby Isaac 
6077df0b854cSToby Isaac   Output parameters:
6078df0b854cSToby Isaac . adaptedDM - the adapted DM object: may be NULL if an adapted DM could not be produced.
6079df0b854cSToby Isaac 
6080df0b854cSToby Isaac   Level: intermediate
6081df0b854cSToby Isaac @*/
6082a1b0c543SToby Isaac PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *adaptedDM)
6083df0b854cSToby Isaac {
6084df0b854cSToby Isaac   PetscErrorCode ierr;
6085df0b854cSToby Isaac 
6086df0b854cSToby Isaac   PetscFunctionBegin;
6087df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6088a1b0c543SToby Isaac   PetscValidPointer(label,2);
6089df0b854cSToby Isaac   PetscValidPointer(adaptedDM,3);
6090df0b854cSToby Isaac   *adaptedDM = NULL;
6091a1b0c543SToby Isaac   ierr = PetscTryMethod((PetscObject)dm,"DMAdaptLabel_C",(DM,DMLabel, DM*),(dm,label,adaptedDM));CHKERRQ(ierr);
6092df0b854cSToby Isaac   PetscFunctionReturn(0);
6093df0b854cSToby Isaac }
6094c4088d22SMatthew G. Knepley 
6095502a2867SDave May /*@C
6096502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6097502a2867SDave May 
6098502a2867SDave May  Not Collective
6099502a2867SDave May 
6100502a2867SDave May  Input Parameter:
6101502a2867SDave May  . dm    - The DM
6102502a2867SDave May 
6103502a2867SDave May  Output Parameter:
6104502a2867SDave May  . nranks - the number of neighbours
6105502a2867SDave May  . ranks - the neighbors ranks
6106502a2867SDave May 
6107502a2867SDave May  Notes:
6108502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6109502a2867SDave May 
6110502a2867SDave May  Level: beginner
6111502a2867SDave May 
6112dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6113502a2867SDave May @*/
6114502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6115502a2867SDave May {
6116502a2867SDave May   PetscErrorCode ierr;
6117502a2867SDave May 
6118502a2867SDave May   PetscFunctionBegin;
6119502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6120502a2867SDave May   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMGetNeighbors",((PetscObject)dm)->type_name);
6121502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6122502a2867SDave May   PetscFunctionReturn(0);
6123502a2867SDave May }
6124502a2867SDave May 
6125531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6126531c7667SBarry Smith 
6127531c7667SBarry Smith /*
6128531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6129531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6130531c7667SBarry Smith */
6131531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6132531c7667SBarry Smith {
6133531c7667SBarry Smith   PetscErrorCode ierr;
6134531c7667SBarry Smith 
6135531c7667SBarry Smith   PetscFunctionBegin;
6136531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6137531c7667SBarry Smith     Vec x1local;
6138531c7667SBarry Smith     DM  dm;
6139531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6140531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6141531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6142531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6143531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6144531c7667SBarry Smith     x1   = x1local;
6145531c7667SBarry Smith   }
6146531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6147531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6148531c7667SBarry Smith     DM  dm;
6149531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6150531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6151531c7667SBarry Smith   }
6152531c7667SBarry Smith   PetscFunctionReturn(0);
6153531c7667SBarry Smith }
6154531c7667SBarry Smith 
6155531c7667SBarry Smith /*@
6156531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6157531c7667SBarry Smith 
6158531c7667SBarry Smith     Input Parameter:
6159531c7667SBarry Smith .    coloring - the MatFDColoring object
6160531c7667SBarry Smith 
6161531c7667SBarry Smith     Developer Notes: this routine exists because the PETSc Mat library does not know about the DM objects
6162531c7667SBarry Smith 
61631b266c99SBarry Smith     Level: advanced
61641b266c99SBarry Smith 
6165531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6166531c7667SBarry Smith @*/
6167531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6168531c7667SBarry Smith {
6169531c7667SBarry Smith   PetscFunctionBegin;
6170531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6171531c7667SBarry Smith   PetscFunctionReturn(0);
6172531c7667SBarry Smith }
6173