xref: /petsc/src/dm/interface/dm.c (revision 0a50eb565eddd4e192871951ca94352b314460e5)
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 
1335*0a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1336*0a50eb56SMatthew G. Knepley {
1337*0a50eb56SMatthew G. Knepley   PetscFunctionBegin;
1338*0a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1339*0a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1340*0a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
1341*0a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
1342*0a50eb56SMatthew G. Knepley }
1343*0a50eb56SMatthew G. Knepley 
13444f3b5142SJed Brown /*@C
13454d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
13464d343eeaSMatthew G Knepley 
13474d343eeaSMatthew G Knepley   Not collective
13484d343eeaSMatthew G Knepley 
13494d343eeaSMatthew G Knepley   Input Parameter:
13504d343eeaSMatthew G Knepley . dm - the DM object
13514d343eeaSMatthew G Knepley 
13524d343eeaSMatthew G Knepley   Output Parameters:
13530298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
13540298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
13550298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
13564d343eeaSMatthew G Knepley 
13574d343eeaSMatthew G Knepley   Level: intermediate
13584d343eeaSMatthew G Knepley 
135921c9b008SJed Brown   Notes:
136021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
136121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
136221c9b008SJed Brown   PetscFree().
136321c9b008SJed Brown 
13644d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
13654d343eeaSMatthew G Knepley @*/
136637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
13674d343eeaSMatthew G Knepley {
136837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
13694d343eeaSMatthew G Knepley   PetscErrorCode ierr;
13704d343eeaSMatthew G Knepley 
13714d343eeaSMatthew G Knepley   PetscFunctionBegin;
13724d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
137369ca1f37SDmitry Karpeev   if (numFields) {
137469ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
137569ca1f37SDmitry Karpeev     *numFields = 0;
137669ca1f37SDmitry Karpeev   }
137737d0c07bSMatthew G Knepley   if (fieldNames) {
137837d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
13790298fd71SBarry Smith     *fieldNames = NULL;
138069ca1f37SDmitry Karpeev   }
138169ca1f37SDmitry Karpeev   if (fields) {
138269ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
13830298fd71SBarry Smith     *fields = NULL;
138469ca1f37SDmitry Karpeev   }
138537d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
138637d0c07bSMatthew G Knepley   if (section) {
138737d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
138837d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
138937d0c07bSMatthew G Knepley 
139037d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
139137d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1392dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
139337d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
139437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
139537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
139637d0c07bSMatthew G Knepley     }
139737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
139837d0c07bSMatthew G Knepley       PetscInt gdof;
139937d0c07bSMatthew G Knepley 
140037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
140137d0c07bSMatthew G Knepley       if (gdof > 0) {
140237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
140337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
140437d0c07bSMatthew G Knepley 
140537d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
140637d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
140737d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
140837d0c07bSMatthew G Knepley         }
140937d0c07bSMatthew G Knepley       }
141037d0c07bSMatthew G Knepley     }
141137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1412785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
141337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
141437d0c07bSMatthew G Knepley     }
141537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
141637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
141737d0c07bSMatthew G Knepley 
141837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
141937d0c07bSMatthew G Knepley       if (gdof > 0) {
142037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
142137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
142237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
142337d0c07bSMatthew G Knepley 
142437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
142537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
142637d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
142737d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
142837d0c07bSMatthew G Knepley           }
142937d0c07bSMatthew G Knepley         }
143037d0c07bSMatthew G Knepley       }
143137d0c07bSMatthew G Knepley     }
14328865f1eaSKarl Rupp     if (numFields) *numFields = nF;
143337d0c07bSMatthew G Knepley     if (fieldNames) {
1434785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
143537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
143637d0c07bSMatthew G Knepley         const char *fieldName;
143737d0c07bSMatthew G Knepley 
143837d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
143937d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
144037d0c07bSMatthew G Knepley       }
144137d0c07bSMatthew G Knepley     }
144237d0c07bSMatthew G Knepley     if (fields) {
1443785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
144437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
144582f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
144637d0c07bSMatthew G Knepley       }
144737d0c07bSMatthew G Knepley     }
144837d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
14498865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
14508865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
145169ca1f37SDmitry Karpeev   }
14524d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
14534d343eeaSMatthew G Knepley }
14544d343eeaSMatthew G Knepley 
145516621825SDmitry Karpeev 
145616621825SDmitry Karpeev /*@C
145716621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
145816621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
145916621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1460e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1461e7c4fc90SDmitry Karpeev 
1462e7c4fc90SDmitry Karpeev   Not collective
1463e7c4fc90SDmitry Karpeev 
1464e7c4fc90SDmitry Karpeev   Input Parameter:
1465e7c4fc90SDmitry Karpeev . dm - the DM object
1466e7c4fc90SDmitry Karpeev 
1467e7c4fc90SDmitry Karpeev   Output Parameters:
14680298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
14690298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
14700298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
14710298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1472e7c4fc90SDmitry Karpeev 
1473e7c4fc90SDmitry Karpeev   Level: intermediate
1474e7c4fc90SDmitry Karpeev 
1475e7c4fc90SDmitry Karpeev   Notes:
1476e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1477e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1478e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1479e7c4fc90SDmitry Karpeev 
1480e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1481e7c4fc90SDmitry Karpeev @*/
148216621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1483e7c4fc90SDmitry Karpeev {
1484e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1485e7c4fc90SDmitry Karpeev 
1486e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1487e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14888865f1eaSKarl Rupp   if (len) {
14898865f1eaSKarl Rupp     PetscValidPointer(len,2);
14908865f1eaSKarl Rupp     *len = 0;
14918865f1eaSKarl Rupp   }
14928865f1eaSKarl Rupp   if (namelist) {
14938865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
14948865f1eaSKarl Rupp     *namelist = 0;
14958865f1eaSKarl Rupp   }
14968865f1eaSKarl Rupp   if (islist) {
14978865f1eaSKarl Rupp     PetscValidPointer(islist,4);
14988865f1eaSKarl Rupp     *islist = 0;
14998865f1eaSKarl Rupp   }
15008865f1eaSKarl Rupp   if (dmlist) {
15018865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
15028865f1eaSKarl Rupp     *dmlist = 0;
15038865f1eaSKarl Rupp   }
1504f3f0edfdSDmitry Karpeev   /*
1505f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1506f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1507f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1508f3f0edfdSDmitry Karpeev    */
1509ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
151016621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1511435a35e8SMatthew G Knepley     PetscSection section;
1512435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1513435a35e8SMatthew G Knepley 
1514435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1515435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1516435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1517f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
151803dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
151903dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
152003dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1521435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1522435a35e8SMatthew G Knepley         const char *fieldName;
1523435a35e8SMatthew G Knepley 
152403dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
152503dc3394SMatthew G. Knepley         if (namelist) {
1526435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1527435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1528435a35e8SMatthew G Knepley         }
152903dc3394SMatthew G. Knepley       }
1530435a35e8SMatthew G Knepley     } else {
153169ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1532e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
15330298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1534e7c4fc90SDmitry Karpeev     }
15358865f1eaSKarl Rupp   } else {
153616621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
153716621825SDmitry Karpeev   }
153816621825SDmitry Karpeev   PetscFunctionReturn(0);
153916621825SDmitry Karpeev }
154016621825SDmitry Karpeev 
1541564cec59SMatthew G. Knepley /*@
1542435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1543435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1544435a35e8SMatthew G Knepley 
1545435a35e8SMatthew G Knepley   Not collective
1546435a35e8SMatthew G Knepley 
1547435a35e8SMatthew G Knepley   Input Parameters:
15482adcc780SMatthew G. Knepley + dm        - The DM object
15492adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
15502adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1551435a35e8SMatthew G Knepley 
1552435a35e8SMatthew G Knepley   Output Parameters:
15532adcc780SMatthew G. Knepley + is - The global indices for the subproblem
15542adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1555435a35e8SMatthew G Knepley 
1556435a35e8SMatthew G Knepley   Level: intermediate
1557435a35e8SMatthew G Knepley 
1558435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1559435a35e8SMatthew G Knepley @*/
156037bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1561435a35e8SMatthew G Knepley {
1562435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1563435a35e8SMatthew G Knepley 
1564435a35e8SMatthew G Knepley   PetscFunctionBegin;
1565435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1566435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
15678865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
15688865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1569435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1570435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
157182f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1572435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1573435a35e8SMatthew G Knepley }
1574435a35e8SMatthew G Knepley 
15752adcc780SMatthew G. Knepley /*@C
15762adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
15772adcc780SMatthew G. Knepley 
15782adcc780SMatthew G. Knepley   Not collective
15792adcc780SMatthew G. Knepley 
15802adcc780SMatthew G. Knepley   Input Parameter:
15812adcc780SMatthew G. Knepley + dms - The DM objects
15822adcc780SMatthew G. Knepley - len - The number of DMs
15832adcc780SMatthew G. Knepley 
15842adcc780SMatthew G. Knepley   Output Parameters:
15852adcc780SMatthew G. Knepley + is - The global indices for the subproblem
15862adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
15872adcc780SMatthew G. Knepley 
15882adcc780SMatthew G. Knepley   Level: intermediate
15892adcc780SMatthew G. Knepley 
15902adcc780SMatthew G. Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
15912adcc780SMatthew G. Knepley @*/
15922adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
15932adcc780SMatthew G. Knepley {
15942adcc780SMatthew G. Knepley   PetscInt       i;
15952adcc780SMatthew G. Knepley   PetscErrorCode ierr;
15962adcc780SMatthew G. Knepley 
15972adcc780SMatthew G. Knepley   PetscFunctionBegin;
15982adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
15992adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
16002adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
16012adcc780SMatthew G. Knepley   if (superdm) PetscValidPointer(superdm,4);
16022adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
16032adcc780SMatthew G. Knepley   if (len) {
16042adcc780SMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {
16052adcc780SMatthew G. Knepley       ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
16062adcc780SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
16072adcc780SMatthew G. Knepley   }
16082adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
16092adcc780SMatthew G. Knepley }
16102adcc780SMatthew G. Knepley 
161116621825SDmitry Karpeev 
161216621825SDmitry Karpeev /*@C
16138d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
16148d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
16158d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
16168d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
16178d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
161816621825SDmitry Karpeev 
161916621825SDmitry Karpeev   Not collective
162016621825SDmitry Karpeev 
162116621825SDmitry Karpeev   Input Parameter:
162216621825SDmitry Karpeev . dm - the DM object
162316621825SDmitry Karpeev 
162416621825SDmitry Karpeev   Output Parameters:
16250298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
16260298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
16270298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
16280298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
16290298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
163016621825SDmitry Karpeev 
163116621825SDmitry Karpeev   Level: intermediate
163216621825SDmitry Karpeev 
163316621825SDmitry Karpeev   Notes:
163416621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
163516621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
163616621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
163716621825SDmitry Karpeev 
16388d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
163916621825SDmitry Karpeev @*/
16408d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
164116621825SDmitry Karpeev {
164216621825SDmitry Karpeev   PetscErrorCode      ierr;
1643be081cd6SPeter Brune   DMSubDomainHookLink link;
1644be081cd6SPeter Brune   PetscInt            i,l;
164516621825SDmitry Karpeev 
164616621825SDmitry Karpeev   PetscFunctionBegin;
164716621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
164814a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
16490298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
16500298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
16510298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
16520298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1653f3f0edfdSDmitry Karpeev   /*
1654f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1655f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1656f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1657f3f0edfdSDmitry Karpeev    */
1658ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
165916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1660be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
166114a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1662f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1663be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1664be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1665be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1666be081cd6SPeter Brune         }
1667648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1668e7c4fc90SDmitry Karpeev       }
166914a18fd3SPeter Brune     }
167014a18fd3SPeter Brune     if (len) *len = l;
167114a18fd3SPeter Brune   }
1672e30e807fSPeter Brune   PetscFunctionReturn(0);
1673e30e807fSPeter Brune }
1674e30e807fSPeter Brune 
1675e30e807fSPeter Brune 
1676e30e807fSPeter Brune /*@C
1677e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1678e30e807fSPeter Brune 
1679e30e807fSPeter Brune   Not collective
1680e30e807fSPeter Brune 
1681e30e807fSPeter Brune   Input Parameters:
1682e30e807fSPeter Brune + dm - the DM object
1683e30e807fSPeter Brune . n  - the number of subdomain scatters
1684e30e807fSPeter Brune - subdms - the local subdomains
1685e30e807fSPeter Brune 
1686e30e807fSPeter Brune   Output Parameters:
1687e30e807fSPeter Brune + n     - the number of scatters returned
1688e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1689e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1690e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1691e30e807fSPeter Brune 
1692e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1693e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1694e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1695e30e807fSPeter Brune   solution and residual data.
1696e30e807fSPeter Brune 
1697e30e807fSPeter Brune   Level: developer
1698e30e807fSPeter Brune 
1699e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1700e30e807fSPeter Brune @*/
1701e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1702e30e807fSPeter Brune {
1703e30e807fSPeter Brune   PetscErrorCode ierr;
1704e30e807fSPeter Brune 
1705e30e807fSPeter Brune   PetscFunctionBegin;
1706e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1707e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1708e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1709e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
17106668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1711e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1712e7c4fc90SDmitry Karpeev }
1713e7c4fc90SDmitry Karpeev 
171447c6ae99SBarry Smith /*@
171547c6ae99SBarry Smith   DMRefine - Refines a DM object
171647c6ae99SBarry Smith 
171747c6ae99SBarry Smith   Collective on DM
171847c6ae99SBarry Smith 
171947c6ae99SBarry Smith   Input Parameter:
172047c6ae99SBarry Smith + dm   - the DM object
172191d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
172247c6ae99SBarry Smith 
172347c6ae99SBarry Smith   Output Parameter:
17240298fd71SBarry Smith . dmf - the refined DM, or NULL
1725ae0a1c52SMatthew G Knepley 
17260298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
172747c6ae99SBarry Smith 
172847c6ae99SBarry Smith   Level: developer
172947c6ae99SBarry Smith 
1730e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
173147c6ae99SBarry Smith @*/
17327087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
173347c6ae99SBarry Smith {
173447c6ae99SBarry Smith   PetscErrorCode   ierr;
1735c833c3b5SJed Brown   DMRefineHookLink link;
173647c6ae99SBarry Smith 
173747c6ae99SBarry Smith   PetscFunctionBegin;
1738732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17391ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1740fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
174147c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
17424057135bSMatthew G Knepley   if (*dmf) {
174343842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
17448865f1eaSKarl Rupp 
17458cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
17468865f1eaSKarl Rupp 
1747644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
17480598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1749656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
17508865f1eaSKarl Rupp 
1751e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1752c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
17538865f1eaSKarl Rupp       if (link->refinehook) {
17548865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
17558865f1eaSKarl Rupp       }
1756c833c3b5SJed Brown     }
1757c833c3b5SJed Brown   }
17581ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1759c833c3b5SJed Brown   PetscFunctionReturn(0);
1760c833c3b5SJed Brown }
1761c833c3b5SJed Brown 
1762bb9467b5SJed Brown /*@C
1763c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1764c833c3b5SJed Brown 
1765c833c3b5SJed Brown    Logically Collective
1766c833c3b5SJed Brown 
1767c833c3b5SJed Brown    Input Arguments:
1768c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1769c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1770c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
17710298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1772c833c3b5SJed Brown 
1773c833c3b5SJed Brown    Calling sequence of refinehook:
1774c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1775c833c3b5SJed Brown 
1776c833c3b5SJed Brown +  coarse - coarse level DM
1777c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1778c833c3b5SJed Brown -  ctx - optional user-defined function context
1779c833c3b5SJed Brown 
1780c833c3b5SJed Brown    Calling sequence for interphook:
1781c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1782c833c3b5SJed Brown 
1783c833c3b5SJed Brown +  coarse - coarse level DM
1784c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1785c833c3b5SJed Brown .  fine - fine level DM to update
1786c833c3b5SJed Brown -  ctx - optional user-defined function context
1787c833c3b5SJed Brown 
1788c833c3b5SJed Brown    Level: advanced
1789c833c3b5SJed Brown 
1790c833c3b5SJed Brown    Notes:
1791c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1792c833c3b5SJed Brown 
1793c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1794c833c3b5SJed Brown 
1795bb9467b5SJed Brown    This function is currently not available from Fortran.
1796bb9467b5SJed Brown 
1797c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1798c833c3b5SJed Brown @*/
1799c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1800c833c3b5SJed Brown {
1801c833c3b5SJed Brown   PetscErrorCode   ierr;
1802c833c3b5SJed Brown   DMRefineHookLink link,*p;
1803c833c3b5SJed Brown 
1804c833c3b5SJed Brown   PetscFunctionBegin;
1805c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
18063d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
18073d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
18083d8e3701SJed Brown   }
180995dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1810c833c3b5SJed Brown   link->refinehook = refinehook;
1811c833c3b5SJed Brown   link->interphook = interphook;
1812c833c3b5SJed Brown   link->ctx        = ctx;
18130298fd71SBarry Smith   link->next       = NULL;
1814c833c3b5SJed Brown   *p               = link;
1815c833c3b5SJed Brown   PetscFunctionReturn(0);
1816c833c3b5SJed Brown }
1817c833c3b5SJed Brown 
18183d8e3701SJed Brown /*@C
18193d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
18203d8e3701SJed Brown 
18213d8e3701SJed Brown    Logically Collective
18223d8e3701SJed Brown 
18233d8e3701SJed Brown    Input Arguments:
18243d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
18253d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
18263d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18273d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
18283d8e3701SJed Brown 
18293d8e3701SJed Brown    Level: advanced
18303d8e3701SJed Brown 
18313d8e3701SJed Brown    Notes:
18323d8e3701SJed Brown    This function does nothing if the hook is not in the list.
18333d8e3701SJed Brown 
18343d8e3701SJed Brown    This function is currently not available from Fortran.
18353d8e3701SJed Brown 
18363d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
18373d8e3701SJed Brown @*/
18383d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
18393d8e3701SJed Brown {
18403d8e3701SJed Brown   PetscErrorCode   ierr;
18413d8e3701SJed Brown   DMRefineHookLink link,*p;
18423d8e3701SJed Brown 
18433d8e3701SJed Brown   PetscFunctionBegin;
18443d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
18453d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
18463d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
18473d8e3701SJed Brown       link = *p;
18483d8e3701SJed Brown       *p = link->next;
18493d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
18503d8e3701SJed Brown       break;
18513d8e3701SJed Brown     }
18523d8e3701SJed Brown   }
18533d8e3701SJed Brown   PetscFunctionReturn(0);
18543d8e3701SJed Brown }
18553d8e3701SJed Brown 
1856c833c3b5SJed Brown /*@
1857c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1858c833c3b5SJed Brown 
1859c833c3b5SJed Brown    Collective if any hooks are
1860c833c3b5SJed Brown 
1861c833c3b5SJed Brown    Input Arguments:
1862c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1863e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1864c833c3b5SJed Brown -  fine - finer DM to update
1865c833c3b5SJed Brown 
1866c833c3b5SJed Brown    Level: developer
1867c833c3b5SJed Brown 
1868c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1869c833c3b5SJed Brown @*/
1870c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1871c833c3b5SJed Brown {
1872c833c3b5SJed Brown   PetscErrorCode   ierr;
1873c833c3b5SJed Brown   DMRefineHookLink link;
1874c833c3b5SJed Brown 
1875c833c3b5SJed Brown   PetscFunctionBegin;
1876c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
18778865f1eaSKarl Rupp     if (link->interphook) {
18788865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
18798865f1eaSKarl Rupp     }
18804057135bSMatthew G Knepley   }
188147c6ae99SBarry Smith   PetscFunctionReturn(0);
188247c6ae99SBarry Smith }
188347c6ae99SBarry Smith 
1884eb3f98d2SBarry Smith /*@
1885eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1886eb3f98d2SBarry Smith 
1887eb3f98d2SBarry Smith     Not Collective
1888eb3f98d2SBarry Smith 
1889eb3f98d2SBarry Smith     Input Parameter:
1890eb3f98d2SBarry Smith .   dm - the DM object
1891eb3f98d2SBarry Smith 
1892eb3f98d2SBarry Smith     Output Parameter:
1893eb3f98d2SBarry Smith .   level - number of refinements
1894eb3f98d2SBarry Smith 
1895eb3f98d2SBarry Smith     Level: developer
1896eb3f98d2SBarry Smith 
18976a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1898eb3f98d2SBarry Smith 
1899eb3f98d2SBarry Smith @*/
1900eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1901eb3f98d2SBarry Smith {
1902eb3f98d2SBarry Smith   PetscFunctionBegin;
1903eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1904eb3f98d2SBarry Smith   *level = dm->levelup;
1905eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1906eb3f98d2SBarry Smith }
1907eb3f98d2SBarry Smith 
1908fef3a512SBarry Smith /*@
1909fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
1910fef3a512SBarry Smith 
1911fef3a512SBarry Smith     Not Collective
1912fef3a512SBarry Smith 
1913fef3a512SBarry Smith     Input Parameter:
1914fef3a512SBarry Smith +   dm - the DM object
1915fef3a512SBarry Smith -   level - number of refinements
1916fef3a512SBarry Smith 
1917fef3a512SBarry Smith     Level: advanced
1918fef3a512SBarry Smith 
1919fef3a512SBarry Smith     Notes: This value is used by PCMG to determine how many multigrid levels to use
1920fef3a512SBarry Smith 
1921fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1922fef3a512SBarry Smith 
1923fef3a512SBarry Smith @*/
1924fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
1925fef3a512SBarry Smith {
1926fef3a512SBarry Smith   PetscFunctionBegin;
1927fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1928fef3a512SBarry Smith   dm->levelup = level;
1929fef3a512SBarry Smith   PetscFunctionReturn(0);
1930fef3a512SBarry Smith }
1931fef3a512SBarry Smith 
1932bb9467b5SJed Brown /*@C
1933baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1934baf369e7SPeter Brune 
1935baf369e7SPeter Brune    Logically Collective
1936baf369e7SPeter Brune 
1937baf369e7SPeter Brune    Input Arguments:
1938baf369e7SPeter Brune +  dm - the DM
1939baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1940baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
19410298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1942baf369e7SPeter Brune 
1943baf369e7SPeter Brune    Calling sequence for beginhook:
1944baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1945baf369e7SPeter Brune 
1946baf369e7SPeter Brune +  dm - global DM
1947baf369e7SPeter Brune .  g - global vector
1948baf369e7SPeter Brune .  mode - mode
1949baf369e7SPeter Brune .  l - local vector
1950baf369e7SPeter Brune -  ctx - optional user-defined function context
1951baf369e7SPeter Brune 
1952baf369e7SPeter Brune 
1953baf369e7SPeter Brune    Calling sequence for endhook:
1954ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1955baf369e7SPeter Brune 
1956baf369e7SPeter Brune +  global - global DM
1957baf369e7SPeter Brune -  ctx - optional user-defined function context
1958baf369e7SPeter Brune 
1959baf369e7SPeter Brune    Level: advanced
1960baf369e7SPeter Brune 
1961baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1962baf369e7SPeter Brune @*/
1963baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1964baf369e7SPeter Brune {
1965baf369e7SPeter Brune   PetscErrorCode          ierr;
1966baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1967baf369e7SPeter Brune 
1968baf369e7SPeter Brune   PetscFunctionBegin;
1969baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1970baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
197195dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
1972baf369e7SPeter Brune   link->beginhook = beginhook;
1973baf369e7SPeter Brune   link->endhook   = endhook;
1974baf369e7SPeter Brune   link->ctx       = ctx;
19750298fd71SBarry Smith   link->next      = NULL;
1976baf369e7SPeter Brune   *p              = link;
1977baf369e7SPeter Brune   PetscFunctionReturn(0);
1978baf369e7SPeter Brune }
1979baf369e7SPeter Brune 
19804c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
19814c274da1SToby Isaac {
19824c274da1SToby Isaac   Mat cMat;
19834c274da1SToby Isaac   Vec cVec;
19844c274da1SToby Isaac   PetscSection section, cSec;
19854c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19864c274da1SToby Isaac   PetscErrorCode ierr;
19874c274da1SToby Isaac 
19884c274da1SToby Isaac   PetscFunctionBegin;
19894c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19904c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19914c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
19925db9a05bSToby Isaac     PetscInt nRows;
19935db9a05bSToby Isaac 
19945db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
19955db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
19964c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19977711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19984c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
19994c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
20004c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
20014c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
20024c274da1SToby Isaac       if (dof) {
20034c274da1SToby Isaac         PetscScalar *vals;
20044c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
20054c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
20064c274da1SToby Isaac       }
20074c274da1SToby Isaac     }
20084c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
20094c274da1SToby Isaac   }
20104c274da1SToby Isaac   PetscFunctionReturn(0);
20114c274da1SToby Isaac }
20124c274da1SToby Isaac 
201347c6ae99SBarry Smith /*@
201447c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
201547c6ae99SBarry Smith 
201647c6ae99SBarry Smith     Neighbor-wise Collective on DM
201747c6ae99SBarry Smith 
201847c6ae99SBarry Smith     Input Parameters:
201947c6ae99SBarry Smith +   dm - the DM object
202047c6ae99SBarry Smith .   g - the global vector
202147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
202247c6ae99SBarry Smith -   l - the local vector
202347c6ae99SBarry Smith 
202447c6ae99SBarry Smith 
202547c6ae99SBarry Smith     Level: beginner
202647c6ae99SBarry Smith 
2027e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
202847c6ae99SBarry Smith 
202947c6ae99SBarry Smith @*/
20307087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
203147c6ae99SBarry Smith {
20327128ae9fSMatthew G Knepley   PetscSF                 sf;
203347c6ae99SBarry Smith   PetscErrorCode          ierr;
2034baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
203547c6ae99SBarry Smith 
203647c6ae99SBarry Smith   PetscFunctionBegin;
2037171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2038baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
20398865f1eaSKarl Rupp     if (link->beginhook) {
20408865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
20418865f1eaSKarl Rupp     }
2042baf369e7SPeter Brune   }
20437128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
20447128ae9fSMatthew G Knepley   if (sf) {
2045ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2046ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
20477128ae9fSMatthew G Knepley 
204882f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20497128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2050ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20517128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20527128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2053ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20547128ae9fSMatthew G Knepley   } else {
2055843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20567128ae9fSMatthew G Knepley   }
205747c6ae99SBarry Smith   PetscFunctionReturn(0);
205847c6ae99SBarry Smith }
205947c6ae99SBarry Smith 
206047c6ae99SBarry Smith /*@
206147c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
206247c6ae99SBarry Smith 
206347c6ae99SBarry Smith     Neighbor-wise Collective on DM
206447c6ae99SBarry Smith 
206547c6ae99SBarry Smith     Input Parameters:
206647c6ae99SBarry Smith +   dm - the DM object
206747c6ae99SBarry Smith .   g - the global vector
206847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
206947c6ae99SBarry Smith -   l - the local vector
207047c6ae99SBarry Smith 
207147c6ae99SBarry Smith 
207247c6ae99SBarry Smith     Level: beginner
207347c6ae99SBarry Smith 
2074e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
207547c6ae99SBarry Smith 
207647c6ae99SBarry Smith @*/
20777087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
207847c6ae99SBarry Smith {
20797128ae9fSMatthew G Knepley   PetscSF                 sf;
208047c6ae99SBarry Smith   PetscErrorCode          ierr;
2081ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2082ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2083baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
208447c6ae99SBarry Smith 
208547c6ae99SBarry Smith   PetscFunctionBegin;
2086171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20877128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
20887128ae9fSMatthew G Knepley   if (sf) {
208982f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20907128ae9fSMatthew G Knepley 
20917128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2092ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20937128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20947128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2095ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20967128ae9fSMatthew G Knepley   } else {
2097843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20987128ae9fSMatthew G Knepley   }
20994c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2100baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2101baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2102baf369e7SPeter Brune   }
210347c6ae99SBarry Smith   PetscFunctionReturn(0);
210447c6ae99SBarry Smith }
210547c6ae99SBarry Smith 
2106d4d07f1eSToby Isaac /*@C
2107d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2108d4d07f1eSToby Isaac 
2109d4d07f1eSToby Isaac    Logically Collective
2110d4d07f1eSToby Isaac 
2111d4d07f1eSToby Isaac    Input Arguments:
2112d4d07f1eSToby Isaac +  dm - the DM
2113d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2114d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2115d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2116d4d07f1eSToby Isaac 
2117d4d07f1eSToby Isaac    Calling sequence for beginhook:
2118d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2119d4d07f1eSToby Isaac 
2120d4d07f1eSToby Isaac +  dm - global DM
2121d4d07f1eSToby Isaac .  l - local vector
2122d4d07f1eSToby Isaac .  mode - mode
2123d4d07f1eSToby Isaac .  g - global vector
2124d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2125d4d07f1eSToby Isaac 
2126d4d07f1eSToby Isaac 
2127d4d07f1eSToby Isaac    Calling sequence for endhook:
2128d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2129d4d07f1eSToby Isaac 
2130d4d07f1eSToby Isaac +  global - global DM
2131d4d07f1eSToby Isaac .  l - local vector
2132d4d07f1eSToby Isaac .  mode - mode
2133d4d07f1eSToby Isaac .  g - global vector
2134d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2135d4d07f1eSToby Isaac 
2136d4d07f1eSToby Isaac    Level: advanced
2137d4d07f1eSToby Isaac 
2138d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2139d4d07f1eSToby Isaac @*/
2140d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2141d4d07f1eSToby Isaac {
2142d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2143d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2144d4d07f1eSToby Isaac 
2145d4d07f1eSToby Isaac   PetscFunctionBegin;
2146d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2147d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
214895dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2149d4d07f1eSToby Isaac   link->beginhook = beginhook;
2150d4d07f1eSToby Isaac   link->endhook   = endhook;
2151d4d07f1eSToby Isaac   link->ctx       = ctx;
2152d4d07f1eSToby Isaac   link->next      = NULL;
2153d4d07f1eSToby Isaac   *p              = link;
2154d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2155d4d07f1eSToby Isaac }
2156d4d07f1eSToby Isaac 
21574c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
21584c274da1SToby Isaac {
21594c274da1SToby Isaac   Mat cMat;
21604c274da1SToby Isaac   Vec cVec;
21614c274da1SToby Isaac   PetscSection section, cSec;
21624c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
21634c274da1SToby Isaac   PetscErrorCode ierr;
21644c274da1SToby Isaac 
21654c274da1SToby Isaac   PetscFunctionBegin;
21664c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21674c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
21684c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
21695db9a05bSToby Isaac     PetscInt nRows;
21705db9a05bSToby Isaac 
21715db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21725db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
21734c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
21747711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21754c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21764c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21774c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21784c274da1SToby Isaac       if (dof) {
21794c274da1SToby Isaac         PetscInt d;
21804c274da1SToby Isaac         PetscScalar *vals;
21814c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
21824c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
21834c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
21844c274da1SToby Isaac          * we just extracted */
21854c274da1SToby Isaac         for (d = 0; d < dof; d++) {
21864c274da1SToby Isaac           vals[d] = 0.;
21874c274da1SToby Isaac         }
21884c274da1SToby Isaac       }
21894c274da1SToby Isaac     }
21904c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
21914c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21924c274da1SToby Isaac   }
21934c274da1SToby Isaac   PetscFunctionReturn(0);
21944c274da1SToby Isaac }
21954c274da1SToby Isaac 
219647c6ae99SBarry Smith /*@
21979a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
21989a42bb27SBarry Smith 
21999a42bb27SBarry Smith     Neighbor-wise Collective on DM
22009a42bb27SBarry Smith 
22019a42bb27SBarry Smith     Input Parameters:
22029a42bb27SBarry Smith +   dm - the DM object
2203f6813fd5SJed Brown .   l - the local vector
22041eb28f2eSBarry 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.
22051eb28f2eSBarry Smith -   g - the global vector
22069a42bb27SBarry Smith 
2207d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
220884330215SMatthew 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.
22099a42bb27SBarry Smith 
22109a42bb27SBarry Smith     Level: beginner
22119a42bb27SBarry Smith 
2212e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
22139a42bb27SBarry Smith 
22149a42bb27SBarry Smith @*/
22157087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
22169a42bb27SBarry Smith {
22177128ae9fSMatthew G Knepley   PetscSF                 sf;
221884330215SMatthew G. Knepley   PetscSection            s, gs;
2219d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2220ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2221ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
222284330215SMatthew G. Knepley   PetscBool               isInsert;
222384330215SMatthew G. Knepley   PetscErrorCode          ierr;
22249a42bb27SBarry Smith 
22259a42bb27SBarry Smith   PetscFunctionBegin;
2226171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2227d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2228d4d07f1eSToby Isaac     if (link->beginhook) {
2229d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2230d4d07f1eSToby Isaac     }
2231d4d07f1eSToby Isaac   }
22324c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
22337128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
223484330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
22357128ae9fSMatthew G Knepley   switch (mode) {
22367128ae9fSMatthew G Knepley   case INSERT_VALUES:
22377128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2238304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
223984330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
22407128ae9fSMatthew G Knepley   case ADD_VALUES:
22417128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2242304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
224384330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
22447128ae9fSMatthew G Knepley   default:
224582f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22467128ae9fSMatthew G Knepley   }
224784330215SMatthew G. Knepley   if (sf && !isInsert) {
2248ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
22497128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2250a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2251ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
225284330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
225384330215SMatthew G. Knepley   } else if (s && isInsert) {
225484330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
225584330215SMatthew G. Knepley 
225684330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
225784330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
225884330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2259ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
226084330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
226184330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2262b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
226384330215SMatthew G. Knepley 
226484330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
226503442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
226684330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2267b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
226884330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
226984330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2270b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
227103442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2272b3b16f48SMatthew 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);
2273b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2274b3b16f48SMatthew G. Knepley       if (!gcdof) {
227584330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2276b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2277b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
227884330215SMatthew G. Knepley         const PetscInt *cdofs;
227984330215SMatthew G. Knepley         PetscInt        cind = 0;
228084330215SMatthew G. Knepley 
228184330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2282b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
228384330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2284b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
228584330215SMatthew G. Knepley         }
2286b3b16f48SMatthew 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);
228784330215SMatthew G. Knepley     }
2288ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22897128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
22907128ae9fSMatthew G Knepley   } else {
2291843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22927128ae9fSMatthew G Knepley   }
22939a42bb27SBarry Smith   PetscFunctionReturn(0);
22949a42bb27SBarry Smith }
22959a42bb27SBarry Smith 
22969a42bb27SBarry Smith /*@
22979a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
229847c6ae99SBarry Smith 
229947c6ae99SBarry Smith     Neighbor-wise Collective on DM
230047c6ae99SBarry Smith 
230147c6ae99SBarry Smith     Input Parameters:
230247c6ae99SBarry Smith +   dm - the DM object
2303f6813fd5SJed Brown .   l - the local vector
230447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2305f6813fd5SJed Brown -   g - the global vector
230647c6ae99SBarry Smith 
230747c6ae99SBarry Smith 
230847c6ae99SBarry Smith     Level: beginner
230947c6ae99SBarry Smith 
2310e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
231147c6ae99SBarry Smith 
231247c6ae99SBarry Smith @*/
23137087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
231447c6ae99SBarry Smith {
23157128ae9fSMatthew G Knepley   PetscSF                 sf;
231684330215SMatthew G. Knepley   PetscSection            s;
2317d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
231884330215SMatthew G. Knepley   PetscBool               isInsert;
231984330215SMatthew G. Knepley   PetscErrorCode          ierr;
232047c6ae99SBarry Smith 
232147c6ae99SBarry Smith   PetscFunctionBegin;
2322171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23237128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
232484330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
23257128ae9fSMatthew G Knepley   switch (mode) {
23267128ae9fSMatthew G Knepley   case INSERT_VALUES:
23277128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
232884330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
23297128ae9fSMatthew G Knepley   case ADD_VALUES:
23307128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
233184330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
23327128ae9fSMatthew G Knepley   default:
233382f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23347128ae9fSMatthew G Knepley   }
233584330215SMatthew G. Knepley   if (sf && !isInsert) {
2336ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2337ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
233884330215SMatthew G. Knepley 
2339ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
23407128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2341a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2342ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
23437128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
234484330215SMatthew G. Knepley   } else if (s && isInsert) {
23457128ae9fSMatthew G Knepley   } else {
2346843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
23477128ae9fSMatthew G Knepley   }
2348d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2349d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2350d4d07f1eSToby Isaac   }
235147c6ae99SBarry Smith   PetscFunctionReturn(0);
235247c6ae99SBarry Smith }
235347c6ae99SBarry Smith 
2354f089877aSRichard Tran Mills /*@
2355bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2356bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2357d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2358f089877aSRichard Tran Mills 
2359bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2360f089877aSRichard Tran Mills 
2361f089877aSRichard Tran Mills    Input Parameters:
2362f089877aSRichard Tran Mills +  dm - the DM object
2363bc0a1609SRichard Tran Mills .  g - the original local vector
2364bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2365f089877aSRichard Tran Mills 
2366bc0a1609SRichard Tran Mills    Output Parameter:
2367bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2368f089877aSRichard Tran Mills 
2369f089877aSRichard Tran Mills    Level: intermediate
2370f089877aSRichard Tran Mills 
2371bc0a1609SRichard Tran Mills    Notes:
2372bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2373bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2374bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2375bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2376bc0a1609SRichard Tran Mills 
2377bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2378bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2379f089877aSRichard Tran Mills 
2380f089877aSRichard Tran Mills @*/
2381f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2382f089877aSRichard Tran Mills {
2383f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2384f089877aSRichard Tran Mills 
2385f089877aSRichard Tran Mills   PetscFunctionBegin;
2386f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2387bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2388f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2389f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2390f089877aSRichard Tran Mills }
2391f089877aSRichard Tran Mills 
2392f089877aSRichard Tran Mills /*@
2393bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2394bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2395d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2396f089877aSRichard Tran Mills 
2397bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2398f089877aSRichard Tran Mills 
2399f089877aSRichard Tran Mills    Input Parameters:
2400bc0a1609SRichard Tran Mills +  da - the DM object
2401bc0a1609SRichard Tran Mills .  g - the original local vector
2402bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2403f089877aSRichard Tran Mills 
2404bc0a1609SRichard Tran Mills    Output Parameter:
2405bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2406f089877aSRichard Tran Mills 
2407f089877aSRichard Tran Mills    Level: intermediate
2408f089877aSRichard Tran Mills 
2409bc0a1609SRichard Tran Mills    Notes:
2410bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2411bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2412bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2413bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2414bc0a1609SRichard Tran Mills 
2415bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2416bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2417f089877aSRichard Tran Mills 
2418f089877aSRichard Tran Mills @*/
2419f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2420f089877aSRichard Tran Mills {
2421f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2422f089877aSRichard Tran Mills 
2423f089877aSRichard Tran Mills   PetscFunctionBegin;
2424f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2425bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2426f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2427f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2428f089877aSRichard Tran Mills }
2429f089877aSRichard Tran Mills 
2430f089877aSRichard Tran Mills 
243147c6ae99SBarry Smith /*@
243247c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
243347c6ae99SBarry Smith 
243447c6ae99SBarry Smith     Collective on DM
243547c6ae99SBarry Smith 
243647c6ae99SBarry Smith     Input Parameter:
243747c6ae99SBarry Smith +   dm - the DM object
243891d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
243947c6ae99SBarry Smith 
244047c6ae99SBarry Smith     Output Parameter:
244147c6ae99SBarry Smith .   dmc - the coarsened DM
244247c6ae99SBarry Smith 
244347c6ae99SBarry Smith     Level: developer
244447c6ae99SBarry Smith 
2445e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
244647c6ae99SBarry Smith 
244747c6ae99SBarry Smith @*/
24487087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
244947c6ae99SBarry Smith {
245047c6ae99SBarry Smith   PetscErrorCode    ierr;
2451b17ce1afSJed Brown   DMCoarsenHookLink link;
245247c6ae99SBarry Smith 
245347c6ae99SBarry Smith   PetscFunctionBegin;
2454171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2455fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
245647a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
245747c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
24588892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2459a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
246043842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
24618cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2462644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
24630598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2464656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2465e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2466b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2467b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2468b17ce1afSJed Brown   }
246947a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2470b17ce1afSJed Brown   PetscFunctionReturn(0);
2471b17ce1afSJed Brown }
2472b17ce1afSJed Brown 
2473bb9467b5SJed Brown /*@C
2474b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2475b17ce1afSJed Brown 
2476b17ce1afSJed Brown    Logically Collective
2477b17ce1afSJed Brown 
2478b17ce1afSJed Brown    Input Arguments:
2479b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2480b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2481b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
24820298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2483b17ce1afSJed Brown 
2484b17ce1afSJed Brown    Calling sequence of coarsenhook:
2485b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2486b17ce1afSJed Brown 
2487b17ce1afSJed Brown +  fine - fine level DM
2488b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2489b17ce1afSJed Brown -  ctx - optional user-defined function context
2490b17ce1afSJed Brown 
2491b17ce1afSJed Brown    Calling sequence for restricthook:
2492c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2493b17ce1afSJed Brown 
2494b17ce1afSJed Brown +  fine - fine level DM
2495b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2496c833c3b5SJed Brown .  rscale - scaling vector for restriction
2497c833c3b5SJed Brown .  inject - matrix restricting by injection
2498b17ce1afSJed Brown .  coarse - coarse level DM to update
2499b17ce1afSJed Brown -  ctx - optional user-defined function context
2500b17ce1afSJed Brown 
2501b17ce1afSJed Brown    Level: advanced
2502b17ce1afSJed Brown 
2503b17ce1afSJed Brown    Notes:
2504b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2505b17ce1afSJed Brown 
2506b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2507b17ce1afSJed Brown 
2508b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2509b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2510b17ce1afSJed Brown 
2511bb9467b5SJed Brown    This function is currently not available from Fortran.
2512bb9467b5SJed Brown 
2513dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2514b17ce1afSJed Brown @*/
2515b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2516b17ce1afSJed Brown {
2517b17ce1afSJed Brown   PetscErrorCode    ierr;
2518b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2519b17ce1afSJed Brown 
2520b17ce1afSJed Brown   PetscFunctionBegin;
2521b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
25221e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
25231e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
25241e3d8eccSJed Brown   }
252595dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2526b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2527b17ce1afSJed Brown   link->restricthook = restricthook;
2528b17ce1afSJed Brown   link->ctx          = ctx;
25290298fd71SBarry Smith   link->next         = NULL;
2530b17ce1afSJed Brown   *p                 = link;
2531b17ce1afSJed Brown   PetscFunctionReturn(0);
2532b17ce1afSJed Brown }
2533b17ce1afSJed Brown 
2534dc822a44SJed Brown /*@C
2535dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2536dc822a44SJed Brown 
2537dc822a44SJed Brown    Logically Collective
2538dc822a44SJed Brown 
2539dc822a44SJed Brown    Input Arguments:
2540dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2541dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2542dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2543dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2544dc822a44SJed Brown 
2545dc822a44SJed Brown    Level: advanced
2546dc822a44SJed Brown 
2547dc822a44SJed Brown    Notes:
2548dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2549dc822a44SJed Brown 
2550dc822a44SJed Brown    This function is currently not available from Fortran.
2551dc822a44SJed Brown 
2552dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2553dc822a44SJed Brown @*/
2554dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2555dc822a44SJed Brown {
2556dc822a44SJed Brown   PetscErrorCode    ierr;
2557dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2558dc822a44SJed Brown 
2559dc822a44SJed Brown   PetscFunctionBegin;
2560dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2561dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2562dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2563dc822a44SJed Brown       link = *p;
2564dc822a44SJed Brown       *p = link->next;
2565dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2566dc822a44SJed Brown       break;
2567dc822a44SJed Brown     }
2568dc822a44SJed Brown   }
2569dc822a44SJed Brown   PetscFunctionReturn(0);
2570dc822a44SJed Brown }
2571dc822a44SJed Brown 
2572dc822a44SJed Brown 
2573b17ce1afSJed Brown /*@
2574b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2575b17ce1afSJed Brown 
2576b17ce1afSJed Brown    Collective if any hooks are
2577b17ce1afSJed Brown 
2578b17ce1afSJed Brown    Input Arguments:
2579b17ce1afSJed Brown +  fine - finer DM to use as a base
2580b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2581e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2582b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2583e91eccc2SStefano Zampini -  coarse - coarser DM to update
2584b17ce1afSJed Brown 
2585b17ce1afSJed Brown    Level: developer
2586b17ce1afSJed Brown 
2587b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2588b17ce1afSJed Brown @*/
2589b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2590b17ce1afSJed Brown {
2591b17ce1afSJed Brown   PetscErrorCode    ierr;
2592b17ce1afSJed Brown   DMCoarsenHookLink link;
2593b17ce1afSJed Brown 
2594b17ce1afSJed Brown   PetscFunctionBegin;
2595b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
25968865f1eaSKarl Rupp     if (link->restricthook) {
25978865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
25988865f1eaSKarl Rupp     }
2599b17ce1afSJed Brown   }
260047c6ae99SBarry Smith   PetscFunctionReturn(0);
260147c6ae99SBarry Smith }
260247c6ae99SBarry Smith 
2603bb9467b5SJed Brown /*@C
2604be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
26055dbd56e3SPeter Brune 
26065dbd56e3SPeter Brune    Logically Collective
26075dbd56e3SPeter Brune 
26085dbd56e3SPeter Brune    Input Arguments:
26095dbd56e3SPeter Brune +  global - global DM
2610ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
26115dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
26120298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
26135dbd56e3SPeter Brune 
2614ec4806b8SPeter Brune 
2615ec4806b8SPeter Brune    Calling sequence for ddhook:
2616ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2617ec4806b8SPeter Brune 
2618ec4806b8SPeter Brune +  global - global DM
2619ec4806b8SPeter Brune .  block  - block DM
2620ec4806b8SPeter Brune -  ctx - optional user-defined function context
2621ec4806b8SPeter Brune 
26225dbd56e3SPeter Brune    Calling sequence for restricthook:
2623ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
26245dbd56e3SPeter Brune 
26255dbd56e3SPeter Brune +  global - global DM
26265dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
26275dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2628ec4806b8SPeter Brune .  block  - block DM
26295dbd56e3SPeter Brune -  ctx - optional user-defined function context
26305dbd56e3SPeter Brune 
26315dbd56e3SPeter Brune    Level: advanced
26325dbd56e3SPeter Brune 
26335dbd56e3SPeter Brune    Notes:
2634ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
26355dbd56e3SPeter Brune 
26365dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
26375dbd56e3SPeter Brune 
26385dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2639ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
26405dbd56e3SPeter Brune 
2641bb9467b5SJed Brown    This function is currently not available from Fortran.
2642bb9467b5SJed Brown 
26435dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
26445dbd56e3SPeter Brune @*/
2645be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
26465dbd56e3SPeter Brune {
26475dbd56e3SPeter Brune   PetscErrorCode      ierr;
2648be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
26495dbd56e3SPeter Brune 
26505dbd56e3SPeter Brune   PetscFunctionBegin;
26515dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2652b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2653b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2654b3a6b972SJed Brown   }
265595dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
26565dbd56e3SPeter Brune   link->restricthook = restricthook;
2657be081cd6SPeter Brune   link->ddhook       = ddhook;
26585dbd56e3SPeter Brune   link->ctx          = ctx;
26590298fd71SBarry Smith   link->next         = NULL;
26605dbd56e3SPeter Brune   *p                 = link;
26615dbd56e3SPeter Brune   PetscFunctionReturn(0);
26625dbd56e3SPeter Brune }
26635dbd56e3SPeter Brune 
2664b3a6b972SJed Brown /*@C
2665b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2666b3a6b972SJed Brown 
2667b3a6b972SJed Brown    Logically Collective
2668b3a6b972SJed Brown 
2669b3a6b972SJed Brown    Input Arguments:
2670b3a6b972SJed Brown +  global - global DM
2671b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2672b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2673b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2674b3a6b972SJed Brown 
2675b3a6b972SJed Brown    Level: advanced
2676b3a6b972SJed Brown 
2677b3a6b972SJed Brown    Notes:
2678b3a6b972SJed Brown 
2679b3a6b972SJed Brown    This function is currently not available from Fortran.
2680b3a6b972SJed Brown 
2681b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2682b3a6b972SJed Brown @*/
2683b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2684b3a6b972SJed Brown {
2685b3a6b972SJed Brown   PetscErrorCode      ierr;
2686b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2687b3a6b972SJed Brown 
2688b3a6b972SJed Brown   PetscFunctionBegin;
2689b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2690b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2691b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2692b3a6b972SJed Brown       link = *p;
2693b3a6b972SJed Brown       *p = link->next;
2694b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2695b3a6b972SJed Brown       break;
2696b3a6b972SJed Brown     }
2697b3a6b972SJed Brown   }
2698b3a6b972SJed Brown   PetscFunctionReturn(0);
2699b3a6b972SJed Brown }
2700b3a6b972SJed Brown 
27015dbd56e3SPeter Brune /*@
2702be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
27035dbd56e3SPeter Brune 
27045dbd56e3SPeter Brune    Collective if any hooks are
27055dbd56e3SPeter Brune 
27065dbd56e3SPeter Brune    Input Arguments:
27075dbd56e3SPeter Brune +  fine - finer DM to use as a base
2708be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2709be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
27105dbd56e3SPeter Brune -  coarse - coarer DM to update
27115dbd56e3SPeter Brune 
27125dbd56e3SPeter Brune    Level: developer
27135dbd56e3SPeter Brune 
27145dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
27155dbd56e3SPeter Brune @*/
2716be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
27175dbd56e3SPeter Brune {
27185dbd56e3SPeter Brune   PetscErrorCode      ierr;
2719be081cd6SPeter Brune   DMSubDomainHookLink link;
27205dbd56e3SPeter Brune 
27215dbd56e3SPeter Brune   PetscFunctionBegin;
2722be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
27238865f1eaSKarl Rupp     if (link->restricthook) {
27248865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
27258865f1eaSKarl Rupp     }
27265dbd56e3SPeter Brune   }
27275dbd56e3SPeter Brune   PetscFunctionReturn(0);
27285dbd56e3SPeter Brune }
27295dbd56e3SPeter Brune 
27305fe1f584SPeter Brune /*@
27316a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
27325fe1f584SPeter Brune 
27335fe1f584SPeter Brune     Not Collective
27345fe1f584SPeter Brune 
27355fe1f584SPeter Brune     Input Parameter:
27365fe1f584SPeter Brune .   dm - the DM object
27375fe1f584SPeter Brune 
27385fe1f584SPeter Brune     Output Parameter:
27396a7d9d85SPeter Brune .   level - number of coarsenings
27405fe1f584SPeter Brune 
27415fe1f584SPeter Brune     Level: developer
27425fe1f584SPeter Brune 
27436a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
27445fe1f584SPeter Brune 
27455fe1f584SPeter Brune @*/
27465fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
27475fe1f584SPeter Brune {
27485fe1f584SPeter Brune   PetscFunctionBegin;
27495fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27505fe1f584SPeter Brune   *level = dm->leveldown;
27515fe1f584SPeter Brune   PetscFunctionReturn(0);
27525fe1f584SPeter Brune }
27535fe1f584SPeter Brune 
27545fe1f584SPeter Brune 
27555fe1f584SPeter Brune 
275647c6ae99SBarry Smith /*@C
275747c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
275847c6ae99SBarry Smith 
275947c6ae99SBarry Smith     Collective on DM
276047c6ae99SBarry Smith 
276147c6ae99SBarry Smith     Input Parameter:
276247c6ae99SBarry Smith +   dm - the DM object
276347c6ae99SBarry Smith -   nlevels - the number of levels of refinement
276447c6ae99SBarry Smith 
276547c6ae99SBarry Smith     Output Parameter:
276647c6ae99SBarry Smith .   dmf - the refined DM hierarchy
276747c6ae99SBarry Smith 
276847c6ae99SBarry Smith     Level: developer
276947c6ae99SBarry Smith 
2770e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
277147c6ae99SBarry Smith 
277247c6ae99SBarry Smith @*/
27737087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
277447c6ae99SBarry Smith {
277547c6ae99SBarry Smith   PetscErrorCode ierr;
277647c6ae99SBarry Smith 
277747c6ae99SBarry Smith   PetscFunctionBegin;
2778171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2779ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
278047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
278147c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
278247c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
278347c6ae99SBarry Smith   } else if (dm->ops->refine) {
278447c6ae99SBarry Smith     PetscInt i;
278547c6ae99SBarry Smith 
2786ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
278747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2788ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
278947c6ae99SBarry Smith     }
2790ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
279147c6ae99SBarry Smith   PetscFunctionReturn(0);
279247c6ae99SBarry Smith }
279347c6ae99SBarry Smith 
279447c6ae99SBarry Smith /*@C
279547c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
279647c6ae99SBarry Smith 
279747c6ae99SBarry Smith     Collective on DM
279847c6ae99SBarry Smith 
279947c6ae99SBarry Smith     Input Parameter:
280047c6ae99SBarry Smith +   dm - the DM object
280147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
280247c6ae99SBarry Smith 
280347c6ae99SBarry Smith     Output Parameter:
280447c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
280547c6ae99SBarry Smith 
280647c6ae99SBarry Smith     Level: developer
280747c6ae99SBarry Smith 
2808e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
280947c6ae99SBarry Smith 
281047c6ae99SBarry Smith @*/
28117087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
281247c6ae99SBarry Smith {
281347c6ae99SBarry Smith   PetscErrorCode ierr;
281447c6ae99SBarry Smith 
281547c6ae99SBarry Smith   PetscFunctionBegin;
2816171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2817ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
281847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
281947c6ae99SBarry Smith   PetscValidPointer(dmc,3);
282047c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
282147c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
282247c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
282347c6ae99SBarry Smith     PetscInt i;
282447c6ae99SBarry Smith 
2825ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
282647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2827ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
282847c6ae99SBarry Smith     }
2829ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
283047c6ae99SBarry Smith   PetscFunctionReturn(0);
283147c6ae99SBarry Smith }
283247c6ae99SBarry Smith 
283347c6ae99SBarry Smith /*@
2834e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
283547c6ae99SBarry Smith    grids associated with two DMs.
283647c6ae99SBarry Smith 
283747c6ae99SBarry Smith    Collective on DM
283847c6ae99SBarry Smith 
283947c6ae99SBarry Smith    Input Parameters:
284047c6ae99SBarry Smith +  dmc - the coarse grid DM
284147c6ae99SBarry Smith -  dmf - the fine grid DM
284247c6ae99SBarry Smith 
284347c6ae99SBarry Smith    Output Parameters:
284447c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
284547c6ae99SBarry Smith 
284647c6ae99SBarry Smith    Level: intermediate
284747c6ae99SBarry Smith 
284847c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
284947c6ae99SBarry Smith 
2850e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
285147c6ae99SBarry Smith @*/
2852e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
285347c6ae99SBarry Smith {
285447c6ae99SBarry Smith   PetscErrorCode ierr;
285547c6ae99SBarry Smith 
285647c6ae99SBarry Smith   PetscFunctionBegin;
2857171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2858171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
285947c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
286047c6ae99SBarry Smith   PetscFunctionReturn(0);
286147c6ae99SBarry Smith }
286247c6ae99SBarry Smith 
28631a266240SBarry Smith /*@C
28641a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
28651a266240SBarry Smith 
28661a266240SBarry Smith     Not Collective
28671a266240SBarry Smith 
28681a266240SBarry Smith     Input Parameters:
28691a266240SBarry Smith +   dm - the DM object
28701a266240SBarry Smith -   destroy - the destroy function
28711a266240SBarry Smith 
28721a266240SBarry Smith     Level: intermediate
28731a266240SBarry Smith 
2874e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
28751a266240SBarry Smith 
2876f07f9ceaSJed Brown @*/
28771a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
28781a266240SBarry Smith {
28791a266240SBarry Smith   PetscFunctionBegin;
2880171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28811a266240SBarry Smith   dm->ctxdestroy = destroy;
28821a266240SBarry Smith   PetscFunctionReturn(0);
28831a266240SBarry Smith }
28841a266240SBarry Smith 
2885b07ff414SBarry Smith /*@
28861b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
288747c6ae99SBarry Smith 
288847c6ae99SBarry Smith     Not Collective
288947c6ae99SBarry Smith 
289047c6ae99SBarry Smith     Input Parameters:
289147c6ae99SBarry Smith +   dm - the DM object
289247c6ae99SBarry Smith -   ctx - the user context
289347c6ae99SBarry Smith 
289447c6ae99SBarry Smith     Level: intermediate
289547c6ae99SBarry Smith 
2896e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
289747c6ae99SBarry Smith 
289847c6ae99SBarry Smith @*/
28991b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
290047c6ae99SBarry Smith {
290147c6ae99SBarry Smith   PetscFunctionBegin;
2902171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
290347c6ae99SBarry Smith   dm->ctx = ctx;
290447c6ae99SBarry Smith   PetscFunctionReturn(0);
290547c6ae99SBarry Smith }
290647c6ae99SBarry Smith 
290747c6ae99SBarry Smith /*@
29081b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
290947c6ae99SBarry Smith 
291047c6ae99SBarry Smith     Not Collective
291147c6ae99SBarry Smith 
291247c6ae99SBarry Smith     Input Parameter:
291347c6ae99SBarry Smith .   dm - the DM object
291447c6ae99SBarry Smith 
291547c6ae99SBarry Smith     Output Parameter:
291647c6ae99SBarry Smith .   ctx - the user context
291747c6ae99SBarry Smith 
291847c6ae99SBarry Smith     Level: intermediate
291947c6ae99SBarry Smith 
2920e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
292147c6ae99SBarry Smith 
292247c6ae99SBarry Smith @*/
29231b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
292447c6ae99SBarry Smith {
292547c6ae99SBarry Smith   PetscFunctionBegin;
2926171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29271b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
292847c6ae99SBarry Smith   PetscFunctionReturn(0);
292947c6ae99SBarry Smith }
293047c6ae99SBarry Smith 
293108da532bSDmitry Karpeev /*@C
2932df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
293308da532bSDmitry Karpeev 
293408da532bSDmitry Karpeev     Logically Collective on DM
293508da532bSDmitry Karpeev 
293608da532bSDmitry Karpeev     Input Parameter:
293708da532bSDmitry Karpeev +   dm - the DM object
29380298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
293908da532bSDmitry Karpeev 
294008da532bSDmitry Karpeev     Level: intermediate
294108da532bSDmitry Karpeev 
2942835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
294308da532bSDmitry Karpeev          DMSetJacobian()
294408da532bSDmitry Karpeev 
294508da532bSDmitry Karpeev @*/
294608da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
294708da532bSDmitry Karpeev {
294808da532bSDmitry Karpeev   PetscFunctionBegin;
294908da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
295008da532bSDmitry Karpeev   PetscFunctionReturn(0);
295108da532bSDmitry Karpeev }
295208da532bSDmitry Karpeev 
295308da532bSDmitry Karpeev /*@
295408da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
295508da532bSDmitry Karpeev 
295608da532bSDmitry Karpeev     Not Collective
295708da532bSDmitry Karpeev 
295808da532bSDmitry Karpeev     Input Parameter:
295908da532bSDmitry Karpeev .   dm - the DM object to destroy
296008da532bSDmitry Karpeev 
296108da532bSDmitry Karpeev     Output Parameter:
296208da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
296308da532bSDmitry Karpeev 
296408da532bSDmitry Karpeev     Level: developer
296508da532bSDmitry Karpeev 
296674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
296708da532bSDmitry Karpeev 
296808da532bSDmitry Karpeev @*/
296908da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
297008da532bSDmitry Karpeev {
297108da532bSDmitry Karpeev   PetscFunctionBegin;
297208da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
297308da532bSDmitry Karpeev   PetscFunctionReturn(0);
297408da532bSDmitry Karpeev }
297508da532bSDmitry Karpeev 
297608da532bSDmitry Karpeev /*@C
297708da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
297808da532bSDmitry Karpeev 
297908da532bSDmitry Karpeev     Logically Collective on DM
298008da532bSDmitry Karpeev 
298108da532bSDmitry Karpeev     Input Parameters:
2982907376e6SBarry Smith .   dm - the DM object
298308da532bSDmitry Karpeev 
298408da532bSDmitry Karpeev     Output parameters:
298508da532bSDmitry Karpeev +   xl - lower bound
298608da532bSDmitry Karpeev -   xu - upper bound
298708da532bSDmitry Karpeev 
2988907376e6SBarry Smith     Level: advanced
2989907376e6SBarry Smith 
2990907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
299108da532bSDmitry Karpeev 
299274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
299308da532bSDmitry Karpeev 
299408da532bSDmitry Karpeev @*/
299508da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
299608da532bSDmitry Karpeev {
299708da532bSDmitry Karpeev   PetscErrorCode ierr;
29985fd66863SKarl Rupp 
299908da532bSDmitry Karpeev   PetscFunctionBegin;
300008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
300108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
300208da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
300308da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
30048865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
300508da532bSDmitry Karpeev   PetscFunctionReturn(0);
300608da532bSDmitry Karpeev }
300708da532bSDmitry Karpeev 
3008b0ae01b7SPeter Brune /*@
3009b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3010b0ae01b7SPeter Brune 
3011b0ae01b7SPeter Brune     Not Collective
3012b0ae01b7SPeter Brune 
3013b0ae01b7SPeter Brune     Input Parameter:
3014b0ae01b7SPeter Brune .   dm - the DM object
3015b0ae01b7SPeter Brune 
3016b0ae01b7SPeter Brune     Output Parameter:
3017b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3018b0ae01b7SPeter Brune 
3019b0ae01b7SPeter Brune     Level: developer
3020b0ae01b7SPeter Brune 
3021b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3022b0ae01b7SPeter Brune 
3023b0ae01b7SPeter Brune @*/
3024b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3025b0ae01b7SPeter Brune {
3026b0ae01b7SPeter Brune   PetscFunctionBegin;
3027b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3028b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3029b0ae01b7SPeter Brune }
3030b0ae01b7SPeter Brune 
30313ad4599aSBarry Smith /*@
30323ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
30333ad4599aSBarry Smith 
30343ad4599aSBarry Smith     Not Collective
30353ad4599aSBarry Smith 
30363ad4599aSBarry Smith     Input Parameter:
30373ad4599aSBarry Smith .   dm - the DM object
30383ad4599aSBarry Smith 
30393ad4599aSBarry Smith     Output Parameter:
30403ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
30413ad4599aSBarry Smith 
30423ad4599aSBarry Smith     Level: developer
30433ad4599aSBarry Smith 
30443ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
30453ad4599aSBarry Smith 
30463ad4599aSBarry Smith @*/
30473ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
30483ad4599aSBarry Smith {
30493ad4599aSBarry Smith   PetscFunctionBegin;
30503ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
30513ad4599aSBarry Smith   PetscFunctionReturn(0);
30523ad4599aSBarry Smith }
30533ad4599aSBarry Smith 
3054a7058e45SLawrence Mitchell 
3055a7058e45SLawrence Mitchell /*@
3056a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3057a7058e45SLawrence Mitchell 
3058a7058e45SLawrence Mitchell     Not Collective
3059a7058e45SLawrence Mitchell 
3060a7058e45SLawrence Mitchell     Input Parameter:
3061a7058e45SLawrence Mitchell .   dm - the DM object
3062a7058e45SLawrence Mitchell 
3063a7058e45SLawrence Mitchell     Output Parameter:
3064a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3065a7058e45SLawrence Mitchell 
3066a7058e45SLawrence Mitchell     Level: developer
3067a7058e45SLawrence Mitchell 
3068a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3069a7058e45SLawrence Mitchell 
3070a7058e45SLawrence Mitchell @*/
3071a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3072a7058e45SLawrence Mitchell {
30734a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3074a7058e45SLawrence Mitchell   PetscFunctionBegin;
30754a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
30764a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3077a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3078a7058e45SLawrence Mitchell }
3079a7058e45SLawrence Mitchell 
3080748fac09SDmitry Karpeev /*@C
308108da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
308208da532bSDmitry Karpeev 
308308da532bSDmitry Karpeev     Collective on DM
308408da532bSDmitry Karpeev 
308508da532bSDmitry Karpeev     Input Parameter:
308608da532bSDmitry Karpeev +   dm - the DM object
30870298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
308808da532bSDmitry Karpeev 
308908da532bSDmitry Karpeev     Level: developer
309008da532bSDmitry Karpeev 
309174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
309208da532bSDmitry Karpeev 
309308da532bSDmitry Karpeev @*/
309408da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
309508da532bSDmitry Karpeev {
309608da532bSDmitry Karpeev   PetscErrorCode ierr;
30975fd66863SKarl Rupp 
309808da532bSDmitry Karpeev   PetscFunctionBegin;
309908da532bSDmitry Karpeev   if (x) {
310008da532bSDmitry Karpeev     if (!dm->x) {
310108da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
310208da532bSDmitry Karpeev     }
310308da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
31048865f1eaSKarl Rupp   } else if (dm->x) {
310508da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
310608da532bSDmitry Karpeev   }
310708da532bSDmitry Karpeev   PetscFunctionReturn(0);
310808da532bSDmitry Karpeev }
310908da532bSDmitry Karpeev 
31100298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3111264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3112264ace61SBarry Smith 
3113264ace61SBarry Smith /*@C
3114264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3115264ace61SBarry Smith 
3116264ace61SBarry Smith   Collective on DM
3117264ace61SBarry Smith 
3118264ace61SBarry Smith   Input Parameters:
3119264ace61SBarry Smith + dm     - The DM object
3120264ace61SBarry Smith - method - The name of the DM type
3121264ace61SBarry Smith 
3122264ace61SBarry Smith   Options Database Key:
3123264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3124264ace61SBarry Smith 
3125264ace61SBarry Smith   Notes:
3126e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3127264ace61SBarry Smith 
3128264ace61SBarry Smith   Level: intermediate
3129264ace61SBarry Smith 
3130264ace61SBarry Smith .keywords: DM, set, type
3131264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3132264ace61SBarry Smith @*/
313319fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3134264ace61SBarry Smith {
3135264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3136264ace61SBarry Smith   PetscBool      match;
3137264ace61SBarry Smith   PetscErrorCode ierr;
3138264ace61SBarry Smith 
3139264ace61SBarry Smith   PetscFunctionBegin;
3140264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3141251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3142264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3143264ace61SBarry Smith 
31440f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
31451c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3146ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3147264ace61SBarry Smith 
3148264ace61SBarry Smith   if (dm->ops->destroy) {
3149264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
31500298fd71SBarry Smith     dm->ops->destroy = NULL;
3151264ace61SBarry Smith   }
3152264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3153264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3154264ace61SBarry Smith   PetscFunctionReturn(0);
3155264ace61SBarry Smith }
3156264ace61SBarry Smith 
3157264ace61SBarry Smith /*@C
3158264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3159264ace61SBarry Smith 
3160264ace61SBarry Smith   Not Collective
3161264ace61SBarry Smith 
3162264ace61SBarry Smith   Input Parameter:
3163264ace61SBarry Smith . dm  - The DM
3164264ace61SBarry Smith 
3165264ace61SBarry Smith   Output Parameter:
3166264ace61SBarry Smith . type - The DM type name
3167264ace61SBarry Smith 
3168264ace61SBarry Smith   Level: intermediate
3169264ace61SBarry Smith 
3170264ace61SBarry Smith .keywords: DM, get, type, name
3171264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3172264ace61SBarry Smith @*/
317319fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3174264ace61SBarry Smith {
3175264ace61SBarry Smith   PetscErrorCode ierr;
3176264ace61SBarry Smith 
3177264ace61SBarry Smith   PetscFunctionBegin;
3178264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3179c959eef4SJed Brown   PetscValidPointer(type,2);
3180607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3181264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3182264ace61SBarry Smith   PetscFunctionReturn(0);
3183264ace61SBarry Smith }
3184264ace61SBarry Smith 
318567a56275SMatthew G Knepley /*@C
318667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
318767a56275SMatthew G Knepley 
318867a56275SMatthew G Knepley   Collective on DM
318967a56275SMatthew G Knepley 
319067a56275SMatthew G Knepley   Input Parameters:
319167a56275SMatthew G Knepley + dm - the DM
319267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
319367a56275SMatthew G Knepley 
319467a56275SMatthew G Knepley   Output Parameter:
319567a56275SMatthew G Knepley . M - pointer to new DM
319667a56275SMatthew G Knepley 
319767a56275SMatthew G Knepley   Notes:
319867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
319967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
320067a56275SMatthew G Knepley   of the input DM.
320167a56275SMatthew G Knepley 
320267a56275SMatthew G Knepley   Level: intermediate
320367a56275SMatthew G Knepley 
320467a56275SMatthew G Knepley .seealso: DMCreate()
320567a56275SMatthew G Knepley @*/
320619fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
320767a56275SMatthew G Knepley {
320867a56275SMatthew G Knepley   DM             B;
320967a56275SMatthew G Knepley   char           convname[256];
3210c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
321167a56275SMatthew G Knepley   PetscErrorCode ierr;
321267a56275SMatthew G Knepley 
321367a56275SMatthew G Knepley   PetscFunctionBegin;
321467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
321567a56275SMatthew G Knepley   PetscValidType(dm,1);
321667a56275SMatthew G Knepley   PetscValidPointer(M,3);
3217251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3218c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3219c067b6caSMatthew G. Knepley   if (sametype) {
3220c067b6caSMatthew G. Knepley     *M   = dm;
3221c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3222c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3223c067b6caSMatthew G. Knepley   } else {
32240298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
322567a56275SMatthew G Knepley 
322667a56275SMatthew G Knepley     /*
322767a56275SMatthew G Knepley        Order of precedence:
322867a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
322967a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
323067a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
323167a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
323267a56275SMatthew G Knepley        5) Use a really basic converter.
323367a56275SMatthew G Knepley     */
323467a56275SMatthew G Knepley 
323567a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3236a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3237a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3238a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3239a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3240a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
32410005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
324267a56275SMatthew G Knepley     if (conv) goto foundconv;
324367a56275SMatthew G Knepley 
324467a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
324582f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
324667a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3247a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3248a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3249a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3250a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3251a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
32520005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
325367a56275SMatthew G Knepley     if (conv) {
3254fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
325567a56275SMatthew G Knepley       goto foundconv;
325667a56275SMatthew G Knepley     }
325767a56275SMatthew G Knepley 
325867a56275SMatthew G Knepley #if 0
325967a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
326067a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3261fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
326267a56275SMatthew G Knepley     if (conv) goto foundconv;
326367a56275SMatthew G Knepley 
326467a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
326567a56275SMatthew G Knepley     if (dm->ops->convert) {
326667a56275SMatthew G Knepley       conv = dm->ops->convert;
326767a56275SMatthew G Knepley     }
326867a56275SMatthew G Knepley     if (conv) goto foundconv;
326967a56275SMatthew G Knepley #endif
327067a56275SMatthew G Knepley 
327167a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
327282f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
327367a56275SMatthew G Knepley 
327467a56275SMatthew G Knepley foundconv:
327567a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
327667a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
327712fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
327890b157c4SStefano Zampini     {
327990b157c4SStefano Zampini       PetscBool             isper;
328012fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
328112fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
328290b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
328390b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
328412fa691eSMatthew G. Knepley     }
328567a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
328667a56275SMatthew G Knepley   }
328767a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
328867a56275SMatthew G Knepley   PetscFunctionReturn(0);
328967a56275SMatthew G Knepley }
3290264ace61SBarry Smith 
3291264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3292264ace61SBarry Smith 
3293264ace61SBarry Smith /*@C
32941c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
32951c84c290SBarry Smith 
32961c84c290SBarry Smith   Not Collective
32971c84c290SBarry Smith 
32981c84c290SBarry Smith   Input Parameters:
32991c84c290SBarry Smith + name        - The name of a new user-defined creation routine
33001c84c290SBarry Smith - create_func - The creation routine itself
33011c84c290SBarry Smith 
33021c84c290SBarry Smith   Notes:
33031c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
33041c84c290SBarry Smith 
33051c84c290SBarry Smith 
33061c84c290SBarry Smith   Sample usage:
33071c84c290SBarry Smith .vb
3308bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
33091c84c290SBarry Smith .ve
33101c84c290SBarry Smith 
33111c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
33121c84c290SBarry Smith .vb
33131c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
33141c84c290SBarry Smith     DMSetType(DM,"my_da");
33151c84c290SBarry Smith .ve
33161c84c290SBarry Smith    or at runtime via the option
33171c84c290SBarry Smith .vb
33181c84c290SBarry Smith     -da_type my_da
33191c84c290SBarry Smith .ve
3320264ace61SBarry Smith 
3321264ace61SBarry Smith   Level: advanced
33221c84c290SBarry Smith 
33231c84c290SBarry Smith .keywords: DM, register
3324bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
33251c84c290SBarry Smith 
3326264ace61SBarry Smith @*/
3327bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3328264ace61SBarry Smith {
3329264ace61SBarry Smith   PetscErrorCode ierr;
3330264ace61SBarry Smith 
3331264ace61SBarry Smith   PetscFunctionBegin;
3332a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3333264ace61SBarry Smith   PetscFunctionReturn(0);
3334264ace61SBarry Smith }
3335264ace61SBarry Smith 
3336b859378eSBarry Smith /*@C
333755849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3338b859378eSBarry Smith 
3339b859378eSBarry Smith   Collective on PetscViewer
3340b859378eSBarry Smith 
3341b859378eSBarry Smith   Input Parameters:
3342b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3343b859378eSBarry Smith            some related function before a call to DMLoad().
3344b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3345b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3346b859378eSBarry Smith 
3347b859378eSBarry Smith    Level: intermediate
3348b859378eSBarry Smith 
3349b859378eSBarry Smith   Notes:
335055849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3351b859378eSBarry Smith 
3352b859378eSBarry Smith   Notes for advanced users:
3353b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3354b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3355b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3356b859378eSBarry Smith   format is
3357b859378eSBarry Smith .vb
3358b859378eSBarry Smith      has not yet been determined
3359b859378eSBarry Smith .ve
3360b859378eSBarry Smith 
3361b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3362b859378eSBarry Smith @*/
3363b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3364b859378eSBarry Smith {
33659331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3366b859378eSBarry Smith   PetscErrorCode ierr;
3367b859378eSBarry Smith 
3368b859378eSBarry Smith   PetscFunctionBegin;
3369b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3370b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
337132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
33729331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
33739331c7a4SMatthew G. Knepley   if (isbinary) {
33749331c7a4SMatthew G. Knepley     PetscInt classid;
33759331c7a4SMatthew G. Knepley     char     type[256];
3376b859378eSBarry Smith 
3377060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
33789200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3379060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
338032c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
33819331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
33829331c7a4SMatthew G. Knepley   } else if (ishdf5) {
33839331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
33849331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3385b859378eSBarry Smith   PetscFunctionReturn(0);
3386b859378eSBarry Smith }
3387b859378eSBarry Smith 
33887da65231SMatthew G Knepley /******************************** FEM Support **********************************/
33897da65231SMatthew G Knepley 
3390a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3391a6dfd86eSKarl Rupp {
33921d47ebbbSSatish Balay   PetscInt       f;
33931b30c384SMatthew G Knepley   PetscErrorCode ierr;
33941b30c384SMatthew G Knepley 
33957da65231SMatthew G Knepley   PetscFunctionBegin;
339674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
33971d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
339857622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
33997da65231SMatthew G Knepley   }
34007da65231SMatthew G Knepley   PetscFunctionReturn(0);
34017da65231SMatthew G Knepley }
34027da65231SMatthew G Knepley 
3403a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3404a6dfd86eSKarl Rupp {
34051b30c384SMatthew G Knepley   PetscInt       f, g;
34067da65231SMatthew G Knepley   PetscErrorCode ierr;
34077da65231SMatthew G Knepley 
34087da65231SMatthew G Knepley   PetscFunctionBegin;
340974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
34101d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
341174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
34121d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3413e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
34147da65231SMatthew G Knepley     }
341574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
34167da65231SMatthew G Knepley   }
34177da65231SMatthew G Knepley   PetscFunctionReturn(0);
34187da65231SMatthew G Knepley }
3419e7c4fc90SDmitry Karpeev 
34206113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3421e759306cSMatthew G. Knepley {
34220c5b8624SToby Isaac   PetscInt          localSize, bs;
34230c5b8624SToby Isaac   PetscMPIInt       size;
34240c5b8624SToby Isaac   Vec               x, xglob;
34250c5b8624SToby Isaac   const PetscScalar *xarray;
3426e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3427e759306cSMatthew G. Knepley 
3428e759306cSMatthew G. Knepley   PetscFunctionBegin;
34299852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3430e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3431e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
34326113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
34330c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
34340c5b8624SToby Isaac   if (size > 1) {
34350c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
34360c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
34370c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
34380c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
34390c5b8624SToby Isaac   } else {
34400c5b8624SToby Isaac     xglob = x;
34410c5b8624SToby Isaac   }
34420c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
34430c5b8624SToby Isaac   if (size > 1) {
34440c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
34450c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
34460c5b8624SToby Isaac   }
3447e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3448e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3449e759306cSMatthew G. Knepley }
3450e759306cSMatthew G. Knepley 
345188ed4aceSMatthew G Knepley /*@
345288ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
345388ed4aceSMatthew G Knepley 
345488ed4aceSMatthew G Knepley   Input Parameter:
345588ed4aceSMatthew G Knepley . dm - The DM
345688ed4aceSMatthew G Knepley 
345788ed4aceSMatthew G Knepley   Output Parameter:
345888ed4aceSMatthew G Knepley . section - The PetscSection
345988ed4aceSMatthew G Knepley 
3460e5893cccSMatthew G. Knepley   Options Database Keys:
3461e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3462e5893cccSMatthew G. Knepley 
346388ed4aceSMatthew G Knepley   Level: intermediate
346488ed4aceSMatthew G Knepley 
346588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
346688ed4aceSMatthew G Knepley 
346788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
346888ed4aceSMatthew G Knepley @*/
34690adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
34700adebc6cSBarry Smith {
3471fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3472fd59a867SMatthew G. Knepley 
347388ed4aceSMatthew G Knepley   PetscFunctionBegin;
347488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
347588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
34762f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
34772f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3478ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
34792f0f8703SMatthew G. Knepley   }
348088ed4aceSMatthew G Knepley   *section = dm->defaultSection;
348188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
348288ed4aceSMatthew G Knepley }
348388ed4aceSMatthew G Knepley 
348488ed4aceSMatthew G Knepley /*@
348588ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
348688ed4aceSMatthew G Knepley 
348788ed4aceSMatthew G Knepley   Input Parameters:
348888ed4aceSMatthew G Knepley + dm - The DM
348988ed4aceSMatthew G Knepley - section - The PetscSection
349088ed4aceSMatthew G Knepley 
349188ed4aceSMatthew G Knepley   Level: intermediate
349288ed4aceSMatthew G Knepley 
349388ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
349488ed4aceSMatthew G Knepley 
349588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
349688ed4aceSMatthew G Knepley @*/
34970adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
34980adebc6cSBarry Smith {
3499c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3500af122d2aSMatthew G Knepley   PetscInt       f;
350188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
350288ed4aceSMatthew G Knepley 
350388ed4aceSMatthew G Knepley   PetscFunctionBegin;
350488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3505c473ab19SMatthew G. Knepley   if (section) {
35061d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
35071d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3508c473ab19SMatthew G. Knepley   }
350988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
351088ed4aceSMatthew G Knepley   dm->defaultSection = section;
3511c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3512af122d2aSMatthew G Knepley   if (numFields) {
3513af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3514af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
35150f21e855SMatthew G. Knepley       PetscObject disc;
3516af122d2aSMatthew G Knepley       const char *name;
3517af122d2aSMatthew G Knepley 
3518af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
35190f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
35200f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3521af122d2aSMatthew G Knepley     }
3522af122d2aSMatthew G Knepley   }
35231d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
35241d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
352588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
352688ed4aceSMatthew G Knepley }
352788ed4aceSMatthew G Knepley 
35289435951eSToby Isaac /*@
35299435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
35309435951eSToby Isaac 
3531e228b242SToby Isaac   not collective
3532e228b242SToby Isaac 
35339435951eSToby Isaac   Input Parameter:
35349435951eSToby Isaac . dm - The DM
35359435951eSToby Isaac 
35369435951eSToby Isaac   Output Parameter:
35379435951eSToby 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.
35389435951eSToby 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.
35399435951eSToby Isaac 
35409435951eSToby Isaac   Level: advanced
35419435951eSToby Isaac 
35429435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
35439435951eSToby Isaac 
35449435951eSToby Isaac .seealso: DMSetDefaultConstraints()
35459435951eSToby Isaac @*/
35469435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
35479435951eSToby Isaac {
35489435951eSToby Isaac   PetscErrorCode ierr;
35499435951eSToby Isaac 
35509435951eSToby Isaac   PetscFunctionBegin;
35519435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35529435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
355345a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
355445a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
35559435951eSToby Isaac   PetscFunctionReturn(0);
35569435951eSToby Isaac }
35579435951eSToby Isaac 
35589435951eSToby Isaac /*@
35599435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
35609435951eSToby Isaac 
35619435951eSToby 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().
35629435951eSToby Isaac 
35639435951eSToby 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.
35649435951eSToby Isaac 
3565e228b242SToby Isaac   collective on dm
3566e228b242SToby Isaac 
35679435951eSToby Isaac   Input Parameters:
35689435951eSToby Isaac + dm - The DM
3569e228b242SToby 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).
3570e228b242SToby 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).
35719435951eSToby Isaac 
35729435951eSToby Isaac   Level: advanced
35739435951eSToby Isaac 
35749435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
35759435951eSToby Isaac 
35769435951eSToby Isaac .seealso: DMGetDefaultConstraints()
35779435951eSToby Isaac @*/
35789435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
35799435951eSToby Isaac {
3580e228b242SToby Isaac   PetscMPIInt result;
35819435951eSToby Isaac   PetscErrorCode ierr;
35829435951eSToby Isaac 
35839435951eSToby Isaac   PetscFunctionBegin;
35849435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3585e228b242SToby Isaac   if (section) {
3586e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3587e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3588f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3589e228b242SToby Isaac   }
3590e228b242SToby Isaac   if (mat) {
3591e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3592e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3593f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3594e228b242SToby Isaac   }
35959435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
35969435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
35979435951eSToby Isaac   dm->defaultConstraintSection = section;
35989435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
35999435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
36009435951eSToby Isaac   dm->defaultConstraintMat = mat;
36019435951eSToby Isaac   PetscFunctionReturn(0);
36029435951eSToby Isaac }
36039435951eSToby Isaac 
3604f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3605507e4973SMatthew G. Knepley /*
3606507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3607507e4973SMatthew G. Knepley 
3608507e4973SMatthew G. Knepley   Input Parameters:
3609507e4973SMatthew G. Knepley + dm - The DM
3610507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3611507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3612507e4973SMatthew G. Knepley 
3613507e4973SMatthew G. Knepley   Level: intermediate
3614507e4973SMatthew G. Knepley 
3615507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3616507e4973SMatthew G. Knepley */
3617f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3618507e4973SMatthew G. Knepley {
3619507e4973SMatthew G. Knepley   MPI_Comm        comm;
3620507e4973SMatthew G. Knepley   PetscLayout     layout;
3621507e4973SMatthew G. Knepley   const PetscInt *ranges;
3622507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3623507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3624507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3625507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3626507e4973SMatthew G. Knepley 
3627507e4973SMatthew G. Knepley   PetscFunctionBegin;
3628507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3629507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3630507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3631507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3632507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3633507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3634507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3635507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3636507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3637507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3638507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3639507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3640f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3641507e4973SMatthew G. Knepley 
3642507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3643507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3644507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3645507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3646507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3647507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3648507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3649507e4973SMatthew 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;}
3650507e4973SMatthew 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;}
3651507e4973SMatthew G. Knepley     if (gdof < 0) {
3652507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3653507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3654507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3655507e4973SMatthew G. Knepley 
3656507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3657507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3658507e4973SMatthew 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;}
3659507e4973SMatthew G. Knepley       }
3660507e4973SMatthew G. Knepley     }
3661507e4973SMatthew G. Knepley   }
3662507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3663507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3664b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3665507e4973SMatthew G. Knepley   if (!gvalid) {
3666507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3667507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3668507e4973SMatthew G. Knepley   }
3669507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3670507e4973SMatthew G. Knepley }
3671f741bcd2SMatthew G. Knepley #endif
3672507e4973SMatthew G. Knepley 
367388ed4aceSMatthew G Knepley /*@
367488ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
367588ed4aceSMatthew G Knepley 
36768b1ab98fSJed Brown   Collective on DM
36778b1ab98fSJed Brown 
367888ed4aceSMatthew G Knepley   Input Parameter:
367988ed4aceSMatthew G Knepley . dm - The DM
368088ed4aceSMatthew G Knepley 
368188ed4aceSMatthew G Knepley   Output Parameter:
368288ed4aceSMatthew G Knepley . section - The PetscSection
368388ed4aceSMatthew G Knepley 
368488ed4aceSMatthew G Knepley   Level: intermediate
368588ed4aceSMatthew G Knepley 
368688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
368788ed4aceSMatthew G Knepley 
368888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
368988ed4aceSMatthew G Knepley @*/
36900adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
36910adebc6cSBarry Smith {
369288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
369388ed4aceSMatthew G Knepley 
369488ed4aceSMatthew G Knepley   PetscFunctionBegin;
369588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
369688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
369788ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3698fd59a867SMatthew G. Knepley     PetscSection s;
3699fd59a867SMatthew G. Knepley 
3700fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3701fd59a867SMatthew 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");
3702fd59a867SMatthew 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");
370315b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3704cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3705ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3706685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
370788ed4aceSMatthew G Knepley   }
370888ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
370988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
371088ed4aceSMatthew G Knepley }
371188ed4aceSMatthew G Knepley 
3712b21d0597SMatthew G Knepley /*@
3713b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3714b21d0597SMatthew G Knepley 
3715b21d0597SMatthew G Knepley   Input Parameters:
3716b21d0597SMatthew G Knepley + dm - The DM
37175080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3718b21d0597SMatthew G Knepley 
3719b21d0597SMatthew G Knepley   Level: intermediate
3720b21d0597SMatthew G Knepley 
3721b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3722b21d0597SMatthew G Knepley 
3723b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3724b21d0597SMatthew G Knepley @*/
37250adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
37260adebc6cSBarry Smith {
3727b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3728b21d0597SMatthew G Knepley 
3729b21d0597SMatthew G Knepley   PetscFunctionBegin;
3730b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37315080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
37321d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3733b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3734b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3735507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3736f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3737507e4973SMatthew G. Knepley #endif
3738b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3739b21d0597SMatthew G Knepley }
3740b21d0597SMatthew G Knepley 
374188ed4aceSMatthew G Knepley /*@
374288ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
374388ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
374488ed4aceSMatthew G Knepley 
374588ed4aceSMatthew G Knepley   Input Parameter:
374688ed4aceSMatthew G Knepley . dm - The DM
374788ed4aceSMatthew G Knepley 
374888ed4aceSMatthew G Knepley   Output Parameter:
374988ed4aceSMatthew G Knepley . sf - The PetscSF
375088ed4aceSMatthew G Knepley 
375188ed4aceSMatthew G Knepley   Level: intermediate
375288ed4aceSMatthew G Knepley 
375388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
375488ed4aceSMatthew G Knepley 
375588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
375688ed4aceSMatthew G Knepley @*/
37570adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
37580adebc6cSBarry Smith {
375988ed4aceSMatthew G Knepley   PetscInt       nroots;
376088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
376188ed4aceSMatthew G Knepley 
376288ed4aceSMatthew G Knepley   PetscFunctionBegin;
376388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
376488ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
37650298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
376688ed4aceSMatthew G Knepley   if (nroots < 0) {
376788ed4aceSMatthew G Knepley     PetscSection section, gSection;
376888ed4aceSMatthew G Knepley 
376988ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
377031ea6d37SMatthew G Knepley     if (section) {
377188ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
377288ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
377331ea6d37SMatthew G Knepley     } else {
37740298fd71SBarry Smith       *sf = NULL;
377531ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
377631ea6d37SMatthew G Knepley     }
377788ed4aceSMatthew G Knepley   }
377888ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
377988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
378088ed4aceSMatthew G Knepley }
378188ed4aceSMatthew G Knepley 
378288ed4aceSMatthew G Knepley /*@
378388ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
378488ed4aceSMatthew G Knepley 
378588ed4aceSMatthew G Knepley   Input Parameters:
378688ed4aceSMatthew G Knepley + dm - The DM
378788ed4aceSMatthew G Knepley - sf - The PetscSF
378888ed4aceSMatthew G Knepley 
378988ed4aceSMatthew G Knepley   Level: intermediate
379088ed4aceSMatthew G Knepley 
379188ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
379288ed4aceSMatthew G Knepley 
379388ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
379488ed4aceSMatthew G Knepley @*/
37950adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
37960adebc6cSBarry Smith {
379788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
379888ed4aceSMatthew G Knepley 
379988ed4aceSMatthew G Knepley   PetscFunctionBegin;
380088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
380188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
380288ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
380388ed4aceSMatthew G Knepley   dm->defaultSF = sf;
380488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
380588ed4aceSMatthew G Knepley }
380688ed4aceSMatthew G Knepley 
380788ed4aceSMatthew G Knepley /*@C
380888ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
380988ed4aceSMatthew G Knepley   describing the data layout.
381088ed4aceSMatthew G Knepley 
381188ed4aceSMatthew G Knepley   Input Parameters:
381288ed4aceSMatthew G Knepley + dm - The DM
381388ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
381488ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
381588ed4aceSMatthew G Knepley 
381688ed4aceSMatthew G Knepley   Level: intermediate
381788ed4aceSMatthew G Knepley 
381888ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
381988ed4aceSMatthew G Knepley @*/
382088ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
382188ed4aceSMatthew G Knepley {
382282f516ccSBarry Smith   MPI_Comm       comm;
382388ed4aceSMatthew G Knepley   PetscLayout    layout;
382488ed4aceSMatthew G Knepley   const PetscInt *ranges;
382588ed4aceSMatthew G Knepley   PetscInt       *local;
382688ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3827ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
382888ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
382988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
383088ed4aceSMatthew G Knepley 
383188ed4aceSMatthew G Knepley   PetscFunctionBegin;
383282f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
383388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
383488ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
383588ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
383688ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
383788ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
383888ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
383988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
384088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
384188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
384288ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3843ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
38446636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
384588ed4aceSMatthew G Knepley 
38466636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
38476636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3848235fbf56SMatthew 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));
38496636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
385088ed4aceSMatthew G Knepley   }
3851785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3852785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
385388ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
38541f588964SMatthew G Knepley     const PetscInt *cind;
38556636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
385688ed4aceSMatthew G Knepley 
385788ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
385888ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
385988ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
386088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
386188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
38626636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
386388ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
38646636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
38656636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
38666636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3867057b4bcdSMatthew 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);
38686636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
38696636e97aSMatthew G Knepley     }
387088ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
387188ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
387288ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
387388ed4aceSMatthew G Knepley     }
387488ed4aceSMatthew G Knepley     if (gdof < 0) {
38756636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
387688ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
387788ed4aceSMatthew G Knepley 
387805376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
387931d3f06eSJed Brown         if (r < 0) r = -(r+2);
388005376888SMatthew 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);
388188ed4aceSMatthew G Knepley         remote[l].rank  = r;
388288ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
388388ed4aceSMatthew G Knepley       }
388488ed4aceSMatthew G Knepley     } else {
38856636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
388688ed4aceSMatthew G Knepley         remote[l].rank  = rank;
388788ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
388888ed4aceSMatthew G Knepley       }
388988ed4aceSMatthew G Knepley     }
389088ed4aceSMatthew G Knepley   }
38916636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
389288ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
389388ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
389488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
389588ed4aceSMatthew G Knepley }
3896af122d2aSMatthew G Knepley 
3897b21d0597SMatthew G Knepley /*@
3898b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3899b21d0597SMatthew G Knepley 
3900b21d0597SMatthew G Knepley   Input Parameter:
3901b21d0597SMatthew G Knepley . dm - The DM
3902b21d0597SMatthew G Knepley 
3903b21d0597SMatthew G Knepley   Output Parameter:
3904b21d0597SMatthew G Knepley . sf - The PetscSF
3905b21d0597SMatthew G Knepley 
3906b21d0597SMatthew G Knepley   Level: intermediate
3907b21d0597SMatthew G Knepley 
3908b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3909b21d0597SMatthew G Knepley 
3910057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3911b21d0597SMatthew G Knepley @*/
39120adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
39130adebc6cSBarry Smith {
3914b21d0597SMatthew G Knepley   PetscFunctionBegin;
3915b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3916b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3917b21d0597SMatthew G Knepley   *sf = dm->sf;
3918b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3919b21d0597SMatthew G Knepley }
3920b21d0597SMatthew G Knepley 
3921057b4bcdSMatthew G Knepley /*@
3922057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3923057b4bcdSMatthew G Knepley 
3924057b4bcdSMatthew G Knepley   Input Parameters:
3925057b4bcdSMatthew G Knepley + dm - The DM
3926057b4bcdSMatthew G Knepley - sf - The PetscSF
3927057b4bcdSMatthew G Knepley 
3928057b4bcdSMatthew G Knepley   Level: intermediate
3929057b4bcdSMatthew G Knepley 
3930057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3931057b4bcdSMatthew G Knepley @*/
39320adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
39330adebc6cSBarry Smith {
3934057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3935057b4bcdSMatthew G Knepley 
3936057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3937057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3938057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3939057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3940057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3941057b4bcdSMatthew G Knepley   dm->sf = sf;
3942057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3943057b4bcdSMatthew G Knepley }
3944057b4bcdSMatthew G Knepley 
39452764a2aaSMatthew G. Knepley /*@
39462764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
39472764a2aaSMatthew G. Knepley 
39482764a2aaSMatthew G. Knepley   Input Parameter:
39492764a2aaSMatthew G. Knepley . dm - The DM
39502764a2aaSMatthew G. Knepley 
39512764a2aaSMatthew G. Knepley   Output Parameter:
39522764a2aaSMatthew G. Knepley . prob - The PetscDS
39532764a2aaSMatthew G. Knepley 
39542764a2aaSMatthew G. Knepley   Level: developer
39552764a2aaSMatthew G. Knepley 
39562764a2aaSMatthew G. Knepley .seealso: DMSetDS()
39572764a2aaSMatthew G. Knepley @*/
39582764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3959af122d2aSMatthew G Knepley {
3960af122d2aSMatthew G Knepley   PetscFunctionBegin;
3961af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39620f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
39630f21e855SMatthew G. Knepley   *prob = dm->prob;
39640f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
39650f21e855SMatthew G. Knepley }
39660f21e855SMatthew G. Knepley 
39672764a2aaSMatthew G. Knepley /*@
39682764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
39692764a2aaSMatthew G. Knepley 
39702764a2aaSMatthew G. Knepley   Input Parameters:
39712764a2aaSMatthew G. Knepley + dm - The DM
39722764a2aaSMatthew G. Knepley - prob - The PetscDS
39732764a2aaSMatthew G. Knepley 
39742764a2aaSMatthew G. Knepley   Level: developer
39752764a2aaSMatthew G. Knepley 
39762764a2aaSMatthew G. Knepley .seealso: DMGetDS()
39772764a2aaSMatthew G. Knepley @*/
39782764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
39790f21e855SMatthew G. Knepley {
3980f17e8794SMatthew G. Knepley   PetscInt       dimEmbed;
39810f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39820f21e855SMatthew G. Knepley 
39830f21e855SMatthew G. Knepley   PetscFunctionBegin;
39840f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39852764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
398637316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
39872764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
39880f21e855SMatthew G. Knepley   dm->prob = prob;
3989f17e8794SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
3990f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
39910f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
39920f21e855SMatthew G. Knepley }
39930f21e855SMatthew G. Knepley 
39940f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
39950f21e855SMatthew G. Knepley {
39960f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39970f21e855SMatthew G. Knepley 
39980f21e855SMatthew G. Knepley   PetscFunctionBegin;
39990f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40002764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
4001af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4002af122d2aSMatthew G Knepley }
4003af122d2aSMatthew G Knepley 
4004af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4005af122d2aSMatthew G Knepley {
40060f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4007af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4008af122d2aSMatthew G Knepley 
4009af122d2aSMatthew G Knepley   PetscFunctionBegin;
4010af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40112764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
40120f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
40130f21e855SMatthew G. Knepley     PetscContainer obj;
40140f21e855SMatthew G. Knepley 
40150f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
40162764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
40170f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4018af122d2aSMatthew G Knepley   }
4019af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4020af122d2aSMatthew G Knepley }
4021af122d2aSMatthew G Knepley 
4022c1929be8SMatthew G. Knepley /*@
4023c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4024c1929be8SMatthew G. Knepley 
4025c1929be8SMatthew G. Knepley   Not collective
4026c1929be8SMatthew G. Knepley 
4027c1929be8SMatthew G. Knepley   Input Parameters:
4028c1929be8SMatthew G. Knepley + dm - The DM
4029c1929be8SMatthew G. Knepley - f  - The field number
4030c1929be8SMatthew G. Knepley 
4031c1929be8SMatthew G. Knepley   Output Parameter:
4032c1929be8SMatthew G. Knepley . field - The discretization object
4033c1929be8SMatthew G. Knepley 
4034c1929be8SMatthew G. Knepley   Level: developer
4035c1929be8SMatthew G. Knepley 
4036c1929be8SMatthew G. Knepley .seealso: DMSetField()
4037c1929be8SMatthew G. Knepley @*/
4038af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
4039af122d2aSMatthew G Knepley {
40400f21e855SMatthew G. Knepley   PetscErrorCode ierr;
40410f21e855SMatthew G. Knepley 
4042af122d2aSMatthew G Knepley   PetscFunctionBegin;
4043af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40442764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4045decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4046decb47aaSMatthew G. Knepley }
4047decb47aaSMatthew G. Knepley 
4048c1929be8SMatthew G. Knepley /*@
4049c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4050c1929be8SMatthew G. Knepley 
4051c1929be8SMatthew G. Knepley   Logically collective on DM
4052c1929be8SMatthew G. Knepley 
4053c1929be8SMatthew G. Knepley   Input Parameters:
4054c1929be8SMatthew G. Knepley + dm - The DM
4055c1929be8SMatthew G. Knepley . f  - The field number
4056c1929be8SMatthew G. Knepley - field - The discretization object
4057c1929be8SMatthew G. Knepley 
4058c1929be8SMatthew G. Knepley   Level: developer
4059c1929be8SMatthew G. Knepley 
4060c1929be8SMatthew G. Knepley .seealso: DMGetField()
4061c1929be8SMatthew G. Knepley @*/
4062decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
4063decb47aaSMatthew G. Knepley {
4064decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4065decb47aaSMatthew G. Knepley 
4066decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4067decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40682764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4069af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4070af122d2aSMatthew G Knepley }
40716636e97aSMatthew G Knepley 
4072b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4073b64e0483SPeter Brune {
4074b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4075b64e0483SPeter Brune   PetscErrorCode ierr;
4076b64e0483SPeter Brune   Vec coords,ccoords;
40776dbf9973SLawrence Mitchell   Mat inject;
4078b64e0483SPeter Brune   PetscFunctionBegin;
4079b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4080b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4081b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4082b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4083b64e0483SPeter Brune   if (coords && !ccoords) {
4084b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
40856668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
40866dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
40872adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
40886dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4089b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4090b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4091b64e0483SPeter Brune   }
4092b64e0483SPeter Brune   PetscFunctionReturn(0);
4093b64e0483SPeter Brune }
4094b64e0483SPeter Brune 
409503dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
409603dadc2fSPeter Brune {
409703dadc2fSPeter Brune   DM dm_coord,subdm_coord;
409803dadc2fSPeter Brune   PetscErrorCode ierr;
409903dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
410003dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
410103dadc2fSPeter Brune   PetscFunctionBegin;
410203dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
410303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
410403dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
410503dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
410603dadc2fSPeter Brune   if (coords && !ccoords) {
410703dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
41086668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
410903dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
411024640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
411103dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
411203dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
411303dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
411403dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
411503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
411603dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
411703dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
411803dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
411903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
412003dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
412103dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
412203dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
412303dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
412403dadc2fSPeter Brune   }
412503dadc2fSPeter Brune   PetscFunctionReturn(0);
412603dadc2fSPeter Brune }
412703dadc2fSPeter Brune 
4128c73cfb54SMatthew G. Knepley /*@
4129c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4130c73cfb54SMatthew G. Knepley 
4131c73cfb54SMatthew G. Knepley   Not collective
4132c73cfb54SMatthew G. Knepley 
4133c73cfb54SMatthew G. Knepley   Input Parameter:
4134c73cfb54SMatthew G. Knepley . dm - The DM
4135c73cfb54SMatthew G. Knepley 
4136c73cfb54SMatthew G. Knepley   Output Parameter:
4137c73cfb54SMatthew G. Knepley . dim - The topological dimension
4138c73cfb54SMatthew G. Knepley 
4139c73cfb54SMatthew G. Knepley   Level: beginner
4140c73cfb54SMatthew G. Knepley 
4141c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4142c73cfb54SMatthew G. Knepley @*/
4143c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4144c73cfb54SMatthew G. Knepley {
4145c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4146c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4147c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4148c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4149c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4150c73cfb54SMatthew G. Knepley }
4151c73cfb54SMatthew G. Knepley 
4152c73cfb54SMatthew G. Knepley /*@
4153c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4154c73cfb54SMatthew G. Knepley 
4155c73cfb54SMatthew G. Knepley   Collective on dm
4156c73cfb54SMatthew G. Knepley 
4157c73cfb54SMatthew G. Knepley   Input Parameters:
4158c73cfb54SMatthew G. Knepley + dm - The DM
4159c73cfb54SMatthew G. Knepley - dim - The topological dimension
4160c73cfb54SMatthew G. Knepley 
4161c73cfb54SMatthew G. Knepley   Level: beginner
4162c73cfb54SMatthew G. Knepley 
4163c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4164c73cfb54SMatthew G. Knepley @*/
4165c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4166c73cfb54SMatthew G. Knepley {
4167f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4168f17e8794SMatthew G. Knepley 
4169c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4170c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4171c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4172c73cfb54SMatthew G. Knepley   dm->dim = dim;
4173f17e8794SMatthew G. Knepley   if (dm->prob->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dim);CHKERRQ(ierr);}
4174c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4175c73cfb54SMatthew G. Knepley }
4176c73cfb54SMatthew G. Knepley 
4177793f3fe5SMatthew G. Knepley /*@
4178793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4179793f3fe5SMatthew G. Knepley 
4180793f3fe5SMatthew G. Knepley   Collective on DM
4181793f3fe5SMatthew G. Knepley 
4182793f3fe5SMatthew G. Knepley   Input Parameters:
4183793f3fe5SMatthew G. Knepley + dm - the DM
4184793f3fe5SMatthew G. Knepley - dim - the dimension
4185793f3fe5SMatthew G. Knepley 
4186793f3fe5SMatthew G. Knepley   Output Parameters:
4187793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4188793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4189793f3fe5SMatthew G. Knepley 
4190793f3fe5SMatthew G. Knepley   Note:
4191793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4192793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4193793f3fe5SMatthew G. Knepley   then the interval is empty.
4194793f3fe5SMatthew G. Knepley 
4195793f3fe5SMatthew G. Knepley   Level: intermediate
4196793f3fe5SMatthew G. Knepley 
4197793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4198793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4199793f3fe5SMatthew G. Knepley @*/
4200793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4201793f3fe5SMatthew G. Knepley {
4202793f3fe5SMatthew G. Knepley   PetscInt       d;
4203793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4204793f3fe5SMatthew G. Knepley 
4205793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4206793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4207793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4208793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4209793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4210793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4211793f3fe5SMatthew G. Knepley }
4212793f3fe5SMatthew G. Knepley 
42136636e97aSMatthew G Knepley /*@
42146636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
42156636e97aSMatthew G Knepley 
42166636e97aSMatthew G Knepley   Collective on DM
42176636e97aSMatthew G Knepley 
42186636e97aSMatthew G Knepley   Input Parameters:
42196636e97aSMatthew G Knepley + dm - the DM
42206636e97aSMatthew G Knepley - c - coordinate vector
42216636e97aSMatthew G Knepley 
42226636e97aSMatthew G Knepley   Note:
42236636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
42246636e97aSMatthew G Knepley 
42256636e97aSMatthew G Knepley   Level: intermediate
42266636e97aSMatthew G Knepley 
42276636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42286636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
42296636e97aSMatthew G Knepley @*/
42306636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
42316636e97aSMatthew G Knepley {
42326636e97aSMatthew G Knepley   PetscErrorCode ierr;
42336636e97aSMatthew G Knepley 
42346636e97aSMatthew G Knepley   PetscFunctionBegin;
42356636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42366636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
42376636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
42386636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
42396636e97aSMatthew G Knepley   dm->coordinates = c;
42406636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4241b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
424203dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
42436636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42446636e97aSMatthew G Knepley }
42456636e97aSMatthew G Knepley 
42466636e97aSMatthew G Knepley /*@
42476636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
42486636e97aSMatthew G Knepley 
42496636e97aSMatthew G Knepley   Collective on DM
42506636e97aSMatthew G Knepley 
42516636e97aSMatthew G Knepley    Input Parameters:
42526636e97aSMatthew G Knepley +  dm - the DM
42536636e97aSMatthew G Knepley -  c - coordinate vector
42546636e97aSMatthew G Knepley 
42556636e97aSMatthew G Knepley   Note:
42566636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
42576636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
42586636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
42596636e97aSMatthew G Knepley 
42606636e97aSMatthew G Knepley   Level: intermediate
42616636e97aSMatthew G Knepley 
42626636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42636636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
42646636e97aSMatthew G Knepley @*/
42656636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
42666636e97aSMatthew G Knepley {
42676636e97aSMatthew G Knepley   PetscErrorCode ierr;
42686636e97aSMatthew G Knepley 
42696636e97aSMatthew G Knepley   PetscFunctionBegin;
42706636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42716636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
42726636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
42736636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
42748865f1eaSKarl Rupp 
42756636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
42768865f1eaSKarl Rupp 
42776636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
42786636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42796636e97aSMatthew G Knepley }
42806636e97aSMatthew G Knepley 
42816636e97aSMatthew G Knepley /*@
42826636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
42836636e97aSMatthew G Knepley 
42846636e97aSMatthew G Knepley   Not Collective
42856636e97aSMatthew G Knepley 
42866636e97aSMatthew G Knepley   Input Parameter:
42876636e97aSMatthew G Knepley . dm - the DM
42886636e97aSMatthew G Knepley 
42896636e97aSMatthew G Knepley   Output Parameter:
42906636e97aSMatthew G Knepley . c - global coordinate vector
42916636e97aSMatthew G Knepley 
42926636e97aSMatthew G Knepley   Note:
42936636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
42946636e97aSMatthew G Knepley 
42956636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
42966636e97aSMatthew G Knepley 
42976636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
42986636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
42996636e97aSMatthew G Knepley 
43006636e97aSMatthew G Knepley   Level: intermediate
43016636e97aSMatthew G Knepley 
43026636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43036636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
43046636e97aSMatthew G Knepley @*/
43056636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
43066636e97aSMatthew G Knepley {
43076636e97aSMatthew G Knepley   PetscErrorCode ierr;
43086636e97aSMatthew G Knepley 
43096636e97aSMatthew G Knepley   PetscFunctionBegin;
43106636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43116636e97aSMatthew G Knepley   PetscValidPointer(c,2);
43121f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
43130298fd71SBarry Smith     DM cdm = NULL;
43146636e97aSMatthew G Knepley 
43156636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
43166636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
43176636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
43186636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
43196636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
43206636e97aSMatthew G Knepley   }
43216636e97aSMatthew G Knepley   *c = dm->coordinates;
43226636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43236636e97aSMatthew G Knepley }
43246636e97aSMatthew G Knepley 
43256636e97aSMatthew G Knepley /*@
43266636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
43276636e97aSMatthew G Knepley 
43286636e97aSMatthew G Knepley   Collective on DM
43296636e97aSMatthew G Knepley 
43306636e97aSMatthew G Knepley   Input Parameter:
43316636e97aSMatthew G Knepley . dm - the DM
43326636e97aSMatthew G Knepley 
43336636e97aSMatthew G Knepley   Output Parameter:
43346636e97aSMatthew G Knepley . c - coordinate vector
43356636e97aSMatthew G Knepley 
43366636e97aSMatthew G Knepley   Note:
43376636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
43386636e97aSMatthew G Knepley 
43396636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
43406636e97aSMatthew G Knepley 
43416636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
43426636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
43436636e97aSMatthew G Knepley 
43446636e97aSMatthew G Knepley   Level: intermediate
43456636e97aSMatthew G Knepley 
43466636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43476636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
43486636e97aSMatthew G Knepley @*/
43496636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
43506636e97aSMatthew G Knepley {
43516636e97aSMatthew G Knepley   PetscErrorCode ierr;
43526636e97aSMatthew G Knepley 
43536636e97aSMatthew G Knepley   PetscFunctionBegin;
43546636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43556636e97aSMatthew G Knepley   PetscValidPointer(c,2);
43561f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
43570298fd71SBarry Smith     DM cdm = NULL;
43586636e97aSMatthew G Knepley 
43596636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
43606636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
43616636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
43626636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
43636636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
43646636e97aSMatthew G Knepley   }
43656636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
43666636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43676636e97aSMatthew G Knepley }
43686636e97aSMatthew G Knepley 
43696636e97aSMatthew G Knepley /*@
43701cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
43716636e97aSMatthew G Knepley 
43726636e97aSMatthew G Knepley   Collective on DM
43736636e97aSMatthew G Knepley 
43746636e97aSMatthew G Knepley   Input Parameter:
43756636e97aSMatthew G Knepley . dm - the DM
43766636e97aSMatthew G Knepley 
43776636e97aSMatthew G Knepley   Output Parameter:
43786636e97aSMatthew G Knepley . cdm - coordinate DM
43796636e97aSMatthew G Knepley 
43806636e97aSMatthew G Knepley   Level: intermediate
43816636e97aSMatthew G Knepley 
43826636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43831cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
43846636e97aSMatthew G Knepley @*/
43856636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
43866636e97aSMatthew G Knepley {
43876636e97aSMatthew G Knepley   PetscErrorCode ierr;
43886636e97aSMatthew G Knepley 
43896636e97aSMatthew G Knepley   PetscFunctionBegin;
43906636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43916636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
43926636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
439382f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
43946636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
43956636e97aSMatthew G Knepley   }
43966636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
43976636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43986636e97aSMatthew G Knepley }
4399e87bb0d3SMatthew G Knepley 
44001cfe2091SMatthew G. Knepley /*@
44011cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
44021cfe2091SMatthew G. Knepley 
44031cfe2091SMatthew G. Knepley   Logically Collective on DM
44041cfe2091SMatthew G. Knepley 
44051cfe2091SMatthew G. Knepley   Input Parameters:
44061cfe2091SMatthew G. Knepley + dm - the DM
44071cfe2091SMatthew G. Knepley - cdm - coordinate DM
44081cfe2091SMatthew G. Knepley 
44091cfe2091SMatthew G. Knepley   Level: intermediate
44101cfe2091SMatthew G. Knepley 
44111cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44121cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
44131cfe2091SMatthew G. Knepley @*/
44141cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
44151cfe2091SMatthew G. Knepley {
44161cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
44171cfe2091SMatthew G. Knepley 
44181cfe2091SMatthew G. Knepley   PetscFunctionBegin;
44191cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44201cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4421f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
44221cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
44231cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
44241cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
44251cfe2091SMatthew G. Knepley }
44261cfe2091SMatthew G. Knepley 
442746e270d4SMatthew G. Knepley /*@
442846e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
442946e270d4SMatthew G. Knepley 
443046e270d4SMatthew G. Knepley   Not Collective
443146e270d4SMatthew G. Knepley 
443246e270d4SMatthew G. Knepley   Input Parameter:
443346e270d4SMatthew G. Knepley . dm - The DM object
443446e270d4SMatthew G. Knepley 
443546e270d4SMatthew G. Knepley   Output Parameter:
443646e270d4SMatthew G. Knepley . dim - The embedding dimension
443746e270d4SMatthew G. Knepley 
443846e270d4SMatthew G. Knepley   Level: intermediate
443946e270d4SMatthew G. Knepley 
444046e270d4SMatthew G. Knepley .keywords: mesh, coordinates
444146e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
444246e270d4SMatthew G. Knepley @*/
444346e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
444446e270d4SMatthew G. Knepley {
444546e270d4SMatthew G. Knepley   PetscFunctionBegin;
444646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
444746e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
44489a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
44499a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
44509a9a41abSToby Isaac   }
445146e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
445246e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
445346e270d4SMatthew G. Knepley }
445446e270d4SMatthew G. Knepley 
445546e270d4SMatthew G. Knepley /*@
445646e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
445746e270d4SMatthew G. Knepley 
445846e270d4SMatthew G. Knepley   Not Collective
445946e270d4SMatthew G. Knepley 
446046e270d4SMatthew G. Knepley   Input Parameters:
446146e270d4SMatthew G. Knepley + dm  - The DM object
446246e270d4SMatthew G. Knepley - dim - The embedding dimension
446346e270d4SMatthew G. Knepley 
446446e270d4SMatthew G. Knepley   Level: intermediate
446546e270d4SMatthew G. Knepley 
446646e270d4SMatthew G. Knepley .keywords: mesh, coordinates
446746e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
446846e270d4SMatthew G. Knepley @*/
446946e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
447046e270d4SMatthew G. Knepley {
4471f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4472f17e8794SMatthew G. Knepley 
447346e270d4SMatthew G. Knepley   PetscFunctionBegin;
447446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
447546e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
4476f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dimEmbed);CHKERRQ(ierr);
447746e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
447846e270d4SMatthew G. Knepley }
447946e270d4SMatthew G. Knepley 
4480e8abe2deSMatthew G. Knepley /*@
4481e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4482e8abe2deSMatthew G. Knepley 
4483e8abe2deSMatthew G. Knepley   Not Collective
4484e8abe2deSMatthew G. Knepley 
4485e8abe2deSMatthew G. Knepley   Input Parameter:
4486e8abe2deSMatthew G. Knepley . dm - The DM object
4487e8abe2deSMatthew G. Knepley 
4488e8abe2deSMatthew G. Knepley   Output Parameter:
4489e8abe2deSMatthew G. Knepley . section - The PetscSection object
4490e8abe2deSMatthew G. Knepley 
4491e8abe2deSMatthew G. Knepley   Level: intermediate
4492e8abe2deSMatthew G. Knepley 
4493e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4494e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4495e8abe2deSMatthew G. Knepley @*/
4496e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4497e8abe2deSMatthew G. Knepley {
4498e8abe2deSMatthew G. Knepley   DM             cdm;
4499e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4500e8abe2deSMatthew G. Knepley 
4501e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4502e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4503e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4504e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4505e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4506e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4507e8abe2deSMatthew G. Knepley }
4508e8abe2deSMatthew G. Knepley 
4509e8abe2deSMatthew G. Knepley /*@
4510e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4511e8abe2deSMatthew G. Knepley 
4512e8abe2deSMatthew G. Knepley   Not Collective
4513e8abe2deSMatthew G. Knepley 
4514e8abe2deSMatthew G. Knepley   Input Parameters:
4515e8abe2deSMatthew G. Knepley + dm      - The DM object
451646e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4517e8abe2deSMatthew G. Knepley - section - The PetscSection object
4518e8abe2deSMatthew G. Knepley 
4519e8abe2deSMatthew G. Knepley   Level: intermediate
4520e8abe2deSMatthew G. Knepley 
4521e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4522e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4523e8abe2deSMatthew G. Knepley @*/
452446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4525e8abe2deSMatthew G. Knepley {
4526e8abe2deSMatthew G. Knepley   DM             cdm;
4527e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4528e8abe2deSMatthew G. Knepley 
4529e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4530e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
453146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4532e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4533e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
453446e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
45354c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
453646e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
453746e270d4SMatthew G. Knepley 
453846e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
453946e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
454046e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
454146e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
454246e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
454346e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
454446e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
454546e270d4SMatthew G. Knepley     }
45466be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
454746e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
454846e270d4SMatthew G. Knepley   }
4549e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4550e8abe2deSMatthew G. Knepley }
4551e8abe2deSMatthew G. Knepley 
45525dc8c3f7SMatthew G. Knepley /*@C
455390b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
45545dc8c3f7SMatthew G. Knepley 
45555dc8c3f7SMatthew G. Knepley   Input Parameters:
455690b157c4SStefano Zampini . dm      - The DM object
455790b157c4SStefano Zampini 
455890b157c4SStefano Zampini   Output Parameters:
455990b157c4SStefano Zampini + per     - Whether the DM is periodic or not
45605dc8c3f7SMatthew 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
45615dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
45625dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
45635dc8c3f7SMatthew G. Knepley 
45645dc8c3f7SMatthew G. Knepley   Level: developer
45655dc8c3f7SMatthew G. Knepley 
45665dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
45675dc8c3f7SMatthew G. Knepley @*/
456890b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4569c6b900c6SMatthew G. Knepley {
4570c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4571c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
457290b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4573c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4574c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
45755dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4576c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4577c6b900c6SMatthew G. Knepley }
4578c6b900c6SMatthew G. Knepley 
45795dc8c3f7SMatthew G. Knepley /*@C
45805dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
45815dc8c3f7SMatthew G. Knepley 
45825dc8c3f7SMatthew G. Knepley   Input Parameters:
45835dc8c3f7SMatthew G. Knepley + dm      - The DM object
458490b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
45855dc8c3f7SMatthew 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
45865dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
45875dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
45885dc8c3f7SMatthew G. Knepley 
45895dc8c3f7SMatthew G. Knepley   Level: developer
45905dc8c3f7SMatthew G. Knepley 
45915dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
45925dc8c3f7SMatthew G. Knepley @*/
459390b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4594c6b900c6SMatthew G. Knepley {
4595c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4596c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4597c6b900c6SMatthew G. Knepley 
4598c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4599c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
460090b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
460190b157c4SStefano Zampini   if (maxCell) {
460290b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
460390b157c4SStefano Zampini     PetscValidPointer(L,4);
460490b157c4SStefano Zampini     PetscValidPointer(bd,5);
460590b157c4SStefano Zampini   }
46065dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
46075dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
460890b157c4SStefano Zampini   if (maxCell) {
46095dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
46105dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
461190b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
461290b157c4SStefano Zampini   } else {
461390b157c4SStefano Zampini     dm->periodic = per;
461490b157c4SStefano Zampini   }
4615c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4616c6b900c6SMatthew G. Knepley }
4617c6b900c6SMatthew G. Knepley 
46182e17dfb7SMatthew G. Knepley /*@
46192e17dfb7SMatthew 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.
46202e17dfb7SMatthew G. Knepley 
46212e17dfb7SMatthew G. Knepley   Input Parameters:
46222e17dfb7SMatthew G. Knepley + dm     - The DM
462365da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
462465da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
46252e17dfb7SMatthew G. Knepley 
46262e17dfb7SMatthew G. Knepley   Output Parameter:
46272e17dfb7SMatthew G. Knepley . out - The localized coordinate point
46282e17dfb7SMatthew G. Knepley 
46292e17dfb7SMatthew G. Knepley   Level: developer
46302e17dfb7SMatthew G. Knepley 
46312e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
46322e17dfb7SMatthew G. Knepley @*/
463365da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
46342e17dfb7SMatthew G. Knepley {
46352e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
46362e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
46372e17dfb7SMatthew G. Knepley 
46382e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46392e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
46402e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46412e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46422e17dfb7SMatthew G. Knepley   } else {
464365da65dcSMatthew G. Knepley     if (endpoint) {
464465da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4645da3333bfSMatthew 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)) {
4646da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
464765da65dcSMatthew G. Knepley         } else {
4648da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
464965da65dcSMatthew G. Knepley         }
465065da65dcSMatthew G. Knepley       }
465165da65dcSMatthew G. Knepley     } else {
46522e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
46531118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
46542e17dfb7SMatthew G. Knepley       }
46552e17dfb7SMatthew G. Knepley     }
465665da65dcSMatthew G. Knepley   }
46572e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46582e17dfb7SMatthew G. Knepley }
46592e17dfb7SMatthew G. Knepley 
46602e17dfb7SMatthew G. Knepley /*
46612e17dfb7SMatthew 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.
46622e17dfb7SMatthew G. Knepley 
46632e17dfb7SMatthew G. Knepley   Input Parameters:
46642e17dfb7SMatthew G. Knepley + dm     - The DM
46652e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
46662e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
46672e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
46682e17dfb7SMatthew G. Knepley 
46692e17dfb7SMatthew G. Knepley   Output Parameter:
46702e17dfb7SMatthew G. Knepley . out - The localized coordinate point
46712e17dfb7SMatthew G. Knepley 
46722e17dfb7SMatthew G. Knepley   Level: developer
46732e17dfb7SMatthew G. Knepley 
46742e17dfb7SMatthew 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
46752e17dfb7SMatthew G. Knepley 
46762e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
46772e17dfb7SMatthew G. Knepley */
46782e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
46792e17dfb7SMatthew G. Knepley {
46802e17dfb7SMatthew G. Knepley   PetscInt d;
46812e17dfb7SMatthew G. Knepley 
46822e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46832e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46842e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46852e17dfb7SMatthew G. Knepley   } else {
46862e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4687908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
46882e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
46892e17dfb7SMatthew G. Knepley       } else {
46902e17dfb7SMatthew G. Knepley         out[d] = in[d];
46912e17dfb7SMatthew G. Knepley       }
46922e17dfb7SMatthew G. Knepley     }
46932e17dfb7SMatthew G. Knepley   }
46942e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46952e17dfb7SMatthew G. Knepley }
46962e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
46972e17dfb7SMatthew G. Knepley {
46982e17dfb7SMatthew G. Knepley   PetscInt d;
46992e17dfb7SMatthew G. Knepley 
47002e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47012e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
47022e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
47032e17dfb7SMatthew G. Knepley   } else {
47042e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4705908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
47062e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
47072e17dfb7SMatthew G. Knepley       } else {
47082e17dfb7SMatthew G. Knepley         out[d] = in[d];
47092e17dfb7SMatthew G. Knepley       }
47102e17dfb7SMatthew G. Knepley     }
47112e17dfb7SMatthew G. Knepley   }
47122e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47132e17dfb7SMatthew G. Knepley }
47142e17dfb7SMatthew G. Knepley 
47152e17dfb7SMatthew G. Knepley /*
47162e17dfb7SMatthew 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.
47172e17dfb7SMatthew G. Knepley 
47182e17dfb7SMatthew G. Knepley   Input Parameters:
47192e17dfb7SMatthew G. Knepley + dm     - The DM
47202e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
47212e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
47222e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
47232e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
47242e17dfb7SMatthew G. Knepley 
47252e17dfb7SMatthew G. Knepley   Output Parameter:
47262e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
47272e17dfb7SMatthew G. Knepley 
47282e17dfb7SMatthew G. Knepley   Level: developer
47292e17dfb7SMatthew G. Knepley 
47302e17dfb7SMatthew 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
47312e17dfb7SMatthew G. Knepley 
47322e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
47332e17dfb7SMatthew G. Knepley */
47342e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
47352e17dfb7SMatthew G. Knepley {
47362e17dfb7SMatthew G. Knepley   PetscInt d;
47372e17dfb7SMatthew G. Knepley 
47382e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47392e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
47402e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
47412e17dfb7SMatthew G. Knepley   } else {
47422e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4743908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
47442e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
47452e17dfb7SMatthew G. Knepley       } else {
47462e17dfb7SMatthew G. Knepley         out[d] += in[d];
47472e17dfb7SMatthew G. Knepley       }
47482e17dfb7SMatthew G. Knepley     }
47492e17dfb7SMatthew G. Knepley   }
47502e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47512e17dfb7SMatthew G. Knepley }
47522e17dfb7SMatthew G. Knepley 
475336447a5eSToby Isaac /*@
475436447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
475536447a5eSToby Isaac 
475636447a5eSToby Isaac   Input Parameter:
475736447a5eSToby Isaac . dm - The DM
475836447a5eSToby Isaac 
475936447a5eSToby Isaac   Output Parameter:
476036447a5eSToby Isaac   areLocalized - True if localized
476136447a5eSToby Isaac 
476236447a5eSToby Isaac   Level: developer
476336447a5eSToby Isaac 
476436447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
476536447a5eSToby Isaac @*/
476636447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
476736447a5eSToby Isaac {
476836447a5eSToby Isaac   DM             cdm;
476936447a5eSToby Isaac   PetscSection   coordSection;
477036447a5eSToby Isaac   PetscInt       cStart, cEnd, c, sStart, sEnd, dof;
477136447a5eSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
477236447a5eSToby Isaac   PetscErrorCode ierr;
477336447a5eSToby Isaac 
477436447a5eSToby Isaac   PetscFunctionBegin;
477536447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
477692c9c85fSStefano Zampini   if (!dm->periodic) {
47778b09590cSToby Isaac     *areLocalized = PETSC_FALSE;
47788b09590cSToby Isaac     PetscFunctionReturn(0);
47798b09590cSToby Isaac   }
478036447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
478136447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
478236447a5eSToby Isaac   {
478336447a5eSToby Isaac     PetscBool isplex;
478436447a5eSToby Isaac 
478536447a5eSToby Isaac     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
478636447a5eSToby Isaac     if (isplex) {
478736447a5eSToby Isaac       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
478836447a5eSToby Isaac     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
478936447a5eSToby Isaac   }
479036447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
479136447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
47925a42012cSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_FALSE;
479336447a5eSToby Isaac   for (c = cStart; c < cEnd; ++c) {
479436447a5eSToby Isaac     if (c < sStart || c >= sEnd) {
479536447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
479636447a5eSToby Isaac       break;
479736447a5eSToby Isaac     }
479836447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
47995a42012cSToby Isaac     if (dof) {
48005a42012cSToby Isaac       alreadyLocalized = PETSC_TRUE;
480136447a5eSToby Isaac       break;
480236447a5eSToby Isaac     }
480336447a5eSToby Isaac   }
48045a42012cSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
480536447a5eSToby Isaac   *areLocalized = alreadyLocalizedGlobal;
480636447a5eSToby Isaac   PetscFunctionReturn(0);
480736447a5eSToby Isaac }
480836447a5eSToby Isaac 
480936447a5eSToby Isaac 
48102e17dfb7SMatthew G. Knepley /*@
4811492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
48122e17dfb7SMatthew G. Knepley 
48132e17dfb7SMatthew G. Knepley   Input Parameter:
48142e17dfb7SMatthew G. Knepley . dm - The DM
48152e17dfb7SMatthew G. Knepley 
48162e17dfb7SMatthew G. Knepley   Level: developer
48172e17dfb7SMatthew G. Knepley 
48182e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
48192e17dfb7SMatthew G. Knepley @*/
48202e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
48212e17dfb7SMatthew G. Knepley {
48222e17dfb7SMatthew G. Knepley   DM             cdm;
48232e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
48242e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
48253e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
48263e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4827e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
48283e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
48293e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
48302e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
48312e17dfb7SMatthew G. Knepley 
48322e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48332e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
483492c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
4835f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
4836f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
4837f7cbd40bSStefano Zampini 
48382e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
48392e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
48402e17dfb7SMatthew G. Knepley   {
48412e17dfb7SMatthew G. Knepley     PetscBool isplex;
48422e17dfb7SMatthew G. Knepley 
48432e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
48442e17dfb7SMatthew G. Knepley     if (isplex) {
48452e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
48463e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
484769291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
48483e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
48493e922f36SToby Isaac       newStart = vStart;
48503e922f36SToby Isaac       newEnd   = vEnd;
48513e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
48523e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
48533e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
48543e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
48553e922f36SToby Isaac       }
48562e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
48572e17dfb7SMatthew G. Knepley   }
48582e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
485943eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
48602e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
48613e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
4862e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
48633e922f36SToby Isaac 
48642e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
48652e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
48662e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
48672e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
48683e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
48693e922f36SToby Isaac 
487069291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
48713e922f36SToby Isaac   localized = &anchor[bs];
48723e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
48733e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
48743e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
48753e922f36SToby Isaac 
48763e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
48773e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
48783e922f36SToby Isaac       PetscInt     b;
48793e922f36SToby Isaac 
48803e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
48813e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48823e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
48833e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
48843e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
48853e922f36SToby Isaac         for (b = 0; b < bs; b++) {
48863e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
48873e922f36SToby Isaac         }
48883e922f36SToby Isaac         if (b < bs) break;
48893e922f36SToby Isaac       }
48903e922f36SToby Isaac       if (d < dof/bs) {
48913e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
48923e922f36SToby Isaac           PetscInt cdof;
48933e922f36SToby Isaac 
48943e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
48953e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
48963e922f36SToby Isaac         }
48973e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
48983e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
48993e922f36SToby Isaac       }
49003e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49013e922f36SToby Isaac     }
49023e922f36SToby Isaac   }
49033e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
49043e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
490569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
49063e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
490769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
49083e922f36SToby Isaac     PetscFunctionReturn(0);
49093e922f36SToby Isaac   }
49102e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
49112e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
49122e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
49132e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
49142e17dfb7SMatthew G. Knepley   }
49152e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
49162e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
4917c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
49182e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
49192e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
49202e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
49212e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
4922c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
49232e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
49242e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
49252e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
49262e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
49272e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
49282e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
49292e17dfb7SMatthew G. Knepley   }
49303e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
49313e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
49323e922f36SToby Isaac 
49332e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
49342e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
49353e922f36SToby Isaac       PetscInt     b, cdof;
49362e17dfb7SMatthew G. Knepley 
49373e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
49383e922f36SToby Isaac       if (!cdof) continue;
49392e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49402e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
49412e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
49422e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
49432e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49442e17dfb7SMatthew G. Knepley     }
49453e922f36SToby Isaac   }
494669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
494769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
4948c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
49492e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
49502e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
49512e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
49522e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
49532e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
49542e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
49552e17dfb7SMatthew G. Knepley }
49562e17dfb7SMatthew G. Knepley 
4957e87bb0d3SMatthew G Knepley /*@
49583a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
4959e87bb0d3SMatthew G Knepley 
49603a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
4961e87bb0d3SMatthew G Knepley 
4962e87bb0d3SMatthew G Knepley   Input Parameters:
4963e87bb0d3SMatthew G Knepley + dm - The DM
49643a93e3b7SToby Isaac . v - The Vec of points
496562a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
49663a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
4967e87bb0d3SMatthew G Knepley 
496861e3bb9bSMatthew G Knepley   Output Parameter:
496962a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
497062a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
49713a93e3b7SToby Isaac 
4972e87bb0d3SMatthew G Knepley 
4973e87bb0d3SMatthew G Knepley   Level: developer
497461e3bb9bSMatthew G Knepley 
497562a38674SMatthew G. Knepley   Notes:
49763a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
497762a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
49783a93e3b7SToby Isaac 
49793a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
498062a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
49813a93e3b7SToby Isaac 
49823a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
49833a93e3b7SToby Isaac 
498462a38674SMatthew G. Knepley $    const PetscSFNode *cells;
498562a38674SMatthew G. Knepley $    PetscInt           nFound;
498662a38674SMatthew G. Knepley $    const PetscSFNode *found;
498762a38674SMatthew G. Knepley $
498862a38674SMatthew G. Knepley $    PetscSFGetGraph(cells,NULL,&nFound,&found,&cells);
49893a93e3b7SToby Isaac 
49903a93e3b7SToby 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
49913a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
49923a93e3b7SToby Isaac 
499361e3bb9bSMatthew G Knepley .keywords: point location, mesh
499462a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
499561e3bb9bSMatthew G Knepley @*/
499662a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
4997e87bb0d3SMatthew G Knepley {
4998735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4999735aa83eSMatthew G Knepley 
5000e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
5001e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5002e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
5003e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
50043a93e3b7SToby Isaac   if (*cellSF) {
50053a93e3b7SToby Isaac     PetscMPIInt result;
50063a93e3b7SToby Isaac 
5007e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
5008a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
50093a93e3b7SToby 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");
5010e0fc9d1bSMatthew G. Knepley   } else {
50113a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
50123a93e3b7SToby Isaac   }
501347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5014735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
501562a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
501682f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
501747a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5018e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
5019e87bb0d3SMatthew G Knepley }
502014f150ffSMatthew G. Knepley 
5021f4d763aaSMatthew G. Knepley /*@
5022f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
5023f4d763aaSMatthew G. Knepley 
5024f4d763aaSMatthew G. Knepley   Input Parameter:
5025f4d763aaSMatthew G. Knepley . dm - The original DM
5026f4d763aaSMatthew G. Knepley 
5027f4d763aaSMatthew G. Knepley   Output Parameter:
5028f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
5029f4d763aaSMatthew G. Knepley 
5030f4d763aaSMatthew G. Knepley   Level: intermediate
5031f4d763aaSMatthew G. Knepley 
5032f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
5033f4d763aaSMatthew G. Knepley @*/
503414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
503514f150ffSMatthew G. Knepley {
5036c26acbdeSMatthew G. Knepley   PetscSection   section;
50372d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
503814f150ffSMatthew G. Knepley   PetscErrorCode ierr;
503914f150ffSMatthew G. Knepley 
504014f150ffSMatthew G. Knepley   PetscFunctionBegin;
504114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
504214f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
5043c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
5044c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5045127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
50462d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
5047c26acbdeSMatthew G. Knepley     *odm = dm;
5048c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
5049c26acbdeSMatthew G. Knepley   }
505014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
50510305aff6SMatthew G. Knepley     PetscDS      ds;
5052c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
505314f150ffSMatthew G. Knepley     PetscSF      sf;
505414f150ffSMatthew G. Knepley 
505514f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
50560305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
50570305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
505814f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
505914f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
506014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
506114f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
506215b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
506314f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
506414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
506514f150ffSMatthew G. Knepley   }
506614f150ffSMatthew G. Knepley   *odm = dm->dmBC;
506714f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
506814f150ffSMatthew G. Knepley }
5069f4d763aaSMatthew G. Knepley 
5070f4d763aaSMatthew G. Knepley /*@
5071cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5072f4d763aaSMatthew G. Knepley 
5073f4d763aaSMatthew G. Knepley   Input Parameter:
5074f4d763aaSMatthew G. Knepley . dm - The original DM
5075f4d763aaSMatthew G. Knepley 
5076cdb7a50dSMatthew G. Knepley   Output Parameters:
5077cdb7a50dSMatthew G. Knepley + num - The output sequence number
5078cdb7a50dSMatthew G. Knepley - val - The output sequence value
5079f4d763aaSMatthew G. Knepley 
5080f4d763aaSMatthew G. Knepley   Level: intermediate
5081f4d763aaSMatthew G. Knepley 
5082f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5083f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5084f4d763aaSMatthew G. Knepley 
5085f4d763aaSMatthew G. Knepley .seealso: VecView()
5086f4d763aaSMatthew G. Knepley @*/
5087cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5088f4d763aaSMatthew G. Knepley {
5089f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5090f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5091cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5092cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5093f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5094f4d763aaSMatthew G. Knepley }
5095f4d763aaSMatthew G. Knepley 
5096f4d763aaSMatthew G. Knepley /*@
5097cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5098f4d763aaSMatthew G. Knepley 
5099f4d763aaSMatthew G. Knepley   Input Parameters:
5100f4d763aaSMatthew G. Knepley + dm - The original DM
5101cdb7a50dSMatthew G. Knepley . num - The output sequence number
5102cdb7a50dSMatthew G. Knepley - val - The output sequence value
5103f4d763aaSMatthew G. Knepley 
5104f4d763aaSMatthew G. Knepley   Level: intermediate
5105f4d763aaSMatthew G. Knepley 
5106f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5107f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5108f4d763aaSMatthew G. Knepley 
5109f4d763aaSMatthew G. Knepley .seealso: VecView()
5110f4d763aaSMatthew G. Knepley @*/
5111cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5112f4d763aaSMatthew G. Knepley {
5113f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5114f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5115f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5116cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5117cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5118cdb7a50dSMatthew G. Knepley }
5119cdb7a50dSMatthew G. Knepley 
5120cdb7a50dSMatthew G. Knepley /*@C
5121cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5122cdb7a50dSMatthew G. Knepley 
5123cdb7a50dSMatthew G. Knepley   Input Parameters:
5124cdb7a50dSMatthew G. Knepley + dm   - The original DM
5125cdb7a50dSMatthew G. Knepley . name - The sequence name
5126cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5127cdb7a50dSMatthew G. Knepley 
5128cdb7a50dSMatthew G. Knepley   Output Parameter:
5129cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5130cdb7a50dSMatthew G. Knepley 
5131cdb7a50dSMatthew G. Knepley   Level: intermediate
5132cdb7a50dSMatthew G. Knepley 
5133cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5134cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5135cdb7a50dSMatthew G. Knepley 
5136cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5137cdb7a50dSMatthew G. Knepley @*/
5138cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5139cdb7a50dSMatthew G. Knepley {
5140cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5141cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5142cdb7a50dSMatthew G. Knepley 
5143cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5144cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5145cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5146cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5147cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5148cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5149cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5150cdb7a50dSMatthew G. Knepley     PetscScalar value;
5151cdb7a50dSMatthew G. Knepley 
515239d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
51534aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5154cdb7a50dSMatthew G. Knepley #endif
5155cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5156f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5157f4d763aaSMatthew G. Knepley }
51588e4ac7eaSMatthew G. Knepley 
51598e4ac7eaSMatthew G. Knepley /*@
51608e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
51618e4ac7eaSMatthew G. Knepley 
51628e4ac7eaSMatthew G. Knepley   Not collective
51638e4ac7eaSMatthew G. Knepley 
51648e4ac7eaSMatthew G. Knepley   Input Parameter:
51658e4ac7eaSMatthew G. Knepley . dm - The DM
51668e4ac7eaSMatthew G. Knepley 
51678e4ac7eaSMatthew G. Knepley   Output Parameter:
51688e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
51698e4ac7eaSMatthew G. Knepley 
51708e4ac7eaSMatthew G. Knepley   Level: beginner
51718e4ac7eaSMatthew G. Knepley 
51728e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
51738e4ac7eaSMatthew G. Knepley @*/
51748e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
51758e4ac7eaSMatthew G. Knepley {
51768e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
51778e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51788e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
51798e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
51808e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
51818e4ac7eaSMatthew G. Knepley }
51828e4ac7eaSMatthew G. Knepley 
51838e4ac7eaSMatthew G. Knepley /*@
51848e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
51858e4ac7eaSMatthew G. Knepley 
51868e4ac7eaSMatthew G. Knepley   Collective on dm
51878e4ac7eaSMatthew G. Knepley 
51888e4ac7eaSMatthew G. Knepley   Input Parameters:
51898e4ac7eaSMatthew G. Knepley + dm - The DM
51908e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
51918e4ac7eaSMatthew G. Knepley 
51928e4ac7eaSMatthew G. Knepley   Level: beginner
51938e4ac7eaSMatthew G. Knepley 
51948e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
51958e4ac7eaSMatthew G. Knepley @*/
51968e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
51978e4ac7eaSMatthew G. Knepley {
51988e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
51998e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52008e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
52018e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
52028e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
52038e4ac7eaSMatthew G. Knepley }
5204c58f1c22SToby Isaac 
5205c58f1c22SToby Isaac 
5206c58f1c22SToby Isaac /*@C
5207c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5208c58f1c22SToby Isaac 
5209c58f1c22SToby Isaac   Not Collective
5210c58f1c22SToby Isaac 
5211c58f1c22SToby Isaac   Input Parameters:
5212c58f1c22SToby Isaac + dm   - The DM object
5213c58f1c22SToby Isaac - name - The label name
5214c58f1c22SToby Isaac 
5215c58f1c22SToby Isaac   Level: intermediate
5216c58f1c22SToby Isaac 
5217c58f1c22SToby Isaac .keywords: mesh
5218c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5219c58f1c22SToby Isaac @*/
5220c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5221c58f1c22SToby Isaac {
5222c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5223c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5224c58f1c22SToby Isaac   PetscErrorCode ierr;
5225c58f1c22SToby Isaac 
5226c58f1c22SToby Isaac   PetscFunctionBegin;
5227c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5228c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5229c58f1c22SToby Isaac   while (next) {
5230c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5231c58f1c22SToby Isaac     if (flg) break;
5232c58f1c22SToby Isaac     next = next->next;
5233c58f1c22SToby Isaac   }
5234c58f1c22SToby Isaac   if (!flg) {
5235c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5236c58f1c22SToby Isaac 
5237c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5238c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5239c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5240c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5241c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5242c58f1c22SToby Isaac   }
5243c58f1c22SToby Isaac   PetscFunctionReturn(0);
5244c58f1c22SToby Isaac }
5245c58f1c22SToby Isaac 
5246c58f1c22SToby Isaac /*@C
5247c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5248c58f1c22SToby Isaac 
5249c58f1c22SToby Isaac   Not Collective
5250c58f1c22SToby Isaac 
5251c58f1c22SToby Isaac   Input Parameters:
5252c58f1c22SToby Isaac + dm   - The DM object
5253c58f1c22SToby Isaac . name - The label name
5254c58f1c22SToby Isaac - point - The mesh point
5255c58f1c22SToby Isaac 
5256c58f1c22SToby Isaac   Output Parameter:
5257c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5258c58f1c22SToby Isaac 
5259c58f1c22SToby Isaac   Level: beginner
5260c58f1c22SToby Isaac 
5261c58f1c22SToby Isaac .keywords: mesh
5262c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5263c58f1c22SToby Isaac @*/
5264c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5265c58f1c22SToby Isaac {
5266c58f1c22SToby Isaac   DMLabel        label;
5267c58f1c22SToby Isaac   PetscErrorCode ierr;
5268c58f1c22SToby Isaac 
5269c58f1c22SToby Isaac   PetscFunctionBegin;
5270c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5271c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5272c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
527313903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5274c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5275c58f1c22SToby Isaac   PetscFunctionReturn(0);
5276c58f1c22SToby Isaac }
5277c58f1c22SToby Isaac 
5278c58f1c22SToby Isaac /*@C
5279c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5280c58f1c22SToby Isaac 
5281c58f1c22SToby Isaac   Not Collective
5282c58f1c22SToby Isaac 
5283c58f1c22SToby Isaac   Input Parameters:
5284c58f1c22SToby Isaac + dm   - The DM object
5285c58f1c22SToby Isaac . name - The label name
5286c58f1c22SToby Isaac . point - The mesh point
5287c58f1c22SToby Isaac - value - The label value for this point
5288c58f1c22SToby Isaac 
5289c58f1c22SToby Isaac   Output Parameter:
5290c58f1c22SToby Isaac 
5291c58f1c22SToby Isaac   Level: beginner
5292c58f1c22SToby Isaac 
5293c58f1c22SToby Isaac .keywords: mesh
5294c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5295c58f1c22SToby Isaac @*/
5296c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5297c58f1c22SToby Isaac {
5298c58f1c22SToby Isaac   DMLabel        label;
5299c58f1c22SToby Isaac   PetscErrorCode ierr;
5300c58f1c22SToby Isaac 
5301c58f1c22SToby Isaac   PetscFunctionBegin;
5302c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5303c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5304c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5305c58f1c22SToby Isaac   if (!label) {
5306c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5307c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5308c58f1c22SToby Isaac   }
5309c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5310c58f1c22SToby Isaac   PetscFunctionReturn(0);
5311c58f1c22SToby Isaac }
5312c58f1c22SToby Isaac 
5313c58f1c22SToby Isaac /*@C
5314c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5315c58f1c22SToby Isaac 
5316c58f1c22SToby Isaac   Not Collective
5317c58f1c22SToby Isaac 
5318c58f1c22SToby Isaac   Input Parameters:
5319c58f1c22SToby Isaac + dm   - The DM object
5320c58f1c22SToby Isaac . name - The label name
5321c58f1c22SToby Isaac . point - The mesh point
5322c58f1c22SToby Isaac - value - The label value for this point
5323c58f1c22SToby Isaac 
5324c58f1c22SToby Isaac   Output Parameter:
5325c58f1c22SToby Isaac 
5326c58f1c22SToby Isaac   Level: beginner
5327c58f1c22SToby Isaac 
5328c58f1c22SToby Isaac .keywords: mesh
5329c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5330c58f1c22SToby Isaac @*/
5331c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5332c58f1c22SToby Isaac {
5333c58f1c22SToby Isaac   DMLabel        label;
5334c58f1c22SToby Isaac   PetscErrorCode ierr;
5335c58f1c22SToby Isaac 
5336c58f1c22SToby Isaac   PetscFunctionBegin;
5337c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5338c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5339c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5340c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5341c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5342c58f1c22SToby Isaac   PetscFunctionReturn(0);
5343c58f1c22SToby Isaac }
5344c58f1c22SToby Isaac 
5345c58f1c22SToby Isaac /*@C
5346c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5347c58f1c22SToby Isaac 
5348c58f1c22SToby Isaac   Not Collective
5349c58f1c22SToby Isaac 
5350c58f1c22SToby Isaac   Input Parameters:
5351c58f1c22SToby Isaac + dm   - The DM object
5352c58f1c22SToby Isaac - name - The label name
5353c58f1c22SToby Isaac 
5354c58f1c22SToby Isaac   Output Parameter:
5355c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5356c58f1c22SToby Isaac 
5357c58f1c22SToby Isaac   Level: beginner
5358c58f1c22SToby Isaac 
5359c58f1c22SToby Isaac .keywords: mesh
5360c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5361c58f1c22SToby Isaac @*/
5362c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5363c58f1c22SToby Isaac {
5364c58f1c22SToby Isaac   DMLabel        label;
5365c58f1c22SToby Isaac   PetscErrorCode ierr;
5366c58f1c22SToby Isaac 
5367c58f1c22SToby Isaac   PetscFunctionBegin;
5368c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5369c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5370c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5371c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5372c58f1c22SToby Isaac   *size = 0;
5373c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5374c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5375c58f1c22SToby Isaac   PetscFunctionReturn(0);
5376c58f1c22SToby Isaac }
5377c58f1c22SToby Isaac 
5378c58f1c22SToby Isaac /*@C
5379c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5380c58f1c22SToby Isaac 
5381c58f1c22SToby Isaac   Not Collective
5382c58f1c22SToby Isaac 
5383c58f1c22SToby Isaac   Input Parameters:
5384c58f1c22SToby Isaac + mesh - The DM object
5385c58f1c22SToby Isaac - name - The label name
5386c58f1c22SToby Isaac 
5387c58f1c22SToby Isaac   Output Parameter:
5388c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5389c58f1c22SToby Isaac 
5390c58f1c22SToby Isaac   Level: beginner
5391c58f1c22SToby Isaac 
5392c58f1c22SToby Isaac .keywords: mesh
5393c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5394c58f1c22SToby Isaac @*/
5395c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5396c58f1c22SToby Isaac {
5397c58f1c22SToby Isaac   DMLabel        label;
5398c58f1c22SToby Isaac   PetscErrorCode ierr;
5399c58f1c22SToby Isaac 
5400c58f1c22SToby Isaac   PetscFunctionBegin;
5401c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5402c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5403c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5404c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5405c58f1c22SToby Isaac   *ids = NULL;
5406dab2e251SBlaise Bourdin  if (label) {
5407c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5408dab2e251SBlaise Bourdin   } else {
5409dab2e251SBlaise Bourdin     /* returning an empty IS */
5410dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
5411dab2e251SBlaise Bourdin   }
5412c58f1c22SToby Isaac   PetscFunctionReturn(0);
5413c58f1c22SToby Isaac }
5414c58f1c22SToby Isaac 
5415c58f1c22SToby Isaac /*@C
5416c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5417c58f1c22SToby Isaac 
5418c58f1c22SToby Isaac   Not Collective
5419c58f1c22SToby Isaac 
5420c58f1c22SToby Isaac   Input Parameters:
5421c58f1c22SToby Isaac + dm - The DM object
5422c58f1c22SToby Isaac . name - The label name
5423c58f1c22SToby Isaac - value - The stratum value
5424c58f1c22SToby Isaac 
5425c58f1c22SToby Isaac   Output Parameter:
5426c58f1c22SToby Isaac . size - The stratum size
5427c58f1c22SToby Isaac 
5428c58f1c22SToby Isaac   Level: beginner
5429c58f1c22SToby Isaac 
5430c58f1c22SToby Isaac .keywords: mesh
5431c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5432c58f1c22SToby Isaac @*/
5433c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5434c58f1c22SToby Isaac {
5435c58f1c22SToby Isaac   DMLabel        label;
5436c58f1c22SToby Isaac   PetscErrorCode ierr;
5437c58f1c22SToby Isaac 
5438c58f1c22SToby Isaac   PetscFunctionBegin;
5439c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5440c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5441c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5442c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5443c58f1c22SToby Isaac   *size = 0;
5444c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5445c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5446c58f1c22SToby Isaac   PetscFunctionReturn(0);
5447c58f1c22SToby Isaac }
5448c58f1c22SToby Isaac 
5449c58f1c22SToby Isaac /*@C
5450c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5451c58f1c22SToby Isaac 
5452c58f1c22SToby Isaac   Not Collective
5453c58f1c22SToby Isaac 
5454c58f1c22SToby Isaac   Input Parameters:
5455c58f1c22SToby Isaac + dm - The DM object
5456c58f1c22SToby Isaac . name - The label name
5457c58f1c22SToby Isaac - value - The stratum value
5458c58f1c22SToby Isaac 
5459c58f1c22SToby Isaac   Output Parameter:
5460c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5461c58f1c22SToby Isaac 
5462c58f1c22SToby Isaac   Level: beginner
5463c58f1c22SToby Isaac 
5464c58f1c22SToby Isaac .keywords: mesh
5465c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5466c58f1c22SToby Isaac @*/
5467c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5468c58f1c22SToby Isaac {
5469c58f1c22SToby Isaac   DMLabel        label;
5470c58f1c22SToby Isaac   PetscErrorCode ierr;
5471c58f1c22SToby Isaac 
5472c58f1c22SToby Isaac   PetscFunctionBegin;
5473c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5474c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5475c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5476c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5477c58f1c22SToby Isaac   *points = NULL;
5478c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5479c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5480c58f1c22SToby Isaac   PetscFunctionReturn(0);
5481c58f1c22SToby Isaac }
5482c58f1c22SToby Isaac 
54834de306b1SToby Isaac /*@C
54844de306b1SToby Isaac   DMGetStratumIS - Set the points in a label stratum
54854de306b1SToby Isaac 
54864de306b1SToby Isaac   Not Collective
54874de306b1SToby Isaac 
54884de306b1SToby Isaac   Input Parameters:
54894de306b1SToby Isaac + dm - The DM object
54904de306b1SToby Isaac . name - The label name
54914de306b1SToby Isaac . value - The stratum value
54924de306b1SToby Isaac - points - The stratum points
54934de306b1SToby Isaac 
54944de306b1SToby Isaac   Level: beginner
54954de306b1SToby Isaac 
54964de306b1SToby Isaac .keywords: mesh
54974de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
54984de306b1SToby Isaac @*/
54994de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
55004de306b1SToby Isaac {
55014de306b1SToby Isaac   DMLabel        label;
55024de306b1SToby Isaac   PetscErrorCode ierr;
55034de306b1SToby Isaac 
55044de306b1SToby Isaac   PetscFunctionBegin;
55054de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55064de306b1SToby Isaac   PetscValidCharPointer(name, 2);
55074de306b1SToby Isaac   PetscValidPointer(points, 4);
55084de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
55094de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
55104de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
55114de306b1SToby Isaac   PetscFunctionReturn(0);
55124de306b1SToby Isaac }
55134de306b1SToby Isaac 
5514c58f1c22SToby Isaac /*@C
5515c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5516c58f1c22SToby Isaac 
5517c58f1c22SToby Isaac   Not Collective
5518c58f1c22SToby Isaac 
5519c58f1c22SToby Isaac   Input Parameters:
5520c58f1c22SToby Isaac + dm   - The DM object
5521c58f1c22SToby Isaac . name - The label name
5522c58f1c22SToby Isaac - value - The label value for this point
5523c58f1c22SToby Isaac 
5524c58f1c22SToby Isaac   Output Parameter:
5525c58f1c22SToby Isaac 
5526c58f1c22SToby Isaac   Level: beginner
5527c58f1c22SToby Isaac 
5528c58f1c22SToby Isaac .keywords: mesh
5529c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5530c58f1c22SToby Isaac @*/
5531c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5532c58f1c22SToby Isaac {
5533c58f1c22SToby Isaac   DMLabel        label;
5534c58f1c22SToby Isaac   PetscErrorCode ierr;
5535c58f1c22SToby Isaac 
5536c58f1c22SToby Isaac   PetscFunctionBegin;
5537c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5538c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5539c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5540c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5541c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5542c58f1c22SToby Isaac   PetscFunctionReturn(0);
5543c58f1c22SToby Isaac }
5544c58f1c22SToby Isaac 
5545c58f1c22SToby Isaac /*@
5546c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5547c58f1c22SToby Isaac 
5548c58f1c22SToby Isaac   Not Collective
5549c58f1c22SToby Isaac 
5550c58f1c22SToby Isaac   Input Parameter:
5551c58f1c22SToby Isaac . dm   - The DM object
5552c58f1c22SToby Isaac 
5553c58f1c22SToby Isaac   Output Parameter:
5554c58f1c22SToby Isaac . numLabels - the number of Labels
5555c58f1c22SToby Isaac 
5556c58f1c22SToby Isaac   Level: intermediate
5557c58f1c22SToby Isaac 
5558c58f1c22SToby Isaac .keywords: mesh
5559c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5560c58f1c22SToby Isaac @*/
5561c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5562c58f1c22SToby Isaac {
5563c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5564c58f1c22SToby Isaac   PetscInt  n    = 0;
5565c58f1c22SToby Isaac 
5566c58f1c22SToby Isaac   PetscFunctionBegin;
5567c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5568c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5569c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5570c58f1c22SToby Isaac   *numLabels = n;
5571c58f1c22SToby Isaac   PetscFunctionReturn(0);
5572c58f1c22SToby Isaac }
5573c58f1c22SToby Isaac 
5574c58f1c22SToby Isaac /*@C
5575c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5576c58f1c22SToby Isaac 
5577c58f1c22SToby Isaac   Not Collective
5578c58f1c22SToby Isaac 
5579c58f1c22SToby Isaac   Input Parameters:
5580c58f1c22SToby Isaac + dm - The DM object
5581c58f1c22SToby Isaac - n  - the label number
5582c58f1c22SToby Isaac 
5583c58f1c22SToby Isaac   Output Parameter:
5584c58f1c22SToby Isaac . name - the label name
5585c58f1c22SToby Isaac 
5586c58f1c22SToby Isaac   Level: intermediate
5587c58f1c22SToby Isaac 
5588c58f1c22SToby Isaac .keywords: mesh
5589c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5590c58f1c22SToby Isaac @*/
5591c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5592c58f1c22SToby Isaac {
5593c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5594c58f1c22SToby Isaac   PetscInt  l    = 0;
5595c58f1c22SToby Isaac 
5596c58f1c22SToby Isaac   PetscFunctionBegin;
5597c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5598c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5599c58f1c22SToby Isaac   while (next) {
5600c58f1c22SToby Isaac     if (l == n) {
5601c58f1c22SToby Isaac       *name = next->label->name;
5602c58f1c22SToby Isaac       PetscFunctionReturn(0);
5603c58f1c22SToby Isaac     }
5604c58f1c22SToby Isaac     ++l;
5605c58f1c22SToby Isaac     next = next->next;
5606c58f1c22SToby Isaac   }
5607c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5608c58f1c22SToby Isaac }
5609c58f1c22SToby Isaac 
5610c58f1c22SToby Isaac /*@C
5611c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5612c58f1c22SToby Isaac 
5613c58f1c22SToby Isaac   Not Collective
5614c58f1c22SToby Isaac 
5615c58f1c22SToby Isaac   Input Parameters:
5616c58f1c22SToby Isaac + dm   - The DM object
5617c58f1c22SToby Isaac - name - The label name
5618c58f1c22SToby Isaac 
5619c58f1c22SToby Isaac   Output Parameter:
5620c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5621c58f1c22SToby Isaac 
5622c58f1c22SToby Isaac   Level: intermediate
5623c58f1c22SToby Isaac 
5624c58f1c22SToby Isaac .keywords: mesh
5625c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5626c58f1c22SToby Isaac @*/
5627c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5628c58f1c22SToby Isaac {
5629c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5630c58f1c22SToby Isaac   PetscErrorCode ierr;
5631c58f1c22SToby Isaac 
5632c58f1c22SToby Isaac   PetscFunctionBegin;
5633c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5634c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5635c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5636c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5637c58f1c22SToby Isaac   while (next) {
5638c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5639c58f1c22SToby Isaac     if (*hasLabel) break;
5640c58f1c22SToby Isaac     next = next->next;
5641c58f1c22SToby Isaac   }
5642c58f1c22SToby Isaac   PetscFunctionReturn(0);
5643c58f1c22SToby Isaac }
5644c58f1c22SToby Isaac 
5645c58f1c22SToby Isaac /*@C
5646c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5647c58f1c22SToby Isaac 
5648c58f1c22SToby Isaac   Not Collective
5649c58f1c22SToby Isaac 
5650c58f1c22SToby Isaac   Input Parameters:
5651c58f1c22SToby Isaac + dm   - The DM object
5652c58f1c22SToby Isaac - name - The label name
5653c58f1c22SToby Isaac 
5654c58f1c22SToby Isaac   Output Parameter:
5655c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5656c58f1c22SToby Isaac 
5657c58f1c22SToby Isaac   Level: intermediate
5658c58f1c22SToby Isaac 
5659c58f1c22SToby Isaac .keywords: mesh
5660c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5661c58f1c22SToby Isaac @*/
5662c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5663c58f1c22SToby Isaac {
5664c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5665c58f1c22SToby Isaac   PetscBool      hasLabel;
5666c58f1c22SToby Isaac   PetscErrorCode ierr;
5667c58f1c22SToby Isaac 
5668c58f1c22SToby Isaac   PetscFunctionBegin;
5669c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5670c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5671c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5672c58f1c22SToby Isaac   *label = NULL;
5673c58f1c22SToby Isaac   while (next) {
5674c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5675c58f1c22SToby Isaac     if (hasLabel) {
5676c58f1c22SToby Isaac       *label = next->label;
5677c58f1c22SToby Isaac       break;
5678c58f1c22SToby Isaac     }
5679c58f1c22SToby Isaac     next = next->next;
5680c58f1c22SToby Isaac   }
5681c58f1c22SToby Isaac   PetscFunctionReturn(0);
5682c58f1c22SToby Isaac }
5683c58f1c22SToby Isaac 
5684c58f1c22SToby Isaac /*@C
5685c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5686c58f1c22SToby Isaac 
5687c58f1c22SToby Isaac   Not Collective
5688c58f1c22SToby Isaac 
5689c58f1c22SToby Isaac   Input Parameters:
5690c58f1c22SToby Isaac + dm - The DM object
5691c58f1c22SToby Isaac - n  - the label number
5692c58f1c22SToby Isaac 
5693c58f1c22SToby Isaac   Output Parameter:
5694c58f1c22SToby Isaac . label - the label
5695c58f1c22SToby Isaac 
5696c58f1c22SToby Isaac   Level: intermediate
5697c58f1c22SToby Isaac 
5698c58f1c22SToby Isaac .keywords: mesh
5699c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5700c58f1c22SToby Isaac @*/
5701c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5702c58f1c22SToby Isaac {
5703c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5704c58f1c22SToby Isaac   PetscInt    l    = 0;
5705c58f1c22SToby Isaac 
5706c58f1c22SToby Isaac   PetscFunctionBegin;
5707c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5708c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5709c58f1c22SToby Isaac   while (next) {
5710c58f1c22SToby Isaac     if (l == n) {
5711c58f1c22SToby Isaac       *label = next->label;
5712c58f1c22SToby Isaac       PetscFunctionReturn(0);
5713c58f1c22SToby Isaac     }
5714c58f1c22SToby Isaac     ++l;
5715c58f1c22SToby Isaac     next = next->next;
5716c58f1c22SToby Isaac   }
5717c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5718c58f1c22SToby Isaac }
5719c58f1c22SToby Isaac 
5720c58f1c22SToby Isaac /*@C
5721c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5722c58f1c22SToby Isaac 
5723c58f1c22SToby Isaac   Not Collective
5724c58f1c22SToby Isaac 
5725c58f1c22SToby Isaac   Input Parameters:
5726c58f1c22SToby Isaac + dm   - The DM object
5727c58f1c22SToby Isaac - label - The DMLabel
5728c58f1c22SToby Isaac 
5729c58f1c22SToby Isaac   Level: developer
5730c58f1c22SToby Isaac 
5731c58f1c22SToby Isaac .keywords: mesh
5732c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5733c58f1c22SToby Isaac @*/
5734c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5735c58f1c22SToby Isaac {
5736c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5737c58f1c22SToby Isaac   PetscBool      hasLabel;
5738c58f1c22SToby Isaac   PetscErrorCode ierr;
5739c58f1c22SToby Isaac 
5740c58f1c22SToby Isaac   PetscFunctionBegin;
5741c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5742c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5743c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5744c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5745c58f1c22SToby Isaac   tmpLabel->label  = label;
5746c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5747c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5748c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5749c58f1c22SToby Isaac   PetscFunctionReturn(0);
5750c58f1c22SToby Isaac }
5751c58f1c22SToby Isaac 
5752c58f1c22SToby Isaac /*@C
5753c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5754c58f1c22SToby Isaac 
5755c58f1c22SToby Isaac   Not Collective
5756c58f1c22SToby Isaac 
5757c58f1c22SToby Isaac   Input Parameters:
5758c58f1c22SToby Isaac + dm   - The DM object
5759c58f1c22SToby Isaac - name - The label name
5760c58f1c22SToby Isaac 
5761c58f1c22SToby Isaac   Output Parameter:
5762c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5763c58f1c22SToby Isaac 
5764c58f1c22SToby Isaac   Level: developer
5765c58f1c22SToby Isaac 
5766c58f1c22SToby Isaac .keywords: mesh
5767c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5768c58f1c22SToby Isaac @*/
5769c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5770c58f1c22SToby Isaac {
5771c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5772c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5773c58f1c22SToby Isaac   PetscBool      hasLabel;
5774c58f1c22SToby Isaac   PetscErrorCode ierr;
5775c58f1c22SToby Isaac 
5776c58f1c22SToby Isaac   PetscFunctionBegin;
5777c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5778c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5779c58f1c22SToby Isaac   *label = NULL;
5780c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5781c58f1c22SToby Isaac   while (next) {
5782c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5783c58f1c22SToby Isaac     if (hasLabel) {
5784c58f1c22SToby Isaac       if (last) last->next       = next->next;
5785c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5786c58f1c22SToby Isaac       next->next = NULL;
5787c58f1c22SToby Isaac       *label     = next->label;
5788c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5789c58f1c22SToby Isaac       if (hasLabel) {
5790c58f1c22SToby Isaac         dm->depthLabel = NULL;
5791c58f1c22SToby Isaac       }
5792c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5793c58f1c22SToby Isaac       break;
5794c58f1c22SToby Isaac     }
5795c58f1c22SToby Isaac     last = next;
5796c58f1c22SToby Isaac     next = next->next;
5797c58f1c22SToby Isaac   }
5798c58f1c22SToby Isaac   PetscFunctionReturn(0);
5799c58f1c22SToby Isaac }
5800c58f1c22SToby Isaac 
5801c58f1c22SToby Isaac /*@C
5802c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5803c58f1c22SToby Isaac 
5804c58f1c22SToby Isaac   Not Collective
5805c58f1c22SToby Isaac 
5806c58f1c22SToby Isaac   Input Parameters:
5807c58f1c22SToby Isaac + dm   - The DM object
5808c58f1c22SToby Isaac - name - The label name
5809c58f1c22SToby Isaac 
5810c58f1c22SToby Isaac   Output Parameter:
5811c58f1c22SToby Isaac . output - The flag for output
5812c58f1c22SToby Isaac 
5813c58f1c22SToby Isaac   Level: developer
5814c58f1c22SToby Isaac 
5815c58f1c22SToby Isaac .keywords: mesh
5816c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5817c58f1c22SToby Isaac @*/
5818c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5819c58f1c22SToby Isaac {
5820c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5821c58f1c22SToby Isaac   PetscErrorCode ierr;
5822c58f1c22SToby Isaac 
5823c58f1c22SToby Isaac   PetscFunctionBegin;
5824c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5825c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5826c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5827c58f1c22SToby Isaac   while (next) {
5828c58f1c22SToby Isaac     PetscBool flg;
5829c58f1c22SToby Isaac 
5830c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5831c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5832c58f1c22SToby Isaac     next = next->next;
5833c58f1c22SToby Isaac   }
5834c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5835c58f1c22SToby Isaac }
5836c58f1c22SToby Isaac 
5837c58f1c22SToby Isaac /*@C
5838c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5839c58f1c22SToby Isaac 
5840c58f1c22SToby Isaac   Not Collective
5841c58f1c22SToby Isaac 
5842c58f1c22SToby Isaac   Input Parameters:
5843c58f1c22SToby Isaac + dm     - The DM object
5844c58f1c22SToby Isaac . name   - The label name
5845c58f1c22SToby Isaac - output - The flag for output
5846c58f1c22SToby Isaac 
5847c58f1c22SToby Isaac   Level: developer
5848c58f1c22SToby Isaac 
5849c58f1c22SToby Isaac .keywords: mesh
5850c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5851c58f1c22SToby Isaac @*/
5852c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5853c58f1c22SToby Isaac {
5854c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5855c58f1c22SToby Isaac   PetscErrorCode ierr;
5856c58f1c22SToby Isaac 
5857c58f1c22SToby Isaac   PetscFunctionBegin;
5858c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5859c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5860c58f1c22SToby Isaac   while (next) {
5861c58f1c22SToby Isaac     PetscBool flg;
5862c58f1c22SToby Isaac 
5863c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5864c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5865c58f1c22SToby Isaac     next = next->next;
5866c58f1c22SToby Isaac   }
5867c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5868c58f1c22SToby Isaac }
5869c58f1c22SToby Isaac 
5870c58f1c22SToby Isaac 
5871c58f1c22SToby Isaac /*@
5872c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5873c58f1c22SToby Isaac 
5874c58f1c22SToby Isaac   Collective on DM
5875c58f1c22SToby Isaac 
5876c58f1c22SToby Isaac   Input Parameter:
58772e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5878c58f1c22SToby Isaac 
5879c58f1c22SToby Isaac   Output Parameter:
58802e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5881c58f1c22SToby Isaac 
5882c58f1c22SToby Isaac   Level: intermediate
5883c58f1c22SToby Isaac 
5884c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5885c58f1c22SToby Isaac 
5886c58f1c22SToby Isaac .keywords: mesh
5887c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5888c58f1c22SToby Isaac @*/
5889c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5890c58f1c22SToby Isaac {
5891c58f1c22SToby Isaac   PetscInt       numLabels, l;
5892c58f1c22SToby Isaac   PetscErrorCode ierr;
5893c58f1c22SToby Isaac 
5894c58f1c22SToby Isaac   PetscFunctionBegin;
5895c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5896c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5897c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5898c58f1c22SToby Isaac     DMLabel     label, labelNew;
5899c58f1c22SToby Isaac     const char *name;
5900c58f1c22SToby Isaac     PetscBool   flg;
5901c58f1c22SToby Isaac 
5902c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5903c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5904c58f1c22SToby Isaac     if (flg) continue;
5905c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5906c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5907c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5908c58f1c22SToby Isaac   }
5909c58f1c22SToby Isaac   PetscFunctionReturn(0);
5910c58f1c22SToby Isaac }
5911a8fb8f29SToby Isaac 
5912a8fb8f29SToby Isaac /*@
5913a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5914a8fb8f29SToby Isaac 
5915a8fb8f29SToby Isaac   Input Parameter:
5916a8fb8f29SToby Isaac . dm - The DM object
5917a8fb8f29SToby Isaac 
5918a8fb8f29SToby Isaac   Output Parameter:
5919a8fb8f29SToby Isaac . cdm - The coarse DM
5920a8fb8f29SToby Isaac 
5921a8fb8f29SToby Isaac   Level: intermediate
5922a8fb8f29SToby Isaac 
5923a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5924a8fb8f29SToby Isaac @*/
5925a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5926a8fb8f29SToby Isaac {
5927a8fb8f29SToby Isaac   PetscFunctionBegin;
5928a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5929a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5930a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5931a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5932a8fb8f29SToby Isaac }
5933a8fb8f29SToby Isaac 
5934a8fb8f29SToby Isaac /*@
5935a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5936a8fb8f29SToby Isaac 
5937a8fb8f29SToby Isaac   Input Parameters:
5938a8fb8f29SToby Isaac + dm - The DM object
5939a8fb8f29SToby Isaac - cdm - The coarse DM
5940a8fb8f29SToby Isaac 
5941a8fb8f29SToby Isaac   Level: intermediate
5942a8fb8f29SToby Isaac 
5943a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5944a8fb8f29SToby Isaac @*/
5945a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5946a8fb8f29SToby Isaac {
5947a8fb8f29SToby Isaac   PetscErrorCode ierr;
5948a8fb8f29SToby Isaac 
5949a8fb8f29SToby Isaac   PetscFunctionBegin;
5950a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5951a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5952a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5953a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5954a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5955a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5956a8fb8f29SToby Isaac }
5957a8fb8f29SToby Isaac 
595888bdff64SToby Isaac /*@
595988bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
596088bdff64SToby Isaac 
596188bdff64SToby Isaac   Input Parameter:
596288bdff64SToby Isaac . dm - The DM object
596388bdff64SToby Isaac 
596488bdff64SToby Isaac   Output Parameter:
596588bdff64SToby Isaac . fdm - The fine DM
596688bdff64SToby Isaac 
596788bdff64SToby Isaac   Level: intermediate
596888bdff64SToby Isaac 
596988bdff64SToby Isaac .seealso: DMSetFineDM()
597088bdff64SToby Isaac @*/
597188bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
597288bdff64SToby Isaac {
597388bdff64SToby Isaac   PetscFunctionBegin;
597488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
597588bdff64SToby Isaac   PetscValidPointer(fdm, 2);
597688bdff64SToby Isaac   *fdm = dm->fineMesh;
597788bdff64SToby Isaac   PetscFunctionReturn(0);
597888bdff64SToby Isaac }
597988bdff64SToby Isaac 
598088bdff64SToby Isaac /*@
598188bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
598288bdff64SToby Isaac 
598388bdff64SToby Isaac   Input Parameters:
598488bdff64SToby Isaac + dm - The DM object
598588bdff64SToby Isaac - fdm - The fine DM
598688bdff64SToby Isaac 
598788bdff64SToby Isaac   Level: intermediate
598888bdff64SToby Isaac 
598988bdff64SToby Isaac .seealso: DMGetFineDM()
599088bdff64SToby Isaac @*/
599188bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
599288bdff64SToby Isaac {
599388bdff64SToby Isaac   PetscErrorCode ierr;
599488bdff64SToby Isaac 
599588bdff64SToby Isaac   PetscFunctionBegin;
599688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
599788bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
599888bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
599988bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
600088bdff64SToby Isaac   dm->fineMesh = fdm;
600188bdff64SToby Isaac   PetscFunctionReturn(0);
600288bdff64SToby Isaac }
600388bdff64SToby Isaac 
6004a6ba4734SToby Isaac /*=== DMBoundary code ===*/
6005a6ba4734SToby Isaac 
6006a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
6007a6ba4734SToby Isaac {
6008a6ba4734SToby Isaac   PetscErrorCode ierr;
6009a6ba4734SToby Isaac 
6010a6ba4734SToby Isaac   PetscFunctionBegin;
6011e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
6012a6ba4734SToby Isaac   PetscFunctionReturn(0);
6013a6ba4734SToby Isaac }
6014a6ba4734SToby Isaac 
6015a6ba4734SToby Isaac /*@C
6016a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
6017a6ba4734SToby Isaac 
6018a6ba4734SToby Isaac   Input Parameters:
60194c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
6020f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6021a6ba4734SToby Isaac . name        - The BC name
6022a6ba4734SToby Isaac . labelname   - The label defining constrained points
6023a6ba4734SToby Isaac . field       - The field to constrain
6024a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6025a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6026a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6027a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6028a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6029a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6030a6ba4734SToby Isaac 
6031a6ba4734SToby Isaac   Options Database Keys:
6032a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6033a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6034a6ba4734SToby Isaac 
6035a6ba4734SToby Isaac   Level: developer
6036a6ba4734SToby Isaac 
6037a6ba4734SToby Isaac .seealso: DMGetBoundary()
6038a6ba4734SToby Isaac @*/
6039a30ec4eaSSatish 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)
6040a6ba4734SToby Isaac {
6041a6ba4734SToby Isaac   PetscErrorCode ierr;
6042a6ba4734SToby Isaac 
6043a6ba4734SToby Isaac   PetscFunctionBegin;
6044a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6045f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
6046a6ba4734SToby Isaac   PetscFunctionReturn(0);
6047a6ba4734SToby Isaac }
6048a6ba4734SToby Isaac 
6049a6ba4734SToby Isaac /*@
6050a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6051a6ba4734SToby Isaac 
6052a6ba4734SToby Isaac   Input Parameters:
6053a6ba4734SToby Isaac . dm - The mesh object
6054a6ba4734SToby Isaac 
6055a6ba4734SToby Isaac   Output Parameters:
6056a6ba4734SToby Isaac . numBd - The number of BC
6057a6ba4734SToby Isaac 
6058a6ba4734SToby Isaac   Level: intermediate
6059a6ba4734SToby Isaac 
6060a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6061a6ba4734SToby Isaac @*/
6062a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6063a6ba4734SToby Isaac {
606458ebd649SToby Isaac   PetscErrorCode ierr;
6065a6ba4734SToby Isaac 
6066a6ba4734SToby Isaac   PetscFunctionBegin;
6067a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
606858ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
6069a6ba4734SToby Isaac   PetscFunctionReturn(0);
6070a6ba4734SToby Isaac }
6071a6ba4734SToby Isaac 
6072a6ba4734SToby Isaac /*@C
60731c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
6074a6ba4734SToby Isaac 
6075a6ba4734SToby Isaac   Input Parameters:
6076a6ba4734SToby Isaac + dm          - The mesh object
6077a6ba4734SToby Isaac - bd          - The BC number
6078a6ba4734SToby Isaac 
6079a6ba4734SToby Isaac   Output Parameters:
6080f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6081a6ba4734SToby Isaac . name        - The BC name
6082a6ba4734SToby Isaac . labelname   - The label defining constrained points
6083a6ba4734SToby Isaac . field       - The field to constrain
6084a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6085a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6086a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6087a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6088a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6089a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6090a6ba4734SToby Isaac 
6091a6ba4734SToby Isaac   Options Database Keys:
6092a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6093a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6094a6ba4734SToby Isaac 
6095a6ba4734SToby Isaac   Level: developer
6096a6ba4734SToby Isaac 
6097a6ba4734SToby Isaac .seealso: DMAddBoundary()
6098a6ba4734SToby Isaac @*/
6099a30ec4eaSSatish 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)
6100a6ba4734SToby Isaac {
610158ebd649SToby Isaac   PetscErrorCode ierr;
6102a6ba4734SToby Isaac 
6103a6ba4734SToby Isaac   PetscFunctionBegin;
6104a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6105f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6106a6ba4734SToby Isaac   PetscFunctionReturn(0);
6107a6ba4734SToby Isaac }
6108a6ba4734SToby Isaac 
6109e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6110e6f8dbb6SToby Isaac {
6111dff059c6SToby Isaac   DMBoundary *lastnext;
6112e6f8dbb6SToby Isaac   DSBoundary dsbound;
6113e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6114e6f8dbb6SToby Isaac 
6115e6f8dbb6SToby Isaac   PetscFunctionBegin;
6116e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
611747a1f5adSToby Isaac   if (dm->boundary) {
611847a1f5adSToby Isaac     DMBoundary next = dm->boundary;
611947a1f5adSToby Isaac 
612047a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
612147a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
612247a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
612347a1f5adSToby Isaac     while (next) {
612447a1f5adSToby Isaac       DMBoundary b = next;
612547a1f5adSToby Isaac 
612647a1f5adSToby Isaac       next = b->next;
612747a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6128a6ba4734SToby Isaac     }
612947a1f5adSToby Isaac     dm->boundary = NULL;
6130a6ba4734SToby Isaac   }
613147a1f5adSToby Isaac 
6132dff059c6SToby Isaac   lastnext = &(dm->boundary);
6133e6f8dbb6SToby Isaac   while (dsbound) {
6134e6f8dbb6SToby Isaac     DMBoundary dmbound;
6135e6f8dbb6SToby Isaac 
6136e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6137e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6138e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6139e6f8dbb6SToby Isaac     if (!dmbound->label) PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);
614047a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6141dff059c6SToby Isaac     *lastnext = dmbound;
6142dff059c6SToby Isaac     lastnext = &(dmbound->next);
6143dff059c6SToby Isaac     dsbound = dsbound->next;
6144a6ba4734SToby Isaac   }
6145a6ba4734SToby Isaac   PetscFunctionReturn(0);
6146a6ba4734SToby Isaac }
6147a6ba4734SToby Isaac 
6148a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6149a6ba4734SToby Isaac {
6150b95f2879SToby Isaac   DMBoundary     b;
6151a6ba4734SToby Isaac   PetscErrorCode ierr;
6152a6ba4734SToby Isaac 
6153a6ba4734SToby Isaac   PetscFunctionBegin;
6154a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6155a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6156a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6157e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6158b95f2879SToby Isaac   b = dm->boundary;
6159a6ba4734SToby Isaac   while (b && !(*isBd)) {
6160e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6161e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
61623424c85cSToby Isaac 
61633424c85cSToby Isaac     if (label) {
6164a6ba4734SToby Isaac       PetscInt i;
6165a6ba4734SToby Isaac 
6166e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6167e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6168a6ba4734SToby Isaac       }
6169a6ba4734SToby Isaac     }
6170a6ba4734SToby Isaac     b = b->next;
6171a6ba4734SToby Isaac   }
6172a6ba4734SToby Isaac   PetscFunctionReturn(0);
6173a6ba4734SToby Isaac }
61744d6f44ffSToby Isaac 
61754d6f44ffSToby Isaac /*@C
61764d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
61774d6f44ffSToby Isaac 
61784d6f44ffSToby Isaac   Input Parameters:
61794d6f44ffSToby Isaac + dm      - The DM
61800709b2feSToby Isaac . time    - The time
61814d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
61824d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
61834d6f44ffSToby Isaac - mode    - The insertion mode for values
61844d6f44ffSToby Isaac 
61854d6f44ffSToby Isaac   Output Parameter:
61864d6f44ffSToby Isaac . X - vector
61874d6f44ffSToby Isaac 
61884d6f44ffSToby Isaac    Calling sequence of func:
61890709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
61904d6f44ffSToby Isaac 
61914d6f44ffSToby Isaac +  dim - The spatial dimension
61924d6f44ffSToby Isaac .  x   - The coordinates
61934d6f44ffSToby Isaac .  Nf  - The number of fields
61944d6f44ffSToby Isaac .  u   - The output field values
61954d6f44ffSToby Isaac -  ctx - optional user-defined function context
61964d6f44ffSToby Isaac 
61974d6f44ffSToby Isaac   Level: developer
61984d6f44ffSToby Isaac 
61992716604bSToby Isaac .seealso: DMComputeL2Diff()
62004d6f44ffSToby Isaac @*/
62010709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
62024d6f44ffSToby Isaac {
62034d6f44ffSToby Isaac   Vec            localX;
62044d6f44ffSToby Isaac   PetscErrorCode ierr;
62054d6f44ffSToby Isaac 
62064d6f44ffSToby Isaac   PetscFunctionBegin;
62074d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62084d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
62090709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62104d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
62114d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
62124d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
62134d6f44ffSToby Isaac   PetscFunctionReturn(0);
62144d6f44ffSToby Isaac }
62154d6f44ffSToby Isaac 
62160709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
62174d6f44ffSToby Isaac {
62184d6f44ffSToby Isaac   PetscErrorCode ierr;
62194d6f44ffSToby Isaac 
62204d6f44ffSToby Isaac   PetscFunctionBegin;
62214d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62224d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
62230918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
62240709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62254d6f44ffSToby Isaac   PetscFunctionReturn(0);
62264d6f44ffSToby Isaac }
62274d6f44ffSToby Isaac 
62282c53366bSMatthew 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)
62292c53366bSMatthew G. Knepley {
62302c53366bSMatthew G. Knepley   Vec            localX;
62312c53366bSMatthew G. Knepley   PetscErrorCode ierr;
62322c53366bSMatthew G. Knepley 
62332c53366bSMatthew G. Knepley   PetscFunctionBegin;
62342c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62352c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
62362c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62372c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
62382c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
62392c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
62402c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
62412c53366bSMatthew G. Knepley }
62422c53366bSMatthew G. Knepley 
62431c531cf8SMatthew 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)
62444d6f44ffSToby Isaac {
62454d6f44ffSToby Isaac   PetscErrorCode ierr;
62464d6f44ffSToby Isaac 
62474d6f44ffSToby Isaac   PetscFunctionBegin;
62484d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62494d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
62500918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
62511c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62524d6f44ffSToby Isaac   PetscFunctionReturn(0);
62534d6f44ffSToby Isaac }
62542716604bSToby Isaac 
62558c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
62568c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
62578c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
62588c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6259191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
62608c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
62618c6c5593SMatthew G. Knepley {
62628c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
62638c6c5593SMatthew G. Knepley 
62648c6c5593SMatthew G. Knepley   PetscFunctionBegin;
62658c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62668c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
62678c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
62680918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
62698c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
62708c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
62718c6c5593SMatthew G. Knepley }
62728c6c5593SMatthew G. Knepley 
62731c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
62748c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
62758c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
62768c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6277191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
62788c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
62798c6c5593SMatthew G. Knepley {
62808c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
62818c6c5593SMatthew G. Knepley 
62828c6c5593SMatthew G. Knepley   PetscFunctionBegin;
62838c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62848c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
62858c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
62860918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
62871c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
62888c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
62898c6c5593SMatthew G. Knepley }
62908c6c5593SMatthew G. Knepley 
62912716604bSToby Isaac /*@C
62922716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
62932716604bSToby Isaac 
62942716604bSToby Isaac   Input Parameters:
62952716604bSToby Isaac + dm    - The DM
62960709b2feSToby Isaac . time  - The time
62972716604bSToby Isaac . funcs - The functions to evaluate for each field component
62982716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6299574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
63002716604bSToby Isaac 
63012716604bSToby Isaac   Output Parameter:
63022716604bSToby Isaac . diff - The diff ||u - u_h||_2
63032716604bSToby Isaac 
63042716604bSToby Isaac   Level: developer
63052716604bSToby Isaac 
63061189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
63072716604bSToby Isaac @*/
63080709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
63092716604bSToby Isaac {
63102716604bSToby Isaac   PetscErrorCode ierr;
63112716604bSToby Isaac 
63122716604bSToby Isaac   PetscFunctionBegin;
63132716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6314b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
63150918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
63160709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
63172716604bSToby Isaac   PetscFunctionReturn(0);
63182716604bSToby Isaac }
6319b698f381SToby Isaac 
6320b698f381SToby Isaac /*@C
6321b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6322b698f381SToby Isaac 
6323b698f381SToby Isaac   Input Parameters:
6324b698f381SToby Isaac + dm    - The DM
6325b698f381SToby Isaac , time  - The time
6326b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6327b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6328574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
6329b698f381SToby Isaac - n     - The vector to project along
6330b698f381SToby Isaac 
6331b698f381SToby Isaac   Output Parameter:
6332b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6333b698f381SToby Isaac 
6334b698f381SToby Isaac   Level: developer
6335b698f381SToby Isaac 
6336b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6337b698f381SToby Isaac @*/
6338b698f381SToby 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)
6339b698f381SToby Isaac {
6340b698f381SToby Isaac   PetscErrorCode ierr;
6341b698f381SToby Isaac 
6342b698f381SToby Isaac   PetscFunctionBegin;
6343b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6344b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6345b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6346b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6347b698f381SToby Isaac   PetscFunctionReturn(0);
6348b698f381SToby Isaac }
6349b698f381SToby Isaac 
63502a16baeaSToby Isaac /*@C
63512a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
63522a16baeaSToby Isaac 
63532a16baeaSToby Isaac   Input Parameters:
63542a16baeaSToby Isaac + dm    - The DM
63552a16baeaSToby Isaac . time  - The time
63562a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
63572a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6358574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
63592a16baeaSToby Isaac 
63602a16baeaSToby Isaac   Output Parameter:
63612a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
63622a16baeaSToby Isaac 
63632a16baeaSToby Isaac   Level: developer
63642a16baeaSToby Isaac 
63651189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
63662a16baeaSToby Isaac @*/
63671189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
63682a16baeaSToby Isaac {
63692a16baeaSToby Isaac   PetscErrorCode ierr;
63702a16baeaSToby Isaac 
63712a16baeaSToby Isaac   PetscFunctionBegin;
63722a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63732a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
63740918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
63752a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
63762a16baeaSToby Isaac   PetscFunctionReturn(0);
63772a16baeaSToby Isaac }
63782a16baeaSToby Isaac 
6379df0b854cSToby Isaac /*@C
6380df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6381cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6382df0b854cSToby Isaac 
6383df0b854cSToby Isaac   Collective on dm
6384df0b854cSToby Isaac 
6385df0b854cSToby Isaac   Input parameters:
6386df0b854cSToby Isaac + dm - the pre-adaptation DM object
6387a1b0c543SToby Isaac - label - label with the flags
6388df0b854cSToby Isaac 
6389df0b854cSToby Isaac   Output parameters:
63900d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6391df0b854cSToby Isaac 
6392df0b854cSToby Isaac   Level: intermediate
63930d1cd5e0SMatthew G. Knepley 
63940d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6395df0b854cSToby Isaac @*/
63960d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6397df0b854cSToby Isaac {
6398df0b854cSToby Isaac   PetscErrorCode ierr;
6399df0b854cSToby Isaac 
6400df0b854cSToby Isaac   PetscFunctionBegin;
6401df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6402a1b0c543SToby Isaac   PetscValidPointer(label,2);
64030d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
64040d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
64056f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
64060d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
64070d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
64080d1cd5e0SMatthew G. Knepley }
64090d1cd5e0SMatthew G. Knepley 
64100d1cd5e0SMatthew G. Knepley /*@C
64110d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
64120d1cd5e0SMatthew G. Knepley 
64130d1cd5e0SMatthew G. Knepley   Input Parameters:
64140d1cd5e0SMatthew G. Knepley + dm - The DM object
64150d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
64166f25b0d8SLisandro 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_".
64170d1cd5e0SMatthew G. Knepley 
64180d1cd5e0SMatthew G. Knepley   Output Parameter:
64190d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
64200d1cd5e0SMatthew G. Knepley 
64210d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
64220d1cd5e0SMatthew G. Knepley 
64230d1cd5e0SMatthew G. Knepley   Level: advanced
64240d1cd5e0SMatthew G. Knepley 
64250d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
64260d1cd5e0SMatthew G. Knepley @*/
64270d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
64280d1cd5e0SMatthew G. Knepley {
64290d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
64300d1cd5e0SMatthew G. Knepley 
64310d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
64320d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64330d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
64340d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
64350d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
64366f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
64376f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
64380d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6439df0b854cSToby Isaac   PetscFunctionReturn(0);
6440df0b854cSToby Isaac }
6441c4088d22SMatthew G. Knepley 
6442502a2867SDave May /*@C
6443502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6444502a2867SDave May 
6445502a2867SDave May  Not Collective
6446502a2867SDave May 
6447502a2867SDave May  Input Parameter:
6448502a2867SDave May  . dm    - The DM
6449502a2867SDave May 
6450502a2867SDave May  Output Parameter:
6451502a2867SDave May  . nranks - the number of neighbours
6452502a2867SDave May  . ranks - the neighbors ranks
6453502a2867SDave May 
6454502a2867SDave May  Notes:
6455502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6456502a2867SDave May 
6457502a2867SDave May  Level: beginner
6458502a2867SDave May 
6459dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6460502a2867SDave May @*/
6461502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6462502a2867SDave May {
6463502a2867SDave May   PetscErrorCode ierr;
6464502a2867SDave May 
6465502a2867SDave May   PetscFunctionBegin;
6466502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64670918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
6468502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6469502a2867SDave May   PetscFunctionReturn(0);
6470502a2867SDave May }
6471502a2867SDave May 
6472531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6473531c7667SBarry Smith 
6474531c7667SBarry Smith /*
6475531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6476531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6477531c7667SBarry Smith */
6478531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6479531c7667SBarry Smith {
6480531c7667SBarry Smith   PetscErrorCode ierr;
6481531c7667SBarry Smith 
6482531c7667SBarry Smith   PetscFunctionBegin;
6483531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6484531c7667SBarry Smith     Vec x1local;
6485531c7667SBarry Smith     DM  dm;
6486531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6487531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6488531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6489531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6490531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6491531c7667SBarry Smith     x1   = x1local;
6492531c7667SBarry Smith   }
6493531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6494531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6495531c7667SBarry Smith     DM  dm;
6496531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6497531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6498531c7667SBarry Smith   }
6499531c7667SBarry Smith   PetscFunctionReturn(0);
6500531c7667SBarry Smith }
6501531c7667SBarry Smith 
6502531c7667SBarry Smith /*@
6503531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6504531c7667SBarry Smith 
6505531c7667SBarry Smith     Input Parameter:
6506531c7667SBarry Smith .    coloring - the MatFDColoring object
6507531c7667SBarry Smith 
6508531c7667SBarry Smith     Developer Notes: this routine exists because the PETSc Mat library does not know about the DM objects
6509531c7667SBarry Smith 
65101b266c99SBarry Smith     Level: advanced
65111b266c99SBarry Smith 
6512531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6513531c7667SBarry Smith @*/
6514531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6515531c7667SBarry Smith {
6516531c7667SBarry Smith   PetscFunctionBegin;
6517531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6518531c7667SBarry Smith   PetscFunctionReturn(0);
6519531c7667SBarry Smith }
6520