xref: /petsc/src/dm/interface/dm.c (revision 4a7ee7d065f8f8fdf1dd77bc0f5949e28c54cac2)
1d0295fc0SJunchao Zhang #include <petscvec.h>
2af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
3c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
4e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
53e922f36SToby Isaac #include <petscdmplex.h>
6f19dbd58SToby Isaac #include <petscdmfield.h>
70c312b8eSJed Brown #include <petscsf.h>
82764a2aaSMatthew G. Knepley #include <petscds.h>
947c6ae99SBarry Smith 
1000d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
1100d952a4SJed Brown #  include <valgrind/memcheck.h>
1200d952a4SJed Brown #endif
1300d952a4SJed Brown 
14732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
15d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
16557cf195SMatthew G. Knepley 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, DM_AdaptInterpolator;
1767a56275SMatthew G Knepley 
18ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_", NULL};
19d1b3049bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID","ESSENTIAL","NATURAL","INVALID","INVALID","ESSENTIAL_FIELD","NATURAL_FIELD","INVALID","INVALID","ESSENTIAL_BD_FIELD","NATURAL_RIEMANN","DMBoundaryConditionType","DM_BC_", NULL};
20da9060c4SMatthew G. Knepley const char *const DMPolytopeTypes[] = {"vertex", "segment", "tensor_segment", "triangle", "quadrilateral", "tensor_quad", "tetrahedron", "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism", "pyramid", "FV_ghost_cell", "interior_ghost_cell", "unknown", "invalid", "DMPolytopeType", "DM_POLYTOPE_", NULL};
2160c22052SBarry Smith 
22a4121054SBarry Smith /*@
23de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
24a4121054SBarry Smith 
25a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
26a4121054SBarry Smith    error when you try to use the vector.
27a4121054SBarry Smith 
28d083f849SBarry Smith   Collective
29a4121054SBarry Smith 
30a4121054SBarry Smith   Input Parameter:
31a4121054SBarry Smith . comm - The communicator for the DM object
32a4121054SBarry Smith 
33a4121054SBarry Smith   Output Parameter:
34a4121054SBarry Smith . dm - The DM object
35a4121054SBarry Smith 
36a4121054SBarry Smith   Level: beginner
37a4121054SBarry Smith 
388472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
39a4121054SBarry Smith @*/
407087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
41a4121054SBarry Smith {
42a4121054SBarry Smith   DM             v;
43e5e52638SMatthew G. Knepley   PetscDS        ds;
44a4121054SBarry Smith   PetscErrorCode ierr;
45a4121054SBarry Smith 
46a4121054SBarry Smith   PetscFunctionBegin;
471411c6eeSJed Brown   PetscValidPointer(dm,2);
480298fd71SBarry Smith   *dm = NULL;
49607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
50a4121054SBarry Smith 
5173107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
52e7c4fc90SDmitry Karpeev 
5349be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
5449be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
550298fd71SBarry Smith   v->ltogmap                  = NULL;
561411c6eeSJed Brown   v->bs                       = 1;
57171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5888ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
591bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
60c58f1c22SToby Isaac   v->labels                   = NULL;
6134aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
6234aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
63c58f1c22SToby Isaac   v->depthLabel               = NULL;
64ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
651bb6d2a8SBarry Smith   v->localSection             = NULL;
661bb6d2a8SBarry Smith   v->globalSection            = NULL;
67fba222abSToby Isaac   v->defaultConstraintSection = NULL;
68fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
69c6b900c6SMatthew G. Knepley   v->L                        = NULL;
70c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
715dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
729a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7396173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
74435a35e8SMatthew G Knepley   {
75435a35e8SMatthew G Knepley     PetscInt i;
76435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
770298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
78f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
79435a35e8SMatthew G Knepley     }
80435a35e8SMatthew G Knepley   }
8145480ffeSMatthew G. Knepley   ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
82b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
83e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
849a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxCreate(&v->auxData);CHKERRQ(ierr);
8514f150ffSMatthew G. Knepley   v->dmBC = NULL;
86a8fb8f29SToby Isaac   v->coarseMesh = NULL;
87f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
88cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
89c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
90b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
914a7a4c06SLawrence Mitchell 
921411c6eeSJed Brown   *dm = v;
93a4121054SBarry Smith   PetscFunctionReturn(0);
94a4121054SBarry Smith }
95a4121054SBarry Smith 
9638221697SMatthew G. Knepley /*@
9738221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9838221697SMatthew G. Knepley 
99d083f849SBarry Smith   Collective
10038221697SMatthew G. Knepley 
10138221697SMatthew G. Knepley   Input Parameter:
10238221697SMatthew G. Knepley . dm - The original DM object
10338221697SMatthew G. Knepley 
10438221697SMatthew G. Knepley   Output Parameter:
10538221697SMatthew G. Knepley . newdm  - The new DM object
10638221697SMatthew G. Knepley 
10738221697SMatthew G. Knepley   Level: beginner
10838221697SMatthew G. Knepley 
1091cb8cacdSPatrick Sanan   Notes:
1101cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1111cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1121cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1131bb6d2a8SBarry Smith 
11489706ed2SPatrick Sanan   The clone is considered set up iff the original is.
11589706ed2SPatrick Sanan 
11692cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1171bb6d2a8SBarry Smith 
11838221697SMatthew G. Knepley @*/
11938221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
12038221697SMatthew G. Knepley {
12138221697SMatthew G. Knepley   PetscSF        sf;
12238221697SMatthew G. Knepley   Vec            coords;
12338221697SMatthew G. Knepley   void          *ctx;
124a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
12538221697SMatthew G. Knepley   PetscErrorCode ierr;
12638221697SMatthew G. Knepley 
12738221697SMatthew G. Knepley   PetscFunctionBegin;
12838221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12938221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
13038221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1315d80c0bfSVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE);CHKERRQ(ierr);
132ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
133ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
134c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
135a587d139SMark   ierr = PetscFree((*newdm)->vectype);CHKERRQ(ierr);
136a587d139SMark   ierr = PetscStrallocpy(dm->vectype,(char**)&(*newdm)->vectype);CHKERRQ(ierr);
137a587d139SMark   ierr = PetscFree((*newdm)->mattype);CHKERRQ(ierr);
138a587d139SMark   ierr = PetscStrallocpy(dm->mattype,(char**)&(*newdm)->mattype);CHKERRQ(ierr);
1391de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1401de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
14138221697SMatthew G. Knepley   if (dm->ops->clone) {
14238221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
14338221697SMatthew G. Knepley   }
1443f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
14538221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
14638221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
14738221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
14838221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
149be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
150be4c1c3eSMatthew G. Knepley     DM           ncdm;
151be4c1c3eSMatthew G. Knepley     PetscSection cs;
1525a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
153be4c1c3eSMatthew G. Knepley 
15492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
155be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
156ffc4695bSBarry Smith     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
1575a0206caSToby Isaac     if (pEndMax >= 0) {
158be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
15983bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
16092fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
161a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
162be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
163be4c1c3eSMatthew G. Knepley     }
164be4c1c3eSMatthew G. Knepley   }
165a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
166a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
16738221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
16838221697SMatthew G. Knepley   if (coords) {
16938221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
17038221697SMatthew G. Knepley   } else {
17138221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
17238221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
17338221697SMatthew G. Knepley   }
17490b157c4SStefano Zampini   {
17590b157c4SStefano Zampini     PetscBool             isper;
176c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1775dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
17890b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
17990b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
180c6b900c6SMatthew G. Knepley   }
18134aa8a36SMatthew G. Knepley   {
18234aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
18334aa8a36SMatthew G. Knepley 
18434aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
18534aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
18634aa8a36SMatthew G. Knepley   }
18738221697SMatthew G. Knepley   PetscFunctionReturn(0);
18838221697SMatthew G. Knepley }
18938221697SMatthew G. Knepley 
1909a42bb27SBarry Smith /*@C
191564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1929a42bb27SBarry Smith 
193d083f849SBarry Smith    Logically Collective on da
1949a42bb27SBarry Smith 
1959a42bb27SBarry Smith    Input Parameter:
1969a42bb27SBarry Smith +  da - initial distributed array
197e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1989a42bb27SBarry Smith 
1999a42bb27SBarry Smith    Options Database:
200dd85299cSBarry Smith .   -dm_vec_type ctype
2019a42bb27SBarry Smith 
2029a42bb27SBarry Smith    Level: intermediate
2039a42bb27SBarry Smith 
204a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
2059a42bb27SBarry Smith @*/
20619fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2079a42bb27SBarry Smith {
2089a42bb27SBarry Smith   PetscErrorCode ierr;
2099a42bb27SBarry Smith 
2109a42bb27SBarry Smith   PetscFunctionBegin;
2119a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2129a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
21319fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2149a42bb27SBarry Smith   PetscFunctionReturn(0);
2159a42bb27SBarry Smith }
2169a42bb27SBarry Smith 
217c0dedaeaSBarry Smith /*@C
218c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
219c0dedaeaSBarry Smith 
220d083f849SBarry Smith    Logically Collective on da
221c0dedaeaSBarry Smith 
222c0dedaeaSBarry Smith    Input Parameter:
223c0dedaeaSBarry Smith .  da - initial distributed array
224c0dedaeaSBarry Smith 
225c0dedaeaSBarry Smith    Output Parameter:
226c0dedaeaSBarry Smith .  ctype - the vector type
227c0dedaeaSBarry Smith 
228c0dedaeaSBarry Smith    Level: intermediate
229c0dedaeaSBarry Smith 
230a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
231c0dedaeaSBarry Smith @*/
232c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
233c0dedaeaSBarry Smith {
234c0dedaeaSBarry Smith   PetscFunctionBegin;
235c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
236c0dedaeaSBarry Smith   *ctype = da->vectype;
237c0dedaeaSBarry Smith   PetscFunctionReturn(0);
238c0dedaeaSBarry Smith }
239c0dedaeaSBarry Smith 
2405f1ad066SMatthew G Knepley /*@
24134f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2425f1ad066SMatthew G Knepley 
2435f1ad066SMatthew G Knepley   Not collective
2445f1ad066SMatthew G Knepley 
2455f1ad066SMatthew G Knepley   Input Parameter:
2465f1ad066SMatthew G Knepley . v - The Vec
2475f1ad066SMatthew G Knepley 
2485f1ad066SMatthew G Knepley   Output Parameter:
2495f1ad066SMatthew G Knepley . dm - The DM
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley   Level: intermediate
2525f1ad066SMatthew G Knepley 
2535f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2545f1ad066SMatthew G Knepley @*/
2555f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2565f1ad066SMatthew G Knepley {
2575f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2585f1ad066SMatthew G Knepley 
2595f1ad066SMatthew G Knepley   PetscFunctionBegin;
2605f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2615f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2625f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2635f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2645f1ad066SMatthew G Knepley }
2655f1ad066SMatthew G Knepley 
2665f1ad066SMatthew G Knepley /*@
267d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2685f1ad066SMatthew G Knepley 
2695f1ad066SMatthew G Knepley   Not collective
2705f1ad066SMatthew G Knepley 
2715f1ad066SMatthew G Knepley   Input Parameters:
2725f1ad066SMatthew G Knepley + v - The Vec
2735f1ad066SMatthew G Knepley - dm - The DM
2745f1ad066SMatthew G Knepley 
275d9805387SMatthew 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.
276d9805387SMatthew G. Knepley 
2775f1ad066SMatthew G Knepley   Level: intermediate
2785f1ad066SMatthew G Knepley 
2795f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2805f1ad066SMatthew G Knepley @*/
2815f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2825f1ad066SMatthew G Knepley {
2835f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2845f1ad066SMatthew G Knepley 
2855f1ad066SMatthew G Knepley   PetscFunctionBegin;
2865f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
287d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2885f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2895f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2905f1ad066SMatthew G Knepley }
2915f1ad066SMatthew G Knepley 
292521d9a4cSLisandro Dalcin /*@C
2938f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2948f1509bcSBarry Smith 
295d083f849SBarry Smith    Logically Collective on dm
2968f1509bcSBarry Smith 
2978f1509bcSBarry Smith    Input Parameters:
2988f1509bcSBarry Smith +  dm - the DM context
2998f1509bcSBarry Smith -  ctype - the matrix type
3008f1509bcSBarry Smith 
3018f1509bcSBarry Smith    Options Database:
3028f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3038f1509bcSBarry Smith 
3048f1509bcSBarry Smith    Level: intermediate
3058f1509bcSBarry Smith 
3068f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3078f1509bcSBarry Smith           DMGetISColoringType()
3088f1509bcSBarry Smith @*/
3098f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3108f1509bcSBarry Smith {
3118f1509bcSBarry Smith   PetscFunctionBegin;
3128f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3138f1509bcSBarry Smith   dm->coloringtype = ctype;
3148f1509bcSBarry Smith   PetscFunctionReturn(0);
3158f1509bcSBarry Smith }
3168f1509bcSBarry Smith 
3178f1509bcSBarry Smith /*@C
3188f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
319521d9a4cSLisandro Dalcin 
320d083f849SBarry Smith    Logically Collective on dm
321521d9a4cSLisandro Dalcin 
322521d9a4cSLisandro Dalcin    Input Parameter:
3238f1509bcSBarry Smith .  dm - the DM context
3248f1509bcSBarry Smith 
3258f1509bcSBarry Smith    Output Parameter:
3268f1509bcSBarry Smith .  ctype - the matrix type
3278f1509bcSBarry Smith 
3288f1509bcSBarry Smith    Options Database:
3298f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3308f1509bcSBarry Smith 
3318f1509bcSBarry Smith    Level: intermediate
3328f1509bcSBarry Smith 
3338f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3348f1509bcSBarry Smith           DMGetISColoringType()
3358f1509bcSBarry Smith @*/
3368f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3378f1509bcSBarry Smith {
3388f1509bcSBarry Smith   PetscFunctionBegin;
3398f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3408f1509bcSBarry Smith   *ctype = dm->coloringtype;
3418f1509bcSBarry Smith   PetscFunctionReturn(0);
3428f1509bcSBarry Smith }
3438f1509bcSBarry Smith 
3448f1509bcSBarry Smith /*@C
3458f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3468f1509bcSBarry Smith 
347d083f849SBarry Smith    Logically Collective on dm
3488f1509bcSBarry Smith 
3498f1509bcSBarry Smith    Input Parameters:
350521d9a4cSLisandro Dalcin +  dm - the DM context
351a2b5a043SBarry Smith -  ctype - the matrix type
352521d9a4cSLisandro Dalcin 
353521d9a4cSLisandro Dalcin    Options Database:
354521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
355521d9a4cSLisandro Dalcin 
356521d9a4cSLisandro Dalcin    Level: intermediate
357521d9a4cSLisandro Dalcin 
358a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
359521d9a4cSLisandro Dalcin @*/
36019fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
361521d9a4cSLisandro Dalcin {
362521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
36388f0584fSBarry Smith 
364521d9a4cSLisandro Dalcin   PetscFunctionBegin;
365521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
366521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
36719fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
368521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
369521d9a4cSLisandro Dalcin }
370521d9a4cSLisandro Dalcin 
371c0dedaeaSBarry Smith /*@C
372c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
373c0dedaeaSBarry Smith 
374d083f849SBarry Smith    Logically Collective on dm
375c0dedaeaSBarry Smith 
376c0dedaeaSBarry Smith    Input Parameter:
377c0dedaeaSBarry Smith .  dm - the DM context
378c0dedaeaSBarry Smith 
379c0dedaeaSBarry Smith    Output Parameter:
380c0dedaeaSBarry Smith .  ctype - the matrix type
381c0dedaeaSBarry Smith 
382c0dedaeaSBarry Smith    Options Database:
383c0dedaeaSBarry Smith .   -dm_mat_type ctype
384c0dedaeaSBarry Smith 
385c0dedaeaSBarry Smith    Level: intermediate
386c0dedaeaSBarry Smith 
387a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
388c0dedaeaSBarry Smith @*/
389c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
390c0dedaeaSBarry Smith {
391c0dedaeaSBarry Smith   PetscFunctionBegin;
392c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
393c0dedaeaSBarry Smith   *ctype = dm->mattype;
394c0dedaeaSBarry Smith   PetscFunctionReturn(0);
395c0dedaeaSBarry Smith }
396c0dedaeaSBarry Smith 
397c688c046SMatthew G Knepley /*@
39834f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
399c688c046SMatthew G Knepley 
400c688c046SMatthew G Knepley   Not collective
401c688c046SMatthew G Knepley 
402c688c046SMatthew G Knepley   Input Parameter:
403c688c046SMatthew G Knepley . A - The Mat
404c688c046SMatthew G Knepley 
405c688c046SMatthew G Knepley   Output Parameter:
406c688c046SMatthew G Knepley . dm - The DM
407c688c046SMatthew G Knepley 
408c688c046SMatthew G Knepley   Level: intermediate
409c688c046SMatthew G Knepley 
4108f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4118f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4128f1509bcSBarry Smith 
413c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
414c688c046SMatthew G Knepley @*/
415c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
416c688c046SMatthew G Knepley {
417c688c046SMatthew G Knepley   PetscErrorCode ierr;
418c688c046SMatthew G Knepley 
419c688c046SMatthew G Knepley   PetscFunctionBegin;
420c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
421c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
422c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
423c688c046SMatthew G Knepley   PetscFunctionReturn(0);
424c688c046SMatthew G Knepley }
425c688c046SMatthew G Knepley 
426c688c046SMatthew G Knepley /*@
427c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
428c688c046SMatthew G Knepley 
429c688c046SMatthew G Knepley   Not collective
430c688c046SMatthew G Knepley 
431c688c046SMatthew G Knepley   Input Parameters:
432c688c046SMatthew G Knepley + A - The Mat
433c688c046SMatthew G Knepley - dm - The DM
434c688c046SMatthew G Knepley 
435c688c046SMatthew G Knepley   Level: intermediate
436c688c046SMatthew G Knepley 
4378f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4388f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4398f1509bcSBarry Smith 
4408f1509bcSBarry Smith 
441c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
442c688c046SMatthew G Knepley @*/
443c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
444c688c046SMatthew G Knepley {
445c688c046SMatthew G Knepley   PetscErrorCode ierr;
446c688c046SMatthew G Knepley 
447c688c046SMatthew G Knepley   PetscFunctionBegin;
448c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4498865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
450c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
451c688c046SMatthew G Knepley   PetscFunctionReturn(0);
452c688c046SMatthew G Knepley }
453c688c046SMatthew G Knepley 
4549a42bb27SBarry Smith /*@C
4559a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4566757b960SDave May    DM options in the database.
4579a42bb27SBarry Smith 
458d083f849SBarry Smith    Logically Collective on dm
4599a42bb27SBarry Smith 
4609a42bb27SBarry Smith    Input Parameter:
4618353ddbbSDave May +  da - the DM context
4629a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4639a42bb27SBarry Smith 
4649a42bb27SBarry Smith    Notes:
4659a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4669a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4679a42bb27SBarry Smith 
4689a42bb27SBarry Smith    Level: advanced
4699a42bb27SBarry Smith 
4709a42bb27SBarry Smith .seealso: DMSetFromOptions()
4719a42bb27SBarry Smith @*/
4727087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4739a42bb27SBarry Smith {
4749a42bb27SBarry Smith   PetscErrorCode ierr;
4759a42bb27SBarry Smith 
4769a42bb27SBarry Smith   PetscFunctionBegin;
4779a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4789a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
479691be533SLawrence Mitchell   if (dm->sf) {
480691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
481691be533SLawrence Mitchell   }
4821bb6d2a8SBarry Smith   if (dm->sectionSF) {
4831bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
484691be533SLawrence Mitchell   }
4859a42bb27SBarry Smith   PetscFunctionReturn(0);
4869a42bb27SBarry Smith }
4879a42bb27SBarry Smith 
48831697293SDave May /*@C
48931697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
49031697293SDave May    DM options in the database.
49131697293SDave May 
492d083f849SBarry Smith    Logically Collective on dm
49331697293SDave May 
49431697293SDave May    Input Parameters:
49531697293SDave May +  dm - the DM context
49631697293SDave May -  prefix - the prefix string to prepend to all DM option requests
49731697293SDave May 
49831697293SDave May    Notes:
49931697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
50031697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
50131697293SDave May 
50231697293SDave May    Level: advanced
50331697293SDave May 
50431697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
50531697293SDave May @*/
50631697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
50731697293SDave May {
50831697293SDave May   PetscErrorCode ierr;
50931697293SDave May 
51031697293SDave May   PetscFunctionBegin;
51131697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
51231697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
51331697293SDave May   PetscFunctionReturn(0);
51431697293SDave May }
51531697293SDave May 
51631697293SDave May /*@C
51731697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
51831697293SDave May    DM options in the database.
51931697293SDave May 
52031697293SDave May    Not Collective
52131697293SDave May 
52231697293SDave May    Input Parameters:
52331697293SDave May .  dm - the DM context
52431697293SDave May 
52531697293SDave May    Output Parameters:
52631697293SDave May .  prefix - pointer to the prefix string used is returned
52731697293SDave May 
52895452b02SPatrick Sanan    Notes:
52995452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
53031697293SDave May    sufficient length to hold the prefix.
53131697293SDave May 
53231697293SDave May    Level: advanced
53331697293SDave May 
53431697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
53531697293SDave May @*/
53631697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
53731697293SDave May {
53831697293SDave May   PetscErrorCode ierr;
53931697293SDave May 
54031697293SDave May   PetscFunctionBegin;
54131697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54231697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
54331697293SDave May   PetscFunctionReturn(0);
54431697293SDave May }
54531697293SDave May 
54688bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
54788bdff64SToby Isaac {
5486eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
54988bdff64SToby Isaac   PetscErrorCode ierr;
55088bdff64SToby Isaac 
55188bdff64SToby Isaac   PetscFunctionBegin;
552aab5bcd8SJed Brown   *ncrefct = 0;
55388bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55488bdff64SToby Isaac     refct--;
55588bdff64SToby Isaac     if (recurseCoarse) {
55688bdff64SToby Isaac       PetscInt coarseCount;
55788bdff64SToby Isaac 
55888bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55988bdff64SToby Isaac       refct += coarseCount;
56088bdff64SToby Isaac     }
56188bdff64SToby Isaac   }
56288bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56388bdff64SToby Isaac     refct--;
56488bdff64SToby Isaac     if (recurseFine) {
56588bdff64SToby Isaac       PetscInt fineCount;
56688bdff64SToby Isaac 
56788bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56888bdff64SToby Isaac       refct += fineCount;
56988bdff64SToby Isaac     }
57088bdff64SToby Isaac   }
57188bdff64SToby Isaac   *ncrefct = refct;
57288bdff64SToby Isaac   PetscFunctionReturn(0);
57388bdff64SToby Isaac }
57488bdff64SToby Isaac 
575f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
576354557abSToby Isaac {
5775d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
578354557abSToby Isaac   PetscErrorCode ierr;
579354557abSToby Isaac 
580354557abSToby Isaac   PetscFunctionBegin;
581354557abSToby Isaac   /* destroy the labels */
582354557abSToby Isaac   while (next) {
583354557abSToby Isaac     DMLabelLink tmp = next->next;
584354557abSToby Isaac 
5855d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
586ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
587354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
588354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
589354557abSToby Isaac     next = tmp;
590354557abSToby Isaac   }
5915d80c0bfSVaclav Hapla   dm->labels = NULL;
592354557abSToby Isaac   PetscFunctionReturn(0);
593354557abSToby Isaac }
594354557abSToby Isaac 
5951fb7b255SJunchao Zhang /*@C
5968472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
59747c6ae99SBarry Smith 
598d083f849SBarry Smith     Collective on dm
59947c6ae99SBarry Smith 
60047c6ae99SBarry Smith     Input Parameter:
60147c6ae99SBarry Smith .   dm - the DM object to destroy
60247c6ae99SBarry Smith 
60347c6ae99SBarry Smith     Level: developer
60447c6ae99SBarry Smith 
605e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60647c6ae99SBarry Smith 
60747c6ae99SBarry Smith @*/
608fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60947c6ae99SBarry Smith {
6106eb26441SStefano Zampini   PetscInt       cnt;
611dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61247c6ae99SBarry Smith   PetscErrorCode ierr;
61347c6ae99SBarry Smith 
61447c6ae99SBarry Smith   PetscFunctionBegin;
6156bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6166bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
61787e657c6SBarry Smith 
61888bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61988bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
62088bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
621ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6226bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6236bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6246eb26441SStefano Zampini 
6256eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6266eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6276eb26441SStefano Zampini 
628f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6290298fd71SBarry Smith   (*dm)->namedglobal = NULL;
630f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6312348bcf4SPeter Brune     nnext = nlink->next;
6322348bcf4SPeter 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);
6332348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6342348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6352348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6362348bcf4SPeter Brune   }
637f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6380298fd71SBarry Smith   (*dm)->namedlocal = NULL;
639f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
640f490541aSPeter Brune     nnext = nlink->next;
641f490541aSPeter 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);
642f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
643f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
644f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
645f490541aSPeter Brune   }
6462348bcf4SPeter Brune 
647b17ce1afSJed Brown   /* Destroy the list of hooks */
648c833c3b5SJed Brown   {
649c833c3b5SJed Brown     DMCoarsenHookLink link,next;
650b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
651b17ce1afSJed Brown       next = link->next;
652b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
653b17ce1afSJed Brown     }
6540298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
655c833c3b5SJed Brown   }
656c833c3b5SJed Brown   {
657c833c3b5SJed Brown     DMRefineHookLink link,next;
658c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
659c833c3b5SJed Brown       next = link->next;
660c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
661c833c3b5SJed Brown     }
6620298fd71SBarry Smith     (*dm)->refinehook = NULL;
663c833c3b5SJed Brown   }
664be081cd6SPeter Brune   {
665be081cd6SPeter Brune     DMSubDomainHookLink link,next;
666be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
667be081cd6SPeter Brune       next = link->next;
668be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
669be081cd6SPeter Brune     }
6700298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
671be081cd6SPeter Brune   }
672baf369e7SPeter Brune   {
673baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
674baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
675baf369e7SPeter Brune       next = link->next;
676baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
677baf369e7SPeter Brune     }
6780298fd71SBarry Smith     (*dm)->gtolhook = NULL;
679baf369e7SPeter Brune   }
680d4d07f1eSToby Isaac   {
681d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
682d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
683d4d07f1eSToby Isaac       next = link->next;
684d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
685d4d07f1eSToby Isaac     }
686d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
687d4d07f1eSToby Isaac   }
688aa1993deSMatthew G Knepley   /* Destroy the work arrays */
689aa1993deSMatthew G Knepley   {
690aa1993deSMatthew G Knepley     DMWorkLink link,next;
691aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
692aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
693aa1993deSMatthew G Knepley       next = link->next;
694aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
695aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
696aa1993deSMatthew G Knepley     }
6970298fd71SBarry Smith     (*dm)->workin = NULL;
698aa1993deSMatthew G Knepley   }
699c58f1c22SToby Isaac   /* destroy the labels */
700f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
701f4cdcedcSVaclav Hapla   /* destroy the fields */
702e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
703f4cdcedcSVaclav Hapla   /* destroy the boundaries */
704e6f8dbb6SToby Isaac   {
705e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
706e6f8dbb6SToby Isaac     while (next) {
707e6f8dbb6SToby Isaac       DMBoundary b = next;
708e6f8dbb6SToby Isaac 
709e6f8dbb6SToby Isaac       next = b->next;
710e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
711e6f8dbb6SToby Isaac     }
712e6f8dbb6SToby Isaac   }
713b17ce1afSJed Brown 
71452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
71552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
71652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
71752536dc3SBarry Smith 
7181a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7191a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7201a266240SBarry Smith   }
72171cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7226bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7236bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
724073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
72588ed4aceSMatthew G Knepley 
7261bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7271bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7288b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
729fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
730fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
73188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7321bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
733736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
734736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7358e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
736736995cdSBlaise Bourdin     }
737736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
738736995cdSBlaise Bourdin   }
7399a2a23afSMatthew G. Knepley   {
7409a2a23afSMatthew G. Knepley     Vec     *auxData;
7419a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7429a2a23afSMatthew G. Knepley 
7439a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxGetSize((*dm)->auxData, &n);CHKERRQ(ierr);
7449a2a23afSMatthew G. Knepley     ierr = PetscMalloc1(n, &auxData);CHKERRQ(ierr);
7459a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxGetVals((*dm)->auxData, &off, auxData);CHKERRQ(ierr);
7469a2a23afSMatthew G. Knepley     for (i = 0; i < n; ++i) {ierr = VecDestroy(&auxData[i]);CHKERRQ(ierr);}
7479a2a23afSMatthew G. Knepley     ierr = PetscFree(auxData);CHKERRQ(ierr);
7489a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxDestroy(&(*dm)->auxData);CHKERRQ(ierr);
7499a2a23afSMatthew G. Knepley   }
75088bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
75188bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
75288bdff64SToby Isaac   }
7536eb26441SStefano Zampini 
754a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
75588bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
75688bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
75788bdff64SToby Isaac   }
75888bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
759f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7606636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7616636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7626636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
763412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
764412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
765412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
766ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
767ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
768ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7696636e97aSMatthew G Knepley 
770e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
77114f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
772e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
773e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
774732e2eb9SMatthew G Knepley 
775ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7766bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
777ed3c66a1SDave May   }
778c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
779435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
780732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
78147c6ae99SBarry Smith   PetscFunctionReturn(0);
78247c6ae99SBarry Smith }
78347c6ae99SBarry Smith 
784d7bf68aeSBarry Smith /*@
785d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
786d7bf68aeSBarry Smith 
787d083f849SBarry Smith     Collective on dm
788d7bf68aeSBarry Smith 
789d7bf68aeSBarry Smith     Input Parameter:
790d7bf68aeSBarry Smith .   dm - the DM object to setup
791d7bf68aeSBarry Smith 
792d7bf68aeSBarry Smith     Level: developer
793d7bf68aeSBarry Smith 
794e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
795d7bf68aeSBarry Smith 
796d7bf68aeSBarry Smith @*/
7977087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
798d7bf68aeSBarry Smith {
799d7bf68aeSBarry Smith   PetscErrorCode ierr;
800d7bf68aeSBarry Smith 
801d7bf68aeSBarry Smith   PetscFunctionBegin;
802171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8038387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
804d7bf68aeSBarry Smith   if (dm->ops->setup) {
805d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
806d7bf68aeSBarry Smith   }
8078387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
808d7bf68aeSBarry Smith   PetscFunctionReturn(0);
809d7bf68aeSBarry Smith }
810d7bf68aeSBarry Smith 
811d7bf68aeSBarry Smith /*@
812d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
813d7bf68aeSBarry Smith 
814d083f849SBarry Smith     Collective on dm
815d7bf68aeSBarry Smith 
816d7bf68aeSBarry Smith     Input Parameter:
817d7bf68aeSBarry Smith .   dm - the DM object to set options for
818d7bf68aeSBarry Smith 
819732e2eb9SMatthew G Knepley     Options Database:
8204164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8214164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8224164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8238f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
824732e2eb9SMatthew G Knepley 
8259318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8269318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8279318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
8289318fe57SMatthew G. Knepley . -dm_plex_shape <shape>            - The domain shape, such as DM_SHAPE_BOX, DM_SHAPE_SPHERE, etc.
8299318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8309318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8319318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
8329318fe57SMatthew G. Knepley . -dm_plex_simplex <bool>           - PETSC_TRUE for simplex elements, PETSC_FALSE for tensor elements
8339318fe57SMatthew G. Knepley . -dm_plex_interpolate <bool>       - PETSC_TRUE turns on topological interpolation (creating edges and faces)
8349318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8359318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8369318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8379318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
8389318fe57SMatthew G. Knepley . -dm_plex_box_bd <bx,by,bz>        - Specify the DMBoundaryType for each direction
8399318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8409318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8419318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8429318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
8439318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8449318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8459318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8469318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
8479318fe57SMatthew G. Knepley . -dm_extrude_layers <l>            - The number of layers to extrude
8489318fe57SMatthew G. Knepley . -dm_extrude_thickness <t>         - The thickness of the layer to be extruded
8499318fe57SMatthew G. Knepley . -dm_extrude_column_first <bool>   - Order the cells in a vertical column first
8509318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8519318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8529318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8539318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8549318fe57SMatthew G. Knepley 
855384a6580SVaclav Hapla     DMPLEX Specific Checks
856384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
857384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
858384a6580SVaclav 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()
859384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
860384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
861384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
862384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
863d7bf68aeSBarry Smith 
86495eb5ee5SVaclav Hapla     Level: intermediate
86595eb5ee5SVaclav Hapla 
86695eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
86795eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
868d7bf68aeSBarry Smith 
869d7bf68aeSBarry Smith @*/
8707087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
871d7bf68aeSBarry Smith {
8727781c08eSBarry Smith   char           typeName[256];
873ca266f36SBarry Smith   PetscBool      flg;
874d7bf68aeSBarry Smith   PetscErrorCode ierr;
875d7bf68aeSBarry Smith 
876d7bf68aeSBarry Smith   PetscFunctionBegin;
877171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87849be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
87949be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8801bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8813194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8820298fd71SBarry 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);
883a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
884f9ba7244SBarry Smith   if (flg) {
885f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
886f9ba7244SBarry Smith   }
887a264d7a6SBarry 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);
888073dac72SJed Brown   if (flg) {
889521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
890073dac72SJed Brown   }
8918f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
892f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
893e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
894f9ba7244SBarry Smith   }
895f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8960633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
89782fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
898d7bf68aeSBarry Smith   PetscFunctionReturn(0);
899d7bf68aeSBarry Smith }
900d7bf68aeSBarry Smith 
901fc9bc008SSatish Balay /*@C
902fe2efc57SMark    DMViewFromOptions - View from Options
903fe2efc57SMark 
904fe2efc57SMark    Collective on DM
905fe2efc57SMark 
906fe2efc57SMark    Input Parameters:
907fe2efc57SMark +  dm - the DM object
908736c3998SJose E. Roman .  obj - Optional object
909736c3998SJose E. Roman -  name - command line option
910fe2efc57SMark 
911fe2efc57SMark    Level: intermediate
912fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
913fe2efc57SMark @*/
914fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
915fe2efc57SMark {
916fe2efc57SMark   PetscErrorCode ierr;
917fe2efc57SMark 
918fe2efc57SMark   PetscFunctionBegin;
919fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
920fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
921fe2efc57SMark   PetscFunctionReturn(0);
922fe2efc57SMark }
923fe2efc57SMark 
924fe2efc57SMark /*@C
925224748a4SBarry Smith     DMView - Views a DM
92647c6ae99SBarry Smith 
927d083f849SBarry Smith     Collective on dm
92847c6ae99SBarry Smith 
92947c6ae99SBarry Smith     Input Parameter:
93047c6ae99SBarry Smith +   dm - the DM object to view
93147c6ae99SBarry Smith -   v - the viewer
93247c6ae99SBarry Smith 
933224748a4SBarry Smith     Level: beginner
93447c6ae99SBarry Smith 
935e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
93647c6ae99SBarry Smith 
93747c6ae99SBarry Smith @*/
9387087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
93947c6ae99SBarry Smith {
94047c6ae99SBarry Smith   PetscErrorCode    ierr;
94132c0f0efSBarry Smith   PetscBool         isbinary;
94276a8abe0SBarry Smith   PetscMPIInt       size;
94376a8abe0SBarry Smith   PetscViewerFormat format;
94447c6ae99SBarry Smith 
94547c6ae99SBarry Smith   PetscFunctionBegin;
946171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9473014e516SBarry Smith   if (!v) {
948ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9493014e516SBarry Smith   }
950b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
95174903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
95274903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
95374903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
95474903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
95574903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
95674903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
95774903a4fSStefano Zampini      in an error here */
95874903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9598bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
960b1b135c8SBarry Smith 
96176a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
962ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRMPI(ierr);
96376a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
96498c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
96532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
96632c0f0efSBarry Smith   if (isbinary) {
96755849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
96832c0f0efSBarry Smith     char     type[256];
96932c0f0efSBarry Smith 
970f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
97132c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
972f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
97332c0f0efSBarry Smith   }
9740c010503SBarry Smith   if (dm->ops->view) {
9750c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
97647c6ae99SBarry Smith   }
97747c6ae99SBarry Smith   PetscFunctionReturn(0);
97847c6ae99SBarry Smith }
97947c6ae99SBarry Smith 
98047c6ae99SBarry Smith /*@
9818472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
98247c6ae99SBarry Smith 
983d083f849SBarry Smith     Collective on dm
98447c6ae99SBarry Smith 
98547c6ae99SBarry Smith     Input Parameter:
98647c6ae99SBarry Smith .   dm - the DM object
98747c6ae99SBarry Smith 
98847c6ae99SBarry Smith     Output Parameter:
98947c6ae99SBarry Smith .   vec - the global vector
99047c6ae99SBarry Smith 
991073dac72SJed Brown     Level: beginner
99247c6ae99SBarry Smith 
993ec075b9fSPatrick Sanan .seealso DMCreateLocalVector(), DMGetGlobalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith @*/
9967087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
99747c6ae99SBarry Smith {
99847c6ae99SBarry Smith   PetscErrorCode ierr;
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith   PetscFunctionBegin;
1001171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1002b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1003b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
100447c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
100576bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1006c6b011d8SStefano Zampini     DM vdm;
1007c6b011d8SStefano Zampini 
1008c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1009c6b011d8SStefano 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);
1010c6b011d8SStefano Zampini   }
101147c6ae99SBarry Smith   PetscFunctionReturn(0);
101247c6ae99SBarry Smith }
101347c6ae99SBarry Smith 
101447c6ae99SBarry Smith /*@
10158472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
101647c6ae99SBarry Smith 
101747c6ae99SBarry Smith     Not Collective
101847c6ae99SBarry Smith 
101947c6ae99SBarry Smith     Input Parameter:
102047c6ae99SBarry Smith .   dm - the DM object
102147c6ae99SBarry Smith 
102247c6ae99SBarry Smith     Output Parameter:
102347c6ae99SBarry Smith .   vec - the local vector
102447c6ae99SBarry Smith 
1025073dac72SJed Brown     Level: beginner
102647c6ae99SBarry Smith 
1027ec075b9fSPatrick Sanan .seealso DMCreateGlobalVector(), DMGetLocalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
102847c6ae99SBarry Smith 
102947c6ae99SBarry Smith @*/
10307087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
103147c6ae99SBarry Smith {
103247c6ae99SBarry Smith   PetscErrorCode ierr;
103347c6ae99SBarry Smith 
103447c6ae99SBarry Smith   PetscFunctionBegin;
1035171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1036b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1037b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
103847c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
103976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1040c6b011d8SStefano Zampini     DM vdm;
1041c6b011d8SStefano Zampini 
1042c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1043c6b011d8SStefano 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);
1044c6b011d8SStefano Zampini   }
104547c6ae99SBarry Smith   PetscFunctionReturn(0);
104647c6ae99SBarry Smith }
104747c6ae99SBarry Smith 
10481411c6eeSJed Brown /*@
10491411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10501411c6eeSJed Brown 
1051d083f849SBarry Smith    Collective on dm
10521411c6eeSJed Brown 
10531411c6eeSJed Brown    Input Parameter:
10541411c6eeSJed Brown .  dm - the DM that provides the mapping
10551411c6eeSJed Brown 
10561411c6eeSJed Brown    Output Parameter:
10571411c6eeSJed Brown .  ltog - the mapping
10581411c6eeSJed Brown 
10591411c6eeSJed Brown    Level: intermediate
10601411c6eeSJed Brown 
10611411c6eeSJed Brown    Notes:
10621411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10631411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10641411c6eeSJed Brown 
1065fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10661411c6eeSJed Brown @*/
10677087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10681411c6eeSJed Brown {
10690be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10701411c6eeSJed Brown   PetscErrorCode ierr;
10711411c6eeSJed Brown 
10721411c6eeSJed Brown   PetscFunctionBegin;
10731411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10741411c6eeSJed Brown   PetscValidPointer(ltog,2);
10751411c6eeSJed Brown   if (!dm->ltogmap) {
107637d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
107737d0c07bSMatthew G Knepley 
107892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
107937d0c07bSMatthew G Knepley     if (section) {
1080a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
108137d0c07bSMatthew G Knepley       PetscInt       *ltog;
1082ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
108337d0c07bSMatthew G Knepley 
1084e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
108537d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1086ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1087ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
108837d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1089a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
109037d0c07bSMatthew G Knepley 
109137d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
109237d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1093a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1094a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
109537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10961a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10971a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10981a7dc684SMatthew G. Knepley         if (dof) {
1099b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1100b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
11011a7dc684SMatthew G. Knepley         }
110237d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1103a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1104a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
110537d0c07bSMatthew G Knepley         }
110637d0c07bSMatthew G Knepley       }
1107bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11080be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11090be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
11100be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11110be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
11127591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11137591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1114ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1115ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1116ccf3bd66SMatthew G. Knepley         n /= bs;
1117ccf3bd66SMatthew G. Knepley       }
1118ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
11193bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
112037d0c07bSMatthew G Knepley     } else {
1121b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1122184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
11231411c6eeSJed Brown     }
112437d0c07bSMatthew G Knepley   }
11251411c6eeSJed Brown   *ltog = dm->ltogmap;
11261411c6eeSJed Brown   PetscFunctionReturn(0);
11271411c6eeSJed Brown }
11281411c6eeSJed Brown 
11291411c6eeSJed Brown /*@
11301411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
11311411c6eeSJed Brown 
11321411c6eeSJed Brown    Not Collective
11331411c6eeSJed Brown 
11341411c6eeSJed Brown    Input Parameter:
11351411c6eeSJed Brown .  dm - the DM with block structure
11361411c6eeSJed Brown 
11371411c6eeSJed Brown    Output Parameter:
11381411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11391411c6eeSJed Brown 
11401411c6eeSJed Brown    Level: intermediate
11411411c6eeSJed Brown 
1142fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
11431411c6eeSJed Brown @*/
11447087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
11451411c6eeSJed Brown {
11461411c6eeSJed Brown   PetscFunctionBegin;
11471411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1148534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11491411c6eeSJed 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");
11501411c6eeSJed Brown   *bs = dm->bs;
11511411c6eeSJed Brown   PetscFunctionReturn(0);
11521411c6eeSJed Brown }
11531411c6eeSJed Brown 
115448eeb7c8SBarry Smith /*@C
11558472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
115647c6ae99SBarry Smith 
1157a5bc1bf3SBarry Smith     Collective on dmc
115847c6ae99SBarry Smith 
115947c6ae99SBarry Smith     Input Parameter:
1160a5bc1bf3SBarry Smith +   dmc - the DM object
1161a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
116247c6ae99SBarry Smith 
116347c6ae99SBarry Smith     Output Parameter:
116447c6ae99SBarry Smith +  mat - the interpolation
116547c6ae99SBarry Smith -  vec - the scaling (optional)
116647c6ae99SBarry Smith 
116747c6ae99SBarry Smith     Level: developer
116847c6ae99SBarry Smith 
116995452b02SPatrick Sanan     Notes:
117095452b02SPatrick 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
117185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1172d52bd9f3SBarry Smith 
11731f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1174d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
117585afcc9aSBarry Smith 
117685afcc9aSBarry Smith 
11772ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
117847c6ae99SBarry Smith 
117947c6ae99SBarry Smith @*/
1180a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
118147c6ae99SBarry Smith {
118247c6ae99SBarry Smith   PetscErrorCode ierr;
118347c6ae99SBarry Smith 
118447c6ae99SBarry Smith   PetscFunctionBegin;
1185a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1186a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1187c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1188a5bc1bf3SBarry Smith   if (!dmc->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1189a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1190a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1191a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
119247c6ae99SBarry Smith   PetscFunctionReturn(0);
119347c6ae99SBarry Smith }
119447c6ae99SBarry Smith 
11953ad4599aSBarry Smith /*@
1196d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11972ed6491fSPatrick Sanan 
11982ed6491fSPatrick Sanan   Input Parameters:
11992ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
12002ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
12012ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12022ed6491fSPatrick Sanan 
12032ed6491fSPatrick Sanan   Output Parameter:
12042ed6491fSPatrick Sanan .    scale - the scaled vector
12052ed6491fSPatrick Sanan 
12062ed6491fSPatrick Sanan   Level: developer
12072ed6491fSPatrick Sanan 
12082ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
12092ed6491fSPatrick Sanan 
12102ed6491fSPatrick Sanan @*/
12112ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
12122ed6491fSPatrick Sanan {
12132ed6491fSPatrick Sanan   PetscErrorCode ierr;
12142ed6491fSPatrick Sanan   Vec            fine;
12152ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
12162ed6491fSPatrick Sanan 
12172ed6491fSPatrick Sanan   PetscFunctionBegin;
12182ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
12192ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
12202ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
12212ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
12222ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
12232ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
12242ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12252ed6491fSPatrick Sanan }
12262ed6491fSPatrick Sanan 
12272ed6491fSPatrick Sanan /*@
12283ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
12293ad4599aSBarry Smith 
1230a5bc1bf3SBarry Smith     Collective on dmc
12313ad4599aSBarry Smith 
12323ad4599aSBarry Smith     Input Parameter:
1233a5bc1bf3SBarry Smith +   dmc - the DM object
1234a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
12353ad4599aSBarry Smith 
12363ad4599aSBarry Smith     Output Parameter:
12373ad4599aSBarry Smith .  mat - the restriction
12383ad4599aSBarry Smith 
12393ad4599aSBarry Smith 
12403ad4599aSBarry Smith     Level: developer
12413ad4599aSBarry Smith 
124295452b02SPatrick Sanan     Notes:
124395452b02SPatrick 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
12443ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
12453ad4599aSBarry Smith 
12463ad4599aSBarry Smith 
12473ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
12483ad4599aSBarry Smith 
12493ad4599aSBarry Smith @*/
1250a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12513ad4599aSBarry Smith {
12523ad4599aSBarry Smith   PetscErrorCode ierr;
12533ad4599aSBarry Smith 
12543ad4599aSBarry Smith   PetscFunctionBegin;
1255a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1256a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12575a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1258a5bc1bf3SBarry Smith   if (!dmc->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1259a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1260a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1261a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
12623ad4599aSBarry Smith   PetscFunctionReturn(0);
12633ad4599aSBarry Smith }
12643ad4599aSBarry Smith 
126547c6ae99SBarry Smith /*@
12668472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
126747c6ae99SBarry Smith 
1268a5bc1bf3SBarry Smith     Collective on dac
126947c6ae99SBarry Smith 
127047c6ae99SBarry Smith     Input Parameter:
1271a5bc1bf3SBarry Smith +   dac - the DM object
1272a5bc1bf3SBarry Smith -   daf - the second, finer DM object
127347c6ae99SBarry Smith 
127447c6ae99SBarry Smith     Output Parameter:
12756dbf9973SLawrence Mitchell .   mat - the injection
127647c6ae99SBarry Smith 
127747c6ae99SBarry Smith     Level: developer
127847c6ae99SBarry Smith 
127995452b02SPatrick Sanan    Notes:
128095452b02SPatrick 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
128185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
128285afcc9aSBarry Smith 
1283e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
128447c6ae99SBarry Smith 
128547c6ae99SBarry Smith @*/
1286a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
128747c6ae99SBarry Smith {
128847c6ae99SBarry Smith   PetscErrorCode ierr;
128947c6ae99SBarry Smith 
129047c6ae99SBarry Smith   PetscFunctionBegin;
1291a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1292a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
12935a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1294a5bc1bf3SBarry Smith   if (!dac->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1295a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1296a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1297a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
129847c6ae99SBarry Smith   PetscFunctionReturn(0);
129947c6ae99SBarry Smith }
130047c6ae99SBarry Smith 
1301b412c318SBarry Smith /*@
1302bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1303bd041c0cSMatthew G. Knepley 
1304a5bc1bf3SBarry Smith   Collective on dac
1305bd041c0cSMatthew G. Knepley 
1306bd041c0cSMatthew G. Knepley   Input Parameter:
1307a5bc1bf3SBarry Smith + dac - the DM object
1308a5bc1bf3SBarry Smith - daf - the second, finer DM object
1309bd041c0cSMatthew G. Knepley 
1310bd041c0cSMatthew G. Knepley   Output Parameter:
1311bd041c0cSMatthew G. Knepley . mat - the interpolation
1312bd041c0cSMatthew G. Knepley 
1313bd041c0cSMatthew G. Knepley   Level: developer
1314bd041c0cSMatthew G. Knepley 
1315bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1316bd041c0cSMatthew G. Knepley @*/
1317a5bc1bf3SBarry Smith PetscErrorCode DMCreateMassMatrix(DM dac, DM daf, Mat *mat)
1318bd041c0cSMatthew G. Knepley {
1319bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1320bd041c0cSMatthew G. Knepley 
1321bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1322a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1323a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13245a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1325a5bc1bf3SBarry Smith   if (!dac->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dac)->type_name);
1326a5bc1bf3SBarry Smith   ierr = (*dac->ops->createmassmatrix)(dac, daf, mat);CHKERRQ(ierr);
1327bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1328bd041c0cSMatthew G. Knepley }
1329bd041c0cSMatthew G. Knepley 
1330bd041c0cSMatthew G. Knepley /*@
1331b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
133247c6ae99SBarry Smith 
1333d083f849SBarry Smith     Collective on dm
133447c6ae99SBarry Smith 
133547c6ae99SBarry Smith     Input Parameter:
133647c6ae99SBarry Smith +   dm - the DM object
13375bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
133847c6ae99SBarry Smith 
133947c6ae99SBarry Smith     Output Parameter:
134047c6ae99SBarry Smith .   coloring - the coloring
134147c6ae99SBarry Smith 
1342ec5066bdSBarry Smith     Notes:
1343ec5066bdSBarry 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
1344ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1345ec5066bdSBarry Smith 
1346ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1347ec5066bdSBarry Smith 
134847c6ae99SBarry Smith     Level: developer
134947c6ae99SBarry Smith 
1350ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
135147c6ae99SBarry Smith 
1352aab9d709SJed Brown @*/
1353b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
135447c6ae99SBarry Smith {
135547c6ae99SBarry Smith   PetscErrorCode ierr;
135647c6ae99SBarry Smith 
135747c6ae99SBarry Smith   PetscFunctionBegin;
1358171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13595a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1360b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1361b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
136247c6ae99SBarry Smith   PetscFunctionReturn(0);
136347c6ae99SBarry Smith }
136447c6ae99SBarry Smith 
1365b412c318SBarry Smith /*@
13668472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
136747c6ae99SBarry Smith 
1368d083f849SBarry Smith     Collective on dm
136947c6ae99SBarry Smith 
137047c6ae99SBarry Smith     Input Parameter:
1371b412c318SBarry Smith .   dm - the DM object
137247c6ae99SBarry Smith 
137347c6ae99SBarry Smith     Output Parameter:
137447c6ae99SBarry Smith .   mat - the empty Jacobian
137547c6ae99SBarry Smith 
1376073dac72SJed Brown     Level: beginner
137747c6ae99SBarry Smith 
137895452b02SPatrick Sanan     Notes:
137995452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
138094013140SBarry Smith        do not need to do it yourself.
138194013140SBarry Smith 
138294013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13837889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
138494013140SBarry Smith 
138594013140SBarry 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
138694013140SBarry Smith        internally by PETSc.
138794013140SBarry Smith 
138894013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1389aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
139094013140SBarry Smith 
1391b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
139247c6ae99SBarry Smith 
1393aab9d709SJed Brown @*/
1394b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
139547c6ae99SBarry Smith {
139647c6ae99SBarry Smith   PetscErrorCode ierr;
139747c6ae99SBarry Smith 
139847c6ae99SBarry Smith   PetscFunctionBegin;
1399171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1400064a246eSJacob Faibussowitsch   PetscValidPointer(mat,2);
1401b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
14025a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1403fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1404b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
140576bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1406c6b011d8SStefano Zampini     DM mdm;
1407c6b011d8SStefano Zampini 
1408c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1409c6b011d8SStefano 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);
1410c6b011d8SStefano Zampini   }
1411e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1412e5e52638SMatthew G. Knepley   if (dm->Nf) {
1413e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1414649ef022SMatthew Knepley     PetscInt     Nf, f;
1415e571a35bSMatthew G. Knepley 
1416e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1417649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1418649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
1419649ef022SMatthew Knepley         ierr = (*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1420e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1421e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1422649ef022SMatthew Knepley         break;
1423e571a35bSMatthew G. Knepley       }
1424649ef022SMatthew Knepley     }
1425649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1426649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
1427649ef022SMatthew Knepley         ierr = (*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1428e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1429e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1430e571a35bSMatthew G. Knepley       }
1431e571a35bSMatthew G. Knepley     }
1432e571a35bSMatthew G. Knepley   }
1433fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
143447c6ae99SBarry Smith   PetscFunctionReturn(0);
143547c6ae99SBarry Smith }
143647c6ae99SBarry Smith 
1437732e2eb9SMatthew G Knepley /*@
1438950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1439732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1440732e2eb9SMatthew G Knepley 
1441d083f849SBarry Smith   Logically Collective on dm
1442732e2eb9SMatthew G Knepley 
1443732e2eb9SMatthew G Knepley   Input Parameter:
1444732e2eb9SMatthew G Knepley + dm - the DM
1445732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1446732e2eb9SMatthew G Knepley 
1447732e2eb9SMatthew G Knepley   Level: developer
1448b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1449732e2eb9SMatthew G Knepley @*/
1450732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1451732e2eb9SMatthew G Knepley {
1452732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1453732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1454732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1455732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1456732e2eb9SMatthew G Knepley }
1457732e2eb9SMatthew G Knepley 
1458b06ff27eSHong Zhang /*@
1459b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1460b06ff27eSHong Zhang     but the array for values will not be allocated.
1461b06ff27eSHong Zhang 
1462d083f849SBarry Smith   Logically Collective on dm
1463b06ff27eSHong Zhang 
1464b06ff27eSHong Zhang   Input Parameter:
1465b06ff27eSHong Zhang + dm - the DM
1466b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1467b06ff27eSHong Zhang 
1468b06ff27eSHong Zhang   Level: developer
1469b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1470b06ff27eSHong Zhang @*/
1471b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1472b06ff27eSHong Zhang {
1473b06ff27eSHong Zhang   PetscFunctionBegin;
1474b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1475b06ff27eSHong Zhang   dm->structure_only = only;
1476b06ff27eSHong Zhang   PetscFunctionReturn(0);
1477b06ff27eSHong Zhang }
1478b06ff27eSHong Zhang 
1479a89ea682SMatthew G Knepley /*@C
1480aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1481a89ea682SMatthew G Knepley 
1482a89ea682SMatthew G Knepley   Not Collective
1483a89ea682SMatthew G Knepley 
1484a89ea682SMatthew G Knepley   Input Parameters:
1485a89ea682SMatthew G Knepley + dm - the DM object
1486aa1993deSMatthew G Knepley . count - The minium size
148769291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1488a89ea682SMatthew G Knepley 
1489a89ea682SMatthew G Knepley   Output Parameter:
1490a89ea682SMatthew G Knepley . array - the work array
1491a89ea682SMatthew G Knepley 
1492a89ea682SMatthew G Knepley   Level: developer
1493a89ea682SMatthew G Knepley 
1494a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1495a89ea682SMatthew G Knepley @*/
149669291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1497a89ea682SMatthew G Knepley {
1498a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1499aa1993deSMatthew G Knepley   DMWorkLink     link;
150069291d52SBarry Smith   PetscMPIInt    dsize;
1501a89ea682SMatthew G Knepley 
1502a89ea682SMatthew G Knepley   PetscFunctionBegin;
1503a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1504aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1505aa1993deSMatthew G Knepley   if (dm->workin) {
1506aa1993deSMatthew G Knepley     link       = dm->workin;
1507aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1508aa1993deSMatthew G Knepley   } else {
1509b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1510a89ea682SMatthew G Knepley   }
1511ffc4695bSBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRMPI(ierr);
15125056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1513aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1514854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1515854ce69bSBarry Smith     link->bytes = dsize*count;
1516aa1993deSMatthew G Knepley   }
1517aa1993deSMatthew G Knepley   link->next   = dm->workout;
1518aa1993deSMatthew G Knepley   dm->workout  = link;
151900d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
152000d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
152100d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
152200d952a4SJed Brown #endif
1523aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1524a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1525a89ea682SMatthew G Knepley }
1526a89ea682SMatthew G Knepley 
1527aa1993deSMatthew G Knepley /*@C
1528aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1529aa1993deSMatthew G Knepley 
1530aa1993deSMatthew G Knepley   Not Collective
1531aa1993deSMatthew G Knepley 
1532aa1993deSMatthew G Knepley   Input Parameters:
1533aa1993deSMatthew G Knepley + dm - the DM object
1534aa1993deSMatthew G Knepley . count - The minium size
153569291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1536aa1993deSMatthew G Knepley 
1537aa1993deSMatthew G Knepley   Output Parameter:
1538aa1993deSMatthew G Knepley . array - the work array
1539aa1993deSMatthew G Knepley 
1540aa1993deSMatthew G Knepley   Level: developer
1541aa1993deSMatthew G Knepley 
154295452b02SPatrick Sanan   Developer Notes:
154395452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1544aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1545aa1993deSMatthew G Knepley @*/
154669291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1547aa1993deSMatthew G Knepley {
1548aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1549aa1993deSMatthew G Knepley 
1550aa1993deSMatthew G Knepley   PetscFunctionBegin;
1551aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1552aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1553aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1554aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1555aa1993deSMatthew G Knepley       *p           = link->next;
1556aa1993deSMatthew G Knepley       link->next   = dm->workin;
1557aa1993deSMatthew G Knepley       dm->workin   = link;
15580298fd71SBarry Smith       *(void**)mem = NULL;
1559aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1560aa1993deSMatthew G Knepley     }
1561aa1993deSMatthew G Knepley   }
1562aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1563aa1993deSMatthew G Knepley }
1564e7c4fc90SDmitry Karpeev 
15658cda7954SMatthew G. Knepley /*@C
15668cda7954SMatthew G. Knepley   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field
15678cda7954SMatthew G. Knepley 
15688cda7954SMatthew G. Knepley   Logically collective on DM
15698cda7954SMatthew G. Knepley 
15708cda7954SMatthew G. Knepley   Input Parameters:
15718cda7954SMatthew G. Knepley + dm     - The DM
15728cda7954SMatthew G. Knepley . field  - The field number for the nullspace
15738cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
15748cda7954SMatthew G. Knepley 
15758cda7954SMatthew G. Knepley   Notes:
15768cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15778cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15788cda7954SMatthew G. Knepley $ dm        - The present DM
15798cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15808cda7954SMatthew G. Knepley $ field     - The field number in dm
15818cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15828cda7954SMatthew G. Knepley 
15838cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
15848cda7954SMatthew G. Knepley 
15858cda7954SMatthew G. Knepley .seealso: DMGetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
15868cda7954SMatthew G. Knepley */
15878cda7954SMatthew G. Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1588435a35e8SMatthew G Knepley {
1589435a35e8SMatthew G Knepley   PetscFunctionBegin;
1590435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
159182f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1592435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1593435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1594435a35e8SMatthew G Knepley }
1595435a35e8SMatthew G Knepley 
15968cda7954SMatthew G. Knepley /*@C
15978cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
15988cda7954SMatthew G. Knepley 
15998cda7954SMatthew G. Knepley   Not collective
16008cda7954SMatthew G. Knepley 
16018cda7954SMatthew G. Knepley   Input Parameters:
16028cda7954SMatthew G. Knepley + dm     - The DM
16038cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16048cda7954SMatthew G. Knepley 
16058cda7954SMatthew G. Knepley   Output Parameter:
16068cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
16078cda7954SMatthew G. Knepley 
16088cda7954SMatthew G. Knepley   Notes:
16098cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16108cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16118cda7954SMatthew G. Knepley $ dm        - The present DM
16128cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16138cda7954SMatthew G. Knepley $ field     - The field number in dm
16148cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16158cda7954SMatthew G. Knepley 
16168cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16178cda7954SMatthew G. Knepley 
16188cda7954SMatthew G. Knepley .seealso: DMSetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16198cda7954SMatthew G. Knepley */
16208cda7954SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
16210a50eb56SMatthew G. Knepley {
16220a50eb56SMatthew G. Knepley   PetscFunctionBegin;
16230a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1624f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
16250a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
16260a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
16270a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
16280a50eb56SMatthew G. Knepley }
16290a50eb56SMatthew G. Knepley 
16308cda7954SMatthew G. Knepley /*@C
16318cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
16328cda7954SMatthew G. Knepley 
16338cda7954SMatthew G. Knepley   Logically collective on DM
16348cda7954SMatthew G. Knepley 
16358cda7954SMatthew G. Knepley   Input Parameters:
16368cda7954SMatthew G. Knepley + dm     - The DM
16378cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16388cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
16398cda7954SMatthew G. Knepley 
16408cda7954SMatthew G. Knepley   Notes:
16418cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16428cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16438cda7954SMatthew G. Knepley $ dm        - The present DM
16448cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16458cda7954SMatthew G. Knepley $ field     - The field number in dm
16468cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16478cda7954SMatthew G. Knepley 
16488cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16498cda7954SMatthew G. Knepley 
16508cda7954SMatthew G. Knepley .seealso: DMGetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16518cda7954SMatthew G. Knepley */
16528cda7954SMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1653f9d4088aSMatthew G. Knepley {
1654f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1655f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1656f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1657f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1658f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1659f9d4088aSMatthew G. Knepley }
1660f9d4088aSMatthew G. Knepley 
16618cda7954SMatthew G. Knepley /*@C
16628cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
16638cda7954SMatthew G. Knepley 
16648cda7954SMatthew G. Knepley   Not collective
16658cda7954SMatthew G. Knepley 
16668cda7954SMatthew G. Knepley   Input Parameters:
16678cda7954SMatthew G. Knepley + dm     - The DM
16688cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16698cda7954SMatthew G. Knepley 
16708cda7954SMatthew G. Knepley   Output Parameter:
16718cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
16728cda7954SMatthew G. Knepley 
16738cda7954SMatthew G. Knepley   Notes:
16748cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16758cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16768cda7954SMatthew G. Knepley $ dm        - The present DM
16778cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16788cda7954SMatthew G. Knepley $ field     - The field number in dm
16798cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16808cda7954SMatthew G. Knepley 
16818cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16828cda7954SMatthew G. Knepley 
16838cda7954SMatthew G. Knepley .seealso: DMSetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16848cda7954SMatthew G. Knepley */
16858cda7954SMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1686f9d4088aSMatthew G. Knepley {
1687f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1688f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1689f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1690f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1691f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1692f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1693f9d4088aSMatthew G. Knepley }
1694f9d4088aSMatthew G. Knepley 
16954f3b5142SJed Brown /*@C
16964d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
16974d343eeaSMatthew G Knepley 
16984d343eeaSMatthew G Knepley   Not collective
16994d343eeaSMatthew G Knepley 
17004d343eeaSMatthew G Knepley   Input Parameter:
17014d343eeaSMatthew G Knepley . dm - the DM object
17024d343eeaSMatthew G Knepley 
17034d343eeaSMatthew G Knepley   Output Parameters:
17040298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
17050298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
17060298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
17074d343eeaSMatthew G Knepley 
17084d343eeaSMatthew G Knepley   Level: intermediate
17094d343eeaSMatthew G Knepley 
171021c9b008SJed Brown   Notes:
171121c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
171221c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
171321c9b008SJed Brown   PetscFree().
171421c9b008SJed Brown 
17154d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
17164d343eeaSMatthew G Knepley @*/
171737d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
17184d343eeaSMatthew G Knepley {
171937d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
17204d343eeaSMatthew G Knepley   PetscErrorCode ierr;
17214d343eeaSMatthew G Knepley 
17224d343eeaSMatthew G Knepley   PetscFunctionBegin;
17234d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
172469ca1f37SDmitry Karpeev   if (numFields) {
1725534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
172669ca1f37SDmitry Karpeev     *numFields = 0;
172769ca1f37SDmitry Karpeev   }
172837d0c07bSMatthew G Knepley   if (fieldNames) {
172937d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
17300298fd71SBarry Smith     *fieldNames = NULL;
173169ca1f37SDmitry Karpeev   }
173269ca1f37SDmitry Karpeev   if (fields) {
173369ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
17340298fd71SBarry Smith     *fields = NULL;
173569ca1f37SDmitry Karpeev   }
173692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
173737d0c07bSMatthew G Knepley   if (section) {
17383a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
173937d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
174037d0c07bSMatthew G Knepley 
1741e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
174237d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
17433a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
174437d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
174537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
174637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
17473a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
174837d0c07bSMatthew G Knepley     }
174937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
175037d0c07bSMatthew G Knepley       PetscInt gdof;
175137d0c07bSMatthew G Knepley 
175237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
175337d0c07bSMatthew G Knepley       if (gdof > 0) {
175437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
17553a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
175637d0c07bSMatthew G Knepley 
175737d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
175837d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
17593a544194SStefano Zampini           fpdof = fdof-fcdof;
17603a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
17613a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
17623a544194SStefano Zampini             fieldNc[f] = 1;
17633a544194SStefano Zampini           }
17643a544194SStefano Zampini           fieldSizes[f] += fpdof;
176537d0c07bSMatthew G Knepley         }
176637d0c07bSMatthew G Knepley       }
176737d0c07bSMatthew G Knepley     }
176837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1769785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
177037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
177137d0c07bSMatthew G Knepley     }
177237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
177337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
177437d0c07bSMatthew G Knepley 
177537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
177637d0c07bSMatthew G Knepley       if (gdof > 0) {
177737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
177837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
177937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
178037d0c07bSMatthew G Knepley 
178137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
178237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
178337d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
178437d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
178537d0c07bSMatthew G Knepley           }
178637d0c07bSMatthew G Knepley         }
178737d0c07bSMatthew G Knepley       }
178837d0c07bSMatthew G Knepley     }
17898865f1eaSKarl Rupp     if (numFields) *numFields = nF;
179037d0c07bSMatthew G Knepley     if (fieldNames) {
1791785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
179237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
179337d0c07bSMatthew G Knepley         const char *fieldName;
179437d0c07bSMatthew G Knepley 
179537d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
179637d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
179737d0c07bSMatthew G Knepley       }
179837d0c07bSMatthew G Knepley     }
179937d0c07bSMatthew G Knepley     if (fields) {
1800785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
180137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
18023a544194SStefano Zampini         PetscInt bs, in[2], out[2];
18033a544194SStefano Zampini 
180482f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
18053a544194SStefano Zampini         in[0] = -fieldNc[f];
18063a544194SStefano Zampini         in[1] = fieldNc[f];
1807820f2d46SBarry Smith         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
18083a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
18093a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
181037d0c07bSMatthew G Knepley       }
181137d0c07bSMatthew G Knepley     }
18123a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
18138865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
18148865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
181569ca1f37SDmitry Karpeev   }
18164d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
18174d343eeaSMatthew G Knepley }
18184d343eeaSMatthew G Knepley 
181916621825SDmitry Karpeev 
182016621825SDmitry Karpeev /*@C
182116621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
182216621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
182316621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1824e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1825e7c4fc90SDmitry Karpeev 
1826e7c4fc90SDmitry Karpeev   Not collective
1827e7c4fc90SDmitry Karpeev 
1828e7c4fc90SDmitry Karpeev   Input Parameter:
1829e7c4fc90SDmitry Karpeev . dm - the DM object
1830e7c4fc90SDmitry Karpeev 
1831e7c4fc90SDmitry Karpeev   Output Parameters:
18320298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
18330298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
18340298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
18350298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1836e7c4fc90SDmitry Karpeev 
1837e7c4fc90SDmitry Karpeev   Level: intermediate
1838e7c4fc90SDmitry Karpeev 
1839e7c4fc90SDmitry Karpeev   Notes:
1840e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1841e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1842e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1843e7c4fc90SDmitry Karpeev 
1844e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1845e7c4fc90SDmitry Karpeev @*/
184616621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1847e7c4fc90SDmitry Karpeev {
1848e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1849e7c4fc90SDmitry Karpeev 
1850e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1851e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18528865f1eaSKarl Rupp   if (len) {
1853534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
18548865f1eaSKarl Rupp     *len = 0;
18558865f1eaSKarl Rupp   }
18568865f1eaSKarl Rupp   if (namelist) {
18578865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1858ea78f98cSLisandro Dalcin     *namelist = NULL;
18598865f1eaSKarl Rupp   }
18608865f1eaSKarl Rupp   if (islist) {
18618865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1862ea78f98cSLisandro Dalcin     *islist = NULL;
18638865f1eaSKarl Rupp   }
18648865f1eaSKarl Rupp   if (dmlist) {
18658865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1866ea78f98cSLisandro Dalcin     *dmlist = NULL;
18678865f1eaSKarl Rupp   }
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->createfielddecomposition) {
1875435a35e8SMatthew G Knepley     PetscSection section;
1876435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1877435a35e8SMatthew G Knepley 
187892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1879435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1880435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1881f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
188203dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
188303dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
188403dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1885435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1886435a35e8SMatthew G Knepley         const char *fieldName;
1887435a35e8SMatthew G Knepley 
188803dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
188903dc3394SMatthew G. Knepley         if (namelist) {
1890435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1891435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1892435a35e8SMatthew G Knepley         }
189303dc3394SMatthew G. Knepley       }
1894435a35e8SMatthew G Knepley     } else {
189569ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1896e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
18970298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1898e7c4fc90SDmitry Karpeev     }
18998865f1eaSKarl Rupp   } else {
190016621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
190116621825SDmitry Karpeev   }
190216621825SDmitry Karpeev   PetscFunctionReturn(0);
190316621825SDmitry Karpeev }
190416621825SDmitry Karpeev 
1905564cec59SMatthew G. Knepley /*@
1906435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1907435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1908435a35e8SMatthew G Knepley 
1909435a35e8SMatthew G Knepley   Not collective
1910435a35e8SMatthew G Knepley 
1911435a35e8SMatthew G Knepley   Input Parameters:
19122adcc780SMatthew G. Knepley + dm        - The DM object
19132adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
19142adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1915435a35e8SMatthew G Knepley 
1916435a35e8SMatthew G Knepley   Output Parameters:
19172adcc780SMatthew G. Knepley + is - The global indices for the subproblem
19182adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1919435a35e8SMatthew G Knepley 
19205d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19215d3b26e6SMatthew G. Knepley 
1922435a35e8SMatthew G Knepley   Level: intermediate
1923435a35e8SMatthew G Knepley 
19245d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1925435a35e8SMatthew G Knepley @*/
192637bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1927435a35e8SMatthew G Knepley {
1928435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1929435a35e8SMatthew G Knepley 
1930435a35e8SMatthew G Knepley   PetscFunctionBegin;
1931435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1932435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
19338865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
19348865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1935b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1936435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1937435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1938435a35e8SMatthew G Knepley }
1939435a35e8SMatthew G Knepley 
19402adcc780SMatthew G. Knepley /*@C
19412adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
19422adcc780SMatthew G. Knepley 
19432adcc780SMatthew G. Knepley   Not collective
19442adcc780SMatthew G. Knepley 
19452adcc780SMatthew G. Knepley   Input Parameter:
19462adcc780SMatthew G. Knepley + dms - The DM objects
19472adcc780SMatthew G. Knepley - len - The number of DMs
19482adcc780SMatthew G. Knepley 
19492adcc780SMatthew G. Knepley   Output Parameters:
1950a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
19512adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
19522adcc780SMatthew G. Knepley 
19535d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19545d3b26e6SMatthew G. Knepley 
19552adcc780SMatthew G. Knepley   Level: intermediate
19562adcc780SMatthew G. Knepley 
19575d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
19582adcc780SMatthew G. Knepley @*/
19592adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
19602adcc780SMatthew G. Knepley {
19612adcc780SMatthew G. Knepley   PetscInt       i;
19622adcc780SMatthew G. Knepley   PetscErrorCode ierr;
19632adcc780SMatthew G. Knepley 
19642adcc780SMatthew G. Knepley   PetscFunctionBegin;
19652adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
19662adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
19672adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1968a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
19692adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
19702adcc780SMatthew G. Knepley   if (len) {
1971b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1972b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1973b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
19742adcc780SMatthew G. Knepley   }
19752adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
19762adcc780SMatthew G. Knepley }
19772adcc780SMatthew G. Knepley 
197816621825SDmitry Karpeev 
197916621825SDmitry Karpeev /*@C
19808d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
19818d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
19828d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
19838d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
19848d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
198516621825SDmitry Karpeev 
198616621825SDmitry Karpeev   Not collective
198716621825SDmitry Karpeev 
198816621825SDmitry Karpeev   Input Parameter:
198916621825SDmitry Karpeev . dm - the DM object
199016621825SDmitry Karpeev 
199116621825SDmitry Karpeev   Output Parameters:
19920298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
19930298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
19940298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
19950298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
19960298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
199716621825SDmitry Karpeev 
199816621825SDmitry Karpeev   Level: intermediate
199916621825SDmitry Karpeev 
200016621825SDmitry Karpeev   Notes:
200116621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
200216621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
200316621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
200416621825SDmitry Karpeev 
2005245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
200616621825SDmitry Karpeev @*/
20078d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
200816621825SDmitry Karpeev {
200916621825SDmitry Karpeev   PetscErrorCode      ierr;
2010be081cd6SPeter Brune   DMSubDomainHookLink link;
2011be081cd6SPeter Brune   PetscInt            i,l;
201216621825SDmitry Karpeev 
201316621825SDmitry Karpeev   PetscFunctionBegin;
201416621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
201514a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
20160298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
20170298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
20180298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
20190298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
2020f3f0edfdSDmitry Karpeev   /*
2021f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2022f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2023f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2024f3f0edfdSDmitry Karpeev    */
2025ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
202616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2027be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
202814a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2029f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2030be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2031be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
2032be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
2033be081cd6SPeter Brune         }
2034648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2035e7c4fc90SDmitry Karpeev       }
203614a18fd3SPeter Brune     }
203714a18fd3SPeter Brune     if (len) *len = l;
203814a18fd3SPeter Brune   }
2039e30e807fSPeter Brune   PetscFunctionReturn(0);
2040e30e807fSPeter Brune }
2041e30e807fSPeter Brune 
2042e30e807fSPeter Brune 
2043e30e807fSPeter Brune /*@C
2044e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2045e30e807fSPeter Brune 
2046e30e807fSPeter Brune   Not collective
2047e30e807fSPeter Brune 
2048e30e807fSPeter Brune   Input Parameters:
2049e30e807fSPeter Brune + dm - the DM object
2050e30e807fSPeter Brune . n  - the number of subdomain scatters
2051e30e807fSPeter Brune - subdms - the local subdomains
2052e30e807fSPeter Brune 
2053e30e807fSPeter Brune   Output Parameters:
2054e30e807fSPeter Brune + n     - the number of scatters returned
2055e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2056e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2057e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2058e30e807fSPeter Brune 
205995452b02SPatrick Sanan   Notes:
206095452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2061e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2062e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2063e30e807fSPeter Brune   solution and residual data.
2064e30e807fSPeter Brune 
2065e30e807fSPeter Brune   Level: developer
2066e30e807fSPeter Brune 
2067e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2068e30e807fSPeter Brune @*/
2069e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2070e30e807fSPeter Brune {
2071e30e807fSPeter Brune   PetscErrorCode ierr;
2072e30e807fSPeter Brune 
2073e30e807fSPeter Brune   PetscFunctionBegin;
2074e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2075e30e807fSPeter Brune   PetscValidPointer(subdms,3);
2076b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
2077e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
2078e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2079e7c4fc90SDmitry Karpeev }
2080e7c4fc90SDmitry Karpeev 
208147c6ae99SBarry Smith /*@
208247c6ae99SBarry Smith   DMRefine - Refines a DM object
208347c6ae99SBarry Smith 
2084d083f849SBarry Smith   Collective on dm
208547c6ae99SBarry Smith 
208647c6ae99SBarry Smith   Input Parameter:
208747c6ae99SBarry Smith + dm   - the DM object
208891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
208947c6ae99SBarry Smith 
209047c6ae99SBarry Smith   Output Parameter:
20910298fd71SBarry Smith . dmf - the refined DM, or NULL
2092ae0a1c52SMatthew G Knepley 
2093412e9a14SMatthew G. Knepley   Options Dtabase Keys:
2094412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2095412e9a14SMatthew G. Knepley 
20960298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
209747c6ae99SBarry Smith 
209847c6ae99SBarry Smith   Level: developer
209947c6ae99SBarry Smith 
2100e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
210147c6ae99SBarry Smith @*/
21027087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
210347c6ae99SBarry Smith {
210447c6ae99SBarry Smith   PetscErrorCode   ierr;
2105c833c3b5SJed Brown   DMRefineHookLink link;
210647c6ae99SBarry Smith 
210747c6ae99SBarry Smith   PetscFunctionBegin;
2108732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2109b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
21101ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
211147c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
21124057135bSMatthew G Knepley   if (*dmf) {
211343842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
21148865f1eaSKarl Rupp 
21158cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
21168865f1eaSKarl Rupp 
2117644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
21180598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2119656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
21208865f1eaSKarl Rupp 
2121e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
2122c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
21238865f1eaSKarl Rupp       if (link->refinehook) {
21248865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
21258865f1eaSKarl Rupp       }
2126c833c3b5SJed Brown     }
2127c833c3b5SJed Brown   }
21281ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
2129c833c3b5SJed Brown   PetscFunctionReturn(0);
2130c833c3b5SJed Brown }
2131c833c3b5SJed Brown 
2132bb9467b5SJed Brown /*@C
2133c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2134c833c3b5SJed Brown 
2135c833c3b5SJed Brown    Logically Collective
2136c833c3b5SJed Brown 
2137c833c3b5SJed Brown    Input Arguments:
2138c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2139c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2140c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21410298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2142c833c3b5SJed Brown 
2143c833c3b5SJed Brown    Calling sequence of refinehook:
2144c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2145c833c3b5SJed Brown 
2146c833c3b5SJed Brown +  coarse - coarse level DM
2147c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2148c833c3b5SJed Brown -  ctx - optional user-defined function context
2149c833c3b5SJed Brown 
2150c833c3b5SJed Brown    Calling sequence for interphook:
2151c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2152c833c3b5SJed Brown 
2153c833c3b5SJed Brown +  coarse - coarse level DM
2154c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2155c833c3b5SJed Brown .  fine - fine level DM to update
2156c833c3b5SJed Brown -  ctx - optional user-defined function context
2157c833c3b5SJed Brown 
2158c833c3b5SJed Brown    Level: advanced
2159c833c3b5SJed Brown 
2160c833c3b5SJed Brown    Notes:
2161c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2162c833c3b5SJed Brown 
2163c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2164c833c3b5SJed Brown 
2165bb9467b5SJed Brown    This function is currently not available from Fortran.
2166bb9467b5SJed Brown 
2167c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2168c833c3b5SJed Brown @*/
2169c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2170c833c3b5SJed Brown {
2171c833c3b5SJed Brown   PetscErrorCode   ierr;
2172c833c3b5SJed Brown   DMRefineHookLink link,*p;
2173c833c3b5SJed Brown 
2174c833c3b5SJed Brown   PetscFunctionBegin;
2175c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
21763d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
21773d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
21783d8e3701SJed Brown   }
217995dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2180c833c3b5SJed Brown   link->refinehook = refinehook;
2181c833c3b5SJed Brown   link->interphook = interphook;
2182c833c3b5SJed Brown   link->ctx        = ctx;
21830298fd71SBarry Smith   link->next       = NULL;
2184c833c3b5SJed Brown   *p               = link;
2185c833c3b5SJed Brown   PetscFunctionReturn(0);
2186c833c3b5SJed Brown }
2187c833c3b5SJed Brown 
21883d8e3701SJed Brown /*@C
21893d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
21903d8e3701SJed Brown 
21913d8e3701SJed Brown    Logically Collective
21923d8e3701SJed Brown 
21933d8e3701SJed Brown    Input Arguments:
21943d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
21953d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
21963d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21973d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
21983d8e3701SJed Brown 
21993d8e3701SJed Brown    Level: advanced
22003d8e3701SJed Brown 
22013d8e3701SJed Brown    Notes:
22023d8e3701SJed Brown    This function does nothing if the hook is not in the list.
22033d8e3701SJed Brown 
22043d8e3701SJed Brown    This function is currently not available from Fortran.
22053d8e3701SJed Brown 
22063d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
22073d8e3701SJed Brown @*/
22083d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
22093d8e3701SJed Brown {
22103d8e3701SJed Brown   PetscErrorCode   ierr;
22113d8e3701SJed Brown   DMRefineHookLink link,*p;
22123d8e3701SJed Brown 
22133d8e3701SJed Brown   PetscFunctionBegin;
22143d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22153d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
22163d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
22173d8e3701SJed Brown       link = *p;
22183d8e3701SJed Brown       *p = link->next;
22193d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
22203d8e3701SJed Brown       break;
22213d8e3701SJed Brown     }
22223d8e3701SJed Brown   }
22233d8e3701SJed Brown   PetscFunctionReturn(0);
22243d8e3701SJed Brown }
22253d8e3701SJed Brown 
2226c833c3b5SJed Brown /*@
2227c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2228c833c3b5SJed Brown 
2229c833c3b5SJed Brown    Collective if any hooks are
2230c833c3b5SJed Brown 
2231c833c3b5SJed Brown    Input Arguments:
2232c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2233e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2234c833c3b5SJed Brown -  fine - finer DM to update
2235c833c3b5SJed Brown 
2236c833c3b5SJed Brown    Level: developer
2237c833c3b5SJed Brown 
2238c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2239c833c3b5SJed Brown @*/
2240c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2241c833c3b5SJed Brown {
2242c833c3b5SJed Brown   PetscErrorCode   ierr;
2243c833c3b5SJed Brown   DMRefineHookLink link;
2244c833c3b5SJed Brown 
2245c833c3b5SJed Brown   PetscFunctionBegin;
2246c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
22478865f1eaSKarl Rupp     if (link->interphook) {
22488865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
22498865f1eaSKarl Rupp     }
22504057135bSMatthew G Knepley   }
225147c6ae99SBarry Smith   PetscFunctionReturn(0);
225247c6ae99SBarry Smith }
225347c6ae99SBarry Smith 
2254eb3f98d2SBarry Smith /*@
22551f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
22561f3379b2SToby Isaac 
22571f3379b2SToby Isaac    Collective on DM
22581f3379b2SToby Isaac 
22591f3379b2SToby Isaac    Input Arguments:
22601f3379b2SToby Isaac +  coarse - coarse DM
22611f3379b2SToby Isaac .  fine   - fine DM
22621f3379b2SToby Isaac .  interp - (optional) the matrix computed by DMCreateInterpolation().  Implementations may not need this, but if it
22631f3379b2SToby Isaac             is available it can avoid some recomputation.  If it is provided, MatInterpolate() will be used if
22641f3379b2SToby Isaac             the coarse DM does not have a specialized implementation.
22651f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
22661f3379b2SToby Isaac 
22671f3379b2SToby Isaac    Output Arguments:
22681f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
22691f3379b2SToby Isaac 
22701f3379b2SToby Isaac    Level: developer
22711f3379b2SToby Isaac 
22721f3379b2SToby Isaac    Note: This function exists because the interpolation of a solution vector between meshes is not always a linear
22731f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
22741f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
22751f3379b2SToby Isaac    slope-limiting reconstruction.
22761f3379b2SToby Isaac 
22771f3379b2SToby Isaac .seealso DMInterpolate(), DMCreateInterpolation()
22781f3379b2SToby Isaac @*/
22791f3379b2SToby Isaac PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
22801f3379b2SToby Isaac {
22811f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM,DM,Mat,Vec,Vec) = NULL;
22821f3379b2SToby Isaac   PetscErrorCode ierr;
22831f3379b2SToby Isaac 
22841f3379b2SToby Isaac   PetscFunctionBegin;
22851f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22861f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp,MAT_CLASSID,3);
22871f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol,VEC_CLASSID,4);
22881f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol,VEC_CLASSID,5);
22891f3379b2SToby Isaac 
22901f3379b2SToby Isaac   ierr = PetscObjectQueryFunction((PetscObject)coarse,"DMInterpolateSolution_C", &interpsol);CHKERRQ(ierr);
22911f3379b2SToby Isaac   if (interpsol) {
22921f3379b2SToby Isaac     ierr = (*interpsol)(coarse, fine, interp, coarseSol, fineSol);CHKERRQ(ierr);
22931f3379b2SToby Isaac   } else if (interp) {
22941f3379b2SToby Isaac     ierr = MatInterpolate(interp, coarseSol, fineSol);CHKERRQ(ierr);
22951f3379b2SToby Isaac   } else SETERRQ1(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
22961f3379b2SToby Isaac   PetscFunctionReturn(0);
22971f3379b2SToby Isaac }
22981f3379b2SToby Isaac 
22991f3379b2SToby Isaac /*@
2300aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2301eb3f98d2SBarry Smith 
2302eb3f98d2SBarry Smith     Not Collective
2303eb3f98d2SBarry Smith 
2304eb3f98d2SBarry Smith     Input Parameter:
2305eb3f98d2SBarry Smith .   dm - the DM object
2306eb3f98d2SBarry Smith 
2307eb3f98d2SBarry Smith     Output Parameter:
2308eb3f98d2SBarry Smith .   level - number of refinements
2309eb3f98d2SBarry Smith 
2310eb3f98d2SBarry Smith     Level: developer
2311eb3f98d2SBarry Smith 
23126a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2313eb3f98d2SBarry Smith 
2314eb3f98d2SBarry Smith @*/
2315eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2316eb3f98d2SBarry Smith {
2317eb3f98d2SBarry Smith   PetscFunctionBegin;
2318eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2319eb3f98d2SBarry Smith   *level = dm->levelup;
2320eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2321eb3f98d2SBarry Smith }
2322eb3f98d2SBarry Smith 
2323fef3a512SBarry Smith /*@
2324aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2325fef3a512SBarry Smith 
2326fef3a512SBarry Smith     Not Collective
2327fef3a512SBarry Smith 
2328fef3a512SBarry Smith     Input Parameter:
2329fef3a512SBarry Smith +   dm - the DM object
2330fef3a512SBarry Smith -   level - number of refinements
2331fef3a512SBarry Smith 
2332fef3a512SBarry Smith     Level: advanced
2333fef3a512SBarry Smith 
233495452b02SPatrick Sanan     Notes:
233595452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2336fef3a512SBarry Smith 
2337fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2338fef3a512SBarry Smith 
2339fef3a512SBarry Smith @*/
2340fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2341fef3a512SBarry Smith {
2342fef3a512SBarry Smith   PetscFunctionBegin;
2343fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2344fef3a512SBarry Smith   dm->levelup = level;
2345fef3a512SBarry Smith   PetscFunctionReturn(0);
2346fef3a512SBarry Smith }
2347fef3a512SBarry Smith 
2348ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2349ca3d3a14SMatthew G. Knepley {
2350ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2351ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2352ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2353ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2354ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2355ca3d3a14SMatthew G. Knepley }
2356ca3d3a14SMatthew G. Knepley 
2357ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2358ca3d3a14SMatthew G. Knepley {
2359ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2360ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2361ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2362ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2363ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2364ca3d3a14SMatthew G. Knepley }
2365ca3d3a14SMatthew G. Knepley 
2366ca3d3a14SMatthew G. Knepley /*@
2367c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2368ca3d3a14SMatthew G. Knepley 
2369ca3d3a14SMatthew G. Knepley   Input Parameter:
2370ca3d3a14SMatthew G. Knepley . dm - The DM
2371ca3d3a14SMatthew G. Knepley 
2372ca3d3a14SMatthew G. Knepley   Output Parameter:
2373ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2374ca3d3a14SMatthew G. Knepley 
2375ca3d3a14SMatthew G. Knepley   Level: developer
2376ca3d3a14SMatthew G. Knepley 
2377436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2378ca3d3a14SMatthew G. Knepley @*/
2379ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2380ca3d3a14SMatthew G. Knepley {
2381ca3d3a14SMatthew G. Knepley   Vec            tv;
2382ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2383ca3d3a14SMatthew G. Knepley 
2384ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2385ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2386534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2387ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2388ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2389ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2390ca3d3a14SMatthew G. Knepley }
2391ca3d3a14SMatthew G. Knepley 
2392ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2393ca3d3a14SMatthew G. Knepley {
2394ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2395ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2396ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2397ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2398ca3d3a14SMatthew G. Knepley 
2399ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2400ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
240192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2402ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2403ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2404ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
240592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2406ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2407ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2408ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2409ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2410ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2411ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2412ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2413ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2414ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2415ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2416ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2417ca3d3a14SMatthew G. Knepley     }
2418ca3d3a14SMatthew G. Knepley   }
2419ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2420ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2421ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2422ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2423ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2424ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2425ca3d3a14SMatthew G. Knepley       if (dof) {
2426ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2427ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2428ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2429ca3d3a14SMatthew G. Knepley 
2430ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2431ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2432ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2433580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2434ca3d3a14SMatthew G. Knepley       }
2435ca3d3a14SMatthew G. Knepley     }
2436ca3d3a14SMatthew G. Knepley   }
2437ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2438ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2439ca3d3a14SMatthew G. Knepley }
2440ca3d3a14SMatthew G. Knepley 
2441ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2442ca3d3a14SMatthew G. Knepley {
2443ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2444ca3d3a14SMatthew G. Knepley 
2445ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2446ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2447ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2448ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2449ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2450ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2451ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2452ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2453ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2454ca3d3a14SMatthew G. Knepley }
2455ca3d3a14SMatthew G. Knepley 
2456bb9467b5SJed Brown /*@C
2457baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2458baf369e7SPeter Brune 
2459baf369e7SPeter Brune    Logically Collective
2460baf369e7SPeter Brune 
2461baf369e7SPeter Brune    Input Arguments:
2462baf369e7SPeter Brune +  dm - the DM
2463baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2464baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
24650298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2466baf369e7SPeter Brune 
2467baf369e7SPeter Brune    Calling sequence for beginhook:
2468baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2469baf369e7SPeter Brune 
2470baf369e7SPeter Brune +  dm - global DM
2471baf369e7SPeter Brune .  g - global vector
2472baf369e7SPeter Brune .  mode - mode
2473baf369e7SPeter Brune .  l - local vector
2474baf369e7SPeter Brune -  ctx - optional user-defined function context
2475baf369e7SPeter Brune 
2476baf369e7SPeter Brune 
2477baf369e7SPeter Brune    Calling sequence for endhook:
2478ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2479baf369e7SPeter Brune 
2480baf369e7SPeter Brune +  global - global DM
2481baf369e7SPeter Brune -  ctx - optional user-defined function context
2482baf369e7SPeter Brune 
2483baf369e7SPeter Brune    Level: advanced
2484baf369e7SPeter Brune 
2485baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2486baf369e7SPeter Brune @*/
2487baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2488baf369e7SPeter Brune {
2489baf369e7SPeter Brune   PetscErrorCode          ierr;
2490baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2491baf369e7SPeter Brune 
2492baf369e7SPeter Brune   PetscFunctionBegin;
2493baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2494baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
249595dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2496baf369e7SPeter Brune   link->beginhook = beginhook;
2497baf369e7SPeter Brune   link->endhook   = endhook;
2498baf369e7SPeter Brune   link->ctx       = ctx;
24990298fd71SBarry Smith   link->next      = NULL;
2500baf369e7SPeter Brune   *p              = link;
2501baf369e7SPeter Brune   PetscFunctionReturn(0);
2502baf369e7SPeter Brune }
2503baf369e7SPeter Brune 
25044c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
25054c274da1SToby Isaac {
25064c274da1SToby Isaac   Mat cMat;
25074c274da1SToby Isaac   Vec cVec;
25084c274da1SToby Isaac   PetscSection section, cSec;
25094c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25104c274da1SToby Isaac   PetscErrorCode ierr;
25114c274da1SToby Isaac 
25124c274da1SToby Isaac   PetscFunctionBegin;
25134c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25144c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
25154c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
25165db9a05bSToby Isaac     PetscInt nRows;
25175db9a05bSToby Isaac 
25185db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
25195db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
252092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
25217711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
25224c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
25234c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
25244c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25254c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
25264c274da1SToby Isaac       if (dof) {
25274c274da1SToby Isaac         PetscScalar *vals;
25284c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
25294c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
25304c274da1SToby Isaac       }
25314c274da1SToby Isaac     }
25324c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
25334c274da1SToby Isaac   }
25344c274da1SToby Isaac   PetscFunctionReturn(0);
25354c274da1SToby Isaac }
25364c274da1SToby Isaac 
253747c6ae99SBarry Smith /*@
253801729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
253901729b5cSPatrick Sanan 
2540d083f849SBarry Smith     Neighbor-wise Collective on dm
254101729b5cSPatrick Sanan 
254201729b5cSPatrick Sanan     Input Parameters:
254301729b5cSPatrick Sanan +   dm - the DM object
254401729b5cSPatrick Sanan .   g - the global vector
254501729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
254601729b5cSPatrick Sanan -   l - the local vector
254701729b5cSPatrick Sanan 
254801729b5cSPatrick Sanan     Notes:
254901729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
255001729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
255101729b5cSPatrick Sanan 
255201729b5cSPatrick Sanan     Level: beginner
255301729b5cSPatrick Sanan 
255401729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
255501729b5cSPatrick Sanan 
255601729b5cSPatrick Sanan @*/
255701729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
255801729b5cSPatrick Sanan {
255901729b5cSPatrick Sanan   PetscErrorCode ierr;
256001729b5cSPatrick Sanan 
256101729b5cSPatrick Sanan   PetscFunctionBegin;
256201729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
256301729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
256401729b5cSPatrick Sanan   PetscFunctionReturn(0);
256501729b5cSPatrick Sanan }
256601729b5cSPatrick Sanan 
256701729b5cSPatrick Sanan /*@
256847c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
256947c6ae99SBarry Smith 
2570d083f849SBarry Smith     Neighbor-wise Collective on dm
257147c6ae99SBarry Smith 
257247c6ae99SBarry Smith     Input Parameters:
257347c6ae99SBarry Smith +   dm - the DM object
257447c6ae99SBarry Smith .   g - the global vector
257547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
257647c6ae99SBarry Smith -   l - the local vector
257747c6ae99SBarry Smith 
257801729b5cSPatrick Sanan     Level: intermediate
257947c6ae99SBarry Smith 
258001729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
258147c6ae99SBarry Smith 
258247c6ae99SBarry Smith @*/
25837087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
258447c6ae99SBarry Smith {
25857128ae9fSMatthew G Knepley   PetscSF                 sf;
258647c6ae99SBarry Smith   PetscErrorCode          ierr;
2587baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
258847c6ae99SBarry Smith 
2589d0295fc0SJunchao Zhang 
259047c6ae99SBarry Smith   PetscFunctionBegin;
2591171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2592baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
25938865f1eaSKarl Rupp     if (link->beginhook) {
25948865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
25958865f1eaSKarl Rupp     }
2596baf369e7SPeter Brune   }
25971bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
25987128ae9fSMatthew G Knepley   if (sf) {
2599ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2600ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2601d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
26027128ae9fSMatthew G Knepley 
260382f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2604a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2605a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2606ad227feaSJunchao Zhang     ierr = PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE);CHKERRQ(ierr);
2607a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2608a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
26097128ae9fSMatthew G Knepley   } else {
261033907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2611843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
26127128ae9fSMatthew G Knepley   }
261347c6ae99SBarry Smith   PetscFunctionReturn(0);
261447c6ae99SBarry Smith }
261547c6ae99SBarry Smith 
261647c6ae99SBarry Smith /*@
261747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
261847c6ae99SBarry Smith 
2619d083f849SBarry Smith     Neighbor-wise Collective on dm
262047c6ae99SBarry Smith 
262147c6ae99SBarry Smith     Input Parameters:
262247c6ae99SBarry Smith +   dm - the DM object
262347c6ae99SBarry Smith .   g - the global vector
262447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
262547c6ae99SBarry Smith -   l - the local vector
262647c6ae99SBarry Smith 
262701729b5cSPatrick Sanan     Level: intermediate
262847c6ae99SBarry Smith 
262901729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
263047c6ae99SBarry Smith 
263147c6ae99SBarry Smith @*/
26327087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
263347c6ae99SBarry Smith {
26347128ae9fSMatthew G Knepley   PetscSF                 sf;
263547c6ae99SBarry Smith   PetscErrorCode          ierr;
2636ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2637ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2638ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2639baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2640d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
264147c6ae99SBarry Smith 
264247c6ae99SBarry Smith   PetscFunctionBegin;
2643171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26441bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2645ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
26467128ae9fSMatthew G Knepley   if (sf) {
264782f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26487128ae9fSMatthew G Knepley 
2649a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2650a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2651ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray,MPI_REPLACE);CHKERRQ(ierr);
2652a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2653a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
2654ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
26557128ae9fSMatthew G Knepley   } else {
265633907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2657843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
26587128ae9fSMatthew G Knepley   }
26594c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2660baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2661baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2662baf369e7SPeter Brune   }
266347c6ae99SBarry Smith   PetscFunctionReturn(0);
266447c6ae99SBarry Smith }
266547c6ae99SBarry Smith 
2666d4d07f1eSToby Isaac /*@C
2667d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2668d4d07f1eSToby Isaac 
2669d4d07f1eSToby Isaac    Logically Collective
2670d4d07f1eSToby Isaac 
2671d4d07f1eSToby Isaac    Input Arguments:
2672d4d07f1eSToby Isaac +  dm - the DM
2673d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2674d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2675d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2676d4d07f1eSToby Isaac 
2677d4d07f1eSToby Isaac    Calling sequence for beginhook:
2678d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2679d4d07f1eSToby Isaac 
2680d4d07f1eSToby Isaac +  dm - global DM
2681d4d07f1eSToby Isaac .  l - local vector
2682d4d07f1eSToby Isaac .  mode - mode
2683d4d07f1eSToby Isaac .  g - global vector
2684d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2685d4d07f1eSToby Isaac 
2686d4d07f1eSToby Isaac 
2687d4d07f1eSToby Isaac    Calling sequence for endhook:
2688d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2689d4d07f1eSToby Isaac 
2690d4d07f1eSToby Isaac +  global - global DM
2691d4d07f1eSToby Isaac .  l - local vector
2692d4d07f1eSToby Isaac .  mode - mode
2693d4d07f1eSToby Isaac .  g - global vector
2694d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2695d4d07f1eSToby Isaac 
2696d4d07f1eSToby Isaac    Level: advanced
2697d4d07f1eSToby Isaac 
2698d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2699d4d07f1eSToby Isaac @*/
2700d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2701d4d07f1eSToby Isaac {
2702d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2703d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2704d4d07f1eSToby Isaac 
2705d4d07f1eSToby Isaac   PetscFunctionBegin;
2706d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2707d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
270895dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2709d4d07f1eSToby Isaac   link->beginhook = beginhook;
2710d4d07f1eSToby Isaac   link->endhook   = endhook;
2711d4d07f1eSToby Isaac   link->ctx       = ctx;
2712d4d07f1eSToby Isaac   link->next      = NULL;
2713d4d07f1eSToby Isaac   *p              = link;
2714d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2715d4d07f1eSToby Isaac }
2716d4d07f1eSToby Isaac 
27174c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
27184c274da1SToby Isaac {
27194c274da1SToby Isaac   Mat cMat;
27204c274da1SToby Isaac   Vec cVec;
27214c274da1SToby Isaac   PetscSection section, cSec;
27224c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
27234c274da1SToby Isaac   PetscErrorCode ierr;
27244c274da1SToby Isaac 
27254c274da1SToby Isaac   PetscFunctionBegin;
27264c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27274c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
27284c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
27295db9a05bSToby Isaac     PetscInt nRows;
27305db9a05bSToby Isaac 
27315db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
27325db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
273392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
27347711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
27354c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
27364c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27374c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
27384c274da1SToby Isaac       if (dof) {
27394c274da1SToby Isaac         PetscInt d;
27404c274da1SToby Isaac         PetscScalar *vals;
27414c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
27424c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
27434c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
27444c274da1SToby Isaac          * we just extracted */
27454c274da1SToby Isaac         for (d = 0; d < dof; d++) {
27464c274da1SToby Isaac           vals[d] = 0.;
27474c274da1SToby Isaac         }
27484c274da1SToby Isaac       }
27494c274da1SToby Isaac     }
27504c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
27514c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
27524c274da1SToby Isaac   }
27534c274da1SToby Isaac   PetscFunctionReturn(0);
27544c274da1SToby Isaac }
275501729b5cSPatrick Sanan /*@
275601729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
275701729b5cSPatrick Sanan 
2758d083f849SBarry Smith     Neighbor-wise Collective on dm
275901729b5cSPatrick Sanan 
276001729b5cSPatrick Sanan     Input Parameters:
276101729b5cSPatrick Sanan +   dm - the DM object
276201729b5cSPatrick Sanan .   l - the local vector
276301729b5cSPatrick 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.
276401729b5cSPatrick Sanan -   g - the global vector
276501729b5cSPatrick Sanan 
276601729b5cSPatrick Sanan     Notes:
276701729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
276801729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
276901729b5cSPatrick Sanan 
277001729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
277101729b5cSPatrick 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.
277201729b5cSPatrick Sanan 
277301729b5cSPatrick Sanan     Level: beginner
277401729b5cSPatrick Sanan 
277501729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
277601729b5cSPatrick Sanan 
277701729b5cSPatrick Sanan @*/
277801729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
277901729b5cSPatrick Sanan {
278001729b5cSPatrick Sanan   PetscErrorCode ierr;
278101729b5cSPatrick Sanan 
278201729b5cSPatrick Sanan   PetscFunctionBegin;
278301729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
278401729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
278501729b5cSPatrick Sanan   PetscFunctionReturn(0);
278601729b5cSPatrick Sanan }
27874c274da1SToby Isaac 
278847c6ae99SBarry Smith /*@
278901729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
27909a42bb27SBarry Smith 
2791d083f849SBarry Smith     Neighbor-wise Collective on dm
27929a42bb27SBarry Smith 
27939a42bb27SBarry Smith     Input Parameters:
27949a42bb27SBarry Smith +   dm - the DM object
2795f6813fd5SJed Brown .   l - the local vector
27961eb28f2eSBarry 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.
27971eb28f2eSBarry Smith -   g - the global vector
27989a42bb27SBarry Smith 
279995452b02SPatrick Sanan     Notes:
280095452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
280184330215SMatthew 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.
28029a42bb27SBarry Smith 
280301729b5cSPatrick Sanan     Level: intermediate
28049a42bb27SBarry Smith 
280501729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
28069a42bb27SBarry Smith 
28079a42bb27SBarry Smith @*/
28087087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
28099a42bb27SBarry Smith {
28107128ae9fSMatthew G Knepley   PetscSF                 sf;
281184330215SMatthew G. Knepley   PetscSection            s, gs;
2812d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2813ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2814ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2815ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2816fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
281784330215SMatthew G. Knepley   PetscErrorCode          ierr;
2818d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
28199a42bb27SBarry Smith 
28209a42bb27SBarry Smith   PetscFunctionBegin;
2821171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2822d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2823d4d07f1eSToby Isaac     if (link->beginhook) {
2824d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2825d4d07f1eSToby Isaac     }
2826d4d07f1eSToby Isaac   }
28274c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
28281bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
282992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
28307128ae9fSMatthew G Knepley   switch (mode) {
28317128ae9fSMatthew G Knepley   case INSERT_VALUES:
28327128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2833304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
283484330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
28357128ae9fSMatthew G Knepley   case ADD_VALUES:
28367128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2837304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
283884330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
28397128ae9fSMatthew G Knepley   default:
284082f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
28417128ae9fSMatthew G Knepley   }
2842ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2843ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2844ca3d3a14SMatthew G. Knepley     if (transform) {
2845ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2846ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2847ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2848ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2849fa88e482SJed Brown     } else if (isInsert) {
2850ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2851fa88e482SJed Brown     } else {
2852a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2853fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2854ca3d3a14SMatthew G. Knepley     }
2855fa88e482SJed Brown     if (s && isInsert) {
28567128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2857fa88e482SJed Brown     } else {
2858a256111fSJunchao Zhang       ierr = VecGetArrayAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2859fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2860fa88e482SJed Brown     }
2861ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2862d0295fc0SJunchao Zhang       ierr = PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM);CHKERRQ(ierr);
286384330215SMatthew G. Knepley     } else if (s && isInsert) {
286484330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
286584330215SMatthew G. Knepley 
2866e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
286784330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
286884330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
286984330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2870b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
287184330215SMatthew G. Knepley 
287284330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
287303442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
287484330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2875b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
287684330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
287784330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2878b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
287903442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2880b3b16f48SMatthew 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);
2881b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2882b3b16f48SMatthew G. Knepley         if (!gcdof) {
288384330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2884b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2885b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
288684330215SMatthew G. Knepley           const PetscInt *cdofs;
288784330215SMatthew G. Knepley           PetscInt        cind = 0;
288884330215SMatthew G. Knepley 
288984330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2890b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
289184330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2892b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
289384330215SMatthew G. Knepley           }
2894b3b16f48SMatthew 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);
289584330215SMatthew G. Knepley       }
2896ca3d3a14SMatthew G. Knepley     }
2897fa88e482SJed Brown     if (g_inplace) {
2898a256111fSJunchao Zhang       ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
2899fa88e482SJed Brown     } else {
29007128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2901fa88e482SJed Brown     }
2902ca3d3a14SMatthew G. Knepley     if (transform) {
2903ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2904ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2905fa88e482SJed Brown     } else if (l_inplace) {
2906a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
2907ca3d3a14SMatthew G. Knepley     } else {
2908ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2909ca3d3a14SMatthew G. Knepley     }
29107128ae9fSMatthew G Knepley   } else {
2911b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2912843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
29137128ae9fSMatthew G Knepley   }
29149a42bb27SBarry Smith   PetscFunctionReturn(0);
29159a42bb27SBarry Smith }
29169a42bb27SBarry Smith 
29179a42bb27SBarry Smith /*@
29189a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
291947c6ae99SBarry Smith 
2920d083f849SBarry Smith     Neighbor-wise Collective on dm
292147c6ae99SBarry Smith 
292247c6ae99SBarry Smith     Input Parameters:
292347c6ae99SBarry Smith +   dm - the DM object
2924f6813fd5SJed Brown .   l - the local vector
292547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2926f6813fd5SJed Brown -   g - the global vector
292747c6ae99SBarry Smith 
292801729b5cSPatrick Sanan     Level: intermediate
292947c6ae99SBarry Smith 
2930e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
293147c6ae99SBarry Smith 
293247c6ae99SBarry Smith @*/
29337087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
293447c6ae99SBarry Smith {
29357128ae9fSMatthew G Knepley   PetscSF                 sf;
293684330215SMatthew G. Knepley   PetscSection            s;
2937d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2938ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
293984330215SMatthew G. Knepley   PetscErrorCode          ierr;
294047c6ae99SBarry Smith 
294147c6ae99SBarry Smith   PetscFunctionBegin;
2942171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29431bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
294492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
29457128ae9fSMatthew G Knepley   switch (mode) {
29467128ae9fSMatthew G Knepley   case INSERT_VALUES:
29477128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
294884330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
29497128ae9fSMatthew G Knepley   case ADD_VALUES:
29507128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
295184330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
29527128ae9fSMatthew G Knepley   default:
295382f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
29547128ae9fSMatthew G Knepley   }
295584330215SMatthew G. Knepley   if (sf && !isInsert) {
2956ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2957ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2958ca3d3a14SMatthew G. Knepley     Vec                tmpl;
295984330215SMatthew G. Knepley 
2960ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2961ca3d3a14SMatthew G. Knepley     if (transform) {
2962ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2963ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2964ca3d3a14SMatthew G. Knepley     } else {
2965a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, NULL);CHKERRQ(ierr);
2966ca3d3a14SMatthew G. Knepley     }
2967a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(g, &gArray, NULL);CHKERRQ(ierr);
2968a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2969ca3d3a14SMatthew G. Knepley     if (transform) {
2970ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2971ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2972ca3d3a14SMatthew G. Knepley     } else {
2973a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
2974ca3d3a14SMatthew G. Knepley     }
2975a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
297684330215SMatthew G. Knepley   } else if (s && isInsert) {
29777128ae9fSMatthew G Knepley   } else {
2978b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2979843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
29807128ae9fSMatthew G Knepley   }
2981d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2982d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2983d4d07f1eSToby Isaac   }
298447c6ae99SBarry Smith   PetscFunctionReturn(0);
298547c6ae99SBarry Smith }
298647c6ae99SBarry Smith 
2987f089877aSRichard Tran Mills /*@
2988bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2989bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2990d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2991f089877aSRichard Tran Mills 
2992d083f849SBarry Smith    Neighbor-wise Collective on dm
2993f089877aSRichard Tran Mills 
2994f089877aSRichard Tran Mills    Input Parameters:
2995f089877aSRichard Tran Mills +  dm - the DM object
2996bc0a1609SRichard Tran Mills .  g - the original local vector
2997bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2998f089877aSRichard Tran Mills 
2999bc0a1609SRichard Tran Mills    Output Parameter:
3000bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3001f089877aSRichard Tran Mills 
3002f089877aSRichard Tran Mills    Level: intermediate
3003f089877aSRichard Tran Mills 
3004bc0a1609SRichard Tran Mills    Notes:
3005bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3006bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3007bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3008bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3009bc0a1609SRichard Tran Mills 
3010bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3011f089877aSRichard Tran Mills 
3012f089877aSRichard Tran Mills @*/
3013f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
3014f089877aSRichard Tran Mills {
3015f089877aSRichard Tran Mills   PetscErrorCode          ierr;
3016f089877aSRichard Tran Mills 
3017f089877aSRichard Tran Mills   PetscFunctionBegin;
3018f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3019bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3020f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
3021f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3022f089877aSRichard Tran Mills }
3023f089877aSRichard Tran Mills 
3024f089877aSRichard Tran Mills /*@
3025bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
3026bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3027d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
3028f089877aSRichard Tran Mills 
3029d083f849SBarry Smith    Neighbor-wise Collective on dm
3030f089877aSRichard Tran Mills 
3031f089877aSRichard Tran Mills    Input Parameters:
3032bc0a1609SRichard Tran Mills +  da - the DM object
3033bc0a1609SRichard Tran Mills .  g - the original local vector
3034bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3035f089877aSRichard Tran Mills 
3036bc0a1609SRichard Tran Mills    Output Parameter:
3037bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3038f089877aSRichard Tran Mills 
3039f089877aSRichard Tran Mills    Level: intermediate
3040f089877aSRichard Tran Mills 
3041bc0a1609SRichard Tran Mills    Notes:
3042bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3043bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3044bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3045bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3046bc0a1609SRichard Tran Mills 
3047bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3048f089877aSRichard Tran Mills 
3049f089877aSRichard Tran Mills @*/
3050f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
3051f089877aSRichard Tran Mills {
3052f089877aSRichard Tran Mills   PetscErrorCode          ierr;
3053f089877aSRichard Tran Mills 
3054f089877aSRichard Tran Mills   PetscFunctionBegin;
3055f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3056bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3057f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
3058f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3059f089877aSRichard Tran Mills }
3060f089877aSRichard Tran Mills 
3061f089877aSRichard Tran Mills 
306247c6ae99SBarry Smith /*@
306347c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
306447c6ae99SBarry Smith 
3065d083f849SBarry Smith     Collective on dm
306647c6ae99SBarry Smith 
306747c6ae99SBarry Smith     Input Parameter:
306847c6ae99SBarry Smith +   dm - the DM object
306991d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
307047c6ae99SBarry Smith 
307147c6ae99SBarry Smith     Output Parameter:
307247c6ae99SBarry Smith .   dmc - the coarsened DM
307347c6ae99SBarry Smith 
307447c6ae99SBarry Smith     Level: developer
307547c6ae99SBarry Smith 
3076e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
307747c6ae99SBarry Smith 
307847c6ae99SBarry Smith @*/
30797087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
308047c6ae99SBarry Smith {
308147c6ae99SBarry Smith   PetscErrorCode    ierr;
3082b17ce1afSJed Brown   DMCoarsenHookLink link;
308347c6ae99SBarry Smith 
308447c6ae99SBarry Smith   PetscFunctionBegin;
3085171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3086b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
308747a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
308847c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
3089b9d85ea2SLisandro Dalcin   if (*dmc) {
3090a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
309143842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
30928cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
3093644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
30940598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3095656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
3096e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
3097b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
3098b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
3099b17ce1afSJed Brown     }
3100b9d85ea2SLisandro Dalcin   }
310147a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
3102b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3103b17ce1afSJed Brown   PetscFunctionReturn(0);
3104b17ce1afSJed Brown }
3105b17ce1afSJed Brown 
3106bb9467b5SJed Brown /*@C
3107b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3108b17ce1afSJed Brown 
3109b17ce1afSJed Brown    Logically Collective
3110b17ce1afSJed Brown 
3111b17ce1afSJed Brown    Input Arguments:
3112b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3113b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3114b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
31150298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3116b17ce1afSJed Brown 
3117b17ce1afSJed Brown    Calling sequence of coarsenhook:
3118b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3119b17ce1afSJed Brown 
3120b17ce1afSJed Brown +  fine - fine level DM
3121b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3122b17ce1afSJed Brown -  ctx - optional user-defined function context
3123b17ce1afSJed Brown 
3124b17ce1afSJed Brown    Calling sequence for restricthook:
3125c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3126b17ce1afSJed Brown 
3127b17ce1afSJed Brown +  fine - fine level DM
3128b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3129c833c3b5SJed Brown .  rscale - scaling vector for restriction
3130c833c3b5SJed Brown .  inject - matrix restricting by injection
3131b17ce1afSJed Brown .  coarse - coarse level DM to update
3132b17ce1afSJed Brown -  ctx - optional user-defined function context
3133b17ce1afSJed Brown 
3134b17ce1afSJed Brown    Level: advanced
3135b17ce1afSJed Brown 
3136b17ce1afSJed Brown    Notes:
3137b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3138b17ce1afSJed Brown 
3139b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3140b17ce1afSJed Brown 
3141b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3142b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3143b17ce1afSJed Brown 
3144bb9467b5SJed Brown    This function is currently not available from Fortran.
3145bb9467b5SJed Brown 
3146dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3147b17ce1afSJed Brown @*/
3148b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3149b17ce1afSJed Brown {
3150b17ce1afSJed Brown   PetscErrorCode    ierr;
3151b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3152b17ce1afSJed Brown 
3153b17ce1afSJed Brown   PetscFunctionBegin;
3154b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
31551e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
31561e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
31571e3d8eccSJed Brown   }
315895dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
3159b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3160b17ce1afSJed Brown   link->restricthook = restricthook;
3161b17ce1afSJed Brown   link->ctx          = ctx;
31620298fd71SBarry Smith   link->next         = NULL;
3163b17ce1afSJed Brown   *p                 = link;
3164b17ce1afSJed Brown   PetscFunctionReturn(0);
3165b17ce1afSJed Brown }
3166b17ce1afSJed Brown 
3167dc822a44SJed Brown /*@C
3168dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3169dc822a44SJed Brown 
3170dc822a44SJed Brown    Logically Collective
3171dc822a44SJed Brown 
3172dc822a44SJed Brown    Input Arguments:
3173dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3174dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3175dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3176dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3177dc822a44SJed Brown 
3178dc822a44SJed Brown    Level: advanced
3179dc822a44SJed Brown 
3180dc822a44SJed Brown    Notes:
3181dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3182dc822a44SJed Brown 
3183dc822a44SJed Brown    This function is currently not available from Fortran.
3184dc822a44SJed Brown 
3185dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3186dc822a44SJed Brown @*/
3187dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3188dc822a44SJed Brown {
3189dc822a44SJed Brown   PetscErrorCode    ierr;
3190dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3191dc822a44SJed Brown 
3192dc822a44SJed Brown   PetscFunctionBegin;
3193dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3194dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3195dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3196dc822a44SJed Brown       link = *p;
3197dc822a44SJed Brown       *p = link->next;
3198dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3199dc822a44SJed Brown       break;
3200dc822a44SJed Brown     }
3201dc822a44SJed Brown   }
3202dc822a44SJed Brown   PetscFunctionReturn(0);
3203dc822a44SJed Brown }
3204dc822a44SJed Brown 
3205dc822a44SJed Brown 
3206b17ce1afSJed Brown /*@
3207b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3208b17ce1afSJed Brown 
3209b17ce1afSJed Brown    Collective if any hooks are
3210b17ce1afSJed Brown 
3211b17ce1afSJed Brown    Input Arguments:
3212b17ce1afSJed Brown +  fine - finer DM to use as a base
3213b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3214e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3215b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3216e91eccc2SStefano Zampini -  coarse - coarser DM to update
3217b17ce1afSJed Brown 
3218b17ce1afSJed Brown    Level: developer
3219b17ce1afSJed Brown 
3220b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3221b17ce1afSJed Brown @*/
3222b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3223b17ce1afSJed Brown {
3224b17ce1afSJed Brown   PetscErrorCode    ierr;
3225b17ce1afSJed Brown   DMCoarsenHookLink link;
3226b17ce1afSJed Brown 
3227b17ce1afSJed Brown   PetscFunctionBegin;
3228b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
32298865f1eaSKarl Rupp     if (link->restricthook) {
32308865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
32318865f1eaSKarl Rupp     }
3232b17ce1afSJed Brown   }
323347c6ae99SBarry Smith   PetscFunctionReturn(0);
323447c6ae99SBarry Smith }
323547c6ae99SBarry Smith 
3236bb9467b5SJed Brown /*@C
3237be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
32385dbd56e3SPeter Brune 
3239d083f849SBarry Smith    Logically Collective on global
32405dbd56e3SPeter Brune 
32415dbd56e3SPeter Brune    Input Arguments:
32425dbd56e3SPeter Brune +  global - global DM
3243ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
32445dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
32450298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
32465dbd56e3SPeter Brune 
3247ec4806b8SPeter Brune 
3248ec4806b8SPeter Brune    Calling sequence for ddhook:
3249ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3250ec4806b8SPeter Brune 
3251ec4806b8SPeter Brune +  global - global DM
3252ec4806b8SPeter Brune .  block  - block DM
3253ec4806b8SPeter Brune -  ctx - optional user-defined function context
3254ec4806b8SPeter Brune 
32555dbd56e3SPeter Brune    Calling sequence for restricthook:
3256ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
32575dbd56e3SPeter Brune 
32585dbd56e3SPeter Brune +  global - global DM
32595dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
32605dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3261ec4806b8SPeter Brune .  block  - block DM
32625dbd56e3SPeter Brune -  ctx - optional user-defined function context
32635dbd56e3SPeter Brune 
32645dbd56e3SPeter Brune    Level: advanced
32655dbd56e3SPeter Brune 
32665dbd56e3SPeter Brune    Notes:
3267ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
32685dbd56e3SPeter Brune 
32695dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
32705dbd56e3SPeter Brune 
32715dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3272ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
32735dbd56e3SPeter Brune 
3274bb9467b5SJed Brown    This function is currently not available from Fortran.
3275bb9467b5SJed Brown 
32765dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
32775dbd56e3SPeter Brune @*/
3278be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
32795dbd56e3SPeter Brune {
32805dbd56e3SPeter Brune   PetscErrorCode      ierr;
3281be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
32825dbd56e3SPeter Brune 
32835dbd56e3SPeter Brune   PetscFunctionBegin;
32845dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3285b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3286b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3287b3a6b972SJed Brown   }
328895dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
32895dbd56e3SPeter Brune   link->restricthook = restricthook;
3290be081cd6SPeter Brune   link->ddhook       = ddhook;
32915dbd56e3SPeter Brune   link->ctx          = ctx;
32920298fd71SBarry Smith   link->next         = NULL;
32935dbd56e3SPeter Brune   *p                 = link;
32945dbd56e3SPeter Brune   PetscFunctionReturn(0);
32955dbd56e3SPeter Brune }
32965dbd56e3SPeter Brune 
3297b3a6b972SJed Brown /*@C
3298b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3299b3a6b972SJed Brown 
3300b3a6b972SJed Brown    Logically Collective
3301b3a6b972SJed Brown 
3302b3a6b972SJed Brown    Input Arguments:
3303b3a6b972SJed Brown +  global - global DM
3304b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3305b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3306b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3307b3a6b972SJed Brown 
3308b3a6b972SJed Brown    Level: advanced
3309b3a6b972SJed Brown 
3310b3a6b972SJed Brown    Notes:
3311b3a6b972SJed Brown 
3312b3a6b972SJed Brown    This function is currently not available from Fortran.
3313b3a6b972SJed Brown 
3314b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3315b3a6b972SJed Brown @*/
3316b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3317b3a6b972SJed Brown {
3318b3a6b972SJed Brown   PetscErrorCode      ierr;
3319b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3320b3a6b972SJed Brown 
3321b3a6b972SJed Brown   PetscFunctionBegin;
3322b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3323b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3324b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3325b3a6b972SJed Brown       link = *p;
3326b3a6b972SJed Brown       *p = link->next;
3327b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3328b3a6b972SJed Brown       break;
3329b3a6b972SJed Brown     }
3330b3a6b972SJed Brown   }
3331b3a6b972SJed Brown   PetscFunctionReturn(0);
3332b3a6b972SJed Brown }
3333b3a6b972SJed Brown 
33345dbd56e3SPeter Brune /*@
3335be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
33365dbd56e3SPeter Brune 
33375dbd56e3SPeter Brune    Collective if any hooks are
33385dbd56e3SPeter Brune 
33395dbd56e3SPeter Brune    Input Arguments:
33405dbd56e3SPeter Brune +  fine - finer DM to use as a base
3341be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3342be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
33435dbd56e3SPeter Brune -  coarse - coarer DM to update
33445dbd56e3SPeter Brune 
33455dbd56e3SPeter Brune    Level: developer
33465dbd56e3SPeter Brune 
33475dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
33485dbd56e3SPeter Brune @*/
3349be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
33505dbd56e3SPeter Brune {
33515dbd56e3SPeter Brune   PetscErrorCode      ierr;
3352be081cd6SPeter Brune   DMSubDomainHookLink link;
33535dbd56e3SPeter Brune 
33545dbd56e3SPeter Brune   PetscFunctionBegin;
3355be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
33568865f1eaSKarl Rupp     if (link->restricthook) {
33578865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
33588865f1eaSKarl Rupp     }
33595dbd56e3SPeter Brune   }
33605dbd56e3SPeter Brune   PetscFunctionReturn(0);
33615dbd56e3SPeter Brune }
33625dbd56e3SPeter Brune 
33635fe1f584SPeter Brune /*@
33646a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
33655fe1f584SPeter Brune 
33665fe1f584SPeter Brune     Not Collective
33675fe1f584SPeter Brune 
33685fe1f584SPeter Brune     Input Parameter:
33695fe1f584SPeter Brune .   dm - the DM object
33705fe1f584SPeter Brune 
33715fe1f584SPeter Brune     Output Parameter:
33726a7d9d85SPeter Brune .   level - number of coarsenings
33735fe1f584SPeter Brune 
33745fe1f584SPeter Brune     Level: developer
33755fe1f584SPeter Brune 
33766a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
33775fe1f584SPeter Brune 
33785fe1f584SPeter Brune @*/
33795fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
33805fe1f584SPeter Brune {
33815fe1f584SPeter Brune   PetscFunctionBegin;
33825fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3383b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
33845fe1f584SPeter Brune   *level = dm->leveldown;
33855fe1f584SPeter Brune   PetscFunctionReturn(0);
33865fe1f584SPeter Brune }
33875fe1f584SPeter Brune 
33889a64c4a8SMatthew G. Knepley /*@
33899a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
33909a64c4a8SMatthew G. Knepley 
33919a64c4a8SMatthew G. Knepley     Not Collective
33929a64c4a8SMatthew G. Knepley 
33939a64c4a8SMatthew G. Knepley     Input Parameters:
33949a64c4a8SMatthew G. Knepley +   dm - the DM object
33959a64c4a8SMatthew G. Knepley -   level - number of coarsenings
33969a64c4a8SMatthew G. Knepley 
33979a64c4a8SMatthew G. Knepley     Level: developer
33989a64c4a8SMatthew G. Knepley 
33999a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
34009a64c4a8SMatthew G. Knepley @*/
34019a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
34029a64c4a8SMatthew G. Knepley {
34039a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
34049a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34059a64c4a8SMatthew G. Knepley   dm->leveldown = level;
34069a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
34079a64c4a8SMatthew G. Knepley }
34089a64c4a8SMatthew G. Knepley 
34095fe1f584SPeter Brune 
34105fe1f584SPeter Brune 
341147c6ae99SBarry Smith /*@C
341247c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
341347c6ae99SBarry Smith 
3414d083f849SBarry Smith     Collective on dm
341547c6ae99SBarry Smith 
341647c6ae99SBarry Smith     Input Parameter:
341747c6ae99SBarry Smith +   dm - the DM object
341847c6ae99SBarry Smith -   nlevels - the number of levels of refinement
341947c6ae99SBarry Smith 
342047c6ae99SBarry Smith     Output Parameter:
342147c6ae99SBarry Smith .   dmf - the refined DM hierarchy
342247c6ae99SBarry Smith 
342347c6ae99SBarry Smith     Level: developer
342447c6ae99SBarry Smith 
3425e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
342647c6ae99SBarry Smith 
342747c6ae99SBarry Smith @*/
34287087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
342947c6ae99SBarry Smith {
343047c6ae99SBarry Smith   PetscErrorCode ierr;
343147c6ae99SBarry Smith 
343247c6ae99SBarry Smith   PetscFunctionBegin;
3433171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3434ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
343547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3436b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
343747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
343847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
343947c6ae99SBarry Smith   } else if (dm->ops->refine) {
344047c6ae99SBarry Smith     PetscInt i;
344147c6ae99SBarry Smith 
3442ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
344347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3444ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
344547c6ae99SBarry Smith     }
3446ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
344747c6ae99SBarry Smith   PetscFunctionReturn(0);
344847c6ae99SBarry Smith }
344947c6ae99SBarry Smith 
345047c6ae99SBarry Smith /*@C
345147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
345247c6ae99SBarry Smith 
3453d083f849SBarry Smith     Collective on dm
345447c6ae99SBarry Smith 
345547c6ae99SBarry Smith     Input Parameter:
345647c6ae99SBarry Smith +   dm - the DM object
345747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
345847c6ae99SBarry Smith 
345947c6ae99SBarry Smith     Output Parameter:
346047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
346147c6ae99SBarry Smith 
346247c6ae99SBarry Smith     Level: developer
346347c6ae99SBarry Smith 
3464e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
346547c6ae99SBarry Smith 
346647c6ae99SBarry Smith @*/
34677087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
346847c6ae99SBarry Smith {
346947c6ae99SBarry Smith   PetscErrorCode ierr;
347047c6ae99SBarry Smith 
347147c6ae99SBarry Smith   PetscFunctionBegin;
3472171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3473ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
347447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
347547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
347647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
347747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
347847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
347947c6ae99SBarry Smith     PetscInt i;
348047c6ae99SBarry Smith 
3481ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
348247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3483ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
348447c6ae99SBarry Smith     }
3485ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
348647c6ae99SBarry Smith   PetscFunctionReturn(0);
348747c6ae99SBarry Smith }
348847c6ae99SBarry Smith 
34891a266240SBarry Smith /*@C
34901a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
34911a266240SBarry Smith 
34921a266240SBarry Smith     Not Collective
34931a266240SBarry Smith 
34941a266240SBarry Smith     Input Parameters:
34951a266240SBarry Smith +   dm - the DM object
34961a266240SBarry Smith -   destroy - the destroy function
34971a266240SBarry Smith 
34981a266240SBarry Smith     Level: intermediate
34991a266240SBarry Smith 
3500e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
35011a266240SBarry Smith 
3502f07f9ceaSJed Brown @*/
35031a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
35041a266240SBarry Smith {
35051a266240SBarry Smith   PetscFunctionBegin;
3506171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35071a266240SBarry Smith   dm->ctxdestroy = destroy;
35081a266240SBarry Smith   PetscFunctionReturn(0);
35091a266240SBarry Smith }
35101a266240SBarry Smith 
3511b07ff414SBarry Smith /*@
35121b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
351347c6ae99SBarry Smith 
351447c6ae99SBarry Smith     Not Collective
351547c6ae99SBarry Smith 
351647c6ae99SBarry Smith     Input Parameters:
351747c6ae99SBarry Smith +   dm - the DM object
351847c6ae99SBarry Smith -   ctx - the user context
351947c6ae99SBarry Smith 
352047c6ae99SBarry Smith     Level: intermediate
352147c6ae99SBarry Smith 
3522e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
352347c6ae99SBarry Smith 
352447c6ae99SBarry Smith @*/
35251b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
352647c6ae99SBarry Smith {
352747c6ae99SBarry Smith   PetscFunctionBegin;
3528171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
352947c6ae99SBarry Smith   dm->ctx = ctx;
353047c6ae99SBarry Smith   PetscFunctionReturn(0);
353147c6ae99SBarry Smith }
353247c6ae99SBarry Smith 
353347c6ae99SBarry Smith /*@
35341b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
353547c6ae99SBarry Smith 
353647c6ae99SBarry Smith     Not Collective
353747c6ae99SBarry Smith 
353847c6ae99SBarry Smith     Input Parameter:
353947c6ae99SBarry Smith .   dm - the DM object
354047c6ae99SBarry Smith 
354147c6ae99SBarry Smith     Output Parameter:
354247c6ae99SBarry Smith .   ctx - the user context
354347c6ae99SBarry Smith 
354447c6ae99SBarry Smith     Level: intermediate
354547c6ae99SBarry Smith 
3546e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
354747c6ae99SBarry Smith 
354847c6ae99SBarry Smith @*/
35491b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
355047c6ae99SBarry Smith {
355147c6ae99SBarry Smith   PetscFunctionBegin;
3552171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35531b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
355447c6ae99SBarry Smith   PetscFunctionReturn(0);
355547c6ae99SBarry Smith }
355647c6ae99SBarry Smith 
355708da532bSDmitry Karpeev /*@C
3558df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
355908da532bSDmitry Karpeev 
3560d083f849SBarry Smith     Logically Collective on dm
356108da532bSDmitry Karpeev 
356208da532bSDmitry Karpeev     Input Parameter:
356308da532bSDmitry Karpeev +   dm - the DM object
35640298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
356508da532bSDmitry Karpeev 
356608da532bSDmitry Karpeev     Level: intermediate
356708da532bSDmitry Karpeev 
3568835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
356908da532bSDmitry Karpeev          DMSetJacobian()
357008da532bSDmitry Karpeev 
357108da532bSDmitry Karpeev @*/
357208da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
357308da532bSDmitry Karpeev {
357408da532bSDmitry Karpeev   PetscFunctionBegin;
35755a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
357608da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
357708da532bSDmitry Karpeev   PetscFunctionReturn(0);
357808da532bSDmitry Karpeev }
357908da532bSDmitry Karpeev 
358008da532bSDmitry Karpeev /*@
358108da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
358208da532bSDmitry Karpeev 
358308da532bSDmitry Karpeev     Not Collective
358408da532bSDmitry Karpeev 
358508da532bSDmitry Karpeev     Input Parameter:
358608da532bSDmitry Karpeev .   dm - the DM object to destroy
358708da532bSDmitry Karpeev 
358808da532bSDmitry Karpeev     Output Parameter:
358908da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
359008da532bSDmitry Karpeev 
359108da532bSDmitry Karpeev     Level: developer
359208da532bSDmitry Karpeev 
359374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
359408da532bSDmitry Karpeev 
359508da532bSDmitry Karpeev @*/
359608da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
359708da532bSDmitry Karpeev {
359808da532bSDmitry Karpeev   PetscFunctionBegin;
35995a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3600534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
360108da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
360208da532bSDmitry Karpeev   PetscFunctionReturn(0);
360308da532bSDmitry Karpeev }
360408da532bSDmitry Karpeev 
360508da532bSDmitry Karpeev /*@C
360608da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
360708da532bSDmitry Karpeev 
3608d083f849SBarry Smith     Logically Collective on dm
360908da532bSDmitry Karpeev 
361008da532bSDmitry Karpeev     Input Parameters:
3611907376e6SBarry Smith .   dm - the DM object
361208da532bSDmitry Karpeev 
361308da532bSDmitry Karpeev     Output parameters:
361408da532bSDmitry Karpeev +   xl - lower bound
361508da532bSDmitry Karpeev -   xu - upper bound
361608da532bSDmitry Karpeev 
3617907376e6SBarry Smith     Level: advanced
3618907376e6SBarry Smith 
361995452b02SPatrick Sanan     Notes:
362095452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
362108da532bSDmitry Karpeev 
362274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
362308da532bSDmitry Karpeev 
362408da532bSDmitry Karpeev @*/
362508da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
362608da532bSDmitry Karpeev {
362708da532bSDmitry Karpeev   PetscErrorCode ierr;
36285fd66863SKarl Rupp 
362908da532bSDmitry Karpeev   PetscFunctionBegin;
36305a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
363108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
36325a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3633b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
363408da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
363508da532bSDmitry Karpeev   PetscFunctionReturn(0);
363608da532bSDmitry Karpeev }
363708da532bSDmitry Karpeev 
3638b0ae01b7SPeter Brune /*@
3639b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3640b0ae01b7SPeter Brune 
3641b0ae01b7SPeter Brune     Not Collective
3642b0ae01b7SPeter Brune 
3643b0ae01b7SPeter Brune     Input Parameter:
3644b0ae01b7SPeter Brune .   dm - the DM object
3645b0ae01b7SPeter Brune 
3646b0ae01b7SPeter Brune     Output Parameter:
3647b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3648b0ae01b7SPeter Brune 
3649b0ae01b7SPeter Brune     Level: developer
3650b0ae01b7SPeter Brune 
36511565f0a7SPatrick Sanan .seealso DMCreateColoring()
3652b0ae01b7SPeter Brune 
3653b0ae01b7SPeter Brune @*/
3654b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3655b0ae01b7SPeter Brune {
3656b0ae01b7SPeter Brune   PetscFunctionBegin;
36575a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3658534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3659b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3660b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3661b0ae01b7SPeter Brune }
3662b0ae01b7SPeter Brune 
36633ad4599aSBarry Smith /*@
36643ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
36653ad4599aSBarry Smith 
36663ad4599aSBarry Smith     Not Collective
36673ad4599aSBarry Smith 
36683ad4599aSBarry Smith     Input Parameter:
36693ad4599aSBarry Smith .   dm - the DM object
36703ad4599aSBarry Smith 
36713ad4599aSBarry Smith     Output Parameter:
36723ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
36733ad4599aSBarry Smith 
36743ad4599aSBarry Smith     Level: developer
36753ad4599aSBarry Smith 
36761565f0a7SPatrick Sanan .seealso DMCreateRestriction()
36773ad4599aSBarry Smith 
36783ad4599aSBarry Smith @*/
36793ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
36803ad4599aSBarry Smith {
36813ad4599aSBarry Smith   PetscFunctionBegin;
36825a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3683534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
36843ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
36853ad4599aSBarry Smith   PetscFunctionReturn(0);
36863ad4599aSBarry Smith }
36873ad4599aSBarry Smith 
3688a7058e45SLawrence Mitchell 
3689a7058e45SLawrence Mitchell /*@
3690a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3691a7058e45SLawrence Mitchell 
3692a7058e45SLawrence Mitchell     Not Collective
3693a7058e45SLawrence Mitchell 
3694a7058e45SLawrence Mitchell     Input Parameter:
3695a7058e45SLawrence Mitchell .   dm - the DM object
3696a7058e45SLawrence Mitchell 
3697a7058e45SLawrence Mitchell     Output Parameter:
3698a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3699a7058e45SLawrence Mitchell 
3700a7058e45SLawrence Mitchell     Level: developer
3701a7058e45SLawrence Mitchell 
37021565f0a7SPatrick Sanan .seealso DMCreateInjection()
3703a7058e45SLawrence Mitchell 
3704a7058e45SLawrence Mitchell @*/
3705a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3706a7058e45SLawrence Mitchell {
37074a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
37085a84ad33SLisandro Dalcin 
3709a7058e45SLawrence Mitchell   PetscFunctionBegin;
37105a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3711534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37125a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
37134a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
37145a84ad33SLisandro Dalcin   } else {
37155a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
37165a84ad33SLisandro Dalcin   }
3717a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3718a7058e45SLawrence Mitchell }
3719a7058e45SLawrence Mitchell 
37200298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3721264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3722264ace61SBarry Smith 
3723264ace61SBarry Smith /*@C
3724264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3725264ace61SBarry Smith 
3726d083f849SBarry Smith   Collective on dm
3727264ace61SBarry Smith 
3728264ace61SBarry Smith   Input Parameters:
3729264ace61SBarry Smith + dm     - The DM object
3730264ace61SBarry Smith - method - The name of the DM type
3731264ace61SBarry Smith 
3732264ace61SBarry Smith   Options Database Key:
3733264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3734264ace61SBarry Smith 
3735264ace61SBarry Smith   Notes:
3736e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3737264ace61SBarry Smith 
3738264ace61SBarry Smith   Level: intermediate
3739264ace61SBarry Smith 
3740264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3741264ace61SBarry Smith @*/
374219fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3743264ace61SBarry Smith {
3744264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3745264ace61SBarry Smith   PetscBool      match;
3746264ace61SBarry Smith   PetscErrorCode ierr;
3747264ace61SBarry Smith 
3748264ace61SBarry Smith   PetscFunctionBegin;
3749264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3750251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3751264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3752264ace61SBarry Smith 
37530f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
37541c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3755ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3756264ace61SBarry Smith 
3757264ace61SBarry Smith   if (dm->ops->destroy) {
3758264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3759264ace61SBarry Smith   }
3760d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3761264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3762d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3763264ace61SBarry Smith   PetscFunctionReturn(0);
3764264ace61SBarry Smith }
3765264ace61SBarry Smith 
3766264ace61SBarry Smith /*@C
3767264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3768264ace61SBarry Smith 
3769264ace61SBarry Smith   Not Collective
3770264ace61SBarry Smith 
3771264ace61SBarry Smith   Input Parameter:
3772264ace61SBarry Smith . dm  - The DM
3773264ace61SBarry Smith 
3774264ace61SBarry Smith   Output Parameter:
3775264ace61SBarry Smith . type - The DM type name
3776264ace61SBarry Smith 
3777264ace61SBarry Smith   Level: intermediate
3778264ace61SBarry Smith 
3779264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3780264ace61SBarry Smith @*/
378119fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3782264ace61SBarry Smith {
3783264ace61SBarry Smith   PetscErrorCode ierr;
3784264ace61SBarry Smith 
3785264ace61SBarry Smith   PetscFunctionBegin;
3786264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3787c959eef4SJed Brown   PetscValidPointer(type,2);
3788607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3789264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3790264ace61SBarry Smith   PetscFunctionReturn(0);
3791264ace61SBarry Smith }
3792264ace61SBarry Smith 
379367a56275SMatthew G Knepley /*@C
379467a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
379567a56275SMatthew G Knepley 
3796d083f849SBarry Smith   Collective on dm
379767a56275SMatthew G Knepley 
379867a56275SMatthew G Knepley   Input Parameters:
379967a56275SMatthew G Knepley + dm - the DM
380067a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
380167a56275SMatthew G Knepley 
380267a56275SMatthew G Knepley   Output Parameter:
380367a56275SMatthew G Knepley . M - pointer to new DM
380467a56275SMatthew G Knepley 
380567a56275SMatthew G Knepley   Notes:
380667a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
380767a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
380867a56275SMatthew G Knepley   of the input DM.
380967a56275SMatthew G Knepley 
381067a56275SMatthew G Knepley   Level: intermediate
381167a56275SMatthew G Knepley 
381267a56275SMatthew G Knepley .seealso: DMCreate()
381367a56275SMatthew G Knepley @*/
381419fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
381567a56275SMatthew G Knepley {
381667a56275SMatthew G Knepley   DM             B;
381767a56275SMatthew G Knepley   char           convname[256];
3818c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
381967a56275SMatthew G Knepley   PetscErrorCode ierr;
382067a56275SMatthew G Knepley 
382167a56275SMatthew G Knepley   PetscFunctionBegin;
382267a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
382367a56275SMatthew G Knepley   PetscValidType(dm,1);
382467a56275SMatthew G Knepley   PetscValidPointer(M,3);
3825251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3826c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3827c067b6caSMatthew G. Knepley   if (sametype) {
3828c067b6caSMatthew G. Knepley     *M   = dm;
3829c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3830c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3831c067b6caSMatthew G. Knepley   } else {
38320298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
383367a56275SMatthew G Knepley 
383467a56275SMatthew G Knepley     /*
383567a56275SMatthew G Knepley        Order of precedence:
383667a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
383767a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
383867a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
383967a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
384067a56275SMatthew G Knepley        5) Use a really basic converter.
384167a56275SMatthew G Knepley     */
384267a56275SMatthew G Knepley 
384367a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3844a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3845a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3846a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3847a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3848a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
38490005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
385067a56275SMatthew G Knepley     if (conv) goto foundconv;
385167a56275SMatthew G Knepley 
385267a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
385382f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
385467a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3855a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3856a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3857a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3858a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3859a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
38600005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
386167a56275SMatthew G Knepley     if (conv) {
3862fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
386367a56275SMatthew G Knepley       goto foundconv;
386467a56275SMatthew G Knepley     }
386567a56275SMatthew G Knepley 
386667a56275SMatthew G Knepley #if 0
386767a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
386867a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3869fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
387067a56275SMatthew G Knepley     if (conv) goto foundconv;
387167a56275SMatthew G Knepley 
387267a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
387367a56275SMatthew G Knepley     if (dm->ops->convert) {
387467a56275SMatthew G Knepley       conv = dm->ops->convert;
387567a56275SMatthew G Knepley     }
387667a56275SMatthew G Knepley     if (conv) goto foundconv;
387767a56275SMatthew G Knepley #endif
387867a56275SMatthew G Knepley 
387967a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
388082f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
388167a56275SMatthew G Knepley 
388267a56275SMatthew G Knepley foundconv:
388367a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
388467a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
388512fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
388690b157c4SStefano Zampini     {
388790b157c4SStefano Zampini       PetscBool             isper;
388812fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
388912fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
389090b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
389190b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
3892c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
3893a587d139SMark       ierr = PetscFree((*M)->vectype);CHKERRQ(ierr);
3894a587d139SMark       ierr = PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype);CHKERRQ(ierr);
3895a587d139SMark       ierr = PetscFree((*M)->mattype);CHKERRQ(ierr);
3896a587d139SMark       ierr = PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype);CHKERRQ(ierr);
389712fa691eSMatthew G. Knepley     }
389867a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
389967a56275SMatthew G Knepley   }
390067a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
390167a56275SMatthew G Knepley   PetscFunctionReturn(0);
390267a56275SMatthew G Knepley }
3903264ace61SBarry Smith 
3904264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3905264ace61SBarry Smith 
3906264ace61SBarry Smith /*@C
39071c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
39081c84c290SBarry Smith 
39091c84c290SBarry Smith   Not Collective
39101c84c290SBarry Smith 
39111c84c290SBarry Smith   Input Parameters:
39121c84c290SBarry Smith + name        - The name of a new user-defined creation routine
39131c84c290SBarry Smith - create_func - The creation routine itself
39141c84c290SBarry Smith 
39151c84c290SBarry Smith   Notes:
39161c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
39171c84c290SBarry Smith 
39181c84c290SBarry Smith 
39191c84c290SBarry Smith   Sample usage:
39201c84c290SBarry Smith .vb
3921bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
39221c84c290SBarry Smith .ve
39231c84c290SBarry Smith 
39241c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
39251c84c290SBarry Smith .vb
39261c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
39271c84c290SBarry Smith     DMSetType(DM,"my_da");
39281c84c290SBarry Smith .ve
39291c84c290SBarry Smith    or at runtime via the option
39301c84c290SBarry Smith .vb
39311c84c290SBarry Smith     -da_type my_da
39321c84c290SBarry Smith .ve
3933264ace61SBarry Smith 
3934264ace61SBarry Smith   Level: advanced
39351c84c290SBarry Smith 
3936bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
39371c84c290SBarry Smith 
3938264ace61SBarry Smith @*/
3939bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3940264ace61SBarry Smith {
3941264ace61SBarry Smith   PetscErrorCode ierr;
3942264ace61SBarry Smith 
3943264ace61SBarry Smith   PetscFunctionBegin;
39441d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3945a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3946264ace61SBarry Smith   PetscFunctionReturn(0);
3947264ace61SBarry Smith }
3948264ace61SBarry Smith 
3949b859378eSBarry Smith /*@C
395055849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3951b859378eSBarry Smith 
3952d083f849SBarry Smith   Collective on viewer
3953b859378eSBarry Smith 
3954b859378eSBarry Smith   Input Parameters:
3955b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3956b859378eSBarry Smith            some related function before a call to DMLoad().
3957b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3958b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3959b859378eSBarry Smith 
3960b859378eSBarry Smith    Level: intermediate
3961b859378eSBarry Smith 
3962b859378eSBarry Smith   Notes:
396355849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3964b859378eSBarry Smith 
3965b859378eSBarry Smith   Notes for advanced users:
3966b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3967b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3968b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3969b859378eSBarry Smith   format is
3970b859378eSBarry Smith .vb
3971b859378eSBarry Smith      has not yet been determined
3972b859378eSBarry Smith .ve
3973b859378eSBarry Smith 
3974b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3975b859378eSBarry Smith @*/
3976b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3977b859378eSBarry Smith {
39789331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3979b859378eSBarry Smith   PetscErrorCode ierr;
3980b859378eSBarry Smith 
3981b859378eSBarry Smith   PetscFunctionBegin;
3982b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3983b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3984fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
398532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
39869331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
398758cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
39889331c7a4SMatthew G. Knepley   if (isbinary) {
39899331c7a4SMatthew G. Knepley     PetscInt classid;
39909331c7a4SMatthew G. Knepley     char     type[256];
3991b859378eSBarry Smith 
3992060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
39939200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3994060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
399532c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
39969331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
39979331c7a4SMatthew G. Knepley   } else if (ishdf5) {
39989331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
39999331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
400058cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
4001b859378eSBarry Smith   PetscFunctionReturn(0);
4002b859378eSBarry Smith }
4003b859378eSBarry Smith 
4004b2e4378dSMatthew G. Knepley /*@
4005b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
4006b2e4378dSMatthew G. Knepley 
4007b2e4378dSMatthew G. Knepley   Not collective
4008b2e4378dSMatthew G. Knepley 
4009b2e4378dSMatthew G. Knepley   Input Parameter:
4010b2e4378dSMatthew G. Knepley . dm - the DM
4011b2e4378dSMatthew G. Knepley 
4012b2e4378dSMatthew G. Knepley   Output Parameters:
4013b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
4014b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
4015b2e4378dSMatthew G. Knepley 
4016b2e4378dSMatthew G. Knepley   Level: beginner
4017b2e4378dSMatthew G. Knepley 
4018b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
4019b2e4378dSMatthew G. Knepley 
4020b2e4378dSMatthew G. Knepley 
4021b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
4022b2e4378dSMatthew G. Knepley @*/
4023b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
4024b2e4378dSMatthew G. Knepley {
4025b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
4026b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
4027b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
4028b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
4029b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
4030b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
4031b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
4032b2e4378dSMatthew G. Knepley 
4033b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4034b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4035b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4036b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
4037b2e4378dSMatthew G. Knepley   if (coords) {
4038b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
4039b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
4040b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
4041b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
4042b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
4043b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4044b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4045b2e4378dSMatthew G. Knepley       }
4046b2e4378dSMatthew G. Knepley     }
4047b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
4048b2e4378dSMatthew G. Knepley   } else {
4049b2e4378dSMatthew G. Knepley     PetscBool isda;
4050b2e4378dSMatthew G. Knepley 
4051b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
4052b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
4053b2e4378dSMatthew G. Knepley   }
4054b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
4055b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
4056b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4057b2e4378dSMatthew G. Knepley }
4058b2e4378dSMatthew G. Knepley 
4059b2e4378dSMatthew G. Knepley /*@
4060b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
4061b2e4378dSMatthew G. Knepley 
4062b2e4378dSMatthew G. Knepley   Collective
4063b2e4378dSMatthew G. Knepley 
4064b2e4378dSMatthew G. Knepley   Input Parameter:
4065b2e4378dSMatthew G. Knepley . dm - the DM
4066b2e4378dSMatthew G. Knepley 
4067b2e4378dSMatthew G. Knepley   Output Parameters:
4068b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
4069b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
4070b2e4378dSMatthew G. Knepley 
4071b2e4378dSMatthew G. Knepley   Level: beginner
4072b2e4378dSMatthew G. Knepley 
4073b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
4074b2e4378dSMatthew G. Knepley @*/
4075b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
4076b2e4378dSMatthew G. Knepley {
4077b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
40786ce308c4SMatthew G. Knepley   PetscInt       cdim;
40796ce308c4SMatthew G. Knepley   PetscMPIInt    count;
4080b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
4081b2e4378dSMatthew G. Knepley 
4082b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4083b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4084b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4085b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
4086b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
4087820f2d46SBarry Smith   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4088820f2d46SBarry Smith   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4089b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4090b2e4378dSMatthew G. Knepley }
4091b2e4378dSMatthew G. Knepley 
40927da65231SMatthew G Knepley /******************************** FEM Support **********************************/
40937da65231SMatthew G Knepley 
4094a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4095a6dfd86eSKarl Rupp {
40961d47ebbbSSatish Balay   PetscInt       f;
40971b30c384SMatthew G Knepley   PetscErrorCode ierr;
40981b30c384SMatthew G Knepley 
40997da65231SMatthew G Knepley   PetscFunctionBegin;
410074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
41011d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
410257622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
41037da65231SMatthew G Knepley   }
41047da65231SMatthew G Knepley   PetscFunctionReturn(0);
41057da65231SMatthew G Knepley }
41067da65231SMatthew G Knepley 
4107a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4108a6dfd86eSKarl Rupp {
41091b30c384SMatthew G Knepley   PetscInt       f, g;
41107da65231SMatthew G Knepley   PetscErrorCode ierr;
41117da65231SMatthew G Knepley 
41127da65231SMatthew G Knepley   PetscFunctionBegin;
411374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
41141d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
411574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
41161d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
4117e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
41187da65231SMatthew G Knepley     }
411974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
41207da65231SMatthew G Knepley   }
41217da65231SMatthew G Knepley   PetscFunctionReturn(0);
41227da65231SMatthew G Knepley }
4123e7c4fc90SDmitry Karpeev 
41246113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4125e759306cSMatthew G. Knepley {
41260c5b8624SToby Isaac   PetscInt          localSize, bs;
41270c5b8624SToby Isaac   PetscMPIInt       size;
41280c5b8624SToby Isaac   Vec               x, xglob;
41290c5b8624SToby Isaac   const PetscScalar *xarray;
4130e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
4131e759306cSMatthew G. Knepley 
4132e759306cSMatthew G. Knepley   PetscFunctionBegin;
4133ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRMPI(ierr);
4134e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
4135e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
41366113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
41370c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
41380c5b8624SToby Isaac   if (size > 1) {
41390c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
41400c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
41410c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
41420c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
41430c5b8624SToby Isaac   } else {
41440c5b8624SToby Isaac     xglob = x;
41450c5b8624SToby Isaac   }
41460c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
41470c5b8624SToby Isaac   if (size > 1) {
41480c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
41490c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
41500c5b8624SToby Isaac   }
4151e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
4152e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4153e759306cSMatthew G. Knepley }
4154e759306cSMatthew G. Knepley 
415588ed4aceSMatthew G Knepley /*@
41561bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4157061576a5SJed Brown 
4158061576a5SJed Brown   Input Parameter:
4159061576a5SJed Brown . dm - The DM
4160061576a5SJed Brown 
4161061576a5SJed Brown   Output Parameter:
4162061576a5SJed Brown . section - The PetscSection
4163061576a5SJed Brown 
4164061576a5SJed Brown   Options Database Keys:
4165061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4166061576a5SJed Brown 
4167061576a5SJed Brown   Level: advanced
4168061576a5SJed Brown 
4169061576a5SJed Brown   Notes:
4170061576a5SJed Brown   Use DMGetLocalSection() in new code.
4171061576a5SJed Brown 
4172061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4173061576a5SJed Brown 
4174061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
4175061576a5SJed Brown @*/
4176061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4177061576a5SJed Brown {
4178061576a5SJed Brown   PetscErrorCode ierr;
4179061576a5SJed Brown 
4180061576a5SJed Brown   PetscFunctionBegin;
4181061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
4182061576a5SJed Brown   PetscFunctionReturn(0);
4183061576a5SJed Brown }
4184061576a5SJed Brown 
4185061576a5SJed Brown /*@
4186061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
418788ed4aceSMatthew G Knepley 
418888ed4aceSMatthew G Knepley   Input Parameter:
418988ed4aceSMatthew G Knepley . dm - The DM
419088ed4aceSMatthew G Knepley 
419188ed4aceSMatthew G Knepley   Output Parameter:
419288ed4aceSMatthew G Knepley . section - The PetscSection
419388ed4aceSMatthew G Knepley 
4194e5893cccSMatthew G. Knepley   Options Database Keys:
4195e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4196e5893cccSMatthew G. Knepley 
419788ed4aceSMatthew G Knepley   Level: intermediate
419888ed4aceSMatthew G Knepley 
419988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
420088ed4aceSMatthew G Knepley 
4201061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
420288ed4aceSMatthew G Knepley @*/
4203061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
42040adebc6cSBarry Smith {
4205fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4206fd59a867SMatthew G. Knepley 
420788ed4aceSMatthew G Knepley   PetscFunctionBegin;
420888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
420988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42101bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4211e5e52638SMatthew G. Knepley     PetscInt d;
4212e5e52638SMatthew G. Knepley 
421345480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
421445480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject) dm;
421545480ffeSMatthew G. Knepley       PetscViewer       viewer;
421645480ffeSMatthew G. Knepley       PetscViewerFormat format;
421745480ffeSMatthew G. Knepley       PetscBool         flg;
421845480ffeSMatthew G. Knepley 
421945480ffeSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg);CHKERRQ(ierr);
422045480ffeSMatthew G. Knepley       if (flg) {ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);}
422145480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
422245480ffeSMatthew G. Knepley         ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);
422345480ffeSMatthew G. Knepley         if (flg) {ierr = PetscDSView(dm->probs[d].ds, viewer);CHKERRQ(ierr);}
422445480ffeSMatthew G. Knepley       }
422545480ffeSMatthew G. Knepley       if (flg) {
422645480ffeSMatthew G. Knepley         ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
422745480ffeSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
422845480ffeSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
422945480ffeSMatthew G. Knepley       }
423045480ffeSMatthew G. Knepley     }
42311bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
42321bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
42332f0f8703SMatthew G. Knepley   }
42341bb6d2a8SBarry Smith   *section = dm->localSection;
423588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
423688ed4aceSMatthew G Knepley }
423788ed4aceSMatthew G Knepley 
423888ed4aceSMatthew G Knepley /*@
42391bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4240061576a5SJed Brown 
4241061576a5SJed Brown   Input Parameters:
4242061576a5SJed Brown + dm - The DM
4243061576a5SJed Brown - section - The PetscSection
4244061576a5SJed Brown 
4245061576a5SJed Brown   Level: advanced
4246061576a5SJed Brown 
4247061576a5SJed Brown   Notes:
4248061576a5SJed Brown   Use DMSetLocalSection() in new code.
4249061576a5SJed Brown 
4250061576a5SJed Brown   Any existing Section will be destroyed
4251061576a5SJed Brown 
4252061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4253061576a5SJed Brown @*/
4254061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4255061576a5SJed Brown {
4256061576a5SJed Brown   PetscErrorCode ierr;
4257061576a5SJed Brown 
4258061576a5SJed Brown   PetscFunctionBegin;
4259061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4260061576a5SJed Brown   PetscFunctionReturn(0);
4261061576a5SJed Brown }
4262061576a5SJed Brown 
4263061576a5SJed Brown /*@
4264061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
426588ed4aceSMatthew G Knepley 
426688ed4aceSMatthew G Knepley   Input Parameters:
426788ed4aceSMatthew G Knepley + dm - The DM
426888ed4aceSMatthew G Knepley - section - The PetscSection
426988ed4aceSMatthew G Knepley 
427088ed4aceSMatthew G Knepley   Level: intermediate
427188ed4aceSMatthew G Knepley 
427288ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
427388ed4aceSMatthew G Knepley 
4274061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
427588ed4aceSMatthew G Knepley @*/
4276061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
42770adebc6cSBarry Smith {
4278c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4279af122d2aSMatthew G Knepley   PetscInt       f;
428088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
428188ed4aceSMatthew G Knepley 
428288ed4aceSMatthew G Knepley   PetscFunctionBegin;
428388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4284b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42851d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42861bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
42871bb6d2a8SBarry Smith   dm->localSection = section;
42881bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4289af122d2aSMatthew G Knepley   if (numFields) {
4290af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4291af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
42920f21e855SMatthew G. Knepley       PetscObject disc;
4293af122d2aSMatthew G Knepley       const char *name;
4294af122d2aSMatthew G Knepley 
42951bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
429644a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
42970f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4298af122d2aSMatthew G Knepley     }
4299af122d2aSMatthew G Knepley   }
4300e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43011bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
430288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
430388ed4aceSMatthew G Knepley }
430488ed4aceSMatthew G Knepley 
43059435951eSToby Isaac /*@
4306b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
43079435951eSToby Isaac 
4308e228b242SToby Isaac   not collective
4309e228b242SToby Isaac 
43109435951eSToby Isaac   Input Parameter:
43119435951eSToby Isaac . dm - The DM
43129435951eSToby Isaac 
43139435951eSToby Isaac   Output Parameter:
43149435951eSToby 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.
43159435951eSToby 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.
43169435951eSToby Isaac 
43179435951eSToby Isaac   Level: advanced
43189435951eSToby Isaac 
43199435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
43209435951eSToby Isaac 
43219435951eSToby Isaac .seealso: DMSetDefaultConstraints()
43229435951eSToby Isaac @*/
43239435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
43249435951eSToby Isaac {
43259435951eSToby Isaac   PetscErrorCode ierr;
43269435951eSToby Isaac 
43279435951eSToby Isaac   PetscFunctionBegin;
43289435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43299435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
433045a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
433145a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
43329435951eSToby Isaac   PetscFunctionReturn(0);
43339435951eSToby Isaac }
43349435951eSToby Isaac 
43359435951eSToby Isaac /*@
4336b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
43379435951eSToby Isaac 
43389435951eSToby 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().
43399435951eSToby Isaac 
43409435951eSToby 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.
43419435951eSToby Isaac 
4342e228b242SToby Isaac   collective on dm
4343e228b242SToby Isaac 
43449435951eSToby Isaac   Input Parameters:
43459435951eSToby Isaac + dm - The DM
4346e228b242SToby 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).
4347e228b242SToby 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).
43489435951eSToby Isaac 
43499435951eSToby Isaac   Level: advanced
43509435951eSToby Isaac 
43519435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
43529435951eSToby Isaac 
43539435951eSToby Isaac .seealso: DMGetDefaultConstraints()
43549435951eSToby Isaac @*/
43559435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
43569435951eSToby Isaac {
4357e228b242SToby Isaac   PetscMPIInt result;
43589435951eSToby Isaac   PetscErrorCode ierr;
43599435951eSToby Isaac 
43609435951eSToby Isaac   PetscFunctionBegin;
43619435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4362e228b242SToby Isaac   if (section) {
4363e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4364ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRMPI(ierr);
4365f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4366e228b242SToby Isaac   }
4367e228b242SToby Isaac   if (mat) {
4368e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4369ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRMPI(ierr);
4370f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4371e228b242SToby Isaac   }
43729435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
43739435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
43749435951eSToby Isaac   dm->defaultConstraintSection = section;
43759435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
43769435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
43779435951eSToby Isaac   dm->defaultConstraintMat = mat;
43789435951eSToby Isaac   PetscFunctionReturn(0);
43799435951eSToby Isaac }
43809435951eSToby Isaac 
4381497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4382507e4973SMatthew G. Knepley /*
4383507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4384507e4973SMatthew G. Knepley 
4385507e4973SMatthew G. Knepley   Input Parameters:
4386507e4973SMatthew G. Knepley + dm - The DM
4387507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4388507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4389507e4973SMatthew G. Knepley 
4390507e4973SMatthew G. Knepley   Level: intermediate
4391507e4973SMatthew G. Knepley 
43921bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4393507e4973SMatthew G. Knepley */
4394f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4395507e4973SMatthew G. Knepley {
4396507e4973SMatthew G. Knepley   MPI_Comm        comm;
4397507e4973SMatthew G. Knepley   PetscLayout     layout;
4398507e4973SMatthew G. Knepley   const PetscInt *ranges;
4399507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4400507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4401507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4402507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4403507e4973SMatthew G. Knepley 
4404507e4973SMatthew G. Knepley   PetscFunctionBegin;
4405507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4406507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4407ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
4408ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
4409507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4410507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4411507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4412507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4413507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4414507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4415507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4416507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4417f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4418507e4973SMatthew G. Knepley 
4419507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4420507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4421507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4422507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4423507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4424507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4425507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4426507e4973SMatthew 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;}
4427507e4973SMatthew 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;}
4428507e4973SMatthew G. Knepley     if (gdof < 0) {
4429507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4430507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4431507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4432507e4973SMatthew G. Knepley 
4433507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4434507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4435507e4973SMatthew 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;}
4436507e4973SMatthew G. Knepley       }
4437507e4973SMatthew G. Knepley     }
4438507e4973SMatthew G. Knepley   }
4439507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4440507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4441820f2d46SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRMPI(ierr);
4442507e4973SMatthew G. Knepley   if (!gvalid) {
4443507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4444507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4445507e4973SMatthew G. Knepley   }
4446507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4447507e4973SMatthew G. Knepley }
4448f741bcd2SMatthew G. Knepley #endif
4449507e4973SMatthew G. Knepley 
445088ed4aceSMatthew G Knepley /*@
4451e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
445288ed4aceSMatthew G Knepley 
4453d083f849SBarry Smith   Collective on dm
44548b1ab98fSJed Brown 
445588ed4aceSMatthew G Knepley   Input Parameter:
445688ed4aceSMatthew G Knepley . dm - The DM
445788ed4aceSMatthew G Knepley 
445888ed4aceSMatthew G Knepley   Output Parameter:
445988ed4aceSMatthew G Knepley . section - The PetscSection
446088ed4aceSMatthew G Knepley 
446188ed4aceSMatthew G Knepley   Level: intermediate
446288ed4aceSMatthew G Knepley 
446388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
446488ed4aceSMatthew G Knepley 
446592fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
446688ed4aceSMatthew G Knepley @*/
4467e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
44680adebc6cSBarry Smith {
446988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
447088ed4aceSMatthew G Knepley 
447188ed4aceSMatthew G Knepley   PetscFunctionBegin;
447288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
447388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
44741bb6d2a8SBarry Smith   if (!dm->globalSection) {
4475fd59a867SMatthew G. Knepley     PetscSection s;
4476fd59a867SMatthew G. Knepley 
447792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4478fd59a867SMatthew 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");
447933907cc2SStefano 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");
44801bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4481cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
44821bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
44831bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
448488ed4aceSMatthew G Knepley   }
44851bb6d2a8SBarry Smith   *section = dm->globalSection;
448688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
448788ed4aceSMatthew G Knepley }
448888ed4aceSMatthew G Knepley 
4489b21d0597SMatthew G Knepley /*@
4490e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4491b21d0597SMatthew G Knepley 
4492b21d0597SMatthew G Knepley   Input Parameters:
4493b21d0597SMatthew G Knepley + dm - The DM
44945080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4495b21d0597SMatthew G Knepley 
4496b21d0597SMatthew G Knepley   Level: intermediate
4497b21d0597SMatthew G Knepley 
4498b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4499b21d0597SMatthew G Knepley 
450092fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4501b21d0597SMatthew G Knepley @*/
4502e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
45030adebc6cSBarry Smith {
4504b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4505b21d0597SMatthew G Knepley 
4506b21d0597SMatthew G Knepley   PetscFunctionBegin;
4507b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45085080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
45091d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
45101bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
45111bb6d2a8SBarry Smith   dm->globalSection = section;
4512497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45131bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4514507e4973SMatthew G. Knepley #endif
4515b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4516b21d0597SMatthew G Knepley }
4517b21d0597SMatthew G Knepley 
451888ed4aceSMatthew G Knepley /*@
45191bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
452088ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
452188ed4aceSMatthew G Knepley 
452288ed4aceSMatthew G Knepley   Input Parameter:
452388ed4aceSMatthew G Knepley . dm - The DM
452488ed4aceSMatthew G Knepley 
452588ed4aceSMatthew G Knepley   Output Parameter:
452688ed4aceSMatthew G Knepley . sf - The PetscSF
452788ed4aceSMatthew G Knepley 
452888ed4aceSMatthew G Knepley   Level: intermediate
452988ed4aceSMatthew G Knepley 
453088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
453188ed4aceSMatthew G Knepley 
45321bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
453388ed4aceSMatthew G Knepley @*/
45341bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
45350adebc6cSBarry Smith {
453688ed4aceSMatthew G Knepley   PetscInt       nroots;
453788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
453888ed4aceSMatthew G Knepley 
453988ed4aceSMatthew G Knepley   PetscFunctionBegin;
454088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
454188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
45421bb6d2a8SBarry Smith   if (!dm->sectionSF) {
45431bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
454433907cc2SStefano Zampini   }
45451bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
454688ed4aceSMatthew G Knepley   if (nroots < 0) {
454788ed4aceSMatthew G Knepley     PetscSection section, gSection;
454888ed4aceSMatthew G Knepley 
454992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
455031ea6d37SMatthew G Knepley     if (section) {
4551e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
45521bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
455331ea6d37SMatthew G Knepley     } else {
45540298fd71SBarry Smith       *sf = NULL;
455531ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
455631ea6d37SMatthew G Knepley     }
455788ed4aceSMatthew G Knepley   }
45581bb6d2a8SBarry Smith   *sf = dm->sectionSF;
455988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
456088ed4aceSMatthew G Knepley }
456188ed4aceSMatthew G Knepley 
456288ed4aceSMatthew G Knepley /*@
45631bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
456488ed4aceSMatthew G Knepley 
456588ed4aceSMatthew G Knepley   Input Parameters:
456688ed4aceSMatthew G Knepley + dm - The DM
456788ed4aceSMatthew G Knepley - sf - The PetscSF
456888ed4aceSMatthew G Knepley 
456988ed4aceSMatthew G Knepley   Level: intermediate
457088ed4aceSMatthew G Knepley 
457188ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
457288ed4aceSMatthew G Knepley 
45731bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
457488ed4aceSMatthew G Knepley @*/
45751bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
45760adebc6cSBarry Smith {
457788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
457888ed4aceSMatthew G Knepley 
457988ed4aceSMatthew G Knepley   PetscFunctionBegin;
458088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4581b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
458233907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
45831bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
45841bb6d2a8SBarry Smith   dm->sectionSF = sf;
458588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
458688ed4aceSMatthew G Knepley }
458788ed4aceSMatthew G Knepley 
458888ed4aceSMatthew G Knepley /*@C
45891bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
459088ed4aceSMatthew G Knepley   describing the data layout.
459188ed4aceSMatthew G Knepley 
459288ed4aceSMatthew G Knepley   Input Parameters:
459388ed4aceSMatthew G Knepley + dm - The DM
459488ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
459588ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
459688ed4aceSMatthew G Knepley 
45971bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
459888ed4aceSMatthew G Knepley 
45991bb6d2a8SBarry Smith   Level: developer
46001bb6d2a8SBarry Smith 
46011bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
46021bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
46031bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
46041bb6d2a8SBarry Smith 
46051bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
460688ed4aceSMatthew G Knepley @*/
46071bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
460888ed4aceSMatthew G Knepley {
460988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
461088ed4aceSMatthew G Knepley 
461188ed4aceSMatthew G Knepley   PetscFunctionBegin;
461288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4613b0c7db22SLisandro Dalcin   ierr = PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection);CHKERRQ(ierr);
461488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
461588ed4aceSMatthew G Knepley }
4616af122d2aSMatthew G Knepley 
4617b21d0597SMatthew G Knepley /*@
4618b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4619b21d0597SMatthew G Knepley 
4620b21d0597SMatthew G Knepley   Input Parameter:
4621b21d0597SMatthew G Knepley . dm - The DM
4622b21d0597SMatthew G Knepley 
4623b21d0597SMatthew G Knepley   Output Parameter:
4624b21d0597SMatthew G Knepley . sf - The PetscSF
4625b21d0597SMatthew G Knepley 
4626b21d0597SMatthew G Knepley   Level: intermediate
4627b21d0597SMatthew G Knepley 
4628b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4629b21d0597SMatthew G Knepley 
46301bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4631b21d0597SMatthew G Knepley @*/
46320adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
46330adebc6cSBarry Smith {
4634b21d0597SMatthew G Knepley   PetscFunctionBegin;
4635b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4636b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4637b21d0597SMatthew G Knepley   *sf = dm->sf;
4638b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4639b21d0597SMatthew G Knepley }
4640b21d0597SMatthew G Knepley 
4641057b4bcdSMatthew G Knepley /*@
4642057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4643057b4bcdSMatthew G Knepley 
4644057b4bcdSMatthew G Knepley   Input Parameters:
4645057b4bcdSMatthew G Knepley + dm - The DM
4646057b4bcdSMatthew G Knepley - sf - The PetscSF
4647057b4bcdSMatthew G Knepley 
4648057b4bcdSMatthew G Knepley   Level: intermediate
4649057b4bcdSMatthew G Knepley 
46501bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4651057b4bcdSMatthew G Knepley @*/
46520adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
46530adebc6cSBarry Smith {
4654057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4655057b4bcdSMatthew G Knepley 
4656057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4657057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4658b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4659057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
466033907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4661057b4bcdSMatthew G Knepley   dm->sf = sf;
4662057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4663057b4bcdSMatthew G Knepley }
4664057b4bcdSMatthew G Knepley 
466534aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
466634aa8a36SMatthew G. Knepley {
466734aa8a36SMatthew G. Knepley   PetscClassId   id;
466834aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
466934aa8a36SMatthew G. Knepley 
467034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
467134aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
467234aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
467334aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
467434aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
467534aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
467617c1d62eSMatthew G. Knepley   } else {
467717c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
467834aa8a36SMatthew G. Knepley   }
467934aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
468034aa8a36SMatthew G. Knepley }
468134aa8a36SMatthew G. Knepley 
468244a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
468344a7f3ddSMatthew G. Knepley {
468444a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
468544a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
468644a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
468744a7f3ddSMatthew G. Knepley 
468844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
468944a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
469044a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
469144a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4692e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
469344a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
469444a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
469544a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
469644a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
469744a7f3ddSMatthew G. Knepley }
469844a7f3ddSMatthew G. Knepley 
469944a7f3ddSMatthew G. Knepley /*@
470044a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
470144a7f3ddSMatthew G. Knepley 
4702d083f849SBarry Smith   Logically collective on dm
470344a7f3ddSMatthew G. Knepley 
470444a7f3ddSMatthew G. Knepley   Input Parameter:
470544a7f3ddSMatthew G. Knepley . dm - The DM
470644a7f3ddSMatthew G. Knepley 
470744a7f3ddSMatthew G. Knepley   Level: intermediate
470844a7f3ddSMatthew G. Knepley 
470944a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
471044a7f3ddSMatthew G. Knepley @*/
471144a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
471244a7f3ddSMatthew G. Knepley {
471344a7f3ddSMatthew G. Knepley   PetscInt       f;
471444a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
471544a7f3ddSMatthew G. Knepley 
471644a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
471744a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
471844a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
471944a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
472044a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
472144a7f3ddSMatthew G. Knepley   }
472244a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
472344a7f3ddSMatthew G. Knepley   dm->fields = NULL;
472444a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
472544a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
472644a7f3ddSMatthew G. Knepley }
472744a7f3ddSMatthew G. Knepley 
4728689b5837SMatthew G. Knepley /*@
4729689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4730689b5837SMatthew G. Knepley 
4731689b5837SMatthew G. Knepley   Not collective
4732689b5837SMatthew G. Knepley 
4733689b5837SMatthew G. Knepley   Input Parameter:
4734689b5837SMatthew G. Knepley . dm - The DM
4735689b5837SMatthew G. Knepley 
4736689b5837SMatthew G. Knepley   Output Parameter:
4737689b5837SMatthew G. Knepley . Nf - The number of fields
4738689b5837SMatthew G. Knepley 
4739689b5837SMatthew G. Knepley   Level: intermediate
4740689b5837SMatthew G. Knepley 
4741689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4742689b5837SMatthew G. Knepley @*/
47430f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
47440f21e855SMatthew G. Knepley {
47450f21e855SMatthew G. Knepley   PetscFunctionBegin;
47460f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4747534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
474844a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4749af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4750af122d2aSMatthew G Knepley }
4751af122d2aSMatthew G Knepley 
4752689b5837SMatthew G. Knepley /*@
4753689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4754689b5837SMatthew G. Knepley 
4755d083f849SBarry Smith   Logically collective on dm
4756689b5837SMatthew G. Knepley 
4757689b5837SMatthew G. Knepley   Input Parameters:
4758689b5837SMatthew G. Knepley + dm - The DM
4759689b5837SMatthew G. Knepley - Nf - The number of fields
4760689b5837SMatthew G. Knepley 
4761689b5837SMatthew G. Knepley   Level: intermediate
4762689b5837SMatthew G. Knepley 
4763689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4764689b5837SMatthew G. Knepley @*/
4765af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4766af122d2aSMatthew G Knepley {
47670f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4768af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4769af122d2aSMatthew G Knepley 
4770af122d2aSMatthew G Knepley   PetscFunctionBegin;
4771af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
477244a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
47730f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
47740f21e855SMatthew G. Knepley     PetscContainer obj;
47750f21e855SMatthew G. Knepley 
47760f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
477744a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
47780f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4779af122d2aSMatthew G Knepley   }
4780af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4781af122d2aSMatthew G Knepley }
4782af122d2aSMatthew G Knepley 
4783c1929be8SMatthew G. Knepley /*@
4784c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4785c1929be8SMatthew G. Knepley 
4786c1929be8SMatthew G. Knepley   Not collective
4787c1929be8SMatthew G. Knepley 
4788c1929be8SMatthew G. Knepley   Input Parameters:
4789c1929be8SMatthew G. Knepley + dm - The DM
4790c1929be8SMatthew G. Knepley - f  - The field number
4791c1929be8SMatthew G. Knepley 
479244a7f3ddSMatthew G. Knepley   Output Parameters:
479344a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
479444a7f3ddSMatthew G. Knepley - field - The discretization object
4795c1929be8SMatthew G. Knepley 
479644a7f3ddSMatthew G. Knepley   Level: intermediate
4797c1929be8SMatthew G. Knepley 
479844a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4799c1929be8SMatthew G. Knepley @*/
480044a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4801af122d2aSMatthew G Knepley {
4802af122d2aSMatthew G Knepley   PetscFunctionBegin;
4803af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4804064a246eSJacob Faibussowitsch   PetscValidPointer(field, 4);
480544a7f3ddSMatthew 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);
480644a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
480744a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4808decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4809decb47aaSMatthew G. Knepley }
4810decb47aaSMatthew G. Knepley 
4811083401c6SMatthew G. Knepley /* Does not clear the DS */
4812083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4813083401c6SMatthew G. Knepley {
4814083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4815083401c6SMatthew G. Knepley 
4816083401c6SMatthew G. Knepley   PetscFunctionBegin;
4817083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4818083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4819083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4820083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4821083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4822083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4823083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4824083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4825083401c6SMatthew G. Knepley }
4826083401c6SMatthew G. Knepley 
4827c1929be8SMatthew G. Knepley /*@
4828c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4829c1929be8SMatthew G. Knepley 
4830d083f849SBarry Smith   Logically collective on dm
4831c1929be8SMatthew G. Knepley 
4832c1929be8SMatthew G. Knepley   Input Parameters:
4833c1929be8SMatthew G. Knepley + dm    - The DM
4834c1929be8SMatthew G. Knepley . f     - The field number
483544a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4836c1929be8SMatthew G. Knepley - field - The discretization object
4837c1929be8SMatthew G. Knepley 
483844a7f3ddSMatthew G. Knepley   Level: intermediate
4839c1929be8SMatthew G. Knepley 
484044a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4841c1929be8SMatthew G. Knepley @*/
484244a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4843decb47aaSMatthew G. Knepley {
4844decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4845decb47aaSMatthew G. Knepley 
4846decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4847decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4848e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
484944a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4850e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4851083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
485234aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
48532df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
485444a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
485544a7f3ddSMatthew G. Knepley }
485644a7f3ddSMatthew G. Knepley 
485744a7f3ddSMatthew G. Knepley /*@
485844a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
485944a7f3ddSMatthew G. Knepley 
4860d083f849SBarry Smith   Logically collective on dm
486144a7f3ddSMatthew G. Knepley 
486244a7f3ddSMatthew G. Knepley   Input Parameters:
486344a7f3ddSMatthew G. Knepley + dm    - The DM
486444a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
486544a7f3ddSMatthew G. Knepley - field - The discretization object
486644a7f3ddSMatthew G. Knepley 
486744a7f3ddSMatthew G. Knepley   Level: intermediate
486844a7f3ddSMatthew G. Knepley 
486944a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
487044a7f3ddSMatthew G. Knepley @*/
487144a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
487244a7f3ddSMatthew G. Knepley {
487344a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
487444a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
487544a7f3ddSMatthew G. Knepley 
487644a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
487744a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4878064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
487944a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
488044a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
488144a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
488244a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
488344a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
488444a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
488534aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
48862df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4887af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4888af122d2aSMatthew G Knepley }
48896636e97aSMatthew G Knepley 
4890e5e52638SMatthew G. Knepley /*@
4891e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4892e0b68406SMatthew Knepley 
4893e0b68406SMatthew Knepley   Logically collective on dm
4894e0b68406SMatthew Knepley 
4895e0b68406SMatthew Knepley   Input Parameters:
4896e0b68406SMatthew Knepley + dm          - The DM
4897e0b68406SMatthew Knepley . f           - The field index
4898e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
4899e0b68406SMatthew Knepley 
4900e0b68406SMatthew Knepley   Level: intermediate
4901e0b68406SMatthew Knepley 
4902e0b68406SMatthew Knepley .seealso: DMGetFieldAvoidTensor(), DMSetField(), DMGetField()
4903e0b68406SMatthew Knepley @*/
4904e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
4905e0b68406SMatthew Knepley {
4906e0b68406SMatthew Knepley   PetscFunctionBegin;
4907e0b68406SMatthew Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
4908e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
4909e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4910e0b68406SMatthew Knepley }
4911e0b68406SMatthew Knepley 
4912e0b68406SMatthew Knepley /*@
4913e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
4914e0b68406SMatthew Knepley 
4915e0b68406SMatthew Knepley   Logically collective on dm
4916e0b68406SMatthew Knepley 
4917e0b68406SMatthew Knepley   Input Parameters:
4918e0b68406SMatthew Knepley + dm          - The DM
4919e0b68406SMatthew Knepley - f           - The field index
4920e0b68406SMatthew Knepley 
4921e0b68406SMatthew Knepley   Output Parameter:
4922e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
4923e0b68406SMatthew Knepley 
4924e0b68406SMatthew Knepley   Level: intermediate
4925e0b68406SMatthew Knepley 
4926e0b68406SMatthew Knepley .seealso: DMSetFieldAvoidTensor(), DMSetField(), DMGetField()
4927e0b68406SMatthew Knepley @*/
4928e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
4929e0b68406SMatthew Knepley {
4930e0b68406SMatthew Knepley   PetscFunctionBegin;
4931e0b68406SMatthew Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
4932e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
4933e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4934e0b68406SMatthew Knepley }
4935e0b68406SMatthew Knepley 
4936e0b68406SMatthew Knepley /*@
4937e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4938e5e52638SMatthew G. Knepley 
4939d083f849SBarry Smith   Collective on dm
4940e5e52638SMatthew G. Knepley 
4941e5e52638SMatthew G. Knepley   Input Parameter:
4942e5e52638SMatthew G. Knepley . dm - The DM
4943e5e52638SMatthew G. Knepley 
4944e5e52638SMatthew G. Knepley   Output Parameter:
4945e5e52638SMatthew G. Knepley . newdm - The DM
4946e5e52638SMatthew G. Knepley 
4947e5e52638SMatthew G. Knepley   Level: advanced
4948e5e52638SMatthew G. Knepley 
4949e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4950e5e52638SMatthew G. Knepley @*/
4951e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4952e5e52638SMatthew G. Knepley {
4953e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4954e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4955e5e52638SMatthew G. Knepley 
4956e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4957e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4958e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4959e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4960e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4961e5e52638SMatthew G. Knepley     DMLabel     label;
4962e5e52638SMatthew G. Knepley     PetscObject field;
496334aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4964e5e52638SMatthew G. Knepley 
4965e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4966e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
496734aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
496834aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
496934aa8a36SMatthew G. Knepley   }
497034aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
497134aa8a36SMatthew G. Knepley }
497234aa8a36SMatthew G. Knepley 
497334aa8a36SMatthew G. Knepley /*@
497434aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
497534aa8a36SMatthew G. Knepley 
497634aa8a36SMatthew G. Knepley   Not collective
497734aa8a36SMatthew G. Knepley 
497834aa8a36SMatthew G. Knepley   Input Parameters:
497934aa8a36SMatthew G. Knepley + dm - The DM object
498034aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
498134aa8a36SMatthew G. Knepley 
498234aa8a36SMatthew G. Knepley   Output Parameter:
498334aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
498434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
498534aa8a36SMatthew G. Knepley 
498634aa8a36SMatthew G. Knepley   Notes:
498734aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
498834aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
498934aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4990979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
499134aa8a36SMatthew G. Knepley 
499234aa8a36SMatthew G. Knepley   Level: developer
499334aa8a36SMatthew G. Knepley 
499434aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
499534aa8a36SMatthew G. Knepley @*/
499634aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
499734aa8a36SMatthew G. Knepley {
499834aa8a36SMatthew G. Knepley   PetscFunctionBegin;
499934aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5000534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
5001534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
500234aa8a36SMatthew G. Knepley   if (f < 0) {
500334aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
500434aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
500534aa8a36SMatthew G. Knepley   } else {
500634aa8a36SMatthew G. Knepley     PetscInt       Nf;
500734aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
500834aa8a36SMatthew G. Knepley 
500934aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
501034aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
501134aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
501234aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
501334aa8a36SMatthew G. Knepley   }
501434aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
501534aa8a36SMatthew G. Knepley }
501634aa8a36SMatthew G. Knepley 
501734aa8a36SMatthew G. Knepley /*@
501834aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
501934aa8a36SMatthew G. Knepley 
502034aa8a36SMatthew G. Knepley   Not collective
502134aa8a36SMatthew G. Knepley 
502234aa8a36SMatthew G. Knepley   Input Parameters:
502334aa8a36SMatthew G. Knepley + dm         - The DM object
502434aa8a36SMatthew G. Knepley . f          - The field number
502534aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
502634aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
502734aa8a36SMatthew G. Knepley 
502834aa8a36SMatthew G. Knepley   Notes:
502934aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
503034aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
503134aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5032979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
503334aa8a36SMatthew G. Knepley 
503434aa8a36SMatthew G. Knepley   Level: developer
503534aa8a36SMatthew G. Knepley 
503634aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
503734aa8a36SMatthew G. Knepley @*/
503834aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
503934aa8a36SMatthew G. Knepley {
504034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
504134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
504234aa8a36SMatthew G. Knepley   if (f < 0) {
504334aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
504434aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
504534aa8a36SMatthew G. Knepley   } else {
504634aa8a36SMatthew G. Knepley     PetscInt       Nf;
504734aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
504834aa8a36SMatthew G. Knepley 
504934aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
505034aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
505134aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
505234aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5053e5e52638SMatthew G. Knepley   }
5054e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5055e5e52638SMatthew G. Knepley }
5056e5e52638SMatthew G. Knepley 
5057b0441da4SMatthew G. Knepley /*@
5058b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5059b0441da4SMatthew G. Knepley 
5060b0441da4SMatthew G. Knepley   Not collective
5061b0441da4SMatthew G. Knepley 
5062b0441da4SMatthew G. Knepley   Input Parameters:
5063b0441da4SMatthew G. Knepley . dm - The DM object
5064b0441da4SMatthew G. Knepley 
5065b0441da4SMatthew G. Knepley   Output Parameter:
5066b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5067b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5068b0441da4SMatthew G. Knepley 
5069b0441da4SMatthew G. Knepley   Notes:
5070b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5071b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5072b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5073b0441da4SMatthew G. Knepley 
5074b0441da4SMatthew G. Knepley   Level: developer
5075b0441da4SMatthew G. Knepley 
5076b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
5077b0441da4SMatthew G. Knepley @*/
5078b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5079b0441da4SMatthew G. Knepley {
5080b0441da4SMatthew G. Knepley   PetscInt       Nf;
5081b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5082b0441da4SMatthew G. Knepley 
5083b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5084b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5085064a246eSJacob Faibussowitsch   if (useCone)    PetscValidBoolPointer(useCone, 2);
5086064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
5087b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5088b0441da4SMatthew G. Knepley   if (!Nf) {
5089b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5090b0441da4SMatthew G. Knepley   } else {
5091b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5092b0441da4SMatthew G. Knepley   }
5093b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5094b0441da4SMatthew G. Knepley }
5095b0441da4SMatthew G. Knepley 
5096b0441da4SMatthew G. Knepley /*@
5097b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5098b0441da4SMatthew G. Knepley 
5099b0441da4SMatthew G. Knepley   Not collective
5100b0441da4SMatthew G. Knepley 
5101b0441da4SMatthew G. Knepley   Input Parameters:
5102b0441da4SMatthew G. Knepley + dm         - The DM object
5103b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5104b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5105b0441da4SMatthew G. Knepley 
5106b0441da4SMatthew G. Knepley   Notes:
5107b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5108b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5109b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5110b0441da4SMatthew G. Knepley 
5111b0441da4SMatthew G. Knepley   Level: developer
5112b0441da4SMatthew G. Knepley 
5113b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
5114b0441da4SMatthew G. Knepley @*/
5115b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5116b0441da4SMatthew G. Knepley {
5117b0441da4SMatthew G. Knepley   PetscInt       Nf;
5118b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5119b0441da4SMatthew G. Knepley 
5120b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5121b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5122b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5123b0441da4SMatthew G. Knepley   if (!Nf) {
5124b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5125b0441da4SMatthew G. Knepley   } else {
5126b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5127e5e52638SMatthew G. Knepley   }
5128e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5129e5e52638SMatthew G. Knepley }
5130e5e52638SMatthew G. Knepley 
5131783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
513245480ffeSMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, DMLabel label)
5133783e2ec8SMatthew G. Knepley {
5134783e2ec8SMatthew G. Knepley   PetscObject    obj;
5135783e2ec8SMatthew G. Knepley   PetscClassId   id;
5136783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
5137783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
5138783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
5139783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
5140783e2ec8SMatthew G. Knepley 
5141783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5142783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
5143783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5144783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
5145783e2ec8SMatthew G. Knepley   if (isFE && label) {
5146783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
5147783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5148783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
514945480ffeSMatthew G. Knepley       DMLabel l;
5150783e2ec8SMatthew G. Knepley 
515145480ffeSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, NULL, &l, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
515245480ffeSMatthew G. Knepley       duplicate = l == label ? PETSC_TRUE : PETSC_FALSE;
5153783e2ec8SMatthew G. Knepley       if (duplicate) break;
5154783e2ec8SMatthew G. Knepley     }
5155783e2ec8SMatthew G. Knepley     if (!duplicate) {
5156783e2ec8SMatthew G. Knepley       DM plex;
5157783e2ec8SMatthew G. Knepley 
5158783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
5159783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
5160783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
5161783e2ec8SMatthew G. Knepley     }
5162783e2ec8SMatthew G. Knepley   }
5163783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5164783e2ec8SMatthew G. Knepley }
5165783e2ec8SMatthew G. Knepley 
5166e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5167e5e52638SMatthew G. Knepley {
5168e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5169e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5170e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5171e5e52638SMatthew G. Knepley 
5172e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5173e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5174e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
5175e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5176b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5177e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5178e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5179e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5180e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5181e5e52638SMatthew G. Knepley }
5182e5e52638SMatthew G. Knepley 
5183e5e52638SMatthew G. Knepley /*@
5184e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5185e5e52638SMatthew G. Knepley 
5186e5e52638SMatthew G. Knepley   Not collective
5187e5e52638SMatthew G. Knepley 
5188e5e52638SMatthew G. Knepley   Input Parameter:
5189e5e52638SMatthew G. Knepley . dm - The DM
5190e5e52638SMatthew G. Knepley 
5191e5e52638SMatthew G. Knepley   Output Parameter:
5192e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5193e5e52638SMatthew G. Knepley 
5194e5e52638SMatthew G. Knepley   Level: intermediate
5195e5e52638SMatthew G. Knepley 
5196e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5197e5e52638SMatthew G. Knepley @*/
5198e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5199e5e52638SMatthew G. Knepley {
5200e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5201e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5202534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5203e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5204e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5205e5e52638SMatthew G. Knepley }
5206e5e52638SMatthew G. Knepley 
5207e5e52638SMatthew G. Knepley /*@
5208e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5209e5e52638SMatthew G. Knepley 
5210d083f849SBarry Smith   Logically collective on dm
5211e5e52638SMatthew G. Knepley 
5212e5e52638SMatthew G. Knepley   Input Parameter:
5213e5e52638SMatthew G. Knepley . dm - The DM
5214e5e52638SMatthew G. Knepley 
5215e5e52638SMatthew G. Knepley   Level: intermediate
5216e5e52638SMatthew G. Knepley 
5217e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5218e5e52638SMatthew G. Knepley @*/
5219e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5220e5e52638SMatthew G. Knepley {
5221e5e52638SMatthew G. Knepley   PetscInt       s;
5222e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5223e5e52638SMatthew G. Knepley 
5224e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5225e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5226e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5227e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5228e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5229b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5230e5e52638SMatthew G. Knepley   }
5231e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5232e5e52638SMatthew G. Knepley   dm->probs = NULL;
5233e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5234e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5235e5e52638SMatthew G. Knepley }
5236e5e52638SMatthew G. Knepley 
5237e5e52638SMatthew G. Knepley /*@
5238e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5239e5e52638SMatthew G. Knepley 
5240e5e52638SMatthew G. Knepley   Not collective
5241e5e52638SMatthew G. Knepley 
5242e5e52638SMatthew G. Knepley   Input Parameter:
5243e5e52638SMatthew G. Knepley . dm    - The DM
5244e5e52638SMatthew G. Knepley 
5245e5e52638SMatthew G. Knepley   Output Parameter:
5246e5e52638SMatthew G. Knepley . prob - The default PetscDS
5247e5e52638SMatthew G. Knepley 
5248e5e52638SMatthew G. Knepley   Level: intermediate
5249e5e52638SMatthew G. Knepley 
5250e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5251e5e52638SMatthew G. Knepley @*/
5252e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5253e5e52638SMatthew G. Knepley {
5254b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5255b0143b4dSMatthew G. Knepley 
5256e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5257e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5258e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5259b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5260b0143b4dSMatthew G. Knepley     PetscDS ds;
5261b0143b4dSMatthew G. Knepley 
526245480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5263b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5264b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5265b0143b4dSMatthew G. Knepley   }
5266b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5267e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5268e5e52638SMatthew G. Knepley }
5269e5e52638SMatthew G. Knepley 
5270e5e52638SMatthew G. Knepley /*@
5271e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5272e5e52638SMatthew G. Knepley 
5273e5e52638SMatthew G. Knepley   Not collective
5274e5e52638SMatthew G. Knepley 
5275e5e52638SMatthew G. Knepley   Input Parameters:
5276e5e52638SMatthew G. Knepley + dm    - The DM
5277e5e52638SMatthew G. Knepley - point - Cell for the DS
5278e5e52638SMatthew G. Knepley 
5279e5e52638SMatthew G. Knepley   Output Parameter:
5280e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5281e5e52638SMatthew G. Knepley 
5282e5e52638SMatthew G. Knepley   Level: developer
5283e5e52638SMatthew G. Knepley 
5284b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5285e5e52638SMatthew G. Knepley @*/
5286e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5287e5e52638SMatthew G. Knepley {
5288e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5289e5e52638SMatthew G. Knepley   PetscInt       s;
5290e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5291e5e52638SMatthew G. Knepley 
5292e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5293e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5294e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5295360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5296e5e52638SMatthew G. Knepley   *prob = NULL;
5297e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5298e5e52638SMatthew G. Knepley     PetscInt val;
5299e5e52638SMatthew G. Knepley 
5300e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5301e5e52638SMatthew G. Knepley     else {
5302e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5303e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5304e5e52638SMatthew G. Knepley     }
5305e5e52638SMatthew G. Knepley   }
5306e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5307e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5308e5e52638SMatthew G. Knepley }
5309e5e52638SMatthew G. Knepley 
5310e5e52638SMatthew G. Knepley /*@
5311e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5312e5e52638SMatthew G. Knepley 
5313e5e52638SMatthew G. Knepley   Not collective
5314e5e52638SMatthew G. Knepley 
5315e5e52638SMatthew G. Knepley   Input Parameters:
5316e5e52638SMatthew G. Knepley + dm    - The DM
5317e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5318e5e52638SMatthew G. Knepley 
5319b3cf3223SMatthew G. Knepley   Output Parameters:
5320b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5321b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5322e5e52638SMatthew G. Knepley 
5323e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5324e5e52638SMatthew G. Knepley 
5325e5e52638SMatthew G. Knepley   Level: advanced
5326e5e52638SMatthew G. Knepley 
5327e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5328e5e52638SMatthew G. Knepley @*/
5329b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5330e5e52638SMatthew G. Knepley {
5331e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5332e5e52638SMatthew G. Knepley 
5333e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5334e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5335e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5336b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5337b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5338e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5339b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5340b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5341b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5342b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5343b3cf3223SMatthew G. Knepley     }
5344e5e52638SMatthew G. Knepley   }
53452df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5346e5e52638SMatthew G. Knepley }
5347e5e52638SMatthew G. Knepley 
5348e5e52638SMatthew G. Knepley /*@
5349083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5350083401c6SMatthew G. Knepley 
5351083401c6SMatthew G. Knepley   Collective on dm
5352083401c6SMatthew G. Knepley 
5353083401c6SMatthew G. Knepley   Input Parameters:
5354083401c6SMatthew G. Knepley + dm     - The DM
5355083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5356083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5357083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5358083401c6SMatthew G. Knepley 
5359083401c6SMatthew 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,
5360083401c6SMatthew G. Knepley   the fields argument is ignored.
5361083401c6SMatthew G. Knepley 
5362083401c6SMatthew G. Knepley   Level: advanced
5363083401c6SMatthew G. Knepley 
5364083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5365083401c6SMatthew G. Knepley @*/
5366083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5367083401c6SMatthew G. Knepley {
5368083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5369083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5370083401c6SMatthew G. Knepley 
5371083401c6SMatthew G. Knepley   PetscFunctionBegin;
5372083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5373083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5374064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5375083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5376083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5377083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5378083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5379083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5380083401c6SMatthew G. Knepley     }
5381083401c6SMatthew G. Knepley   }
5382083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5383083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5384083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5385083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5386083401c6SMatthew G. Knepley   if (!label) {
5387083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5388083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5389083401c6SMatthew G. Knepley     Nds = 0;
5390083401c6SMatthew G. Knepley   }
5391083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5392083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5393083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5394083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5395083401c6SMatthew G. Knepley }
5396083401c6SMatthew G. Knepley 
5397083401c6SMatthew G. Knepley /*@
5398e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5399e5e52638SMatthew G. Knepley 
5400e5e52638SMatthew G. Knepley   Not collective
5401e5e52638SMatthew G. Knepley 
5402e5e52638SMatthew G. Knepley   Input Parameters:
5403e5e52638SMatthew G. Knepley + dm  - The DM
5404e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5405e5e52638SMatthew G. Knepley 
5406e5e52638SMatthew G. Knepley   Output Parameters:
5407b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5408b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5409083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5410e5e52638SMatthew G. Knepley 
5411e5e52638SMatthew G. Knepley   Level: advanced
5412e5e52638SMatthew G. Knepley 
5413e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5414e5e52638SMatthew G. Knepley @*/
5415b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5416e5e52638SMatthew G. Knepley {
5417e5e52638SMatthew G. Knepley   PetscInt       Nds;
5418e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5419e5e52638SMatthew G. Knepley 
5420e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5421e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5422e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5423e5e52638SMatthew 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);
5424e5e52638SMatthew G. Knepley   if (label) {
5425e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5426e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5427e5e52638SMatthew G. Knepley   }
5428b3cf3223SMatthew G. Knepley   if (fields) {
5429b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5430b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5431b3cf3223SMatthew G. Knepley   }
5432e5e52638SMatthew G. Knepley   if (ds) {
5433b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5434e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5435e5e52638SMatthew G. Knepley   }
5436e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5437e5e52638SMatthew G. Knepley }
5438e5e52638SMatthew G. Knepley 
5439e5e52638SMatthew G. Knepley /*@
5440083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5441e5e52638SMatthew G. Knepley 
5442083401c6SMatthew G. Knepley   Not collective
5443e5e52638SMatthew G. Knepley 
5444e5e52638SMatthew G. Knepley   Input Parameters:
5445e5e52638SMatthew G. Knepley + dm     - The DM
5446083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5447083401c6SMatthew G. Knepley . label  - The region label, or NULL
5448083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5449083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5450e5e52638SMatthew G. Knepley 
5451e5e52638SMatthew G. Knepley   Level: advanced
5452e5e52638SMatthew G. Knepley 
5453083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5454e5e52638SMatthew G. Knepley @*/
5455083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5456e5e52638SMatthew G. Knepley {
5457083401c6SMatthew G. Knepley   PetscInt       Nds;
5458e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5459e5e52638SMatthew G. Knepley 
5460e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5461e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5462083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5463083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5464083401c6SMatthew 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);
5465e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5466083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5467083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5468083401c6SMatthew G. Knepley   if (fields) {
5469083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5470b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5471083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5472083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5473e5e52638SMatthew G. Knepley   }
5474083401c6SMatthew G. Knepley   if (ds) {
5475083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5476083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5477083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5478083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5479083401c6SMatthew G. Knepley   }
5480e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5481e5e52638SMatthew G. Knepley }
5482e5e52638SMatthew G. Knepley 
5483e5e52638SMatthew G. Knepley /*@
54841d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
54851d3af9e0SMatthew G. Knepley 
54861d3af9e0SMatthew G. Knepley   Not collective
54871d3af9e0SMatthew G. Knepley 
54881d3af9e0SMatthew G. Knepley   Input Parameters:
54891d3af9e0SMatthew G. Knepley + dm  - The DM
54901d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
54911d3af9e0SMatthew G. Knepley 
54921d3af9e0SMatthew G. Knepley   Output Parameter:
54931d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
54941d3af9e0SMatthew G. Knepley 
54951d3af9e0SMatthew G. Knepley   Level: advanced
54961d3af9e0SMatthew G. Knepley 
54971d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
54981d3af9e0SMatthew G. Knepley @*/
54991d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
55001d3af9e0SMatthew G. Knepley {
55011d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
55021d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
55031d3af9e0SMatthew G. Knepley 
55041d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
55051d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55061d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
55071d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
55081d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
55091d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
55101d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
55111d3af9e0SMatthew G. Knepley   else          *num = n;
55121d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
55131d3af9e0SMatthew G. Knepley }
55141d3af9e0SMatthew G. Knepley 
55151d3af9e0SMatthew G. Knepley /*@
5516e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5517e5e52638SMatthew G. Knepley 
5518d083f849SBarry Smith   Collective on dm
5519e5e52638SMatthew G. Knepley 
5520e5e52638SMatthew G. Knepley   Input Parameter:
5521e5e52638SMatthew G. Knepley . dm - The DM
5522e5e52638SMatthew G. Knepley 
552345480ffeSMatthew G. Knepley   Options Database Keys:
552445480ffeSMatthew G. Knepley . -dm_petscds_view - View all the PetscDS objects in this DM
552545480ffeSMatthew G. Knepley 
5526e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5527e5e52638SMatthew G. Knepley 
5528e5e52638SMatthew G. Knepley   Level: intermediate
5529e5e52638SMatthew G. Knepley 
5530e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5531e5e52638SMatthew G. Knepley @*/
5532e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5533e5e52638SMatthew G. Knepley {
5534e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5535083401c6SMatthew G. Knepley   PetscDS        dsDef;
5536083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5537f9244615SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5538f9244615SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE, flg;
5539e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5540e5e52638SMatthew G. Knepley 
5541e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5542e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5543e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5544e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5545083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5546083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5547083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5548083401c6SMatthew G. Knepley   Nl   = 0;
5549083401c6SMatthew G. Knepley   Ndef = 0;
5550083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5551083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5552083401c6SMatthew G. Knepley     PetscInt l;
5553083401c6SMatthew G. Knepley 
5554083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5555083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5556083401c6SMatthew G. Knepley     if (l < Nl) continue;
5557083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5558083401c6SMatthew G. Knepley   }
5559083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5560083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5561083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5562b3cf3223SMatthew G. Knepley     IS        fields;
5563b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5564b3cf3223SMatthew G. Knepley 
5565b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5566083401c6SMatthew G. Knepley     if (nf) {
5567b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5568b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
556988f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
557088f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
557188f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
557288f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
557388f0c812SMatthew G. Knepley 
557445480ffeSMatthew G. Knepley       ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5575083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5576083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5577b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
55782df9ee95SMatthew G. Knepley     }
5579083401c6SMatthew G. Knepley   }
5580083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5581083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5582083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5583083401c6SMatthew G. Knepley   if (Ndef && Nl) {
55840122748bSMatthew G. Knepley     DM              plex;
5585083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5586083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5587083401c6SMatthew G. Knepley     PetscInt       *fields;
5588083401c6SMatthew G. Knepley     const PetscInt *cells;
5589083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
55900122748bSMatthew G. Knepley 
55910122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
55920122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5593083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5594083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5595083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5596083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5597083401c6SMatthew G. Knepley       IS      pointIS;
5598083401c6SMatthew G. Knepley 
5599083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5600083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5601083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5602083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5603083401c6SMatthew G. Knepley     }
5604083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5605083401c6SMatthew G. Knepley 
5606083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5607083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5608083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5609083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5610083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5611083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5612083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5613083401c6SMatthew G. Knepley 
5614083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5615083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5616083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5617083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5618083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5619083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5620083401c6SMatthew G. Knepley 
562145480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5622083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5623083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5624083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5625083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5626083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
56270122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5628083401c6SMatthew G. Knepley   }
5629083401c6SMatthew G. Knepley   /* Create label DSes
5630083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5631083401c6SMatthew G. Knepley   */
5632083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5633083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5634083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5635083401c6SMatthew G. Knepley     PetscDS   ds;
5636083401c6SMatthew G. Knepley     IS        fields;
5637083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5638083401c6SMatthew G. Knepley 
563945480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5640083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5641083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5642083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5643083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5644083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5645083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5646083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5647083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5648083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5649083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5650083401c6SMatthew G. Knepley     {
5651083401c6SMatthew G. Knepley       DMPolytopeType ct;
5652083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5653083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
56540122748bSMatthew G. Knepley 
5655e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5656665f567fSMatthew G. Knepley       if (lStart >= 0) {
5657412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5658412e9a14SMatthew G. Knepley         switch (ct) {
5659412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5660412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5661412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5662412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5663083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5664083401c6SMatthew G. Knepley           default: break;
5665412e9a14SMatthew G. Knepley         }
5666665f567fSMatthew G. Knepley       }
5667ffc4695bSBarry Smith       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRMPI(ierr);
5668083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5669e5e52638SMatthew G. Knepley     }
5670083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5671e5e52638SMatthew G. Knepley   }
5672083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5673e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5674083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5675083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5676083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5677083401c6SMatthew G. Knepley     const PetscInt *fld;
5678083401c6SMatthew G. Knepley     PetscInt        nf;
5679e5e52638SMatthew G. Knepley 
5680083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5681083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5682083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5683083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5684083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5685e5e52638SMatthew G. Knepley       PetscClassId id;
5686e5e52638SMatthew G. Knepley 
5687083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5688083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5689083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5690083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5691083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5692e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5693e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5694e5e52638SMatthew G. Knepley     }
5695083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5696e5e52638SMatthew G. Knepley   }
5697f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
5698f9244615SMatthew G. Knepley   ierr = PetscOptionsGetInt(NULL, ((PetscObject) dm)->prefix, "-dm_ds_jet_degree", &k, &flg);CHKERRQ(ierr);
5699f9244615SMatthew G. Knepley   if (flg) {
57003b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
57013b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
57023b4aee56SMatthew G. Knepley       PetscInt Nf, f;
57033b4aee56SMatthew G. Knepley 
57043b4aee56SMatthew G. Knepley       ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
57053b4aee56SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {ierr = PetscDSSetJetDegree(ds, f, k);CHKERRQ(ierr);}
57063b4aee56SMatthew G. Knepley     }
5707f9244615SMatthew G. Knepley   }
5708e5e52638SMatthew G. Knepley   /* Setup DSes */
5709e5e52638SMatthew G. Knepley   if (doSetup) {
5710e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5711e5e52638SMatthew G. Knepley   }
5712e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5713e5e52638SMatthew G. Knepley }
5714e5e52638SMatthew G. Knepley 
5715e5e52638SMatthew G. Knepley /*@
57167f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
57177f96f943SMatthew G. Knepley 
5718f2cacb80SMatthew G. Knepley   Collective on DM
5719f2cacb80SMatthew G. Knepley 
57207f96f943SMatthew G. Knepley   Input Parameters:
57217f96f943SMatthew G. Knepley + dm   - The DM
57227f96f943SMatthew G. Knepley - time - The time
57237f96f943SMatthew G. Knepley 
57247f96f943SMatthew G. Knepley   Output Parameters:
5725f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5726f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
57277f96f943SMatthew G. Knepley 
57287f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
57297f96f943SMatthew G. Knepley 
57307f96f943SMatthew G. Knepley   Level: developer
57317f96f943SMatthew G. Knepley 
57327f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
57337f96f943SMatthew G. Knepley @*/
5734f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
57357f96f943SMatthew G. Knepley {
57367f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
57377f96f943SMatthew G. Knepley   void            **ectxs;
57387f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
57397f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
57407f96f943SMatthew G. Knepley 
57417f96f943SMatthew G. Knepley   PetscFunctionBegin;
5742f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5743f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5744f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
57457f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
57467f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
57477f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
57487f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
57497f96f943SMatthew G. Knepley     PetscDS         ds;
57507f96f943SMatthew G. Knepley     DMLabel         label;
57517f96f943SMatthew G. Knepley     IS              fieldIS;
57527f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
57537f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
57547f96f943SMatthew G. Knepley 
57557f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
57567f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
57577f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
57587f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
57597f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5760f2cacb80SMatthew G. Knepley     if (u) {
57617f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
57627f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
57637f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
57647f96f943SMatthew G. Knepley       }
57657f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
57667f96f943SMatthew G. Knepley       if (label) {
57677f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
57687f96f943SMatthew G. Knepley       } else {
57697f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
57707f96f943SMatthew G. Knepley       }
57717f96f943SMatthew G. Knepley     }
5772f2cacb80SMatthew G. Knepley     if (u_t) {
5773f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5774f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5775f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5776f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5777f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5778f2cacb80SMatthew G. Knepley       }
5779f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5780f2cacb80SMatthew G. Knepley       if (label) {
5781f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5782f2cacb80SMatthew G. Knepley       } else {
5783f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5784f2cacb80SMatthew G. Knepley       }
5785f2cacb80SMatthew G. Knepley     }
5786f2cacb80SMatthew G. Knepley   }
5787f2cacb80SMatthew G. Knepley   if (u) {
57887f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
57897f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5790f2cacb80SMatthew G. Knepley   }
5791f2cacb80SMatthew G. Knepley   if (u_t) {
5792f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
5793f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
5794f2cacb80SMatthew G. Knepley   }
57957f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
57967f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
57977f96f943SMatthew G. Knepley }
57987f96f943SMatthew G. Knepley 
579945480ffeSMatthew G. Knepley PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
580045480ffeSMatthew G. Knepley {
580145480ffeSMatthew G. Knepley   PetscDS        dsNew;
580245480ffeSMatthew G. Knepley   DSBoundary     b;
580345480ffeSMatthew G. Knepley   PetscInt       cdim, Nf, f;
580445480ffeSMatthew G. Knepley   void          *ctx;
580545480ffeSMatthew G. Knepley   PetscErrorCode ierr;
580645480ffeSMatthew G. Knepley 
580745480ffeSMatthew G. Knepley   PetscFunctionBegin;
580845480ffeSMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) ds), &dsNew);CHKERRQ(ierr);
580945480ffeSMatthew G. Knepley   ierr = PetscDSCopyConstants(ds, dsNew);CHKERRQ(ierr);
581045480ffeSMatthew G. Knepley   ierr = PetscDSCopyExactSolutions(ds, dsNew);CHKERRQ(ierr);
581145480ffeSMatthew G. Knepley   ierr = PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
581245480ffeSMatthew G. Knepley   ierr = PetscDSCopyEquations(ds, dsNew);CHKERRQ(ierr);
581345480ffeSMatthew G. Knepley   ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
581445480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
581545480ffeSMatthew G. Knepley     ierr = PetscDSGetContext(ds, f, &ctx);CHKERRQ(ierr);
581645480ffeSMatthew G. Knepley     ierr = PetscDSSetContext(dsNew, f, ctx);CHKERRQ(ierr);
581745480ffeSMatthew G. Knepley   }
581845480ffeSMatthew G. Knepley   if (Nf) {
581945480ffeSMatthew G. Knepley     ierr = PetscDSGetCoordinateDimension(ds, &cdim);CHKERRQ(ierr);
582045480ffeSMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsNew, cdim);CHKERRQ(ierr);
582145480ffeSMatthew G. Knepley   }
582245480ffeSMatthew G. Knepley   ierr = PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
582345480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
582445480ffeSMatthew G. Knepley     ierr = DMGetLabel(dm, b->lname, &b->label);CHKERRQ(ierr);
582545480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
582645480ffeSMatthew G. Knepley     //if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
582745480ffeSMatthew G. Knepley   }
582845480ffeSMatthew G. Knepley 
582945480ffeSMatthew G. Knepley   ierr = DMSetRegionDS(dm, label, fields, dsNew);CHKERRQ(ierr);
583045480ffeSMatthew G. Knepley   ierr = PetscDSDestroy(&dsNew);CHKERRQ(ierr);
583145480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
583245480ffeSMatthew G. Knepley }
583345480ffeSMatthew G. Knepley 
58347f96f943SMatthew G. Knepley /*@
5835e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5836e5e52638SMatthew G. Knepley 
5837d083f849SBarry Smith   Collective on dm
5838e5e52638SMatthew G. Knepley 
5839e5e52638SMatthew G. Knepley   Input Parameter:
5840e5e52638SMatthew G. Knepley . dm - The DM
5841e5e52638SMatthew G. Knepley 
5842e5e52638SMatthew G. Knepley   Output Parameter:
5843e5e52638SMatthew G. Knepley . newdm - The DM
5844e5e52638SMatthew G. Knepley 
5845e5e52638SMatthew G. Knepley   Level: advanced
5846e5e52638SMatthew G. Knepley 
5847e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5848e5e52638SMatthew G. Knepley @*/
5849e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5850e5e52638SMatthew G. Knepley {
5851e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5852e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5853e5e52638SMatthew G. Knepley 
5854e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5855e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5856e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5857e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5858e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5859e5e52638SMatthew G. Knepley     DMLabel  label;
5860b3cf3223SMatthew G. Knepley     IS       fields;
586145480ffeSMatthew G. Knepley     PetscDS  ds, newds;
5862783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5863e5e52638SMatthew G. Knepley 
5864b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
586545480ffeSMatthew G. Knepley     ierr = DMTransferDS_Internal(newdm, label, fields, ds);CHKERRQ(ierr);
586645480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
586745480ffeSMatthew G. Knepley     ierr = DMGetRegionDS(newdm, label, NULL, &newds);CHKERRQ(ierr);
586845480ffeSMatthew G. Knepley     ierr = PetscDSGetNumBoundary(newds, &Nbd);CHKERRQ(ierr);
5869783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
587045480ffeSMatthew G. Knepley       DMLabel  label;
5871783e2ec8SMatthew G. Knepley       PetscInt field;
5872783e2ec8SMatthew G. Knepley 
587345480ffeSMatthew G. Knepley       ierr = PetscDSGetBoundary(newds, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
587445480ffeSMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, newds, field, bd, label);CHKERRQ(ierr);
5875783e2ec8SMatthew G. Knepley     }
5876e5e52638SMatthew G. Knepley   }
5877e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5878e5e52638SMatthew G. Knepley }
5879e5e52638SMatthew G. Knepley 
5880e5e52638SMatthew G. Knepley /*@
5881e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5882e5e52638SMatthew G. Knepley 
5883d083f849SBarry Smith   Collective on dm
5884e5e52638SMatthew G. Knepley 
5885e5e52638SMatthew G. Knepley   Input Parameter:
5886e5e52638SMatthew G. Knepley . dm - The DM
5887e5e52638SMatthew G. Knepley 
5888e5e52638SMatthew G. Knepley   Output Parameter:
5889e5e52638SMatthew G. Knepley . newdm - The DM
5890e5e52638SMatthew G. Knepley 
5891e5e52638SMatthew G. Knepley   Level: advanced
5892e5e52638SMatthew G. Knepley 
5893e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5894e5e52638SMatthew G. Knepley @*/
5895e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5896e5e52638SMatthew G. Knepley {
5897e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5898e5e52638SMatthew G. Knepley 
5899e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5900e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5901e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5902e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5903e5e52638SMatthew G. Knepley }
5904e5e52638SMatthew G. Knepley 
5905b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5906b64e0483SPeter Brune {
5907b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5908b64e0483SPeter Brune   PetscErrorCode ierr;
5909b64e0483SPeter Brune   Vec coords,ccoords;
59106dbf9973SLawrence Mitchell   Mat inject;
5911b64e0483SPeter Brune   PetscFunctionBegin;
5912b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5913b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5914b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5915b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5916b64e0483SPeter Brune   if (coords && !ccoords) {
5917b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
59186668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
59196dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
59202adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
59216dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5922b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5923b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5924b64e0483SPeter Brune   }
5925b64e0483SPeter Brune   PetscFunctionReturn(0);
5926b64e0483SPeter Brune }
5927b64e0483SPeter Brune 
592803dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
592903dadc2fSPeter Brune {
593003dadc2fSPeter Brune   DM dm_coord,subdm_coord;
593103dadc2fSPeter Brune   PetscErrorCode ierr;
593203dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
593303dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
593403dadc2fSPeter Brune   PetscFunctionBegin;
593503dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
593603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
593703dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
593803dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
593903dadc2fSPeter Brune   if (coords && !ccoords) {
594003dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
59416668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
594203dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
594324640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
594403dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
594503dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
594603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
59471ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
594803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
594903dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
595003dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
595103dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
595203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
595303dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
595403dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
595503dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
595603dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
595703dadc2fSPeter Brune   }
595803dadc2fSPeter Brune   PetscFunctionReturn(0);
595903dadc2fSPeter Brune }
596003dadc2fSPeter Brune 
5961c73cfb54SMatthew G. Knepley /*@
5962c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5963c73cfb54SMatthew G. Knepley 
5964c73cfb54SMatthew G. Knepley   Not collective
5965c73cfb54SMatthew G. Knepley 
5966c73cfb54SMatthew G. Knepley   Input Parameter:
5967c73cfb54SMatthew G. Knepley . dm - The DM
5968c73cfb54SMatthew G. Knepley 
5969c73cfb54SMatthew G. Knepley   Output Parameter:
5970c73cfb54SMatthew G. Knepley . dim - The topological dimension
5971c73cfb54SMatthew G. Knepley 
5972c73cfb54SMatthew G. Knepley   Level: beginner
5973c73cfb54SMatthew G. Knepley 
5974c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5975c73cfb54SMatthew G. Knepley @*/
5976c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5977c73cfb54SMatthew G. Knepley {
5978c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5979c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5980534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5981c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5982c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5983c73cfb54SMatthew G. Knepley }
5984c73cfb54SMatthew G. Knepley 
5985c73cfb54SMatthew G. Knepley /*@
5986c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5987c73cfb54SMatthew G. Knepley 
5988c73cfb54SMatthew G. Knepley   Collective on dm
5989c73cfb54SMatthew G. Knepley 
5990c73cfb54SMatthew G. Knepley   Input Parameters:
5991c73cfb54SMatthew G. Knepley + dm - The DM
5992c73cfb54SMatthew G. Knepley - dim - The topological dimension
5993c73cfb54SMatthew G. Knepley 
5994c73cfb54SMatthew G. Knepley   Level: beginner
5995c73cfb54SMatthew G. Knepley 
5996c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5997c73cfb54SMatthew G. Knepley @*/
5998c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5999c73cfb54SMatthew G. Knepley {
6000e5e52638SMatthew G. Knepley   PetscDS        ds;
600145480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6002f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6003f17e8794SMatthew G. Knepley 
6004c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6005c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6006c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6007c73cfb54SMatthew G. Knepley   dm->dim = dim;
600845480ffeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
600945480ffeSMatthew G. Knepley   for (n = 0; n < Nds; ++n) {
601045480ffeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
601145480ffeSMatthew G. Knepley     if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);}
601245480ffeSMatthew G. Knepley   }
6013c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6014c73cfb54SMatthew G. Knepley }
6015c73cfb54SMatthew G. Knepley 
6016793f3fe5SMatthew G. Knepley /*@
6017793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6018793f3fe5SMatthew G. Knepley 
6019d083f849SBarry Smith   Collective on dm
6020793f3fe5SMatthew G. Knepley 
6021793f3fe5SMatthew G. Knepley   Input Parameters:
6022793f3fe5SMatthew G. Knepley + dm - the DM
6023793f3fe5SMatthew G. Knepley - dim - the dimension
6024793f3fe5SMatthew G. Knepley 
6025793f3fe5SMatthew G. Knepley   Output Parameters:
6026793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6027aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6028793f3fe5SMatthew G. Knepley 
6029793f3fe5SMatthew G. Knepley   Note:
6030793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6031a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6032793f3fe5SMatthew G. Knepley   then the interval is empty.
6033793f3fe5SMatthew G. Knepley 
6034793f3fe5SMatthew G. Knepley   Level: intermediate
6035793f3fe5SMatthew G. Knepley 
6036793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
6037793f3fe5SMatthew G. Knepley @*/
6038793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6039793f3fe5SMatthew G. Knepley {
6040793f3fe5SMatthew G. Knepley   PetscInt       d;
6041793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
6042793f3fe5SMatthew G. Knepley 
6043793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6044793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6045793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
6046793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
6047b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
6048793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
6049793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6050793f3fe5SMatthew G. Knepley }
6051793f3fe5SMatthew G. Knepley 
60526636e97aSMatthew G Knepley /*@
60536636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
60546636e97aSMatthew G Knepley 
6055d083f849SBarry Smith   Collective on dm
60566636e97aSMatthew G Knepley 
60576636e97aSMatthew G Knepley   Input Parameters:
60586636e97aSMatthew G Knepley + dm - the DM
60596636e97aSMatthew G Knepley - c - coordinate vector
60606636e97aSMatthew G Knepley 
60612dd40e9bSPatrick Sanan   Notes:
60622dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
60632dd40e9bSPatrick Sanan 
60642dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
60656636e97aSMatthew G Knepley 
60666636e97aSMatthew G Knepley   Level: intermediate
60676636e97aSMatthew G Knepley 
606860c22052SBarry Smith .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
60696636e97aSMatthew G Knepley @*/
60706636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
60716636e97aSMatthew G Knepley {
60726636e97aSMatthew G Knepley   PetscErrorCode ierr;
60736636e97aSMatthew G Knepley 
60746636e97aSMatthew G Knepley   PetscFunctionBegin;
60756636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60766636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
60776636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
60786636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
60796636e97aSMatthew G Knepley   dm->coordinates = c;
60806636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
6081b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
608203dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
60836636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60846636e97aSMatthew G Knepley }
60856636e97aSMatthew G Knepley 
60866636e97aSMatthew G Knepley /*@
60876636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
60886636e97aSMatthew G Knepley 
60897058e716SVaclav Hapla   Not collective
60906636e97aSMatthew G Knepley 
60916636e97aSMatthew G Knepley    Input Parameters:
60926636e97aSMatthew G Knepley +  dm - the DM
60936636e97aSMatthew G Knepley -  c - coordinate vector
60946636e97aSMatthew G Knepley 
60952dd40e9bSPatrick Sanan   Notes:
60966636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
60976636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
60986636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
60996636e97aSMatthew G Knepley 
61002dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
61012dd40e9bSPatrick Sanan 
61026636e97aSMatthew G Knepley   Level: intermediate
61036636e97aSMatthew G Knepley 
61046636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
61056636e97aSMatthew G Knepley @*/
61066636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
61076636e97aSMatthew G Knepley {
61086636e97aSMatthew G Knepley   PetscErrorCode ierr;
61096636e97aSMatthew G Knepley 
61106636e97aSMatthew G Knepley   PetscFunctionBegin;
61116636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61126636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
61136636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
61146636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
61158865f1eaSKarl Rupp 
61166636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
61178865f1eaSKarl Rupp 
61186636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
61196636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61206636e97aSMatthew G Knepley }
61216636e97aSMatthew G Knepley 
61226636e97aSMatthew G Knepley /*@
61236636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
61246636e97aSMatthew G Knepley 
6125d083f849SBarry Smith   Collective on dm
61266636e97aSMatthew G Knepley 
61276636e97aSMatthew G Knepley   Input Parameter:
61286636e97aSMatthew G Knepley . dm - the DM
61296636e97aSMatthew G Knepley 
61306636e97aSMatthew G Knepley   Output Parameter:
61316636e97aSMatthew G Knepley . c - global coordinate vector
61326636e97aSMatthew G Knepley 
61336636e97aSMatthew G Knepley   Note:
613460c22052SBarry Smith   This is a borrowed reference, so the user should NOT destroy this vector. When the DM is
613560c22052SBarry Smith   destroyed the array will no longer be valid.
61366636e97aSMatthew G Knepley 
6137959c7569SPatrick Sanan   Each process has only the locally-owned portion of the global coordinates (does NOT have the ghost coordinates).
61386636e97aSMatthew G Knepley 
61396636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
61406636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
61416636e97aSMatthew G Knepley 
61426636e97aSMatthew G Knepley   Level: intermediate
61436636e97aSMatthew G Knepley 
614460c22052SBarry Smith .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
61456636e97aSMatthew G Knepley @*/
61466636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
61476636e97aSMatthew G Knepley {
61486636e97aSMatthew G Knepley   PetscErrorCode ierr;
61496636e97aSMatthew G Knepley 
61506636e97aSMatthew G Knepley   PetscFunctionBegin;
61516636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61526636e97aSMatthew G Knepley   PetscValidPointer(c,2);
61531f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
61540298fd71SBarry Smith     DM        cdm = NULL;
61551970a576SMatthew G. Knepley     PetscBool localized;
61566636e97aSMatthew G Knepley 
61576636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
61586636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
61591970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
61601970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
61611970a576SMatthew G. Knepley     if (localized) {
61621970a576SMatthew G. Knepley       PetscInt cdim;
61631970a576SMatthew G. Knepley 
61641970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
61651970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
61661970a576SMatthew G. Knepley     }
61676636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
61686636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
61696636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
61706636e97aSMatthew G Knepley   }
61716636e97aSMatthew G Knepley   *c = dm->coordinates;
61726636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61736636e97aSMatthew G Knepley }
61746636e97aSMatthew G Knepley 
61756636e97aSMatthew G Knepley /*@
617681e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
617781e9a530SVaclav Hapla 
6178d083f849SBarry Smith   Collective on dm
617981e9a530SVaclav Hapla 
618081e9a530SVaclav Hapla   Input Parameter:
618181e9a530SVaclav Hapla . dm - the DM
618281e9a530SVaclav Hapla 
618381e9a530SVaclav Hapla   Level: advanced
618481e9a530SVaclav Hapla 
618581e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
618681e9a530SVaclav Hapla @*/
618781e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
618881e9a530SVaclav Hapla {
618981e9a530SVaclav Hapla   PetscErrorCode ierr;
619081e9a530SVaclav Hapla 
619181e9a530SVaclav Hapla   PetscFunctionBegin;
619281e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
619381e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
61941970a576SMatthew G. Knepley     DM        cdm = NULL;
61951970a576SMatthew G. Knepley     PetscBool localized;
61961970a576SMatthew G. Knepley 
619781e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
619881e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
61991970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
62001970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
62011970a576SMatthew G. Knepley     if (localized) {
62021970a576SMatthew G. Knepley       PetscInt cdim;
62031970a576SMatthew G. Knepley 
62041970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
62051970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
62061970a576SMatthew G. Knepley     }
620781e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
620881e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
620981e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
621081e9a530SVaclav Hapla   }
621181e9a530SVaclav Hapla   PetscFunctionReturn(0);
621281e9a530SVaclav Hapla }
621381e9a530SVaclav Hapla 
621481e9a530SVaclav Hapla /*@
62156636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
62166636e97aSMatthew G Knepley 
6217d083f849SBarry Smith   Collective on dm
62186636e97aSMatthew G Knepley 
62196636e97aSMatthew G Knepley   Input Parameter:
62206636e97aSMatthew G Knepley . dm - the DM
62216636e97aSMatthew G Knepley 
62226636e97aSMatthew G Knepley   Output Parameter:
62236636e97aSMatthew G Knepley . c - coordinate vector
62246636e97aSMatthew G Knepley 
62256636e97aSMatthew G Knepley   Note:
62266636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
62276636e97aSMatthew G Knepley 
62286636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
62296636e97aSMatthew G Knepley 
62306636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
62316636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
62326636e97aSMatthew G Knepley 
62336636e97aSMatthew G Knepley   Level: intermediate
62346636e97aSMatthew G Knepley 
623581e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
62366636e97aSMatthew G Knepley @*/
62376636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
62386636e97aSMatthew G Knepley {
62396636e97aSMatthew G Knepley   PetscErrorCode ierr;
62406636e97aSMatthew G Knepley 
62416636e97aSMatthew G Knepley   PetscFunctionBegin;
62426636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62436636e97aSMatthew G Knepley   PetscValidPointer(c,2);
624481e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
62456636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
62466636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62476636e97aSMatthew G Knepley }
62486636e97aSMatthew G Knepley 
624981e9a530SVaclav Hapla /*@
625081e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
625181e9a530SVaclav Hapla 
625281e9a530SVaclav Hapla   Not collective
625381e9a530SVaclav Hapla 
625481e9a530SVaclav Hapla   Input Parameter:
625581e9a530SVaclav Hapla . dm - the DM
625681e9a530SVaclav Hapla 
625781e9a530SVaclav Hapla   Output Parameter:
625881e9a530SVaclav Hapla . c - coordinate vector
625981e9a530SVaclav Hapla 
626081e9a530SVaclav Hapla   Level: advanced
626181e9a530SVaclav Hapla 
626281e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
626381e9a530SVaclav Hapla @*/
626481e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
626581e9a530SVaclav Hapla {
626681e9a530SVaclav Hapla   PetscFunctionBegin;
626781e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
626881e9a530SVaclav Hapla   PetscValidPointer(c,2);
626981e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
62706636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
62716636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62726636e97aSMatthew G Knepley }
62736636e97aSMatthew G Knepley 
62742db98f8dSVaclav Hapla /*@
62752db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
62762db98f8dSVaclav Hapla 
62772db98f8dSVaclav Hapla   Not collective
62782db98f8dSVaclav Hapla 
62792db98f8dSVaclav Hapla   Input Parameter:
62802db98f8dSVaclav Hapla + dm - the DM
62812db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
62822db98f8dSVaclav Hapla 
62832db98f8dSVaclav Hapla   Output Parameter:
62842db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
62852db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
62862db98f8dSVaclav Hapla 
62872db98f8dSVaclav Hapla   Note:
62882db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
62892db98f8dSVaclav Hapla 
62902db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
62912db98f8dSVaclav Hapla 
62922db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
62932db98f8dSVaclav Hapla 
62942db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
62952db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
62962db98f8dSVaclav Hapla 
62972db98f8dSVaclav Hapla   Level: advanced
62982db98f8dSVaclav Hapla 
62992db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
63002db98f8dSVaclav Hapla @*/
63012db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
63022db98f8dSVaclav Hapla {
63032db98f8dSVaclav Hapla   PetscSection        cs, newcs;
63042db98f8dSVaclav Hapla   Vec                 coords;
63052db98f8dSVaclav Hapla   const PetscScalar   *arr;
63062db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
63072db98f8dSVaclav Hapla   PetscInt            n;
63082db98f8dSVaclav Hapla   PetscErrorCode      ierr;
63092db98f8dSVaclav Hapla 
63102db98f8dSVaclav Hapla   PetscFunctionBegin;
63112db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63122db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
63132db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
63142db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
63152db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
63161bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
63171bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
63182db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
63192db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
63202db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
63212db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
63222db98f8dSVaclav Hapla   if (pCoord) {
63232db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
63242db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
63252db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
63262db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6327ad9ac99dSVaclav Hapla   } else {
6328ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
63292db98f8dSVaclav Hapla   }
6330ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6331ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
63322db98f8dSVaclav Hapla   PetscFunctionReturn(0);
63332db98f8dSVaclav Hapla }
63342db98f8dSVaclav Hapla 
6335f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6336f19dbd58SToby Isaac {
6337f19dbd58SToby Isaac   PetscErrorCode ierr;
6338f19dbd58SToby Isaac 
6339f19dbd58SToby Isaac   PetscFunctionBegin;
6340f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6341f19dbd58SToby Isaac   PetscValidPointer(field,2);
6342f19dbd58SToby Isaac   if (!dm->coordinateField) {
6343f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6344f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6345f19dbd58SToby Isaac     }
6346f19dbd58SToby Isaac   }
6347f19dbd58SToby Isaac   *field = dm->coordinateField;
6348f19dbd58SToby Isaac   PetscFunctionReturn(0);
6349f19dbd58SToby Isaac }
6350f19dbd58SToby Isaac 
6351f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6352f19dbd58SToby Isaac {
6353f19dbd58SToby Isaac   PetscErrorCode ierr;
6354f19dbd58SToby Isaac 
6355f19dbd58SToby Isaac   PetscFunctionBegin;
6356f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6357f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6358c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6359f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6360f19dbd58SToby Isaac   dm->coordinateField = field;
6361f19dbd58SToby Isaac   PetscFunctionReturn(0);
6362f19dbd58SToby Isaac }
6363f19dbd58SToby Isaac 
63646636e97aSMatthew G Knepley /*@
63651cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
63666636e97aSMatthew G Knepley 
6367d083f849SBarry Smith   Collective on dm
63686636e97aSMatthew G Knepley 
63696636e97aSMatthew G Knepley   Input Parameter:
63706636e97aSMatthew G Knepley . dm - the DM
63716636e97aSMatthew G Knepley 
63726636e97aSMatthew G Knepley   Output Parameter:
63736636e97aSMatthew G Knepley . cdm - coordinate DM
63746636e97aSMatthew G Knepley 
63756636e97aSMatthew G Knepley   Level: intermediate
63766636e97aSMatthew G Knepley 
63771cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
63786636e97aSMatthew G Knepley @*/
63796636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
63806636e97aSMatthew G Knepley {
63816636e97aSMatthew G Knepley   PetscErrorCode ierr;
63826636e97aSMatthew G Knepley 
63836636e97aSMatthew G Knepley   PetscFunctionBegin;
63846636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63856636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
63866636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6387308f8a94SToby Isaac     DM cdm;
6388308f8a94SToby Isaac 
638982f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6390308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6391308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6392308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6393308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6394308f8a94SToby Isaac     dm->coordinateDM = cdm;
63956636e97aSMatthew G Knepley   }
63966636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
63976636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63986636e97aSMatthew G Knepley }
6399e87bb0d3SMatthew G Knepley 
64001cfe2091SMatthew G. Knepley /*@
64011cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
64021cfe2091SMatthew G. Knepley 
6403d083f849SBarry Smith   Logically Collective on dm
64041cfe2091SMatthew G. Knepley 
64051cfe2091SMatthew G. Knepley   Input Parameters:
64061cfe2091SMatthew G. Knepley + dm - the DM
64071cfe2091SMatthew G. Knepley - cdm - coordinate DM
64081cfe2091SMatthew G. Knepley 
64091cfe2091SMatthew G. Knepley   Level: intermediate
64101cfe2091SMatthew G. Knepley 
64111cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
64121cfe2091SMatthew G. Knepley @*/
64131cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
64141cfe2091SMatthew G. Knepley {
64151cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
64161cfe2091SMatthew G. Knepley 
64171cfe2091SMatthew G. Knepley   PetscFunctionBegin;
64181cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64191cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6420f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
64211cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
64221cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
64231cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
64241cfe2091SMatthew G. Knepley }
64251cfe2091SMatthew G. Knepley 
642646e270d4SMatthew G. Knepley /*@
642746e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
642846e270d4SMatthew G. Knepley 
642946e270d4SMatthew G. Knepley   Not Collective
643046e270d4SMatthew G. Knepley 
643146e270d4SMatthew G. Knepley   Input Parameter:
643246e270d4SMatthew G. Knepley . dm - The DM object
643346e270d4SMatthew G. Knepley 
643446e270d4SMatthew G. Knepley   Output Parameter:
643546e270d4SMatthew G. Knepley . dim - The embedding dimension
643646e270d4SMatthew G. Knepley 
643746e270d4SMatthew G. Knepley   Level: intermediate
643846e270d4SMatthew G. Knepley 
643992fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
644046e270d4SMatthew G. Knepley @*/
644146e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
644246e270d4SMatthew G. Knepley {
644346e270d4SMatthew G. Knepley   PetscFunctionBegin;
644446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6445534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
64469a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
64479a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
64489a9a41abSToby Isaac   }
644946e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
645046e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
645146e270d4SMatthew G. Knepley }
645246e270d4SMatthew G. Knepley 
645346e270d4SMatthew G. Knepley /*@
645446e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
645546e270d4SMatthew G. Knepley 
645646e270d4SMatthew G. Knepley   Not Collective
645746e270d4SMatthew G. Knepley 
645846e270d4SMatthew G. Knepley   Input Parameters:
645946e270d4SMatthew G. Knepley + dm  - The DM object
646046e270d4SMatthew G. Knepley - dim - The embedding dimension
646146e270d4SMatthew G. Knepley 
646246e270d4SMatthew G. Knepley   Level: intermediate
646346e270d4SMatthew G. Knepley 
646492fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
646546e270d4SMatthew G. Knepley @*/
646646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
646746e270d4SMatthew G. Knepley {
6468e5e52638SMatthew G. Knepley   PetscDS        ds;
646945480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6470f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6471f17e8794SMatthew G. Knepley 
647246e270d4SMatthew G. Knepley   PetscFunctionBegin;
647346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
647446e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
647545480ffeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
647645480ffeSMatthew G. Knepley   for (n = 0; n < Nds; ++n) {
647745480ffeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
6478e5e52638SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
647945480ffeSMatthew G. Knepley   }
648046e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
648146e270d4SMatthew G. Knepley }
648246e270d4SMatthew G. Knepley 
6483e8abe2deSMatthew G. Knepley /*@
6484e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6485e8abe2deSMatthew G. Knepley 
6486d083f849SBarry Smith   Collective on dm
6487e8abe2deSMatthew G. Knepley 
6488e8abe2deSMatthew G. Knepley   Input Parameter:
6489e8abe2deSMatthew G. Knepley . dm - The DM object
6490e8abe2deSMatthew G. Knepley 
6491e8abe2deSMatthew G. Knepley   Output Parameter:
6492e8abe2deSMatthew G. Knepley . section - The PetscSection object
6493e8abe2deSMatthew G. Knepley 
6494e8abe2deSMatthew G. Knepley   Level: intermediate
6495e8abe2deSMatthew G. Knepley 
649692fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6497e8abe2deSMatthew G. Knepley @*/
6498e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6499e8abe2deSMatthew G. Knepley {
6500e8abe2deSMatthew G. Knepley   DM             cdm;
6501e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6502e8abe2deSMatthew G. Knepley 
6503e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6504e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6505e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6506e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
650792fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6508e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6509e8abe2deSMatthew G. Knepley }
6510e8abe2deSMatthew G. Knepley 
6511e8abe2deSMatthew G. Knepley /*@
6512e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6513e8abe2deSMatthew G. Knepley 
6514e8abe2deSMatthew G. Knepley   Not Collective
6515e8abe2deSMatthew G. Knepley 
6516e8abe2deSMatthew G. Knepley   Input Parameters:
6517e8abe2deSMatthew G. Knepley + dm      - The DM object
651846e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6519e8abe2deSMatthew G. Knepley - section - The PetscSection object
6520e8abe2deSMatthew G. Knepley 
6521e8abe2deSMatthew G. Knepley   Level: intermediate
6522e8abe2deSMatthew G. Knepley 
652392fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6524e8abe2deSMatthew G. Knepley @*/
652546e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6526e8abe2deSMatthew G. Knepley {
6527e8abe2deSMatthew G. Knepley   DM             cdm;
6528e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6529e8abe2deSMatthew G. Knepley 
6530e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6531e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
653246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6533e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
653492fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
653546e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
65364c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
653746e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
653846e270d4SMatthew G. Knepley 
653946e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
654046e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
654146e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
654246e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
654346e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
654446e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
654546e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
654646e270d4SMatthew G. Knepley     }
6547ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
654846e270d4SMatthew G. Knepley   }
6549e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6550e8abe2deSMatthew G. Knepley }
6551e8abe2deSMatthew G. Knepley 
6552d864a3eaSLisandro Dalcin /*@
6553d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6554d864a3eaSLisandro Dalcin 
6555d864a3eaSLisandro Dalcin   Input Parameters:
6556d864a3eaSLisandro Dalcin + dm      - The DM object
6557d864a3eaSLisandro Dalcin - disc    - The new coordinate discretization
6558d864a3eaSLisandro Dalcin 
6559d864a3eaSLisandro Dalcin   Level: intermediate
6560d864a3eaSLisandro Dalcin 
6561d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6562d864a3eaSLisandro Dalcin @*/
6563d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6564d864a3eaSLisandro Dalcin {
6565d864a3eaSLisandro Dalcin   PetscObject    discOld;
6566d864a3eaSLisandro Dalcin   PetscClassId   classid;
6567d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6568d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6569d864a3eaSLisandro Dalcin   Mat            matInterp;
6570d864a3eaSLisandro Dalcin   PetscErrorCode ierr;
6571d864a3eaSLisandro Dalcin 
6572d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6573d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6574d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6575d864a3eaSLisandro Dalcin 
6576d864a3eaSLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdmOld);CHKERRQ(ierr);
6577d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
6578d864a3eaSLisandro Dalcin   ierr = DMGetField(cdmOld, 0, NULL, &discOld);CHKERRQ(ierr);
6579d864a3eaSLisandro Dalcin   ierr = PetscObjectGetClassId(discOld, &classid);CHKERRQ(ierr);
658029ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
658129ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
658229ad44c5SMatthew G. Knepley       PetscFE        feLinear;
658329ad44c5SMatthew G. Knepley       DMPolytopeType ct;
658429ad44c5SMatthew G. Knepley       PetscInt       dim, dE, cStart;
658529ad44c5SMatthew G. Knepley       PetscBool      simplex;
658629ad44c5SMatthew G. Knepley 
658729ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
658829ad44c5SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
658929ad44c5SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
659029ad44c5SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdmOld, 0, &cStart, NULL);CHKERRQ(ierr);
659129ad44c5SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
659229ad44c5SMatthew G. Knepley       switch (ct) {
659329ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM:
659429ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
659529ad44c5SMatthew G. Knepley           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
659629ad44c5SMatthew G. Knepley         default: break;
659729ad44c5SMatthew G. Knepley       }
659829ad44c5SMatthew G. Knepley       simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
659929ad44c5SMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear);CHKERRQ(ierr);
660029ad44c5SMatthew G. Knepley       ierr = DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear);CHKERRQ(ierr);
660129ad44c5SMatthew G. Knepley       ierr = PetscFEDestroy(&feLinear);CHKERRQ(ierr);
660229ad44c5SMatthew G. Knepley       ierr = DMCreateDS(cdmOld);CHKERRQ(ierr);
660329ad44c5SMatthew G. Knepley     } else {
660429ad44c5SMatthew G. Knepley       const char *discname;
660529ad44c5SMatthew G. Knepley 
660629ad44c5SMatthew G. Knepley       ierr = PetscObjectGetType(discOld, &discname);CHKERRQ(ierr);
660729ad44c5SMatthew G. Knepley       SETERRQ1(PetscObjectComm(discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
660829ad44c5SMatthew G. Knepley     }
660929ad44c5SMatthew G. Knepley   }
6610d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6611d864a3eaSLisandro Dalcin   ierr = DMClone(cdmOld, &cdmNew);CHKERRQ(ierr);
6612d864a3eaSLisandro Dalcin   ierr = DMSetField(cdmNew, 0, NULL, (PetscObject) disc);CHKERRQ(ierr);
6613d864a3eaSLisandro Dalcin   ierr = DMCreateDS(cdmNew);CHKERRQ(ierr);
6614d864a3eaSLisandro Dalcin   /* Project the coordinate vector from old to new space  */
6615d864a3eaSLisandro Dalcin   ierr = DMGetCoordinates(dm, &coordsOld);CHKERRQ(ierr);
6616d864a3eaSLisandro Dalcin   ierr = DMCreateGlobalVector(cdmNew, &coordsNew);CHKERRQ(ierr);
6617d864a3eaSLisandro Dalcin   ierr = DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL);CHKERRQ(ierr);
6618d864a3eaSLisandro Dalcin   ierr = MatInterpolate(matInterp, coordsOld, coordsNew);CHKERRQ(ierr);
6619d864a3eaSLisandro Dalcin   ierr = MatDestroy(&matInterp);CHKERRQ(ierr);
6620d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6621d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateField(dm, NULL);CHKERRQ(ierr);
6622d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateDM(dm, cdmNew);CHKERRQ(ierr);
6623d864a3eaSLisandro Dalcin   ierr = DMSetCoordinates(dm, coordsNew);CHKERRQ(ierr);
6624d864a3eaSLisandro Dalcin   ierr = VecDestroy(&coordsNew);CHKERRQ(ierr);
6625d864a3eaSLisandro Dalcin   ierr = DMDestroy(&cdmNew);CHKERRQ(ierr);
6626d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6627d864a3eaSLisandro Dalcin }
6628d864a3eaSLisandro Dalcin 
66295dc8c3f7SMatthew G. Knepley /*@C
663090b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
66315dc8c3f7SMatthew G. Knepley 
66325dc8c3f7SMatthew G. Knepley   Input Parameters:
663390b157c4SStefano Zampini . dm      - The DM object
663490b157c4SStefano Zampini 
663590b157c4SStefano Zampini   Output Parameters:
663690b157c4SStefano Zampini + per     - Whether the DM is periodic or not
66375dc8c3f7SMatthew 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
66385dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
66395dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
66405dc8c3f7SMatthew G. Knepley 
66415dc8c3f7SMatthew G. Knepley   Level: developer
66425dc8c3f7SMatthew G. Knepley 
66435dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
66445dc8c3f7SMatthew G. Knepley @*/
664590b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6646c6b900c6SMatthew G. Knepley {
6647c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6648c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
664990b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6650c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6651c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
66525dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6653c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6654c6b900c6SMatthew G. Knepley }
6655c6b900c6SMatthew G. Knepley 
66565dc8c3f7SMatthew G. Knepley /*@C
66575dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
66585dc8c3f7SMatthew G. Knepley 
66595dc8c3f7SMatthew G. Knepley   Input Parameters:
66605dc8c3f7SMatthew G. Knepley + dm      - The DM object
6661db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6662db2bf62eSStefano Zampini . maxCell - Over distances greater than this, we can assume a point has crossed over to another sheet, when trying to localize cell coordinates. Pass NULL to remove such information.
66635dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
66645dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
66655dc8c3f7SMatthew G. Knepley 
6666db2bf62eSStefano Zampini   Notes: If per is PETSC_TRUE and maxCell is not provided, coordinates need to be already localized, or must be localized by hand by the user.
6667db2bf62eSStefano Zampini 
66685dc8c3f7SMatthew G. Knepley   Level: developer
66695dc8c3f7SMatthew G. Knepley 
66705dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
66715dc8c3f7SMatthew G. Knepley @*/
667290b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6673c6b900c6SMatthew G. Knepley {
6674c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6675c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6676c6b900c6SMatthew G. Knepley 
6677c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6678c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
667990b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6680412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6681412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6682412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
66835dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
668490b157c4SStefano Zampini   if (maxCell) {
6685412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6686412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6687db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6688db2bf62eSStefano Zampini     ierr = PetscFree(dm->maxCell);CHKERRQ(ierr);
6689412e9a14SMatthew G. Knepley   }
6690db2bf62eSStefano Zampini 
6691412e9a14SMatthew G. Knepley   if (L) {
6692412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6693412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6694412e9a14SMatthew G. Knepley   }
6695412e9a14SMatthew G. Knepley   if (bd) {
6696412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6697412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
669890b157c4SStefano Zampini   }
6699072d7d67SStefano Zampini   dm->periodic = per;
6700c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6701c6b900c6SMatthew G. Knepley }
6702c6b900c6SMatthew G. Knepley 
67032e17dfb7SMatthew G. Knepley /*@
67042e17dfb7SMatthew 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.
67052e17dfb7SMatthew G. Knepley 
67062e17dfb7SMatthew G. Knepley   Input Parameters:
67072e17dfb7SMatthew G. Knepley + dm     - The DM
670865da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
670965da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
67102e17dfb7SMatthew G. Knepley 
67112e17dfb7SMatthew G. Knepley   Output Parameter:
67122e17dfb7SMatthew G. Knepley . out - The localized coordinate point
67132e17dfb7SMatthew G. Knepley 
67142e17dfb7SMatthew G. Knepley   Level: developer
67152e17dfb7SMatthew G. Knepley 
67162e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
67172e17dfb7SMatthew G. Knepley @*/
671865da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
67192e17dfb7SMatthew G. Knepley {
67202e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
67212e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
67222e17dfb7SMatthew G. Knepley 
67232e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
67242e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
67252e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
67262e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
67272e17dfb7SMatthew G. Knepley   } else {
672865da65dcSMatthew G. Knepley     if (endpoint) {
672965da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6730da3333bfSMatthew 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)) {
6731da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
673265da65dcSMatthew G. Knepley         } else {
6733da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
673465da65dcSMatthew G. Knepley         }
673565da65dcSMatthew G. Knepley       }
673665da65dcSMatthew G. Knepley     } else {
67372e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
67381118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
67392e17dfb7SMatthew G. Knepley       }
67402e17dfb7SMatthew G. Knepley     }
674165da65dcSMatthew G. Knepley   }
67422e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
67432e17dfb7SMatthew G. Knepley }
67442e17dfb7SMatthew G. Knepley 
67452e17dfb7SMatthew G. Knepley /*
67462e17dfb7SMatthew 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.
67472e17dfb7SMatthew G. Knepley 
67482e17dfb7SMatthew G. Knepley   Input Parameters:
67492e17dfb7SMatthew G. Knepley + dm     - The DM
67502e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
67512e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
67522e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
67532e17dfb7SMatthew G. Knepley 
67542e17dfb7SMatthew G. Knepley   Output Parameter:
67552e17dfb7SMatthew G. Knepley . out - The localized coordinate point
67562e17dfb7SMatthew G. Knepley 
67572e17dfb7SMatthew G. Knepley   Level: developer
67582e17dfb7SMatthew G. Knepley 
67592e17dfb7SMatthew 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
67602e17dfb7SMatthew G. Knepley 
67612e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
67622e17dfb7SMatthew G. Knepley */
67632e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
67642e17dfb7SMatthew G. Knepley {
67652e17dfb7SMatthew G. Knepley   PetscInt d;
67662e17dfb7SMatthew G. Knepley 
67672e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
67682e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
67692e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
67702e17dfb7SMatthew G. Knepley   } else {
67712e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6772908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
67732e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
67742e17dfb7SMatthew G. Knepley       } else {
67752e17dfb7SMatthew G. Knepley         out[d] = in[d];
67762e17dfb7SMatthew G. Knepley       }
67772e17dfb7SMatthew G. Knepley     }
67782e17dfb7SMatthew G. Knepley   }
67792e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
67802e17dfb7SMatthew G. Knepley }
6781a5801f52SStefano Zampini 
67822e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
67832e17dfb7SMatthew G. Knepley {
67842e17dfb7SMatthew G. Knepley   PetscInt d;
67852e17dfb7SMatthew G. Knepley 
67862e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
67872e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
67882e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
67892e17dfb7SMatthew G. Knepley   } else {
67902e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6791908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
67922e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
67932e17dfb7SMatthew G. Knepley       } else {
67942e17dfb7SMatthew G. Knepley         out[d] = in[d];
67952e17dfb7SMatthew G. Knepley       }
67962e17dfb7SMatthew G. Knepley     }
67972e17dfb7SMatthew G. Knepley   }
67982e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
67992e17dfb7SMatthew G. Knepley }
68002e17dfb7SMatthew G. Knepley 
68012e17dfb7SMatthew G. Knepley /*
68022e17dfb7SMatthew 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.
68032e17dfb7SMatthew G. Knepley 
68042e17dfb7SMatthew G. Knepley   Input Parameters:
68052e17dfb7SMatthew G. Knepley + dm     - The DM
68062e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
68072e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
68082e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
68092e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
68102e17dfb7SMatthew G. Knepley 
68112e17dfb7SMatthew G. Knepley   Output Parameter:
68122e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
68132e17dfb7SMatthew G. Knepley 
68142e17dfb7SMatthew G. Knepley   Level: developer
68152e17dfb7SMatthew G. Knepley 
68162e17dfb7SMatthew 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
68172e17dfb7SMatthew G. Knepley 
68182e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
68192e17dfb7SMatthew G. Knepley */
68202e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
68212e17dfb7SMatthew G. Knepley {
68222e17dfb7SMatthew G. Knepley   PetscInt d;
68232e17dfb7SMatthew G. Knepley 
68242e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68252e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68262e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
68272e17dfb7SMatthew G. Knepley   } else {
68282e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6829412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6830412e9a14SMatthew G. Knepley 
6831412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6832412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6833412e9a14SMatthew G. Knepley 
6834412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6835412e9a14SMatthew 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]));
6836412e9a14SMatthew G. Knepley         out[d] += newCoord;
68372e17dfb7SMatthew G. Knepley       } else {
68382e17dfb7SMatthew G. Knepley         out[d] += in[d];
68392e17dfb7SMatthew G. Knepley       }
68402e17dfb7SMatthew G. Knepley     }
68412e17dfb7SMatthew G. Knepley   }
68422e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68432e17dfb7SMatthew G. Knepley }
68442e17dfb7SMatthew G. Knepley 
684536447a5eSToby Isaac /*@
68468f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
68478f700142SStefano Zampini 
68488f700142SStefano Zampini   Not collective
684936447a5eSToby Isaac 
685036447a5eSToby Isaac   Input Parameter:
685136447a5eSToby Isaac . dm - The DM
685236447a5eSToby Isaac 
685336447a5eSToby Isaac   Output Parameter:
685436447a5eSToby Isaac   areLocalized - True if localized
685536447a5eSToby Isaac 
685636447a5eSToby Isaac   Level: developer
685736447a5eSToby Isaac 
68588f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
685936447a5eSToby Isaac @*/
68608f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
686136447a5eSToby Isaac {
686236447a5eSToby Isaac   DM             cdm;
686336447a5eSToby Isaac   PetscSection   coordSection;
686446a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
686546a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
686636447a5eSToby Isaac   PetscErrorCode ierr;
686736447a5eSToby Isaac 
686836447a5eSToby Isaac   PetscFunctionBegin;
686936447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6870534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
68718b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
687246a3a80fSLisandro Dalcin 
687336447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
687436447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
687546a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
68769f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
687746a3a80fSLisandro Dalcin 
68789f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
687946a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
688036447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
688136447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
688246a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
688346a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
688436447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
688546a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
688636447a5eSToby Isaac   }
68878f700142SStefano Zampini   *areLocalized = alreadyLocalized;
688836447a5eSToby Isaac   PetscFunctionReturn(0);
688936447a5eSToby Isaac }
689036447a5eSToby Isaac 
68918f700142SStefano Zampini /*@
68928f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
68938f700142SStefano Zampini 
68948f700142SStefano Zampini   Collective on dm
68958f700142SStefano Zampini 
68968f700142SStefano Zampini   Input Parameter:
68978f700142SStefano Zampini . dm - The DM
68988f700142SStefano Zampini 
68998f700142SStefano Zampini   Output Parameter:
69008f700142SStefano Zampini   areLocalized - True if localized
69018f700142SStefano Zampini 
69028f700142SStefano Zampini   Level: developer
69038f700142SStefano Zampini 
69048f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
69058f700142SStefano Zampini @*/
69068f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
69078f700142SStefano Zampini {
69088f700142SStefano Zampini   PetscBool      localized;
69098f700142SStefano Zampini   PetscErrorCode ierr;
69108f700142SStefano Zampini 
69118f700142SStefano Zampini   PetscFunctionBegin;
69128f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6913534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
69148f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
6915820f2d46SBarry Smith   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
69168f700142SStefano Zampini   PetscFunctionReturn(0);
69178f700142SStefano Zampini }
691836447a5eSToby Isaac 
69192e17dfb7SMatthew G. Knepley /*@
6920492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
69212e17dfb7SMatthew G. Knepley 
69228f700142SStefano Zampini   Collective on dm
69238f700142SStefano Zampini 
69242e17dfb7SMatthew G. Knepley   Input Parameter:
69252e17dfb7SMatthew G. Knepley . dm - The DM
69262e17dfb7SMatthew G. Knepley 
69272e17dfb7SMatthew G. Knepley   Level: developer
69282e17dfb7SMatthew G. Knepley 
69298f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
69302e17dfb7SMatthew G. Knepley @*/
69312e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
69322e17dfb7SMatthew G. Knepley {
69332e17dfb7SMatthew G. Knepley   DM             cdm;
69342e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
69352e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
69363e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
69373e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6938e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
69393e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
69403e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
69412e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
69422e17dfb7SMatthew G. Knepley 
69432e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69442e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
694592c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6946f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6947f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6948f7cbd40bSStefano Zampini 
69492e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
69502e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
69512e17dfb7SMatthew G. Knepley   {
69522e17dfb7SMatthew G. Knepley     PetscBool isplex;
69532e17dfb7SMatthew G. Knepley 
69542e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
69552e17dfb7SMatthew G. Knepley     if (isplex) {
69562e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
69573e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
695869291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
69593e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
69603e922f36SToby Isaac       newStart = vStart;
69613e922f36SToby Isaac       newEnd   = vEnd;
69623e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
69633e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
69643e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
69653e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
69663e922f36SToby Isaac       }
69672e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
69682e17dfb7SMatthew G. Knepley   }
69692e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
697043eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
69712e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
69723e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6973e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
69743e922f36SToby Isaac 
69752e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
69762e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
69772e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
69782e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
69793e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
69803e922f36SToby Isaac 
698169291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
69823e922f36SToby Isaac   localized = &anchor[bs];
69833e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
69843e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
69853e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
69863e922f36SToby Isaac 
69873e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
69883e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
69893e922f36SToby Isaac       PetscInt     b;
69903e922f36SToby Isaac 
69913e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
69923e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
69933e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
69943e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
69953e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
69963e922f36SToby Isaac         for (b = 0; b < bs; b++) {
69973e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
69983e922f36SToby Isaac         }
69993e922f36SToby Isaac         if (b < bs) break;
70003e922f36SToby Isaac       }
70013e922f36SToby Isaac       if (d < dof/bs) {
70023e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
70033e922f36SToby Isaac           PetscInt cdof;
70043e922f36SToby Isaac 
70053e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
70063e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
70073e922f36SToby Isaac         }
70083e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
70093e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
70103e922f36SToby Isaac       }
70113e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
70123e922f36SToby Isaac     }
70133e922f36SToby Isaac   }
7014ffc4695bSBarry Smith   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
70153e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
701669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
70173e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
701869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
70193e922f36SToby Isaac     PetscFunctionReturn(0);
70203e922f36SToby Isaac   }
70212e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
70222e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
70232e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
70242e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
70252e17dfb7SMatthew G. Knepley   }
70262e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
70272e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
7028c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
70292e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
70302e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
70312e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
70322e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
7033c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
70342e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
70352e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
70362e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
70372e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
70382e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
70392e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
70402e17dfb7SMatthew G. Knepley   }
70413e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
70423e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
70433e922f36SToby Isaac 
70442e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
70452e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
70463e922f36SToby Isaac       PetscInt     b, cdof;
70472e17dfb7SMatthew G. Knepley 
70483e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
70493e922f36SToby Isaac       if (!cdof) continue;
70502e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
70512e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
70522e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
70532e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
70542e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
70552e17dfb7SMatthew G. Knepley     }
70563e922f36SToby Isaac   }
705769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
705869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
7059c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
70602e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
70612e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
70622e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
70632e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
70642e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
70652e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
70662e17dfb7SMatthew G. Knepley }
70672e17dfb7SMatthew G. Knepley 
7068e87bb0d3SMatthew G Knepley /*@
70693a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
7070e87bb0d3SMatthew G Knepley 
7071d083f849SBarry Smith   Collective on v (see explanation below)
7072e87bb0d3SMatthew G Knepley 
7073e87bb0d3SMatthew G Knepley   Input Parameters:
7074e87bb0d3SMatthew G Knepley + dm - The DM
70753a93e3b7SToby Isaac . v - The Vec of points
707662a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
70773a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
7078e87bb0d3SMatthew G Knepley 
707961e3bb9bSMatthew G Knepley   Output Parameter:
708062a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
708162a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
70823a93e3b7SToby Isaac 
7083e87bb0d3SMatthew G Knepley 
7084e87bb0d3SMatthew G Knepley   Level: developer
708561e3bb9bSMatthew G Knepley 
708662a38674SMatthew G. Knepley   Notes:
70873a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
708862a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
70893a93e3b7SToby Isaac 
70903a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
709162a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
70923a93e3b7SToby Isaac 
70933a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
70943a93e3b7SToby Isaac 
709562a38674SMatthew G. Knepley $    const PetscSFNode *cells;
709662a38674SMatthew G. Knepley $    PetscInt           nFound;
7097a6216909SToby Isaac $    const PetscInt    *found;
709862a38674SMatthew G. Knepley $
7099a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
71003a93e3b7SToby Isaac 
71013a93e3b7SToby 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
71023a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
71033a93e3b7SToby Isaac 
710462a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
710561e3bb9bSMatthew G Knepley @*/
710662a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
7107e87bb0d3SMatthew G Knepley {
7108735aa83eSMatthew G Knepley   PetscErrorCode ierr;
7109735aa83eSMatthew G Knepley 
7110e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
7111e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7112e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
7113e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
71143a93e3b7SToby Isaac   if (*cellSF) {
71153a93e3b7SToby Isaac     PetscMPIInt result;
71163a93e3b7SToby Isaac 
7117e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
7118ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRMPI(ierr);
71193a93e3b7SToby 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");
7120e0fc9d1bSMatthew G. Knepley   } else {
71213a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
71223a93e3b7SToby Isaac   }
7123b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
712447a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
712562a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
712647a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
7127e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
7128e87bb0d3SMatthew G Knepley }
712914f150ffSMatthew G. Knepley 
7130f4d763aaSMatthew G. Knepley /*@
7131f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
7132f4d763aaSMatthew G. Knepley 
71338f700142SStefano Zampini   Collective on dm
71348f700142SStefano Zampini 
7135f4d763aaSMatthew G. Knepley   Input Parameter:
7136f4d763aaSMatthew G. Knepley . dm - The original DM
7137f4d763aaSMatthew G. Knepley 
7138f4d763aaSMatthew G. Knepley   Output Parameter:
7139f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
7140f4d763aaSMatthew G. Knepley 
7141f4d763aaSMatthew G. Knepley   Level: intermediate
7142f4d763aaSMatthew G. Knepley 
7143e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
7144f4d763aaSMatthew G. Knepley @*/
714514f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
714614f150ffSMatthew G. Knepley {
7147c26acbdeSMatthew G. Knepley   PetscSection   section;
71482d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
714914f150ffSMatthew G. Knepley   PetscErrorCode ierr;
715014f150ffSMatthew G. Knepley 
715114f150ffSMatthew G. Knepley   PetscFunctionBegin;
715214f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
715314f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
715492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
7155c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
7156ffc4695bSBarry Smith   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
71572d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
7158c26acbdeSMatthew G. Knepley     *odm = dm;
7159c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
7160c26acbdeSMatthew G. Knepley   }
716114f150ffSMatthew G. Knepley   if (!dm->dmBC) {
7162c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
716314f150ffSMatthew G. Knepley     PetscSF      sf;
716414f150ffSMatthew G. Knepley 
716514f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
7166e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
716714f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
716892fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
716914f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
717014f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
717115b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
7172e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
717314f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
717414f150ffSMatthew G. Knepley   }
717514f150ffSMatthew G. Knepley   *odm = dm->dmBC;
717614f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
717714f150ffSMatthew G. Knepley }
7178f4d763aaSMatthew G. Knepley 
7179f4d763aaSMatthew G. Knepley /*@
7180cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7181f4d763aaSMatthew G. Knepley 
7182f4d763aaSMatthew G. Knepley   Input Parameter:
7183f4d763aaSMatthew G. Knepley . dm - The original DM
7184f4d763aaSMatthew G. Knepley 
7185cdb7a50dSMatthew G. Knepley   Output Parameters:
7186cdb7a50dSMatthew G. Knepley + num - The output sequence number
7187cdb7a50dSMatthew G. Knepley - val - The output sequence value
7188f4d763aaSMatthew G. Knepley 
7189f4d763aaSMatthew G. Knepley   Level: intermediate
7190f4d763aaSMatthew G. Knepley 
7191f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7192f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7193f4d763aaSMatthew G. Knepley 
7194f4d763aaSMatthew G. Knepley .seealso: VecView()
7195f4d763aaSMatthew G. Knepley @*/
7196cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7197f4d763aaSMatthew G. Knepley {
7198f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7199f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7200534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7201534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7202f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7203f4d763aaSMatthew G. Knepley }
7204f4d763aaSMatthew G. Knepley 
7205f4d763aaSMatthew G. Knepley /*@
7206cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7207f4d763aaSMatthew G. Knepley 
7208f4d763aaSMatthew G. Knepley   Input Parameters:
7209f4d763aaSMatthew G. Knepley + dm - The original DM
7210cdb7a50dSMatthew G. Knepley . num - The output sequence number
7211cdb7a50dSMatthew G. Knepley - val - The output sequence value
7212f4d763aaSMatthew G. Knepley 
7213f4d763aaSMatthew G. Knepley   Level: intermediate
7214f4d763aaSMatthew G. Knepley 
7215f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7216f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7217f4d763aaSMatthew G. Knepley 
7218f4d763aaSMatthew G. Knepley .seealso: VecView()
7219f4d763aaSMatthew G. Knepley @*/
7220cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7221f4d763aaSMatthew G. Knepley {
7222f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7223f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7224f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7225cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7226cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7227cdb7a50dSMatthew G. Knepley }
7228cdb7a50dSMatthew G. Knepley 
7229cdb7a50dSMatthew G. Knepley /*@C
7230cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7231cdb7a50dSMatthew G. Knepley 
7232cdb7a50dSMatthew G. Knepley   Input Parameters:
7233cdb7a50dSMatthew G. Knepley + dm   - The original DM
7234cdb7a50dSMatthew G. Knepley . name - The sequence name
7235cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7236cdb7a50dSMatthew G. Knepley 
7237cdb7a50dSMatthew G. Knepley   Output Parameter:
7238cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7239cdb7a50dSMatthew G. Knepley 
7240cdb7a50dSMatthew G. Knepley   Level: intermediate
7241cdb7a50dSMatthew G. Knepley 
7242cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7243cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7244cdb7a50dSMatthew G. Knepley 
7245cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7246cdb7a50dSMatthew G. Knepley @*/
7247cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7248cdb7a50dSMatthew G. Knepley {
7249cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7250cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
7251cdb7a50dSMatthew G. Knepley 
7252cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7253cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7254cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7255064a246eSJacob Faibussowitsch   PetscValidRealPointer(val,5);
7256cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
7257cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7258cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7259cdb7a50dSMatthew G. Knepley     PetscScalar value;
7260cdb7a50dSMatthew G. Knepley 
726139d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
72624aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7263cdb7a50dSMatthew G. Knepley #endif
7264cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7265f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7266f4d763aaSMatthew G. Knepley }
72678e4ac7eaSMatthew G. Knepley 
72688e4ac7eaSMatthew G. Knepley /*@
72698e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
72708e4ac7eaSMatthew G. Knepley 
72718e4ac7eaSMatthew G. Knepley   Not collective
72728e4ac7eaSMatthew G. Knepley 
72738e4ac7eaSMatthew G. Knepley   Input Parameter:
72748e4ac7eaSMatthew G. Knepley . dm - The DM
72758e4ac7eaSMatthew G. Knepley 
72768e4ac7eaSMatthew G. Knepley   Output Parameter:
72778e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
72788e4ac7eaSMatthew G. Knepley 
72798e4ac7eaSMatthew G. Knepley   Level: beginner
72808e4ac7eaSMatthew G. Knepley 
72818e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
72828e4ac7eaSMatthew G. Knepley @*/
72838e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
72848e4ac7eaSMatthew G. Knepley {
72858e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
72868e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7287534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
72888e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
72898e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
72908e4ac7eaSMatthew G. Knepley }
72918e4ac7eaSMatthew G. Knepley 
72928e4ac7eaSMatthew G. Knepley /*@
72935d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
72948e4ac7eaSMatthew G. Knepley 
72958e4ac7eaSMatthew G. Knepley   Collective on dm
72968e4ac7eaSMatthew G. Knepley 
72978e4ac7eaSMatthew G. Knepley   Input Parameters:
72988e4ac7eaSMatthew G. Knepley + dm - The DM
72998e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
73008e4ac7eaSMatthew G. Knepley 
73015d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
73025d3b26e6SMatthew G. Knepley 
73038e4ac7eaSMatthew G. Knepley   Level: beginner
73048e4ac7eaSMatthew G. Knepley 
73055d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
73068e4ac7eaSMatthew G. Knepley @*/
73078e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
73088e4ac7eaSMatthew G. Knepley {
73098e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
73108e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73118833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
73128e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
73138e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
73148e4ac7eaSMatthew G. Knepley }
7315c58f1c22SToby Isaac 
7316c58f1c22SToby Isaac 
7317c58f1c22SToby Isaac /*@C
7318c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7319c58f1c22SToby Isaac 
7320c58f1c22SToby Isaac   Not Collective
7321c58f1c22SToby Isaac 
7322c58f1c22SToby Isaac   Input Parameters:
7323c58f1c22SToby Isaac + dm   - The DM object
7324c58f1c22SToby Isaac - name - The label name
7325c58f1c22SToby Isaac 
7326c58f1c22SToby Isaac   Level: intermediate
7327c58f1c22SToby Isaac 
7328c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7329c58f1c22SToby Isaac @*/
7330c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7331c58f1c22SToby Isaac {
73325d80c0bfSVaclav Hapla   PetscBool      flg;
73335d80c0bfSVaclav Hapla   DMLabel        label;
7334c58f1c22SToby Isaac   PetscErrorCode ierr;
7335c58f1c22SToby Isaac 
7336c58f1c22SToby Isaac   PetscFunctionBegin;
7337c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7338c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
73395d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7340c58f1c22SToby Isaac   if (!flg) {
73415d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
73425d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
73435d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7344c58f1c22SToby Isaac   }
7345c58f1c22SToby Isaac   PetscFunctionReturn(0);
7346c58f1c22SToby Isaac }
7347c58f1c22SToby Isaac 
7348c58f1c22SToby Isaac /*@C
73490fdc7489SMatthew Knepley   DMCreateLabelAtIndex - Create a label of the given name at the iven index. If it already exists, move it to this index.
73500fdc7489SMatthew Knepley 
73510fdc7489SMatthew Knepley   Not Collective
73520fdc7489SMatthew Knepley 
73530fdc7489SMatthew Knepley   Input Parameters:
73540fdc7489SMatthew Knepley + dm   - The DM object
73550fdc7489SMatthew Knepley . l    - The index for the label
73560fdc7489SMatthew Knepley - name - The label name
73570fdc7489SMatthew Knepley 
73580fdc7489SMatthew Knepley   Level: intermediate
73590fdc7489SMatthew Knepley 
73600fdc7489SMatthew Knepley .seealso: DMCreateLabel(), DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
73610fdc7489SMatthew Knepley @*/
73620fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
73630fdc7489SMatthew Knepley {
73640fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
73650fdc7489SMatthew Knepley   DMLabel        label;
73660fdc7489SMatthew Knepley   PetscInt       Nl, m;
73670fdc7489SMatthew Knepley   PetscBool      flg, match;
73680fdc7489SMatthew Knepley   const char    *lname;
73690fdc7489SMatthew Knepley   PetscErrorCode ierr;
73700fdc7489SMatthew Knepley 
73710fdc7489SMatthew Knepley   PetscFunctionBegin;
73720fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7373064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
73740fdc7489SMatthew Knepley   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
73750fdc7489SMatthew Knepley   if (!flg) {
73760fdc7489SMatthew Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
73770fdc7489SMatthew Knepley     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
73780fdc7489SMatthew Knepley     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
73790fdc7489SMatthew Knepley   }
73800fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
73810fdc7489SMatthew Knepley   if (l >= Nl) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %D must be in [0, %D)", l, Nl);
73820fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
73830fdc7489SMatthew Knepley     ierr = PetscObjectGetName((PetscObject) orig->label, &lname);CHKERRQ(ierr);
73840fdc7489SMatthew Knepley     ierr = PetscStrcmp(name, lname, &match);CHKERRQ(ierr);
73850fdc7489SMatthew Knepley     if (match) break;
73860fdc7489SMatthew Knepley   }
73870fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
73880fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
73890fdc7489SMatthew Knepley   else    prev->next = orig->next;
73900fdc7489SMatthew Knepley   if (!l) {
73910fdc7489SMatthew Knepley     orig->next = dm->labels;
73920fdc7489SMatthew Knepley     dm->labels = orig;
73930fdc7489SMatthew Knepley   } else {
73940fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
73950fdc7489SMatthew Knepley     orig->next = prev->next;
73960fdc7489SMatthew Knepley     prev->next = orig;
73970fdc7489SMatthew Knepley   }
73980fdc7489SMatthew Knepley   PetscFunctionReturn(0);
73990fdc7489SMatthew Knepley }
74000fdc7489SMatthew Knepley 
74010fdc7489SMatthew Knepley /*@C
7402c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7403c58f1c22SToby Isaac 
7404c58f1c22SToby Isaac   Not Collective
7405c58f1c22SToby Isaac 
7406c58f1c22SToby Isaac   Input Parameters:
7407c58f1c22SToby Isaac + dm   - The DM object
7408c58f1c22SToby Isaac . name - The label name
7409c58f1c22SToby Isaac - point - The mesh point
7410c58f1c22SToby Isaac 
7411c58f1c22SToby Isaac   Output Parameter:
7412c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7413c58f1c22SToby Isaac 
7414c58f1c22SToby Isaac   Level: beginner
7415c58f1c22SToby Isaac 
7416c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7417c58f1c22SToby Isaac @*/
7418c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7419c58f1c22SToby Isaac {
7420c58f1c22SToby Isaac   DMLabel        label;
7421c58f1c22SToby Isaac   PetscErrorCode ierr;
7422c58f1c22SToby Isaac 
7423c58f1c22SToby Isaac   PetscFunctionBegin;
7424c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7425c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7426c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
742713903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7428c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7429c58f1c22SToby Isaac   PetscFunctionReturn(0);
7430c58f1c22SToby Isaac }
7431c58f1c22SToby Isaac 
7432c58f1c22SToby Isaac /*@C
7433c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7434c58f1c22SToby Isaac 
7435c58f1c22SToby Isaac   Not Collective
7436c58f1c22SToby Isaac 
7437c58f1c22SToby Isaac   Input Parameters:
7438c58f1c22SToby Isaac + dm   - The DM object
7439c58f1c22SToby Isaac . name - The label name
7440c58f1c22SToby Isaac . point - The mesh point
7441c58f1c22SToby Isaac - value - The label value for this point
7442c58f1c22SToby Isaac 
7443c58f1c22SToby Isaac   Output Parameter:
7444c58f1c22SToby Isaac 
7445c58f1c22SToby Isaac   Level: beginner
7446c58f1c22SToby Isaac 
7447c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7448c58f1c22SToby Isaac @*/
7449c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7450c58f1c22SToby Isaac {
7451c58f1c22SToby Isaac   DMLabel        label;
7452c58f1c22SToby Isaac   PetscErrorCode ierr;
7453c58f1c22SToby Isaac 
7454c58f1c22SToby Isaac   PetscFunctionBegin;
7455c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7456c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7457c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7458c58f1c22SToby Isaac   if (!label) {
7459c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7460c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7461c58f1c22SToby Isaac   }
7462c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7463c58f1c22SToby Isaac   PetscFunctionReturn(0);
7464c58f1c22SToby Isaac }
7465c58f1c22SToby Isaac 
7466c58f1c22SToby Isaac /*@C
7467c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7468c58f1c22SToby Isaac 
7469c58f1c22SToby Isaac   Not Collective
7470c58f1c22SToby Isaac 
7471c58f1c22SToby Isaac   Input Parameters:
7472c58f1c22SToby Isaac + dm   - The DM object
7473c58f1c22SToby Isaac . name - The label name
7474c58f1c22SToby Isaac . point - The mesh point
7475c58f1c22SToby Isaac - value - The label value for this point
7476c58f1c22SToby Isaac 
7477c58f1c22SToby Isaac   Output Parameter:
7478c58f1c22SToby Isaac 
7479c58f1c22SToby Isaac   Level: beginner
7480c58f1c22SToby Isaac 
7481c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7482c58f1c22SToby Isaac @*/
7483c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7484c58f1c22SToby Isaac {
7485c58f1c22SToby Isaac   DMLabel        label;
7486c58f1c22SToby Isaac   PetscErrorCode ierr;
7487c58f1c22SToby Isaac 
7488c58f1c22SToby Isaac   PetscFunctionBegin;
7489c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7490c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7491c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7492c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7493c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7494c58f1c22SToby Isaac   PetscFunctionReturn(0);
7495c58f1c22SToby Isaac }
7496c58f1c22SToby Isaac 
7497c58f1c22SToby Isaac /*@C
7498c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7499c58f1c22SToby Isaac 
7500c58f1c22SToby Isaac   Not Collective
7501c58f1c22SToby Isaac 
7502c58f1c22SToby Isaac   Input Parameters:
7503c58f1c22SToby Isaac + dm   - The DM object
7504c58f1c22SToby Isaac - name - The label name
7505c58f1c22SToby Isaac 
7506c58f1c22SToby Isaac   Output Parameter:
7507c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7508c58f1c22SToby Isaac 
7509c58f1c22SToby Isaac   Level: beginner
7510c58f1c22SToby Isaac 
7511df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7512c58f1c22SToby Isaac @*/
7513c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7514c58f1c22SToby Isaac {
7515c58f1c22SToby Isaac   DMLabel        label;
7516c58f1c22SToby Isaac   PetscErrorCode ierr;
7517c58f1c22SToby Isaac 
7518c58f1c22SToby Isaac   PetscFunctionBegin;
7519c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7520c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7521534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7522c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7523c58f1c22SToby Isaac   *size = 0;
7524c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7525c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7526c58f1c22SToby Isaac   PetscFunctionReturn(0);
7527c58f1c22SToby Isaac }
7528c58f1c22SToby Isaac 
7529c58f1c22SToby Isaac /*@C
7530c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7531c58f1c22SToby Isaac 
7532c58f1c22SToby Isaac   Not Collective
7533c58f1c22SToby Isaac 
7534c58f1c22SToby Isaac   Input Parameters:
7535c58f1c22SToby Isaac + mesh - The DM object
7536c58f1c22SToby Isaac - name - The label name
7537c58f1c22SToby Isaac 
7538c58f1c22SToby Isaac   Output Parameter:
7539c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7540c58f1c22SToby Isaac 
7541c58f1c22SToby Isaac   Level: beginner
7542c58f1c22SToby Isaac 
7543c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7544c58f1c22SToby Isaac @*/
7545c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7546c58f1c22SToby Isaac {
7547c58f1c22SToby Isaac   DMLabel        label;
7548c58f1c22SToby Isaac   PetscErrorCode ierr;
7549c58f1c22SToby Isaac 
7550c58f1c22SToby Isaac   PetscFunctionBegin;
7551c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7552c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7553c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7554c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7555c58f1c22SToby Isaac   *ids = NULL;
7556dab2e251SBlaise Bourdin  if (label) {
7557c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7558dab2e251SBlaise Bourdin   } else {
7559dab2e251SBlaise Bourdin     /* returning an empty IS */
7560dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7561dab2e251SBlaise Bourdin   }
7562c58f1c22SToby Isaac   PetscFunctionReturn(0);
7563c58f1c22SToby Isaac }
7564c58f1c22SToby Isaac 
7565c58f1c22SToby Isaac /*@C
7566c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7567c58f1c22SToby Isaac 
7568c58f1c22SToby Isaac   Not Collective
7569c58f1c22SToby Isaac 
7570c58f1c22SToby Isaac   Input Parameters:
7571c58f1c22SToby Isaac + dm - The DM object
7572c58f1c22SToby Isaac . name - The label name
7573c58f1c22SToby Isaac - value - The stratum value
7574c58f1c22SToby Isaac 
7575c58f1c22SToby Isaac   Output Parameter:
7576c58f1c22SToby Isaac . size - The stratum size
7577c58f1c22SToby Isaac 
7578c58f1c22SToby Isaac   Level: beginner
7579c58f1c22SToby Isaac 
7580c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7581c58f1c22SToby Isaac @*/
7582c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7583c58f1c22SToby Isaac {
7584c58f1c22SToby Isaac   DMLabel        label;
7585c58f1c22SToby Isaac   PetscErrorCode ierr;
7586c58f1c22SToby Isaac 
7587c58f1c22SToby Isaac   PetscFunctionBegin;
7588c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7589c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7590534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7591c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7592c58f1c22SToby Isaac   *size = 0;
7593c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7594c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7595c58f1c22SToby Isaac   PetscFunctionReturn(0);
7596c58f1c22SToby Isaac }
7597c58f1c22SToby Isaac 
7598c58f1c22SToby Isaac /*@C
7599c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7600c58f1c22SToby Isaac 
7601c58f1c22SToby Isaac   Not Collective
7602c58f1c22SToby Isaac 
7603c58f1c22SToby Isaac   Input Parameters:
7604c58f1c22SToby Isaac + dm - The DM object
7605c58f1c22SToby Isaac . name - The label name
7606c58f1c22SToby Isaac - value - The stratum value
7607c58f1c22SToby Isaac 
7608c58f1c22SToby Isaac   Output Parameter:
7609c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7610c58f1c22SToby Isaac 
7611c58f1c22SToby Isaac   Level: beginner
7612c58f1c22SToby Isaac 
7613c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7614c58f1c22SToby Isaac @*/
7615c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7616c58f1c22SToby Isaac {
7617c58f1c22SToby Isaac   DMLabel        label;
7618c58f1c22SToby Isaac   PetscErrorCode ierr;
7619c58f1c22SToby Isaac 
7620c58f1c22SToby Isaac   PetscFunctionBegin;
7621c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7622c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7623c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7624c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7625c58f1c22SToby Isaac   *points = NULL;
7626c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7627c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7628c58f1c22SToby Isaac   PetscFunctionReturn(0);
7629c58f1c22SToby Isaac }
7630c58f1c22SToby Isaac 
76314de306b1SToby Isaac /*@C
76329044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
76334de306b1SToby Isaac 
76344de306b1SToby Isaac   Not Collective
76354de306b1SToby Isaac 
76364de306b1SToby Isaac   Input Parameters:
76374de306b1SToby Isaac + dm - The DM object
76384de306b1SToby Isaac . name - The label name
76394de306b1SToby Isaac . value - The stratum value
76404de306b1SToby Isaac - points - The stratum points
76414de306b1SToby Isaac 
76424de306b1SToby Isaac   Level: beginner
76434de306b1SToby Isaac 
76444de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
76454de306b1SToby Isaac @*/
76464de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
76474de306b1SToby Isaac {
76484de306b1SToby Isaac   DMLabel        label;
76494de306b1SToby Isaac   PetscErrorCode ierr;
76504de306b1SToby Isaac 
76514de306b1SToby Isaac   PetscFunctionBegin;
76524de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
76534de306b1SToby Isaac   PetscValidCharPointer(name, 2);
76544de306b1SToby Isaac   PetscValidPointer(points, 4);
76554de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
76564de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
76574de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
76584de306b1SToby Isaac   PetscFunctionReturn(0);
76594de306b1SToby Isaac }
76604de306b1SToby Isaac 
7661c58f1c22SToby Isaac /*@C
7662c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7663c58f1c22SToby Isaac 
7664c58f1c22SToby Isaac   Not Collective
7665c58f1c22SToby Isaac 
7666c58f1c22SToby Isaac   Input Parameters:
7667c58f1c22SToby Isaac + dm   - The DM object
7668c58f1c22SToby Isaac . name - The label name
7669c58f1c22SToby Isaac - value - The label value for this point
7670c58f1c22SToby Isaac 
7671c58f1c22SToby Isaac   Output Parameter:
7672c58f1c22SToby Isaac 
7673c58f1c22SToby Isaac   Level: beginner
7674c58f1c22SToby Isaac 
7675c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7676c58f1c22SToby Isaac @*/
7677c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7678c58f1c22SToby Isaac {
7679c58f1c22SToby Isaac   DMLabel        label;
7680c58f1c22SToby Isaac   PetscErrorCode ierr;
7681c58f1c22SToby Isaac 
7682c58f1c22SToby Isaac   PetscFunctionBegin;
7683c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7684c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7685c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7686c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7687c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7688c58f1c22SToby Isaac   PetscFunctionReturn(0);
7689c58f1c22SToby Isaac }
7690c58f1c22SToby Isaac 
7691c58f1c22SToby Isaac /*@
7692c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7693c58f1c22SToby Isaac 
7694c58f1c22SToby Isaac   Not Collective
7695c58f1c22SToby Isaac 
7696c58f1c22SToby Isaac   Input Parameter:
7697c58f1c22SToby Isaac . dm   - The DM object
7698c58f1c22SToby Isaac 
7699c58f1c22SToby Isaac   Output Parameter:
7700c58f1c22SToby Isaac . numLabels - the number of Labels
7701c58f1c22SToby Isaac 
7702c58f1c22SToby Isaac   Level: intermediate
7703c58f1c22SToby Isaac 
7704c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7705c58f1c22SToby Isaac @*/
7706c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7707c58f1c22SToby Isaac {
77085d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7709c58f1c22SToby Isaac   PetscInt  n    = 0;
7710c58f1c22SToby Isaac 
7711c58f1c22SToby Isaac   PetscFunctionBegin;
7712c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7713534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7714c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7715c58f1c22SToby Isaac   *numLabels = n;
7716c58f1c22SToby Isaac   PetscFunctionReturn(0);
7717c58f1c22SToby Isaac }
7718c58f1c22SToby Isaac 
7719c58f1c22SToby Isaac /*@C
7720c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7721c58f1c22SToby Isaac 
7722c58f1c22SToby Isaac   Not Collective
7723c58f1c22SToby Isaac 
7724c58f1c22SToby Isaac   Input Parameters:
7725c58f1c22SToby Isaac + dm - The DM object
7726c58f1c22SToby Isaac - n  - the label number
7727c58f1c22SToby Isaac 
7728c58f1c22SToby Isaac   Output Parameter:
7729c58f1c22SToby Isaac . name - the label name
7730c58f1c22SToby Isaac 
7731c58f1c22SToby Isaac   Level: intermediate
7732c58f1c22SToby Isaac 
7733c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7734c58f1c22SToby Isaac @*/
7735c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7736c58f1c22SToby Isaac {
77375d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7738c58f1c22SToby Isaac   PetscInt       l    = 0;
7739d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7740c58f1c22SToby Isaac 
7741c58f1c22SToby Isaac   PetscFunctionBegin;
7742c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7743c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7744c58f1c22SToby Isaac   while (next) {
7745c58f1c22SToby Isaac     if (l == n) {
7746d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7747c58f1c22SToby Isaac       PetscFunctionReturn(0);
7748c58f1c22SToby Isaac     }
7749c58f1c22SToby Isaac     ++l;
7750c58f1c22SToby Isaac     next = next->next;
7751c58f1c22SToby Isaac   }
7752c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7753c58f1c22SToby Isaac }
7754c58f1c22SToby Isaac 
7755c58f1c22SToby Isaac /*@C
7756c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7757c58f1c22SToby Isaac 
7758c58f1c22SToby Isaac   Not Collective
7759c58f1c22SToby Isaac 
7760c58f1c22SToby Isaac   Input Parameters:
7761c58f1c22SToby Isaac + dm   - The DM object
7762c58f1c22SToby Isaac - name - The label name
7763c58f1c22SToby Isaac 
7764c58f1c22SToby Isaac   Output Parameter:
7765c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7766c58f1c22SToby Isaac 
7767c58f1c22SToby Isaac   Level: intermediate
7768c58f1c22SToby Isaac 
7769c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7770c58f1c22SToby Isaac @*/
7771c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7772c58f1c22SToby Isaac {
77735d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7774d67d17b1SMatthew G. Knepley   const char    *lname;
7775c58f1c22SToby Isaac   PetscErrorCode ierr;
7776c58f1c22SToby Isaac 
7777c58f1c22SToby Isaac   PetscFunctionBegin;
7778c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7779c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7780534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7781c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7782c58f1c22SToby Isaac   while (next) {
7783d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7784d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7785c58f1c22SToby Isaac     if (*hasLabel) break;
7786c58f1c22SToby Isaac     next = next->next;
7787c58f1c22SToby Isaac   }
7788c58f1c22SToby Isaac   PetscFunctionReturn(0);
7789c58f1c22SToby Isaac }
7790c58f1c22SToby Isaac 
7791c58f1c22SToby Isaac /*@C
7792c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7793c58f1c22SToby Isaac 
7794c58f1c22SToby Isaac   Not Collective
7795c58f1c22SToby Isaac 
7796c58f1c22SToby Isaac   Input Parameters:
7797c58f1c22SToby Isaac + dm   - The DM object
7798c58f1c22SToby Isaac - name - The label name
7799c58f1c22SToby Isaac 
7800c58f1c22SToby Isaac   Output Parameter:
7801c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7802c58f1c22SToby Isaac 
78036d7c9049SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
78046d7c9049SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
78056d7c9049SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
78066d7c9049SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
78076d7c9049SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
78086d7c9049SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
78096d7c9049SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
78106d7c9049SMatthew G. Knepley 
7811c58f1c22SToby Isaac   Level: intermediate
7812c58f1c22SToby Isaac 
78136d7c9049SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
7814c58f1c22SToby Isaac @*/
7815c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7816c58f1c22SToby Isaac {
78175d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7818c58f1c22SToby Isaac   PetscBool      hasLabel;
7819d67d17b1SMatthew G. Knepley   const char    *lname;
7820c58f1c22SToby Isaac   PetscErrorCode ierr;
7821c58f1c22SToby Isaac 
7822c58f1c22SToby Isaac   PetscFunctionBegin;
7823c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7824c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7825c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7826c58f1c22SToby Isaac   *label = NULL;
7827c58f1c22SToby Isaac   while (next) {
7828d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7829d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7830c58f1c22SToby Isaac     if (hasLabel) {
7831c58f1c22SToby Isaac       *label = next->label;
7832c58f1c22SToby Isaac       break;
7833c58f1c22SToby Isaac     }
7834c58f1c22SToby Isaac     next = next->next;
7835c58f1c22SToby Isaac   }
7836c58f1c22SToby Isaac   PetscFunctionReturn(0);
7837c58f1c22SToby Isaac }
7838c58f1c22SToby Isaac 
7839c58f1c22SToby Isaac /*@C
7840c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7841c58f1c22SToby Isaac 
7842c58f1c22SToby Isaac   Not Collective
7843c58f1c22SToby Isaac 
7844c58f1c22SToby Isaac   Input Parameters:
7845c58f1c22SToby Isaac + dm - The DM object
7846c58f1c22SToby Isaac - n  - the label number
7847c58f1c22SToby Isaac 
7848c58f1c22SToby Isaac   Output Parameter:
7849c58f1c22SToby Isaac . label - the label
7850c58f1c22SToby Isaac 
7851c58f1c22SToby Isaac   Level: intermediate
7852c58f1c22SToby Isaac 
7853c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7854c58f1c22SToby Isaac @*/
7855c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7856c58f1c22SToby Isaac {
78575d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7858c58f1c22SToby Isaac   PetscInt    l    = 0;
7859c58f1c22SToby Isaac 
7860c58f1c22SToby Isaac   PetscFunctionBegin;
7861c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7862c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7863c58f1c22SToby Isaac   while (next) {
7864c58f1c22SToby Isaac     if (l == n) {
7865c58f1c22SToby Isaac       *label = next->label;
7866c58f1c22SToby Isaac       PetscFunctionReturn(0);
7867c58f1c22SToby Isaac     }
7868c58f1c22SToby Isaac     ++l;
7869c58f1c22SToby Isaac     next = next->next;
7870c58f1c22SToby Isaac   }
7871c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7872c58f1c22SToby Isaac }
7873c58f1c22SToby Isaac 
7874c58f1c22SToby Isaac /*@C
7875c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7876c58f1c22SToby Isaac 
7877c58f1c22SToby Isaac   Not Collective
7878c58f1c22SToby Isaac 
7879c58f1c22SToby Isaac   Input Parameters:
7880c58f1c22SToby Isaac + dm   - The DM object
7881c58f1c22SToby Isaac - label - The DMLabel
7882c58f1c22SToby Isaac 
7883c58f1c22SToby Isaac   Level: developer
7884c58f1c22SToby Isaac 
7885c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7886c58f1c22SToby Isaac @*/
7887c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7888c58f1c22SToby Isaac {
78895d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7890c58f1c22SToby Isaac   PetscBool      hasLabel;
7891d67d17b1SMatthew G. Knepley   const char    *lname;
78925d80c0bfSVaclav Hapla   PetscBool      flg;
7893c58f1c22SToby Isaac   PetscErrorCode ierr;
7894c58f1c22SToby Isaac 
7895c58f1c22SToby Isaac   PetscFunctionBegin;
7896c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7897d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7898d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7899d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7900c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7901c58f1c22SToby Isaac   tmpLabel->label  = label;
7902c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
79035d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
79045d80c0bfSVaclav Hapla   *p = tmpLabel;
790508f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
79065d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
79075d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7908ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7909ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7910c58f1c22SToby Isaac   PetscFunctionReturn(0);
7911c58f1c22SToby Isaac }
7912c58f1c22SToby Isaac 
7913c58f1c22SToby Isaac /*@C
7914*4a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
7915*4a7ee7d0SMatthew G. Knepley 
7916*4a7ee7d0SMatthew G. Knepley   Not Collective
7917*4a7ee7d0SMatthew G. Knepley 
7918*4a7ee7d0SMatthew G. Knepley   Input Parameters:
7919*4a7ee7d0SMatthew G. Knepley + dm    - The DM object
7920*4a7ee7d0SMatthew G. Knepley - label - The DMLabel, having the same name, to substitute
7921*4a7ee7d0SMatthew G. Knepley 
7922*4a7ee7d0SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
7923*4a7ee7d0SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
7924*4a7ee7d0SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
7925*4a7ee7d0SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7926*4a7ee7d0SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7927*4a7ee7d0SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7928*4a7ee7d0SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
7929*4a7ee7d0SMatthew G. Knepley 
7930*4a7ee7d0SMatthew G. Knepley   Level: intermediate
7931*4a7ee7d0SMatthew G. Knepley 
7932*4a7ee7d0SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
7933*4a7ee7d0SMatthew G. Knepley @*/
7934*4a7ee7d0SMatthew G. Knepley PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7935*4a7ee7d0SMatthew G. Knepley {
7936*4a7ee7d0SMatthew G. Knepley   DMLabelLink    next = dm->labels;
7937*4a7ee7d0SMatthew G. Knepley   PetscBool      hasLabel, flg;
7938*4a7ee7d0SMatthew G. Knepley   const char    *name, *lname;
7939*4a7ee7d0SMatthew G. Knepley   PetscErrorCode ierr;
7940*4a7ee7d0SMatthew G. Knepley 
7941*4a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
7942*4a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7943*4a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
7944*4a7ee7d0SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &name);CHKERRQ(ierr);
7945*4a7ee7d0SMatthew G. Knepley   while (next) {
7946*4a7ee7d0SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7947*4a7ee7d0SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7948*4a7ee7d0SMatthew G. Knepley     if (hasLabel) {
7949*4a7ee7d0SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
7950*4a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
7951*4a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
7952*4a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7953*4a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
7954*4a7ee7d0SMatthew G. Knepley       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
7955*4a7ee7d0SMatthew G. Knepley       next->label = label;
7956*4a7ee7d0SMatthew G. Knepley       break;
7957*4a7ee7d0SMatthew G. Knepley     }
7958*4a7ee7d0SMatthew G. Knepley     next = next->next;
7959*4a7ee7d0SMatthew G. Knepley   }
7960*4a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
7961*4a7ee7d0SMatthew G. Knepley }
7962*4a7ee7d0SMatthew G. Knepley 
7963*4a7ee7d0SMatthew G. Knepley /*@C
7964e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7965c58f1c22SToby Isaac 
7966c58f1c22SToby Isaac   Not Collective
7967c58f1c22SToby Isaac 
7968c58f1c22SToby Isaac   Input Parameters:
7969c58f1c22SToby Isaac + dm   - The DM object
7970c58f1c22SToby Isaac - name - The label name
7971c58f1c22SToby Isaac 
7972c58f1c22SToby Isaac   Output Parameter:
7973c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7974c58f1c22SToby Isaac 
7975c58f1c22SToby Isaac   Level: developer
7976c58f1c22SToby Isaac 
7977e5472504SVaclav Hapla   Notes:
7978e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7979e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7980e5472504SVaclav Hapla 
7981e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7982e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7983e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7984e5472504SVaclav Hapla 
7985e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7986c58f1c22SToby Isaac @*/
7987c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7988c58f1c22SToby Isaac {
798995d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7990c58f1c22SToby Isaac   PetscBool      hasLabel;
7991d67d17b1SMatthew G. Knepley   const char    *lname;
7992c58f1c22SToby Isaac   PetscErrorCode ierr;
7993c58f1c22SToby Isaac 
7994c58f1c22SToby Isaac   PetscFunctionBegin;
7995c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7996e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7997e5472504SVaclav Hapla   if (label) {
7998e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7999c58f1c22SToby Isaac     *label = NULL;
8000e5472504SVaclav Hapla   }
80015d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
800295d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
8003d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
8004c58f1c22SToby Isaac     if (hasLabel) {
800595d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
8006c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
800795d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
8008ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
8009ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
801095d578d6SVaclav Hapla       if (label) *label = link->label;
801195d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
801295d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8013c58f1c22SToby Isaac       break;
8014c58f1c22SToby Isaac     }
8015c58f1c22SToby Isaac   }
8016c58f1c22SToby Isaac   PetscFunctionReturn(0);
8017c58f1c22SToby Isaac }
8018c58f1c22SToby Isaac 
8019306894acSVaclav Hapla /*@
8020306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
8021306894acSVaclav Hapla 
8022306894acSVaclav Hapla   Not Collective
8023306894acSVaclav Hapla 
8024306894acSVaclav Hapla   Input Parameters:
8025306894acSVaclav Hapla + dm   - The DM object
8026306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
8027306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
8028306894acSVaclav Hapla 
8029306894acSVaclav Hapla   Level: developer
8030306894acSVaclav Hapla 
8031306894acSVaclav Hapla   Notes:
8032306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
8033306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
8034306894acSVaclav Hapla   *label nullified.
8035306894acSVaclav Hapla 
8036306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
8037306894acSVaclav Hapla @*/
8038306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
8039306894acSVaclav Hapla {
804043e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
8041306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
8042306894acSVaclav Hapla   PetscErrorCode ierr;
8043306894acSVaclav Hapla 
8044306894acSVaclav Hapla   PetscFunctionBegin;
8045306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8046306894acSVaclav Hapla   PetscValidPointer(label, 2);
8047f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
8048306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
8049306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
80505d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
805143e45a93SVaclav Hapla     if (*label == link->label) {
8052306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
805343e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
8054306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
8055ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
805643e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
805743e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
805843e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8059306894acSVaclav Hapla       break;
8060306894acSVaclav Hapla     }
8061306894acSVaclav Hapla   }
8062306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
8063306894acSVaclav Hapla   PetscFunctionReturn(0);
8064306894acSVaclav Hapla }
8065306894acSVaclav Hapla 
8066c58f1c22SToby Isaac /*@C
8067c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
8068c58f1c22SToby Isaac 
8069c58f1c22SToby Isaac   Not Collective
8070c58f1c22SToby Isaac 
8071c58f1c22SToby Isaac   Input Parameters:
8072c58f1c22SToby Isaac + dm   - The DM object
8073c58f1c22SToby Isaac - name - The label name
8074c58f1c22SToby Isaac 
8075c58f1c22SToby Isaac   Output Parameter:
8076c58f1c22SToby Isaac . output - The flag for output
8077c58f1c22SToby Isaac 
8078c58f1c22SToby Isaac   Level: developer
8079c58f1c22SToby Isaac 
8080c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8081c58f1c22SToby Isaac @*/
8082c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
8083c58f1c22SToby Isaac {
80845d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8085d67d17b1SMatthew G. Knepley   const char    *lname;
8086c58f1c22SToby Isaac   PetscErrorCode ierr;
8087c58f1c22SToby Isaac 
8088c58f1c22SToby Isaac   PetscFunctionBegin;
8089c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8090c58f1c22SToby Isaac   PetscValidPointer(name, 2);
8091c58f1c22SToby Isaac   PetscValidPointer(output, 3);
8092c58f1c22SToby Isaac   while (next) {
8093c58f1c22SToby Isaac     PetscBool flg;
8094c58f1c22SToby Isaac 
8095d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8096d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8097c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
8098c58f1c22SToby Isaac     next = next->next;
8099c58f1c22SToby Isaac   }
8100c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8101c58f1c22SToby Isaac }
8102c58f1c22SToby Isaac 
8103c58f1c22SToby Isaac /*@C
8104c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
8105c58f1c22SToby Isaac 
8106c58f1c22SToby Isaac   Not Collective
8107c58f1c22SToby Isaac 
8108c58f1c22SToby Isaac   Input Parameters:
8109c58f1c22SToby Isaac + dm     - The DM object
8110c58f1c22SToby Isaac . name   - The label name
8111c58f1c22SToby Isaac - output - The flag for output
8112c58f1c22SToby Isaac 
8113c58f1c22SToby Isaac   Level: developer
8114c58f1c22SToby Isaac 
8115c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8116c58f1c22SToby Isaac @*/
8117c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
8118c58f1c22SToby Isaac {
81195d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8120d67d17b1SMatthew G. Knepley   const char    *lname;
8121c58f1c22SToby Isaac   PetscErrorCode ierr;
8122c58f1c22SToby Isaac 
8123c58f1c22SToby Isaac   PetscFunctionBegin;
8124c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8125534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
8126c58f1c22SToby Isaac   while (next) {
8127c58f1c22SToby Isaac     PetscBool flg;
8128c58f1c22SToby Isaac 
8129d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8130d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8131c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
8132c58f1c22SToby Isaac     next = next->next;
8133c58f1c22SToby Isaac   }
8134c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8135c58f1c22SToby Isaac }
8136c58f1c22SToby Isaac 
8137c58f1c22SToby Isaac /*@
8138c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
8139c58f1c22SToby Isaac 
8140d083f849SBarry Smith   Collective on dmA
8141c58f1c22SToby Isaac 
8142c58f1c22SToby Isaac   Input Parameter:
81435d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
81442e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
81455d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
8146ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
8147c58f1c22SToby Isaac 
8148c58f1c22SToby Isaac   Level: intermediate
8149c58f1c22SToby Isaac 
8150c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
8151c58f1c22SToby Isaac 
81525d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
8153c58f1c22SToby Isaac @*/
81545d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
8155c58f1c22SToby Isaac {
8156c58f1c22SToby Isaac   DMLabel        label, labelNew;
8157c58f1c22SToby Isaac   const char    *name;
8158c58f1c22SToby Isaac   PetscBool      flg;
81595d80c0bfSVaclav Hapla   DMLabelLink    link;
81605d80c0bfSVaclav Hapla   PetscErrorCode ierr;
8161c58f1c22SToby Isaac 
81625d80c0bfSVaclav Hapla   PetscFunctionBegin;
81635d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
81645d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
81655d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
81665d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
81675d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
81685d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
81695d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
81705d80c0bfSVaclav Hapla     label=link->label;
81715d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
81725d80c0bfSVaclav Hapla     if (!all) {
8173c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
8174c58f1c22SToby Isaac       if (flg) continue;
81757d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
81767d5acc75SStefano Zampini       if (flg) continue;
8177ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
8178ba2698f1SMatthew G. Knepley       if (flg) continue;
81795d80c0bfSVaclav Hapla     }
81805d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
8181c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
81825d80c0bfSVaclav Hapla     } else {
81835d80c0bfSVaclav Hapla       labelNew = label;
81845d80c0bfSVaclav Hapla     }
8185c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
81865d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
8187c58f1c22SToby Isaac   }
8188c58f1c22SToby Isaac   PetscFunctionReturn(0);
8189c58f1c22SToby Isaac }
81900fdc7489SMatthew Knepley /*
81910fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
81920fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
81930fdc7489SMatthew Knepley   (label, id) pair in the DM.
81940fdc7489SMatthew Knepley 
81950fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
81960fdc7489SMatthew Knepley   each label.
81970fdc7489SMatthew Knepley */
81980fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
81990fdc7489SMatthew Knepley {
82000fdc7489SMatthew Knepley   DMUniversalLabel ul;
82010fdc7489SMatthew Knepley   PetscBool       *active;
82020fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
82030fdc7489SMatthew Knepley   PetscErrorCode   ierr;
82040fdc7489SMatthew Knepley 
82050fdc7489SMatthew Knepley   PetscFunctionBegin;
82060fdc7489SMatthew Knepley   ierr = PetscMalloc1(1, &ul);CHKERRQ(ierr);
82070fdc7489SMatthew Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label);CHKERRQ(ierr);
82080fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
82090fdc7489SMatthew Knepley   ierr = PetscCalloc1(Nl, &active);CHKERRQ(ierr);
82100fdc7489SMatthew Knepley   ul->Nl = 0;
82110fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
82120fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
82130fdc7489SMatthew Knepley     const char *name;
82140fdc7489SMatthew Knepley 
82150fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
82160fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "depth", 6, &isdepth);CHKERRQ(ierr);
82170fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "celltype", 9, &iscelltype);CHKERRQ(ierr);
82180fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
82190fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
82200fdc7489SMatthew Knepley   }
82210fdc7489SMatthew Knepley   ierr = PetscCalloc5(ul->Nl, &ul->names, ul->Nl, &ul->indices, ul->Nl+1, &ul->offsets, ul->Nl+1, &ul->bits, ul->Nl, &ul->masks);CHKERRQ(ierr);
82220fdc7489SMatthew Knepley   ul->Nv = 0;
82230fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
82240fdc7489SMatthew Knepley     DMLabel     label;
82250fdc7489SMatthew Knepley     PetscInt    nv;
82260fdc7489SMatthew Knepley     const char *name;
82270fdc7489SMatthew Knepley 
82280fdc7489SMatthew Knepley     if (!active[l]) continue;
82290fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
82300fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
82310fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
82320fdc7489SMatthew Knepley     ierr = PetscStrallocpy(name, &ul->names[m]);CHKERRQ(ierr);
82330fdc7489SMatthew Knepley     ul->indices[m]   = l;
82340fdc7489SMatthew Knepley     ul->Nv          += nv;
82350fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
82360fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
82370fdc7489SMatthew Knepley     ++m;
82380fdc7489SMatthew Knepley   }
82390fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
82400fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
82410fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
82420fdc7489SMatthew Knepley   }
82430fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
82440fdc7489SMatthew Knepley     PetscInt b;
82450fdc7489SMatthew Knepley 
82460fdc7489SMatthew Knepley     ul->masks[l] = 0;
82470fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
82480fdc7489SMatthew Knepley   }
82490fdc7489SMatthew Knepley   ierr = PetscMalloc1(ul->Nv, &ul->values);CHKERRQ(ierr);
82500fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
82510fdc7489SMatthew Knepley     DMLabel         label;
82520fdc7489SMatthew Knepley     IS              valueIS;
82530fdc7489SMatthew Knepley     const PetscInt *varr;
82540fdc7489SMatthew Knepley     PetscInt        nv, v;
82550fdc7489SMatthew Knepley 
82560fdc7489SMatthew Knepley     if (!active[l]) continue;
82570fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
82580fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
82590fdc7489SMatthew Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
82600fdc7489SMatthew Knepley     ierr = ISGetIndices(valueIS, &varr);CHKERRQ(ierr);
82610fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
82620fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
82630fdc7489SMatthew Knepley     }
82640fdc7489SMatthew Knepley     ierr = ISRestoreIndices(valueIS, &varr);CHKERRQ(ierr);
82650fdc7489SMatthew Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
82660fdc7489SMatthew Knepley     ierr = PetscSortInt(nv, &ul->values[ul->offsets[m]]);CHKERRQ(ierr);
82670fdc7489SMatthew Knepley     ++m;
82680fdc7489SMatthew Knepley   }
82690fdc7489SMatthew Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
82700fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
82710fdc7489SMatthew Knepley     PetscInt  uval = 0;
82720fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
82730fdc7489SMatthew Knepley 
82740fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
82750fdc7489SMatthew Knepley       DMLabel  label;
82760649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
82770fdc7489SMatthew Knepley 
82780fdc7489SMatthew Knepley       if (!active[l]) continue;
82790fdc7489SMatthew Knepley       ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
82800fdc7489SMatthew Knepley       ierr = DMLabelGetValue(label, p, &val);CHKERRQ(ierr);
82810fdc7489SMatthew Knepley       ierr = DMLabelGetDefaultValue(label, &defval);CHKERRQ(ierr);
82820fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
82830649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
82840fdc7489SMatthew Knepley       marked = PETSC_TRUE;
82850fdc7489SMatthew Knepley       ierr = PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc);CHKERRQ(ierr);
82860fdc7489SMatthew Knepley       if (loc < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %D not found in compression array", val);
82870fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
82880fdc7489SMatthew Knepley       ++m;
82890fdc7489SMatthew Knepley     }
82900fdc7489SMatthew Knepley     if (marked) {ierr = DMLabelSetValue(ul->label, p, uval);CHKERRQ(ierr);}
82910fdc7489SMatthew Knepley   }
82920fdc7489SMatthew Knepley   ierr = PetscFree(active);CHKERRQ(ierr);
82930fdc7489SMatthew Knepley   *universal = ul;
82940fdc7489SMatthew Knepley   PetscFunctionReturn(0);
82950fdc7489SMatthew Knepley }
82960fdc7489SMatthew Knepley 
82970fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
82980fdc7489SMatthew Knepley {
82990fdc7489SMatthew Knepley   PetscInt       l;
83000fdc7489SMatthew Knepley   PetscErrorCode ierr;
83010fdc7489SMatthew Knepley 
83020fdc7489SMatthew Knepley   PetscFunctionBegin;
83030fdc7489SMatthew Knepley   for (l = 0; l < (*universal)->Nl; ++l) {ierr = PetscFree((*universal)->names[l]);CHKERRQ(ierr);}
83040fdc7489SMatthew Knepley   ierr = DMLabelDestroy(&(*universal)->label);CHKERRQ(ierr);
83050fdc7489SMatthew Knepley   ierr = PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks);CHKERRQ(ierr);
83060fdc7489SMatthew Knepley   ierr = PetscFree((*universal)->values);CHKERRQ(ierr);
83070fdc7489SMatthew Knepley   ierr = PetscFree(*universal);CHKERRQ(ierr);
83080fdc7489SMatthew Knepley   *universal = NULL;
83090fdc7489SMatthew Knepley   PetscFunctionReturn(0);
83100fdc7489SMatthew Knepley }
83110fdc7489SMatthew Knepley 
83120fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
83130fdc7489SMatthew Knepley {
83140fdc7489SMatthew Knepley   PetscFunctionBegin;
83150fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
83160fdc7489SMatthew Knepley   *ulabel = ul->label;
83170fdc7489SMatthew Knepley   PetscFunctionReturn(0);
83180fdc7489SMatthew Knepley }
83190fdc7489SMatthew Knepley 
83200fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
83210fdc7489SMatthew Knepley {
83220fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
83230fdc7489SMatthew Knepley   PetscErrorCode ierr;
83240fdc7489SMatthew Knepley 
83250fdc7489SMatthew Knepley   PetscFunctionBegin;
8326064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
83270fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
83280fdc7489SMatthew Knepley     if (preserveOrder) {ierr = DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]);CHKERRQ(ierr);}
83290fdc7489SMatthew Knepley     else               {ierr = DMCreateLabel(dm, ul->names[l]);CHKERRQ(ierr);}
83300fdc7489SMatthew Knepley   }
83310fdc7489SMatthew Knepley   if (preserveOrder) {
83320fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
83330fdc7489SMatthew Knepley       const char *name;
83340fdc7489SMatthew Knepley       PetscBool   match;
83350fdc7489SMatthew Knepley 
83360fdc7489SMatthew Knepley       ierr = DMGetLabelName(dm, ul->indices[l], &name);CHKERRQ(ierr);
83370fdc7489SMatthew Knepley       ierr = PetscStrcmp(name, ul->names[l], &match);CHKERRQ(ierr);
83380fdc7489SMatthew Knepley       if (!match) SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Label %D name %s does not match new name %s", l, name, ul->names[l]);
83390fdc7489SMatthew Knepley     }
83400fdc7489SMatthew Knepley   }
83410fdc7489SMatthew Knepley   PetscFunctionReturn(0);
83420fdc7489SMatthew Knepley }
83430fdc7489SMatthew Knepley 
83440fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
83450fdc7489SMatthew Knepley {
83460fdc7489SMatthew Knepley   PetscInt       l;
83470fdc7489SMatthew Knepley   PetscErrorCode ierr;
83480fdc7489SMatthew Knepley 
83490fdc7489SMatthew Knepley   PetscFunctionBegin;
83500fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
83510fdc7489SMatthew Knepley     DMLabel  label;
83520fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
83530fdc7489SMatthew Knepley 
83540fdc7489SMatthew Knepley     if (lval) {
83550fdc7489SMatthew Knepley       if (useIndex) {ierr = DMGetLabelByNum(dm, ul->indices[l], &label);CHKERRQ(ierr);}
83560fdc7489SMatthew Knepley       else          {ierr = DMGetLabel(dm, ul->names[l], &label);CHKERRQ(ierr);}
83570fdc7489SMatthew Knepley       ierr = DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]);CHKERRQ(ierr);
83580fdc7489SMatthew Knepley     }
83590fdc7489SMatthew Knepley   }
83600fdc7489SMatthew Knepley   PetscFunctionReturn(0);
83610fdc7489SMatthew Knepley }
8362a8fb8f29SToby Isaac 
8363a8fb8f29SToby Isaac /*@
8364a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
8365a8fb8f29SToby Isaac 
8366a8fb8f29SToby Isaac   Input Parameter:
8367a8fb8f29SToby Isaac . dm - The DM object
8368a8fb8f29SToby Isaac 
8369a8fb8f29SToby Isaac   Output Parameter:
8370a8fb8f29SToby Isaac . cdm - The coarse DM
8371a8fb8f29SToby Isaac 
8372a8fb8f29SToby Isaac   Level: intermediate
8373a8fb8f29SToby Isaac 
8374a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
8375a8fb8f29SToby Isaac @*/
8376a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
8377a8fb8f29SToby Isaac {
8378a8fb8f29SToby Isaac   PetscFunctionBegin;
8379a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8380a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
8381a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
8382a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8383a8fb8f29SToby Isaac }
8384a8fb8f29SToby Isaac 
8385a8fb8f29SToby Isaac /*@
8386a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
8387a8fb8f29SToby Isaac 
8388a8fb8f29SToby Isaac   Input Parameters:
8389a8fb8f29SToby Isaac + dm - The DM object
8390a8fb8f29SToby Isaac - cdm - The coarse DM
8391a8fb8f29SToby Isaac 
8392a8fb8f29SToby Isaac   Level: intermediate
8393a8fb8f29SToby Isaac 
8394a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
8395a8fb8f29SToby Isaac @*/
8396a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
8397a8fb8f29SToby Isaac {
8398a8fb8f29SToby Isaac   PetscErrorCode ierr;
8399a8fb8f29SToby Isaac 
8400a8fb8f29SToby Isaac   PetscFunctionBegin;
8401a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8402a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
8403a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
8404a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
8405a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
8406a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8407a8fb8f29SToby Isaac }
8408a8fb8f29SToby Isaac 
840988bdff64SToby Isaac /*@
841088bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
841188bdff64SToby Isaac 
841288bdff64SToby Isaac   Input Parameter:
841388bdff64SToby Isaac . dm - The DM object
841488bdff64SToby Isaac 
841588bdff64SToby Isaac   Output Parameter:
841688bdff64SToby Isaac . fdm - The fine DM
841788bdff64SToby Isaac 
841888bdff64SToby Isaac   Level: intermediate
841988bdff64SToby Isaac 
842088bdff64SToby Isaac .seealso: DMSetFineDM()
842188bdff64SToby Isaac @*/
842288bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
842388bdff64SToby Isaac {
842488bdff64SToby Isaac   PetscFunctionBegin;
842588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
842688bdff64SToby Isaac   PetscValidPointer(fdm, 2);
842788bdff64SToby Isaac   *fdm = dm->fineMesh;
842888bdff64SToby Isaac   PetscFunctionReturn(0);
842988bdff64SToby Isaac }
843088bdff64SToby Isaac 
843188bdff64SToby Isaac /*@
843288bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
843388bdff64SToby Isaac 
843488bdff64SToby Isaac   Input Parameters:
843588bdff64SToby Isaac + dm - The DM object
843688bdff64SToby Isaac - fdm - The fine DM
843788bdff64SToby Isaac 
843888bdff64SToby Isaac   Level: intermediate
843988bdff64SToby Isaac 
844088bdff64SToby Isaac .seealso: DMGetFineDM()
844188bdff64SToby Isaac @*/
844288bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
844388bdff64SToby Isaac {
844488bdff64SToby Isaac   PetscErrorCode ierr;
844588bdff64SToby Isaac 
844688bdff64SToby Isaac   PetscFunctionBegin;
844788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
844888bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
844988bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
845088bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
845188bdff64SToby Isaac   dm->fineMesh = fdm;
845288bdff64SToby Isaac   PetscFunctionReturn(0);
845388bdff64SToby Isaac }
845488bdff64SToby Isaac 
8455a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8456a6ba4734SToby Isaac 
8457a6ba4734SToby Isaac /*@C
8458a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8459a6ba4734SToby Isaac 
8460783e2ec8SMatthew G. Knepley   Collective on dm
8461783e2ec8SMatthew G. Knepley 
8462a6ba4734SToby Isaac   Input Parameters:
84634c258f51SMatthew G. Knepley + dm       - The DM, with a PetscDS that matches the problem being constrained
8464f971fd6bSMatthew G. Knepley . type     - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8465a6ba4734SToby Isaac . name     - The BC name
846645480ffeSMatthew G. Knepley . label    - The label defining constrained points
846745480ffeSMatthew G. Knepley . Nv       - The number of DMLabel values for constrained points
846845480ffeSMatthew G. Knepley . values   - An array of values for constrained points
8469a6ba4734SToby Isaac . field    - The field to constrain
847045480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
8471a6ba4734SToby Isaac . comps    - An array of constrained component numbers
8472a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
847356cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
8474a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
8475a6ba4734SToby Isaac 
847645480ffeSMatthew G. Knepley   Output Parameter:
847745480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
847845480ffeSMatthew G. Knepley 
8479a6ba4734SToby Isaac   Options Database Keys:
8480a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8481a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8482a6ba4734SToby Isaac 
848356cf3b9cSMatthew G. Knepley   Note:
848456cf3b9cSMatthew G. Knepley   Both bcFunc abd bcFunc_t will depend on the boundary condition type. If the type if DM_BC_ESSENTIAL, Then the calling sequence is:
848556cf3b9cSMatthew G. Knepley 
848656cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
848756cf3b9cSMatthew G. Knepley 
848856cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
848956cf3b9cSMatthew G. Knepley 
849056cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
849156cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
849256cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
849356cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
849456cf3b9cSMatthew G. Knepley 
849556cf3b9cSMatthew G. Knepley + dim - the spatial dimension
849656cf3b9cSMatthew G. Knepley . Nf - the number of fields
849756cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
849856cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
849956cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
850056cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
850156cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
850256cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
850356cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
850456cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
850556cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
850656cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
850756cf3b9cSMatthew G. Knepley . t - current time
850856cf3b9cSMatthew G. Knepley . x - coordinates of the current point
850956cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
851056cf3b9cSMatthew G. Knepley . constants - constant parameters
851156cf3b9cSMatthew G. Knepley - bcval - output values at the current point
851256cf3b9cSMatthew G. Knepley 
8513a6ba4734SToby Isaac   Level: developer
8514a6ba4734SToby Isaac 
851545480ffeSMatthew G. Knepley .seealso: DSGetBoundary(), PetscDSAddBoundary()
8516a6ba4734SToby Isaac @*/
851745480ffeSMatthew G. Knepley PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], DMLabel label, PetscInt Nv, const PetscInt values[], PetscInt field, PetscInt Nc, const PetscInt comps[], void (*bcFunc)(void), void (*bcFunc_t)(void), void *ctx, PetscInt *bd)
8518a6ba4734SToby Isaac {
8519e5e52638SMatthew G. Knepley   PetscDS        ds;
8520a6ba4734SToby Isaac   PetscErrorCode ierr;
8521a6ba4734SToby Isaac 
8522a6ba4734SToby Isaac   PetscFunctionBegin;
8523a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8524783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
852545480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
852645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
852745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
852845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
8529e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
853045480ffeSMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, label);CHKERRQ(ierr);
853145480ffeSMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd);CHKERRQ(ierr);
8532a6ba4734SToby Isaac   PetscFunctionReturn(0);
8533a6ba4734SToby Isaac }
8534a6ba4734SToby Isaac 
853545480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8536e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8537e6f8dbb6SToby Isaac {
8538e5e52638SMatthew G. Knepley   PetscDS        ds;
8539dff059c6SToby Isaac   DMBoundary    *lastnext;
8540e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8541e6f8dbb6SToby Isaac   PetscErrorCode ierr;
8542e6f8dbb6SToby Isaac 
8543e6f8dbb6SToby Isaac   PetscFunctionBegin;
8544e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8545e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
854647a1f5adSToby Isaac   if (dm->boundary) {
854747a1f5adSToby Isaac     DMBoundary next = dm->boundary;
854847a1f5adSToby Isaac 
854947a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
855047a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
855147a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
855247a1f5adSToby Isaac     while (next) {
855347a1f5adSToby Isaac       DMBoundary b = next;
855447a1f5adSToby Isaac 
855547a1f5adSToby Isaac       next = b->next;
855647a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
8557a6ba4734SToby Isaac     }
855847a1f5adSToby Isaac     dm->boundary = NULL;
8559a6ba4734SToby Isaac   }
856047a1f5adSToby Isaac 
8561dff059c6SToby Isaac   lastnext = &(dm->boundary);
8562e6f8dbb6SToby Isaac   while (dsbound) {
8563e6f8dbb6SToby Isaac     DMBoundary dmbound;
8564e6f8dbb6SToby Isaac 
8565e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
8566e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
856745480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
856847a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8569dff059c6SToby Isaac     *lastnext = dmbound;
8570dff059c6SToby Isaac     lastnext = &(dmbound->next);
8571dff059c6SToby Isaac     dsbound = dsbound->next;
8572a6ba4734SToby Isaac   }
8573a6ba4734SToby Isaac   PetscFunctionReturn(0);
8574a6ba4734SToby Isaac }
8575a6ba4734SToby Isaac 
8576a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8577a6ba4734SToby Isaac {
8578b95f2879SToby Isaac   DMBoundary     b;
8579a6ba4734SToby Isaac   PetscErrorCode ierr;
8580a6ba4734SToby Isaac 
8581a6ba4734SToby Isaac   PetscFunctionBegin;
8582a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8583534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8584a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8585e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
8586b95f2879SToby Isaac   b = dm->boundary;
8587a6ba4734SToby Isaac   while (b && !(*isBd)) {
8588e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8589e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8590a6ba4734SToby Isaac     PetscInt   i;
8591a6ba4734SToby Isaac 
859245480ffeSMatthew G. Knepley     if (label) {
859345480ffeSMatthew G. Knepley       for (i = 0; i < dsb->Nv && !(*isBd); ++i) {ierr = DMLabelStratumHasPoint(label, dsb->values[i], point, isBd);CHKERRQ(ierr);}
8594a6ba4734SToby Isaac     }
8595a6ba4734SToby Isaac     b = b->next;
8596a6ba4734SToby Isaac   }
8597a6ba4734SToby Isaac   PetscFunctionReturn(0);
8598a6ba4734SToby Isaac }
85994d6f44ffSToby Isaac 
86004d6f44ffSToby Isaac /*@C
8601a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8602a6e0b375SMatthew G. Knepley 
8603a6e0b375SMatthew G. Knepley   Collective on DM
86044d6f44ffSToby Isaac 
86054d6f44ffSToby Isaac   Input Parameters:
86064d6f44ffSToby Isaac + dm      - The DM
86070709b2feSToby Isaac . time    - The time
86084d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
86094d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
86104d6f44ffSToby Isaac - mode    - The insertion mode for values
86114d6f44ffSToby Isaac 
86124d6f44ffSToby Isaac   Output Parameter:
86134d6f44ffSToby Isaac . X - vector
86144d6f44ffSToby Isaac 
86154d6f44ffSToby Isaac    Calling sequence of func:
86160709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
86174d6f44ffSToby Isaac 
86184d6f44ffSToby Isaac +  dim - The spatial dimension
86198ec8862eSJed Brown .  time - The time at which to sample
86204d6f44ffSToby Isaac .  x   - The coordinates
86214d6f44ffSToby Isaac .  Nf  - The number of fields
86224d6f44ffSToby Isaac .  u   - The output field values
86234d6f44ffSToby Isaac -  ctx - optional user-defined function context
86244d6f44ffSToby Isaac 
86254d6f44ffSToby Isaac   Level: developer
86264d6f44ffSToby Isaac 
8627a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
86284d6f44ffSToby Isaac @*/
86290709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
86304d6f44ffSToby Isaac {
86314d6f44ffSToby Isaac   Vec            localX;
86324d6f44ffSToby Isaac   PetscErrorCode ierr;
86334d6f44ffSToby Isaac 
86344d6f44ffSToby Isaac   PetscFunctionBegin;
86354d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86364d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
86370709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
86384d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
86394d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
86404d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
86414d6f44ffSToby Isaac   PetscFunctionReturn(0);
86424d6f44ffSToby Isaac }
86434d6f44ffSToby Isaac 
8644a6e0b375SMatthew G. Knepley /*@C
8645a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8646a6e0b375SMatthew G. Knepley 
8647a6e0b375SMatthew G. Knepley   Not collective
8648a6e0b375SMatthew G. Knepley 
8649a6e0b375SMatthew G. Knepley   Input Parameters:
8650a6e0b375SMatthew G. Knepley + dm      - The DM
8651a6e0b375SMatthew G. Knepley . time    - The time
8652a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8653a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8654a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8655a6e0b375SMatthew G. Knepley 
8656a6e0b375SMatthew G. Knepley   Output Parameter:
8657a6e0b375SMatthew G. Knepley . localX - vector
8658a6e0b375SMatthew G. Knepley 
8659a6e0b375SMatthew G. Knepley    Calling sequence of func:
8660a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8661a6e0b375SMatthew G. Knepley 
8662a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8663a6e0b375SMatthew G. Knepley .  x   - The coordinates
8664a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8665a6e0b375SMatthew G. Knepley .  u   - The output field values
8666a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8667a6e0b375SMatthew G. Knepley 
8668a6e0b375SMatthew G. Knepley   Level: developer
8669a6e0b375SMatthew G. Knepley 
8670a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8671a6e0b375SMatthew G. Knepley @*/
86720709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
86734d6f44ffSToby Isaac {
86744d6f44ffSToby Isaac   PetscErrorCode ierr;
86754d6f44ffSToby Isaac 
86764d6f44ffSToby Isaac   PetscFunctionBegin;
86774d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8678064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
86790918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
86800709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
86814d6f44ffSToby Isaac   PetscFunctionReturn(0);
86824d6f44ffSToby Isaac }
86834d6f44ffSToby Isaac 
8684a6e0b375SMatthew G. Knepley /*@C
8685a6e0b375SMatthew 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.
8686a6e0b375SMatthew G. Knepley 
8687a6e0b375SMatthew G. Knepley   Collective on DM
8688a6e0b375SMatthew G. Knepley 
8689a6e0b375SMatthew G. Knepley   Input Parameters:
8690a6e0b375SMatthew G. Knepley + dm      - The DM
8691a6e0b375SMatthew G. Knepley . time    - The time
8692a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8693a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8694a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8695a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8696a6e0b375SMatthew G. Knepley 
8697a6e0b375SMatthew G. Knepley   Output Parameter:
8698a6e0b375SMatthew G. Knepley . X - vector
8699a6e0b375SMatthew G. Knepley 
8700a6e0b375SMatthew G. Knepley    Calling sequence of func:
8701a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8702a6e0b375SMatthew G. Knepley 
8703a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8704a6e0b375SMatthew G. Knepley .  x   - The coordinates
8705a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8706a6e0b375SMatthew G. Knepley .  u   - The output field values
8707a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8708a6e0b375SMatthew G. Knepley 
8709a6e0b375SMatthew G. Knepley   Level: developer
8710a6e0b375SMatthew G. Knepley 
8711a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8712a6e0b375SMatthew G. Knepley @*/
87132c53366bSMatthew 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)
87142c53366bSMatthew G. Knepley {
87152c53366bSMatthew G. Knepley   Vec            localX;
87162c53366bSMatthew G. Knepley   PetscErrorCode ierr;
87172c53366bSMatthew G. Knepley 
87182c53366bSMatthew G. Knepley   PetscFunctionBegin;
87192c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87202c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
87212c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
87222c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
87232c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
87242c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
87252c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
87262c53366bSMatthew G. Knepley }
87272c53366bSMatthew G. Knepley 
8728a6e0b375SMatthew G. Knepley /*@C
8729a6e0b375SMatthew 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.
8730a6e0b375SMatthew G. Knepley 
8731a6e0b375SMatthew G. Knepley   Not collective
8732a6e0b375SMatthew G. Knepley 
8733a6e0b375SMatthew G. Knepley   Input Parameters:
8734a6e0b375SMatthew G. Knepley + dm      - The DM
8735a6e0b375SMatthew G. Knepley . time    - The time
8736a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8737a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8738a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8739a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8740a6e0b375SMatthew G. Knepley 
8741a6e0b375SMatthew G. Knepley   Output Parameter:
8742a6e0b375SMatthew G. Knepley . localX - vector
8743a6e0b375SMatthew G. Knepley 
8744a6e0b375SMatthew G. Knepley    Calling sequence of func:
8745a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8746a6e0b375SMatthew G. Knepley 
8747a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8748a6e0b375SMatthew G. Knepley .  x   - The coordinates
8749a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8750a6e0b375SMatthew G. Knepley .  u   - The output field values
8751a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8752a6e0b375SMatthew G. Knepley 
8753a6e0b375SMatthew G. Knepley   Level: developer
8754a6e0b375SMatthew G. Knepley 
8755a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8756a6e0b375SMatthew G. Knepley @*/
87571c531cf8SMatthew 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)
87584d6f44ffSToby Isaac {
87594d6f44ffSToby Isaac   PetscErrorCode ierr;
87604d6f44ffSToby Isaac 
87614d6f44ffSToby Isaac   PetscFunctionBegin;
87624d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8763064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
87640918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
87651c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
87664d6f44ffSToby Isaac   PetscFunctionReturn(0);
87674d6f44ffSToby Isaac }
87682716604bSToby Isaac 
8769a6e0b375SMatthew G. Knepley /*@C
8770a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8771a6e0b375SMatthew G. Knepley 
8772a6e0b375SMatthew G. Knepley   Not collective
8773a6e0b375SMatthew G. Knepley 
8774a6e0b375SMatthew G. Knepley   Input Parameters:
8775a6e0b375SMatthew G. Knepley + dm      - The DM
8776a6e0b375SMatthew G. Knepley . time    - The time
8777a6e0b375SMatthew G. Knepley . localU  - The input field vector
8778a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8779a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8780a6e0b375SMatthew G. Knepley 
8781a6e0b375SMatthew G. Knepley   Output Parameter:
8782a6e0b375SMatthew G. Knepley . localX  - The output vector
8783a6e0b375SMatthew G. Knepley 
8784a6e0b375SMatthew G. Knepley    Calling sequence of func:
8785a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8786a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8787a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8788a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8789a6e0b375SMatthew G. Knepley 
8790a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8791a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8792a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8793a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8794a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8795a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8796a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8797a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8798a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8799a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8800a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8801a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8802a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8803a6e0b375SMatthew G. Knepley .  t            - The current time
8804a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8805a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8806a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8807a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8808a6e0b375SMatthew G. Knepley 
8809a6e0b375SMatthew 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.
8810a6e0b375SMatthew 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
8811a6e0b375SMatthew 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
8812a6e0b375SMatthew 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.
8813a6e0b375SMatthew G. Knepley 
8814a6e0b375SMatthew G. Knepley   Level: intermediate
8815a6e0b375SMatthew G. Knepley 
8816a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8817a6e0b375SMatthew G. Knepley @*/
88188c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
88198c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
88208c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
88218c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8822191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
88238c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
88248c6c5593SMatthew G. Knepley {
88258c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
88268c6c5593SMatthew G. Knepley 
88278c6c5593SMatthew G. Knepley   PetscFunctionBegin;
88288c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
88298c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
88308c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
88310918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
88328c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
88338c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
88348c6c5593SMatthew G. Knepley }
88358c6c5593SMatthew G. Knepley 
8836a6e0b375SMatthew G. Knepley /*@C
8837a6e0b375SMatthew 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.
8838a6e0b375SMatthew G. Knepley 
8839a6e0b375SMatthew G. Knepley   Not collective
8840a6e0b375SMatthew G. Knepley 
8841a6e0b375SMatthew G. Knepley   Input Parameters:
8842a6e0b375SMatthew G. Knepley + dm      - The DM
8843a6e0b375SMatthew G. Knepley . time    - The time
8844a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
8845a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8846a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8847a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8848a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8849a6e0b375SMatthew G. Knepley . localU  - The input field vector
8850a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8851a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8852a6e0b375SMatthew G. Knepley 
8853a6e0b375SMatthew G. Knepley   Output Parameter:
8854a6e0b375SMatthew G. Knepley . localX  - The output vector
8855a6e0b375SMatthew G. Knepley 
8856a6e0b375SMatthew G. Knepley    Calling sequence of func:
8857a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8858a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8859a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8860a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8861a6e0b375SMatthew G. Knepley 
8862a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8863a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8864a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8865a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8866a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8867a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8868a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8869a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8870a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8871a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8872a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8873a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8874a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8875a6e0b375SMatthew G. Knepley .  t            - The current time
8876a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8877a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8878a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8879a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8880a6e0b375SMatthew G. Knepley 
8881a6e0b375SMatthew 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.
8882a6e0b375SMatthew 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
8883a6e0b375SMatthew 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
8884a6e0b375SMatthew 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.
8885a6e0b375SMatthew G. Knepley 
8886a6e0b375SMatthew G. Knepley   Level: intermediate
8887a6e0b375SMatthew G. Knepley 
8888a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8889a6e0b375SMatthew G. Knepley @*/
88901c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
88918c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
88928c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
88938c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8894191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
88958c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
88968c6c5593SMatthew G. Knepley {
88978c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
88988c6c5593SMatthew G. Knepley 
88998c6c5593SMatthew G. Knepley   PetscFunctionBegin;
89008c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8901064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
8902064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
8903ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
89041c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
89058c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
89068c6c5593SMatthew G. Knepley }
89078c6c5593SMatthew G. Knepley 
89082716604bSToby Isaac /*@C
8909ece3a9fcSMatthew 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.
8910ece3a9fcSMatthew G. Knepley 
8911ece3a9fcSMatthew G. Knepley   Not collective
8912ece3a9fcSMatthew G. Knepley 
8913ece3a9fcSMatthew G. Knepley   Input Parameters:
8914ece3a9fcSMatthew G. Knepley + dm      - The DM
8915ece3a9fcSMatthew G. Knepley . time    - The time
8916ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
8917ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8918ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8919ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8920ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8921ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8922ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8923ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8924ece3a9fcSMatthew G. Knepley 
8925ece3a9fcSMatthew G. Knepley   Output Parameter:
8926ece3a9fcSMatthew G. Knepley . localX  - The output vector
8927ece3a9fcSMatthew G. Knepley 
8928ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8929ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8930ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8931ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8932ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8933ece3a9fcSMatthew G. Knepley 
8934ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8935ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8936ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8937ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8938ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8939ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8940ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8941ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8942ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8943ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8944ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8945ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8946ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8947ece3a9fcSMatthew G. Knepley .  t            - The current time
8948ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8949ece3a9fcSMatthew G. Knepley .  n            - The face normal
8950ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8951ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8952ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8953ece3a9fcSMatthew G. Knepley 
8954ece3a9fcSMatthew G. Knepley   Note:
8955ece3a9fcSMatthew 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.
8956ece3a9fcSMatthew 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
8957ece3a9fcSMatthew 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
8958ece3a9fcSMatthew 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.
8959ece3a9fcSMatthew G. Knepley 
8960ece3a9fcSMatthew G. Knepley   Level: intermediate
8961ece3a9fcSMatthew G. Knepley 
8962ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8963ece3a9fcSMatthew G. Knepley @*/
8964ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
8965ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
8966ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8967ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8968ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
8969ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
8970ece3a9fcSMatthew G. Knepley {
8971ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
8972ece3a9fcSMatthew G. Knepley 
8973ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8974ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8975064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
8976064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
8977ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
8978ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
8979ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8980ece3a9fcSMatthew G. Knepley }
8981ece3a9fcSMatthew G. Knepley 
8982ece3a9fcSMatthew G. Knepley /*@C
89832716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
89842716604bSToby Isaac 
89852716604bSToby Isaac   Input Parameters:
89862716604bSToby Isaac + dm    - The DM
89870709b2feSToby Isaac . time  - The time
89882716604bSToby Isaac . funcs - The functions to evaluate for each field component
89892716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8990574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
89912716604bSToby Isaac 
89922716604bSToby Isaac   Output Parameter:
89932716604bSToby Isaac . diff - The diff ||u - u_h||_2
89942716604bSToby Isaac 
89952716604bSToby Isaac   Level: developer
89962716604bSToby Isaac 
89971189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
89982716604bSToby Isaac @*/
89990709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
90002716604bSToby Isaac {
90012716604bSToby Isaac   PetscErrorCode ierr;
90022716604bSToby Isaac 
90032716604bSToby Isaac   PetscFunctionBegin;
90042716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9005b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
90060918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
90070709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
90082716604bSToby Isaac   PetscFunctionReturn(0);
90092716604bSToby Isaac }
9010b698f381SToby Isaac 
9011b698f381SToby Isaac /*@C
9012b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
9013b698f381SToby Isaac 
9014d083f849SBarry Smith   Collective on dm
9015d083f849SBarry Smith 
9016b698f381SToby Isaac   Input Parameters:
9017b698f381SToby Isaac + dm    - The DM
9018b698f381SToby Isaac , time  - The time
9019b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
9020b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9021574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
9022b698f381SToby Isaac - n     - The vector to project along
9023b698f381SToby Isaac 
9024b698f381SToby Isaac   Output Parameter:
9025b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
9026b698f381SToby Isaac 
9027b698f381SToby Isaac   Level: developer
9028b698f381SToby Isaac 
9029b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
9030b698f381SToby Isaac @*/
9031b698f381SToby 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)
9032b698f381SToby Isaac {
9033b698f381SToby Isaac   PetscErrorCode ierr;
9034b698f381SToby Isaac 
9035b698f381SToby Isaac   PetscFunctionBegin;
9036b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9037b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
9038b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
9039b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
9040b698f381SToby Isaac   PetscFunctionReturn(0);
9041b698f381SToby Isaac }
9042b698f381SToby Isaac 
90432a16baeaSToby Isaac /*@C
90442a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
90452a16baeaSToby Isaac 
9046d083f849SBarry Smith   Collective on dm
9047d083f849SBarry Smith 
90482a16baeaSToby Isaac   Input Parameters:
90492a16baeaSToby Isaac + dm    - The DM
90502a16baeaSToby Isaac . time  - The time
90512a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
90522a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9053574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
90542a16baeaSToby Isaac 
90552a16baeaSToby Isaac   Output Parameter:
90562a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
90572a16baeaSToby Isaac 
90582a16baeaSToby Isaac   Level: developer
90592a16baeaSToby Isaac 
90601189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
90612a16baeaSToby Isaac @*/
90621189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
90632a16baeaSToby Isaac {
90642a16baeaSToby Isaac   PetscErrorCode ierr;
90652a16baeaSToby Isaac 
90662a16baeaSToby Isaac   PetscFunctionBegin;
90672a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
90682a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
90690918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
90702a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
90712a16baeaSToby Isaac   PetscFunctionReturn(0);
90722a16baeaSToby Isaac }
90732a16baeaSToby Isaac 
9074df0b854cSToby Isaac /*@C
9075df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
9076cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
9077df0b854cSToby Isaac 
9078df0b854cSToby Isaac   Collective on dm
9079df0b854cSToby Isaac 
9080df0b854cSToby Isaac   Input parameters:
9081df0b854cSToby Isaac + dm - the pre-adaptation DM object
9082a1b0c543SToby Isaac - label - label with the flags
9083df0b854cSToby Isaac 
9084df0b854cSToby Isaac   Output parameters:
90850d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
9086df0b854cSToby Isaac 
9087df0b854cSToby Isaac   Level: intermediate
90880d1cd5e0SMatthew G. Knepley 
90890d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
9090df0b854cSToby Isaac @*/
90910d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
9092df0b854cSToby Isaac {
9093df0b854cSToby Isaac   PetscErrorCode ierr;
9094df0b854cSToby Isaac 
9095df0b854cSToby Isaac   PetscFunctionBegin;
9096df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9097a1b0c543SToby Isaac   PetscValidPointer(label,2);
90980d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
90990d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
91006f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
91010d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
9102a587d139SMark   if (*dmAdapt) {
9103a587d139SMark     (*dmAdapt)->prealloc_only = dm->prealloc_only;  /* maybe this should go .... */
9104a587d139SMark     ierr = PetscFree((*dmAdapt)->vectype);CHKERRQ(ierr);
9105a587d139SMark     ierr = PetscStrallocpy(dm->vectype,(char**)&(*dmAdapt)->vectype);CHKERRQ(ierr);
9106a587d139SMark     ierr = PetscFree((*dmAdapt)->mattype);CHKERRQ(ierr);
9107a587d139SMark     ierr = PetscStrallocpy(dm->mattype,(char**)&(*dmAdapt)->mattype);CHKERRQ(ierr);
9108a587d139SMark   }
91090d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
91100d1cd5e0SMatthew G. Knepley }
91110d1cd5e0SMatthew G. Knepley 
91120d1cd5e0SMatthew G. Knepley /*@C
91130d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
91140d1cd5e0SMatthew G. Knepley 
91150d1cd5e0SMatthew G. Knepley   Input Parameters:
91160d1cd5e0SMatthew G. Knepley + dm - The DM object
91170d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
91186f25b0d8SLisandro 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_".
91190d1cd5e0SMatthew G. Knepley 
91200d1cd5e0SMatthew G. Knepley   Output Parameter:
91210d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
91220d1cd5e0SMatthew G. Knepley 
91230d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
91240d1cd5e0SMatthew G. Knepley 
91250d1cd5e0SMatthew G. Knepley   Level: advanced
91260d1cd5e0SMatthew G. Knepley 
91270d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
91280d1cd5e0SMatthew G. Knepley @*/
91290d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
91300d1cd5e0SMatthew G. Knepley {
91310d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
91320d1cd5e0SMatthew G. Knepley 
91330d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
91340d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91350d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
91360d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
91370d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
91386f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
91396f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
91400d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
9141df0b854cSToby Isaac   PetscFunctionReturn(0);
9142df0b854cSToby Isaac }
9143c4088d22SMatthew G. Knepley 
9144502a2867SDave May /*@C
9145502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
9146502a2867SDave May 
9147502a2867SDave May  Not Collective
9148502a2867SDave May 
9149502a2867SDave May  Input Parameter:
9150502a2867SDave May .  dm    - The DM
9151502a2867SDave May 
91520a19bb7dSprj-  Output Parameters:
91530a19bb7dSprj- +  nranks - the number of neighbours
91540a19bb7dSprj- -  ranks - the neighbors ranks
9155502a2867SDave May 
9156502a2867SDave May  Notes:
9157502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
9158502a2867SDave May 
9159502a2867SDave May  Level: beginner
9160502a2867SDave May 
9161dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
9162502a2867SDave May @*/
9163502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
9164502a2867SDave May {
9165502a2867SDave May   PetscErrorCode ierr;
9166502a2867SDave May 
9167502a2867SDave May   PetscFunctionBegin;
9168502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
91690918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
9170502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
9171502a2867SDave May   PetscFunctionReturn(0);
9172502a2867SDave May }
9173502a2867SDave May 
9174531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
9175531c7667SBarry Smith 
9176531c7667SBarry Smith /*
9177531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
9178531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
9179531c7667SBarry Smith */
9180531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
9181531c7667SBarry Smith {
9182531c7667SBarry Smith   PetscErrorCode ierr;
9183531c7667SBarry Smith 
9184531c7667SBarry Smith   PetscFunctionBegin;
9185531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9186531c7667SBarry Smith     Vec x1local;
9187531c7667SBarry Smith     DM  dm;
9188531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9189531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
9190531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
9191531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9192531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9193531c7667SBarry Smith     x1   = x1local;
9194531c7667SBarry Smith   }
9195531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
9196531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9197531c7667SBarry Smith     DM  dm;
9198531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9199531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
9200531c7667SBarry Smith   }
9201531c7667SBarry Smith   PetscFunctionReturn(0);
9202531c7667SBarry Smith }
9203531c7667SBarry Smith 
9204531c7667SBarry Smith /*@
9205531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
9206531c7667SBarry Smith 
9207531c7667SBarry Smith     Input Parameter:
9208531c7667SBarry Smith .    coloring - the MatFDColoring object
9209531c7667SBarry Smith 
921095452b02SPatrick Sanan     Developer Notes:
921195452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
9212531c7667SBarry Smith 
92131b266c99SBarry Smith     Level: advanced
92141b266c99SBarry Smith 
9215531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
9216531c7667SBarry Smith @*/
9217531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
9218531c7667SBarry Smith {
9219531c7667SBarry Smith   PetscFunctionBegin;
9220531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
9221531c7667SBarry Smith   PetscFunctionReturn(0);
9222531c7667SBarry Smith }
92238320bc6fSPatrick Sanan 
92248320bc6fSPatrick Sanan /*@
92258320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
92268320bc6fSPatrick Sanan 
92278320bc6fSPatrick Sanan     Collective
92288320bc6fSPatrick Sanan 
92298320bc6fSPatrick Sanan     Input Parameters:
9230a5bc1bf3SBarry Smith +    dm1 - the first DM
92318320bc6fSPatrick Sanan -    dm2 - the second DM
92328320bc6fSPatrick Sanan 
92338320bc6fSPatrick Sanan     Output Parameters:
92348320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
92358320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
92368320bc6fSPatrick Sanan 
92378320bc6fSPatrick Sanan     Notes:
92388320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
92393d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
92408320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
92413d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
92423d862458SPatrick Sanan     decomposition, but hold different data.
92438320bc6fSPatrick Sanan 
92448320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
92458320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
92468320bc6fSPatrick Sanan 
92478320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
92488320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
92498320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
92508320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
92518320bc6fSPatrick Sanan 
92528320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
92538320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
92548320bc6fSPatrick Sanan .vb
92558320bc6fSPatrick Sanan   ...
92568320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
92578320bc6fSPatrick Sanan   if (set && compatible)  {
92588320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
92598320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
92603d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
92618320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
92628320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
92638320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
92648320bc6fSPatrick Sanan       }
92658320bc6fSPatrick Sanan     }
92668320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
92678320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
92688320bc6fSPatrick Sanan   } else {
92698320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
92708320bc6fSPatrick Sanan   }
92718320bc6fSPatrick Sanan   ...
92728320bc6fSPatrick Sanan .ve
92738320bc6fSPatrick Sanan 
92748320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
92758320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
92768320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
92778320bc6fSPatrick Sanan     always check the "set" output parameter.
92788320bc6fSPatrick Sanan 
92798320bc6fSPatrick Sanan     A DM is always compatible with itself.
92808320bc6fSPatrick Sanan 
92818320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
92828320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
92838320bc6fSPatrick Sanan     incompatible.
92848320bc6fSPatrick Sanan 
92858320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
92868320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
92878320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
92888320bc6fSPatrick Sanan 
92898320bc6fSPatrick Sanan     Developer Notes:
92903d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
92913d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
9292a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
92938320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
92948320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
92953d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
92963d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
92978320bc6fSPatrick Sanan 
92988320bc6fSPatrick Sanan     Level: advanced
92998320bc6fSPatrick Sanan 
93003d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
93018320bc6fSPatrick Sanan @*/
93028320bc6fSPatrick Sanan 
9303a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
93048320bc6fSPatrick Sanan {
93058320bc6fSPatrick Sanan   PetscErrorCode ierr;
93068320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
93078320bc6fSPatrick Sanan   DMType         type,type2;
93088320bc6fSPatrick Sanan   PetscBool      sameType;
93098320bc6fSPatrick Sanan 
93108320bc6fSPatrick Sanan   PetscFunctionBegin;
9311a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
93128320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
93138320bc6fSPatrick Sanan 
93148320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
9315a5bc1bf3SBarry Smith   if (dm1 == dm2) {
93168320bc6fSPatrick Sanan     *set = PETSC_TRUE;
93178320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
93188320bc6fSPatrick Sanan     PetscFunctionReturn(0);
93198320bc6fSPatrick Sanan   }
93208320bc6fSPatrick Sanan 
93218320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
93228320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
93238320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
93248320bc6fSPatrick Sanan      determined by the implementation-specific logic */
9325ffc4695bSBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRMPI(ierr);
93268320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
93278320bc6fSPatrick Sanan     *set = PETSC_TRUE;
93288320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
93298320bc6fSPatrick Sanan     PetscFunctionReturn(0);
93308320bc6fSPatrick Sanan   }
93318320bc6fSPatrick Sanan 
93328320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
9333a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
9334a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
9335b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
93368320bc6fSPatrick Sanan   }
93378320bc6fSPatrick Sanan 
9338a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
93398320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
9340a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
93418320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
93428320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
93438320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
9344a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
93458320bc6fSPatrick Sanan   } else {
93468320bc6fSPatrick Sanan     *set = PETSC_FALSE;
93478320bc6fSPatrick Sanan   }
93488320bc6fSPatrick Sanan   PetscFunctionReturn(0);
93498320bc6fSPatrick Sanan }
9350c0f0dcc3SMatthew G. Knepley 
9351c0f0dcc3SMatthew G. Knepley /*@C
9352c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
9353c0f0dcc3SMatthew G. Knepley 
9354c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9355c0f0dcc3SMatthew G. Knepley 
9356c0f0dcc3SMatthew G. Knepley   Input Parameters:
9357c0f0dcc3SMatthew G. Knepley + DM - the DM
9358c0f0dcc3SMatthew G. Knepley . f - the monitor function
9359c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
9360c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
9361c0f0dcc3SMatthew G. Knepley 
9362c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9363c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
9364c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
9365c0f0dcc3SMatthew G. Knepley 
9366c0f0dcc3SMatthew G. Knepley   Notes:
9367c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
9368c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
9369c0f0dcc3SMatthew G. Knepley   order in which they were set.
9370c0f0dcc3SMatthew G. Knepley 
9371c0f0dcc3SMatthew G. Knepley   Fortran Notes:
9372c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
9373c0f0dcc3SMatthew G. Knepley 
9374c0f0dcc3SMatthew G. Knepley   Level: intermediate
9375c0f0dcc3SMatthew G. Knepley 
9376c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
9377c0f0dcc3SMatthew G. Knepley @*/
9378c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
9379c0f0dcc3SMatthew G. Knepley {
9380c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9381c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9382c0f0dcc3SMatthew G. Knepley 
9383c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9384c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9385c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9386c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9387c0f0dcc3SMatthew G. Knepley 
9388c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
9389c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9390c0f0dcc3SMatthew G. Knepley   }
9391c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9392c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9393c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9394c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9395c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9396c0f0dcc3SMatthew G. Knepley }
9397c0f0dcc3SMatthew G. Knepley 
9398c0f0dcc3SMatthew G. Knepley /*@
9399c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9400c0f0dcc3SMatthew G. Knepley 
9401c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9402c0f0dcc3SMatthew G. Knepley 
9403c0f0dcc3SMatthew G. Knepley   Input Parameter:
9404c0f0dcc3SMatthew G. Knepley . dm - the DM
9405c0f0dcc3SMatthew G. Knepley 
9406c0f0dcc3SMatthew G. Knepley   Options Database Key:
9407c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9408c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9409c0f0dcc3SMatthew G. Knepley   set via the options database
9410c0f0dcc3SMatthew G. Knepley 
9411c0f0dcc3SMatthew G. Knepley   Notes:
9412c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9413c0f0dcc3SMatthew G. Knepley 
9414c0f0dcc3SMatthew G. Knepley   Level: intermediate
9415c0f0dcc3SMatthew G. Knepley 
9416c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9417c0f0dcc3SMatthew G. Knepley @*/
9418c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9419c0f0dcc3SMatthew G. Knepley {
9420c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9421c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9422c0f0dcc3SMatthew G. Knepley 
9423c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9424c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9425c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9426c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
9427c0f0dcc3SMatthew G. Knepley   }
9428c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9429c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9430c0f0dcc3SMatthew G. Knepley }
9431c0f0dcc3SMatthew G. Knepley 
9432c0f0dcc3SMatthew G. Knepley /*@C
9433c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9434c0f0dcc3SMatthew G. Knepley 
9435c0f0dcc3SMatthew G. Knepley   Collective on DM
9436c0f0dcc3SMatthew G. Knepley 
9437c0f0dcc3SMatthew G. Knepley   Input Parameters:
9438c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9439c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9440c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9441c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9442c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9443c0f0dcc3SMatthew 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
9444c0f0dcc3SMatthew G. Knepley 
9445c0f0dcc3SMatthew G. Knepley   Output Parameter:
9446c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9447c0f0dcc3SMatthew G. Knepley 
9448c0f0dcc3SMatthew G. Knepley   Level: developer
9449c0f0dcc3SMatthew G. Knepley 
9450c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9451c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9452c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9453c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9454c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9455c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9456c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9457c0f0dcc3SMatthew G. Knepley @*/
9458c0f0dcc3SMatthew 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)
9459c0f0dcc3SMatthew G. Knepley {
9460c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9461c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9462c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
9463c0f0dcc3SMatthew G. Knepley 
9464c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9465c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9466c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
9467c0f0dcc3SMatthew G. Knepley   if (*flg) {
9468c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9469c0f0dcc3SMatthew G. Knepley 
9470c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
9471c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
9472c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
9473c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
9474c0f0dcc3SMatthew G. Knepley   }
9475c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9476c0f0dcc3SMatthew G. Knepley }
9477c0f0dcc3SMatthew G. Knepley 
9478c0f0dcc3SMatthew G. Knepley /*@
9479c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9480c0f0dcc3SMatthew G. Knepley 
9481c0f0dcc3SMatthew G. Knepley    Collective on DM
9482c0f0dcc3SMatthew G. Knepley 
9483c0f0dcc3SMatthew G. Knepley    Input Parameters:
9484c0f0dcc3SMatthew G. Knepley .  dm - The DM
9485c0f0dcc3SMatthew G. Knepley 
9486c0f0dcc3SMatthew G. Knepley    Level: developer
9487c0f0dcc3SMatthew G. Knepley 
9488c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9489c0f0dcc3SMatthew G. Knepley @*/
9490c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9491c0f0dcc3SMatthew G. Knepley {
9492c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9493c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9494c0f0dcc3SMatthew G. Knepley 
9495c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9496c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9497c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9498c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9499c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
9500c0f0dcc3SMatthew G. Knepley   }
9501c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9502c0f0dcc3SMatthew G. Knepley }
95032e4af2aeSMatthew G. Knepley 
95042e4af2aeSMatthew G. Knepley /*@
95052e4af2aeSMatthew G. Knepley   DMComputeError - Computes the error assuming the user has given exact solution functions
95062e4af2aeSMatthew G. Knepley 
95072e4af2aeSMatthew G. Knepley   Collective on DM
95082e4af2aeSMatthew G. Knepley 
95092e4af2aeSMatthew G. Knepley   Input Parameters:
95102e4af2aeSMatthew G. Knepley + dm     - The DM
95112e4af2aeSMatthew G. Knepley . sol    - The solution vector
95122e4af2aeSMatthew G. Knepley . errors - An array of length Nf, the number of fields, or NULL for no output
951399c90e12SSatish Balay - errorVec - A Vec pointer, or NULL for no output
95142e4af2aeSMatthew G. Knepley 
95152e4af2aeSMatthew G. Knepley   Output Parameters:
95162e4af2aeSMatthew G. Knepley + errors   - The error in each field
95172e4af2aeSMatthew G. Knepley - errorVec - Creates a vector to hold the cellwise error
95182e4af2aeSMatthew G. Knepley 
95192e4af2aeSMatthew G. Knepley   Note: The exact solutions come from the PetscDS object, and the time comes from DMGetOutputSequenceNumber().
95202e4af2aeSMatthew G. Knepley 
95212e4af2aeSMatthew G. Knepley   Level: developer
95222e4af2aeSMatthew G. Knepley 
95232e4af2aeSMatthew G. Knepley .seealso: DMMonitorSet(), DMGetRegionNumDS(), PetscDSGetExactSolution(), DMGetOutputSequenceNumber()
95242e4af2aeSMatthew G. Knepley @*/
95252e4af2aeSMatthew G. Knepley PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
95262e4af2aeSMatthew G. Knepley {
95272e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
95282e4af2aeSMatthew G. Knepley   void            **ctxs;
95292e4af2aeSMatthew G. Knepley   PetscReal         time;
95302e4af2aeSMatthew G. Knepley   PetscInt          Nf, f, Nds, s;
95312e4af2aeSMatthew G. Knepley   PetscErrorCode    ierr;
95322e4af2aeSMatthew G. Knepley 
95332e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
95342e4af2aeSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
95352e4af2aeSMatthew G. Knepley   ierr = PetscCalloc2(Nf, &exactSol, Nf, &ctxs);CHKERRQ(ierr);
95362e4af2aeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
95372e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
95382e4af2aeSMatthew G. Knepley     PetscDS         ds;
95392e4af2aeSMatthew G. Knepley     DMLabel         label;
95402e4af2aeSMatthew G. Knepley     IS              fieldIS;
95412e4af2aeSMatthew G. Knepley     const PetscInt *fields;
95422e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
95432e4af2aeSMatthew G. Knepley 
95442e4af2aeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
95452e4af2aeSMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
95462e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);}
95472e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
95482e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
95492e4af2aeSMatthew G. Knepley       ierr = PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]);CHKERRQ(ierr);
95502e4af2aeSMatthew G. Knepley     }
95512e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);}
95522e4af2aeSMatthew G. Knepley   }
95532e4af2aeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
95542e4af2aeSMatthew G. Knepley     if (!exactSol[f]) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "DS must contain exact solution functions in order to calculate error, missing for field %D", f);
95552e4af2aeSMatthew G. Knepley   }
95562e4af2aeSMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
95572e4af2aeSMatthew G. Knepley   if (errors) {ierr = DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors);CHKERRQ(ierr);}
95582e4af2aeSMatthew G. Knepley   if (errorVec) {
95592e4af2aeSMatthew G. Knepley     DM             edm;
95602e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
95612e4af2aeSMatthew G. Knepley     PetscBool      simplex;
95622e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
95632e4af2aeSMatthew G. Knepley 
95642e4af2aeSMatthew G. Knepley     ierr = DMClone(dm, &edm);CHKERRQ(ierr);
95652e4af2aeSMatthew G. Knepley     ierr = DMGetDimension(edm, &dim);CHKERRQ(ierr);
95662e4af2aeSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr);
95672e4af2aeSMatthew G. Knepley     ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
95682e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
95692e4af2aeSMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
95702e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
95712e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
95722e4af2aeSMatthew G. Knepley       PetscQuadrature q;
95732e4af2aeSMatthew G. Knepley       const char     *name;
95742e4af2aeSMatthew G. Knepley 
95752e4af2aeSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
95762e4af2aeSMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe);CHKERRQ(ierr);
95772e4af2aeSMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
95782e4af2aeSMatthew G. Knepley       ierr = PetscObjectSetName((PetscObject) efe, name);CHKERRQ(ierr);
95792e4af2aeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
95802e4af2aeSMatthew G. Knepley       ierr = PetscFESetQuadrature(efe, q);CHKERRQ(ierr);
95812e4af2aeSMatthew G. Knepley       ierr = DMSetField(edm, f, NULL, (PetscObject) efe);CHKERRQ(ierr);
95822e4af2aeSMatthew G. Knepley       ierr = PetscFEDestroy(&efe);CHKERRQ(ierr);
95832e4af2aeSMatthew G. Knepley     }
95842e4af2aeSMatthew G. Knepley     ierr = DMCreateDS(edm);CHKERRQ(ierr);
95852e4af2aeSMatthew G. Knepley 
95862e4af2aeSMatthew G. Knepley     ierr = DMCreateGlobalVector(edm, errorVec);
95872e4af2aeSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) *errorVec, "Error");CHKERRQ(ierr);
95882e4af2aeSMatthew G. Knepley     ierr = DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec);CHKERRQ(ierr);
95892e4af2aeSMatthew G. Knepley     ierr = DMDestroy(&edm);CHKERRQ(ierr);
95902e4af2aeSMatthew G. Knepley   }
95912e4af2aeSMatthew G. Knepley   ierr = PetscFree2(exactSol, ctxs);CHKERRQ(ierr);
95922e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
95932e4af2aeSMatthew G. Knepley }
95949a2a23afSMatthew G. Knepley 
95959a2a23afSMatthew G. Knepley /*@
95969a2a23afSMatthew G. Knepley   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this DM
95979a2a23afSMatthew G. Knepley 
95989a2a23afSMatthew G. Knepley   Not collective
95999a2a23afSMatthew G. Knepley 
96009a2a23afSMatthew G. Knepley   Input Parameter:
96019a2a23afSMatthew G. Knepley . dm     - The DM
96029a2a23afSMatthew G. Knepley 
96039a2a23afSMatthew G. Knepley   Output Parameter:
96049a2a23afSMatthew G. Knepley . numAux - The nubmer of auxiliary data vectors
96059a2a23afSMatthew G. Knepley 
96069a2a23afSMatthew G. Knepley   Level: advanced
96079a2a23afSMatthew G. Knepley 
96089a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryLabels(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
96099a2a23afSMatthew G. Knepley @*/
96109a2a23afSMatthew G. Knepley PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
96119a2a23afSMatthew G. Knepley {
96129a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
96139a2a23afSMatthew G. Knepley 
96149a2a23afSMatthew G. Knepley   PetscFunctionBegin;
96159a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
96169a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetSize(dm->auxData, numAux);CHKERRQ(ierr);
96179a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
96189a2a23afSMatthew G. Knepley }
96199a2a23afSMatthew G. Knepley 
96209a2a23afSMatthew G. Knepley /*@
96219a2a23afSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value
96229a2a23afSMatthew G. Knepley 
96239a2a23afSMatthew G. Knepley   Not collective
96249a2a23afSMatthew G. Knepley 
96259a2a23afSMatthew G. Knepley   Input Parameters:
96269a2a23afSMatthew G. Knepley + dm     - The DM
96279a2a23afSMatthew G. Knepley . label  - The DMLabel
96289a2a23afSMatthew G. Knepley - value  - The label value indicating the region
96299a2a23afSMatthew G. Knepley 
96309a2a23afSMatthew G. Knepley   Output Parameter:
96319a2a23afSMatthew G. Knepley . aux    - The Vec holding auxiliary field data
96329a2a23afSMatthew G. Knepley 
96339a2a23afSMatthew G. Knepley   Level: advanced
96349a2a23afSMatthew G. Knepley 
96359a2a23afSMatthew G. Knepley .seealso: DMSetAuxiliaryVec(), DMGetNumAuxiliaryVec()
96369a2a23afSMatthew G. Knepley @*/
96379a2a23afSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, Vec *aux)
96389a2a23afSMatthew G. Knepley {
96399a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
96409a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
96419a2a23afSMatthew G. Knepley 
96429a2a23afSMatthew G. Knepley   PetscFunctionBegin;
96439a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
96449a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
96459a2a23afSMatthew G. Knepley   key.label = label;
96469a2a23afSMatthew G. Knepley   key.value = value;
96479a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGet(dm->auxData, key, aux);CHKERRQ(ierr);
96489a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
96499a2a23afSMatthew G. Knepley }
96509a2a23afSMatthew G. Knepley 
96519a2a23afSMatthew G. Knepley /*@
96529a2a23afSMatthew G. Knepley   DMSetAuxiliaryVec - Set the auxiliary vector for region specified by the given label and value
96539a2a23afSMatthew G. Knepley 
96549a2a23afSMatthew G. Knepley   Not collective
96559a2a23afSMatthew G. Knepley 
96569a2a23afSMatthew G. Knepley   Input Parameters:
96579a2a23afSMatthew G. Knepley + dm     - The DM
96589a2a23afSMatthew G. Knepley . label  - The DMLabel
96599a2a23afSMatthew G. Knepley . value  - The label value indicating the region
96609a2a23afSMatthew G. Knepley - aux    - The Vec holding auxiliary field data
96619a2a23afSMatthew G. Knepley 
96629a2a23afSMatthew G. Knepley   Level: advanced
96639a2a23afSMatthew G. Knepley 
96649a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryVec()
96659a2a23afSMatthew G. Knepley @*/
96669a2a23afSMatthew G. Knepley PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, Vec aux)
96679a2a23afSMatthew G. Knepley {
96689a2a23afSMatthew G. Knepley   Vec             old;
96699a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
96709a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
96719a2a23afSMatthew G. Knepley 
96729a2a23afSMatthew G. Knepley   PetscFunctionBegin;
96739a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
96749a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
96759a2a23afSMatthew G. Knepley   key.label = label;
96769a2a23afSMatthew G. Knepley   key.value = value;
96779a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGet(dm->auxData, key, &old);CHKERRQ(ierr);
96789a2a23afSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) aux);CHKERRQ(ierr);
96799a2a23afSMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) old);CHKERRQ(ierr);
96809a2a23afSMatthew G. Knepley   if (!aux) {ierr = PetscHMapAuxDel(dm->auxData, key);CHKERRQ(ierr);}
96819a2a23afSMatthew G. Knepley   else      {ierr = PetscHMapAuxSet(dm->auxData, key, aux);CHKERRQ(ierr);}
96829a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
96839a2a23afSMatthew G. Knepley }
96849a2a23afSMatthew G. Knepley 
96859a2a23afSMatthew G. Knepley /*@C
96869a2a23afSMatthew G. Knepley   DMGetAuxiliaryLabels - Get the labels and values for all auxiliary vectors in this DM
96879a2a23afSMatthew G. Knepley 
96889a2a23afSMatthew G. Knepley   Not collective
96899a2a23afSMatthew G. Knepley 
96909a2a23afSMatthew G. Knepley   Input Parameter:
96919a2a23afSMatthew G. Knepley . dm      - The DM
96929a2a23afSMatthew G. Knepley 
96939a2a23afSMatthew G. Knepley   Output Parameters:
96949a2a23afSMatthew G. Knepley + labels  - The DMLabels for each Vec
96959a2a23afSMatthew G. Knepley - values  - The label values for each Vec
96969a2a23afSMatthew G. Knepley 
96979a2a23afSMatthew G. Knepley   Note: The arrays passed in must be at least as large as DMGetNumAuxiliaryVec().
96989a2a23afSMatthew G. Knepley 
96999a2a23afSMatthew G. Knepley   Level: advanced
97009a2a23afSMatthew G. Knepley 
97019a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
97029a2a23afSMatthew G. Knepley @*/
97039a2a23afSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[])
97049a2a23afSMatthew G. Knepley {
97059a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
97069a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
97079a2a23afSMatthew G. Knepley   PetscErrorCode   ierr;
97089a2a23afSMatthew G. Knepley 
97099a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97109a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97119a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
97129a2a23afSMatthew G. Knepley   PetscValidPointer(values, 3);
97139a2a23afSMatthew G. Knepley   ierr = DMGetNumAuxiliaryVec(dm, &n);CHKERRQ(ierr);
97149a2a23afSMatthew G. Knepley   ierr = PetscMalloc1(n, &keys);CHKERRQ(ierr);
97159a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetKeys(dm->auxData, &off, keys);CHKERRQ(ierr);
97169a2a23afSMatthew G. Knepley   for (i = 0; i < n; ++i) {labels[i] = keys[i].label; values[i] = keys[i].value;}
97179a2a23afSMatthew G. Knepley   ierr = PetscFree(keys);CHKERRQ(ierr);
97189a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
97199a2a23afSMatthew G. Knepley }
97209a2a23afSMatthew G. Knepley 
97219a2a23afSMatthew G. Knepley /*@
97229a2a23afSMatthew G. Knepley   DMCopyAuxiliaryVec - Copy the auxiliary data to a new DM
97239a2a23afSMatthew G. Knepley 
97249a2a23afSMatthew G. Knepley   Not collective
97259a2a23afSMatthew G. Knepley 
97269a2a23afSMatthew G. Knepley   Input Parameter:
97279a2a23afSMatthew G. Knepley . dm    - The DM
97289a2a23afSMatthew G. Knepley 
97299a2a23afSMatthew G. Knepley   Output Parameter:
97309a2a23afSMatthew G. Knepley . dmNew - The new DM, now with the same auxiliary data
97319a2a23afSMatthew G. Knepley 
97329a2a23afSMatthew G. Knepley   Level: advanced
97339a2a23afSMatthew G. Knepley 
97349a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
97359a2a23afSMatthew G. Knepley @*/
97369a2a23afSMatthew G. Knepley PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
97379a2a23afSMatthew G. Knepley {
97389a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
97399a2a23afSMatthew G. Knepley 
97409a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97419a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97429a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDestroy(&dmNew->auxData);CHKERRQ(ierr);
97439a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData);CHKERRQ(ierr);
97449a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
97459a2a23afSMatthew G. Knepley }
9746