xref: /petsc/src/dm/interface/dm.c (revision e5893cccc5eb5f80acff7e5e7fe1e2714c660077)
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 
134a7a4c06SLawrence Mitchell static PetscErrorCode DMHasCreateInjection_Default(DM dm, PetscBool *flg)
144a7a4c06SLawrence Mitchell {
154a7a4c06SLawrence Mitchell   PetscFunctionBegin;
164a7a4c06SLawrence Mitchell   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
174a7a4c06SLawrence Mitchell   PetscValidPointer(flg,2);
184a7a4c06SLawrence Mitchell   *flg = PETSC_FALSE;
194a7a4c06SLawrence Mitchell   PetscFunctionReturn(0);
204a7a4c06SLawrence Mitchell }
214a7a4c06SLawrence 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;
854a7a4c06SLawrence Mitchell 
864a7a4c06SLawrence Mitchell   v->ops->hascreateinjection = DMHasCreateInjection_Default;
874a7a4c06SLawrence 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;
81463b15415SAlp Dener   PetscInt          tabs;
81547c6ae99SBarry Smith 
81647c6ae99SBarry Smith   PetscFunctionBegin;
817171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8183014e516SBarry Smith   if (!v) {
819ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8203014e516SBarry Smith   }
82176a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
82276a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
82376a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
82463b15415SAlp Dener   ierr = PetscViewerASCIIGetTab(v, &tabs);CHKERRQ(ierr);
82555784310SAlp Dener   ierr = PetscViewerASCIISetTab(v, ((PetscObject)dm)->tablevel);CHKERRQ(ierr);
82698c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
82732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
82832c0f0efSBarry Smith   if (isbinary) {
82955849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
83032c0f0efSBarry Smith     char     type[256];
83132c0f0efSBarry Smith 
83232c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
83332c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
83432c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
83532c0f0efSBarry Smith   }
8360c010503SBarry Smith   if (dm->ops->view) {
8370c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
83847c6ae99SBarry Smith   }
83963b15415SAlp Dener   ierr = PetscViewerASCIISetTab(v, tabs);CHKERRQ(ierr);
84047c6ae99SBarry Smith   PetscFunctionReturn(0);
84147c6ae99SBarry Smith }
84247c6ae99SBarry Smith 
84347c6ae99SBarry Smith /*@
8448472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
84547c6ae99SBarry Smith 
84647c6ae99SBarry Smith     Collective on DM
84747c6ae99SBarry Smith 
84847c6ae99SBarry Smith     Input Parameter:
84947c6ae99SBarry Smith .   dm - the DM object
85047c6ae99SBarry Smith 
85147c6ae99SBarry Smith     Output Parameter:
85247c6ae99SBarry Smith .   vec - the global vector
85347c6ae99SBarry Smith 
854073dac72SJed Brown     Level: beginner
85547c6ae99SBarry Smith 
856e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
85747c6ae99SBarry Smith 
85847c6ae99SBarry Smith @*/
8597087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
86047c6ae99SBarry Smith {
86147c6ae99SBarry Smith   PetscErrorCode ierr;
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith   PetscFunctionBegin;
864171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
86547c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
86647c6ae99SBarry Smith   PetscFunctionReturn(0);
86747c6ae99SBarry Smith }
86847c6ae99SBarry Smith 
86947c6ae99SBarry Smith /*@
8708472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
87147c6ae99SBarry Smith 
87247c6ae99SBarry Smith     Not Collective
87347c6ae99SBarry Smith 
87447c6ae99SBarry Smith     Input Parameter:
87547c6ae99SBarry Smith .   dm - the DM object
87647c6ae99SBarry Smith 
87747c6ae99SBarry Smith     Output Parameter:
87847c6ae99SBarry Smith .   vec - the local vector
87947c6ae99SBarry Smith 
880073dac72SJed Brown     Level: beginner
88147c6ae99SBarry Smith 
882e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88347c6ae99SBarry Smith 
88447c6ae99SBarry Smith @*/
8857087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
88647c6ae99SBarry Smith {
88747c6ae99SBarry Smith   PetscErrorCode ierr;
88847c6ae99SBarry Smith 
88947c6ae99SBarry Smith   PetscFunctionBegin;
890171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
89147c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
89247c6ae99SBarry Smith   PetscFunctionReturn(0);
89347c6ae99SBarry Smith }
89447c6ae99SBarry Smith 
8951411c6eeSJed Brown /*@
8961411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8971411c6eeSJed Brown 
8981411c6eeSJed Brown    Collective on DM
8991411c6eeSJed Brown 
9001411c6eeSJed Brown    Input Parameter:
9011411c6eeSJed Brown .  dm - the DM that provides the mapping
9021411c6eeSJed Brown 
9031411c6eeSJed Brown    Output Parameter:
9041411c6eeSJed Brown .  ltog - the mapping
9051411c6eeSJed Brown 
9061411c6eeSJed Brown    Level: intermediate
9071411c6eeSJed Brown 
9081411c6eeSJed Brown    Notes:
9091411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9101411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9111411c6eeSJed Brown 
912fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9131411c6eeSJed Brown @*/
9147087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9151411c6eeSJed Brown {
9160be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9171411c6eeSJed Brown   PetscErrorCode ierr;
9181411c6eeSJed Brown 
9191411c6eeSJed Brown   PetscFunctionBegin;
9201411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9211411c6eeSJed Brown   PetscValidPointer(ltog,2);
9221411c6eeSJed Brown   if (!dm->ltogmap) {
92337d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
92437d0c07bSMatthew G Knepley 
92537d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
92637d0c07bSMatthew G Knepley     if (section) {
927a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
92837d0c07bSMatthew G Knepley       PetscInt       *ltog;
929ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
93037d0c07bSMatthew G Knepley 
93137d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
93237d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
933ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
934ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
93537d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
936a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
93737d0c07bSMatthew G Knepley 
93837d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
93937d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
940a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
941a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
94237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
9431a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
9441a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
9451a7dc684SMatthew G. Knepley         if (dof) {
946b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
947b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
9481a7dc684SMatthew G. Knepley         }
94937d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
950a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
951a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
95237d0c07bSMatthew G Knepley         }
95337d0c07bSMatthew G Knepley       }
954bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
9550be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
9560be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
9570be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
9580be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
9597591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
9607591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
961ccf3bd66SMatthew G. Knepley       if (bs > 1) {
962ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
963ccf3bd66SMatthew G. Knepley         n /= bs;
964ccf3bd66SMatthew G. Knepley       }
965ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9663bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
96737d0c07bSMatthew G Knepley     } else {
968184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
969184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9701411c6eeSJed Brown     }
97137d0c07bSMatthew G Knepley   }
9721411c6eeSJed Brown   *ltog = dm->ltogmap;
9731411c6eeSJed Brown   PetscFunctionReturn(0);
9741411c6eeSJed Brown }
9751411c6eeSJed Brown 
9761411c6eeSJed Brown /*@
9771411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9781411c6eeSJed Brown 
9791411c6eeSJed Brown    Not Collective
9801411c6eeSJed Brown 
9811411c6eeSJed Brown    Input Parameter:
9821411c6eeSJed Brown .  dm - the DM with block structure
9831411c6eeSJed Brown 
9841411c6eeSJed Brown    Output Parameter:
9851411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
9861411c6eeSJed Brown 
9871411c6eeSJed Brown    Level: intermediate
9881411c6eeSJed Brown 
989fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
9901411c6eeSJed Brown @*/
9917087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
9921411c6eeSJed Brown {
9931411c6eeSJed Brown   PetscFunctionBegin;
9941411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9951411c6eeSJed Brown   PetscValidPointer(bs,2);
9961411c6eeSJed 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");
9971411c6eeSJed Brown   *bs = dm->bs;
9981411c6eeSJed Brown   PetscFunctionReturn(0);
9991411c6eeSJed Brown }
10001411c6eeSJed Brown 
100147c6ae99SBarry Smith /*@
10028472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
100347c6ae99SBarry Smith 
100447c6ae99SBarry Smith     Collective on DM
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith     Input Parameter:
100747c6ae99SBarry Smith +   dm1 - the DM object
100847c6ae99SBarry Smith -   dm2 - the second, finer DM object
100947c6ae99SBarry Smith 
101047c6ae99SBarry Smith     Output Parameter:
101147c6ae99SBarry Smith +  mat - the interpolation
101247c6ae99SBarry Smith -  vec - the scaling (optional)
101347c6ae99SBarry Smith 
101447c6ae99SBarry Smith     Level: developer
101547c6ae99SBarry Smith 
101685afcc9aSBarry 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
101785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1018d52bd9f3SBarry Smith 
10191f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1020d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
102185afcc9aSBarry Smith 
102285afcc9aSBarry Smith 
10233ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
102447c6ae99SBarry Smith 
102547c6ae99SBarry Smith @*/
1026e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
102747c6ae99SBarry Smith {
102847c6ae99SBarry Smith   PetscErrorCode ierr;
102947c6ae99SBarry Smith 
103047c6ae99SBarry Smith   PetscFunctionBegin;
1031171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1032171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1033cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
103425296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1035cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
103647c6ae99SBarry Smith   PetscFunctionReturn(0);
103747c6ae99SBarry Smith }
103847c6ae99SBarry Smith 
10393ad4599aSBarry Smith /*@
10403ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
10413ad4599aSBarry Smith 
10423ad4599aSBarry Smith     Collective on DM
10433ad4599aSBarry Smith 
10443ad4599aSBarry Smith     Input Parameter:
10453ad4599aSBarry Smith +   dm1 - the DM object
10463ad4599aSBarry Smith -   dm2 - the second, finer DM object
10473ad4599aSBarry Smith 
10483ad4599aSBarry Smith     Output Parameter:
10493ad4599aSBarry Smith .  mat - the restriction
10503ad4599aSBarry Smith 
10513ad4599aSBarry Smith 
10523ad4599aSBarry Smith     Level: developer
10533ad4599aSBarry Smith 
10543ad4599aSBarry 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
10553ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
10563ad4599aSBarry Smith 
10573ad4599aSBarry Smith 
10583ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
10593ad4599aSBarry Smith 
10603ad4599aSBarry Smith @*/
10613ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
10623ad4599aSBarry Smith {
10633ad4599aSBarry Smith   PetscErrorCode ierr;
10643ad4599aSBarry Smith 
10653ad4599aSBarry Smith   PetscFunctionBegin;
10663ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
10673ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10683ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10697294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
10703ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
10713ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10723ad4599aSBarry Smith   PetscFunctionReturn(0);
10733ad4599aSBarry Smith }
10743ad4599aSBarry Smith 
107547c6ae99SBarry Smith /*@
10768472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
107747c6ae99SBarry Smith 
107847c6ae99SBarry Smith     Collective on DM
107947c6ae99SBarry Smith 
108047c6ae99SBarry Smith     Input Parameter:
108147c6ae99SBarry Smith +   dm1 - the DM object
108247c6ae99SBarry Smith -   dm2 - the second, finer DM object
108347c6ae99SBarry Smith 
108447c6ae99SBarry Smith     Output Parameter:
10856dbf9973SLawrence Mitchell .   mat - the injection
108647c6ae99SBarry Smith 
108747c6ae99SBarry Smith     Level: developer
108847c6ae99SBarry Smith 
108985afcc9aSBarry 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
109085afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
109185afcc9aSBarry Smith 
1092e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
109347c6ae99SBarry Smith 
109447c6ae99SBarry Smith @*/
10956dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
109647c6ae99SBarry Smith {
109747c6ae99SBarry Smith   PetscErrorCode ierr;
109847c6ae99SBarry Smith 
109947c6ae99SBarry Smith   PetscFunctionBegin;
1100171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1101171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11020c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
11036dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
110447c6ae99SBarry Smith   PetscFunctionReturn(0);
110547c6ae99SBarry Smith }
110647c6ae99SBarry Smith 
1107b412c318SBarry Smith /*@
1108bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1109bd041c0cSMatthew G. Knepley 
1110bd041c0cSMatthew G. Knepley   Collective on DM
1111bd041c0cSMatthew G. Knepley 
1112bd041c0cSMatthew G. Knepley   Input Parameter:
1113bd041c0cSMatthew G. Knepley + dm1 - the DM object
1114bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1115bd041c0cSMatthew G. Knepley 
1116bd041c0cSMatthew G. Knepley   Output Parameter:
1117bd041c0cSMatthew G. Knepley . mat - the interpolation
1118bd041c0cSMatthew G. Knepley 
1119bd041c0cSMatthew G. Knepley   Level: developer
1120bd041c0cSMatthew G. Knepley 
1121bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1122bd041c0cSMatthew G. Knepley @*/
1123bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1124bd041c0cSMatthew G. Knepley {
1125bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1126bd041c0cSMatthew G. Knepley 
1127bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1128bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1129bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1130bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1131bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1132bd041c0cSMatthew G. Knepley }
1133bd041c0cSMatthew G. Knepley 
1134bd041c0cSMatthew G. Knepley /*@
1135b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
113647c6ae99SBarry Smith 
113747c6ae99SBarry Smith     Collective on DM
113847c6ae99SBarry Smith 
113947c6ae99SBarry Smith     Input Parameter:
114047c6ae99SBarry Smith +   dm - the DM object
11415bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
114247c6ae99SBarry Smith 
114347c6ae99SBarry Smith     Output Parameter:
114447c6ae99SBarry Smith .   coloring - the coloring
114547c6ae99SBarry Smith 
114647c6ae99SBarry Smith     Level: developer
114747c6ae99SBarry Smith 
1148b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
114947c6ae99SBarry Smith 
1150aab9d709SJed Brown @*/
1151b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
115247c6ae99SBarry Smith {
115347c6ae99SBarry Smith   PetscErrorCode ierr;
115447c6ae99SBarry Smith 
115547c6ae99SBarry Smith   PetscFunctionBegin;
1156171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1157ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1158b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
115947c6ae99SBarry Smith   PetscFunctionReturn(0);
116047c6ae99SBarry Smith }
116147c6ae99SBarry Smith 
1162b412c318SBarry Smith /*@
11638472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
116447c6ae99SBarry Smith 
116547c6ae99SBarry Smith     Collective on DM
116647c6ae99SBarry Smith 
116747c6ae99SBarry Smith     Input Parameter:
1168b412c318SBarry Smith .   dm - the DM object
116947c6ae99SBarry Smith 
117047c6ae99SBarry Smith     Output Parameter:
117147c6ae99SBarry Smith .   mat - the empty Jacobian
117247c6ae99SBarry Smith 
1173073dac72SJed Brown     Level: beginner
117447c6ae99SBarry Smith 
117594013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
117694013140SBarry Smith        do not need to do it yourself.
117794013140SBarry Smith 
117894013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
11797889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
118094013140SBarry Smith 
118194013140SBarry 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
118294013140SBarry Smith        internally by PETSc.
118394013140SBarry Smith 
118494013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1185aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
118694013140SBarry Smith 
1187b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
118847c6ae99SBarry Smith 
1189aab9d709SJed Brown @*/
1190b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
119147c6ae99SBarry Smith {
119247c6ae99SBarry Smith   PetscErrorCode ierr;
119347c6ae99SBarry Smith 
119447c6ae99SBarry Smith   PetscFunctionBegin;
1195171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1196607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1197c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1198c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1199b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
120047c6ae99SBarry Smith   PetscFunctionReturn(0);
120147c6ae99SBarry Smith }
120247c6ae99SBarry Smith 
1203732e2eb9SMatthew G Knepley /*@
1204950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1205732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1206732e2eb9SMatthew G Knepley 
12078472ad0fSDave May   Logically Collective on DM
1208732e2eb9SMatthew G Knepley 
1209732e2eb9SMatthew G Knepley   Input Parameter:
1210732e2eb9SMatthew G Knepley + dm - the DM
1211732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1212732e2eb9SMatthew G Knepley 
1213732e2eb9SMatthew G Knepley   Level: developer
1214b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1215732e2eb9SMatthew G Knepley @*/
1216732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1217732e2eb9SMatthew G Knepley {
1218732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1219732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1220732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1221732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1222732e2eb9SMatthew G Knepley }
1223732e2eb9SMatthew G Knepley 
1224b06ff27eSHong Zhang /*@
1225b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1226b06ff27eSHong Zhang     but the array for values will not be allocated.
1227b06ff27eSHong Zhang 
1228b06ff27eSHong Zhang   Logically Collective on DM
1229b06ff27eSHong Zhang 
1230b06ff27eSHong Zhang   Input Parameter:
1231b06ff27eSHong Zhang + dm - the DM
1232b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1233b06ff27eSHong Zhang 
1234b06ff27eSHong Zhang   Level: developer
1235b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1236b06ff27eSHong Zhang @*/
1237b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1238b06ff27eSHong Zhang {
1239b06ff27eSHong Zhang   PetscFunctionBegin;
1240b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1241b06ff27eSHong Zhang   dm->structure_only = only;
1242b06ff27eSHong Zhang   PetscFunctionReturn(0);
1243b06ff27eSHong Zhang }
1244b06ff27eSHong Zhang 
1245a89ea682SMatthew G Knepley /*@C
1246aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1247a89ea682SMatthew G Knepley 
1248a89ea682SMatthew G Knepley   Not Collective
1249a89ea682SMatthew G Knepley 
1250a89ea682SMatthew G Knepley   Input Parameters:
1251a89ea682SMatthew G Knepley + dm - the DM object
1252aa1993deSMatthew G Knepley . count - The minium size
125369291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1254a89ea682SMatthew G Knepley 
1255a89ea682SMatthew G Knepley   Output Parameter:
1256a89ea682SMatthew G Knepley . array - the work array
1257a89ea682SMatthew G Knepley 
1258a89ea682SMatthew G Knepley   Level: developer
1259a89ea682SMatthew G Knepley 
1260a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1261a89ea682SMatthew G Knepley @*/
126269291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1263a89ea682SMatthew G Knepley {
1264a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1265aa1993deSMatthew G Knepley   DMWorkLink     link;
126669291d52SBarry Smith   PetscMPIInt    dsize;
1267a89ea682SMatthew G Knepley 
1268a89ea682SMatthew G Knepley   PetscFunctionBegin;
1269a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1270aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1271aa1993deSMatthew G Knepley   if (dm->workin) {
1272aa1993deSMatthew G Knepley     link       = dm->workin;
1273aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1274aa1993deSMatthew G Knepley   } else {
1275b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1276a89ea682SMatthew G Knepley   }
127769291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
12785056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1279aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1280854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1281854ce69bSBarry Smith     link->bytes = dsize*count;
1282aa1993deSMatthew G Knepley   }
1283aa1993deSMatthew G Knepley   link->next   = dm->workout;
1284aa1993deSMatthew G Knepley   dm->workout  = link;
1285aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1286a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1287a89ea682SMatthew G Knepley }
1288a89ea682SMatthew G Knepley 
1289aa1993deSMatthew G Knepley /*@C
1290aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1291aa1993deSMatthew G Knepley 
1292aa1993deSMatthew G Knepley   Not Collective
1293aa1993deSMatthew G Knepley 
1294aa1993deSMatthew G Knepley   Input Parameters:
1295aa1993deSMatthew G Knepley + dm - the DM object
1296aa1993deSMatthew G Knepley . count - The minium size
129769291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1298aa1993deSMatthew G Knepley 
1299aa1993deSMatthew G Knepley   Output Parameter:
1300aa1993deSMatthew G Knepley . array - the work array
1301aa1993deSMatthew G Knepley 
1302aa1993deSMatthew G Knepley   Level: developer
1303aa1993deSMatthew G Knepley 
130469291d52SBarry Smith   Developer Notes: count and dtype are ignored, they are only needed for DMGetWorkArray()
1305aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1306aa1993deSMatthew G Knepley @*/
130769291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1308aa1993deSMatthew G Knepley {
1309aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1310aa1993deSMatthew G Knepley 
1311aa1993deSMatthew G Knepley   PetscFunctionBegin;
1312aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1313aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1314aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1315aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1316aa1993deSMatthew G Knepley       *p           = link->next;
1317aa1993deSMatthew G Knepley       link->next   = dm->workin;
1318aa1993deSMatthew G Knepley       dm->workin   = link;
13190298fd71SBarry Smith       *(void**)mem = NULL;
1320aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1321aa1993deSMatthew G Knepley     }
1322aa1993deSMatthew G Knepley   }
1323aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1324aa1993deSMatthew G Knepley }
1325e7c4fc90SDmitry Karpeev 
1326435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1327435a35e8SMatthew G Knepley {
1328435a35e8SMatthew G Knepley   PetscFunctionBegin;
1329435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
133082f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1331435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1332435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1333435a35e8SMatthew G Knepley }
1334435a35e8SMatthew G Knepley 
13354f3b5142SJed Brown /*@C
13364d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
13374d343eeaSMatthew G Knepley 
13384d343eeaSMatthew G Knepley   Not collective
13394d343eeaSMatthew G Knepley 
13404d343eeaSMatthew G Knepley   Input Parameter:
13414d343eeaSMatthew G Knepley . dm - the DM object
13424d343eeaSMatthew G Knepley 
13434d343eeaSMatthew G Knepley   Output Parameters:
13440298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
13450298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
13460298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
13474d343eeaSMatthew G Knepley 
13484d343eeaSMatthew G Knepley   Level: intermediate
13494d343eeaSMatthew G Knepley 
135021c9b008SJed Brown   Notes:
135121c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
135221c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
135321c9b008SJed Brown   PetscFree().
135421c9b008SJed Brown 
13554d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
13564d343eeaSMatthew G Knepley @*/
135737d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
13584d343eeaSMatthew G Knepley {
135937d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
13604d343eeaSMatthew G Knepley   PetscErrorCode ierr;
13614d343eeaSMatthew G Knepley 
13624d343eeaSMatthew G Knepley   PetscFunctionBegin;
13634d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
136469ca1f37SDmitry Karpeev   if (numFields) {
136569ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
136669ca1f37SDmitry Karpeev     *numFields = 0;
136769ca1f37SDmitry Karpeev   }
136837d0c07bSMatthew G Knepley   if (fieldNames) {
136937d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
13700298fd71SBarry Smith     *fieldNames = NULL;
137169ca1f37SDmitry Karpeev   }
137269ca1f37SDmitry Karpeev   if (fields) {
137369ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
13740298fd71SBarry Smith     *fields = NULL;
137569ca1f37SDmitry Karpeev   }
137637d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
137737d0c07bSMatthew G Knepley   if (section) {
137837d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
137937d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
138037d0c07bSMatthew G Knepley 
138137d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
138237d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1383dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
138437d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
138537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
138637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
138737d0c07bSMatthew G Knepley     }
138837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
138937d0c07bSMatthew G Knepley       PetscInt gdof;
139037d0c07bSMatthew G Knepley 
139137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
139237d0c07bSMatthew G Knepley       if (gdof > 0) {
139337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
139437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
139537d0c07bSMatthew G Knepley 
139637d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
139737d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
139837d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
139937d0c07bSMatthew G Knepley         }
140037d0c07bSMatthew G Knepley       }
140137d0c07bSMatthew G Knepley     }
140237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1403785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
140437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
140537d0c07bSMatthew G Knepley     }
140637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
140737d0c07bSMatthew G Knepley       PetscInt gdof, goff;
140837d0c07bSMatthew G Knepley 
140937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
141037d0c07bSMatthew G Knepley       if (gdof > 0) {
141137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
141237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
141337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
141437d0c07bSMatthew G Knepley 
141537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
141637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
141737d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
141837d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
141937d0c07bSMatthew G Knepley           }
142037d0c07bSMatthew G Knepley         }
142137d0c07bSMatthew G Knepley       }
142237d0c07bSMatthew G Knepley     }
14238865f1eaSKarl Rupp     if (numFields) *numFields = nF;
142437d0c07bSMatthew G Knepley     if (fieldNames) {
1425785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
142637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
142737d0c07bSMatthew G Knepley         const char *fieldName;
142837d0c07bSMatthew G Knepley 
142937d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
143037d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
143137d0c07bSMatthew G Knepley       }
143237d0c07bSMatthew G Knepley     }
143337d0c07bSMatthew G Knepley     if (fields) {
1434785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
143537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
143682f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
143737d0c07bSMatthew G Knepley       }
143837d0c07bSMatthew G Knepley     }
143937d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
14408865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
14418865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
144269ca1f37SDmitry Karpeev   }
14434d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
14444d343eeaSMatthew G Knepley }
14454d343eeaSMatthew G Knepley 
144616621825SDmitry Karpeev 
144716621825SDmitry Karpeev /*@C
144816621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
144916621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
145016621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1451e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1452e7c4fc90SDmitry Karpeev 
1453e7c4fc90SDmitry Karpeev   Not collective
1454e7c4fc90SDmitry Karpeev 
1455e7c4fc90SDmitry Karpeev   Input Parameter:
1456e7c4fc90SDmitry Karpeev . dm - the DM object
1457e7c4fc90SDmitry Karpeev 
1458e7c4fc90SDmitry Karpeev   Output Parameters:
14590298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
14600298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
14610298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
14620298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1463e7c4fc90SDmitry Karpeev 
1464e7c4fc90SDmitry Karpeev   Level: intermediate
1465e7c4fc90SDmitry Karpeev 
1466e7c4fc90SDmitry Karpeev   Notes:
1467e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1468e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1469e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1470e7c4fc90SDmitry Karpeev 
1471e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1472e7c4fc90SDmitry Karpeev @*/
147316621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1474e7c4fc90SDmitry Karpeev {
1475e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1476e7c4fc90SDmitry Karpeev 
1477e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1478e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14798865f1eaSKarl Rupp   if (len) {
14808865f1eaSKarl Rupp     PetscValidPointer(len,2);
14818865f1eaSKarl Rupp     *len = 0;
14828865f1eaSKarl Rupp   }
14838865f1eaSKarl Rupp   if (namelist) {
14848865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
14858865f1eaSKarl Rupp     *namelist = 0;
14868865f1eaSKarl Rupp   }
14878865f1eaSKarl Rupp   if (islist) {
14888865f1eaSKarl Rupp     PetscValidPointer(islist,4);
14898865f1eaSKarl Rupp     *islist = 0;
14908865f1eaSKarl Rupp   }
14918865f1eaSKarl Rupp   if (dmlist) {
14928865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
14938865f1eaSKarl Rupp     *dmlist = 0;
14948865f1eaSKarl Rupp   }
1495f3f0edfdSDmitry Karpeev   /*
1496f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1497f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1498f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1499f3f0edfdSDmitry Karpeev    */
1500ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
150116621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1502435a35e8SMatthew G Knepley     PetscSection section;
1503435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1504435a35e8SMatthew G Knepley 
1505435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1506435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1507435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1508f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
150903dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
151003dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
151103dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1512435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1513435a35e8SMatthew G Knepley         const char *fieldName;
1514435a35e8SMatthew G Knepley 
151503dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
151603dc3394SMatthew G. Knepley         if (namelist) {
1517435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1518435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1519435a35e8SMatthew G Knepley         }
152003dc3394SMatthew G. Knepley       }
1521435a35e8SMatthew G Knepley     } else {
152269ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1523e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
15240298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1525e7c4fc90SDmitry Karpeev     }
15268865f1eaSKarl Rupp   } else {
152716621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
152816621825SDmitry Karpeev   }
152916621825SDmitry Karpeev   PetscFunctionReturn(0);
153016621825SDmitry Karpeev }
153116621825SDmitry Karpeev 
1532564cec59SMatthew G. Knepley /*@
1533435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1534435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1535435a35e8SMatthew G Knepley 
1536435a35e8SMatthew G Knepley   Not collective
1537435a35e8SMatthew G Knepley 
1538435a35e8SMatthew G Knepley   Input Parameters:
15392adcc780SMatthew G. Knepley + dm        - The DM object
15402adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
15412adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1542435a35e8SMatthew G Knepley 
1543435a35e8SMatthew G Knepley   Output Parameters:
15442adcc780SMatthew G. Knepley + is - The global indices for the subproblem
15452adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1546435a35e8SMatthew G Knepley 
1547435a35e8SMatthew G Knepley   Level: intermediate
1548435a35e8SMatthew G Knepley 
1549435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1550435a35e8SMatthew G Knepley @*/
155137bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1552435a35e8SMatthew G Knepley {
1553435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1554435a35e8SMatthew G Knepley 
1555435a35e8SMatthew G Knepley   PetscFunctionBegin;
1556435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1557435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
15588865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
15598865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1560435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1561435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
156282f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1563435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1564435a35e8SMatthew G Knepley }
1565435a35e8SMatthew G Knepley 
15662adcc780SMatthew G. Knepley /*@C
15672adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
15682adcc780SMatthew G. Knepley 
15692adcc780SMatthew G. Knepley   Not collective
15702adcc780SMatthew G. Knepley 
15712adcc780SMatthew G. Knepley   Input Parameter:
15722adcc780SMatthew G. Knepley + dms - The DM objects
15732adcc780SMatthew G. Knepley - len - The number of DMs
15742adcc780SMatthew G. Knepley 
15752adcc780SMatthew G. Knepley   Output Parameters:
15762adcc780SMatthew G. Knepley + is - The global indices for the subproblem
15772adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
15782adcc780SMatthew G. Knepley 
15792adcc780SMatthew G. Knepley   Level: intermediate
15802adcc780SMatthew G. Knepley 
15812adcc780SMatthew G. Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
15822adcc780SMatthew G. Knepley @*/
15832adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
15842adcc780SMatthew G. Knepley {
15852adcc780SMatthew G. Knepley   PetscInt       i;
15862adcc780SMatthew G. Knepley   PetscErrorCode ierr;
15872adcc780SMatthew G. Knepley 
15882adcc780SMatthew G. Knepley   PetscFunctionBegin;
15892adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
15902adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
15912adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
15922adcc780SMatthew G. Knepley   if (superdm) PetscValidPointer(superdm,4);
15932adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
15942adcc780SMatthew G. Knepley   if (len) {
15952adcc780SMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {
15962adcc780SMatthew G. Knepley       ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
15972adcc780SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
15982adcc780SMatthew G. Knepley   }
15992adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
16002adcc780SMatthew G. Knepley }
16012adcc780SMatthew G. Knepley 
160216621825SDmitry Karpeev 
160316621825SDmitry Karpeev /*@C
16048d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
16058d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
16068d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
16078d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
16088d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
160916621825SDmitry Karpeev 
161016621825SDmitry Karpeev   Not collective
161116621825SDmitry Karpeev 
161216621825SDmitry Karpeev   Input Parameter:
161316621825SDmitry Karpeev . dm - the DM object
161416621825SDmitry Karpeev 
161516621825SDmitry Karpeev   Output Parameters:
16160298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
16170298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
16180298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
16190298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
16200298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
162116621825SDmitry Karpeev 
162216621825SDmitry Karpeev   Level: intermediate
162316621825SDmitry Karpeev 
162416621825SDmitry Karpeev   Notes:
162516621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
162616621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
162716621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
162816621825SDmitry Karpeev 
16298d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
163016621825SDmitry Karpeev @*/
16318d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
163216621825SDmitry Karpeev {
163316621825SDmitry Karpeev   PetscErrorCode      ierr;
1634be081cd6SPeter Brune   DMSubDomainHookLink link;
1635be081cd6SPeter Brune   PetscInt            i,l;
163616621825SDmitry Karpeev 
163716621825SDmitry Karpeev   PetscFunctionBegin;
163816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
163914a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
16400298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
16410298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
16420298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
16430298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1644f3f0edfdSDmitry Karpeev   /*
1645f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1646f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1647f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1648f3f0edfdSDmitry Karpeev    */
1649ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
165016621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1651be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
165214a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1653f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1654be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1655be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1656be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1657be081cd6SPeter Brune         }
1658648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1659e7c4fc90SDmitry Karpeev       }
166014a18fd3SPeter Brune     }
166114a18fd3SPeter Brune     if (len) *len = l;
166214a18fd3SPeter Brune   }
1663e30e807fSPeter Brune   PetscFunctionReturn(0);
1664e30e807fSPeter Brune }
1665e30e807fSPeter Brune 
1666e30e807fSPeter Brune 
1667e30e807fSPeter Brune /*@C
1668e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1669e30e807fSPeter Brune 
1670e30e807fSPeter Brune   Not collective
1671e30e807fSPeter Brune 
1672e30e807fSPeter Brune   Input Parameters:
1673e30e807fSPeter Brune + dm - the DM object
1674e30e807fSPeter Brune . n  - the number of subdomain scatters
1675e30e807fSPeter Brune - subdms - the local subdomains
1676e30e807fSPeter Brune 
1677e30e807fSPeter Brune   Output Parameters:
1678e30e807fSPeter Brune + n     - the number of scatters returned
1679e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1680e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1681e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1682e30e807fSPeter Brune 
1683e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1684e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1685e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1686e30e807fSPeter Brune   solution and residual data.
1687e30e807fSPeter Brune 
1688e30e807fSPeter Brune   Level: developer
1689e30e807fSPeter Brune 
1690e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1691e30e807fSPeter Brune @*/
1692e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1693e30e807fSPeter Brune {
1694e30e807fSPeter Brune   PetscErrorCode ierr;
1695e30e807fSPeter Brune 
1696e30e807fSPeter Brune   PetscFunctionBegin;
1697e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1698e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1699e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1700e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
17016668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1702e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1703e7c4fc90SDmitry Karpeev }
1704e7c4fc90SDmitry Karpeev 
170547c6ae99SBarry Smith /*@
170647c6ae99SBarry Smith   DMRefine - Refines a DM object
170747c6ae99SBarry Smith 
170847c6ae99SBarry Smith   Collective on DM
170947c6ae99SBarry Smith 
171047c6ae99SBarry Smith   Input Parameter:
171147c6ae99SBarry Smith + dm   - the DM object
171291d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
171347c6ae99SBarry Smith 
171447c6ae99SBarry Smith   Output Parameter:
17150298fd71SBarry Smith . dmf - the refined DM, or NULL
1716ae0a1c52SMatthew G Knepley 
17170298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
171847c6ae99SBarry Smith 
171947c6ae99SBarry Smith   Level: developer
172047c6ae99SBarry Smith 
1721e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
172247c6ae99SBarry Smith @*/
17237087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
172447c6ae99SBarry Smith {
172547c6ae99SBarry Smith   PetscErrorCode   ierr;
1726c833c3b5SJed Brown   DMRefineHookLink link;
172747c6ae99SBarry Smith 
172847c6ae99SBarry Smith   PetscFunctionBegin;
1729732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17301ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1731fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
173247c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
17334057135bSMatthew G Knepley   if (*dmf) {
173443842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
17358865f1eaSKarl Rupp 
17368cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
17378865f1eaSKarl Rupp 
1738644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
17390598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1740656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
17418865f1eaSKarl Rupp 
1742e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1743c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
17448865f1eaSKarl Rupp       if (link->refinehook) {
17458865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
17468865f1eaSKarl Rupp       }
1747c833c3b5SJed Brown     }
1748c833c3b5SJed Brown   }
17491ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1750c833c3b5SJed Brown   PetscFunctionReturn(0);
1751c833c3b5SJed Brown }
1752c833c3b5SJed Brown 
1753bb9467b5SJed Brown /*@C
1754c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1755c833c3b5SJed Brown 
1756c833c3b5SJed Brown    Logically Collective
1757c833c3b5SJed Brown 
1758c833c3b5SJed Brown    Input Arguments:
1759c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1760c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1761c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
17620298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1763c833c3b5SJed Brown 
1764c833c3b5SJed Brown    Calling sequence of refinehook:
1765c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1766c833c3b5SJed Brown 
1767c833c3b5SJed Brown +  coarse - coarse level DM
1768c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1769c833c3b5SJed Brown -  ctx - optional user-defined function context
1770c833c3b5SJed Brown 
1771c833c3b5SJed Brown    Calling sequence for interphook:
1772c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1773c833c3b5SJed Brown 
1774c833c3b5SJed Brown +  coarse - coarse level DM
1775c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1776c833c3b5SJed Brown .  fine - fine level DM to update
1777c833c3b5SJed Brown -  ctx - optional user-defined function context
1778c833c3b5SJed Brown 
1779c833c3b5SJed Brown    Level: advanced
1780c833c3b5SJed Brown 
1781c833c3b5SJed Brown    Notes:
1782c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1783c833c3b5SJed Brown 
1784c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1785c833c3b5SJed Brown 
1786bb9467b5SJed Brown    This function is currently not available from Fortran.
1787bb9467b5SJed Brown 
1788c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1789c833c3b5SJed Brown @*/
1790c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1791c833c3b5SJed Brown {
1792c833c3b5SJed Brown   PetscErrorCode   ierr;
1793c833c3b5SJed Brown   DMRefineHookLink link,*p;
1794c833c3b5SJed Brown 
1795c833c3b5SJed Brown   PetscFunctionBegin;
1796c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
17973d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
17983d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
17993d8e3701SJed Brown   }
180095dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1801c833c3b5SJed Brown   link->refinehook = refinehook;
1802c833c3b5SJed Brown   link->interphook = interphook;
1803c833c3b5SJed Brown   link->ctx        = ctx;
18040298fd71SBarry Smith   link->next       = NULL;
1805c833c3b5SJed Brown   *p               = link;
1806c833c3b5SJed Brown   PetscFunctionReturn(0);
1807c833c3b5SJed Brown }
1808c833c3b5SJed Brown 
18093d8e3701SJed Brown /*@C
18103d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
18113d8e3701SJed Brown 
18123d8e3701SJed Brown    Logically Collective
18133d8e3701SJed Brown 
18143d8e3701SJed Brown    Input Arguments:
18153d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
18163d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
18173d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18183d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
18193d8e3701SJed Brown 
18203d8e3701SJed Brown    Level: advanced
18213d8e3701SJed Brown 
18223d8e3701SJed Brown    Notes:
18233d8e3701SJed Brown    This function does nothing if the hook is not in the list.
18243d8e3701SJed Brown 
18253d8e3701SJed Brown    This function is currently not available from Fortran.
18263d8e3701SJed Brown 
18273d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
18283d8e3701SJed Brown @*/
18293d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
18303d8e3701SJed Brown {
18313d8e3701SJed Brown   PetscErrorCode   ierr;
18323d8e3701SJed Brown   DMRefineHookLink link,*p;
18333d8e3701SJed Brown 
18343d8e3701SJed Brown   PetscFunctionBegin;
18353d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
18363d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
18373d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
18383d8e3701SJed Brown       link = *p;
18393d8e3701SJed Brown       *p = link->next;
18403d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
18413d8e3701SJed Brown       break;
18423d8e3701SJed Brown     }
18433d8e3701SJed Brown   }
18443d8e3701SJed Brown   PetscFunctionReturn(0);
18453d8e3701SJed Brown }
18463d8e3701SJed Brown 
1847c833c3b5SJed Brown /*@
1848c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1849c833c3b5SJed Brown 
1850c833c3b5SJed Brown    Collective if any hooks are
1851c833c3b5SJed Brown 
1852c833c3b5SJed Brown    Input Arguments:
1853c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1854e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1855c833c3b5SJed Brown -  fine - finer DM to update
1856c833c3b5SJed Brown 
1857c833c3b5SJed Brown    Level: developer
1858c833c3b5SJed Brown 
1859c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1860c833c3b5SJed Brown @*/
1861c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1862c833c3b5SJed Brown {
1863c833c3b5SJed Brown   PetscErrorCode   ierr;
1864c833c3b5SJed Brown   DMRefineHookLink link;
1865c833c3b5SJed Brown 
1866c833c3b5SJed Brown   PetscFunctionBegin;
1867c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
18688865f1eaSKarl Rupp     if (link->interphook) {
18698865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
18708865f1eaSKarl Rupp     }
18714057135bSMatthew G Knepley   }
187247c6ae99SBarry Smith   PetscFunctionReturn(0);
187347c6ae99SBarry Smith }
187447c6ae99SBarry Smith 
1875eb3f98d2SBarry Smith /*@
1876eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1877eb3f98d2SBarry Smith 
1878eb3f98d2SBarry Smith     Not Collective
1879eb3f98d2SBarry Smith 
1880eb3f98d2SBarry Smith     Input Parameter:
1881eb3f98d2SBarry Smith .   dm - the DM object
1882eb3f98d2SBarry Smith 
1883eb3f98d2SBarry Smith     Output Parameter:
1884eb3f98d2SBarry Smith .   level - number of refinements
1885eb3f98d2SBarry Smith 
1886eb3f98d2SBarry Smith     Level: developer
1887eb3f98d2SBarry Smith 
18886a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1889eb3f98d2SBarry Smith 
1890eb3f98d2SBarry Smith @*/
1891eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1892eb3f98d2SBarry Smith {
1893eb3f98d2SBarry Smith   PetscFunctionBegin;
1894eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1895eb3f98d2SBarry Smith   *level = dm->levelup;
1896eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1897eb3f98d2SBarry Smith }
1898eb3f98d2SBarry Smith 
1899fef3a512SBarry Smith /*@
1900fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
1901fef3a512SBarry Smith 
1902fef3a512SBarry Smith     Not Collective
1903fef3a512SBarry Smith 
1904fef3a512SBarry Smith     Input Parameter:
1905fef3a512SBarry Smith +   dm - the DM object
1906fef3a512SBarry Smith -   level - number of refinements
1907fef3a512SBarry Smith 
1908fef3a512SBarry Smith     Level: advanced
1909fef3a512SBarry Smith 
1910fef3a512SBarry Smith     Notes: This value is used by PCMG to determine how many multigrid levels to use
1911fef3a512SBarry Smith 
1912fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1913fef3a512SBarry Smith 
1914fef3a512SBarry Smith @*/
1915fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
1916fef3a512SBarry Smith {
1917fef3a512SBarry Smith   PetscFunctionBegin;
1918fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1919fef3a512SBarry Smith   dm->levelup = level;
1920fef3a512SBarry Smith   PetscFunctionReturn(0);
1921fef3a512SBarry Smith }
1922fef3a512SBarry Smith 
1923bb9467b5SJed Brown /*@C
1924baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1925baf369e7SPeter Brune 
1926baf369e7SPeter Brune    Logically Collective
1927baf369e7SPeter Brune 
1928baf369e7SPeter Brune    Input Arguments:
1929baf369e7SPeter Brune +  dm - the DM
1930baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1931baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
19320298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1933baf369e7SPeter Brune 
1934baf369e7SPeter Brune    Calling sequence for beginhook:
1935baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1936baf369e7SPeter Brune 
1937baf369e7SPeter Brune +  dm - global DM
1938baf369e7SPeter Brune .  g - global vector
1939baf369e7SPeter Brune .  mode - mode
1940baf369e7SPeter Brune .  l - local vector
1941baf369e7SPeter Brune -  ctx - optional user-defined function context
1942baf369e7SPeter Brune 
1943baf369e7SPeter Brune 
1944baf369e7SPeter Brune    Calling sequence for endhook:
1945ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1946baf369e7SPeter Brune 
1947baf369e7SPeter Brune +  global - global DM
1948baf369e7SPeter Brune -  ctx - optional user-defined function context
1949baf369e7SPeter Brune 
1950baf369e7SPeter Brune    Level: advanced
1951baf369e7SPeter Brune 
1952baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1953baf369e7SPeter Brune @*/
1954baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1955baf369e7SPeter Brune {
1956baf369e7SPeter Brune   PetscErrorCode          ierr;
1957baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1958baf369e7SPeter Brune 
1959baf369e7SPeter Brune   PetscFunctionBegin;
1960baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1961baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
196295dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
1963baf369e7SPeter Brune   link->beginhook = beginhook;
1964baf369e7SPeter Brune   link->endhook   = endhook;
1965baf369e7SPeter Brune   link->ctx       = ctx;
19660298fd71SBarry Smith   link->next      = NULL;
1967baf369e7SPeter Brune   *p              = link;
1968baf369e7SPeter Brune   PetscFunctionReturn(0);
1969baf369e7SPeter Brune }
1970baf369e7SPeter Brune 
19714c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
19724c274da1SToby Isaac {
19734c274da1SToby Isaac   Mat cMat;
19744c274da1SToby Isaac   Vec cVec;
19754c274da1SToby Isaac   PetscSection section, cSec;
19764c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19774c274da1SToby Isaac   PetscErrorCode ierr;
19784c274da1SToby Isaac 
19794c274da1SToby Isaac   PetscFunctionBegin;
19804c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19814c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19824c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
19835db9a05bSToby Isaac     PetscInt nRows;
19845db9a05bSToby Isaac 
19855db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
19865db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
19874c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19887711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19894c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
19904c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19914c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19924c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19934c274da1SToby Isaac       if (dof) {
19944c274da1SToby Isaac         PetscScalar *vals;
19954c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
19964c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
19974c274da1SToby Isaac       }
19984c274da1SToby Isaac     }
19994c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
20004c274da1SToby Isaac   }
20014c274da1SToby Isaac   PetscFunctionReturn(0);
20024c274da1SToby Isaac }
20034c274da1SToby Isaac 
200447c6ae99SBarry Smith /*@
200547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
200647c6ae99SBarry Smith 
200747c6ae99SBarry Smith     Neighbor-wise Collective on DM
200847c6ae99SBarry Smith 
200947c6ae99SBarry Smith     Input Parameters:
201047c6ae99SBarry Smith +   dm - the DM object
201147c6ae99SBarry Smith .   g - the global vector
201247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
201347c6ae99SBarry Smith -   l - the local vector
201447c6ae99SBarry Smith 
201547c6ae99SBarry Smith 
201647c6ae99SBarry Smith     Level: beginner
201747c6ae99SBarry Smith 
2018e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
201947c6ae99SBarry Smith 
202047c6ae99SBarry Smith @*/
20217087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
202247c6ae99SBarry Smith {
20237128ae9fSMatthew G Knepley   PetscSF                 sf;
202447c6ae99SBarry Smith   PetscErrorCode          ierr;
2025baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
202647c6ae99SBarry Smith 
202747c6ae99SBarry Smith   PetscFunctionBegin;
2028171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2029baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
20308865f1eaSKarl Rupp     if (link->beginhook) {
20318865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
20328865f1eaSKarl Rupp     }
2033baf369e7SPeter Brune   }
20347128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
20357128ae9fSMatthew G Knepley   if (sf) {
2036ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2037ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
20387128ae9fSMatthew G Knepley 
203982f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20407128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2041ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20427128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20437128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2044ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20457128ae9fSMatthew G Knepley   } else {
2046843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20477128ae9fSMatthew G Knepley   }
204847c6ae99SBarry Smith   PetscFunctionReturn(0);
204947c6ae99SBarry Smith }
205047c6ae99SBarry Smith 
205147c6ae99SBarry Smith /*@
205247c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
205347c6ae99SBarry Smith 
205447c6ae99SBarry Smith     Neighbor-wise Collective on DM
205547c6ae99SBarry Smith 
205647c6ae99SBarry Smith     Input Parameters:
205747c6ae99SBarry Smith +   dm - the DM object
205847c6ae99SBarry Smith .   g - the global vector
205947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
206047c6ae99SBarry Smith -   l - the local vector
206147c6ae99SBarry Smith 
206247c6ae99SBarry Smith 
206347c6ae99SBarry Smith     Level: beginner
206447c6ae99SBarry Smith 
2065e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
206647c6ae99SBarry Smith 
206747c6ae99SBarry Smith @*/
20687087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
206947c6ae99SBarry Smith {
20707128ae9fSMatthew G Knepley   PetscSF                 sf;
207147c6ae99SBarry Smith   PetscErrorCode          ierr;
2072ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2073ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2074baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
207547c6ae99SBarry Smith 
207647c6ae99SBarry Smith   PetscFunctionBegin;
2077171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20787128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
20797128ae9fSMatthew G Knepley   if (sf) {
208082f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20817128ae9fSMatthew G Knepley 
20827128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2083ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20847128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20857128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2086ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20877128ae9fSMatthew G Knepley   } else {
2088843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20897128ae9fSMatthew G Knepley   }
20904c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2091baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2092baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2093baf369e7SPeter Brune   }
209447c6ae99SBarry Smith   PetscFunctionReturn(0);
209547c6ae99SBarry Smith }
209647c6ae99SBarry Smith 
2097d4d07f1eSToby Isaac /*@C
2098d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2099d4d07f1eSToby Isaac 
2100d4d07f1eSToby Isaac    Logically Collective
2101d4d07f1eSToby Isaac 
2102d4d07f1eSToby Isaac    Input Arguments:
2103d4d07f1eSToby Isaac +  dm - the DM
2104d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2105d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2106d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2107d4d07f1eSToby Isaac 
2108d4d07f1eSToby Isaac    Calling sequence for beginhook:
2109d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2110d4d07f1eSToby Isaac 
2111d4d07f1eSToby Isaac +  dm - global DM
2112d4d07f1eSToby Isaac .  l - local vector
2113d4d07f1eSToby Isaac .  mode - mode
2114d4d07f1eSToby Isaac .  g - global vector
2115d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2116d4d07f1eSToby Isaac 
2117d4d07f1eSToby Isaac 
2118d4d07f1eSToby Isaac    Calling sequence for endhook:
2119d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2120d4d07f1eSToby Isaac 
2121d4d07f1eSToby Isaac +  global - global DM
2122d4d07f1eSToby Isaac .  l - local vector
2123d4d07f1eSToby Isaac .  mode - mode
2124d4d07f1eSToby Isaac .  g - global vector
2125d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2126d4d07f1eSToby Isaac 
2127d4d07f1eSToby Isaac    Level: advanced
2128d4d07f1eSToby Isaac 
2129d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2130d4d07f1eSToby Isaac @*/
2131d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2132d4d07f1eSToby Isaac {
2133d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2134d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2135d4d07f1eSToby Isaac 
2136d4d07f1eSToby Isaac   PetscFunctionBegin;
2137d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2138d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
213995dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2140d4d07f1eSToby Isaac   link->beginhook = beginhook;
2141d4d07f1eSToby Isaac   link->endhook   = endhook;
2142d4d07f1eSToby Isaac   link->ctx       = ctx;
2143d4d07f1eSToby Isaac   link->next      = NULL;
2144d4d07f1eSToby Isaac   *p              = link;
2145d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2146d4d07f1eSToby Isaac }
2147d4d07f1eSToby Isaac 
21484c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
21494c274da1SToby Isaac {
21504c274da1SToby Isaac   Mat cMat;
21514c274da1SToby Isaac   Vec cVec;
21524c274da1SToby Isaac   PetscSection section, cSec;
21534c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
21544c274da1SToby Isaac   PetscErrorCode ierr;
21554c274da1SToby Isaac 
21564c274da1SToby Isaac   PetscFunctionBegin;
21574c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21584c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
21594c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
21605db9a05bSToby Isaac     PetscInt nRows;
21615db9a05bSToby Isaac 
21625db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21635db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
21644c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
21657711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21664c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21674c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21684c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21694c274da1SToby Isaac       if (dof) {
21704c274da1SToby Isaac         PetscInt d;
21714c274da1SToby Isaac         PetscScalar *vals;
21724c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
21734c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
21744c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
21754c274da1SToby Isaac          * we just extracted */
21764c274da1SToby Isaac         for (d = 0; d < dof; d++) {
21774c274da1SToby Isaac           vals[d] = 0.;
21784c274da1SToby Isaac         }
21794c274da1SToby Isaac       }
21804c274da1SToby Isaac     }
21814c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
21824c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21834c274da1SToby Isaac   }
21844c274da1SToby Isaac   PetscFunctionReturn(0);
21854c274da1SToby Isaac }
21864c274da1SToby Isaac 
218747c6ae99SBarry Smith /*@
21889a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
21899a42bb27SBarry Smith 
21909a42bb27SBarry Smith     Neighbor-wise Collective on DM
21919a42bb27SBarry Smith 
21929a42bb27SBarry Smith     Input Parameters:
21939a42bb27SBarry Smith +   dm - the DM object
2194f6813fd5SJed Brown .   l - the local vector
21951eb28f2eSBarry 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.
21961eb28f2eSBarry Smith -   g - the global vector
21979a42bb27SBarry Smith 
2198d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
219984330215SMatthew 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.
22009a42bb27SBarry Smith 
22019a42bb27SBarry Smith     Level: beginner
22029a42bb27SBarry Smith 
2203e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
22049a42bb27SBarry Smith 
22059a42bb27SBarry Smith @*/
22067087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
22079a42bb27SBarry Smith {
22087128ae9fSMatthew G Knepley   PetscSF                 sf;
220984330215SMatthew G. Knepley   PetscSection            s, gs;
2210d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2211ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2212ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
221384330215SMatthew G. Knepley   PetscBool               isInsert;
221484330215SMatthew G. Knepley   PetscErrorCode          ierr;
22159a42bb27SBarry Smith 
22169a42bb27SBarry Smith   PetscFunctionBegin;
2217171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2218d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2219d4d07f1eSToby Isaac     if (link->beginhook) {
2220d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2221d4d07f1eSToby Isaac     }
2222d4d07f1eSToby Isaac   }
22234c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
22247128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
222584330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
22267128ae9fSMatthew G Knepley   switch (mode) {
22277128ae9fSMatthew G Knepley   case INSERT_VALUES:
22287128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2229304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
223084330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
22317128ae9fSMatthew G Knepley   case ADD_VALUES:
22327128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2233304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
223484330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
22357128ae9fSMatthew G Knepley   default:
223682f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22377128ae9fSMatthew G Knepley   }
223884330215SMatthew G. Knepley   if (sf && !isInsert) {
2239ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
22407128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2241a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2242ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
224384330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
224484330215SMatthew G. Knepley   } else if (s && isInsert) {
224584330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
224684330215SMatthew G. Knepley 
224784330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
224884330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
224984330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2250ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
225184330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
225284330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2253b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
225484330215SMatthew G. Knepley 
225584330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
225603442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
225784330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2258b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
225984330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
226084330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2261b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
226203442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2263b3b16f48SMatthew 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);
2264b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2265b3b16f48SMatthew G. Knepley       if (!gcdof) {
226684330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2267b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2268b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
226984330215SMatthew G. Knepley         const PetscInt *cdofs;
227084330215SMatthew G. Knepley         PetscInt        cind = 0;
227184330215SMatthew G. Knepley 
227284330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2273b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
227484330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2275b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
227684330215SMatthew G. Knepley         }
2277b3b16f48SMatthew 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);
227884330215SMatthew G. Knepley     }
2279ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22807128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
22817128ae9fSMatthew G Knepley   } else {
2282843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22837128ae9fSMatthew G Knepley   }
22849a42bb27SBarry Smith   PetscFunctionReturn(0);
22859a42bb27SBarry Smith }
22869a42bb27SBarry Smith 
22879a42bb27SBarry Smith /*@
22889a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
228947c6ae99SBarry Smith 
229047c6ae99SBarry Smith     Neighbor-wise Collective on DM
229147c6ae99SBarry Smith 
229247c6ae99SBarry Smith     Input Parameters:
229347c6ae99SBarry Smith +   dm - the DM object
2294f6813fd5SJed Brown .   l - the local vector
229547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2296f6813fd5SJed Brown -   g - the global vector
229747c6ae99SBarry Smith 
229847c6ae99SBarry Smith 
229947c6ae99SBarry Smith     Level: beginner
230047c6ae99SBarry Smith 
2301e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
230247c6ae99SBarry Smith 
230347c6ae99SBarry Smith @*/
23047087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
230547c6ae99SBarry Smith {
23067128ae9fSMatthew G Knepley   PetscSF                 sf;
230784330215SMatthew G. Knepley   PetscSection            s;
2308d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
230984330215SMatthew G. Knepley   PetscBool               isInsert;
231084330215SMatthew G. Knepley   PetscErrorCode          ierr;
231147c6ae99SBarry Smith 
231247c6ae99SBarry Smith   PetscFunctionBegin;
2313171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23147128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
231584330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
23167128ae9fSMatthew G Knepley   switch (mode) {
23177128ae9fSMatthew G Knepley   case INSERT_VALUES:
23187128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
231984330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
23207128ae9fSMatthew G Knepley   case ADD_VALUES:
23217128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
232284330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
23237128ae9fSMatthew G Knepley   default:
232482f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23257128ae9fSMatthew G Knepley   }
232684330215SMatthew G. Knepley   if (sf && !isInsert) {
2327ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2328ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
232984330215SMatthew G. Knepley 
2330ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
23317128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2332a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2333ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
23347128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
233584330215SMatthew G. Knepley   } else if (s && isInsert) {
23367128ae9fSMatthew G Knepley   } else {
2337843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
23387128ae9fSMatthew G Knepley   }
2339d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2340d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2341d4d07f1eSToby Isaac   }
234247c6ae99SBarry Smith   PetscFunctionReturn(0);
234347c6ae99SBarry Smith }
234447c6ae99SBarry Smith 
2345f089877aSRichard Tran Mills /*@
2346bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2347bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2348d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2349f089877aSRichard Tran Mills 
2350bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2351f089877aSRichard Tran Mills 
2352f089877aSRichard Tran Mills    Input Parameters:
2353f089877aSRichard Tran Mills +  dm - the DM object
2354bc0a1609SRichard Tran Mills .  g - the original local vector
2355bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2356f089877aSRichard Tran Mills 
2357bc0a1609SRichard Tran Mills    Output Parameter:
2358bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2359f089877aSRichard Tran Mills 
2360f089877aSRichard Tran Mills    Level: intermediate
2361f089877aSRichard Tran Mills 
2362bc0a1609SRichard Tran Mills    Notes:
2363bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2364bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2365bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2366bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2367bc0a1609SRichard Tran Mills 
2368bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2369bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2370f089877aSRichard Tran Mills 
2371f089877aSRichard Tran Mills @*/
2372f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2373f089877aSRichard Tran Mills {
2374f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2375f089877aSRichard Tran Mills 
2376f089877aSRichard Tran Mills   PetscFunctionBegin;
2377f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2378bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2379f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2380f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2381f089877aSRichard Tran Mills }
2382f089877aSRichard Tran Mills 
2383f089877aSRichard Tran Mills /*@
2384bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2385bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2386d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2387f089877aSRichard Tran Mills 
2388bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2389f089877aSRichard Tran Mills 
2390f089877aSRichard Tran Mills    Input Parameters:
2391bc0a1609SRichard Tran Mills +  da - the DM object
2392bc0a1609SRichard Tran Mills .  g - the original local vector
2393bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2394f089877aSRichard Tran Mills 
2395bc0a1609SRichard Tran Mills    Output Parameter:
2396bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2397f089877aSRichard Tran Mills 
2398f089877aSRichard Tran Mills    Level: intermediate
2399f089877aSRichard Tran Mills 
2400bc0a1609SRichard Tran Mills    Notes:
2401bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2402bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2403bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2404bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2405bc0a1609SRichard Tran Mills 
2406bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2407bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2408f089877aSRichard Tran Mills 
2409f089877aSRichard Tran Mills @*/
2410f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2411f089877aSRichard Tran Mills {
2412f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2413f089877aSRichard Tran Mills 
2414f089877aSRichard Tran Mills   PetscFunctionBegin;
2415f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2416bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2417f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2418f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2419f089877aSRichard Tran Mills }
2420f089877aSRichard Tran Mills 
2421f089877aSRichard Tran Mills 
242247c6ae99SBarry Smith /*@
242347c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
242447c6ae99SBarry Smith 
242547c6ae99SBarry Smith     Collective on DM
242647c6ae99SBarry Smith 
242747c6ae99SBarry Smith     Input Parameter:
242847c6ae99SBarry Smith +   dm - the DM object
242991d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
243047c6ae99SBarry Smith 
243147c6ae99SBarry Smith     Output Parameter:
243247c6ae99SBarry Smith .   dmc - the coarsened DM
243347c6ae99SBarry Smith 
243447c6ae99SBarry Smith     Level: developer
243547c6ae99SBarry Smith 
2436e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
243747c6ae99SBarry Smith 
243847c6ae99SBarry Smith @*/
24397087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
244047c6ae99SBarry Smith {
244147c6ae99SBarry Smith   PetscErrorCode    ierr;
2442b17ce1afSJed Brown   DMCoarsenHookLink link;
244347c6ae99SBarry Smith 
244447c6ae99SBarry Smith   PetscFunctionBegin;
2445171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2446fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
244747a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
244847c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
24498892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2450a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
245143842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
24528cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2453644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
24540598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2455656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2456e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2457b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2458b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2459b17ce1afSJed Brown   }
246047a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2461b17ce1afSJed Brown   PetscFunctionReturn(0);
2462b17ce1afSJed Brown }
2463b17ce1afSJed Brown 
2464bb9467b5SJed Brown /*@C
2465b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2466b17ce1afSJed Brown 
2467b17ce1afSJed Brown    Logically Collective
2468b17ce1afSJed Brown 
2469b17ce1afSJed Brown    Input Arguments:
2470b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2471b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2472b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
24730298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2474b17ce1afSJed Brown 
2475b17ce1afSJed Brown    Calling sequence of coarsenhook:
2476b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2477b17ce1afSJed Brown 
2478b17ce1afSJed Brown +  fine - fine level DM
2479b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2480b17ce1afSJed Brown -  ctx - optional user-defined function context
2481b17ce1afSJed Brown 
2482b17ce1afSJed Brown    Calling sequence for restricthook:
2483c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2484b17ce1afSJed Brown 
2485b17ce1afSJed Brown +  fine - fine level DM
2486b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2487c833c3b5SJed Brown .  rscale - scaling vector for restriction
2488c833c3b5SJed Brown .  inject - matrix restricting by injection
2489b17ce1afSJed Brown .  coarse - coarse level DM to update
2490b17ce1afSJed Brown -  ctx - optional user-defined function context
2491b17ce1afSJed Brown 
2492b17ce1afSJed Brown    Level: advanced
2493b17ce1afSJed Brown 
2494b17ce1afSJed Brown    Notes:
2495b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2496b17ce1afSJed Brown 
2497b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2498b17ce1afSJed Brown 
2499b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2500b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2501b17ce1afSJed Brown 
2502bb9467b5SJed Brown    This function is currently not available from Fortran.
2503bb9467b5SJed Brown 
2504dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2505b17ce1afSJed Brown @*/
2506b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2507b17ce1afSJed Brown {
2508b17ce1afSJed Brown   PetscErrorCode    ierr;
2509b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2510b17ce1afSJed Brown 
2511b17ce1afSJed Brown   PetscFunctionBegin;
2512b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
25131e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
25141e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
25151e3d8eccSJed Brown   }
251695dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2517b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2518b17ce1afSJed Brown   link->restricthook = restricthook;
2519b17ce1afSJed Brown   link->ctx          = ctx;
25200298fd71SBarry Smith   link->next         = NULL;
2521b17ce1afSJed Brown   *p                 = link;
2522b17ce1afSJed Brown   PetscFunctionReturn(0);
2523b17ce1afSJed Brown }
2524b17ce1afSJed Brown 
2525dc822a44SJed Brown /*@C
2526dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2527dc822a44SJed Brown 
2528dc822a44SJed Brown    Logically Collective
2529dc822a44SJed Brown 
2530dc822a44SJed Brown    Input Arguments:
2531dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2532dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2533dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2534dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2535dc822a44SJed Brown 
2536dc822a44SJed Brown    Level: advanced
2537dc822a44SJed Brown 
2538dc822a44SJed Brown    Notes:
2539dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2540dc822a44SJed Brown 
2541dc822a44SJed Brown    This function is currently not available from Fortran.
2542dc822a44SJed Brown 
2543dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2544dc822a44SJed Brown @*/
2545dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2546dc822a44SJed Brown {
2547dc822a44SJed Brown   PetscErrorCode    ierr;
2548dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2549dc822a44SJed Brown 
2550dc822a44SJed Brown   PetscFunctionBegin;
2551dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2552dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2553dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2554dc822a44SJed Brown       link = *p;
2555dc822a44SJed Brown       *p = link->next;
2556dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2557dc822a44SJed Brown       break;
2558dc822a44SJed Brown     }
2559dc822a44SJed Brown   }
2560dc822a44SJed Brown   PetscFunctionReturn(0);
2561dc822a44SJed Brown }
2562dc822a44SJed Brown 
2563dc822a44SJed Brown 
2564b17ce1afSJed Brown /*@
2565b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2566b17ce1afSJed Brown 
2567b17ce1afSJed Brown    Collective if any hooks are
2568b17ce1afSJed Brown 
2569b17ce1afSJed Brown    Input Arguments:
2570b17ce1afSJed Brown +  fine - finer DM to use as a base
2571b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2572e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2573b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2574e91eccc2SStefano Zampini -  coarse - coarser DM to update
2575b17ce1afSJed Brown 
2576b17ce1afSJed Brown    Level: developer
2577b17ce1afSJed Brown 
2578b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2579b17ce1afSJed Brown @*/
2580b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2581b17ce1afSJed Brown {
2582b17ce1afSJed Brown   PetscErrorCode    ierr;
2583b17ce1afSJed Brown   DMCoarsenHookLink link;
2584b17ce1afSJed Brown 
2585b17ce1afSJed Brown   PetscFunctionBegin;
2586b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
25878865f1eaSKarl Rupp     if (link->restricthook) {
25888865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
25898865f1eaSKarl Rupp     }
2590b17ce1afSJed Brown   }
259147c6ae99SBarry Smith   PetscFunctionReturn(0);
259247c6ae99SBarry Smith }
259347c6ae99SBarry Smith 
2594bb9467b5SJed Brown /*@C
2595be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
25965dbd56e3SPeter Brune 
25975dbd56e3SPeter Brune    Logically Collective
25985dbd56e3SPeter Brune 
25995dbd56e3SPeter Brune    Input Arguments:
26005dbd56e3SPeter Brune +  global - global DM
2601ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
26025dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
26030298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
26045dbd56e3SPeter Brune 
2605ec4806b8SPeter Brune 
2606ec4806b8SPeter Brune    Calling sequence for ddhook:
2607ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2608ec4806b8SPeter Brune 
2609ec4806b8SPeter Brune +  global - global DM
2610ec4806b8SPeter Brune .  block  - block DM
2611ec4806b8SPeter Brune -  ctx - optional user-defined function context
2612ec4806b8SPeter Brune 
26135dbd56e3SPeter Brune    Calling sequence for restricthook:
2614ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
26155dbd56e3SPeter Brune 
26165dbd56e3SPeter Brune +  global - global DM
26175dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
26185dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2619ec4806b8SPeter Brune .  block  - block DM
26205dbd56e3SPeter Brune -  ctx - optional user-defined function context
26215dbd56e3SPeter Brune 
26225dbd56e3SPeter Brune    Level: advanced
26235dbd56e3SPeter Brune 
26245dbd56e3SPeter Brune    Notes:
2625ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
26265dbd56e3SPeter Brune 
26275dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
26285dbd56e3SPeter Brune 
26295dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2630ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
26315dbd56e3SPeter Brune 
2632bb9467b5SJed Brown    This function is currently not available from Fortran.
2633bb9467b5SJed Brown 
26345dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
26355dbd56e3SPeter Brune @*/
2636be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
26375dbd56e3SPeter Brune {
26385dbd56e3SPeter Brune   PetscErrorCode      ierr;
2639be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
26405dbd56e3SPeter Brune 
26415dbd56e3SPeter Brune   PetscFunctionBegin;
26425dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2643b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2644b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2645b3a6b972SJed Brown   }
264695dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
26475dbd56e3SPeter Brune   link->restricthook = restricthook;
2648be081cd6SPeter Brune   link->ddhook       = ddhook;
26495dbd56e3SPeter Brune   link->ctx          = ctx;
26500298fd71SBarry Smith   link->next         = NULL;
26515dbd56e3SPeter Brune   *p                 = link;
26525dbd56e3SPeter Brune   PetscFunctionReturn(0);
26535dbd56e3SPeter Brune }
26545dbd56e3SPeter Brune 
2655b3a6b972SJed Brown /*@C
2656b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2657b3a6b972SJed Brown 
2658b3a6b972SJed Brown    Logically Collective
2659b3a6b972SJed Brown 
2660b3a6b972SJed Brown    Input Arguments:
2661b3a6b972SJed Brown +  global - global DM
2662b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2663b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2664b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2665b3a6b972SJed Brown 
2666b3a6b972SJed Brown    Level: advanced
2667b3a6b972SJed Brown 
2668b3a6b972SJed Brown    Notes:
2669b3a6b972SJed Brown 
2670b3a6b972SJed Brown    This function is currently not available from Fortran.
2671b3a6b972SJed Brown 
2672b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2673b3a6b972SJed Brown @*/
2674b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2675b3a6b972SJed Brown {
2676b3a6b972SJed Brown   PetscErrorCode      ierr;
2677b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2678b3a6b972SJed Brown 
2679b3a6b972SJed Brown   PetscFunctionBegin;
2680b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2681b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2682b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2683b3a6b972SJed Brown       link = *p;
2684b3a6b972SJed Brown       *p = link->next;
2685b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2686b3a6b972SJed Brown       break;
2687b3a6b972SJed Brown     }
2688b3a6b972SJed Brown   }
2689b3a6b972SJed Brown   PetscFunctionReturn(0);
2690b3a6b972SJed Brown }
2691b3a6b972SJed Brown 
26925dbd56e3SPeter Brune /*@
2693be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
26945dbd56e3SPeter Brune 
26955dbd56e3SPeter Brune    Collective if any hooks are
26965dbd56e3SPeter Brune 
26975dbd56e3SPeter Brune    Input Arguments:
26985dbd56e3SPeter Brune +  fine - finer DM to use as a base
2699be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2700be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
27015dbd56e3SPeter Brune -  coarse - coarer DM to update
27025dbd56e3SPeter Brune 
27035dbd56e3SPeter Brune    Level: developer
27045dbd56e3SPeter Brune 
27055dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
27065dbd56e3SPeter Brune @*/
2707be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
27085dbd56e3SPeter Brune {
27095dbd56e3SPeter Brune   PetscErrorCode      ierr;
2710be081cd6SPeter Brune   DMSubDomainHookLink link;
27115dbd56e3SPeter Brune 
27125dbd56e3SPeter Brune   PetscFunctionBegin;
2713be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
27148865f1eaSKarl Rupp     if (link->restricthook) {
27158865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
27168865f1eaSKarl Rupp     }
27175dbd56e3SPeter Brune   }
27185dbd56e3SPeter Brune   PetscFunctionReturn(0);
27195dbd56e3SPeter Brune }
27205dbd56e3SPeter Brune 
27215fe1f584SPeter Brune /*@
27226a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
27235fe1f584SPeter Brune 
27245fe1f584SPeter Brune     Not Collective
27255fe1f584SPeter Brune 
27265fe1f584SPeter Brune     Input Parameter:
27275fe1f584SPeter Brune .   dm - the DM object
27285fe1f584SPeter Brune 
27295fe1f584SPeter Brune     Output Parameter:
27306a7d9d85SPeter Brune .   level - number of coarsenings
27315fe1f584SPeter Brune 
27325fe1f584SPeter Brune     Level: developer
27335fe1f584SPeter Brune 
27346a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
27355fe1f584SPeter Brune 
27365fe1f584SPeter Brune @*/
27375fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
27385fe1f584SPeter Brune {
27395fe1f584SPeter Brune   PetscFunctionBegin;
27405fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27415fe1f584SPeter Brune   *level = dm->leveldown;
27425fe1f584SPeter Brune   PetscFunctionReturn(0);
27435fe1f584SPeter Brune }
27445fe1f584SPeter Brune 
27455fe1f584SPeter Brune 
27465fe1f584SPeter Brune 
274747c6ae99SBarry Smith /*@C
274847c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
274947c6ae99SBarry Smith 
275047c6ae99SBarry Smith     Collective on DM
275147c6ae99SBarry Smith 
275247c6ae99SBarry Smith     Input Parameter:
275347c6ae99SBarry Smith +   dm - the DM object
275447c6ae99SBarry Smith -   nlevels - the number of levels of refinement
275547c6ae99SBarry Smith 
275647c6ae99SBarry Smith     Output Parameter:
275747c6ae99SBarry Smith .   dmf - the refined DM hierarchy
275847c6ae99SBarry Smith 
275947c6ae99SBarry Smith     Level: developer
276047c6ae99SBarry Smith 
2761e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
276247c6ae99SBarry Smith 
276347c6ae99SBarry Smith @*/
27647087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
276547c6ae99SBarry Smith {
276647c6ae99SBarry Smith   PetscErrorCode ierr;
276747c6ae99SBarry Smith 
276847c6ae99SBarry Smith   PetscFunctionBegin;
2769171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2770ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
277147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
277247c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
277347c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
277447c6ae99SBarry Smith   } else if (dm->ops->refine) {
277547c6ae99SBarry Smith     PetscInt i;
277647c6ae99SBarry Smith 
2777ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
277847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2779ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
278047c6ae99SBarry Smith     }
2781ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
278247c6ae99SBarry Smith   PetscFunctionReturn(0);
278347c6ae99SBarry Smith }
278447c6ae99SBarry Smith 
278547c6ae99SBarry Smith /*@C
278647c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
278747c6ae99SBarry Smith 
278847c6ae99SBarry Smith     Collective on DM
278947c6ae99SBarry Smith 
279047c6ae99SBarry Smith     Input Parameter:
279147c6ae99SBarry Smith +   dm - the DM object
279247c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
279347c6ae99SBarry Smith 
279447c6ae99SBarry Smith     Output Parameter:
279547c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
279647c6ae99SBarry Smith 
279747c6ae99SBarry Smith     Level: developer
279847c6ae99SBarry Smith 
2799e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
280047c6ae99SBarry Smith 
280147c6ae99SBarry Smith @*/
28027087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
280347c6ae99SBarry Smith {
280447c6ae99SBarry Smith   PetscErrorCode ierr;
280547c6ae99SBarry Smith 
280647c6ae99SBarry Smith   PetscFunctionBegin;
2807171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2808ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
280947c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
281047c6ae99SBarry Smith   PetscValidPointer(dmc,3);
281147c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
281247c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
281347c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
281447c6ae99SBarry Smith     PetscInt i;
281547c6ae99SBarry Smith 
2816ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
281747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2818ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
281947c6ae99SBarry Smith     }
2820ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
282147c6ae99SBarry Smith   PetscFunctionReturn(0);
282247c6ae99SBarry Smith }
282347c6ae99SBarry Smith 
282447c6ae99SBarry Smith /*@
2825e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
282647c6ae99SBarry Smith    grids associated with two DMs.
282747c6ae99SBarry Smith 
282847c6ae99SBarry Smith    Collective on DM
282947c6ae99SBarry Smith 
283047c6ae99SBarry Smith    Input Parameters:
283147c6ae99SBarry Smith +  dmc - the coarse grid DM
283247c6ae99SBarry Smith -  dmf - the fine grid DM
283347c6ae99SBarry Smith 
283447c6ae99SBarry Smith    Output Parameters:
283547c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
283647c6ae99SBarry Smith 
283747c6ae99SBarry Smith    Level: intermediate
283847c6ae99SBarry Smith 
283947c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
284047c6ae99SBarry Smith 
2841e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
284247c6ae99SBarry Smith @*/
2843e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
284447c6ae99SBarry Smith {
284547c6ae99SBarry Smith   PetscErrorCode ierr;
284647c6ae99SBarry Smith 
284747c6ae99SBarry Smith   PetscFunctionBegin;
2848171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2849171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
285047c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
285147c6ae99SBarry Smith   PetscFunctionReturn(0);
285247c6ae99SBarry Smith }
285347c6ae99SBarry Smith 
28541a266240SBarry Smith /*@C
28551a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
28561a266240SBarry Smith 
28571a266240SBarry Smith     Not Collective
28581a266240SBarry Smith 
28591a266240SBarry Smith     Input Parameters:
28601a266240SBarry Smith +   dm - the DM object
28611a266240SBarry Smith -   destroy - the destroy function
28621a266240SBarry Smith 
28631a266240SBarry Smith     Level: intermediate
28641a266240SBarry Smith 
2865e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
28661a266240SBarry Smith 
2867f07f9ceaSJed Brown @*/
28681a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
28691a266240SBarry Smith {
28701a266240SBarry Smith   PetscFunctionBegin;
2871171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28721a266240SBarry Smith   dm->ctxdestroy = destroy;
28731a266240SBarry Smith   PetscFunctionReturn(0);
28741a266240SBarry Smith }
28751a266240SBarry Smith 
2876b07ff414SBarry Smith /*@
28771b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
287847c6ae99SBarry Smith 
287947c6ae99SBarry Smith     Not Collective
288047c6ae99SBarry Smith 
288147c6ae99SBarry Smith     Input Parameters:
288247c6ae99SBarry Smith +   dm - the DM object
288347c6ae99SBarry Smith -   ctx - the user context
288447c6ae99SBarry Smith 
288547c6ae99SBarry Smith     Level: intermediate
288647c6ae99SBarry Smith 
2887e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
288847c6ae99SBarry Smith 
288947c6ae99SBarry Smith @*/
28901b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
289147c6ae99SBarry Smith {
289247c6ae99SBarry Smith   PetscFunctionBegin;
2893171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
289447c6ae99SBarry Smith   dm->ctx = ctx;
289547c6ae99SBarry Smith   PetscFunctionReturn(0);
289647c6ae99SBarry Smith }
289747c6ae99SBarry Smith 
289847c6ae99SBarry Smith /*@
28991b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
290047c6ae99SBarry Smith 
290147c6ae99SBarry Smith     Not Collective
290247c6ae99SBarry Smith 
290347c6ae99SBarry Smith     Input Parameter:
290447c6ae99SBarry Smith .   dm - the DM object
290547c6ae99SBarry Smith 
290647c6ae99SBarry Smith     Output Parameter:
290747c6ae99SBarry Smith .   ctx - the user context
290847c6ae99SBarry Smith 
290947c6ae99SBarry Smith     Level: intermediate
291047c6ae99SBarry Smith 
2911e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
291247c6ae99SBarry Smith 
291347c6ae99SBarry Smith @*/
29141b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
291547c6ae99SBarry Smith {
291647c6ae99SBarry Smith   PetscFunctionBegin;
2917171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29181b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
291947c6ae99SBarry Smith   PetscFunctionReturn(0);
292047c6ae99SBarry Smith }
292147c6ae99SBarry Smith 
292208da532bSDmitry Karpeev /*@C
2923df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
292408da532bSDmitry Karpeev 
292508da532bSDmitry Karpeev     Logically Collective on DM
292608da532bSDmitry Karpeev 
292708da532bSDmitry Karpeev     Input Parameter:
292808da532bSDmitry Karpeev +   dm - the DM object
29290298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
293008da532bSDmitry Karpeev 
293108da532bSDmitry Karpeev     Level: intermediate
293208da532bSDmitry Karpeev 
2933835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
293408da532bSDmitry Karpeev          DMSetJacobian()
293508da532bSDmitry Karpeev 
293608da532bSDmitry Karpeev @*/
293708da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
293808da532bSDmitry Karpeev {
293908da532bSDmitry Karpeev   PetscFunctionBegin;
294008da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
294108da532bSDmitry Karpeev   PetscFunctionReturn(0);
294208da532bSDmitry Karpeev }
294308da532bSDmitry Karpeev 
294408da532bSDmitry Karpeev /*@
294508da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
294608da532bSDmitry Karpeev 
294708da532bSDmitry Karpeev     Not Collective
294808da532bSDmitry Karpeev 
294908da532bSDmitry Karpeev     Input Parameter:
295008da532bSDmitry Karpeev .   dm - the DM object to destroy
295108da532bSDmitry Karpeev 
295208da532bSDmitry Karpeev     Output Parameter:
295308da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
295408da532bSDmitry Karpeev 
295508da532bSDmitry Karpeev     Level: developer
295608da532bSDmitry Karpeev 
295774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
295808da532bSDmitry Karpeev 
295908da532bSDmitry Karpeev @*/
296008da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
296108da532bSDmitry Karpeev {
296208da532bSDmitry Karpeev   PetscFunctionBegin;
296308da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
296408da532bSDmitry Karpeev   PetscFunctionReturn(0);
296508da532bSDmitry Karpeev }
296608da532bSDmitry Karpeev 
296708da532bSDmitry Karpeev /*@C
296808da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
296908da532bSDmitry Karpeev 
297008da532bSDmitry Karpeev     Logically Collective on DM
297108da532bSDmitry Karpeev 
297208da532bSDmitry Karpeev     Input Parameters:
2973907376e6SBarry Smith .   dm - the DM object
297408da532bSDmitry Karpeev 
297508da532bSDmitry Karpeev     Output parameters:
297608da532bSDmitry Karpeev +   xl - lower bound
297708da532bSDmitry Karpeev -   xu - upper bound
297808da532bSDmitry Karpeev 
2979907376e6SBarry Smith     Level: advanced
2980907376e6SBarry Smith 
2981907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
298208da532bSDmitry Karpeev 
298374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
298408da532bSDmitry Karpeev 
298508da532bSDmitry Karpeev @*/
298608da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
298708da532bSDmitry Karpeev {
298808da532bSDmitry Karpeev   PetscErrorCode ierr;
29895fd66863SKarl Rupp 
299008da532bSDmitry Karpeev   PetscFunctionBegin;
299108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
299208da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
299308da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
299408da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
29958865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
299608da532bSDmitry Karpeev   PetscFunctionReturn(0);
299708da532bSDmitry Karpeev }
299808da532bSDmitry Karpeev 
2999b0ae01b7SPeter Brune /*@
3000b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3001b0ae01b7SPeter Brune 
3002b0ae01b7SPeter Brune     Not Collective
3003b0ae01b7SPeter Brune 
3004b0ae01b7SPeter Brune     Input Parameter:
3005b0ae01b7SPeter Brune .   dm - the DM object
3006b0ae01b7SPeter Brune 
3007b0ae01b7SPeter Brune     Output Parameter:
3008b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3009b0ae01b7SPeter Brune 
3010b0ae01b7SPeter Brune     Level: developer
3011b0ae01b7SPeter Brune 
3012b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3013b0ae01b7SPeter Brune 
3014b0ae01b7SPeter Brune @*/
3015b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3016b0ae01b7SPeter Brune {
3017b0ae01b7SPeter Brune   PetscFunctionBegin;
3018b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3019b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3020b0ae01b7SPeter Brune }
3021b0ae01b7SPeter Brune 
30223ad4599aSBarry Smith /*@
30233ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
30243ad4599aSBarry Smith 
30253ad4599aSBarry Smith     Not Collective
30263ad4599aSBarry Smith 
30273ad4599aSBarry Smith     Input Parameter:
30283ad4599aSBarry Smith .   dm - the DM object
30293ad4599aSBarry Smith 
30303ad4599aSBarry Smith     Output Parameter:
30313ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
30323ad4599aSBarry Smith 
30333ad4599aSBarry Smith     Level: developer
30343ad4599aSBarry Smith 
30353ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
30363ad4599aSBarry Smith 
30373ad4599aSBarry Smith @*/
30383ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
30393ad4599aSBarry Smith {
30403ad4599aSBarry Smith   PetscFunctionBegin;
30413ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
30423ad4599aSBarry Smith   PetscFunctionReturn(0);
30433ad4599aSBarry Smith }
30443ad4599aSBarry Smith 
3045a7058e45SLawrence Mitchell 
3046a7058e45SLawrence Mitchell /*@
3047a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3048a7058e45SLawrence Mitchell 
3049a7058e45SLawrence Mitchell     Not Collective
3050a7058e45SLawrence Mitchell 
3051a7058e45SLawrence Mitchell     Input Parameter:
3052a7058e45SLawrence Mitchell .   dm - the DM object
3053a7058e45SLawrence Mitchell 
3054a7058e45SLawrence Mitchell     Output Parameter:
3055a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3056a7058e45SLawrence Mitchell 
3057a7058e45SLawrence Mitchell     Level: developer
3058a7058e45SLawrence Mitchell 
3059a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3060a7058e45SLawrence Mitchell 
3061a7058e45SLawrence Mitchell @*/
3062a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3063a7058e45SLawrence Mitchell {
30644a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3065a7058e45SLawrence Mitchell   PetscFunctionBegin;
30664a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
30674a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3068a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3069a7058e45SLawrence Mitchell }
3070a7058e45SLawrence Mitchell 
3071748fac09SDmitry Karpeev /*@C
307208da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
307308da532bSDmitry Karpeev 
307408da532bSDmitry Karpeev     Collective on DM
307508da532bSDmitry Karpeev 
307608da532bSDmitry Karpeev     Input Parameter:
307708da532bSDmitry Karpeev +   dm - the DM object
30780298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
307908da532bSDmitry Karpeev 
308008da532bSDmitry Karpeev     Level: developer
308108da532bSDmitry Karpeev 
308274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
308308da532bSDmitry Karpeev 
308408da532bSDmitry Karpeev @*/
308508da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
308608da532bSDmitry Karpeev {
308708da532bSDmitry Karpeev   PetscErrorCode ierr;
30885fd66863SKarl Rupp 
308908da532bSDmitry Karpeev   PetscFunctionBegin;
309008da532bSDmitry Karpeev   if (x) {
309108da532bSDmitry Karpeev     if (!dm->x) {
309208da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
309308da532bSDmitry Karpeev     }
309408da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
30958865f1eaSKarl Rupp   } else if (dm->x) {
309608da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
309708da532bSDmitry Karpeev   }
309808da532bSDmitry Karpeev   PetscFunctionReturn(0);
309908da532bSDmitry Karpeev }
310008da532bSDmitry Karpeev 
31010298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3102264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3103264ace61SBarry Smith 
3104264ace61SBarry Smith /*@C
3105264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3106264ace61SBarry Smith 
3107264ace61SBarry Smith   Collective on DM
3108264ace61SBarry Smith 
3109264ace61SBarry Smith   Input Parameters:
3110264ace61SBarry Smith + dm     - The DM object
3111264ace61SBarry Smith - method - The name of the DM type
3112264ace61SBarry Smith 
3113264ace61SBarry Smith   Options Database Key:
3114264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3115264ace61SBarry Smith 
3116264ace61SBarry Smith   Notes:
3117e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3118264ace61SBarry Smith 
3119264ace61SBarry Smith   Level: intermediate
3120264ace61SBarry Smith 
3121264ace61SBarry Smith .keywords: DM, set, type
3122264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3123264ace61SBarry Smith @*/
312419fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3125264ace61SBarry Smith {
3126264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3127264ace61SBarry Smith   PetscBool      match;
3128264ace61SBarry Smith   PetscErrorCode ierr;
3129264ace61SBarry Smith 
3130264ace61SBarry Smith   PetscFunctionBegin;
3131264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3132251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3133264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3134264ace61SBarry Smith 
31350f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
31361c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3137ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3138264ace61SBarry Smith 
3139264ace61SBarry Smith   if (dm->ops->destroy) {
3140264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
31410298fd71SBarry Smith     dm->ops->destroy = NULL;
3142264ace61SBarry Smith   }
3143264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3144264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3145264ace61SBarry Smith   PetscFunctionReturn(0);
3146264ace61SBarry Smith }
3147264ace61SBarry Smith 
3148264ace61SBarry Smith /*@C
3149264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3150264ace61SBarry Smith 
3151264ace61SBarry Smith   Not Collective
3152264ace61SBarry Smith 
3153264ace61SBarry Smith   Input Parameter:
3154264ace61SBarry Smith . dm  - The DM
3155264ace61SBarry Smith 
3156264ace61SBarry Smith   Output Parameter:
3157264ace61SBarry Smith . type - The DM type name
3158264ace61SBarry Smith 
3159264ace61SBarry Smith   Level: intermediate
3160264ace61SBarry Smith 
3161264ace61SBarry Smith .keywords: DM, get, type, name
3162264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3163264ace61SBarry Smith @*/
316419fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3165264ace61SBarry Smith {
3166264ace61SBarry Smith   PetscErrorCode ierr;
3167264ace61SBarry Smith 
3168264ace61SBarry Smith   PetscFunctionBegin;
3169264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3170c959eef4SJed Brown   PetscValidPointer(type,2);
3171607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3172264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3173264ace61SBarry Smith   PetscFunctionReturn(0);
3174264ace61SBarry Smith }
3175264ace61SBarry Smith 
317667a56275SMatthew G Knepley /*@C
317767a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
317867a56275SMatthew G Knepley 
317967a56275SMatthew G Knepley   Collective on DM
318067a56275SMatthew G Knepley 
318167a56275SMatthew G Knepley   Input Parameters:
318267a56275SMatthew G Knepley + dm - the DM
318367a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
318467a56275SMatthew G Knepley 
318567a56275SMatthew G Knepley   Output Parameter:
318667a56275SMatthew G Knepley . M - pointer to new DM
318767a56275SMatthew G Knepley 
318867a56275SMatthew G Knepley   Notes:
318967a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
319067a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
319167a56275SMatthew G Knepley   of the input DM.
319267a56275SMatthew G Knepley 
319367a56275SMatthew G Knepley   Level: intermediate
319467a56275SMatthew G Knepley 
319567a56275SMatthew G Knepley .seealso: DMCreate()
319667a56275SMatthew G Knepley @*/
319719fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
319867a56275SMatthew G Knepley {
319967a56275SMatthew G Knepley   DM             B;
320067a56275SMatthew G Knepley   char           convname[256];
3201c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
320267a56275SMatthew G Knepley   PetscErrorCode ierr;
320367a56275SMatthew G Knepley 
320467a56275SMatthew G Knepley   PetscFunctionBegin;
320567a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
320667a56275SMatthew G Knepley   PetscValidType(dm,1);
320767a56275SMatthew G Knepley   PetscValidPointer(M,3);
3208251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3209c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3210c067b6caSMatthew G. Knepley   if (sametype) {
3211c067b6caSMatthew G. Knepley     *M   = dm;
3212c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3213c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3214c067b6caSMatthew G. Knepley   } else {
32150298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
321667a56275SMatthew G Knepley 
321767a56275SMatthew G Knepley     /*
321867a56275SMatthew G Knepley        Order of precedence:
321967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
322067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
322167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
322267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
322367a56275SMatthew G Knepley        5) Use a really basic converter.
322467a56275SMatthew G Knepley     */
322567a56275SMatthew G Knepley 
322667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3227a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3228a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3229a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3230a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3231a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
32320005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
323367a56275SMatthew G Knepley     if (conv) goto foundconv;
323467a56275SMatthew G Knepley 
323567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
323682f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
323767a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3238a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3239a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3240a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3241a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3242a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
32430005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
324467a56275SMatthew G Knepley     if (conv) {
3245fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
324667a56275SMatthew G Knepley       goto foundconv;
324767a56275SMatthew G Knepley     }
324867a56275SMatthew G Knepley 
324967a56275SMatthew G Knepley #if 0
325067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
325167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3252fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
325367a56275SMatthew G Knepley     if (conv) goto foundconv;
325467a56275SMatthew G Knepley 
325567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
325667a56275SMatthew G Knepley     if (dm->ops->convert) {
325767a56275SMatthew G Knepley       conv = dm->ops->convert;
325867a56275SMatthew G Knepley     }
325967a56275SMatthew G Knepley     if (conv) goto foundconv;
326067a56275SMatthew G Knepley #endif
326167a56275SMatthew G Knepley 
326267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
326382f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
326467a56275SMatthew G Knepley 
326567a56275SMatthew G Knepley foundconv:
326667a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
326767a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
326812fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
326990b157c4SStefano Zampini     {
327090b157c4SStefano Zampini       PetscBool             isper;
327112fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
327212fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
327390b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
327490b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
327512fa691eSMatthew G. Knepley     }
327667a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
327767a56275SMatthew G Knepley   }
327867a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
327967a56275SMatthew G Knepley   PetscFunctionReturn(0);
328067a56275SMatthew G Knepley }
3281264ace61SBarry Smith 
3282264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3283264ace61SBarry Smith 
3284264ace61SBarry Smith /*@C
32851c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
32861c84c290SBarry Smith 
32871c84c290SBarry Smith   Not Collective
32881c84c290SBarry Smith 
32891c84c290SBarry Smith   Input Parameters:
32901c84c290SBarry Smith + name        - The name of a new user-defined creation routine
32911c84c290SBarry Smith - create_func - The creation routine itself
32921c84c290SBarry Smith 
32931c84c290SBarry Smith   Notes:
32941c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
32951c84c290SBarry Smith 
32961c84c290SBarry Smith 
32971c84c290SBarry Smith   Sample usage:
32981c84c290SBarry Smith .vb
3299bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
33001c84c290SBarry Smith .ve
33011c84c290SBarry Smith 
33021c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
33031c84c290SBarry Smith .vb
33041c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
33051c84c290SBarry Smith     DMSetType(DM,"my_da");
33061c84c290SBarry Smith .ve
33071c84c290SBarry Smith    or at runtime via the option
33081c84c290SBarry Smith .vb
33091c84c290SBarry Smith     -da_type my_da
33101c84c290SBarry Smith .ve
3311264ace61SBarry Smith 
3312264ace61SBarry Smith   Level: advanced
33131c84c290SBarry Smith 
33141c84c290SBarry Smith .keywords: DM, register
3315bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
33161c84c290SBarry Smith 
3317264ace61SBarry Smith @*/
3318bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3319264ace61SBarry Smith {
3320264ace61SBarry Smith   PetscErrorCode ierr;
3321264ace61SBarry Smith 
3322264ace61SBarry Smith   PetscFunctionBegin;
3323a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3324264ace61SBarry Smith   PetscFunctionReturn(0);
3325264ace61SBarry Smith }
3326264ace61SBarry Smith 
3327b859378eSBarry Smith /*@C
332855849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3329b859378eSBarry Smith 
3330b859378eSBarry Smith   Collective on PetscViewer
3331b859378eSBarry Smith 
3332b859378eSBarry Smith   Input Parameters:
3333b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3334b859378eSBarry Smith            some related function before a call to DMLoad().
3335b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3336b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3337b859378eSBarry Smith 
3338b859378eSBarry Smith    Level: intermediate
3339b859378eSBarry Smith 
3340b859378eSBarry Smith   Notes:
334155849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3342b859378eSBarry Smith 
3343b859378eSBarry Smith   Notes for advanced users:
3344b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3345b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3346b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3347b859378eSBarry Smith   format is
3348b859378eSBarry Smith .vb
3349b859378eSBarry Smith      has not yet been determined
3350b859378eSBarry Smith .ve
3351b859378eSBarry Smith 
3352b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3353b859378eSBarry Smith @*/
3354b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3355b859378eSBarry Smith {
33569331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3357b859378eSBarry Smith   PetscErrorCode ierr;
3358b859378eSBarry Smith 
3359b859378eSBarry Smith   PetscFunctionBegin;
3360b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3361b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
336232c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
33639331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
33649331c7a4SMatthew G. Knepley   if (isbinary) {
33659331c7a4SMatthew G. Knepley     PetscInt classid;
33669331c7a4SMatthew G. Knepley     char     type[256];
3367b859378eSBarry Smith 
3368060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
33699200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3370060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
337132c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
33729331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
33739331c7a4SMatthew G. Knepley   } else if (ishdf5) {
33749331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
33759331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3376b859378eSBarry Smith   PetscFunctionReturn(0);
3377b859378eSBarry Smith }
3378b859378eSBarry Smith 
33797da65231SMatthew G Knepley /******************************** FEM Support **********************************/
33807da65231SMatthew G Knepley 
3381a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3382a6dfd86eSKarl Rupp {
33831d47ebbbSSatish Balay   PetscInt       f;
33841b30c384SMatthew G Knepley   PetscErrorCode ierr;
33851b30c384SMatthew G Knepley 
33867da65231SMatthew G Knepley   PetscFunctionBegin;
338774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
33881d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
338957622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
33907da65231SMatthew G Knepley   }
33917da65231SMatthew G Knepley   PetscFunctionReturn(0);
33927da65231SMatthew G Knepley }
33937da65231SMatthew G Knepley 
3394a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3395a6dfd86eSKarl Rupp {
33961b30c384SMatthew G Knepley   PetscInt       f, g;
33977da65231SMatthew G Knepley   PetscErrorCode ierr;
33987da65231SMatthew G Knepley 
33997da65231SMatthew G Knepley   PetscFunctionBegin;
340074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
34011d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
340274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
34031d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3404e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
34057da65231SMatthew G Knepley     }
340674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
34077da65231SMatthew G Knepley   }
34087da65231SMatthew G Knepley   PetscFunctionReturn(0);
34097da65231SMatthew G Knepley }
3410e7c4fc90SDmitry Karpeev 
34116113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3412e759306cSMatthew G. Knepley {
34130c5b8624SToby Isaac   PetscInt          localSize, bs;
34140c5b8624SToby Isaac   PetscMPIInt       size;
34150c5b8624SToby Isaac   Vec               x, xglob;
34160c5b8624SToby Isaac   const PetscScalar *xarray;
3417e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3418e759306cSMatthew G. Knepley 
3419e759306cSMatthew G. Knepley   PetscFunctionBegin;
34209852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3421e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3422e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
34236113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
34240c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
34250c5b8624SToby Isaac   if (size > 1) {
34260c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
34270c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
34280c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
34290c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
34300c5b8624SToby Isaac   } else {
34310c5b8624SToby Isaac     xglob = x;
34320c5b8624SToby Isaac   }
34330c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
34340c5b8624SToby Isaac   if (size > 1) {
34350c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
34360c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
34370c5b8624SToby Isaac   }
3438e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3439e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3440e759306cSMatthew G. Knepley }
3441e759306cSMatthew G. Knepley 
344288ed4aceSMatthew G Knepley /*@
344388ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
344488ed4aceSMatthew G Knepley 
344588ed4aceSMatthew G Knepley   Input Parameter:
344688ed4aceSMatthew G Knepley . dm - The DM
344788ed4aceSMatthew G Knepley 
344888ed4aceSMatthew G Knepley   Output Parameter:
344988ed4aceSMatthew G Knepley . section - The PetscSection
345088ed4aceSMatthew G Knepley 
3451*e5893cccSMatthew G. Knepley   Options Database Keys:
3452*e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3453*e5893cccSMatthew G. Knepley 
345488ed4aceSMatthew G Knepley   Level: intermediate
345588ed4aceSMatthew G Knepley 
345688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
345788ed4aceSMatthew G Knepley 
345888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
345988ed4aceSMatthew G Knepley @*/
34600adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
34610adebc6cSBarry Smith {
3462fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3463fd59a867SMatthew G. Knepley 
346488ed4aceSMatthew G Knepley   PetscFunctionBegin;
346588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
346688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
34672f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
34682f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3469ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
34702f0f8703SMatthew G. Knepley   }
347188ed4aceSMatthew G Knepley   *section = dm->defaultSection;
347288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
347388ed4aceSMatthew G Knepley }
347488ed4aceSMatthew G Knepley 
347588ed4aceSMatthew G Knepley /*@
347688ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
347788ed4aceSMatthew G Knepley 
347888ed4aceSMatthew G Knepley   Input Parameters:
347988ed4aceSMatthew G Knepley + dm - The DM
348088ed4aceSMatthew G Knepley - section - The PetscSection
348188ed4aceSMatthew G Knepley 
348288ed4aceSMatthew G Knepley   Level: intermediate
348388ed4aceSMatthew G Knepley 
348488ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
348588ed4aceSMatthew G Knepley 
348688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
348788ed4aceSMatthew G Knepley @*/
34880adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
34890adebc6cSBarry Smith {
3490c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3491af122d2aSMatthew G Knepley   PetscInt       f;
349288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
349388ed4aceSMatthew G Knepley 
349488ed4aceSMatthew G Knepley   PetscFunctionBegin;
349588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3496c473ab19SMatthew G. Knepley   if (section) {
34971d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
34981d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3499c473ab19SMatthew G. Knepley   }
350088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
350188ed4aceSMatthew G Knepley   dm->defaultSection = section;
3502c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3503af122d2aSMatthew G Knepley   if (numFields) {
3504af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3505af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
35060f21e855SMatthew G. Knepley       PetscObject disc;
3507af122d2aSMatthew G Knepley       const char *name;
3508af122d2aSMatthew G Knepley 
3509af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
35100f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
35110f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3512af122d2aSMatthew G Knepley     }
3513af122d2aSMatthew G Knepley   }
35141d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
35151d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
351688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
351788ed4aceSMatthew G Knepley }
351888ed4aceSMatthew G Knepley 
35199435951eSToby Isaac /*@
35209435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
35219435951eSToby Isaac 
3522e228b242SToby Isaac   not collective
3523e228b242SToby Isaac 
35249435951eSToby Isaac   Input Parameter:
35259435951eSToby Isaac . dm - The DM
35269435951eSToby Isaac 
35279435951eSToby Isaac   Output Parameter:
35289435951eSToby 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.
35299435951eSToby 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.
35309435951eSToby Isaac 
35319435951eSToby Isaac   Level: advanced
35329435951eSToby Isaac 
35339435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
35349435951eSToby Isaac 
35359435951eSToby Isaac .seealso: DMSetDefaultConstraints()
35369435951eSToby Isaac @*/
35379435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
35389435951eSToby Isaac {
35399435951eSToby Isaac   PetscErrorCode ierr;
35409435951eSToby Isaac 
35419435951eSToby Isaac   PetscFunctionBegin;
35429435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35439435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
354445a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
354545a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
35469435951eSToby Isaac   PetscFunctionReturn(0);
35479435951eSToby Isaac }
35489435951eSToby Isaac 
35499435951eSToby Isaac /*@
35509435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
35519435951eSToby Isaac 
35529435951eSToby 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().
35539435951eSToby Isaac 
35549435951eSToby 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.
35559435951eSToby Isaac 
3556e228b242SToby Isaac   collective on dm
3557e228b242SToby Isaac 
35589435951eSToby Isaac   Input Parameters:
35599435951eSToby Isaac + dm - The DM
3560e228b242SToby 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).
3561e228b242SToby 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).
35629435951eSToby Isaac 
35639435951eSToby Isaac   Level: advanced
35649435951eSToby Isaac 
35659435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
35669435951eSToby Isaac 
35679435951eSToby Isaac .seealso: DMGetDefaultConstraints()
35689435951eSToby Isaac @*/
35699435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
35709435951eSToby Isaac {
3571e228b242SToby Isaac   PetscMPIInt result;
35729435951eSToby Isaac   PetscErrorCode ierr;
35739435951eSToby Isaac 
35749435951eSToby Isaac   PetscFunctionBegin;
35759435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3576e228b242SToby Isaac   if (section) {
3577e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3578e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3579f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3580e228b242SToby Isaac   }
3581e228b242SToby Isaac   if (mat) {
3582e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3583e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3584f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3585e228b242SToby Isaac   }
35869435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
35879435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
35889435951eSToby Isaac   dm->defaultConstraintSection = section;
35899435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
35909435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
35919435951eSToby Isaac   dm->defaultConstraintMat = mat;
35929435951eSToby Isaac   PetscFunctionReturn(0);
35939435951eSToby Isaac }
35949435951eSToby Isaac 
3595f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3596507e4973SMatthew G. Knepley /*
3597507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3598507e4973SMatthew G. Knepley 
3599507e4973SMatthew G. Knepley   Input Parameters:
3600507e4973SMatthew G. Knepley + dm - The DM
3601507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3602507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3603507e4973SMatthew G. Knepley 
3604507e4973SMatthew G. Knepley   Level: intermediate
3605507e4973SMatthew G. Knepley 
3606507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3607507e4973SMatthew G. Knepley */
3608f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3609507e4973SMatthew G. Knepley {
3610507e4973SMatthew G. Knepley   MPI_Comm        comm;
3611507e4973SMatthew G. Knepley   PetscLayout     layout;
3612507e4973SMatthew G. Knepley   const PetscInt *ranges;
3613507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3614507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3615507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3616507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3617507e4973SMatthew G. Knepley 
3618507e4973SMatthew G. Knepley   PetscFunctionBegin;
3619507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3620507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3621507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3622507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3623507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3624507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3625507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3626507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3627507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3628507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3629507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3630507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3631f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3632507e4973SMatthew G. Knepley 
3633507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3634507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3635507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3636507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3637507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3638507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3639507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3640507e4973SMatthew 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;}
3641507e4973SMatthew 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;}
3642507e4973SMatthew G. Knepley     if (gdof < 0) {
3643507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3644507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3645507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3646507e4973SMatthew G. Knepley 
3647507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3648507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3649507e4973SMatthew 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;}
3650507e4973SMatthew G. Knepley       }
3651507e4973SMatthew G. Knepley     }
3652507e4973SMatthew G. Knepley   }
3653507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3654507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3655b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3656507e4973SMatthew G. Knepley   if (!gvalid) {
3657507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3658507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3659507e4973SMatthew G. Knepley   }
3660507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3661507e4973SMatthew G. Knepley }
3662f741bcd2SMatthew G. Knepley #endif
3663507e4973SMatthew G. Knepley 
366488ed4aceSMatthew G Knepley /*@
366588ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
366688ed4aceSMatthew G Knepley 
36678b1ab98fSJed Brown   Collective on DM
36688b1ab98fSJed Brown 
366988ed4aceSMatthew G Knepley   Input Parameter:
367088ed4aceSMatthew G Knepley . dm - The DM
367188ed4aceSMatthew G Knepley 
367288ed4aceSMatthew G Knepley   Output Parameter:
367388ed4aceSMatthew G Knepley . section - The PetscSection
367488ed4aceSMatthew G Knepley 
367588ed4aceSMatthew G Knepley   Level: intermediate
367688ed4aceSMatthew G Knepley 
367788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
367888ed4aceSMatthew G Knepley 
367988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
368088ed4aceSMatthew G Knepley @*/
36810adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
36820adebc6cSBarry Smith {
368388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
368488ed4aceSMatthew G Knepley 
368588ed4aceSMatthew G Knepley   PetscFunctionBegin;
368688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
368788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
368888ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3689fd59a867SMatthew G. Knepley     PetscSection s;
3690fd59a867SMatthew G. Knepley 
3691fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3692fd59a867SMatthew 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");
3693fd59a867SMatthew 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");
369415b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3695cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3696ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3697685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
369888ed4aceSMatthew G Knepley   }
369988ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
370088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
370188ed4aceSMatthew G Knepley }
370288ed4aceSMatthew G Knepley 
3703b21d0597SMatthew G Knepley /*@
3704b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3705b21d0597SMatthew G Knepley 
3706b21d0597SMatthew G Knepley   Input Parameters:
3707b21d0597SMatthew G Knepley + dm - The DM
37085080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3709b21d0597SMatthew G Knepley 
3710b21d0597SMatthew G Knepley   Level: intermediate
3711b21d0597SMatthew G Knepley 
3712b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3713b21d0597SMatthew G Knepley 
3714b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3715b21d0597SMatthew G Knepley @*/
37160adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
37170adebc6cSBarry Smith {
3718b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3719b21d0597SMatthew G Knepley 
3720b21d0597SMatthew G Knepley   PetscFunctionBegin;
3721b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37225080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
37231d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3724b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3725b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3726507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3727f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3728507e4973SMatthew G. Knepley #endif
3729b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3730b21d0597SMatthew G Knepley }
3731b21d0597SMatthew G Knepley 
373288ed4aceSMatthew G Knepley /*@
373388ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
373488ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
373588ed4aceSMatthew G Knepley 
373688ed4aceSMatthew G Knepley   Input Parameter:
373788ed4aceSMatthew G Knepley . dm - The DM
373888ed4aceSMatthew G Knepley 
373988ed4aceSMatthew G Knepley   Output Parameter:
374088ed4aceSMatthew G Knepley . sf - The PetscSF
374188ed4aceSMatthew G Knepley 
374288ed4aceSMatthew G Knepley   Level: intermediate
374388ed4aceSMatthew G Knepley 
374488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
374588ed4aceSMatthew G Knepley 
374688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
374788ed4aceSMatthew G Knepley @*/
37480adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
37490adebc6cSBarry Smith {
375088ed4aceSMatthew G Knepley   PetscInt       nroots;
375188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
375288ed4aceSMatthew G Knepley 
375388ed4aceSMatthew G Knepley   PetscFunctionBegin;
375488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
375588ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
37560298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
375788ed4aceSMatthew G Knepley   if (nroots < 0) {
375888ed4aceSMatthew G Knepley     PetscSection section, gSection;
375988ed4aceSMatthew G Knepley 
376088ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
376131ea6d37SMatthew G Knepley     if (section) {
376288ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
376388ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
376431ea6d37SMatthew G Knepley     } else {
37650298fd71SBarry Smith       *sf = NULL;
376631ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
376731ea6d37SMatthew G Knepley     }
376888ed4aceSMatthew G Knepley   }
376988ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
377088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
377188ed4aceSMatthew G Knepley }
377288ed4aceSMatthew G Knepley 
377388ed4aceSMatthew G Knepley /*@
377488ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
377588ed4aceSMatthew G Knepley 
377688ed4aceSMatthew G Knepley   Input Parameters:
377788ed4aceSMatthew G Knepley + dm - The DM
377888ed4aceSMatthew G Knepley - sf - The PetscSF
377988ed4aceSMatthew G Knepley 
378088ed4aceSMatthew G Knepley   Level: intermediate
378188ed4aceSMatthew G Knepley 
378288ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
378388ed4aceSMatthew G Knepley 
378488ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
378588ed4aceSMatthew G Knepley @*/
37860adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
37870adebc6cSBarry Smith {
378888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
378988ed4aceSMatthew G Knepley 
379088ed4aceSMatthew G Knepley   PetscFunctionBegin;
379188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
379288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
379388ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
379488ed4aceSMatthew G Knepley   dm->defaultSF = sf;
379588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
379688ed4aceSMatthew G Knepley }
379788ed4aceSMatthew G Knepley 
379888ed4aceSMatthew G Knepley /*@C
379988ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
380088ed4aceSMatthew G Knepley   describing the data layout.
380188ed4aceSMatthew G Knepley 
380288ed4aceSMatthew G Knepley   Input Parameters:
380388ed4aceSMatthew G Knepley + dm - The DM
380488ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
380588ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
380688ed4aceSMatthew G Knepley 
380788ed4aceSMatthew G Knepley   Level: intermediate
380888ed4aceSMatthew G Knepley 
380988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
381088ed4aceSMatthew G Knepley @*/
381188ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
381288ed4aceSMatthew G Knepley {
381382f516ccSBarry Smith   MPI_Comm       comm;
381488ed4aceSMatthew G Knepley   PetscLayout    layout;
381588ed4aceSMatthew G Knepley   const PetscInt *ranges;
381688ed4aceSMatthew G Knepley   PetscInt       *local;
381788ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3818ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
381988ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
382088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
382188ed4aceSMatthew G Knepley 
382288ed4aceSMatthew G Knepley   PetscFunctionBegin;
382382f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
382488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
382588ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
382688ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
382788ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
382888ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
382988ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
383088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
383188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
383288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
383388ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3834ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
38356636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
383688ed4aceSMatthew G Knepley 
38376636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
38386636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3839235fbf56SMatthew 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));
38406636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
384188ed4aceSMatthew G Knepley   }
3842785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3843785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
384488ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
38451f588964SMatthew G Knepley     const PetscInt *cind;
38466636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
384788ed4aceSMatthew G Knepley 
384888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
384988ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
385088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
385188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
385288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
38536636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
385488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
38556636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
38566636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
38576636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3858057b4bcdSMatthew 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);
38596636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
38606636e97aSMatthew G Knepley     }
386188ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
386288ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
386388ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
386488ed4aceSMatthew G Knepley     }
386588ed4aceSMatthew G Knepley     if (gdof < 0) {
38666636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
386788ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
386888ed4aceSMatthew G Knepley 
386905376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
387031d3f06eSJed Brown         if (r < 0) r = -(r+2);
387105376888SMatthew 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);
387288ed4aceSMatthew G Knepley         remote[l].rank  = r;
387388ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
387488ed4aceSMatthew G Knepley       }
387588ed4aceSMatthew G Knepley     } else {
38766636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
387788ed4aceSMatthew G Knepley         remote[l].rank  = rank;
387888ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
387988ed4aceSMatthew G Knepley       }
388088ed4aceSMatthew G Knepley     }
388188ed4aceSMatthew G Knepley   }
38826636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
388388ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
388488ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
388588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
388688ed4aceSMatthew G Knepley }
3887af122d2aSMatthew G Knepley 
3888b21d0597SMatthew G Knepley /*@
3889b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3890b21d0597SMatthew G Knepley 
3891b21d0597SMatthew G Knepley   Input Parameter:
3892b21d0597SMatthew G Knepley . dm - The DM
3893b21d0597SMatthew G Knepley 
3894b21d0597SMatthew G Knepley   Output Parameter:
3895b21d0597SMatthew G Knepley . sf - The PetscSF
3896b21d0597SMatthew G Knepley 
3897b21d0597SMatthew G Knepley   Level: intermediate
3898b21d0597SMatthew G Knepley 
3899b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3900b21d0597SMatthew G Knepley 
3901057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3902b21d0597SMatthew G Knepley @*/
39030adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
39040adebc6cSBarry Smith {
3905b21d0597SMatthew G Knepley   PetscFunctionBegin;
3906b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3907b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3908b21d0597SMatthew G Knepley   *sf = dm->sf;
3909b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3910b21d0597SMatthew G Knepley }
3911b21d0597SMatthew G Knepley 
3912057b4bcdSMatthew G Knepley /*@
3913057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3914057b4bcdSMatthew G Knepley 
3915057b4bcdSMatthew G Knepley   Input Parameters:
3916057b4bcdSMatthew G Knepley + dm - The DM
3917057b4bcdSMatthew G Knepley - sf - The PetscSF
3918057b4bcdSMatthew G Knepley 
3919057b4bcdSMatthew G Knepley   Level: intermediate
3920057b4bcdSMatthew G Knepley 
3921057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3922057b4bcdSMatthew G Knepley @*/
39230adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
39240adebc6cSBarry Smith {
3925057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3926057b4bcdSMatthew G Knepley 
3927057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3928057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3929057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3930057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3931057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3932057b4bcdSMatthew G Knepley   dm->sf = sf;
3933057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3934057b4bcdSMatthew G Knepley }
3935057b4bcdSMatthew G Knepley 
39362764a2aaSMatthew G. Knepley /*@
39372764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
39382764a2aaSMatthew G. Knepley 
39392764a2aaSMatthew G. Knepley   Input Parameter:
39402764a2aaSMatthew G. Knepley . dm - The DM
39412764a2aaSMatthew G. Knepley 
39422764a2aaSMatthew G. Knepley   Output Parameter:
39432764a2aaSMatthew G. Knepley . prob - The PetscDS
39442764a2aaSMatthew G. Knepley 
39452764a2aaSMatthew G. Knepley   Level: developer
39462764a2aaSMatthew G. Knepley 
39472764a2aaSMatthew G. Knepley .seealso: DMSetDS()
39482764a2aaSMatthew G. Knepley @*/
39492764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3950af122d2aSMatthew G Knepley {
3951af122d2aSMatthew G Knepley   PetscFunctionBegin;
3952af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39530f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
39540f21e855SMatthew G. Knepley   *prob = dm->prob;
39550f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
39560f21e855SMatthew G. Knepley }
39570f21e855SMatthew G. Knepley 
39582764a2aaSMatthew G. Knepley /*@
39592764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
39602764a2aaSMatthew G. Knepley 
39612764a2aaSMatthew G. Knepley   Input Parameters:
39622764a2aaSMatthew G. Knepley + dm - The DM
39632764a2aaSMatthew G. Knepley - prob - The PetscDS
39642764a2aaSMatthew G. Knepley 
39652764a2aaSMatthew G. Knepley   Level: developer
39662764a2aaSMatthew G. Knepley 
39672764a2aaSMatthew G. Knepley .seealso: DMGetDS()
39682764a2aaSMatthew G. Knepley @*/
39692764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
39700f21e855SMatthew G. Knepley {
3971f17e8794SMatthew G. Knepley   PetscInt       dimEmbed;
39720f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39730f21e855SMatthew G. Knepley 
39740f21e855SMatthew G. Knepley   PetscFunctionBegin;
39750f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39762764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
397737316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
39782764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
39790f21e855SMatthew G. Knepley   dm->prob = prob;
3980f17e8794SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
3981f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
39820f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
39830f21e855SMatthew G. Knepley }
39840f21e855SMatthew G. Knepley 
39850f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
39860f21e855SMatthew G. Knepley {
39870f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39880f21e855SMatthew G. Knepley 
39890f21e855SMatthew G. Knepley   PetscFunctionBegin;
39900f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39912764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3992af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3993af122d2aSMatthew G Knepley }
3994af122d2aSMatthew G Knepley 
3995af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3996af122d2aSMatthew G Knepley {
39970f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3998af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3999af122d2aSMatthew G Knepley 
4000af122d2aSMatthew G Knepley   PetscFunctionBegin;
4001af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40022764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
40030f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
40040f21e855SMatthew G. Knepley     PetscContainer obj;
40050f21e855SMatthew G. Knepley 
40060f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
40072764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
40080f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4009af122d2aSMatthew G Knepley   }
4010af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4011af122d2aSMatthew G Knepley }
4012af122d2aSMatthew G Knepley 
4013c1929be8SMatthew G. Knepley /*@
4014c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4015c1929be8SMatthew G. Knepley 
4016c1929be8SMatthew G. Knepley   Not collective
4017c1929be8SMatthew G. Knepley 
4018c1929be8SMatthew G. Knepley   Input Parameters:
4019c1929be8SMatthew G. Knepley + dm - The DM
4020c1929be8SMatthew G. Knepley - f  - The field number
4021c1929be8SMatthew G. Knepley 
4022c1929be8SMatthew G. Knepley   Output Parameter:
4023c1929be8SMatthew G. Knepley . field - The discretization object
4024c1929be8SMatthew G. Knepley 
4025c1929be8SMatthew G. Knepley   Level: developer
4026c1929be8SMatthew G. Knepley 
4027c1929be8SMatthew G. Knepley .seealso: DMSetField()
4028c1929be8SMatthew G. Knepley @*/
4029af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
4030af122d2aSMatthew G Knepley {
40310f21e855SMatthew G. Knepley   PetscErrorCode ierr;
40320f21e855SMatthew G. Knepley 
4033af122d2aSMatthew G Knepley   PetscFunctionBegin;
4034af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40352764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4036decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4037decb47aaSMatthew G. Knepley }
4038decb47aaSMatthew G. Knepley 
4039c1929be8SMatthew G. Knepley /*@
4040c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4041c1929be8SMatthew G. Knepley 
4042c1929be8SMatthew G. Knepley   Logically collective on DM
4043c1929be8SMatthew G. Knepley 
4044c1929be8SMatthew G. Knepley   Input Parameters:
4045c1929be8SMatthew G. Knepley + dm - The DM
4046c1929be8SMatthew G. Knepley . f  - The field number
4047c1929be8SMatthew G. Knepley - field - The discretization object
4048c1929be8SMatthew G. Knepley 
4049c1929be8SMatthew G. Knepley   Level: developer
4050c1929be8SMatthew G. Knepley 
4051c1929be8SMatthew G. Knepley .seealso: DMGetField()
4052c1929be8SMatthew G. Knepley @*/
4053decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
4054decb47aaSMatthew G. Knepley {
4055decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4056decb47aaSMatthew G. Knepley 
4057decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4058decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40592764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4060af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4061af122d2aSMatthew G Knepley }
40626636e97aSMatthew G Knepley 
4063b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4064b64e0483SPeter Brune {
4065b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4066b64e0483SPeter Brune   PetscErrorCode ierr;
4067b64e0483SPeter Brune   Vec coords,ccoords;
40686dbf9973SLawrence Mitchell   Mat inject;
4069b64e0483SPeter Brune   PetscFunctionBegin;
4070b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4071b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4072b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4073b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4074b64e0483SPeter Brune   if (coords && !ccoords) {
4075b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
40766668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
40776dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
40782adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
40796dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4080b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4081b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4082b64e0483SPeter Brune   }
4083b64e0483SPeter Brune   PetscFunctionReturn(0);
4084b64e0483SPeter Brune }
4085b64e0483SPeter Brune 
408603dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
408703dadc2fSPeter Brune {
408803dadc2fSPeter Brune   DM dm_coord,subdm_coord;
408903dadc2fSPeter Brune   PetscErrorCode ierr;
409003dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
409103dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
409203dadc2fSPeter Brune   PetscFunctionBegin;
409303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
409403dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
409503dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
409603dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
409703dadc2fSPeter Brune   if (coords && !ccoords) {
409803dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
40996668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
410003dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
410124640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
410203dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
410303dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
410403dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
410503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
410603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
410703dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
410803dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
410903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
411003dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
411103dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
411203dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
411303dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
411403dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
411503dadc2fSPeter Brune   }
411603dadc2fSPeter Brune   PetscFunctionReturn(0);
411703dadc2fSPeter Brune }
411803dadc2fSPeter Brune 
4119c73cfb54SMatthew G. Knepley /*@
4120c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4121c73cfb54SMatthew G. Knepley 
4122c73cfb54SMatthew G. Knepley   Not collective
4123c73cfb54SMatthew G. Knepley 
4124c73cfb54SMatthew G. Knepley   Input Parameter:
4125c73cfb54SMatthew G. Knepley . dm - The DM
4126c73cfb54SMatthew G. Knepley 
4127c73cfb54SMatthew G. Knepley   Output Parameter:
4128c73cfb54SMatthew G. Knepley . dim - The topological dimension
4129c73cfb54SMatthew G. Knepley 
4130c73cfb54SMatthew G. Knepley   Level: beginner
4131c73cfb54SMatthew G. Knepley 
4132c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4133c73cfb54SMatthew G. Knepley @*/
4134c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4135c73cfb54SMatthew G. Knepley {
4136c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4137c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4138c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4139c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4140c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4141c73cfb54SMatthew G. Knepley }
4142c73cfb54SMatthew G. Knepley 
4143c73cfb54SMatthew G. Knepley /*@
4144c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4145c73cfb54SMatthew G. Knepley 
4146c73cfb54SMatthew G. Knepley   Collective on dm
4147c73cfb54SMatthew G. Knepley 
4148c73cfb54SMatthew G. Knepley   Input Parameters:
4149c73cfb54SMatthew G. Knepley + dm - The DM
4150c73cfb54SMatthew G. Knepley - dim - The topological dimension
4151c73cfb54SMatthew G. Knepley 
4152c73cfb54SMatthew G. Knepley   Level: beginner
4153c73cfb54SMatthew G. Knepley 
4154c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4155c73cfb54SMatthew G. Knepley @*/
4156c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4157c73cfb54SMatthew G. Knepley {
4158f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4159f17e8794SMatthew G. Knepley 
4160c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4161c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4162c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4163c73cfb54SMatthew G. Knepley   dm->dim = dim;
4164f17e8794SMatthew G. Knepley   if (dm->prob->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dim);CHKERRQ(ierr);}
4165c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4166c73cfb54SMatthew G. Knepley }
4167c73cfb54SMatthew G. Knepley 
4168793f3fe5SMatthew G. Knepley /*@
4169793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4170793f3fe5SMatthew G. Knepley 
4171793f3fe5SMatthew G. Knepley   Collective on DM
4172793f3fe5SMatthew G. Knepley 
4173793f3fe5SMatthew G. Knepley   Input Parameters:
4174793f3fe5SMatthew G. Knepley + dm - the DM
4175793f3fe5SMatthew G. Knepley - dim - the dimension
4176793f3fe5SMatthew G. Knepley 
4177793f3fe5SMatthew G. Knepley   Output Parameters:
4178793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4179793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4180793f3fe5SMatthew G. Knepley 
4181793f3fe5SMatthew G. Knepley   Note:
4182793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4183793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4184793f3fe5SMatthew G. Knepley   then the interval is empty.
4185793f3fe5SMatthew G. Knepley 
4186793f3fe5SMatthew G. Knepley   Level: intermediate
4187793f3fe5SMatthew G. Knepley 
4188793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4189793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4190793f3fe5SMatthew G. Knepley @*/
4191793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4192793f3fe5SMatthew G. Knepley {
4193793f3fe5SMatthew G. Knepley   PetscInt       d;
4194793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4195793f3fe5SMatthew G. Knepley 
4196793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4197793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4198793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4199793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4200793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4201793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4202793f3fe5SMatthew G. Knepley }
4203793f3fe5SMatthew G. Knepley 
42046636e97aSMatthew G Knepley /*@
42056636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
42066636e97aSMatthew G Knepley 
42076636e97aSMatthew G Knepley   Collective on DM
42086636e97aSMatthew G Knepley 
42096636e97aSMatthew G Knepley   Input Parameters:
42106636e97aSMatthew G Knepley + dm - the DM
42116636e97aSMatthew G Knepley - c - coordinate vector
42126636e97aSMatthew G Knepley 
42136636e97aSMatthew G Knepley   Note:
42146636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
42156636e97aSMatthew G Knepley 
42166636e97aSMatthew G Knepley   Level: intermediate
42176636e97aSMatthew G Knepley 
42186636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42196636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
42206636e97aSMatthew G Knepley @*/
42216636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
42226636e97aSMatthew G Knepley {
42236636e97aSMatthew G Knepley   PetscErrorCode ierr;
42246636e97aSMatthew G Knepley 
42256636e97aSMatthew G Knepley   PetscFunctionBegin;
42266636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42276636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
42286636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
42296636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
42306636e97aSMatthew G Knepley   dm->coordinates = c;
42316636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4232b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
423303dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
42346636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42356636e97aSMatthew G Knepley }
42366636e97aSMatthew G Knepley 
42376636e97aSMatthew G Knepley /*@
42386636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
42396636e97aSMatthew G Knepley 
42406636e97aSMatthew G Knepley   Collective on DM
42416636e97aSMatthew G Knepley 
42426636e97aSMatthew G Knepley    Input Parameters:
42436636e97aSMatthew G Knepley +  dm - the DM
42446636e97aSMatthew G Knepley -  c - coordinate vector
42456636e97aSMatthew G Knepley 
42466636e97aSMatthew G Knepley   Note:
42476636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
42486636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
42496636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
42506636e97aSMatthew G Knepley 
42516636e97aSMatthew G Knepley   Level: intermediate
42526636e97aSMatthew G Knepley 
42536636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42546636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
42556636e97aSMatthew G Knepley @*/
42566636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
42576636e97aSMatthew G Knepley {
42586636e97aSMatthew G Knepley   PetscErrorCode ierr;
42596636e97aSMatthew G Knepley 
42606636e97aSMatthew G Knepley   PetscFunctionBegin;
42616636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42626636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
42636636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
42646636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
42658865f1eaSKarl Rupp 
42666636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
42678865f1eaSKarl Rupp 
42686636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
42696636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42706636e97aSMatthew G Knepley }
42716636e97aSMatthew G Knepley 
42726636e97aSMatthew G Knepley /*@
42736636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
42746636e97aSMatthew G Knepley 
42756636e97aSMatthew G Knepley   Not Collective
42766636e97aSMatthew G Knepley 
42776636e97aSMatthew G Knepley   Input Parameter:
42786636e97aSMatthew G Knepley . dm - the DM
42796636e97aSMatthew G Knepley 
42806636e97aSMatthew G Knepley   Output Parameter:
42816636e97aSMatthew G Knepley . c - global coordinate vector
42826636e97aSMatthew G Knepley 
42836636e97aSMatthew G Knepley   Note:
42846636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
42856636e97aSMatthew G Knepley 
42866636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
42876636e97aSMatthew G Knepley 
42886636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
42896636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
42906636e97aSMatthew G Knepley 
42916636e97aSMatthew G Knepley   Level: intermediate
42926636e97aSMatthew G Knepley 
42936636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42946636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
42956636e97aSMatthew G Knepley @*/
42966636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
42976636e97aSMatthew G Knepley {
42986636e97aSMatthew G Knepley   PetscErrorCode ierr;
42996636e97aSMatthew G Knepley 
43006636e97aSMatthew G Knepley   PetscFunctionBegin;
43016636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43026636e97aSMatthew G Knepley   PetscValidPointer(c,2);
43031f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
43040298fd71SBarry Smith     DM cdm = NULL;
43056636e97aSMatthew G Knepley 
43066636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
43076636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
43086636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
43096636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
43106636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
43116636e97aSMatthew G Knepley   }
43126636e97aSMatthew G Knepley   *c = dm->coordinates;
43136636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43146636e97aSMatthew G Knepley }
43156636e97aSMatthew G Knepley 
43166636e97aSMatthew G Knepley /*@
43176636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
43186636e97aSMatthew G Knepley 
43196636e97aSMatthew G Knepley   Collective on DM
43206636e97aSMatthew G Knepley 
43216636e97aSMatthew G Knepley   Input Parameter:
43226636e97aSMatthew G Knepley . dm - the DM
43236636e97aSMatthew G Knepley 
43246636e97aSMatthew G Knepley   Output Parameter:
43256636e97aSMatthew G Knepley . c - coordinate vector
43266636e97aSMatthew G Knepley 
43276636e97aSMatthew G Knepley   Note:
43286636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
43296636e97aSMatthew G Knepley 
43306636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
43316636e97aSMatthew G Knepley 
43326636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
43336636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
43346636e97aSMatthew G Knepley 
43356636e97aSMatthew G Knepley   Level: intermediate
43366636e97aSMatthew G Knepley 
43376636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43386636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
43396636e97aSMatthew G Knepley @*/
43406636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
43416636e97aSMatthew G Knepley {
43426636e97aSMatthew G Knepley   PetscErrorCode ierr;
43436636e97aSMatthew G Knepley 
43446636e97aSMatthew G Knepley   PetscFunctionBegin;
43456636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43466636e97aSMatthew G Knepley   PetscValidPointer(c,2);
43471f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
43480298fd71SBarry Smith     DM cdm = NULL;
43496636e97aSMatthew G Knepley 
43506636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
43516636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
43526636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
43536636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
43546636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
43556636e97aSMatthew G Knepley   }
43566636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
43576636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43586636e97aSMatthew G Knepley }
43596636e97aSMatthew G Knepley 
43606636e97aSMatthew G Knepley /*@
43611cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
43626636e97aSMatthew G Knepley 
43636636e97aSMatthew G Knepley   Collective on DM
43646636e97aSMatthew G Knepley 
43656636e97aSMatthew G Knepley   Input Parameter:
43666636e97aSMatthew G Knepley . dm - the DM
43676636e97aSMatthew G Knepley 
43686636e97aSMatthew G Knepley   Output Parameter:
43696636e97aSMatthew G Knepley . cdm - coordinate DM
43706636e97aSMatthew G Knepley 
43716636e97aSMatthew G Knepley   Level: intermediate
43726636e97aSMatthew G Knepley 
43736636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43741cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
43756636e97aSMatthew G Knepley @*/
43766636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
43776636e97aSMatthew G Knepley {
43786636e97aSMatthew G Knepley   PetscErrorCode ierr;
43796636e97aSMatthew G Knepley 
43806636e97aSMatthew G Knepley   PetscFunctionBegin;
43816636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43826636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
43836636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
438482f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
43856636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
43866636e97aSMatthew G Knepley   }
43876636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
43886636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43896636e97aSMatthew G Knepley }
4390e87bb0d3SMatthew G Knepley 
43911cfe2091SMatthew G. Knepley /*@
43921cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
43931cfe2091SMatthew G. Knepley 
43941cfe2091SMatthew G. Knepley   Logically Collective on DM
43951cfe2091SMatthew G. Knepley 
43961cfe2091SMatthew G. Knepley   Input Parameters:
43971cfe2091SMatthew G. Knepley + dm - the DM
43981cfe2091SMatthew G. Knepley - cdm - coordinate DM
43991cfe2091SMatthew G. Knepley 
44001cfe2091SMatthew G. Knepley   Level: intermediate
44011cfe2091SMatthew G. Knepley 
44021cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44031cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
44041cfe2091SMatthew G. Knepley @*/
44051cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
44061cfe2091SMatthew G. Knepley {
44071cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
44081cfe2091SMatthew G. Knepley 
44091cfe2091SMatthew G. Knepley   PetscFunctionBegin;
44101cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44111cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4412f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
44131cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
44141cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
44151cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
44161cfe2091SMatthew G. Knepley }
44171cfe2091SMatthew G. Knepley 
441846e270d4SMatthew G. Knepley /*@
441946e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
442046e270d4SMatthew G. Knepley 
442146e270d4SMatthew G. Knepley   Not Collective
442246e270d4SMatthew G. Knepley 
442346e270d4SMatthew G. Knepley   Input Parameter:
442446e270d4SMatthew G. Knepley . dm - The DM object
442546e270d4SMatthew G. Knepley 
442646e270d4SMatthew G. Knepley   Output Parameter:
442746e270d4SMatthew G. Knepley . dim - The embedding dimension
442846e270d4SMatthew G. Knepley 
442946e270d4SMatthew G. Knepley   Level: intermediate
443046e270d4SMatthew G. Knepley 
443146e270d4SMatthew G. Knepley .keywords: mesh, coordinates
443246e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
443346e270d4SMatthew G. Knepley @*/
443446e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
443546e270d4SMatthew G. Knepley {
443646e270d4SMatthew G. Knepley   PetscFunctionBegin;
443746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
443846e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
44399a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
44409a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
44419a9a41abSToby Isaac   }
444246e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
444346e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
444446e270d4SMatthew G. Knepley }
444546e270d4SMatthew G. Knepley 
444646e270d4SMatthew G. Knepley /*@
444746e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
444846e270d4SMatthew G. Knepley 
444946e270d4SMatthew G. Knepley   Not Collective
445046e270d4SMatthew G. Knepley 
445146e270d4SMatthew G. Knepley   Input Parameters:
445246e270d4SMatthew G. Knepley + dm  - The DM object
445346e270d4SMatthew G. Knepley - dim - The embedding dimension
445446e270d4SMatthew G. Knepley 
445546e270d4SMatthew G. Knepley   Level: intermediate
445646e270d4SMatthew G. Knepley 
445746e270d4SMatthew G. Knepley .keywords: mesh, coordinates
445846e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
445946e270d4SMatthew G. Knepley @*/
446046e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
446146e270d4SMatthew G. Knepley {
4462f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4463f17e8794SMatthew G. Knepley 
446446e270d4SMatthew G. Knepley   PetscFunctionBegin;
446546e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
446646e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
4467f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dimEmbed);CHKERRQ(ierr);
446846e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
446946e270d4SMatthew G. Knepley }
447046e270d4SMatthew G. Knepley 
4471e8abe2deSMatthew G. Knepley /*@
4472e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4473e8abe2deSMatthew G. Knepley 
4474e8abe2deSMatthew G. Knepley   Not Collective
4475e8abe2deSMatthew G. Knepley 
4476e8abe2deSMatthew G. Knepley   Input Parameter:
4477e8abe2deSMatthew G. Knepley . dm - The DM object
4478e8abe2deSMatthew G. Knepley 
4479e8abe2deSMatthew G. Knepley   Output Parameter:
4480e8abe2deSMatthew G. Knepley . section - The PetscSection object
4481e8abe2deSMatthew G. Knepley 
4482e8abe2deSMatthew G. Knepley   Level: intermediate
4483e8abe2deSMatthew G. Knepley 
4484e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4485e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4486e8abe2deSMatthew G. Knepley @*/
4487e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4488e8abe2deSMatthew G. Knepley {
4489e8abe2deSMatthew G. Knepley   DM             cdm;
4490e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4491e8abe2deSMatthew G. Knepley 
4492e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4493e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4494e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4495e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4496e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4497e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4498e8abe2deSMatthew G. Knepley }
4499e8abe2deSMatthew G. Knepley 
4500e8abe2deSMatthew G. Knepley /*@
4501e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4502e8abe2deSMatthew G. Knepley 
4503e8abe2deSMatthew G. Knepley   Not Collective
4504e8abe2deSMatthew G. Knepley 
4505e8abe2deSMatthew G. Knepley   Input Parameters:
4506e8abe2deSMatthew G. Knepley + dm      - The DM object
450746e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4508e8abe2deSMatthew G. Knepley - section - The PetscSection object
4509e8abe2deSMatthew G. Knepley 
4510e8abe2deSMatthew G. Knepley   Level: intermediate
4511e8abe2deSMatthew G. Knepley 
4512e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4513e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4514e8abe2deSMatthew G. Knepley @*/
451546e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4516e8abe2deSMatthew G. Knepley {
4517e8abe2deSMatthew G. Knepley   DM             cdm;
4518e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4519e8abe2deSMatthew G. Knepley 
4520e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4521e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
452246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4523e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4524e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
452546e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
45264c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
452746e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
452846e270d4SMatthew G. Knepley 
452946e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
453046e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
453146e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
453246e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
453346e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
453446e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
453546e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
453646e270d4SMatthew G. Knepley     }
45376be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
453846e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
453946e270d4SMatthew G. Knepley   }
4540e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4541e8abe2deSMatthew G. Knepley }
4542e8abe2deSMatthew G. Knepley 
45435dc8c3f7SMatthew G. Knepley /*@C
454490b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
45455dc8c3f7SMatthew G. Knepley 
45465dc8c3f7SMatthew G. Knepley   Input Parameters:
454790b157c4SStefano Zampini . dm      - The DM object
454890b157c4SStefano Zampini 
454990b157c4SStefano Zampini   Output Parameters:
455090b157c4SStefano Zampini + per     - Whether the DM is periodic or not
45515dc8c3f7SMatthew 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
45525dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
45535dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
45545dc8c3f7SMatthew G. Knepley 
45555dc8c3f7SMatthew G. Knepley   Level: developer
45565dc8c3f7SMatthew G. Knepley 
45575dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
45585dc8c3f7SMatthew G. Knepley @*/
455990b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4560c6b900c6SMatthew G. Knepley {
4561c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4562c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
456390b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4564c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4565c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
45665dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4567c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4568c6b900c6SMatthew G. Knepley }
4569c6b900c6SMatthew G. Knepley 
45705dc8c3f7SMatthew G. Knepley /*@C
45715dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
45725dc8c3f7SMatthew G. Knepley 
45735dc8c3f7SMatthew G. Knepley   Input Parameters:
45745dc8c3f7SMatthew G. Knepley + dm      - The DM object
457590b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
45765dc8c3f7SMatthew 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
45775dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
45785dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
45795dc8c3f7SMatthew G. Knepley 
45805dc8c3f7SMatthew G. Knepley   Level: developer
45815dc8c3f7SMatthew G. Knepley 
45825dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
45835dc8c3f7SMatthew G. Knepley @*/
458490b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4585c6b900c6SMatthew G. Knepley {
4586c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4587c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4588c6b900c6SMatthew G. Knepley 
4589c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4590c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
459190b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
459290b157c4SStefano Zampini   if (maxCell) {
459390b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
459490b157c4SStefano Zampini     PetscValidPointer(L,4);
459590b157c4SStefano Zampini     PetscValidPointer(bd,5);
459690b157c4SStefano Zampini   }
45975dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
45985dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
459990b157c4SStefano Zampini   if (maxCell) {
46005dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
46015dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
460290b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
460390b157c4SStefano Zampini   } else {
460490b157c4SStefano Zampini     dm->periodic = per;
460590b157c4SStefano Zampini   }
4606c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4607c6b900c6SMatthew G. Knepley }
4608c6b900c6SMatthew G. Knepley 
46092e17dfb7SMatthew G. Knepley /*@
46102e17dfb7SMatthew 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.
46112e17dfb7SMatthew G. Knepley 
46122e17dfb7SMatthew G. Knepley   Input Parameters:
46132e17dfb7SMatthew G. Knepley + dm     - The DM
461465da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
461565da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
46162e17dfb7SMatthew G. Knepley 
46172e17dfb7SMatthew G. Knepley   Output Parameter:
46182e17dfb7SMatthew G. Knepley . out - The localized coordinate point
46192e17dfb7SMatthew G. Knepley 
46202e17dfb7SMatthew G. Knepley   Level: developer
46212e17dfb7SMatthew G. Knepley 
46222e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
46232e17dfb7SMatthew G. Knepley @*/
462465da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
46252e17dfb7SMatthew G. Knepley {
46262e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
46272e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
46282e17dfb7SMatthew G. Knepley 
46292e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46302e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
46312e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46322e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46332e17dfb7SMatthew G. Knepley   } else {
463465da65dcSMatthew G. Knepley     if (endpoint) {
463565da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4636da3333bfSMatthew 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)) {
4637da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
463865da65dcSMatthew G. Knepley         } else {
4639da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
464065da65dcSMatthew G. Knepley         }
464165da65dcSMatthew G. Knepley       }
464265da65dcSMatthew G. Knepley     } else {
46432e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
46441118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
46452e17dfb7SMatthew G. Knepley       }
46462e17dfb7SMatthew G. Knepley     }
464765da65dcSMatthew G. Knepley   }
46482e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46492e17dfb7SMatthew G. Knepley }
46502e17dfb7SMatthew G. Knepley 
46512e17dfb7SMatthew G. Knepley /*
46522e17dfb7SMatthew 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.
46532e17dfb7SMatthew G. Knepley 
46542e17dfb7SMatthew G. Knepley   Input Parameters:
46552e17dfb7SMatthew G. Knepley + dm     - The DM
46562e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
46572e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
46582e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
46592e17dfb7SMatthew G. Knepley 
46602e17dfb7SMatthew G. Knepley   Output Parameter:
46612e17dfb7SMatthew G. Knepley . out - The localized coordinate point
46622e17dfb7SMatthew G. Knepley 
46632e17dfb7SMatthew G. Knepley   Level: developer
46642e17dfb7SMatthew G. Knepley 
46652e17dfb7SMatthew 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
46662e17dfb7SMatthew G. Knepley 
46672e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
46682e17dfb7SMatthew G. Knepley */
46692e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
46702e17dfb7SMatthew G. Knepley {
46712e17dfb7SMatthew G. Knepley   PetscInt d;
46722e17dfb7SMatthew G. Knepley 
46732e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46742e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46752e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46762e17dfb7SMatthew G. Knepley   } else {
46772e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4678908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
46792e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
46802e17dfb7SMatthew G. Knepley       } else {
46812e17dfb7SMatthew G. Knepley         out[d] = in[d];
46822e17dfb7SMatthew G. Knepley       }
46832e17dfb7SMatthew G. Knepley     }
46842e17dfb7SMatthew G. Knepley   }
46852e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46862e17dfb7SMatthew G. Knepley }
46872e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
46882e17dfb7SMatthew G. Knepley {
46892e17dfb7SMatthew G. Knepley   PetscInt d;
46902e17dfb7SMatthew G. Knepley 
46912e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46922e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46932e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46942e17dfb7SMatthew G. Knepley   } else {
46952e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4696908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
46972e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
46982e17dfb7SMatthew G. Knepley       } else {
46992e17dfb7SMatthew G. Knepley         out[d] = in[d];
47002e17dfb7SMatthew G. Knepley       }
47012e17dfb7SMatthew G. Knepley     }
47022e17dfb7SMatthew G. Knepley   }
47032e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47042e17dfb7SMatthew G. Knepley }
47052e17dfb7SMatthew G. Knepley 
47062e17dfb7SMatthew G. Knepley /*
47072e17dfb7SMatthew 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.
47082e17dfb7SMatthew G. Knepley 
47092e17dfb7SMatthew G. Knepley   Input Parameters:
47102e17dfb7SMatthew G. Knepley + dm     - The DM
47112e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
47122e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
47132e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
47142e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
47152e17dfb7SMatthew G. Knepley 
47162e17dfb7SMatthew G. Knepley   Output Parameter:
47172e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
47182e17dfb7SMatthew G. Knepley 
47192e17dfb7SMatthew G. Knepley   Level: developer
47202e17dfb7SMatthew G. Knepley 
47212e17dfb7SMatthew 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
47222e17dfb7SMatthew G. Knepley 
47232e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
47242e17dfb7SMatthew G. Knepley */
47252e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
47262e17dfb7SMatthew G. Knepley {
47272e17dfb7SMatthew G. Knepley   PetscInt d;
47282e17dfb7SMatthew G. Knepley 
47292e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47302e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
47312e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
47322e17dfb7SMatthew G. Knepley   } else {
47332e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4734908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
47352e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
47362e17dfb7SMatthew G. Knepley       } else {
47372e17dfb7SMatthew G. Knepley         out[d] += in[d];
47382e17dfb7SMatthew G. Knepley       }
47392e17dfb7SMatthew G. Knepley     }
47402e17dfb7SMatthew G. Knepley   }
47412e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47422e17dfb7SMatthew G. Knepley }
47432e17dfb7SMatthew G. Knepley 
474436447a5eSToby Isaac /*@
474536447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
474636447a5eSToby Isaac 
474736447a5eSToby Isaac   Input Parameter:
474836447a5eSToby Isaac . dm - The DM
474936447a5eSToby Isaac 
475036447a5eSToby Isaac   Output Parameter:
475136447a5eSToby Isaac   areLocalized - True if localized
475236447a5eSToby Isaac 
475336447a5eSToby Isaac   Level: developer
475436447a5eSToby Isaac 
475536447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
475636447a5eSToby Isaac @*/
475736447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
475836447a5eSToby Isaac {
475936447a5eSToby Isaac   DM             cdm;
476036447a5eSToby Isaac   PetscSection   coordSection;
476136447a5eSToby Isaac   PetscInt       cStart, cEnd, c, sStart, sEnd, dof;
476236447a5eSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
476336447a5eSToby Isaac   PetscErrorCode ierr;
476436447a5eSToby Isaac 
476536447a5eSToby Isaac   PetscFunctionBegin;
476636447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
476792c9c85fSStefano Zampini   if (!dm->periodic) {
47688b09590cSToby Isaac     *areLocalized = PETSC_FALSE;
47698b09590cSToby Isaac     PetscFunctionReturn(0);
47708b09590cSToby Isaac   }
477136447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
477236447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
477336447a5eSToby Isaac   {
477436447a5eSToby Isaac     PetscBool isplex;
477536447a5eSToby Isaac 
477636447a5eSToby Isaac     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
477736447a5eSToby Isaac     if (isplex) {
477836447a5eSToby Isaac       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
477936447a5eSToby Isaac     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
478036447a5eSToby Isaac   }
478136447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
478236447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
47835a42012cSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_FALSE;
478436447a5eSToby Isaac   for (c = cStart; c < cEnd; ++c) {
478536447a5eSToby Isaac     if (c < sStart || c >= sEnd) {
478636447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
478736447a5eSToby Isaac       break;
478836447a5eSToby Isaac     }
478936447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
47905a42012cSToby Isaac     if (dof) {
47915a42012cSToby Isaac       alreadyLocalized = PETSC_TRUE;
479236447a5eSToby Isaac       break;
479336447a5eSToby Isaac     }
479436447a5eSToby Isaac   }
47955a42012cSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
479636447a5eSToby Isaac   *areLocalized = alreadyLocalizedGlobal;
479736447a5eSToby Isaac   PetscFunctionReturn(0);
479836447a5eSToby Isaac }
479936447a5eSToby Isaac 
480036447a5eSToby Isaac 
48012e17dfb7SMatthew G. Knepley /*@
4802492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
48032e17dfb7SMatthew G. Knepley 
48042e17dfb7SMatthew G. Knepley   Input Parameter:
48052e17dfb7SMatthew G. Knepley . dm - The DM
48062e17dfb7SMatthew G. Knepley 
48072e17dfb7SMatthew G. Knepley   Level: developer
48082e17dfb7SMatthew G. Knepley 
48092e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
48102e17dfb7SMatthew G. Knepley @*/
48112e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
48122e17dfb7SMatthew G. Knepley {
48132e17dfb7SMatthew G. Knepley   DM             cdm;
48142e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
48152e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
48163e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
48173e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4818e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
48193e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
48203e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
48212e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
48222e17dfb7SMatthew G. Knepley 
48232e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48242e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
482592c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
4826f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
4827f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
4828f7cbd40bSStefano Zampini 
48292e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
48302e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
48312e17dfb7SMatthew G. Knepley   {
48322e17dfb7SMatthew G. Knepley     PetscBool isplex;
48332e17dfb7SMatthew G. Knepley 
48342e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
48352e17dfb7SMatthew G. Knepley     if (isplex) {
48362e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
48373e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
483869291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
48393e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
48403e922f36SToby Isaac       newStart = vStart;
48413e922f36SToby Isaac       newEnd   = vEnd;
48423e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
48433e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
48443e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
48453e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
48463e922f36SToby Isaac       }
48472e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
48482e17dfb7SMatthew G. Knepley   }
48492e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
485043eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
48512e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
48523e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
4853e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
48543e922f36SToby Isaac 
48552e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
48562e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
48572e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
48582e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
48593e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
48603e922f36SToby Isaac 
486169291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
48623e922f36SToby Isaac   localized = &anchor[bs];
48633e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
48643e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
48653e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
48663e922f36SToby Isaac 
48673e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
48683e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
48693e922f36SToby Isaac       PetscInt     b;
48703e922f36SToby Isaac 
48713e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
48723e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48733e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
48743e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
48753e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
48763e922f36SToby Isaac         for (b = 0; b < bs; b++) {
48773e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
48783e922f36SToby Isaac         }
48793e922f36SToby Isaac         if (b < bs) break;
48803e922f36SToby Isaac       }
48813e922f36SToby Isaac       if (d < dof/bs) {
48823e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
48833e922f36SToby Isaac           PetscInt cdof;
48843e922f36SToby Isaac 
48853e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
48863e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
48873e922f36SToby Isaac         }
48883e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
48893e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
48903e922f36SToby Isaac       }
48913e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48923e922f36SToby Isaac     }
48933e922f36SToby Isaac   }
48943e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
48953e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
489669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
48973e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
489869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
48993e922f36SToby Isaac     PetscFunctionReturn(0);
49003e922f36SToby Isaac   }
49012e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
49022e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
49032e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
49042e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
49052e17dfb7SMatthew G. Knepley   }
49062e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
49072e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
4908c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
49092e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
49102e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
49112e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
49122e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
4913c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
49142e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
49152e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
49162e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
49172e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
49182e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
49192e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
49202e17dfb7SMatthew G. Knepley   }
49213e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
49223e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
49233e922f36SToby Isaac 
49242e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
49252e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
49263e922f36SToby Isaac       PetscInt     b, cdof;
49272e17dfb7SMatthew G. Knepley 
49283e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
49293e922f36SToby Isaac       if (!cdof) continue;
49302e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49312e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
49322e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
49332e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
49342e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49352e17dfb7SMatthew G. Knepley     }
49363e922f36SToby Isaac   }
493769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
493869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
4939c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
49402e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
49412e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
49422e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
49432e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
49442e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
49452e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
49462e17dfb7SMatthew G. Knepley }
49472e17dfb7SMatthew G. Knepley 
4948e87bb0d3SMatthew G Knepley /*@
49493a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
4950e87bb0d3SMatthew G Knepley 
49513a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
4952e87bb0d3SMatthew G Knepley 
4953e87bb0d3SMatthew G Knepley   Input Parameters:
4954e87bb0d3SMatthew G Knepley + dm - The DM
49553a93e3b7SToby Isaac . v - The Vec of points
495662a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
49573a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
4958e87bb0d3SMatthew G Knepley 
495961e3bb9bSMatthew G Knepley   Output Parameter:
496062a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
496162a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
49623a93e3b7SToby Isaac 
4963e87bb0d3SMatthew G Knepley 
4964e87bb0d3SMatthew G Knepley   Level: developer
496561e3bb9bSMatthew G Knepley 
496662a38674SMatthew G. Knepley   Notes:
49673a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
496862a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
49693a93e3b7SToby Isaac 
49703a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
497162a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
49723a93e3b7SToby Isaac 
49733a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
49743a93e3b7SToby Isaac 
497562a38674SMatthew G. Knepley $    const PetscSFNode *cells;
497662a38674SMatthew G. Knepley $    PetscInt           nFound;
497762a38674SMatthew G. Knepley $    const PetscSFNode *found;
497862a38674SMatthew G. Knepley $
497962a38674SMatthew G. Knepley $    PetscSFGetGraph(cells,NULL,&nFound,&found,&cells);
49803a93e3b7SToby Isaac 
49813a93e3b7SToby 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
49823a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
49833a93e3b7SToby Isaac 
498461e3bb9bSMatthew G Knepley .keywords: point location, mesh
498562a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
498661e3bb9bSMatthew G Knepley @*/
498762a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
4988e87bb0d3SMatthew G Knepley {
4989735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4990735aa83eSMatthew G Knepley 
4991e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4992e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4993e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4994e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
49953a93e3b7SToby Isaac   if (*cellSF) {
49963a93e3b7SToby Isaac     PetscMPIInt result;
49973a93e3b7SToby Isaac 
4998e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
4999a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
50003a93e3b7SToby 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");
5001e0fc9d1bSMatthew G. Knepley   } else {
50023a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
50033a93e3b7SToby Isaac   }
500447a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5005735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
500662a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
500782f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
500847a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5009e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
5010e87bb0d3SMatthew G Knepley }
501114f150ffSMatthew G. Knepley 
5012f4d763aaSMatthew G. Knepley /*@
5013f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
5014f4d763aaSMatthew G. Knepley 
5015f4d763aaSMatthew G. Knepley   Input Parameter:
5016f4d763aaSMatthew G. Knepley . dm - The original DM
5017f4d763aaSMatthew G. Knepley 
5018f4d763aaSMatthew G. Knepley   Output Parameter:
5019f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
5020f4d763aaSMatthew G. Knepley 
5021f4d763aaSMatthew G. Knepley   Level: intermediate
5022f4d763aaSMatthew G. Knepley 
5023f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
5024f4d763aaSMatthew G. Knepley @*/
502514f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
502614f150ffSMatthew G. Knepley {
5027c26acbdeSMatthew G. Knepley   PetscSection   section;
50282d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
502914f150ffSMatthew G. Knepley   PetscErrorCode ierr;
503014f150ffSMatthew G. Knepley 
503114f150ffSMatthew G. Knepley   PetscFunctionBegin;
503214f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
503314f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
5034c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
5035c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5036127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
50372d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
5038c26acbdeSMatthew G. Knepley     *odm = dm;
5039c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
5040c26acbdeSMatthew G. Knepley   }
504114f150ffSMatthew G. Knepley   if (!dm->dmBC) {
50420305aff6SMatthew G. Knepley     PetscDS      ds;
5043c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
504414f150ffSMatthew G. Knepley     PetscSF      sf;
504514f150ffSMatthew G. Knepley 
504614f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
50470305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
50480305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
504914f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
505014f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
505114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
505214f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
505315b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
505414f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
505514f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
505614f150ffSMatthew G. Knepley   }
505714f150ffSMatthew G. Knepley   *odm = dm->dmBC;
505814f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
505914f150ffSMatthew G. Knepley }
5060f4d763aaSMatthew G. Knepley 
5061f4d763aaSMatthew G. Knepley /*@
5062cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5063f4d763aaSMatthew G. Knepley 
5064f4d763aaSMatthew G. Knepley   Input Parameter:
5065f4d763aaSMatthew G. Knepley . dm - The original DM
5066f4d763aaSMatthew G. Knepley 
5067cdb7a50dSMatthew G. Knepley   Output Parameters:
5068cdb7a50dSMatthew G. Knepley + num - The output sequence number
5069cdb7a50dSMatthew G. Knepley - val - The output sequence value
5070f4d763aaSMatthew G. Knepley 
5071f4d763aaSMatthew G. Knepley   Level: intermediate
5072f4d763aaSMatthew G. Knepley 
5073f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5074f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5075f4d763aaSMatthew G. Knepley 
5076f4d763aaSMatthew G. Knepley .seealso: VecView()
5077f4d763aaSMatthew G. Knepley @*/
5078cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5079f4d763aaSMatthew G. Knepley {
5080f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5081f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5082cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5083cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5084f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5085f4d763aaSMatthew G. Knepley }
5086f4d763aaSMatthew G. Knepley 
5087f4d763aaSMatthew G. Knepley /*@
5088cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5089f4d763aaSMatthew G. Knepley 
5090f4d763aaSMatthew G. Knepley   Input Parameters:
5091f4d763aaSMatthew G. Knepley + dm - The original DM
5092cdb7a50dSMatthew G. Knepley . num - The output sequence number
5093cdb7a50dSMatthew G. Knepley - val - The output sequence value
5094f4d763aaSMatthew G. Knepley 
5095f4d763aaSMatthew G. Knepley   Level: intermediate
5096f4d763aaSMatthew G. Knepley 
5097f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5098f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5099f4d763aaSMatthew G. Knepley 
5100f4d763aaSMatthew G. Knepley .seealso: VecView()
5101f4d763aaSMatthew G. Knepley @*/
5102cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5103f4d763aaSMatthew G. Knepley {
5104f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5105f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5106f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5107cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5108cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5109cdb7a50dSMatthew G. Knepley }
5110cdb7a50dSMatthew G. Knepley 
5111cdb7a50dSMatthew G. Knepley /*@C
5112cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5113cdb7a50dSMatthew G. Knepley 
5114cdb7a50dSMatthew G. Knepley   Input Parameters:
5115cdb7a50dSMatthew G. Knepley + dm   - The original DM
5116cdb7a50dSMatthew G. Knepley . name - The sequence name
5117cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5118cdb7a50dSMatthew G. Knepley 
5119cdb7a50dSMatthew G. Knepley   Output Parameter:
5120cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5121cdb7a50dSMatthew G. Knepley 
5122cdb7a50dSMatthew G. Knepley   Level: intermediate
5123cdb7a50dSMatthew G. Knepley 
5124cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5125cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5126cdb7a50dSMatthew G. Knepley 
5127cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5128cdb7a50dSMatthew G. Knepley @*/
5129cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5130cdb7a50dSMatthew G. Knepley {
5131cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5132cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5133cdb7a50dSMatthew G. Knepley 
5134cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5135cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5136cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5137cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5138cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5139cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5140cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5141cdb7a50dSMatthew G. Knepley     PetscScalar value;
5142cdb7a50dSMatthew G. Knepley 
514339d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
51444aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5145cdb7a50dSMatthew G. Knepley #endif
5146cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5147f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5148f4d763aaSMatthew G. Knepley }
51498e4ac7eaSMatthew G. Knepley 
51508e4ac7eaSMatthew G. Knepley /*@
51518e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
51528e4ac7eaSMatthew G. Knepley 
51538e4ac7eaSMatthew G. Knepley   Not collective
51548e4ac7eaSMatthew G. Knepley 
51558e4ac7eaSMatthew G. Knepley   Input Parameter:
51568e4ac7eaSMatthew G. Knepley . dm - The DM
51578e4ac7eaSMatthew G. Knepley 
51588e4ac7eaSMatthew G. Knepley   Output Parameter:
51598e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
51608e4ac7eaSMatthew G. Knepley 
51618e4ac7eaSMatthew G. Knepley   Level: beginner
51628e4ac7eaSMatthew G. Knepley 
51638e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
51648e4ac7eaSMatthew G. Knepley @*/
51658e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
51668e4ac7eaSMatthew G. Knepley {
51678e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
51688e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51698e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
51708e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
51718e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
51728e4ac7eaSMatthew G. Knepley }
51738e4ac7eaSMatthew G. Knepley 
51748e4ac7eaSMatthew G. Knepley /*@
51758e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
51768e4ac7eaSMatthew G. Knepley 
51778e4ac7eaSMatthew G. Knepley   Collective on dm
51788e4ac7eaSMatthew G. Knepley 
51798e4ac7eaSMatthew G. Knepley   Input Parameters:
51808e4ac7eaSMatthew G. Knepley + dm - The DM
51818e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
51828e4ac7eaSMatthew G. Knepley 
51838e4ac7eaSMatthew G. Knepley   Level: beginner
51848e4ac7eaSMatthew G. Knepley 
51858e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
51868e4ac7eaSMatthew G. Knepley @*/
51878e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
51888e4ac7eaSMatthew G. Knepley {
51898e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
51908e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51918e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
51928e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
51938e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
51948e4ac7eaSMatthew G. Knepley }
5195c58f1c22SToby Isaac 
5196c58f1c22SToby Isaac 
5197c58f1c22SToby Isaac /*@C
5198c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5199c58f1c22SToby Isaac 
5200c58f1c22SToby Isaac   Not Collective
5201c58f1c22SToby Isaac 
5202c58f1c22SToby Isaac   Input Parameters:
5203c58f1c22SToby Isaac + dm   - The DM object
5204c58f1c22SToby Isaac - name - The label name
5205c58f1c22SToby Isaac 
5206c58f1c22SToby Isaac   Level: intermediate
5207c58f1c22SToby Isaac 
5208c58f1c22SToby Isaac .keywords: mesh
5209c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5210c58f1c22SToby Isaac @*/
5211c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5212c58f1c22SToby Isaac {
5213c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5214c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5215c58f1c22SToby Isaac   PetscErrorCode ierr;
5216c58f1c22SToby Isaac 
5217c58f1c22SToby Isaac   PetscFunctionBegin;
5218c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5219c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5220c58f1c22SToby Isaac   while (next) {
5221c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5222c58f1c22SToby Isaac     if (flg) break;
5223c58f1c22SToby Isaac     next = next->next;
5224c58f1c22SToby Isaac   }
5225c58f1c22SToby Isaac   if (!flg) {
5226c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5227c58f1c22SToby Isaac 
5228c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5229c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5230c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5231c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5232c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5233c58f1c22SToby Isaac   }
5234c58f1c22SToby Isaac   PetscFunctionReturn(0);
5235c58f1c22SToby Isaac }
5236c58f1c22SToby Isaac 
5237c58f1c22SToby Isaac /*@C
5238c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5239c58f1c22SToby Isaac 
5240c58f1c22SToby Isaac   Not Collective
5241c58f1c22SToby Isaac 
5242c58f1c22SToby Isaac   Input Parameters:
5243c58f1c22SToby Isaac + dm   - The DM object
5244c58f1c22SToby Isaac . name - The label name
5245c58f1c22SToby Isaac - point - The mesh point
5246c58f1c22SToby Isaac 
5247c58f1c22SToby Isaac   Output Parameter:
5248c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5249c58f1c22SToby Isaac 
5250c58f1c22SToby Isaac   Level: beginner
5251c58f1c22SToby Isaac 
5252c58f1c22SToby Isaac .keywords: mesh
5253c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5254c58f1c22SToby Isaac @*/
5255c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5256c58f1c22SToby Isaac {
5257c58f1c22SToby Isaac   DMLabel        label;
5258c58f1c22SToby Isaac   PetscErrorCode ierr;
5259c58f1c22SToby Isaac 
5260c58f1c22SToby Isaac   PetscFunctionBegin;
5261c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5262c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5263c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
526413903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5265c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5266c58f1c22SToby Isaac   PetscFunctionReturn(0);
5267c58f1c22SToby Isaac }
5268c58f1c22SToby Isaac 
5269c58f1c22SToby Isaac /*@C
5270c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5271c58f1c22SToby Isaac 
5272c58f1c22SToby Isaac   Not Collective
5273c58f1c22SToby Isaac 
5274c58f1c22SToby Isaac   Input Parameters:
5275c58f1c22SToby Isaac + dm   - The DM object
5276c58f1c22SToby Isaac . name - The label name
5277c58f1c22SToby Isaac . point - The mesh point
5278c58f1c22SToby Isaac - value - The label value for this point
5279c58f1c22SToby Isaac 
5280c58f1c22SToby Isaac   Output Parameter:
5281c58f1c22SToby Isaac 
5282c58f1c22SToby Isaac   Level: beginner
5283c58f1c22SToby Isaac 
5284c58f1c22SToby Isaac .keywords: mesh
5285c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5286c58f1c22SToby Isaac @*/
5287c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5288c58f1c22SToby Isaac {
5289c58f1c22SToby Isaac   DMLabel        label;
5290c58f1c22SToby Isaac   PetscErrorCode ierr;
5291c58f1c22SToby Isaac 
5292c58f1c22SToby Isaac   PetscFunctionBegin;
5293c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5294c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5295c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5296c58f1c22SToby Isaac   if (!label) {
5297c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5298c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5299c58f1c22SToby Isaac   }
5300c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5301c58f1c22SToby Isaac   PetscFunctionReturn(0);
5302c58f1c22SToby Isaac }
5303c58f1c22SToby Isaac 
5304c58f1c22SToby Isaac /*@C
5305c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5306c58f1c22SToby Isaac 
5307c58f1c22SToby Isaac   Not Collective
5308c58f1c22SToby Isaac 
5309c58f1c22SToby Isaac   Input Parameters:
5310c58f1c22SToby Isaac + dm   - The DM object
5311c58f1c22SToby Isaac . name - The label name
5312c58f1c22SToby Isaac . point - The mesh point
5313c58f1c22SToby Isaac - value - The label value for this point
5314c58f1c22SToby Isaac 
5315c58f1c22SToby Isaac   Output Parameter:
5316c58f1c22SToby Isaac 
5317c58f1c22SToby Isaac   Level: beginner
5318c58f1c22SToby Isaac 
5319c58f1c22SToby Isaac .keywords: mesh
5320c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5321c58f1c22SToby Isaac @*/
5322c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5323c58f1c22SToby Isaac {
5324c58f1c22SToby Isaac   DMLabel        label;
5325c58f1c22SToby Isaac   PetscErrorCode ierr;
5326c58f1c22SToby Isaac 
5327c58f1c22SToby Isaac   PetscFunctionBegin;
5328c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5329c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5330c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5331c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5332c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5333c58f1c22SToby Isaac   PetscFunctionReturn(0);
5334c58f1c22SToby Isaac }
5335c58f1c22SToby Isaac 
5336c58f1c22SToby Isaac /*@C
5337c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5338c58f1c22SToby Isaac 
5339c58f1c22SToby Isaac   Not Collective
5340c58f1c22SToby Isaac 
5341c58f1c22SToby Isaac   Input Parameters:
5342c58f1c22SToby Isaac + dm   - The DM object
5343c58f1c22SToby Isaac - name - The label name
5344c58f1c22SToby Isaac 
5345c58f1c22SToby Isaac   Output Parameter:
5346c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5347c58f1c22SToby Isaac 
5348c58f1c22SToby Isaac   Level: beginner
5349c58f1c22SToby Isaac 
5350c58f1c22SToby Isaac .keywords: mesh
5351c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5352c58f1c22SToby Isaac @*/
5353c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5354c58f1c22SToby Isaac {
5355c58f1c22SToby Isaac   DMLabel        label;
5356c58f1c22SToby Isaac   PetscErrorCode ierr;
5357c58f1c22SToby Isaac 
5358c58f1c22SToby Isaac   PetscFunctionBegin;
5359c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5360c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5361c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5362c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5363c58f1c22SToby Isaac   *size = 0;
5364c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5365c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5366c58f1c22SToby Isaac   PetscFunctionReturn(0);
5367c58f1c22SToby Isaac }
5368c58f1c22SToby Isaac 
5369c58f1c22SToby Isaac /*@C
5370c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5371c58f1c22SToby Isaac 
5372c58f1c22SToby Isaac   Not Collective
5373c58f1c22SToby Isaac 
5374c58f1c22SToby Isaac   Input Parameters:
5375c58f1c22SToby Isaac + mesh - The DM object
5376c58f1c22SToby Isaac - name - The label name
5377c58f1c22SToby Isaac 
5378c58f1c22SToby Isaac   Output Parameter:
5379c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5380c58f1c22SToby Isaac 
5381c58f1c22SToby Isaac   Level: beginner
5382c58f1c22SToby Isaac 
5383c58f1c22SToby Isaac .keywords: mesh
5384c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5385c58f1c22SToby Isaac @*/
5386c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5387c58f1c22SToby Isaac {
5388c58f1c22SToby Isaac   DMLabel        label;
5389c58f1c22SToby Isaac   PetscErrorCode ierr;
5390c58f1c22SToby Isaac 
5391c58f1c22SToby Isaac   PetscFunctionBegin;
5392c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5393c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5394c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5395c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5396c58f1c22SToby Isaac   *ids = NULL;
5397dab2e251SBlaise Bourdin  if (label) {
5398c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5399dab2e251SBlaise Bourdin   } else {
5400dab2e251SBlaise Bourdin     /* returning an empty IS */
5401dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
5402dab2e251SBlaise Bourdin   }
5403c58f1c22SToby Isaac   PetscFunctionReturn(0);
5404c58f1c22SToby Isaac }
5405c58f1c22SToby Isaac 
5406c58f1c22SToby Isaac /*@C
5407c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5408c58f1c22SToby Isaac 
5409c58f1c22SToby Isaac   Not Collective
5410c58f1c22SToby Isaac 
5411c58f1c22SToby Isaac   Input Parameters:
5412c58f1c22SToby Isaac + dm - The DM object
5413c58f1c22SToby Isaac . name - The label name
5414c58f1c22SToby Isaac - value - The stratum value
5415c58f1c22SToby Isaac 
5416c58f1c22SToby Isaac   Output Parameter:
5417c58f1c22SToby Isaac . size - The stratum size
5418c58f1c22SToby Isaac 
5419c58f1c22SToby Isaac   Level: beginner
5420c58f1c22SToby Isaac 
5421c58f1c22SToby Isaac .keywords: mesh
5422c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5423c58f1c22SToby Isaac @*/
5424c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5425c58f1c22SToby Isaac {
5426c58f1c22SToby Isaac   DMLabel        label;
5427c58f1c22SToby Isaac   PetscErrorCode ierr;
5428c58f1c22SToby Isaac 
5429c58f1c22SToby Isaac   PetscFunctionBegin;
5430c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5431c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5432c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5433c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5434c58f1c22SToby Isaac   *size = 0;
5435c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5436c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5437c58f1c22SToby Isaac   PetscFunctionReturn(0);
5438c58f1c22SToby Isaac }
5439c58f1c22SToby Isaac 
5440c58f1c22SToby Isaac /*@C
5441c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5442c58f1c22SToby Isaac 
5443c58f1c22SToby Isaac   Not Collective
5444c58f1c22SToby Isaac 
5445c58f1c22SToby Isaac   Input Parameters:
5446c58f1c22SToby Isaac + dm - The DM object
5447c58f1c22SToby Isaac . name - The label name
5448c58f1c22SToby Isaac - value - The stratum value
5449c58f1c22SToby Isaac 
5450c58f1c22SToby Isaac   Output Parameter:
5451c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5452c58f1c22SToby Isaac 
5453c58f1c22SToby Isaac   Level: beginner
5454c58f1c22SToby Isaac 
5455c58f1c22SToby Isaac .keywords: mesh
5456c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5457c58f1c22SToby Isaac @*/
5458c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5459c58f1c22SToby Isaac {
5460c58f1c22SToby Isaac   DMLabel        label;
5461c58f1c22SToby Isaac   PetscErrorCode ierr;
5462c58f1c22SToby Isaac 
5463c58f1c22SToby Isaac   PetscFunctionBegin;
5464c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5465c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5466c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5467c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5468c58f1c22SToby Isaac   *points = NULL;
5469c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5470c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5471c58f1c22SToby Isaac   PetscFunctionReturn(0);
5472c58f1c22SToby Isaac }
5473c58f1c22SToby Isaac 
54744de306b1SToby Isaac /*@C
54754de306b1SToby Isaac   DMGetStratumIS - Set the points in a label stratum
54764de306b1SToby Isaac 
54774de306b1SToby Isaac   Not Collective
54784de306b1SToby Isaac 
54794de306b1SToby Isaac   Input Parameters:
54804de306b1SToby Isaac + dm - The DM object
54814de306b1SToby Isaac . name - The label name
54824de306b1SToby Isaac . value - The stratum value
54834de306b1SToby Isaac - points - The stratum points
54844de306b1SToby Isaac 
54854de306b1SToby Isaac   Level: beginner
54864de306b1SToby Isaac 
54874de306b1SToby Isaac .keywords: mesh
54884de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
54894de306b1SToby Isaac @*/
54904de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
54914de306b1SToby Isaac {
54924de306b1SToby Isaac   DMLabel        label;
54934de306b1SToby Isaac   PetscErrorCode ierr;
54944de306b1SToby Isaac 
54954de306b1SToby Isaac   PetscFunctionBegin;
54964de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54974de306b1SToby Isaac   PetscValidCharPointer(name, 2);
54984de306b1SToby Isaac   PetscValidPointer(points, 4);
54994de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
55004de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
55014de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
55024de306b1SToby Isaac   PetscFunctionReturn(0);
55034de306b1SToby Isaac }
55044de306b1SToby Isaac 
5505c58f1c22SToby Isaac /*@C
5506c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5507c58f1c22SToby Isaac 
5508c58f1c22SToby Isaac   Not Collective
5509c58f1c22SToby Isaac 
5510c58f1c22SToby Isaac   Input Parameters:
5511c58f1c22SToby Isaac + dm   - The DM object
5512c58f1c22SToby Isaac . name - The label name
5513c58f1c22SToby Isaac - value - The label value for this point
5514c58f1c22SToby Isaac 
5515c58f1c22SToby Isaac   Output Parameter:
5516c58f1c22SToby Isaac 
5517c58f1c22SToby Isaac   Level: beginner
5518c58f1c22SToby Isaac 
5519c58f1c22SToby Isaac .keywords: mesh
5520c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5521c58f1c22SToby Isaac @*/
5522c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5523c58f1c22SToby Isaac {
5524c58f1c22SToby Isaac   DMLabel        label;
5525c58f1c22SToby Isaac   PetscErrorCode ierr;
5526c58f1c22SToby Isaac 
5527c58f1c22SToby Isaac   PetscFunctionBegin;
5528c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5529c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5530c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5531c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5532c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5533c58f1c22SToby Isaac   PetscFunctionReturn(0);
5534c58f1c22SToby Isaac }
5535c58f1c22SToby Isaac 
5536c58f1c22SToby Isaac /*@
5537c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5538c58f1c22SToby Isaac 
5539c58f1c22SToby Isaac   Not Collective
5540c58f1c22SToby Isaac 
5541c58f1c22SToby Isaac   Input Parameter:
5542c58f1c22SToby Isaac . dm   - The DM object
5543c58f1c22SToby Isaac 
5544c58f1c22SToby Isaac   Output Parameter:
5545c58f1c22SToby Isaac . numLabels - the number of Labels
5546c58f1c22SToby Isaac 
5547c58f1c22SToby Isaac   Level: intermediate
5548c58f1c22SToby Isaac 
5549c58f1c22SToby Isaac .keywords: mesh
5550c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5551c58f1c22SToby Isaac @*/
5552c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5553c58f1c22SToby Isaac {
5554c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5555c58f1c22SToby Isaac   PetscInt  n    = 0;
5556c58f1c22SToby Isaac 
5557c58f1c22SToby Isaac   PetscFunctionBegin;
5558c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5559c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5560c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5561c58f1c22SToby Isaac   *numLabels = n;
5562c58f1c22SToby Isaac   PetscFunctionReturn(0);
5563c58f1c22SToby Isaac }
5564c58f1c22SToby Isaac 
5565c58f1c22SToby Isaac /*@C
5566c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5567c58f1c22SToby Isaac 
5568c58f1c22SToby Isaac   Not Collective
5569c58f1c22SToby Isaac 
5570c58f1c22SToby Isaac   Input Parameters:
5571c58f1c22SToby Isaac + dm - The DM object
5572c58f1c22SToby Isaac - n  - the label number
5573c58f1c22SToby Isaac 
5574c58f1c22SToby Isaac   Output Parameter:
5575c58f1c22SToby Isaac . name - the label name
5576c58f1c22SToby Isaac 
5577c58f1c22SToby Isaac   Level: intermediate
5578c58f1c22SToby Isaac 
5579c58f1c22SToby Isaac .keywords: mesh
5580c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5581c58f1c22SToby Isaac @*/
5582c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5583c58f1c22SToby Isaac {
5584c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5585c58f1c22SToby Isaac   PetscInt  l    = 0;
5586c58f1c22SToby Isaac 
5587c58f1c22SToby Isaac   PetscFunctionBegin;
5588c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5589c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5590c58f1c22SToby Isaac   while (next) {
5591c58f1c22SToby Isaac     if (l == n) {
5592c58f1c22SToby Isaac       *name = next->label->name;
5593c58f1c22SToby Isaac       PetscFunctionReturn(0);
5594c58f1c22SToby Isaac     }
5595c58f1c22SToby Isaac     ++l;
5596c58f1c22SToby Isaac     next = next->next;
5597c58f1c22SToby Isaac   }
5598c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5599c58f1c22SToby Isaac }
5600c58f1c22SToby Isaac 
5601c58f1c22SToby Isaac /*@C
5602c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5603c58f1c22SToby Isaac 
5604c58f1c22SToby Isaac   Not Collective
5605c58f1c22SToby Isaac 
5606c58f1c22SToby Isaac   Input Parameters:
5607c58f1c22SToby Isaac + dm   - The DM object
5608c58f1c22SToby Isaac - name - The label name
5609c58f1c22SToby Isaac 
5610c58f1c22SToby Isaac   Output Parameter:
5611c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5612c58f1c22SToby Isaac 
5613c58f1c22SToby Isaac   Level: intermediate
5614c58f1c22SToby Isaac 
5615c58f1c22SToby Isaac .keywords: mesh
5616c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5617c58f1c22SToby Isaac @*/
5618c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5619c58f1c22SToby Isaac {
5620c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5621c58f1c22SToby Isaac   PetscErrorCode ierr;
5622c58f1c22SToby Isaac 
5623c58f1c22SToby Isaac   PetscFunctionBegin;
5624c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5625c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5626c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5627c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5628c58f1c22SToby Isaac   while (next) {
5629c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5630c58f1c22SToby Isaac     if (*hasLabel) break;
5631c58f1c22SToby Isaac     next = next->next;
5632c58f1c22SToby Isaac   }
5633c58f1c22SToby Isaac   PetscFunctionReturn(0);
5634c58f1c22SToby Isaac }
5635c58f1c22SToby Isaac 
5636c58f1c22SToby Isaac /*@C
5637c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5638c58f1c22SToby Isaac 
5639c58f1c22SToby Isaac   Not Collective
5640c58f1c22SToby Isaac 
5641c58f1c22SToby Isaac   Input Parameters:
5642c58f1c22SToby Isaac + dm   - The DM object
5643c58f1c22SToby Isaac - name - The label name
5644c58f1c22SToby Isaac 
5645c58f1c22SToby Isaac   Output Parameter:
5646c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5647c58f1c22SToby Isaac 
5648c58f1c22SToby Isaac   Level: intermediate
5649c58f1c22SToby Isaac 
5650c58f1c22SToby Isaac .keywords: mesh
5651c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5652c58f1c22SToby Isaac @*/
5653c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5654c58f1c22SToby Isaac {
5655c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5656c58f1c22SToby Isaac   PetscBool      hasLabel;
5657c58f1c22SToby Isaac   PetscErrorCode ierr;
5658c58f1c22SToby Isaac 
5659c58f1c22SToby Isaac   PetscFunctionBegin;
5660c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5661c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5662c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5663c58f1c22SToby Isaac   *label = NULL;
5664c58f1c22SToby Isaac   while (next) {
5665c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5666c58f1c22SToby Isaac     if (hasLabel) {
5667c58f1c22SToby Isaac       *label = next->label;
5668c58f1c22SToby Isaac       break;
5669c58f1c22SToby Isaac     }
5670c58f1c22SToby Isaac     next = next->next;
5671c58f1c22SToby Isaac   }
5672c58f1c22SToby Isaac   PetscFunctionReturn(0);
5673c58f1c22SToby Isaac }
5674c58f1c22SToby Isaac 
5675c58f1c22SToby Isaac /*@C
5676c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5677c58f1c22SToby Isaac 
5678c58f1c22SToby Isaac   Not Collective
5679c58f1c22SToby Isaac 
5680c58f1c22SToby Isaac   Input Parameters:
5681c58f1c22SToby Isaac + dm - The DM object
5682c58f1c22SToby Isaac - n  - the label number
5683c58f1c22SToby Isaac 
5684c58f1c22SToby Isaac   Output Parameter:
5685c58f1c22SToby Isaac . label - the label
5686c58f1c22SToby Isaac 
5687c58f1c22SToby Isaac   Level: intermediate
5688c58f1c22SToby Isaac 
5689c58f1c22SToby Isaac .keywords: mesh
5690c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5691c58f1c22SToby Isaac @*/
5692c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5693c58f1c22SToby Isaac {
5694c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5695c58f1c22SToby Isaac   PetscInt    l    = 0;
5696c58f1c22SToby Isaac 
5697c58f1c22SToby Isaac   PetscFunctionBegin;
5698c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5699c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5700c58f1c22SToby Isaac   while (next) {
5701c58f1c22SToby Isaac     if (l == n) {
5702c58f1c22SToby Isaac       *label = next->label;
5703c58f1c22SToby Isaac       PetscFunctionReturn(0);
5704c58f1c22SToby Isaac     }
5705c58f1c22SToby Isaac     ++l;
5706c58f1c22SToby Isaac     next = next->next;
5707c58f1c22SToby Isaac   }
5708c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5709c58f1c22SToby Isaac }
5710c58f1c22SToby Isaac 
5711c58f1c22SToby Isaac /*@C
5712c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5713c58f1c22SToby Isaac 
5714c58f1c22SToby Isaac   Not Collective
5715c58f1c22SToby Isaac 
5716c58f1c22SToby Isaac   Input Parameters:
5717c58f1c22SToby Isaac + dm   - The DM object
5718c58f1c22SToby Isaac - label - The DMLabel
5719c58f1c22SToby Isaac 
5720c58f1c22SToby Isaac   Level: developer
5721c58f1c22SToby Isaac 
5722c58f1c22SToby Isaac .keywords: mesh
5723c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5724c58f1c22SToby Isaac @*/
5725c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5726c58f1c22SToby Isaac {
5727c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5728c58f1c22SToby Isaac   PetscBool      hasLabel;
5729c58f1c22SToby Isaac   PetscErrorCode ierr;
5730c58f1c22SToby Isaac 
5731c58f1c22SToby Isaac   PetscFunctionBegin;
5732c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5733c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5734c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5735c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5736c58f1c22SToby Isaac   tmpLabel->label  = label;
5737c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5738c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5739c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5740c58f1c22SToby Isaac   PetscFunctionReturn(0);
5741c58f1c22SToby Isaac }
5742c58f1c22SToby Isaac 
5743c58f1c22SToby Isaac /*@C
5744c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5745c58f1c22SToby Isaac 
5746c58f1c22SToby Isaac   Not Collective
5747c58f1c22SToby Isaac 
5748c58f1c22SToby Isaac   Input Parameters:
5749c58f1c22SToby Isaac + dm   - The DM object
5750c58f1c22SToby Isaac - name - The label name
5751c58f1c22SToby Isaac 
5752c58f1c22SToby Isaac   Output Parameter:
5753c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5754c58f1c22SToby Isaac 
5755c58f1c22SToby Isaac   Level: developer
5756c58f1c22SToby Isaac 
5757c58f1c22SToby Isaac .keywords: mesh
5758c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5759c58f1c22SToby Isaac @*/
5760c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5761c58f1c22SToby Isaac {
5762c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5763c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5764c58f1c22SToby Isaac   PetscBool      hasLabel;
5765c58f1c22SToby Isaac   PetscErrorCode ierr;
5766c58f1c22SToby Isaac 
5767c58f1c22SToby Isaac   PetscFunctionBegin;
5768c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5769c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5770c58f1c22SToby Isaac   *label = NULL;
5771c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5772c58f1c22SToby Isaac   while (next) {
5773c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5774c58f1c22SToby Isaac     if (hasLabel) {
5775c58f1c22SToby Isaac       if (last) last->next       = next->next;
5776c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5777c58f1c22SToby Isaac       next->next = NULL;
5778c58f1c22SToby Isaac       *label     = next->label;
5779c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5780c58f1c22SToby Isaac       if (hasLabel) {
5781c58f1c22SToby Isaac         dm->depthLabel = NULL;
5782c58f1c22SToby Isaac       }
5783c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5784c58f1c22SToby Isaac       break;
5785c58f1c22SToby Isaac     }
5786c58f1c22SToby Isaac     last = next;
5787c58f1c22SToby Isaac     next = next->next;
5788c58f1c22SToby Isaac   }
5789c58f1c22SToby Isaac   PetscFunctionReturn(0);
5790c58f1c22SToby Isaac }
5791c58f1c22SToby Isaac 
5792c58f1c22SToby Isaac /*@C
5793c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5794c58f1c22SToby Isaac 
5795c58f1c22SToby Isaac   Not Collective
5796c58f1c22SToby Isaac 
5797c58f1c22SToby Isaac   Input Parameters:
5798c58f1c22SToby Isaac + dm   - The DM object
5799c58f1c22SToby Isaac - name - The label name
5800c58f1c22SToby Isaac 
5801c58f1c22SToby Isaac   Output Parameter:
5802c58f1c22SToby Isaac . output - The flag for output
5803c58f1c22SToby Isaac 
5804c58f1c22SToby Isaac   Level: developer
5805c58f1c22SToby Isaac 
5806c58f1c22SToby Isaac .keywords: mesh
5807c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5808c58f1c22SToby Isaac @*/
5809c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5810c58f1c22SToby Isaac {
5811c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5812c58f1c22SToby Isaac   PetscErrorCode ierr;
5813c58f1c22SToby Isaac 
5814c58f1c22SToby Isaac   PetscFunctionBegin;
5815c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5816c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5817c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5818c58f1c22SToby Isaac   while (next) {
5819c58f1c22SToby Isaac     PetscBool flg;
5820c58f1c22SToby Isaac 
5821c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5822c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5823c58f1c22SToby Isaac     next = next->next;
5824c58f1c22SToby Isaac   }
5825c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5826c58f1c22SToby Isaac }
5827c58f1c22SToby Isaac 
5828c58f1c22SToby Isaac /*@C
5829c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5830c58f1c22SToby Isaac 
5831c58f1c22SToby Isaac   Not Collective
5832c58f1c22SToby Isaac 
5833c58f1c22SToby Isaac   Input Parameters:
5834c58f1c22SToby Isaac + dm     - The DM object
5835c58f1c22SToby Isaac . name   - The label name
5836c58f1c22SToby Isaac - output - The flag for output
5837c58f1c22SToby Isaac 
5838c58f1c22SToby Isaac   Level: developer
5839c58f1c22SToby Isaac 
5840c58f1c22SToby Isaac .keywords: mesh
5841c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5842c58f1c22SToby Isaac @*/
5843c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5844c58f1c22SToby Isaac {
5845c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5846c58f1c22SToby Isaac   PetscErrorCode ierr;
5847c58f1c22SToby Isaac 
5848c58f1c22SToby Isaac   PetscFunctionBegin;
5849c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5850c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5851c58f1c22SToby Isaac   while (next) {
5852c58f1c22SToby Isaac     PetscBool flg;
5853c58f1c22SToby Isaac 
5854c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5855c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5856c58f1c22SToby Isaac     next = next->next;
5857c58f1c22SToby Isaac   }
5858c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5859c58f1c22SToby Isaac }
5860c58f1c22SToby Isaac 
5861c58f1c22SToby Isaac 
5862c58f1c22SToby Isaac /*@
5863c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5864c58f1c22SToby Isaac 
5865c58f1c22SToby Isaac   Collective on DM
5866c58f1c22SToby Isaac 
5867c58f1c22SToby Isaac   Input Parameter:
58682e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5869c58f1c22SToby Isaac 
5870c58f1c22SToby Isaac   Output Parameter:
58712e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5872c58f1c22SToby Isaac 
5873c58f1c22SToby Isaac   Level: intermediate
5874c58f1c22SToby Isaac 
5875c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5876c58f1c22SToby Isaac 
5877c58f1c22SToby Isaac .keywords: mesh
5878c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5879c58f1c22SToby Isaac @*/
5880c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5881c58f1c22SToby Isaac {
5882c58f1c22SToby Isaac   PetscInt       numLabels, l;
5883c58f1c22SToby Isaac   PetscErrorCode ierr;
5884c58f1c22SToby Isaac 
5885c58f1c22SToby Isaac   PetscFunctionBegin;
5886c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5887c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5888c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5889c58f1c22SToby Isaac     DMLabel     label, labelNew;
5890c58f1c22SToby Isaac     const char *name;
5891c58f1c22SToby Isaac     PetscBool   flg;
5892c58f1c22SToby Isaac 
5893c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5894c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5895c58f1c22SToby Isaac     if (flg) continue;
5896c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5897c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5898c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5899c58f1c22SToby Isaac   }
5900c58f1c22SToby Isaac   PetscFunctionReturn(0);
5901c58f1c22SToby Isaac }
5902a8fb8f29SToby Isaac 
5903a8fb8f29SToby Isaac /*@
5904a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5905a8fb8f29SToby Isaac 
5906a8fb8f29SToby Isaac   Input Parameter:
5907a8fb8f29SToby Isaac . dm - The DM object
5908a8fb8f29SToby Isaac 
5909a8fb8f29SToby Isaac   Output Parameter:
5910a8fb8f29SToby Isaac . cdm - The coarse DM
5911a8fb8f29SToby Isaac 
5912a8fb8f29SToby Isaac   Level: intermediate
5913a8fb8f29SToby Isaac 
5914a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5915a8fb8f29SToby Isaac @*/
5916a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5917a8fb8f29SToby Isaac {
5918a8fb8f29SToby Isaac   PetscFunctionBegin;
5919a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5920a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5921a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5922a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5923a8fb8f29SToby Isaac }
5924a8fb8f29SToby Isaac 
5925a8fb8f29SToby Isaac /*@
5926a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5927a8fb8f29SToby Isaac 
5928a8fb8f29SToby Isaac   Input Parameters:
5929a8fb8f29SToby Isaac + dm - The DM object
5930a8fb8f29SToby Isaac - cdm - The coarse DM
5931a8fb8f29SToby Isaac 
5932a8fb8f29SToby Isaac   Level: intermediate
5933a8fb8f29SToby Isaac 
5934a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5935a8fb8f29SToby Isaac @*/
5936a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5937a8fb8f29SToby Isaac {
5938a8fb8f29SToby Isaac   PetscErrorCode ierr;
5939a8fb8f29SToby Isaac 
5940a8fb8f29SToby Isaac   PetscFunctionBegin;
5941a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5942a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5943a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5944a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5945a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5946a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5947a8fb8f29SToby Isaac }
5948a8fb8f29SToby Isaac 
594988bdff64SToby Isaac /*@
595088bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
595188bdff64SToby Isaac 
595288bdff64SToby Isaac   Input Parameter:
595388bdff64SToby Isaac . dm - The DM object
595488bdff64SToby Isaac 
595588bdff64SToby Isaac   Output Parameter:
595688bdff64SToby Isaac . fdm - The fine DM
595788bdff64SToby Isaac 
595888bdff64SToby Isaac   Level: intermediate
595988bdff64SToby Isaac 
596088bdff64SToby Isaac .seealso: DMSetFineDM()
596188bdff64SToby Isaac @*/
596288bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
596388bdff64SToby Isaac {
596488bdff64SToby Isaac   PetscFunctionBegin;
596588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
596688bdff64SToby Isaac   PetscValidPointer(fdm, 2);
596788bdff64SToby Isaac   *fdm = dm->fineMesh;
596888bdff64SToby Isaac   PetscFunctionReturn(0);
596988bdff64SToby Isaac }
597088bdff64SToby Isaac 
597188bdff64SToby Isaac /*@
597288bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
597388bdff64SToby Isaac 
597488bdff64SToby Isaac   Input Parameters:
597588bdff64SToby Isaac + dm - The DM object
597688bdff64SToby Isaac - fdm - The fine DM
597788bdff64SToby Isaac 
597888bdff64SToby Isaac   Level: intermediate
597988bdff64SToby Isaac 
598088bdff64SToby Isaac .seealso: DMGetFineDM()
598188bdff64SToby Isaac @*/
598288bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
598388bdff64SToby Isaac {
598488bdff64SToby Isaac   PetscErrorCode ierr;
598588bdff64SToby Isaac 
598688bdff64SToby Isaac   PetscFunctionBegin;
598788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
598888bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
598988bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
599088bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
599188bdff64SToby Isaac   dm->fineMesh = fdm;
599288bdff64SToby Isaac   PetscFunctionReturn(0);
599388bdff64SToby Isaac }
599488bdff64SToby Isaac 
5995a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5996a6ba4734SToby Isaac 
5997a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5998a6ba4734SToby Isaac {
5999a6ba4734SToby Isaac   PetscErrorCode ierr;
6000a6ba4734SToby Isaac 
6001a6ba4734SToby Isaac   PetscFunctionBegin;
6002e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
6003a6ba4734SToby Isaac   PetscFunctionReturn(0);
6004a6ba4734SToby Isaac }
6005a6ba4734SToby Isaac 
6006a6ba4734SToby Isaac /*@C
6007a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
6008a6ba4734SToby Isaac 
6009a6ba4734SToby Isaac   Input Parameters:
60104c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
6011f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6012a6ba4734SToby Isaac . name        - The BC name
6013a6ba4734SToby Isaac . labelname   - The label defining constrained points
6014a6ba4734SToby Isaac . field       - The field to constrain
6015a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6016a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6017a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6018a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6019a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6020a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6021a6ba4734SToby Isaac 
6022a6ba4734SToby Isaac   Options Database Keys:
6023a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6024a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6025a6ba4734SToby Isaac 
6026a6ba4734SToby Isaac   Level: developer
6027a6ba4734SToby Isaac 
6028a6ba4734SToby Isaac .seealso: DMGetBoundary()
6029a6ba4734SToby Isaac @*/
6030a30ec4eaSSatish 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)
6031a6ba4734SToby Isaac {
6032a6ba4734SToby Isaac   PetscErrorCode ierr;
6033a6ba4734SToby Isaac 
6034a6ba4734SToby Isaac   PetscFunctionBegin;
6035a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6036f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
6037a6ba4734SToby Isaac   PetscFunctionReturn(0);
6038a6ba4734SToby Isaac }
6039a6ba4734SToby Isaac 
6040a6ba4734SToby Isaac /*@
6041a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6042a6ba4734SToby Isaac 
6043a6ba4734SToby Isaac   Input Parameters:
6044a6ba4734SToby Isaac . dm - The mesh object
6045a6ba4734SToby Isaac 
6046a6ba4734SToby Isaac   Output Parameters:
6047a6ba4734SToby Isaac . numBd - The number of BC
6048a6ba4734SToby Isaac 
6049a6ba4734SToby Isaac   Level: intermediate
6050a6ba4734SToby Isaac 
6051a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6052a6ba4734SToby Isaac @*/
6053a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6054a6ba4734SToby Isaac {
605558ebd649SToby Isaac   PetscErrorCode ierr;
6056a6ba4734SToby Isaac 
6057a6ba4734SToby Isaac   PetscFunctionBegin;
6058a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
605958ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
6060a6ba4734SToby Isaac   PetscFunctionReturn(0);
6061a6ba4734SToby Isaac }
6062a6ba4734SToby Isaac 
6063a6ba4734SToby Isaac /*@C
60641c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
6065a6ba4734SToby Isaac 
6066a6ba4734SToby Isaac   Input Parameters:
6067a6ba4734SToby Isaac + dm          - The mesh object
6068a6ba4734SToby Isaac - bd          - The BC number
6069a6ba4734SToby Isaac 
6070a6ba4734SToby Isaac   Output Parameters:
6071f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6072a6ba4734SToby Isaac . name        - The BC name
6073a6ba4734SToby Isaac . labelname   - The label defining constrained points
6074a6ba4734SToby Isaac . field       - The field to constrain
6075a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6076a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6077a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6078a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6079a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6080a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6081a6ba4734SToby Isaac 
6082a6ba4734SToby Isaac   Options Database Keys:
6083a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6084a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6085a6ba4734SToby Isaac 
6086a6ba4734SToby Isaac   Level: developer
6087a6ba4734SToby Isaac 
6088a6ba4734SToby Isaac .seealso: DMAddBoundary()
6089a6ba4734SToby Isaac @*/
6090a30ec4eaSSatish 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)
6091a6ba4734SToby Isaac {
609258ebd649SToby Isaac   PetscErrorCode ierr;
6093a6ba4734SToby Isaac 
6094a6ba4734SToby Isaac   PetscFunctionBegin;
6095a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6096f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6097a6ba4734SToby Isaac   PetscFunctionReturn(0);
6098a6ba4734SToby Isaac }
6099a6ba4734SToby Isaac 
6100e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6101e6f8dbb6SToby Isaac {
6102dff059c6SToby Isaac   DMBoundary *lastnext;
6103e6f8dbb6SToby Isaac   DSBoundary dsbound;
6104e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6105e6f8dbb6SToby Isaac 
6106e6f8dbb6SToby Isaac   PetscFunctionBegin;
6107e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
610847a1f5adSToby Isaac   if (dm->boundary) {
610947a1f5adSToby Isaac     DMBoundary next = dm->boundary;
611047a1f5adSToby Isaac 
611147a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
611247a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
611347a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
611447a1f5adSToby Isaac     while (next) {
611547a1f5adSToby Isaac       DMBoundary b = next;
611647a1f5adSToby Isaac 
611747a1f5adSToby Isaac       next = b->next;
611847a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6119a6ba4734SToby Isaac     }
612047a1f5adSToby Isaac     dm->boundary = NULL;
6121a6ba4734SToby Isaac   }
612247a1f5adSToby Isaac 
6123dff059c6SToby Isaac   lastnext = &(dm->boundary);
6124e6f8dbb6SToby Isaac   while (dsbound) {
6125e6f8dbb6SToby Isaac     DMBoundary dmbound;
6126e6f8dbb6SToby Isaac 
6127e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6128e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6129e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6130e6f8dbb6SToby Isaac     if (!dmbound->label) PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);
613147a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6132dff059c6SToby Isaac     *lastnext = dmbound;
6133dff059c6SToby Isaac     lastnext = &(dmbound->next);
6134dff059c6SToby Isaac     dsbound = dsbound->next;
6135a6ba4734SToby Isaac   }
6136a6ba4734SToby Isaac   PetscFunctionReturn(0);
6137a6ba4734SToby Isaac }
6138a6ba4734SToby Isaac 
6139a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6140a6ba4734SToby Isaac {
6141b95f2879SToby Isaac   DMBoundary     b;
6142a6ba4734SToby Isaac   PetscErrorCode ierr;
6143a6ba4734SToby Isaac 
6144a6ba4734SToby Isaac   PetscFunctionBegin;
6145a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6146a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6147a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6148e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6149b95f2879SToby Isaac   b = dm->boundary;
6150a6ba4734SToby Isaac   while (b && !(*isBd)) {
6151e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6152e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
61533424c85cSToby Isaac 
61543424c85cSToby Isaac     if (label) {
6155a6ba4734SToby Isaac       PetscInt i;
6156a6ba4734SToby Isaac 
6157e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6158e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6159a6ba4734SToby Isaac       }
6160a6ba4734SToby Isaac     }
6161a6ba4734SToby Isaac     b = b->next;
6162a6ba4734SToby Isaac   }
6163a6ba4734SToby Isaac   PetscFunctionReturn(0);
6164a6ba4734SToby Isaac }
61654d6f44ffSToby Isaac 
61664d6f44ffSToby Isaac /*@C
61674d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
61684d6f44ffSToby Isaac 
61694d6f44ffSToby Isaac   Input Parameters:
61704d6f44ffSToby Isaac + dm      - The DM
61710709b2feSToby Isaac . time    - The time
61724d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
61734d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
61744d6f44ffSToby Isaac - mode    - The insertion mode for values
61754d6f44ffSToby Isaac 
61764d6f44ffSToby Isaac   Output Parameter:
61774d6f44ffSToby Isaac . X - vector
61784d6f44ffSToby Isaac 
61794d6f44ffSToby Isaac    Calling sequence of func:
61800709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
61814d6f44ffSToby Isaac 
61824d6f44ffSToby Isaac +  dim - The spatial dimension
61834d6f44ffSToby Isaac .  x   - The coordinates
61844d6f44ffSToby Isaac .  Nf  - The number of fields
61854d6f44ffSToby Isaac .  u   - The output field values
61864d6f44ffSToby Isaac -  ctx - optional user-defined function context
61874d6f44ffSToby Isaac 
61884d6f44ffSToby Isaac   Level: developer
61894d6f44ffSToby Isaac 
61902716604bSToby Isaac .seealso: DMComputeL2Diff()
61914d6f44ffSToby Isaac @*/
61920709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
61934d6f44ffSToby Isaac {
61944d6f44ffSToby Isaac   Vec            localX;
61954d6f44ffSToby Isaac   PetscErrorCode ierr;
61964d6f44ffSToby Isaac 
61974d6f44ffSToby Isaac   PetscFunctionBegin;
61984d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61994d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
62000709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62014d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
62024d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
62034d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
62044d6f44ffSToby Isaac   PetscFunctionReturn(0);
62054d6f44ffSToby Isaac }
62064d6f44ffSToby Isaac 
62070709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
62084d6f44ffSToby Isaac {
62094d6f44ffSToby Isaac   PetscErrorCode ierr;
62104d6f44ffSToby Isaac 
62114d6f44ffSToby Isaac   PetscFunctionBegin;
62124d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62134d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
62140918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
62150709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62164d6f44ffSToby Isaac   PetscFunctionReturn(0);
62174d6f44ffSToby Isaac }
62184d6f44ffSToby Isaac 
62192c53366bSMatthew 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)
62202c53366bSMatthew G. Knepley {
62212c53366bSMatthew G. Knepley   Vec            localX;
62222c53366bSMatthew G. Knepley   PetscErrorCode ierr;
62232c53366bSMatthew G. Knepley 
62242c53366bSMatthew G. Knepley   PetscFunctionBegin;
62252c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62262c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
62272c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62282c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
62292c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
62302c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
62312c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
62322c53366bSMatthew G. Knepley }
62332c53366bSMatthew G. Knepley 
62341c531cf8SMatthew 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)
62354d6f44ffSToby Isaac {
62364d6f44ffSToby Isaac   PetscErrorCode ierr;
62374d6f44ffSToby Isaac 
62384d6f44ffSToby Isaac   PetscFunctionBegin;
62394d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62404d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
62410918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
62421c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62434d6f44ffSToby Isaac   PetscFunctionReturn(0);
62444d6f44ffSToby Isaac }
62452716604bSToby Isaac 
62468c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
62478c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
62488c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
62498c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6250191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
62518c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
62528c6c5593SMatthew G. Knepley {
62538c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
62548c6c5593SMatthew G. Knepley 
62558c6c5593SMatthew G. Knepley   PetscFunctionBegin;
62568c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62578c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
62588c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
62590918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
62608c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
62618c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
62628c6c5593SMatthew G. Knepley }
62638c6c5593SMatthew G. Knepley 
62641c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
62658c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
62668c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
62678c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6268191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
62698c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
62708c6c5593SMatthew G. Knepley {
62718c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
62728c6c5593SMatthew G. Knepley 
62738c6c5593SMatthew G. Knepley   PetscFunctionBegin;
62748c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62758c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
62768c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
62770918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
62781c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
62798c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
62808c6c5593SMatthew G. Knepley }
62818c6c5593SMatthew G. Knepley 
62822716604bSToby Isaac /*@C
62832716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
62842716604bSToby Isaac 
62852716604bSToby Isaac   Input Parameters:
62862716604bSToby Isaac + dm    - The DM
62870709b2feSToby Isaac . time  - The time
62882716604bSToby Isaac . funcs - The functions to evaluate for each field component
62892716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6290574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
62912716604bSToby Isaac 
62922716604bSToby Isaac   Output Parameter:
62932716604bSToby Isaac . diff - The diff ||u - u_h||_2
62942716604bSToby Isaac 
62952716604bSToby Isaac   Level: developer
62962716604bSToby Isaac 
62971189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
62982716604bSToby Isaac @*/
62990709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
63002716604bSToby Isaac {
63012716604bSToby Isaac   PetscErrorCode ierr;
63022716604bSToby Isaac 
63032716604bSToby Isaac   PetscFunctionBegin;
63042716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6305b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
63060918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
63070709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
63082716604bSToby Isaac   PetscFunctionReturn(0);
63092716604bSToby Isaac }
6310b698f381SToby Isaac 
6311b698f381SToby Isaac /*@C
6312b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6313b698f381SToby Isaac 
6314b698f381SToby Isaac   Input Parameters:
6315b698f381SToby Isaac + dm    - The DM
6316b698f381SToby Isaac , time  - The time
6317b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6318b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6319574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
6320b698f381SToby Isaac - n     - The vector to project along
6321b698f381SToby Isaac 
6322b698f381SToby Isaac   Output Parameter:
6323b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6324b698f381SToby Isaac 
6325b698f381SToby Isaac   Level: developer
6326b698f381SToby Isaac 
6327b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6328b698f381SToby Isaac @*/
6329b698f381SToby 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)
6330b698f381SToby Isaac {
6331b698f381SToby Isaac   PetscErrorCode ierr;
6332b698f381SToby Isaac 
6333b698f381SToby Isaac   PetscFunctionBegin;
6334b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6335b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6336b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6337b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6338b698f381SToby Isaac   PetscFunctionReturn(0);
6339b698f381SToby Isaac }
6340b698f381SToby Isaac 
63412a16baeaSToby Isaac /*@C
63422a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
63432a16baeaSToby Isaac 
63442a16baeaSToby Isaac   Input Parameters:
63452a16baeaSToby Isaac + dm    - The DM
63462a16baeaSToby Isaac . time  - The time
63472a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
63482a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6349574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
63502a16baeaSToby Isaac 
63512a16baeaSToby Isaac   Output Parameter:
63522a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
63532a16baeaSToby Isaac 
63542a16baeaSToby Isaac   Level: developer
63552a16baeaSToby Isaac 
63561189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
63572a16baeaSToby Isaac @*/
63581189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
63592a16baeaSToby Isaac {
63602a16baeaSToby Isaac   PetscErrorCode ierr;
63612a16baeaSToby Isaac 
63622a16baeaSToby Isaac   PetscFunctionBegin;
63632a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63642a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
63650918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
63662a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
63672a16baeaSToby Isaac   PetscFunctionReturn(0);
63682a16baeaSToby Isaac }
63692a16baeaSToby Isaac 
6370df0b854cSToby Isaac /*@C
6371df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6372cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6373df0b854cSToby Isaac 
6374df0b854cSToby Isaac   Collective on dm
6375df0b854cSToby Isaac 
6376df0b854cSToby Isaac   Input parameters:
6377df0b854cSToby Isaac + dm - the pre-adaptation DM object
6378a1b0c543SToby Isaac - label - label with the flags
6379df0b854cSToby Isaac 
6380df0b854cSToby Isaac   Output parameters:
63810d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6382df0b854cSToby Isaac 
6383df0b854cSToby Isaac   Level: intermediate
63840d1cd5e0SMatthew G. Knepley 
63850d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6386df0b854cSToby Isaac @*/
63870d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6388df0b854cSToby Isaac {
6389df0b854cSToby Isaac   PetscErrorCode ierr;
6390df0b854cSToby Isaac 
6391df0b854cSToby Isaac   PetscFunctionBegin;
6392df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6393a1b0c543SToby Isaac   PetscValidPointer(label,2);
63940d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
63950d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
63966f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
63970d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
63980d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
63990d1cd5e0SMatthew G. Knepley }
64000d1cd5e0SMatthew G. Knepley 
64010d1cd5e0SMatthew G. Knepley /*@C
64020d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
64030d1cd5e0SMatthew G. Knepley 
64040d1cd5e0SMatthew G. Knepley   Input Parameters:
64050d1cd5e0SMatthew G. Knepley + dm - The DM object
64060d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
64076f25b0d8SLisandro 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_".
64080d1cd5e0SMatthew G. Knepley 
64090d1cd5e0SMatthew G. Knepley   Output Parameter:
64100d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
64110d1cd5e0SMatthew G. Knepley 
64120d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
64130d1cd5e0SMatthew G. Knepley 
64140d1cd5e0SMatthew G. Knepley   Level: advanced
64150d1cd5e0SMatthew G. Knepley 
64160d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
64170d1cd5e0SMatthew G. Knepley @*/
64180d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
64190d1cd5e0SMatthew G. Knepley {
64200d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
64210d1cd5e0SMatthew G. Knepley 
64220d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
64230d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64240d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
64250d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
64260d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
64276f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
64286f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
64290d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6430df0b854cSToby Isaac   PetscFunctionReturn(0);
6431df0b854cSToby Isaac }
6432c4088d22SMatthew G. Knepley 
6433502a2867SDave May /*@C
6434502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6435502a2867SDave May 
6436502a2867SDave May  Not Collective
6437502a2867SDave May 
6438502a2867SDave May  Input Parameter:
6439502a2867SDave May  . dm    - The DM
6440502a2867SDave May 
6441502a2867SDave May  Output Parameter:
6442502a2867SDave May  . nranks - the number of neighbours
6443502a2867SDave May  . ranks - the neighbors ranks
6444502a2867SDave May 
6445502a2867SDave May  Notes:
6446502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6447502a2867SDave May 
6448502a2867SDave May  Level: beginner
6449502a2867SDave May 
6450dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6451502a2867SDave May @*/
6452502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6453502a2867SDave May {
6454502a2867SDave May   PetscErrorCode ierr;
6455502a2867SDave May 
6456502a2867SDave May   PetscFunctionBegin;
6457502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64580918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
6459502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6460502a2867SDave May   PetscFunctionReturn(0);
6461502a2867SDave May }
6462502a2867SDave May 
6463531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6464531c7667SBarry Smith 
6465531c7667SBarry Smith /*
6466531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6467531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6468531c7667SBarry Smith */
6469531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6470531c7667SBarry Smith {
6471531c7667SBarry Smith   PetscErrorCode ierr;
6472531c7667SBarry Smith 
6473531c7667SBarry Smith   PetscFunctionBegin;
6474531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6475531c7667SBarry Smith     Vec x1local;
6476531c7667SBarry Smith     DM  dm;
6477531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6478531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6479531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6480531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6481531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6482531c7667SBarry Smith     x1   = x1local;
6483531c7667SBarry Smith   }
6484531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6485531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6486531c7667SBarry Smith     DM  dm;
6487531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6488531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6489531c7667SBarry Smith   }
6490531c7667SBarry Smith   PetscFunctionReturn(0);
6491531c7667SBarry Smith }
6492531c7667SBarry Smith 
6493531c7667SBarry Smith /*@
6494531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6495531c7667SBarry Smith 
6496531c7667SBarry Smith     Input Parameter:
6497531c7667SBarry Smith .    coloring - the MatFDColoring object
6498531c7667SBarry Smith 
6499531c7667SBarry Smith     Developer Notes: this routine exists because the PETSc Mat library does not know about the DM objects
6500531c7667SBarry Smith 
65011b266c99SBarry Smith     Level: advanced
65021b266c99SBarry Smith 
6503531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6504531c7667SBarry Smith @*/
6505531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6506531c7667SBarry Smith {
6507531c7667SBarry Smith   PetscFunctionBegin;
6508531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6509531c7667SBarry Smith   PetscFunctionReturn(0);
6510531c7667SBarry Smith }
6511