xref: /petsc/src/dm/interface/dm.c (revision a5801f5210a4703806200fd446442862a331ed88)
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>
5f19dbd58SToby Isaac #include <petscdmfield.h>
60c312b8eSJed Brown #include <petscsf.h>
72764a2aaSMatthew G. Knepley #include <petscds.h>
847c6ae99SBarry Smith 
9732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
10d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
1158cd63d5SVaclav Hapla PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix, DM_Load;
1267a56275SMatthew G Knepley 
13e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
1440967b3bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID","ESSENTIAL","NATURAL","INVALID","INVALID","ESSENTIAL_FIELD","NATURAL_FIELD","INVALID","INVALID","INVALID","NATURAL_RIEMANN","DMBoundaryConditionType","DM_BC_",0};
15412e9a14SMatthew G. Knepley const char *const DMPolytopeTypes[] = {"vertex", "segment", "tensor_segment", "triangle", "quadrilateral", "tensor_quad", "tetrahedron", "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism", "FV_ghost_cell", "unknown", "DMPolytopeType", "DM_POLYTOPE_", 0};
16a4121054SBarry Smith /*@
17de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
18a4121054SBarry Smith 
19a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
20a4121054SBarry Smith    error when you try to use the vector.
21a4121054SBarry Smith 
22d083f849SBarry Smith   Collective
23a4121054SBarry Smith 
24a4121054SBarry Smith   Input Parameter:
25a4121054SBarry Smith . comm - The communicator for the DM object
26a4121054SBarry Smith 
27a4121054SBarry Smith   Output Parameter:
28a4121054SBarry Smith . dm - The DM object
29a4121054SBarry Smith 
30a4121054SBarry Smith   Level: beginner
31a4121054SBarry Smith 
328472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
33a4121054SBarry Smith @*/
347087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
35a4121054SBarry Smith {
36a4121054SBarry Smith   DM             v;
37e5e52638SMatthew G. Knepley   PetscDS        ds;
38a4121054SBarry Smith   PetscErrorCode ierr;
39a4121054SBarry Smith 
40a4121054SBarry Smith   PetscFunctionBegin;
411411c6eeSJed Brown   PetscValidPointer(dm,2);
420298fd71SBarry Smith   *dm = NULL;
43607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
44a4121054SBarry Smith 
4573107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
46e7c4fc90SDmitry Karpeev 
4749be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
4849be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
490298fd71SBarry Smith   v->ltogmap                  = NULL;
501411c6eeSJed Brown   v->bs                       = 1;
51171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5288ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
531bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
54c58f1c22SToby Isaac   v->labels                   = NULL;
5534aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
5634aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
57c58f1c22SToby Isaac   v->depthLabel               = NULL;
58ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
591bb6d2a8SBarry Smith   v->localSection             = NULL;
601bb6d2a8SBarry Smith   v->globalSection            = NULL;
61fba222abSToby Isaac   v->defaultConstraintSection = NULL;
62fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
63c6b900c6SMatthew G. Knepley   v->L                        = NULL;
64c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
655dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
669a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
6796173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
68435a35e8SMatthew G Knepley   {
69435a35e8SMatthew G Knepley     PetscInt i;
70435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
710298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
72f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
73435a35e8SMatthew G Knepley     }
74435a35e8SMatthew G Knepley   }
75e5e52638SMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) v), &ds);CHKERRQ(ierr);
76b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
77e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
7814f150ffSMatthew G. Knepley   v->dmBC = NULL;
79a8fb8f29SToby Isaac   v->coarseMesh = NULL;
80f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
81cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
82c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
83b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
844a7a4c06SLawrence Mitchell 
851411c6eeSJed Brown   *dm = v;
86a4121054SBarry Smith   PetscFunctionReturn(0);
87a4121054SBarry Smith }
88a4121054SBarry Smith 
8938221697SMatthew G. Knepley /*@
9038221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9138221697SMatthew G. Knepley 
92d083f849SBarry Smith   Collective
9338221697SMatthew G. Knepley 
9438221697SMatthew G. Knepley   Input Parameter:
9538221697SMatthew G. Knepley . dm - The original DM object
9638221697SMatthew G. Knepley 
9738221697SMatthew G. Knepley   Output Parameter:
9838221697SMatthew G. Knepley . newdm  - The new DM object
9938221697SMatthew G. Knepley 
10038221697SMatthew G. Knepley   Level: beginner
10138221697SMatthew G. Knepley 
1021cb8cacdSPatrick Sanan   Notes:
1031cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1041cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1051cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1061bb6d2a8SBarry Smith 
10789706ed2SPatrick Sanan   The clone is considered set up iff the original is.
10889706ed2SPatrick Sanan 
10992cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1101bb6d2a8SBarry Smith 
11138221697SMatthew G. Knepley @*/
11238221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11338221697SMatthew G. Knepley {
11438221697SMatthew G. Knepley   PetscSF        sf;
11538221697SMatthew G. Knepley   Vec            coords;
11638221697SMatthew G. Knepley   void          *ctx;
117a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
11838221697SMatthew G. Knepley   PetscErrorCode ierr;
11938221697SMatthew G. Knepley 
12038221697SMatthew G. Knepley   PetscFunctionBegin;
12138221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12238221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12338221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1245d80c0bfSVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE);CHKERRQ(ierr);
125ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
126ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
1271de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1281de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
12938221697SMatthew G. Knepley   if (dm->ops->clone) {
13038221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13138221697SMatthew G. Knepley   }
1323f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13338221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13438221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13538221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
13638221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
137be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
138be4c1c3eSMatthew G. Knepley     DM           ncdm;
139be4c1c3eSMatthew G. Knepley     PetscSection cs;
1405a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
141be4c1c3eSMatthew G. Knepley 
14292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
143be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1445a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1455a0206caSToby Isaac     if (pEndMax >= 0) {
146be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
14783bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
14892fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
149a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
150be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
151be4c1c3eSMatthew G. Knepley     }
152be4c1c3eSMatthew G. Knepley   }
153a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
154a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15538221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
15638221697SMatthew G. Knepley   if (coords) {
15738221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
15838221697SMatthew G. Knepley   } else {
15938221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
16038221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
16138221697SMatthew G. Knepley   }
16290b157c4SStefano Zampini   {
16390b157c4SStefano Zampini     PetscBool             isper;
164c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1655dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
16690b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
16790b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
168c6b900c6SMatthew G. Knepley   }
16934aa8a36SMatthew G. Knepley   {
17034aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
17134aa8a36SMatthew G. Knepley 
17234aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
17334aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
17434aa8a36SMatthew G. Knepley   }
17538221697SMatthew G. Knepley   PetscFunctionReturn(0);
17638221697SMatthew G. Knepley }
17738221697SMatthew G. Knepley 
1789a42bb27SBarry Smith /*@C
179564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1809a42bb27SBarry Smith 
181d083f849SBarry Smith    Logically Collective on da
1829a42bb27SBarry Smith 
1839a42bb27SBarry Smith    Input Parameter:
1849a42bb27SBarry Smith +  da - initial distributed array
185e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1869a42bb27SBarry Smith 
1879a42bb27SBarry Smith    Options Database:
188dd85299cSBarry Smith .   -dm_vec_type ctype
1899a42bb27SBarry Smith 
1909a42bb27SBarry Smith    Level: intermediate
1919a42bb27SBarry Smith 
192a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1939a42bb27SBarry Smith @*/
19419fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1959a42bb27SBarry Smith {
1969a42bb27SBarry Smith   PetscErrorCode ierr;
1979a42bb27SBarry Smith 
1989a42bb27SBarry Smith   PetscFunctionBegin;
1999a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2009a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
20119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2029a42bb27SBarry Smith   PetscFunctionReturn(0);
2039a42bb27SBarry Smith }
2049a42bb27SBarry Smith 
205c0dedaeaSBarry Smith /*@C
206c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
207c0dedaeaSBarry Smith 
208d083f849SBarry Smith    Logically Collective on da
209c0dedaeaSBarry Smith 
210c0dedaeaSBarry Smith    Input Parameter:
211c0dedaeaSBarry Smith .  da - initial distributed array
212c0dedaeaSBarry Smith 
213c0dedaeaSBarry Smith    Output Parameter:
214c0dedaeaSBarry Smith .  ctype - the vector type
215c0dedaeaSBarry Smith 
216c0dedaeaSBarry Smith    Level: intermediate
217c0dedaeaSBarry Smith 
218a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
219c0dedaeaSBarry Smith @*/
220c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
221c0dedaeaSBarry Smith {
222c0dedaeaSBarry Smith   PetscFunctionBegin;
223c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
224c0dedaeaSBarry Smith   *ctype = da->vectype;
225c0dedaeaSBarry Smith   PetscFunctionReturn(0);
226c0dedaeaSBarry Smith }
227c0dedaeaSBarry Smith 
2285f1ad066SMatthew G Knepley /*@
22934f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2305f1ad066SMatthew G Knepley 
2315f1ad066SMatthew G Knepley   Not collective
2325f1ad066SMatthew G Knepley 
2335f1ad066SMatthew G Knepley   Input Parameter:
2345f1ad066SMatthew G Knepley . v - The Vec
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley   Output Parameter:
2375f1ad066SMatthew G Knepley . dm - The DM
2385f1ad066SMatthew G Knepley 
2395f1ad066SMatthew G Knepley   Level: intermediate
2405f1ad066SMatthew G Knepley 
2415f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2425f1ad066SMatthew G Knepley @*/
2435f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2445f1ad066SMatthew G Knepley {
2455f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2465f1ad066SMatthew G Knepley 
2475f1ad066SMatthew G Knepley   PetscFunctionBegin;
2485f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2495f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2505f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2515f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2525f1ad066SMatthew G Knepley }
2535f1ad066SMatthew G Knepley 
2545f1ad066SMatthew G Knepley /*@
255d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2565f1ad066SMatthew G Knepley 
2575f1ad066SMatthew G Knepley   Not collective
2585f1ad066SMatthew G Knepley 
2595f1ad066SMatthew G Knepley   Input Parameters:
2605f1ad066SMatthew G Knepley + v - The Vec
2615f1ad066SMatthew G Knepley - dm - The DM
2625f1ad066SMatthew G Knepley 
263d9805387SMatthew 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.
264d9805387SMatthew G. Knepley 
2655f1ad066SMatthew G Knepley   Level: intermediate
2665f1ad066SMatthew G Knepley 
2675f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2685f1ad066SMatthew G Knepley @*/
2695f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2705f1ad066SMatthew G Knepley {
2715f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2725f1ad066SMatthew G Knepley 
2735f1ad066SMatthew G Knepley   PetscFunctionBegin;
2745f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
275d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2765f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2775f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2785f1ad066SMatthew G Knepley }
2795f1ad066SMatthew G Knepley 
280521d9a4cSLisandro Dalcin /*@C
2818f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2828f1509bcSBarry Smith 
283d083f849SBarry Smith    Logically Collective on dm
2848f1509bcSBarry Smith 
2858f1509bcSBarry Smith    Input Parameters:
2868f1509bcSBarry Smith +  dm - the DM context
2878f1509bcSBarry Smith -  ctype - the matrix type
2888f1509bcSBarry Smith 
2898f1509bcSBarry Smith    Options Database:
2908f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2918f1509bcSBarry Smith 
2928f1509bcSBarry Smith    Level: intermediate
2938f1509bcSBarry Smith 
2948f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2958f1509bcSBarry Smith           DMGetISColoringType()
2968f1509bcSBarry Smith @*/
2978f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
2988f1509bcSBarry Smith {
2998f1509bcSBarry Smith   PetscFunctionBegin;
3008f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3018f1509bcSBarry Smith   dm->coloringtype = ctype;
3028f1509bcSBarry Smith   PetscFunctionReturn(0);
3038f1509bcSBarry Smith }
3048f1509bcSBarry Smith 
3058f1509bcSBarry Smith /*@C
3068f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
307521d9a4cSLisandro Dalcin 
308d083f849SBarry Smith    Logically Collective on dm
309521d9a4cSLisandro Dalcin 
310521d9a4cSLisandro Dalcin    Input Parameter:
3118f1509bcSBarry Smith .  dm - the DM context
3128f1509bcSBarry Smith 
3138f1509bcSBarry Smith    Output Parameter:
3148f1509bcSBarry Smith .  ctype - the matrix type
3158f1509bcSBarry Smith 
3168f1509bcSBarry Smith    Options Database:
3178f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3188f1509bcSBarry Smith 
3198f1509bcSBarry Smith    Level: intermediate
3208f1509bcSBarry Smith 
3218f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3228f1509bcSBarry Smith           DMGetISColoringType()
3238f1509bcSBarry Smith @*/
3248f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3258f1509bcSBarry Smith {
3268f1509bcSBarry Smith   PetscFunctionBegin;
3278f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3288f1509bcSBarry Smith   *ctype = dm->coloringtype;
3298f1509bcSBarry Smith   PetscFunctionReturn(0);
3308f1509bcSBarry Smith }
3318f1509bcSBarry Smith 
3328f1509bcSBarry Smith /*@C
3338f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3348f1509bcSBarry Smith 
335d083f849SBarry Smith    Logically Collective on dm
3368f1509bcSBarry Smith 
3378f1509bcSBarry Smith    Input Parameters:
338521d9a4cSLisandro Dalcin +  dm - the DM context
339a2b5a043SBarry Smith -  ctype - the matrix type
340521d9a4cSLisandro Dalcin 
341521d9a4cSLisandro Dalcin    Options Database:
342521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
343521d9a4cSLisandro Dalcin 
344521d9a4cSLisandro Dalcin    Level: intermediate
345521d9a4cSLisandro Dalcin 
346a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
347521d9a4cSLisandro Dalcin @*/
34819fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
349521d9a4cSLisandro Dalcin {
350521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
35188f0584fSBarry Smith 
352521d9a4cSLisandro Dalcin   PetscFunctionBegin;
353521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
354521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
35519fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
356521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
357521d9a4cSLisandro Dalcin }
358521d9a4cSLisandro Dalcin 
359c0dedaeaSBarry Smith /*@C
360c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
361c0dedaeaSBarry Smith 
362d083f849SBarry Smith    Logically Collective on dm
363c0dedaeaSBarry Smith 
364c0dedaeaSBarry Smith    Input Parameter:
365c0dedaeaSBarry Smith .  dm - the DM context
366c0dedaeaSBarry Smith 
367c0dedaeaSBarry Smith    Output Parameter:
368c0dedaeaSBarry Smith .  ctype - the matrix type
369c0dedaeaSBarry Smith 
370c0dedaeaSBarry Smith    Options Database:
371c0dedaeaSBarry Smith .   -dm_mat_type ctype
372c0dedaeaSBarry Smith 
373c0dedaeaSBarry Smith    Level: intermediate
374c0dedaeaSBarry Smith 
375a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
376c0dedaeaSBarry Smith @*/
377c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
378c0dedaeaSBarry Smith {
379c0dedaeaSBarry Smith   PetscFunctionBegin;
380c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
381c0dedaeaSBarry Smith   *ctype = dm->mattype;
382c0dedaeaSBarry Smith   PetscFunctionReturn(0);
383c0dedaeaSBarry Smith }
384c0dedaeaSBarry Smith 
385c688c046SMatthew G Knepley /*@
38634f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
387c688c046SMatthew G Knepley 
388c688c046SMatthew G Knepley   Not collective
389c688c046SMatthew G Knepley 
390c688c046SMatthew G Knepley   Input Parameter:
391c688c046SMatthew G Knepley . A - The Mat
392c688c046SMatthew G Knepley 
393c688c046SMatthew G Knepley   Output Parameter:
394c688c046SMatthew G Knepley . dm - The DM
395c688c046SMatthew G Knepley 
396c688c046SMatthew G Knepley   Level: intermediate
397c688c046SMatthew G Knepley 
3988f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
3998f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4008f1509bcSBarry Smith 
401c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
402c688c046SMatthew G Knepley @*/
403c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
404c688c046SMatthew G Knepley {
405c688c046SMatthew G Knepley   PetscErrorCode ierr;
406c688c046SMatthew G Knepley 
407c688c046SMatthew G Knepley   PetscFunctionBegin;
408c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
409c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
410c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
411c688c046SMatthew G Knepley   PetscFunctionReturn(0);
412c688c046SMatthew G Knepley }
413c688c046SMatthew G Knepley 
414c688c046SMatthew G Knepley /*@
415c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
416c688c046SMatthew G Knepley 
417c688c046SMatthew G Knepley   Not collective
418c688c046SMatthew G Knepley 
419c688c046SMatthew G Knepley   Input Parameters:
420c688c046SMatthew G Knepley + A - The Mat
421c688c046SMatthew G Knepley - dm - The DM
422c688c046SMatthew G Knepley 
423c688c046SMatthew G Knepley   Level: intermediate
424c688c046SMatthew G Knepley 
4258f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4268f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4278f1509bcSBarry Smith 
4288f1509bcSBarry Smith 
429c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
430c688c046SMatthew G Knepley @*/
431c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
432c688c046SMatthew G Knepley {
433c688c046SMatthew G Knepley   PetscErrorCode ierr;
434c688c046SMatthew G Knepley 
435c688c046SMatthew G Knepley   PetscFunctionBegin;
436c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4378865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
438c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
439c688c046SMatthew G Knepley   PetscFunctionReturn(0);
440c688c046SMatthew G Knepley }
441c688c046SMatthew G Knepley 
4429a42bb27SBarry Smith /*@C
4439a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4446757b960SDave May    DM options in the database.
4459a42bb27SBarry Smith 
446d083f849SBarry Smith    Logically Collective on dm
4479a42bb27SBarry Smith 
4489a42bb27SBarry Smith    Input Parameter:
4498353ddbbSDave May +  da - the DM context
4509a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4519a42bb27SBarry Smith 
4529a42bb27SBarry Smith    Notes:
4539a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4549a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4559a42bb27SBarry Smith 
4569a42bb27SBarry Smith    Level: advanced
4579a42bb27SBarry Smith 
4589a42bb27SBarry Smith .seealso: DMSetFromOptions()
4599a42bb27SBarry Smith @*/
4607087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4619a42bb27SBarry Smith {
4629a42bb27SBarry Smith   PetscErrorCode ierr;
4639a42bb27SBarry Smith 
4649a42bb27SBarry Smith   PetscFunctionBegin;
4659a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4669a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
467691be533SLawrence Mitchell   if (dm->sf) {
468691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
469691be533SLawrence Mitchell   }
4701bb6d2a8SBarry Smith   if (dm->sectionSF) {
4711bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
472691be533SLawrence Mitchell   }
4739a42bb27SBarry Smith   PetscFunctionReturn(0);
4749a42bb27SBarry Smith }
4759a42bb27SBarry Smith 
47631697293SDave May /*@C
47731697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
47831697293SDave May    DM options in the database.
47931697293SDave May 
480d083f849SBarry Smith    Logically Collective on dm
48131697293SDave May 
48231697293SDave May    Input Parameters:
48331697293SDave May +  dm - the DM context
48431697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48531697293SDave May 
48631697293SDave May    Notes:
48731697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
48831697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
48931697293SDave May 
49031697293SDave May    Level: advanced
49131697293SDave May 
49231697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49331697293SDave May @*/
49431697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49531697293SDave May {
49631697293SDave May   PetscErrorCode ierr;
49731697293SDave May 
49831697293SDave May   PetscFunctionBegin;
49931697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50031697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
50131697293SDave May   PetscFunctionReturn(0);
50231697293SDave May }
50331697293SDave May 
50431697293SDave May /*@C
50531697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50631697293SDave May    DM options in the database.
50731697293SDave May 
50831697293SDave May    Not Collective
50931697293SDave May 
51031697293SDave May    Input Parameters:
51131697293SDave May .  dm - the DM context
51231697293SDave May 
51331697293SDave May    Output Parameters:
51431697293SDave May .  prefix - pointer to the prefix string used is returned
51531697293SDave May 
51695452b02SPatrick Sanan    Notes:
51795452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51831697293SDave May    sufficient length to hold the prefix.
51931697293SDave May 
52031697293SDave May    Level: advanced
52131697293SDave May 
52231697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52331697293SDave May @*/
52431697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52531697293SDave May {
52631697293SDave May   PetscErrorCode ierr;
52731697293SDave May 
52831697293SDave May   PetscFunctionBegin;
52931697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53031697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
53131697293SDave May   PetscFunctionReturn(0);
53231697293SDave May }
53331697293SDave May 
53488bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53588bdff64SToby Isaac {
5366eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
53788bdff64SToby Isaac   PetscErrorCode ierr;
53888bdff64SToby Isaac 
53988bdff64SToby Isaac   PetscFunctionBegin;
540aab5bcd8SJed Brown   *ncrefct = 0;
54188bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
54288bdff64SToby Isaac     refct--;
54388bdff64SToby Isaac     if (recurseCoarse) {
54488bdff64SToby Isaac       PetscInt coarseCount;
54588bdff64SToby Isaac 
54688bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
54788bdff64SToby Isaac       refct += coarseCount;
54888bdff64SToby Isaac     }
54988bdff64SToby Isaac   }
55088bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
55188bdff64SToby Isaac     refct--;
55288bdff64SToby Isaac     if (recurseFine) {
55388bdff64SToby Isaac       PetscInt fineCount;
55488bdff64SToby Isaac 
55588bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
55688bdff64SToby Isaac       refct += fineCount;
55788bdff64SToby Isaac     }
55888bdff64SToby Isaac   }
55988bdff64SToby Isaac   *ncrefct = refct;
56088bdff64SToby Isaac   PetscFunctionReturn(0);
56188bdff64SToby Isaac }
56288bdff64SToby Isaac 
563f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
564354557abSToby Isaac {
5655d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
566354557abSToby Isaac   PetscErrorCode ierr;
567354557abSToby Isaac 
568354557abSToby Isaac   PetscFunctionBegin;
569354557abSToby Isaac   /* destroy the labels */
570354557abSToby Isaac   while (next) {
571354557abSToby Isaac     DMLabelLink tmp = next->next;
572354557abSToby Isaac 
5735d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
574ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
575354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
576354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
577354557abSToby Isaac     next = tmp;
578354557abSToby Isaac   }
5795d80c0bfSVaclav Hapla   dm->labels = NULL;
580354557abSToby Isaac   PetscFunctionReturn(0);
581354557abSToby Isaac }
582354557abSToby Isaac 
58347c6ae99SBarry Smith /*@
5848472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
58547c6ae99SBarry Smith 
586d083f849SBarry Smith     Collective on dm
58747c6ae99SBarry Smith 
58847c6ae99SBarry Smith     Input Parameter:
58947c6ae99SBarry Smith .   dm - the DM object to destroy
59047c6ae99SBarry Smith 
59147c6ae99SBarry Smith     Level: developer
59247c6ae99SBarry Smith 
593e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
59447c6ae99SBarry Smith 
59547c6ae99SBarry Smith @*/
596fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
59747c6ae99SBarry Smith {
5986eb26441SStefano Zampini   PetscInt       cnt;
599dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
60047c6ae99SBarry Smith   PetscErrorCode ierr;
60147c6ae99SBarry Smith 
60247c6ae99SBarry Smith   PetscFunctionBegin;
6036bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6046bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
60587e657c6SBarry Smith 
60688bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
60788bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
60888bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
60988bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
6106bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6116bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6126eb26441SStefano Zampini 
6136eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6146eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6156eb26441SStefano Zampini 
616f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6170298fd71SBarry Smith   (*dm)->namedglobal = NULL;
618f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6192348bcf4SPeter Brune     nnext = nlink->next;
6202348bcf4SPeter 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);
6212348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6222348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6232348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6242348bcf4SPeter Brune   }
625f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6260298fd71SBarry Smith   (*dm)->namedlocal = NULL;
627f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
628f490541aSPeter Brune     nnext = nlink->next;
629f490541aSPeter 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);
630f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
631f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
632f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
633f490541aSPeter Brune   }
6342348bcf4SPeter Brune 
635b17ce1afSJed Brown   /* Destroy the list of hooks */
636c833c3b5SJed Brown   {
637c833c3b5SJed Brown     DMCoarsenHookLink link,next;
638b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
639b17ce1afSJed Brown       next = link->next;
640b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
641b17ce1afSJed Brown     }
6420298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
643c833c3b5SJed Brown   }
644c833c3b5SJed Brown   {
645c833c3b5SJed Brown     DMRefineHookLink link,next;
646c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
647c833c3b5SJed Brown       next = link->next;
648c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
649c833c3b5SJed Brown     }
6500298fd71SBarry Smith     (*dm)->refinehook = NULL;
651c833c3b5SJed Brown   }
652be081cd6SPeter Brune   {
653be081cd6SPeter Brune     DMSubDomainHookLink link,next;
654be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
655be081cd6SPeter Brune       next = link->next;
656be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
657be081cd6SPeter Brune     }
6580298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
659be081cd6SPeter Brune   }
660baf369e7SPeter Brune   {
661baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
662baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
663baf369e7SPeter Brune       next = link->next;
664baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
665baf369e7SPeter Brune     }
6660298fd71SBarry Smith     (*dm)->gtolhook = NULL;
667baf369e7SPeter Brune   }
668d4d07f1eSToby Isaac   {
669d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
670d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
671d4d07f1eSToby Isaac       next = link->next;
672d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
673d4d07f1eSToby Isaac     }
674d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
675d4d07f1eSToby Isaac   }
676aa1993deSMatthew G Knepley   /* Destroy the work arrays */
677aa1993deSMatthew G Knepley   {
678aa1993deSMatthew G Knepley     DMWorkLink link,next;
679aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
680aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
681aa1993deSMatthew G Knepley       next = link->next;
682aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
683aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
684aa1993deSMatthew G Knepley     }
6850298fd71SBarry Smith     (*dm)->workin = NULL;
686aa1993deSMatthew G Knepley   }
687c58f1c22SToby Isaac   /* destroy the labels */
688f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
689f4cdcedcSVaclav Hapla   /* destroy the fields */
690e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
691f4cdcedcSVaclav Hapla   /* destroy the boundaries */
692e6f8dbb6SToby Isaac   {
693e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
694e6f8dbb6SToby Isaac     while (next) {
695e6f8dbb6SToby Isaac       DMBoundary b = next;
696e6f8dbb6SToby Isaac 
697e6f8dbb6SToby Isaac       next = b->next;
698e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
699e6f8dbb6SToby Isaac     }
700e6f8dbb6SToby Isaac   }
701b17ce1afSJed Brown 
70252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
70352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
70452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
70552536dc3SBarry Smith 
7061a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7071a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7081a266240SBarry Smith   }
70971cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7106bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7116bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
712073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
71388ed4aceSMatthew G Knepley 
7141bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7151bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7168b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
717fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
718fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
71988ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7201bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
721736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
722736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7238e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
724736995cdSBlaise Bourdin     }
725736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
726736995cdSBlaise Bourdin   }
72788bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
72888bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
72988bdff64SToby Isaac   }
7306eb26441SStefano Zampini 
731a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
73288bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
73388bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
73488bdff64SToby Isaac   }
73588bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
736f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7376636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7386636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7396636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
740412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
741412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
742412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
743ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
744ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
745ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7466636e97aSMatthew G Knepley 
747e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
74814f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
749e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
750e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
751732e2eb9SMatthew G Knepley 
752ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7536bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
754ed3c66a1SDave May   }
755c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
756435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
757732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
75847c6ae99SBarry Smith   PetscFunctionReturn(0);
75947c6ae99SBarry Smith }
76047c6ae99SBarry Smith 
761d7bf68aeSBarry Smith /*@
762d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
763d7bf68aeSBarry Smith 
764d083f849SBarry Smith     Collective on dm
765d7bf68aeSBarry Smith 
766d7bf68aeSBarry Smith     Input Parameter:
767d7bf68aeSBarry Smith .   dm - the DM object to setup
768d7bf68aeSBarry Smith 
769d7bf68aeSBarry Smith     Level: developer
770d7bf68aeSBarry Smith 
771e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
772d7bf68aeSBarry Smith 
773d7bf68aeSBarry Smith @*/
7747087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
775d7bf68aeSBarry Smith {
776d7bf68aeSBarry Smith   PetscErrorCode ierr;
777d7bf68aeSBarry Smith 
778d7bf68aeSBarry Smith   PetscFunctionBegin;
779171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7808387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
781d7bf68aeSBarry Smith   if (dm->ops->setup) {
782d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
783d7bf68aeSBarry Smith   }
7848387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
785d7bf68aeSBarry Smith   PetscFunctionReturn(0);
786d7bf68aeSBarry Smith }
787d7bf68aeSBarry Smith 
788d7bf68aeSBarry Smith /*@
789d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
790d7bf68aeSBarry Smith 
791d083f849SBarry Smith     Collective on dm
792d7bf68aeSBarry Smith 
793d7bf68aeSBarry Smith     Input Parameter:
794d7bf68aeSBarry Smith .   dm - the DM object to set options for
795d7bf68aeSBarry Smith 
796732e2eb9SMatthew G Knepley     Options Database:
7974164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7984164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7994164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8008f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
801732e2eb9SMatthew G Knepley 
802384a6580SVaclav Hapla     DMPLEX Specific Checks
803384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
804384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
805384a6580SVaclav Hapla .   -dm_plex_check_faces           - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type - DMPlexCheckFaces()
806384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
807384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
808384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
809384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
810d7bf68aeSBarry Smith 
81195eb5ee5SVaclav Hapla     Level: intermediate
81295eb5ee5SVaclav Hapla 
81395eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
81495eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
815d7bf68aeSBarry Smith 
816d7bf68aeSBarry Smith @*/
8177087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
818d7bf68aeSBarry Smith {
8197781c08eSBarry Smith   char           typeName[256];
820ca266f36SBarry Smith   PetscBool      flg;
821d7bf68aeSBarry Smith   PetscErrorCode ierr;
822d7bf68aeSBarry Smith 
823d7bf68aeSBarry Smith   PetscFunctionBegin;
824171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82549be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
82649be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8271bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8283194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8290298fd71SBarry 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);
830a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
831f9ba7244SBarry Smith   if (flg) {
832f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
833f9ba7244SBarry Smith   }
834a264d7a6SBarry 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);
835073dac72SJed Brown   if (flg) {
836521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
837073dac72SJed Brown   }
8388f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
839f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
840e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
841f9ba7244SBarry Smith   }
842f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8430633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
84482fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
845d7bf68aeSBarry Smith   PetscFunctionReturn(0);
846d7bf68aeSBarry Smith }
847d7bf68aeSBarry Smith 
848fc9bc008SSatish Balay /*@C
849fe2efc57SMark    DMViewFromOptions - View from Options
850fe2efc57SMark 
851fe2efc57SMark    Collective on DM
852fe2efc57SMark 
853fe2efc57SMark    Input Parameters:
854fe2efc57SMark +  dm - the DM object
855736c3998SJose E. Roman .  obj - Optional object
856736c3998SJose E. Roman -  name - command line option
857fe2efc57SMark 
858fe2efc57SMark    Level: intermediate
859fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
860fe2efc57SMark @*/
861fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
862fe2efc57SMark {
863fe2efc57SMark   PetscErrorCode ierr;
864fe2efc57SMark 
865fe2efc57SMark   PetscFunctionBegin;
866fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
867fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
868fe2efc57SMark   PetscFunctionReturn(0);
869fe2efc57SMark }
870fe2efc57SMark 
871fe2efc57SMark /*@C
872224748a4SBarry Smith     DMView - Views a DM
87347c6ae99SBarry Smith 
874d083f849SBarry Smith     Collective on dm
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith     Input Parameter:
87747c6ae99SBarry Smith +   dm - the DM object to view
87847c6ae99SBarry Smith -   v - the viewer
87947c6ae99SBarry Smith 
880224748a4SBarry Smith     Level: beginner
88147c6ae99SBarry Smith 
882e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88347c6ae99SBarry Smith 
88447c6ae99SBarry Smith @*/
8857087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
88647c6ae99SBarry Smith {
88747c6ae99SBarry Smith   PetscErrorCode    ierr;
88832c0f0efSBarry Smith   PetscBool         isbinary;
88976a8abe0SBarry Smith   PetscMPIInt       size;
89076a8abe0SBarry Smith   PetscViewerFormat format;
89147c6ae99SBarry Smith 
89247c6ae99SBarry Smith   PetscFunctionBegin;
893171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8943014e516SBarry Smith   if (!v) {
895ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8963014e516SBarry Smith   }
897b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
89874903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
89974903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
90074903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
90174903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
90274903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
90374903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
90474903a4fSStefano Zampini      in an error here */
90574903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9068bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
907b1b135c8SBarry Smith 
90876a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
90976a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
91076a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
91198c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
91232c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
91332c0f0efSBarry Smith   if (isbinary) {
91455849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
91532c0f0efSBarry Smith     char     type[256];
91632c0f0efSBarry Smith 
917f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
91832c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
919f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
92032c0f0efSBarry Smith   }
9210c010503SBarry Smith   if (dm->ops->view) {
9220c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
92347c6ae99SBarry Smith   }
92447c6ae99SBarry Smith   PetscFunctionReturn(0);
92547c6ae99SBarry Smith }
92647c6ae99SBarry Smith 
92747c6ae99SBarry Smith /*@
9288472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
92947c6ae99SBarry Smith 
930d083f849SBarry Smith     Collective on dm
93147c6ae99SBarry Smith 
93247c6ae99SBarry Smith     Input Parameter:
93347c6ae99SBarry Smith .   dm - the DM object
93447c6ae99SBarry Smith 
93547c6ae99SBarry Smith     Output Parameter:
93647c6ae99SBarry Smith .   vec - the global vector
93747c6ae99SBarry Smith 
938073dac72SJed Brown     Level: beginner
93947c6ae99SBarry Smith 
940e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94147c6ae99SBarry Smith 
94247c6ae99SBarry Smith @*/
9437087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
94447c6ae99SBarry Smith {
94547c6ae99SBarry Smith   PetscErrorCode ierr;
94647c6ae99SBarry Smith 
94747c6ae99SBarry Smith   PetscFunctionBegin;
948171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
949b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
950b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
95147c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
95276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
953c6b011d8SStefano Zampini     DM vdm;
954c6b011d8SStefano Zampini 
955c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
956c6b011d8SStefano Zampini     if (!vdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the vector\n",((PetscObject)dm)->type_name);
957c6b011d8SStefano Zampini   }
95847c6ae99SBarry Smith   PetscFunctionReturn(0);
95947c6ae99SBarry Smith }
96047c6ae99SBarry Smith 
96147c6ae99SBarry Smith /*@
9628472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
96347c6ae99SBarry Smith 
96447c6ae99SBarry Smith     Not Collective
96547c6ae99SBarry Smith 
96647c6ae99SBarry Smith     Input Parameter:
96747c6ae99SBarry Smith .   dm - the DM object
96847c6ae99SBarry Smith 
96947c6ae99SBarry Smith     Output Parameter:
97047c6ae99SBarry Smith .   vec - the local vector
97147c6ae99SBarry Smith 
972073dac72SJed Brown     Level: beginner
97347c6ae99SBarry Smith 
974e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
97547c6ae99SBarry Smith 
97647c6ae99SBarry Smith @*/
9777087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
97847c6ae99SBarry Smith {
97947c6ae99SBarry Smith   PetscErrorCode ierr;
98047c6ae99SBarry Smith 
98147c6ae99SBarry Smith   PetscFunctionBegin;
982171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
983b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
984b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
98547c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
98676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
987c6b011d8SStefano Zampini     DM vdm;
988c6b011d8SStefano Zampini 
989c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
990c6b011d8SStefano Zampini     if (!vdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"DM type '%s' did not attach the DM to the vector\n",((PetscObject)dm)->type_name);
991c6b011d8SStefano Zampini   }
99247c6ae99SBarry Smith   PetscFunctionReturn(0);
99347c6ae99SBarry Smith }
99447c6ae99SBarry Smith 
9951411c6eeSJed Brown /*@
9961411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9971411c6eeSJed Brown 
998d083f849SBarry Smith    Collective on dm
9991411c6eeSJed Brown 
10001411c6eeSJed Brown    Input Parameter:
10011411c6eeSJed Brown .  dm - the DM that provides the mapping
10021411c6eeSJed Brown 
10031411c6eeSJed Brown    Output Parameter:
10041411c6eeSJed Brown .  ltog - the mapping
10051411c6eeSJed Brown 
10061411c6eeSJed Brown    Level: intermediate
10071411c6eeSJed Brown 
10081411c6eeSJed Brown    Notes:
10091411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10101411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10111411c6eeSJed Brown 
1012fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10131411c6eeSJed Brown @*/
10147087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10151411c6eeSJed Brown {
10160be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10171411c6eeSJed Brown   PetscErrorCode ierr;
10181411c6eeSJed Brown 
10191411c6eeSJed Brown   PetscFunctionBegin;
10201411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10211411c6eeSJed Brown   PetscValidPointer(ltog,2);
10221411c6eeSJed Brown   if (!dm->ltogmap) {
102337d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
102437d0c07bSMatthew G Knepley 
102592fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
102637d0c07bSMatthew G Knepley     if (section) {
1027a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
102837d0c07bSMatthew G Knepley       PetscInt       *ltog;
1029ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
103037d0c07bSMatthew G Knepley 
1031e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
103237d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1033ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1034ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
103537d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1036a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
103737d0c07bSMatthew G Knepley 
103837d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
103937d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1040a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1041a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
104237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10431a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10441a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10451a7dc684SMatthew G. Knepley         if (dof) {
1046b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1047b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10481a7dc684SMatthew G. Knepley         }
104937d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1050a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1051a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
105237d0c07bSMatthew G Knepley         }
105337d0c07bSMatthew G Knepley       }
1054bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10550be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10560be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10570be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10580be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10597591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10607591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1061ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1062ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1063ccf3bd66SMatthew G. Knepley         n /= bs;
1064ccf3bd66SMatthew G. Knepley       }
1065ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10663bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
106737d0c07bSMatthew G Knepley     } else {
1068b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1069184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10701411c6eeSJed Brown     }
107137d0c07bSMatthew G Knepley   }
10721411c6eeSJed Brown   *ltog = dm->ltogmap;
10731411c6eeSJed Brown   PetscFunctionReturn(0);
10741411c6eeSJed Brown }
10751411c6eeSJed Brown 
10761411c6eeSJed Brown /*@
10771411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10781411c6eeSJed Brown 
10791411c6eeSJed Brown    Not Collective
10801411c6eeSJed Brown 
10811411c6eeSJed Brown    Input Parameter:
10821411c6eeSJed Brown .  dm - the DM with block structure
10831411c6eeSJed Brown 
10841411c6eeSJed Brown    Output Parameter:
10851411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10861411c6eeSJed Brown 
10871411c6eeSJed Brown    Level: intermediate
10881411c6eeSJed Brown 
1089fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10901411c6eeSJed Brown @*/
10917087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10921411c6eeSJed Brown {
10931411c6eeSJed Brown   PetscFunctionBegin;
10941411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1095534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
10961411c6eeSJed 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");
10971411c6eeSJed Brown   *bs = dm->bs;
10981411c6eeSJed Brown   PetscFunctionReturn(0);
10991411c6eeSJed Brown }
11001411c6eeSJed Brown 
110147c6ae99SBarry Smith /*@
11028472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
110347c6ae99SBarry Smith 
1104a5bc1bf3SBarry Smith     Collective on dmc
110547c6ae99SBarry Smith 
110647c6ae99SBarry Smith     Input Parameter:
1107a5bc1bf3SBarry Smith +   dmc - the DM object
1108a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
110947c6ae99SBarry Smith 
111047c6ae99SBarry Smith     Output Parameter:
111147c6ae99SBarry Smith +  mat - the interpolation
111247c6ae99SBarry Smith -  vec - the scaling (optional)
111347c6ae99SBarry Smith 
111447c6ae99SBarry Smith     Level: developer
111547c6ae99SBarry Smith 
111695452b02SPatrick Sanan     Notes:
111795452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
111885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1119d52bd9f3SBarry Smith 
11201f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1121d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
112285afcc9aSBarry Smith 
112385afcc9aSBarry Smith 
11242ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
112547c6ae99SBarry Smith 
112647c6ae99SBarry Smith @*/
1127a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
112847c6ae99SBarry Smith {
112947c6ae99SBarry Smith   PetscErrorCode ierr;
113047c6ae99SBarry Smith 
113147c6ae99SBarry Smith   PetscFunctionBegin;
1132a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1133a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1134c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1135a5bc1bf3SBarry Smith   if (!dmc->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1136a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1137a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1138a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
113947c6ae99SBarry Smith   PetscFunctionReturn(0);
114047c6ae99SBarry Smith }
114147c6ae99SBarry Smith 
11423ad4599aSBarry Smith /*@
1143d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11442ed6491fSPatrick Sanan 
11452ed6491fSPatrick Sanan   Input Parameters:
11462ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
11472ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
11482ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
11492ed6491fSPatrick Sanan 
11502ed6491fSPatrick Sanan   Output Parameter:
11512ed6491fSPatrick Sanan .    scale - the scaled vector
11522ed6491fSPatrick Sanan 
11532ed6491fSPatrick Sanan   Level: developer
11542ed6491fSPatrick Sanan 
11552ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
11562ed6491fSPatrick Sanan 
11572ed6491fSPatrick Sanan @*/
11582ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
11592ed6491fSPatrick Sanan {
11602ed6491fSPatrick Sanan   PetscErrorCode ierr;
11612ed6491fSPatrick Sanan   Vec            fine;
11622ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
11632ed6491fSPatrick Sanan 
11642ed6491fSPatrick Sanan   PetscFunctionBegin;
11652ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
11662ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
11672ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
11682ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
11692ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
11702ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
11712ed6491fSPatrick Sanan   PetscFunctionReturn(0);
11722ed6491fSPatrick Sanan }
11732ed6491fSPatrick Sanan 
11742ed6491fSPatrick Sanan /*@
11753ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11763ad4599aSBarry Smith 
1177a5bc1bf3SBarry Smith     Collective on dmc
11783ad4599aSBarry Smith 
11793ad4599aSBarry Smith     Input Parameter:
1180a5bc1bf3SBarry Smith +   dmc - the DM object
1181a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
11823ad4599aSBarry Smith 
11833ad4599aSBarry Smith     Output Parameter:
11843ad4599aSBarry Smith .  mat - the restriction
11853ad4599aSBarry Smith 
11863ad4599aSBarry Smith 
11873ad4599aSBarry Smith     Level: developer
11883ad4599aSBarry Smith 
118995452b02SPatrick Sanan     Notes:
119095452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
11913ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11923ad4599aSBarry Smith 
11933ad4599aSBarry Smith 
11943ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11953ad4599aSBarry Smith 
11963ad4599aSBarry Smith @*/
1197a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
11983ad4599aSBarry Smith {
11993ad4599aSBarry Smith   PetscErrorCode ierr;
12003ad4599aSBarry Smith 
12013ad4599aSBarry Smith   PetscFunctionBegin;
1202a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1203a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12045a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1205a5bc1bf3SBarry Smith   if (!dmc->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1206a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1207a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1208a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
12093ad4599aSBarry Smith   PetscFunctionReturn(0);
12103ad4599aSBarry Smith }
12113ad4599aSBarry Smith 
121247c6ae99SBarry Smith /*@
12138472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
121447c6ae99SBarry Smith 
1215a5bc1bf3SBarry Smith     Collective on dac
121647c6ae99SBarry Smith 
121747c6ae99SBarry Smith     Input Parameter:
1218a5bc1bf3SBarry Smith +   dac - the DM object
1219a5bc1bf3SBarry Smith -   daf - the second, finer DM object
122047c6ae99SBarry Smith 
122147c6ae99SBarry Smith     Output Parameter:
12226dbf9973SLawrence Mitchell .   mat - the injection
122347c6ae99SBarry Smith 
122447c6ae99SBarry Smith     Level: developer
122547c6ae99SBarry Smith 
122695452b02SPatrick Sanan    Notes:
122795452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
122885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
122985afcc9aSBarry Smith 
1230e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
123147c6ae99SBarry Smith 
123247c6ae99SBarry Smith @*/
1233a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
123447c6ae99SBarry Smith {
123547c6ae99SBarry Smith   PetscErrorCode ierr;
123647c6ae99SBarry Smith 
123747c6ae99SBarry Smith   PetscFunctionBegin;
1238a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1239a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
12405a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1241a5bc1bf3SBarry Smith   if (!dac->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1242a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1243a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1244a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
124547c6ae99SBarry Smith   PetscFunctionReturn(0);
124647c6ae99SBarry Smith }
124747c6ae99SBarry Smith 
1248b412c318SBarry Smith /*@
1249bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1250bd041c0cSMatthew G. Knepley 
1251a5bc1bf3SBarry Smith   Collective on dac
1252bd041c0cSMatthew G. Knepley 
1253bd041c0cSMatthew G. Knepley   Input Parameter:
1254a5bc1bf3SBarry Smith + dac - the DM object
1255a5bc1bf3SBarry Smith - daf - the second, finer DM object
1256bd041c0cSMatthew G. Knepley 
1257bd041c0cSMatthew G. Knepley   Output Parameter:
1258bd041c0cSMatthew G. Knepley . mat - the interpolation
1259bd041c0cSMatthew G. Knepley 
1260bd041c0cSMatthew G. Knepley   Level: developer
1261bd041c0cSMatthew G. Knepley 
1262bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1263bd041c0cSMatthew G. Knepley @*/
1264a5bc1bf3SBarry Smith PetscErrorCode DMCreateMassMatrix(DM dac, DM daf, Mat *mat)
1265bd041c0cSMatthew G. Knepley {
1266bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1267bd041c0cSMatthew G. Knepley 
1268bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1269a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1270a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
12715a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1272a5bc1bf3SBarry Smith   if (!dac->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dac)->type_name);
1273a5bc1bf3SBarry Smith   ierr = (*dac->ops->createmassmatrix)(dac, daf, mat);CHKERRQ(ierr);
1274bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1275bd041c0cSMatthew G. Knepley }
1276bd041c0cSMatthew G. Knepley 
1277bd041c0cSMatthew G. Knepley /*@
1278b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
127947c6ae99SBarry Smith 
1280d083f849SBarry Smith     Collective on dm
128147c6ae99SBarry Smith 
128247c6ae99SBarry Smith     Input Parameter:
128347c6ae99SBarry Smith +   dm - the DM object
12845bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
128547c6ae99SBarry Smith 
128647c6ae99SBarry Smith     Output Parameter:
128747c6ae99SBarry Smith .   coloring - the coloring
128847c6ae99SBarry Smith 
1289ec5066bdSBarry Smith     Notes:
1290ec5066bdSBarry Smith        Coloring of matrices can be computed directly from the sparse matrix nonzero structure via the MatColoring object or from the mesh from which the
1291ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1292ec5066bdSBarry Smith 
1293ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1294ec5066bdSBarry Smith 
129547c6ae99SBarry Smith     Level: developer
129647c6ae99SBarry Smith 
1297ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
129847c6ae99SBarry Smith 
1299aab9d709SJed Brown @*/
1300b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
130147c6ae99SBarry Smith {
130247c6ae99SBarry Smith   PetscErrorCode ierr;
130347c6ae99SBarry Smith 
130447c6ae99SBarry Smith   PetscFunctionBegin;
1305171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13065a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1307b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1308b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
130947c6ae99SBarry Smith   PetscFunctionReturn(0);
131047c6ae99SBarry Smith }
131147c6ae99SBarry Smith 
1312b412c318SBarry Smith /*@
13138472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
131447c6ae99SBarry Smith 
1315d083f849SBarry Smith     Collective on dm
131647c6ae99SBarry Smith 
131747c6ae99SBarry Smith     Input Parameter:
1318b412c318SBarry Smith .   dm - the DM object
131947c6ae99SBarry Smith 
132047c6ae99SBarry Smith     Output Parameter:
132147c6ae99SBarry Smith .   mat - the empty Jacobian
132247c6ae99SBarry Smith 
1323073dac72SJed Brown     Level: beginner
132447c6ae99SBarry Smith 
132595452b02SPatrick Sanan     Notes:
132695452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
132794013140SBarry Smith        do not need to do it yourself.
132894013140SBarry Smith 
132994013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13307889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
133194013140SBarry Smith 
133294013140SBarry 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
133394013140SBarry Smith        internally by PETSc.
133494013140SBarry Smith 
133594013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1336aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
133794013140SBarry Smith 
1338b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
133947c6ae99SBarry Smith 
1340aab9d709SJed Brown @*/
1341b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
134247c6ae99SBarry Smith {
134347c6ae99SBarry Smith   PetscErrorCode ierr;
134447c6ae99SBarry Smith 
134547c6ae99SBarry Smith   PetscFunctionBegin;
1346171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1347c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1348b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
13495a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1350fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1351b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
135276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1353c6b011d8SStefano Zampini     DM mdm;
1354c6b011d8SStefano Zampini 
1355c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1356c6b011d8SStefano Zampini     if (!mdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the matrix\n",((PetscObject)dm)->type_name);
1357c6b011d8SStefano Zampini   }
1358e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1359e5e52638SMatthew G. Knepley   if (dm->Nf) {
1360e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1361e571a35bSMatthew G. Knepley     PetscInt     Nf;
1362e571a35bSMatthew G. Knepley 
1363e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1364e571a35bSMatthew G. Knepley     if (Nf == 1) {
1365e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1366e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1367e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1368e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1369e571a35bSMatthew G. Knepley       }
1370e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1371e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1372e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1373e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1374e571a35bSMatthew G. Knepley       }
1375e571a35bSMatthew G. Knepley     }
1376e571a35bSMatthew G. Knepley   }
1377fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
137847c6ae99SBarry Smith   PetscFunctionReturn(0);
137947c6ae99SBarry Smith }
138047c6ae99SBarry Smith 
1381732e2eb9SMatthew G Knepley /*@
1382950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1383732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1384732e2eb9SMatthew G Knepley 
1385d083f849SBarry Smith   Logically Collective on dm
1386732e2eb9SMatthew G Knepley 
1387732e2eb9SMatthew G Knepley   Input Parameter:
1388732e2eb9SMatthew G Knepley + dm - the DM
1389732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1390732e2eb9SMatthew G Knepley 
1391732e2eb9SMatthew G Knepley   Level: developer
1392b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1393732e2eb9SMatthew G Knepley @*/
1394732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1395732e2eb9SMatthew G Knepley {
1396732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1397732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1398732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1399732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1400732e2eb9SMatthew G Knepley }
1401732e2eb9SMatthew G Knepley 
1402b06ff27eSHong Zhang /*@
1403b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1404b06ff27eSHong Zhang     but the array for values will not be allocated.
1405b06ff27eSHong Zhang 
1406d083f849SBarry Smith   Logically Collective on dm
1407b06ff27eSHong Zhang 
1408b06ff27eSHong Zhang   Input Parameter:
1409b06ff27eSHong Zhang + dm - the DM
1410b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1411b06ff27eSHong Zhang 
1412b06ff27eSHong Zhang   Level: developer
1413b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1414b06ff27eSHong Zhang @*/
1415b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1416b06ff27eSHong Zhang {
1417b06ff27eSHong Zhang   PetscFunctionBegin;
1418b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1419b06ff27eSHong Zhang   dm->structure_only = only;
1420b06ff27eSHong Zhang   PetscFunctionReturn(0);
1421b06ff27eSHong Zhang }
1422b06ff27eSHong Zhang 
1423a89ea682SMatthew G Knepley /*@C
1424aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1425a89ea682SMatthew G Knepley 
1426a89ea682SMatthew G Knepley   Not Collective
1427a89ea682SMatthew G Knepley 
1428a89ea682SMatthew G Knepley   Input Parameters:
1429a89ea682SMatthew G Knepley + dm - the DM object
1430aa1993deSMatthew G Knepley . count - The minium size
143169291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1432a89ea682SMatthew G Knepley 
1433a89ea682SMatthew G Knepley   Output Parameter:
1434a89ea682SMatthew G Knepley . array - the work array
1435a89ea682SMatthew G Knepley 
1436a89ea682SMatthew G Knepley   Level: developer
1437a89ea682SMatthew G Knepley 
1438a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1439a89ea682SMatthew G Knepley @*/
144069291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1441a89ea682SMatthew G Knepley {
1442a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1443aa1993deSMatthew G Knepley   DMWorkLink     link;
144469291d52SBarry Smith   PetscMPIInt    dsize;
1445a89ea682SMatthew G Knepley 
1446a89ea682SMatthew G Knepley   PetscFunctionBegin;
1447a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1448aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1449aa1993deSMatthew G Knepley   if (dm->workin) {
1450aa1993deSMatthew G Knepley     link       = dm->workin;
1451aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1452aa1993deSMatthew G Knepley   } else {
1453b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1454a89ea682SMatthew G Knepley   }
145569291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
14565056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1457aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1458854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1459854ce69bSBarry Smith     link->bytes = dsize*count;
1460aa1993deSMatthew G Knepley   }
1461aa1993deSMatthew G Knepley   link->next   = dm->workout;
1462aa1993deSMatthew G Knepley   dm->workout  = link;
1463aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1464a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1465a89ea682SMatthew G Knepley }
1466a89ea682SMatthew G Knepley 
1467aa1993deSMatthew G Knepley /*@C
1468aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1469aa1993deSMatthew G Knepley 
1470aa1993deSMatthew G Knepley   Not Collective
1471aa1993deSMatthew G Knepley 
1472aa1993deSMatthew G Knepley   Input Parameters:
1473aa1993deSMatthew G Knepley + dm - the DM object
1474aa1993deSMatthew G Knepley . count - The minium size
147569291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1476aa1993deSMatthew G Knepley 
1477aa1993deSMatthew G Knepley   Output Parameter:
1478aa1993deSMatthew G Knepley . array - the work array
1479aa1993deSMatthew G Knepley 
1480aa1993deSMatthew G Knepley   Level: developer
1481aa1993deSMatthew G Knepley 
148295452b02SPatrick Sanan   Developer Notes:
148395452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1484aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1485aa1993deSMatthew G Knepley @*/
148669291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1487aa1993deSMatthew G Knepley {
1488aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1489aa1993deSMatthew G Knepley 
1490aa1993deSMatthew G Knepley   PetscFunctionBegin;
1491aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1492aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1493aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1494aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1495aa1993deSMatthew G Knepley       *p           = link->next;
1496aa1993deSMatthew G Knepley       link->next   = dm->workin;
1497aa1993deSMatthew G Knepley       dm->workin   = link;
14980298fd71SBarry Smith       *(void**)mem = NULL;
1499aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1500aa1993deSMatthew G Knepley     }
1501aa1993deSMatthew G Knepley   }
1502aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1503aa1993deSMatthew G Knepley }
1504e7c4fc90SDmitry Karpeev 
1505435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1506435a35e8SMatthew G Knepley {
1507435a35e8SMatthew G Knepley   PetscFunctionBegin;
1508435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
150982f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1510435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1511435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1512435a35e8SMatthew G Knepley }
1513435a35e8SMatthew G Knepley 
15140a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
15150a50eb56SMatthew G. Knepley {
15160a50eb56SMatthew G. Knepley   PetscFunctionBegin;
15170a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1518f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
15190a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
15200a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
15210a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
15220a50eb56SMatthew G. Knepley }
15230a50eb56SMatthew G. Knepley 
1524f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1525f9d4088aSMatthew G. Knepley {
1526f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1527f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1528f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1529f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1530f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1531f9d4088aSMatthew G. Knepley }
1532f9d4088aSMatthew G. Knepley 
1533f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1534f9d4088aSMatthew G. Knepley {
1535f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1536f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1537f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1538f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1539f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1540f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1541f9d4088aSMatthew G. Knepley }
1542f9d4088aSMatthew G. Knepley 
15434f3b5142SJed Brown /*@C
15444d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
15454d343eeaSMatthew G Knepley 
15464d343eeaSMatthew G Knepley   Not collective
15474d343eeaSMatthew G Knepley 
15484d343eeaSMatthew G Knepley   Input Parameter:
15494d343eeaSMatthew G Knepley . dm - the DM object
15504d343eeaSMatthew G Knepley 
15514d343eeaSMatthew G Knepley   Output Parameters:
15520298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
15530298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
15540298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
15554d343eeaSMatthew G Knepley 
15564d343eeaSMatthew G Knepley   Level: intermediate
15574d343eeaSMatthew G Knepley 
155821c9b008SJed Brown   Notes:
155921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
156021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
156121c9b008SJed Brown   PetscFree().
156221c9b008SJed Brown 
15634d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
15644d343eeaSMatthew G Knepley @*/
156537d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
15664d343eeaSMatthew G Knepley {
156737d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
15684d343eeaSMatthew G Knepley   PetscErrorCode ierr;
15694d343eeaSMatthew G Knepley 
15704d343eeaSMatthew G Knepley   PetscFunctionBegin;
15714d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
157269ca1f37SDmitry Karpeev   if (numFields) {
1573534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
157469ca1f37SDmitry Karpeev     *numFields = 0;
157569ca1f37SDmitry Karpeev   }
157637d0c07bSMatthew G Knepley   if (fieldNames) {
157737d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
15780298fd71SBarry Smith     *fieldNames = NULL;
157969ca1f37SDmitry Karpeev   }
158069ca1f37SDmitry Karpeev   if (fields) {
158169ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15820298fd71SBarry Smith     *fields = NULL;
158369ca1f37SDmitry Karpeev   }
158492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
158537d0c07bSMatthew G Knepley   if (section) {
15863a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
158737d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
158837d0c07bSMatthew G Knepley 
1589e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
159037d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15913a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
159237d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
159337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
159437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15953a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
159637d0c07bSMatthew G Knepley     }
159737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
159837d0c07bSMatthew G Knepley       PetscInt gdof;
159937d0c07bSMatthew G Knepley 
160037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
160137d0c07bSMatthew G Knepley       if (gdof > 0) {
160237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
16033a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
160437d0c07bSMatthew G Knepley 
160537d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
160637d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
16073a544194SStefano Zampini           fpdof = fdof-fcdof;
16083a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
16093a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
16103a544194SStefano Zampini             fieldNc[f] = 1;
16113a544194SStefano Zampini           }
16123a544194SStefano Zampini           fieldSizes[f] += fpdof;
161337d0c07bSMatthew G Knepley         }
161437d0c07bSMatthew G Knepley       }
161537d0c07bSMatthew G Knepley     }
161637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1617785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
161837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
161937d0c07bSMatthew G Knepley     }
162037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
162137d0c07bSMatthew G Knepley       PetscInt gdof, goff;
162237d0c07bSMatthew G Knepley 
162337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
162437d0c07bSMatthew G Knepley       if (gdof > 0) {
162537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
162637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
162737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
162837d0c07bSMatthew G Knepley 
162937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
163037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
163137d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
163237d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
163337d0c07bSMatthew G Knepley           }
163437d0c07bSMatthew G Knepley         }
163537d0c07bSMatthew G Knepley       }
163637d0c07bSMatthew G Knepley     }
16378865f1eaSKarl Rupp     if (numFields) *numFields = nF;
163837d0c07bSMatthew G Knepley     if (fieldNames) {
1639785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
164037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
164137d0c07bSMatthew G Knepley         const char *fieldName;
164237d0c07bSMatthew G Knepley 
164337d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
164437d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
164537d0c07bSMatthew G Knepley       }
164637d0c07bSMatthew G Knepley     }
164737d0c07bSMatthew G Knepley     if (fields) {
1648785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
164937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
16503a544194SStefano Zampini         PetscInt bs, in[2], out[2];
16513a544194SStefano Zampini 
165282f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
16533a544194SStefano Zampini         in[0] = -fieldNc[f];
16543a544194SStefano Zampini         in[1] = fieldNc[f];
16553a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
16563a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
16573a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
165837d0c07bSMatthew G Knepley       }
165937d0c07bSMatthew G Knepley     }
16603a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
16618865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
16628865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
166369ca1f37SDmitry Karpeev   }
16644d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
16654d343eeaSMatthew G Knepley }
16664d343eeaSMatthew G Knepley 
166716621825SDmitry Karpeev 
166816621825SDmitry Karpeev /*@C
166916621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
167016621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
167116621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1672e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1673e7c4fc90SDmitry Karpeev 
1674e7c4fc90SDmitry Karpeev   Not collective
1675e7c4fc90SDmitry Karpeev 
1676e7c4fc90SDmitry Karpeev   Input Parameter:
1677e7c4fc90SDmitry Karpeev . dm - the DM object
1678e7c4fc90SDmitry Karpeev 
1679e7c4fc90SDmitry Karpeev   Output Parameters:
16800298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16810298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16820298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16830298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1684e7c4fc90SDmitry Karpeev 
1685e7c4fc90SDmitry Karpeev   Level: intermediate
1686e7c4fc90SDmitry Karpeev 
1687e7c4fc90SDmitry Karpeev   Notes:
1688e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1689e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1690e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1691e7c4fc90SDmitry Karpeev 
1692e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1693e7c4fc90SDmitry Karpeev @*/
169416621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1695e7c4fc90SDmitry Karpeev {
1696e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1697e7c4fc90SDmitry Karpeev 
1698e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1699e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17008865f1eaSKarl Rupp   if (len) {
1701534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
17028865f1eaSKarl Rupp     *len = 0;
17038865f1eaSKarl Rupp   }
17048865f1eaSKarl Rupp   if (namelist) {
17058865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
17068865f1eaSKarl Rupp     *namelist = 0;
17078865f1eaSKarl Rupp   }
17088865f1eaSKarl Rupp   if (islist) {
17098865f1eaSKarl Rupp     PetscValidPointer(islist,4);
17108865f1eaSKarl Rupp     *islist = 0;
17118865f1eaSKarl Rupp   }
17128865f1eaSKarl Rupp   if (dmlist) {
17138865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
17148865f1eaSKarl Rupp     *dmlist = 0;
17158865f1eaSKarl Rupp   }
1716f3f0edfdSDmitry Karpeev   /*
1717f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1718f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1719f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1720f3f0edfdSDmitry Karpeev    */
1721ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
172216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1723435a35e8SMatthew G Knepley     PetscSection section;
1724435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1725435a35e8SMatthew G Knepley 
172692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1727435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1728435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1729f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
173003dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
173103dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
173203dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1733435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1734435a35e8SMatthew G Knepley         const char *fieldName;
1735435a35e8SMatthew G Knepley 
173603dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
173703dc3394SMatthew G. Knepley         if (namelist) {
1738435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1739435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1740435a35e8SMatthew G Knepley         }
174103dc3394SMatthew G. Knepley       }
1742435a35e8SMatthew G Knepley     } else {
174369ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1744e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
17450298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1746e7c4fc90SDmitry Karpeev     }
17478865f1eaSKarl Rupp   } else {
174816621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
174916621825SDmitry Karpeev   }
175016621825SDmitry Karpeev   PetscFunctionReturn(0);
175116621825SDmitry Karpeev }
175216621825SDmitry Karpeev 
1753564cec59SMatthew G. Knepley /*@
1754435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1755435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1756435a35e8SMatthew G Knepley 
1757435a35e8SMatthew G Knepley   Not collective
1758435a35e8SMatthew G Knepley 
1759435a35e8SMatthew G Knepley   Input Parameters:
17602adcc780SMatthew G. Knepley + dm        - The DM object
17612adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
17622adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1763435a35e8SMatthew G Knepley 
1764435a35e8SMatthew G Knepley   Output Parameters:
17652adcc780SMatthew G. Knepley + is - The global indices for the subproblem
17662adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1767435a35e8SMatthew G Knepley 
17685d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17695d3b26e6SMatthew G. Knepley 
1770435a35e8SMatthew G Knepley   Level: intermediate
1771435a35e8SMatthew G Knepley 
17725d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1773435a35e8SMatthew G Knepley @*/
177437bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1775435a35e8SMatthew G Knepley {
1776435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1777435a35e8SMatthew G Knepley 
1778435a35e8SMatthew G Knepley   PetscFunctionBegin;
1779435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1780435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17818865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17828865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1783b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1784435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1785435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1786435a35e8SMatthew G Knepley }
1787435a35e8SMatthew G Knepley 
17882adcc780SMatthew G. Knepley /*@C
17892adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17902adcc780SMatthew G. Knepley 
17912adcc780SMatthew G. Knepley   Not collective
17922adcc780SMatthew G. Knepley 
17932adcc780SMatthew G. Knepley   Input Parameter:
17942adcc780SMatthew G. Knepley + dms - The DM objects
17952adcc780SMatthew G. Knepley - len - The number of DMs
17962adcc780SMatthew G. Knepley 
17972adcc780SMatthew G. Knepley   Output Parameters:
1798a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17992adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
18002adcc780SMatthew G. Knepley 
18015d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
18025d3b26e6SMatthew G. Knepley 
18032adcc780SMatthew G. Knepley   Level: intermediate
18042adcc780SMatthew G. Knepley 
18055d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
18062adcc780SMatthew G. Knepley @*/
18072adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
18082adcc780SMatthew G. Knepley {
18092adcc780SMatthew G. Knepley   PetscInt       i;
18102adcc780SMatthew G. Knepley   PetscErrorCode ierr;
18112adcc780SMatthew G. Knepley 
18122adcc780SMatthew G. Knepley   PetscFunctionBegin;
18132adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
18142adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
18152adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1816a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
18172adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
18182adcc780SMatthew G. Knepley   if (len) {
1819b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1820b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1821b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
18222adcc780SMatthew G. Knepley   }
18232adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
18242adcc780SMatthew G. Knepley }
18252adcc780SMatthew G. Knepley 
182616621825SDmitry Karpeev 
182716621825SDmitry Karpeev /*@C
18288d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
18298d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
18308d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
18318d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
18328d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
183316621825SDmitry Karpeev 
183416621825SDmitry Karpeev   Not collective
183516621825SDmitry Karpeev 
183616621825SDmitry Karpeev   Input Parameter:
183716621825SDmitry Karpeev . dm - the DM object
183816621825SDmitry Karpeev 
183916621825SDmitry Karpeev   Output Parameters:
18400298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
18410298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
18420298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
18430298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
18440298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
184516621825SDmitry Karpeev 
184616621825SDmitry Karpeev   Level: intermediate
184716621825SDmitry Karpeev 
184816621825SDmitry Karpeev   Notes:
184916621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
185016621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
185116621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
185216621825SDmitry Karpeev 
1853245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
185416621825SDmitry Karpeev @*/
18558d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
185616621825SDmitry Karpeev {
185716621825SDmitry Karpeev   PetscErrorCode      ierr;
1858be081cd6SPeter Brune   DMSubDomainHookLink link;
1859be081cd6SPeter Brune   PetscInt            i,l;
186016621825SDmitry Karpeev 
186116621825SDmitry Karpeev   PetscFunctionBegin;
186216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
186314a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
18640298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
18650298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
18660298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
18670298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1868f3f0edfdSDmitry Karpeev   /*
1869f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1870f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1871f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1872f3f0edfdSDmitry Karpeev    */
1873ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
187416621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1875be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
187614a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1877f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1878be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1879be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1880be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1881be081cd6SPeter Brune         }
1882648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1883e7c4fc90SDmitry Karpeev       }
188414a18fd3SPeter Brune     }
188514a18fd3SPeter Brune     if (len) *len = l;
188614a18fd3SPeter Brune   }
1887e30e807fSPeter Brune   PetscFunctionReturn(0);
1888e30e807fSPeter Brune }
1889e30e807fSPeter Brune 
1890e30e807fSPeter Brune 
1891e30e807fSPeter Brune /*@C
1892e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1893e30e807fSPeter Brune 
1894e30e807fSPeter Brune   Not collective
1895e30e807fSPeter Brune 
1896e30e807fSPeter Brune   Input Parameters:
1897e30e807fSPeter Brune + dm - the DM object
1898e30e807fSPeter Brune . n  - the number of subdomain scatters
1899e30e807fSPeter Brune - subdms - the local subdomains
1900e30e807fSPeter Brune 
1901e30e807fSPeter Brune   Output Parameters:
1902e30e807fSPeter Brune + n     - the number of scatters returned
1903e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1904e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1905e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1906e30e807fSPeter Brune 
190795452b02SPatrick Sanan   Notes:
190895452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1909e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1910e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1911e30e807fSPeter Brune   solution and residual data.
1912e30e807fSPeter Brune 
1913e30e807fSPeter Brune   Level: developer
1914e30e807fSPeter Brune 
1915e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1916e30e807fSPeter Brune @*/
1917e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1918e30e807fSPeter Brune {
1919e30e807fSPeter Brune   PetscErrorCode ierr;
1920e30e807fSPeter Brune 
1921e30e807fSPeter Brune   PetscFunctionBegin;
1922e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1923e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1924b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
1925e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
1926e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1927e7c4fc90SDmitry Karpeev }
1928e7c4fc90SDmitry Karpeev 
192947c6ae99SBarry Smith /*@
193047c6ae99SBarry Smith   DMRefine - Refines a DM object
193147c6ae99SBarry Smith 
1932d083f849SBarry Smith   Collective on dm
193347c6ae99SBarry Smith 
193447c6ae99SBarry Smith   Input Parameter:
193547c6ae99SBarry Smith + dm   - the DM object
193691d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
193747c6ae99SBarry Smith 
193847c6ae99SBarry Smith   Output Parameter:
19390298fd71SBarry Smith . dmf - the refined DM, or NULL
1940ae0a1c52SMatthew G Knepley 
1941412e9a14SMatthew G. Knepley   Options Dtabase Keys:
1942412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
1943412e9a14SMatthew G. Knepley 
19440298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
194547c6ae99SBarry Smith 
194647c6ae99SBarry Smith   Level: developer
194747c6ae99SBarry Smith 
1948e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
194947c6ae99SBarry Smith @*/
19507087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
195147c6ae99SBarry Smith {
195247c6ae99SBarry Smith   PetscErrorCode   ierr;
1953c833c3b5SJed Brown   DMRefineHookLink link;
195447c6ae99SBarry Smith 
195547c6ae99SBarry Smith   PetscFunctionBegin;
1956732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1957b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
19581ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
195947c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
19604057135bSMatthew G Knepley   if (*dmf) {
196143842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
19628865f1eaSKarl Rupp 
19638cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
19648865f1eaSKarl Rupp 
1965644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
19660598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1967656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
19688865f1eaSKarl Rupp 
1969e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1970c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
19718865f1eaSKarl Rupp       if (link->refinehook) {
19728865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
19738865f1eaSKarl Rupp       }
1974c833c3b5SJed Brown     }
1975c833c3b5SJed Brown   }
19761ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1977c833c3b5SJed Brown   PetscFunctionReturn(0);
1978c833c3b5SJed Brown }
1979c833c3b5SJed Brown 
1980bb9467b5SJed Brown /*@C
1981c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1982c833c3b5SJed Brown 
1983c833c3b5SJed Brown    Logically Collective
1984c833c3b5SJed Brown 
1985c833c3b5SJed Brown    Input Arguments:
1986c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1987c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1988c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19890298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1990c833c3b5SJed Brown 
1991c833c3b5SJed Brown    Calling sequence of refinehook:
1992c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1993c833c3b5SJed Brown 
1994c833c3b5SJed Brown +  coarse - coarse level DM
1995c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1996c833c3b5SJed Brown -  ctx - optional user-defined function context
1997c833c3b5SJed Brown 
1998c833c3b5SJed Brown    Calling sequence for interphook:
1999c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2000c833c3b5SJed Brown 
2001c833c3b5SJed Brown +  coarse - coarse level DM
2002c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2003c833c3b5SJed Brown .  fine - fine level DM to update
2004c833c3b5SJed Brown -  ctx - optional user-defined function context
2005c833c3b5SJed Brown 
2006c833c3b5SJed Brown    Level: advanced
2007c833c3b5SJed Brown 
2008c833c3b5SJed Brown    Notes:
2009c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2010c833c3b5SJed Brown 
2011c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2012c833c3b5SJed Brown 
2013bb9467b5SJed Brown    This function is currently not available from Fortran.
2014bb9467b5SJed Brown 
2015c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2016c833c3b5SJed Brown @*/
2017c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2018c833c3b5SJed Brown {
2019c833c3b5SJed Brown   PetscErrorCode   ierr;
2020c833c3b5SJed Brown   DMRefineHookLink link,*p;
2021c833c3b5SJed Brown 
2022c833c3b5SJed Brown   PetscFunctionBegin;
2023c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20243d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
20253d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
20263d8e3701SJed Brown   }
202795dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2028c833c3b5SJed Brown   link->refinehook = refinehook;
2029c833c3b5SJed Brown   link->interphook = interphook;
2030c833c3b5SJed Brown   link->ctx        = ctx;
20310298fd71SBarry Smith   link->next       = NULL;
2032c833c3b5SJed Brown   *p               = link;
2033c833c3b5SJed Brown   PetscFunctionReturn(0);
2034c833c3b5SJed Brown }
2035c833c3b5SJed Brown 
20363d8e3701SJed Brown /*@C
20373d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
20383d8e3701SJed Brown 
20393d8e3701SJed Brown    Logically Collective
20403d8e3701SJed Brown 
20413d8e3701SJed Brown    Input Arguments:
20423d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
20433d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
20443d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
20453d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
20463d8e3701SJed Brown 
20473d8e3701SJed Brown    Level: advanced
20483d8e3701SJed Brown 
20493d8e3701SJed Brown    Notes:
20503d8e3701SJed Brown    This function does nothing if the hook is not in the list.
20513d8e3701SJed Brown 
20523d8e3701SJed Brown    This function is currently not available from Fortran.
20533d8e3701SJed Brown 
20543d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20553d8e3701SJed Brown @*/
20563d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
20573d8e3701SJed Brown {
20583d8e3701SJed Brown   PetscErrorCode   ierr;
20593d8e3701SJed Brown   DMRefineHookLink link,*p;
20603d8e3701SJed Brown 
20613d8e3701SJed Brown   PetscFunctionBegin;
20623d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20633d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
20643d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
20653d8e3701SJed Brown       link = *p;
20663d8e3701SJed Brown       *p = link->next;
20673d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
20683d8e3701SJed Brown       break;
20693d8e3701SJed Brown     }
20703d8e3701SJed Brown   }
20713d8e3701SJed Brown   PetscFunctionReturn(0);
20723d8e3701SJed Brown }
20733d8e3701SJed Brown 
2074c833c3b5SJed Brown /*@
2075c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2076c833c3b5SJed Brown 
2077c833c3b5SJed Brown    Collective if any hooks are
2078c833c3b5SJed Brown 
2079c833c3b5SJed Brown    Input Arguments:
2080c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2081e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2082c833c3b5SJed Brown -  fine - finer DM to update
2083c833c3b5SJed Brown 
2084c833c3b5SJed Brown    Level: developer
2085c833c3b5SJed Brown 
2086c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2087c833c3b5SJed Brown @*/
2088c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2089c833c3b5SJed Brown {
2090c833c3b5SJed Brown   PetscErrorCode   ierr;
2091c833c3b5SJed Brown   DMRefineHookLink link;
2092c833c3b5SJed Brown 
2093c833c3b5SJed Brown   PetscFunctionBegin;
2094c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20958865f1eaSKarl Rupp     if (link->interphook) {
20968865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20978865f1eaSKarl Rupp     }
20984057135bSMatthew G Knepley   }
209947c6ae99SBarry Smith   PetscFunctionReturn(0);
210047c6ae99SBarry Smith }
210147c6ae99SBarry Smith 
2102eb3f98d2SBarry Smith /*@
2103eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2104eb3f98d2SBarry Smith 
2105eb3f98d2SBarry Smith     Not Collective
2106eb3f98d2SBarry Smith 
2107eb3f98d2SBarry Smith     Input Parameter:
2108eb3f98d2SBarry Smith .   dm - the DM object
2109eb3f98d2SBarry Smith 
2110eb3f98d2SBarry Smith     Output Parameter:
2111eb3f98d2SBarry Smith .   level - number of refinements
2112eb3f98d2SBarry Smith 
2113eb3f98d2SBarry Smith     Level: developer
2114eb3f98d2SBarry Smith 
21156a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2116eb3f98d2SBarry Smith 
2117eb3f98d2SBarry Smith @*/
2118eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2119eb3f98d2SBarry Smith {
2120eb3f98d2SBarry Smith   PetscFunctionBegin;
2121eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2122eb3f98d2SBarry Smith   *level = dm->levelup;
2123eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2124eb3f98d2SBarry Smith }
2125eb3f98d2SBarry Smith 
2126fef3a512SBarry Smith /*@
2127fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2128fef3a512SBarry Smith 
2129fef3a512SBarry Smith     Not Collective
2130fef3a512SBarry Smith 
2131fef3a512SBarry Smith     Input Parameter:
2132fef3a512SBarry Smith +   dm - the DM object
2133fef3a512SBarry Smith -   level - number of refinements
2134fef3a512SBarry Smith 
2135fef3a512SBarry Smith     Level: advanced
2136fef3a512SBarry Smith 
213795452b02SPatrick Sanan     Notes:
213895452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2139fef3a512SBarry Smith 
2140fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2141fef3a512SBarry Smith 
2142fef3a512SBarry Smith @*/
2143fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2144fef3a512SBarry Smith {
2145fef3a512SBarry Smith   PetscFunctionBegin;
2146fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2147fef3a512SBarry Smith   dm->levelup = level;
2148fef3a512SBarry Smith   PetscFunctionReturn(0);
2149fef3a512SBarry Smith }
2150fef3a512SBarry Smith 
2151ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2152ca3d3a14SMatthew G. Knepley {
2153ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2154ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2155ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2156ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2157ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2158ca3d3a14SMatthew G. Knepley }
2159ca3d3a14SMatthew G. Knepley 
2160ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2161ca3d3a14SMatthew G. Knepley {
2162ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2163ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2164ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2165ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2166ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2167ca3d3a14SMatthew G. Knepley }
2168ca3d3a14SMatthew G. Knepley 
2169ca3d3a14SMatthew G. Knepley /*@
2170c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2171ca3d3a14SMatthew G. Knepley 
2172ca3d3a14SMatthew G. Knepley   Input Parameter:
2173ca3d3a14SMatthew G. Knepley . dm - The DM
2174ca3d3a14SMatthew G. Knepley 
2175ca3d3a14SMatthew G. Knepley   Output Parameter:
2176ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2177ca3d3a14SMatthew G. Knepley 
2178ca3d3a14SMatthew G. Knepley   Level: developer
2179ca3d3a14SMatthew G. Knepley 
2180436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2181ca3d3a14SMatthew G. Knepley @*/
2182ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2183ca3d3a14SMatthew G. Knepley {
2184ca3d3a14SMatthew G. Knepley   Vec            tv;
2185ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2186ca3d3a14SMatthew G. Knepley 
2187ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2188ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2189534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2190ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2191ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2192ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2193ca3d3a14SMatthew G. Knepley }
2194ca3d3a14SMatthew G. Knepley 
2195ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2196ca3d3a14SMatthew G. Knepley {
2197ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2198ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2199ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2200ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2201ca3d3a14SMatthew G. Knepley 
2202ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2203ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
220492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2205ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2206ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2207ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
220892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2209ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2210ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2211ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2212ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2213ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2214ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2215ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2216ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2217ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2218ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2219ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2220ca3d3a14SMatthew G. Knepley     }
2221ca3d3a14SMatthew G. Knepley   }
2222ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2223ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2224ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2225ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2226ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2227ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2228ca3d3a14SMatthew G. Knepley       if (dof) {
2229ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2230ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2231ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2232ca3d3a14SMatthew G. Knepley 
2233ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2234ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2235ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2236580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2237ca3d3a14SMatthew G. Knepley       }
2238ca3d3a14SMatthew G. Knepley     }
2239ca3d3a14SMatthew G. Knepley   }
2240ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2241ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2242ca3d3a14SMatthew G. Knepley }
2243ca3d3a14SMatthew G. Knepley 
2244ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2245ca3d3a14SMatthew G. Knepley {
2246ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2247ca3d3a14SMatthew G. Knepley 
2248ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2249ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2250ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2251ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2252ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2253ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2254ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2255ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2256ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2257ca3d3a14SMatthew G. Knepley }
2258ca3d3a14SMatthew G. Knepley 
2259bb9467b5SJed Brown /*@C
2260baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2261baf369e7SPeter Brune 
2262baf369e7SPeter Brune    Logically Collective
2263baf369e7SPeter Brune 
2264baf369e7SPeter Brune    Input Arguments:
2265baf369e7SPeter Brune +  dm - the DM
2266baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2267baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
22680298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2269baf369e7SPeter Brune 
2270baf369e7SPeter Brune    Calling sequence for beginhook:
2271baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2272baf369e7SPeter Brune 
2273baf369e7SPeter Brune +  dm - global DM
2274baf369e7SPeter Brune .  g - global vector
2275baf369e7SPeter Brune .  mode - mode
2276baf369e7SPeter Brune .  l - local vector
2277baf369e7SPeter Brune -  ctx - optional user-defined function context
2278baf369e7SPeter Brune 
2279baf369e7SPeter Brune 
2280baf369e7SPeter Brune    Calling sequence for endhook:
2281ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2282baf369e7SPeter Brune 
2283baf369e7SPeter Brune +  global - global DM
2284baf369e7SPeter Brune -  ctx - optional user-defined function context
2285baf369e7SPeter Brune 
2286baf369e7SPeter Brune    Level: advanced
2287baf369e7SPeter Brune 
2288baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2289baf369e7SPeter Brune @*/
2290baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2291baf369e7SPeter Brune {
2292baf369e7SPeter Brune   PetscErrorCode          ierr;
2293baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2294baf369e7SPeter Brune 
2295baf369e7SPeter Brune   PetscFunctionBegin;
2296baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2297baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
229895dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2299baf369e7SPeter Brune   link->beginhook = beginhook;
2300baf369e7SPeter Brune   link->endhook   = endhook;
2301baf369e7SPeter Brune   link->ctx       = ctx;
23020298fd71SBarry Smith   link->next      = NULL;
2303baf369e7SPeter Brune   *p              = link;
2304baf369e7SPeter Brune   PetscFunctionReturn(0);
2305baf369e7SPeter Brune }
2306baf369e7SPeter Brune 
23074c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
23084c274da1SToby Isaac {
23094c274da1SToby Isaac   Mat cMat;
23104c274da1SToby Isaac   Vec cVec;
23114c274da1SToby Isaac   PetscSection section, cSec;
23124c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
23134c274da1SToby Isaac   PetscErrorCode ierr;
23144c274da1SToby Isaac 
23154c274da1SToby Isaac   PetscFunctionBegin;
23164c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23174c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
23184c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
23195db9a05bSToby Isaac     PetscInt nRows;
23205db9a05bSToby Isaac 
23215db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
23225db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
232392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
23247711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
23254c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
23264c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
23274c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
23284c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
23294c274da1SToby Isaac       if (dof) {
23304c274da1SToby Isaac         PetscScalar *vals;
23314c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
23324c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
23334c274da1SToby Isaac       }
23344c274da1SToby Isaac     }
23354c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
23364c274da1SToby Isaac   }
23374c274da1SToby Isaac   PetscFunctionReturn(0);
23384c274da1SToby Isaac }
23394c274da1SToby Isaac 
234047c6ae99SBarry Smith /*@
234101729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
234201729b5cSPatrick Sanan 
2343d083f849SBarry Smith     Neighbor-wise Collective on dm
234401729b5cSPatrick Sanan 
234501729b5cSPatrick Sanan     Input Parameters:
234601729b5cSPatrick Sanan +   dm - the DM object
234701729b5cSPatrick Sanan .   g - the global vector
234801729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
234901729b5cSPatrick Sanan -   l - the local vector
235001729b5cSPatrick Sanan 
235101729b5cSPatrick Sanan     Notes:
235201729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
235301729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
235401729b5cSPatrick Sanan 
235501729b5cSPatrick Sanan     Level: beginner
235601729b5cSPatrick Sanan 
235701729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
235801729b5cSPatrick Sanan 
235901729b5cSPatrick Sanan @*/
236001729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
236101729b5cSPatrick Sanan {
236201729b5cSPatrick Sanan   PetscErrorCode ierr;
236301729b5cSPatrick Sanan 
236401729b5cSPatrick Sanan   PetscFunctionBegin;
236501729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
236601729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
236701729b5cSPatrick Sanan   PetscFunctionReturn(0);
236801729b5cSPatrick Sanan }
236901729b5cSPatrick Sanan 
237001729b5cSPatrick Sanan /*@
237147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
237247c6ae99SBarry Smith 
2373d083f849SBarry Smith     Neighbor-wise Collective on dm
237447c6ae99SBarry Smith 
237547c6ae99SBarry Smith     Input Parameters:
237647c6ae99SBarry Smith +   dm - the DM object
237747c6ae99SBarry Smith .   g - the global vector
237847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
237947c6ae99SBarry Smith -   l - the local vector
238047c6ae99SBarry Smith 
238101729b5cSPatrick Sanan     Level: intermediate
238247c6ae99SBarry Smith 
238301729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
238447c6ae99SBarry Smith 
238547c6ae99SBarry Smith @*/
23867087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
238747c6ae99SBarry Smith {
23887128ae9fSMatthew G Knepley   PetscSF                 sf;
238947c6ae99SBarry Smith   PetscErrorCode          ierr;
2390baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
239147c6ae99SBarry Smith 
239247c6ae99SBarry Smith   PetscFunctionBegin;
2393171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2394baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
23958865f1eaSKarl Rupp     if (link->beginhook) {
23968865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
23978865f1eaSKarl Rupp     }
2398baf369e7SPeter Brune   }
23991bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
24007128ae9fSMatthew G Knepley   if (sf) {
2401ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2402ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
24037128ae9fSMatthew G Knepley 
240482f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2405fa88e482SJed Brown     ierr = VecGetArrayInPlace(l, &lArray);CHKERRQ(ierr);
2406fa88e482SJed Brown     ierr = VecGetArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
24077128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2408fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(l, &lArray);CHKERRQ(ierr);
2409fa88e482SJed Brown     ierr = VecRestoreArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
24107128ae9fSMatthew G Knepley   } else {
241133907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2412843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
24137128ae9fSMatthew G Knepley   }
241447c6ae99SBarry Smith   PetscFunctionReturn(0);
241547c6ae99SBarry Smith }
241647c6ae99SBarry Smith 
241747c6ae99SBarry Smith /*@
241847c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
241947c6ae99SBarry Smith 
2420d083f849SBarry Smith     Neighbor-wise Collective on dm
242147c6ae99SBarry Smith 
242247c6ae99SBarry Smith     Input Parameters:
242347c6ae99SBarry Smith +   dm - the DM object
242447c6ae99SBarry Smith .   g - the global vector
242547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
242647c6ae99SBarry Smith -   l - the local vector
242747c6ae99SBarry Smith 
242801729b5cSPatrick Sanan     Level: intermediate
242947c6ae99SBarry Smith 
243001729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
243147c6ae99SBarry Smith 
243247c6ae99SBarry Smith @*/
24337087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
243447c6ae99SBarry Smith {
24357128ae9fSMatthew G Knepley   PetscSF                 sf;
243647c6ae99SBarry Smith   PetscErrorCode          ierr;
2437ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2438ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2439ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2440baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
244147c6ae99SBarry Smith 
244247c6ae99SBarry Smith   PetscFunctionBegin;
2443171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24441bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2445ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
24467128ae9fSMatthew G Knepley   if (sf) {
244782f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24487128ae9fSMatthew G Knepley 
2449fa88e482SJed Brown     ierr = VecGetArrayInPlace(l, &lArray);CHKERRQ(ierr);
2450fa88e482SJed Brown     ierr = VecGetArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
24517128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2452fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(l, &lArray);CHKERRQ(ierr);
2453fa88e482SJed Brown     ierr = VecRestoreArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
2454ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
24557128ae9fSMatthew G Knepley   } else {
245633907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2457843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
24587128ae9fSMatthew G Knepley   }
24594c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2460baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2461baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2462baf369e7SPeter Brune   }
246347c6ae99SBarry Smith   PetscFunctionReturn(0);
246447c6ae99SBarry Smith }
246547c6ae99SBarry Smith 
2466d4d07f1eSToby Isaac /*@C
2467d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2468d4d07f1eSToby Isaac 
2469d4d07f1eSToby Isaac    Logically Collective
2470d4d07f1eSToby Isaac 
2471d4d07f1eSToby Isaac    Input Arguments:
2472d4d07f1eSToby Isaac +  dm - the DM
2473d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2474d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2475d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2476d4d07f1eSToby Isaac 
2477d4d07f1eSToby Isaac    Calling sequence for beginhook:
2478d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2479d4d07f1eSToby Isaac 
2480d4d07f1eSToby Isaac +  dm - global DM
2481d4d07f1eSToby Isaac .  l - local vector
2482d4d07f1eSToby Isaac .  mode - mode
2483d4d07f1eSToby Isaac .  g - global vector
2484d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2485d4d07f1eSToby Isaac 
2486d4d07f1eSToby Isaac 
2487d4d07f1eSToby Isaac    Calling sequence for endhook:
2488d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2489d4d07f1eSToby Isaac 
2490d4d07f1eSToby Isaac +  global - global DM
2491d4d07f1eSToby Isaac .  l - local vector
2492d4d07f1eSToby Isaac .  mode - mode
2493d4d07f1eSToby Isaac .  g - global vector
2494d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2495d4d07f1eSToby Isaac 
2496d4d07f1eSToby Isaac    Level: advanced
2497d4d07f1eSToby Isaac 
2498d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2499d4d07f1eSToby Isaac @*/
2500d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2501d4d07f1eSToby Isaac {
2502d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2503d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2504d4d07f1eSToby Isaac 
2505d4d07f1eSToby Isaac   PetscFunctionBegin;
2506d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2507d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
250895dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2509d4d07f1eSToby Isaac   link->beginhook = beginhook;
2510d4d07f1eSToby Isaac   link->endhook   = endhook;
2511d4d07f1eSToby Isaac   link->ctx       = ctx;
2512d4d07f1eSToby Isaac   link->next      = NULL;
2513d4d07f1eSToby Isaac   *p              = link;
2514d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2515d4d07f1eSToby Isaac }
2516d4d07f1eSToby Isaac 
25174c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
25184c274da1SToby Isaac {
25194c274da1SToby Isaac   Mat cMat;
25204c274da1SToby Isaac   Vec cVec;
25214c274da1SToby Isaac   PetscSection section, cSec;
25224c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25234c274da1SToby Isaac   PetscErrorCode ierr;
25244c274da1SToby Isaac 
25254c274da1SToby Isaac   PetscFunctionBegin;
25264c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25274c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
25284c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
25295db9a05bSToby Isaac     PetscInt nRows;
25305db9a05bSToby Isaac 
25315db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
25325db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
253392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
25347711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
25354c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
25364c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25374c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
25384c274da1SToby Isaac       if (dof) {
25394c274da1SToby Isaac         PetscInt d;
25404c274da1SToby Isaac         PetscScalar *vals;
25414c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
25424c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
25434c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
25444c274da1SToby Isaac          * we just extracted */
25454c274da1SToby Isaac         for (d = 0; d < dof; d++) {
25464c274da1SToby Isaac           vals[d] = 0.;
25474c274da1SToby Isaac         }
25484c274da1SToby Isaac       }
25494c274da1SToby Isaac     }
25504c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
25514c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
25524c274da1SToby Isaac   }
25534c274da1SToby Isaac   PetscFunctionReturn(0);
25544c274da1SToby Isaac }
255501729b5cSPatrick Sanan /*@
255601729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
255701729b5cSPatrick Sanan 
2558d083f849SBarry Smith     Neighbor-wise Collective on dm
255901729b5cSPatrick Sanan 
256001729b5cSPatrick Sanan     Input Parameters:
256101729b5cSPatrick Sanan +   dm - the DM object
256201729b5cSPatrick Sanan .   l - the local vector
256301729b5cSPatrick Sanan .   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.
256401729b5cSPatrick Sanan -   g - the global vector
256501729b5cSPatrick Sanan 
256601729b5cSPatrick Sanan     Notes:
256701729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
256801729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
256901729b5cSPatrick Sanan 
257001729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
257101729b5cSPatrick Sanan            INSERT_VALUES is not supported for DMDA; in that case simply compute the values directly into a global vector instead of a local one.
257201729b5cSPatrick Sanan 
257301729b5cSPatrick Sanan     Level: beginner
257401729b5cSPatrick Sanan 
257501729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
257601729b5cSPatrick Sanan 
257701729b5cSPatrick Sanan @*/
257801729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
257901729b5cSPatrick Sanan {
258001729b5cSPatrick Sanan   PetscErrorCode ierr;
258101729b5cSPatrick Sanan 
258201729b5cSPatrick Sanan   PetscFunctionBegin;
258301729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
258401729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
258501729b5cSPatrick Sanan   PetscFunctionReturn(0);
258601729b5cSPatrick Sanan }
25874c274da1SToby Isaac 
258847c6ae99SBarry Smith /*@
258901729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
25909a42bb27SBarry Smith 
2591d083f849SBarry Smith     Neighbor-wise Collective on dm
25929a42bb27SBarry Smith 
25939a42bb27SBarry Smith     Input Parameters:
25949a42bb27SBarry Smith +   dm - the DM object
2595f6813fd5SJed Brown .   l - the local vector
25961eb28f2eSBarry 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.
25971eb28f2eSBarry Smith -   g - the global vector
25989a42bb27SBarry Smith 
259995452b02SPatrick Sanan     Notes:
260095452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
260184330215SMatthew 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.
26029a42bb27SBarry Smith 
260301729b5cSPatrick Sanan     Level: intermediate
26049a42bb27SBarry Smith 
260501729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
26069a42bb27SBarry Smith 
26079a42bb27SBarry Smith @*/
26087087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
26099a42bb27SBarry Smith {
26107128ae9fSMatthew G Knepley   PetscSF                 sf;
261184330215SMatthew G. Knepley   PetscSection            s, gs;
2612d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2613ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2614ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2615ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2616fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
261784330215SMatthew G. Knepley   PetscErrorCode          ierr;
26189a42bb27SBarry Smith 
26199a42bb27SBarry Smith   PetscFunctionBegin;
2620171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2621d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2622d4d07f1eSToby Isaac     if (link->beginhook) {
2623d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2624d4d07f1eSToby Isaac     }
2625d4d07f1eSToby Isaac   }
26264c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
26271bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
262892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
26297128ae9fSMatthew G Knepley   switch (mode) {
26307128ae9fSMatthew G Knepley   case INSERT_VALUES:
26317128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2632304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
263384330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
26347128ae9fSMatthew G Knepley   case ADD_VALUES:
26357128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2636304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
263784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
26387128ae9fSMatthew G Knepley   default:
263982f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26407128ae9fSMatthew G Knepley   }
2641ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2642ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2643ca3d3a14SMatthew G. Knepley     if (transform) {
2644ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2645ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2646ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2647ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2648fa88e482SJed Brown     } else if (isInsert) {
2649ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2650fa88e482SJed Brown     } else {
2651fa88e482SJed Brown       ierr = VecGetArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2652fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2653ca3d3a14SMatthew G. Knepley     }
2654fa88e482SJed Brown     if (s && isInsert) {
26557128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2656fa88e482SJed Brown     } else {
2657fa88e482SJed Brown       ierr = VecGetArrayInPlace(g, &gArray);CHKERRQ(ierr);
2658fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2659fa88e482SJed Brown     }
2660ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2661a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
266284330215SMatthew G. Knepley     } else if (s && isInsert) {
266384330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
266484330215SMatthew G. Knepley 
2665e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
266684330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
266784330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
266884330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2669b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
267084330215SMatthew G. Knepley 
267184330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
267203442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
267384330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2674b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
267584330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
267684330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2677b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
267803442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2679b3b16f48SMatthew 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);
2680b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2681b3b16f48SMatthew G. Knepley         if (!gcdof) {
268284330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2683b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2684b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
268584330215SMatthew G. Knepley           const PetscInt *cdofs;
268684330215SMatthew G. Knepley           PetscInt        cind = 0;
268784330215SMatthew G. Knepley 
268884330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2689b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
269084330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2691b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
269284330215SMatthew G. Knepley           }
2693b3b16f48SMatthew 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);
269484330215SMatthew G. Knepley       }
2695ca3d3a14SMatthew G. Knepley     }
2696fa88e482SJed Brown     if (g_inplace) {
2697fa88e482SJed Brown       ierr = VecRestoreArrayInPlace(g, &gArray);CHKERRQ(ierr);
2698fa88e482SJed Brown     } else {
26997128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2700fa88e482SJed Brown     }
2701ca3d3a14SMatthew G. Knepley     if (transform) {
2702ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2703ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2704fa88e482SJed Brown     } else if (l_inplace) {
2705fa88e482SJed Brown       ierr = VecRestoreArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2706ca3d3a14SMatthew G. Knepley     } else {
2707ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2708ca3d3a14SMatthew G. Knepley     }
27097128ae9fSMatthew G Knepley   } else {
2710b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2711843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
27127128ae9fSMatthew G Knepley   }
27139a42bb27SBarry Smith   PetscFunctionReturn(0);
27149a42bb27SBarry Smith }
27159a42bb27SBarry Smith 
27169a42bb27SBarry Smith /*@
27179a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
271847c6ae99SBarry Smith 
2719d083f849SBarry Smith     Neighbor-wise Collective on dm
272047c6ae99SBarry Smith 
272147c6ae99SBarry Smith     Input Parameters:
272247c6ae99SBarry Smith +   dm - the DM object
2723f6813fd5SJed Brown .   l - the local vector
272447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2725f6813fd5SJed Brown -   g - the global vector
272647c6ae99SBarry Smith 
272701729b5cSPatrick Sanan     Level: intermediate
272847c6ae99SBarry Smith 
2729e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
273047c6ae99SBarry Smith 
273147c6ae99SBarry Smith @*/
27327087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
273347c6ae99SBarry Smith {
27347128ae9fSMatthew G Knepley   PetscSF                 sf;
273584330215SMatthew G. Knepley   PetscSection            s;
2736d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2737ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
273884330215SMatthew G. Knepley   PetscErrorCode          ierr;
273947c6ae99SBarry Smith 
274047c6ae99SBarry Smith   PetscFunctionBegin;
2741171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27421bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
274392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
27447128ae9fSMatthew G Knepley   switch (mode) {
27457128ae9fSMatthew G Knepley   case INSERT_VALUES:
27467128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
274784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
27487128ae9fSMatthew G Knepley   case ADD_VALUES:
27497128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
275084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
27517128ae9fSMatthew G Knepley   default:
275282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
27537128ae9fSMatthew G Knepley   }
275484330215SMatthew G. Knepley   if (sf && !isInsert) {
2755ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2756ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2757ca3d3a14SMatthew G. Knepley     Vec                tmpl;
275884330215SMatthew G. Knepley 
2759ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2760ca3d3a14SMatthew G. Knepley     if (transform) {
2761ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2762ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2763ca3d3a14SMatthew G. Knepley     } else {
2764fa88e482SJed Brown       ierr = VecGetArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2765ca3d3a14SMatthew G. Knepley     }
2766fa88e482SJed Brown     ierr = VecGetArrayInPlace(g, &gArray);CHKERRQ(ierr);
2767a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2768ca3d3a14SMatthew G. Knepley     if (transform) {
2769ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2770ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2771ca3d3a14SMatthew G. Knepley     } else {
2772fa88e482SJed Brown       ierr = VecRestoreArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2773ca3d3a14SMatthew G. Knepley     }
2774fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(g, &gArray);CHKERRQ(ierr);
277584330215SMatthew G. Knepley   } else if (s && isInsert) {
27767128ae9fSMatthew G Knepley   } else {
2777b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2778843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
27797128ae9fSMatthew G Knepley   }
2780d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2781d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2782d4d07f1eSToby Isaac   }
278347c6ae99SBarry Smith   PetscFunctionReturn(0);
278447c6ae99SBarry Smith }
278547c6ae99SBarry Smith 
2786f089877aSRichard Tran Mills /*@
2787bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2788bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2789d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2790f089877aSRichard Tran Mills 
2791d083f849SBarry Smith    Neighbor-wise Collective on dm
2792f089877aSRichard Tran Mills 
2793f089877aSRichard Tran Mills    Input Parameters:
2794f089877aSRichard Tran Mills +  dm - the DM object
2795bc0a1609SRichard Tran Mills .  g - the original local vector
2796bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2797f089877aSRichard Tran Mills 
2798bc0a1609SRichard Tran Mills    Output Parameter:
2799bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2800f089877aSRichard Tran Mills 
2801f089877aSRichard Tran Mills    Level: intermediate
2802f089877aSRichard Tran Mills 
2803bc0a1609SRichard Tran Mills    Notes:
2804bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2805bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2806bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2807bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2808bc0a1609SRichard Tran Mills 
2809bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2810f089877aSRichard Tran Mills 
2811f089877aSRichard Tran Mills @*/
2812f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2813f089877aSRichard Tran Mills {
2814f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2815f089877aSRichard Tran Mills 
2816f089877aSRichard Tran Mills   PetscFunctionBegin;
2817f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2818bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2819f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2820f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2821f089877aSRichard Tran Mills }
2822f089877aSRichard Tran Mills 
2823f089877aSRichard Tran Mills /*@
2824bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2825bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2826d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2827f089877aSRichard Tran Mills 
2828d083f849SBarry Smith    Neighbor-wise Collective on dm
2829f089877aSRichard Tran Mills 
2830f089877aSRichard Tran Mills    Input Parameters:
2831bc0a1609SRichard Tran Mills +  da - the DM object
2832bc0a1609SRichard Tran Mills .  g - the original local vector
2833bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2834f089877aSRichard Tran Mills 
2835bc0a1609SRichard Tran Mills    Output Parameter:
2836bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2837f089877aSRichard Tran Mills 
2838f089877aSRichard Tran Mills    Level: intermediate
2839f089877aSRichard Tran Mills 
2840bc0a1609SRichard Tran Mills    Notes:
2841bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2842bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2843bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2844bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2845bc0a1609SRichard Tran Mills 
2846bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2847f089877aSRichard Tran Mills 
2848f089877aSRichard Tran Mills @*/
2849f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2850f089877aSRichard Tran Mills {
2851f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2852f089877aSRichard Tran Mills 
2853f089877aSRichard Tran Mills   PetscFunctionBegin;
2854f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2855bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2856f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2857f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2858f089877aSRichard Tran Mills }
2859f089877aSRichard Tran Mills 
2860f089877aSRichard Tran Mills 
286147c6ae99SBarry Smith /*@
286247c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
286347c6ae99SBarry Smith 
2864d083f849SBarry Smith     Collective on dm
286547c6ae99SBarry Smith 
286647c6ae99SBarry Smith     Input Parameter:
286747c6ae99SBarry Smith +   dm - the DM object
286891d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
286947c6ae99SBarry Smith 
287047c6ae99SBarry Smith     Output Parameter:
287147c6ae99SBarry Smith .   dmc - the coarsened DM
287247c6ae99SBarry Smith 
287347c6ae99SBarry Smith     Level: developer
287447c6ae99SBarry Smith 
2875e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
287647c6ae99SBarry Smith 
287747c6ae99SBarry Smith @*/
28787087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
287947c6ae99SBarry Smith {
288047c6ae99SBarry Smith   PetscErrorCode    ierr;
2881b17ce1afSJed Brown   DMCoarsenHookLink link;
288247c6ae99SBarry Smith 
288347c6ae99SBarry Smith   PetscFunctionBegin;
2884171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2885b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
288647a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
288747c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2888b9d85ea2SLisandro Dalcin   if (*dmc) {
2889a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
289043842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
28918cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2892644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
28930598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
2894656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
2895e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2896b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
2897b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2898b17ce1afSJed Brown     }
2899b9d85ea2SLisandro Dalcin   }
290047a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2901b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2902b17ce1afSJed Brown   PetscFunctionReturn(0);
2903b17ce1afSJed Brown }
2904b17ce1afSJed Brown 
2905bb9467b5SJed Brown /*@C
2906b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2907b17ce1afSJed Brown 
2908b17ce1afSJed Brown    Logically Collective
2909b17ce1afSJed Brown 
2910b17ce1afSJed Brown    Input Arguments:
2911b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2912b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2913b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
29140298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2915b17ce1afSJed Brown 
2916b17ce1afSJed Brown    Calling sequence of coarsenhook:
2917b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2918b17ce1afSJed Brown 
2919b17ce1afSJed Brown +  fine - fine level DM
2920b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2921b17ce1afSJed Brown -  ctx - optional user-defined function context
2922b17ce1afSJed Brown 
2923b17ce1afSJed Brown    Calling sequence for restricthook:
2924c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2925b17ce1afSJed Brown 
2926b17ce1afSJed Brown +  fine - fine level DM
2927b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2928c833c3b5SJed Brown .  rscale - scaling vector for restriction
2929c833c3b5SJed Brown .  inject - matrix restricting by injection
2930b17ce1afSJed Brown .  coarse - coarse level DM to update
2931b17ce1afSJed Brown -  ctx - optional user-defined function context
2932b17ce1afSJed Brown 
2933b17ce1afSJed Brown    Level: advanced
2934b17ce1afSJed Brown 
2935b17ce1afSJed Brown    Notes:
2936b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2937b17ce1afSJed Brown 
2938b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2939b17ce1afSJed Brown 
2940b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2941b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2942b17ce1afSJed Brown 
2943bb9467b5SJed Brown    This function is currently not available from Fortran.
2944bb9467b5SJed Brown 
2945dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2946b17ce1afSJed Brown @*/
2947b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2948b17ce1afSJed Brown {
2949b17ce1afSJed Brown   PetscErrorCode    ierr;
2950b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2951b17ce1afSJed Brown 
2952b17ce1afSJed Brown   PetscFunctionBegin;
2953b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
29541e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
29551e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
29561e3d8eccSJed Brown   }
295795dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2958b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2959b17ce1afSJed Brown   link->restricthook = restricthook;
2960b17ce1afSJed Brown   link->ctx          = ctx;
29610298fd71SBarry Smith   link->next         = NULL;
2962b17ce1afSJed Brown   *p                 = link;
2963b17ce1afSJed Brown   PetscFunctionReturn(0);
2964b17ce1afSJed Brown }
2965b17ce1afSJed Brown 
2966dc822a44SJed Brown /*@C
2967dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2968dc822a44SJed Brown 
2969dc822a44SJed Brown    Logically Collective
2970dc822a44SJed Brown 
2971dc822a44SJed Brown    Input Arguments:
2972dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2973dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2974dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2975dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2976dc822a44SJed Brown 
2977dc822a44SJed Brown    Level: advanced
2978dc822a44SJed Brown 
2979dc822a44SJed Brown    Notes:
2980dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2981dc822a44SJed Brown 
2982dc822a44SJed Brown    This function is currently not available from Fortran.
2983dc822a44SJed Brown 
2984dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2985dc822a44SJed Brown @*/
2986dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2987dc822a44SJed Brown {
2988dc822a44SJed Brown   PetscErrorCode    ierr;
2989dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2990dc822a44SJed Brown 
2991dc822a44SJed Brown   PetscFunctionBegin;
2992dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2993dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2994dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2995dc822a44SJed Brown       link = *p;
2996dc822a44SJed Brown       *p = link->next;
2997dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2998dc822a44SJed Brown       break;
2999dc822a44SJed Brown     }
3000dc822a44SJed Brown   }
3001dc822a44SJed Brown   PetscFunctionReturn(0);
3002dc822a44SJed Brown }
3003dc822a44SJed Brown 
3004dc822a44SJed Brown 
3005b17ce1afSJed Brown /*@
3006b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3007b17ce1afSJed Brown 
3008b17ce1afSJed Brown    Collective if any hooks are
3009b17ce1afSJed Brown 
3010b17ce1afSJed Brown    Input Arguments:
3011b17ce1afSJed Brown +  fine - finer DM to use as a base
3012b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3013e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3014b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3015e91eccc2SStefano Zampini -  coarse - coarser DM to update
3016b17ce1afSJed Brown 
3017b17ce1afSJed Brown    Level: developer
3018b17ce1afSJed Brown 
3019b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3020b17ce1afSJed Brown @*/
3021b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3022b17ce1afSJed Brown {
3023b17ce1afSJed Brown   PetscErrorCode    ierr;
3024b17ce1afSJed Brown   DMCoarsenHookLink link;
3025b17ce1afSJed Brown 
3026b17ce1afSJed Brown   PetscFunctionBegin;
3027b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
30288865f1eaSKarl Rupp     if (link->restricthook) {
30298865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
30308865f1eaSKarl Rupp     }
3031b17ce1afSJed Brown   }
303247c6ae99SBarry Smith   PetscFunctionReturn(0);
303347c6ae99SBarry Smith }
303447c6ae99SBarry Smith 
3035bb9467b5SJed Brown /*@C
3036be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
30375dbd56e3SPeter Brune 
3038d083f849SBarry Smith    Logically Collective on global
30395dbd56e3SPeter Brune 
30405dbd56e3SPeter Brune    Input Arguments:
30415dbd56e3SPeter Brune +  global - global DM
3042ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
30435dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
30440298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
30455dbd56e3SPeter Brune 
3046ec4806b8SPeter Brune 
3047ec4806b8SPeter Brune    Calling sequence for ddhook:
3048ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3049ec4806b8SPeter Brune 
3050ec4806b8SPeter Brune +  global - global DM
3051ec4806b8SPeter Brune .  block  - block DM
3052ec4806b8SPeter Brune -  ctx - optional user-defined function context
3053ec4806b8SPeter Brune 
30545dbd56e3SPeter Brune    Calling sequence for restricthook:
3055ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
30565dbd56e3SPeter Brune 
30575dbd56e3SPeter Brune +  global - global DM
30585dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
30595dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3060ec4806b8SPeter Brune .  block  - block DM
30615dbd56e3SPeter Brune -  ctx - optional user-defined function context
30625dbd56e3SPeter Brune 
30635dbd56e3SPeter Brune    Level: advanced
30645dbd56e3SPeter Brune 
30655dbd56e3SPeter Brune    Notes:
3066ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
30675dbd56e3SPeter Brune 
30685dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
30695dbd56e3SPeter Brune 
30705dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3071ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
30725dbd56e3SPeter Brune 
3073bb9467b5SJed Brown    This function is currently not available from Fortran.
3074bb9467b5SJed Brown 
30755dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
30765dbd56e3SPeter Brune @*/
3077be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
30785dbd56e3SPeter Brune {
30795dbd56e3SPeter Brune   PetscErrorCode      ierr;
3080be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
30815dbd56e3SPeter Brune 
30825dbd56e3SPeter Brune   PetscFunctionBegin;
30835dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3084b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3085b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3086b3a6b972SJed Brown   }
308795dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
30885dbd56e3SPeter Brune   link->restricthook = restricthook;
3089be081cd6SPeter Brune   link->ddhook       = ddhook;
30905dbd56e3SPeter Brune   link->ctx          = ctx;
30910298fd71SBarry Smith   link->next         = NULL;
30925dbd56e3SPeter Brune   *p                 = link;
30935dbd56e3SPeter Brune   PetscFunctionReturn(0);
30945dbd56e3SPeter Brune }
30955dbd56e3SPeter Brune 
3096b3a6b972SJed Brown /*@C
3097b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3098b3a6b972SJed Brown 
3099b3a6b972SJed Brown    Logically Collective
3100b3a6b972SJed Brown 
3101b3a6b972SJed Brown    Input Arguments:
3102b3a6b972SJed Brown +  global - global DM
3103b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3104b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3105b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3106b3a6b972SJed Brown 
3107b3a6b972SJed Brown    Level: advanced
3108b3a6b972SJed Brown 
3109b3a6b972SJed Brown    Notes:
3110b3a6b972SJed Brown 
3111b3a6b972SJed Brown    This function is currently not available from Fortran.
3112b3a6b972SJed Brown 
3113b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3114b3a6b972SJed Brown @*/
3115b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3116b3a6b972SJed Brown {
3117b3a6b972SJed Brown   PetscErrorCode      ierr;
3118b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3119b3a6b972SJed Brown 
3120b3a6b972SJed Brown   PetscFunctionBegin;
3121b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3122b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3123b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3124b3a6b972SJed Brown       link = *p;
3125b3a6b972SJed Brown       *p = link->next;
3126b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3127b3a6b972SJed Brown       break;
3128b3a6b972SJed Brown     }
3129b3a6b972SJed Brown   }
3130b3a6b972SJed Brown   PetscFunctionReturn(0);
3131b3a6b972SJed Brown }
3132b3a6b972SJed Brown 
31335dbd56e3SPeter Brune /*@
3134be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
31355dbd56e3SPeter Brune 
31365dbd56e3SPeter Brune    Collective if any hooks are
31375dbd56e3SPeter Brune 
31385dbd56e3SPeter Brune    Input Arguments:
31395dbd56e3SPeter Brune +  fine - finer DM to use as a base
3140be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3141be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
31425dbd56e3SPeter Brune -  coarse - coarer DM to update
31435dbd56e3SPeter Brune 
31445dbd56e3SPeter Brune    Level: developer
31455dbd56e3SPeter Brune 
31465dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
31475dbd56e3SPeter Brune @*/
3148be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
31495dbd56e3SPeter Brune {
31505dbd56e3SPeter Brune   PetscErrorCode      ierr;
3151be081cd6SPeter Brune   DMSubDomainHookLink link;
31525dbd56e3SPeter Brune 
31535dbd56e3SPeter Brune   PetscFunctionBegin;
3154be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
31558865f1eaSKarl Rupp     if (link->restricthook) {
31568865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
31578865f1eaSKarl Rupp     }
31585dbd56e3SPeter Brune   }
31595dbd56e3SPeter Brune   PetscFunctionReturn(0);
31605dbd56e3SPeter Brune }
31615dbd56e3SPeter Brune 
31625fe1f584SPeter Brune /*@
31636a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
31645fe1f584SPeter Brune 
31655fe1f584SPeter Brune     Not Collective
31665fe1f584SPeter Brune 
31675fe1f584SPeter Brune     Input Parameter:
31685fe1f584SPeter Brune .   dm - the DM object
31695fe1f584SPeter Brune 
31705fe1f584SPeter Brune     Output Parameter:
31716a7d9d85SPeter Brune .   level - number of coarsenings
31725fe1f584SPeter Brune 
31735fe1f584SPeter Brune     Level: developer
31745fe1f584SPeter Brune 
31756a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31765fe1f584SPeter Brune 
31775fe1f584SPeter Brune @*/
31785fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
31795fe1f584SPeter Brune {
31805fe1f584SPeter Brune   PetscFunctionBegin;
31815fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3182b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
31835fe1f584SPeter Brune   *level = dm->leveldown;
31845fe1f584SPeter Brune   PetscFunctionReturn(0);
31855fe1f584SPeter Brune }
31865fe1f584SPeter Brune 
31879a64c4a8SMatthew G. Knepley /*@
31889a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
31899a64c4a8SMatthew G. Knepley 
31909a64c4a8SMatthew G. Knepley     Not Collective
31919a64c4a8SMatthew G. Knepley 
31929a64c4a8SMatthew G. Knepley     Input Parameters:
31939a64c4a8SMatthew G. Knepley +   dm - the DM object
31949a64c4a8SMatthew G. Knepley -   level - number of coarsenings
31959a64c4a8SMatthew G. Knepley 
31969a64c4a8SMatthew G. Knepley     Level: developer
31979a64c4a8SMatthew G. Knepley 
31989a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31999a64c4a8SMatthew G. Knepley @*/
32009a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
32019a64c4a8SMatthew G. Knepley {
32029a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
32039a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32049a64c4a8SMatthew G. Knepley   dm->leveldown = level;
32059a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
32069a64c4a8SMatthew G. Knepley }
32079a64c4a8SMatthew G. Knepley 
32085fe1f584SPeter Brune 
32095fe1f584SPeter Brune 
321047c6ae99SBarry Smith /*@C
321147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
321247c6ae99SBarry Smith 
3213d083f849SBarry Smith     Collective on dm
321447c6ae99SBarry Smith 
321547c6ae99SBarry Smith     Input Parameter:
321647c6ae99SBarry Smith +   dm - the DM object
321747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
321847c6ae99SBarry Smith 
321947c6ae99SBarry Smith     Output Parameter:
322047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
322147c6ae99SBarry Smith 
322247c6ae99SBarry Smith     Level: developer
322347c6ae99SBarry Smith 
3224e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
322547c6ae99SBarry Smith 
322647c6ae99SBarry Smith @*/
32277087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
322847c6ae99SBarry Smith {
322947c6ae99SBarry Smith   PetscErrorCode ierr;
323047c6ae99SBarry Smith 
323147c6ae99SBarry Smith   PetscFunctionBegin;
3232171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3233ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
323447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3235b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
323647c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
323747c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
323847c6ae99SBarry Smith   } else if (dm->ops->refine) {
323947c6ae99SBarry Smith     PetscInt i;
324047c6ae99SBarry Smith 
3241ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
324247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3243ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
324447c6ae99SBarry Smith     }
3245ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
324647c6ae99SBarry Smith   PetscFunctionReturn(0);
324747c6ae99SBarry Smith }
324847c6ae99SBarry Smith 
324947c6ae99SBarry Smith /*@C
325047c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
325147c6ae99SBarry Smith 
3252d083f849SBarry Smith     Collective on dm
325347c6ae99SBarry Smith 
325447c6ae99SBarry Smith     Input Parameter:
325547c6ae99SBarry Smith +   dm - the DM object
325647c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
325747c6ae99SBarry Smith 
325847c6ae99SBarry Smith     Output Parameter:
325947c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
326047c6ae99SBarry Smith 
326147c6ae99SBarry Smith     Level: developer
326247c6ae99SBarry Smith 
3263e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
326447c6ae99SBarry Smith 
326547c6ae99SBarry Smith @*/
32667087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
326747c6ae99SBarry Smith {
326847c6ae99SBarry Smith   PetscErrorCode ierr;
326947c6ae99SBarry Smith 
327047c6ae99SBarry Smith   PetscFunctionBegin;
3271171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3272ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
327347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
327447c6ae99SBarry Smith   PetscValidPointer(dmc,3);
327547c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
327647c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
327747c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
327847c6ae99SBarry Smith     PetscInt i;
327947c6ae99SBarry Smith 
3280ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
328147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3282ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
328347c6ae99SBarry Smith     }
3284ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
328547c6ae99SBarry Smith   PetscFunctionReturn(0);
328647c6ae99SBarry Smith }
328747c6ae99SBarry Smith 
32881a266240SBarry Smith /*@C
32891a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
32901a266240SBarry Smith 
32911a266240SBarry Smith     Not Collective
32921a266240SBarry Smith 
32931a266240SBarry Smith     Input Parameters:
32941a266240SBarry Smith +   dm - the DM object
32951a266240SBarry Smith -   destroy - the destroy function
32961a266240SBarry Smith 
32971a266240SBarry Smith     Level: intermediate
32981a266240SBarry Smith 
3299e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
33001a266240SBarry Smith 
3301f07f9ceaSJed Brown @*/
33021a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
33031a266240SBarry Smith {
33041a266240SBarry Smith   PetscFunctionBegin;
3305171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33061a266240SBarry Smith   dm->ctxdestroy = destroy;
33071a266240SBarry Smith   PetscFunctionReturn(0);
33081a266240SBarry Smith }
33091a266240SBarry Smith 
3310b07ff414SBarry Smith /*@
33111b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
331247c6ae99SBarry Smith 
331347c6ae99SBarry Smith     Not Collective
331447c6ae99SBarry Smith 
331547c6ae99SBarry Smith     Input Parameters:
331647c6ae99SBarry Smith +   dm - the DM object
331747c6ae99SBarry Smith -   ctx - the user context
331847c6ae99SBarry Smith 
331947c6ae99SBarry Smith     Level: intermediate
332047c6ae99SBarry Smith 
3321e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
332247c6ae99SBarry Smith 
332347c6ae99SBarry Smith @*/
33241b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
332547c6ae99SBarry Smith {
332647c6ae99SBarry Smith   PetscFunctionBegin;
3327171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
332847c6ae99SBarry Smith   dm->ctx = ctx;
332947c6ae99SBarry Smith   PetscFunctionReturn(0);
333047c6ae99SBarry Smith }
333147c6ae99SBarry Smith 
333247c6ae99SBarry Smith /*@
33331b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
333447c6ae99SBarry Smith 
333547c6ae99SBarry Smith     Not Collective
333647c6ae99SBarry Smith 
333747c6ae99SBarry Smith     Input Parameter:
333847c6ae99SBarry Smith .   dm - the DM object
333947c6ae99SBarry Smith 
334047c6ae99SBarry Smith     Output Parameter:
334147c6ae99SBarry Smith .   ctx - the user context
334247c6ae99SBarry Smith 
334347c6ae99SBarry Smith     Level: intermediate
334447c6ae99SBarry Smith 
3345e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
334647c6ae99SBarry Smith 
334747c6ae99SBarry Smith @*/
33481b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
334947c6ae99SBarry Smith {
335047c6ae99SBarry Smith   PetscFunctionBegin;
3351171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33521b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
335347c6ae99SBarry Smith   PetscFunctionReturn(0);
335447c6ae99SBarry Smith }
335547c6ae99SBarry Smith 
335608da532bSDmitry Karpeev /*@C
3357df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
335808da532bSDmitry Karpeev 
3359d083f849SBarry Smith     Logically Collective on dm
336008da532bSDmitry Karpeev 
336108da532bSDmitry Karpeev     Input Parameter:
336208da532bSDmitry Karpeev +   dm - the DM object
33630298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
336408da532bSDmitry Karpeev 
336508da532bSDmitry Karpeev     Level: intermediate
336608da532bSDmitry Karpeev 
3367835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
336808da532bSDmitry Karpeev          DMSetJacobian()
336908da532bSDmitry Karpeev 
337008da532bSDmitry Karpeev @*/
337108da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
337208da532bSDmitry Karpeev {
337308da532bSDmitry Karpeev   PetscFunctionBegin;
33745a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
337508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
337608da532bSDmitry Karpeev   PetscFunctionReturn(0);
337708da532bSDmitry Karpeev }
337808da532bSDmitry Karpeev 
337908da532bSDmitry Karpeev /*@
338008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
338108da532bSDmitry Karpeev 
338208da532bSDmitry Karpeev     Not Collective
338308da532bSDmitry Karpeev 
338408da532bSDmitry Karpeev     Input Parameter:
338508da532bSDmitry Karpeev .   dm - the DM object to destroy
338608da532bSDmitry Karpeev 
338708da532bSDmitry Karpeev     Output Parameter:
338808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
338908da532bSDmitry Karpeev 
339008da532bSDmitry Karpeev     Level: developer
339108da532bSDmitry Karpeev 
339274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
339308da532bSDmitry Karpeev 
339408da532bSDmitry Karpeev @*/
339508da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
339608da532bSDmitry Karpeev {
339708da532bSDmitry Karpeev   PetscFunctionBegin;
33985a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3399534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
340008da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
340108da532bSDmitry Karpeev   PetscFunctionReturn(0);
340208da532bSDmitry Karpeev }
340308da532bSDmitry Karpeev 
340408da532bSDmitry Karpeev /*@C
340508da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
340608da532bSDmitry Karpeev 
3407d083f849SBarry Smith     Logically Collective on dm
340808da532bSDmitry Karpeev 
340908da532bSDmitry Karpeev     Input Parameters:
3410907376e6SBarry Smith .   dm - the DM object
341108da532bSDmitry Karpeev 
341208da532bSDmitry Karpeev     Output parameters:
341308da532bSDmitry Karpeev +   xl - lower bound
341408da532bSDmitry Karpeev -   xu - upper bound
341508da532bSDmitry Karpeev 
3416907376e6SBarry Smith     Level: advanced
3417907376e6SBarry Smith 
341895452b02SPatrick Sanan     Notes:
341995452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
342008da532bSDmitry Karpeev 
342174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
342208da532bSDmitry Karpeev 
342308da532bSDmitry Karpeev @*/
342408da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
342508da532bSDmitry Karpeev {
342608da532bSDmitry Karpeev   PetscErrorCode ierr;
34275fd66863SKarl Rupp 
342808da532bSDmitry Karpeev   PetscFunctionBegin;
34295a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
343008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
34315a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3432b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
343308da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
343408da532bSDmitry Karpeev   PetscFunctionReturn(0);
343508da532bSDmitry Karpeev }
343608da532bSDmitry Karpeev 
3437b0ae01b7SPeter Brune /*@
3438b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3439b0ae01b7SPeter Brune 
3440b0ae01b7SPeter Brune     Not Collective
3441b0ae01b7SPeter Brune 
3442b0ae01b7SPeter Brune     Input Parameter:
3443b0ae01b7SPeter Brune .   dm - the DM object
3444b0ae01b7SPeter Brune 
3445b0ae01b7SPeter Brune     Output Parameter:
3446b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3447b0ae01b7SPeter Brune 
3448b0ae01b7SPeter Brune     Level: developer
3449b0ae01b7SPeter Brune 
34501565f0a7SPatrick Sanan .seealso DMCreateColoring()
3451b0ae01b7SPeter Brune 
3452b0ae01b7SPeter Brune @*/
3453b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3454b0ae01b7SPeter Brune {
3455b0ae01b7SPeter Brune   PetscFunctionBegin;
34565a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3457534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3458b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3459b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3460b0ae01b7SPeter Brune }
3461b0ae01b7SPeter Brune 
34623ad4599aSBarry Smith /*@
34633ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
34643ad4599aSBarry Smith 
34653ad4599aSBarry Smith     Not Collective
34663ad4599aSBarry Smith 
34673ad4599aSBarry Smith     Input Parameter:
34683ad4599aSBarry Smith .   dm - the DM object
34693ad4599aSBarry Smith 
34703ad4599aSBarry Smith     Output Parameter:
34713ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
34723ad4599aSBarry Smith 
34733ad4599aSBarry Smith     Level: developer
34743ad4599aSBarry Smith 
34751565f0a7SPatrick Sanan .seealso DMCreateRestriction()
34763ad4599aSBarry Smith 
34773ad4599aSBarry Smith @*/
34783ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
34793ad4599aSBarry Smith {
34803ad4599aSBarry Smith   PetscFunctionBegin;
34815a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3482534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34833ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
34843ad4599aSBarry Smith   PetscFunctionReturn(0);
34853ad4599aSBarry Smith }
34863ad4599aSBarry Smith 
3487a7058e45SLawrence Mitchell 
3488a7058e45SLawrence Mitchell /*@
3489a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3490a7058e45SLawrence Mitchell 
3491a7058e45SLawrence Mitchell     Not Collective
3492a7058e45SLawrence Mitchell 
3493a7058e45SLawrence Mitchell     Input Parameter:
3494a7058e45SLawrence Mitchell .   dm - the DM object
3495a7058e45SLawrence Mitchell 
3496a7058e45SLawrence Mitchell     Output Parameter:
3497a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3498a7058e45SLawrence Mitchell 
3499a7058e45SLawrence Mitchell     Level: developer
3500a7058e45SLawrence Mitchell 
35011565f0a7SPatrick Sanan .seealso DMCreateInjection()
3502a7058e45SLawrence Mitchell 
3503a7058e45SLawrence Mitchell @*/
3504a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3505a7058e45SLawrence Mitchell {
35064a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
35075a84ad33SLisandro Dalcin 
3508a7058e45SLawrence Mitchell   PetscFunctionBegin;
35095a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3510534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
35115a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
35124a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
35135a84ad33SLisandro Dalcin   } else {
35145a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
35155a84ad33SLisandro Dalcin   }
3516a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3517a7058e45SLawrence Mitchell }
3518a7058e45SLawrence Mitchell 
35190298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3520264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3521264ace61SBarry Smith 
3522264ace61SBarry Smith /*@C
3523264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3524264ace61SBarry Smith 
3525d083f849SBarry Smith   Collective on dm
3526264ace61SBarry Smith 
3527264ace61SBarry Smith   Input Parameters:
3528264ace61SBarry Smith + dm     - The DM object
3529264ace61SBarry Smith - method - The name of the DM type
3530264ace61SBarry Smith 
3531264ace61SBarry Smith   Options Database Key:
3532264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3533264ace61SBarry Smith 
3534264ace61SBarry Smith   Notes:
3535e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3536264ace61SBarry Smith 
3537264ace61SBarry Smith   Level: intermediate
3538264ace61SBarry Smith 
3539264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3540264ace61SBarry Smith @*/
354119fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3542264ace61SBarry Smith {
3543264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3544264ace61SBarry Smith   PetscBool      match;
3545264ace61SBarry Smith   PetscErrorCode ierr;
3546264ace61SBarry Smith 
3547264ace61SBarry Smith   PetscFunctionBegin;
3548264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3549251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3550264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3551264ace61SBarry Smith 
35520f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
35531c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3554ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3555264ace61SBarry Smith 
3556264ace61SBarry Smith   if (dm->ops->destroy) {
3557264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3558264ace61SBarry Smith   }
3559d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3560264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3561d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3562264ace61SBarry Smith   PetscFunctionReturn(0);
3563264ace61SBarry Smith }
3564264ace61SBarry Smith 
3565264ace61SBarry Smith /*@C
3566264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3567264ace61SBarry Smith 
3568264ace61SBarry Smith   Not Collective
3569264ace61SBarry Smith 
3570264ace61SBarry Smith   Input Parameter:
3571264ace61SBarry Smith . dm  - The DM
3572264ace61SBarry Smith 
3573264ace61SBarry Smith   Output Parameter:
3574264ace61SBarry Smith . type - The DM type name
3575264ace61SBarry Smith 
3576264ace61SBarry Smith   Level: intermediate
3577264ace61SBarry Smith 
3578264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3579264ace61SBarry Smith @*/
358019fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3581264ace61SBarry Smith {
3582264ace61SBarry Smith   PetscErrorCode ierr;
3583264ace61SBarry Smith 
3584264ace61SBarry Smith   PetscFunctionBegin;
3585264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3586c959eef4SJed Brown   PetscValidPointer(type,2);
3587607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3588264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3589264ace61SBarry Smith   PetscFunctionReturn(0);
3590264ace61SBarry Smith }
3591264ace61SBarry Smith 
359267a56275SMatthew G Knepley /*@C
359367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
359467a56275SMatthew G Knepley 
3595d083f849SBarry Smith   Collective on dm
359667a56275SMatthew G Knepley 
359767a56275SMatthew G Knepley   Input Parameters:
359867a56275SMatthew G Knepley + dm - the DM
359967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
360067a56275SMatthew G Knepley 
360167a56275SMatthew G Knepley   Output Parameter:
360267a56275SMatthew G Knepley . M - pointer to new DM
360367a56275SMatthew G Knepley 
360467a56275SMatthew G Knepley   Notes:
360567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
360667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
360767a56275SMatthew G Knepley   of the input DM.
360867a56275SMatthew G Knepley 
360967a56275SMatthew G Knepley   Level: intermediate
361067a56275SMatthew G Knepley 
361167a56275SMatthew G Knepley .seealso: DMCreate()
361267a56275SMatthew G Knepley @*/
361319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
361467a56275SMatthew G Knepley {
361567a56275SMatthew G Knepley   DM             B;
361667a56275SMatthew G Knepley   char           convname[256];
3617c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
361867a56275SMatthew G Knepley   PetscErrorCode ierr;
361967a56275SMatthew G Knepley 
362067a56275SMatthew G Knepley   PetscFunctionBegin;
362167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
362267a56275SMatthew G Knepley   PetscValidType(dm,1);
362367a56275SMatthew G Knepley   PetscValidPointer(M,3);
3624251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3625c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3626c067b6caSMatthew G. Knepley   if (sametype) {
3627c067b6caSMatthew G. Knepley     *M   = dm;
3628c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3629c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3630c067b6caSMatthew G. Knepley   } else {
36310298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
363267a56275SMatthew G Knepley 
363367a56275SMatthew G Knepley     /*
363467a56275SMatthew G Knepley        Order of precedence:
363567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
363667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
363767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
363867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
363967a56275SMatthew G Knepley        5) Use a really basic converter.
364067a56275SMatthew G Knepley     */
364167a56275SMatthew G Knepley 
364267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3643a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3644a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3645a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3646a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3647a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36480005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
364967a56275SMatthew G Knepley     if (conv) goto foundconv;
365067a56275SMatthew G Knepley 
365167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
365282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
365367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3654a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3655a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3656a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3657a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3658a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36590005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
366067a56275SMatthew G Knepley     if (conv) {
3661fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
366267a56275SMatthew G Knepley       goto foundconv;
366367a56275SMatthew G Knepley     }
366467a56275SMatthew G Knepley 
366567a56275SMatthew G Knepley #if 0
366667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
366767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3668fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
366967a56275SMatthew G Knepley     if (conv) goto foundconv;
367067a56275SMatthew G Knepley 
367167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
367267a56275SMatthew G Knepley     if (dm->ops->convert) {
367367a56275SMatthew G Knepley       conv = dm->ops->convert;
367467a56275SMatthew G Knepley     }
367567a56275SMatthew G Knepley     if (conv) goto foundconv;
367667a56275SMatthew G Knepley #endif
367767a56275SMatthew G Knepley 
367867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
367982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
368067a56275SMatthew G Knepley 
368167a56275SMatthew G Knepley foundconv:
368267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
368367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
368412fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
368590b157c4SStefano Zampini     {
368690b157c4SStefano Zampini       PetscBool             isper;
368712fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
368812fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
368990b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
369090b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
369112fa691eSMatthew G. Knepley     }
369267a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
369367a56275SMatthew G Knepley   }
369467a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
369567a56275SMatthew G Knepley   PetscFunctionReturn(0);
369667a56275SMatthew G Knepley }
3697264ace61SBarry Smith 
3698264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3699264ace61SBarry Smith 
3700264ace61SBarry Smith /*@C
37011c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
37021c84c290SBarry Smith 
37031c84c290SBarry Smith   Not Collective
37041c84c290SBarry Smith 
37051c84c290SBarry Smith   Input Parameters:
37061c84c290SBarry Smith + name        - The name of a new user-defined creation routine
37071c84c290SBarry Smith - create_func - The creation routine itself
37081c84c290SBarry Smith 
37091c84c290SBarry Smith   Notes:
37101c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
37111c84c290SBarry Smith 
37121c84c290SBarry Smith 
37131c84c290SBarry Smith   Sample usage:
37141c84c290SBarry Smith .vb
3715bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
37161c84c290SBarry Smith .ve
37171c84c290SBarry Smith 
37181c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
37191c84c290SBarry Smith .vb
37201c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
37211c84c290SBarry Smith     DMSetType(DM,"my_da");
37221c84c290SBarry Smith .ve
37231c84c290SBarry Smith    or at runtime via the option
37241c84c290SBarry Smith .vb
37251c84c290SBarry Smith     -da_type my_da
37261c84c290SBarry Smith .ve
3727264ace61SBarry Smith 
3728264ace61SBarry Smith   Level: advanced
37291c84c290SBarry Smith 
3730bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
37311c84c290SBarry Smith 
3732264ace61SBarry Smith @*/
3733bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3734264ace61SBarry Smith {
3735264ace61SBarry Smith   PetscErrorCode ierr;
3736264ace61SBarry Smith 
3737264ace61SBarry Smith   PetscFunctionBegin;
37381d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3739a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3740264ace61SBarry Smith   PetscFunctionReturn(0);
3741264ace61SBarry Smith }
3742264ace61SBarry Smith 
3743b859378eSBarry Smith /*@C
374455849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3745b859378eSBarry Smith 
3746d083f849SBarry Smith   Collective on viewer
3747b859378eSBarry Smith 
3748b859378eSBarry Smith   Input Parameters:
3749b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3750b859378eSBarry Smith            some related function before a call to DMLoad().
3751b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3752b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3753b859378eSBarry Smith 
3754b859378eSBarry Smith    Level: intermediate
3755b859378eSBarry Smith 
3756b859378eSBarry Smith   Notes:
375755849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3758b859378eSBarry Smith 
3759b859378eSBarry Smith   Notes for advanced users:
3760b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3761b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3762b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3763b859378eSBarry Smith   format is
3764b859378eSBarry Smith .vb
3765b859378eSBarry Smith      has not yet been determined
3766b859378eSBarry Smith .ve
3767b859378eSBarry Smith 
3768b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3769b859378eSBarry Smith @*/
3770b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3771b859378eSBarry Smith {
37729331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3773b859378eSBarry Smith   PetscErrorCode ierr;
3774b859378eSBarry Smith 
3775b859378eSBarry Smith   PetscFunctionBegin;
3776b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3777b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3778fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
377932c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
37809331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
378158cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
37829331c7a4SMatthew G. Knepley   if (isbinary) {
37839331c7a4SMatthew G. Knepley     PetscInt classid;
37849331c7a4SMatthew G. Knepley     char     type[256];
3785b859378eSBarry Smith 
3786060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
37879200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3788060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
378932c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
37909331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37919331c7a4SMatthew G. Knepley   } else if (ishdf5) {
37929331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37939331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
379458cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
3795b859378eSBarry Smith   PetscFunctionReturn(0);
3796b859378eSBarry Smith }
3797b859378eSBarry Smith 
3798b2e4378dSMatthew G. Knepley /*@
3799b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3800b2e4378dSMatthew G. Knepley 
3801b2e4378dSMatthew G. Knepley   Not collective
3802b2e4378dSMatthew G. Knepley 
3803b2e4378dSMatthew G. Knepley   Input Parameter:
3804b2e4378dSMatthew G. Knepley . dm - the DM
3805b2e4378dSMatthew G. Knepley 
3806b2e4378dSMatthew G. Knepley   Output Parameters:
3807b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3808b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3809b2e4378dSMatthew G. Knepley 
3810b2e4378dSMatthew G. Knepley   Level: beginner
3811b2e4378dSMatthew G. Knepley 
3812b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3813b2e4378dSMatthew G. Knepley 
3814b2e4378dSMatthew G. Knepley 
3815b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3816b2e4378dSMatthew G. Knepley @*/
3817b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3818b2e4378dSMatthew G. Knepley {
3819b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3820b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3821b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3822b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3823b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3824b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3825b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3826b2e4378dSMatthew G. Knepley 
3827b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3828b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3829b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3830b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3831b2e4378dSMatthew G. Knepley   if (coords) {
3832b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3833b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3834b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3835b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3836b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3837b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3838b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3839b2e4378dSMatthew G. Knepley       }
3840b2e4378dSMatthew G. Knepley     }
3841b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3842b2e4378dSMatthew G. Knepley   } else {
3843b2e4378dSMatthew G. Knepley     PetscBool isda;
3844b2e4378dSMatthew G. Knepley 
3845b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3846b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3847b2e4378dSMatthew G. Knepley   }
3848b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3849b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3850b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3851b2e4378dSMatthew G. Knepley }
3852b2e4378dSMatthew G. Knepley 
3853b2e4378dSMatthew G. Knepley /*@
3854b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3855b2e4378dSMatthew G. Knepley 
3856b2e4378dSMatthew G. Knepley   Collective
3857b2e4378dSMatthew G. Knepley 
3858b2e4378dSMatthew G. Knepley   Input Parameter:
3859b2e4378dSMatthew G. Knepley . dm - the DM
3860b2e4378dSMatthew G. Knepley 
3861b2e4378dSMatthew G. Knepley   Output Parameters:
3862b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3863b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3864b2e4378dSMatthew G. Knepley 
3865b2e4378dSMatthew G. Knepley   Level: beginner
3866b2e4378dSMatthew G. Knepley 
3867b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3868b2e4378dSMatthew G. Knepley @*/
3869b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3870b2e4378dSMatthew G. Knepley {
3871b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
38726ce308c4SMatthew G. Knepley   PetscInt       cdim;
38736ce308c4SMatthew G. Knepley   PetscMPIInt    count;
3874b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3875b2e4378dSMatthew G. Knepley 
3876b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3877b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3878b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3879b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3880b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3881b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3882b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3883b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3884b2e4378dSMatthew G. Knepley }
3885b2e4378dSMatthew G. Knepley 
38867da65231SMatthew G Knepley /******************************** FEM Support **********************************/
38877da65231SMatthew G Knepley 
3888a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3889a6dfd86eSKarl Rupp {
38901d47ebbbSSatish Balay   PetscInt       f;
38911b30c384SMatthew G Knepley   PetscErrorCode ierr;
38921b30c384SMatthew G Knepley 
38937da65231SMatthew G Knepley   PetscFunctionBegin;
389474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
38951d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
389657622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
38977da65231SMatthew G Knepley   }
38987da65231SMatthew G Knepley   PetscFunctionReturn(0);
38997da65231SMatthew G Knepley }
39007da65231SMatthew G Knepley 
3901a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3902a6dfd86eSKarl Rupp {
39031b30c384SMatthew G Knepley   PetscInt       f, g;
39047da65231SMatthew G Knepley   PetscErrorCode ierr;
39057da65231SMatthew G Knepley 
39067da65231SMatthew G Knepley   PetscFunctionBegin;
390774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
39081d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
390974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
39101d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3911e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
39127da65231SMatthew G Knepley     }
391374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
39147da65231SMatthew G Knepley   }
39157da65231SMatthew G Knepley   PetscFunctionReturn(0);
39167da65231SMatthew G Knepley }
3917e7c4fc90SDmitry Karpeev 
39186113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3919e759306cSMatthew G. Knepley {
39200c5b8624SToby Isaac   PetscInt          localSize, bs;
39210c5b8624SToby Isaac   PetscMPIInt       size;
39220c5b8624SToby Isaac   Vec               x, xglob;
39230c5b8624SToby Isaac   const PetscScalar *xarray;
3924e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3925e759306cSMatthew G. Knepley 
3926e759306cSMatthew G. Knepley   PetscFunctionBegin;
39279852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3928e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3929e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
39306113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
39310c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
39320c5b8624SToby Isaac   if (size > 1) {
39330c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
39340c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
39350c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
39360c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
39370c5b8624SToby Isaac   } else {
39380c5b8624SToby Isaac     xglob = x;
39390c5b8624SToby Isaac   }
39400c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
39410c5b8624SToby Isaac   if (size > 1) {
39420c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
39430c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
39440c5b8624SToby Isaac   }
3945e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3946e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3947e759306cSMatthew G. Knepley }
3948e759306cSMatthew G. Knepley 
394988ed4aceSMatthew G Knepley /*@
39501bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
3951061576a5SJed Brown 
3952061576a5SJed Brown   Input Parameter:
3953061576a5SJed Brown . dm - The DM
3954061576a5SJed Brown 
3955061576a5SJed Brown   Output Parameter:
3956061576a5SJed Brown . section - The PetscSection
3957061576a5SJed Brown 
3958061576a5SJed Brown   Options Database Keys:
3959061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
3960061576a5SJed Brown 
3961061576a5SJed Brown   Level: advanced
3962061576a5SJed Brown 
3963061576a5SJed Brown   Notes:
3964061576a5SJed Brown   Use DMGetLocalSection() in new code.
3965061576a5SJed Brown 
3966061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
3967061576a5SJed Brown 
3968061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
3969061576a5SJed Brown @*/
3970061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
3971061576a5SJed Brown {
3972061576a5SJed Brown   PetscErrorCode ierr;
3973061576a5SJed Brown 
3974061576a5SJed Brown   PetscFunctionBegin;
3975061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
3976061576a5SJed Brown   PetscFunctionReturn(0);
3977061576a5SJed Brown }
3978061576a5SJed Brown 
3979061576a5SJed Brown /*@
3980061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
398188ed4aceSMatthew G Knepley 
398288ed4aceSMatthew G Knepley   Input Parameter:
398388ed4aceSMatthew G Knepley . dm - The DM
398488ed4aceSMatthew G Knepley 
398588ed4aceSMatthew G Knepley   Output Parameter:
398688ed4aceSMatthew G Knepley . section - The PetscSection
398788ed4aceSMatthew G Knepley 
3988e5893cccSMatthew G. Knepley   Options Database Keys:
3989e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3990e5893cccSMatthew G. Knepley 
399188ed4aceSMatthew G Knepley   Level: intermediate
399288ed4aceSMatthew G Knepley 
399388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
399488ed4aceSMatthew G Knepley 
3995061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
399688ed4aceSMatthew G Knepley @*/
3997061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
39980adebc6cSBarry Smith {
3999fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4000fd59a867SMatthew G. Knepley 
400188ed4aceSMatthew G Knepley   PetscFunctionBegin;
400288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
400388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
40041bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4005e5e52638SMatthew G. Knepley     PetscInt d;
4006e5e52638SMatthew G. Knepley 
4007e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
40081bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
40091bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
40102f0f8703SMatthew G. Knepley   }
40111bb6d2a8SBarry Smith   *section = dm->localSection;
401288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
401388ed4aceSMatthew G Knepley }
401488ed4aceSMatthew G Knepley 
401588ed4aceSMatthew G Knepley /*@
40161bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4017061576a5SJed Brown 
4018061576a5SJed Brown   Input Parameters:
4019061576a5SJed Brown + dm - The DM
4020061576a5SJed Brown - section - The PetscSection
4021061576a5SJed Brown 
4022061576a5SJed Brown   Level: advanced
4023061576a5SJed Brown 
4024061576a5SJed Brown   Notes:
4025061576a5SJed Brown   Use DMSetLocalSection() in new code.
4026061576a5SJed Brown 
4027061576a5SJed Brown   Any existing Section will be destroyed
4028061576a5SJed Brown 
4029061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4030061576a5SJed Brown @*/
4031061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4032061576a5SJed Brown {
4033061576a5SJed Brown   PetscErrorCode ierr;
4034061576a5SJed Brown 
4035061576a5SJed Brown   PetscFunctionBegin;
4036061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4037061576a5SJed Brown   PetscFunctionReturn(0);
4038061576a5SJed Brown }
4039061576a5SJed Brown 
4040061576a5SJed Brown /*@
4041061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
404288ed4aceSMatthew G Knepley 
404388ed4aceSMatthew G Knepley   Input Parameters:
404488ed4aceSMatthew G Knepley + dm - The DM
404588ed4aceSMatthew G Knepley - section - The PetscSection
404688ed4aceSMatthew G Knepley 
404788ed4aceSMatthew G Knepley   Level: intermediate
404888ed4aceSMatthew G Knepley 
404988ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
405088ed4aceSMatthew G Knepley 
4051061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
405288ed4aceSMatthew G Knepley @*/
4053061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
40540adebc6cSBarry Smith {
4055c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4056af122d2aSMatthew G Knepley   PetscInt       f;
405788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
405888ed4aceSMatthew G Knepley 
405988ed4aceSMatthew G Knepley   PetscFunctionBegin;
406088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4061b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
40621d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
40631bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
40641bb6d2a8SBarry Smith   dm->localSection = section;
40651bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4066af122d2aSMatthew G Knepley   if (numFields) {
4067af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4068af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
40690f21e855SMatthew G. Knepley       PetscObject disc;
4070af122d2aSMatthew G Knepley       const char *name;
4071af122d2aSMatthew G Knepley 
40721bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
407344a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
40740f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4075af122d2aSMatthew G Knepley     }
4076af122d2aSMatthew G Knepley   }
4077e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
40781bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
407988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
408088ed4aceSMatthew G Knepley }
408188ed4aceSMatthew G Knepley 
40829435951eSToby Isaac /*@
4083b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
40849435951eSToby Isaac 
4085e228b242SToby Isaac   not collective
4086e228b242SToby Isaac 
40879435951eSToby Isaac   Input Parameter:
40889435951eSToby Isaac . dm - The DM
40899435951eSToby Isaac 
40909435951eSToby Isaac   Output Parameter:
40919435951eSToby 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.
40929435951eSToby 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.
40939435951eSToby Isaac 
40949435951eSToby Isaac   Level: advanced
40959435951eSToby Isaac 
40969435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
40979435951eSToby Isaac 
40989435951eSToby Isaac .seealso: DMSetDefaultConstraints()
40999435951eSToby Isaac @*/
41009435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
41019435951eSToby Isaac {
41029435951eSToby Isaac   PetscErrorCode ierr;
41039435951eSToby Isaac 
41049435951eSToby Isaac   PetscFunctionBegin;
41059435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41069435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
410745a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
410845a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
41099435951eSToby Isaac   PetscFunctionReturn(0);
41109435951eSToby Isaac }
41119435951eSToby Isaac 
41129435951eSToby Isaac /*@
4113b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
41149435951eSToby Isaac 
41159435951eSToby 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().
41169435951eSToby Isaac 
41179435951eSToby 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.
41189435951eSToby Isaac 
4119e228b242SToby Isaac   collective on dm
4120e228b242SToby Isaac 
41219435951eSToby Isaac   Input Parameters:
41229435951eSToby Isaac + dm - The DM
4123e228b242SToby 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).
4124e228b242SToby 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).
41259435951eSToby Isaac 
41269435951eSToby Isaac   Level: advanced
41279435951eSToby Isaac 
41289435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
41299435951eSToby Isaac 
41309435951eSToby Isaac .seealso: DMGetDefaultConstraints()
41319435951eSToby Isaac @*/
41329435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
41339435951eSToby Isaac {
4134e228b242SToby Isaac   PetscMPIInt result;
41359435951eSToby Isaac   PetscErrorCode ierr;
41369435951eSToby Isaac 
41379435951eSToby Isaac   PetscFunctionBegin;
41389435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4139e228b242SToby Isaac   if (section) {
4140e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4141e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4142f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4143e228b242SToby Isaac   }
4144e228b242SToby Isaac   if (mat) {
4145e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4146e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4147f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4148e228b242SToby Isaac   }
41499435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
41509435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
41519435951eSToby Isaac   dm->defaultConstraintSection = section;
41529435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
41539435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
41549435951eSToby Isaac   dm->defaultConstraintMat = mat;
41559435951eSToby Isaac   PetscFunctionReturn(0);
41569435951eSToby Isaac }
41579435951eSToby Isaac 
4158497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4159507e4973SMatthew G. Knepley /*
4160507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4161507e4973SMatthew G. Knepley 
4162507e4973SMatthew G. Knepley   Input Parameters:
4163507e4973SMatthew G. Knepley + dm - The DM
4164507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4165507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4166507e4973SMatthew G. Knepley 
4167507e4973SMatthew G. Knepley   Level: intermediate
4168507e4973SMatthew G. Knepley 
41691bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4170507e4973SMatthew G. Knepley */
4171f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4172507e4973SMatthew G. Knepley {
4173507e4973SMatthew G. Knepley   MPI_Comm        comm;
4174507e4973SMatthew G. Knepley   PetscLayout     layout;
4175507e4973SMatthew G. Knepley   const PetscInt *ranges;
4176507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4177507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4178507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4179507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4180507e4973SMatthew G. Knepley 
4181507e4973SMatthew G. Knepley   PetscFunctionBegin;
4182507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4183507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4184507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4185507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4186507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4187507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4188507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4189507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4190507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4191507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4192507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4193507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4194f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4195507e4973SMatthew G. Knepley 
4196507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4197507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4198507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4199507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4200507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4201507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4202507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4203507e4973SMatthew 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;}
4204507e4973SMatthew 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;}
4205507e4973SMatthew G. Knepley     if (gdof < 0) {
4206507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4207507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4208507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4209507e4973SMatthew G. Knepley 
4210507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4211507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4212507e4973SMatthew 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;}
4213507e4973SMatthew G. Knepley       }
4214507e4973SMatthew G. Knepley     }
4215507e4973SMatthew G. Knepley   }
4216507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4217507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4218b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4219507e4973SMatthew G. Knepley   if (!gvalid) {
4220507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4221507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4222507e4973SMatthew G. Knepley   }
4223507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4224507e4973SMatthew G. Knepley }
4225f741bcd2SMatthew G. Knepley #endif
4226507e4973SMatthew G. Knepley 
422788ed4aceSMatthew G Knepley /*@
4228e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
422988ed4aceSMatthew G Knepley 
4230d083f849SBarry Smith   Collective on dm
42318b1ab98fSJed Brown 
423288ed4aceSMatthew G Knepley   Input Parameter:
423388ed4aceSMatthew G Knepley . dm - The DM
423488ed4aceSMatthew G Knepley 
423588ed4aceSMatthew G Knepley   Output Parameter:
423688ed4aceSMatthew G Knepley . section - The PetscSection
423788ed4aceSMatthew G Knepley 
423888ed4aceSMatthew G Knepley   Level: intermediate
423988ed4aceSMatthew G Knepley 
424088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
424188ed4aceSMatthew G Knepley 
424292fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
424388ed4aceSMatthew G Knepley @*/
4244e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
42450adebc6cSBarry Smith {
424688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
424788ed4aceSMatthew G Knepley 
424888ed4aceSMatthew G Knepley   PetscFunctionBegin;
424988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
425088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42511bb6d2a8SBarry Smith   if (!dm->globalSection) {
4252fd59a867SMatthew G. Knepley     PetscSection s;
4253fd59a867SMatthew G. Knepley 
425492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4255fd59a867SMatthew 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");
425633907cc2SStefano Zampini     if (!dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
42571bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4258cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
42591bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
42601bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
426188ed4aceSMatthew G Knepley   }
42621bb6d2a8SBarry Smith   *section = dm->globalSection;
426388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
426488ed4aceSMatthew G Knepley }
426588ed4aceSMatthew G Knepley 
4266b21d0597SMatthew G Knepley /*@
4267e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4268b21d0597SMatthew G Knepley 
4269b21d0597SMatthew G Knepley   Input Parameters:
4270b21d0597SMatthew G Knepley + dm - The DM
42715080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4272b21d0597SMatthew G Knepley 
4273b21d0597SMatthew G Knepley   Level: intermediate
4274b21d0597SMatthew G Knepley 
4275b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4276b21d0597SMatthew G Knepley 
427792fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4278b21d0597SMatthew G Knepley @*/
4279e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
42800adebc6cSBarry Smith {
4281b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4282b21d0597SMatthew G Knepley 
4283b21d0597SMatthew G Knepley   PetscFunctionBegin;
4284b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42855080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42861d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42871bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
42881bb6d2a8SBarry Smith   dm->globalSection = section;
4289497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
42901bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4291507e4973SMatthew G. Knepley #endif
4292b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4293b21d0597SMatthew G Knepley }
4294b21d0597SMatthew G Knepley 
429588ed4aceSMatthew G Knepley /*@
42961bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
429788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
429888ed4aceSMatthew G Knepley 
429988ed4aceSMatthew G Knepley   Input Parameter:
430088ed4aceSMatthew G Knepley . dm - The DM
430188ed4aceSMatthew G Knepley 
430288ed4aceSMatthew G Knepley   Output Parameter:
430388ed4aceSMatthew G Knepley . sf - The PetscSF
430488ed4aceSMatthew G Knepley 
430588ed4aceSMatthew G Knepley   Level: intermediate
430688ed4aceSMatthew G Knepley 
430788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
430888ed4aceSMatthew G Knepley 
43091bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
431088ed4aceSMatthew G Knepley @*/
43111bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
43120adebc6cSBarry Smith {
431388ed4aceSMatthew G Knepley   PetscInt       nroots;
431488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
431588ed4aceSMatthew G Knepley 
431688ed4aceSMatthew G Knepley   PetscFunctionBegin;
431788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
431888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
43191bb6d2a8SBarry Smith   if (!dm->sectionSF) {
43201bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
432133907cc2SStefano Zampini   }
43221bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
432388ed4aceSMatthew G Knepley   if (nroots < 0) {
432488ed4aceSMatthew G Knepley     PetscSection section, gSection;
432588ed4aceSMatthew G Knepley 
432692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
432731ea6d37SMatthew G Knepley     if (section) {
4328e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
43291bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
433031ea6d37SMatthew G Knepley     } else {
43310298fd71SBarry Smith       *sf = NULL;
433231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
433331ea6d37SMatthew G Knepley     }
433488ed4aceSMatthew G Knepley   }
43351bb6d2a8SBarry Smith   *sf = dm->sectionSF;
433688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
433788ed4aceSMatthew G Knepley }
433888ed4aceSMatthew G Knepley 
433988ed4aceSMatthew G Knepley /*@
43401bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
434188ed4aceSMatthew G Knepley 
434288ed4aceSMatthew G Knepley   Input Parameters:
434388ed4aceSMatthew G Knepley + dm - The DM
434488ed4aceSMatthew G Knepley - sf - The PetscSF
434588ed4aceSMatthew G Knepley 
434688ed4aceSMatthew G Knepley   Level: intermediate
434788ed4aceSMatthew G Knepley 
434888ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
434988ed4aceSMatthew G Knepley 
43501bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
435188ed4aceSMatthew G Knepley @*/
43521bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
43530adebc6cSBarry Smith {
435488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
435588ed4aceSMatthew G Knepley 
435688ed4aceSMatthew G Knepley   PetscFunctionBegin;
435788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4358b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
435933907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
43601bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
43611bb6d2a8SBarry Smith   dm->sectionSF = sf;
436288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
436388ed4aceSMatthew G Knepley }
436488ed4aceSMatthew G Knepley 
436588ed4aceSMatthew G Knepley /*@C
43661bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
436788ed4aceSMatthew G Knepley   describing the data layout.
436888ed4aceSMatthew G Knepley 
436988ed4aceSMatthew G Knepley   Input Parameters:
437088ed4aceSMatthew G Knepley + dm - The DM
437188ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
437288ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
437388ed4aceSMatthew G Knepley 
43741bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
437588ed4aceSMatthew G Knepley 
43761bb6d2a8SBarry Smith   Level: developer
43771bb6d2a8SBarry Smith 
43781bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
43791bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
43801bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
43811bb6d2a8SBarry Smith 
43821bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
438388ed4aceSMatthew G Knepley @*/
43841bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
438588ed4aceSMatthew G Knepley {
438682f516ccSBarry Smith   MPI_Comm       comm;
438788ed4aceSMatthew G Knepley   PetscLayout    layout;
438888ed4aceSMatthew G Knepley   const PetscInt *ranges;
438988ed4aceSMatthew G Knepley   PetscInt       *local;
439088ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4391ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
439288ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
439388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
439488ed4aceSMatthew G Knepley 
439588ed4aceSMatthew G Knepley   PetscFunctionBegin;
439688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4397367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
439888ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
439988ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
440088ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
440188ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
440288ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
440388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
440488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
440588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
440688ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4407ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
44086636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
440988ed4aceSMatthew G Knepley 
44106636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
44116636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4412235fbf56SMatthew 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));
44136636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
441488ed4aceSMatthew G Knepley   }
4415785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4416785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
441788ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
44181f588964SMatthew G Knepley     const PetscInt *cind;
44196636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
442088ed4aceSMatthew G Knepley 
442188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
442288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
442388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
442488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
442588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
44266636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
442788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
44286636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
44296636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
44306636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4431057b4bcdSMatthew 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);
44326636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
44336636e97aSMatthew G Knepley     }
443488ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
443588ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
443688ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
443788ed4aceSMatthew G Knepley     }
443888ed4aceSMatthew G Knepley     if (gdof < 0) {
44396636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
444088ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
444188ed4aceSMatthew G Knepley 
444205376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
444331d3f06eSJed Brown         if (r < 0) r = -(r+2);
444405376888SMatthew 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);
444588ed4aceSMatthew G Knepley         remote[l].rank  = r;
444688ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
444788ed4aceSMatthew G Knepley       }
444888ed4aceSMatthew G Knepley     } else {
44496636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
445088ed4aceSMatthew G Knepley         remote[l].rank  = rank;
445188ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
445288ed4aceSMatthew G Knepley       }
445388ed4aceSMatthew G Knepley     }
445488ed4aceSMatthew G Knepley   }
44556636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
445688ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
44571bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
445888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
445988ed4aceSMatthew G Knepley }
4460af122d2aSMatthew G Knepley 
4461b21d0597SMatthew G Knepley /*@
4462b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4463b21d0597SMatthew G Knepley 
4464b21d0597SMatthew G Knepley   Input Parameter:
4465b21d0597SMatthew G Knepley . dm - The DM
4466b21d0597SMatthew G Knepley 
4467b21d0597SMatthew G Knepley   Output Parameter:
4468b21d0597SMatthew G Knepley . sf - The PetscSF
4469b21d0597SMatthew G Knepley 
4470b21d0597SMatthew G Knepley   Level: intermediate
4471b21d0597SMatthew G Knepley 
4472b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4473b21d0597SMatthew G Knepley 
44741bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4475b21d0597SMatthew G Knepley @*/
44760adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
44770adebc6cSBarry Smith {
4478b21d0597SMatthew G Knepley   PetscFunctionBegin;
4479b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4480b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4481b21d0597SMatthew G Knepley   *sf = dm->sf;
4482b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4483b21d0597SMatthew G Knepley }
4484b21d0597SMatthew G Knepley 
4485057b4bcdSMatthew G Knepley /*@
4486057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4487057b4bcdSMatthew G Knepley 
4488057b4bcdSMatthew G Knepley   Input Parameters:
4489057b4bcdSMatthew G Knepley + dm - The DM
4490057b4bcdSMatthew G Knepley - sf - The PetscSF
4491057b4bcdSMatthew G Knepley 
4492057b4bcdSMatthew G Knepley   Level: intermediate
4493057b4bcdSMatthew G Knepley 
44941bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4495057b4bcdSMatthew G Knepley @*/
44960adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
44970adebc6cSBarry Smith {
4498057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4499057b4bcdSMatthew G Knepley 
4500057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4501057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4502b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4503057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
450433907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4505057b4bcdSMatthew G Knepley   dm->sf = sf;
4506057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4507057b4bcdSMatthew G Knepley }
4508057b4bcdSMatthew G Knepley 
450934aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
451034aa8a36SMatthew G. Knepley {
451134aa8a36SMatthew G. Knepley   PetscClassId   id;
451234aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
451334aa8a36SMatthew G. Knepley 
451434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
451534aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
451634aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
451734aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
451834aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
451934aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
452017c1d62eSMatthew G. Knepley   } else {
452117c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
452234aa8a36SMatthew G. Knepley   }
452334aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
452434aa8a36SMatthew G. Knepley }
452534aa8a36SMatthew G. Knepley 
452644a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
452744a7f3ddSMatthew G. Knepley {
452844a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
452944a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
453044a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
453144a7f3ddSMatthew G. Knepley 
453244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
453344a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
453444a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
453544a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
453644a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
453744a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
453844a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
453944a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
454044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
454144a7f3ddSMatthew G. Knepley }
454244a7f3ddSMatthew G. Knepley 
454344a7f3ddSMatthew G. Knepley /*@
454444a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
454544a7f3ddSMatthew G. Knepley 
4546d083f849SBarry Smith   Logically collective on dm
454744a7f3ddSMatthew G. Knepley 
454844a7f3ddSMatthew G. Knepley   Input Parameter:
454944a7f3ddSMatthew G. Knepley . dm - The DM
455044a7f3ddSMatthew G. Knepley 
455144a7f3ddSMatthew G. Knepley   Level: intermediate
455244a7f3ddSMatthew G. Knepley 
455344a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
455444a7f3ddSMatthew G. Knepley @*/
455544a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
455644a7f3ddSMatthew G. Knepley {
455744a7f3ddSMatthew G. Knepley   PetscInt       f;
455844a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
455944a7f3ddSMatthew G. Knepley 
456044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
456144a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
456244a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
456344a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
456444a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
456544a7f3ddSMatthew G. Knepley   }
456644a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
456744a7f3ddSMatthew G. Knepley   dm->fields = NULL;
456844a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
456944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
457044a7f3ddSMatthew G. Knepley }
457144a7f3ddSMatthew G. Knepley 
4572689b5837SMatthew G. Knepley /*@
4573689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4574689b5837SMatthew G. Knepley 
4575689b5837SMatthew G. Knepley   Not collective
4576689b5837SMatthew G. Knepley 
4577689b5837SMatthew G. Knepley   Input Parameter:
4578689b5837SMatthew G. Knepley . dm - The DM
4579689b5837SMatthew G. Knepley 
4580689b5837SMatthew G. Knepley   Output Parameter:
4581689b5837SMatthew G. Knepley . Nf - The number of fields
4582689b5837SMatthew G. Knepley 
4583689b5837SMatthew G. Knepley   Level: intermediate
4584689b5837SMatthew G. Knepley 
4585689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4586689b5837SMatthew G. Knepley @*/
45870f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
45880f21e855SMatthew G. Knepley {
45890f21e855SMatthew G. Knepley   PetscFunctionBegin;
45900f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4591534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
459244a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4593af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4594af122d2aSMatthew G Knepley }
4595af122d2aSMatthew G Knepley 
4596689b5837SMatthew G. Knepley /*@
4597689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4598689b5837SMatthew G. Knepley 
4599d083f849SBarry Smith   Logically collective on dm
4600689b5837SMatthew G. Knepley 
4601689b5837SMatthew G. Knepley   Input Parameters:
4602689b5837SMatthew G. Knepley + dm - The DM
4603689b5837SMatthew G. Knepley - Nf - The number of fields
4604689b5837SMatthew G. Knepley 
4605689b5837SMatthew G. Knepley   Level: intermediate
4606689b5837SMatthew G. Knepley 
4607689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4608689b5837SMatthew G. Knepley @*/
4609af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4610af122d2aSMatthew G Knepley {
46110f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4612af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4613af122d2aSMatthew G Knepley 
4614af122d2aSMatthew G Knepley   PetscFunctionBegin;
4615af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
461644a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
46170f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
46180f21e855SMatthew G. Knepley     PetscContainer obj;
46190f21e855SMatthew G. Knepley 
46200f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
462144a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
46220f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4623af122d2aSMatthew G Knepley   }
4624af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4625af122d2aSMatthew G Knepley }
4626af122d2aSMatthew G Knepley 
4627c1929be8SMatthew G. Knepley /*@
4628c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4629c1929be8SMatthew G. Knepley 
4630c1929be8SMatthew G. Knepley   Not collective
4631c1929be8SMatthew G. Knepley 
4632c1929be8SMatthew G. Knepley   Input Parameters:
4633c1929be8SMatthew G. Knepley + dm - The DM
4634c1929be8SMatthew G. Knepley - f  - The field number
4635c1929be8SMatthew G. Knepley 
463644a7f3ddSMatthew G. Knepley   Output Parameters:
463744a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
463844a7f3ddSMatthew G. Knepley - field - The discretization object
4639c1929be8SMatthew G. Knepley 
464044a7f3ddSMatthew G. Knepley   Level: intermediate
4641c1929be8SMatthew G. Knepley 
464244a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4643c1929be8SMatthew G. Knepley @*/
464444a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4645af122d2aSMatthew G Knepley {
4646af122d2aSMatthew G Knepley   PetscFunctionBegin;
4647af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
464844a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
464944a7f3ddSMatthew G. Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, dm->Nf);
465044a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
465144a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4652decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4653decb47aaSMatthew G. Knepley }
4654decb47aaSMatthew G. Knepley 
4655083401c6SMatthew G. Knepley /* Does not clear the DS */
4656083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4657083401c6SMatthew G. Knepley {
4658083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4659083401c6SMatthew G. Knepley 
4660083401c6SMatthew G. Knepley   PetscFunctionBegin;
4661083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4662083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4663083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4664083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4665083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4666083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4667083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4668083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4669083401c6SMatthew G. Knepley }
4670083401c6SMatthew G. Knepley 
4671c1929be8SMatthew G. Knepley /*@
4672c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4673c1929be8SMatthew G. Knepley 
4674d083f849SBarry Smith   Logically collective on dm
4675c1929be8SMatthew G. Knepley 
4676c1929be8SMatthew G. Knepley   Input Parameters:
4677c1929be8SMatthew G. Knepley + dm    - The DM
4678c1929be8SMatthew G. Knepley . f     - The field number
467944a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4680c1929be8SMatthew G. Knepley - field - The discretization object
4681c1929be8SMatthew G. Knepley 
468244a7f3ddSMatthew G. Knepley   Level: intermediate
4683c1929be8SMatthew G. Knepley 
468444a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4685c1929be8SMatthew G. Knepley @*/
468644a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4687decb47aaSMatthew G. Knepley {
4688decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4689decb47aaSMatthew G. Knepley 
4690decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4691decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4692e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
469344a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4694e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4695083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
469634aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
46972df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
469844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
469944a7f3ddSMatthew G. Knepley }
470044a7f3ddSMatthew G. Knepley 
470144a7f3ddSMatthew G. Knepley /*@
470244a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
470344a7f3ddSMatthew G. Knepley 
4704d083f849SBarry Smith   Logically collective on dm
470544a7f3ddSMatthew G. Knepley 
470644a7f3ddSMatthew G. Knepley   Input Parameters:
470744a7f3ddSMatthew G. Knepley + dm    - The DM
470844a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
470944a7f3ddSMatthew G. Knepley - field - The discretization object
471044a7f3ddSMatthew G. Knepley 
471144a7f3ddSMatthew G. Knepley   Level: intermediate
471244a7f3ddSMatthew G. Knepley 
471344a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
471444a7f3ddSMatthew G. Knepley @*/
471544a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
471644a7f3ddSMatthew G. Knepley {
471744a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
471844a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
471944a7f3ddSMatthew G. Knepley 
472044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
472144a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
472244a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
472344a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
472444a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
472544a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
472644a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
472744a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
472844a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
472934aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
47302df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4731af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4732af122d2aSMatthew G Knepley }
47336636e97aSMatthew G Knepley 
4734e5e52638SMatthew G. Knepley /*@
4735e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4736e5e52638SMatthew G. Knepley 
4737d083f849SBarry Smith   Collective on dm
4738e5e52638SMatthew G. Knepley 
4739e5e52638SMatthew G. Knepley   Input Parameter:
4740e5e52638SMatthew G. Knepley . dm - The DM
4741e5e52638SMatthew G. Knepley 
4742e5e52638SMatthew G. Knepley   Output Parameter:
4743e5e52638SMatthew G. Knepley . newdm - The DM
4744e5e52638SMatthew G. Knepley 
4745e5e52638SMatthew G. Knepley   Level: advanced
4746e5e52638SMatthew G. Knepley 
4747e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4748e5e52638SMatthew G. Knepley @*/
4749e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4750e5e52638SMatthew G. Knepley {
4751e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4752e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4753e5e52638SMatthew G. Knepley 
4754e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4755e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4756e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4757e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4758e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4759e5e52638SMatthew G. Knepley     DMLabel     label;
4760e5e52638SMatthew G. Knepley     PetscObject field;
476134aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4762e5e52638SMatthew G. Knepley 
4763e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4764e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
476534aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
476634aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
476734aa8a36SMatthew G. Knepley   }
476834aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
476934aa8a36SMatthew G. Knepley }
477034aa8a36SMatthew G. Knepley 
477134aa8a36SMatthew G. Knepley /*@
477234aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
477334aa8a36SMatthew G. Knepley 
477434aa8a36SMatthew G. Knepley   Not collective
477534aa8a36SMatthew G. Knepley 
477634aa8a36SMatthew G. Knepley   Input Parameters:
477734aa8a36SMatthew G. Knepley + dm - The DM object
477834aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
477934aa8a36SMatthew G. Knepley 
478034aa8a36SMatthew G. Knepley   Output Parameter:
478134aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
478234aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
478334aa8a36SMatthew G. Knepley 
478434aa8a36SMatthew G. Knepley   Notes:
478534aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
478634aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
478734aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4788979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
478934aa8a36SMatthew G. Knepley 
479034aa8a36SMatthew G. Knepley   Level: developer
479134aa8a36SMatthew G. Knepley 
479234aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
479334aa8a36SMatthew G. Knepley @*/
479434aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
479534aa8a36SMatthew G. Knepley {
479634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
479734aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4798534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4799534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
480034aa8a36SMatthew G. Knepley   if (f < 0) {
480134aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
480234aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
480334aa8a36SMatthew G. Knepley   } else {
480434aa8a36SMatthew G. Knepley     PetscInt       Nf;
480534aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
480634aa8a36SMatthew G. Knepley 
480734aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
480834aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
480934aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
481034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
481134aa8a36SMatthew G. Knepley   }
481234aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
481334aa8a36SMatthew G. Knepley }
481434aa8a36SMatthew G. Knepley 
481534aa8a36SMatthew G. Knepley /*@
481634aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
481734aa8a36SMatthew G. Knepley 
481834aa8a36SMatthew G. Knepley   Not collective
481934aa8a36SMatthew G. Knepley 
482034aa8a36SMatthew G. Knepley   Input Parameters:
482134aa8a36SMatthew G. Knepley + dm         - The DM object
482234aa8a36SMatthew G. Knepley . f          - The field number
482334aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
482434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
482534aa8a36SMatthew G. Knepley 
482634aa8a36SMatthew G. Knepley   Notes:
482734aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
482834aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
482934aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4830979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
483134aa8a36SMatthew G. Knepley 
483234aa8a36SMatthew G. Knepley   Level: developer
483334aa8a36SMatthew G. Knepley 
483434aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
483534aa8a36SMatthew G. Knepley @*/
483634aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
483734aa8a36SMatthew G. Knepley {
483834aa8a36SMatthew G. Knepley   PetscFunctionBegin;
483934aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
484034aa8a36SMatthew G. Knepley   if (f < 0) {
484134aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
484234aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
484334aa8a36SMatthew G. Knepley   } else {
484434aa8a36SMatthew G. Knepley     PetscInt       Nf;
484534aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
484634aa8a36SMatthew G. Knepley 
484734aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
484834aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
484934aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
485034aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4851e5e52638SMatthew G. Knepley   }
4852e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4853e5e52638SMatthew G. Knepley }
4854e5e52638SMatthew G. Knepley 
4855b0441da4SMatthew G. Knepley /*@
4856b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4857b0441da4SMatthew G. Knepley 
4858b0441da4SMatthew G. Knepley   Not collective
4859b0441da4SMatthew G. Knepley 
4860b0441da4SMatthew G. Knepley   Input Parameters:
4861b0441da4SMatthew G. Knepley . dm - The DM object
4862b0441da4SMatthew G. Knepley 
4863b0441da4SMatthew G. Knepley   Output Parameter:
4864b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4865b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4866b0441da4SMatthew G. Knepley 
4867b0441da4SMatthew G. Knepley   Notes:
4868b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4869b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4870b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4871b0441da4SMatthew G. Knepley 
4872b0441da4SMatthew G. Knepley   Level: developer
4873b0441da4SMatthew G. Knepley 
4874b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4875b0441da4SMatthew G. Knepley @*/
4876b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4877b0441da4SMatthew G. Knepley {
4878b0441da4SMatthew G. Knepley   PetscInt       Nf;
4879b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4880b0441da4SMatthew G. Knepley 
4881b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4882b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4883534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4884534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4885b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4886b0441da4SMatthew G. Knepley   if (!Nf) {
4887b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4888b0441da4SMatthew G. Knepley   } else {
4889b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4890b0441da4SMatthew G. Knepley   }
4891b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4892b0441da4SMatthew G. Knepley }
4893b0441da4SMatthew G. Knepley 
4894b0441da4SMatthew G. Knepley /*@
4895b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4896b0441da4SMatthew G. Knepley 
4897b0441da4SMatthew G. Knepley   Not collective
4898b0441da4SMatthew G. Knepley 
4899b0441da4SMatthew G. Knepley   Input Parameters:
4900b0441da4SMatthew G. Knepley + dm         - The DM object
4901b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4902b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4903b0441da4SMatthew G. Knepley 
4904b0441da4SMatthew G. Knepley   Notes:
4905b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4906b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4907b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4908b0441da4SMatthew G. Knepley 
4909b0441da4SMatthew G. Knepley   Level: developer
4910b0441da4SMatthew G. Knepley 
4911b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4912b0441da4SMatthew G. Knepley @*/
4913b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4914b0441da4SMatthew G. Knepley {
4915b0441da4SMatthew G. Knepley   PetscInt       Nf;
4916b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4917b0441da4SMatthew G. Knepley 
4918b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4919b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4920b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4921b0441da4SMatthew G. Knepley   if (!Nf) {
4922b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4923b0441da4SMatthew G. Knepley   } else {
4924b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4925e5e52638SMatthew G. Knepley   }
4926e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4927e5e52638SMatthew G. Knepley }
4928e5e52638SMatthew G. Knepley 
4929783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
4930783e2ec8SMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, const char labelname[])
4931783e2ec8SMatthew G. Knepley {
4932783e2ec8SMatthew G. Knepley   DMLabel        label;
4933783e2ec8SMatthew G. Knepley   PetscObject    obj;
4934783e2ec8SMatthew G. Knepley   PetscClassId   id;
4935783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
4936783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
4937783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
4938783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
4939783e2ec8SMatthew G. Knepley 
4940783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
4941783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
4942783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
4943783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
4944783e2ec8SMatthew G. Knepley   ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
4945783e2ec8SMatthew G. Knepley   if (isFE && label) {
4946783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
4947783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
4948783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
4949783e2ec8SMatthew G. Knepley       const char *lname;
4950783e2ec8SMatthew G. Knepley 
4951783e2ec8SMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, &lname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
4952783e2ec8SMatthew G. Knepley       ierr = PetscStrcmp(lname, labelname, &duplicate);CHKERRQ(ierr);
4953783e2ec8SMatthew G. Knepley       if (duplicate) break;
4954783e2ec8SMatthew G. Knepley     }
4955783e2ec8SMatthew G. Knepley     if (!duplicate) {
4956783e2ec8SMatthew G. Knepley       DM plex;
4957783e2ec8SMatthew G. Knepley 
4958783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
4959783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
4960783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
4961783e2ec8SMatthew G. Knepley     }
4962783e2ec8SMatthew G. Knepley   }
4963783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
4964783e2ec8SMatthew G. Knepley }
4965783e2ec8SMatthew G. Knepley 
4966e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4967e5e52638SMatthew G. Knepley {
4968e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4969e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4970e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4971e5e52638SMatthew G. Knepley 
4972e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4973e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4974e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4975e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4976b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4977e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4978e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4979e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4980e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4981e5e52638SMatthew G. Knepley }
4982e5e52638SMatthew G. Knepley 
4983e5e52638SMatthew G. Knepley /*@
4984e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4985e5e52638SMatthew G. Knepley 
4986e5e52638SMatthew G. Knepley   Not collective
4987e5e52638SMatthew G. Knepley 
4988e5e52638SMatthew G. Knepley   Input Parameter:
4989e5e52638SMatthew G. Knepley . dm - The DM
4990e5e52638SMatthew G. Knepley 
4991e5e52638SMatthew G. Knepley   Output Parameter:
4992e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
4993e5e52638SMatthew G. Knepley 
4994e5e52638SMatthew G. Knepley   Level: intermediate
4995e5e52638SMatthew G. Knepley 
4996e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
4997e5e52638SMatthew G. Knepley @*/
4998e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4999e5e52638SMatthew G. Knepley {
5000e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5001e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5002534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5003e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5004e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5005e5e52638SMatthew G. Knepley }
5006e5e52638SMatthew G. Knepley 
5007e5e52638SMatthew G. Knepley /*@
5008e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5009e5e52638SMatthew G. Knepley 
5010d083f849SBarry Smith   Logically collective on dm
5011e5e52638SMatthew G. Knepley 
5012e5e52638SMatthew G. Knepley   Input Parameter:
5013e5e52638SMatthew G. Knepley . dm - The DM
5014e5e52638SMatthew G. Knepley 
5015e5e52638SMatthew G. Knepley   Level: intermediate
5016e5e52638SMatthew G. Knepley 
5017e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5018e5e52638SMatthew G. Knepley @*/
5019e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5020e5e52638SMatthew G. Knepley {
5021e5e52638SMatthew G. Knepley   PetscInt       s;
5022e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5023e5e52638SMatthew G. Knepley 
5024e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5025e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5026e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5027e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5028e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5029b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5030e5e52638SMatthew G. Knepley   }
5031e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5032e5e52638SMatthew G. Knepley   dm->probs = NULL;
5033e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5034e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5035e5e52638SMatthew G. Knepley }
5036e5e52638SMatthew G. Knepley 
5037e5e52638SMatthew G. Knepley /*@
5038e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5039e5e52638SMatthew G. Knepley 
5040e5e52638SMatthew G. Knepley   Not collective
5041e5e52638SMatthew G. Knepley 
5042e5e52638SMatthew G. Knepley   Input Parameter:
5043e5e52638SMatthew G. Knepley . dm    - The DM
5044e5e52638SMatthew G. Knepley 
5045e5e52638SMatthew G. Knepley   Output Parameter:
5046e5e52638SMatthew G. Knepley . prob - The default PetscDS
5047e5e52638SMatthew G. Knepley 
5048e5e52638SMatthew G. Knepley   Level: intermediate
5049e5e52638SMatthew G. Knepley 
5050e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5051e5e52638SMatthew G. Knepley @*/
5052e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5053e5e52638SMatthew G. Knepley {
5054b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5055b0143b4dSMatthew G. Knepley 
5056e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5057e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5058e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5059b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5060b0143b4dSMatthew G. Knepley     PetscDS ds;
5061b0143b4dSMatthew G. Knepley 
5062b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
5063b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5064b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5065b0143b4dSMatthew G. Knepley   }
5066b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5067e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5068e5e52638SMatthew G. Knepley }
5069e5e52638SMatthew G. Knepley 
5070e5e52638SMatthew G. Knepley /*@
5071e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5072e5e52638SMatthew G. Knepley 
5073e5e52638SMatthew G. Knepley   Not collective
5074e5e52638SMatthew G. Knepley 
5075e5e52638SMatthew G. Knepley   Input Parameters:
5076e5e52638SMatthew G. Knepley + dm    - The DM
5077e5e52638SMatthew G. Knepley - point - Cell for the DS
5078e5e52638SMatthew G. Knepley 
5079e5e52638SMatthew G. Knepley   Output Parameter:
5080e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5081e5e52638SMatthew G. Knepley 
5082e5e52638SMatthew G. Knepley   Level: developer
5083e5e52638SMatthew G. Knepley 
5084b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5085e5e52638SMatthew G. Knepley @*/
5086e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5087e5e52638SMatthew G. Knepley {
5088e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5089e5e52638SMatthew G. Knepley   PetscInt       s;
5090e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5091e5e52638SMatthew G. Knepley 
5092e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5093e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5094e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5095360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5096e5e52638SMatthew G. Knepley   *prob = NULL;
5097e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5098e5e52638SMatthew G. Knepley     PetscInt val;
5099e5e52638SMatthew G. Knepley 
5100e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5101e5e52638SMatthew G. Knepley     else {
5102e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5103e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5104e5e52638SMatthew G. Knepley     }
5105e5e52638SMatthew G. Knepley   }
5106e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5107e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5108e5e52638SMatthew G. Knepley }
5109e5e52638SMatthew G. Knepley 
5110e5e52638SMatthew G. Knepley /*@
5111e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5112e5e52638SMatthew G. Knepley 
5113e5e52638SMatthew G. Knepley   Not collective
5114e5e52638SMatthew G. Knepley 
5115e5e52638SMatthew G. Knepley   Input Parameters:
5116e5e52638SMatthew G. Knepley + dm    - The DM
5117e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5118e5e52638SMatthew G. Knepley 
5119b3cf3223SMatthew G. Knepley   Output Parameters:
5120b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5121b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5122e5e52638SMatthew G. Knepley 
5123e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5124e5e52638SMatthew G. Knepley 
5125e5e52638SMatthew G. Knepley   Level: advanced
5126e5e52638SMatthew G. Knepley 
5127e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5128e5e52638SMatthew G. Knepley @*/
5129b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5130e5e52638SMatthew G. Knepley {
5131e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5132e5e52638SMatthew G. Knepley 
5133e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5134e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5135e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5136b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5137b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5138e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5139b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5140b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5141b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5142b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5143b3cf3223SMatthew G. Knepley     }
5144e5e52638SMatthew G. Knepley   }
51452df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5146e5e52638SMatthew G. Knepley }
5147e5e52638SMatthew G. Knepley 
5148e5e52638SMatthew G. Knepley /*@
5149083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5150083401c6SMatthew G. Knepley 
5151083401c6SMatthew G. Knepley   Collective on dm
5152083401c6SMatthew G. Knepley 
5153083401c6SMatthew G. Knepley   Input Parameters:
5154083401c6SMatthew G. Knepley + dm     - The DM
5155083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5156083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5157083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5158083401c6SMatthew G. Knepley 
5159083401c6SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM. If DS is replaced,
5160083401c6SMatthew G. Knepley   the fields argument is ignored.
5161083401c6SMatthew G. Knepley 
5162083401c6SMatthew G. Knepley   Level: advanced
5163083401c6SMatthew G. Knepley 
5164083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5165083401c6SMatthew G. Knepley @*/
5166083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5167083401c6SMatthew G. Knepley {
5168083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5169083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5170083401c6SMatthew G. Knepley 
5171083401c6SMatthew G. Knepley   PetscFunctionBegin;
5172083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5173083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5174083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5175083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5176083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5177083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5178083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5179083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5180083401c6SMatthew G. Knepley     }
5181083401c6SMatthew G. Knepley   }
5182083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5183083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5184083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5185083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5186083401c6SMatthew G. Knepley   if (!label) {
5187083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5188083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5189083401c6SMatthew G. Knepley     Nds = 0;
5190083401c6SMatthew G. Knepley   }
5191083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5192083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5193083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5194083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5195083401c6SMatthew G. Knepley }
5196083401c6SMatthew G. Knepley 
5197083401c6SMatthew G. Knepley /*@
5198e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5199e5e52638SMatthew G. Knepley 
5200e5e52638SMatthew G. Knepley   Not collective
5201e5e52638SMatthew G. Knepley 
5202e5e52638SMatthew G. Knepley   Input Parameters:
5203e5e52638SMatthew G. Knepley + dm  - The DM
5204e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5205e5e52638SMatthew G. Knepley 
5206e5e52638SMatthew G. Knepley   Output Parameters:
5207b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5208b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5209083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5210e5e52638SMatthew G. Knepley 
5211e5e52638SMatthew G. Knepley   Level: advanced
5212e5e52638SMatthew G. Knepley 
5213e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5214e5e52638SMatthew G. Knepley @*/
5215b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5216e5e52638SMatthew G. Knepley {
5217e5e52638SMatthew G. Knepley   PetscInt       Nds;
5218e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5219e5e52638SMatthew G. Knepley 
5220e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5221e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5222e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5223e5e52638SMatthew G. Knepley   if ((num < 0) || (num >= Nds)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
5224e5e52638SMatthew G. Knepley   if (label) {
5225e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5226e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5227e5e52638SMatthew G. Knepley   }
5228b3cf3223SMatthew G. Knepley   if (fields) {
5229b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5230b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5231b3cf3223SMatthew G. Knepley   }
5232e5e52638SMatthew G. Knepley   if (ds) {
5233b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5234e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5235e5e52638SMatthew G. Knepley   }
5236e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5237e5e52638SMatthew G. Knepley }
5238e5e52638SMatthew G. Knepley 
5239e5e52638SMatthew G. Knepley /*@
5240083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5241e5e52638SMatthew G. Knepley 
5242083401c6SMatthew G. Knepley   Not collective
5243e5e52638SMatthew G. Knepley 
5244e5e52638SMatthew G. Knepley   Input Parameters:
5245e5e52638SMatthew G. Knepley + dm     - The DM
5246083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5247083401c6SMatthew G. Knepley . label  - The region label, or NULL
5248083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5249083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5250e5e52638SMatthew G. Knepley 
5251e5e52638SMatthew G. Knepley   Level: advanced
5252e5e52638SMatthew G. Knepley 
5253083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5254e5e52638SMatthew G. Knepley @*/
5255083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5256e5e52638SMatthew G. Knepley {
5257083401c6SMatthew G. Knepley   PetscInt       Nds;
5258e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5259e5e52638SMatthew G. Knepley 
5260e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5261e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5262083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5263083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5264083401c6SMatthew G. Knepley   if ((num < 0) || (num >= Nds)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
5265e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5266083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5267083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5268083401c6SMatthew G. Knepley   if (fields) {
5269083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5270b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5271083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5272083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5273e5e52638SMatthew G. Knepley   }
5274083401c6SMatthew G. Knepley   if (ds) {
5275083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5276083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5277083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5278083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5279083401c6SMatthew G. Knepley   }
5280e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5281e5e52638SMatthew G. Knepley }
5282e5e52638SMatthew G. Knepley 
5283e5e52638SMatthew G. Knepley /*@
5284e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5285e5e52638SMatthew G. Knepley 
5286d083f849SBarry Smith   Collective on dm
5287e5e52638SMatthew G. Knepley 
5288e5e52638SMatthew G. Knepley   Input Parameter:
5289e5e52638SMatthew G. Knepley . dm - The DM
5290e5e52638SMatthew G. Knepley 
5291e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5292e5e52638SMatthew G. Knepley 
5293e5e52638SMatthew G. Knepley   Level: intermediate
5294e5e52638SMatthew G. Knepley 
5295e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5296e5e52638SMatthew G. Knepley @*/
5297e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5298e5e52638SMatthew G. Knepley {
5299e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5300083401c6SMatthew G. Knepley   PetscDS        dsDef;
5301083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5302083401c6SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef;
5303e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5304e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5305e5e52638SMatthew G. Knepley 
5306e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5307e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5308e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5309e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5310083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5311083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5312083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5313083401c6SMatthew G. Knepley   Nl   = 0;
5314083401c6SMatthew G. Knepley   Ndef = 0;
5315083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5316083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5317083401c6SMatthew G. Knepley     PetscInt l;
5318083401c6SMatthew G. Knepley 
5319083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5320083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5321083401c6SMatthew G. Knepley     if (l < Nl) continue;
5322083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5323083401c6SMatthew G. Knepley   }
5324083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5325083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5326083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5327b3cf3223SMatthew G. Knepley     IS        fields;
5328b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5329b3cf3223SMatthew G. Knepley 
5330b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5331083401c6SMatthew G. Knepley     if (nf) {
5332b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5333b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
533488f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
533588f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
533688f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
533788f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
533888f0c812SMatthew G. Knepley 
5339083401c6SMatthew G. Knepley       ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5340083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5341083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5342b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
53432df9ee95SMatthew G. Knepley     }
5344083401c6SMatthew G. Knepley   }
5345083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5346083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5347083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5348083401c6SMatthew G. Knepley   if (Ndef && Nl) {
53490122748bSMatthew G. Knepley     DM              plex;
5350083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5351083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5352083401c6SMatthew G. Knepley     PetscInt       *fields;
5353083401c6SMatthew G. Knepley     const PetscInt *cells;
5354083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
53550122748bSMatthew G. Knepley 
53560122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
53570122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5358083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5359083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5360083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5361083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5362083401c6SMatthew G. Knepley       IS      pointIS;
5363083401c6SMatthew G. Knepley 
5364083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5365083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5366083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5367083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5368083401c6SMatthew G. Knepley     }
5369083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5370083401c6SMatthew G. Knepley 
5371083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5372083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5373083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5374083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5375083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5376083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5377083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5378083401c6SMatthew G. Knepley 
5379083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5380083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5381083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5382083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5383083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5384083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5385083401c6SMatthew G. Knepley 
5386083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5387083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5388083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5389083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5390083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5391083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
53920122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5393083401c6SMatthew G. Knepley   }
5394083401c6SMatthew G. Knepley   /* Create label DSes
5395083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5396083401c6SMatthew G. Knepley   */
5397083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5398083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5399083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5400083401c6SMatthew G. Knepley     PetscDS   ds;
5401083401c6SMatthew G. Knepley     IS        fields;
5402083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5403083401c6SMatthew G. Knepley 
5404083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &ds);CHKERRQ(ierr);
5405083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5406083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5407083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5408083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5409083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5410083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5411083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5412083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5413083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5414083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5415083401c6SMatthew G. Knepley     {
5416083401c6SMatthew G. Knepley       DMPolytopeType ct;
5417083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5418083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
54190122748bSMatthew G. Knepley 
5420e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5421665f567fSMatthew G. Knepley       if (lStart >= 0) {
5422412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5423412e9a14SMatthew G. Knepley         switch (ct) {
5424412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5425412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5426412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5427412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5428083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5429083401c6SMatthew G. Knepley           default: break;
5430412e9a14SMatthew G. Knepley         }
5431665f567fSMatthew G. Knepley       }
5432083401c6SMatthew G. Knepley       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
5433083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5434e5e52638SMatthew G. Knepley     }
5435083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5436e5e52638SMatthew G. Knepley   }
5437083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5438e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5439083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5440083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5441083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5442083401c6SMatthew G. Knepley     const PetscInt *fld;
5443083401c6SMatthew G. Knepley     PetscInt        nf;
5444e5e52638SMatthew G. Knepley 
5445083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5446083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5447083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5448083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5449083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5450e5e52638SMatthew G. Knepley       PetscClassId id;
5451e5e52638SMatthew G. Knepley 
5452083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5453083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5454083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5455083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5456083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5457e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5458e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5459e5e52638SMatthew G. Knepley     }
5460083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5461e5e52638SMatthew G. Knepley   }
5462e5e52638SMatthew G. Knepley   /* Setup DSes */
5463e5e52638SMatthew G. Knepley   if (doSetup) {
5464e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5465e5e52638SMatthew G. Knepley   }
5466e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5467e5e52638SMatthew G. Knepley }
5468e5e52638SMatthew G. Knepley 
5469e5e52638SMatthew G. Knepley /*@
5470e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5471e5e52638SMatthew G. Knepley 
5472d083f849SBarry Smith   Collective on dm
5473e5e52638SMatthew G. Knepley 
5474e5e52638SMatthew G. Knepley   Input Parameter:
5475e5e52638SMatthew G. Knepley . dm - The DM
5476e5e52638SMatthew G. Knepley 
5477e5e52638SMatthew G. Knepley   Output Parameter:
5478e5e52638SMatthew G. Knepley . newdm - The DM
5479e5e52638SMatthew G. Knepley 
5480e5e52638SMatthew G. Knepley   Level: advanced
5481e5e52638SMatthew G. Knepley 
5482e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5483e5e52638SMatthew G. Knepley @*/
5484e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5485e5e52638SMatthew G. Knepley {
5486e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5487e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5488e5e52638SMatthew G. Knepley 
5489e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5490e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5491e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5492e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5493e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5494e5e52638SMatthew G. Knepley     DMLabel  label;
5495b3cf3223SMatthew G. Knepley     IS       fields;
5496e5e52638SMatthew G. Knepley     PetscDS  ds;
5497783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5498e5e52638SMatthew G. Knepley 
5499b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5500b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5501783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5502783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5503783e2ec8SMatthew G. Knepley       const char *labelname, *name;
5504783e2ec8SMatthew G. Knepley       PetscInt    field;
5505783e2ec8SMatthew G. Knepley 
5506783e2ec8SMatthew G. Knepley       /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
5507783e2ec8SMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, &name, &labelname, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5508783e2ec8SMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, ds, field, bd, labelname);CHKERRQ(ierr);
5509783e2ec8SMatthew G. Knepley     }
5510e5e52638SMatthew G. Knepley   }
5511e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5512e5e52638SMatthew G. Knepley }
5513e5e52638SMatthew G. Knepley 
5514e5e52638SMatthew G. Knepley /*@
5515e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5516e5e52638SMatthew G. Knepley 
5517d083f849SBarry Smith   Collective on dm
5518e5e52638SMatthew G. Knepley 
5519e5e52638SMatthew G. Knepley   Input Parameter:
5520e5e52638SMatthew G. Knepley . dm - The DM
5521e5e52638SMatthew G. Knepley 
5522e5e52638SMatthew G. Knepley   Output Parameter:
5523e5e52638SMatthew G. Knepley . newdm - The DM
5524e5e52638SMatthew G. Knepley 
5525e5e52638SMatthew G. Knepley   Level: advanced
5526e5e52638SMatthew G. Knepley 
5527e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5528e5e52638SMatthew G. Knepley @*/
5529e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5530e5e52638SMatthew G. Knepley {
5531e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5532e5e52638SMatthew G. Knepley 
5533e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5534e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5535e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5536e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5537e5e52638SMatthew G. Knepley }
5538e5e52638SMatthew G. Knepley 
5539b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5540b64e0483SPeter Brune {
5541b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5542b64e0483SPeter Brune   PetscErrorCode ierr;
5543b64e0483SPeter Brune   Vec coords,ccoords;
55446dbf9973SLawrence Mitchell   Mat inject;
5545b64e0483SPeter Brune   PetscFunctionBegin;
5546b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5547b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5548b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5549b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5550b64e0483SPeter Brune   if (coords && !ccoords) {
5551b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
55526668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
55536dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
55542adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
55556dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5556b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5557b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5558b64e0483SPeter Brune   }
5559b64e0483SPeter Brune   PetscFunctionReturn(0);
5560b64e0483SPeter Brune }
5561b64e0483SPeter Brune 
556203dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
556303dadc2fSPeter Brune {
556403dadc2fSPeter Brune   DM dm_coord,subdm_coord;
556503dadc2fSPeter Brune   PetscErrorCode ierr;
556603dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
556703dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
556803dadc2fSPeter Brune   PetscFunctionBegin;
556903dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
557003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
557103dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
557203dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
557303dadc2fSPeter Brune   if (coords && !ccoords) {
557403dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
55756668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
557603dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
557724640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
557803dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
557903dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
558003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
55811ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
558203dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
558303dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
558403dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
558503dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
558603dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
558703dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
558803dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
558903dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
559003dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
559103dadc2fSPeter Brune   }
559203dadc2fSPeter Brune   PetscFunctionReturn(0);
559303dadc2fSPeter Brune }
559403dadc2fSPeter Brune 
5595c73cfb54SMatthew G. Knepley /*@
5596c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5597c73cfb54SMatthew G. Knepley 
5598c73cfb54SMatthew G. Knepley   Not collective
5599c73cfb54SMatthew G. Knepley 
5600c73cfb54SMatthew G. Knepley   Input Parameter:
5601c73cfb54SMatthew G. Knepley . dm - The DM
5602c73cfb54SMatthew G. Knepley 
5603c73cfb54SMatthew G. Knepley   Output Parameter:
5604c73cfb54SMatthew G. Knepley . dim - The topological dimension
5605c73cfb54SMatthew G. Knepley 
5606c73cfb54SMatthew G. Knepley   Level: beginner
5607c73cfb54SMatthew G. Knepley 
5608c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5609c73cfb54SMatthew G. Knepley @*/
5610c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5611c73cfb54SMatthew G. Knepley {
5612c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5613c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5614534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5615c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5616c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5617c73cfb54SMatthew G. Knepley }
5618c73cfb54SMatthew G. Knepley 
5619c73cfb54SMatthew G. Knepley /*@
5620c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5621c73cfb54SMatthew G. Knepley 
5622c73cfb54SMatthew G. Knepley   Collective on dm
5623c73cfb54SMatthew G. Knepley 
5624c73cfb54SMatthew G. Knepley   Input Parameters:
5625c73cfb54SMatthew G. Knepley + dm - The DM
5626c73cfb54SMatthew G. Knepley - dim - The topological dimension
5627c73cfb54SMatthew G. Knepley 
5628c73cfb54SMatthew G. Knepley   Level: beginner
5629c73cfb54SMatthew G. Knepley 
5630c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5631c73cfb54SMatthew G. Knepley @*/
5632c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5633c73cfb54SMatthew G. Knepley {
5634e5e52638SMatthew G. Knepley   PetscDS        ds;
5635f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5636f17e8794SMatthew G. Knepley 
5637c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5638c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5639c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5640c73cfb54SMatthew G. Knepley   dm->dim = dim;
5641e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5642e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5643c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5644c73cfb54SMatthew G. Knepley }
5645c73cfb54SMatthew G. Knepley 
5646793f3fe5SMatthew G. Knepley /*@
5647793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5648793f3fe5SMatthew G. Knepley 
5649d083f849SBarry Smith   Collective on dm
5650793f3fe5SMatthew G. Knepley 
5651793f3fe5SMatthew G. Knepley   Input Parameters:
5652793f3fe5SMatthew G. Knepley + dm - the DM
5653793f3fe5SMatthew G. Knepley - dim - the dimension
5654793f3fe5SMatthew G. Knepley 
5655793f3fe5SMatthew G. Knepley   Output Parameters:
5656793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5657aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5658793f3fe5SMatthew G. Knepley 
5659793f3fe5SMatthew G. Knepley   Note:
5660793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5661a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5662793f3fe5SMatthew G. Knepley   then the interval is empty.
5663793f3fe5SMatthew G. Knepley 
5664793f3fe5SMatthew G. Knepley   Level: intermediate
5665793f3fe5SMatthew G. Knepley 
5666793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5667793f3fe5SMatthew G. Knepley @*/
5668793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5669793f3fe5SMatthew G. Knepley {
5670793f3fe5SMatthew G. Knepley   PetscInt       d;
5671793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5672793f3fe5SMatthew G. Knepley 
5673793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5674793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5675793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5676793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5677b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5678793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5679793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5680793f3fe5SMatthew G. Knepley }
5681793f3fe5SMatthew G. Knepley 
56826636e97aSMatthew G Knepley /*@
56836636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
56846636e97aSMatthew G Knepley 
5685d083f849SBarry Smith   Collective on dm
56866636e97aSMatthew G Knepley 
56876636e97aSMatthew G Knepley   Input Parameters:
56886636e97aSMatthew G Knepley + dm - the DM
56896636e97aSMatthew G Knepley - c - coordinate vector
56906636e97aSMatthew G Knepley 
56912dd40e9bSPatrick Sanan   Notes:
56922dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
56932dd40e9bSPatrick Sanan 
56942dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
56956636e97aSMatthew G Knepley 
56966636e97aSMatthew G Knepley   Level: intermediate
56976636e97aSMatthew G Knepley 
56982dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
56996636e97aSMatthew G Knepley @*/
57006636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
57016636e97aSMatthew G Knepley {
57026636e97aSMatthew G Knepley   PetscErrorCode ierr;
57036636e97aSMatthew G Knepley 
57046636e97aSMatthew G Knepley   PetscFunctionBegin;
57056636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57066636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
57076636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
57086636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
57096636e97aSMatthew G Knepley   dm->coordinates = c;
57106636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5711b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
571203dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
57136636e97aSMatthew G Knepley   PetscFunctionReturn(0);
57146636e97aSMatthew G Knepley }
57156636e97aSMatthew G Knepley 
57166636e97aSMatthew G Knepley /*@
57176636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
57186636e97aSMatthew G Knepley 
57197058e716SVaclav Hapla   Not collective
57206636e97aSMatthew G Knepley 
57216636e97aSMatthew G Knepley    Input Parameters:
57226636e97aSMatthew G Knepley +  dm - the DM
57236636e97aSMatthew G Knepley -  c - coordinate vector
57246636e97aSMatthew G Knepley 
57252dd40e9bSPatrick Sanan   Notes:
57266636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
57276636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
57286636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
57296636e97aSMatthew G Knepley 
57302dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
57312dd40e9bSPatrick Sanan 
57326636e97aSMatthew G Knepley   Level: intermediate
57336636e97aSMatthew G Knepley 
57346636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
57356636e97aSMatthew G Knepley @*/
57366636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
57376636e97aSMatthew G Knepley {
57386636e97aSMatthew G Knepley   PetscErrorCode ierr;
57396636e97aSMatthew G Knepley 
57406636e97aSMatthew G Knepley   PetscFunctionBegin;
57416636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57426636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
57436636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
57446636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
57458865f1eaSKarl Rupp 
57466636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
57478865f1eaSKarl Rupp 
57486636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
57496636e97aSMatthew G Knepley   PetscFunctionReturn(0);
57506636e97aSMatthew G Knepley }
57516636e97aSMatthew G Knepley 
57526636e97aSMatthew G Knepley /*@
57536636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
57546636e97aSMatthew G Knepley 
5755d083f849SBarry Smith   Collective on dm
57566636e97aSMatthew G Knepley 
57576636e97aSMatthew G Knepley   Input Parameter:
57586636e97aSMatthew G Knepley . dm - the DM
57596636e97aSMatthew G Knepley 
57606636e97aSMatthew G Knepley   Output Parameter:
57616636e97aSMatthew G Knepley . c - global coordinate vector
57626636e97aSMatthew G Knepley 
57636636e97aSMatthew G Knepley   Note:
57646636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
57656636e97aSMatthew G Knepley 
57666636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
57676636e97aSMatthew G Knepley 
57686636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
57696636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
57706636e97aSMatthew G Knepley 
57716636e97aSMatthew G Knepley   Level: intermediate
57726636e97aSMatthew G Knepley 
57736636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
57746636e97aSMatthew G Knepley @*/
57756636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
57766636e97aSMatthew G Knepley {
57776636e97aSMatthew G Knepley   PetscErrorCode ierr;
57786636e97aSMatthew G Knepley 
57796636e97aSMatthew G Knepley   PetscFunctionBegin;
57806636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57816636e97aSMatthew G Knepley   PetscValidPointer(c,2);
57821f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
57830298fd71SBarry Smith     DM        cdm = NULL;
57841970a576SMatthew G. Knepley     PetscBool localized;
57856636e97aSMatthew G Knepley 
57866636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
57876636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
57881970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
57891970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
57901970a576SMatthew G. Knepley     if (localized) {
57911970a576SMatthew G. Knepley       PetscInt cdim;
57921970a576SMatthew G. Knepley 
57931970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
57941970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
57951970a576SMatthew G. Knepley     }
57966636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
57976636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
57986636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
57996636e97aSMatthew G Knepley   }
58006636e97aSMatthew G Knepley   *c = dm->coordinates;
58016636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58026636e97aSMatthew G Knepley }
58036636e97aSMatthew G Knepley 
58046636e97aSMatthew G Knepley /*@
580581e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
580681e9a530SVaclav Hapla 
5807d083f849SBarry Smith   Collective on dm
580881e9a530SVaclav Hapla 
580981e9a530SVaclav Hapla   Input Parameter:
581081e9a530SVaclav Hapla . dm - the DM
581181e9a530SVaclav Hapla 
581281e9a530SVaclav Hapla   Level: advanced
581381e9a530SVaclav Hapla 
581481e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
581581e9a530SVaclav Hapla @*/
581681e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
581781e9a530SVaclav Hapla {
581881e9a530SVaclav Hapla   PetscErrorCode ierr;
581981e9a530SVaclav Hapla 
582081e9a530SVaclav Hapla   PetscFunctionBegin;
582181e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
582281e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
58231970a576SMatthew G. Knepley     DM        cdm = NULL;
58241970a576SMatthew G. Knepley     PetscBool localized;
58251970a576SMatthew G. Knepley 
582681e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
582781e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
58281970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
58291970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
58301970a576SMatthew G. Knepley     if (localized) {
58311970a576SMatthew G. Knepley       PetscInt cdim;
58321970a576SMatthew G. Knepley 
58331970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
58341970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
58351970a576SMatthew G. Knepley     }
583681e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
583781e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
583881e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
583981e9a530SVaclav Hapla   }
584081e9a530SVaclav Hapla   PetscFunctionReturn(0);
584181e9a530SVaclav Hapla }
584281e9a530SVaclav Hapla 
584381e9a530SVaclav Hapla /*@
58446636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
58456636e97aSMatthew G Knepley 
5846d083f849SBarry Smith   Collective on dm
58476636e97aSMatthew G Knepley 
58486636e97aSMatthew G Knepley   Input Parameter:
58496636e97aSMatthew G Knepley . dm - the DM
58506636e97aSMatthew G Knepley 
58516636e97aSMatthew G Knepley   Output Parameter:
58526636e97aSMatthew G Knepley . c - coordinate vector
58536636e97aSMatthew G Knepley 
58546636e97aSMatthew G Knepley   Note:
58556636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
58566636e97aSMatthew G Knepley 
58576636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
58586636e97aSMatthew G Knepley 
58596636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
58606636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
58616636e97aSMatthew G Knepley 
58626636e97aSMatthew G Knepley   Level: intermediate
58636636e97aSMatthew G Knepley 
586481e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
58656636e97aSMatthew G Knepley @*/
58666636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
58676636e97aSMatthew G Knepley {
58686636e97aSMatthew G Knepley   PetscErrorCode ierr;
58696636e97aSMatthew G Knepley 
58706636e97aSMatthew G Knepley   PetscFunctionBegin;
58716636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58726636e97aSMatthew G Knepley   PetscValidPointer(c,2);
587381e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
58746636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
58756636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58766636e97aSMatthew G Knepley }
58776636e97aSMatthew G Knepley 
587881e9a530SVaclav Hapla /*@
587981e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
588081e9a530SVaclav Hapla 
588181e9a530SVaclav Hapla   Not collective
588281e9a530SVaclav Hapla 
588381e9a530SVaclav Hapla   Input Parameter:
588481e9a530SVaclav Hapla . dm - the DM
588581e9a530SVaclav Hapla 
588681e9a530SVaclav Hapla   Output Parameter:
588781e9a530SVaclav Hapla . c - coordinate vector
588881e9a530SVaclav Hapla 
588981e9a530SVaclav Hapla   Level: advanced
589081e9a530SVaclav Hapla 
589181e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
589281e9a530SVaclav Hapla @*/
589381e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
589481e9a530SVaclav Hapla {
589581e9a530SVaclav Hapla   PetscFunctionBegin;
589681e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
589781e9a530SVaclav Hapla   PetscValidPointer(c,2);
589881e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
58996636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
59006636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59016636e97aSMatthew G Knepley }
59026636e97aSMatthew G Knepley 
59032db98f8dSVaclav Hapla /*@
59042db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
59052db98f8dSVaclav Hapla 
59062db98f8dSVaclav Hapla   Not collective
59072db98f8dSVaclav Hapla 
59082db98f8dSVaclav Hapla   Input Parameter:
59092db98f8dSVaclav Hapla + dm - the DM
59102db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
59112db98f8dSVaclav Hapla 
59122db98f8dSVaclav Hapla   Output Parameter:
59132db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
59142db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
59152db98f8dSVaclav Hapla 
59162db98f8dSVaclav Hapla   Note:
59172db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
59182db98f8dSVaclav Hapla 
59192db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
59202db98f8dSVaclav Hapla 
59212db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
59222db98f8dSVaclav Hapla 
59232db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
59242db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
59252db98f8dSVaclav Hapla 
59262db98f8dSVaclav Hapla   Level: advanced
59272db98f8dSVaclav Hapla 
59282db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
59292db98f8dSVaclav Hapla @*/
59302db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
59312db98f8dSVaclav Hapla {
59322db98f8dSVaclav Hapla   PetscSection        cs, newcs;
59332db98f8dSVaclav Hapla   Vec                 coords;
59342db98f8dSVaclav Hapla   const PetscScalar   *arr;
59352db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
59362db98f8dSVaclav Hapla   PetscInt            n;
59372db98f8dSVaclav Hapla   PetscErrorCode      ierr;
59382db98f8dSVaclav Hapla 
59392db98f8dSVaclav Hapla   PetscFunctionBegin;
59402db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59412db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
59422db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
59432db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
59442db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
59451bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
59461bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
59472db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
59482db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
59492db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
59502db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
59512db98f8dSVaclav Hapla   if (pCoord) {
59522db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
59532db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
59542db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
59552db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5956ad9ac99dSVaclav Hapla   } else {
5957ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
59582db98f8dSVaclav Hapla   }
5959ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5960ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
59612db98f8dSVaclav Hapla   PetscFunctionReturn(0);
59622db98f8dSVaclav Hapla }
59632db98f8dSVaclav Hapla 
5964f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5965f19dbd58SToby Isaac {
5966f19dbd58SToby Isaac   PetscErrorCode ierr;
5967f19dbd58SToby Isaac 
5968f19dbd58SToby Isaac   PetscFunctionBegin;
5969f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5970f19dbd58SToby Isaac   PetscValidPointer(field,2);
5971f19dbd58SToby Isaac   if (!dm->coordinateField) {
5972f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5973f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5974f19dbd58SToby Isaac     }
5975f19dbd58SToby Isaac   }
5976f19dbd58SToby Isaac   *field = dm->coordinateField;
5977f19dbd58SToby Isaac   PetscFunctionReturn(0);
5978f19dbd58SToby Isaac }
5979f19dbd58SToby Isaac 
5980f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5981f19dbd58SToby Isaac {
5982f19dbd58SToby Isaac   PetscErrorCode ierr;
5983f19dbd58SToby Isaac 
5984f19dbd58SToby Isaac   PetscFunctionBegin;
5985f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5986f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5987c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5988f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5989f19dbd58SToby Isaac   dm->coordinateField = field;
5990f19dbd58SToby Isaac   PetscFunctionReturn(0);
5991f19dbd58SToby Isaac }
5992f19dbd58SToby Isaac 
59936636e97aSMatthew G Knepley /*@
59941cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
59956636e97aSMatthew G Knepley 
5996d083f849SBarry Smith   Collective on dm
59976636e97aSMatthew G Knepley 
59986636e97aSMatthew G Knepley   Input Parameter:
59996636e97aSMatthew G Knepley . dm - the DM
60006636e97aSMatthew G Knepley 
60016636e97aSMatthew G Knepley   Output Parameter:
60026636e97aSMatthew G Knepley . cdm - coordinate DM
60036636e97aSMatthew G Knepley 
60046636e97aSMatthew G Knepley   Level: intermediate
60056636e97aSMatthew G Knepley 
60061cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
60076636e97aSMatthew G Knepley @*/
60086636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
60096636e97aSMatthew G Knepley {
60106636e97aSMatthew G Knepley   PetscErrorCode ierr;
60116636e97aSMatthew G Knepley 
60126636e97aSMatthew G Knepley   PetscFunctionBegin;
60136636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60146636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
60156636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6016308f8a94SToby Isaac     DM cdm;
6017308f8a94SToby Isaac 
601882f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6019308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6020308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6021308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6022308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6023308f8a94SToby Isaac     dm->coordinateDM = cdm;
60246636e97aSMatthew G Knepley   }
60256636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
60266636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60276636e97aSMatthew G Knepley }
6028e87bb0d3SMatthew G Knepley 
60291cfe2091SMatthew G. Knepley /*@
60301cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
60311cfe2091SMatthew G. Knepley 
6032d083f849SBarry Smith   Logically Collective on dm
60331cfe2091SMatthew G. Knepley 
60341cfe2091SMatthew G. Knepley   Input Parameters:
60351cfe2091SMatthew G. Knepley + dm - the DM
60361cfe2091SMatthew G. Knepley - cdm - coordinate DM
60371cfe2091SMatthew G. Knepley 
60381cfe2091SMatthew G. Knepley   Level: intermediate
60391cfe2091SMatthew G. Knepley 
60401cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
60411cfe2091SMatthew G. Knepley @*/
60421cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
60431cfe2091SMatthew G. Knepley {
60441cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
60451cfe2091SMatthew G. Knepley 
60461cfe2091SMatthew G. Knepley   PetscFunctionBegin;
60471cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60481cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6049f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
60501cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
60511cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
60521cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
60531cfe2091SMatthew G. Knepley }
60541cfe2091SMatthew G. Knepley 
605546e270d4SMatthew G. Knepley /*@
605646e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
605746e270d4SMatthew G. Knepley 
605846e270d4SMatthew G. Knepley   Not Collective
605946e270d4SMatthew G. Knepley 
606046e270d4SMatthew G. Knepley   Input Parameter:
606146e270d4SMatthew G. Knepley . dm - The DM object
606246e270d4SMatthew G. Knepley 
606346e270d4SMatthew G. Knepley   Output Parameter:
606446e270d4SMatthew G. Knepley . dim - The embedding dimension
606546e270d4SMatthew G. Knepley 
606646e270d4SMatthew G. Knepley   Level: intermediate
606746e270d4SMatthew G. Knepley 
606892fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
606946e270d4SMatthew G. Knepley @*/
607046e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
607146e270d4SMatthew G. Knepley {
607246e270d4SMatthew G. Knepley   PetscFunctionBegin;
607346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6074534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
60759a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
60769a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
60779a9a41abSToby Isaac   }
607846e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
607946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
608046e270d4SMatthew G. Knepley }
608146e270d4SMatthew G. Knepley 
608246e270d4SMatthew G. Knepley /*@
608346e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
608446e270d4SMatthew G. Knepley 
608546e270d4SMatthew G. Knepley   Not Collective
608646e270d4SMatthew G. Knepley 
608746e270d4SMatthew G. Knepley   Input Parameters:
608846e270d4SMatthew G. Knepley + dm  - The DM object
608946e270d4SMatthew G. Knepley - dim - The embedding dimension
609046e270d4SMatthew G. Knepley 
609146e270d4SMatthew G. Knepley   Level: intermediate
609246e270d4SMatthew G. Knepley 
609392fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
609446e270d4SMatthew G. Knepley @*/
609546e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
609646e270d4SMatthew G. Knepley {
6097e5e52638SMatthew G. Knepley   PetscDS        ds;
6098f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6099f17e8794SMatthew G. Knepley 
610046e270d4SMatthew G. Knepley   PetscFunctionBegin;
610146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
610246e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6103e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
6104e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
610546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
610646e270d4SMatthew G. Knepley }
610746e270d4SMatthew G. Knepley 
6108e8abe2deSMatthew G. Knepley /*@
6109e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6110e8abe2deSMatthew G. Knepley 
6111d083f849SBarry Smith   Collective on dm
6112e8abe2deSMatthew G. Knepley 
6113e8abe2deSMatthew G. Knepley   Input Parameter:
6114e8abe2deSMatthew G. Knepley . dm - The DM object
6115e8abe2deSMatthew G. Knepley 
6116e8abe2deSMatthew G. Knepley   Output Parameter:
6117e8abe2deSMatthew G. Knepley . section - The PetscSection object
6118e8abe2deSMatthew G. Knepley 
6119e8abe2deSMatthew G. Knepley   Level: intermediate
6120e8abe2deSMatthew G. Knepley 
612192fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6122e8abe2deSMatthew G. Knepley @*/
6123e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6124e8abe2deSMatthew G. Knepley {
6125e8abe2deSMatthew G. Knepley   DM             cdm;
6126e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6127e8abe2deSMatthew G. Knepley 
6128e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6129e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6130e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6131e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
613292fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6133e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6134e8abe2deSMatthew G. Knepley }
6135e8abe2deSMatthew G. Knepley 
6136e8abe2deSMatthew G. Knepley /*@
6137e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6138e8abe2deSMatthew G. Knepley 
6139e8abe2deSMatthew G. Knepley   Not Collective
6140e8abe2deSMatthew G. Knepley 
6141e8abe2deSMatthew G. Knepley   Input Parameters:
6142e8abe2deSMatthew G. Knepley + dm      - The DM object
614346e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6144e8abe2deSMatthew G. Knepley - section - The PetscSection object
6145e8abe2deSMatthew G. Knepley 
6146e8abe2deSMatthew G. Knepley   Level: intermediate
6147e8abe2deSMatthew G. Knepley 
614892fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6149e8abe2deSMatthew G. Knepley @*/
615046e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6151e8abe2deSMatthew G. Knepley {
6152e8abe2deSMatthew G. Knepley   DM             cdm;
6153e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6154e8abe2deSMatthew G. Knepley 
6155e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6156e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
615746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6158e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
615992fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
616046e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
61614c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
616246e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
616346e270d4SMatthew G. Knepley 
616446e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
616546e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
616646e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
616746e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
616846e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
616946e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
617046e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
617146e270d4SMatthew G. Knepley     }
6172ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
617346e270d4SMatthew G. Knepley   }
6174e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6175e8abe2deSMatthew G. Knepley }
6176e8abe2deSMatthew G. Knepley 
61775dc8c3f7SMatthew G. Knepley /*@C
617890b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
61795dc8c3f7SMatthew G. Knepley 
61805dc8c3f7SMatthew G. Knepley   Input Parameters:
618190b157c4SStefano Zampini . dm      - The DM object
618290b157c4SStefano Zampini 
618390b157c4SStefano Zampini   Output Parameters:
618490b157c4SStefano Zampini + per     - Whether the DM is periodic or not
61855dc8c3f7SMatthew 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
61865dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
61875dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
61885dc8c3f7SMatthew G. Knepley 
61895dc8c3f7SMatthew G. Knepley   Level: developer
61905dc8c3f7SMatthew G. Knepley 
61915dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
61925dc8c3f7SMatthew G. Knepley @*/
619390b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6194c6b900c6SMatthew G. Knepley {
6195c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6196c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
619790b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6198c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6199c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
62005dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6201c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6202c6b900c6SMatthew G. Knepley }
6203c6b900c6SMatthew G. Knepley 
62045dc8c3f7SMatthew G. Knepley /*@C
62055dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
62065dc8c3f7SMatthew G. Knepley 
62075dc8c3f7SMatthew G. Knepley   Input Parameters:
62085dc8c3f7SMatthew G. Knepley + dm      - The DM object
620990b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
62105dc8c3f7SMatthew 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
62115dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
62125dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
62135dc8c3f7SMatthew G. Knepley 
62145dc8c3f7SMatthew G. Knepley   Level: developer
62155dc8c3f7SMatthew G. Knepley 
62165dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
62175dc8c3f7SMatthew G. Knepley @*/
621890b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6219c6b900c6SMatthew G. Knepley {
6220c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6221c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6222c6b900c6SMatthew G. Knepley 
6223c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6224c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
622590b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6226412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6227412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6228412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
62295dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
623090b157c4SStefano Zampini   if (maxCell) {
6231412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6232412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6233412e9a14SMatthew G. Knepley   }
6234412e9a14SMatthew G. Knepley   if (L) {
6235412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6236412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6237412e9a14SMatthew G. Knepley   }
6238412e9a14SMatthew G. Knepley   if (bd) {
6239412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6240412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
624190b157c4SStefano Zampini   }
6242072d7d67SStefano Zampini   dm->periodic = per;
6243c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6244c6b900c6SMatthew G. Knepley }
6245c6b900c6SMatthew G. Knepley 
62462e17dfb7SMatthew G. Knepley /*@
62472e17dfb7SMatthew 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.
62482e17dfb7SMatthew G. Knepley 
62492e17dfb7SMatthew G. Knepley   Input Parameters:
62502e17dfb7SMatthew G. Knepley + dm     - The DM
625165da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
625265da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
62532e17dfb7SMatthew G. Knepley 
62542e17dfb7SMatthew G. Knepley   Output Parameter:
62552e17dfb7SMatthew G. Knepley . out - The localized coordinate point
62562e17dfb7SMatthew G. Knepley 
62572e17dfb7SMatthew G. Knepley   Level: developer
62582e17dfb7SMatthew G. Knepley 
62592e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
62602e17dfb7SMatthew G. Knepley @*/
626165da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
62622e17dfb7SMatthew G. Knepley {
62632e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
62642e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
62652e17dfb7SMatthew G. Knepley 
62662e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
62672e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
62682e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
62692e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
62702e17dfb7SMatthew G. Knepley   } else {
627165da65dcSMatthew G. Knepley     if (endpoint) {
627265da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6273da3333bfSMatthew 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)) {
6274da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
627565da65dcSMatthew G. Knepley         } else {
6276da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
627765da65dcSMatthew G. Knepley         }
627865da65dcSMatthew G. Knepley       }
627965da65dcSMatthew G. Knepley     } else {
62802e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
62811118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
62822e17dfb7SMatthew G. Knepley       }
62832e17dfb7SMatthew G. Knepley     }
628465da65dcSMatthew G. Knepley   }
62852e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
62862e17dfb7SMatthew G. Knepley }
62872e17dfb7SMatthew G. Knepley 
62882e17dfb7SMatthew G. Knepley /*
62892e17dfb7SMatthew 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.
62902e17dfb7SMatthew G. Knepley 
62912e17dfb7SMatthew G. Knepley   Input Parameters:
62922e17dfb7SMatthew G. Knepley + dm     - The DM
62932e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
62942e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
62952e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
62962e17dfb7SMatthew G. Knepley 
62972e17dfb7SMatthew G. Knepley   Output Parameter:
62982e17dfb7SMatthew G. Knepley . out - The localized coordinate point
62992e17dfb7SMatthew G. Knepley 
63002e17dfb7SMatthew G. Knepley   Level: developer
63012e17dfb7SMatthew G. Knepley 
63022e17dfb7SMatthew 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
63032e17dfb7SMatthew G. Knepley 
63042e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
63052e17dfb7SMatthew G. Knepley */
63062e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
63072e17dfb7SMatthew G. Knepley {
63082e17dfb7SMatthew G. Knepley   PetscInt d;
63092e17dfb7SMatthew G. Knepley 
63102e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
63112e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
63122e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
63132e17dfb7SMatthew G. Knepley   } else {
63142e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6315908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
63162e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
63172e17dfb7SMatthew G. Knepley       } else {
63182e17dfb7SMatthew G. Knepley         out[d] = in[d];
63192e17dfb7SMatthew G. Knepley       }
63202e17dfb7SMatthew G. Knepley     }
63212e17dfb7SMatthew G. Knepley   }
63222e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
63232e17dfb7SMatthew G. Knepley }
6324*a5801f52SStefano Zampini 
63252e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
63262e17dfb7SMatthew G. Knepley {
63272e17dfb7SMatthew G. Knepley   PetscInt d;
63282e17dfb7SMatthew G. Knepley 
63292e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
63302e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
63312e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
63322e17dfb7SMatthew G. Knepley   } else {
63332e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6334908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
63352e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
63362e17dfb7SMatthew G. Knepley       } else {
63372e17dfb7SMatthew G. Knepley         out[d] = in[d];
63382e17dfb7SMatthew G. Knepley       }
63392e17dfb7SMatthew G. Knepley     }
63402e17dfb7SMatthew G. Knepley   }
63412e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
63422e17dfb7SMatthew G. Knepley }
63432e17dfb7SMatthew G. Knepley 
63442e17dfb7SMatthew G. Knepley /*
63452e17dfb7SMatthew 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.
63462e17dfb7SMatthew G. Knepley 
63472e17dfb7SMatthew G. Knepley   Input Parameters:
63482e17dfb7SMatthew G. Knepley + dm     - The DM
63492e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
63502e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
63512e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
63522e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
63532e17dfb7SMatthew G. Knepley 
63542e17dfb7SMatthew G. Knepley   Output Parameter:
63552e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
63562e17dfb7SMatthew G. Knepley 
63572e17dfb7SMatthew G. Knepley   Level: developer
63582e17dfb7SMatthew G. Knepley 
63592e17dfb7SMatthew 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
63602e17dfb7SMatthew G. Knepley 
63612e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
63622e17dfb7SMatthew G. Knepley */
63632e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
63642e17dfb7SMatthew G. Knepley {
63652e17dfb7SMatthew G. Knepley   PetscInt d;
63662e17dfb7SMatthew G. Knepley 
63672e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
63682e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
63692e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
63702e17dfb7SMatthew G. Knepley   } else {
63712e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6372412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6373412e9a14SMatthew G. Knepley 
6374412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6375412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6376412e9a14SMatthew G. Knepley 
6377412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6378412e9a14SMatthew G. Knepley           SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%D-Coordinate %g more than %g away from anchor %g", d, (double) PetscRealPart(in[d]), (double) maxC, (double) PetscRealPart(anchor[d]));
6379412e9a14SMatthew G. Knepley         out[d] += newCoord;
63802e17dfb7SMatthew G. Knepley       } else {
63812e17dfb7SMatthew G. Knepley         out[d] += in[d];
63822e17dfb7SMatthew G. Knepley       }
63832e17dfb7SMatthew G. Knepley     }
63842e17dfb7SMatthew G. Knepley   }
63852e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
63862e17dfb7SMatthew G. Knepley }
63872e17dfb7SMatthew G. Knepley 
638836447a5eSToby Isaac /*@
63898f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
63908f700142SStefano Zampini 
63918f700142SStefano Zampini   Not collective
639236447a5eSToby Isaac 
639336447a5eSToby Isaac   Input Parameter:
639436447a5eSToby Isaac . dm - The DM
639536447a5eSToby Isaac 
639636447a5eSToby Isaac   Output Parameter:
639736447a5eSToby Isaac   areLocalized - True if localized
639836447a5eSToby Isaac 
639936447a5eSToby Isaac   Level: developer
640036447a5eSToby Isaac 
64018f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
640236447a5eSToby Isaac @*/
64038f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
640436447a5eSToby Isaac {
640536447a5eSToby Isaac   DM             cdm;
640636447a5eSToby Isaac   PetscSection   coordSection;
640746a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
640846a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
640936447a5eSToby Isaac   PetscErrorCode ierr;
641036447a5eSToby Isaac 
641136447a5eSToby Isaac   PetscFunctionBegin;
641236447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6413534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
64148b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
641546a3a80fSLisandro Dalcin 
641636447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
641736447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
641846a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
64199f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
642046a3a80fSLisandro Dalcin 
64219f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
642246a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
642336447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
642436447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
642546a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
642646a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
642736447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
642846a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
642936447a5eSToby Isaac   }
64308f700142SStefano Zampini   *areLocalized = alreadyLocalized;
643136447a5eSToby Isaac   PetscFunctionReturn(0);
643236447a5eSToby Isaac }
643336447a5eSToby Isaac 
64348f700142SStefano Zampini /*@
64358f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
64368f700142SStefano Zampini 
64378f700142SStefano Zampini   Collective on dm
64388f700142SStefano Zampini 
64398f700142SStefano Zampini   Input Parameter:
64408f700142SStefano Zampini . dm - The DM
64418f700142SStefano Zampini 
64428f700142SStefano Zampini   Output Parameter:
64438f700142SStefano Zampini   areLocalized - True if localized
64448f700142SStefano Zampini 
64458f700142SStefano Zampini   Level: developer
64468f700142SStefano Zampini 
64478f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
64488f700142SStefano Zampini @*/
64498f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
64508f700142SStefano Zampini {
64518f700142SStefano Zampini   PetscBool      localized;
64528f700142SStefano Zampini   PetscErrorCode ierr;
64538f700142SStefano Zampini 
64548f700142SStefano Zampini   PetscFunctionBegin;
64558f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6456534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
64578f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
64588f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
64598f700142SStefano Zampini   PetscFunctionReturn(0);
64608f700142SStefano Zampini }
646136447a5eSToby Isaac 
64622e17dfb7SMatthew G. Knepley /*@
6463492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
64642e17dfb7SMatthew G. Knepley 
64658f700142SStefano Zampini   Collective on dm
64668f700142SStefano Zampini 
64672e17dfb7SMatthew G. Knepley   Input Parameter:
64682e17dfb7SMatthew G. Knepley . dm - The DM
64692e17dfb7SMatthew G. Knepley 
64702e17dfb7SMatthew G. Knepley   Level: developer
64712e17dfb7SMatthew G. Knepley 
64728f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
64732e17dfb7SMatthew G. Knepley @*/
64742e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
64752e17dfb7SMatthew G. Knepley {
64762e17dfb7SMatthew G. Knepley   DM             cdm;
64772e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
64782e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
64793e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
64803e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6481e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
64823e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
64833e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
64842e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
64852e17dfb7SMatthew G. Knepley 
64862e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
64872e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
648892c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6489f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6490f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6491f7cbd40bSStefano Zampini 
64922e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
64932e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
64942e17dfb7SMatthew G. Knepley   {
64952e17dfb7SMatthew G. Knepley     PetscBool isplex;
64962e17dfb7SMatthew G. Knepley 
64972e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
64982e17dfb7SMatthew G. Knepley     if (isplex) {
64992e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
65003e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
650169291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
65023e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
65033e922f36SToby Isaac       newStart = vStart;
65043e922f36SToby Isaac       newEnd   = vEnd;
65053e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
65063e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
65073e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
65083e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
65093e922f36SToby Isaac       }
65102e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
65112e17dfb7SMatthew G. Knepley   }
65122e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
651343eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
65142e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
65153e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6516e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
65173e922f36SToby Isaac 
65182e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
65192e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
65202e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
65212e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
65223e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
65233e922f36SToby Isaac 
652469291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
65253e922f36SToby Isaac   localized = &anchor[bs];
65263e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
65273e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
65283e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
65293e922f36SToby Isaac 
65303e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
65313e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
65323e922f36SToby Isaac       PetscInt     b;
65333e922f36SToby Isaac 
65343e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
65353e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
65363e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
65373e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
65383e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
65393e922f36SToby Isaac         for (b = 0; b < bs; b++) {
65403e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
65413e922f36SToby Isaac         }
65423e922f36SToby Isaac         if (b < bs) break;
65433e922f36SToby Isaac       }
65443e922f36SToby Isaac       if (d < dof/bs) {
65453e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
65463e922f36SToby Isaac           PetscInt cdof;
65473e922f36SToby Isaac 
65483e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
65493e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
65503e922f36SToby Isaac         }
65513e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
65523e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
65533e922f36SToby Isaac       }
65543e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
65553e922f36SToby Isaac     }
65563e922f36SToby Isaac   }
65573e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
65583e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
655969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
65603e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
656169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
65623e922f36SToby Isaac     PetscFunctionReturn(0);
65633e922f36SToby Isaac   }
65642e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
65652e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
65662e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
65672e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
65682e17dfb7SMatthew G. Knepley   }
65692e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
65702e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6571c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
65722e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
65732e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
65742e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
65752e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6576c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
65772e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
65782e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
65792e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
65802e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
65812e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
65822e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
65832e17dfb7SMatthew G. Knepley   }
65843e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
65853e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
65863e922f36SToby Isaac 
65872e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
65882e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
65893e922f36SToby Isaac       PetscInt     b, cdof;
65902e17dfb7SMatthew G. Knepley 
65913e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
65923e922f36SToby Isaac       if (!cdof) continue;
65932e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
65942e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
65952e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
65962e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
65972e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
65982e17dfb7SMatthew G. Knepley     }
65993e922f36SToby Isaac   }
660069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
660169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6602c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
66032e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
66042e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
66052e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
66062e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
66072e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
66082e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66092e17dfb7SMatthew G. Knepley }
66102e17dfb7SMatthew G. Knepley 
6611e87bb0d3SMatthew G Knepley /*@
66123a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6613e87bb0d3SMatthew G Knepley 
6614d083f849SBarry Smith   Collective on v (see explanation below)
6615e87bb0d3SMatthew G Knepley 
6616e87bb0d3SMatthew G Knepley   Input Parameters:
6617e87bb0d3SMatthew G Knepley + dm - The DM
66183a93e3b7SToby Isaac . v - The Vec of points
661962a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
66203a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6621e87bb0d3SMatthew G Knepley 
662261e3bb9bSMatthew G Knepley   Output Parameter:
662362a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
662462a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
66253a93e3b7SToby Isaac 
6626e87bb0d3SMatthew G Knepley 
6627e87bb0d3SMatthew G Knepley   Level: developer
662861e3bb9bSMatthew G Knepley 
662962a38674SMatthew G. Knepley   Notes:
66303a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
663162a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
66323a93e3b7SToby Isaac 
66333a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
663462a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
66353a93e3b7SToby Isaac 
66363a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
66373a93e3b7SToby Isaac 
663862a38674SMatthew G. Knepley $    const PetscSFNode *cells;
663962a38674SMatthew G. Knepley $    PetscInt           nFound;
6640a6216909SToby Isaac $    const PetscInt    *found;
664162a38674SMatthew G. Knepley $
6642a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
66433a93e3b7SToby Isaac 
66443a93e3b7SToby 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
66453a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
66463a93e3b7SToby Isaac 
664762a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
664861e3bb9bSMatthew G Knepley @*/
664962a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6650e87bb0d3SMatthew G Knepley {
6651735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6652735aa83eSMatthew G Knepley 
6653e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6654e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6655e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6656e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
66573a93e3b7SToby Isaac   if (*cellSF) {
66583a93e3b7SToby Isaac     PetscMPIInt result;
66593a93e3b7SToby Isaac 
6660e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6661a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
66623a93e3b7SToby 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");
6663e0fc9d1bSMatthew G. Knepley   } else {
66643a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
66653a93e3b7SToby Isaac   }
6666b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
666747a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
666862a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
666947a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6670e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6671e87bb0d3SMatthew G Knepley }
667214f150ffSMatthew G. Knepley 
6673f4d763aaSMatthew G. Knepley /*@
6674f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6675f4d763aaSMatthew G. Knepley 
66768f700142SStefano Zampini   Collective on dm
66778f700142SStefano Zampini 
6678f4d763aaSMatthew G. Knepley   Input Parameter:
6679f4d763aaSMatthew G. Knepley . dm - The original DM
6680f4d763aaSMatthew G. Knepley 
6681f4d763aaSMatthew G. Knepley   Output Parameter:
6682f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6683f4d763aaSMatthew G. Knepley 
6684f4d763aaSMatthew G. Knepley   Level: intermediate
6685f4d763aaSMatthew G. Knepley 
6686e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6687f4d763aaSMatthew G. Knepley @*/
668814f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
668914f150ffSMatthew G. Knepley {
6690c26acbdeSMatthew G. Knepley   PetscSection   section;
66912d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
669214f150ffSMatthew G. Knepley   PetscErrorCode ierr;
669314f150ffSMatthew G. Knepley 
669414f150ffSMatthew G. Knepley   PetscFunctionBegin;
669514f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
669614f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
669792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6698c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6699127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
67002d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6701c26acbdeSMatthew G. Knepley     *odm = dm;
6702c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6703c26acbdeSMatthew G. Knepley   }
670414f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6705c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
670614f150ffSMatthew G. Knepley     PetscSF      sf;
670714f150ffSMatthew G. Knepley 
670814f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6709e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
671014f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
671192fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
671214f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
671314f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
671415b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6715e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
671614f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
671714f150ffSMatthew G. Knepley   }
671814f150ffSMatthew G. Knepley   *odm = dm->dmBC;
671914f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
672014f150ffSMatthew G. Knepley }
6721f4d763aaSMatthew G. Knepley 
6722f4d763aaSMatthew G. Knepley /*@
6723cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6724f4d763aaSMatthew G. Knepley 
6725f4d763aaSMatthew G. Knepley   Input Parameter:
6726f4d763aaSMatthew G. Knepley . dm - The original DM
6727f4d763aaSMatthew G. Knepley 
6728cdb7a50dSMatthew G. Knepley   Output Parameters:
6729cdb7a50dSMatthew G. Knepley + num - The output sequence number
6730cdb7a50dSMatthew G. Knepley - val - The output sequence value
6731f4d763aaSMatthew G. Knepley 
6732f4d763aaSMatthew G. Knepley   Level: intermediate
6733f4d763aaSMatthew G. Knepley 
6734f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6735f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6736f4d763aaSMatthew G. Knepley 
6737f4d763aaSMatthew G. Knepley .seealso: VecView()
6738f4d763aaSMatthew G. Knepley @*/
6739cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6740f4d763aaSMatthew G. Knepley {
6741f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6742f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6743534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6744534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6745f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6746f4d763aaSMatthew G. Knepley }
6747f4d763aaSMatthew G. Knepley 
6748f4d763aaSMatthew G. Knepley /*@
6749cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6750f4d763aaSMatthew G. Knepley 
6751f4d763aaSMatthew G. Knepley   Input Parameters:
6752f4d763aaSMatthew G. Knepley + dm - The original DM
6753cdb7a50dSMatthew G. Knepley . num - The output sequence number
6754cdb7a50dSMatthew G. Knepley - val - The output sequence value
6755f4d763aaSMatthew G. Knepley 
6756f4d763aaSMatthew G. Knepley   Level: intermediate
6757f4d763aaSMatthew G. Knepley 
6758f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6759f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6760f4d763aaSMatthew G. Knepley 
6761f4d763aaSMatthew G. Knepley .seealso: VecView()
6762f4d763aaSMatthew G. Knepley @*/
6763cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6764f4d763aaSMatthew G. Knepley {
6765f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6766f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6767f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6768cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6769cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6770cdb7a50dSMatthew G. Knepley }
6771cdb7a50dSMatthew G. Knepley 
6772cdb7a50dSMatthew G. Knepley /*@C
6773cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6774cdb7a50dSMatthew G. Knepley 
6775cdb7a50dSMatthew G. Knepley   Input Parameters:
6776cdb7a50dSMatthew G. Knepley + dm   - The original DM
6777cdb7a50dSMatthew G. Knepley . name - The sequence name
6778cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6779cdb7a50dSMatthew G. Knepley 
6780cdb7a50dSMatthew G. Knepley   Output Parameter:
6781cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6782cdb7a50dSMatthew G. Knepley 
6783cdb7a50dSMatthew G. Knepley   Level: intermediate
6784cdb7a50dSMatthew G. Knepley 
6785cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6786cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6787cdb7a50dSMatthew G. Knepley 
6788cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6789cdb7a50dSMatthew G. Knepley @*/
6790cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6791cdb7a50dSMatthew G. Knepley {
6792cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6793cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6794cdb7a50dSMatthew G. Knepley 
6795cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6796cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6797cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6798534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
6799cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6800cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6801cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6802cdb7a50dSMatthew G. Knepley     PetscScalar value;
6803cdb7a50dSMatthew G. Knepley 
680439d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
68054aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6806cdb7a50dSMatthew G. Knepley #endif
6807cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6808f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6809f4d763aaSMatthew G. Knepley }
68108e4ac7eaSMatthew G. Knepley 
68118e4ac7eaSMatthew G. Knepley /*@
68128e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
68138e4ac7eaSMatthew G. Knepley 
68148e4ac7eaSMatthew G. Knepley   Not collective
68158e4ac7eaSMatthew G. Knepley 
68168e4ac7eaSMatthew G. Knepley   Input Parameter:
68178e4ac7eaSMatthew G. Knepley . dm - The DM
68188e4ac7eaSMatthew G. Knepley 
68198e4ac7eaSMatthew G. Knepley   Output Parameter:
68208e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
68218e4ac7eaSMatthew G. Knepley 
68228e4ac7eaSMatthew G. Knepley   Level: beginner
68238e4ac7eaSMatthew G. Knepley 
68248e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
68258e4ac7eaSMatthew G. Knepley @*/
68268e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
68278e4ac7eaSMatthew G. Knepley {
68288e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
68298e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6830534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
68318e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
68328e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
68338e4ac7eaSMatthew G. Knepley }
68348e4ac7eaSMatthew G. Knepley 
68358e4ac7eaSMatthew G. Knepley /*@
68365d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
68378e4ac7eaSMatthew G. Knepley 
68388e4ac7eaSMatthew G. Knepley   Collective on dm
68398e4ac7eaSMatthew G. Knepley 
68408e4ac7eaSMatthew G. Knepley   Input Parameters:
68418e4ac7eaSMatthew G. Knepley + dm - The DM
68428e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
68438e4ac7eaSMatthew G. Knepley 
68445d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
68455d3b26e6SMatthew G. Knepley 
68468e4ac7eaSMatthew G. Knepley   Level: beginner
68478e4ac7eaSMatthew G. Knepley 
68485d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
68498e4ac7eaSMatthew G. Knepley @*/
68508e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
68518e4ac7eaSMatthew G. Knepley {
68528e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
68538e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68548833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
68558e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
68568e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
68578e4ac7eaSMatthew G. Knepley }
6858c58f1c22SToby Isaac 
6859c58f1c22SToby Isaac 
6860c58f1c22SToby Isaac /*@C
6861c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6862c58f1c22SToby Isaac 
6863c58f1c22SToby Isaac   Not Collective
6864c58f1c22SToby Isaac 
6865c58f1c22SToby Isaac   Input Parameters:
6866c58f1c22SToby Isaac + dm   - The DM object
6867c58f1c22SToby Isaac - name - The label name
6868c58f1c22SToby Isaac 
6869c58f1c22SToby Isaac   Level: intermediate
6870c58f1c22SToby Isaac 
6871c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6872c58f1c22SToby Isaac @*/
6873c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6874c58f1c22SToby Isaac {
68755d80c0bfSVaclav Hapla   PetscBool      flg;
68765d80c0bfSVaclav Hapla   DMLabel        label;
6877c58f1c22SToby Isaac   PetscErrorCode ierr;
6878c58f1c22SToby Isaac 
6879c58f1c22SToby Isaac   PetscFunctionBegin;
6880c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6881c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
68825d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
6883c58f1c22SToby Isaac   if (!flg) {
68845d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
68855d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
68865d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
6887c58f1c22SToby Isaac   }
6888c58f1c22SToby Isaac   PetscFunctionReturn(0);
6889c58f1c22SToby Isaac }
6890c58f1c22SToby Isaac 
6891c58f1c22SToby Isaac /*@C
6892c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6893c58f1c22SToby Isaac 
6894c58f1c22SToby Isaac   Not Collective
6895c58f1c22SToby Isaac 
6896c58f1c22SToby Isaac   Input Parameters:
6897c58f1c22SToby Isaac + dm   - The DM object
6898c58f1c22SToby Isaac . name - The label name
6899c58f1c22SToby Isaac - point - The mesh point
6900c58f1c22SToby Isaac 
6901c58f1c22SToby Isaac   Output Parameter:
6902c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6903c58f1c22SToby Isaac 
6904c58f1c22SToby Isaac   Level: beginner
6905c58f1c22SToby Isaac 
6906c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6907c58f1c22SToby Isaac @*/
6908c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6909c58f1c22SToby Isaac {
6910c58f1c22SToby Isaac   DMLabel        label;
6911c58f1c22SToby Isaac   PetscErrorCode ierr;
6912c58f1c22SToby Isaac 
6913c58f1c22SToby Isaac   PetscFunctionBegin;
6914c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6915c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6916c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
691713903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6918c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6919c58f1c22SToby Isaac   PetscFunctionReturn(0);
6920c58f1c22SToby Isaac }
6921c58f1c22SToby Isaac 
6922c58f1c22SToby Isaac /*@C
6923c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6924c58f1c22SToby Isaac 
6925c58f1c22SToby Isaac   Not Collective
6926c58f1c22SToby Isaac 
6927c58f1c22SToby Isaac   Input Parameters:
6928c58f1c22SToby Isaac + dm   - The DM object
6929c58f1c22SToby Isaac . name - The label name
6930c58f1c22SToby Isaac . point - The mesh point
6931c58f1c22SToby Isaac - value - The label value for this point
6932c58f1c22SToby Isaac 
6933c58f1c22SToby Isaac   Output Parameter:
6934c58f1c22SToby Isaac 
6935c58f1c22SToby Isaac   Level: beginner
6936c58f1c22SToby Isaac 
6937c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6938c58f1c22SToby Isaac @*/
6939c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6940c58f1c22SToby Isaac {
6941c58f1c22SToby Isaac   DMLabel        label;
6942c58f1c22SToby Isaac   PetscErrorCode ierr;
6943c58f1c22SToby Isaac 
6944c58f1c22SToby Isaac   PetscFunctionBegin;
6945c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6946c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6947c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6948c58f1c22SToby Isaac   if (!label) {
6949c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6950c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6951c58f1c22SToby Isaac   }
6952c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6953c58f1c22SToby Isaac   PetscFunctionReturn(0);
6954c58f1c22SToby Isaac }
6955c58f1c22SToby Isaac 
6956c58f1c22SToby Isaac /*@C
6957c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac   Not Collective
6960c58f1c22SToby Isaac 
6961c58f1c22SToby Isaac   Input Parameters:
6962c58f1c22SToby Isaac + dm   - The DM object
6963c58f1c22SToby Isaac . name - The label name
6964c58f1c22SToby Isaac . point - The mesh point
6965c58f1c22SToby Isaac - value - The label value for this point
6966c58f1c22SToby Isaac 
6967c58f1c22SToby Isaac   Output Parameter:
6968c58f1c22SToby Isaac 
6969c58f1c22SToby Isaac   Level: beginner
6970c58f1c22SToby Isaac 
6971c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6972c58f1c22SToby Isaac @*/
6973c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6974c58f1c22SToby Isaac {
6975c58f1c22SToby Isaac   DMLabel        label;
6976c58f1c22SToby Isaac   PetscErrorCode ierr;
6977c58f1c22SToby Isaac 
6978c58f1c22SToby Isaac   PetscFunctionBegin;
6979c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6980c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6981c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6982c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6983c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6984c58f1c22SToby Isaac   PetscFunctionReturn(0);
6985c58f1c22SToby Isaac }
6986c58f1c22SToby Isaac 
6987c58f1c22SToby Isaac /*@C
6988c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6989c58f1c22SToby Isaac 
6990c58f1c22SToby Isaac   Not Collective
6991c58f1c22SToby Isaac 
6992c58f1c22SToby Isaac   Input Parameters:
6993c58f1c22SToby Isaac + dm   - The DM object
6994c58f1c22SToby Isaac - name - The label name
6995c58f1c22SToby Isaac 
6996c58f1c22SToby Isaac   Output Parameter:
6997c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6998c58f1c22SToby Isaac 
6999c58f1c22SToby Isaac   Level: beginner
7000c58f1c22SToby Isaac 
7001df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7002c58f1c22SToby Isaac @*/
7003c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7004c58f1c22SToby Isaac {
7005c58f1c22SToby Isaac   DMLabel        label;
7006c58f1c22SToby Isaac   PetscErrorCode ierr;
7007c58f1c22SToby Isaac 
7008c58f1c22SToby Isaac   PetscFunctionBegin;
7009c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7010c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7011534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7012c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7013c58f1c22SToby Isaac   *size = 0;
7014c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7015c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7016c58f1c22SToby Isaac   PetscFunctionReturn(0);
7017c58f1c22SToby Isaac }
7018c58f1c22SToby Isaac 
7019c58f1c22SToby Isaac /*@C
7020c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7021c58f1c22SToby Isaac 
7022c58f1c22SToby Isaac   Not Collective
7023c58f1c22SToby Isaac 
7024c58f1c22SToby Isaac   Input Parameters:
7025c58f1c22SToby Isaac + mesh - The DM object
7026c58f1c22SToby Isaac - name - The label name
7027c58f1c22SToby Isaac 
7028c58f1c22SToby Isaac   Output Parameter:
7029c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7030c58f1c22SToby Isaac 
7031c58f1c22SToby Isaac   Level: beginner
7032c58f1c22SToby Isaac 
7033c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7034c58f1c22SToby Isaac @*/
7035c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7036c58f1c22SToby Isaac {
7037c58f1c22SToby Isaac   DMLabel        label;
7038c58f1c22SToby Isaac   PetscErrorCode ierr;
7039c58f1c22SToby Isaac 
7040c58f1c22SToby Isaac   PetscFunctionBegin;
7041c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7042c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7043c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7044c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7045c58f1c22SToby Isaac   *ids = NULL;
7046dab2e251SBlaise Bourdin  if (label) {
7047c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7048dab2e251SBlaise Bourdin   } else {
7049dab2e251SBlaise Bourdin     /* returning an empty IS */
7050dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7051dab2e251SBlaise Bourdin   }
7052c58f1c22SToby Isaac   PetscFunctionReturn(0);
7053c58f1c22SToby Isaac }
7054c58f1c22SToby Isaac 
7055c58f1c22SToby Isaac /*@C
7056c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7057c58f1c22SToby Isaac 
7058c58f1c22SToby Isaac   Not Collective
7059c58f1c22SToby Isaac 
7060c58f1c22SToby Isaac   Input Parameters:
7061c58f1c22SToby Isaac + dm - The DM object
7062c58f1c22SToby Isaac . name - The label name
7063c58f1c22SToby Isaac - value - The stratum value
7064c58f1c22SToby Isaac 
7065c58f1c22SToby Isaac   Output Parameter:
7066c58f1c22SToby Isaac . size - The stratum size
7067c58f1c22SToby Isaac 
7068c58f1c22SToby Isaac   Level: beginner
7069c58f1c22SToby Isaac 
7070c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7071c58f1c22SToby Isaac @*/
7072c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7073c58f1c22SToby Isaac {
7074c58f1c22SToby Isaac   DMLabel        label;
7075c58f1c22SToby Isaac   PetscErrorCode ierr;
7076c58f1c22SToby Isaac 
7077c58f1c22SToby Isaac   PetscFunctionBegin;
7078c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7079c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7080534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7081c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7082c58f1c22SToby Isaac   *size = 0;
7083c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7084c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7085c58f1c22SToby Isaac   PetscFunctionReturn(0);
7086c58f1c22SToby Isaac }
7087c58f1c22SToby Isaac 
7088c58f1c22SToby Isaac /*@C
7089c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7090c58f1c22SToby Isaac 
7091c58f1c22SToby Isaac   Not Collective
7092c58f1c22SToby Isaac 
7093c58f1c22SToby Isaac   Input Parameters:
7094c58f1c22SToby Isaac + dm - The DM object
7095c58f1c22SToby Isaac . name - The label name
7096c58f1c22SToby Isaac - value - The stratum value
7097c58f1c22SToby Isaac 
7098c58f1c22SToby Isaac   Output Parameter:
7099c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7100c58f1c22SToby Isaac 
7101c58f1c22SToby Isaac   Level: beginner
7102c58f1c22SToby Isaac 
7103c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7104c58f1c22SToby Isaac @*/
7105c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7106c58f1c22SToby Isaac {
7107c58f1c22SToby Isaac   DMLabel        label;
7108c58f1c22SToby Isaac   PetscErrorCode ierr;
7109c58f1c22SToby Isaac 
7110c58f1c22SToby Isaac   PetscFunctionBegin;
7111c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7112c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7113c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7114c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7115c58f1c22SToby Isaac   *points = NULL;
7116c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7117c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7118c58f1c22SToby Isaac   PetscFunctionReturn(0);
7119c58f1c22SToby Isaac }
7120c58f1c22SToby Isaac 
71214de306b1SToby Isaac /*@C
71229044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
71234de306b1SToby Isaac 
71244de306b1SToby Isaac   Not Collective
71254de306b1SToby Isaac 
71264de306b1SToby Isaac   Input Parameters:
71274de306b1SToby Isaac + dm - The DM object
71284de306b1SToby Isaac . name - The label name
71294de306b1SToby Isaac . value - The stratum value
71304de306b1SToby Isaac - points - The stratum points
71314de306b1SToby Isaac 
71324de306b1SToby Isaac   Level: beginner
71334de306b1SToby Isaac 
71344de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
71354de306b1SToby Isaac @*/
71364de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
71374de306b1SToby Isaac {
71384de306b1SToby Isaac   DMLabel        label;
71394de306b1SToby Isaac   PetscErrorCode ierr;
71404de306b1SToby Isaac 
71414de306b1SToby Isaac   PetscFunctionBegin;
71424de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71434de306b1SToby Isaac   PetscValidCharPointer(name, 2);
71444de306b1SToby Isaac   PetscValidPointer(points, 4);
71454de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
71464de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
71474de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
71484de306b1SToby Isaac   PetscFunctionReturn(0);
71494de306b1SToby Isaac }
71504de306b1SToby Isaac 
7151c58f1c22SToby Isaac /*@C
7152c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7153c58f1c22SToby Isaac 
7154c58f1c22SToby Isaac   Not Collective
7155c58f1c22SToby Isaac 
7156c58f1c22SToby Isaac   Input Parameters:
7157c58f1c22SToby Isaac + dm   - The DM object
7158c58f1c22SToby Isaac . name - The label name
7159c58f1c22SToby Isaac - value - The label value for this point
7160c58f1c22SToby Isaac 
7161c58f1c22SToby Isaac   Output Parameter:
7162c58f1c22SToby Isaac 
7163c58f1c22SToby Isaac   Level: beginner
7164c58f1c22SToby Isaac 
7165c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7166c58f1c22SToby Isaac @*/
7167c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7168c58f1c22SToby Isaac {
7169c58f1c22SToby Isaac   DMLabel        label;
7170c58f1c22SToby Isaac   PetscErrorCode ierr;
7171c58f1c22SToby Isaac 
7172c58f1c22SToby Isaac   PetscFunctionBegin;
7173c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7174c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7175c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7176c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7177c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7178c58f1c22SToby Isaac   PetscFunctionReturn(0);
7179c58f1c22SToby Isaac }
7180c58f1c22SToby Isaac 
7181c58f1c22SToby Isaac /*@
7182c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7183c58f1c22SToby Isaac 
7184c58f1c22SToby Isaac   Not Collective
7185c58f1c22SToby Isaac 
7186c58f1c22SToby Isaac   Input Parameter:
7187c58f1c22SToby Isaac . dm   - The DM object
7188c58f1c22SToby Isaac 
7189c58f1c22SToby Isaac   Output Parameter:
7190c58f1c22SToby Isaac . numLabels - the number of Labels
7191c58f1c22SToby Isaac 
7192c58f1c22SToby Isaac   Level: intermediate
7193c58f1c22SToby Isaac 
7194c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7195c58f1c22SToby Isaac @*/
7196c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7197c58f1c22SToby Isaac {
71985d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7199c58f1c22SToby Isaac   PetscInt  n    = 0;
7200c58f1c22SToby Isaac 
7201c58f1c22SToby Isaac   PetscFunctionBegin;
7202c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7203534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7204c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7205c58f1c22SToby Isaac   *numLabels = n;
7206c58f1c22SToby Isaac   PetscFunctionReturn(0);
7207c58f1c22SToby Isaac }
7208c58f1c22SToby Isaac 
7209c58f1c22SToby Isaac /*@C
7210c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7211c58f1c22SToby Isaac 
7212c58f1c22SToby Isaac   Not Collective
7213c58f1c22SToby Isaac 
7214c58f1c22SToby Isaac   Input Parameters:
7215c58f1c22SToby Isaac + dm - The DM object
7216c58f1c22SToby Isaac - n  - the label number
7217c58f1c22SToby Isaac 
7218c58f1c22SToby Isaac   Output Parameter:
7219c58f1c22SToby Isaac . name - the label name
7220c58f1c22SToby Isaac 
7221c58f1c22SToby Isaac   Level: intermediate
7222c58f1c22SToby Isaac 
7223c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7224c58f1c22SToby Isaac @*/
7225c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7226c58f1c22SToby Isaac {
72275d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7228c58f1c22SToby Isaac   PetscInt       l    = 0;
7229d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7230c58f1c22SToby Isaac 
7231c58f1c22SToby Isaac   PetscFunctionBegin;
7232c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7233c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7234c58f1c22SToby Isaac   while (next) {
7235c58f1c22SToby Isaac     if (l == n) {
7236d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7237c58f1c22SToby Isaac       PetscFunctionReturn(0);
7238c58f1c22SToby Isaac     }
7239c58f1c22SToby Isaac     ++l;
7240c58f1c22SToby Isaac     next = next->next;
7241c58f1c22SToby Isaac   }
7242c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7243c58f1c22SToby Isaac }
7244c58f1c22SToby Isaac 
7245c58f1c22SToby Isaac /*@C
7246c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7247c58f1c22SToby Isaac 
7248c58f1c22SToby Isaac   Not Collective
7249c58f1c22SToby Isaac 
7250c58f1c22SToby Isaac   Input Parameters:
7251c58f1c22SToby Isaac + dm   - The DM object
7252c58f1c22SToby Isaac - name - The label name
7253c58f1c22SToby Isaac 
7254c58f1c22SToby Isaac   Output Parameter:
7255c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7256c58f1c22SToby Isaac 
7257c58f1c22SToby Isaac   Level: intermediate
7258c58f1c22SToby Isaac 
7259c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7260c58f1c22SToby Isaac @*/
7261c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7262c58f1c22SToby Isaac {
72635d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7264d67d17b1SMatthew G. Knepley   const char    *lname;
7265c58f1c22SToby Isaac   PetscErrorCode ierr;
7266c58f1c22SToby Isaac 
7267c58f1c22SToby Isaac   PetscFunctionBegin;
7268c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7269c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7270534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7271c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7272c58f1c22SToby Isaac   while (next) {
7273d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7274d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7275c58f1c22SToby Isaac     if (*hasLabel) break;
7276c58f1c22SToby Isaac     next = next->next;
7277c58f1c22SToby Isaac   }
7278c58f1c22SToby Isaac   PetscFunctionReturn(0);
7279c58f1c22SToby Isaac }
7280c58f1c22SToby Isaac 
7281c58f1c22SToby Isaac /*@C
7282c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7283c58f1c22SToby Isaac 
7284c58f1c22SToby Isaac   Not Collective
7285c58f1c22SToby Isaac 
7286c58f1c22SToby Isaac   Input Parameters:
7287c58f1c22SToby Isaac + dm   - The DM object
7288c58f1c22SToby Isaac - name - The label name
7289c58f1c22SToby Isaac 
7290c58f1c22SToby Isaac   Output Parameter:
7291c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7292c58f1c22SToby Isaac 
7293c58f1c22SToby Isaac   Level: intermediate
7294c58f1c22SToby Isaac 
7295c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7296c58f1c22SToby Isaac @*/
7297c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7298c58f1c22SToby Isaac {
72995d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7300c58f1c22SToby Isaac   PetscBool      hasLabel;
7301d67d17b1SMatthew G. Knepley   const char    *lname;
7302c58f1c22SToby Isaac   PetscErrorCode ierr;
7303c58f1c22SToby Isaac 
7304c58f1c22SToby Isaac   PetscFunctionBegin;
7305c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7306c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7307c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7308c58f1c22SToby Isaac   *label = NULL;
7309c58f1c22SToby Isaac   while (next) {
7310d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7311d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7312c58f1c22SToby Isaac     if (hasLabel) {
7313c58f1c22SToby Isaac       *label = next->label;
7314c58f1c22SToby Isaac       break;
7315c58f1c22SToby Isaac     }
7316c58f1c22SToby Isaac     next = next->next;
7317c58f1c22SToby Isaac   }
7318c58f1c22SToby Isaac   PetscFunctionReturn(0);
7319c58f1c22SToby Isaac }
7320c58f1c22SToby Isaac 
7321c58f1c22SToby Isaac /*@C
7322c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7323c58f1c22SToby Isaac 
7324c58f1c22SToby Isaac   Not Collective
7325c58f1c22SToby Isaac 
7326c58f1c22SToby Isaac   Input Parameters:
7327c58f1c22SToby Isaac + dm - The DM object
7328c58f1c22SToby Isaac - n  - the label number
7329c58f1c22SToby Isaac 
7330c58f1c22SToby Isaac   Output Parameter:
7331c58f1c22SToby Isaac . label - the label
7332c58f1c22SToby Isaac 
7333c58f1c22SToby Isaac   Level: intermediate
7334c58f1c22SToby Isaac 
7335c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7336c58f1c22SToby Isaac @*/
7337c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7338c58f1c22SToby Isaac {
73395d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7340c58f1c22SToby Isaac   PetscInt    l    = 0;
7341c58f1c22SToby Isaac 
7342c58f1c22SToby Isaac   PetscFunctionBegin;
7343c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7344c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7345c58f1c22SToby Isaac   while (next) {
7346c58f1c22SToby Isaac     if (l == n) {
7347c58f1c22SToby Isaac       *label = next->label;
7348c58f1c22SToby Isaac       PetscFunctionReturn(0);
7349c58f1c22SToby Isaac     }
7350c58f1c22SToby Isaac     ++l;
7351c58f1c22SToby Isaac     next = next->next;
7352c58f1c22SToby Isaac   }
7353c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7354c58f1c22SToby Isaac }
7355c58f1c22SToby Isaac 
7356c58f1c22SToby Isaac /*@C
7357c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7358c58f1c22SToby Isaac 
7359c58f1c22SToby Isaac   Not Collective
7360c58f1c22SToby Isaac 
7361c58f1c22SToby Isaac   Input Parameters:
7362c58f1c22SToby Isaac + dm   - The DM object
7363c58f1c22SToby Isaac - label - The DMLabel
7364c58f1c22SToby Isaac 
7365c58f1c22SToby Isaac   Level: developer
7366c58f1c22SToby Isaac 
7367c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7368c58f1c22SToby Isaac @*/
7369c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7370c58f1c22SToby Isaac {
73715d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7372c58f1c22SToby Isaac   PetscBool      hasLabel;
7373d67d17b1SMatthew G. Knepley   const char    *lname;
73745d80c0bfSVaclav Hapla   PetscBool      flg;
7375c58f1c22SToby Isaac   PetscErrorCode ierr;
7376c58f1c22SToby Isaac 
7377c58f1c22SToby Isaac   PetscFunctionBegin;
7378c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7379d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7380d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7381d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7382c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7383c58f1c22SToby Isaac   tmpLabel->label  = label;
7384c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
73855d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
73865d80c0bfSVaclav Hapla   *p = tmpLabel;
738708f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
73885d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
73895d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7390ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7391ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7392c58f1c22SToby Isaac   PetscFunctionReturn(0);
7393c58f1c22SToby Isaac }
7394c58f1c22SToby Isaac 
7395c58f1c22SToby Isaac /*@C
7396e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7397c58f1c22SToby Isaac 
7398c58f1c22SToby Isaac   Not Collective
7399c58f1c22SToby Isaac 
7400c58f1c22SToby Isaac   Input Parameters:
7401c58f1c22SToby Isaac + dm   - The DM object
7402c58f1c22SToby Isaac - name - The label name
7403c58f1c22SToby Isaac 
7404c58f1c22SToby Isaac   Output Parameter:
7405c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7406c58f1c22SToby Isaac 
7407c58f1c22SToby Isaac   Level: developer
7408c58f1c22SToby Isaac 
7409e5472504SVaclav Hapla   Notes:
7410e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7411e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7412e5472504SVaclav Hapla 
7413e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7414e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7415e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7416e5472504SVaclav Hapla 
7417e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7418c58f1c22SToby Isaac @*/
7419c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7420c58f1c22SToby Isaac {
742195d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7422c58f1c22SToby Isaac   PetscBool      hasLabel;
7423d67d17b1SMatthew G. Knepley   const char    *lname;
7424c58f1c22SToby Isaac   PetscErrorCode ierr;
7425c58f1c22SToby Isaac 
7426c58f1c22SToby Isaac   PetscFunctionBegin;
7427c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7428e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7429e5472504SVaclav Hapla   if (label) {
7430e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7431c58f1c22SToby Isaac     *label = NULL;
7432e5472504SVaclav Hapla   }
74335d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
743495d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7435d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7436c58f1c22SToby Isaac     if (hasLabel) {
743795d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7438c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
743995d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7440ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7441ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
744295d578d6SVaclav Hapla       if (label) *label = link->label;
744395d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
744495d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7445c58f1c22SToby Isaac       break;
7446c58f1c22SToby Isaac     }
7447c58f1c22SToby Isaac   }
7448c58f1c22SToby Isaac   PetscFunctionReturn(0);
7449c58f1c22SToby Isaac }
7450c58f1c22SToby Isaac 
7451306894acSVaclav Hapla /*@
7452306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7453306894acSVaclav Hapla 
7454306894acSVaclav Hapla   Not Collective
7455306894acSVaclav Hapla 
7456306894acSVaclav Hapla   Input Parameters:
7457306894acSVaclav Hapla + dm   - The DM object
7458306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7459306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7460306894acSVaclav Hapla 
7461306894acSVaclav Hapla   Level: developer
7462306894acSVaclav Hapla 
7463306894acSVaclav Hapla   Notes:
7464306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7465306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7466306894acSVaclav Hapla   *label nullified.
7467306894acSVaclav Hapla 
7468306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7469306894acSVaclav Hapla @*/
7470306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7471306894acSVaclav Hapla {
747243e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7473306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7474306894acSVaclav Hapla   PetscErrorCode ierr;
7475306894acSVaclav Hapla 
7476306894acSVaclav Hapla   PetscFunctionBegin;
7477306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7478306894acSVaclav Hapla   PetscValidPointer(label, 2);
7479f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7480306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7481306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
74825d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
748343e45a93SVaclav Hapla     if (*label == link->label) {
7484306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
748543e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7486306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7487ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
748843e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
748943e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
749043e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7491306894acSVaclav Hapla       break;
7492306894acSVaclav Hapla     }
7493306894acSVaclav Hapla   }
7494306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7495306894acSVaclav Hapla   PetscFunctionReturn(0);
7496306894acSVaclav Hapla }
7497306894acSVaclav Hapla 
7498c58f1c22SToby Isaac /*@C
7499c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7500c58f1c22SToby Isaac 
7501c58f1c22SToby Isaac   Not Collective
7502c58f1c22SToby Isaac 
7503c58f1c22SToby Isaac   Input Parameters:
7504c58f1c22SToby Isaac + dm   - The DM object
7505c58f1c22SToby Isaac - name - The label name
7506c58f1c22SToby Isaac 
7507c58f1c22SToby Isaac   Output Parameter:
7508c58f1c22SToby Isaac . output - The flag for output
7509c58f1c22SToby Isaac 
7510c58f1c22SToby Isaac   Level: developer
7511c58f1c22SToby Isaac 
7512c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7513c58f1c22SToby Isaac @*/
7514c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7515c58f1c22SToby Isaac {
75165d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7517d67d17b1SMatthew G. Knepley   const char    *lname;
7518c58f1c22SToby Isaac   PetscErrorCode ierr;
7519c58f1c22SToby Isaac 
7520c58f1c22SToby Isaac   PetscFunctionBegin;
7521c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7522c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7523c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7524c58f1c22SToby Isaac   while (next) {
7525c58f1c22SToby Isaac     PetscBool flg;
7526c58f1c22SToby Isaac 
7527d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7528d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7529c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7530c58f1c22SToby Isaac     next = next->next;
7531c58f1c22SToby Isaac   }
7532c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7533c58f1c22SToby Isaac }
7534c58f1c22SToby Isaac 
7535c58f1c22SToby Isaac /*@C
7536c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7537c58f1c22SToby Isaac 
7538c58f1c22SToby Isaac   Not Collective
7539c58f1c22SToby Isaac 
7540c58f1c22SToby Isaac   Input Parameters:
7541c58f1c22SToby Isaac + dm     - The DM object
7542c58f1c22SToby Isaac . name   - The label name
7543c58f1c22SToby Isaac - output - The flag for output
7544c58f1c22SToby Isaac 
7545c58f1c22SToby Isaac   Level: developer
7546c58f1c22SToby Isaac 
7547c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7548c58f1c22SToby Isaac @*/
7549c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7550c58f1c22SToby Isaac {
75515d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7552d67d17b1SMatthew G. Knepley   const char    *lname;
7553c58f1c22SToby Isaac   PetscErrorCode ierr;
7554c58f1c22SToby Isaac 
7555c58f1c22SToby Isaac   PetscFunctionBegin;
7556c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7557534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7558c58f1c22SToby Isaac   while (next) {
7559c58f1c22SToby Isaac     PetscBool flg;
7560c58f1c22SToby Isaac 
7561d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7562d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7563c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7564c58f1c22SToby Isaac     next = next->next;
7565c58f1c22SToby Isaac   }
7566c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7567c58f1c22SToby Isaac }
7568c58f1c22SToby Isaac 
7569c58f1c22SToby Isaac /*@
7570c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7571c58f1c22SToby Isaac 
7572d083f849SBarry Smith   Collective on dmA
7573c58f1c22SToby Isaac 
7574c58f1c22SToby Isaac   Input Parameter:
75755d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
75762e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
75775d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7578ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7579c58f1c22SToby Isaac 
7580c58f1c22SToby Isaac   Level: intermediate
7581c58f1c22SToby Isaac 
7582c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7583c58f1c22SToby Isaac 
75845d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7585c58f1c22SToby Isaac @*/
75865d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7587c58f1c22SToby Isaac {
7588c58f1c22SToby Isaac   DMLabel        label, labelNew;
7589c58f1c22SToby Isaac   const char    *name;
7590c58f1c22SToby Isaac   PetscBool      flg;
75915d80c0bfSVaclav Hapla   DMLabelLink    link;
75925d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7593c58f1c22SToby Isaac 
75945d80c0bfSVaclav Hapla   PetscFunctionBegin;
75955d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
75965d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
75975d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
75985d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
75995d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
76005d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
76015d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
76025d80c0bfSVaclav Hapla     label=link->label;
76035d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
76045d80c0bfSVaclav Hapla     if (!all) {
7605c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7606c58f1c22SToby Isaac       if (flg) continue;
76077d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
76087d5acc75SStefano Zampini       if (flg) continue;
7609ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7610ba2698f1SMatthew G. Knepley       if (flg) continue;
76115d80c0bfSVaclav Hapla     }
76125d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7613c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
76145d80c0bfSVaclav Hapla     } else {
76155d80c0bfSVaclav Hapla       labelNew = label;
76165d80c0bfSVaclav Hapla     }
7617c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
76185d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7619c58f1c22SToby Isaac   }
7620c58f1c22SToby Isaac   PetscFunctionReturn(0);
7621c58f1c22SToby Isaac }
7622a8fb8f29SToby Isaac 
7623a8fb8f29SToby Isaac /*@
7624a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7625a8fb8f29SToby Isaac 
7626a8fb8f29SToby Isaac   Input Parameter:
7627a8fb8f29SToby Isaac . dm - The DM object
7628a8fb8f29SToby Isaac 
7629a8fb8f29SToby Isaac   Output Parameter:
7630a8fb8f29SToby Isaac . cdm - The coarse DM
7631a8fb8f29SToby Isaac 
7632a8fb8f29SToby Isaac   Level: intermediate
7633a8fb8f29SToby Isaac 
7634a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7635a8fb8f29SToby Isaac @*/
7636a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7637a8fb8f29SToby Isaac {
7638a8fb8f29SToby Isaac   PetscFunctionBegin;
7639a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7640a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7641a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7642a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7643a8fb8f29SToby Isaac }
7644a8fb8f29SToby Isaac 
7645a8fb8f29SToby Isaac /*@
7646a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7647a8fb8f29SToby Isaac 
7648a8fb8f29SToby Isaac   Input Parameters:
7649a8fb8f29SToby Isaac + dm - The DM object
7650a8fb8f29SToby Isaac - cdm - The coarse DM
7651a8fb8f29SToby Isaac 
7652a8fb8f29SToby Isaac   Level: intermediate
7653a8fb8f29SToby Isaac 
7654a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7655a8fb8f29SToby Isaac @*/
7656a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7657a8fb8f29SToby Isaac {
7658a8fb8f29SToby Isaac   PetscErrorCode ierr;
7659a8fb8f29SToby Isaac 
7660a8fb8f29SToby Isaac   PetscFunctionBegin;
7661a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7662a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7663a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7664a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7665a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7666a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7667a8fb8f29SToby Isaac }
7668a8fb8f29SToby Isaac 
766988bdff64SToby Isaac /*@
767088bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
767188bdff64SToby Isaac 
767288bdff64SToby Isaac   Input Parameter:
767388bdff64SToby Isaac . dm - The DM object
767488bdff64SToby Isaac 
767588bdff64SToby Isaac   Output Parameter:
767688bdff64SToby Isaac . fdm - The fine DM
767788bdff64SToby Isaac 
767888bdff64SToby Isaac   Level: intermediate
767988bdff64SToby Isaac 
768088bdff64SToby Isaac .seealso: DMSetFineDM()
768188bdff64SToby Isaac @*/
768288bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
768388bdff64SToby Isaac {
768488bdff64SToby Isaac   PetscFunctionBegin;
768588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
768688bdff64SToby Isaac   PetscValidPointer(fdm, 2);
768788bdff64SToby Isaac   *fdm = dm->fineMesh;
768888bdff64SToby Isaac   PetscFunctionReturn(0);
768988bdff64SToby Isaac }
769088bdff64SToby Isaac 
769188bdff64SToby Isaac /*@
769288bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
769388bdff64SToby Isaac 
769488bdff64SToby Isaac   Input Parameters:
769588bdff64SToby Isaac + dm - The DM object
769688bdff64SToby Isaac - fdm - The fine DM
769788bdff64SToby Isaac 
769888bdff64SToby Isaac   Level: intermediate
769988bdff64SToby Isaac 
770088bdff64SToby Isaac .seealso: DMGetFineDM()
770188bdff64SToby Isaac @*/
770288bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
770388bdff64SToby Isaac {
770488bdff64SToby Isaac   PetscErrorCode ierr;
770588bdff64SToby Isaac 
770688bdff64SToby Isaac   PetscFunctionBegin;
770788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
770888bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
770988bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
771088bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
771188bdff64SToby Isaac   dm->fineMesh = fdm;
771288bdff64SToby Isaac   PetscFunctionReturn(0);
771388bdff64SToby Isaac }
771488bdff64SToby Isaac 
7715a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7716a6ba4734SToby Isaac 
7717a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7718a6ba4734SToby Isaac {
7719e5e52638SMatthew G. Knepley   PetscInt       d;
7720a6ba4734SToby Isaac   PetscErrorCode ierr;
7721a6ba4734SToby Isaac 
7722a6ba4734SToby Isaac   PetscFunctionBegin;
7723e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7724e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7725e5e52638SMatthew G. Knepley   }
7726a6ba4734SToby Isaac   PetscFunctionReturn(0);
7727a6ba4734SToby Isaac }
7728a6ba4734SToby Isaac 
7729a6ba4734SToby Isaac /*@C
7730a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7731a6ba4734SToby Isaac 
7732783e2ec8SMatthew G. Knepley   Collective on dm
7733783e2ec8SMatthew G. Knepley 
7734a6ba4734SToby Isaac   Input Parameters:
77354c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7736f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7737a6ba4734SToby Isaac . name        - The BC name
7738a6ba4734SToby Isaac . labelname   - The label defining constrained points
7739a6ba4734SToby Isaac . field       - The field to constrain
7740e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7741a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7742a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7743a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7744a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7745a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7746a6ba4734SToby Isaac 
7747a6ba4734SToby Isaac   Options Database Keys:
7748a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7749a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7750a6ba4734SToby Isaac 
7751a6ba4734SToby Isaac   Level: developer
7752a6ba4734SToby Isaac 
7753a6ba4734SToby Isaac .seealso: DMGetBoundary()
7754a6ba4734SToby Isaac @*/
7755a30ec4eaSSatish 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)
7756a6ba4734SToby Isaac {
7757e5e52638SMatthew G. Knepley   PetscDS        ds;
7758a6ba4734SToby Isaac   PetscErrorCode ierr;
7759a6ba4734SToby Isaac 
7760a6ba4734SToby Isaac   PetscFunctionBegin;
7761a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7762783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
7763783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 5);
7764783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numcomps, 6);
7765783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numids, 9);
7766e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7767783e2ec8SMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, labelname);CHKERRQ(ierr);
7768e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7769a6ba4734SToby Isaac   PetscFunctionReturn(0);
7770a6ba4734SToby Isaac }
7771a6ba4734SToby Isaac 
7772a6ba4734SToby Isaac /*@
7773a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7774a6ba4734SToby Isaac 
7775a6ba4734SToby Isaac   Input Parameters:
7776a6ba4734SToby Isaac . dm - The mesh object
7777a6ba4734SToby Isaac 
7778a6ba4734SToby Isaac   Output Parameters:
7779a6ba4734SToby Isaac . numBd - The number of BC
7780a6ba4734SToby Isaac 
7781a6ba4734SToby Isaac   Level: intermediate
7782a6ba4734SToby Isaac 
7783a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7784a6ba4734SToby Isaac @*/
7785a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7786a6ba4734SToby Isaac {
7787e5e52638SMatthew G. Knepley   PetscDS        ds;
778858ebd649SToby Isaac   PetscErrorCode ierr;
7789a6ba4734SToby Isaac 
7790a6ba4734SToby Isaac   PetscFunctionBegin;
7791a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7792e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7793e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7794a6ba4734SToby Isaac   PetscFunctionReturn(0);
7795a6ba4734SToby Isaac }
7796a6ba4734SToby Isaac 
7797a6ba4734SToby Isaac /*@C
77981c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7799a6ba4734SToby Isaac 
7800a6ba4734SToby Isaac   Input Parameters:
7801a6ba4734SToby Isaac + dm          - The mesh object
7802a6ba4734SToby Isaac - bd          - The BC number
7803a6ba4734SToby Isaac 
7804a6ba4734SToby Isaac   Output Parameters:
7805f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7806a6ba4734SToby Isaac . name        - The BC name
7807a6ba4734SToby Isaac . labelname   - The label defining constrained points
7808a6ba4734SToby Isaac . field       - The field to constrain
7809a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7810a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7811a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7812a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7813a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7814a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7815a6ba4734SToby Isaac 
7816a6ba4734SToby Isaac   Options Database Keys:
7817a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7818a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7819a6ba4734SToby Isaac 
7820a6ba4734SToby Isaac   Level: developer
7821a6ba4734SToby Isaac 
7822a6ba4734SToby Isaac .seealso: DMAddBoundary()
7823a6ba4734SToby Isaac @*/
7824a30ec4eaSSatish 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)
7825a6ba4734SToby Isaac {
7826e5e52638SMatthew G. Knepley   PetscDS        ds;
782758ebd649SToby Isaac   PetscErrorCode ierr;
7828a6ba4734SToby Isaac 
7829a6ba4734SToby Isaac   PetscFunctionBegin;
7830a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7831e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7832e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7833a6ba4734SToby Isaac   PetscFunctionReturn(0);
7834a6ba4734SToby Isaac }
7835a6ba4734SToby Isaac 
7836e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7837e6f8dbb6SToby Isaac {
7838e5e52638SMatthew G. Knepley   PetscDS        ds;
7839dff059c6SToby Isaac   DMBoundary    *lastnext;
7840e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7841e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7842e6f8dbb6SToby Isaac 
7843e6f8dbb6SToby Isaac   PetscFunctionBegin;
7844e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7845e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
784647a1f5adSToby Isaac   if (dm->boundary) {
784747a1f5adSToby Isaac     DMBoundary next = dm->boundary;
784847a1f5adSToby Isaac 
784947a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
785047a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
785147a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
785247a1f5adSToby Isaac     while (next) {
785347a1f5adSToby Isaac       DMBoundary b = next;
785447a1f5adSToby Isaac 
785547a1f5adSToby Isaac       next = b->next;
785647a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7857a6ba4734SToby Isaac     }
785847a1f5adSToby Isaac     dm->boundary = NULL;
7859a6ba4734SToby Isaac   }
786047a1f5adSToby Isaac 
7861dff059c6SToby Isaac   lastnext = &(dm->boundary);
7862e6f8dbb6SToby Isaac   while (dsbound) {
7863e6f8dbb6SToby Isaac     DMBoundary dmbound;
7864e6f8dbb6SToby Isaac 
7865e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7866e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7867e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7868994fe344SLisandro Dalcin     if (!dmbound->label) {ierr = PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);}
786947a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7870dff059c6SToby Isaac     *lastnext = dmbound;
7871dff059c6SToby Isaac     lastnext = &(dmbound->next);
7872dff059c6SToby Isaac     dsbound = dsbound->next;
7873a6ba4734SToby Isaac   }
7874a6ba4734SToby Isaac   PetscFunctionReturn(0);
7875a6ba4734SToby Isaac }
7876a6ba4734SToby Isaac 
7877a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7878a6ba4734SToby Isaac {
7879b95f2879SToby Isaac   DMBoundary     b;
7880a6ba4734SToby Isaac   PetscErrorCode ierr;
7881a6ba4734SToby Isaac 
7882a6ba4734SToby Isaac   PetscFunctionBegin;
7883a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7884534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7885a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7886e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7887b95f2879SToby Isaac   b = dm->boundary;
7888a6ba4734SToby Isaac   while (b && !(*isBd)) {
7889e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7890e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
78913424c85cSToby Isaac 
78923424c85cSToby Isaac     if (label) {
7893a6ba4734SToby Isaac       PetscInt i;
7894a6ba4734SToby Isaac 
7895e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7896e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7897a6ba4734SToby Isaac       }
7898a6ba4734SToby Isaac     }
7899a6ba4734SToby Isaac     b = b->next;
7900a6ba4734SToby Isaac   }
7901a6ba4734SToby Isaac   PetscFunctionReturn(0);
7902a6ba4734SToby Isaac }
79034d6f44ffSToby Isaac 
79044d6f44ffSToby Isaac /*@C
7905a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
7906a6e0b375SMatthew G. Knepley 
7907a6e0b375SMatthew G. Knepley   Collective on DM
79084d6f44ffSToby Isaac 
79094d6f44ffSToby Isaac   Input Parameters:
79104d6f44ffSToby Isaac + dm      - The DM
79110709b2feSToby Isaac . time    - The time
79124d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
79134d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
79144d6f44ffSToby Isaac - mode    - The insertion mode for values
79154d6f44ffSToby Isaac 
79164d6f44ffSToby Isaac   Output Parameter:
79174d6f44ffSToby Isaac . X - vector
79184d6f44ffSToby Isaac 
79194d6f44ffSToby Isaac    Calling sequence of func:
79200709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
79214d6f44ffSToby Isaac 
79224d6f44ffSToby Isaac +  dim - The spatial dimension
79234d6f44ffSToby Isaac .  x   - The coordinates
79244d6f44ffSToby Isaac .  Nf  - The number of fields
79254d6f44ffSToby Isaac .  u   - The output field values
79264d6f44ffSToby Isaac -  ctx - optional user-defined function context
79274d6f44ffSToby Isaac 
79284d6f44ffSToby Isaac   Level: developer
79294d6f44ffSToby Isaac 
7930a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
79314d6f44ffSToby Isaac @*/
79320709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
79334d6f44ffSToby Isaac {
79344d6f44ffSToby Isaac   Vec            localX;
79354d6f44ffSToby Isaac   PetscErrorCode ierr;
79364d6f44ffSToby Isaac 
79374d6f44ffSToby Isaac   PetscFunctionBegin;
79384d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79394d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
79400709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
79414d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
79424d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
79434d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
79444d6f44ffSToby Isaac   PetscFunctionReturn(0);
79454d6f44ffSToby Isaac }
79464d6f44ffSToby Isaac 
7947a6e0b375SMatthew G. Knepley /*@C
7948a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
7949a6e0b375SMatthew G. Knepley 
7950a6e0b375SMatthew G. Knepley   Not collective
7951a6e0b375SMatthew G. Knepley 
7952a6e0b375SMatthew G. Knepley   Input Parameters:
7953a6e0b375SMatthew G. Knepley + dm      - The DM
7954a6e0b375SMatthew G. Knepley . time    - The time
7955a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7956a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7957a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7958a6e0b375SMatthew G. Knepley 
7959a6e0b375SMatthew G. Knepley   Output Parameter:
7960a6e0b375SMatthew G. Knepley . localX - vector
7961a6e0b375SMatthew G. Knepley 
7962a6e0b375SMatthew G. Knepley    Calling sequence of func:
7963a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7964a6e0b375SMatthew G. Knepley 
7965a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7966a6e0b375SMatthew G. Knepley .  x   - The coordinates
7967a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
7968a6e0b375SMatthew G. Knepley .  u   - The output field values
7969a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7970a6e0b375SMatthew G. Knepley 
7971a6e0b375SMatthew G. Knepley   Level: developer
7972a6e0b375SMatthew G. Knepley 
7973a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
7974a6e0b375SMatthew G. Knepley @*/
79750709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
79764d6f44ffSToby Isaac {
79774d6f44ffSToby Isaac   PetscErrorCode ierr;
79784d6f44ffSToby Isaac 
79794d6f44ffSToby Isaac   PetscFunctionBegin;
79804d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79814d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
79820918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
79830709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
79844d6f44ffSToby Isaac   PetscFunctionReturn(0);
79854d6f44ffSToby Isaac }
79864d6f44ffSToby Isaac 
7987a6e0b375SMatthew G. Knepley /*@C
7988a6e0b375SMatthew G. Knepley   DMProjectFunctionLabel - This projects the given function into the function space provided, putting the coefficients in a global vector, setting values only for points in the given label.
7989a6e0b375SMatthew G. Knepley 
7990a6e0b375SMatthew G. Knepley   Collective on DM
7991a6e0b375SMatthew G. Knepley 
7992a6e0b375SMatthew G. Knepley   Input Parameters:
7993a6e0b375SMatthew G. Knepley + dm      - The DM
7994a6e0b375SMatthew G. Knepley . time    - The time
7995a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
7996a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7997a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7998a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7999a6e0b375SMatthew G. Knepley 
8000a6e0b375SMatthew G. Knepley   Output Parameter:
8001a6e0b375SMatthew G. Knepley . X - vector
8002a6e0b375SMatthew G. Knepley 
8003a6e0b375SMatthew G. Knepley    Calling sequence of func:
8004a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8005a6e0b375SMatthew G. Knepley 
8006a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8007a6e0b375SMatthew G. Knepley .  x   - The coordinates
8008a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8009a6e0b375SMatthew G. Knepley .  u   - The output field values
8010a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8011a6e0b375SMatthew G. Knepley 
8012a6e0b375SMatthew G. Knepley   Level: developer
8013a6e0b375SMatthew G. Knepley 
8014a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8015a6e0b375SMatthew G. Knepley @*/
80162c53366bSMatthew 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)
80172c53366bSMatthew G. Knepley {
80182c53366bSMatthew G. Knepley   Vec            localX;
80192c53366bSMatthew G. Knepley   PetscErrorCode ierr;
80202c53366bSMatthew G. Knepley 
80212c53366bSMatthew G. Knepley   PetscFunctionBegin;
80222c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80232c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
80242c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
80252c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
80262c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
80272c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
80282c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
80292c53366bSMatthew G. Knepley }
80302c53366bSMatthew G. Knepley 
8031a6e0b375SMatthew G. Knepley /*@C
8032a6e0b375SMatthew G. Knepley   DMProjectFunctionLabelLocal - This projects the given function into the function space provided, putting the coefficients in a local vector, setting values only for points in the given label.
8033a6e0b375SMatthew G. Knepley 
8034a6e0b375SMatthew G. Knepley   Not collective
8035a6e0b375SMatthew G. Knepley 
8036a6e0b375SMatthew G. Knepley   Input Parameters:
8037a6e0b375SMatthew G. Knepley + dm      - The DM
8038a6e0b375SMatthew G. Knepley . time    - The time
8039a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8040a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8041a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8042a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8043a6e0b375SMatthew G. Knepley 
8044a6e0b375SMatthew G. Knepley   Output Parameter:
8045a6e0b375SMatthew G. Knepley . localX - vector
8046a6e0b375SMatthew G. Knepley 
8047a6e0b375SMatthew G. Knepley    Calling sequence of func:
8048a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8049a6e0b375SMatthew G. Knepley 
8050a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8051a6e0b375SMatthew G. Knepley .  x   - The coordinates
8052a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8053a6e0b375SMatthew G. Knepley .  u   - The output field values
8054a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8055a6e0b375SMatthew G. Knepley 
8056a6e0b375SMatthew G. Knepley   Level: developer
8057a6e0b375SMatthew G. Knepley 
8058a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8059a6e0b375SMatthew G. Knepley @*/
80601c531cf8SMatthew 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)
80614d6f44ffSToby Isaac {
80624d6f44ffSToby Isaac   PetscErrorCode ierr;
80634d6f44ffSToby Isaac 
80644d6f44ffSToby Isaac   PetscFunctionBegin;
80654d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80664d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
80670918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
80681c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
80694d6f44ffSToby Isaac   PetscFunctionReturn(0);
80704d6f44ffSToby Isaac }
80712716604bSToby Isaac 
8072a6e0b375SMatthew G. Knepley /*@C
8073a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8074a6e0b375SMatthew G. Knepley 
8075a6e0b375SMatthew G. Knepley   Not collective
8076a6e0b375SMatthew G. Knepley 
8077a6e0b375SMatthew G. Knepley   Input Parameters:
8078a6e0b375SMatthew G. Knepley + dm      - The DM
8079a6e0b375SMatthew G. Knepley . time    - The time
8080a6e0b375SMatthew G. Knepley . localU  - The input field vector
8081a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8082a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8083a6e0b375SMatthew G. Knepley 
8084a6e0b375SMatthew G. Knepley   Output Parameter:
8085a6e0b375SMatthew G. Knepley . localX  - The output vector
8086a6e0b375SMatthew G. Knepley 
8087a6e0b375SMatthew G. Knepley    Calling sequence of func:
8088a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8089a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8090a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8091a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8092a6e0b375SMatthew G. Knepley 
8093a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8094a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8095a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8096a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8097a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8098a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8099a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8100a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8101a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8102a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8103a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8104a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8105a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8106a6e0b375SMatthew G. Knepley .  t            - The current time
8107a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8108a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8109a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8110a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8111a6e0b375SMatthew G. Knepley 
8112a6e0b375SMatthew G. Knepley   Note: There are three different DMs that potentially interact in this function. The output DM, dm, specifies the layout of the values calculates by funcs.
8113a6e0b375SMatthew G. Knepley   The input DM, attached to U, may be different. For example, you can input the solution over the full domain, but output over a piece of the boundary, or
8114a6e0b375SMatthew G. Knepley   a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary DM, attached to the
8115a6e0b375SMatthew G. Knepley   auxiliary field vector, which is attached to dm, can also be different. It can have a different topology, number of fields, and discretizations.
8116a6e0b375SMatthew G. Knepley 
8117a6e0b375SMatthew G. Knepley   Level: intermediate
8118a6e0b375SMatthew G. Knepley 
8119a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8120a6e0b375SMatthew G. Knepley @*/
81218c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
81228c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
81238c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
81248c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8125191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
81268c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
81278c6c5593SMatthew G. Knepley {
81288c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
81298c6c5593SMatthew G. Knepley 
81308c6c5593SMatthew G. Knepley   PetscFunctionBegin;
81318c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81328c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
81338c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
81340918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
81358c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
81368c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
81378c6c5593SMatthew G. Knepley }
81388c6c5593SMatthew G. Knepley 
8139a6e0b375SMatthew G. Knepley /*@C
8140a6e0b375SMatthew G. Knepley   DMProjectFieldLabelLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector, calculating only over the portion of the domain specified by the label.
8141a6e0b375SMatthew G. Knepley 
8142a6e0b375SMatthew G. Knepley   Not collective
8143a6e0b375SMatthew G. Knepley 
8144a6e0b375SMatthew G. Knepley   Input Parameters:
8145a6e0b375SMatthew G. Knepley + dm      - The DM
8146a6e0b375SMatthew G. Knepley . time    - The time
8147a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
8148a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8149a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8150a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8151a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8152a6e0b375SMatthew G. Knepley . localU  - The input field vector
8153a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8154a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8155a6e0b375SMatthew G. Knepley 
8156a6e0b375SMatthew G. Knepley   Output Parameter:
8157a6e0b375SMatthew G. Knepley . localX  - The output vector
8158a6e0b375SMatthew G. Knepley 
8159a6e0b375SMatthew G. Knepley    Calling sequence of func:
8160a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8161a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8162a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8163a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8164a6e0b375SMatthew G. Knepley 
8165a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8166a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8167a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8168a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8169a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8170a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8171a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8172a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8173a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8174a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8175a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8176a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8177a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8178a6e0b375SMatthew G. Knepley .  t            - The current time
8179a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8180a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8181a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8182a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8183a6e0b375SMatthew G. Knepley 
8184a6e0b375SMatthew G. Knepley   Note: There are three different DMs that potentially interact in this function. The output DM, dm, specifies the layout of the values calculates by funcs.
8185a6e0b375SMatthew G. Knepley   The input DM, attached to U, may be different. For example, you can input the solution over the full domain, but output over a piece of the boundary, or
8186a6e0b375SMatthew G. Knepley   a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary DM, attached to the
8187a6e0b375SMatthew G. Knepley   auxiliary field vector, which is attached to dm, can also be different. It can have a different topology, number of fields, and discretizations.
8188a6e0b375SMatthew G. Knepley 
8189a6e0b375SMatthew G. Knepley   Level: intermediate
8190a6e0b375SMatthew G. Knepley 
8191a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8192a6e0b375SMatthew G. Knepley @*/
81931c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
81948c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
81958c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
81968c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8197191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
81988c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
81998c6c5593SMatthew G. Knepley {
82008c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
82018c6c5593SMatthew G. Knepley 
82028c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82038c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82048c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
82058c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8206ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
82071c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
82088c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
82098c6c5593SMatthew G. Knepley }
82108c6c5593SMatthew G. Knepley 
82112716604bSToby Isaac /*@C
8212ece3a9fcSMatthew G. Knepley   DMProjectBdFieldLabelLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector, calculating only over the portion of the domain boundary specified by the label.
8213ece3a9fcSMatthew G. Knepley 
8214ece3a9fcSMatthew G. Knepley   Not collective
8215ece3a9fcSMatthew G. Knepley 
8216ece3a9fcSMatthew G. Knepley   Input Parameters:
8217ece3a9fcSMatthew G. Knepley + dm      - The DM
8218ece3a9fcSMatthew G. Knepley . time    - The time
8219ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
8220ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8221ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8222ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8223ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8224ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8225ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8226ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8227ece3a9fcSMatthew G. Knepley 
8228ece3a9fcSMatthew G. Knepley   Output Parameter:
8229ece3a9fcSMatthew G. Knepley . localX  - The output vector
8230ece3a9fcSMatthew G. Knepley 
8231ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8232ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8233ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8234ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8235ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8236ece3a9fcSMatthew G. Knepley 
8237ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8238ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8239ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8240ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8241ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8242ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8243ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8244ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8245ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8246ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8247ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8248ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8249ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8250ece3a9fcSMatthew G. Knepley .  t            - The current time
8251ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8252ece3a9fcSMatthew G. Knepley .  n            - The face normal
8253ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8254ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8255ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8256ece3a9fcSMatthew G. Knepley 
8257ece3a9fcSMatthew G. Knepley   Note:
8258ece3a9fcSMatthew G. Knepley   There are three different DMs that potentially interact in this function. The output DM, dm, specifies the layout of the values calculates by funcs.
8259ece3a9fcSMatthew G. Knepley   The input DM, attached to U, may be different. For example, you can input the solution over the full domain, but output over a piece of the boundary, or
8260ece3a9fcSMatthew G. Knepley   a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary DM, attached to the
8261ece3a9fcSMatthew G. Knepley   auxiliary field vector, which is attached to dm, can also be different. It can have a different topology, number of fields, and discretizations.
8262ece3a9fcSMatthew G. Knepley 
8263ece3a9fcSMatthew G. Knepley   Level: intermediate
8264ece3a9fcSMatthew G. Knepley 
8265ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8266ece3a9fcSMatthew G. Knepley @*/
8267ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
8268ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
8269ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8270ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8271ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
8272ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
8273ece3a9fcSMatthew G. Knepley {
8274ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
8275ece3a9fcSMatthew G. Knepley 
8276ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8277ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8278ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
8279ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8280ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
8281ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
8282ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8283ece3a9fcSMatthew G. Knepley }
8284ece3a9fcSMatthew G. Knepley 
8285ece3a9fcSMatthew G. Knepley /*@C
82862716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
82872716604bSToby Isaac 
82882716604bSToby Isaac   Input Parameters:
82892716604bSToby Isaac + dm    - The DM
82900709b2feSToby Isaac . time  - The time
82912716604bSToby Isaac . funcs - The functions to evaluate for each field component
82922716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8293574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
82942716604bSToby Isaac 
82952716604bSToby Isaac   Output Parameter:
82962716604bSToby Isaac . diff - The diff ||u - u_h||_2
82972716604bSToby Isaac 
82982716604bSToby Isaac   Level: developer
82992716604bSToby Isaac 
83001189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
83012716604bSToby Isaac @*/
83020709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
83032716604bSToby Isaac {
83042716604bSToby Isaac   PetscErrorCode ierr;
83052716604bSToby Isaac 
83062716604bSToby Isaac   PetscFunctionBegin;
83072716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8308b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
83090918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
83100709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
83112716604bSToby Isaac   PetscFunctionReturn(0);
83122716604bSToby Isaac }
8313b698f381SToby Isaac 
8314b698f381SToby Isaac /*@C
8315b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8316b698f381SToby Isaac 
8317d083f849SBarry Smith   Collective on dm
8318d083f849SBarry Smith 
8319b698f381SToby Isaac   Input Parameters:
8320b698f381SToby Isaac + dm    - The DM
8321b698f381SToby Isaac , time  - The time
8322b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8323b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8324574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8325b698f381SToby Isaac - n     - The vector to project along
8326b698f381SToby Isaac 
8327b698f381SToby Isaac   Output Parameter:
8328b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8329b698f381SToby Isaac 
8330b698f381SToby Isaac   Level: developer
8331b698f381SToby Isaac 
8332b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8333b698f381SToby Isaac @*/
8334b698f381SToby 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)
8335b698f381SToby Isaac {
8336b698f381SToby Isaac   PetscErrorCode ierr;
8337b698f381SToby Isaac 
8338b698f381SToby Isaac   PetscFunctionBegin;
8339b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8340b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8341b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8342b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8343b698f381SToby Isaac   PetscFunctionReturn(0);
8344b698f381SToby Isaac }
8345b698f381SToby Isaac 
83462a16baeaSToby Isaac /*@C
83472a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
83482a16baeaSToby Isaac 
8349d083f849SBarry Smith   Collective on dm
8350d083f849SBarry Smith 
83512a16baeaSToby Isaac   Input Parameters:
83522a16baeaSToby Isaac + dm    - The DM
83532a16baeaSToby Isaac . time  - The time
83542a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
83552a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8356574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
83572a16baeaSToby Isaac 
83582a16baeaSToby Isaac   Output Parameter:
83592a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
83602a16baeaSToby Isaac 
83612a16baeaSToby Isaac   Level: developer
83622a16baeaSToby Isaac 
83631189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
83642a16baeaSToby Isaac @*/
83651189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
83662a16baeaSToby Isaac {
83672a16baeaSToby Isaac   PetscErrorCode ierr;
83682a16baeaSToby Isaac 
83692a16baeaSToby Isaac   PetscFunctionBegin;
83702a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83712a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
83720918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
83732a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
83742a16baeaSToby Isaac   PetscFunctionReturn(0);
83752a16baeaSToby Isaac }
83762a16baeaSToby Isaac 
8377df0b854cSToby Isaac /*@C
8378df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8379cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8380df0b854cSToby Isaac 
8381df0b854cSToby Isaac   Collective on dm
8382df0b854cSToby Isaac 
8383df0b854cSToby Isaac   Input parameters:
8384df0b854cSToby Isaac + dm - the pre-adaptation DM object
8385a1b0c543SToby Isaac - label - label with the flags
8386df0b854cSToby Isaac 
8387df0b854cSToby Isaac   Output parameters:
83880d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8389df0b854cSToby Isaac 
8390df0b854cSToby Isaac   Level: intermediate
83910d1cd5e0SMatthew G. Knepley 
83920d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8393df0b854cSToby Isaac @*/
83940d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8395df0b854cSToby Isaac {
8396df0b854cSToby Isaac   PetscErrorCode ierr;
8397df0b854cSToby Isaac 
8398df0b854cSToby Isaac   PetscFunctionBegin;
8399df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8400a1b0c543SToby Isaac   PetscValidPointer(label,2);
84010d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
84020d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
84036f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
84040d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
84050d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
84060d1cd5e0SMatthew G. Knepley }
84070d1cd5e0SMatthew G. Knepley 
84080d1cd5e0SMatthew G. Knepley /*@C
84090d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
84100d1cd5e0SMatthew G. Knepley 
84110d1cd5e0SMatthew G. Knepley   Input Parameters:
84120d1cd5e0SMatthew G. Knepley + dm - The DM object
84130d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
84146f25b0d8SLisandro 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_".
84150d1cd5e0SMatthew G. Knepley 
84160d1cd5e0SMatthew G. Knepley   Output Parameter:
84170d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
84180d1cd5e0SMatthew G. Knepley 
84190d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
84200d1cd5e0SMatthew G. Knepley 
84210d1cd5e0SMatthew G. Knepley   Level: advanced
84220d1cd5e0SMatthew G. Knepley 
84230d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
84240d1cd5e0SMatthew G. Knepley @*/
84250d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
84260d1cd5e0SMatthew G. Knepley {
84270d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
84280d1cd5e0SMatthew G. Knepley 
84290d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
84300d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84310d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
84320d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
84330d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
84346f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
84356f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
84360d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8437df0b854cSToby Isaac   PetscFunctionReturn(0);
8438df0b854cSToby Isaac }
8439c4088d22SMatthew G. Knepley 
8440502a2867SDave May /*@C
8441502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8442502a2867SDave May 
8443502a2867SDave May  Not Collective
8444502a2867SDave May 
8445502a2867SDave May  Input Parameter:
8446502a2867SDave May .  dm    - The DM
8447502a2867SDave May 
84480a19bb7dSprj-  Output Parameters:
84490a19bb7dSprj- +  nranks - the number of neighbours
84500a19bb7dSprj- -  ranks - the neighbors ranks
8451502a2867SDave May 
8452502a2867SDave May  Notes:
8453502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8454502a2867SDave May 
8455502a2867SDave May  Level: beginner
8456502a2867SDave May 
8457dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
8458502a2867SDave May @*/
8459502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8460502a2867SDave May {
8461502a2867SDave May   PetscErrorCode ierr;
8462502a2867SDave May 
8463502a2867SDave May   PetscFunctionBegin;
8464502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84650918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
8466502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
8467502a2867SDave May   PetscFunctionReturn(0);
8468502a2867SDave May }
8469502a2867SDave May 
8470531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8471531c7667SBarry Smith 
8472531c7667SBarry Smith /*
8473531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8474531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8475531c7667SBarry Smith */
8476531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8477531c7667SBarry Smith {
8478531c7667SBarry Smith   PetscErrorCode ierr;
8479531c7667SBarry Smith 
8480531c7667SBarry Smith   PetscFunctionBegin;
8481531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8482531c7667SBarry Smith     Vec x1local;
8483531c7667SBarry Smith     DM  dm;
8484531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8485531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
8486531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
8487531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8488531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8489531c7667SBarry Smith     x1   = x1local;
8490531c7667SBarry Smith   }
8491531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
8492531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8493531c7667SBarry Smith     DM  dm;
8494531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8495531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
8496531c7667SBarry Smith   }
8497531c7667SBarry Smith   PetscFunctionReturn(0);
8498531c7667SBarry Smith }
8499531c7667SBarry Smith 
8500531c7667SBarry Smith /*@
8501531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8502531c7667SBarry Smith 
8503531c7667SBarry Smith     Input Parameter:
8504531c7667SBarry Smith .    coloring - the MatFDColoring object
8505531c7667SBarry Smith 
850695452b02SPatrick Sanan     Developer Notes:
850795452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8508531c7667SBarry Smith 
85091b266c99SBarry Smith     Level: advanced
85101b266c99SBarry Smith 
8511531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8512531c7667SBarry Smith @*/
8513531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8514531c7667SBarry Smith {
8515531c7667SBarry Smith   PetscFunctionBegin;
8516531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8517531c7667SBarry Smith   PetscFunctionReturn(0);
8518531c7667SBarry Smith }
85198320bc6fSPatrick Sanan 
85208320bc6fSPatrick Sanan /*@
85218320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
85228320bc6fSPatrick Sanan 
85238320bc6fSPatrick Sanan     Collective
85248320bc6fSPatrick Sanan 
85258320bc6fSPatrick Sanan     Input Parameters:
8526a5bc1bf3SBarry Smith +    dm1 - the first DM
85278320bc6fSPatrick Sanan -    dm2 - the second DM
85288320bc6fSPatrick Sanan 
85298320bc6fSPatrick Sanan     Output Parameters:
85308320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
85318320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
85328320bc6fSPatrick Sanan 
85338320bc6fSPatrick Sanan     Notes:
85348320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
85353d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
85368320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
85373d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
85383d862458SPatrick Sanan     decomposition, but hold different data.
85398320bc6fSPatrick Sanan 
85408320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
85418320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
85428320bc6fSPatrick Sanan 
85438320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
85448320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
85458320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
85468320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
85478320bc6fSPatrick Sanan 
85488320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
85498320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
85508320bc6fSPatrick Sanan .vb
85518320bc6fSPatrick Sanan   ...
85528320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
85538320bc6fSPatrick Sanan   if (set && compatible)  {
85548320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
85558320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
85563d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
85578320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
85588320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
85598320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
85608320bc6fSPatrick Sanan       }
85618320bc6fSPatrick Sanan     }
85628320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
85638320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
85648320bc6fSPatrick Sanan   } else {
85658320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
85668320bc6fSPatrick Sanan   }
85678320bc6fSPatrick Sanan   ...
85688320bc6fSPatrick Sanan .ve
85698320bc6fSPatrick Sanan 
85708320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
85718320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
85728320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
85738320bc6fSPatrick Sanan     always check the "set" output parameter.
85748320bc6fSPatrick Sanan 
85758320bc6fSPatrick Sanan     A DM is always compatible with itself.
85768320bc6fSPatrick Sanan 
85778320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
85788320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
85798320bc6fSPatrick Sanan     incompatible.
85808320bc6fSPatrick Sanan 
85818320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
85828320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
85838320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
85848320bc6fSPatrick Sanan 
85858320bc6fSPatrick Sanan     Developer Notes:
85863d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
85873d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8588a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
85898320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
85908320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
85913d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
85923d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
85938320bc6fSPatrick Sanan 
85948320bc6fSPatrick Sanan     Level: advanced
85958320bc6fSPatrick Sanan 
85963d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
85978320bc6fSPatrick Sanan @*/
85988320bc6fSPatrick Sanan 
8599a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
86008320bc6fSPatrick Sanan {
86018320bc6fSPatrick Sanan   PetscErrorCode ierr;
86028320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
86038320bc6fSPatrick Sanan   DMType         type,type2;
86048320bc6fSPatrick Sanan   PetscBool      sameType;
86058320bc6fSPatrick Sanan 
86068320bc6fSPatrick Sanan   PetscFunctionBegin;
8607a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
86088320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
86098320bc6fSPatrick Sanan 
86108320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8611a5bc1bf3SBarry Smith   if (dm1 == dm2) {
86128320bc6fSPatrick Sanan     *set = PETSC_TRUE;
86138320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
86148320bc6fSPatrick Sanan     PetscFunctionReturn(0);
86158320bc6fSPatrick Sanan   }
86168320bc6fSPatrick Sanan 
86178320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
86188320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
86198320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
86208320bc6fSPatrick Sanan      determined by the implementation-specific logic */
8621a5bc1bf3SBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
86228320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
86238320bc6fSPatrick Sanan     *set = PETSC_TRUE;
86248320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
86258320bc6fSPatrick Sanan     PetscFunctionReturn(0);
86268320bc6fSPatrick Sanan   }
86278320bc6fSPatrick Sanan 
86288320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8629a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8630a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
8631b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
86328320bc6fSPatrick Sanan   }
86338320bc6fSPatrick Sanan 
8634a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
86358320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
8636a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
86378320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
86388320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
86398320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8640a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
86418320bc6fSPatrick Sanan   } else {
86428320bc6fSPatrick Sanan     *set = PETSC_FALSE;
86438320bc6fSPatrick Sanan   }
86448320bc6fSPatrick Sanan   PetscFunctionReturn(0);
86458320bc6fSPatrick Sanan }
8646c0f0dcc3SMatthew G. Knepley 
8647c0f0dcc3SMatthew G. Knepley /*@C
8648c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8649c0f0dcc3SMatthew G. Knepley 
8650c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8651c0f0dcc3SMatthew G. Knepley 
8652c0f0dcc3SMatthew G. Knepley   Input Parameters:
8653c0f0dcc3SMatthew G. Knepley + DM - the DM
8654c0f0dcc3SMatthew G. Knepley . f - the monitor function
8655c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8656c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8657c0f0dcc3SMatthew G. Knepley 
8658c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8659c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8660c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8661c0f0dcc3SMatthew G. Knepley 
8662c0f0dcc3SMatthew G. Knepley   Notes:
8663c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8664c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8665c0f0dcc3SMatthew G. Knepley   order in which they were set.
8666c0f0dcc3SMatthew G. Knepley 
8667c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8668c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
8669c0f0dcc3SMatthew G. Knepley 
8670c0f0dcc3SMatthew G. Knepley   Level: intermediate
8671c0f0dcc3SMatthew G. Knepley 
8672c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
8673c0f0dcc3SMatthew G. Knepley @*/
8674c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8675c0f0dcc3SMatthew G. Knepley {
8676c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8677c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8678c0f0dcc3SMatthew G. Knepley 
8679c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8680c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8681c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8682c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8683c0f0dcc3SMatthew G. Knepley 
8684c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
8685c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8686c0f0dcc3SMatthew G. Knepley   }
8687c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8688c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8689c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8690c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
8691c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8692c0f0dcc3SMatthew G. Knepley }
8693c0f0dcc3SMatthew G. Knepley 
8694c0f0dcc3SMatthew G. Knepley /*@
8695c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
8696c0f0dcc3SMatthew G. Knepley 
8697c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8698c0f0dcc3SMatthew G. Knepley 
8699c0f0dcc3SMatthew G. Knepley   Input Parameter:
8700c0f0dcc3SMatthew G. Knepley . dm - the DM
8701c0f0dcc3SMatthew G. Knepley 
8702c0f0dcc3SMatthew G. Knepley   Options Database Key:
8703c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8704c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
8705c0f0dcc3SMatthew G. Knepley   set via the options database
8706c0f0dcc3SMatthew G. Knepley 
8707c0f0dcc3SMatthew G. Knepley   Notes:
8708c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
8709c0f0dcc3SMatthew G. Knepley 
8710c0f0dcc3SMatthew G. Knepley   Level: intermediate
8711c0f0dcc3SMatthew G. Knepley 
8712c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8713c0f0dcc3SMatthew G. Knepley @*/
8714c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
8715c0f0dcc3SMatthew G. Knepley {
8716c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8717c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8718c0f0dcc3SMatthew G. Knepley 
8719c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8720c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8721c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8722c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
8723c0f0dcc3SMatthew G. Knepley   }
8724c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8725c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8726c0f0dcc3SMatthew G. Knepley }
8727c0f0dcc3SMatthew G. Knepley 
8728c0f0dcc3SMatthew G. Knepley /*@C
8729c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8730c0f0dcc3SMatthew G. Knepley 
8731c0f0dcc3SMatthew G. Knepley   Collective on DM
8732c0f0dcc3SMatthew G. Knepley 
8733c0f0dcc3SMatthew G. Knepley   Input Parameters:
8734c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
8735c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8736c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8737c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8738c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8739c0f0dcc3SMatthew G. Knepley - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the DM or PetscViewer objects
8740c0f0dcc3SMatthew G. Knepley 
8741c0f0dcc3SMatthew G. Knepley   Output Parameter:
8742c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8743c0f0dcc3SMatthew G. Knepley 
8744c0f0dcc3SMatthew G. Knepley   Level: developer
8745c0f0dcc3SMatthew G. Knepley 
8746c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
8747c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
8748c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
8749c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
8750c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
8751c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
8752c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
8753c0f0dcc3SMatthew G. Knepley @*/
8754c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8755c0f0dcc3SMatthew G. Knepley {
8756c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8757c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8758c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
8759c0f0dcc3SMatthew G. Knepley 
8760c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8761c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8762c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
8763c0f0dcc3SMatthew G. Knepley   if (*flg) {
8764c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8765c0f0dcc3SMatthew G. Knepley 
8766c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
8767c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
8768c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
8769c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
8770c0f0dcc3SMatthew G. Knepley   }
8771c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8772c0f0dcc3SMatthew G. Knepley }
8773c0f0dcc3SMatthew G. Knepley 
8774c0f0dcc3SMatthew G. Knepley /*@
8775c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8776c0f0dcc3SMatthew G. Knepley 
8777c0f0dcc3SMatthew G. Knepley    Collective on DM
8778c0f0dcc3SMatthew G. Knepley 
8779c0f0dcc3SMatthew G. Knepley    Input Parameters:
8780c0f0dcc3SMatthew G. Knepley .  dm - The DM
8781c0f0dcc3SMatthew G. Knepley 
8782c0f0dcc3SMatthew G. Knepley    Level: developer
8783c0f0dcc3SMatthew G. Knepley 
8784c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
8785c0f0dcc3SMatthew G. Knepley @*/
8786c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
8787c0f0dcc3SMatthew G. Knepley {
8788c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8789c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8790c0f0dcc3SMatthew G. Knepley 
8791c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8792c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8793c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8794c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8795c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
8796c0f0dcc3SMatthew G. Knepley   }
8797c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8798c0f0dcc3SMatthew G. Knepley }
8799