xref: /petsc/src/dm/interface/dm.c (revision 4a7a4c067c23afaf9be5d87be8702050260bf46c)
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 
13*4a7a4c06SLawrence Mitchell static PetscErrorCode DMHasCreateInjection_Default(DM dm, PetscBool *flg)
14*4a7a4c06SLawrence Mitchell {
15*4a7a4c06SLawrence Mitchell   PetscFunctionBegin;
16*4a7a4c06SLawrence Mitchell   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17*4a7a4c06SLawrence Mitchell   PetscValidPointer(flg,2);
18*4a7a4c06SLawrence Mitchell   *flg = PETSC_FALSE;
19*4a7a4c06SLawrence Mitchell   PetscFunctionReturn(0);
20*4a7a4c06SLawrence Mitchell }
21*4a7a4c06SLawrence Mitchell 
22a4121054SBarry Smith /*@
23de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
24a4121054SBarry Smith 
25a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
26a4121054SBarry Smith    error when you try to use the vector.
27a4121054SBarry Smith 
28a4121054SBarry Smith   Collective on MPI_Comm
29a4121054SBarry Smith 
30a4121054SBarry Smith   Input Parameter:
31a4121054SBarry Smith . comm - The communicator for the DM object
32a4121054SBarry Smith 
33a4121054SBarry Smith   Output Parameter:
34a4121054SBarry Smith . dm - The DM object
35a4121054SBarry Smith 
36a4121054SBarry Smith   Level: beginner
37a4121054SBarry Smith 
388472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
39a4121054SBarry Smith @*/
407087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
41a4121054SBarry Smith {
42a4121054SBarry Smith   DM             v;
43a4121054SBarry Smith   PetscErrorCode ierr;
44a4121054SBarry Smith 
45a4121054SBarry Smith   PetscFunctionBegin;
461411c6eeSJed Brown   PetscValidPointer(dm,2);
470298fd71SBarry Smith   *dm = NULL;
488b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
49607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
50607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
51607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
52a4121054SBarry Smith 
5373107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
54e7c4fc90SDmitry Karpeev 
550298fd71SBarry Smith   v->ltogmap                  = NULL;
561411c6eeSJed Brown   v->bs                       = 1;
57171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5888ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5988ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
60c58f1c22SToby Isaac   v->labels                   = NULL;
61c58f1c22SToby Isaac   v->depthLabel               = NULL;
620298fd71SBarry Smith   v->defaultSection           = NULL;
630298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
64fba222abSToby Isaac   v->defaultConstraintSection = NULL;
65fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
66c6b900c6SMatthew G. Knepley   v->L                        = NULL;
67c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
685dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
699a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
70435a35e8SMatthew G Knepley   {
71435a35e8SMatthew G Knepley     PetscInt i;
72435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
730298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
74435a35e8SMatthew G Knepley     }
75435a35e8SMatthew G Knepley   }
762764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
7714f150ffSMatthew G. Knepley   v->dmBC = NULL;
78a8fb8f29SToby Isaac   v->coarseMesh = NULL;
79f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
80cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
81c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
82b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
83bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
84c58f1c22SToby Isaac   v->labels->refct = 1;
85*4a7a4c06SLawrence Mitchell 
86*4a7a4c06SLawrence Mitchell   v->ops->hascreateinjection = DMHasCreateInjection_Default;
87*4a7a4c06SLawrence Mitchell 
881411c6eeSJed Brown   *dm = v;
89a4121054SBarry Smith   PetscFunctionReturn(0);
90a4121054SBarry Smith }
91a4121054SBarry Smith 
9238221697SMatthew G. Knepley /*@
9338221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9438221697SMatthew G. Knepley 
9538221697SMatthew G. Knepley   Collective on MPI_Comm
9638221697SMatthew G. Knepley 
9738221697SMatthew G. Knepley   Input Parameter:
9838221697SMatthew G. Knepley . dm - The original DM object
9938221697SMatthew G. Knepley 
10038221697SMatthew G. Knepley   Output Parameter:
10138221697SMatthew G. Knepley . newdm  - The new DM object
10238221697SMatthew G. Knepley 
10338221697SMatthew G. Knepley   Level: beginner
10438221697SMatthew G. Knepley 
10538221697SMatthew G. Knepley .keywords: DM, topology, create
10638221697SMatthew G. Knepley @*/
10738221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
10838221697SMatthew G. Knepley {
10938221697SMatthew G. Knepley   PetscSF        sf;
11038221697SMatthew G. Knepley   Vec            coords;
11138221697SMatthew G. Knepley   void          *ctx;
112a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
11338221697SMatthew G. Knepley   PetscErrorCode ierr;
11438221697SMatthew G. Knepley 
11538221697SMatthew G. Knepley   PetscFunctionBegin;
11638221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11738221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
11838221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
119c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
120c58f1c22SToby Isaac   dm->labels->refct++;
121c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
122c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
1231de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1241de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
12538221697SMatthew G. Knepley   if (dm->ops->clone) {
12638221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
12738221697SMatthew G. Knepley   }
1283f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
12938221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13038221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13138221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
13238221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
133be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
134be4c1c3eSMatthew G. Knepley     DM           ncdm;
135be4c1c3eSMatthew G. Knepley     PetscSection cs;
1365a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
137be4c1c3eSMatthew G. Knepley 
138be4c1c3eSMatthew G. Knepley     ierr = DMGetDefaultSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
139be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1405a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1415a0206caSToby Isaac     if (pEndMax >= 0) {
142be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
14313bffc4cSMatthew G. Knepley       ierr = DMSetDefaultSection(ncdm, cs);CHKERRQ(ierr);
144a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
145be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
146be4c1c3eSMatthew G. Knepley     }
147be4c1c3eSMatthew G. Knepley   }
148a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
149a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15038221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
15138221697SMatthew G. Knepley   if (coords) {
15238221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
15338221697SMatthew G. Knepley   } else {
15438221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
15538221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
15638221697SMatthew G. Knepley   }
15790b157c4SStefano Zampini   {
15890b157c4SStefano Zampini     PetscBool             isper;
159c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1605dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
16190b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
16290b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
163c6b900c6SMatthew G. Knepley   }
16438221697SMatthew G. Knepley   PetscFunctionReturn(0);
16538221697SMatthew G. Knepley }
16638221697SMatthew G. Knepley 
1679a42bb27SBarry Smith /*@C
168564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1699a42bb27SBarry Smith 
1708472ad0fSDave May    Logically Collective on DM
1719a42bb27SBarry Smith 
1729a42bb27SBarry Smith    Input Parameter:
1739a42bb27SBarry Smith +  da - initial distributed array
1748154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1759a42bb27SBarry Smith 
1769a42bb27SBarry Smith    Options Database:
177dd85299cSBarry Smith .   -dm_vec_type ctype
1789a42bb27SBarry Smith 
1799a42bb27SBarry Smith    Level: intermediate
1809a42bb27SBarry Smith 
1818472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1829a42bb27SBarry Smith @*/
18319fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1849a42bb27SBarry Smith {
1859a42bb27SBarry Smith   PetscErrorCode ierr;
1869a42bb27SBarry Smith 
1879a42bb27SBarry Smith   PetscFunctionBegin;
1889a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1899a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
19019fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1919a42bb27SBarry Smith   PetscFunctionReturn(0);
1929a42bb27SBarry Smith }
1939a42bb27SBarry Smith 
194c0dedaeaSBarry Smith /*@C
195c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
196c0dedaeaSBarry Smith 
1978472ad0fSDave May    Logically Collective on DM
198c0dedaeaSBarry Smith 
199c0dedaeaSBarry Smith    Input Parameter:
200c0dedaeaSBarry Smith .  da - initial distributed array
201c0dedaeaSBarry Smith 
202c0dedaeaSBarry Smith    Output Parameter:
203c0dedaeaSBarry Smith .  ctype - the vector type
204c0dedaeaSBarry Smith 
205c0dedaeaSBarry Smith    Level: intermediate
206c0dedaeaSBarry Smith 
2078472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
208c0dedaeaSBarry Smith @*/
209c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
210c0dedaeaSBarry Smith {
211c0dedaeaSBarry Smith   PetscFunctionBegin;
212c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
213c0dedaeaSBarry Smith   *ctype = da->vectype;
214c0dedaeaSBarry Smith   PetscFunctionReturn(0);
215c0dedaeaSBarry Smith }
216c0dedaeaSBarry Smith 
2175f1ad066SMatthew G Knepley /*@
21834f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2195f1ad066SMatthew G Knepley 
2205f1ad066SMatthew G Knepley   Not collective
2215f1ad066SMatthew G Knepley 
2225f1ad066SMatthew G Knepley   Input Parameter:
2235f1ad066SMatthew G Knepley . v - The Vec
2245f1ad066SMatthew G Knepley 
2255f1ad066SMatthew G Knepley   Output Parameter:
2265f1ad066SMatthew G Knepley . dm - The DM
2275f1ad066SMatthew G Knepley 
2285f1ad066SMatthew G Knepley   Level: intermediate
2295f1ad066SMatthew G Knepley 
2305f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2315f1ad066SMatthew G Knepley @*/
2325f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2335f1ad066SMatthew G Knepley {
2345f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley   PetscFunctionBegin;
2375f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2385f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2395f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2405f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2415f1ad066SMatthew G Knepley }
2425f1ad066SMatthew G Knepley 
2435f1ad066SMatthew G Knepley /*@
244d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2455f1ad066SMatthew G Knepley 
2465f1ad066SMatthew G Knepley   Not collective
2475f1ad066SMatthew G Knepley 
2485f1ad066SMatthew G Knepley   Input Parameters:
2495f1ad066SMatthew G Knepley + v - The Vec
2505f1ad066SMatthew G Knepley - dm - The DM
2515f1ad066SMatthew G Knepley 
252d9805387SMatthew 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.
253d9805387SMatthew G. Knepley 
2545f1ad066SMatthew G Knepley   Level: intermediate
2555f1ad066SMatthew G Knepley 
2565f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2575f1ad066SMatthew G Knepley @*/
2585f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2595f1ad066SMatthew G Knepley {
2605f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2615f1ad066SMatthew G Knepley 
2625f1ad066SMatthew G Knepley   PetscFunctionBegin;
2635f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
264d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2655f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2665f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2675f1ad066SMatthew G Knepley }
2685f1ad066SMatthew G Knepley 
269521d9a4cSLisandro Dalcin /*@C
270521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
271521d9a4cSLisandro Dalcin 
272521d9a4cSLisandro Dalcin    Logically Collective on DM
273521d9a4cSLisandro Dalcin 
274521d9a4cSLisandro Dalcin    Input Parameter:
275521d9a4cSLisandro Dalcin +  dm - the DM context
276a2b5a043SBarry Smith -  ctype - the matrix type
277521d9a4cSLisandro Dalcin 
278521d9a4cSLisandro Dalcin    Options Database:
279521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
280521d9a4cSLisandro Dalcin 
281521d9a4cSLisandro Dalcin    Level: intermediate
282521d9a4cSLisandro Dalcin 
283c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
284521d9a4cSLisandro Dalcin @*/
28519fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
286521d9a4cSLisandro Dalcin {
287521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
28888f0584fSBarry Smith 
289521d9a4cSLisandro Dalcin   PetscFunctionBegin;
290521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
291521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
29219fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
293521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
294521d9a4cSLisandro Dalcin }
295521d9a4cSLisandro Dalcin 
296c0dedaeaSBarry Smith /*@C
297c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
298c0dedaeaSBarry Smith 
299c0dedaeaSBarry Smith    Logically Collective on DM
300c0dedaeaSBarry Smith 
301c0dedaeaSBarry Smith    Input Parameter:
302c0dedaeaSBarry Smith .  dm - the DM context
303c0dedaeaSBarry Smith 
304c0dedaeaSBarry Smith    Output Parameter:
305c0dedaeaSBarry Smith .  ctype - the matrix type
306c0dedaeaSBarry Smith 
307c0dedaeaSBarry Smith    Options Database:
308c0dedaeaSBarry Smith .   -dm_mat_type ctype
309c0dedaeaSBarry Smith 
310c0dedaeaSBarry Smith    Level: intermediate
311c0dedaeaSBarry Smith 
312c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
313c0dedaeaSBarry Smith @*/
314c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
315c0dedaeaSBarry Smith {
316c0dedaeaSBarry Smith   PetscFunctionBegin;
317c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
318c0dedaeaSBarry Smith   *ctype = dm->mattype;
319c0dedaeaSBarry Smith   PetscFunctionReturn(0);
320c0dedaeaSBarry Smith }
321c0dedaeaSBarry Smith 
322c688c046SMatthew G Knepley /*@
32334f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
324c688c046SMatthew G Knepley 
325c688c046SMatthew G Knepley   Not collective
326c688c046SMatthew G Knepley 
327c688c046SMatthew G Knepley   Input Parameter:
328c688c046SMatthew G Knepley . A - The Mat
329c688c046SMatthew G Knepley 
330c688c046SMatthew G Knepley   Output Parameter:
331c688c046SMatthew G Knepley . dm - The DM
332c688c046SMatthew G Knepley 
333c688c046SMatthew G Knepley   Level: intermediate
334c688c046SMatthew G Knepley 
335c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
336c688c046SMatthew G Knepley @*/
337c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
338c688c046SMatthew G Knepley {
339c688c046SMatthew G Knepley   PetscErrorCode ierr;
340c688c046SMatthew G Knepley 
341c688c046SMatthew G Knepley   PetscFunctionBegin;
342c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
343c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
344c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
345c688c046SMatthew G Knepley   PetscFunctionReturn(0);
346c688c046SMatthew G Knepley }
347c688c046SMatthew G Knepley 
348c688c046SMatthew G Knepley /*@
349c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
350c688c046SMatthew G Knepley 
351c688c046SMatthew G Knepley   Not collective
352c688c046SMatthew G Knepley 
353c688c046SMatthew G Knepley   Input Parameters:
354c688c046SMatthew G Knepley + A - The Mat
355c688c046SMatthew G Knepley - dm - The DM
356c688c046SMatthew G Knepley 
357c688c046SMatthew G Knepley   Level: intermediate
358c688c046SMatthew G Knepley 
359c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
360c688c046SMatthew G Knepley @*/
361c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
362c688c046SMatthew G Knepley {
363c688c046SMatthew G Knepley   PetscErrorCode ierr;
364c688c046SMatthew G Knepley 
365c688c046SMatthew G Knepley   PetscFunctionBegin;
366c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3678865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
368c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
369c688c046SMatthew G Knepley   PetscFunctionReturn(0);
370c688c046SMatthew G Knepley }
371c688c046SMatthew G Knepley 
3729a42bb27SBarry Smith /*@C
3739a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3746757b960SDave May    DM options in the database.
3759a42bb27SBarry Smith 
3768353ddbbSDave May    Logically Collective on DM
3779a42bb27SBarry Smith 
3789a42bb27SBarry Smith    Input Parameter:
3798353ddbbSDave May +  da - the DM context
3809a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3819a42bb27SBarry Smith 
3829a42bb27SBarry Smith    Notes:
3839a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3849a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3859a42bb27SBarry Smith 
3869a42bb27SBarry Smith    Level: advanced
3879a42bb27SBarry Smith 
3888353ddbbSDave May .keywords: DM, set, options, prefix, database
3899a42bb27SBarry Smith 
3909a42bb27SBarry Smith .seealso: DMSetFromOptions()
3919a42bb27SBarry Smith @*/
3927087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3939a42bb27SBarry Smith {
3949a42bb27SBarry Smith   PetscErrorCode ierr;
3959a42bb27SBarry Smith 
3969a42bb27SBarry Smith   PetscFunctionBegin;
3979a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3989a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
399691be533SLawrence Mitchell   if (dm->sf) {
400691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
401691be533SLawrence Mitchell   }
402691be533SLawrence Mitchell   if (dm->defaultSF) {
403691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
404691be533SLawrence Mitchell   }
4059a42bb27SBarry Smith   PetscFunctionReturn(0);
4069a42bb27SBarry Smith }
4079a42bb27SBarry Smith 
40831697293SDave May /*@C
40931697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
41031697293SDave May    DM options in the database.
41131697293SDave May 
41231697293SDave May    Logically Collective on DM
41331697293SDave May 
41431697293SDave May    Input Parameters:
41531697293SDave May +  dm - the DM context
41631697293SDave May -  prefix - the prefix string to prepend to all DM option requests
41731697293SDave May 
41831697293SDave May    Notes:
41931697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
42031697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
42131697293SDave May 
42231697293SDave May    Level: advanced
42331697293SDave May 
42431697293SDave May .keywords: DM, append, options, prefix, database
42531697293SDave May 
42631697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
42731697293SDave May @*/
42831697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
42931697293SDave May {
43031697293SDave May   PetscErrorCode ierr;
43131697293SDave May 
43231697293SDave May   PetscFunctionBegin;
43331697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43431697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
43531697293SDave May   PetscFunctionReturn(0);
43631697293SDave May }
43731697293SDave May 
43831697293SDave May /*@C
43931697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
44031697293SDave May    DM options in the database.
44131697293SDave May 
44231697293SDave May    Not Collective
44331697293SDave May 
44431697293SDave May    Input Parameters:
44531697293SDave May .  dm - the DM context
44631697293SDave May 
44731697293SDave May    Output Parameters:
44831697293SDave May .  prefix - pointer to the prefix string used is returned
44931697293SDave May 
45031697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
45131697293SDave May    sufficient length to hold the prefix.
45231697293SDave May 
45331697293SDave May    Level: advanced
45431697293SDave May 
45531697293SDave May .keywords: DM, set, options, prefix, database
45631697293SDave May 
45731697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
45831697293SDave May @*/
45931697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
46031697293SDave May {
46131697293SDave May   PetscErrorCode ierr;
46231697293SDave May 
46331697293SDave May   PetscFunctionBegin;
46431697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
46531697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
46631697293SDave May   PetscFunctionReturn(0);
46731697293SDave May }
46831697293SDave May 
46988bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
47088bdff64SToby Isaac {
47188bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
47288bdff64SToby Isaac   DMNamedVecLink nlink;
47388bdff64SToby Isaac   PetscErrorCode ierr;
47488bdff64SToby Isaac 
47588bdff64SToby Isaac   PetscFunctionBegin;
476aab5bcd8SJed Brown   *ncrefct = 0;
47788bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
47888bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
47988bdff64SToby Isaac     if (dm->localin[i])  refct--;
48088bdff64SToby Isaac     if (dm->globalin[i]) refct--;
48188bdff64SToby Isaac   }
48288bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
48388bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
48488bdff64SToby Isaac   if (dm->x) {
48588bdff64SToby Isaac     DM obj;
48688bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
48788bdff64SToby Isaac     if (obj == dm) refct--;
48888bdff64SToby Isaac   }
48988bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
49088bdff64SToby Isaac     refct--;
49188bdff64SToby Isaac     if (recurseCoarse) {
49288bdff64SToby Isaac       PetscInt coarseCount;
49388bdff64SToby Isaac 
49488bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
49588bdff64SToby Isaac       refct += coarseCount;
49688bdff64SToby Isaac     }
49788bdff64SToby Isaac   }
49888bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
49988bdff64SToby Isaac     refct--;
50088bdff64SToby Isaac     if (recurseFine) {
50188bdff64SToby Isaac       PetscInt fineCount;
50288bdff64SToby Isaac 
50388bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
50488bdff64SToby Isaac       refct += fineCount;
50588bdff64SToby Isaac     }
50688bdff64SToby Isaac   }
50788bdff64SToby Isaac   *ncrefct = refct;
50888bdff64SToby Isaac   PetscFunctionReturn(0);
50988bdff64SToby Isaac }
51088bdff64SToby Isaac 
511354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
512354557abSToby Isaac {
513354557abSToby Isaac   PetscErrorCode ierr;
514354557abSToby Isaac 
515354557abSToby Isaac   PetscFunctionBegin;
516354557abSToby Isaac   if (!--(dm->labels->refct)) {
517354557abSToby Isaac     DMLabelLink next = dm->labels->next;
518354557abSToby Isaac 
519354557abSToby Isaac     /* destroy the labels */
520354557abSToby Isaac     while (next) {
521354557abSToby Isaac       DMLabelLink tmp = next->next;
522354557abSToby Isaac 
523354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
524354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
525354557abSToby Isaac       next = tmp;
526354557abSToby Isaac     }
527354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
528354557abSToby Isaac   }
529354557abSToby Isaac   PetscFunctionReturn(0);
530354557abSToby Isaac }
531354557abSToby Isaac 
53247c6ae99SBarry Smith /*@
5338472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
53447c6ae99SBarry Smith 
53547c6ae99SBarry Smith     Collective on DM
53647c6ae99SBarry Smith 
53747c6ae99SBarry Smith     Input Parameter:
53847c6ae99SBarry Smith .   dm - the DM object to destroy
53947c6ae99SBarry Smith 
54047c6ae99SBarry Smith     Level: developer
54147c6ae99SBarry Smith 
542e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
54347c6ae99SBarry Smith 
54447c6ae99SBarry Smith @*/
545fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
54647c6ae99SBarry Smith {
54788bdff64SToby Isaac   PetscInt       i, cnt;
548dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
54947c6ae99SBarry Smith   PetscErrorCode ierr;
55047c6ae99SBarry Smith 
55147c6ae99SBarry Smith   PetscFunctionBegin;
5526bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5536bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
55487e657c6SBarry Smith 
55588bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
55688bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
55788bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
55888bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
559732e2eb9SMatthew G Knepley   /*
560732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
561732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
562732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
563732e2eb9SMatthew G Knepley   */
5646bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5656bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
566732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5676bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5686bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
569732e2eb9SMatthew G Knepley   }
570f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5710298fd71SBarry Smith   (*dm)->namedglobal = NULL;
572f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5732348bcf4SPeter Brune     nnext = nlink->next;
5742348bcf4SPeter 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);
5752348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5762348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5772348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5782348bcf4SPeter Brune   }
579f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5800298fd71SBarry Smith   (*dm)->namedlocal = NULL;
581f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
582f490541aSPeter Brune     nnext = nlink->next;
583f490541aSPeter 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);
584f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
585f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
586f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
587f490541aSPeter Brune   }
5882348bcf4SPeter Brune 
589b17ce1afSJed Brown   /* Destroy the list of hooks */
590c833c3b5SJed Brown   {
591c833c3b5SJed Brown     DMCoarsenHookLink link,next;
592b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
593b17ce1afSJed Brown       next = link->next;
594b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
595b17ce1afSJed Brown     }
5960298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
597c833c3b5SJed Brown   }
598c833c3b5SJed Brown   {
599c833c3b5SJed Brown     DMRefineHookLink link,next;
600c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
601c833c3b5SJed Brown       next = link->next;
602c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
603c833c3b5SJed Brown     }
6040298fd71SBarry Smith     (*dm)->refinehook = NULL;
605c833c3b5SJed Brown   }
606be081cd6SPeter Brune   {
607be081cd6SPeter Brune     DMSubDomainHookLink link,next;
608be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
609be081cd6SPeter Brune       next = link->next;
610be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
611be081cd6SPeter Brune     }
6120298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
613be081cd6SPeter Brune   }
614baf369e7SPeter Brune   {
615baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
616baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
617baf369e7SPeter Brune       next = link->next;
618baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
619baf369e7SPeter Brune     }
6200298fd71SBarry Smith     (*dm)->gtolhook = NULL;
621baf369e7SPeter Brune   }
622d4d07f1eSToby Isaac   {
623d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
624d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
625d4d07f1eSToby Isaac       next = link->next;
626d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
627d4d07f1eSToby Isaac     }
628d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
629d4d07f1eSToby Isaac   }
630aa1993deSMatthew G Knepley   /* Destroy the work arrays */
631aa1993deSMatthew G Knepley   {
632aa1993deSMatthew G Knepley     DMWorkLink link,next;
633aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
634aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
635aa1993deSMatthew G Knepley       next = link->next;
636aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
637aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
638aa1993deSMatthew G Knepley     }
6390298fd71SBarry Smith     (*dm)->workin = NULL;
640aa1993deSMatthew G Knepley   }
641c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
642c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
643c58f1c22SToby Isaac 
644c58f1c22SToby Isaac     /* destroy the labels */
645c58f1c22SToby Isaac     while (next) {
646c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
647c58f1c22SToby Isaac 
648c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
649c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
650c58f1c22SToby Isaac       next = tmp;
651c58f1c22SToby Isaac     }
652c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
653c58f1c22SToby Isaac   }
654e6f8dbb6SToby Isaac   {
655e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
656e6f8dbb6SToby Isaac     while (next) {
657e6f8dbb6SToby Isaac       DMBoundary b = next;
658e6f8dbb6SToby Isaac 
659e6f8dbb6SToby Isaac       next = b->next;
660e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
661e6f8dbb6SToby Isaac     }
662e6f8dbb6SToby Isaac   }
663b17ce1afSJed Brown 
66452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
66552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
66652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
66752536dc3SBarry Smith 
6681a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
6691a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
6701a266240SBarry Smith   }
67187e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
67271cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
6734dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6746bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
6756bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
676073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
67788ed4aceSMatthew G Knepley 
67888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
67988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6808b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
681fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
682fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
68388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
68488ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
6858e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
686af122d2aSMatthew G Knepley 
68788bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
68888bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
68988bdff64SToby Isaac   }
690a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
69188bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
69288bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
69388bdff64SToby Isaac   }
69488bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
6956636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
6966636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
6976636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
6985dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
6996636e97aSMatthew G Knepley 
7002764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
70114f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
702e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
703e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
704732e2eb9SMatthew G Knepley 
7056bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
706435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
707732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
70847c6ae99SBarry Smith   PetscFunctionReturn(0);
70947c6ae99SBarry Smith }
71047c6ae99SBarry Smith 
711d7bf68aeSBarry Smith /*@
712d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
713d7bf68aeSBarry Smith 
714d7bf68aeSBarry Smith     Collective on DM
715d7bf68aeSBarry Smith 
716d7bf68aeSBarry Smith     Input Parameter:
717d7bf68aeSBarry Smith .   dm - the DM object to setup
718d7bf68aeSBarry Smith 
719d7bf68aeSBarry Smith     Level: developer
720d7bf68aeSBarry Smith 
721e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
722d7bf68aeSBarry Smith 
723d7bf68aeSBarry Smith @*/
7247087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
725d7bf68aeSBarry Smith {
726d7bf68aeSBarry Smith   PetscErrorCode ierr;
727d7bf68aeSBarry Smith 
728d7bf68aeSBarry Smith   PetscFunctionBegin;
729171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7308387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
731d7bf68aeSBarry Smith   if (dm->ops->setup) {
732d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
733d7bf68aeSBarry Smith   }
7348387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
735d7bf68aeSBarry Smith   PetscFunctionReturn(0);
736d7bf68aeSBarry Smith }
737d7bf68aeSBarry Smith 
738d7bf68aeSBarry Smith /*@
739d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
740d7bf68aeSBarry Smith 
741d7bf68aeSBarry Smith     Collective on DM
742d7bf68aeSBarry Smith 
743d7bf68aeSBarry Smith     Input Parameter:
744d7bf68aeSBarry Smith .   dm - the DM object to set options for
745d7bf68aeSBarry Smith 
746732e2eb9SMatthew G Knepley     Options Database:
7474164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7484164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7494164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
7504164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
751732e2eb9SMatthew G Knepley 
752d7bf68aeSBarry Smith     Level: developer
753d7bf68aeSBarry Smith 
754e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
755d7bf68aeSBarry Smith 
756d7bf68aeSBarry Smith @*/
7577087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
758d7bf68aeSBarry Smith {
7597781c08eSBarry Smith   char           typeName[256];
760ca266f36SBarry Smith   PetscBool      flg;
761d7bf68aeSBarry Smith   PetscErrorCode ierr;
762d7bf68aeSBarry Smith 
763d7bf68aeSBarry Smith   PetscFunctionBegin;
764171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
765f1fd5e65SToby Isaac   if (dm->prob) {
766f1fd5e65SToby Isaac     ierr = PetscDSSetFromOptions(dm->prob);CHKERRQ(ierr);
767f1fd5e65SToby Isaac   }
768691be533SLawrence Mitchell   if (dm->sf) {
769691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
770691be533SLawrence Mitchell   }
771691be533SLawrence Mitchell   if (dm->defaultSF) {
772691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
773691be533SLawrence Mitchell   }
7743194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
7750298fd71SBarry 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);
776a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
777f9ba7244SBarry Smith   if (flg) {
778f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
779f9ba7244SBarry Smith   }
780a264d7a6SBarry 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);
781073dac72SJed Brown   if (flg) {
782521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
783073dac72SJed Brown   }
7840298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
785f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
786e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
787f9ba7244SBarry Smith   }
788f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
7890633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
79082fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
791d7bf68aeSBarry Smith   PetscFunctionReturn(0);
792d7bf68aeSBarry Smith }
793d7bf68aeSBarry Smith 
794fc9bc008SSatish Balay /*@C
795224748a4SBarry Smith     DMView - Views a DM
79647c6ae99SBarry Smith 
79747c6ae99SBarry Smith     Collective on DM
79847c6ae99SBarry Smith 
79947c6ae99SBarry Smith     Input Parameter:
80047c6ae99SBarry Smith +   dm - the DM object to view
80147c6ae99SBarry Smith -   v - the viewer
80247c6ae99SBarry Smith 
803224748a4SBarry Smith     Level: beginner
80447c6ae99SBarry Smith 
805e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
80647c6ae99SBarry Smith 
80747c6ae99SBarry Smith @*/
8087087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
80947c6ae99SBarry Smith {
81047c6ae99SBarry Smith   PetscErrorCode    ierr;
81132c0f0efSBarry Smith   PetscBool         isbinary;
81276a8abe0SBarry Smith   PetscMPIInt       size;
81376a8abe0SBarry Smith   PetscViewerFormat format;
81447c6ae99SBarry Smith 
81547c6ae99SBarry Smith   PetscFunctionBegin;
816171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8173014e516SBarry Smith   if (!v) {
818ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8193014e516SBarry Smith   }
82076a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
82176a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
82276a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
82398c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
82432c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
82532c0f0efSBarry Smith   if (isbinary) {
82655849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
82732c0f0efSBarry Smith     char     type[256];
82832c0f0efSBarry Smith 
82932c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
83032c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
83132c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
83232c0f0efSBarry Smith   }
8330c010503SBarry Smith   if (dm->ops->view) {
8340c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
83547c6ae99SBarry Smith   }
83647c6ae99SBarry Smith   PetscFunctionReturn(0);
83747c6ae99SBarry Smith }
83847c6ae99SBarry Smith 
83947c6ae99SBarry Smith /*@
8408472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
84147c6ae99SBarry Smith 
84247c6ae99SBarry Smith     Collective on DM
84347c6ae99SBarry Smith 
84447c6ae99SBarry Smith     Input Parameter:
84547c6ae99SBarry Smith .   dm - the DM object
84647c6ae99SBarry Smith 
84747c6ae99SBarry Smith     Output Parameter:
84847c6ae99SBarry Smith .   vec - the global vector
84947c6ae99SBarry Smith 
850073dac72SJed Brown     Level: beginner
85147c6ae99SBarry Smith 
852e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
85347c6ae99SBarry Smith 
85447c6ae99SBarry Smith @*/
8557087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
85647c6ae99SBarry Smith {
85747c6ae99SBarry Smith   PetscErrorCode ierr;
85847c6ae99SBarry Smith 
85947c6ae99SBarry Smith   PetscFunctionBegin;
860171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
86147c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
86247c6ae99SBarry Smith   PetscFunctionReturn(0);
86347c6ae99SBarry Smith }
86447c6ae99SBarry Smith 
86547c6ae99SBarry Smith /*@
8668472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith     Not Collective
86947c6ae99SBarry Smith 
87047c6ae99SBarry Smith     Input Parameter:
87147c6ae99SBarry Smith .   dm - the DM object
87247c6ae99SBarry Smith 
87347c6ae99SBarry Smith     Output Parameter:
87447c6ae99SBarry Smith .   vec - the local vector
87547c6ae99SBarry Smith 
876073dac72SJed Brown     Level: beginner
87747c6ae99SBarry Smith 
878e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
87947c6ae99SBarry Smith 
88047c6ae99SBarry Smith @*/
8817087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
88247c6ae99SBarry Smith {
88347c6ae99SBarry Smith   PetscErrorCode ierr;
88447c6ae99SBarry Smith 
88547c6ae99SBarry Smith   PetscFunctionBegin;
886171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
88747c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
88847c6ae99SBarry Smith   PetscFunctionReturn(0);
88947c6ae99SBarry Smith }
89047c6ae99SBarry Smith 
8911411c6eeSJed Brown /*@
8921411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8931411c6eeSJed Brown 
8941411c6eeSJed Brown    Collective on DM
8951411c6eeSJed Brown 
8961411c6eeSJed Brown    Input Parameter:
8971411c6eeSJed Brown .  dm - the DM that provides the mapping
8981411c6eeSJed Brown 
8991411c6eeSJed Brown    Output Parameter:
9001411c6eeSJed Brown .  ltog - the mapping
9011411c6eeSJed Brown 
9021411c6eeSJed Brown    Level: intermediate
9031411c6eeSJed Brown 
9041411c6eeSJed Brown    Notes:
9051411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9061411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9071411c6eeSJed Brown 
908fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9091411c6eeSJed Brown @*/
9107087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9111411c6eeSJed Brown {
9120be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9131411c6eeSJed Brown   PetscErrorCode ierr;
9141411c6eeSJed Brown 
9151411c6eeSJed Brown   PetscFunctionBegin;
9161411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9171411c6eeSJed Brown   PetscValidPointer(ltog,2);
9181411c6eeSJed Brown   if (!dm->ltogmap) {
91937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
92037d0c07bSMatthew G Knepley 
92137d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
92237d0c07bSMatthew G Knepley     if (section) {
923a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
92437d0c07bSMatthew G Knepley       PetscInt       *ltog;
925ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
92637d0c07bSMatthew G Knepley 
92737d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
92837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
929ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
930ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
93137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
932a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
93337d0c07bSMatthew G Knepley 
93437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
93537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
936a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
937a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
93837d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
9391a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
9401a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
9411a7dc684SMatthew G. Knepley         if (dof) {
942b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
943b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
9441a7dc684SMatthew G. Knepley         }
94537d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
946a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
947a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
94837d0c07bSMatthew G Knepley         }
94937d0c07bSMatthew G Knepley       }
950bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
9510be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
9520be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
9530be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
9540be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
9557591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
9567591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
957ccf3bd66SMatthew G. Knepley       if (bs > 1) {
958ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
959ccf3bd66SMatthew G. Knepley         n /= bs;
960ccf3bd66SMatthew G. Knepley       }
961ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9623bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
96337d0c07bSMatthew G Knepley     } else {
964184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
965184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9661411c6eeSJed Brown     }
96737d0c07bSMatthew G Knepley   }
9681411c6eeSJed Brown   *ltog = dm->ltogmap;
9691411c6eeSJed Brown   PetscFunctionReturn(0);
9701411c6eeSJed Brown }
9711411c6eeSJed Brown 
9721411c6eeSJed Brown /*@
9731411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9741411c6eeSJed Brown 
9751411c6eeSJed Brown    Not Collective
9761411c6eeSJed Brown 
9771411c6eeSJed Brown    Input Parameter:
9781411c6eeSJed Brown .  dm - the DM with block structure
9791411c6eeSJed Brown 
9801411c6eeSJed Brown    Output Parameter:
9811411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
9821411c6eeSJed Brown 
9831411c6eeSJed Brown    Level: intermediate
9841411c6eeSJed Brown 
985fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
9861411c6eeSJed Brown @*/
9877087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
9881411c6eeSJed Brown {
9891411c6eeSJed Brown   PetscFunctionBegin;
9901411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9911411c6eeSJed Brown   PetscValidPointer(bs,2);
9921411c6eeSJed 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");
9931411c6eeSJed Brown   *bs = dm->bs;
9941411c6eeSJed Brown   PetscFunctionReturn(0);
9951411c6eeSJed Brown }
9961411c6eeSJed Brown 
99747c6ae99SBarry Smith /*@
9988472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith     Collective on DM
100147c6ae99SBarry Smith 
100247c6ae99SBarry Smith     Input Parameter:
100347c6ae99SBarry Smith +   dm1 - the DM object
100447c6ae99SBarry Smith -   dm2 - the second, finer DM object
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith     Output Parameter:
100747c6ae99SBarry Smith +  mat - the interpolation
100847c6ae99SBarry Smith -  vec - the scaling (optional)
100947c6ae99SBarry Smith 
101047c6ae99SBarry Smith     Level: developer
101147c6ae99SBarry Smith 
101285afcc9aSBarry 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
101385afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1014d52bd9f3SBarry Smith 
10151f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1016d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
101785afcc9aSBarry Smith 
101885afcc9aSBarry Smith 
10193ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
102047c6ae99SBarry Smith 
102147c6ae99SBarry Smith @*/
1022e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
102347c6ae99SBarry Smith {
102447c6ae99SBarry Smith   PetscErrorCode ierr;
102547c6ae99SBarry Smith 
102647c6ae99SBarry Smith   PetscFunctionBegin;
1027171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1028171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1029cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
103025296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1031cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
103247c6ae99SBarry Smith   PetscFunctionReturn(0);
103347c6ae99SBarry Smith }
103447c6ae99SBarry Smith 
10353ad4599aSBarry Smith /*@
10363ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
10373ad4599aSBarry Smith 
10383ad4599aSBarry Smith     Collective on DM
10393ad4599aSBarry Smith 
10403ad4599aSBarry Smith     Input Parameter:
10413ad4599aSBarry Smith +   dm1 - the DM object
10423ad4599aSBarry Smith -   dm2 - the second, finer DM object
10433ad4599aSBarry Smith 
10443ad4599aSBarry Smith     Output Parameter:
10453ad4599aSBarry Smith .  mat - the restriction
10463ad4599aSBarry Smith 
10473ad4599aSBarry Smith 
10483ad4599aSBarry Smith     Level: developer
10493ad4599aSBarry Smith 
10503ad4599aSBarry 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
10513ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
10523ad4599aSBarry Smith 
10533ad4599aSBarry Smith 
10543ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
10553ad4599aSBarry Smith 
10563ad4599aSBarry Smith @*/
10573ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
10583ad4599aSBarry Smith {
10593ad4599aSBarry Smith   PetscErrorCode ierr;
10603ad4599aSBarry Smith 
10613ad4599aSBarry Smith   PetscFunctionBegin;
10623ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
10633ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10643ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10657294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
10663ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
10673ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10683ad4599aSBarry Smith   PetscFunctionReturn(0);
10693ad4599aSBarry Smith }
10703ad4599aSBarry Smith 
107147c6ae99SBarry Smith /*@
10728472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
107347c6ae99SBarry Smith 
107447c6ae99SBarry Smith     Collective on DM
107547c6ae99SBarry Smith 
107647c6ae99SBarry Smith     Input Parameter:
107747c6ae99SBarry Smith +   dm1 - the DM object
107847c6ae99SBarry Smith -   dm2 - the second, finer DM object
107947c6ae99SBarry Smith 
108047c6ae99SBarry Smith     Output Parameter:
10816dbf9973SLawrence Mitchell .   mat - the injection
108247c6ae99SBarry Smith 
108347c6ae99SBarry Smith     Level: developer
108447c6ae99SBarry Smith 
108585afcc9aSBarry 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
108685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
108785afcc9aSBarry Smith 
1088e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
108947c6ae99SBarry Smith 
109047c6ae99SBarry Smith @*/
10916dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
109247c6ae99SBarry Smith {
109347c6ae99SBarry Smith   PetscErrorCode ierr;
109447c6ae99SBarry Smith 
109547c6ae99SBarry Smith   PetscFunctionBegin;
1096171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1097171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10980c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
10996dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
110047c6ae99SBarry Smith   PetscFunctionReturn(0);
110147c6ae99SBarry Smith }
110247c6ae99SBarry Smith 
1103b412c318SBarry Smith /*@
1104bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1105bd041c0cSMatthew G. Knepley 
1106bd041c0cSMatthew G. Knepley   Collective on DM
1107bd041c0cSMatthew G. Knepley 
1108bd041c0cSMatthew G. Knepley   Input Parameter:
1109bd041c0cSMatthew G. Knepley + dm1 - the DM object
1110bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1111bd041c0cSMatthew G. Knepley 
1112bd041c0cSMatthew G. Knepley   Output Parameter:
1113bd041c0cSMatthew G. Knepley . mat - the interpolation
1114bd041c0cSMatthew G. Knepley 
1115bd041c0cSMatthew G. Knepley   Level: developer
1116bd041c0cSMatthew G. Knepley 
1117bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1118bd041c0cSMatthew G. Knepley @*/
1119bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1120bd041c0cSMatthew G. Knepley {
1121bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1122bd041c0cSMatthew G. Knepley 
1123bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1124bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1125bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1126bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1127bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1128bd041c0cSMatthew G. Knepley }
1129bd041c0cSMatthew G. Knepley 
1130bd041c0cSMatthew G. Knepley /*@
1131b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
113247c6ae99SBarry Smith 
113347c6ae99SBarry Smith     Collective on DM
113447c6ae99SBarry Smith 
113547c6ae99SBarry Smith     Input Parameter:
113647c6ae99SBarry Smith +   dm - the DM object
11375bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
113847c6ae99SBarry Smith 
113947c6ae99SBarry Smith     Output Parameter:
114047c6ae99SBarry Smith .   coloring - the coloring
114147c6ae99SBarry Smith 
114247c6ae99SBarry Smith     Level: developer
114347c6ae99SBarry Smith 
1144b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
114547c6ae99SBarry Smith 
1146aab9d709SJed Brown @*/
1147b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
114847c6ae99SBarry Smith {
114947c6ae99SBarry Smith   PetscErrorCode ierr;
115047c6ae99SBarry Smith 
115147c6ae99SBarry Smith   PetscFunctionBegin;
1152171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1153ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1154b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
115547c6ae99SBarry Smith   PetscFunctionReturn(0);
115647c6ae99SBarry Smith }
115747c6ae99SBarry Smith 
1158b412c318SBarry Smith /*@
11598472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
116047c6ae99SBarry Smith 
116147c6ae99SBarry Smith     Collective on DM
116247c6ae99SBarry Smith 
116347c6ae99SBarry Smith     Input Parameter:
1164b412c318SBarry Smith .   dm - the DM object
116547c6ae99SBarry Smith 
116647c6ae99SBarry Smith     Output Parameter:
116747c6ae99SBarry Smith .   mat - the empty Jacobian
116847c6ae99SBarry Smith 
1169073dac72SJed Brown     Level: beginner
117047c6ae99SBarry Smith 
117194013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
117294013140SBarry Smith        do not need to do it yourself.
117394013140SBarry Smith 
117494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
11757889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
117694013140SBarry Smith 
117794013140SBarry 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
117894013140SBarry Smith        internally by PETSc.
117994013140SBarry Smith 
118094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1181aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
118294013140SBarry Smith 
1183b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
118447c6ae99SBarry Smith 
1185aab9d709SJed Brown @*/
1186b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
118747c6ae99SBarry Smith {
118847c6ae99SBarry Smith   PetscErrorCode ierr;
118947c6ae99SBarry Smith 
119047c6ae99SBarry Smith   PetscFunctionBegin;
1191171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1192607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1193c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1194c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1195b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
119647c6ae99SBarry Smith   PetscFunctionReturn(0);
119747c6ae99SBarry Smith }
119847c6ae99SBarry Smith 
1199732e2eb9SMatthew G Knepley /*@
1200950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1201732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1202732e2eb9SMatthew G Knepley 
12038472ad0fSDave May   Logically Collective on DM
1204732e2eb9SMatthew G Knepley 
1205732e2eb9SMatthew G Knepley   Input Parameter:
1206732e2eb9SMatthew G Knepley + dm - the DM
1207732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1208732e2eb9SMatthew G Knepley 
1209732e2eb9SMatthew G Knepley   Level: developer
1210b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1211732e2eb9SMatthew G Knepley @*/
1212732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1213732e2eb9SMatthew G Knepley {
1214732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1215732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1216732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1217732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1218732e2eb9SMatthew G Knepley }
1219732e2eb9SMatthew G Knepley 
1220b06ff27eSHong Zhang /*@
1221b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1222b06ff27eSHong Zhang     but the array for values will not be allocated.
1223b06ff27eSHong Zhang 
1224b06ff27eSHong Zhang   Logically Collective on DM
1225b06ff27eSHong Zhang 
1226b06ff27eSHong Zhang   Input Parameter:
1227b06ff27eSHong Zhang + dm - the DM
1228b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1229b06ff27eSHong Zhang 
1230b06ff27eSHong Zhang   Level: developer
1231b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1232b06ff27eSHong Zhang @*/
1233b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1234b06ff27eSHong Zhang {
1235b06ff27eSHong Zhang   PetscFunctionBegin;
1236b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1237b06ff27eSHong Zhang   dm->structure_only = only;
1238b06ff27eSHong Zhang   PetscFunctionReturn(0);
1239b06ff27eSHong Zhang }
1240b06ff27eSHong Zhang 
1241a89ea682SMatthew G Knepley /*@C
1242aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1243a89ea682SMatthew G Knepley 
1244a89ea682SMatthew G Knepley   Not Collective
1245a89ea682SMatthew G Knepley 
1246a89ea682SMatthew G Knepley   Input Parameters:
1247a89ea682SMatthew G Knepley + dm - the DM object
1248aa1993deSMatthew G Knepley . count - The minium size
124969291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1250a89ea682SMatthew G Knepley 
1251a89ea682SMatthew G Knepley   Output Parameter:
1252a89ea682SMatthew G Knepley . array - the work array
1253a89ea682SMatthew G Knepley 
1254a89ea682SMatthew G Knepley   Level: developer
1255a89ea682SMatthew G Knepley 
1256a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1257a89ea682SMatthew G Knepley @*/
125869291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1259a89ea682SMatthew G Knepley {
1260a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1261aa1993deSMatthew G Knepley   DMWorkLink     link;
126269291d52SBarry Smith   PetscMPIInt    dsize;
1263a89ea682SMatthew G Knepley 
1264a89ea682SMatthew G Knepley   PetscFunctionBegin;
1265a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1266aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1267aa1993deSMatthew G Knepley   if (dm->workin) {
1268aa1993deSMatthew G Knepley     link       = dm->workin;
1269aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1270aa1993deSMatthew G Knepley   } else {
1271b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1272a89ea682SMatthew G Knepley   }
127369291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
12745056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1275aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1276854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1277854ce69bSBarry Smith     link->bytes = dsize*count;
1278aa1993deSMatthew G Knepley   }
1279aa1993deSMatthew G Knepley   link->next   = dm->workout;
1280aa1993deSMatthew G Knepley   dm->workout  = link;
1281aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1282a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1283a89ea682SMatthew G Knepley }
1284a89ea682SMatthew G Knepley 
1285aa1993deSMatthew G Knepley /*@C
1286aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1287aa1993deSMatthew G Knepley 
1288aa1993deSMatthew G Knepley   Not Collective
1289aa1993deSMatthew G Knepley 
1290aa1993deSMatthew G Knepley   Input Parameters:
1291aa1993deSMatthew G Knepley + dm - the DM object
1292aa1993deSMatthew G Knepley . count - The minium size
129369291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1294aa1993deSMatthew G Knepley 
1295aa1993deSMatthew G Knepley   Output Parameter:
1296aa1993deSMatthew G Knepley . array - the work array
1297aa1993deSMatthew G Knepley 
1298aa1993deSMatthew G Knepley   Level: developer
1299aa1993deSMatthew G Knepley 
130069291d52SBarry Smith   Developer Notes: count and dtype are ignored, they are only needed for DMGetWorkArray()
1301aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1302aa1993deSMatthew G Knepley @*/
130369291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1304aa1993deSMatthew G Knepley {
1305aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1306aa1993deSMatthew G Knepley 
1307aa1993deSMatthew G Knepley   PetscFunctionBegin;
1308aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1309aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1310aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1311aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1312aa1993deSMatthew G Knepley       *p           = link->next;
1313aa1993deSMatthew G Knepley       link->next   = dm->workin;
1314aa1993deSMatthew G Knepley       dm->workin   = link;
13150298fd71SBarry Smith       *(void**)mem = NULL;
1316aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1317aa1993deSMatthew G Knepley     }
1318aa1993deSMatthew G Knepley   }
1319aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1320aa1993deSMatthew G Knepley }
1321e7c4fc90SDmitry Karpeev 
1322435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1323435a35e8SMatthew G Knepley {
1324435a35e8SMatthew G Knepley   PetscFunctionBegin;
1325435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
132682f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1327435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1328435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1329435a35e8SMatthew G Knepley }
1330435a35e8SMatthew G Knepley 
13314f3b5142SJed Brown /*@C
13324d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
13334d343eeaSMatthew G Knepley 
13344d343eeaSMatthew G Knepley   Not collective
13354d343eeaSMatthew G Knepley 
13364d343eeaSMatthew G Knepley   Input Parameter:
13374d343eeaSMatthew G Knepley . dm - the DM object
13384d343eeaSMatthew G Knepley 
13394d343eeaSMatthew G Knepley   Output Parameters:
13400298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
13410298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
13420298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
13434d343eeaSMatthew G Knepley 
13444d343eeaSMatthew G Knepley   Level: intermediate
13454d343eeaSMatthew G Knepley 
134621c9b008SJed Brown   Notes:
134721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
134821c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
134921c9b008SJed Brown   PetscFree().
135021c9b008SJed Brown 
13514d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
13524d343eeaSMatthew G Knepley @*/
135337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
13544d343eeaSMatthew G Knepley {
135537d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
13564d343eeaSMatthew G Knepley   PetscErrorCode ierr;
13574d343eeaSMatthew G Knepley 
13584d343eeaSMatthew G Knepley   PetscFunctionBegin;
13594d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
136069ca1f37SDmitry Karpeev   if (numFields) {
136169ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
136269ca1f37SDmitry Karpeev     *numFields = 0;
136369ca1f37SDmitry Karpeev   }
136437d0c07bSMatthew G Knepley   if (fieldNames) {
136537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
13660298fd71SBarry Smith     *fieldNames = NULL;
136769ca1f37SDmitry Karpeev   }
136869ca1f37SDmitry Karpeev   if (fields) {
136969ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
13700298fd71SBarry Smith     *fields = NULL;
137169ca1f37SDmitry Karpeev   }
137237d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
137337d0c07bSMatthew G Knepley   if (section) {
137437d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
137537d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
137637d0c07bSMatthew G Knepley 
137737d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
137837d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1379dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
138037d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
138137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
138237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
138337d0c07bSMatthew G Knepley     }
138437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
138537d0c07bSMatthew G Knepley       PetscInt gdof;
138637d0c07bSMatthew G Knepley 
138737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
138837d0c07bSMatthew G Knepley       if (gdof > 0) {
138937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
139037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
139137d0c07bSMatthew G Knepley 
139237d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
139337d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
139437d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
139537d0c07bSMatthew G Knepley         }
139637d0c07bSMatthew G Knepley       }
139737d0c07bSMatthew G Knepley     }
139837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1399785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
140037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
140137d0c07bSMatthew G Knepley     }
140237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
140337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
140437d0c07bSMatthew G Knepley 
140537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
140637d0c07bSMatthew G Knepley       if (gdof > 0) {
140737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
140837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
140937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
141037d0c07bSMatthew G Knepley 
141137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
141237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
141337d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
141437d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
141537d0c07bSMatthew G Knepley           }
141637d0c07bSMatthew G Knepley         }
141737d0c07bSMatthew G Knepley       }
141837d0c07bSMatthew G Knepley     }
14198865f1eaSKarl Rupp     if (numFields) *numFields = nF;
142037d0c07bSMatthew G Knepley     if (fieldNames) {
1421785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
142237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
142337d0c07bSMatthew G Knepley         const char *fieldName;
142437d0c07bSMatthew G Knepley 
142537d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
142637d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
142737d0c07bSMatthew G Knepley       }
142837d0c07bSMatthew G Knepley     }
142937d0c07bSMatthew G Knepley     if (fields) {
1430785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
143137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
143282f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
143337d0c07bSMatthew G Knepley       }
143437d0c07bSMatthew G Knepley     }
143537d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
14368865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
14378865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
143869ca1f37SDmitry Karpeev   }
14394d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
14404d343eeaSMatthew G Knepley }
14414d343eeaSMatthew G Knepley 
144216621825SDmitry Karpeev 
144316621825SDmitry Karpeev /*@C
144416621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
144516621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
144616621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1447e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1448e7c4fc90SDmitry Karpeev 
1449e7c4fc90SDmitry Karpeev   Not collective
1450e7c4fc90SDmitry Karpeev 
1451e7c4fc90SDmitry Karpeev   Input Parameter:
1452e7c4fc90SDmitry Karpeev . dm - the DM object
1453e7c4fc90SDmitry Karpeev 
1454e7c4fc90SDmitry Karpeev   Output Parameters:
14550298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
14560298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
14570298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
14580298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1459e7c4fc90SDmitry Karpeev 
1460e7c4fc90SDmitry Karpeev   Level: intermediate
1461e7c4fc90SDmitry Karpeev 
1462e7c4fc90SDmitry Karpeev   Notes:
1463e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1464e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1465e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1466e7c4fc90SDmitry Karpeev 
1467e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1468e7c4fc90SDmitry Karpeev @*/
146916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1470e7c4fc90SDmitry Karpeev {
1471e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1472e7c4fc90SDmitry Karpeev 
1473e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1474e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14758865f1eaSKarl Rupp   if (len) {
14768865f1eaSKarl Rupp     PetscValidPointer(len,2);
14778865f1eaSKarl Rupp     *len = 0;
14788865f1eaSKarl Rupp   }
14798865f1eaSKarl Rupp   if (namelist) {
14808865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
14818865f1eaSKarl Rupp     *namelist = 0;
14828865f1eaSKarl Rupp   }
14838865f1eaSKarl Rupp   if (islist) {
14848865f1eaSKarl Rupp     PetscValidPointer(islist,4);
14858865f1eaSKarl Rupp     *islist = 0;
14868865f1eaSKarl Rupp   }
14878865f1eaSKarl Rupp   if (dmlist) {
14888865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
14898865f1eaSKarl Rupp     *dmlist = 0;
14908865f1eaSKarl Rupp   }
1491f3f0edfdSDmitry Karpeev   /*
1492f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1493f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1494f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1495f3f0edfdSDmitry Karpeev    */
1496ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
149716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1498435a35e8SMatthew G Knepley     PetscSection section;
1499435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1500435a35e8SMatthew G Knepley 
1501435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1502435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1503435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1504f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
150503dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
150603dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
150703dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1508435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1509435a35e8SMatthew G Knepley         const char *fieldName;
1510435a35e8SMatthew G Knepley 
151103dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
151203dc3394SMatthew G. Knepley         if (namelist) {
1513435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1514435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1515435a35e8SMatthew G Knepley         }
151603dc3394SMatthew G. Knepley       }
1517435a35e8SMatthew G Knepley     } else {
151869ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1519e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
15200298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1521e7c4fc90SDmitry Karpeev     }
15228865f1eaSKarl Rupp   } else {
152316621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
152416621825SDmitry Karpeev   }
152516621825SDmitry Karpeev   PetscFunctionReturn(0);
152616621825SDmitry Karpeev }
152716621825SDmitry Karpeev 
1528564cec59SMatthew G. Knepley /*@
1529435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1530435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1531435a35e8SMatthew G Knepley 
1532435a35e8SMatthew G Knepley   Not collective
1533435a35e8SMatthew G Knepley 
1534435a35e8SMatthew G Knepley   Input Parameters:
15352adcc780SMatthew G. Knepley + dm        - The DM object
15362adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
15372adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1538435a35e8SMatthew G Knepley 
1539435a35e8SMatthew G Knepley   Output Parameters:
15402adcc780SMatthew G. Knepley + is - The global indices for the subproblem
15412adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1542435a35e8SMatthew G Knepley 
1543435a35e8SMatthew G Knepley   Level: intermediate
1544435a35e8SMatthew G Knepley 
1545435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1546435a35e8SMatthew G Knepley @*/
154737bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1548435a35e8SMatthew G Knepley {
1549435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1550435a35e8SMatthew G Knepley 
1551435a35e8SMatthew G Knepley   PetscFunctionBegin;
1552435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1553435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
15548865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
15558865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1556435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1557435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
155882f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1559435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1560435a35e8SMatthew G Knepley }
1561435a35e8SMatthew G Knepley 
15622adcc780SMatthew G. Knepley /*@C
15632adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
15642adcc780SMatthew G. Knepley 
15652adcc780SMatthew G. Knepley   Not collective
15662adcc780SMatthew G. Knepley 
15672adcc780SMatthew G. Knepley   Input Parameter:
15682adcc780SMatthew G. Knepley + dms - The DM objects
15692adcc780SMatthew G. Knepley - len - The number of DMs
15702adcc780SMatthew G. Knepley 
15712adcc780SMatthew G. Knepley   Output Parameters:
15722adcc780SMatthew G. Knepley + is - The global indices for the subproblem
15732adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
15742adcc780SMatthew G. Knepley 
15752adcc780SMatthew G. Knepley   Level: intermediate
15762adcc780SMatthew G. Knepley 
15772adcc780SMatthew G. Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
15782adcc780SMatthew G. Knepley @*/
15792adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
15802adcc780SMatthew G. Knepley {
15812adcc780SMatthew G. Knepley   PetscInt       i;
15822adcc780SMatthew G. Knepley   PetscErrorCode ierr;
15832adcc780SMatthew G. Knepley 
15842adcc780SMatthew G. Knepley   PetscFunctionBegin;
15852adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
15862adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
15872adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
15882adcc780SMatthew G. Knepley   if (superdm) PetscValidPointer(superdm,4);
15892adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
15902adcc780SMatthew G. Knepley   if (len) {
15912adcc780SMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {
15922adcc780SMatthew G. Knepley       ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
15932adcc780SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
15942adcc780SMatthew G. Knepley   }
15952adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
15962adcc780SMatthew G. Knepley }
15972adcc780SMatthew G. Knepley 
159816621825SDmitry Karpeev 
159916621825SDmitry Karpeev /*@C
16008d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
16018d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
16028d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
16038d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
16048d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
160516621825SDmitry Karpeev 
160616621825SDmitry Karpeev   Not collective
160716621825SDmitry Karpeev 
160816621825SDmitry Karpeev   Input Parameter:
160916621825SDmitry Karpeev . dm - the DM object
161016621825SDmitry Karpeev 
161116621825SDmitry Karpeev   Output Parameters:
16120298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
16130298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
16140298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
16150298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
16160298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
161716621825SDmitry Karpeev 
161816621825SDmitry Karpeev   Level: intermediate
161916621825SDmitry Karpeev 
162016621825SDmitry Karpeev   Notes:
162116621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
162216621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
162316621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
162416621825SDmitry Karpeev 
16258d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
162616621825SDmitry Karpeev @*/
16278d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
162816621825SDmitry Karpeev {
162916621825SDmitry Karpeev   PetscErrorCode      ierr;
1630be081cd6SPeter Brune   DMSubDomainHookLink link;
1631be081cd6SPeter Brune   PetscInt            i,l;
163216621825SDmitry Karpeev 
163316621825SDmitry Karpeev   PetscFunctionBegin;
163416621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
163514a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
16360298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
16370298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
16380298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
16390298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1640f3f0edfdSDmitry Karpeev   /*
1641f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1642f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1643f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1644f3f0edfdSDmitry Karpeev    */
1645ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
164616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1647be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
164814a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1649f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1650be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1651be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1652be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1653be081cd6SPeter Brune         }
1654648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1655e7c4fc90SDmitry Karpeev       }
165614a18fd3SPeter Brune     }
165714a18fd3SPeter Brune     if (len) *len = l;
165814a18fd3SPeter Brune   }
1659e30e807fSPeter Brune   PetscFunctionReturn(0);
1660e30e807fSPeter Brune }
1661e30e807fSPeter Brune 
1662e30e807fSPeter Brune 
1663e30e807fSPeter Brune /*@C
1664e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1665e30e807fSPeter Brune 
1666e30e807fSPeter Brune   Not collective
1667e30e807fSPeter Brune 
1668e30e807fSPeter Brune   Input Parameters:
1669e30e807fSPeter Brune + dm - the DM object
1670e30e807fSPeter Brune . n  - the number of subdomain scatters
1671e30e807fSPeter Brune - subdms - the local subdomains
1672e30e807fSPeter Brune 
1673e30e807fSPeter Brune   Output Parameters:
1674e30e807fSPeter Brune + n     - the number of scatters returned
1675e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1676e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1677e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1678e30e807fSPeter Brune 
1679e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1680e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1681e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1682e30e807fSPeter Brune   solution and residual data.
1683e30e807fSPeter Brune 
1684e30e807fSPeter Brune   Level: developer
1685e30e807fSPeter Brune 
1686e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1687e30e807fSPeter Brune @*/
1688e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1689e30e807fSPeter Brune {
1690e30e807fSPeter Brune   PetscErrorCode ierr;
1691e30e807fSPeter Brune 
1692e30e807fSPeter Brune   PetscFunctionBegin;
1693e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1694e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1695e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1696e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
16976668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1698e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1699e7c4fc90SDmitry Karpeev }
1700e7c4fc90SDmitry Karpeev 
170147c6ae99SBarry Smith /*@
170247c6ae99SBarry Smith   DMRefine - Refines a DM object
170347c6ae99SBarry Smith 
170447c6ae99SBarry Smith   Collective on DM
170547c6ae99SBarry Smith 
170647c6ae99SBarry Smith   Input Parameter:
170747c6ae99SBarry Smith + dm   - the DM object
170891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
170947c6ae99SBarry Smith 
171047c6ae99SBarry Smith   Output Parameter:
17110298fd71SBarry Smith . dmf - the refined DM, or NULL
1712ae0a1c52SMatthew G Knepley 
17130298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
171447c6ae99SBarry Smith 
171547c6ae99SBarry Smith   Level: developer
171647c6ae99SBarry Smith 
1717e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
171847c6ae99SBarry Smith @*/
17197087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
172047c6ae99SBarry Smith {
172147c6ae99SBarry Smith   PetscErrorCode   ierr;
1722c833c3b5SJed Brown   DMRefineHookLink link;
172347c6ae99SBarry Smith 
172447c6ae99SBarry Smith   PetscFunctionBegin;
1725732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17261ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1727fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
172847c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
17294057135bSMatthew G Knepley   if (*dmf) {
173043842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
17318865f1eaSKarl Rupp 
17328cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
17338865f1eaSKarl Rupp 
1734644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
17350598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1736656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
17378865f1eaSKarl Rupp 
1738e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1739c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
17408865f1eaSKarl Rupp       if (link->refinehook) {
17418865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
17428865f1eaSKarl Rupp       }
1743c833c3b5SJed Brown     }
1744c833c3b5SJed Brown   }
17451ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1746c833c3b5SJed Brown   PetscFunctionReturn(0);
1747c833c3b5SJed Brown }
1748c833c3b5SJed Brown 
1749bb9467b5SJed Brown /*@C
1750c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1751c833c3b5SJed Brown 
1752c833c3b5SJed Brown    Logically Collective
1753c833c3b5SJed Brown 
1754c833c3b5SJed Brown    Input Arguments:
1755c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1756c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1757c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
17580298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1759c833c3b5SJed Brown 
1760c833c3b5SJed Brown    Calling sequence of refinehook:
1761c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1762c833c3b5SJed Brown 
1763c833c3b5SJed Brown +  coarse - coarse level DM
1764c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1765c833c3b5SJed Brown -  ctx - optional user-defined function context
1766c833c3b5SJed Brown 
1767c833c3b5SJed Brown    Calling sequence for interphook:
1768c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1769c833c3b5SJed Brown 
1770c833c3b5SJed Brown +  coarse - coarse level DM
1771c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1772c833c3b5SJed Brown .  fine - fine level DM to update
1773c833c3b5SJed Brown -  ctx - optional user-defined function context
1774c833c3b5SJed Brown 
1775c833c3b5SJed Brown    Level: advanced
1776c833c3b5SJed Brown 
1777c833c3b5SJed Brown    Notes:
1778c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1779c833c3b5SJed Brown 
1780c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1781c833c3b5SJed Brown 
1782bb9467b5SJed Brown    This function is currently not available from Fortran.
1783bb9467b5SJed Brown 
1784c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1785c833c3b5SJed Brown @*/
1786c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1787c833c3b5SJed Brown {
1788c833c3b5SJed Brown   PetscErrorCode   ierr;
1789c833c3b5SJed Brown   DMRefineHookLink link,*p;
1790c833c3b5SJed Brown 
1791c833c3b5SJed Brown   PetscFunctionBegin;
1792c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
17933d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
17943d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
17953d8e3701SJed Brown   }
179695dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1797c833c3b5SJed Brown   link->refinehook = refinehook;
1798c833c3b5SJed Brown   link->interphook = interphook;
1799c833c3b5SJed Brown   link->ctx        = ctx;
18000298fd71SBarry Smith   link->next       = NULL;
1801c833c3b5SJed Brown   *p               = link;
1802c833c3b5SJed Brown   PetscFunctionReturn(0);
1803c833c3b5SJed Brown }
1804c833c3b5SJed Brown 
18053d8e3701SJed Brown /*@C
18063d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
18073d8e3701SJed Brown 
18083d8e3701SJed Brown    Logically Collective
18093d8e3701SJed Brown 
18103d8e3701SJed Brown    Input Arguments:
18113d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
18123d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
18133d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18143d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
18153d8e3701SJed Brown 
18163d8e3701SJed Brown    Level: advanced
18173d8e3701SJed Brown 
18183d8e3701SJed Brown    Notes:
18193d8e3701SJed Brown    This function does nothing if the hook is not in the list.
18203d8e3701SJed Brown 
18213d8e3701SJed Brown    This function is currently not available from Fortran.
18223d8e3701SJed Brown 
18233d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
18243d8e3701SJed Brown @*/
18253d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
18263d8e3701SJed Brown {
18273d8e3701SJed Brown   PetscErrorCode   ierr;
18283d8e3701SJed Brown   DMRefineHookLink link,*p;
18293d8e3701SJed Brown 
18303d8e3701SJed Brown   PetscFunctionBegin;
18313d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
18323d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
18333d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
18343d8e3701SJed Brown       link = *p;
18353d8e3701SJed Brown       *p = link->next;
18363d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
18373d8e3701SJed Brown       break;
18383d8e3701SJed Brown     }
18393d8e3701SJed Brown   }
18403d8e3701SJed Brown   PetscFunctionReturn(0);
18413d8e3701SJed Brown }
18423d8e3701SJed Brown 
1843c833c3b5SJed Brown /*@
1844c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1845c833c3b5SJed Brown 
1846c833c3b5SJed Brown    Collective if any hooks are
1847c833c3b5SJed Brown 
1848c833c3b5SJed Brown    Input Arguments:
1849c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1850e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1851c833c3b5SJed Brown -  fine - finer DM to update
1852c833c3b5SJed Brown 
1853c833c3b5SJed Brown    Level: developer
1854c833c3b5SJed Brown 
1855c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1856c833c3b5SJed Brown @*/
1857c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1858c833c3b5SJed Brown {
1859c833c3b5SJed Brown   PetscErrorCode   ierr;
1860c833c3b5SJed Brown   DMRefineHookLink link;
1861c833c3b5SJed Brown 
1862c833c3b5SJed Brown   PetscFunctionBegin;
1863c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
18648865f1eaSKarl Rupp     if (link->interphook) {
18658865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
18668865f1eaSKarl Rupp     }
18674057135bSMatthew G Knepley   }
186847c6ae99SBarry Smith   PetscFunctionReturn(0);
186947c6ae99SBarry Smith }
187047c6ae99SBarry Smith 
1871eb3f98d2SBarry Smith /*@
1872eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1873eb3f98d2SBarry Smith 
1874eb3f98d2SBarry Smith     Not Collective
1875eb3f98d2SBarry Smith 
1876eb3f98d2SBarry Smith     Input Parameter:
1877eb3f98d2SBarry Smith .   dm - the DM object
1878eb3f98d2SBarry Smith 
1879eb3f98d2SBarry Smith     Output Parameter:
1880eb3f98d2SBarry Smith .   level - number of refinements
1881eb3f98d2SBarry Smith 
1882eb3f98d2SBarry Smith     Level: developer
1883eb3f98d2SBarry Smith 
18846a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1885eb3f98d2SBarry Smith 
1886eb3f98d2SBarry Smith @*/
1887eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1888eb3f98d2SBarry Smith {
1889eb3f98d2SBarry Smith   PetscFunctionBegin;
1890eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1891eb3f98d2SBarry Smith   *level = dm->levelup;
1892eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1893eb3f98d2SBarry Smith }
1894eb3f98d2SBarry Smith 
1895fef3a512SBarry Smith /*@
1896fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
1897fef3a512SBarry Smith 
1898fef3a512SBarry Smith     Not Collective
1899fef3a512SBarry Smith 
1900fef3a512SBarry Smith     Input Parameter:
1901fef3a512SBarry Smith +   dm - the DM object
1902fef3a512SBarry Smith -   level - number of refinements
1903fef3a512SBarry Smith 
1904fef3a512SBarry Smith     Level: advanced
1905fef3a512SBarry Smith 
1906fef3a512SBarry Smith     Notes: This value is used by PCMG to determine how many multigrid levels to use
1907fef3a512SBarry Smith 
1908fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1909fef3a512SBarry Smith 
1910fef3a512SBarry Smith @*/
1911fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
1912fef3a512SBarry Smith {
1913fef3a512SBarry Smith   PetscFunctionBegin;
1914fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1915fef3a512SBarry Smith   dm->levelup = level;
1916fef3a512SBarry Smith   PetscFunctionReturn(0);
1917fef3a512SBarry Smith }
1918fef3a512SBarry Smith 
1919bb9467b5SJed Brown /*@C
1920baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1921baf369e7SPeter Brune 
1922baf369e7SPeter Brune    Logically Collective
1923baf369e7SPeter Brune 
1924baf369e7SPeter Brune    Input Arguments:
1925baf369e7SPeter Brune +  dm - the DM
1926baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1927baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
19280298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1929baf369e7SPeter Brune 
1930baf369e7SPeter Brune    Calling sequence for beginhook:
1931baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1932baf369e7SPeter Brune 
1933baf369e7SPeter Brune +  dm - global DM
1934baf369e7SPeter Brune .  g - global vector
1935baf369e7SPeter Brune .  mode - mode
1936baf369e7SPeter Brune .  l - local vector
1937baf369e7SPeter Brune -  ctx - optional user-defined function context
1938baf369e7SPeter Brune 
1939baf369e7SPeter Brune 
1940baf369e7SPeter Brune    Calling sequence for endhook:
1941ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1942baf369e7SPeter Brune 
1943baf369e7SPeter Brune +  global - global DM
1944baf369e7SPeter Brune -  ctx - optional user-defined function context
1945baf369e7SPeter Brune 
1946baf369e7SPeter Brune    Level: advanced
1947baf369e7SPeter Brune 
1948baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1949baf369e7SPeter Brune @*/
1950baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1951baf369e7SPeter Brune {
1952baf369e7SPeter Brune   PetscErrorCode          ierr;
1953baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1954baf369e7SPeter Brune 
1955baf369e7SPeter Brune   PetscFunctionBegin;
1956baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1957baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
195895dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
1959baf369e7SPeter Brune   link->beginhook = beginhook;
1960baf369e7SPeter Brune   link->endhook   = endhook;
1961baf369e7SPeter Brune   link->ctx       = ctx;
19620298fd71SBarry Smith   link->next      = NULL;
1963baf369e7SPeter Brune   *p              = link;
1964baf369e7SPeter Brune   PetscFunctionReturn(0);
1965baf369e7SPeter Brune }
1966baf369e7SPeter Brune 
19674c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
19684c274da1SToby Isaac {
19694c274da1SToby Isaac   Mat cMat;
19704c274da1SToby Isaac   Vec cVec;
19714c274da1SToby Isaac   PetscSection section, cSec;
19724c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19734c274da1SToby Isaac   PetscErrorCode ierr;
19744c274da1SToby Isaac 
19754c274da1SToby Isaac   PetscFunctionBegin;
19764c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19774c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19784c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
19795db9a05bSToby Isaac     PetscInt nRows;
19805db9a05bSToby Isaac 
19815db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
19825db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
19834c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19847711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19854c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
19864c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19874c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19884c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19894c274da1SToby Isaac       if (dof) {
19904c274da1SToby Isaac         PetscScalar *vals;
19914c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
19924c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
19934c274da1SToby Isaac       }
19944c274da1SToby Isaac     }
19954c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19964c274da1SToby Isaac   }
19974c274da1SToby Isaac   PetscFunctionReturn(0);
19984c274da1SToby Isaac }
19994c274da1SToby Isaac 
200047c6ae99SBarry Smith /*@
200147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
200247c6ae99SBarry Smith 
200347c6ae99SBarry Smith     Neighbor-wise Collective on DM
200447c6ae99SBarry Smith 
200547c6ae99SBarry Smith     Input Parameters:
200647c6ae99SBarry Smith +   dm - the DM object
200747c6ae99SBarry Smith .   g - the global vector
200847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
200947c6ae99SBarry Smith -   l - the local vector
201047c6ae99SBarry Smith 
201147c6ae99SBarry Smith 
201247c6ae99SBarry Smith     Level: beginner
201347c6ae99SBarry Smith 
2014e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
201547c6ae99SBarry Smith 
201647c6ae99SBarry Smith @*/
20177087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
201847c6ae99SBarry Smith {
20197128ae9fSMatthew G Knepley   PetscSF                 sf;
202047c6ae99SBarry Smith   PetscErrorCode          ierr;
2021baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
202247c6ae99SBarry Smith 
202347c6ae99SBarry Smith   PetscFunctionBegin;
2024171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2025baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
20268865f1eaSKarl Rupp     if (link->beginhook) {
20278865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
20288865f1eaSKarl Rupp     }
2029baf369e7SPeter Brune   }
20307128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
20317128ae9fSMatthew G Knepley   if (sf) {
2032ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2033ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
20347128ae9fSMatthew G Knepley 
203582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20367128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2037ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20387128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20397128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2040ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20417128ae9fSMatthew G Knepley   } else {
2042843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20437128ae9fSMatthew G Knepley   }
204447c6ae99SBarry Smith   PetscFunctionReturn(0);
204547c6ae99SBarry Smith }
204647c6ae99SBarry Smith 
204747c6ae99SBarry Smith /*@
204847c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
204947c6ae99SBarry Smith 
205047c6ae99SBarry Smith     Neighbor-wise Collective on DM
205147c6ae99SBarry Smith 
205247c6ae99SBarry Smith     Input Parameters:
205347c6ae99SBarry Smith +   dm - the DM object
205447c6ae99SBarry Smith .   g - the global vector
205547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
205647c6ae99SBarry Smith -   l - the local vector
205747c6ae99SBarry Smith 
205847c6ae99SBarry Smith 
205947c6ae99SBarry Smith     Level: beginner
206047c6ae99SBarry Smith 
2061e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
206247c6ae99SBarry Smith 
206347c6ae99SBarry Smith @*/
20647087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
206547c6ae99SBarry Smith {
20667128ae9fSMatthew G Knepley   PetscSF                 sf;
206747c6ae99SBarry Smith   PetscErrorCode          ierr;
2068ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2069ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2070baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
207147c6ae99SBarry Smith 
207247c6ae99SBarry Smith   PetscFunctionBegin;
2073171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20747128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
20757128ae9fSMatthew G Knepley   if (sf) {
207682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20777128ae9fSMatthew G Knepley 
20787128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2079ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20807128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20817128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2082ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20837128ae9fSMatthew G Knepley   } else {
2084843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20857128ae9fSMatthew G Knepley   }
20864c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2087baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2088baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2089baf369e7SPeter Brune   }
209047c6ae99SBarry Smith   PetscFunctionReturn(0);
209147c6ae99SBarry Smith }
209247c6ae99SBarry Smith 
2093d4d07f1eSToby Isaac /*@C
2094d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2095d4d07f1eSToby Isaac 
2096d4d07f1eSToby Isaac    Logically Collective
2097d4d07f1eSToby Isaac 
2098d4d07f1eSToby Isaac    Input Arguments:
2099d4d07f1eSToby Isaac +  dm - the DM
2100d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2101d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2102d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2103d4d07f1eSToby Isaac 
2104d4d07f1eSToby Isaac    Calling sequence for beginhook:
2105d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2106d4d07f1eSToby Isaac 
2107d4d07f1eSToby Isaac +  dm - global DM
2108d4d07f1eSToby Isaac .  l - local vector
2109d4d07f1eSToby Isaac .  mode - mode
2110d4d07f1eSToby Isaac .  g - global vector
2111d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2112d4d07f1eSToby Isaac 
2113d4d07f1eSToby Isaac 
2114d4d07f1eSToby Isaac    Calling sequence for endhook:
2115d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2116d4d07f1eSToby Isaac 
2117d4d07f1eSToby Isaac +  global - global DM
2118d4d07f1eSToby Isaac .  l - local vector
2119d4d07f1eSToby Isaac .  mode - mode
2120d4d07f1eSToby Isaac .  g - global vector
2121d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2122d4d07f1eSToby Isaac 
2123d4d07f1eSToby Isaac    Level: advanced
2124d4d07f1eSToby Isaac 
2125d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2126d4d07f1eSToby Isaac @*/
2127d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2128d4d07f1eSToby Isaac {
2129d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2130d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2131d4d07f1eSToby Isaac 
2132d4d07f1eSToby Isaac   PetscFunctionBegin;
2133d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2134d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
213595dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2136d4d07f1eSToby Isaac   link->beginhook = beginhook;
2137d4d07f1eSToby Isaac   link->endhook   = endhook;
2138d4d07f1eSToby Isaac   link->ctx       = ctx;
2139d4d07f1eSToby Isaac   link->next      = NULL;
2140d4d07f1eSToby Isaac   *p              = link;
2141d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2142d4d07f1eSToby Isaac }
2143d4d07f1eSToby Isaac 
21444c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
21454c274da1SToby Isaac {
21464c274da1SToby Isaac   Mat cMat;
21474c274da1SToby Isaac   Vec cVec;
21484c274da1SToby Isaac   PetscSection section, cSec;
21494c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
21504c274da1SToby Isaac   PetscErrorCode ierr;
21514c274da1SToby Isaac 
21524c274da1SToby Isaac   PetscFunctionBegin;
21534c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21544c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
21554c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
21565db9a05bSToby Isaac     PetscInt nRows;
21575db9a05bSToby Isaac 
21585db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21595db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
21604c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
21617711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21624c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21634c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21644c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21654c274da1SToby Isaac       if (dof) {
21664c274da1SToby Isaac         PetscInt d;
21674c274da1SToby Isaac         PetscScalar *vals;
21684c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
21694c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
21704c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
21714c274da1SToby Isaac          * we just extracted */
21724c274da1SToby Isaac         for (d = 0; d < dof; d++) {
21734c274da1SToby Isaac           vals[d] = 0.;
21744c274da1SToby Isaac         }
21754c274da1SToby Isaac       }
21764c274da1SToby Isaac     }
21774c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
21784c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21794c274da1SToby Isaac   }
21804c274da1SToby Isaac   PetscFunctionReturn(0);
21814c274da1SToby Isaac }
21824c274da1SToby Isaac 
218347c6ae99SBarry Smith /*@
21849a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
21859a42bb27SBarry Smith 
21869a42bb27SBarry Smith     Neighbor-wise Collective on DM
21879a42bb27SBarry Smith 
21889a42bb27SBarry Smith     Input Parameters:
21899a42bb27SBarry Smith +   dm - the DM object
2190f6813fd5SJed Brown .   l - the local vector
21911eb28f2eSBarry 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.
21921eb28f2eSBarry Smith -   g - the global vector
21939a42bb27SBarry Smith 
2194d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
219584330215SMatthew 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.
21969a42bb27SBarry Smith 
21979a42bb27SBarry Smith     Level: beginner
21989a42bb27SBarry Smith 
2199e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
22009a42bb27SBarry Smith 
22019a42bb27SBarry Smith @*/
22027087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
22039a42bb27SBarry Smith {
22047128ae9fSMatthew G Knepley   PetscSF                 sf;
220584330215SMatthew G. Knepley   PetscSection            s, gs;
2206d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2207ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2208ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
220984330215SMatthew G. Knepley   PetscBool               isInsert;
221084330215SMatthew G. Knepley   PetscErrorCode          ierr;
22119a42bb27SBarry Smith 
22129a42bb27SBarry Smith   PetscFunctionBegin;
2213171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2214d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2215d4d07f1eSToby Isaac     if (link->beginhook) {
2216d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2217d4d07f1eSToby Isaac     }
2218d4d07f1eSToby Isaac   }
22194c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
22207128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
222184330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
22227128ae9fSMatthew G Knepley   switch (mode) {
22237128ae9fSMatthew G Knepley   case INSERT_VALUES:
22247128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2225304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
222684330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
22277128ae9fSMatthew G Knepley   case ADD_VALUES:
22287128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2229304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
223084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
22317128ae9fSMatthew G Knepley   default:
223282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22337128ae9fSMatthew G Knepley   }
223484330215SMatthew G. Knepley   if (sf && !isInsert) {
2235ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
22367128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2237a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2238ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
223984330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
224084330215SMatthew G. Knepley   } else if (s && isInsert) {
224184330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
224284330215SMatthew G. Knepley 
224384330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
224484330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
224584330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2246ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
224784330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
224884330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2249b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
225084330215SMatthew G. Knepley 
225184330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
225203442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
225384330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2254b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
225584330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
225684330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2257b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
225803442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2259b3b16f48SMatthew 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);
2260b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2261b3b16f48SMatthew G. Knepley       if (!gcdof) {
226284330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2263b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2264b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
226584330215SMatthew G. Knepley         const PetscInt *cdofs;
226684330215SMatthew G. Knepley         PetscInt        cind = 0;
226784330215SMatthew G. Knepley 
226884330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2269b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
227084330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2271b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
227284330215SMatthew G. Knepley         }
2273b3b16f48SMatthew 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);
227484330215SMatthew G. Knepley     }
2275ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22767128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
22777128ae9fSMatthew G Knepley   } else {
2278843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22797128ae9fSMatthew G Knepley   }
22809a42bb27SBarry Smith   PetscFunctionReturn(0);
22819a42bb27SBarry Smith }
22829a42bb27SBarry Smith 
22839a42bb27SBarry Smith /*@
22849a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
228547c6ae99SBarry Smith 
228647c6ae99SBarry Smith     Neighbor-wise Collective on DM
228747c6ae99SBarry Smith 
228847c6ae99SBarry Smith     Input Parameters:
228947c6ae99SBarry Smith +   dm - the DM object
2290f6813fd5SJed Brown .   l - the local vector
229147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2292f6813fd5SJed Brown -   g - the global vector
229347c6ae99SBarry Smith 
229447c6ae99SBarry Smith 
229547c6ae99SBarry Smith     Level: beginner
229647c6ae99SBarry Smith 
2297e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
229847c6ae99SBarry Smith 
229947c6ae99SBarry Smith @*/
23007087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
230147c6ae99SBarry Smith {
23027128ae9fSMatthew G Knepley   PetscSF                 sf;
230384330215SMatthew G. Knepley   PetscSection            s;
2304d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
230584330215SMatthew G. Knepley   PetscBool               isInsert;
230684330215SMatthew G. Knepley   PetscErrorCode          ierr;
230747c6ae99SBarry Smith 
230847c6ae99SBarry Smith   PetscFunctionBegin;
2309171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23107128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
231184330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
23127128ae9fSMatthew G Knepley   switch (mode) {
23137128ae9fSMatthew G Knepley   case INSERT_VALUES:
23147128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
231584330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
23167128ae9fSMatthew G Knepley   case ADD_VALUES:
23177128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
231884330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
23197128ae9fSMatthew G Knepley   default:
232082f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23217128ae9fSMatthew G Knepley   }
232284330215SMatthew G. Knepley   if (sf && !isInsert) {
2323ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2324ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
232584330215SMatthew G. Knepley 
2326ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
23277128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2328a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2329ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
23307128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
233184330215SMatthew G. Knepley   } else if (s && isInsert) {
23327128ae9fSMatthew G Knepley   } else {
2333843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
23347128ae9fSMatthew G Knepley   }
2335d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2336d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2337d4d07f1eSToby Isaac   }
233847c6ae99SBarry Smith   PetscFunctionReturn(0);
233947c6ae99SBarry Smith }
234047c6ae99SBarry Smith 
2341f089877aSRichard Tran Mills /*@
2342bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2343bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2344d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2345f089877aSRichard Tran Mills 
2346bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2347f089877aSRichard Tran Mills 
2348f089877aSRichard Tran Mills    Input Parameters:
2349f089877aSRichard Tran Mills +  dm - the DM object
2350bc0a1609SRichard Tran Mills .  g - the original local vector
2351bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2352f089877aSRichard Tran Mills 
2353bc0a1609SRichard Tran Mills    Output Parameter:
2354bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2355f089877aSRichard Tran Mills 
2356f089877aSRichard Tran Mills    Level: intermediate
2357f089877aSRichard Tran Mills 
2358bc0a1609SRichard Tran Mills    Notes:
2359bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2360bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2361bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2362bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2363bc0a1609SRichard Tran Mills 
2364bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2365bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2366f089877aSRichard Tran Mills 
2367f089877aSRichard Tran Mills @*/
2368f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2369f089877aSRichard Tran Mills {
2370f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2371f089877aSRichard Tran Mills 
2372f089877aSRichard Tran Mills   PetscFunctionBegin;
2373f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2374bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2375f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2376f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2377f089877aSRichard Tran Mills }
2378f089877aSRichard Tran Mills 
2379f089877aSRichard Tran Mills /*@
2380bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2381bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2382d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2383f089877aSRichard Tran Mills 
2384bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2385f089877aSRichard Tran Mills 
2386f089877aSRichard Tran Mills    Input Parameters:
2387bc0a1609SRichard Tran Mills +  da - the DM object
2388bc0a1609SRichard Tran Mills .  g - the original local vector
2389bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2390f089877aSRichard Tran Mills 
2391bc0a1609SRichard Tran Mills    Output Parameter:
2392bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2393f089877aSRichard Tran Mills 
2394f089877aSRichard Tran Mills    Level: intermediate
2395f089877aSRichard Tran Mills 
2396bc0a1609SRichard Tran Mills    Notes:
2397bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2398bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2399bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2400bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2401bc0a1609SRichard Tran Mills 
2402bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2403bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2404f089877aSRichard Tran Mills 
2405f089877aSRichard Tran Mills @*/
2406f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2407f089877aSRichard Tran Mills {
2408f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2409f089877aSRichard Tran Mills 
2410f089877aSRichard Tran Mills   PetscFunctionBegin;
2411f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2412bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2413f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2414f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2415f089877aSRichard Tran Mills }
2416f089877aSRichard Tran Mills 
2417f089877aSRichard Tran Mills 
241847c6ae99SBarry Smith /*@
241947c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
242047c6ae99SBarry Smith 
242147c6ae99SBarry Smith     Collective on DM
242247c6ae99SBarry Smith 
242347c6ae99SBarry Smith     Input Parameter:
242447c6ae99SBarry Smith +   dm - the DM object
242591d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
242647c6ae99SBarry Smith 
242747c6ae99SBarry Smith     Output Parameter:
242847c6ae99SBarry Smith .   dmc - the coarsened DM
242947c6ae99SBarry Smith 
243047c6ae99SBarry Smith     Level: developer
243147c6ae99SBarry Smith 
2432e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
243347c6ae99SBarry Smith 
243447c6ae99SBarry Smith @*/
24357087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
243647c6ae99SBarry Smith {
243747c6ae99SBarry Smith   PetscErrorCode    ierr;
2438b17ce1afSJed Brown   DMCoarsenHookLink link;
243947c6ae99SBarry Smith 
244047c6ae99SBarry Smith   PetscFunctionBegin;
2441171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2442fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
244347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
244447c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
24458892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2446a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
244743842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
24488cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2449644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
24500598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2451656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2452e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2453b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2454b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2455b17ce1afSJed Brown   }
245647a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2457b17ce1afSJed Brown   PetscFunctionReturn(0);
2458b17ce1afSJed Brown }
2459b17ce1afSJed Brown 
2460bb9467b5SJed Brown /*@C
2461b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2462b17ce1afSJed Brown 
2463b17ce1afSJed Brown    Logically Collective
2464b17ce1afSJed Brown 
2465b17ce1afSJed Brown    Input Arguments:
2466b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2467b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2468b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
24690298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2470b17ce1afSJed Brown 
2471b17ce1afSJed Brown    Calling sequence of coarsenhook:
2472b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2473b17ce1afSJed Brown 
2474b17ce1afSJed Brown +  fine - fine level DM
2475b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2476b17ce1afSJed Brown -  ctx - optional user-defined function context
2477b17ce1afSJed Brown 
2478b17ce1afSJed Brown    Calling sequence for restricthook:
2479c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2480b17ce1afSJed Brown 
2481b17ce1afSJed Brown +  fine - fine level DM
2482b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2483c833c3b5SJed Brown .  rscale - scaling vector for restriction
2484c833c3b5SJed Brown .  inject - matrix restricting by injection
2485b17ce1afSJed Brown .  coarse - coarse level DM to update
2486b17ce1afSJed Brown -  ctx - optional user-defined function context
2487b17ce1afSJed Brown 
2488b17ce1afSJed Brown    Level: advanced
2489b17ce1afSJed Brown 
2490b17ce1afSJed Brown    Notes:
2491b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2492b17ce1afSJed Brown 
2493b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2494b17ce1afSJed Brown 
2495b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2496b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2497b17ce1afSJed Brown 
2498bb9467b5SJed Brown    This function is currently not available from Fortran.
2499bb9467b5SJed Brown 
2500dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2501b17ce1afSJed Brown @*/
2502b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2503b17ce1afSJed Brown {
2504b17ce1afSJed Brown   PetscErrorCode    ierr;
2505b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2506b17ce1afSJed Brown 
2507b17ce1afSJed Brown   PetscFunctionBegin;
2508b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
25091e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
25101e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
25111e3d8eccSJed Brown   }
251295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2513b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2514b17ce1afSJed Brown   link->restricthook = restricthook;
2515b17ce1afSJed Brown   link->ctx          = ctx;
25160298fd71SBarry Smith   link->next         = NULL;
2517b17ce1afSJed Brown   *p                 = link;
2518b17ce1afSJed Brown   PetscFunctionReturn(0);
2519b17ce1afSJed Brown }
2520b17ce1afSJed Brown 
2521dc822a44SJed Brown /*@C
2522dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2523dc822a44SJed Brown 
2524dc822a44SJed Brown    Logically Collective
2525dc822a44SJed Brown 
2526dc822a44SJed Brown    Input Arguments:
2527dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2528dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2529dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2530dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2531dc822a44SJed Brown 
2532dc822a44SJed Brown    Level: advanced
2533dc822a44SJed Brown 
2534dc822a44SJed Brown    Notes:
2535dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2536dc822a44SJed Brown 
2537dc822a44SJed Brown    This function is currently not available from Fortran.
2538dc822a44SJed Brown 
2539dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2540dc822a44SJed Brown @*/
2541dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2542dc822a44SJed Brown {
2543dc822a44SJed Brown   PetscErrorCode    ierr;
2544dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2545dc822a44SJed Brown 
2546dc822a44SJed Brown   PetscFunctionBegin;
2547dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2548dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2549dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2550dc822a44SJed Brown       link = *p;
2551dc822a44SJed Brown       *p = link->next;
2552dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2553dc822a44SJed Brown       break;
2554dc822a44SJed Brown     }
2555dc822a44SJed Brown   }
2556dc822a44SJed Brown   PetscFunctionReturn(0);
2557dc822a44SJed Brown }
2558dc822a44SJed Brown 
2559dc822a44SJed Brown 
2560b17ce1afSJed Brown /*@
2561b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2562b17ce1afSJed Brown 
2563b17ce1afSJed Brown    Collective if any hooks are
2564b17ce1afSJed Brown 
2565b17ce1afSJed Brown    Input Arguments:
2566b17ce1afSJed Brown +  fine - finer DM to use as a base
2567b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2568e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2569b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2570e91eccc2SStefano Zampini -  coarse - coarser DM to update
2571b17ce1afSJed Brown 
2572b17ce1afSJed Brown    Level: developer
2573b17ce1afSJed Brown 
2574b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2575b17ce1afSJed Brown @*/
2576b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2577b17ce1afSJed Brown {
2578b17ce1afSJed Brown   PetscErrorCode    ierr;
2579b17ce1afSJed Brown   DMCoarsenHookLink link;
2580b17ce1afSJed Brown 
2581b17ce1afSJed Brown   PetscFunctionBegin;
2582b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
25838865f1eaSKarl Rupp     if (link->restricthook) {
25848865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
25858865f1eaSKarl Rupp     }
2586b17ce1afSJed Brown   }
258747c6ae99SBarry Smith   PetscFunctionReturn(0);
258847c6ae99SBarry Smith }
258947c6ae99SBarry Smith 
2590bb9467b5SJed Brown /*@C
2591be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
25925dbd56e3SPeter Brune 
25935dbd56e3SPeter Brune    Logically Collective
25945dbd56e3SPeter Brune 
25955dbd56e3SPeter Brune    Input Arguments:
25965dbd56e3SPeter Brune +  global - global DM
2597ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
25985dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
25990298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
26005dbd56e3SPeter Brune 
2601ec4806b8SPeter Brune 
2602ec4806b8SPeter Brune    Calling sequence for ddhook:
2603ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2604ec4806b8SPeter Brune 
2605ec4806b8SPeter Brune +  global - global DM
2606ec4806b8SPeter Brune .  block  - block DM
2607ec4806b8SPeter Brune -  ctx - optional user-defined function context
2608ec4806b8SPeter Brune 
26095dbd56e3SPeter Brune    Calling sequence for restricthook:
2610ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
26115dbd56e3SPeter Brune 
26125dbd56e3SPeter Brune +  global - global DM
26135dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
26145dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2615ec4806b8SPeter Brune .  block  - block DM
26165dbd56e3SPeter Brune -  ctx - optional user-defined function context
26175dbd56e3SPeter Brune 
26185dbd56e3SPeter Brune    Level: advanced
26195dbd56e3SPeter Brune 
26205dbd56e3SPeter Brune    Notes:
2621ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
26225dbd56e3SPeter Brune 
26235dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
26245dbd56e3SPeter Brune 
26255dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2626ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
26275dbd56e3SPeter Brune 
2628bb9467b5SJed Brown    This function is currently not available from Fortran.
2629bb9467b5SJed Brown 
26305dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
26315dbd56e3SPeter Brune @*/
2632be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
26335dbd56e3SPeter Brune {
26345dbd56e3SPeter Brune   PetscErrorCode      ierr;
2635be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
26365dbd56e3SPeter Brune 
26375dbd56e3SPeter Brune   PetscFunctionBegin;
26385dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2639b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2640b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2641b3a6b972SJed Brown   }
264295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
26435dbd56e3SPeter Brune   link->restricthook = restricthook;
2644be081cd6SPeter Brune   link->ddhook       = ddhook;
26455dbd56e3SPeter Brune   link->ctx          = ctx;
26460298fd71SBarry Smith   link->next         = NULL;
26475dbd56e3SPeter Brune   *p                 = link;
26485dbd56e3SPeter Brune   PetscFunctionReturn(0);
26495dbd56e3SPeter Brune }
26505dbd56e3SPeter Brune 
2651b3a6b972SJed Brown /*@C
2652b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2653b3a6b972SJed Brown 
2654b3a6b972SJed Brown    Logically Collective
2655b3a6b972SJed Brown 
2656b3a6b972SJed Brown    Input Arguments:
2657b3a6b972SJed Brown +  global - global DM
2658b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2659b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2660b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2661b3a6b972SJed Brown 
2662b3a6b972SJed Brown    Level: advanced
2663b3a6b972SJed Brown 
2664b3a6b972SJed Brown    Notes:
2665b3a6b972SJed Brown 
2666b3a6b972SJed Brown    This function is currently not available from Fortran.
2667b3a6b972SJed Brown 
2668b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2669b3a6b972SJed Brown @*/
2670b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2671b3a6b972SJed Brown {
2672b3a6b972SJed Brown   PetscErrorCode      ierr;
2673b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2674b3a6b972SJed Brown 
2675b3a6b972SJed Brown   PetscFunctionBegin;
2676b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2677b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2678b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2679b3a6b972SJed Brown       link = *p;
2680b3a6b972SJed Brown       *p = link->next;
2681b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2682b3a6b972SJed Brown       break;
2683b3a6b972SJed Brown     }
2684b3a6b972SJed Brown   }
2685b3a6b972SJed Brown   PetscFunctionReturn(0);
2686b3a6b972SJed Brown }
2687b3a6b972SJed Brown 
26885dbd56e3SPeter Brune /*@
2689be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
26905dbd56e3SPeter Brune 
26915dbd56e3SPeter Brune    Collective if any hooks are
26925dbd56e3SPeter Brune 
26935dbd56e3SPeter Brune    Input Arguments:
26945dbd56e3SPeter Brune +  fine - finer DM to use as a base
2695be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2696be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
26975dbd56e3SPeter Brune -  coarse - coarer DM to update
26985dbd56e3SPeter Brune 
26995dbd56e3SPeter Brune    Level: developer
27005dbd56e3SPeter Brune 
27015dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
27025dbd56e3SPeter Brune @*/
2703be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
27045dbd56e3SPeter Brune {
27055dbd56e3SPeter Brune   PetscErrorCode      ierr;
2706be081cd6SPeter Brune   DMSubDomainHookLink link;
27075dbd56e3SPeter Brune 
27085dbd56e3SPeter Brune   PetscFunctionBegin;
2709be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
27108865f1eaSKarl Rupp     if (link->restricthook) {
27118865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
27128865f1eaSKarl Rupp     }
27135dbd56e3SPeter Brune   }
27145dbd56e3SPeter Brune   PetscFunctionReturn(0);
27155dbd56e3SPeter Brune }
27165dbd56e3SPeter Brune 
27175fe1f584SPeter Brune /*@
27186a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
27195fe1f584SPeter Brune 
27205fe1f584SPeter Brune     Not Collective
27215fe1f584SPeter Brune 
27225fe1f584SPeter Brune     Input Parameter:
27235fe1f584SPeter Brune .   dm - the DM object
27245fe1f584SPeter Brune 
27255fe1f584SPeter Brune     Output Parameter:
27266a7d9d85SPeter Brune .   level - number of coarsenings
27275fe1f584SPeter Brune 
27285fe1f584SPeter Brune     Level: developer
27295fe1f584SPeter Brune 
27306a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
27315fe1f584SPeter Brune 
27325fe1f584SPeter Brune @*/
27335fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
27345fe1f584SPeter Brune {
27355fe1f584SPeter Brune   PetscFunctionBegin;
27365fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27375fe1f584SPeter Brune   *level = dm->leveldown;
27385fe1f584SPeter Brune   PetscFunctionReturn(0);
27395fe1f584SPeter Brune }
27405fe1f584SPeter Brune 
27415fe1f584SPeter Brune 
27425fe1f584SPeter Brune 
274347c6ae99SBarry Smith /*@C
274447c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
274547c6ae99SBarry Smith 
274647c6ae99SBarry Smith     Collective on DM
274747c6ae99SBarry Smith 
274847c6ae99SBarry Smith     Input Parameter:
274947c6ae99SBarry Smith +   dm - the DM object
275047c6ae99SBarry Smith -   nlevels - the number of levels of refinement
275147c6ae99SBarry Smith 
275247c6ae99SBarry Smith     Output Parameter:
275347c6ae99SBarry Smith .   dmf - the refined DM hierarchy
275447c6ae99SBarry Smith 
275547c6ae99SBarry Smith     Level: developer
275647c6ae99SBarry Smith 
2757e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
275847c6ae99SBarry Smith 
275947c6ae99SBarry Smith @*/
27607087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
276147c6ae99SBarry Smith {
276247c6ae99SBarry Smith   PetscErrorCode ierr;
276347c6ae99SBarry Smith 
276447c6ae99SBarry Smith   PetscFunctionBegin;
2765171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2766ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
276747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
276847c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
276947c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
277047c6ae99SBarry Smith   } else if (dm->ops->refine) {
277147c6ae99SBarry Smith     PetscInt i;
277247c6ae99SBarry Smith 
2773ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
277447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2775ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
277647c6ae99SBarry Smith     }
2777ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
277847c6ae99SBarry Smith   PetscFunctionReturn(0);
277947c6ae99SBarry Smith }
278047c6ae99SBarry Smith 
278147c6ae99SBarry Smith /*@C
278247c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
278347c6ae99SBarry Smith 
278447c6ae99SBarry Smith     Collective on DM
278547c6ae99SBarry Smith 
278647c6ae99SBarry Smith     Input Parameter:
278747c6ae99SBarry Smith +   dm - the DM object
278847c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
278947c6ae99SBarry Smith 
279047c6ae99SBarry Smith     Output Parameter:
279147c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
279247c6ae99SBarry Smith 
279347c6ae99SBarry Smith     Level: developer
279447c6ae99SBarry Smith 
2795e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
279647c6ae99SBarry Smith 
279747c6ae99SBarry Smith @*/
27987087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
279947c6ae99SBarry Smith {
280047c6ae99SBarry Smith   PetscErrorCode ierr;
280147c6ae99SBarry Smith 
280247c6ae99SBarry Smith   PetscFunctionBegin;
2803171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2804ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
280547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
280647c6ae99SBarry Smith   PetscValidPointer(dmc,3);
280747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
280847c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
280947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
281047c6ae99SBarry Smith     PetscInt i;
281147c6ae99SBarry Smith 
2812ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
281347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2814ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
281547c6ae99SBarry Smith     }
2816ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
281747c6ae99SBarry Smith   PetscFunctionReturn(0);
281847c6ae99SBarry Smith }
281947c6ae99SBarry Smith 
282047c6ae99SBarry Smith /*@
2821e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
282247c6ae99SBarry Smith    grids associated with two DMs.
282347c6ae99SBarry Smith 
282447c6ae99SBarry Smith    Collective on DM
282547c6ae99SBarry Smith 
282647c6ae99SBarry Smith    Input Parameters:
282747c6ae99SBarry Smith +  dmc - the coarse grid DM
282847c6ae99SBarry Smith -  dmf - the fine grid DM
282947c6ae99SBarry Smith 
283047c6ae99SBarry Smith    Output Parameters:
283147c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
283247c6ae99SBarry Smith 
283347c6ae99SBarry Smith    Level: intermediate
283447c6ae99SBarry Smith 
283547c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
283647c6ae99SBarry Smith 
2837e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
283847c6ae99SBarry Smith @*/
2839e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
284047c6ae99SBarry Smith {
284147c6ae99SBarry Smith   PetscErrorCode ierr;
284247c6ae99SBarry Smith 
284347c6ae99SBarry Smith   PetscFunctionBegin;
2844171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2845171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
284647c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
284747c6ae99SBarry Smith   PetscFunctionReturn(0);
284847c6ae99SBarry Smith }
284947c6ae99SBarry Smith 
28501a266240SBarry Smith /*@C
28511a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
28521a266240SBarry Smith 
28531a266240SBarry Smith     Not Collective
28541a266240SBarry Smith 
28551a266240SBarry Smith     Input Parameters:
28561a266240SBarry Smith +   dm - the DM object
28571a266240SBarry Smith -   destroy - the destroy function
28581a266240SBarry Smith 
28591a266240SBarry Smith     Level: intermediate
28601a266240SBarry Smith 
2861e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
28621a266240SBarry Smith 
2863f07f9ceaSJed Brown @*/
28641a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
28651a266240SBarry Smith {
28661a266240SBarry Smith   PetscFunctionBegin;
2867171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28681a266240SBarry Smith   dm->ctxdestroy = destroy;
28691a266240SBarry Smith   PetscFunctionReturn(0);
28701a266240SBarry Smith }
28711a266240SBarry Smith 
2872b07ff414SBarry Smith /*@
28731b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
287447c6ae99SBarry Smith 
287547c6ae99SBarry Smith     Not Collective
287647c6ae99SBarry Smith 
287747c6ae99SBarry Smith     Input Parameters:
287847c6ae99SBarry Smith +   dm - the DM object
287947c6ae99SBarry Smith -   ctx - the user context
288047c6ae99SBarry Smith 
288147c6ae99SBarry Smith     Level: intermediate
288247c6ae99SBarry Smith 
2883e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
288447c6ae99SBarry Smith 
288547c6ae99SBarry Smith @*/
28861b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
288747c6ae99SBarry Smith {
288847c6ae99SBarry Smith   PetscFunctionBegin;
2889171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
289047c6ae99SBarry Smith   dm->ctx = ctx;
289147c6ae99SBarry Smith   PetscFunctionReturn(0);
289247c6ae99SBarry Smith }
289347c6ae99SBarry Smith 
289447c6ae99SBarry Smith /*@
28951b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
289647c6ae99SBarry Smith 
289747c6ae99SBarry Smith     Not Collective
289847c6ae99SBarry Smith 
289947c6ae99SBarry Smith     Input Parameter:
290047c6ae99SBarry Smith .   dm - the DM object
290147c6ae99SBarry Smith 
290247c6ae99SBarry Smith     Output Parameter:
290347c6ae99SBarry Smith .   ctx - the user context
290447c6ae99SBarry Smith 
290547c6ae99SBarry Smith     Level: intermediate
290647c6ae99SBarry Smith 
2907e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
290847c6ae99SBarry Smith 
290947c6ae99SBarry Smith @*/
29101b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
291147c6ae99SBarry Smith {
291247c6ae99SBarry Smith   PetscFunctionBegin;
2913171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29141b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
291547c6ae99SBarry Smith   PetscFunctionReturn(0);
291647c6ae99SBarry Smith }
291747c6ae99SBarry Smith 
291808da532bSDmitry Karpeev /*@C
2919df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
292008da532bSDmitry Karpeev 
292108da532bSDmitry Karpeev     Logically Collective on DM
292208da532bSDmitry Karpeev 
292308da532bSDmitry Karpeev     Input Parameter:
292408da532bSDmitry Karpeev +   dm - the DM object
29250298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
292608da532bSDmitry Karpeev 
292708da532bSDmitry Karpeev     Level: intermediate
292808da532bSDmitry Karpeev 
2929835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
293008da532bSDmitry Karpeev          DMSetJacobian()
293108da532bSDmitry Karpeev 
293208da532bSDmitry Karpeev @*/
293308da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
293408da532bSDmitry Karpeev {
293508da532bSDmitry Karpeev   PetscFunctionBegin;
293608da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
293708da532bSDmitry Karpeev   PetscFunctionReturn(0);
293808da532bSDmitry Karpeev }
293908da532bSDmitry Karpeev 
294008da532bSDmitry Karpeev /*@
294108da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
294208da532bSDmitry Karpeev 
294308da532bSDmitry Karpeev     Not Collective
294408da532bSDmitry Karpeev 
294508da532bSDmitry Karpeev     Input Parameter:
294608da532bSDmitry Karpeev .   dm - the DM object to destroy
294708da532bSDmitry Karpeev 
294808da532bSDmitry Karpeev     Output Parameter:
294908da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
295008da532bSDmitry Karpeev 
295108da532bSDmitry Karpeev     Level: developer
295208da532bSDmitry Karpeev 
295374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
295408da532bSDmitry Karpeev 
295508da532bSDmitry Karpeev @*/
295608da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
295708da532bSDmitry Karpeev {
295808da532bSDmitry Karpeev   PetscFunctionBegin;
295908da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
296008da532bSDmitry Karpeev   PetscFunctionReturn(0);
296108da532bSDmitry Karpeev }
296208da532bSDmitry Karpeev 
296308da532bSDmitry Karpeev /*@C
296408da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
296508da532bSDmitry Karpeev 
296608da532bSDmitry Karpeev     Logically Collective on DM
296708da532bSDmitry Karpeev 
296808da532bSDmitry Karpeev     Input Parameters:
2969907376e6SBarry Smith .   dm - the DM object
297008da532bSDmitry Karpeev 
297108da532bSDmitry Karpeev     Output parameters:
297208da532bSDmitry Karpeev +   xl - lower bound
297308da532bSDmitry Karpeev -   xu - upper bound
297408da532bSDmitry Karpeev 
2975907376e6SBarry Smith     Level: advanced
2976907376e6SBarry Smith 
2977907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
297808da532bSDmitry Karpeev 
297974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
298008da532bSDmitry Karpeev 
298108da532bSDmitry Karpeev @*/
298208da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
298308da532bSDmitry Karpeev {
298408da532bSDmitry Karpeev   PetscErrorCode ierr;
29855fd66863SKarl Rupp 
298608da532bSDmitry Karpeev   PetscFunctionBegin;
298708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
298808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
298908da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
299008da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
29918865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
299208da532bSDmitry Karpeev   PetscFunctionReturn(0);
299308da532bSDmitry Karpeev }
299408da532bSDmitry Karpeev 
2995b0ae01b7SPeter Brune /*@
2996b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2997b0ae01b7SPeter Brune 
2998b0ae01b7SPeter Brune     Not Collective
2999b0ae01b7SPeter Brune 
3000b0ae01b7SPeter Brune     Input Parameter:
3001b0ae01b7SPeter Brune .   dm - the DM object
3002b0ae01b7SPeter Brune 
3003b0ae01b7SPeter Brune     Output Parameter:
3004b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3005b0ae01b7SPeter Brune 
3006b0ae01b7SPeter Brune     Level: developer
3007b0ae01b7SPeter Brune 
3008b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3009b0ae01b7SPeter Brune 
3010b0ae01b7SPeter Brune @*/
3011b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3012b0ae01b7SPeter Brune {
3013b0ae01b7SPeter Brune   PetscFunctionBegin;
3014b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3015b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3016b0ae01b7SPeter Brune }
3017b0ae01b7SPeter Brune 
30183ad4599aSBarry Smith /*@
30193ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
30203ad4599aSBarry Smith 
30213ad4599aSBarry Smith     Not Collective
30223ad4599aSBarry Smith 
30233ad4599aSBarry Smith     Input Parameter:
30243ad4599aSBarry Smith .   dm - the DM object
30253ad4599aSBarry Smith 
30263ad4599aSBarry Smith     Output Parameter:
30273ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
30283ad4599aSBarry Smith 
30293ad4599aSBarry Smith     Level: developer
30303ad4599aSBarry Smith 
30313ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
30323ad4599aSBarry Smith 
30333ad4599aSBarry Smith @*/
30343ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
30353ad4599aSBarry Smith {
30363ad4599aSBarry Smith   PetscFunctionBegin;
30373ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
30383ad4599aSBarry Smith   PetscFunctionReturn(0);
30393ad4599aSBarry Smith }
30403ad4599aSBarry Smith 
3041a7058e45SLawrence Mitchell 
3042a7058e45SLawrence Mitchell /*@
3043a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3044a7058e45SLawrence Mitchell 
3045a7058e45SLawrence Mitchell     Not Collective
3046a7058e45SLawrence Mitchell 
3047a7058e45SLawrence Mitchell     Input Parameter:
3048a7058e45SLawrence Mitchell .   dm - the DM object
3049a7058e45SLawrence Mitchell 
3050a7058e45SLawrence Mitchell     Output Parameter:
3051a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3052a7058e45SLawrence Mitchell 
3053a7058e45SLawrence Mitchell     Level: developer
3054a7058e45SLawrence Mitchell 
3055a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3056a7058e45SLawrence Mitchell 
3057a7058e45SLawrence Mitchell @*/
3058a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3059a7058e45SLawrence Mitchell {
3060*4a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3061a7058e45SLawrence Mitchell   PetscFunctionBegin;
3062*4a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
3063*4a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3064a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3065a7058e45SLawrence Mitchell }
3066a7058e45SLawrence Mitchell 
3067748fac09SDmitry Karpeev /*@C
306808da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
306908da532bSDmitry Karpeev 
307008da532bSDmitry Karpeev     Collective on DM
307108da532bSDmitry Karpeev 
307208da532bSDmitry Karpeev     Input Parameter:
307308da532bSDmitry Karpeev +   dm - the DM object
30740298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
307508da532bSDmitry Karpeev 
307608da532bSDmitry Karpeev     Level: developer
307708da532bSDmitry Karpeev 
307874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
307908da532bSDmitry Karpeev 
308008da532bSDmitry Karpeev @*/
308108da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
308208da532bSDmitry Karpeev {
308308da532bSDmitry Karpeev   PetscErrorCode ierr;
30845fd66863SKarl Rupp 
308508da532bSDmitry Karpeev   PetscFunctionBegin;
308608da532bSDmitry Karpeev   if (x) {
308708da532bSDmitry Karpeev     if (!dm->x) {
308808da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
308908da532bSDmitry Karpeev     }
309008da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
30918865f1eaSKarl Rupp   } else if (dm->x) {
309208da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
309308da532bSDmitry Karpeev   }
309408da532bSDmitry Karpeev   PetscFunctionReturn(0);
309508da532bSDmitry Karpeev }
309608da532bSDmitry Karpeev 
30970298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3098264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3099264ace61SBarry Smith 
3100264ace61SBarry Smith /*@C
3101264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3102264ace61SBarry Smith 
3103264ace61SBarry Smith   Collective on DM
3104264ace61SBarry Smith 
3105264ace61SBarry Smith   Input Parameters:
3106264ace61SBarry Smith + dm     - The DM object
3107264ace61SBarry Smith - method - The name of the DM type
3108264ace61SBarry Smith 
3109264ace61SBarry Smith   Options Database Key:
3110264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3111264ace61SBarry Smith 
3112264ace61SBarry Smith   Notes:
3113e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3114264ace61SBarry Smith 
3115264ace61SBarry Smith   Level: intermediate
3116264ace61SBarry Smith 
3117264ace61SBarry Smith .keywords: DM, set, type
3118264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3119264ace61SBarry Smith @*/
312019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3121264ace61SBarry Smith {
3122264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3123264ace61SBarry Smith   PetscBool      match;
3124264ace61SBarry Smith   PetscErrorCode ierr;
3125264ace61SBarry Smith 
3126264ace61SBarry Smith   PetscFunctionBegin;
3127264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3128251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3129264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3130264ace61SBarry Smith 
31310f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
31321c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3133ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3134264ace61SBarry Smith 
3135264ace61SBarry Smith   if (dm->ops->destroy) {
3136264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
31370298fd71SBarry Smith     dm->ops->destroy = NULL;
3138264ace61SBarry Smith   }
3139264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3140264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3141264ace61SBarry Smith   PetscFunctionReturn(0);
3142264ace61SBarry Smith }
3143264ace61SBarry Smith 
3144264ace61SBarry Smith /*@C
3145264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3146264ace61SBarry Smith 
3147264ace61SBarry Smith   Not Collective
3148264ace61SBarry Smith 
3149264ace61SBarry Smith   Input Parameter:
3150264ace61SBarry Smith . dm  - The DM
3151264ace61SBarry Smith 
3152264ace61SBarry Smith   Output Parameter:
3153264ace61SBarry Smith . type - The DM type name
3154264ace61SBarry Smith 
3155264ace61SBarry Smith   Level: intermediate
3156264ace61SBarry Smith 
3157264ace61SBarry Smith .keywords: DM, get, type, name
3158264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3159264ace61SBarry Smith @*/
316019fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3161264ace61SBarry Smith {
3162264ace61SBarry Smith   PetscErrorCode ierr;
3163264ace61SBarry Smith 
3164264ace61SBarry Smith   PetscFunctionBegin;
3165264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3166c959eef4SJed Brown   PetscValidPointer(type,2);
3167607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3168264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3169264ace61SBarry Smith   PetscFunctionReturn(0);
3170264ace61SBarry Smith }
3171264ace61SBarry Smith 
317267a56275SMatthew G Knepley /*@C
317367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
317467a56275SMatthew G Knepley 
317567a56275SMatthew G Knepley   Collective on DM
317667a56275SMatthew G Knepley 
317767a56275SMatthew G Knepley   Input Parameters:
317867a56275SMatthew G Knepley + dm - the DM
317967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
318067a56275SMatthew G Knepley 
318167a56275SMatthew G Knepley   Output Parameter:
318267a56275SMatthew G Knepley . M - pointer to new DM
318367a56275SMatthew G Knepley 
318467a56275SMatthew G Knepley   Notes:
318567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
318667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
318767a56275SMatthew G Knepley   of the input DM.
318867a56275SMatthew G Knepley 
318967a56275SMatthew G Knepley   Level: intermediate
319067a56275SMatthew G Knepley 
319167a56275SMatthew G Knepley .seealso: DMCreate()
319267a56275SMatthew G Knepley @*/
319319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
319467a56275SMatthew G Knepley {
319567a56275SMatthew G Knepley   DM             B;
319667a56275SMatthew G Knepley   char           convname[256];
3197c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
319867a56275SMatthew G Knepley   PetscErrorCode ierr;
319967a56275SMatthew G Knepley 
320067a56275SMatthew G Knepley   PetscFunctionBegin;
320167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
320267a56275SMatthew G Knepley   PetscValidType(dm,1);
320367a56275SMatthew G Knepley   PetscValidPointer(M,3);
3204251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3205c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3206c067b6caSMatthew G. Knepley   if (sametype) {
3207c067b6caSMatthew G. Knepley     *M   = dm;
3208c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3209c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3210c067b6caSMatthew G. Knepley   } else {
32110298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
321267a56275SMatthew G Knepley 
321367a56275SMatthew G Knepley     /*
321467a56275SMatthew G Knepley        Order of precedence:
321567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
321667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
321767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
321867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
321967a56275SMatthew G Knepley        5) Use a really basic converter.
322067a56275SMatthew G Knepley     */
322167a56275SMatthew G Knepley 
322267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
322367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
322467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
322567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
322667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
322767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
32280005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
322967a56275SMatthew G Knepley     if (conv) goto foundconv;
323067a56275SMatthew G Knepley 
323167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
323282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
323367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
323467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
323567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
323667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
323767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
323867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
32390005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
324067a56275SMatthew G Knepley     if (conv) {
3241fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
324267a56275SMatthew G Knepley       goto foundconv;
324367a56275SMatthew G Knepley     }
324467a56275SMatthew G Knepley 
324567a56275SMatthew G Knepley #if 0
324667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
324767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3248fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
324967a56275SMatthew G Knepley     if (conv) goto foundconv;
325067a56275SMatthew G Knepley 
325167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
325267a56275SMatthew G Knepley     if (dm->ops->convert) {
325367a56275SMatthew G Knepley       conv = dm->ops->convert;
325467a56275SMatthew G Knepley     }
325567a56275SMatthew G Knepley     if (conv) goto foundconv;
325667a56275SMatthew G Knepley #endif
325767a56275SMatthew G Knepley 
325867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
325982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
326067a56275SMatthew G Knepley 
326167a56275SMatthew G Knepley foundconv:
326267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
326367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
326412fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
326590b157c4SStefano Zampini     {
326690b157c4SStefano Zampini       PetscBool             isper;
326712fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
326812fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
326990b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
327090b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
327112fa691eSMatthew G. Knepley     }
327267a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
327367a56275SMatthew G Knepley   }
327467a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
327567a56275SMatthew G Knepley   PetscFunctionReturn(0);
327667a56275SMatthew G Knepley }
3277264ace61SBarry Smith 
3278264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3279264ace61SBarry Smith 
3280264ace61SBarry Smith /*@C
32811c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
32821c84c290SBarry Smith 
32831c84c290SBarry Smith   Not Collective
32841c84c290SBarry Smith 
32851c84c290SBarry Smith   Input Parameters:
32861c84c290SBarry Smith + name        - The name of a new user-defined creation routine
32871c84c290SBarry Smith - create_func - The creation routine itself
32881c84c290SBarry Smith 
32891c84c290SBarry Smith   Notes:
32901c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
32911c84c290SBarry Smith 
32921c84c290SBarry Smith 
32931c84c290SBarry Smith   Sample usage:
32941c84c290SBarry Smith .vb
3295bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
32961c84c290SBarry Smith .ve
32971c84c290SBarry Smith 
32981c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
32991c84c290SBarry Smith .vb
33001c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
33011c84c290SBarry Smith     DMSetType(DM,"my_da");
33021c84c290SBarry Smith .ve
33031c84c290SBarry Smith    or at runtime via the option
33041c84c290SBarry Smith .vb
33051c84c290SBarry Smith     -da_type my_da
33061c84c290SBarry Smith .ve
3307264ace61SBarry Smith 
3308264ace61SBarry Smith   Level: advanced
33091c84c290SBarry Smith 
33101c84c290SBarry Smith .keywords: DM, register
3311bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
33121c84c290SBarry Smith 
3313264ace61SBarry Smith @*/
3314bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3315264ace61SBarry Smith {
3316264ace61SBarry Smith   PetscErrorCode ierr;
3317264ace61SBarry Smith 
3318264ace61SBarry Smith   PetscFunctionBegin;
3319a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3320264ace61SBarry Smith   PetscFunctionReturn(0);
3321264ace61SBarry Smith }
3322264ace61SBarry Smith 
3323b859378eSBarry Smith /*@C
332455849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3325b859378eSBarry Smith 
3326b859378eSBarry Smith   Collective on PetscViewer
3327b859378eSBarry Smith 
3328b859378eSBarry Smith   Input Parameters:
3329b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3330b859378eSBarry Smith            some related function before a call to DMLoad().
3331b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3332b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3333b859378eSBarry Smith 
3334b859378eSBarry Smith    Level: intermediate
3335b859378eSBarry Smith 
3336b859378eSBarry Smith   Notes:
333755849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3338b859378eSBarry Smith 
3339b859378eSBarry Smith   Notes for advanced users:
3340b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3341b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3342b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3343b859378eSBarry Smith   format is
3344b859378eSBarry Smith .vb
3345b859378eSBarry Smith      has not yet been determined
3346b859378eSBarry Smith .ve
3347b859378eSBarry Smith 
3348b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3349b859378eSBarry Smith @*/
3350b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3351b859378eSBarry Smith {
33529331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3353b859378eSBarry Smith   PetscErrorCode ierr;
3354b859378eSBarry Smith 
3355b859378eSBarry Smith   PetscFunctionBegin;
3356b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3357b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
335832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
33599331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
33609331c7a4SMatthew G. Knepley   if (isbinary) {
33619331c7a4SMatthew G. Knepley     PetscInt classid;
33629331c7a4SMatthew G. Knepley     char     type[256];
3363b859378eSBarry Smith 
3364060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
33659200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3366060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
336732c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
33689331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
33699331c7a4SMatthew G. Knepley   } else if (ishdf5) {
33709331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
33719331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3372b859378eSBarry Smith   PetscFunctionReturn(0);
3373b859378eSBarry Smith }
3374b859378eSBarry Smith 
33757da65231SMatthew G Knepley /******************************** FEM Support **********************************/
33767da65231SMatthew G Knepley 
3377a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3378a6dfd86eSKarl Rupp {
33791d47ebbbSSatish Balay   PetscInt       f;
33801b30c384SMatthew G Knepley   PetscErrorCode ierr;
33811b30c384SMatthew G Knepley 
33827da65231SMatthew G Knepley   PetscFunctionBegin;
338374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
33841d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
338557622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
33867da65231SMatthew G Knepley   }
33877da65231SMatthew G Knepley   PetscFunctionReturn(0);
33887da65231SMatthew G Knepley }
33897da65231SMatthew G Knepley 
3390a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3391a6dfd86eSKarl Rupp {
33921b30c384SMatthew G Knepley   PetscInt       f, g;
33937da65231SMatthew G Knepley   PetscErrorCode ierr;
33947da65231SMatthew G Knepley 
33957da65231SMatthew G Knepley   PetscFunctionBegin;
339674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
33971d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
339874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
33991d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3400e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
34017da65231SMatthew G Knepley     }
340274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
34037da65231SMatthew G Knepley   }
34047da65231SMatthew G Knepley   PetscFunctionReturn(0);
34057da65231SMatthew G Knepley }
3406e7c4fc90SDmitry Karpeev 
34076113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3408e759306cSMatthew G. Knepley {
34090c5b8624SToby Isaac   PetscInt          localSize, bs;
34100c5b8624SToby Isaac   PetscMPIInt       size;
34110c5b8624SToby Isaac   Vec               x, xglob;
34120c5b8624SToby Isaac   const PetscScalar *xarray;
3413e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3414e759306cSMatthew G. Knepley 
3415e759306cSMatthew G. Knepley   PetscFunctionBegin;
34169852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3417e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3418e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
34196113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
34200c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
34210c5b8624SToby Isaac   if (size > 1) {
34220c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
34230c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
34240c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
34250c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
34260c5b8624SToby Isaac   } else {
34270c5b8624SToby Isaac     xglob = x;
34280c5b8624SToby Isaac   }
34290c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
34300c5b8624SToby Isaac   if (size > 1) {
34310c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
34320c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
34330c5b8624SToby Isaac   }
3434e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3435e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3436e759306cSMatthew G. Knepley }
3437e759306cSMatthew G. Knepley 
343888ed4aceSMatthew G Knepley /*@
343988ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
344088ed4aceSMatthew G Knepley 
344188ed4aceSMatthew G Knepley   Input Parameter:
344288ed4aceSMatthew G Knepley . dm - The DM
344388ed4aceSMatthew G Knepley 
344488ed4aceSMatthew G Knepley   Output Parameter:
344588ed4aceSMatthew G Knepley . section - The PetscSection
344688ed4aceSMatthew G Knepley 
344788ed4aceSMatthew G Knepley   Level: intermediate
344888ed4aceSMatthew G Knepley 
344988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
345088ed4aceSMatthew G Knepley 
345188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
345288ed4aceSMatthew G Knepley @*/
34530adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
34540adebc6cSBarry Smith {
3455fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3456fd59a867SMatthew G. Knepley 
345788ed4aceSMatthew G Knepley   PetscFunctionBegin;
345888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
345988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
34602f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
34612f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3462ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
34632f0f8703SMatthew G. Knepley   }
346488ed4aceSMatthew G Knepley   *section = dm->defaultSection;
346588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
346688ed4aceSMatthew G Knepley }
346788ed4aceSMatthew G Knepley 
346888ed4aceSMatthew G Knepley /*@
346988ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
347088ed4aceSMatthew G Knepley 
347188ed4aceSMatthew G Knepley   Input Parameters:
347288ed4aceSMatthew G Knepley + dm - The DM
347388ed4aceSMatthew G Knepley - section - The PetscSection
347488ed4aceSMatthew G Knepley 
347588ed4aceSMatthew G Knepley   Level: intermediate
347688ed4aceSMatthew G Knepley 
347788ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
347888ed4aceSMatthew G Knepley 
347988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
348088ed4aceSMatthew G Knepley @*/
34810adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
34820adebc6cSBarry Smith {
3483c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3484af122d2aSMatthew G Knepley   PetscInt       f;
348588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
348688ed4aceSMatthew G Knepley 
348788ed4aceSMatthew G Knepley   PetscFunctionBegin;
348888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3489c473ab19SMatthew G. Knepley   if (section) {
34901d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
34911d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3492c473ab19SMatthew G. Knepley   }
349388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
349488ed4aceSMatthew G Knepley   dm->defaultSection = section;
3495c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3496af122d2aSMatthew G Knepley   if (numFields) {
3497af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3498af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
34990f21e855SMatthew G. Knepley       PetscObject disc;
3500af122d2aSMatthew G Knepley       const char *name;
3501af122d2aSMatthew G Knepley 
3502af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
35030f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
35040f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3505af122d2aSMatthew G Knepley     }
3506af122d2aSMatthew G Knepley   }
35071d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
35081d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
350988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
351088ed4aceSMatthew G Knepley }
351188ed4aceSMatthew G Knepley 
35129435951eSToby Isaac /*@
35139435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
35149435951eSToby Isaac 
3515e228b242SToby Isaac   not collective
3516e228b242SToby Isaac 
35179435951eSToby Isaac   Input Parameter:
35189435951eSToby Isaac . dm - The DM
35199435951eSToby Isaac 
35209435951eSToby Isaac   Output Parameter:
35219435951eSToby 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.
35229435951eSToby 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.
35239435951eSToby Isaac 
35249435951eSToby Isaac   Level: advanced
35259435951eSToby Isaac 
35269435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
35279435951eSToby Isaac 
35289435951eSToby Isaac .seealso: DMSetDefaultConstraints()
35299435951eSToby Isaac @*/
35309435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
35319435951eSToby Isaac {
35329435951eSToby Isaac   PetscErrorCode ierr;
35339435951eSToby Isaac 
35349435951eSToby Isaac   PetscFunctionBegin;
35359435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35369435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
353745a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
353845a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
35399435951eSToby Isaac   PetscFunctionReturn(0);
35409435951eSToby Isaac }
35419435951eSToby Isaac 
35429435951eSToby Isaac /*@
35439435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
35449435951eSToby Isaac 
35459435951eSToby 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().
35469435951eSToby Isaac 
35479435951eSToby 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.
35489435951eSToby Isaac 
3549e228b242SToby Isaac   collective on dm
3550e228b242SToby Isaac 
35519435951eSToby Isaac   Input Parameters:
35529435951eSToby Isaac + dm - The DM
3553e228b242SToby 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).
3554e228b242SToby 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).
35559435951eSToby Isaac 
35569435951eSToby Isaac   Level: advanced
35579435951eSToby Isaac 
35589435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
35599435951eSToby Isaac 
35609435951eSToby Isaac .seealso: DMGetDefaultConstraints()
35619435951eSToby Isaac @*/
35629435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
35639435951eSToby Isaac {
3564e228b242SToby Isaac   PetscMPIInt result;
35659435951eSToby Isaac   PetscErrorCode ierr;
35669435951eSToby Isaac 
35679435951eSToby Isaac   PetscFunctionBegin;
35689435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3569e228b242SToby Isaac   if (section) {
3570e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3571e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3572f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3573e228b242SToby Isaac   }
3574e228b242SToby Isaac   if (mat) {
3575e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3576e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3577f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3578e228b242SToby Isaac   }
35799435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
35809435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
35819435951eSToby Isaac   dm->defaultConstraintSection = section;
35829435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
35839435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
35849435951eSToby Isaac   dm->defaultConstraintMat = mat;
35859435951eSToby Isaac   PetscFunctionReturn(0);
35869435951eSToby Isaac }
35879435951eSToby Isaac 
3588f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3589507e4973SMatthew G. Knepley /*
3590507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3591507e4973SMatthew G. Knepley 
3592507e4973SMatthew G. Knepley   Input Parameters:
3593507e4973SMatthew G. Knepley + dm - The DM
3594507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3595507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3596507e4973SMatthew G. Knepley 
3597507e4973SMatthew G. Knepley   Level: intermediate
3598507e4973SMatthew G. Knepley 
3599507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3600507e4973SMatthew G. Knepley */
3601f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3602507e4973SMatthew G. Knepley {
3603507e4973SMatthew G. Knepley   MPI_Comm        comm;
3604507e4973SMatthew G. Knepley   PetscLayout     layout;
3605507e4973SMatthew G. Knepley   const PetscInt *ranges;
3606507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3607507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3608507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3609507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3610507e4973SMatthew G. Knepley 
3611507e4973SMatthew G. Knepley   PetscFunctionBegin;
3612507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3613507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3614507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3615507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3616507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3617507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3618507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3619507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3620507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3621507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3622507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3623507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3624f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3625507e4973SMatthew G. Knepley 
3626507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3627507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3628507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3629507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3630507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3631507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3632507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3633507e4973SMatthew 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;}
3634507e4973SMatthew 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;}
3635507e4973SMatthew G. Knepley     if (gdof < 0) {
3636507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3637507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3638507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3639507e4973SMatthew G. Knepley 
3640507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3641507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3642507e4973SMatthew 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;}
3643507e4973SMatthew G. Knepley       }
3644507e4973SMatthew G. Knepley     }
3645507e4973SMatthew G. Knepley   }
3646507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3647507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3648b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3649507e4973SMatthew G. Knepley   if (!gvalid) {
3650507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3651507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3652507e4973SMatthew G. Knepley   }
3653507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3654507e4973SMatthew G. Knepley }
3655f741bcd2SMatthew G. Knepley #endif
3656507e4973SMatthew G. Knepley 
365788ed4aceSMatthew G Knepley /*@
365888ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
365988ed4aceSMatthew G Knepley 
36608b1ab98fSJed Brown   Collective on DM
36618b1ab98fSJed Brown 
366288ed4aceSMatthew G Knepley   Input Parameter:
366388ed4aceSMatthew G Knepley . dm - The DM
366488ed4aceSMatthew G Knepley 
366588ed4aceSMatthew G Knepley   Output Parameter:
366688ed4aceSMatthew G Knepley . section - The PetscSection
366788ed4aceSMatthew G Knepley 
366888ed4aceSMatthew G Knepley   Level: intermediate
366988ed4aceSMatthew G Knepley 
367088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
367188ed4aceSMatthew G Knepley 
367288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
367388ed4aceSMatthew G Knepley @*/
36740adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
36750adebc6cSBarry Smith {
367688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
367788ed4aceSMatthew G Knepley 
367888ed4aceSMatthew G Knepley   PetscFunctionBegin;
367988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
368088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
368188ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3682fd59a867SMatthew G. Knepley     PetscSection s;
3683fd59a867SMatthew G. Knepley 
3684fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3685fd59a867SMatthew 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");
3686fd59a867SMatthew 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");
368715b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3688cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3689ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3690685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
369188ed4aceSMatthew G Knepley   }
369288ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
369388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
369488ed4aceSMatthew G Knepley }
369588ed4aceSMatthew G Knepley 
3696b21d0597SMatthew G Knepley /*@
3697b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3698b21d0597SMatthew G Knepley 
3699b21d0597SMatthew G Knepley   Input Parameters:
3700b21d0597SMatthew G Knepley + dm - The DM
37015080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3702b21d0597SMatthew G Knepley 
3703b21d0597SMatthew G Knepley   Level: intermediate
3704b21d0597SMatthew G Knepley 
3705b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3706b21d0597SMatthew G Knepley 
3707b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3708b21d0597SMatthew G Knepley @*/
37090adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
37100adebc6cSBarry Smith {
3711b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3712b21d0597SMatthew G Knepley 
3713b21d0597SMatthew G Knepley   PetscFunctionBegin;
3714b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37155080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
37161d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3717b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3718b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3719507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3720f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3721507e4973SMatthew G. Knepley #endif
3722b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3723b21d0597SMatthew G Knepley }
3724b21d0597SMatthew G Knepley 
372588ed4aceSMatthew G Knepley /*@
372688ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
372788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
372888ed4aceSMatthew G Knepley 
372988ed4aceSMatthew G Knepley   Input Parameter:
373088ed4aceSMatthew G Knepley . dm - The DM
373188ed4aceSMatthew G Knepley 
373288ed4aceSMatthew G Knepley   Output Parameter:
373388ed4aceSMatthew G Knepley . sf - The PetscSF
373488ed4aceSMatthew G Knepley 
373588ed4aceSMatthew G Knepley   Level: intermediate
373688ed4aceSMatthew G Knepley 
373788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
373888ed4aceSMatthew G Knepley 
373988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
374088ed4aceSMatthew G Knepley @*/
37410adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
37420adebc6cSBarry Smith {
374388ed4aceSMatthew G Knepley   PetscInt       nroots;
374488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
374588ed4aceSMatthew G Knepley 
374688ed4aceSMatthew G Knepley   PetscFunctionBegin;
374788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
374888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
37490298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
375088ed4aceSMatthew G Knepley   if (nroots < 0) {
375188ed4aceSMatthew G Knepley     PetscSection section, gSection;
375288ed4aceSMatthew G Knepley 
375388ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
375431ea6d37SMatthew G Knepley     if (section) {
375588ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
375688ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
375731ea6d37SMatthew G Knepley     } else {
37580298fd71SBarry Smith       *sf = NULL;
375931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
376031ea6d37SMatthew G Knepley     }
376188ed4aceSMatthew G Knepley   }
376288ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
376388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
376488ed4aceSMatthew G Knepley }
376588ed4aceSMatthew G Knepley 
376688ed4aceSMatthew G Knepley /*@
376788ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
376888ed4aceSMatthew G Knepley 
376988ed4aceSMatthew G Knepley   Input Parameters:
377088ed4aceSMatthew G Knepley + dm - The DM
377188ed4aceSMatthew G Knepley - sf - The PetscSF
377288ed4aceSMatthew G Knepley 
377388ed4aceSMatthew G Knepley   Level: intermediate
377488ed4aceSMatthew G Knepley 
377588ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
377688ed4aceSMatthew G Knepley 
377788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
377888ed4aceSMatthew G Knepley @*/
37790adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
37800adebc6cSBarry Smith {
378188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
378288ed4aceSMatthew G Knepley 
378388ed4aceSMatthew G Knepley   PetscFunctionBegin;
378488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
378588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
378688ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
378788ed4aceSMatthew G Knepley   dm->defaultSF = sf;
378888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
378988ed4aceSMatthew G Knepley }
379088ed4aceSMatthew G Knepley 
379188ed4aceSMatthew G Knepley /*@C
379288ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
379388ed4aceSMatthew G Knepley   describing the data layout.
379488ed4aceSMatthew G Knepley 
379588ed4aceSMatthew G Knepley   Input Parameters:
379688ed4aceSMatthew G Knepley + dm - The DM
379788ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
379888ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
379988ed4aceSMatthew G Knepley 
380088ed4aceSMatthew G Knepley   Level: intermediate
380188ed4aceSMatthew G Knepley 
380288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
380388ed4aceSMatthew G Knepley @*/
380488ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
380588ed4aceSMatthew G Knepley {
380682f516ccSBarry Smith   MPI_Comm       comm;
380788ed4aceSMatthew G Knepley   PetscLayout    layout;
380888ed4aceSMatthew G Knepley   const PetscInt *ranges;
380988ed4aceSMatthew G Knepley   PetscInt       *local;
381088ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3811ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
381288ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
381388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
381488ed4aceSMatthew G Knepley 
381588ed4aceSMatthew G Knepley   PetscFunctionBegin;
381682f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
381788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
381888ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
381988ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
382088ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
382188ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
382288ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
382388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
382488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
382588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
382688ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3827ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
38286636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
382988ed4aceSMatthew G Knepley 
38306636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
38316636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3832235fbf56SMatthew 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));
38336636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
383488ed4aceSMatthew G Knepley   }
3835785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3836785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
383788ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
38381f588964SMatthew G Knepley     const PetscInt *cind;
38396636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
384088ed4aceSMatthew G Knepley 
384188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
384288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
384388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
384488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
384588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
38466636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
384788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
38486636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
38496636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
38506636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3851057b4bcdSMatthew 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);
38526636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
38536636e97aSMatthew G Knepley     }
385488ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
385588ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
385688ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
385788ed4aceSMatthew G Knepley     }
385888ed4aceSMatthew G Knepley     if (gdof < 0) {
38596636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
386088ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
386188ed4aceSMatthew G Knepley 
386205376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
386331d3f06eSJed Brown         if (r < 0) r = -(r+2);
386405376888SMatthew 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);
386588ed4aceSMatthew G Knepley         remote[l].rank  = r;
386688ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
386788ed4aceSMatthew G Knepley       }
386888ed4aceSMatthew G Knepley     } else {
38696636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
387088ed4aceSMatthew G Knepley         remote[l].rank  = rank;
387188ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
387288ed4aceSMatthew G Knepley       }
387388ed4aceSMatthew G Knepley     }
387488ed4aceSMatthew G Knepley   }
38756636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
387688ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
387788ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
387888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
387988ed4aceSMatthew G Knepley }
3880af122d2aSMatthew G Knepley 
3881b21d0597SMatthew G Knepley /*@
3882b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3883b21d0597SMatthew G Knepley 
3884b21d0597SMatthew G Knepley   Input Parameter:
3885b21d0597SMatthew G Knepley . dm - The DM
3886b21d0597SMatthew G Knepley 
3887b21d0597SMatthew G Knepley   Output Parameter:
3888b21d0597SMatthew G Knepley . sf - The PetscSF
3889b21d0597SMatthew G Knepley 
3890b21d0597SMatthew G Knepley   Level: intermediate
3891b21d0597SMatthew G Knepley 
3892b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3893b21d0597SMatthew G Knepley 
3894057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3895b21d0597SMatthew G Knepley @*/
38960adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
38970adebc6cSBarry Smith {
3898b21d0597SMatthew G Knepley   PetscFunctionBegin;
3899b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3900b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3901b21d0597SMatthew G Knepley   *sf = dm->sf;
3902b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3903b21d0597SMatthew G Knepley }
3904b21d0597SMatthew G Knepley 
3905057b4bcdSMatthew G Knepley /*@
3906057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3907057b4bcdSMatthew G Knepley 
3908057b4bcdSMatthew G Knepley   Input Parameters:
3909057b4bcdSMatthew G Knepley + dm - The DM
3910057b4bcdSMatthew G Knepley - sf - The PetscSF
3911057b4bcdSMatthew G Knepley 
3912057b4bcdSMatthew G Knepley   Level: intermediate
3913057b4bcdSMatthew G Knepley 
3914057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3915057b4bcdSMatthew G Knepley @*/
39160adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
39170adebc6cSBarry Smith {
3918057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3919057b4bcdSMatthew G Knepley 
3920057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3921057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3922057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3923057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3924057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3925057b4bcdSMatthew G Knepley   dm->sf = sf;
3926057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3927057b4bcdSMatthew G Knepley }
3928057b4bcdSMatthew G Knepley 
39292764a2aaSMatthew G. Knepley /*@
39302764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
39312764a2aaSMatthew G. Knepley 
39322764a2aaSMatthew G. Knepley   Input Parameter:
39332764a2aaSMatthew G. Knepley . dm - The DM
39342764a2aaSMatthew G. Knepley 
39352764a2aaSMatthew G. Knepley   Output Parameter:
39362764a2aaSMatthew G. Knepley . prob - The PetscDS
39372764a2aaSMatthew G. Knepley 
39382764a2aaSMatthew G. Knepley   Level: developer
39392764a2aaSMatthew G. Knepley 
39402764a2aaSMatthew G. Knepley .seealso: DMSetDS()
39412764a2aaSMatthew G. Knepley @*/
39422764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3943af122d2aSMatthew G Knepley {
3944af122d2aSMatthew G Knepley   PetscFunctionBegin;
3945af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39460f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
39470f21e855SMatthew G. Knepley   *prob = dm->prob;
39480f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
39490f21e855SMatthew G. Knepley }
39500f21e855SMatthew G. Knepley 
39512764a2aaSMatthew G. Knepley /*@
39522764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
39532764a2aaSMatthew G. Knepley 
39542764a2aaSMatthew G. Knepley   Input Parameters:
39552764a2aaSMatthew G. Knepley + dm - The DM
39562764a2aaSMatthew G. Knepley - prob - The PetscDS
39572764a2aaSMatthew G. Knepley 
39582764a2aaSMatthew G. Knepley   Level: developer
39592764a2aaSMatthew G. Knepley 
39602764a2aaSMatthew G. Knepley .seealso: DMGetDS()
39612764a2aaSMatthew G. Knepley @*/
39622764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
39630f21e855SMatthew G. Knepley {
3964f17e8794SMatthew G. Knepley   PetscInt       dimEmbed;
39650f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39660f21e855SMatthew G. Knepley 
39670f21e855SMatthew G. Knepley   PetscFunctionBegin;
39680f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39692764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
397037316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
39712764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
39720f21e855SMatthew G. Knepley   dm->prob = prob;
3973f17e8794SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
3974f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
39750f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
39760f21e855SMatthew G. Knepley }
39770f21e855SMatthew G. Knepley 
39780f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
39790f21e855SMatthew G. Knepley {
39800f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39810f21e855SMatthew G. Knepley 
39820f21e855SMatthew G. Knepley   PetscFunctionBegin;
39830f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39842764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3985af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3986af122d2aSMatthew G Knepley }
3987af122d2aSMatthew G Knepley 
3988af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3989af122d2aSMatthew G Knepley {
39900f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3991af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3992af122d2aSMatthew G Knepley 
3993af122d2aSMatthew G Knepley   PetscFunctionBegin;
3994af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39952764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
39960f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
39970f21e855SMatthew G. Knepley     PetscContainer obj;
39980f21e855SMatthew G. Knepley 
39990f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
40002764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
40010f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4002af122d2aSMatthew G Knepley   }
4003af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4004af122d2aSMatthew G Knepley }
4005af122d2aSMatthew G Knepley 
4006c1929be8SMatthew G. Knepley /*@
4007c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4008c1929be8SMatthew G. Knepley 
4009c1929be8SMatthew G. Knepley   Not collective
4010c1929be8SMatthew G. Knepley 
4011c1929be8SMatthew G. Knepley   Input Parameters:
4012c1929be8SMatthew G. Knepley + dm - The DM
4013c1929be8SMatthew G. Knepley - f  - The field number
4014c1929be8SMatthew G. Knepley 
4015c1929be8SMatthew G. Knepley   Output Parameter:
4016c1929be8SMatthew G. Knepley . field - The discretization object
4017c1929be8SMatthew G. Knepley 
4018c1929be8SMatthew G. Knepley   Level: developer
4019c1929be8SMatthew G. Knepley 
4020c1929be8SMatthew G. Knepley .seealso: DMSetField()
4021c1929be8SMatthew G. Knepley @*/
4022af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
4023af122d2aSMatthew G Knepley {
40240f21e855SMatthew G. Knepley   PetscErrorCode ierr;
40250f21e855SMatthew G. Knepley 
4026af122d2aSMatthew G Knepley   PetscFunctionBegin;
4027af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40282764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4029decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4030decb47aaSMatthew G. Knepley }
4031decb47aaSMatthew G. Knepley 
4032c1929be8SMatthew G. Knepley /*@
4033c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4034c1929be8SMatthew G. Knepley 
4035c1929be8SMatthew G. Knepley   Logically collective on DM
4036c1929be8SMatthew G. Knepley 
4037c1929be8SMatthew G. Knepley   Input Parameters:
4038c1929be8SMatthew G. Knepley + dm - The DM
4039c1929be8SMatthew G. Knepley . f  - The field number
4040c1929be8SMatthew G. Knepley - field - The discretization object
4041c1929be8SMatthew G. Knepley 
4042c1929be8SMatthew G. Knepley   Level: developer
4043c1929be8SMatthew G. Knepley 
4044c1929be8SMatthew G. Knepley .seealso: DMGetField()
4045c1929be8SMatthew G. Knepley @*/
4046decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
4047decb47aaSMatthew G. Knepley {
4048decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4049decb47aaSMatthew G. Knepley 
4050decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4051decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40522764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4053af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4054af122d2aSMatthew G Knepley }
40556636e97aSMatthew G Knepley 
4056b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4057b64e0483SPeter Brune {
4058b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4059b64e0483SPeter Brune   PetscErrorCode ierr;
4060b64e0483SPeter Brune   Vec coords,ccoords;
40616dbf9973SLawrence Mitchell   Mat inject;
4062b64e0483SPeter Brune   PetscFunctionBegin;
4063b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4064b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4065b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4066b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4067b64e0483SPeter Brune   if (coords && !ccoords) {
4068b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
40696668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
40706dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
40712adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
40726dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4073b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4074b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4075b64e0483SPeter Brune   }
4076b64e0483SPeter Brune   PetscFunctionReturn(0);
4077b64e0483SPeter Brune }
4078b64e0483SPeter Brune 
407903dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
408003dadc2fSPeter Brune {
408103dadc2fSPeter Brune   DM dm_coord,subdm_coord;
408203dadc2fSPeter Brune   PetscErrorCode ierr;
408303dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
408403dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
408503dadc2fSPeter Brune   PetscFunctionBegin;
408603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
408703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
408803dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
408903dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
409003dadc2fSPeter Brune   if (coords && !ccoords) {
409103dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
40926668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
409303dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
409424640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
409503dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
409603dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
409703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
409803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
409903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
410003dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
410103dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
410203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
410303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
410403dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
410503dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
410603dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
410703dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
410803dadc2fSPeter Brune   }
410903dadc2fSPeter Brune   PetscFunctionReturn(0);
411003dadc2fSPeter Brune }
411103dadc2fSPeter Brune 
4112c73cfb54SMatthew G. Knepley /*@
4113c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4114c73cfb54SMatthew G. Knepley 
4115c73cfb54SMatthew G. Knepley   Not collective
4116c73cfb54SMatthew G. Knepley 
4117c73cfb54SMatthew G. Knepley   Input Parameter:
4118c73cfb54SMatthew G. Knepley . dm - The DM
4119c73cfb54SMatthew G. Knepley 
4120c73cfb54SMatthew G. Knepley   Output Parameter:
4121c73cfb54SMatthew G. Knepley . dim - The topological dimension
4122c73cfb54SMatthew G. Knepley 
4123c73cfb54SMatthew G. Knepley   Level: beginner
4124c73cfb54SMatthew G. Knepley 
4125c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4126c73cfb54SMatthew G. Knepley @*/
4127c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4128c73cfb54SMatthew G. Knepley {
4129c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4130c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4131c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4132c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4133c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4134c73cfb54SMatthew G. Knepley }
4135c73cfb54SMatthew G. Knepley 
4136c73cfb54SMatthew G. Knepley /*@
4137c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4138c73cfb54SMatthew G. Knepley 
4139c73cfb54SMatthew G. Knepley   Collective on dm
4140c73cfb54SMatthew G. Knepley 
4141c73cfb54SMatthew G. Knepley   Input Parameters:
4142c73cfb54SMatthew G. Knepley + dm - The DM
4143c73cfb54SMatthew G. Knepley - dim - The topological dimension
4144c73cfb54SMatthew G. Knepley 
4145c73cfb54SMatthew G. Knepley   Level: beginner
4146c73cfb54SMatthew G. Knepley 
4147c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4148c73cfb54SMatthew G. Knepley @*/
4149c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4150c73cfb54SMatthew G. Knepley {
4151f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4152f17e8794SMatthew G. Knepley 
4153c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4154c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4155c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4156c73cfb54SMatthew G. Knepley   dm->dim = dim;
4157f17e8794SMatthew G. Knepley   if (dm->prob->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dim);CHKERRQ(ierr);}
4158c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4159c73cfb54SMatthew G. Knepley }
4160c73cfb54SMatthew G. Knepley 
4161793f3fe5SMatthew G. Knepley /*@
4162793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4163793f3fe5SMatthew G. Knepley 
4164793f3fe5SMatthew G. Knepley   Collective on DM
4165793f3fe5SMatthew G. Knepley 
4166793f3fe5SMatthew G. Knepley   Input Parameters:
4167793f3fe5SMatthew G. Knepley + dm - the DM
4168793f3fe5SMatthew G. Knepley - dim - the dimension
4169793f3fe5SMatthew G. Knepley 
4170793f3fe5SMatthew G. Knepley   Output Parameters:
4171793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4172793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4173793f3fe5SMatthew G. Knepley 
4174793f3fe5SMatthew G. Knepley   Note:
4175793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4176793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4177793f3fe5SMatthew G. Knepley   then the interval is empty.
4178793f3fe5SMatthew G. Knepley 
4179793f3fe5SMatthew G. Knepley   Level: intermediate
4180793f3fe5SMatthew G. Knepley 
4181793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4182793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4183793f3fe5SMatthew G. Knepley @*/
4184793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4185793f3fe5SMatthew G. Knepley {
4186793f3fe5SMatthew G. Knepley   PetscInt       d;
4187793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4188793f3fe5SMatthew G. Knepley 
4189793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4190793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4191793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4192793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4193793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4194793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4195793f3fe5SMatthew G. Knepley }
4196793f3fe5SMatthew G. Knepley 
41976636e97aSMatthew G Knepley /*@
41986636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
41996636e97aSMatthew G Knepley 
42006636e97aSMatthew G Knepley   Collective on DM
42016636e97aSMatthew G Knepley 
42026636e97aSMatthew G Knepley   Input Parameters:
42036636e97aSMatthew G Knepley + dm - the DM
42046636e97aSMatthew G Knepley - c - coordinate vector
42056636e97aSMatthew G Knepley 
42066636e97aSMatthew G Knepley   Note:
42076636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
42086636e97aSMatthew G Knepley 
42096636e97aSMatthew G Knepley   Level: intermediate
42106636e97aSMatthew G Knepley 
42116636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42126636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
42136636e97aSMatthew G Knepley @*/
42146636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
42156636e97aSMatthew G Knepley {
42166636e97aSMatthew G Knepley   PetscErrorCode ierr;
42176636e97aSMatthew G Knepley 
42186636e97aSMatthew G Knepley   PetscFunctionBegin;
42196636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42206636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
42216636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
42226636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
42236636e97aSMatthew G Knepley   dm->coordinates = c;
42246636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4225b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
422603dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
42276636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42286636e97aSMatthew G Knepley }
42296636e97aSMatthew G Knepley 
42306636e97aSMatthew G Knepley /*@
42316636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
42326636e97aSMatthew G Knepley 
42336636e97aSMatthew G Knepley   Collective on DM
42346636e97aSMatthew G Knepley 
42356636e97aSMatthew G Knepley    Input Parameters:
42366636e97aSMatthew G Knepley +  dm - the DM
42376636e97aSMatthew G Knepley -  c - coordinate vector
42386636e97aSMatthew G Knepley 
42396636e97aSMatthew G Knepley   Note:
42406636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
42416636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
42426636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
42436636e97aSMatthew G Knepley 
42446636e97aSMatthew G Knepley   Level: intermediate
42456636e97aSMatthew G Knepley 
42466636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42476636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
42486636e97aSMatthew G Knepley @*/
42496636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
42506636e97aSMatthew G Knepley {
42516636e97aSMatthew G Knepley   PetscErrorCode ierr;
42526636e97aSMatthew G Knepley 
42536636e97aSMatthew G Knepley   PetscFunctionBegin;
42546636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42556636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
42566636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
42576636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
42588865f1eaSKarl Rupp 
42596636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
42608865f1eaSKarl Rupp 
42616636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
42626636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42636636e97aSMatthew G Knepley }
42646636e97aSMatthew G Knepley 
42656636e97aSMatthew G Knepley /*@
42666636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
42676636e97aSMatthew G Knepley 
42686636e97aSMatthew G Knepley   Not Collective
42696636e97aSMatthew G Knepley 
42706636e97aSMatthew G Knepley   Input Parameter:
42716636e97aSMatthew G Knepley . dm - the DM
42726636e97aSMatthew G Knepley 
42736636e97aSMatthew G Knepley   Output Parameter:
42746636e97aSMatthew G Knepley . c - global coordinate vector
42756636e97aSMatthew G Knepley 
42766636e97aSMatthew G Knepley   Note:
42776636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
42786636e97aSMatthew G Knepley 
42796636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
42806636e97aSMatthew G Knepley 
42816636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
42826636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
42836636e97aSMatthew G Knepley 
42846636e97aSMatthew G Knepley   Level: intermediate
42856636e97aSMatthew G Knepley 
42866636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42876636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
42886636e97aSMatthew G Knepley @*/
42896636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
42906636e97aSMatthew G Knepley {
42916636e97aSMatthew G Knepley   PetscErrorCode ierr;
42926636e97aSMatthew G Knepley 
42936636e97aSMatthew G Knepley   PetscFunctionBegin;
42946636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42956636e97aSMatthew G Knepley   PetscValidPointer(c,2);
42961f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
42970298fd71SBarry Smith     DM cdm = NULL;
42986636e97aSMatthew G Knepley 
42996636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
43006636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
43016636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
43026636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
43036636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
43046636e97aSMatthew G Knepley   }
43056636e97aSMatthew G Knepley   *c = dm->coordinates;
43066636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43076636e97aSMatthew G Knepley }
43086636e97aSMatthew G Knepley 
43096636e97aSMatthew G Knepley /*@
43106636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
43116636e97aSMatthew G Knepley 
43126636e97aSMatthew G Knepley   Collective on DM
43136636e97aSMatthew G Knepley 
43146636e97aSMatthew G Knepley   Input Parameter:
43156636e97aSMatthew G Knepley . dm - the DM
43166636e97aSMatthew G Knepley 
43176636e97aSMatthew G Knepley   Output Parameter:
43186636e97aSMatthew G Knepley . c - coordinate vector
43196636e97aSMatthew G Knepley 
43206636e97aSMatthew G Knepley   Note:
43216636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
43226636e97aSMatthew G Knepley 
43236636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
43246636e97aSMatthew G Knepley 
43256636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
43266636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
43276636e97aSMatthew G Knepley 
43286636e97aSMatthew G Knepley   Level: intermediate
43296636e97aSMatthew G Knepley 
43306636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43316636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
43326636e97aSMatthew G Knepley @*/
43336636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
43346636e97aSMatthew G Knepley {
43356636e97aSMatthew G Knepley   PetscErrorCode ierr;
43366636e97aSMatthew G Knepley 
43376636e97aSMatthew G Knepley   PetscFunctionBegin;
43386636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43396636e97aSMatthew G Knepley   PetscValidPointer(c,2);
43401f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
43410298fd71SBarry Smith     DM cdm = NULL;
43426636e97aSMatthew G Knepley 
43436636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
43446636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
43456636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
43466636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
43476636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
43486636e97aSMatthew G Knepley   }
43496636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
43506636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43516636e97aSMatthew G Knepley }
43526636e97aSMatthew G Knepley 
43536636e97aSMatthew G Knepley /*@
43541cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
43556636e97aSMatthew G Knepley 
43566636e97aSMatthew G Knepley   Collective on DM
43576636e97aSMatthew G Knepley 
43586636e97aSMatthew G Knepley   Input Parameter:
43596636e97aSMatthew G Knepley . dm - the DM
43606636e97aSMatthew G Knepley 
43616636e97aSMatthew G Knepley   Output Parameter:
43626636e97aSMatthew G Knepley . cdm - coordinate DM
43636636e97aSMatthew G Knepley 
43646636e97aSMatthew G Knepley   Level: intermediate
43656636e97aSMatthew G Knepley 
43666636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43671cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
43686636e97aSMatthew G Knepley @*/
43696636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
43706636e97aSMatthew G Knepley {
43716636e97aSMatthew G Knepley   PetscErrorCode ierr;
43726636e97aSMatthew G Knepley 
43736636e97aSMatthew G Knepley   PetscFunctionBegin;
43746636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43756636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
43766636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
437782f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
43786636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
43796636e97aSMatthew G Knepley   }
43806636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
43816636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43826636e97aSMatthew G Knepley }
4383e87bb0d3SMatthew G Knepley 
43841cfe2091SMatthew G. Knepley /*@
43851cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
43861cfe2091SMatthew G. Knepley 
43871cfe2091SMatthew G. Knepley   Logically Collective on DM
43881cfe2091SMatthew G. Knepley 
43891cfe2091SMatthew G. Knepley   Input Parameters:
43901cfe2091SMatthew G. Knepley + dm - the DM
43911cfe2091SMatthew G. Knepley - cdm - coordinate DM
43921cfe2091SMatthew G. Knepley 
43931cfe2091SMatthew G. Knepley   Level: intermediate
43941cfe2091SMatthew G. Knepley 
43951cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43961cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
43971cfe2091SMatthew G. Knepley @*/
43981cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
43991cfe2091SMatthew G. Knepley {
44001cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
44011cfe2091SMatthew G. Knepley 
44021cfe2091SMatthew G. Knepley   PetscFunctionBegin;
44031cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44041cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4405f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
44061cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
44071cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
44081cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
44091cfe2091SMatthew G. Knepley }
44101cfe2091SMatthew G. Knepley 
441146e270d4SMatthew G. Knepley /*@
441246e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
441346e270d4SMatthew G. Knepley 
441446e270d4SMatthew G. Knepley   Not Collective
441546e270d4SMatthew G. Knepley 
441646e270d4SMatthew G. Knepley   Input Parameter:
441746e270d4SMatthew G. Knepley . dm - The DM object
441846e270d4SMatthew G. Knepley 
441946e270d4SMatthew G. Knepley   Output Parameter:
442046e270d4SMatthew G. Knepley . dim - The embedding dimension
442146e270d4SMatthew G. Knepley 
442246e270d4SMatthew G. Knepley   Level: intermediate
442346e270d4SMatthew G. Knepley 
442446e270d4SMatthew G. Knepley .keywords: mesh, coordinates
442546e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
442646e270d4SMatthew G. Knepley @*/
442746e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
442846e270d4SMatthew G. Knepley {
442946e270d4SMatthew G. Knepley   PetscFunctionBegin;
443046e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
443146e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
44329a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
44339a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
44349a9a41abSToby Isaac   }
443546e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
443646e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
443746e270d4SMatthew G. Knepley }
443846e270d4SMatthew G. Knepley 
443946e270d4SMatthew G. Knepley /*@
444046e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
444146e270d4SMatthew G. Knepley 
444246e270d4SMatthew G. Knepley   Not Collective
444346e270d4SMatthew G. Knepley 
444446e270d4SMatthew G. Knepley   Input Parameters:
444546e270d4SMatthew G. Knepley + dm  - The DM object
444646e270d4SMatthew G. Knepley - dim - The embedding dimension
444746e270d4SMatthew G. Knepley 
444846e270d4SMatthew G. Knepley   Level: intermediate
444946e270d4SMatthew G. Knepley 
445046e270d4SMatthew G. Knepley .keywords: mesh, coordinates
445146e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
445246e270d4SMatthew G. Knepley @*/
445346e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
445446e270d4SMatthew G. Knepley {
4455f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4456f17e8794SMatthew G. Knepley 
445746e270d4SMatthew G. Knepley   PetscFunctionBegin;
445846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
445946e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
4460f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dimEmbed);CHKERRQ(ierr);
446146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
446246e270d4SMatthew G. Knepley }
446346e270d4SMatthew G. Knepley 
4464e8abe2deSMatthew G. Knepley /*@
4465e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4466e8abe2deSMatthew G. Knepley 
4467e8abe2deSMatthew G. Knepley   Not Collective
4468e8abe2deSMatthew G. Knepley 
4469e8abe2deSMatthew G. Knepley   Input Parameter:
4470e8abe2deSMatthew G. Knepley . dm - The DM object
4471e8abe2deSMatthew G. Knepley 
4472e8abe2deSMatthew G. Knepley   Output Parameter:
4473e8abe2deSMatthew G. Knepley . section - The PetscSection object
4474e8abe2deSMatthew G. Knepley 
4475e8abe2deSMatthew G. Knepley   Level: intermediate
4476e8abe2deSMatthew G. Knepley 
4477e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4478e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4479e8abe2deSMatthew G. Knepley @*/
4480e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4481e8abe2deSMatthew G. Knepley {
4482e8abe2deSMatthew G. Knepley   DM             cdm;
4483e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4484e8abe2deSMatthew G. Knepley 
4485e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4486e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4487e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4488e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4489e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4490e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4491e8abe2deSMatthew G. Knepley }
4492e8abe2deSMatthew G. Knepley 
4493e8abe2deSMatthew G. Knepley /*@
4494e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4495e8abe2deSMatthew G. Knepley 
4496e8abe2deSMatthew G. Knepley   Not Collective
4497e8abe2deSMatthew G. Knepley 
4498e8abe2deSMatthew G. Knepley   Input Parameters:
4499e8abe2deSMatthew G. Knepley + dm      - The DM object
450046e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4501e8abe2deSMatthew G. Knepley - section - The PetscSection object
4502e8abe2deSMatthew G. Knepley 
4503e8abe2deSMatthew G. Knepley   Level: intermediate
4504e8abe2deSMatthew G. Knepley 
4505e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4506e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4507e8abe2deSMatthew G. Knepley @*/
450846e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4509e8abe2deSMatthew G. Knepley {
4510e8abe2deSMatthew G. Knepley   DM             cdm;
4511e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4512e8abe2deSMatthew G. Knepley 
4513e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4514e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
451546e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4516e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4517e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
451846e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
45194c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
452046e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
452146e270d4SMatthew G. Knepley 
452246e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
452346e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
452446e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
452546e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
452646e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
452746e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
452846e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
452946e270d4SMatthew G. Knepley     }
45306be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
453146e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
453246e270d4SMatthew G. Knepley   }
4533e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4534e8abe2deSMatthew G. Knepley }
4535e8abe2deSMatthew G. Knepley 
45365dc8c3f7SMatthew G. Knepley /*@C
453790b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
45385dc8c3f7SMatthew G. Knepley 
45395dc8c3f7SMatthew G. Knepley   Input Parameters:
454090b157c4SStefano Zampini . dm      - The DM object
454190b157c4SStefano Zampini 
454290b157c4SStefano Zampini   Output Parameters:
454390b157c4SStefano Zampini + per     - Whether the DM is periodic or not
45445dc8c3f7SMatthew 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
45455dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
45465dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
45475dc8c3f7SMatthew G. Knepley 
45485dc8c3f7SMatthew G. Knepley   Level: developer
45495dc8c3f7SMatthew G. Knepley 
45505dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
45515dc8c3f7SMatthew G. Knepley @*/
455290b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4553c6b900c6SMatthew G. Knepley {
4554c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4555c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
455690b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4557c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4558c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
45595dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4560c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4561c6b900c6SMatthew G. Knepley }
4562c6b900c6SMatthew G. Knepley 
45635dc8c3f7SMatthew G. Knepley /*@C
45645dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
45655dc8c3f7SMatthew G. Knepley 
45665dc8c3f7SMatthew G. Knepley   Input Parameters:
45675dc8c3f7SMatthew G. Knepley + dm      - The DM object
456890b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
45695dc8c3f7SMatthew 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
45705dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
45715dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
45725dc8c3f7SMatthew G. Knepley 
45735dc8c3f7SMatthew G. Knepley   Level: developer
45745dc8c3f7SMatthew G. Knepley 
45755dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
45765dc8c3f7SMatthew G. Knepley @*/
457790b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4578c6b900c6SMatthew G. Knepley {
4579c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4580c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4581c6b900c6SMatthew G. Knepley 
4582c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4583c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
458490b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
458590b157c4SStefano Zampini   if (maxCell) {
458690b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
458790b157c4SStefano Zampini     PetscValidPointer(L,4);
458890b157c4SStefano Zampini     PetscValidPointer(bd,5);
458990b157c4SStefano Zampini   }
45905dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
45915dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
459290b157c4SStefano Zampini   if (maxCell) {
45935dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
45945dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
459590b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
459690b157c4SStefano Zampini   } else {
459790b157c4SStefano Zampini     dm->periodic = per;
459890b157c4SStefano Zampini   }
4599c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4600c6b900c6SMatthew G. Knepley }
4601c6b900c6SMatthew G. Knepley 
46022e17dfb7SMatthew G. Knepley /*@
46032e17dfb7SMatthew 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.
46042e17dfb7SMatthew G. Knepley 
46052e17dfb7SMatthew G. Knepley   Input Parameters:
46062e17dfb7SMatthew G. Knepley + dm     - The DM
460765da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
460865da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
46092e17dfb7SMatthew G. Knepley 
46102e17dfb7SMatthew G. Knepley   Output Parameter:
46112e17dfb7SMatthew G. Knepley . out - The localized coordinate point
46122e17dfb7SMatthew G. Knepley 
46132e17dfb7SMatthew G. Knepley   Level: developer
46142e17dfb7SMatthew G. Knepley 
46152e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
46162e17dfb7SMatthew G. Knepley @*/
461765da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
46182e17dfb7SMatthew G. Knepley {
46192e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
46202e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
46212e17dfb7SMatthew G. Knepley 
46222e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46232e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
46242e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46252e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46262e17dfb7SMatthew G. Knepley   } else {
462765da65dcSMatthew G. Knepley     if (endpoint) {
462865da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4629da3333bfSMatthew G. Knepley         if ((PetscAbsReal(PetscRealPart(in[d])/dm->L[d] - PetscFloorReal(PetscRealPart(in[d])/dm->L[d])) < PETSC_SMALL) && (PetscRealPart(in[d])/dm->L[d] > PETSC_SMALL)) {
4630da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
463165da65dcSMatthew G. Knepley         } else {
4632da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
463365da65dcSMatthew G. Knepley         }
463465da65dcSMatthew G. Knepley       }
463565da65dcSMatthew G. Knepley     } else {
46362e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
46371118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
46382e17dfb7SMatthew G. Knepley       }
46392e17dfb7SMatthew G. Knepley     }
464065da65dcSMatthew G. Knepley   }
46412e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46422e17dfb7SMatthew G. Knepley }
46432e17dfb7SMatthew G. Knepley 
46442e17dfb7SMatthew G. Knepley /*
46452e17dfb7SMatthew 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.
46462e17dfb7SMatthew G. Knepley 
46472e17dfb7SMatthew G. Knepley   Input Parameters:
46482e17dfb7SMatthew G. Knepley + dm     - The DM
46492e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
46502e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
46512e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
46522e17dfb7SMatthew G. Knepley 
46532e17dfb7SMatthew G. Knepley   Output Parameter:
46542e17dfb7SMatthew G. Knepley . out - The localized coordinate point
46552e17dfb7SMatthew G. Knepley 
46562e17dfb7SMatthew G. Knepley   Level: developer
46572e17dfb7SMatthew G. Knepley 
46582e17dfb7SMatthew G. Knepley   Note: This is meant to get a set of coordinates close to each other, as in a cell. The anchor is usually the one of the vertices on a containing cell
46592e17dfb7SMatthew G. Knepley 
46602e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
46612e17dfb7SMatthew G. Knepley */
46622e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
46632e17dfb7SMatthew G. Knepley {
46642e17dfb7SMatthew G. Knepley   PetscInt d;
46652e17dfb7SMatthew G. Knepley 
46662e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46672e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46682e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46692e17dfb7SMatthew G. Knepley   } else {
46702e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4671908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
46722e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
46732e17dfb7SMatthew G. Knepley       } else {
46742e17dfb7SMatthew G. Knepley         out[d] = in[d];
46752e17dfb7SMatthew G. Knepley       }
46762e17dfb7SMatthew G. Knepley     }
46772e17dfb7SMatthew G. Knepley   }
46782e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46792e17dfb7SMatthew G. Knepley }
46802e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
46812e17dfb7SMatthew G. Knepley {
46822e17dfb7SMatthew G. Knepley   PetscInt d;
46832e17dfb7SMatthew G. Knepley 
46842e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46852e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46862e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46872e17dfb7SMatthew G. Knepley   } else {
46882e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4689908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
46902e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
46912e17dfb7SMatthew G. Knepley       } else {
46922e17dfb7SMatthew G. Knepley         out[d] = in[d];
46932e17dfb7SMatthew G. Knepley       }
46942e17dfb7SMatthew G. Knepley     }
46952e17dfb7SMatthew G. Knepley   }
46962e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46972e17dfb7SMatthew G. Knepley }
46982e17dfb7SMatthew G. Knepley 
46992e17dfb7SMatthew G. Knepley /*
47002e17dfb7SMatthew 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.
47012e17dfb7SMatthew G. Knepley 
47022e17dfb7SMatthew G. Knepley   Input Parameters:
47032e17dfb7SMatthew G. Knepley + dm     - The DM
47042e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
47052e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
47062e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
47072e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
47082e17dfb7SMatthew G. Knepley 
47092e17dfb7SMatthew G. Knepley   Output Parameter:
47102e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
47112e17dfb7SMatthew G. Knepley 
47122e17dfb7SMatthew G. Knepley   Level: developer
47132e17dfb7SMatthew G. Knepley 
47142e17dfb7SMatthew 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
47152e17dfb7SMatthew G. Knepley 
47162e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
47172e17dfb7SMatthew G. Knepley */
47182e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
47192e17dfb7SMatthew G. Knepley {
47202e17dfb7SMatthew G. Knepley   PetscInt d;
47212e17dfb7SMatthew G. Knepley 
47222e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47232e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
47242e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
47252e17dfb7SMatthew G. Knepley   } else {
47262e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4727908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
47282e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
47292e17dfb7SMatthew G. Knepley       } else {
47302e17dfb7SMatthew G. Knepley         out[d] += in[d];
47312e17dfb7SMatthew G. Knepley       }
47322e17dfb7SMatthew G. Knepley     }
47332e17dfb7SMatthew G. Knepley   }
47342e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47352e17dfb7SMatthew G. Knepley }
47362e17dfb7SMatthew G. Knepley 
473736447a5eSToby Isaac /*@
473836447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
473936447a5eSToby Isaac 
474036447a5eSToby Isaac   Input Parameter:
474136447a5eSToby Isaac . dm - The DM
474236447a5eSToby Isaac 
474336447a5eSToby Isaac   Output Parameter:
474436447a5eSToby Isaac   areLocalized - True if localized
474536447a5eSToby Isaac 
474636447a5eSToby Isaac   Level: developer
474736447a5eSToby Isaac 
474836447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
474936447a5eSToby Isaac @*/
475036447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
475136447a5eSToby Isaac {
475236447a5eSToby Isaac   DM             cdm;
475336447a5eSToby Isaac   PetscSection   coordSection;
475436447a5eSToby Isaac   PetscInt       cStart, cEnd, c, sStart, sEnd, dof;
475536447a5eSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
475636447a5eSToby Isaac   PetscErrorCode ierr;
475736447a5eSToby Isaac 
475836447a5eSToby Isaac   PetscFunctionBegin;
475936447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
476092c9c85fSStefano Zampini   if (!dm->periodic) {
47618b09590cSToby Isaac     *areLocalized = PETSC_FALSE;
47628b09590cSToby Isaac     PetscFunctionReturn(0);
47638b09590cSToby Isaac   }
476436447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
476536447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
476636447a5eSToby Isaac   {
476736447a5eSToby Isaac     PetscBool isplex;
476836447a5eSToby Isaac 
476936447a5eSToby Isaac     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
477036447a5eSToby Isaac     if (isplex) {
477136447a5eSToby Isaac       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
477236447a5eSToby Isaac     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
477336447a5eSToby Isaac   }
477436447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
477536447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
47765a42012cSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_FALSE;
477736447a5eSToby Isaac   for (c = cStart; c < cEnd; ++c) {
477836447a5eSToby Isaac     if (c < sStart || c >= sEnd) {
477936447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
478036447a5eSToby Isaac       break;
478136447a5eSToby Isaac     }
478236447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
47835a42012cSToby Isaac     if (dof) {
47845a42012cSToby Isaac       alreadyLocalized = PETSC_TRUE;
478536447a5eSToby Isaac       break;
478636447a5eSToby Isaac     }
478736447a5eSToby Isaac   }
47885a42012cSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
478936447a5eSToby Isaac   *areLocalized = alreadyLocalizedGlobal;
479036447a5eSToby Isaac   PetscFunctionReturn(0);
479136447a5eSToby Isaac }
479236447a5eSToby Isaac 
479336447a5eSToby Isaac 
47942e17dfb7SMatthew G. Knepley /*@
4795492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
47962e17dfb7SMatthew G. Knepley 
47972e17dfb7SMatthew G. Knepley   Input Parameter:
47982e17dfb7SMatthew G. Knepley . dm - The DM
47992e17dfb7SMatthew G. Knepley 
48002e17dfb7SMatthew G. Knepley   Level: developer
48012e17dfb7SMatthew G. Knepley 
48022e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
48032e17dfb7SMatthew G. Knepley @*/
48042e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
48052e17dfb7SMatthew G. Knepley {
48062e17dfb7SMatthew G. Knepley   DM             cdm;
48072e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
48082e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
48093e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
48103e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4811e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
48123e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
48133e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
48142e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
48152e17dfb7SMatthew G. Knepley 
48162e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48172e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
481892c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
4819f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
4820f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
4821f7cbd40bSStefano Zampini 
48222e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
48232e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
48242e17dfb7SMatthew G. Knepley   {
48252e17dfb7SMatthew G. Knepley     PetscBool isplex;
48262e17dfb7SMatthew G. Knepley 
48272e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
48282e17dfb7SMatthew G. Knepley     if (isplex) {
48292e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
48303e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
483169291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
48323e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
48333e922f36SToby Isaac       newStart = vStart;
48343e922f36SToby Isaac       newEnd   = vEnd;
48353e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
48363e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
48373e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
48383e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
48393e922f36SToby Isaac       }
48402e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
48412e17dfb7SMatthew G. Knepley   }
48422e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
484343eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
48442e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
48453e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
4846e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
48473e922f36SToby Isaac 
48482e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
48492e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
48502e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
48512e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
48523e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
48533e922f36SToby Isaac 
485469291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
48553e922f36SToby Isaac   localized = &anchor[bs];
48563e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
48573e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
48583e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
48593e922f36SToby Isaac 
48603e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
48613e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
48623e922f36SToby Isaac       PetscInt     b;
48633e922f36SToby Isaac 
48643e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
48653e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48663e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
48673e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
48683e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
48693e922f36SToby Isaac         for (b = 0; b < bs; b++) {
48703e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
48713e922f36SToby Isaac         }
48723e922f36SToby Isaac         if (b < bs) break;
48733e922f36SToby Isaac       }
48743e922f36SToby Isaac       if (d < dof/bs) {
48753e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
48763e922f36SToby Isaac           PetscInt cdof;
48773e922f36SToby Isaac 
48783e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
48793e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
48803e922f36SToby Isaac         }
48813e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
48823e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
48833e922f36SToby Isaac       }
48843e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48853e922f36SToby Isaac     }
48863e922f36SToby Isaac   }
48873e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
48883e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
488969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
48903e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
489169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
48923e922f36SToby Isaac     PetscFunctionReturn(0);
48933e922f36SToby Isaac   }
48942e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
48952e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
48962e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
48972e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
48982e17dfb7SMatthew G. Knepley   }
48992e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
49002e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
4901c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
49022e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
49032e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
49042e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
49052e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
4906c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
49072e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
49082e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
49092e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
49102e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
49112e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
49122e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
49132e17dfb7SMatthew G. Knepley   }
49143e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
49153e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
49163e922f36SToby Isaac 
49172e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
49182e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
49193e922f36SToby Isaac       PetscInt     b, cdof;
49202e17dfb7SMatthew G. Knepley 
49213e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
49223e922f36SToby Isaac       if (!cdof) continue;
49232e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49242e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
49252e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
49262e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
49272e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49282e17dfb7SMatthew G. Knepley     }
49293e922f36SToby Isaac   }
493069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
493169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
4932c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
49332e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
49342e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
49352e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
49362e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
49372e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
49382e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
49392e17dfb7SMatthew G. Knepley }
49402e17dfb7SMatthew G. Knepley 
4941e87bb0d3SMatthew G Knepley /*@
49423a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
4943e87bb0d3SMatthew G Knepley 
49443a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
4945e87bb0d3SMatthew G Knepley 
4946e87bb0d3SMatthew G Knepley   Input Parameters:
4947e87bb0d3SMatthew G Knepley + dm - The DM
49483a93e3b7SToby Isaac . v - The Vec of points
494962a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
49503a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
4951e87bb0d3SMatthew G Knepley 
495261e3bb9bSMatthew G Knepley   Output Parameter:
495362a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
495462a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
49553a93e3b7SToby Isaac 
4956e87bb0d3SMatthew G Knepley 
4957e87bb0d3SMatthew G Knepley   Level: developer
495861e3bb9bSMatthew G Knepley 
495962a38674SMatthew G. Knepley   Notes:
49603a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
496162a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
49623a93e3b7SToby Isaac 
49633a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
496462a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
49653a93e3b7SToby Isaac 
49663a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
49673a93e3b7SToby Isaac 
496862a38674SMatthew G. Knepley $    const PetscSFNode *cells;
496962a38674SMatthew G. Knepley $    PetscInt           nFound;
497062a38674SMatthew G. Knepley $    const PetscSFNode *found;
497162a38674SMatthew G. Knepley $
497262a38674SMatthew G. Knepley $    PetscSFGetGraph(cells,NULL,&nFound,&found,&cells);
49733a93e3b7SToby Isaac 
49743a93e3b7SToby 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
49753a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
49763a93e3b7SToby Isaac 
497761e3bb9bSMatthew G Knepley .keywords: point location, mesh
497862a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
497961e3bb9bSMatthew G Knepley @*/
498062a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
4981e87bb0d3SMatthew G Knepley {
4982735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4983735aa83eSMatthew G Knepley 
4984e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4985e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4986e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4987e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
49883a93e3b7SToby Isaac   if (*cellSF) {
49893a93e3b7SToby Isaac     PetscMPIInt result;
49903a93e3b7SToby Isaac 
4991e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
4992a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
49933a93e3b7SToby 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");
4994e0fc9d1bSMatthew G. Knepley   } else {
49953a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
49963a93e3b7SToby Isaac   }
499747a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4998735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
499962a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
500082f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
500147a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5002e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
5003e87bb0d3SMatthew G Knepley }
500414f150ffSMatthew G. Knepley 
5005f4d763aaSMatthew G. Knepley /*@
5006f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
5007f4d763aaSMatthew G. Knepley 
5008f4d763aaSMatthew G. Knepley   Input Parameter:
5009f4d763aaSMatthew G. Knepley . dm - The original DM
5010f4d763aaSMatthew G. Knepley 
5011f4d763aaSMatthew G. Knepley   Output Parameter:
5012f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
5013f4d763aaSMatthew G. Knepley 
5014f4d763aaSMatthew G. Knepley   Level: intermediate
5015f4d763aaSMatthew G. Knepley 
5016f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
5017f4d763aaSMatthew G. Knepley @*/
501814f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
501914f150ffSMatthew G. Knepley {
5020c26acbdeSMatthew G. Knepley   PetscSection   section;
50212d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
502214f150ffSMatthew G. Knepley   PetscErrorCode ierr;
502314f150ffSMatthew G. Knepley 
502414f150ffSMatthew G. Knepley   PetscFunctionBegin;
502514f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
502614f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
5027c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
5028c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5029127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
50302d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
5031c26acbdeSMatthew G. Knepley     *odm = dm;
5032c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
5033c26acbdeSMatthew G. Knepley   }
503414f150ffSMatthew G. Knepley   if (!dm->dmBC) {
50350305aff6SMatthew G. Knepley     PetscDS      ds;
5036c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
503714f150ffSMatthew G. Knepley     PetscSF      sf;
503814f150ffSMatthew G. Knepley 
503914f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
50400305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
50410305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
504214f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
504314f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
504414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
504514f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
504615b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
504714f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
504814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
504914f150ffSMatthew G. Knepley   }
505014f150ffSMatthew G. Knepley   *odm = dm->dmBC;
505114f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
505214f150ffSMatthew G. Knepley }
5053f4d763aaSMatthew G. Knepley 
5054f4d763aaSMatthew G. Knepley /*@
5055cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5056f4d763aaSMatthew G. Knepley 
5057f4d763aaSMatthew G. Knepley   Input Parameter:
5058f4d763aaSMatthew G. Knepley . dm - The original DM
5059f4d763aaSMatthew G. Knepley 
5060cdb7a50dSMatthew G. Knepley   Output Parameters:
5061cdb7a50dSMatthew G. Knepley + num - The output sequence number
5062cdb7a50dSMatthew G. Knepley - val - The output sequence value
5063f4d763aaSMatthew G. Knepley 
5064f4d763aaSMatthew G. Knepley   Level: intermediate
5065f4d763aaSMatthew G. Knepley 
5066f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5067f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5068f4d763aaSMatthew G. Knepley 
5069f4d763aaSMatthew G. Knepley .seealso: VecView()
5070f4d763aaSMatthew G. Knepley @*/
5071cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5072f4d763aaSMatthew G. Knepley {
5073f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5074f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5075cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5076cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5077f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5078f4d763aaSMatthew G. Knepley }
5079f4d763aaSMatthew G. Knepley 
5080f4d763aaSMatthew G. Knepley /*@
5081cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5082f4d763aaSMatthew G. Knepley 
5083f4d763aaSMatthew G. Knepley   Input Parameters:
5084f4d763aaSMatthew G. Knepley + dm - The original DM
5085cdb7a50dSMatthew G. Knepley . num - The output sequence number
5086cdb7a50dSMatthew G. Knepley - val - The output sequence value
5087f4d763aaSMatthew G. Knepley 
5088f4d763aaSMatthew G. Knepley   Level: intermediate
5089f4d763aaSMatthew G. Knepley 
5090f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5091f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5092f4d763aaSMatthew G. Knepley 
5093f4d763aaSMatthew G. Knepley .seealso: VecView()
5094f4d763aaSMatthew G. Knepley @*/
5095cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5096f4d763aaSMatthew G. Knepley {
5097f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5098f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5099f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5100cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5101cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5102cdb7a50dSMatthew G. Knepley }
5103cdb7a50dSMatthew G. Knepley 
5104cdb7a50dSMatthew G. Knepley /*@C
5105cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5106cdb7a50dSMatthew G. Knepley 
5107cdb7a50dSMatthew G. Knepley   Input Parameters:
5108cdb7a50dSMatthew G. Knepley + dm   - The original DM
5109cdb7a50dSMatthew G. Knepley . name - The sequence name
5110cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5111cdb7a50dSMatthew G. Knepley 
5112cdb7a50dSMatthew G. Knepley   Output Parameter:
5113cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5114cdb7a50dSMatthew G. Knepley 
5115cdb7a50dSMatthew G. Knepley   Level: intermediate
5116cdb7a50dSMatthew G. Knepley 
5117cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5118cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5119cdb7a50dSMatthew G. Knepley 
5120cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5121cdb7a50dSMatthew G. Knepley @*/
5122cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5123cdb7a50dSMatthew G. Knepley {
5124cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5125cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5126cdb7a50dSMatthew G. Knepley 
5127cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5128cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5129cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5130cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5131cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5132cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5133cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5134cdb7a50dSMatthew G. Knepley     PetscScalar value;
5135cdb7a50dSMatthew G. Knepley 
513639d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
51374aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5138cdb7a50dSMatthew G. Knepley #endif
5139cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5140f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5141f4d763aaSMatthew G. Knepley }
51428e4ac7eaSMatthew G. Knepley 
51438e4ac7eaSMatthew G. Knepley /*@
51448e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
51458e4ac7eaSMatthew G. Knepley 
51468e4ac7eaSMatthew G. Knepley   Not collective
51478e4ac7eaSMatthew G. Knepley 
51488e4ac7eaSMatthew G. Knepley   Input Parameter:
51498e4ac7eaSMatthew G. Knepley . dm - The DM
51508e4ac7eaSMatthew G. Knepley 
51518e4ac7eaSMatthew G. Knepley   Output Parameter:
51528e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
51538e4ac7eaSMatthew G. Knepley 
51548e4ac7eaSMatthew G. Knepley   Level: beginner
51558e4ac7eaSMatthew G. Knepley 
51568e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
51578e4ac7eaSMatthew G. Knepley @*/
51588e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
51598e4ac7eaSMatthew G. Knepley {
51608e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
51618e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51628e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
51638e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
51648e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
51658e4ac7eaSMatthew G. Knepley }
51668e4ac7eaSMatthew G. Knepley 
51678e4ac7eaSMatthew G. Knepley /*@
51688e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
51698e4ac7eaSMatthew G. Knepley 
51708e4ac7eaSMatthew G. Knepley   Collective on dm
51718e4ac7eaSMatthew G. Knepley 
51728e4ac7eaSMatthew G. Knepley   Input Parameters:
51738e4ac7eaSMatthew G. Knepley + dm - The DM
51748e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
51758e4ac7eaSMatthew G. Knepley 
51768e4ac7eaSMatthew G. Knepley   Level: beginner
51778e4ac7eaSMatthew G. Knepley 
51788e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
51798e4ac7eaSMatthew G. Knepley @*/
51808e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
51818e4ac7eaSMatthew G. Knepley {
51828e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
51838e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51848e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
51858e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
51868e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
51878e4ac7eaSMatthew G. Knepley }
5188c58f1c22SToby Isaac 
5189c58f1c22SToby Isaac 
5190c58f1c22SToby Isaac /*@C
5191c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5192c58f1c22SToby Isaac 
5193c58f1c22SToby Isaac   Not Collective
5194c58f1c22SToby Isaac 
5195c58f1c22SToby Isaac   Input Parameters:
5196c58f1c22SToby Isaac + dm   - The DM object
5197c58f1c22SToby Isaac - name - The label name
5198c58f1c22SToby Isaac 
5199c58f1c22SToby Isaac   Level: intermediate
5200c58f1c22SToby Isaac 
5201c58f1c22SToby Isaac .keywords: mesh
5202c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5203c58f1c22SToby Isaac @*/
5204c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5205c58f1c22SToby Isaac {
5206c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5207c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5208c58f1c22SToby Isaac   PetscErrorCode ierr;
5209c58f1c22SToby Isaac 
5210c58f1c22SToby Isaac   PetscFunctionBegin;
5211c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5212c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5213c58f1c22SToby Isaac   while (next) {
5214c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5215c58f1c22SToby Isaac     if (flg) break;
5216c58f1c22SToby Isaac     next = next->next;
5217c58f1c22SToby Isaac   }
5218c58f1c22SToby Isaac   if (!flg) {
5219c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5220c58f1c22SToby Isaac 
5221c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5222c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5223c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5224c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5225c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5226c58f1c22SToby Isaac   }
5227c58f1c22SToby Isaac   PetscFunctionReturn(0);
5228c58f1c22SToby Isaac }
5229c58f1c22SToby Isaac 
5230c58f1c22SToby Isaac /*@C
5231c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5232c58f1c22SToby Isaac 
5233c58f1c22SToby Isaac   Not Collective
5234c58f1c22SToby Isaac 
5235c58f1c22SToby Isaac   Input Parameters:
5236c58f1c22SToby Isaac + dm   - The DM object
5237c58f1c22SToby Isaac . name - The label name
5238c58f1c22SToby Isaac - point - The mesh point
5239c58f1c22SToby Isaac 
5240c58f1c22SToby Isaac   Output Parameter:
5241c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5242c58f1c22SToby Isaac 
5243c58f1c22SToby Isaac   Level: beginner
5244c58f1c22SToby Isaac 
5245c58f1c22SToby Isaac .keywords: mesh
5246c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5247c58f1c22SToby Isaac @*/
5248c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5249c58f1c22SToby Isaac {
5250c58f1c22SToby Isaac   DMLabel        label;
5251c58f1c22SToby Isaac   PetscErrorCode ierr;
5252c58f1c22SToby Isaac 
5253c58f1c22SToby Isaac   PetscFunctionBegin;
5254c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5255c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5256c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
525713903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5258c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5259c58f1c22SToby Isaac   PetscFunctionReturn(0);
5260c58f1c22SToby Isaac }
5261c58f1c22SToby Isaac 
5262c58f1c22SToby Isaac /*@C
5263c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5264c58f1c22SToby Isaac 
5265c58f1c22SToby Isaac   Not Collective
5266c58f1c22SToby Isaac 
5267c58f1c22SToby Isaac   Input Parameters:
5268c58f1c22SToby Isaac + dm   - The DM object
5269c58f1c22SToby Isaac . name - The label name
5270c58f1c22SToby Isaac . point - The mesh point
5271c58f1c22SToby Isaac - value - The label value for this point
5272c58f1c22SToby Isaac 
5273c58f1c22SToby Isaac   Output Parameter:
5274c58f1c22SToby Isaac 
5275c58f1c22SToby Isaac   Level: beginner
5276c58f1c22SToby Isaac 
5277c58f1c22SToby Isaac .keywords: mesh
5278c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5279c58f1c22SToby Isaac @*/
5280c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5281c58f1c22SToby Isaac {
5282c58f1c22SToby Isaac   DMLabel        label;
5283c58f1c22SToby Isaac   PetscErrorCode ierr;
5284c58f1c22SToby Isaac 
5285c58f1c22SToby Isaac   PetscFunctionBegin;
5286c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5287c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5288c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5289c58f1c22SToby Isaac   if (!label) {
5290c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5291c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5292c58f1c22SToby Isaac   }
5293c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5294c58f1c22SToby Isaac   PetscFunctionReturn(0);
5295c58f1c22SToby Isaac }
5296c58f1c22SToby Isaac 
5297c58f1c22SToby Isaac /*@C
5298c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5299c58f1c22SToby Isaac 
5300c58f1c22SToby Isaac   Not Collective
5301c58f1c22SToby Isaac 
5302c58f1c22SToby Isaac   Input Parameters:
5303c58f1c22SToby Isaac + dm   - The DM object
5304c58f1c22SToby Isaac . name - The label name
5305c58f1c22SToby Isaac . point - The mesh point
5306c58f1c22SToby Isaac - value - The label value for this point
5307c58f1c22SToby Isaac 
5308c58f1c22SToby Isaac   Output Parameter:
5309c58f1c22SToby Isaac 
5310c58f1c22SToby Isaac   Level: beginner
5311c58f1c22SToby Isaac 
5312c58f1c22SToby Isaac .keywords: mesh
5313c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5314c58f1c22SToby Isaac @*/
5315c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5316c58f1c22SToby Isaac {
5317c58f1c22SToby Isaac   DMLabel        label;
5318c58f1c22SToby Isaac   PetscErrorCode ierr;
5319c58f1c22SToby Isaac 
5320c58f1c22SToby Isaac   PetscFunctionBegin;
5321c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5322c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5323c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5324c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5325c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5326c58f1c22SToby Isaac   PetscFunctionReturn(0);
5327c58f1c22SToby Isaac }
5328c58f1c22SToby Isaac 
5329c58f1c22SToby Isaac /*@C
5330c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5331c58f1c22SToby Isaac 
5332c58f1c22SToby Isaac   Not Collective
5333c58f1c22SToby Isaac 
5334c58f1c22SToby Isaac   Input Parameters:
5335c58f1c22SToby Isaac + dm   - The DM object
5336c58f1c22SToby Isaac - name - The label name
5337c58f1c22SToby Isaac 
5338c58f1c22SToby Isaac   Output Parameter:
5339c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5340c58f1c22SToby Isaac 
5341c58f1c22SToby Isaac   Level: beginner
5342c58f1c22SToby Isaac 
5343c58f1c22SToby Isaac .keywords: mesh
5344c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5345c58f1c22SToby Isaac @*/
5346c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5347c58f1c22SToby Isaac {
5348c58f1c22SToby Isaac   DMLabel        label;
5349c58f1c22SToby Isaac   PetscErrorCode ierr;
5350c58f1c22SToby Isaac 
5351c58f1c22SToby Isaac   PetscFunctionBegin;
5352c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5353c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5354c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5355c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5356c58f1c22SToby Isaac   *size = 0;
5357c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5358c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5359c58f1c22SToby Isaac   PetscFunctionReturn(0);
5360c58f1c22SToby Isaac }
5361c58f1c22SToby Isaac 
5362c58f1c22SToby Isaac /*@C
5363c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5364c58f1c22SToby Isaac 
5365c58f1c22SToby Isaac   Not Collective
5366c58f1c22SToby Isaac 
5367c58f1c22SToby Isaac   Input Parameters:
5368c58f1c22SToby Isaac + mesh - The DM object
5369c58f1c22SToby Isaac - name - The label name
5370c58f1c22SToby Isaac 
5371c58f1c22SToby Isaac   Output Parameter:
5372c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5373c58f1c22SToby Isaac 
5374c58f1c22SToby Isaac   Level: beginner
5375c58f1c22SToby Isaac 
5376c58f1c22SToby Isaac .keywords: mesh
5377c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5378c58f1c22SToby Isaac @*/
5379c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5380c58f1c22SToby Isaac {
5381c58f1c22SToby Isaac   DMLabel        label;
5382c58f1c22SToby Isaac   PetscErrorCode ierr;
5383c58f1c22SToby Isaac 
5384c58f1c22SToby Isaac   PetscFunctionBegin;
5385c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5386c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5387c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5388c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5389c58f1c22SToby Isaac   *ids = NULL;
5390c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5391c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5392c58f1c22SToby Isaac   PetscFunctionReturn(0);
5393c58f1c22SToby Isaac }
5394c58f1c22SToby Isaac 
5395c58f1c22SToby Isaac /*@C
5396c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5397c58f1c22SToby Isaac 
5398c58f1c22SToby Isaac   Not Collective
5399c58f1c22SToby Isaac 
5400c58f1c22SToby Isaac   Input Parameters:
5401c58f1c22SToby Isaac + dm - The DM object
5402c58f1c22SToby Isaac . name - The label name
5403c58f1c22SToby Isaac - value - The stratum value
5404c58f1c22SToby Isaac 
5405c58f1c22SToby Isaac   Output Parameter:
5406c58f1c22SToby Isaac . size - The stratum size
5407c58f1c22SToby Isaac 
5408c58f1c22SToby Isaac   Level: beginner
5409c58f1c22SToby Isaac 
5410c58f1c22SToby Isaac .keywords: mesh
5411c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5412c58f1c22SToby Isaac @*/
5413c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5414c58f1c22SToby Isaac {
5415c58f1c22SToby Isaac   DMLabel        label;
5416c58f1c22SToby Isaac   PetscErrorCode ierr;
5417c58f1c22SToby Isaac 
5418c58f1c22SToby Isaac   PetscFunctionBegin;
5419c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5420c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5421c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5422c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5423c58f1c22SToby Isaac   *size = 0;
5424c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5425c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5426c58f1c22SToby Isaac   PetscFunctionReturn(0);
5427c58f1c22SToby Isaac }
5428c58f1c22SToby Isaac 
5429c58f1c22SToby Isaac /*@C
5430c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5431c58f1c22SToby Isaac 
5432c58f1c22SToby Isaac   Not Collective
5433c58f1c22SToby Isaac 
5434c58f1c22SToby Isaac   Input Parameters:
5435c58f1c22SToby Isaac + dm - The DM object
5436c58f1c22SToby Isaac . name - The label name
5437c58f1c22SToby Isaac - value - The stratum value
5438c58f1c22SToby Isaac 
5439c58f1c22SToby Isaac   Output Parameter:
5440c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5441c58f1c22SToby Isaac 
5442c58f1c22SToby Isaac   Level: beginner
5443c58f1c22SToby Isaac 
5444c58f1c22SToby Isaac .keywords: mesh
5445c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5446c58f1c22SToby Isaac @*/
5447c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5448c58f1c22SToby Isaac {
5449c58f1c22SToby Isaac   DMLabel        label;
5450c58f1c22SToby Isaac   PetscErrorCode ierr;
5451c58f1c22SToby Isaac 
5452c58f1c22SToby Isaac   PetscFunctionBegin;
5453c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5454c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5455c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5456c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5457c58f1c22SToby Isaac   *points = NULL;
5458c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5459c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5460c58f1c22SToby Isaac   PetscFunctionReturn(0);
5461c58f1c22SToby Isaac }
5462c58f1c22SToby Isaac 
54634de306b1SToby Isaac /*@C
54644de306b1SToby Isaac   DMGetStratumIS - Set the points in a label stratum
54654de306b1SToby Isaac 
54664de306b1SToby Isaac   Not Collective
54674de306b1SToby Isaac 
54684de306b1SToby Isaac   Input Parameters:
54694de306b1SToby Isaac + dm - The DM object
54704de306b1SToby Isaac . name - The label name
54714de306b1SToby Isaac . value - The stratum value
54724de306b1SToby Isaac - points - The stratum points
54734de306b1SToby Isaac 
54744de306b1SToby Isaac   Level: beginner
54754de306b1SToby Isaac 
54764de306b1SToby Isaac .keywords: mesh
54774de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
54784de306b1SToby Isaac @*/
54794de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
54804de306b1SToby Isaac {
54814de306b1SToby Isaac   DMLabel        label;
54824de306b1SToby Isaac   PetscErrorCode ierr;
54834de306b1SToby Isaac 
54844de306b1SToby Isaac   PetscFunctionBegin;
54854de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54864de306b1SToby Isaac   PetscValidCharPointer(name, 2);
54874de306b1SToby Isaac   PetscValidPointer(points, 4);
54884de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
54894de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
54904de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
54914de306b1SToby Isaac   PetscFunctionReturn(0);
54924de306b1SToby Isaac }
54934de306b1SToby Isaac 
5494c58f1c22SToby Isaac /*@C
5495c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5496c58f1c22SToby Isaac 
5497c58f1c22SToby Isaac   Not Collective
5498c58f1c22SToby Isaac 
5499c58f1c22SToby Isaac   Input Parameters:
5500c58f1c22SToby Isaac + dm   - The DM object
5501c58f1c22SToby Isaac . name - The label name
5502c58f1c22SToby Isaac - value - The label value for this point
5503c58f1c22SToby Isaac 
5504c58f1c22SToby Isaac   Output Parameter:
5505c58f1c22SToby Isaac 
5506c58f1c22SToby Isaac   Level: beginner
5507c58f1c22SToby Isaac 
5508c58f1c22SToby Isaac .keywords: mesh
5509c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5510c58f1c22SToby Isaac @*/
5511c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5512c58f1c22SToby Isaac {
5513c58f1c22SToby Isaac   DMLabel        label;
5514c58f1c22SToby Isaac   PetscErrorCode ierr;
5515c58f1c22SToby Isaac 
5516c58f1c22SToby Isaac   PetscFunctionBegin;
5517c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5518c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5519c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5520c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5521c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5522c58f1c22SToby Isaac   PetscFunctionReturn(0);
5523c58f1c22SToby Isaac }
5524c58f1c22SToby Isaac 
5525c58f1c22SToby Isaac /*@
5526c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5527c58f1c22SToby Isaac 
5528c58f1c22SToby Isaac   Not Collective
5529c58f1c22SToby Isaac 
5530c58f1c22SToby Isaac   Input Parameter:
5531c58f1c22SToby Isaac . dm   - The DM object
5532c58f1c22SToby Isaac 
5533c58f1c22SToby Isaac   Output Parameter:
5534c58f1c22SToby Isaac . numLabels - the number of Labels
5535c58f1c22SToby Isaac 
5536c58f1c22SToby Isaac   Level: intermediate
5537c58f1c22SToby Isaac 
5538c58f1c22SToby Isaac .keywords: mesh
5539c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5540c58f1c22SToby Isaac @*/
5541c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5542c58f1c22SToby Isaac {
5543c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5544c58f1c22SToby Isaac   PetscInt  n    = 0;
5545c58f1c22SToby Isaac 
5546c58f1c22SToby Isaac   PetscFunctionBegin;
5547c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5548c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5549c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5550c58f1c22SToby Isaac   *numLabels = n;
5551c58f1c22SToby Isaac   PetscFunctionReturn(0);
5552c58f1c22SToby Isaac }
5553c58f1c22SToby Isaac 
5554c58f1c22SToby Isaac /*@C
5555c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5556c58f1c22SToby Isaac 
5557c58f1c22SToby Isaac   Not Collective
5558c58f1c22SToby Isaac 
5559c58f1c22SToby Isaac   Input Parameters:
5560c58f1c22SToby Isaac + dm - The DM object
5561c58f1c22SToby Isaac - n  - the label number
5562c58f1c22SToby Isaac 
5563c58f1c22SToby Isaac   Output Parameter:
5564c58f1c22SToby Isaac . name - the label name
5565c58f1c22SToby Isaac 
5566c58f1c22SToby Isaac   Level: intermediate
5567c58f1c22SToby Isaac 
5568c58f1c22SToby Isaac .keywords: mesh
5569c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5570c58f1c22SToby Isaac @*/
5571c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5572c58f1c22SToby Isaac {
5573c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5574c58f1c22SToby Isaac   PetscInt  l    = 0;
5575c58f1c22SToby Isaac 
5576c58f1c22SToby Isaac   PetscFunctionBegin;
5577c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5578c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5579c58f1c22SToby Isaac   while (next) {
5580c58f1c22SToby Isaac     if (l == n) {
5581c58f1c22SToby Isaac       *name = next->label->name;
5582c58f1c22SToby Isaac       PetscFunctionReturn(0);
5583c58f1c22SToby Isaac     }
5584c58f1c22SToby Isaac     ++l;
5585c58f1c22SToby Isaac     next = next->next;
5586c58f1c22SToby Isaac   }
5587c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5588c58f1c22SToby Isaac }
5589c58f1c22SToby Isaac 
5590c58f1c22SToby Isaac /*@C
5591c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5592c58f1c22SToby Isaac 
5593c58f1c22SToby Isaac   Not Collective
5594c58f1c22SToby Isaac 
5595c58f1c22SToby Isaac   Input Parameters:
5596c58f1c22SToby Isaac + dm   - The DM object
5597c58f1c22SToby Isaac - name - The label name
5598c58f1c22SToby Isaac 
5599c58f1c22SToby Isaac   Output Parameter:
5600c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5601c58f1c22SToby Isaac 
5602c58f1c22SToby Isaac   Level: intermediate
5603c58f1c22SToby Isaac 
5604c58f1c22SToby Isaac .keywords: mesh
5605c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5606c58f1c22SToby Isaac @*/
5607c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5608c58f1c22SToby Isaac {
5609c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5610c58f1c22SToby Isaac   PetscErrorCode ierr;
5611c58f1c22SToby Isaac 
5612c58f1c22SToby Isaac   PetscFunctionBegin;
5613c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5614c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5615c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5616c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5617c58f1c22SToby Isaac   while (next) {
5618c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5619c58f1c22SToby Isaac     if (*hasLabel) break;
5620c58f1c22SToby Isaac     next = next->next;
5621c58f1c22SToby Isaac   }
5622c58f1c22SToby Isaac   PetscFunctionReturn(0);
5623c58f1c22SToby Isaac }
5624c58f1c22SToby Isaac 
5625c58f1c22SToby Isaac /*@C
5626c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5627c58f1c22SToby Isaac 
5628c58f1c22SToby Isaac   Not Collective
5629c58f1c22SToby Isaac 
5630c58f1c22SToby Isaac   Input Parameters:
5631c58f1c22SToby Isaac + dm   - The DM object
5632c58f1c22SToby Isaac - name - The label name
5633c58f1c22SToby Isaac 
5634c58f1c22SToby Isaac   Output Parameter:
5635c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5636c58f1c22SToby Isaac 
5637c58f1c22SToby Isaac   Level: intermediate
5638c58f1c22SToby Isaac 
5639c58f1c22SToby Isaac .keywords: mesh
5640c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5641c58f1c22SToby Isaac @*/
5642c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5643c58f1c22SToby Isaac {
5644c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5645c58f1c22SToby Isaac   PetscBool      hasLabel;
5646c58f1c22SToby Isaac   PetscErrorCode ierr;
5647c58f1c22SToby Isaac 
5648c58f1c22SToby Isaac   PetscFunctionBegin;
5649c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5650c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5651c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5652c58f1c22SToby Isaac   *label = NULL;
5653c58f1c22SToby Isaac   while (next) {
5654c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5655c58f1c22SToby Isaac     if (hasLabel) {
5656c58f1c22SToby Isaac       *label = next->label;
5657c58f1c22SToby Isaac       break;
5658c58f1c22SToby Isaac     }
5659c58f1c22SToby Isaac     next = next->next;
5660c58f1c22SToby Isaac   }
5661c58f1c22SToby Isaac   PetscFunctionReturn(0);
5662c58f1c22SToby Isaac }
5663c58f1c22SToby Isaac 
5664c58f1c22SToby Isaac /*@C
5665c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5666c58f1c22SToby Isaac 
5667c58f1c22SToby Isaac   Not Collective
5668c58f1c22SToby Isaac 
5669c58f1c22SToby Isaac   Input Parameters:
5670c58f1c22SToby Isaac + dm - The DM object
5671c58f1c22SToby Isaac - n  - the label number
5672c58f1c22SToby Isaac 
5673c58f1c22SToby Isaac   Output Parameter:
5674c58f1c22SToby Isaac . label - the label
5675c58f1c22SToby Isaac 
5676c58f1c22SToby Isaac   Level: intermediate
5677c58f1c22SToby Isaac 
5678c58f1c22SToby Isaac .keywords: mesh
5679c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5680c58f1c22SToby Isaac @*/
5681c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5682c58f1c22SToby Isaac {
5683c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5684c58f1c22SToby Isaac   PetscInt    l    = 0;
5685c58f1c22SToby Isaac 
5686c58f1c22SToby Isaac   PetscFunctionBegin;
5687c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5688c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5689c58f1c22SToby Isaac   while (next) {
5690c58f1c22SToby Isaac     if (l == n) {
5691c58f1c22SToby Isaac       *label = next->label;
5692c58f1c22SToby Isaac       PetscFunctionReturn(0);
5693c58f1c22SToby Isaac     }
5694c58f1c22SToby Isaac     ++l;
5695c58f1c22SToby Isaac     next = next->next;
5696c58f1c22SToby Isaac   }
5697c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5698c58f1c22SToby Isaac }
5699c58f1c22SToby Isaac 
5700c58f1c22SToby Isaac /*@C
5701c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5702c58f1c22SToby Isaac 
5703c58f1c22SToby Isaac   Not Collective
5704c58f1c22SToby Isaac 
5705c58f1c22SToby Isaac   Input Parameters:
5706c58f1c22SToby Isaac + dm   - The DM object
5707c58f1c22SToby Isaac - label - The DMLabel
5708c58f1c22SToby Isaac 
5709c58f1c22SToby Isaac   Level: developer
5710c58f1c22SToby Isaac 
5711c58f1c22SToby Isaac .keywords: mesh
5712c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5713c58f1c22SToby Isaac @*/
5714c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5715c58f1c22SToby Isaac {
5716c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5717c58f1c22SToby Isaac   PetscBool      hasLabel;
5718c58f1c22SToby Isaac   PetscErrorCode ierr;
5719c58f1c22SToby Isaac 
5720c58f1c22SToby Isaac   PetscFunctionBegin;
5721c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5722c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5723c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5724c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5725c58f1c22SToby Isaac   tmpLabel->label  = label;
5726c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5727c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5728c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5729c58f1c22SToby Isaac   PetscFunctionReturn(0);
5730c58f1c22SToby Isaac }
5731c58f1c22SToby Isaac 
5732c58f1c22SToby Isaac /*@C
5733c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5734c58f1c22SToby Isaac 
5735c58f1c22SToby Isaac   Not Collective
5736c58f1c22SToby Isaac 
5737c58f1c22SToby Isaac   Input Parameters:
5738c58f1c22SToby Isaac + dm   - The DM object
5739c58f1c22SToby Isaac - name - The label name
5740c58f1c22SToby Isaac 
5741c58f1c22SToby Isaac   Output Parameter:
5742c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5743c58f1c22SToby Isaac 
5744c58f1c22SToby Isaac   Level: developer
5745c58f1c22SToby Isaac 
5746c58f1c22SToby Isaac .keywords: mesh
5747c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5748c58f1c22SToby Isaac @*/
5749c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5750c58f1c22SToby Isaac {
5751c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5752c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5753c58f1c22SToby Isaac   PetscBool      hasLabel;
5754c58f1c22SToby Isaac   PetscErrorCode ierr;
5755c58f1c22SToby Isaac 
5756c58f1c22SToby Isaac   PetscFunctionBegin;
5757c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5758c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5759c58f1c22SToby Isaac   *label = NULL;
5760c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5761c58f1c22SToby Isaac   while (next) {
5762c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5763c58f1c22SToby Isaac     if (hasLabel) {
5764c58f1c22SToby Isaac       if (last) last->next       = next->next;
5765c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5766c58f1c22SToby Isaac       next->next = NULL;
5767c58f1c22SToby Isaac       *label     = next->label;
5768c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5769c58f1c22SToby Isaac       if (hasLabel) {
5770c58f1c22SToby Isaac         dm->depthLabel = NULL;
5771c58f1c22SToby Isaac       }
5772c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5773c58f1c22SToby Isaac       break;
5774c58f1c22SToby Isaac     }
5775c58f1c22SToby Isaac     last = next;
5776c58f1c22SToby Isaac     next = next->next;
5777c58f1c22SToby Isaac   }
5778c58f1c22SToby Isaac   PetscFunctionReturn(0);
5779c58f1c22SToby Isaac }
5780c58f1c22SToby Isaac 
5781c58f1c22SToby Isaac /*@C
5782c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5783c58f1c22SToby Isaac 
5784c58f1c22SToby Isaac   Not Collective
5785c58f1c22SToby Isaac 
5786c58f1c22SToby Isaac   Input Parameters:
5787c58f1c22SToby Isaac + dm   - The DM object
5788c58f1c22SToby Isaac - name - The label name
5789c58f1c22SToby Isaac 
5790c58f1c22SToby Isaac   Output Parameter:
5791c58f1c22SToby Isaac . output - The flag for output
5792c58f1c22SToby Isaac 
5793c58f1c22SToby Isaac   Level: developer
5794c58f1c22SToby Isaac 
5795c58f1c22SToby Isaac .keywords: mesh
5796c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5797c58f1c22SToby Isaac @*/
5798c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5799c58f1c22SToby Isaac {
5800c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5801c58f1c22SToby Isaac   PetscErrorCode ierr;
5802c58f1c22SToby Isaac 
5803c58f1c22SToby Isaac   PetscFunctionBegin;
5804c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5805c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5806c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5807c58f1c22SToby Isaac   while (next) {
5808c58f1c22SToby Isaac     PetscBool flg;
5809c58f1c22SToby Isaac 
5810c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5811c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5812c58f1c22SToby Isaac     next = next->next;
5813c58f1c22SToby Isaac   }
5814c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5815c58f1c22SToby Isaac }
5816c58f1c22SToby Isaac 
5817c58f1c22SToby Isaac /*@C
5818c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5819c58f1c22SToby Isaac 
5820c58f1c22SToby Isaac   Not Collective
5821c58f1c22SToby Isaac 
5822c58f1c22SToby Isaac   Input Parameters:
5823c58f1c22SToby Isaac + dm     - The DM object
5824c58f1c22SToby Isaac . name   - The label name
5825c58f1c22SToby Isaac - output - The flag for output
5826c58f1c22SToby Isaac 
5827c58f1c22SToby Isaac   Level: developer
5828c58f1c22SToby Isaac 
5829c58f1c22SToby Isaac .keywords: mesh
5830c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5831c58f1c22SToby Isaac @*/
5832c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5833c58f1c22SToby Isaac {
5834c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5835c58f1c22SToby Isaac   PetscErrorCode ierr;
5836c58f1c22SToby Isaac 
5837c58f1c22SToby Isaac   PetscFunctionBegin;
5838c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5839c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5840c58f1c22SToby Isaac   while (next) {
5841c58f1c22SToby Isaac     PetscBool flg;
5842c58f1c22SToby Isaac 
5843c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5844c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5845c58f1c22SToby Isaac     next = next->next;
5846c58f1c22SToby Isaac   }
5847c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5848c58f1c22SToby Isaac }
5849c58f1c22SToby Isaac 
5850c58f1c22SToby Isaac 
5851c58f1c22SToby Isaac /*@
5852c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5853c58f1c22SToby Isaac 
5854c58f1c22SToby Isaac   Collective on DM
5855c58f1c22SToby Isaac 
5856c58f1c22SToby Isaac   Input Parameter:
58572e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5858c58f1c22SToby Isaac 
5859c58f1c22SToby Isaac   Output Parameter:
58602e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5861c58f1c22SToby Isaac 
5862c58f1c22SToby Isaac   Level: intermediate
5863c58f1c22SToby Isaac 
5864c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5865c58f1c22SToby Isaac 
5866c58f1c22SToby Isaac .keywords: mesh
5867c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5868c58f1c22SToby Isaac @*/
5869c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5870c58f1c22SToby Isaac {
5871c58f1c22SToby Isaac   PetscInt       numLabels, l;
5872c58f1c22SToby Isaac   PetscErrorCode ierr;
5873c58f1c22SToby Isaac 
5874c58f1c22SToby Isaac   PetscFunctionBegin;
5875c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5876c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5877c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5878c58f1c22SToby Isaac     DMLabel     label, labelNew;
5879c58f1c22SToby Isaac     const char *name;
5880c58f1c22SToby Isaac     PetscBool   flg;
5881c58f1c22SToby Isaac 
5882c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5883c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5884c58f1c22SToby Isaac     if (flg) continue;
5885c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5886c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5887c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5888c58f1c22SToby Isaac   }
5889c58f1c22SToby Isaac   PetscFunctionReturn(0);
5890c58f1c22SToby Isaac }
5891a8fb8f29SToby Isaac 
5892a8fb8f29SToby Isaac /*@
5893a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5894a8fb8f29SToby Isaac 
5895a8fb8f29SToby Isaac   Input Parameter:
5896a8fb8f29SToby Isaac . dm - The DM object
5897a8fb8f29SToby Isaac 
5898a8fb8f29SToby Isaac   Output Parameter:
5899a8fb8f29SToby Isaac . cdm - The coarse DM
5900a8fb8f29SToby Isaac 
5901a8fb8f29SToby Isaac   Level: intermediate
5902a8fb8f29SToby Isaac 
5903a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5904a8fb8f29SToby Isaac @*/
5905a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5906a8fb8f29SToby Isaac {
5907a8fb8f29SToby Isaac   PetscFunctionBegin;
5908a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5909a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5910a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5911a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5912a8fb8f29SToby Isaac }
5913a8fb8f29SToby Isaac 
5914a8fb8f29SToby Isaac /*@
5915a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5916a8fb8f29SToby Isaac 
5917a8fb8f29SToby Isaac   Input Parameters:
5918a8fb8f29SToby Isaac + dm - The DM object
5919a8fb8f29SToby Isaac - cdm - The coarse DM
5920a8fb8f29SToby Isaac 
5921a8fb8f29SToby Isaac   Level: intermediate
5922a8fb8f29SToby Isaac 
5923a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5924a8fb8f29SToby Isaac @*/
5925a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5926a8fb8f29SToby Isaac {
5927a8fb8f29SToby Isaac   PetscErrorCode ierr;
5928a8fb8f29SToby Isaac 
5929a8fb8f29SToby Isaac   PetscFunctionBegin;
5930a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5931a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5932a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5933a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5934a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5935a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5936a8fb8f29SToby Isaac }
5937a8fb8f29SToby Isaac 
593888bdff64SToby Isaac /*@
593988bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
594088bdff64SToby Isaac 
594188bdff64SToby Isaac   Input Parameter:
594288bdff64SToby Isaac . dm - The DM object
594388bdff64SToby Isaac 
594488bdff64SToby Isaac   Output Parameter:
594588bdff64SToby Isaac . fdm - The fine DM
594688bdff64SToby Isaac 
594788bdff64SToby Isaac   Level: intermediate
594888bdff64SToby Isaac 
594988bdff64SToby Isaac .seealso: DMSetFineDM()
595088bdff64SToby Isaac @*/
595188bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
595288bdff64SToby Isaac {
595388bdff64SToby Isaac   PetscFunctionBegin;
595488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
595588bdff64SToby Isaac   PetscValidPointer(fdm, 2);
595688bdff64SToby Isaac   *fdm = dm->fineMesh;
595788bdff64SToby Isaac   PetscFunctionReturn(0);
595888bdff64SToby Isaac }
595988bdff64SToby Isaac 
596088bdff64SToby Isaac /*@
596188bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
596288bdff64SToby Isaac 
596388bdff64SToby Isaac   Input Parameters:
596488bdff64SToby Isaac + dm - The DM object
596588bdff64SToby Isaac - fdm - The fine DM
596688bdff64SToby Isaac 
596788bdff64SToby Isaac   Level: intermediate
596888bdff64SToby Isaac 
596988bdff64SToby Isaac .seealso: DMGetFineDM()
597088bdff64SToby Isaac @*/
597188bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
597288bdff64SToby Isaac {
597388bdff64SToby Isaac   PetscErrorCode ierr;
597488bdff64SToby Isaac 
597588bdff64SToby Isaac   PetscFunctionBegin;
597688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
597788bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
597888bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
597988bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
598088bdff64SToby Isaac   dm->fineMesh = fdm;
598188bdff64SToby Isaac   PetscFunctionReturn(0);
598288bdff64SToby Isaac }
598388bdff64SToby Isaac 
5984a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5985a6ba4734SToby Isaac 
5986a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5987a6ba4734SToby Isaac {
5988a6ba4734SToby Isaac   PetscErrorCode ierr;
5989a6ba4734SToby Isaac 
5990a6ba4734SToby Isaac   PetscFunctionBegin;
5991e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
5992a6ba4734SToby Isaac   PetscFunctionReturn(0);
5993a6ba4734SToby Isaac }
5994a6ba4734SToby Isaac 
5995a6ba4734SToby Isaac /*@C
5996a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
5997a6ba4734SToby Isaac 
5998a6ba4734SToby Isaac   Input Parameters:
59994c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
6000f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6001a6ba4734SToby Isaac . name        - The BC name
6002a6ba4734SToby Isaac . labelname   - The label defining constrained points
6003a6ba4734SToby Isaac . field       - The field to constrain
6004a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6005a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6006a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6007a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6008a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6009a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6010a6ba4734SToby Isaac 
6011a6ba4734SToby Isaac   Options Database Keys:
6012a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6013a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6014a6ba4734SToby Isaac 
6015a6ba4734SToby Isaac   Level: developer
6016a6ba4734SToby Isaac 
6017a6ba4734SToby Isaac .seealso: DMGetBoundary()
6018a6ba4734SToby Isaac @*/
6019a30ec4eaSSatish Balay PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], const char labelname[], PetscInt field, PetscInt numcomps, const PetscInt *comps, void (*bcFunc)(void), PetscInt numids, const PetscInt *ids, void *ctx)
6020a6ba4734SToby Isaac {
6021a6ba4734SToby Isaac   PetscErrorCode ierr;
6022a6ba4734SToby Isaac 
6023a6ba4734SToby Isaac   PetscFunctionBegin;
6024a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6025f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
6026a6ba4734SToby Isaac   PetscFunctionReturn(0);
6027a6ba4734SToby Isaac }
6028a6ba4734SToby Isaac 
6029a6ba4734SToby Isaac /*@
6030a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6031a6ba4734SToby Isaac 
6032a6ba4734SToby Isaac   Input Parameters:
6033a6ba4734SToby Isaac . dm - The mesh object
6034a6ba4734SToby Isaac 
6035a6ba4734SToby Isaac   Output Parameters:
6036a6ba4734SToby Isaac . numBd - The number of BC
6037a6ba4734SToby Isaac 
6038a6ba4734SToby Isaac   Level: intermediate
6039a6ba4734SToby Isaac 
6040a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6041a6ba4734SToby Isaac @*/
6042a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6043a6ba4734SToby Isaac {
604458ebd649SToby Isaac   PetscErrorCode ierr;
6045a6ba4734SToby Isaac 
6046a6ba4734SToby Isaac   PetscFunctionBegin;
6047a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
604858ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
6049a6ba4734SToby Isaac   PetscFunctionReturn(0);
6050a6ba4734SToby Isaac }
6051a6ba4734SToby Isaac 
6052a6ba4734SToby Isaac /*@C
60531c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
6054a6ba4734SToby Isaac 
6055a6ba4734SToby Isaac   Input Parameters:
6056a6ba4734SToby Isaac + dm          - The mesh object
6057a6ba4734SToby Isaac - bd          - The BC number
6058a6ba4734SToby Isaac 
6059a6ba4734SToby Isaac   Output Parameters:
6060f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6061a6ba4734SToby Isaac . name        - The BC name
6062a6ba4734SToby Isaac . labelname   - The label defining constrained points
6063a6ba4734SToby Isaac . field       - The field to constrain
6064a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6065a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6066a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6067a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6068a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6069a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6070a6ba4734SToby Isaac 
6071a6ba4734SToby Isaac   Options Database Keys:
6072a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6073a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6074a6ba4734SToby Isaac 
6075a6ba4734SToby Isaac   Level: developer
6076a6ba4734SToby Isaac 
6077a6ba4734SToby Isaac .seealso: DMAddBoundary()
6078a6ba4734SToby Isaac @*/
6079a30ec4eaSSatish Balay PetscErrorCode DMGetBoundary(DM dm, PetscInt bd, DMBoundaryConditionType *type, const char **name, const char **labelname, PetscInt *field, PetscInt *numcomps, const PetscInt **comps, void (**func)(void), PetscInt *numids, const PetscInt **ids, void **ctx)
6080a6ba4734SToby Isaac {
608158ebd649SToby Isaac   PetscErrorCode ierr;
6082a6ba4734SToby Isaac 
6083a6ba4734SToby Isaac   PetscFunctionBegin;
6084a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6085f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6086a6ba4734SToby Isaac   PetscFunctionReturn(0);
6087a6ba4734SToby Isaac }
6088a6ba4734SToby Isaac 
6089e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6090e6f8dbb6SToby Isaac {
6091dff059c6SToby Isaac   DMBoundary *lastnext;
6092e6f8dbb6SToby Isaac   DSBoundary dsbound;
6093e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6094e6f8dbb6SToby Isaac 
6095e6f8dbb6SToby Isaac   PetscFunctionBegin;
6096e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
609747a1f5adSToby Isaac   if (dm->boundary) {
609847a1f5adSToby Isaac     DMBoundary next = dm->boundary;
609947a1f5adSToby Isaac 
610047a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
610147a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
610247a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
610347a1f5adSToby Isaac     while (next) {
610447a1f5adSToby Isaac       DMBoundary b = next;
610547a1f5adSToby Isaac 
610647a1f5adSToby Isaac       next = b->next;
610747a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6108a6ba4734SToby Isaac     }
610947a1f5adSToby Isaac     dm->boundary = NULL;
6110a6ba4734SToby Isaac   }
611147a1f5adSToby Isaac 
6112dff059c6SToby Isaac   lastnext = &(dm->boundary);
6113e6f8dbb6SToby Isaac   while (dsbound) {
6114e6f8dbb6SToby Isaac     DMBoundary dmbound;
6115e6f8dbb6SToby Isaac 
6116e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6117e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6118e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6119e6f8dbb6SToby Isaac     if (!dmbound->label) PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);
612047a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6121dff059c6SToby Isaac     *lastnext = dmbound;
6122dff059c6SToby Isaac     lastnext = &(dmbound->next);
6123dff059c6SToby Isaac     dsbound = dsbound->next;
6124a6ba4734SToby Isaac   }
6125a6ba4734SToby Isaac   PetscFunctionReturn(0);
6126a6ba4734SToby Isaac }
6127a6ba4734SToby Isaac 
6128a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6129a6ba4734SToby Isaac {
6130b95f2879SToby Isaac   DMBoundary     b;
6131a6ba4734SToby Isaac   PetscErrorCode ierr;
6132a6ba4734SToby Isaac 
6133a6ba4734SToby Isaac   PetscFunctionBegin;
6134a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6135a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6136a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6137e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6138b95f2879SToby Isaac   b = dm->boundary;
6139a6ba4734SToby Isaac   while (b && !(*isBd)) {
6140e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6141e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
61423424c85cSToby Isaac 
61433424c85cSToby Isaac     if (label) {
6144a6ba4734SToby Isaac       PetscInt i;
6145a6ba4734SToby Isaac 
6146e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6147e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6148a6ba4734SToby Isaac       }
6149a6ba4734SToby Isaac     }
6150a6ba4734SToby Isaac     b = b->next;
6151a6ba4734SToby Isaac   }
6152a6ba4734SToby Isaac   PetscFunctionReturn(0);
6153a6ba4734SToby Isaac }
61544d6f44ffSToby Isaac 
61554d6f44ffSToby Isaac /*@C
61564d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
61574d6f44ffSToby Isaac 
61584d6f44ffSToby Isaac   Input Parameters:
61594d6f44ffSToby Isaac + dm      - The DM
61600709b2feSToby Isaac . time    - The time
61614d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
61624d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
61634d6f44ffSToby Isaac - mode    - The insertion mode for values
61644d6f44ffSToby Isaac 
61654d6f44ffSToby Isaac   Output Parameter:
61664d6f44ffSToby Isaac . X - vector
61674d6f44ffSToby Isaac 
61684d6f44ffSToby Isaac    Calling sequence of func:
61690709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
61704d6f44ffSToby Isaac 
61714d6f44ffSToby Isaac +  dim - The spatial dimension
61724d6f44ffSToby Isaac .  x   - The coordinates
61734d6f44ffSToby Isaac .  Nf  - The number of fields
61744d6f44ffSToby Isaac .  u   - The output field values
61754d6f44ffSToby Isaac -  ctx - optional user-defined function context
61764d6f44ffSToby Isaac 
61774d6f44ffSToby Isaac   Level: developer
61784d6f44ffSToby Isaac 
61792716604bSToby Isaac .seealso: DMComputeL2Diff()
61804d6f44ffSToby Isaac @*/
61810709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
61824d6f44ffSToby Isaac {
61834d6f44ffSToby Isaac   Vec            localX;
61844d6f44ffSToby Isaac   PetscErrorCode ierr;
61854d6f44ffSToby Isaac 
61864d6f44ffSToby Isaac   PetscFunctionBegin;
61874d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61884d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
61890709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
61904d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
61914d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
61924d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
61934d6f44ffSToby Isaac   PetscFunctionReturn(0);
61944d6f44ffSToby Isaac }
61954d6f44ffSToby Isaac 
61960709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
61974d6f44ffSToby Isaac {
61984d6f44ffSToby Isaac   PetscErrorCode ierr;
61994d6f44ffSToby Isaac 
62004d6f44ffSToby Isaac   PetscFunctionBegin;
62014d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62024d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
62030918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
62040709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62054d6f44ffSToby Isaac   PetscFunctionReturn(0);
62064d6f44ffSToby Isaac }
62074d6f44ffSToby Isaac 
62082c53366bSMatthew G. Knepley PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
62092c53366bSMatthew G. Knepley {
62102c53366bSMatthew G. Knepley   Vec            localX;
62112c53366bSMatthew G. Knepley   PetscErrorCode ierr;
62122c53366bSMatthew G. Knepley 
62132c53366bSMatthew G. Knepley   PetscFunctionBegin;
62142c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62152c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
62162c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62172c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
62182c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
62192c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
62202c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
62212c53366bSMatthew G. Knepley }
62222c53366bSMatthew G. Knepley 
62231c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
62244d6f44ffSToby Isaac {
62254d6f44ffSToby Isaac   PetscErrorCode ierr;
62264d6f44ffSToby Isaac 
62274d6f44ffSToby Isaac   PetscFunctionBegin;
62284d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62294d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
62300918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
62311c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62324d6f44ffSToby Isaac   PetscFunctionReturn(0);
62334d6f44ffSToby Isaac }
62342716604bSToby Isaac 
62358c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
62368c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
62378c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
62388c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6239191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
62408c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
62418c6c5593SMatthew G. Knepley {
62428c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
62438c6c5593SMatthew G. Knepley 
62448c6c5593SMatthew G. Knepley   PetscFunctionBegin;
62458c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62468c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
62478c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
62480918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
62498c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
62508c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
62518c6c5593SMatthew G. Knepley }
62528c6c5593SMatthew G. Knepley 
62531c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
62548c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
62558c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
62568c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6257191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
62588c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
62598c6c5593SMatthew G. Knepley {
62608c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
62618c6c5593SMatthew G. Knepley 
62628c6c5593SMatthew G. Knepley   PetscFunctionBegin;
62638c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62648c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
62658c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
62660918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
62671c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
62688c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
62698c6c5593SMatthew G. Knepley }
62708c6c5593SMatthew G. Knepley 
62712716604bSToby Isaac /*@C
62722716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
62732716604bSToby Isaac 
62742716604bSToby Isaac   Input Parameters:
62752716604bSToby Isaac + dm    - The DM
62760709b2feSToby Isaac . time  - The time
62772716604bSToby Isaac . funcs - The functions to evaluate for each field component
62782716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6279574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
62802716604bSToby Isaac 
62812716604bSToby Isaac   Output Parameter:
62822716604bSToby Isaac . diff - The diff ||u - u_h||_2
62832716604bSToby Isaac 
62842716604bSToby Isaac   Level: developer
62852716604bSToby Isaac 
62861189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
62872716604bSToby Isaac @*/
62880709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
62892716604bSToby Isaac {
62902716604bSToby Isaac   PetscErrorCode ierr;
62912716604bSToby Isaac 
62922716604bSToby Isaac   PetscFunctionBegin;
62932716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6294b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
62950918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
62960709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
62972716604bSToby Isaac   PetscFunctionReturn(0);
62982716604bSToby Isaac }
6299b698f381SToby Isaac 
6300b698f381SToby Isaac /*@C
6301b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6302b698f381SToby Isaac 
6303b698f381SToby Isaac   Input Parameters:
6304b698f381SToby Isaac + dm    - The DM
6305b698f381SToby Isaac , time  - The time
6306b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6307b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6308574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
6309b698f381SToby Isaac - n     - The vector to project along
6310b698f381SToby Isaac 
6311b698f381SToby Isaac   Output Parameter:
6312b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6313b698f381SToby Isaac 
6314b698f381SToby Isaac   Level: developer
6315b698f381SToby Isaac 
6316b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6317b698f381SToby Isaac @*/
6318b698f381SToby 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)
6319b698f381SToby Isaac {
6320b698f381SToby Isaac   PetscErrorCode ierr;
6321b698f381SToby Isaac 
6322b698f381SToby Isaac   PetscFunctionBegin;
6323b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6324b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6325b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6326b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6327b698f381SToby Isaac   PetscFunctionReturn(0);
6328b698f381SToby Isaac }
6329b698f381SToby Isaac 
63302a16baeaSToby Isaac /*@C
63312a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
63322a16baeaSToby Isaac 
63332a16baeaSToby Isaac   Input Parameters:
63342a16baeaSToby Isaac + dm    - The DM
63352a16baeaSToby Isaac . time  - The time
63362a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
63372a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6338574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
63392a16baeaSToby Isaac 
63402a16baeaSToby Isaac   Output Parameter:
63412a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
63422a16baeaSToby Isaac 
63432a16baeaSToby Isaac   Level: developer
63442a16baeaSToby Isaac 
63451189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
63462a16baeaSToby Isaac @*/
63471189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
63482a16baeaSToby Isaac {
63492a16baeaSToby Isaac   PetscErrorCode ierr;
63502a16baeaSToby Isaac 
63512a16baeaSToby Isaac   PetscFunctionBegin;
63522a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63532a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
63540918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
63552a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
63562a16baeaSToby Isaac   PetscFunctionReturn(0);
63572a16baeaSToby Isaac }
63582a16baeaSToby Isaac 
6359df0b854cSToby Isaac /*@C
6360df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6361cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6362df0b854cSToby Isaac 
6363df0b854cSToby Isaac   Collective on dm
6364df0b854cSToby Isaac 
6365df0b854cSToby Isaac   Input parameters:
6366df0b854cSToby Isaac + dm - the pre-adaptation DM object
6367a1b0c543SToby Isaac - label - label with the flags
6368df0b854cSToby Isaac 
6369df0b854cSToby Isaac   Output parameters:
63700d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6371df0b854cSToby Isaac 
6372df0b854cSToby Isaac   Level: intermediate
63730d1cd5e0SMatthew G. Knepley 
63740d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6375df0b854cSToby Isaac @*/
63760d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6377df0b854cSToby Isaac {
6378df0b854cSToby Isaac   PetscErrorCode ierr;
6379df0b854cSToby Isaac 
6380df0b854cSToby Isaac   PetscFunctionBegin;
6381df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6382a1b0c543SToby Isaac   PetscValidPointer(label,2);
63830d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
63840d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
63856f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
63860d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
63870d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
63880d1cd5e0SMatthew G. Knepley }
63890d1cd5e0SMatthew G. Knepley 
63900d1cd5e0SMatthew G. Knepley /*@C
63910d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
63920d1cd5e0SMatthew G. Knepley 
63930d1cd5e0SMatthew G. Knepley   Input Parameters:
63940d1cd5e0SMatthew G. Knepley + dm - The DM object
63950d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
63966f25b0d8SLisandro Dalcin - bdLabel - Label for boundary tags, which will be preserved in the output mesh. bdLabel should be NULL if there is no such label, and should be different from "_boundary_".
63970d1cd5e0SMatthew G. Knepley 
63980d1cd5e0SMatthew G. Knepley   Output Parameter:
63990d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
64000d1cd5e0SMatthew G. Knepley 
64010d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
64020d1cd5e0SMatthew G. Knepley 
64030d1cd5e0SMatthew G. Knepley   Level: advanced
64040d1cd5e0SMatthew G. Knepley 
64050d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
64060d1cd5e0SMatthew G. Knepley @*/
64070d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
64080d1cd5e0SMatthew G. Knepley {
64090d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
64100d1cd5e0SMatthew G. Knepley 
64110d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
64120d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64130d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
64140d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
64150d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
64166f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
64176f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
64180d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6419df0b854cSToby Isaac   PetscFunctionReturn(0);
6420df0b854cSToby Isaac }
6421c4088d22SMatthew G. Knepley 
6422502a2867SDave May /*@C
6423502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6424502a2867SDave May 
6425502a2867SDave May  Not Collective
6426502a2867SDave May 
6427502a2867SDave May  Input Parameter:
6428502a2867SDave May  . dm    - The DM
6429502a2867SDave May 
6430502a2867SDave May  Output Parameter:
6431502a2867SDave May  . nranks - the number of neighbours
6432502a2867SDave May  . ranks - the neighbors ranks
6433502a2867SDave May 
6434502a2867SDave May  Notes:
6435502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6436502a2867SDave May 
6437502a2867SDave May  Level: beginner
6438502a2867SDave May 
6439dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6440502a2867SDave May @*/
6441502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6442502a2867SDave May {
6443502a2867SDave May   PetscErrorCode ierr;
6444502a2867SDave May 
6445502a2867SDave May   PetscFunctionBegin;
6446502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64470918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
6448502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6449502a2867SDave May   PetscFunctionReturn(0);
6450502a2867SDave May }
6451502a2867SDave May 
6452531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6453531c7667SBarry Smith 
6454531c7667SBarry Smith /*
6455531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6456531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6457531c7667SBarry Smith */
6458531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6459531c7667SBarry Smith {
6460531c7667SBarry Smith   PetscErrorCode ierr;
6461531c7667SBarry Smith 
6462531c7667SBarry Smith   PetscFunctionBegin;
6463531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6464531c7667SBarry Smith     Vec x1local;
6465531c7667SBarry Smith     DM  dm;
6466531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6467531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6468531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6469531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6470531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6471531c7667SBarry Smith     x1   = x1local;
6472531c7667SBarry Smith   }
6473531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6474531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6475531c7667SBarry Smith     DM  dm;
6476531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6477531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6478531c7667SBarry Smith   }
6479531c7667SBarry Smith   PetscFunctionReturn(0);
6480531c7667SBarry Smith }
6481531c7667SBarry Smith 
6482531c7667SBarry Smith /*@
6483531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6484531c7667SBarry Smith 
6485531c7667SBarry Smith     Input Parameter:
6486531c7667SBarry Smith .    coloring - the MatFDColoring object
6487531c7667SBarry Smith 
6488531c7667SBarry Smith     Developer Notes: this routine exists because the PETSc Mat library does not know about the DM objects
6489531c7667SBarry Smith 
64901b266c99SBarry Smith     Level: advanced
64911b266c99SBarry Smith 
6492531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6493531c7667SBarry Smith @*/
6494531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6495531c7667SBarry Smith {
6496531c7667SBarry Smith   PetscFunctionBegin;
6497531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6498531c7667SBarry Smith   PetscFunctionReturn(0);
6499531c7667SBarry Smith }
6500