xref: /petsc/src/dm/interface/dm.c (revision 2cbb9b060ba499b906381460b4cdae4d5453cc6f)
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 
10f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
11f918ec44SMatthew G. Knepley #include <petscfeceed.h>
12f918ec44SMatthew G. Knepley #endif
13f918ec44SMatthew G. Knepley 
1400d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
1500d952a4SJed Brown #  include <valgrind/memcheck.h>
1600d952a4SJed Brown #endif
1700d952a4SJed Brown 
18732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
19d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
20557cf195SMatthew 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;
2167a56275SMatthew G Knepley 
22ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_", NULL};
23d1b3049bSMatthew 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};
24da9060c4SMatthew 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};
25*2cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace","keep","fail","DMCopyLabelsMode","DM_COPY_LABELS_", NULL};
2660c22052SBarry Smith 
27a4121054SBarry Smith /*@
28de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
29a4121054SBarry Smith 
30a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
31a4121054SBarry Smith    error when you try to use the vector.
32a4121054SBarry Smith 
33d083f849SBarry Smith   Collective
34a4121054SBarry Smith 
35a4121054SBarry Smith   Input Parameter:
36a4121054SBarry Smith . comm - The communicator for the DM object
37a4121054SBarry Smith 
38a4121054SBarry Smith   Output Parameter:
39a4121054SBarry Smith . dm - The DM object
40a4121054SBarry Smith 
41a4121054SBarry Smith   Level: beginner
42a4121054SBarry Smith 
438472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
44a4121054SBarry Smith @*/
457087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
46a4121054SBarry Smith {
47a4121054SBarry Smith   DM             v;
48e5e52638SMatthew G. Knepley   PetscDS        ds;
49a4121054SBarry Smith   PetscErrorCode ierr;
50a4121054SBarry Smith 
51a4121054SBarry Smith   PetscFunctionBegin;
521411c6eeSJed Brown   PetscValidPointer(dm,2);
530298fd71SBarry Smith   *dm = NULL;
54607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
55a4121054SBarry Smith 
5673107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
57e7c4fc90SDmitry Karpeev 
5849be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
5949be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
600298fd71SBarry Smith   v->ltogmap                  = NULL;
611411c6eeSJed Brown   v->bs                       = 1;
62171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
6388ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
641bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
65c58f1c22SToby Isaac   v->labels                   = NULL;
6634aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
6734aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
68c58f1c22SToby Isaac   v->depthLabel               = NULL;
69ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
701bb6d2a8SBarry Smith   v->localSection             = NULL;
711bb6d2a8SBarry Smith   v->globalSection            = NULL;
72fba222abSToby Isaac   v->defaultConstraintSection = NULL;
73fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
74c6b900c6SMatthew G. Knepley   v->L                        = NULL;
75c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
765dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
779a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7896173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
79435a35e8SMatthew G Knepley   {
80435a35e8SMatthew G Knepley     PetscInt i;
81435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
820298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
83f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
84435a35e8SMatthew G Knepley     }
85435a35e8SMatthew G Knepley   }
8645480ffeSMatthew G. Knepley   ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
87b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
88e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
899a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxCreate(&v->auxData);CHKERRQ(ierr);
9014f150ffSMatthew G. Knepley   v->dmBC = NULL;
91a8fb8f29SToby Isaac   v->coarseMesh = NULL;
92f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
93cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
94c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
95b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
964a7a4c06SLawrence Mitchell 
971411c6eeSJed Brown   *dm = v;
98a4121054SBarry Smith   PetscFunctionReturn(0);
99a4121054SBarry Smith }
100a4121054SBarry Smith 
10138221697SMatthew G. Knepley /*@
10238221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
10338221697SMatthew G. Knepley 
104d083f849SBarry Smith   Collective
10538221697SMatthew G. Knepley 
10638221697SMatthew G. Knepley   Input Parameter:
10738221697SMatthew G. Knepley . dm - The original DM object
10838221697SMatthew G. Knepley 
10938221697SMatthew G. Knepley   Output Parameter:
11038221697SMatthew G. Knepley . newdm  - The new DM object
11138221697SMatthew G. Knepley 
11238221697SMatthew G. Knepley   Level: beginner
11338221697SMatthew G. Knepley 
1141cb8cacdSPatrick Sanan   Notes:
1151cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1161cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1171cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1181bb6d2a8SBarry Smith 
11989706ed2SPatrick Sanan   The clone is considered set up iff the original is.
12089706ed2SPatrick Sanan 
12192cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1221bb6d2a8SBarry Smith 
12338221697SMatthew G. Knepley @*/
12438221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
12538221697SMatthew G. Knepley {
12638221697SMatthew G. Knepley   PetscSF        sf;
12738221697SMatthew G. Knepley   Vec            coords;
12838221697SMatthew G. Knepley   void          *ctx;
129a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
13038221697SMatthew G. Knepley   PetscErrorCode ierr;
13138221697SMatthew G. Knepley 
13238221697SMatthew G. Knepley   PetscFunctionBegin;
13338221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13438221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
13538221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
136*2cbb9b06SVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL);CHKERRQ(ierr);
137ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
138ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
139c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
140a587d139SMark   ierr = PetscFree((*newdm)->vectype);CHKERRQ(ierr);
141a587d139SMark   ierr = PetscStrallocpy(dm->vectype,(char**)&(*newdm)->vectype);CHKERRQ(ierr);
142a587d139SMark   ierr = PetscFree((*newdm)->mattype);CHKERRQ(ierr);
143a587d139SMark   ierr = PetscStrallocpy(dm->mattype,(char**)&(*newdm)->mattype);CHKERRQ(ierr);
1441de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1451de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
14638221697SMatthew G. Knepley   if (dm->ops->clone) {
14738221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
14838221697SMatthew G. Knepley   }
1493f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
15038221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
15138221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
15238221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
15338221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
154be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
155be4c1c3eSMatthew G. Knepley     DM           ncdm;
156be4c1c3eSMatthew G. Knepley     PetscSection cs;
1575a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
158be4c1c3eSMatthew G. Knepley 
15992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
160be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
161ffc4695bSBarry Smith     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
1625a0206caSToby Isaac     if (pEndMax >= 0) {
163be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
16483bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
16592fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
166a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
167be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
168be4c1c3eSMatthew G. Knepley     }
169be4c1c3eSMatthew G. Knepley   }
170a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
171a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
17238221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
17338221697SMatthew G. Knepley   if (coords) {
17438221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
17538221697SMatthew G. Knepley   } else {
17638221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
17738221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
17838221697SMatthew G. Knepley   }
17990b157c4SStefano Zampini   {
18090b157c4SStefano Zampini     PetscBool             isper;
181c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1825dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
18390b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
18490b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
185c6b900c6SMatthew G. Knepley   }
18634aa8a36SMatthew G. Knepley   {
18734aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
18834aa8a36SMatthew G. Knepley 
18934aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
19034aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
19134aa8a36SMatthew G. Knepley   }
19238221697SMatthew G. Knepley   PetscFunctionReturn(0);
19338221697SMatthew G. Knepley }
19438221697SMatthew G. Knepley 
1959a42bb27SBarry Smith /*@C
196564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1979a42bb27SBarry Smith 
198d083f849SBarry Smith    Logically Collective on da
1999a42bb27SBarry Smith 
2009a42bb27SBarry Smith    Input Parameter:
2019a42bb27SBarry Smith +  da - initial distributed array
202e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
2039a42bb27SBarry Smith 
2049a42bb27SBarry Smith    Options Database:
205dd85299cSBarry Smith .   -dm_vec_type ctype
2069a42bb27SBarry Smith 
2079a42bb27SBarry Smith    Level: intermediate
2089a42bb27SBarry Smith 
209a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
2109a42bb27SBarry Smith @*/
21119fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2129a42bb27SBarry Smith {
2139a42bb27SBarry Smith   PetscErrorCode ierr;
2149a42bb27SBarry Smith 
2159a42bb27SBarry Smith   PetscFunctionBegin;
2169a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2179a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
21819fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2199a42bb27SBarry Smith   PetscFunctionReturn(0);
2209a42bb27SBarry Smith }
2219a42bb27SBarry Smith 
222c0dedaeaSBarry Smith /*@C
223c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
224c0dedaeaSBarry Smith 
225d083f849SBarry Smith    Logically Collective on da
226c0dedaeaSBarry Smith 
227c0dedaeaSBarry Smith    Input Parameter:
228c0dedaeaSBarry Smith .  da - initial distributed array
229c0dedaeaSBarry Smith 
230c0dedaeaSBarry Smith    Output Parameter:
231c0dedaeaSBarry Smith .  ctype - the vector type
232c0dedaeaSBarry Smith 
233c0dedaeaSBarry Smith    Level: intermediate
234c0dedaeaSBarry Smith 
235a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
236c0dedaeaSBarry Smith @*/
237c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
238c0dedaeaSBarry Smith {
239c0dedaeaSBarry Smith   PetscFunctionBegin;
240c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
241c0dedaeaSBarry Smith   *ctype = da->vectype;
242c0dedaeaSBarry Smith   PetscFunctionReturn(0);
243c0dedaeaSBarry Smith }
244c0dedaeaSBarry Smith 
2455f1ad066SMatthew G Knepley /*@
24634f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2475f1ad066SMatthew G Knepley 
2485f1ad066SMatthew G Knepley   Not collective
2495f1ad066SMatthew G Knepley 
2505f1ad066SMatthew G Knepley   Input Parameter:
2515f1ad066SMatthew G Knepley . v - The Vec
2525f1ad066SMatthew G Knepley 
2535f1ad066SMatthew G Knepley   Output Parameter:
2545f1ad066SMatthew G Knepley . dm - The DM
2555f1ad066SMatthew G Knepley 
2565f1ad066SMatthew G Knepley   Level: intermediate
2575f1ad066SMatthew G Knepley 
2585f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2595f1ad066SMatthew G Knepley @*/
2605f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2615f1ad066SMatthew G Knepley {
2625f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley   PetscFunctionBegin;
2655f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2665f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2675f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2685f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2695f1ad066SMatthew G Knepley }
2705f1ad066SMatthew G Knepley 
2715f1ad066SMatthew G Knepley /*@
272d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2735f1ad066SMatthew G Knepley 
2745f1ad066SMatthew G Knepley   Not collective
2755f1ad066SMatthew G Knepley 
2765f1ad066SMatthew G Knepley   Input Parameters:
2775f1ad066SMatthew G Knepley + v - The Vec
2785f1ad066SMatthew G Knepley - dm - The DM
2795f1ad066SMatthew G Knepley 
280d9805387SMatthew 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.
281d9805387SMatthew G. Knepley 
2825f1ad066SMatthew G Knepley   Level: intermediate
2835f1ad066SMatthew G Knepley 
2845f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2855f1ad066SMatthew G Knepley @*/
2865f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2875f1ad066SMatthew G Knepley {
2885f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2895f1ad066SMatthew G Knepley 
2905f1ad066SMatthew G Knepley   PetscFunctionBegin;
2915f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
292d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2935f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2945f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2955f1ad066SMatthew G Knepley }
2965f1ad066SMatthew G Knepley 
297521d9a4cSLisandro Dalcin /*@C
2988f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2998f1509bcSBarry Smith 
300d083f849SBarry Smith    Logically Collective on dm
3018f1509bcSBarry Smith 
3028f1509bcSBarry Smith    Input Parameters:
3038f1509bcSBarry Smith +  dm - the DM context
3048f1509bcSBarry Smith -  ctype - the matrix type
3058f1509bcSBarry Smith 
3068f1509bcSBarry Smith    Options Database:
3078f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3088f1509bcSBarry Smith 
3098f1509bcSBarry Smith    Level: intermediate
3108f1509bcSBarry Smith 
3118f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3128f1509bcSBarry Smith           DMGetISColoringType()
3138f1509bcSBarry Smith @*/
3148f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3158f1509bcSBarry Smith {
3168f1509bcSBarry Smith   PetscFunctionBegin;
3178f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3188f1509bcSBarry Smith   dm->coloringtype = ctype;
3198f1509bcSBarry Smith   PetscFunctionReturn(0);
3208f1509bcSBarry Smith }
3218f1509bcSBarry Smith 
3228f1509bcSBarry Smith /*@C
3238f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
324521d9a4cSLisandro Dalcin 
325d083f849SBarry Smith    Logically Collective on dm
326521d9a4cSLisandro Dalcin 
327521d9a4cSLisandro Dalcin    Input Parameter:
3288f1509bcSBarry Smith .  dm - the DM context
3298f1509bcSBarry Smith 
3308f1509bcSBarry Smith    Output Parameter:
3318f1509bcSBarry Smith .  ctype - the matrix type
3328f1509bcSBarry Smith 
3338f1509bcSBarry Smith    Options Database:
3348f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3358f1509bcSBarry Smith 
3368f1509bcSBarry Smith    Level: intermediate
3378f1509bcSBarry Smith 
3388f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3398f1509bcSBarry Smith           DMGetISColoringType()
3408f1509bcSBarry Smith @*/
3418f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3428f1509bcSBarry Smith {
3438f1509bcSBarry Smith   PetscFunctionBegin;
3448f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3458f1509bcSBarry Smith   *ctype = dm->coloringtype;
3468f1509bcSBarry Smith   PetscFunctionReturn(0);
3478f1509bcSBarry Smith }
3488f1509bcSBarry Smith 
3498f1509bcSBarry Smith /*@C
3508f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3518f1509bcSBarry Smith 
352d083f849SBarry Smith    Logically Collective on dm
3538f1509bcSBarry Smith 
3548f1509bcSBarry Smith    Input Parameters:
355521d9a4cSLisandro Dalcin +  dm - the DM context
356a2b5a043SBarry Smith -  ctype - the matrix type
357521d9a4cSLisandro Dalcin 
358521d9a4cSLisandro Dalcin    Options Database:
359521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
360521d9a4cSLisandro Dalcin 
361521d9a4cSLisandro Dalcin    Level: intermediate
362521d9a4cSLisandro Dalcin 
363a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
364521d9a4cSLisandro Dalcin @*/
36519fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
366521d9a4cSLisandro Dalcin {
367521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
36888f0584fSBarry Smith 
369521d9a4cSLisandro Dalcin   PetscFunctionBegin;
370521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
371521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
37219fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
373521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
374521d9a4cSLisandro Dalcin }
375521d9a4cSLisandro Dalcin 
376c0dedaeaSBarry Smith /*@C
377c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
378c0dedaeaSBarry Smith 
379d083f849SBarry Smith    Logically Collective on dm
380c0dedaeaSBarry Smith 
381c0dedaeaSBarry Smith    Input Parameter:
382c0dedaeaSBarry Smith .  dm - the DM context
383c0dedaeaSBarry Smith 
384c0dedaeaSBarry Smith    Output Parameter:
385c0dedaeaSBarry Smith .  ctype - the matrix type
386c0dedaeaSBarry Smith 
387c0dedaeaSBarry Smith    Options Database:
388c0dedaeaSBarry Smith .   -dm_mat_type ctype
389c0dedaeaSBarry Smith 
390c0dedaeaSBarry Smith    Level: intermediate
391c0dedaeaSBarry Smith 
392a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
393c0dedaeaSBarry Smith @*/
394c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
395c0dedaeaSBarry Smith {
396c0dedaeaSBarry Smith   PetscFunctionBegin;
397c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
398c0dedaeaSBarry Smith   *ctype = dm->mattype;
399c0dedaeaSBarry Smith   PetscFunctionReturn(0);
400c0dedaeaSBarry Smith }
401c0dedaeaSBarry Smith 
402c688c046SMatthew G Knepley /*@
40334f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
404c688c046SMatthew G Knepley 
405c688c046SMatthew G Knepley   Not collective
406c688c046SMatthew G Knepley 
407c688c046SMatthew G Knepley   Input Parameter:
408c688c046SMatthew G Knepley . A - The Mat
409c688c046SMatthew G Knepley 
410c688c046SMatthew G Knepley   Output Parameter:
411c688c046SMatthew G Knepley . dm - The DM
412c688c046SMatthew G Knepley 
413c688c046SMatthew G Knepley   Level: intermediate
414c688c046SMatthew G Knepley 
4158f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4168f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4178f1509bcSBarry Smith 
418c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
419c688c046SMatthew G Knepley @*/
420c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
421c688c046SMatthew G Knepley {
422c688c046SMatthew G Knepley   PetscErrorCode ierr;
423c688c046SMatthew G Knepley 
424c688c046SMatthew G Knepley   PetscFunctionBegin;
425c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
426c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
427c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
428c688c046SMatthew G Knepley   PetscFunctionReturn(0);
429c688c046SMatthew G Knepley }
430c688c046SMatthew G Knepley 
431c688c046SMatthew G Knepley /*@
432c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
433c688c046SMatthew G Knepley 
434c688c046SMatthew G Knepley   Not collective
435c688c046SMatthew G Knepley 
436c688c046SMatthew G Knepley   Input Parameters:
437c688c046SMatthew G Knepley + A - The Mat
438c688c046SMatthew G Knepley - dm - The DM
439c688c046SMatthew G Knepley 
440c688c046SMatthew G Knepley   Level: intermediate
441c688c046SMatthew G Knepley 
4428f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4438f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4448f1509bcSBarry Smith 
445c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
446c688c046SMatthew G Knepley @*/
447c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
448c688c046SMatthew G Knepley {
449c688c046SMatthew G Knepley   PetscErrorCode ierr;
450c688c046SMatthew G Knepley 
451c688c046SMatthew G Knepley   PetscFunctionBegin;
452c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4538865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
454c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
455c688c046SMatthew G Knepley   PetscFunctionReturn(0);
456c688c046SMatthew G Knepley }
457c688c046SMatthew G Knepley 
4589a42bb27SBarry Smith /*@C
4599a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4606757b960SDave May    DM options in the database.
4619a42bb27SBarry Smith 
462d083f849SBarry Smith    Logically Collective on dm
4639a42bb27SBarry Smith 
464d8d19677SJose E. Roman    Input Parameters:
4658353ddbbSDave May +  da - the DM context
4669a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4679a42bb27SBarry Smith 
4689a42bb27SBarry Smith    Notes:
4699a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4709a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4719a42bb27SBarry Smith 
4729a42bb27SBarry Smith    Level: advanced
4739a42bb27SBarry Smith 
4749a42bb27SBarry Smith .seealso: DMSetFromOptions()
4759a42bb27SBarry Smith @*/
4767087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4779a42bb27SBarry Smith {
4789a42bb27SBarry Smith   PetscErrorCode ierr;
4799a42bb27SBarry Smith 
4809a42bb27SBarry Smith   PetscFunctionBegin;
4819a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4829a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
483691be533SLawrence Mitchell   if (dm->sf) {
484691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
485691be533SLawrence Mitchell   }
4861bb6d2a8SBarry Smith   if (dm->sectionSF) {
4871bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
488691be533SLawrence Mitchell   }
4899a42bb27SBarry Smith   PetscFunctionReturn(0);
4909a42bb27SBarry Smith }
4919a42bb27SBarry Smith 
49231697293SDave May /*@C
49331697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
49431697293SDave May    DM options in the database.
49531697293SDave May 
496d083f849SBarry Smith    Logically Collective on dm
49731697293SDave May 
49831697293SDave May    Input Parameters:
49931697293SDave May +  dm - the DM context
50031697293SDave May -  prefix - the prefix string to prepend to all DM option requests
50131697293SDave May 
50231697293SDave May    Notes:
50331697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
50431697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
50531697293SDave May 
50631697293SDave May    Level: advanced
50731697293SDave May 
50831697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
50931697293SDave May @*/
51031697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
51131697293SDave May {
51231697293SDave May   PetscErrorCode ierr;
51331697293SDave May 
51431697293SDave May   PetscFunctionBegin;
51531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
51631697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
51731697293SDave May   PetscFunctionReturn(0);
51831697293SDave May }
51931697293SDave May 
52031697293SDave May /*@C
52131697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
52231697293SDave May    DM options in the database.
52331697293SDave May 
52431697293SDave May    Not Collective
52531697293SDave May 
52631697293SDave May    Input Parameters:
52731697293SDave May .  dm - the DM context
52831697293SDave May 
52931697293SDave May    Output Parameters:
53031697293SDave May .  prefix - pointer to the prefix string used is returned
53131697293SDave May 
53295452b02SPatrick Sanan    Notes:
53395452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
53431697293SDave May    sufficient length to hold the prefix.
53531697293SDave May 
53631697293SDave May    Level: advanced
53731697293SDave May 
53831697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
53931697293SDave May @*/
54031697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
54131697293SDave May {
54231697293SDave May   PetscErrorCode ierr;
54331697293SDave May 
54431697293SDave May   PetscFunctionBegin;
54531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54631697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
54731697293SDave May   PetscFunctionReturn(0);
54831697293SDave May }
54931697293SDave May 
55088bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
55188bdff64SToby Isaac {
5526eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
55388bdff64SToby Isaac   PetscErrorCode ierr;
55488bdff64SToby Isaac 
55588bdff64SToby Isaac   PetscFunctionBegin;
556aab5bcd8SJed Brown   *ncrefct = 0;
55788bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55888bdff64SToby Isaac     refct--;
55988bdff64SToby Isaac     if (recurseCoarse) {
56088bdff64SToby Isaac       PetscInt coarseCount;
56188bdff64SToby Isaac 
56288bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
56388bdff64SToby Isaac       refct += coarseCount;
56488bdff64SToby Isaac     }
56588bdff64SToby Isaac   }
56688bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56788bdff64SToby Isaac     refct--;
56888bdff64SToby Isaac     if (recurseFine) {
56988bdff64SToby Isaac       PetscInt fineCount;
57088bdff64SToby Isaac 
57188bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
57288bdff64SToby Isaac       refct += fineCount;
57388bdff64SToby Isaac     }
57488bdff64SToby Isaac   }
57588bdff64SToby Isaac   *ncrefct = refct;
57688bdff64SToby Isaac   PetscFunctionReturn(0);
57788bdff64SToby Isaac }
57888bdff64SToby Isaac 
579f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
580354557abSToby Isaac {
5815d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
582354557abSToby Isaac   PetscErrorCode ierr;
583354557abSToby Isaac 
584354557abSToby Isaac   PetscFunctionBegin;
585354557abSToby Isaac   /* destroy the labels */
586354557abSToby Isaac   while (next) {
587354557abSToby Isaac     DMLabelLink tmp = next->next;
588354557abSToby Isaac 
5895d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
590ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
591354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
592354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
593354557abSToby Isaac     next = tmp;
594354557abSToby Isaac   }
5955d80c0bfSVaclav Hapla   dm->labels = NULL;
596354557abSToby Isaac   PetscFunctionReturn(0);
597354557abSToby Isaac }
598354557abSToby Isaac 
5991fb7b255SJunchao Zhang /*@C
6008472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
60147c6ae99SBarry Smith 
602d083f849SBarry Smith     Collective on dm
60347c6ae99SBarry Smith 
60447c6ae99SBarry Smith     Input Parameter:
60547c6ae99SBarry Smith .   dm - the DM object to destroy
60647c6ae99SBarry Smith 
60747c6ae99SBarry Smith     Level: developer
60847c6ae99SBarry Smith 
609e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
61047c6ae99SBarry Smith 
61147c6ae99SBarry Smith @*/
612fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
61347c6ae99SBarry Smith {
6146eb26441SStefano Zampini   PetscInt       cnt;
615dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61647c6ae99SBarry Smith   PetscErrorCode ierr;
61747c6ae99SBarry Smith 
61847c6ae99SBarry Smith   PetscFunctionBegin;
6196bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6206bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
62187e657c6SBarry Smith 
62288bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
62388bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
62488bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
625ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6266bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6276bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6286eb26441SStefano Zampini 
6296eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6306eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6316eb26441SStefano Zampini 
632f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6330298fd71SBarry Smith   (*dm)->namedglobal = NULL;
634f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6352348bcf4SPeter Brune     nnext = nlink->next;
6362348bcf4SPeter 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);
6372348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6382348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6392348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6402348bcf4SPeter Brune   }
641f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6420298fd71SBarry Smith   (*dm)->namedlocal = NULL;
643f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
644f490541aSPeter Brune     nnext = nlink->next;
645f490541aSPeter 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);
646f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
647f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
648f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
649f490541aSPeter Brune   }
6502348bcf4SPeter Brune 
651b17ce1afSJed Brown   /* Destroy the list of hooks */
652c833c3b5SJed Brown   {
653c833c3b5SJed Brown     DMCoarsenHookLink link,next;
654b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
655b17ce1afSJed Brown       next = link->next;
656b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
657b17ce1afSJed Brown     }
6580298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
659c833c3b5SJed Brown   }
660c833c3b5SJed Brown   {
661c833c3b5SJed Brown     DMRefineHookLink link,next;
662c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
663c833c3b5SJed Brown       next = link->next;
664c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
665c833c3b5SJed Brown     }
6660298fd71SBarry Smith     (*dm)->refinehook = NULL;
667c833c3b5SJed Brown   }
668be081cd6SPeter Brune   {
669be081cd6SPeter Brune     DMSubDomainHookLink link,next;
670be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
671be081cd6SPeter Brune       next = link->next;
672be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
673be081cd6SPeter Brune     }
6740298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
675be081cd6SPeter Brune   }
676baf369e7SPeter Brune   {
677baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
678baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
679baf369e7SPeter Brune       next = link->next;
680baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
681baf369e7SPeter Brune     }
6820298fd71SBarry Smith     (*dm)->gtolhook = NULL;
683baf369e7SPeter Brune   }
684d4d07f1eSToby Isaac   {
685d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
686d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
687d4d07f1eSToby Isaac       next = link->next;
688d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
689d4d07f1eSToby Isaac     }
690d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
691d4d07f1eSToby Isaac   }
692aa1993deSMatthew G Knepley   /* Destroy the work arrays */
693aa1993deSMatthew G Knepley   {
694aa1993deSMatthew G Knepley     DMWorkLink link,next;
695aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
696aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
697aa1993deSMatthew G Knepley       next = link->next;
698aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
699aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
700aa1993deSMatthew G Knepley     }
7010298fd71SBarry Smith     (*dm)->workin = NULL;
702aa1993deSMatthew G Knepley   }
703c58f1c22SToby Isaac   /* destroy the labels */
704f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
705f4cdcedcSVaclav Hapla   /* destroy the fields */
706e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
707f4cdcedcSVaclav Hapla   /* destroy the boundaries */
708e6f8dbb6SToby Isaac   {
709e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
710e6f8dbb6SToby Isaac     while (next) {
711e6f8dbb6SToby Isaac       DMBoundary b = next;
712e6f8dbb6SToby Isaac 
713e6f8dbb6SToby Isaac       next = b->next;
714e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
715e6f8dbb6SToby Isaac     }
716e6f8dbb6SToby Isaac   }
717b17ce1afSJed Brown 
71852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
71952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
72052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
72152536dc3SBarry Smith 
7221a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7231a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7241a266240SBarry Smith   }
72571cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7266bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7276bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
728073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
72988ed4aceSMatthew G Knepley 
7301bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7311bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7328b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
733fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
734fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
73588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7361bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
737736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
738736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7398e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
740736995cdSBlaise Bourdin     }
741736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
742736995cdSBlaise Bourdin   }
7439a2a23afSMatthew G. Knepley   {
7449a2a23afSMatthew G. Knepley     Vec     *auxData;
7459a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7469a2a23afSMatthew G. Knepley 
7479a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxGetSize((*dm)->auxData, &n);CHKERRQ(ierr);
7489a2a23afSMatthew G. Knepley     ierr = PetscMalloc1(n, &auxData);CHKERRQ(ierr);
7499a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxGetVals((*dm)->auxData, &off, auxData);CHKERRQ(ierr);
7509a2a23afSMatthew G. Knepley     for (i = 0; i < n; ++i) {ierr = VecDestroy(&auxData[i]);CHKERRQ(ierr);}
7519a2a23afSMatthew G. Knepley     ierr = PetscFree(auxData);CHKERRQ(ierr);
7529a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxDestroy(&(*dm)->auxData);CHKERRQ(ierr);
7539a2a23afSMatthew G. Knepley   }
75488bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
75588bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
75688bdff64SToby Isaac   }
7576eb26441SStefano Zampini 
758a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
75988bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
76088bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
76188bdff64SToby Isaac   }
76288bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
763f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7646636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7656636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7666636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
767412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
768412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
769412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
770ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
771ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
772ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7736636e97aSMatthew G Knepley 
774e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
77514f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
776e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
777e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
778732e2eb9SMatthew G Knepley 
779ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7806bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
781ed3c66a1SDave May   }
782c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
783f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
784f918ec44SMatthew G. Knepley   ierr = CeedElemRestrictionDestroy(&(*dm)->ceedERestrict);CHKERRQ(ierr);
785f918ec44SMatthew G. Knepley   ierr = CeedDestroy(&(*dm)->ceed);CHKERRQ(ierr);
786f918ec44SMatthew G. Knepley #endif
787435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
788732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
78947c6ae99SBarry Smith   PetscFunctionReturn(0);
79047c6ae99SBarry Smith }
79147c6ae99SBarry Smith 
792d7bf68aeSBarry Smith /*@
793d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
794d7bf68aeSBarry Smith 
795d083f849SBarry Smith     Collective on dm
796d7bf68aeSBarry Smith 
797d7bf68aeSBarry Smith     Input Parameter:
798d7bf68aeSBarry Smith .   dm - the DM object to setup
799d7bf68aeSBarry Smith 
800d7bf68aeSBarry Smith     Level: developer
801d7bf68aeSBarry Smith 
802e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
803d7bf68aeSBarry Smith 
804d7bf68aeSBarry Smith @*/
8057087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
806d7bf68aeSBarry Smith {
807d7bf68aeSBarry Smith   PetscErrorCode ierr;
808d7bf68aeSBarry Smith 
809d7bf68aeSBarry Smith   PetscFunctionBegin;
810171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8118387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
812d7bf68aeSBarry Smith   if (dm->ops->setup) {
813d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
814d7bf68aeSBarry Smith   }
8158387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
816d7bf68aeSBarry Smith   PetscFunctionReturn(0);
817d7bf68aeSBarry Smith }
818d7bf68aeSBarry Smith 
819d7bf68aeSBarry Smith /*@
820d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
821d7bf68aeSBarry Smith 
822d083f849SBarry Smith     Collective on dm
823d7bf68aeSBarry Smith 
824d7bf68aeSBarry Smith     Input Parameter:
825d7bf68aeSBarry Smith .   dm - the DM object to set options for
826d7bf68aeSBarry Smith 
827732e2eb9SMatthew G Knepley     Options Database:
8284164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8294164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8304164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8318f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
832732e2eb9SMatthew G Knepley 
8339318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8349318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8359318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
8369318fe57SMatthew G. Knepley . -dm_plex_shape <shape>            - The domain shape, such as DM_SHAPE_BOX, DM_SHAPE_SPHERE, etc.
8379318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8389318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8399318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
8409318fe57SMatthew G. Knepley . -dm_plex_simplex <bool>           - PETSC_TRUE for simplex elements, PETSC_FALSE for tensor elements
8419318fe57SMatthew G. Knepley . -dm_plex_interpolate <bool>       - PETSC_TRUE turns on topological interpolation (creating edges and faces)
8429318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8439318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8449318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8459318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
8469318fe57SMatthew G. Knepley . -dm_plex_box_bd <bx,by,bz>        - Specify the DMBoundaryType for each direction
8479318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8489318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8499318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8509318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
8519318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8529318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8539318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8549318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
8559318fe57SMatthew G. Knepley . -dm_extrude_layers <l>            - The number of layers to extrude
8569318fe57SMatthew G. Knepley . -dm_extrude_thickness <t>         - The thickness of the layer to be extruded
8579318fe57SMatthew G. Knepley . -dm_extrude_column_first <bool>   - Order the cells in a vertical column first
858909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
859909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8609318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8619318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8629318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8639318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8649318fe57SMatthew G. Knepley 
865384a6580SVaclav Hapla     DMPLEX Specific Checks
866384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
867384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
868384a6580SVaclav 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()
869384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
870384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
871384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
872384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
873d7bf68aeSBarry Smith 
87495eb5ee5SVaclav Hapla     Level: intermediate
87595eb5ee5SVaclav Hapla 
87695eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
87795eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
878d7bf68aeSBarry Smith 
879d7bf68aeSBarry Smith @*/
8807087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
881d7bf68aeSBarry Smith {
8827781c08eSBarry Smith   char           typeName[256];
883ca266f36SBarry Smith   PetscBool      flg;
884d7bf68aeSBarry Smith   PetscErrorCode ierr;
885d7bf68aeSBarry Smith 
886d7bf68aeSBarry Smith   PetscFunctionBegin;
887171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
88849be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
88949be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8901bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8913194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8920298fd71SBarry 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);
893a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
894f9ba7244SBarry Smith   if (flg) {
895f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
896f9ba7244SBarry Smith   }
897a264d7a6SBarry 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);
898073dac72SJed Brown   if (flg) {
899521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
900073dac72SJed Brown   }
9018f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
902f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
903e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
904f9ba7244SBarry Smith   }
905f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
9060633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
90782fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
908d7bf68aeSBarry Smith   PetscFunctionReturn(0);
909d7bf68aeSBarry Smith }
910d7bf68aeSBarry Smith 
911fc9bc008SSatish Balay /*@C
912fe2efc57SMark    DMViewFromOptions - View from Options
913fe2efc57SMark 
914fe2efc57SMark    Collective on DM
915fe2efc57SMark 
916fe2efc57SMark    Input Parameters:
917fe2efc57SMark +  dm - the DM object
918736c3998SJose E. Roman .  obj - Optional object
919736c3998SJose E. Roman -  name - command line option
920fe2efc57SMark 
921fe2efc57SMark    Level: intermediate
922fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
923fe2efc57SMark @*/
924fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
925fe2efc57SMark {
926fe2efc57SMark   PetscErrorCode ierr;
927fe2efc57SMark 
928fe2efc57SMark   PetscFunctionBegin;
929fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
930fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
931fe2efc57SMark   PetscFunctionReturn(0);
932fe2efc57SMark }
933fe2efc57SMark 
934fe2efc57SMark /*@C
935224748a4SBarry Smith     DMView - Views a DM
93647c6ae99SBarry Smith 
937d083f849SBarry Smith     Collective on dm
93847c6ae99SBarry Smith 
939d8d19677SJose E. Roman     Input Parameters:
94047c6ae99SBarry Smith +   dm - the DM object to view
94147c6ae99SBarry Smith -   v - the viewer
94247c6ae99SBarry Smith 
943224748a4SBarry Smith     Level: beginner
94447c6ae99SBarry Smith 
945e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94647c6ae99SBarry Smith 
94747c6ae99SBarry Smith @*/
9487087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
94947c6ae99SBarry Smith {
95047c6ae99SBarry Smith   PetscErrorCode    ierr;
95132c0f0efSBarry Smith   PetscBool         isbinary;
95276a8abe0SBarry Smith   PetscMPIInt       size;
95376a8abe0SBarry Smith   PetscViewerFormat format;
95447c6ae99SBarry Smith 
95547c6ae99SBarry Smith   PetscFunctionBegin;
956171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9573014e516SBarry Smith   if (!v) {
958ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9593014e516SBarry Smith   }
960b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
96174903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
96274903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
96374903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
96474903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
96574903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
96674903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
96774903a4fSStefano Zampini      in an error here */
96874903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9698bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
970b1b135c8SBarry Smith 
97176a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
972ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRMPI(ierr);
97376a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
97498c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
97532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
97632c0f0efSBarry Smith   if (isbinary) {
97755849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
97832c0f0efSBarry Smith     char     type[256];
97932c0f0efSBarry Smith 
980f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
98132c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
982f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
98332c0f0efSBarry Smith   }
9840c010503SBarry Smith   if (dm->ops->view) {
9850c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
98647c6ae99SBarry Smith   }
98747c6ae99SBarry Smith   PetscFunctionReturn(0);
98847c6ae99SBarry Smith }
98947c6ae99SBarry Smith 
99047c6ae99SBarry Smith /*@
9918472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
99247c6ae99SBarry Smith 
993d083f849SBarry Smith     Collective on dm
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith     Input Parameter:
99647c6ae99SBarry Smith .   dm - the DM object
99747c6ae99SBarry Smith 
99847c6ae99SBarry Smith     Output Parameter:
99947c6ae99SBarry Smith .   vec - the global vector
100047c6ae99SBarry Smith 
1001073dac72SJed Brown     Level: beginner
100247c6ae99SBarry Smith 
1003ec075b9fSPatrick Sanan .seealso DMCreateLocalVector(), DMGetGlobalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
100447c6ae99SBarry Smith 
100547c6ae99SBarry Smith @*/
10067087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
100747c6ae99SBarry Smith {
100847c6ae99SBarry Smith   PetscErrorCode ierr;
100947c6ae99SBarry Smith 
101047c6ae99SBarry Smith   PetscFunctionBegin;
1011171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1012b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1013b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
101447c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
101576bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1016c6b011d8SStefano Zampini     DM vdm;
1017c6b011d8SStefano Zampini 
1018c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1019c6b011d8SStefano 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);
1020c6b011d8SStefano Zampini   }
102147c6ae99SBarry Smith   PetscFunctionReturn(0);
102247c6ae99SBarry Smith }
102347c6ae99SBarry Smith 
102447c6ae99SBarry Smith /*@
10258472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
102647c6ae99SBarry Smith 
102747c6ae99SBarry Smith     Not Collective
102847c6ae99SBarry Smith 
102947c6ae99SBarry Smith     Input Parameter:
103047c6ae99SBarry Smith .   dm - the DM object
103147c6ae99SBarry Smith 
103247c6ae99SBarry Smith     Output Parameter:
103347c6ae99SBarry Smith .   vec - the local vector
103447c6ae99SBarry Smith 
1035073dac72SJed Brown     Level: beginner
103647c6ae99SBarry Smith 
1037ec075b9fSPatrick Sanan .seealso DMCreateGlobalVector(), DMGetLocalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
103847c6ae99SBarry Smith 
103947c6ae99SBarry Smith @*/
10407087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
104147c6ae99SBarry Smith {
104247c6ae99SBarry Smith   PetscErrorCode ierr;
104347c6ae99SBarry Smith 
104447c6ae99SBarry Smith   PetscFunctionBegin;
1045171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1046b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1047b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
104847c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
104976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1050c6b011d8SStefano Zampini     DM vdm;
1051c6b011d8SStefano Zampini 
1052c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1053c6b011d8SStefano 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);
1054c6b011d8SStefano Zampini   }
105547c6ae99SBarry Smith   PetscFunctionReturn(0);
105647c6ae99SBarry Smith }
105747c6ae99SBarry Smith 
10581411c6eeSJed Brown /*@
10591411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10601411c6eeSJed Brown 
1061d083f849SBarry Smith    Collective on dm
10621411c6eeSJed Brown 
10631411c6eeSJed Brown    Input Parameter:
10641411c6eeSJed Brown .  dm - the DM that provides the mapping
10651411c6eeSJed Brown 
10661411c6eeSJed Brown    Output Parameter:
10671411c6eeSJed Brown .  ltog - the mapping
10681411c6eeSJed Brown 
10691411c6eeSJed Brown    Level: intermediate
10701411c6eeSJed Brown 
10711411c6eeSJed Brown    Notes:
10721411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10731411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10741411c6eeSJed Brown 
1075fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10761411c6eeSJed Brown @*/
10777087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10781411c6eeSJed Brown {
10790be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10801411c6eeSJed Brown   PetscErrorCode ierr;
10811411c6eeSJed Brown 
10821411c6eeSJed Brown   PetscFunctionBegin;
10831411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10841411c6eeSJed Brown   PetscValidPointer(ltog,2);
10851411c6eeSJed Brown   if (!dm->ltogmap) {
108637d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
108737d0c07bSMatthew G Knepley 
108892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
108937d0c07bSMatthew G Knepley     if (section) {
1090a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
109137d0c07bSMatthew G Knepley       PetscInt       *ltog;
1092ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
109337d0c07bSMatthew G Knepley 
1094e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
109537d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1096ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1097ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
109837d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1099a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
110037d0c07bSMatthew G Knepley 
110137d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
110237d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1103a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1104a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
110537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
11061a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11071a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
11081a7dc684SMatthew G. Knepley         if (dof) {
1109b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1110b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
11111a7dc684SMatthew G. Knepley         }
111237d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1113a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1114a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
111537d0c07bSMatthew G Knepley         }
111637d0c07bSMatthew G Knepley       }
1117bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11180be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11190be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
11200be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11210be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
11227591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11237591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1124ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1125ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1126ccf3bd66SMatthew G. Knepley         n /= bs;
1127ccf3bd66SMatthew G. Knepley       }
1128ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
11293bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
113037d0c07bSMatthew G Knepley     } else {
1131b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1132184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
11331411c6eeSJed Brown     }
113437d0c07bSMatthew G Knepley   }
11351411c6eeSJed Brown   *ltog = dm->ltogmap;
11361411c6eeSJed Brown   PetscFunctionReturn(0);
11371411c6eeSJed Brown }
11381411c6eeSJed Brown 
11391411c6eeSJed Brown /*@
11401411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
11411411c6eeSJed Brown 
11421411c6eeSJed Brown    Not Collective
11431411c6eeSJed Brown 
11441411c6eeSJed Brown    Input Parameter:
11451411c6eeSJed Brown .  dm - the DM with block structure
11461411c6eeSJed Brown 
11471411c6eeSJed Brown    Output Parameter:
11481411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11491411c6eeSJed Brown 
11501411c6eeSJed Brown    Level: intermediate
11511411c6eeSJed Brown 
1152fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
11531411c6eeSJed Brown @*/
11547087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
11551411c6eeSJed Brown {
11561411c6eeSJed Brown   PetscFunctionBegin;
11571411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1158534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11591411c6eeSJed 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");
11601411c6eeSJed Brown   *bs = dm->bs;
11611411c6eeSJed Brown   PetscFunctionReturn(0);
11621411c6eeSJed Brown }
11631411c6eeSJed Brown 
116448eeb7c8SBarry Smith /*@C
11658472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
116647c6ae99SBarry Smith 
1167a5bc1bf3SBarry Smith     Collective on dmc
116847c6ae99SBarry Smith 
1169d8d19677SJose E. Roman     Input Parameters:
1170a5bc1bf3SBarry Smith +   dmc - the DM object
1171a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
117247c6ae99SBarry Smith 
1173d8d19677SJose E. Roman     Output Parameters:
117447c6ae99SBarry Smith +  mat - the interpolation
117547c6ae99SBarry Smith -  vec - the scaling (optional)
117647c6ae99SBarry Smith 
117747c6ae99SBarry Smith     Level: developer
117847c6ae99SBarry Smith 
117995452b02SPatrick Sanan     Notes:
118095452b02SPatrick 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
118185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1182d52bd9f3SBarry Smith 
11831f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1184d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
118585afcc9aSBarry Smith 
11862ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
118747c6ae99SBarry Smith 
118847c6ae99SBarry Smith @*/
1189a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
119047c6ae99SBarry Smith {
119147c6ae99SBarry Smith   PetscErrorCode ierr;
119247c6ae99SBarry Smith 
119347c6ae99SBarry Smith   PetscFunctionBegin;
1194a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1195a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1196c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1197a5bc1bf3SBarry Smith   if (!dmc->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1198a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1199a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1200a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
120147c6ae99SBarry Smith   PetscFunctionReturn(0);
120247c6ae99SBarry Smith }
120347c6ae99SBarry Smith 
12043ad4599aSBarry Smith /*@
1205d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
12062ed6491fSPatrick Sanan 
12072ed6491fSPatrick Sanan   Input Parameters:
12082ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
12092ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
12102ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12112ed6491fSPatrick Sanan 
12122ed6491fSPatrick Sanan   Output Parameter:
12132ed6491fSPatrick Sanan .    scale - the scaled vector
12142ed6491fSPatrick Sanan 
12152ed6491fSPatrick Sanan   Level: developer
12162ed6491fSPatrick Sanan 
12172ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
12182ed6491fSPatrick Sanan 
12192ed6491fSPatrick Sanan @*/
12202ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
12212ed6491fSPatrick Sanan {
12222ed6491fSPatrick Sanan   PetscErrorCode ierr;
12232ed6491fSPatrick Sanan   Vec            fine;
12242ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
12252ed6491fSPatrick Sanan 
12262ed6491fSPatrick Sanan   PetscFunctionBegin;
12272ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
12282ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
12292ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
12302ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
12312ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
12322ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
12332ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12342ed6491fSPatrick Sanan }
12352ed6491fSPatrick Sanan 
12362ed6491fSPatrick Sanan /*@
12373ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
12383ad4599aSBarry Smith 
1239a5bc1bf3SBarry Smith     Collective on dmc
12403ad4599aSBarry Smith 
1241d8d19677SJose E. Roman     Input Parameters:
1242a5bc1bf3SBarry Smith +   dmc - the DM object
1243a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
12443ad4599aSBarry Smith 
12453ad4599aSBarry Smith     Output Parameter:
12463ad4599aSBarry Smith .  mat - the restriction
12473ad4599aSBarry Smith 
12483ad4599aSBarry Smith     Level: developer
12493ad4599aSBarry Smith 
125095452b02SPatrick Sanan     Notes:
125195452b02SPatrick 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
12523ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
12533ad4599aSBarry Smith 
12543ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
12553ad4599aSBarry Smith 
12563ad4599aSBarry Smith @*/
1257a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12583ad4599aSBarry Smith {
12593ad4599aSBarry Smith   PetscErrorCode ierr;
12603ad4599aSBarry Smith 
12613ad4599aSBarry Smith   PetscFunctionBegin;
1262a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1263a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12645a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1265a5bc1bf3SBarry Smith   if (!dmc->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1266a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1267a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1268a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
12693ad4599aSBarry Smith   PetscFunctionReturn(0);
12703ad4599aSBarry Smith }
12713ad4599aSBarry Smith 
127247c6ae99SBarry Smith /*@
12738472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
127447c6ae99SBarry Smith 
1275a5bc1bf3SBarry Smith     Collective on dac
127647c6ae99SBarry Smith 
1277d8d19677SJose E. Roman     Input Parameters:
1278a5bc1bf3SBarry Smith +   dac - the DM object
1279a5bc1bf3SBarry Smith -   daf - the second, finer DM object
128047c6ae99SBarry Smith 
128147c6ae99SBarry Smith     Output Parameter:
12826dbf9973SLawrence Mitchell .   mat - the injection
128347c6ae99SBarry Smith 
128447c6ae99SBarry Smith     Level: developer
128547c6ae99SBarry Smith 
128695452b02SPatrick Sanan    Notes:
128795452b02SPatrick 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
128885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
128985afcc9aSBarry Smith 
1290e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
129147c6ae99SBarry Smith 
129247c6ae99SBarry Smith @*/
1293a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
129447c6ae99SBarry Smith {
129547c6ae99SBarry Smith   PetscErrorCode ierr;
129647c6ae99SBarry Smith 
129747c6ae99SBarry Smith   PetscFunctionBegin;
1298a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1299a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
13005a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1301a5bc1bf3SBarry Smith   if (!dac->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1302a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1303a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1304a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
130547c6ae99SBarry Smith   PetscFunctionReturn(0);
130647c6ae99SBarry Smith }
130747c6ae99SBarry Smith 
1308b412c318SBarry Smith /*@
1309bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1310bd041c0cSMatthew G. Knepley 
1311a5bc1bf3SBarry Smith   Collective on dac
1312bd041c0cSMatthew G. Knepley 
1313d8d19677SJose E. Roman   Input Parameters:
1314a5bc1bf3SBarry Smith + dac - the DM object
1315a5bc1bf3SBarry Smith - daf - the second, finer DM object
1316bd041c0cSMatthew G. Knepley 
1317bd041c0cSMatthew G. Knepley   Output Parameter:
1318bd041c0cSMatthew G. Knepley . mat - the interpolation
1319bd041c0cSMatthew G. Knepley 
1320bd041c0cSMatthew G. Knepley   Level: developer
1321bd041c0cSMatthew G. Knepley 
1322bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1323bd041c0cSMatthew G. Knepley @*/
1324a5bc1bf3SBarry Smith PetscErrorCode DMCreateMassMatrix(DM dac, DM daf, Mat *mat)
1325bd041c0cSMatthew G. Knepley {
1326bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1327bd041c0cSMatthew G. Knepley 
1328bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1329a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1330a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13315a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1332a5bc1bf3SBarry Smith   if (!dac->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dac)->type_name);
1333a5bc1bf3SBarry Smith   ierr = (*dac->ops->createmassmatrix)(dac, daf, mat);CHKERRQ(ierr);
1334bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1335bd041c0cSMatthew G. Knepley }
1336bd041c0cSMatthew G. Knepley 
1337bd041c0cSMatthew G. Knepley /*@
1338b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
133947c6ae99SBarry Smith 
1340d083f849SBarry Smith     Collective on dm
134147c6ae99SBarry Smith 
1342d8d19677SJose E. Roman     Input Parameters:
134347c6ae99SBarry Smith +   dm - the DM object
13445bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
134547c6ae99SBarry Smith 
134647c6ae99SBarry Smith     Output Parameter:
134747c6ae99SBarry Smith .   coloring - the coloring
134847c6ae99SBarry Smith 
1349ec5066bdSBarry Smith     Notes:
1350ec5066bdSBarry 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
1351ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1352ec5066bdSBarry Smith 
1353ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1354ec5066bdSBarry Smith 
135547c6ae99SBarry Smith     Level: developer
135647c6ae99SBarry Smith 
1357ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
135847c6ae99SBarry Smith 
1359aab9d709SJed Brown @*/
1360b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
136147c6ae99SBarry Smith {
136247c6ae99SBarry Smith   PetscErrorCode ierr;
136347c6ae99SBarry Smith 
136447c6ae99SBarry Smith   PetscFunctionBegin;
1365171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13665a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1367b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1368b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
136947c6ae99SBarry Smith   PetscFunctionReturn(0);
137047c6ae99SBarry Smith }
137147c6ae99SBarry Smith 
1372b412c318SBarry Smith /*@
13738472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
137447c6ae99SBarry Smith 
1375d083f849SBarry Smith     Collective on dm
137647c6ae99SBarry Smith 
137747c6ae99SBarry Smith     Input Parameter:
1378b412c318SBarry Smith .   dm - the DM object
137947c6ae99SBarry Smith 
138047c6ae99SBarry Smith     Output Parameter:
138147c6ae99SBarry Smith .   mat - the empty Jacobian
138247c6ae99SBarry Smith 
1383073dac72SJed Brown     Level: beginner
138447c6ae99SBarry Smith 
1385f27dd7c6SMatthew G. Knepley     Options Database Keys:
1386f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1387f27dd7c6SMatthew G. Knepley 
138895452b02SPatrick Sanan     Notes:
138995452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
139094013140SBarry Smith        do not need to do it yourself.
139194013140SBarry Smith 
139294013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13937889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
139494013140SBarry Smith 
139594013140SBarry 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
139694013140SBarry Smith        internally by PETSc.
139794013140SBarry Smith 
139894013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1399aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
140094013140SBarry Smith 
1401b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
140247c6ae99SBarry Smith 
1403aab9d709SJed Brown @*/
1404b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
140547c6ae99SBarry Smith {
140647c6ae99SBarry Smith   PetscErrorCode ierr;
140747c6ae99SBarry Smith 
140847c6ae99SBarry Smith   PetscFunctionBegin;
1409171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1410064a246eSJacob Faibussowitsch   PetscValidPointer(mat,2);
1411b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
14125a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1413fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1414b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
141576bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1416c6b011d8SStefano Zampini     DM mdm;
1417c6b011d8SStefano Zampini 
1418c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1419c6b011d8SStefano 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);
1420c6b011d8SStefano Zampini   }
1421e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1422e5e52638SMatthew G. Knepley   if (dm->Nf) {
1423e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1424649ef022SMatthew Knepley     PetscInt     Nf, f;
1425e571a35bSMatthew G. Knepley 
1426e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1427649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1428649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
1429649ef022SMatthew Knepley         ierr = (*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1430e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1431e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1432649ef022SMatthew Knepley         break;
1433e571a35bSMatthew G. Knepley       }
1434649ef022SMatthew Knepley     }
1435649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1436649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
1437649ef022SMatthew Knepley         ierr = (*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1438e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1439e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1440e571a35bSMatthew G. Knepley       }
1441e571a35bSMatthew G. Knepley     }
1442e571a35bSMatthew G. Knepley   }
1443fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
144447c6ae99SBarry Smith   PetscFunctionReturn(0);
144547c6ae99SBarry Smith }
144647c6ae99SBarry Smith 
1447732e2eb9SMatthew G Knepley /*@
1448950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1449732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1450732e2eb9SMatthew G Knepley 
1451d083f849SBarry Smith   Logically Collective on dm
1452732e2eb9SMatthew G Knepley 
1453d8d19677SJose E. Roman   Input Parameters:
1454732e2eb9SMatthew G Knepley + dm - the DM
1455732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1456732e2eb9SMatthew G Knepley 
1457732e2eb9SMatthew G Knepley   Level: developer
1458f27dd7c6SMatthew G. Knepley 
1459f27dd7c6SMatthew G. Knepley   Options Database Keys:
1460f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1461f27dd7c6SMatthew G. Knepley 
1462b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1463732e2eb9SMatthew G Knepley @*/
1464732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1465732e2eb9SMatthew G Knepley {
1466732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1467732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1468732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1469732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1470732e2eb9SMatthew G Knepley }
1471732e2eb9SMatthew G Knepley 
1472b06ff27eSHong Zhang /*@
1473b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1474b06ff27eSHong Zhang     but the array for values will not be allocated.
1475b06ff27eSHong Zhang 
1476d083f849SBarry Smith   Logically Collective on dm
1477b06ff27eSHong Zhang 
1478d8d19677SJose E. Roman   Input Parameters:
1479b06ff27eSHong Zhang + dm - the DM
1480b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1481b06ff27eSHong Zhang 
1482b06ff27eSHong Zhang   Level: developer
1483b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1484b06ff27eSHong Zhang @*/
1485b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1486b06ff27eSHong Zhang {
1487b06ff27eSHong Zhang   PetscFunctionBegin;
1488b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1489b06ff27eSHong Zhang   dm->structure_only = only;
1490b06ff27eSHong Zhang   PetscFunctionReturn(0);
1491b06ff27eSHong Zhang }
1492b06ff27eSHong Zhang 
1493a89ea682SMatthew G Knepley /*@C
1494aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1495a89ea682SMatthew G Knepley 
1496a89ea682SMatthew G Knepley   Not Collective
1497a89ea682SMatthew G Knepley 
1498a89ea682SMatthew G Knepley   Input Parameters:
1499a89ea682SMatthew G Knepley + dm - the DM object
1500a5b23f4aSJose E. Roman . count - The minimum size
150169291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1502a89ea682SMatthew G Knepley 
1503a89ea682SMatthew G Knepley   Output Parameter:
1504a89ea682SMatthew G Knepley . array - the work array
1505a89ea682SMatthew G Knepley 
1506a89ea682SMatthew G Knepley   Level: developer
1507a89ea682SMatthew G Knepley 
1508a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1509a89ea682SMatthew G Knepley @*/
151069291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1511a89ea682SMatthew G Knepley {
1512a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1513aa1993deSMatthew G Knepley   DMWorkLink     link;
151469291d52SBarry Smith   PetscMPIInt    dsize;
1515a89ea682SMatthew G Knepley 
1516a89ea682SMatthew G Knepley   PetscFunctionBegin;
1517a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1518aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1519aa1993deSMatthew G Knepley   if (dm->workin) {
1520aa1993deSMatthew G Knepley     link       = dm->workin;
1521aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1522aa1993deSMatthew G Knepley   } else {
1523b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1524a89ea682SMatthew G Knepley   }
1525ffc4695bSBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRMPI(ierr);
15265056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1527aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1528854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1529854ce69bSBarry Smith     link->bytes = dsize*count;
1530aa1993deSMatthew G Knepley   }
1531aa1993deSMatthew G Knepley   link->next   = dm->workout;
1532aa1993deSMatthew G Knepley   dm->workout  = link;
153300d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
153400d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
153500d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
153600d952a4SJed Brown #endif
1537aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1538a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1539a89ea682SMatthew G Knepley }
1540a89ea682SMatthew G Knepley 
1541aa1993deSMatthew G Knepley /*@C
1542aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1543aa1993deSMatthew G Knepley 
1544aa1993deSMatthew G Knepley   Not Collective
1545aa1993deSMatthew G Knepley 
1546aa1993deSMatthew G Knepley   Input Parameters:
1547aa1993deSMatthew G Knepley + dm - the DM object
1548a5b23f4aSJose E. Roman . count - The minimum size
154969291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1550aa1993deSMatthew G Knepley 
1551aa1993deSMatthew G Knepley   Output Parameter:
1552aa1993deSMatthew G Knepley . array - the work array
1553aa1993deSMatthew G Knepley 
1554aa1993deSMatthew G Knepley   Level: developer
1555aa1993deSMatthew G Knepley 
155695452b02SPatrick Sanan   Developer Notes:
155795452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1558aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1559aa1993deSMatthew G Knepley @*/
156069291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1561aa1993deSMatthew G Knepley {
1562aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1563aa1993deSMatthew G Knepley 
1564aa1993deSMatthew G Knepley   PetscFunctionBegin;
1565aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1566aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1567aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1568aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1569aa1993deSMatthew G Knepley       *p           = link->next;
1570aa1993deSMatthew G Knepley       link->next   = dm->workin;
1571aa1993deSMatthew G Knepley       dm->workin   = link;
15720298fd71SBarry Smith       *(void**)mem = NULL;
1573aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1574aa1993deSMatthew G Knepley     }
1575aa1993deSMatthew G Knepley   }
1576aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1577aa1993deSMatthew G Knepley }
1578e7c4fc90SDmitry Karpeev 
15798cda7954SMatthew G. Knepley /*@C
15808cda7954SMatthew G. Knepley   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field
15818cda7954SMatthew G. Knepley 
15828cda7954SMatthew G. Knepley   Logically collective on DM
15838cda7954SMatthew G. Knepley 
15848cda7954SMatthew G. Knepley   Input Parameters:
15858cda7954SMatthew G. Knepley + dm     - The DM
15868cda7954SMatthew G. Knepley . field  - The field number for the nullspace
15878cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
15888cda7954SMatthew G. Knepley 
15898cda7954SMatthew G. Knepley   Notes:
15908cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15918cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15928cda7954SMatthew G. Knepley $ dm        - The present DM
15938cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15948cda7954SMatthew G. Knepley $ field     - The field number in dm
15958cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15968cda7954SMatthew G. Knepley 
15978cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
15988cda7954SMatthew G. Knepley 
15998cda7954SMatthew G. Knepley .seealso: DMGetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16008cda7954SMatthew G. Knepley */
16018cda7954SMatthew G. Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1602435a35e8SMatthew G Knepley {
1603435a35e8SMatthew G Knepley   PetscFunctionBegin;
1604435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
160582f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1606435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1607435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1608435a35e8SMatthew G Knepley }
1609435a35e8SMatthew G Knepley 
16108cda7954SMatthew G. Knepley /*@C
16118cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
16128cda7954SMatthew G. Knepley 
16138cda7954SMatthew G. Knepley   Not collective
16148cda7954SMatthew G. Knepley 
16158cda7954SMatthew G. Knepley   Input Parameters:
16168cda7954SMatthew G. Knepley + dm     - The DM
16178cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16188cda7954SMatthew G. Knepley 
16198cda7954SMatthew G. Knepley   Output Parameter:
16208cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
16218cda7954SMatthew G. Knepley 
16228cda7954SMatthew G. Knepley   Notes:
16238cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16248cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16258cda7954SMatthew G. Knepley $ dm        - The present DM
16268cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16278cda7954SMatthew G. Knepley $ field     - The field number in dm
16288cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16298cda7954SMatthew G. Knepley 
16308cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16318cda7954SMatthew G. Knepley 
16328cda7954SMatthew G. Knepley .seealso: DMSetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16338cda7954SMatthew G. Knepley */
16348cda7954SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
16350a50eb56SMatthew G. Knepley {
16360a50eb56SMatthew G. Knepley   PetscFunctionBegin;
16370a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1638f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
16390a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
16400a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
16410a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
16420a50eb56SMatthew G. Knepley }
16430a50eb56SMatthew G. Knepley 
16448cda7954SMatthew G. Knepley /*@C
16458cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
16468cda7954SMatthew G. Knepley 
16478cda7954SMatthew G. Knepley   Logically collective on DM
16488cda7954SMatthew G. Knepley 
16498cda7954SMatthew G. Knepley   Input Parameters:
16508cda7954SMatthew G. Knepley + dm     - The DM
16518cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16528cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
16538cda7954SMatthew G. Knepley 
16548cda7954SMatthew G. Knepley   Notes:
16558cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16568cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16578cda7954SMatthew G. Knepley $ dm        - The present DM
16588cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16598cda7954SMatthew G. Knepley $ field     - The field number in dm
16608cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16618cda7954SMatthew G. Knepley 
16628cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16638cda7954SMatthew G. Knepley 
16648cda7954SMatthew G. Knepley .seealso: DMGetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16658cda7954SMatthew G. Knepley */
16668cda7954SMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1667f9d4088aSMatthew G. Knepley {
1668f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1669f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1670f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1671f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1672f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1673f9d4088aSMatthew G. Knepley }
1674f9d4088aSMatthew G. Knepley 
16758cda7954SMatthew G. Knepley /*@C
16768cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
16778cda7954SMatthew G. Knepley 
16788cda7954SMatthew G. Knepley   Not collective
16798cda7954SMatthew G. Knepley 
16808cda7954SMatthew G. Knepley   Input Parameters:
16818cda7954SMatthew G. Knepley + dm     - The DM
16828cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16838cda7954SMatthew G. Knepley 
16848cda7954SMatthew G. Knepley   Output Parameter:
16858cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
16868cda7954SMatthew G. Knepley 
16878cda7954SMatthew G. Knepley   Notes:
16888cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16898cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16908cda7954SMatthew G. Knepley $ dm        - The present DM
16918cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16928cda7954SMatthew G. Knepley $ field     - The field number in dm
16938cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16948cda7954SMatthew G. Knepley 
16958cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16968cda7954SMatthew G. Knepley 
16978cda7954SMatthew G. Knepley .seealso: DMSetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16988cda7954SMatthew G. Knepley */
16998cda7954SMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1700f9d4088aSMatthew G. Knepley {
1701f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1702f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1703f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1704f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1705f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1706f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1707f9d4088aSMatthew G. Knepley }
1708f9d4088aSMatthew G. Knepley 
17094f3b5142SJed Brown /*@C
17104d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
17114d343eeaSMatthew G Knepley 
17124d343eeaSMatthew G Knepley   Not collective
17134d343eeaSMatthew G Knepley 
17144d343eeaSMatthew G Knepley   Input Parameter:
17154d343eeaSMatthew G Knepley . dm - the DM object
17164d343eeaSMatthew G Knepley 
17174d343eeaSMatthew G Knepley   Output Parameters:
17180298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
17190298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
17200298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
17214d343eeaSMatthew G Knepley 
17224d343eeaSMatthew G Knepley   Level: intermediate
17234d343eeaSMatthew G Knepley 
172421c9b008SJed Brown   Notes:
172521c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
172621c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
172721c9b008SJed Brown   PetscFree().
172821c9b008SJed Brown 
17294d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
17304d343eeaSMatthew G Knepley @*/
173137d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
17324d343eeaSMatthew G Knepley {
173337d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
17344d343eeaSMatthew G Knepley   PetscErrorCode ierr;
17354d343eeaSMatthew G Knepley 
17364d343eeaSMatthew G Knepley   PetscFunctionBegin;
17374d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
173869ca1f37SDmitry Karpeev   if (numFields) {
1739534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
174069ca1f37SDmitry Karpeev     *numFields = 0;
174169ca1f37SDmitry Karpeev   }
174237d0c07bSMatthew G Knepley   if (fieldNames) {
174337d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
17440298fd71SBarry Smith     *fieldNames = NULL;
174569ca1f37SDmitry Karpeev   }
174669ca1f37SDmitry Karpeev   if (fields) {
174769ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
17480298fd71SBarry Smith     *fields = NULL;
174969ca1f37SDmitry Karpeev   }
175092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
175137d0c07bSMatthew G Knepley   if (section) {
17523a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
175337d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
175437d0c07bSMatthew G Knepley 
1755e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
175637d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
17573a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
175837d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
175937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
176037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
17613a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
176237d0c07bSMatthew G Knepley     }
176337d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
176437d0c07bSMatthew G Knepley       PetscInt gdof;
176537d0c07bSMatthew G Knepley 
176637d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
176737d0c07bSMatthew G Knepley       if (gdof > 0) {
176837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
17693a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
177037d0c07bSMatthew G Knepley 
177137d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
177237d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
17733a544194SStefano Zampini           fpdof = fdof-fcdof;
17743a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
17753a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
17763a544194SStefano Zampini             fieldNc[f] = 1;
17773a544194SStefano Zampini           }
17783a544194SStefano Zampini           fieldSizes[f] += fpdof;
177937d0c07bSMatthew G Knepley         }
178037d0c07bSMatthew G Knepley       }
178137d0c07bSMatthew G Knepley     }
178237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1783785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
178437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
178537d0c07bSMatthew G Knepley     }
178637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
178737d0c07bSMatthew G Knepley       PetscInt gdof, goff;
178837d0c07bSMatthew G Knepley 
178937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
179037d0c07bSMatthew G Knepley       if (gdof > 0) {
179137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
179237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
179337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
179437d0c07bSMatthew G Knepley 
179537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
179637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
179737d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
179837d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
179937d0c07bSMatthew G Knepley           }
180037d0c07bSMatthew G Knepley         }
180137d0c07bSMatthew G Knepley       }
180237d0c07bSMatthew G Knepley     }
18038865f1eaSKarl Rupp     if (numFields) *numFields = nF;
180437d0c07bSMatthew G Knepley     if (fieldNames) {
1805785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
180637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
180737d0c07bSMatthew G Knepley         const char *fieldName;
180837d0c07bSMatthew G Knepley 
180937d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
181037d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
181137d0c07bSMatthew G Knepley       }
181237d0c07bSMatthew G Knepley     }
181337d0c07bSMatthew G Knepley     if (fields) {
1814785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
181537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
18163a544194SStefano Zampini         PetscInt bs, in[2], out[2];
18173a544194SStefano Zampini 
181882f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
18193a544194SStefano Zampini         in[0] = -fieldNc[f];
18203a544194SStefano Zampini         in[1] = fieldNc[f];
1821820f2d46SBarry Smith         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
18223a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
18233a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
182437d0c07bSMatthew G Knepley       }
182537d0c07bSMatthew G Knepley     }
18263a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
18278865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
18288865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
182969ca1f37SDmitry Karpeev   }
18304d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
18314d343eeaSMatthew G Knepley }
18324d343eeaSMatthew G Knepley 
183316621825SDmitry Karpeev /*@C
183416621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
183516621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
183616621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1837e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1838e7c4fc90SDmitry Karpeev 
1839e7c4fc90SDmitry Karpeev   Not collective
1840e7c4fc90SDmitry Karpeev 
1841e7c4fc90SDmitry Karpeev   Input Parameter:
1842e7c4fc90SDmitry Karpeev . dm - the DM object
1843e7c4fc90SDmitry Karpeev 
1844e7c4fc90SDmitry Karpeev   Output Parameters:
18450298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
18460298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
18470298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
18480298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1849e7c4fc90SDmitry Karpeev 
1850e7c4fc90SDmitry Karpeev   Level: intermediate
1851e7c4fc90SDmitry Karpeev 
1852e7c4fc90SDmitry Karpeev   Notes:
1853e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1854e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1855e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1856e7c4fc90SDmitry Karpeev 
1857e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1858e7c4fc90SDmitry Karpeev @*/
185916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1860e7c4fc90SDmitry Karpeev {
1861e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1862e7c4fc90SDmitry Karpeev 
1863e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1864e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18658865f1eaSKarl Rupp   if (len) {
1866534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
18678865f1eaSKarl Rupp     *len = 0;
18688865f1eaSKarl Rupp   }
18698865f1eaSKarl Rupp   if (namelist) {
18708865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1871ea78f98cSLisandro Dalcin     *namelist = NULL;
18728865f1eaSKarl Rupp   }
18738865f1eaSKarl Rupp   if (islist) {
18748865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1875ea78f98cSLisandro Dalcin     *islist = NULL;
18768865f1eaSKarl Rupp   }
18778865f1eaSKarl Rupp   if (dmlist) {
18788865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1879ea78f98cSLisandro Dalcin     *dmlist = NULL;
18808865f1eaSKarl Rupp   }
1881f3f0edfdSDmitry Karpeev   /*
1882f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1883f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1884f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1885f3f0edfdSDmitry Karpeev    */
1886ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
188716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1888435a35e8SMatthew G Knepley     PetscSection section;
1889435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1890435a35e8SMatthew G Knepley 
189192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1892435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1893435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1894f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
189503dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
189603dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
189703dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1898435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1899435a35e8SMatthew G Knepley         const char *fieldName;
1900435a35e8SMatthew G Knepley 
190103dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
190203dc3394SMatthew G. Knepley         if (namelist) {
1903435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1904435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1905435a35e8SMatthew G Knepley         }
190603dc3394SMatthew G. Knepley       }
1907435a35e8SMatthew G Knepley     } else {
190869ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1909e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
19100298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1911e7c4fc90SDmitry Karpeev     }
19128865f1eaSKarl Rupp   } else {
191316621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
191416621825SDmitry Karpeev   }
191516621825SDmitry Karpeev   PetscFunctionReturn(0);
191616621825SDmitry Karpeev }
191716621825SDmitry Karpeev 
1918564cec59SMatthew G. Knepley /*@
1919435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1920435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1921435a35e8SMatthew G Knepley 
1922435a35e8SMatthew G Knepley   Not collective
1923435a35e8SMatthew G Knepley 
1924435a35e8SMatthew G Knepley   Input Parameters:
19252adcc780SMatthew G. Knepley + dm        - The DM object
19262adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
19272adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1928435a35e8SMatthew G Knepley 
1929435a35e8SMatthew G Knepley   Output Parameters:
19302adcc780SMatthew G. Knepley + is - The global indices for the subproblem
19312adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1932435a35e8SMatthew G Knepley 
19335d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19345d3b26e6SMatthew G. Knepley 
1935435a35e8SMatthew G Knepley   Level: intermediate
1936435a35e8SMatthew G Knepley 
19375d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1938435a35e8SMatthew G Knepley @*/
193937bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1940435a35e8SMatthew G Knepley {
1941435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1942435a35e8SMatthew G Knepley 
1943435a35e8SMatthew G Knepley   PetscFunctionBegin;
1944435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1945435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
19468865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
19478865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1948b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1949435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1950435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1951435a35e8SMatthew G Knepley }
1952435a35e8SMatthew G Knepley 
19532adcc780SMatthew G. Knepley /*@C
19542adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
19552adcc780SMatthew G. Knepley 
19562adcc780SMatthew G. Knepley   Not collective
19572adcc780SMatthew G. Knepley 
1958d8d19677SJose E. Roman   Input Parameters:
19592adcc780SMatthew G. Knepley + dms - The DM objects
19602adcc780SMatthew G. Knepley - len - The number of DMs
19612adcc780SMatthew G. Knepley 
19622adcc780SMatthew G. Knepley   Output Parameters:
1963a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
19642adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
19652adcc780SMatthew G. Knepley 
19665d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19675d3b26e6SMatthew G. Knepley 
19682adcc780SMatthew G. Knepley   Level: intermediate
19692adcc780SMatthew G. Knepley 
19705d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
19712adcc780SMatthew G. Knepley @*/
19722adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
19732adcc780SMatthew G. Knepley {
19742adcc780SMatthew G. Knepley   PetscInt       i;
19752adcc780SMatthew G. Knepley   PetscErrorCode ierr;
19762adcc780SMatthew G. Knepley 
19772adcc780SMatthew G. Knepley   PetscFunctionBegin;
19782adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
19792adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
19802adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1981a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
19822adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
19832adcc780SMatthew G. Knepley   if (len) {
1984b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1985b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1986b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
19872adcc780SMatthew G. Knepley   }
19882adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
19892adcc780SMatthew G. Knepley }
19902adcc780SMatthew G. Knepley 
199116621825SDmitry Karpeev /*@C
19928d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
19938d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
19948d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
19958d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
19968d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
199716621825SDmitry Karpeev 
199816621825SDmitry Karpeev   Not collective
199916621825SDmitry Karpeev 
200016621825SDmitry Karpeev   Input Parameter:
200116621825SDmitry Karpeev . dm - the DM object
200216621825SDmitry Karpeev 
200316621825SDmitry Karpeev   Output Parameters:
20040298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
20050298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
20060298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
20070298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
20080298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
200916621825SDmitry Karpeev 
201016621825SDmitry Karpeev   Level: intermediate
201116621825SDmitry Karpeev 
201216621825SDmitry Karpeev   Notes:
201316621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
201416621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
201516621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
201616621825SDmitry Karpeev 
2017245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
201816621825SDmitry Karpeev @*/
20198d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
202016621825SDmitry Karpeev {
202116621825SDmitry Karpeev   PetscErrorCode      ierr;
2022be081cd6SPeter Brune   DMSubDomainHookLink link;
2023be081cd6SPeter Brune   PetscInt            i,l;
202416621825SDmitry Karpeev 
202516621825SDmitry Karpeev   PetscFunctionBegin;
202616621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
202714a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
20280298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
20290298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
20300298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
20310298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
2032f3f0edfdSDmitry Karpeev   /*
2033f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2034f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2035f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2036f3f0edfdSDmitry Karpeev    */
2037ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
203816621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2039be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
204014a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2041f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2042be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2043be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
2044be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
2045be081cd6SPeter Brune         }
2046648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2047e7c4fc90SDmitry Karpeev       }
204814a18fd3SPeter Brune     }
204914a18fd3SPeter Brune     if (len) *len = l;
205014a18fd3SPeter Brune   }
2051e30e807fSPeter Brune   PetscFunctionReturn(0);
2052e30e807fSPeter Brune }
2053e30e807fSPeter Brune 
2054e30e807fSPeter Brune /*@C
2055e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2056e30e807fSPeter Brune 
2057e30e807fSPeter Brune   Not collective
2058e30e807fSPeter Brune 
2059e30e807fSPeter Brune   Input Parameters:
2060e30e807fSPeter Brune + dm - the DM object
2061e30e807fSPeter Brune . n  - the number of subdomain scatters
2062e30e807fSPeter Brune - subdms - the local subdomains
2063e30e807fSPeter Brune 
2064e30e807fSPeter Brune   Output Parameters:
20656b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2066e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2067e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2068e30e807fSPeter Brune 
206995452b02SPatrick Sanan   Notes:
207095452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2071e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2072e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2073e30e807fSPeter Brune   solution and residual data.
2074e30e807fSPeter Brune 
2075e30e807fSPeter Brune   Level: developer
2076e30e807fSPeter Brune 
2077e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2078e30e807fSPeter Brune @*/
2079e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2080e30e807fSPeter Brune {
2081e30e807fSPeter Brune   PetscErrorCode ierr;
2082e30e807fSPeter Brune 
2083e30e807fSPeter Brune   PetscFunctionBegin;
2084e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2085e30e807fSPeter Brune   PetscValidPointer(subdms,3);
2086b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
2087e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
2088e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2089e7c4fc90SDmitry Karpeev }
2090e7c4fc90SDmitry Karpeev 
209147c6ae99SBarry Smith /*@
209247c6ae99SBarry Smith   DMRefine - Refines a DM object
209347c6ae99SBarry Smith 
2094d083f849SBarry Smith   Collective on dm
209547c6ae99SBarry Smith 
2096d8d19677SJose E. Roman   Input Parameters:
209747c6ae99SBarry Smith + dm   - the DM object
209891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
209947c6ae99SBarry Smith 
210047c6ae99SBarry Smith   Output Parameter:
21010298fd71SBarry Smith . dmf - the refined DM, or NULL
2102ae0a1c52SMatthew G Knepley 
2103f27dd7c6SMatthew G. Knepley   Options Database Keys:
2104412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2105412e9a14SMatthew G. Knepley 
21060298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
210747c6ae99SBarry Smith 
210847c6ae99SBarry Smith   Level: developer
210947c6ae99SBarry Smith 
2110e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
211147c6ae99SBarry Smith @*/
21127087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
211347c6ae99SBarry Smith {
211447c6ae99SBarry Smith   PetscErrorCode   ierr;
2115c833c3b5SJed Brown   DMRefineHookLink link;
211647c6ae99SBarry Smith 
211747c6ae99SBarry Smith   PetscFunctionBegin;
2118732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2119b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
21201ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
212147c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
21224057135bSMatthew G Knepley   if (*dmf) {
212343842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
21248865f1eaSKarl Rupp 
21258cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
21268865f1eaSKarl Rupp 
2127644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
21280598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2129656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
21308865f1eaSKarl Rupp 
2131e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
2132c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
21338865f1eaSKarl Rupp       if (link->refinehook) {
21348865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
21358865f1eaSKarl Rupp       }
2136c833c3b5SJed Brown     }
2137c833c3b5SJed Brown   }
21381ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
2139c833c3b5SJed Brown   PetscFunctionReturn(0);
2140c833c3b5SJed Brown }
2141c833c3b5SJed Brown 
2142bb9467b5SJed Brown /*@C
2143c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2144c833c3b5SJed Brown 
2145c833c3b5SJed Brown    Logically Collective
2146c833c3b5SJed Brown 
21474165533cSJose E. Roman    Input Parameters:
2148c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2149c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2150c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21510298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2152c833c3b5SJed Brown 
2153c833c3b5SJed Brown    Calling sequence of refinehook:
2154c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2155c833c3b5SJed Brown 
2156c833c3b5SJed Brown +  coarse - coarse level DM
2157c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2158c833c3b5SJed Brown -  ctx - optional user-defined function context
2159c833c3b5SJed Brown 
2160c833c3b5SJed Brown    Calling sequence for interphook:
2161c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2162c833c3b5SJed Brown 
2163c833c3b5SJed Brown +  coarse - coarse level DM
2164c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2165c833c3b5SJed Brown .  fine - fine level DM to update
2166c833c3b5SJed Brown -  ctx - optional user-defined function context
2167c833c3b5SJed Brown 
2168c833c3b5SJed Brown    Level: advanced
2169c833c3b5SJed Brown 
2170c833c3b5SJed Brown    Notes:
2171c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2172c833c3b5SJed Brown 
2173c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2174c833c3b5SJed Brown 
2175bb9467b5SJed Brown    This function is currently not available from Fortran.
2176bb9467b5SJed Brown 
2177c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2178c833c3b5SJed Brown @*/
2179c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2180c833c3b5SJed Brown {
2181c833c3b5SJed Brown   PetscErrorCode   ierr;
2182c833c3b5SJed Brown   DMRefineHookLink link,*p;
2183c833c3b5SJed Brown 
2184c833c3b5SJed Brown   PetscFunctionBegin;
2185c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
21863d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
21873d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
21883d8e3701SJed Brown   }
218995dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2190c833c3b5SJed Brown   link->refinehook = refinehook;
2191c833c3b5SJed Brown   link->interphook = interphook;
2192c833c3b5SJed Brown   link->ctx        = ctx;
21930298fd71SBarry Smith   link->next       = NULL;
2194c833c3b5SJed Brown   *p               = link;
2195c833c3b5SJed Brown   PetscFunctionReturn(0);
2196c833c3b5SJed Brown }
2197c833c3b5SJed Brown 
21983d8e3701SJed Brown /*@C
21993d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
22003d8e3701SJed Brown 
22013d8e3701SJed Brown    Logically Collective
22023d8e3701SJed Brown 
22034165533cSJose E. Roman    Input Parameters:
22043d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
22053d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
22063d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
22073d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22083d8e3701SJed Brown 
22093d8e3701SJed Brown    Level: advanced
22103d8e3701SJed Brown 
22113d8e3701SJed Brown    Notes:
22123d8e3701SJed Brown    This function does nothing if the hook is not in the list.
22133d8e3701SJed Brown 
22143d8e3701SJed Brown    This function is currently not available from Fortran.
22153d8e3701SJed Brown 
22163d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
22173d8e3701SJed Brown @*/
22183d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
22193d8e3701SJed Brown {
22203d8e3701SJed Brown   PetscErrorCode   ierr;
22213d8e3701SJed Brown   DMRefineHookLink link,*p;
22223d8e3701SJed Brown 
22233d8e3701SJed Brown   PetscFunctionBegin;
22243d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22253d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
22263d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
22273d8e3701SJed Brown       link = *p;
22283d8e3701SJed Brown       *p = link->next;
22293d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
22303d8e3701SJed Brown       break;
22313d8e3701SJed Brown     }
22323d8e3701SJed Brown   }
22333d8e3701SJed Brown   PetscFunctionReturn(0);
22343d8e3701SJed Brown }
22353d8e3701SJed Brown 
2236c833c3b5SJed Brown /*@
2237c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2238c833c3b5SJed Brown 
2239c833c3b5SJed Brown    Collective if any hooks are
2240c833c3b5SJed Brown 
22414165533cSJose E. Roman    Input Parameters:
2242c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2243e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2244c833c3b5SJed Brown -  fine - finer DM to update
2245c833c3b5SJed Brown 
2246c833c3b5SJed Brown    Level: developer
2247c833c3b5SJed Brown 
2248c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2249c833c3b5SJed Brown @*/
2250c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2251c833c3b5SJed Brown {
2252c833c3b5SJed Brown   PetscErrorCode   ierr;
2253c833c3b5SJed Brown   DMRefineHookLink link;
2254c833c3b5SJed Brown 
2255c833c3b5SJed Brown   PetscFunctionBegin;
2256c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
22578865f1eaSKarl Rupp     if (link->interphook) {
22588865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
22598865f1eaSKarl Rupp     }
22604057135bSMatthew G Knepley   }
226147c6ae99SBarry Smith   PetscFunctionReturn(0);
226247c6ae99SBarry Smith }
226347c6ae99SBarry Smith 
2264eb3f98d2SBarry Smith /*@
22651f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
22661f3379b2SToby Isaac 
22671f3379b2SToby Isaac    Collective on DM
22681f3379b2SToby Isaac 
22694165533cSJose E. Roman    Input Parameters:
22701f3379b2SToby Isaac +  coarse - coarse DM
22711f3379b2SToby Isaac .  fine   - fine DM
22721f3379b2SToby Isaac .  interp - (optional) the matrix computed by DMCreateInterpolation().  Implementations may not need this, but if it
22731f3379b2SToby Isaac             is available it can avoid some recomputation.  If it is provided, MatInterpolate() will be used if
22741f3379b2SToby Isaac             the coarse DM does not have a specialized implementation.
22751f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
22761f3379b2SToby Isaac 
22774165533cSJose E. Roman    Output Parameter:
22781f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
22791f3379b2SToby Isaac 
22801f3379b2SToby Isaac    Level: developer
22811f3379b2SToby Isaac 
22821f3379b2SToby Isaac    Note: This function exists because the interpolation of a solution vector between meshes is not always a linear
22831f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
22841f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
22851f3379b2SToby Isaac    slope-limiting reconstruction.
22861f3379b2SToby Isaac 
22871f3379b2SToby Isaac .seealso DMInterpolate(), DMCreateInterpolation()
22881f3379b2SToby Isaac @*/
22891f3379b2SToby Isaac PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
22901f3379b2SToby Isaac {
22911f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM,DM,Mat,Vec,Vec) = NULL;
22921f3379b2SToby Isaac   PetscErrorCode ierr;
22931f3379b2SToby Isaac 
22941f3379b2SToby Isaac   PetscFunctionBegin;
22951f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22961f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp,MAT_CLASSID,3);
22971f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol,VEC_CLASSID,4);
22981f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol,VEC_CLASSID,5);
22991f3379b2SToby Isaac 
23001f3379b2SToby Isaac   ierr = PetscObjectQueryFunction((PetscObject)coarse,"DMInterpolateSolution_C", &interpsol);CHKERRQ(ierr);
23011f3379b2SToby Isaac   if (interpsol) {
23021f3379b2SToby Isaac     ierr = (*interpsol)(coarse, fine, interp, coarseSol, fineSol);CHKERRQ(ierr);
23031f3379b2SToby Isaac   } else if (interp) {
23041f3379b2SToby Isaac     ierr = MatInterpolate(interp, coarseSol, fineSol);CHKERRQ(ierr);
23051f3379b2SToby Isaac   } else SETERRQ1(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
23061f3379b2SToby Isaac   PetscFunctionReturn(0);
23071f3379b2SToby Isaac }
23081f3379b2SToby Isaac 
23091f3379b2SToby Isaac /*@
2310aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2311eb3f98d2SBarry Smith 
2312eb3f98d2SBarry Smith     Not Collective
2313eb3f98d2SBarry Smith 
2314eb3f98d2SBarry Smith     Input Parameter:
2315eb3f98d2SBarry Smith .   dm - the DM object
2316eb3f98d2SBarry Smith 
2317eb3f98d2SBarry Smith     Output Parameter:
2318eb3f98d2SBarry Smith .   level - number of refinements
2319eb3f98d2SBarry Smith 
2320eb3f98d2SBarry Smith     Level: developer
2321eb3f98d2SBarry Smith 
23226a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2323eb3f98d2SBarry Smith 
2324eb3f98d2SBarry Smith @*/
2325eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2326eb3f98d2SBarry Smith {
2327eb3f98d2SBarry Smith   PetscFunctionBegin;
2328eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2329eb3f98d2SBarry Smith   *level = dm->levelup;
2330eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2331eb3f98d2SBarry Smith }
2332eb3f98d2SBarry Smith 
2333fef3a512SBarry Smith /*@
2334aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2335fef3a512SBarry Smith 
2336fef3a512SBarry Smith     Not Collective
2337fef3a512SBarry Smith 
2338d8d19677SJose E. Roman     Input Parameters:
2339fef3a512SBarry Smith +   dm - the DM object
2340fef3a512SBarry Smith -   level - number of refinements
2341fef3a512SBarry Smith 
2342fef3a512SBarry Smith     Level: advanced
2343fef3a512SBarry Smith 
234495452b02SPatrick Sanan     Notes:
234595452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2346fef3a512SBarry Smith 
2347fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2348fef3a512SBarry Smith 
2349fef3a512SBarry Smith @*/
2350fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2351fef3a512SBarry Smith {
2352fef3a512SBarry Smith   PetscFunctionBegin;
2353fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2354fef3a512SBarry Smith   dm->levelup = level;
2355fef3a512SBarry Smith   PetscFunctionReturn(0);
2356fef3a512SBarry Smith }
2357fef3a512SBarry Smith 
2358ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2359ca3d3a14SMatthew G. Knepley {
2360ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2361ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2362ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2363ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2364ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2365ca3d3a14SMatthew G. Knepley }
2366ca3d3a14SMatthew G. Knepley 
2367ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2368ca3d3a14SMatthew G. Knepley {
2369ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2370ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2371ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2372ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2373ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2374ca3d3a14SMatthew G. Knepley }
2375ca3d3a14SMatthew G. Knepley 
2376ca3d3a14SMatthew G. Knepley /*@
2377c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2378ca3d3a14SMatthew G. Knepley 
2379ca3d3a14SMatthew G. Knepley   Input Parameter:
2380ca3d3a14SMatthew G. Knepley . dm - The DM
2381ca3d3a14SMatthew G. Knepley 
2382ca3d3a14SMatthew G. Knepley   Output Parameter:
2383ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2384ca3d3a14SMatthew G. Knepley 
2385ca3d3a14SMatthew G. Knepley   Level: developer
2386ca3d3a14SMatthew G. Knepley 
2387436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2388ca3d3a14SMatthew G. Knepley @*/
2389ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2390ca3d3a14SMatthew G. Knepley {
2391ca3d3a14SMatthew G. Knepley   Vec            tv;
2392ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2393ca3d3a14SMatthew G. Knepley 
2394ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2395ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2396534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2397ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2398ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2399ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2400ca3d3a14SMatthew G. Knepley }
2401ca3d3a14SMatthew G. Knepley 
2402ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2403ca3d3a14SMatthew G. Knepley {
2404ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2405ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2406ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2407ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2408ca3d3a14SMatthew G. Knepley 
2409ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2410ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
241192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2412ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2413ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2414ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
241592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2416ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2417ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2418ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2419ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2420ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2421ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2422ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2423ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2424ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2425ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2426ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2427ca3d3a14SMatthew G. Knepley     }
2428ca3d3a14SMatthew G. Knepley   }
2429ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2430ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2431ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2432ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2433ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2434ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2435ca3d3a14SMatthew G. Knepley       if (dof) {
2436ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2437ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2438ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2439ca3d3a14SMatthew G. Knepley 
2440ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2441ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2442ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2443580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2444ca3d3a14SMatthew G. Knepley       }
2445ca3d3a14SMatthew G. Knepley     }
2446ca3d3a14SMatthew G. Knepley   }
2447ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2448ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2449ca3d3a14SMatthew G. Knepley }
2450ca3d3a14SMatthew G. Knepley 
2451ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2452ca3d3a14SMatthew G. Knepley {
2453ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2454ca3d3a14SMatthew G. Knepley 
2455ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2456ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2457ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2458ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2459ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2460ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2461ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2462ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2463ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2464ca3d3a14SMatthew G. Knepley }
2465ca3d3a14SMatthew G. Knepley 
2466bb9467b5SJed Brown /*@C
2467baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2468baf369e7SPeter Brune 
2469baf369e7SPeter Brune    Logically Collective
2470baf369e7SPeter Brune 
24714165533cSJose E. Roman    Input Parameters:
2472baf369e7SPeter Brune +  dm - the DM
2473baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2474baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
24750298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2476baf369e7SPeter Brune 
2477baf369e7SPeter Brune    Calling sequence for beginhook:
2478baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2479baf369e7SPeter Brune 
2480baf369e7SPeter Brune +  dm - global DM
2481baf369e7SPeter Brune .  g - global vector
2482baf369e7SPeter Brune .  mode - mode
2483baf369e7SPeter Brune .  l - local vector
2484baf369e7SPeter Brune -  ctx - optional user-defined function context
2485baf369e7SPeter Brune 
2486baf369e7SPeter Brune    Calling sequence for endhook:
2487ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2488baf369e7SPeter Brune 
2489baf369e7SPeter Brune +  global - global DM
2490baf369e7SPeter Brune -  ctx - optional user-defined function context
2491baf369e7SPeter Brune 
2492baf369e7SPeter Brune    Level: advanced
2493baf369e7SPeter Brune 
2494baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2495baf369e7SPeter Brune @*/
2496baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2497baf369e7SPeter Brune {
2498baf369e7SPeter Brune   PetscErrorCode          ierr;
2499baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2500baf369e7SPeter Brune 
2501baf369e7SPeter Brune   PetscFunctionBegin;
2502baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2503baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
250495dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2505baf369e7SPeter Brune   link->beginhook = beginhook;
2506baf369e7SPeter Brune   link->endhook   = endhook;
2507baf369e7SPeter Brune   link->ctx       = ctx;
25080298fd71SBarry Smith   link->next      = NULL;
2509baf369e7SPeter Brune   *p              = link;
2510baf369e7SPeter Brune   PetscFunctionReturn(0);
2511baf369e7SPeter Brune }
2512baf369e7SPeter Brune 
25134c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
25144c274da1SToby Isaac {
25154c274da1SToby Isaac   Mat cMat;
25164c274da1SToby Isaac   Vec cVec;
25174c274da1SToby Isaac   PetscSection section, cSec;
25184c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25194c274da1SToby Isaac   PetscErrorCode ierr;
25204c274da1SToby Isaac 
25214c274da1SToby Isaac   PetscFunctionBegin;
25224c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25234c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
25244c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
25255db9a05bSToby Isaac     PetscInt nRows;
25265db9a05bSToby Isaac 
25275db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
25285db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
252992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
25307711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
25314c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
25324c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
25334c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25344c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
25354c274da1SToby Isaac       if (dof) {
25364c274da1SToby Isaac         PetscScalar *vals;
25374c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
25384c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
25394c274da1SToby Isaac       }
25404c274da1SToby Isaac     }
25414c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
25424c274da1SToby Isaac   }
25434c274da1SToby Isaac   PetscFunctionReturn(0);
25444c274da1SToby Isaac }
25454c274da1SToby Isaac 
254647c6ae99SBarry Smith /*@
254701729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
254801729b5cSPatrick Sanan 
2549d083f849SBarry Smith     Neighbor-wise Collective on dm
255001729b5cSPatrick Sanan 
255101729b5cSPatrick Sanan     Input Parameters:
255201729b5cSPatrick Sanan +   dm - the DM object
255301729b5cSPatrick Sanan .   g - the global vector
255401729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
255501729b5cSPatrick Sanan -   l - the local vector
255601729b5cSPatrick Sanan 
255701729b5cSPatrick Sanan     Notes:
255801729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
255901729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
256001729b5cSPatrick Sanan 
256101729b5cSPatrick Sanan     Level: beginner
256201729b5cSPatrick Sanan 
256301729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
256401729b5cSPatrick Sanan 
256501729b5cSPatrick Sanan @*/
256601729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
256701729b5cSPatrick Sanan {
256801729b5cSPatrick Sanan   PetscErrorCode ierr;
256901729b5cSPatrick Sanan 
257001729b5cSPatrick Sanan   PetscFunctionBegin;
257101729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
257201729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
257301729b5cSPatrick Sanan   PetscFunctionReturn(0);
257401729b5cSPatrick Sanan }
257501729b5cSPatrick Sanan 
257601729b5cSPatrick Sanan /*@
257747c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
257847c6ae99SBarry Smith 
2579d083f849SBarry Smith     Neighbor-wise Collective on dm
258047c6ae99SBarry Smith 
258147c6ae99SBarry Smith     Input Parameters:
258247c6ae99SBarry Smith +   dm - the DM object
258347c6ae99SBarry Smith .   g - the global vector
258447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
258547c6ae99SBarry Smith -   l - the local vector
258647c6ae99SBarry Smith 
258701729b5cSPatrick Sanan     Level: intermediate
258847c6ae99SBarry Smith 
258901729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
259047c6ae99SBarry Smith 
259147c6ae99SBarry Smith @*/
25927087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
259347c6ae99SBarry Smith {
25947128ae9fSMatthew G Knepley   PetscSF                 sf;
259547c6ae99SBarry Smith   PetscErrorCode          ierr;
2596baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
259747c6ae99SBarry Smith 
259847c6ae99SBarry Smith   PetscFunctionBegin;
2599171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2600baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
26018865f1eaSKarl Rupp     if (link->beginhook) {
26028865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
26038865f1eaSKarl Rupp     }
2604baf369e7SPeter Brune   }
26051bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
26067128ae9fSMatthew G Knepley   if (sf) {
2607ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2608ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2609d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
26107128ae9fSMatthew G Knepley 
261182f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2612a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2613a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2614ad227feaSJunchao Zhang     ierr = PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE);CHKERRQ(ierr);
2615a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2616a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
26177128ae9fSMatthew G Knepley   } else {
261833907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2619843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
26207128ae9fSMatthew G Knepley   }
262147c6ae99SBarry Smith   PetscFunctionReturn(0);
262247c6ae99SBarry Smith }
262347c6ae99SBarry Smith 
262447c6ae99SBarry Smith /*@
262547c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
262647c6ae99SBarry Smith 
2627d083f849SBarry Smith     Neighbor-wise Collective on dm
262847c6ae99SBarry Smith 
262947c6ae99SBarry Smith     Input Parameters:
263047c6ae99SBarry Smith +   dm - the DM object
263147c6ae99SBarry Smith .   g - the global vector
263247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
263347c6ae99SBarry Smith -   l - the local vector
263447c6ae99SBarry Smith 
263501729b5cSPatrick Sanan     Level: intermediate
263647c6ae99SBarry Smith 
263701729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
263847c6ae99SBarry Smith 
263947c6ae99SBarry Smith @*/
26407087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
264147c6ae99SBarry Smith {
26427128ae9fSMatthew G Knepley   PetscSF                 sf;
264347c6ae99SBarry Smith   PetscErrorCode          ierr;
2644ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2645ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2646ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2647baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2648d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
264947c6ae99SBarry Smith 
265047c6ae99SBarry Smith   PetscFunctionBegin;
2651171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26521bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2653ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
26547128ae9fSMatthew G Knepley   if (sf) {
265582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26567128ae9fSMatthew G Knepley 
2657a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2658a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2659ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray,MPI_REPLACE);CHKERRQ(ierr);
2660a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2661a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
2662ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
26637128ae9fSMatthew G Knepley   } else {
266433907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2665843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
26667128ae9fSMatthew G Knepley   }
26674c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2668baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2669baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2670baf369e7SPeter Brune   }
267147c6ae99SBarry Smith   PetscFunctionReturn(0);
267247c6ae99SBarry Smith }
267347c6ae99SBarry Smith 
2674d4d07f1eSToby Isaac /*@C
2675d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2676d4d07f1eSToby Isaac 
2677d4d07f1eSToby Isaac    Logically Collective
2678d4d07f1eSToby Isaac 
26794165533cSJose E. Roman    Input Parameters:
2680d4d07f1eSToby Isaac +  dm - the DM
2681d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2682d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2683d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2684d4d07f1eSToby Isaac 
2685d4d07f1eSToby Isaac    Calling sequence for beginhook:
2686d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2687d4d07f1eSToby Isaac 
2688d4d07f1eSToby Isaac +  dm - global DM
2689d4d07f1eSToby Isaac .  l - local vector
2690d4d07f1eSToby Isaac .  mode - mode
2691d4d07f1eSToby Isaac .  g - global vector
2692d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2693d4d07f1eSToby Isaac 
2694d4d07f1eSToby Isaac    Calling sequence for endhook:
2695d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2696d4d07f1eSToby Isaac 
2697d4d07f1eSToby Isaac +  global - global DM
2698d4d07f1eSToby Isaac .  l - local vector
2699d4d07f1eSToby Isaac .  mode - mode
2700d4d07f1eSToby Isaac .  g - global vector
2701d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2702d4d07f1eSToby Isaac 
2703d4d07f1eSToby Isaac    Level: advanced
2704d4d07f1eSToby Isaac 
2705d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2706d4d07f1eSToby Isaac @*/
2707d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2708d4d07f1eSToby Isaac {
2709d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2710d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2711d4d07f1eSToby Isaac 
2712d4d07f1eSToby Isaac   PetscFunctionBegin;
2713d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2714d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
271595dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2716d4d07f1eSToby Isaac   link->beginhook = beginhook;
2717d4d07f1eSToby Isaac   link->endhook   = endhook;
2718d4d07f1eSToby Isaac   link->ctx       = ctx;
2719d4d07f1eSToby Isaac   link->next      = NULL;
2720d4d07f1eSToby Isaac   *p              = link;
2721d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2722d4d07f1eSToby Isaac }
2723d4d07f1eSToby Isaac 
27244c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
27254c274da1SToby Isaac {
27264c274da1SToby Isaac   Mat cMat;
27274c274da1SToby Isaac   Vec cVec;
27284c274da1SToby Isaac   PetscSection section, cSec;
27294c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
27304c274da1SToby Isaac   PetscErrorCode ierr;
27314c274da1SToby Isaac 
27324c274da1SToby Isaac   PetscFunctionBegin;
27334c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27344c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
27354c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
27365db9a05bSToby Isaac     PetscInt nRows;
27375db9a05bSToby Isaac 
27385db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
27395db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
274092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
27417711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
27424c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
27434c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27444c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
27454c274da1SToby Isaac       if (dof) {
27464c274da1SToby Isaac         PetscInt d;
27474c274da1SToby Isaac         PetscScalar *vals;
27484c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
27494c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
27504c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
27514c274da1SToby Isaac          * we just extracted */
27524c274da1SToby Isaac         for (d = 0; d < dof; d++) {
27534c274da1SToby Isaac           vals[d] = 0.;
27544c274da1SToby Isaac         }
27554c274da1SToby Isaac       }
27564c274da1SToby Isaac     }
27574c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
27584c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
27594c274da1SToby Isaac   }
27604c274da1SToby Isaac   PetscFunctionReturn(0);
27614c274da1SToby Isaac }
276201729b5cSPatrick Sanan /*@
276301729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
276401729b5cSPatrick Sanan 
2765d083f849SBarry Smith     Neighbor-wise Collective on dm
276601729b5cSPatrick Sanan 
276701729b5cSPatrick Sanan     Input Parameters:
276801729b5cSPatrick Sanan +   dm - the DM object
276901729b5cSPatrick Sanan .   l - the local vector
277001729b5cSPatrick 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.
277101729b5cSPatrick Sanan -   g - the global vector
277201729b5cSPatrick Sanan 
277301729b5cSPatrick Sanan     Notes:
277401729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
277501729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
277601729b5cSPatrick Sanan 
277701729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
277801729b5cSPatrick 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.
277901729b5cSPatrick Sanan 
278001729b5cSPatrick Sanan     Level: beginner
278101729b5cSPatrick Sanan 
278201729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
278301729b5cSPatrick Sanan 
278401729b5cSPatrick Sanan @*/
278501729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
278601729b5cSPatrick Sanan {
278701729b5cSPatrick Sanan   PetscErrorCode ierr;
278801729b5cSPatrick Sanan 
278901729b5cSPatrick Sanan   PetscFunctionBegin;
279001729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
279101729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
279201729b5cSPatrick Sanan   PetscFunctionReturn(0);
279301729b5cSPatrick Sanan }
27944c274da1SToby Isaac 
279547c6ae99SBarry Smith /*@
279601729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
27979a42bb27SBarry Smith 
2798d083f849SBarry Smith     Neighbor-wise Collective on dm
27999a42bb27SBarry Smith 
28009a42bb27SBarry Smith     Input Parameters:
28019a42bb27SBarry Smith +   dm - the DM object
2802f6813fd5SJed Brown .   l - the local vector
28031eb28f2eSBarry 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.
28041eb28f2eSBarry Smith -   g - the global vector
28059a42bb27SBarry Smith 
280695452b02SPatrick Sanan     Notes:
280795452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
280884330215SMatthew 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.
28099a42bb27SBarry Smith 
281001729b5cSPatrick Sanan     Level: intermediate
28119a42bb27SBarry Smith 
281201729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
28139a42bb27SBarry Smith 
28149a42bb27SBarry Smith @*/
28157087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
28169a42bb27SBarry Smith {
28177128ae9fSMatthew G Knepley   PetscSF                 sf;
281884330215SMatthew G. Knepley   PetscSection            s, gs;
2819d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2820ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2821ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2822ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2823fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
282484330215SMatthew G. Knepley   PetscErrorCode          ierr;
2825d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
28269a42bb27SBarry Smith 
28279a42bb27SBarry Smith   PetscFunctionBegin;
2828171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2829d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2830d4d07f1eSToby Isaac     if (link->beginhook) {
2831d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2832d4d07f1eSToby Isaac     }
2833d4d07f1eSToby Isaac   }
28344c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
28351bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
283692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
28377128ae9fSMatthew G Knepley   switch (mode) {
28387128ae9fSMatthew G Knepley   case INSERT_VALUES:
28397128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2840304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
284184330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
28427128ae9fSMatthew G Knepley   case ADD_VALUES:
28437128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2844304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
284584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
28467128ae9fSMatthew G Knepley   default:
284782f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
28487128ae9fSMatthew G Knepley   }
2849ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2850ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2851ca3d3a14SMatthew G. Knepley     if (transform) {
2852ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2853ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2854ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2855ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2856fa88e482SJed Brown     } else if (isInsert) {
2857ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2858fa88e482SJed Brown     } else {
2859a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2860fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2861ca3d3a14SMatthew G. Knepley     }
2862fa88e482SJed Brown     if (s && isInsert) {
28637128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2864fa88e482SJed Brown     } else {
2865a256111fSJunchao Zhang       ierr = VecGetArrayAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2866fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2867fa88e482SJed Brown     }
2868ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2869d0295fc0SJunchao Zhang       ierr = PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM);CHKERRQ(ierr);
287084330215SMatthew G. Knepley     } else if (s && isInsert) {
287184330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
287284330215SMatthew G. Knepley 
2873e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
287484330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
287584330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
287684330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2877b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
287884330215SMatthew G. Knepley 
287984330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
288003442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
288184330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2882b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
288384330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
288484330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2885b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
288603442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2887b3b16f48SMatthew 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);
2888b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2889b3b16f48SMatthew G. Knepley         if (!gcdof) {
289084330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2891b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2892b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
289384330215SMatthew G. Knepley           const PetscInt *cdofs;
289484330215SMatthew G. Knepley           PetscInt        cind = 0;
289584330215SMatthew G. Knepley 
289684330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2897b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
289884330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2899b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
290084330215SMatthew G. Knepley           }
2901b3b16f48SMatthew 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);
290284330215SMatthew G. Knepley       }
2903ca3d3a14SMatthew G. Knepley     }
2904fa88e482SJed Brown     if (g_inplace) {
2905a256111fSJunchao Zhang       ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
2906fa88e482SJed Brown     } else {
29077128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2908fa88e482SJed Brown     }
2909ca3d3a14SMatthew G. Knepley     if (transform) {
2910ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2911ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2912fa88e482SJed Brown     } else if (l_inplace) {
2913a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
2914ca3d3a14SMatthew G. Knepley     } else {
2915ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2916ca3d3a14SMatthew G. Knepley     }
29177128ae9fSMatthew G Knepley   } else {
2918b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2919843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
29207128ae9fSMatthew G Knepley   }
29219a42bb27SBarry Smith   PetscFunctionReturn(0);
29229a42bb27SBarry Smith }
29239a42bb27SBarry Smith 
29249a42bb27SBarry Smith /*@
29259a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
292647c6ae99SBarry Smith 
2927d083f849SBarry Smith     Neighbor-wise Collective on dm
292847c6ae99SBarry Smith 
292947c6ae99SBarry Smith     Input Parameters:
293047c6ae99SBarry Smith +   dm - the DM object
2931f6813fd5SJed Brown .   l - the local vector
293247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2933f6813fd5SJed Brown -   g - the global vector
293447c6ae99SBarry Smith 
293501729b5cSPatrick Sanan     Level: intermediate
293647c6ae99SBarry Smith 
2937e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
293847c6ae99SBarry Smith 
293947c6ae99SBarry Smith @*/
29407087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
294147c6ae99SBarry Smith {
29427128ae9fSMatthew G Knepley   PetscSF                 sf;
294384330215SMatthew G. Knepley   PetscSection            s;
2944d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2945ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
294684330215SMatthew G. Knepley   PetscErrorCode          ierr;
294747c6ae99SBarry Smith 
294847c6ae99SBarry Smith   PetscFunctionBegin;
2949171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29501bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
295192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
29527128ae9fSMatthew G Knepley   switch (mode) {
29537128ae9fSMatthew G Knepley   case INSERT_VALUES:
29547128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
295584330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
29567128ae9fSMatthew G Knepley   case ADD_VALUES:
29577128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
295884330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
29597128ae9fSMatthew G Knepley   default:
296082f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
29617128ae9fSMatthew G Knepley   }
296284330215SMatthew G. Knepley   if (sf && !isInsert) {
2963ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2964ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2965ca3d3a14SMatthew G. Knepley     Vec                tmpl;
296684330215SMatthew G. Knepley 
2967ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2968ca3d3a14SMatthew G. Knepley     if (transform) {
2969ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2970ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2971ca3d3a14SMatthew G. Knepley     } else {
2972a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, NULL);CHKERRQ(ierr);
2973ca3d3a14SMatthew G. Knepley     }
2974a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(g, &gArray, NULL);CHKERRQ(ierr);
2975a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2976ca3d3a14SMatthew G. Knepley     if (transform) {
2977ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2978ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2979ca3d3a14SMatthew G. Knepley     } else {
2980a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
2981ca3d3a14SMatthew G. Knepley     }
2982a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
298384330215SMatthew G. Knepley   } else if (s && isInsert) {
29847128ae9fSMatthew G Knepley   } else {
2985b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2986843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
29877128ae9fSMatthew G Knepley   }
2988d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2989d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2990d4d07f1eSToby Isaac   }
299147c6ae99SBarry Smith   PetscFunctionReturn(0);
299247c6ae99SBarry Smith }
299347c6ae99SBarry Smith 
2994f089877aSRichard Tran Mills /*@
2995bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2996bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2997d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2998f089877aSRichard Tran Mills 
2999d083f849SBarry Smith    Neighbor-wise Collective on dm
3000f089877aSRichard Tran Mills 
3001f089877aSRichard Tran Mills    Input Parameters:
3002f089877aSRichard Tran Mills +  dm - the DM object
3003bc0a1609SRichard Tran Mills .  g - the original local vector
3004bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3005f089877aSRichard Tran Mills 
3006bc0a1609SRichard Tran Mills    Output Parameter:
3007bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3008f089877aSRichard Tran Mills 
3009f089877aSRichard Tran Mills    Level: intermediate
3010f089877aSRichard Tran Mills 
3011bc0a1609SRichard Tran Mills    Notes:
3012bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3013bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3014bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3015bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3016bc0a1609SRichard Tran Mills 
3017bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3018f089877aSRichard Tran Mills 
3019f089877aSRichard Tran Mills @*/
3020f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
3021f089877aSRichard Tran Mills {
3022f089877aSRichard Tran Mills   PetscErrorCode          ierr;
3023f089877aSRichard Tran Mills 
3024f089877aSRichard Tran Mills   PetscFunctionBegin;
3025f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3026bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3027f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
3028f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3029f089877aSRichard Tran Mills }
3030f089877aSRichard Tran Mills 
3031f089877aSRichard Tran Mills /*@
3032bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
3033bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3034d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
3035f089877aSRichard Tran Mills 
3036d083f849SBarry Smith    Neighbor-wise Collective on dm
3037f089877aSRichard Tran Mills 
3038f089877aSRichard Tran Mills    Input Parameters:
3039bc0a1609SRichard Tran Mills +  da - the DM object
3040bc0a1609SRichard Tran Mills .  g - the original local vector
3041bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3042f089877aSRichard Tran Mills 
3043bc0a1609SRichard Tran Mills    Output Parameter:
3044bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3045f089877aSRichard Tran Mills 
3046f089877aSRichard Tran Mills    Level: intermediate
3047f089877aSRichard Tran Mills 
3048bc0a1609SRichard Tran Mills    Notes:
3049bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3050bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3051bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3052bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3053bc0a1609SRichard Tran Mills 
3054bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3055f089877aSRichard Tran Mills 
3056f089877aSRichard Tran Mills @*/
3057f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
3058f089877aSRichard Tran Mills {
3059f089877aSRichard Tran Mills   PetscErrorCode          ierr;
3060f089877aSRichard Tran Mills 
3061f089877aSRichard Tran Mills   PetscFunctionBegin;
3062f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3063bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3064f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
3065f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3066f089877aSRichard Tran Mills }
3067f089877aSRichard Tran Mills 
306847c6ae99SBarry Smith /*@
306947c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
307047c6ae99SBarry Smith 
3071d083f849SBarry Smith     Collective on dm
307247c6ae99SBarry Smith 
3073d8d19677SJose E. Roman     Input Parameters:
307447c6ae99SBarry Smith +   dm - the DM object
307591d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
307647c6ae99SBarry Smith 
307747c6ae99SBarry Smith     Output Parameter:
307847c6ae99SBarry Smith .   dmc - the coarsened DM
307947c6ae99SBarry Smith 
308047c6ae99SBarry Smith     Level: developer
308147c6ae99SBarry Smith 
3082e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
308347c6ae99SBarry Smith 
308447c6ae99SBarry Smith @*/
30857087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
308647c6ae99SBarry Smith {
308747c6ae99SBarry Smith   PetscErrorCode    ierr;
3088b17ce1afSJed Brown   DMCoarsenHookLink link;
308947c6ae99SBarry Smith 
309047c6ae99SBarry Smith   PetscFunctionBegin;
3091171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3092b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
309347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
309447c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
3095b9d85ea2SLisandro Dalcin   if (*dmc) {
3096a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
309743842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
30988cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
3099644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
31000598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3101656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
3102e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
3103b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
3104b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
3105b17ce1afSJed Brown     }
3106b9d85ea2SLisandro Dalcin   }
310747a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
3108b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3109b17ce1afSJed Brown   PetscFunctionReturn(0);
3110b17ce1afSJed Brown }
3111b17ce1afSJed Brown 
3112bb9467b5SJed Brown /*@C
3113b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3114b17ce1afSJed Brown 
3115b17ce1afSJed Brown    Logically Collective
3116b17ce1afSJed Brown 
31174165533cSJose E. Roman    Input Parameters:
3118b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3119b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3120b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
31210298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3122b17ce1afSJed Brown 
3123b17ce1afSJed Brown    Calling sequence of coarsenhook:
3124b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3125b17ce1afSJed Brown 
3126b17ce1afSJed Brown +  fine - fine level DM
3127b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3128b17ce1afSJed Brown -  ctx - optional user-defined function context
3129b17ce1afSJed Brown 
3130b17ce1afSJed Brown    Calling sequence for restricthook:
3131c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3132b17ce1afSJed Brown 
3133b17ce1afSJed Brown +  fine - fine level DM
3134b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3135c833c3b5SJed Brown .  rscale - scaling vector for restriction
3136c833c3b5SJed Brown .  inject - matrix restricting by injection
3137b17ce1afSJed Brown .  coarse - coarse level DM to update
3138b17ce1afSJed Brown -  ctx - optional user-defined function context
3139b17ce1afSJed Brown 
3140b17ce1afSJed Brown    Level: advanced
3141b17ce1afSJed Brown 
3142b17ce1afSJed Brown    Notes:
3143b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3144b17ce1afSJed Brown 
3145b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3146b17ce1afSJed Brown 
3147b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3148b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3149b17ce1afSJed Brown 
3150bb9467b5SJed Brown    This function is currently not available from Fortran.
3151bb9467b5SJed Brown 
3152dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3153b17ce1afSJed Brown @*/
3154b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3155b17ce1afSJed Brown {
3156b17ce1afSJed Brown   PetscErrorCode    ierr;
3157b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3158b17ce1afSJed Brown 
3159b17ce1afSJed Brown   PetscFunctionBegin;
3160b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
31611e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
31621e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
31631e3d8eccSJed Brown   }
316495dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
3165b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3166b17ce1afSJed Brown   link->restricthook = restricthook;
3167b17ce1afSJed Brown   link->ctx          = ctx;
31680298fd71SBarry Smith   link->next         = NULL;
3169b17ce1afSJed Brown   *p                 = link;
3170b17ce1afSJed Brown   PetscFunctionReturn(0);
3171b17ce1afSJed Brown }
3172b17ce1afSJed Brown 
3173dc822a44SJed Brown /*@C
3174dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3175dc822a44SJed Brown 
3176dc822a44SJed Brown    Logically Collective
3177dc822a44SJed Brown 
31784165533cSJose E. Roman    Input Parameters:
3179dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3180dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3181dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3182dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3183dc822a44SJed Brown 
3184dc822a44SJed Brown    Level: advanced
3185dc822a44SJed Brown 
3186dc822a44SJed Brown    Notes:
3187dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3188dc822a44SJed Brown 
3189dc822a44SJed Brown    This function is currently not available from Fortran.
3190dc822a44SJed Brown 
3191dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3192dc822a44SJed Brown @*/
3193dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3194dc822a44SJed Brown {
3195dc822a44SJed Brown   PetscErrorCode    ierr;
3196dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3197dc822a44SJed Brown 
3198dc822a44SJed Brown   PetscFunctionBegin;
3199dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3200dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3201dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3202dc822a44SJed Brown       link = *p;
3203dc822a44SJed Brown       *p = link->next;
3204dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3205dc822a44SJed Brown       break;
3206dc822a44SJed Brown     }
3207dc822a44SJed Brown   }
3208dc822a44SJed Brown   PetscFunctionReturn(0);
3209dc822a44SJed Brown }
3210dc822a44SJed Brown 
3211b17ce1afSJed Brown /*@
3212b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3213b17ce1afSJed Brown 
3214b17ce1afSJed Brown    Collective if any hooks are
3215b17ce1afSJed Brown 
32164165533cSJose E. Roman    Input Parameters:
3217b17ce1afSJed Brown +  fine - finer DM to use as a base
3218b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3219e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3220b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3221e91eccc2SStefano Zampini -  coarse - coarser DM to update
3222b17ce1afSJed Brown 
3223b17ce1afSJed Brown    Level: developer
3224b17ce1afSJed Brown 
3225b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3226b17ce1afSJed Brown @*/
3227b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3228b17ce1afSJed Brown {
3229b17ce1afSJed Brown   PetscErrorCode    ierr;
3230b17ce1afSJed Brown   DMCoarsenHookLink link;
3231b17ce1afSJed Brown 
3232b17ce1afSJed Brown   PetscFunctionBegin;
3233b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
32348865f1eaSKarl Rupp     if (link->restricthook) {
32358865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
32368865f1eaSKarl Rupp     }
3237b17ce1afSJed Brown   }
323847c6ae99SBarry Smith   PetscFunctionReturn(0);
323947c6ae99SBarry Smith }
324047c6ae99SBarry Smith 
3241bb9467b5SJed Brown /*@C
3242be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
32435dbd56e3SPeter Brune 
3244d083f849SBarry Smith    Logically Collective on global
32455dbd56e3SPeter Brune 
32464165533cSJose E. Roman    Input Parameters:
32475dbd56e3SPeter Brune +  global - global DM
3248ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
32495dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
32500298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
32515dbd56e3SPeter Brune 
3252ec4806b8SPeter Brune    Calling sequence for ddhook:
3253ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3254ec4806b8SPeter Brune 
3255ec4806b8SPeter Brune +  global - global DM
3256ec4806b8SPeter Brune .  block  - block DM
3257ec4806b8SPeter Brune -  ctx - optional user-defined function context
3258ec4806b8SPeter Brune 
32595dbd56e3SPeter Brune    Calling sequence for restricthook:
3260ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
32615dbd56e3SPeter Brune 
32625dbd56e3SPeter Brune +  global - global DM
32635dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
32645dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3265ec4806b8SPeter Brune .  block  - block DM
32665dbd56e3SPeter Brune -  ctx - optional user-defined function context
32675dbd56e3SPeter Brune 
32685dbd56e3SPeter Brune    Level: advanced
32695dbd56e3SPeter Brune 
32705dbd56e3SPeter Brune    Notes:
3271ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
32725dbd56e3SPeter Brune 
32735dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
32745dbd56e3SPeter Brune 
32755dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3276ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
32775dbd56e3SPeter Brune 
3278bb9467b5SJed Brown    This function is currently not available from Fortran.
3279bb9467b5SJed Brown 
32805dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
32815dbd56e3SPeter Brune @*/
3282be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
32835dbd56e3SPeter Brune {
32845dbd56e3SPeter Brune   PetscErrorCode      ierr;
3285be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
32865dbd56e3SPeter Brune 
32875dbd56e3SPeter Brune   PetscFunctionBegin;
32885dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3289b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3290b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3291b3a6b972SJed Brown   }
329295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
32935dbd56e3SPeter Brune   link->restricthook = restricthook;
3294be081cd6SPeter Brune   link->ddhook       = ddhook;
32955dbd56e3SPeter Brune   link->ctx          = ctx;
32960298fd71SBarry Smith   link->next         = NULL;
32975dbd56e3SPeter Brune   *p                 = link;
32985dbd56e3SPeter Brune   PetscFunctionReturn(0);
32995dbd56e3SPeter Brune }
33005dbd56e3SPeter Brune 
3301b3a6b972SJed Brown /*@C
3302b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3303b3a6b972SJed Brown 
3304b3a6b972SJed Brown    Logically Collective
3305b3a6b972SJed Brown 
33064165533cSJose E. Roman    Input Parameters:
3307b3a6b972SJed Brown +  global - global DM
3308b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3309b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3310b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3311b3a6b972SJed Brown 
3312b3a6b972SJed Brown    Level: advanced
3313b3a6b972SJed Brown 
3314b3a6b972SJed Brown    Notes:
3315b3a6b972SJed Brown 
3316b3a6b972SJed Brown    This function is currently not available from Fortran.
3317b3a6b972SJed Brown 
3318b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3319b3a6b972SJed Brown @*/
3320b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3321b3a6b972SJed Brown {
3322b3a6b972SJed Brown   PetscErrorCode      ierr;
3323b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3324b3a6b972SJed Brown 
3325b3a6b972SJed Brown   PetscFunctionBegin;
3326b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3327b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3328b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3329b3a6b972SJed Brown       link = *p;
3330b3a6b972SJed Brown       *p = link->next;
3331b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3332b3a6b972SJed Brown       break;
3333b3a6b972SJed Brown     }
3334b3a6b972SJed Brown   }
3335b3a6b972SJed Brown   PetscFunctionReturn(0);
3336b3a6b972SJed Brown }
3337b3a6b972SJed Brown 
33385dbd56e3SPeter Brune /*@
3339be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
33405dbd56e3SPeter Brune 
33415dbd56e3SPeter Brune    Collective if any hooks are
33425dbd56e3SPeter Brune 
33434165533cSJose E. Roman    Input Parameters:
33445dbd56e3SPeter Brune +  fine - finer DM to use as a base
3345be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3346be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
33475dbd56e3SPeter Brune -  coarse - coarer DM to update
33485dbd56e3SPeter Brune 
33495dbd56e3SPeter Brune    Level: developer
33505dbd56e3SPeter Brune 
33515dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
33525dbd56e3SPeter Brune @*/
3353be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
33545dbd56e3SPeter Brune {
33555dbd56e3SPeter Brune   PetscErrorCode      ierr;
3356be081cd6SPeter Brune   DMSubDomainHookLink link;
33575dbd56e3SPeter Brune 
33585dbd56e3SPeter Brune   PetscFunctionBegin;
3359be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
33608865f1eaSKarl Rupp     if (link->restricthook) {
33618865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
33628865f1eaSKarl Rupp     }
33635dbd56e3SPeter Brune   }
33645dbd56e3SPeter Brune   PetscFunctionReturn(0);
33655dbd56e3SPeter Brune }
33665dbd56e3SPeter Brune 
33675fe1f584SPeter Brune /*@
33686a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
33695fe1f584SPeter Brune 
33705fe1f584SPeter Brune     Not Collective
33715fe1f584SPeter Brune 
33725fe1f584SPeter Brune     Input Parameter:
33735fe1f584SPeter Brune .   dm - the DM object
33745fe1f584SPeter Brune 
33755fe1f584SPeter Brune     Output Parameter:
33766a7d9d85SPeter Brune .   level - number of coarsenings
33775fe1f584SPeter Brune 
33785fe1f584SPeter Brune     Level: developer
33795fe1f584SPeter Brune 
33806a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
33815fe1f584SPeter Brune 
33825fe1f584SPeter Brune @*/
33835fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
33845fe1f584SPeter Brune {
33855fe1f584SPeter Brune   PetscFunctionBegin;
33865fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3387b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
33885fe1f584SPeter Brune   *level = dm->leveldown;
33895fe1f584SPeter Brune   PetscFunctionReturn(0);
33905fe1f584SPeter Brune }
33915fe1f584SPeter Brune 
33929a64c4a8SMatthew G. Knepley /*@
33939a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
33949a64c4a8SMatthew G. Knepley 
33959a64c4a8SMatthew G. Knepley     Not Collective
33969a64c4a8SMatthew G. Knepley 
33979a64c4a8SMatthew G. Knepley     Input Parameters:
33989a64c4a8SMatthew G. Knepley +   dm - the DM object
33999a64c4a8SMatthew G. Knepley -   level - number of coarsenings
34009a64c4a8SMatthew G. Knepley 
34019a64c4a8SMatthew G. Knepley     Level: developer
34029a64c4a8SMatthew G. Knepley 
34039a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
34049a64c4a8SMatthew G. Knepley @*/
34059a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
34069a64c4a8SMatthew G. Knepley {
34079a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
34089a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34099a64c4a8SMatthew G. Knepley   dm->leveldown = level;
34109a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
34119a64c4a8SMatthew G. Knepley }
34129a64c4a8SMatthew G. Knepley 
341347c6ae99SBarry Smith /*@C
341447c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
341547c6ae99SBarry Smith 
3416d083f849SBarry Smith     Collective on dm
341747c6ae99SBarry Smith 
3418d8d19677SJose E. Roman     Input Parameters:
341947c6ae99SBarry Smith +   dm - the DM object
342047c6ae99SBarry Smith -   nlevels - the number of levels of refinement
342147c6ae99SBarry Smith 
342247c6ae99SBarry Smith     Output Parameter:
342347c6ae99SBarry Smith .   dmf - the refined DM hierarchy
342447c6ae99SBarry Smith 
342547c6ae99SBarry Smith     Level: developer
342647c6ae99SBarry Smith 
3427e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
342847c6ae99SBarry Smith 
342947c6ae99SBarry Smith @*/
34307087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
343147c6ae99SBarry Smith {
343247c6ae99SBarry Smith   PetscErrorCode ierr;
343347c6ae99SBarry Smith 
343447c6ae99SBarry Smith   PetscFunctionBegin;
3435171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3436ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
343747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3438b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
343947c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
344047c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
344147c6ae99SBarry Smith   } else if (dm->ops->refine) {
344247c6ae99SBarry Smith     PetscInt i;
344347c6ae99SBarry Smith 
3444ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
344547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3446ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
344747c6ae99SBarry Smith     }
3448ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
344947c6ae99SBarry Smith   PetscFunctionReturn(0);
345047c6ae99SBarry Smith }
345147c6ae99SBarry Smith 
345247c6ae99SBarry Smith /*@C
345347c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
345447c6ae99SBarry Smith 
3455d083f849SBarry Smith     Collective on dm
345647c6ae99SBarry Smith 
3457d8d19677SJose E. Roman     Input Parameters:
345847c6ae99SBarry Smith +   dm - the DM object
345947c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
346047c6ae99SBarry Smith 
346147c6ae99SBarry Smith     Output Parameter:
346247c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
346347c6ae99SBarry Smith 
346447c6ae99SBarry Smith     Level: developer
346547c6ae99SBarry Smith 
3466e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
346747c6ae99SBarry Smith 
346847c6ae99SBarry Smith @*/
34697087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
347047c6ae99SBarry Smith {
347147c6ae99SBarry Smith   PetscErrorCode ierr;
347247c6ae99SBarry Smith 
347347c6ae99SBarry Smith   PetscFunctionBegin;
3474171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3475ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
347647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
347747c6ae99SBarry Smith   PetscValidPointer(dmc,3);
347847c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
347947c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
348047c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
348147c6ae99SBarry Smith     PetscInt i;
348247c6ae99SBarry Smith 
3483ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
348447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3485ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
348647c6ae99SBarry Smith     }
3487ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
348847c6ae99SBarry Smith   PetscFunctionReturn(0);
348947c6ae99SBarry Smith }
349047c6ae99SBarry Smith 
34911a266240SBarry Smith /*@C
34921a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
34931a266240SBarry Smith 
34941a266240SBarry Smith     Not Collective
34951a266240SBarry Smith 
34961a266240SBarry Smith     Input Parameters:
34971a266240SBarry Smith +   dm - the DM object
34981a266240SBarry Smith -   destroy - the destroy function
34991a266240SBarry Smith 
35001a266240SBarry Smith     Level: intermediate
35011a266240SBarry Smith 
3502e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
35031a266240SBarry Smith 
3504f07f9ceaSJed Brown @*/
35051a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
35061a266240SBarry Smith {
35071a266240SBarry Smith   PetscFunctionBegin;
3508171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35091a266240SBarry Smith   dm->ctxdestroy = destroy;
35101a266240SBarry Smith   PetscFunctionReturn(0);
35111a266240SBarry Smith }
35121a266240SBarry Smith 
3513b07ff414SBarry Smith /*@
35141b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
351547c6ae99SBarry Smith 
351647c6ae99SBarry Smith     Not Collective
351747c6ae99SBarry Smith 
351847c6ae99SBarry Smith     Input Parameters:
351947c6ae99SBarry Smith +   dm - the DM object
352047c6ae99SBarry Smith -   ctx - the user context
352147c6ae99SBarry Smith 
352247c6ae99SBarry Smith     Level: intermediate
352347c6ae99SBarry Smith 
3524e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
352547c6ae99SBarry Smith 
352647c6ae99SBarry Smith @*/
35271b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
352847c6ae99SBarry Smith {
352947c6ae99SBarry Smith   PetscFunctionBegin;
3530171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
353147c6ae99SBarry Smith   dm->ctx = ctx;
353247c6ae99SBarry Smith   PetscFunctionReturn(0);
353347c6ae99SBarry Smith }
353447c6ae99SBarry Smith 
353547c6ae99SBarry Smith /*@
35361b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
353747c6ae99SBarry Smith 
353847c6ae99SBarry Smith     Not Collective
353947c6ae99SBarry Smith 
354047c6ae99SBarry Smith     Input Parameter:
354147c6ae99SBarry Smith .   dm - the DM object
354247c6ae99SBarry Smith 
354347c6ae99SBarry Smith     Output Parameter:
354447c6ae99SBarry Smith .   ctx - the user context
354547c6ae99SBarry Smith 
354647c6ae99SBarry Smith     Level: intermediate
354747c6ae99SBarry Smith 
3548e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
354947c6ae99SBarry Smith 
355047c6ae99SBarry Smith @*/
35511b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
355247c6ae99SBarry Smith {
355347c6ae99SBarry Smith   PetscFunctionBegin;
3554171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35551b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
355647c6ae99SBarry Smith   PetscFunctionReturn(0);
355747c6ae99SBarry Smith }
355847c6ae99SBarry Smith 
355908da532bSDmitry Karpeev /*@C
3560df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
356108da532bSDmitry Karpeev 
3562d083f849SBarry Smith     Logically Collective on dm
356308da532bSDmitry Karpeev 
3564d8d19677SJose E. Roman     Input Parameters:
356508da532bSDmitry Karpeev +   dm - the DM object
35660298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
356708da532bSDmitry Karpeev 
356808da532bSDmitry Karpeev     Level: intermediate
356908da532bSDmitry Karpeev 
3570835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
357108da532bSDmitry Karpeev          DMSetJacobian()
357208da532bSDmitry Karpeev 
357308da532bSDmitry Karpeev @*/
357408da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
357508da532bSDmitry Karpeev {
357608da532bSDmitry Karpeev   PetscFunctionBegin;
35775a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
357808da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
357908da532bSDmitry Karpeev   PetscFunctionReturn(0);
358008da532bSDmitry Karpeev }
358108da532bSDmitry Karpeev 
358208da532bSDmitry Karpeev /*@
358308da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
358408da532bSDmitry Karpeev 
358508da532bSDmitry Karpeev     Not Collective
358608da532bSDmitry Karpeev 
358708da532bSDmitry Karpeev     Input Parameter:
358808da532bSDmitry Karpeev .   dm - the DM object to destroy
358908da532bSDmitry Karpeev 
359008da532bSDmitry Karpeev     Output Parameter:
359108da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
359208da532bSDmitry Karpeev 
359308da532bSDmitry Karpeev     Level: developer
359408da532bSDmitry Karpeev 
359574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
359608da532bSDmitry Karpeev 
359708da532bSDmitry Karpeev @*/
359808da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
359908da532bSDmitry Karpeev {
360008da532bSDmitry Karpeev   PetscFunctionBegin;
36015a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3602534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
360308da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
360408da532bSDmitry Karpeev   PetscFunctionReturn(0);
360508da532bSDmitry Karpeev }
360608da532bSDmitry Karpeev 
360708da532bSDmitry Karpeev /*@C
360808da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
360908da532bSDmitry Karpeev 
3610d083f849SBarry Smith     Logically Collective on dm
361108da532bSDmitry Karpeev 
3612f899ff85SJose E. Roman     Input Parameter:
3613907376e6SBarry Smith .   dm - the DM object
361408da532bSDmitry Karpeev 
361508da532bSDmitry Karpeev     Output parameters:
361608da532bSDmitry Karpeev +   xl - lower bound
361708da532bSDmitry Karpeev -   xu - upper bound
361808da532bSDmitry Karpeev 
3619907376e6SBarry Smith     Level: advanced
3620907376e6SBarry Smith 
362195452b02SPatrick Sanan     Notes:
362295452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
362308da532bSDmitry Karpeev 
362474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
362508da532bSDmitry Karpeev 
362608da532bSDmitry Karpeev @*/
362708da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
362808da532bSDmitry Karpeev {
362908da532bSDmitry Karpeev   PetscErrorCode ierr;
36305fd66863SKarl Rupp 
363108da532bSDmitry Karpeev   PetscFunctionBegin;
36325a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
363308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
36345a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3635b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
363608da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
363708da532bSDmitry Karpeev   PetscFunctionReturn(0);
363808da532bSDmitry Karpeev }
363908da532bSDmitry Karpeev 
3640b0ae01b7SPeter Brune /*@
3641b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3642b0ae01b7SPeter Brune 
3643b0ae01b7SPeter Brune     Not Collective
3644b0ae01b7SPeter Brune 
3645b0ae01b7SPeter Brune     Input Parameter:
3646b0ae01b7SPeter Brune .   dm - the DM object
3647b0ae01b7SPeter Brune 
3648b0ae01b7SPeter Brune     Output Parameter:
3649b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3650b0ae01b7SPeter Brune 
3651b0ae01b7SPeter Brune     Level: developer
3652b0ae01b7SPeter Brune 
36531565f0a7SPatrick Sanan .seealso DMCreateColoring()
3654b0ae01b7SPeter Brune 
3655b0ae01b7SPeter Brune @*/
3656b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3657b0ae01b7SPeter Brune {
3658b0ae01b7SPeter Brune   PetscFunctionBegin;
36595a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3660534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3661b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3662b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3663b0ae01b7SPeter Brune }
3664b0ae01b7SPeter Brune 
36653ad4599aSBarry Smith /*@
36663ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
36673ad4599aSBarry Smith 
36683ad4599aSBarry Smith     Not Collective
36693ad4599aSBarry Smith 
36703ad4599aSBarry Smith     Input Parameter:
36713ad4599aSBarry Smith .   dm - the DM object
36723ad4599aSBarry Smith 
36733ad4599aSBarry Smith     Output Parameter:
36743ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
36753ad4599aSBarry Smith 
36763ad4599aSBarry Smith     Level: developer
36773ad4599aSBarry Smith 
36781565f0a7SPatrick Sanan .seealso DMCreateRestriction()
36793ad4599aSBarry Smith 
36803ad4599aSBarry Smith @*/
36813ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
36823ad4599aSBarry Smith {
36833ad4599aSBarry Smith   PetscFunctionBegin;
36845a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3685534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
36863ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
36873ad4599aSBarry Smith   PetscFunctionReturn(0);
36883ad4599aSBarry Smith }
36893ad4599aSBarry Smith 
3690a7058e45SLawrence Mitchell /*@
3691a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3692a7058e45SLawrence Mitchell 
3693a7058e45SLawrence Mitchell     Not Collective
3694a7058e45SLawrence Mitchell 
3695a7058e45SLawrence Mitchell     Input Parameter:
3696a7058e45SLawrence Mitchell .   dm - the DM object
3697a7058e45SLawrence Mitchell 
3698a7058e45SLawrence Mitchell     Output Parameter:
3699a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3700a7058e45SLawrence Mitchell 
3701a7058e45SLawrence Mitchell     Level: developer
3702a7058e45SLawrence Mitchell 
37031565f0a7SPatrick Sanan .seealso DMCreateInjection()
3704a7058e45SLawrence Mitchell 
3705a7058e45SLawrence Mitchell @*/
3706a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3707a7058e45SLawrence Mitchell {
37084a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
37095a84ad33SLisandro Dalcin 
3710a7058e45SLawrence Mitchell   PetscFunctionBegin;
37115a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3712534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37135a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
37144a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
37155a84ad33SLisandro Dalcin   } else {
37165a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
37175a84ad33SLisandro Dalcin   }
3718a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3719a7058e45SLawrence Mitchell }
3720a7058e45SLawrence Mitchell 
37210298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3722264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3723264ace61SBarry Smith 
3724264ace61SBarry Smith /*@C
3725264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3726264ace61SBarry Smith 
3727d083f849SBarry Smith   Collective on dm
3728264ace61SBarry Smith 
3729264ace61SBarry Smith   Input Parameters:
3730264ace61SBarry Smith + dm     - The DM object
3731264ace61SBarry Smith - method - The name of the DM type
3732264ace61SBarry Smith 
3733264ace61SBarry Smith   Options Database Key:
3734264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3735264ace61SBarry Smith 
3736264ace61SBarry Smith   Notes:
3737e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3738264ace61SBarry Smith 
3739264ace61SBarry Smith   Level: intermediate
3740264ace61SBarry Smith 
3741264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3742264ace61SBarry Smith @*/
374319fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3744264ace61SBarry Smith {
3745264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3746264ace61SBarry Smith   PetscBool      match;
3747264ace61SBarry Smith   PetscErrorCode ierr;
3748264ace61SBarry Smith 
3749264ace61SBarry Smith   PetscFunctionBegin;
3750264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3751251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3752264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3753264ace61SBarry Smith 
37540f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
37551c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3756ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3757264ace61SBarry Smith 
3758264ace61SBarry Smith   if (dm->ops->destroy) {
3759264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3760264ace61SBarry Smith   }
3761d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3762264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3763d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3764264ace61SBarry Smith   PetscFunctionReturn(0);
3765264ace61SBarry Smith }
3766264ace61SBarry Smith 
3767264ace61SBarry Smith /*@C
3768264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3769264ace61SBarry Smith 
3770264ace61SBarry Smith   Not Collective
3771264ace61SBarry Smith 
3772264ace61SBarry Smith   Input Parameter:
3773264ace61SBarry Smith . dm  - The DM
3774264ace61SBarry Smith 
3775264ace61SBarry Smith   Output Parameter:
3776264ace61SBarry Smith . type - The DM type name
3777264ace61SBarry Smith 
3778264ace61SBarry Smith   Level: intermediate
3779264ace61SBarry Smith 
3780264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3781264ace61SBarry Smith @*/
378219fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3783264ace61SBarry Smith {
3784264ace61SBarry Smith   PetscErrorCode ierr;
3785264ace61SBarry Smith 
3786264ace61SBarry Smith   PetscFunctionBegin;
3787264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3788c959eef4SJed Brown   PetscValidPointer(type,2);
3789607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3790264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3791264ace61SBarry Smith   PetscFunctionReturn(0);
3792264ace61SBarry Smith }
3793264ace61SBarry Smith 
379467a56275SMatthew G Knepley /*@C
379567a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
379667a56275SMatthew G Knepley 
3797d083f849SBarry Smith   Collective on dm
379867a56275SMatthew G Knepley 
379967a56275SMatthew G Knepley   Input Parameters:
380067a56275SMatthew G Knepley + dm - the DM
380167a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
380267a56275SMatthew G Knepley 
380367a56275SMatthew G Knepley   Output Parameter:
380467a56275SMatthew G Knepley . M - pointer to new DM
380567a56275SMatthew G Knepley 
380667a56275SMatthew G Knepley   Notes:
380767a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
380867a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
380967a56275SMatthew G Knepley   of the input DM.
381067a56275SMatthew G Knepley 
381167a56275SMatthew G Knepley   Level: intermediate
381267a56275SMatthew G Knepley 
381367a56275SMatthew G Knepley .seealso: DMCreate()
381467a56275SMatthew G Knepley @*/
381519fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
381667a56275SMatthew G Knepley {
381767a56275SMatthew G Knepley   DM             B;
381867a56275SMatthew G Knepley   char           convname[256];
3819c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
382067a56275SMatthew G Knepley   PetscErrorCode ierr;
382167a56275SMatthew G Knepley 
382267a56275SMatthew G Knepley   PetscFunctionBegin;
382367a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
382467a56275SMatthew G Knepley   PetscValidType(dm,1);
382567a56275SMatthew G Knepley   PetscValidPointer(M,3);
3826251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3827c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3828c067b6caSMatthew G. Knepley   if (sametype) {
3829c067b6caSMatthew G. Knepley     *M   = dm;
3830c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3831c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3832c067b6caSMatthew G. Knepley   } else {
38330298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
383467a56275SMatthew G Knepley 
383567a56275SMatthew G Knepley     /*
383667a56275SMatthew G Knepley        Order of precedence:
383767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
383867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
383967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
384067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
384167a56275SMatthew G Knepley        5) Use a really basic converter.
384267a56275SMatthew G Knepley     */
384367a56275SMatthew G Knepley 
384467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3845a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3846a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3847a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3848a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3849a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
38500005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
385167a56275SMatthew G Knepley     if (conv) goto foundconv;
385267a56275SMatthew G Knepley 
385367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
385482f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
385567a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3856a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3857a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3858a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3859a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3860a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
38610005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
386267a56275SMatthew G Knepley     if (conv) {
3863fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
386467a56275SMatthew G Knepley       goto foundconv;
386567a56275SMatthew G Knepley     }
386667a56275SMatthew G Knepley 
386767a56275SMatthew G Knepley #if 0
386867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
386967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3870fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
387167a56275SMatthew G Knepley     if (conv) goto foundconv;
387267a56275SMatthew G Knepley 
387367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
387467a56275SMatthew G Knepley     if (dm->ops->convert) {
387567a56275SMatthew G Knepley       conv = dm->ops->convert;
387667a56275SMatthew G Knepley     }
387767a56275SMatthew G Knepley     if (conv) goto foundconv;
387867a56275SMatthew G Knepley #endif
387967a56275SMatthew G Knepley 
388067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
388182f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
388267a56275SMatthew G Knepley 
388367a56275SMatthew G Knepley foundconv:
388467a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
388567a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
388612fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
388790b157c4SStefano Zampini     {
388890b157c4SStefano Zampini       PetscBool             isper;
388912fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
389012fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
389190b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
389290b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
3893c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
3894a587d139SMark       ierr = PetscFree((*M)->vectype);CHKERRQ(ierr);
3895a587d139SMark       ierr = PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype);CHKERRQ(ierr);
3896a587d139SMark       ierr = PetscFree((*M)->mattype);CHKERRQ(ierr);
3897a587d139SMark       ierr = PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype);CHKERRQ(ierr);
389812fa691eSMatthew G. Knepley     }
389967a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
390067a56275SMatthew G Knepley   }
390167a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
390267a56275SMatthew G Knepley   PetscFunctionReturn(0);
390367a56275SMatthew G Knepley }
3904264ace61SBarry Smith 
3905264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3906264ace61SBarry Smith 
3907264ace61SBarry Smith /*@C
39081c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
39091c84c290SBarry Smith 
39101c84c290SBarry Smith   Not Collective
39111c84c290SBarry Smith 
39121c84c290SBarry Smith   Input Parameters:
39131c84c290SBarry Smith + name        - The name of a new user-defined creation routine
39141c84c290SBarry Smith - create_func - The creation routine itself
39151c84c290SBarry Smith 
39161c84c290SBarry Smith   Notes:
39171c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
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 .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
4021b2e4378dSMatthew G. Knepley @*/
4022b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
4023b2e4378dSMatthew G. Knepley {
4024b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
4025b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
4026b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
4027b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
4028b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
4029b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
4030b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
4031b2e4378dSMatthew G. Knepley 
4032b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4033b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4034b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4035b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
4036b2e4378dSMatthew G. Knepley   if (coords) {
4037b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
4038b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
4039b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
4040b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
4041b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
4042b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4043b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4044b2e4378dSMatthew G. Knepley       }
4045b2e4378dSMatthew G. Knepley     }
4046b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
4047b2e4378dSMatthew G. Knepley   } else {
4048b2e4378dSMatthew G. Knepley     PetscBool isda;
4049b2e4378dSMatthew G. Knepley 
4050b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
4051b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
4052b2e4378dSMatthew G. Knepley   }
4053b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
4054b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
4055b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4056b2e4378dSMatthew G. Knepley }
4057b2e4378dSMatthew G. Knepley 
4058b2e4378dSMatthew G. Knepley /*@
4059b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
4060b2e4378dSMatthew G. Knepley 
4061b2e4378dSMatthew G. Knepley   Collective
4062b2e4378dSMatthew G. Knepley 
4063b2e4378dSMatthew G. Knepley   Input Parameter:
4064b2e4378dSMatthew G. Knepley . dm - the DM
4065b2e4378dSMatthew G. Knepley 
4066b2e4378dSMatthew G. Knepley   Output Parameters:
4067b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
4068b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
4069b2e4378dSMatthew G. Knepley 
4070b2e4378dSMatthew G. Knepley   Level: beginner
4071b2e4378dSMatthew G. Knepley 
4072b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
4073b2e4378dSMatthew G. Knepley @*/
4074b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
4075b2e4378dSMatthew G. Knepley {
4076b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
40776ce308c4SMatthew G. Knepley   PetscInt       cdim;
40786ce308c4SMatthew G. Knepley   PetscMPIInt    count;
4079b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
4080b2e4378dSMatthew G. Knepley 
4081b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4082b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4083b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4084b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
4085b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
4086820f2d46SBarry Smith   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4087820f2d46SBarry Smith   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4088b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4089b2e4378dSMatthew G. Knepley }
4090b2e4378dSMatthew G. Knepley 
40917da65231SMatthew G Knepley /******************************** FEM Support **********************************/
40927da65231SMatthew G Knepley 
4093a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4094a6dfd86eSKarl Rupp {
40951d47ebbbSSatish Balay   PetscInt       f;
40961b30c384SMatthew G Knepley   PetscErrorCode ierr;
40971b30c384SMatthew G Knepley 
40987da65231SMatthew G Knepley   PetscFunctionBegin;
409974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
41001d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
410157622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
41027da65231SMatthew G Knepley   }
41037da65231SMatthew G Knepley   PetscFunctionReturn(0);
41047da65231SMatthew G Knepley }
41057da65231SMatthew G Knepley 
4106a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4107a6dfd86eSKarl Rupp {
41081b30c384SMatthew G Knepley   PetscInt       f, g;
41097da65231SMatthew G Knepley   PetscErrorCode ierr;
41107da65231SMatthew G Knepley 
41117da65231SMatthew G Knepley   PetscFunctionBegin;
411274778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
41131d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
411474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
41151d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
4116e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
41177da65231SMatthew G Knepley     }
411874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
41197da65231SMatthew G Knepley   }
41207da65231SMatthew G Knepley   PetscFunctionReturn(0);
41217da65231SMatthew G Knepley }
4122e7c4fc90SDmitry Karpeev 
41236113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4124e759306cSMatthew G. Knepley {
41250c5b8624SToby Isaac   PetscInt          localSize, bs;
41260c5b8624SToby Isaac   PetscMPIInt       size;
41270c5b8624SToby Isaac   Vec               x, xglob;
41280c5b8624SToby Isaac   const PetscScalar *xarray;
4129e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
4130e759306cSMatthew G. Knepley 
4131e759306cSMatthew G. Knepley   PetscFunctionBegin;
4132ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRMPI(ierr);
4133e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
4134e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
41356113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
41360c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
41370c5b8624SToby Isaac   if (size > 1) {
41380c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
41390c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
41400c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
41410c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
41420c5b8624SToby Isaac   } else {
41430c5b8624SToby Isaac     xglob = x;
41440c5b8624SToby Isaac   }
41450c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
41460c5b8624SToby Isaac   if (size > 1) {
41470c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
41480c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
41490c5b8624SToby Isaac   }
4150e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
4151e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4152e759306cSMatthew G. Knepley }
4153e759306cSMatthew G. Knepley 
415488ed4aceSMatthew G Knepley /*@
41551bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4156061576a5SJed Brown 
4157061576a5SJed Brown   Input Parameter:
4158061576a5SJed Brown . dm - The DM
4159061576a5SJed Brown 
4160061576a5SJed Brown   Output Parameter:
4161061576a5SJed Brown . section - The PetscSection
4162061576a5SJed Brown 
4163061576a5SJed Brown   Options Database Keys:
4164061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4165061576a5SJed Brown 
4166061576a5SJed Brown   Level: advanced
4167061576a5SJed Brown 
4168061576a5SJed Brown   Notes:
4169061576a5SJed Brown   Use DMGetLocalSection() in new code.
4170061576a5SJed Brown 
4171061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4172061576a5SJed Brown 
4173061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
4174061576a5SJed Brown @*/
4175061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4176061576a5SJed Brown {
4177061576a5SJed Brown   PetscErrorCode ierr;
4178061576a5SJed Brown 
4179061576a5SJed Brown   PetscFunctionBegin;
4180061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
4181061576a5SJed Brown   PetscFunctionReturn(0);
4182061576a5SJed Brown }
4183061576a5SJed Brown 
4184061576a5SJed Brown /*@
4185061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
418688ed4aceSMatthew G Knepley 
418788ed4aceSMatthew G Knepley   Input Parameter:
418888ed4aceSMatthew G Knepley . dm - The DM
418988ed4aceSMatthew G Knepley 
419088ed4aceSMatthew G Knepley   Output Parameter:
419188ed4aceSMatthew G Knepley . section - The PetscSection
419288ed4aceSMatthew G Knepley 
4193e5893cccSMatthew G. Knepley   Options Database Keys:
4194e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4195e5893cccSMatthew G. Knepley 
419688ed4aceSMatthew G Knepley   Level: intermediate
419788ed4aceSMatthew G Knepley 
419888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
419988ed4aceSMatthew G Knepley 
4200061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
420188ed4aceSMatthew G Knepley @*/
4202061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
42030adebc6cSBarry Smith {
4204fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4205fd59a867SMatthew G. Knepley 
420688ed4aceSMatthew G Knepley   PetscFunctionBegin;
420788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
420888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42091bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4210e5e52638SMatthew G. Knepley     PetscInt d;
4211e5e52638SMatthew G. Knepley 
421245480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
421345480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject) dm;
421445480ffeSMatthew G. Knepley       PetscViewer       viewer;
421545480ffeSMatthew G. Knepley       PetscViewerFormat format;
421645480ffeSMatthew G. Knepley       PetscBool         flg;
421745480ffeSMatthew G. Knepley 
421845480ffeSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg);CHKERRQ(ierr);
421945480ffeSMatthew G. Knepley       if (flg) {ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);}
422045480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
422145480ffeSMatthew G. Knepley         ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);
422245480ffeSMatthew G. Knepley         if (flg) {ierr = PetscDSView(dm->probs[d].ds, viewer);CHKERRQ(ierr);}
422345480ffeSMatthew G. Knepley       }
422445480ffeSMatthew G. Knepley       if (flg) {
422545480ffeSMatthew G. Knepley         ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
422645480ffeSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
422745480ffeSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
422845480ffeSMatthew G. Knepley       }
422945480ffeSMatthew G. Knepley     }
42301bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
42311bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
42322f0f8703SMatthew G. Knepley   }
42331bb6d2a8SBarry Smith   *section = dm->localSection;
423488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
423588ed4aceSMatthew G Knepley }
423688ed4aceSMatthew G Knepley 
423788ed4aceSMatthew G Knepley /*@
42381bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4239061576a5SJed Brown 
4240061576a5SJed Brown   Input Parameters:
4241061576a5SJed Brown + dm - The DM
4242061576a5SJed Brown - section - The PetscSection
4243061576a5SJed Brown 
4244061576a5SJed Brown   Level: advanced
4245061576a5SJed Brown 
4246061576a5SJed Brown   Notes:
4247061576a5SJed Brown   Use DMSetLocalSection() in new code.
4248061576a5SJed Brown 
4249061576a5SJed Brown   Any existing Section will be destroyed
4250061576a5SJed Brown 
4251061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4252061576a5SJed Brown @*/
4253061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4254061576a5SJed Brown {
4255061576a5SJed Brown   PetscErrorCode ierr;
4256061576a5SJed Brown 
4257061576a5SJed Brown   PetscFunctionBegin;
4258061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4259061576a5SJed Brown   PetscFunctionReturn(0);
4260061576a5SJed Brown }
4261061576a5SJed Brown 
4262061576a5SJed Brown /*@
4263061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
426488ed4aceSMatthew G Knepley 
426588ed4aceSMatthew G Knepley   Input Parameters:
426688ed4aceSMatthew G Knepley + dm - The DM
426788ed4aceSMatthew G Knepley - section - The PetscSection
426888ed4aceSMatthew G Knepley 
426988ed4aceSMatthew G Knepley   Level: intermediate
427088ed4aceSMatthew G Knepley 
427188ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
427288ed4aceSMatthew G Knepley 
4273061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
427488ed4aceSMatthew G Knepley @*/
4275061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
42760adebc6cSBarry Smith {
4277c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4278af122d2aSMatthew G Knepley   PetscInt       f;
427988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
428088ed4aceSMatthew G Knepley 
428188ed4aceSMatthew G Knepley   PetscFunctionBegin;
428288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4283b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42841d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42851bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
42861bb6d2a8SBarry Smith   dm->localSection = section;
42871bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4288af122d2aSMatthew G Knepley   if (numFields) {
4289af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4290af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
42910f21e855SMatthew G. Knepley       PetscObject disc;
4292af122d2aSMatthew G Knepley       const char *name;
4293af122d2aSMatthew G Knepley 
42941bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
429544a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
42960f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4297af122d2aSMatthew G Knepley     }
4298af122d2aSMatthew G Knepley   }
4299e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43001bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
430188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
430288ed4aceSMatthew G Knepley }
430388ed4aceSMatthew G Knepley 
43049435951eSToby Isaac /*@
4305b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
43069435951eSToby Isaac 
4307e228b242SToby Isaac   not collective
4308e228b242SToby Isaac 
43099435951eSToby Isaac   Input Parameter:
43109435951eSToby Isaac . dm - The DM
43119435951eSToby Isaac 
4312d8d19677SJose E. Roman   Output Parameters:
43139435951eSToby 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.
43149435951eSToby 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.
43159435951eSToby Isaac 
43169435951eSToby Isaac   Level: advanced
43179435951eSToby Isaac 
43189435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
43199435951eSToby Isaac 
43209435951eSToby Isaac .seealso: DMSetDefaultConstraints()
43219435951eSToby Isaac @*/
43229435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
43239435951eSToby Isaac {
43249435951eSToby Isaac   PetscErrorCode ierr;
43259435951eSToby Isaac 
43269435951eSToby Isaac   PetscFunctionBegin;
43279435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43289435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
432945a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
433045a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
43319435951eSToby Isaac   PetscFunctionReturn(0);
43329435951eSToby Isaac }
43339435951eSToby Isaac 
43349435951eSToby Isaac /*@
4335b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
43369435951eSToby Isaac 
43379435951eSToby 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().
43389435951eSToby Isaac 
43399435951eSToby 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.
43409435951eSToby Isaac 
4341e228b242SToby Isaac   collective on dm
4342e228b242SToby Isaac 
43439435951eSToby Isaac   Input Parameters:
43449435951eSToby Isaac + dm - The DM
4345e228b242SToby 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).
4346e228b242SToby 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).
43479435951eSToby Isaac 
43489435951eSToby Isaac   Level: advanced
43499435951eSToby Isaac 
43509435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
43519435951eSToby Isaac 
43529435951eSToby Isaac .seealso: DMGetDefaultConstraints()
43539435951eSToby Isaac @*/
43549435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
43559435951eSToby Isaac {
4356e228b242SToby Isaac   PetscMPIInt result;
43579435951eSToby Isaac   PetscErrorCode ierr;
43589435951eSToby Isaac 
43599435951eSToby Isaac   PetscFunctionBegin;
43609435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4361e228b242SToby Isaac   if (section) {
4362e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4363ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRMPI(ierr);
4364f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4365e228b242SToby Isaac   }
4366e228b242SToby Isaac   if (mat) {
4367e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4368ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRMPI(ierr);
4369f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4370e228b242SToby Isaac   }
43719435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
43729435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
43739435951eSToby Isaac   dm->defaultConstraintSection = section;
43749435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
43759435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
43769435951eSToby Isaac   dm->defaultConstraintMat = mat;
43779435951eSToby Isaac   PetscFunctionReturn(0);
43789435951eSToby Isaac }
43799435951eSToby Isaac 
4380497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4381507e4973SMatthew G. Knepley /*
4382507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4383507e4973SMatthew G. Knepley 
4384507e4973SMatthew G. Knepley   Input Parameters:
4385507e4973SMatthew G. Knepley + dm - The DM
4386507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4387507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4388507e4973SMatthew G. Knepley 
4389507e4973SMatthew G. Knepley   Level: intermediate
4390507e4973SMatthew G. Knepley 
43911bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4392507e4973SMatthew G. Knepley */
4393f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4394507e4973SMatthew G. Knepley {
4395507e4973SMatthew G. Knepley   MPI_Comm        comm;
4396507e4973SMatthew G. Knepley   PetscLayout     layout;
4397507e4973SMatthew G. Knepley   const PetscInt *ranges;
4398507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4399507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4400507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4401507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4402507e4973SMatthew G. Knepley 
4403507e4973SMatthew G. Knepley   PetscFunctionBegin;
4404507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4405507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4406ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
4407ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
4408507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4409507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4410507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4411507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4412507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4413507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4414507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4415507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4416f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4417507e4973SMatthew G. Knepley 
4418507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4419507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4420507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4421507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4422507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4423507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4424507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4425507e4973SMatthew 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;}
4426507e4973SMatthew 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;}
4427507e4973SMatthew G. Knepley     if (gdof < 0) {
4428507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4429507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4430507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4431507e4973SMatthew G. Knepley 
4432507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4433507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4434507e4973SMatthew 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;}
4435507e4973SMatthew G. Knepley       }
4436507e4973SMatthew G. Knepley     }
4437507e4973SMatthew G. Knepley   }
4438507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4439507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4440820f2d46SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRMPI(ierr);
4441507e4973SMatthew G. Knepley   if (!gvalid) {
4442507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4443507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4444507e4973SMatthew G. Knepley   }
4445507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4446507e4973SMatthew G. Knepley }
4447f741bcd2SMatthew G. Knepley #endif
4448507e4973SMatthew G. Knepley 
444988ed4aceSMatthew G Knepley /*@
4450e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
445188ed4aceSMatthew G Knepley 
4452d083f849SBarry Smith   Collective on dm
44538b1ab98fSJed Brown 
445488ed4aceSMatthew G Knepley   Input Parameter:
445588ed4aceSMatthew G Knepley . dm - The DM
445688ed4aceSMatthew G Knepley 
445788ed4aceSMatthew G Knepley   Output Parameter:
445888ed4aceSMatthew G Knepley . section - The PetscSection
445988ed4aceSMatthew G Knepley 
446088ed4aceSMatthew G Knepley   Level: intermediate
446188ed4aceSMatthew G Knepley 
446288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
446388ed4aceSMatthew G Knepley 
446492fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
446588ed4aceSMatthew G Knepley @*/
4466e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
44670adebc6cSBarry Smith {
446888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
446988ed4aceSMatthew G Knepley 
447088ed4aceSMatthew G Knepley   PetscFunctionBegin;
447188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
447288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
44731bb6d2a8SBarry Smith   if (!dm->globalSection) {
4474fd59a867SMatthew G. Knepley     PetscSection s;
4475fd59a867SMatthew G. Knepley 
447692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4477fd59a867SMatthew 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");
447833907cc2SStefano 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");
44791bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4480cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
44811bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
44821bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
448388ed4aceSMatthew G Knepley   }
44841bb6d2a8SBarry Smith   *section = dm->globalSection;
448588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
448688ed4aceSMatthew G Knepley }
448788ed4aceSMatthew G Knepley 
4488b21d0597SMatthew G Knepley /*@
4489e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4490b21d0597SMatthew G Knepley 
4491b21d0597SMatthew G Knepley   Input Parameters:
4492b21d0597SMatthew G Knepley + dm - The DM
44935080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4494b21d0597SMatthew G Knepley 
4495b21d0597SMatthew G Knepley   Level: intermediate
4496b21d0597SMatthew G Knepley 
4497b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4498b21d0597SMatthew G Knepley 
449992fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4500b21d0597SMatthew G Knepley @*/
4501e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
45020adebc6cSBarry Smith {
4503b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4504b21d0597SMatthew G Knepley 
4505b21d0597SMatthew G Knepley   PetscFunctionBegin;
4506b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45075080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
45081d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
45091bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
45101bb6d2a8SBarry Smith   dm->globalSection = section;
4511497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45121bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4513507e4973SMatthew G. Knepley #endif
4514b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4515b21d0597SMatthew G Knepley }
4516b21d0597SMatthew G Knepley 
451788ed4aceSMatthew G Knepley /*@
45181bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
451988ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
452088ed4aceSMatthew G Knepley 
452188ed4aceSMatthew G Knepley   Input Parameter:
452288ed4aceSMatthew G Knepley . dm - The DM
452388ed4aceSMatthew G Knepley 
452488ed4aceSMatthew G Knepley   Output Parameter:
452588ed4aceSMatthew G Knepley . sf - The PetscSF
452688ed4aceSMatthew G Knepley 
452788ed4aceSMatthew G Knepley   Level: intermediate
452888ed4aceSMatthew G Knepley 
452988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
453088ed4aceSMatthew G Knepley 
45311bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
453288ed4aceSMatthew G Knepley @*/
45331bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
45340adebc6cSBarry Smith {
453588ed4aceSMatthew G Knepley   PetscInt       nroots;
453688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
453788ed4aceSMatthew G Knepley 
453888ed4aceSMatthew G Knepley   PetscFunctionBegin;
453988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
454088ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
45411bb6d2a8SBarry Smith   if (!dm->sectionSF) {
45421bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
454333907cc2SStefano Zampini   }
45441bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
454588ed4aceSMatthew G Knepley   if (nroots < 0) {
454688ed4aceSMatthew G Knepley     PetscSection section, gSection;
454788ed4aceSMatthew G Knepley 
454892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
454931ea6d37SMatthew G Knepley     if (section) {
4550e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
45511bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
455231ea6d37SMatthew G Knepley     } else {
45530298fd71SBarry Smith       *sf = NULL;
455431ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
455531ea6d37SMatthew G Knepley     }
455688ed4aceSMatthew G Knepley   }
45571bb6d2a8SBarry Smith   *sf = dm->sectionSF;
455888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
455988ed4aceSMatthew G Knepley }
456088ed4aceSMatthew G Knepley 
456188ed4aceSMatthew G Knepley /*@
45621bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
456388ed4aceSMatthew G Knepley 
456488ed4aceSMatthew G Knepley   Input Parameters:
456588ed4aceSMatthew G Knepley + dm - The DM
456688ed4aceSMatthew G Knepley - sf - The PetscSF
456788ed4aceSMatthew G Knepley 
456888ed4aceSMatthew G Knepley   Level: intermediate
456988ed4aceSMatthew G Knepley 
457088ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
457188ed4aceSMatthew G Knepley 
45721bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
457388ed4aceSMatthew G Knepley @*/
45741bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
45750adebc6cSBarry Smith {
457688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
457788ed4aceSMatthew G Knepley 
457888ed4aceSMatthew G Knepley   PetscFunctionBegin;
457988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4580b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
458133907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
45821bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
45831bb6d2a8SBarry Smith   dm->sectionSF = sf;
458488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
458588ed4aceSMatthew G Knepley }
458688ed4aceSMatthew G Knepley 
458788ed4aceSMatthew G Knepley /*@C
45881bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
458988ed4aceSMatthew G Knepley   describing the data layout.
459088ed4aceSMatthew G Knepley 
459188ed4aceSMatthew G Knepley   Input Parameters:
459288ed4aceSMatthew G Knepley + dm - The DM
459388ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
459488ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
459588ed4aceSMatthew G Knepley 
45961bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
459788ed4aceSMatthew G Knepley 
45981bb6d2a8SBarry Smith   Level: developer
45991bb6d2a8SBarry Smith 
46001bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
46011bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
46021bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
46031bb6d2a8SBarry Smith 
46041bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
460588ed4aceSMatthew G Knepley @*/
46061bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
460788ed4aceSMatthew G Knepley {
460888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
460988ed4aceSMatthew G Knepley 
461088ed4aceSMatthew G Knepley   PetscFunctionBegin;
461188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4612b0c7db22SLisandro Dalcin   ierr = PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection);CHKERRQ(ierr);
461388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
461488ed4aceSMatthew G Knepley }
4615af122d2aSMatthew G Knepley 
4616b21d0597SMatthew G Knepley /*@
4617b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4618b21d0597SMatthew G Knepley 
4619b21d0597SMatthew G Knepley   Input Parameter:
4620b21d0597SMatthew G Knepley . dm - The DM
4621b21d0597SMatthew G Knepley 
4622b21d0597SMatthew G Knepley   Output Parameter:
4623b21d0597SMatthew G Knepley . sf - The PetscSF
4624b21d0597SMatthew G Knepley 
4625b21d0597SMatthew G Knepley   Level: intermediate
4626b21d0597SMatthew G Knepley 
4627b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4628b21d0597SMatthew G Knepley 
46291bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4630b21d0597SMatthew G Knepley @*/
46310adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
46320adebc6cSBarry Smith {
4633b21d0597SMatthew G Knepley   PetscFunctionBegin;
4634b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4635b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4636b21d0597SMatthew G Knepley   *sf = dm->sf;
4637b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4638b21d0597SMatthew G Knepley }
4639b21d0597SMatthew G Knepley 
4640057b4bcdSMatthew G Knepley /*@
4641057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4642057b4bcdSMatthew G Knepley 
4643057b4bcdSMatthew G Knepley   Input Parameters:
4644057b4bcdSMatthew G Knepley + dm - The DM
4645057b4bcdSMatthew G Knepley - sf - The PetscSF
4646057b4bcdSMatthew G Knepley 
4647057b4bcdSMatthew G Knepley   Level: intermediate
4648057b4bcdSMatthew G Knepley 
46491bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4650057b4bcdSMatthew G Knepley @*/
46510adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
46520adebc6cSBarry Smith {
4653057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4654057b4bcdSMatthew G Knepley 
4655057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4656057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4657b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4658057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
465933907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4660057b4bcdSMatthew G Knepley   dm->sf = sf;
4661057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4662057b4bcdSMatthew G Knepley }
4663057b4bcdSMatthew G Knepley 
466434aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
466534aa8a36SMatthew G. Knepley {
466634aa8a36SMatthew G. Knepley   PetscClassId   id;
466734aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
466834aa8a36SMatthew G. Knepley 
466934aa8a36SMatthew G. Knepley   PetscFunctionBegin;
467034aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
467134aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
467234aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
467334aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
467434aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
467517c1d62eSMatthew G. Knepley   } else {
467617c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
467734aa8a36SMatthew G. Knepley   }
467834aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
467934aa8a36SMatthew G. Knepley }
468034aa8a36SMatthew G. Knepley 
468144a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
468244a7f3ddSMatthew G. Knepley {
468344a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
468444a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
468544a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
468644a7f3ddSMatthew G. Knepley 
468744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
468844a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
468944a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
469044a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4691e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
469244a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
469344a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
469444a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
469544a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
469644a7f3ddSMatthew G. Knepley }
469744a7f3ddSMatthew G. Knepley 
469844a7f3ddSMatthew G. Knepley /*@
469944a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
470044a7f3ddSMatthew G. Knepley 
4701d083f849SBarry Smith   Logically collective on dm
470244a7f3ddSMatthew G. Knepley 
470344a7f3ddSMatthew G. Knepley   Input Parameter:
470444a7f3ddSMatthew G. Knepley . dm - The DM
470544a7f3ddSMatthew G. Knepley 
470644a7f3ddSMatthew G. Knepley   Level: intermediate
470744a7f3ddSMatthew G. Knepley 
470844a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
470944a7f3ddSMatthew G. Knepley @*/
471044a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
471144a7f3ddSMatthew G. Knepley {
471244a7f3ddSMatthew G. Knepley   PetscInt       f;
471344a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
471444a7f3ddSMatthew G. Knepley 
471544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
471644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
471744a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
471844a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
471944a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
472044a7f3ddSMatthew G. Knepley   }
472144a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
472244a7f3ddSMatthew G. Knepley   dm->fields = NULL;
472344a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
472444a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
472544a7f3ddSMatthew G. Knepley }
472644a7f3ddSMatthew G. Knepley 
4727689b5837SMatthew G. Knepley /*@
4728689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4729689b5837SMatthew G. Knepley 
4730689b5837SMatthew G. Knepley   Not collective
4731689b5837SMatthew G. Knepley 
4732689b5837SMatthew G. Knepley   Input Parameter:
4733689b5837SMatthew G. Knepley . dm - The DM
4734689b5837SMatthew G. Knepley 
4735689b5837SMatthew G. Knepley   Output Parameter:
4736689b5837SMatthew G. Knepley . Nf - The number of fields
4737689b5837SMatthew G. Knepley 
4738689b5837SMatthew G. Knepley   Level: intermediate
4739689b5837SMatthew G. Knepley 
4740689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4741689b5837SMatthew G. Knepley @*/
47420f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
47430f21e855SMatthew G. Knepley {
47440f21e855SMatthew G. Knepley   PetscFunctionBegin;
47450f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4746534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
474744a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4748af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4749af122d2aSMatthew G Knepley }
4750af122d2aSMatthew G Knepley 
4751689b5837SMatthew G. Knepley /*@
4752689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4753689b5837SMatthew G. Knepley 
4754d083f849SBarry Smith   Logically collective on dm
4755689b5837SMatthew G. Knepley 
4756689b5837SMatthew G. Knepley   Input Parameters:
4757689b5837SMatthew G. Knepley + dm - The DM
4758689b5837SMatthew G. Knepley - Nf - The number of fields
4759689b5837SMatthew G. Knepley 
4760689b5837SMatthew G. Knepley   Level: intermediate
4761689b5837SMatthew G. Knepley 
4762689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4763689b5837SMatthew G. Knepley @*/
4764af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4765af122d2aSMatthew G Knepley {
47660f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4767af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4768af122d2aSMatthew G Knepley 
4769af122d2aSMatthew G Knepley   PetscFunctionBegin;
4770af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
477144a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
47720f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
47730f21e855SMatthew G. Knepley     PetscContainer obj;
47740f21e855SMatthew G. Knepley 
47750f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
477644a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
47770f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4778af122d2aSMatthew G Knepley   }
4779af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4780af122d2aSMatthew G Knepley }
4781af122d2aSMatthew G Knepley 
4782c1929be8SMatthew G. Knepley /*@
4783c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4784c1929be8SMatthew G. Knepley 
4785c1929be8SMatthew G. Knepley   Not collective
4786c1929be8SMatthew G. Knepley 
4787c1929be8SMatthew G. Knepley   Input Parameters:
4788c1929be8SMatthew G. Knepley + dm - The DM
4789c1929be8SMatthew G. Knepley - f  - The field number
4790c1929be8SMatthew G. Knepley 
479144a7f3ddSMatthew G. Knepley   Output Parameters:
479244a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
479344a7f3ddSMatthew G. Knepley - field - The discretization object
4794c1929be8SMatthew G. Knepley 
479544a7f3ddSMatthew G. Knepley   Level: intermediate
4796c1929be8SMatthew G. Knepley 
479744a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4798c1929be8SMatthew G. Knepley @*/
479944a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4800af122d2aSMatthew G Knepley {
4801af122d2aSMatthew G Knepley   PetscFunctionBegin;
4802af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4803064a246eSJacob Faibussowitsch   PetscValidPointer(field, 4);
480444a7f3ddSMatthew 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);
480544a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
480644a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4807decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4808decb47aaSMatthew G. Knepley }
4809decb47aaSMatthew G. Knepley 
4810083401c6SMatthew G. Knepley /* Does not clear the DS */
4811083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4812083401c6SMatthew G. Knepley {
4813083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4814083401c6SMatthew G. Knepley 
4815083401c6SMatthew G. Knepley   PetscFunctionBegin;
4816083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4817083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4818083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4819083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4820083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4821083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4822083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4823083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4824083401c6SMatthew G. Knepley }
4825083401c6SMatthew G. Knepley 
4826c1929be8SMatthew G. Knepley /*@
4827c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4828c1929be8SMatthew G. Knepley 
4829d083f849SBarry Smith   Logically collective on dm
4830c1929be8SMatthew G. Knepley 
4831c1929be8SMatthew G. Knepley   Input Parameters:
4832c1929be8SMatthew G. Knepley + dm    - The DM
4833c1929be8SMatthew G. Knepley . f     - The field number
483444a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4835c1929be8SMatthew G. Knepley - field - The discretization object
4836c1929be8SMatthew G. Knepley 
483744a7f3ddSMatthew G. Knepley   Level: intermediate
4838c1929be8SMatthew G. Knepley 
483944a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4840c1929be8SMatthew G. Knepley @*/
484144a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4842decb47aaSMatthew G. Knepley {
4843decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4844decb47aaSMatthew G. Knepley 
4845decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4846decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4847e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
484844a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4849e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4850083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
485134aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
48522df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
485344a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
485444a7f3ddSMatthew G. Knepley }
485544a7f3ddSMatthew G. Knepley 
485644a7f3ddSMatthew G. Knepley /*@
485744a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
485844a7f3ddSMatthew G. Knepley 
4859d083f849SBarry Smith   Logically collective on dm
486044a7f3ddSMatthew G. Knepley 
486144a7f3ddSMatthew G. Knepley   Input Parameters:
486244a7f3ddSMatthew G. Knepley + dm    - The DM
486344a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
486444a7f3ddSMatthew G. Knepley - field - The discretization object
486544a7f3ddSMatthew G. Knepley 
486644a7f3ddSMatthew G. Knepley   Level: intermediate
486744a7f3ddSMatthew G. Knepley 
486844a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
486944a7f3ddSMatthew G. Knepley @*/
487044a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
487144a7f3ddSMatthew G. Knepley {
487244a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
487344a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
487444a7f3ddSMatthew G. Knepley 
487544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
487644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4877064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
487844a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
487944a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
488044a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
488144a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
488244a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
488344a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
488434aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
48852df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4886af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4887af122d2aSMatthew G Knepley }
48886636e97aSMatthew G Knepley 
4889e5e52638SMatthew G. Knepley /*@
4890e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4891e0b68406SMatthew Knepley 
4892e0b68406SMatthew Knepley   Logically collective on dm
4893e0b68406SMatthew Knepley 
4894e0b68406SMatthew Knepley   Input Parameters:
4895e0b68406SMatthew Knepley + dm          - The DM
4896e0b68406SMatthew Knepley . f           - The field index
4897e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
4898e0b68406SMatthew Knepley 
4899e0b68406SMatthew Knepley   Level: intermediate
4900e0b68406SMatthew Knepley 
4901e0b68406SMatthew Knepley .seealso: DMGetFieldAvoidTensor(), DMSetField(), DMGetField()
4902e0b68406SMatthew Knepley @*/
4903e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
4904e0b68406SMatthew Knepley {
4905e0b68406SMatthew Knepley   PetscFunctionBegin;
4906e0b68406SMatthew 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);
4907e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
4908e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4909e0b68406SMatthew Knepley }
4910e0b68406SMatthew Knepley 
4911e0b68406SMatthew Knepley /*@
4912e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
4913e0b68406SMatthew Knepley 
4914e0b68406SMatthew Knepley   Logically collective on dm
4915e0b68406SMatthew Knepley 
4916e0b68406SMatthew Knepley   Input Parameters:
4917e0b68406SMatthew Knepley + dm          - The DM
4918e0b68406SMatthew Knepley - f           - The field index
4919e0b68406SMatthew Knepley 
4920e0b68406SMatthew Knepley   Output Parameter:
4921e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
4922e0b68406SMatthew Knepley 
4923e0b68406SMatthew Knepley   Level: intermediate
4924e0b68406SMatthew Knepley 
4925e0b68406SMatthew Knepley .seealso: DMSetFieldAvoidTensor(), DMSetField(), DMGetField()
4926e0b68406SMatthew Knepley @*/
4927e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
4928e0b68406SMatthew Knepley {
4929e0b68406SMatthew Knepley   PetscFunctionBegin;
4930e0b68406SMatthew 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);
4931e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
4932e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4933e0b68406SMatthew Knepley }
4934e0b68406SMatthew Knepley 
4935e0b68406SMatthew Knepley /*@
4936e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4937e5e52638SMatthew G. Knepley 
4938d083f849SBarry Smith   Collective on dm
4939e5e52638SMatthew G. Knepley 
4940e5e52638SMatthew G. Knepley   Input Parameter:
4941e5e52638SMatthew G. Knepley . dm - The DM
4942e5e52638SMatthew G. Knepley 
4943e5e52638SMatthew G. Knepley   Output Parameter:
4944e5e52638SMatthew G. Knepley . newdm - The DM
4945e5e52638SMatthew G. Knepley 
4946e5e52638SMatthew G. Knepley   Level: advanced
4947e5e52638SMatthew G. Knepley 
4948e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4949e5e52638SMatthew G. Knepley @*/
4950e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4951e5e52638SMatthew G. Knepley {
4952e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4953e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4954e5e52638SMatthew G. Knepley 
4955e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4956e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4957e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4958e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4959e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4960e5e52638SMatthew G. Knepley     DMLabel     label;
4961e5e52638SMatthew G. Knepley     PetscObject field;
496234aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4963e5e52638SMatthew G. Knepley 
4964e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4965e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
496634aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
496734aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
496834aa8a36SMatthew G. Knepley   }
496934aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
497034aa8a36SMatthew G. Knepley }
497134aa8a36SMatthew G. Knepley 
497234aa8a36SMatthew G. Knepley /*@
497334aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
497434aa8a36SMatthew G. Knepley 
497534aa8a36SMatthew G. Knepley   Not collective
497634aa8a36SMatthew G. Knepley 
497734aa8a36SMatthew G. Knepley   Input Parameters:
497834aa8a36SMatthew G. Knepley + dm - The DM object
497934aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
498034aa8a36SMatthew G. Knepley 
4981d8d19677SJose E. Roman   Output Parameters:
498234aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
498334aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
498434aa8a36SMatthew G. Knepley 
498534aa8a36SMatthew G. Knepley   Notes:
498634aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
498734aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
498834aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4989979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
499034aa8a36SMatthew G. Knepley 
499134aa8a36SMatthew G. Knepley   Level: developer
499234aa8a36SMatthew G. Knepley 
499334aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
499434aa8a36SMatthew G. Knepley @*/
499534aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
499634aa8a36SMatthew G. Knepley {
499734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
499834aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4999534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
5000534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
500134aa8a36SMatthew G. Knepley   if (f < 0) {
500234aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
500334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
500434aa8a36SMatthew G. Knepley   } else {
500534aa8a36SMatthew G. Knepley     PetscInt       Nf;
500634aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
500734aa8a36SMatthew G. Knepley 
500834aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
500934aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
501034aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
501134aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
501234aa8a36SMatthew G. Knepley   }
501334aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
501434aa8a36SMatthew G. Knepley }
501534aa8a36SMatthew G. Knepley 
501634aa8a36SMatthew G. Knepley /*@
501734aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
501834aa8a36SMatthew G. Knepley 
501934aa8a36SMatthew G. Knepley   Not collective
502034aa8a36SMatthew G. Knepley 
502134aa8a36SMatthew G. Knepley   Input Parameters:
502234aa8a36SMatthew G. Knepley + dm         - The DM object
502334aa8a36SMatthew G. Knepley . f          - The field number
502434aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
502534aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
502634aa8a36SMatthew G. Knepley 
502734aa8a36SMatthew G. Knepley   Notes:
502834aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
502934aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
503034aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5031979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
503234aa8a36SMatthew G. Knepley 
503334aa8a36SMatthew G. Knepley   Level: developer
503434aa8a36SMatthew G. Knepley 
503534aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
503634aa8a36SMatthew G. Knepley @*/
503734aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
503834aa8a36SMatthew G. Knepley {
503934aa8a36SMatthew G. Knepley   PetscFunctionBegin;
504034aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
504134aa8a36SMatthew G. Knepley   if (f < 0) {
504234aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
504334aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
504434aa8a36SMatthew G. Knepley   } else {
504534aa8a36SMatthew G. Knepley     PetscInt       Nf;
504634aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
504734aa8a36SMatthew G. Knepley 
504834aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
504934aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
505034aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
505134aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5052e5e52638SMatthew G. Knepley   }
5053e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5054e5e52638SMatthew G. Knepley }
5055e5e52638SMatthew G. Knepley 
5056b0441da4SMatthew G. Knepley /*@
5057b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5058b0441da4SMatthew G. Knepley 
5059b0441da4SMatthew G. Knepley   Not collective
5060b0441da4SMatthew G. Knepley 
5061f899ff85SJose E. Roman   Input Parameter:
5062b0441da4SMatthew G. Knepley . dm - The DM object
5063b0441da4SMatthew G. Knepley 
5064d8d19677SJose E. Roman   Output Parameters:
5065b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5066b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5067b0441da4SMatthew G. Knepley 
5068b0441da4SMatthew G. Knepley   Notes:
5069b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5070b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5071b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5072b0441da4SMatthew G. Knepley 
5073b0441da4SMatthew G. Knepley   Level: developer
5074b0441da4SMatthew G. Knepley 
5075b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
5076b0441da4SMatthew G. Knepley @*/
5077b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5078b0441da4SMatthew G. Knepley {
5079b0441da4SMatthew G. Knepley   PetscInt       Nf;
5080b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5081b0441da4SMatthew G. Knepley 
5082b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5083b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5084064a246eSJacob Faibussowitsch   if (useCone)    PetscValidBoolPointer(useCone, 2);
5085064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
5086b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5087b0441da4SMatthew G. Knepley   if (!Nf) {
5088b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5089b0441da4SMatthew G. Knepley   } else {
5090b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5091b0441da4SMatthew G. Knepley   }
5092b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5093b0441da4SMatthew G. Knepley }
5094b0441da4SMatthew G. Knepley 
5095b0441da4SMatthew G. Knepley /*@
5096b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5097b0441da4SMatthew G. Knepley 
5098b0441da4SMatthew G. Knepley   Not collective
5099b0441da4SMatthew G. Knepley 
5100b0441da4SMatthew G. Knepley   Input Parameters:
5101b0441da4SMatthew G. Knepley + dm         - The DM object
5102b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5103b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5104b0441da4SMatthew G. Knepley 
5105b0441da4SMatthew G. Knepley   Notes:
5106b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5107b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5108b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5109b0441da4SMatthew G. Knepley 
5110b0441da4SMatthew G. Knepley   Level: developer
5111b0441da4SMatthew G. Knepley 
5112b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
5113b0441da4SMatthew G. Knepley @*/
5114b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5115b0441da4SMatthew G. Knepley {
5116b0441da4SMatthew G. Knepley   PetscInt       Nf;
5117b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5118b0441da4SMatthew G. Knepley 
5119b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5120b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5121b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5122b0441da4SMatthew G. Knepley   if (!Nf) {
5123b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5124b0441da4SMatthew G. Knepley   } else {
5125b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5126e5e52638SMatthew G. Knepley   }
5127e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5128e5e52638SMatthew G. Knepley }
5129e5e52638SMatthew G. Knepley 
5130783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
513145480ffeSMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, DMLabel label)
5132783e2ec8SMatthew G. Knepley {
5133783e2ec8SMatthew G. Knepley   PetscObject    obj;
5134783e2ec8SMatthew G. Knepley   PetscClassId   id;
5135783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
5136783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
5137783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
5138783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
5139783e2ec8SMatthew G. Knepley 
5140783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5141783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
5142783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5143783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
5144783e2ec8SMatthew G. Knepley   if (isFE && label) {
5145783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
5146783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5147783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
514845480ffeSMatthew G. Knepley       DMLabel l;
5149783e2ec8SMatthew G. Knepley 
515045480ffeSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, NULL, &l, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
515145480ffeSMatthew G. Knepley       duplicate = l == label ? PETSC_TRUE : PETSC_FALSE;
5152783e2ec8SMatthew G. Knepley       if (duplicate) break;
5153783e2ec8SMatthew G. Knepley     }
5154783e2ec8SMatthew G. Knepley     if (!duplicate) {
5155783e2ec8SMatthew G. Knepley       DM plex;
5156783e2ec8SMatthew G. Knepley 
5157783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
5158783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
5159783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
5160783e2ec8SMatthew G. Knepley     }
5161783e2ec8SMatthew G. Knepley   }
5162783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5163783e2ec8SMatthew G. Knepley }
5164783e2ec8SMatthew G. Knepley 
5165e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5166e5e52638SMatthew G. Knepley {
5167e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5168e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5169e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5170e5e52638SMatthew G. Knepley 
5171e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5172e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5173e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
5174e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5175b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5176e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5177e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5178e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5179e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5180e5e52638SMatthew G. Knepley }
5181e5e52638SMatthew G. Knepley 
5182e5e52638SMatthew G. Knepley /*@
5183e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5184e5e52638SMatthew G. Knepley 
5185e5e52638SMatthew G. Knepley   Not collective
5186e5e52638SMatthew G. Knepley 
5187e5e52638SMatthew G. Knepley   Input Parameter:
5188e5e52638SMatthew G. Knepley . dm - The DM
5189e5e52638SMatthew G. Knepley 
5190e5e52638SMatthew G. Knepley   Output Parameter:
5191e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5192e5e52638SMatthew G. Knepley 
5193e5e52638SMatthew G. Knepley   Level: intermediate
5194e5e52638SMatthew G. Knepley 
5195e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5196e5e52638SMatthew G. Knepley @*/
5197e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5198e5e52638SMatthew G. Knepley {
5199e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5200e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5201534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5202e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5203e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5204e5e52638SMatthew G. Knepley }
5205e5e52638SMatthew G. Knepley 
5206e5e52638SMatthew G. Knepley /*@
5207e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5208e5e52638SMatthew G. Knepley 
5209d083f849SBarry Smith   Logically collective on dm
5210e5e52638SMatthew G. Knepley 
5211e5e52638SMatthew G. Knepley   Input Parameter:
5212e5e52638SMatthew G. Knepley . dm - The DM
5213e5e52638SMatthew G. Knepley 
5214e5e52638SMatthew G. Knepley   Level: intermediate
5215e5e52638SMatthew G. Knepley 
5216e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5217e5e52638SMatthew G. Knepley @*/
5218e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5219e5e52638SMatthew G. Knepley {
5220e5e52638SMatthew G. Knepley   PetscInt       s;
5221e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5222e5e52638SMatthew G. Knepley 
5223e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5224e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5225e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5226e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5227e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5228b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5229e5e52638SMatthew G. Knepley   }
5230e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5231e5e52638SMatthew G. Knepley   dm->probs = NULL;
5232e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5233e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5234e5e52638SMatthew G. Knepley }
5235e5e52638SMatthew G. Knepley 
5236e5e52638SMatthew G. Knepley /*@
5237e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5238e5e52638SMatthew G. Knepley 
5239e5e52638SMatthew G. Knepley   Not collective
5240e5e52638SMatthew G. Knepley 
5241e5e52638SMatthew G. Knepley   Input Parameter:
5242e5e52638SMatthew G. Knepley . dm    - The DM
5243e5e52638SMatthew G. Knepley 
5244e5e52638SMatthew G. Knepley   Output Parameter:
5245e5e52638SMatthew G. Knepley . prob - The default PetscDS
5246e5e52638SMatthew G. Knepley 
5247e5e52638SMatthew G. Knepley   Level: intermediate
5248e5e52638SMatthew G. Knepley 
5249e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5250e5e52638SMatthew G. Knepley @*/
5251e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5252e5e52638SMatthew G. Knepley {
5253b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5254b0143b4dSMatthew G. Knepley 
5255e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5256e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5257e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5258b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5259b0143b4dSMatthew G. Knepley     PetscDS ds;
5260b0143b4dSMatthew G. Knepley 
526145480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5262b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5263b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5264b0143b4dSMatthew G. Knepley   }
5265b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5266e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5267e5e52638SMatthew G. Knepley }
5268e5e52638SMatthew G. Knepley 
5269e5e52638SMatthew G. Knepley /*@
5270e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5271e5e52638SMatthew G. Knepley 
5272e5e52638SMatthew G. Knepley   Not collective
5273e5e52638SMatthew G. Knepley 
5274e5e52638SMatthew G. Knepley   Input Parameters:
5275e5e52638SMatthew G. Knepley + dm    - The DM
5276e5e52638SMatthew G. Knepley - point - Cell for the DS
5277e5e52638SMatthew G. Knepley 
5278e5e52638SMatthew G. Knepley   Output Parameter:
5279e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5280e5e52638SMatthew G. Knepley 
5281e5e52638SMatthew G. Knepley   Level: developer
5282e5e52638SMatthew G. Knepley 
5283b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5284e5e52638SMatthew G. Knepley @*/
5285e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5286e5e52638SMatthew G. Knepley {
5287e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5288e5e52638SMatthew G. Knepley   PetscInt       s;
5289e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5290e5e52638SMatthew G. Knepley 
5291e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5292e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5293e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5294360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5295e5e52638SMatthew G. Knepley   *prob = NULL;
5296e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5297e5e52638SMatthew G. Knepley     PetscInt val;
5298e5e52638SMatthew G. Knepley 
5299e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5300e5e52638SMatthew G. Knepley     else {
5301e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5302e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5303e5e52638SMatthew G. Knepley     }
5304e5e52638SMatthew G. Knepley   }
5305e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5306e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5307e5e52638SMatthew G. Knepley }
5308e5e52638SMatthew G. Knepley 
5309e5e52638SMatthew G. Knepley /*@
5310e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5311e5e52638SMatthew G. Knepley 
5312e5e52638SMatthew G. Knepley   Not collective
5313e5e52638SMatthew G. Knepley 
5314e5e52638SMatthew G. Knepley   Input Parameters:
5315e5e52638SMatthew G. Knepley + dm    - The DM
5316e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5317e5e52638SMatthew G. Knepley 
5318b3cf3223SMatthew G. Knepley   Output Parameters:
5319b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5320b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5321e5e52638SMatthew G. Knepley 
5322e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5323e5e52638SMatthew G. Knepley 
5324e5e52638SMatthew G. Knepley   Level: advanced
5325e5e52638SMatthew G. Knepley 
5326e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5327e5e52638SMatthew G. Knepley @*/
5328b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5329e5e52638SMatthew G. Knepley {
5330e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5331e5e52638SMatthew G. Knepley 
5332e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5333e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5334e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5335b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5336b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5337e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5338b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5339b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5340b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5341b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5342b3cf3223SMatthew G. Knepley     }
5343e5e52638SMatthew G. Knepley   }
53442df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5345e5e52638SMatthew G. Knepley }
5346e5e52638SMatthew G. Knepley 
5347e5e52638SMatthew G. Knepley /*@
5348083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5349083401c6SMatthew G. Knepley 
5350083401c6SMatthew G. Knepley   Collective on dm
5351083401c6SMatthew G. Knepley 
5352083401c6SMatthew G. Knepley   Input Parameters:
5353083401c6SMatthew G. Knepley + dm     - The DM
5354083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5355083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5356083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5357083401c6SMatthew G. Knepley 
5358083401c6SMatthew 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,
5359083401c6SMatthew G. Knepley   the fields argument is ignored.
5360083401c6SMatthew G. Knepley 
5361083401c6SMatthew G. Knepley   Level: advanced
5362083401c6SMatthew G. Knepley 
5363083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5364083401c6SMatthew G. Knepley @*/
5365083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5366083401c6SMatthew G. Knepley {
5367083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5368083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5369083401c6SMatthew G. Knepley 
5370083401c6SMatthew G. Knepley   PetscFunctionBegin;
5371083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5372083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5373064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5374083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5375083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5376083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5377083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5378083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5379083401c6SMatthew G. Knepley     }
5380083401c6SMatthew G. Knepley   }
5381083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5382083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5383083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5384083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5385083401c6SMatthew G. Knepley   if (!label) {
5386083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5387083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5388083401c6SMatthew G. Knepley     Nds = 0;
5389083401c6SMatthew G. Knepley   }
5390083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5391083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5392083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5393083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5394083401c6SMatthew G. Knepley }
5395083401c6SMatthew G. Knepley 
5396083401c6SMatthew G. Knepley /*@
5397e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5398e5e52638SMatthew G. Knepley 
5399e5e52638SMatthew G. Knepley   Not collective
5400e5e52638SMatthew G. Knepley 
5401e5e52638SMatthew G. Knepley   Input Parameters:
5402e5e52638SMatthew G. Knepley + dm  - The DM
5403e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5404e5e52638SMatthew G. Knepley 
5405e5e52638SMatthew G. Knepley   Output Parameters:
5406b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5407b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5408083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5409e5e52638SMatthew G. Knepley 
5410e5e52638SMatthew G. Knepley   Level: advanced
5411e5e52638SMatthew G. Knepley 
5412e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5413e5e52638SMatthew G. Knepley @*/
5414b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5415e5e52638SMatthew G. Knepley {
5416e5e52638SMatthew G. Knepley   PetscInt       Nds;
5417e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5418e5e52638SMatthew G. Knepley 
5419e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5420e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5421e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5422e5e52638SMatthew 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);
5423e5e52638SMatthew G. Knepley   if (label) {
5424e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5425e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5426e5e52638SMatthew G. Knepley   }
5427b3cf3223SMatthew G. Knepley   if (fields) {
5428b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5429b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5430b3cf3223SMatthew G. Knepley   }
5431e5e52638SMatthew G. Knepley   if (ds) {
5432b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5433e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5434e5e52638SMatthew G. Knepley   }
5435e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5436e5e52638SMatthew G. Knepley }
5437e5e52638SMatthew G. Knepley 
5438e5e52638SMatthew G. Knepley /*@
5439083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5440e5e52638SMatthew G. Knepley 
5441083401c6SMatthew G. Knepley   Not collective
5442e5e52638SMatthew G. Knepley 
5443e5e52638SMatthew G. Knepley   Input Parameters:
5444e5e52638SMatthew G. Knepley + dm     - The DM
5445083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5446083401c6SMatthew G. Knepley . label  - The region label, or NULL
5447083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5448083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5449e5e52638SMatthew G. Knepley 
5450e5e52638SMatthew G. Knepley   Level: advanced
5451e5e52638SMatthew G. Knepley 
5452083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5453e5e52638SMatthew G. Knepley @*/
5454083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5455e5e52638SMatthew G. Knepley {
5456083401c6SMatthew G. Knepley   PetscInt       Nds;
5457e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5458e5e52638SMatthew G. Knepley 
5459e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5460e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5461083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5462083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5463083401c6SMatthew 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);
5464e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5465083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5466083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5467083401c6SMatthew G. Knepley   if (fields) {
5468083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5469b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5470083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5471083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5472e5e52638SMatthew G. Knepley   }
5473083401c6SMatthew G. Knepley   if (ds) {
5474083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5475083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5476083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5477083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5478083401c6SMatthew G. Knepley   }
5479e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5480e5e52638SMatthew G. Knepley }
5481e5e52638SMatthew G. Knepley 
5482e5e52638SMatthew G. Knepley /*@
54831d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
54841d3af9e0SMatthew G. Knepley 
54851d3af9e0SMatthew G. Knepley   Not collective
54861d3af9e0SMatthew G. Knepley 
54871d3af9e0SMatthew G. Knepley   Input Parameters:
54881d3af9e0SMatthew G. Knepley + dm  - The DM
54891d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
54901d3af9e0SMatthew G. Knepley 
54911d3af9e0SMatthew G. Knepley   Output Parameter:
54921d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
54931d3af9e0SMatthew G. Knepley 
54941d3af9e0SMatthew G. Knepley   Level: advanced
54951d3af9e0SMatthew G. Knepley 
54961d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
54971d3af9e0SMatthew G. Knepley @*/
54981d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
54991d3af9e0SMatthew G. Knepley {
55001d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
55011d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
55021d3af9e0SMatthew G. Knepley 
55031d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
55041d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55051d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
55061d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
55071d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
55081d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
55091d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
55101d3af9e0SMatthew G. Knepley   else          *num = n;
55111d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
55121d3af9e0SMatthew G. Knepley }
55131d3af9e0SMatthew G. Knepley 
55141d3af9e0SMatthew G. Knepley /*@
5515e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5516e5e52638SMatthew G. Knepley 
5517d083f849SBarry Smith   Collective on dm
5518e5e52638SMatthew G. Knepley 
5519e5e52638SMatthew G. Knepley   Input Parameter:
5520e5e52638SMatthew G. Knepley . dm - The DM
5521e5e52638SMatthew G. Knepley 
552245480ffeSMatthew G. Knepley   Options Database Keys:
552345480ffeSMatthew G. Knepley . -dm_petscds_view - View all the PetscDS objects in this DM
552445480ffeSMatthew G. Knepley 
5525e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5526e5e52638SMatthew G. Knepley 
5527e5e52638SMatthew G. Knepley   Level: intermediate
5528e5e52638SMatthew G. Knepley 
5529e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5530e5e52638SMatthew G. Knepley @*/
5531e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5532e5e52638SMatthew G. Knepley {
5533e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5534083401c6SMatthew G. Knepley   PetscDS        dsDef;
5535083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5536f9244615SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5537f9244615SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE, flg;
5538e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5539e5e52638SMatthew G. Knepley 
5540e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5541e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5542e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5543e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5544083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5545083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5546083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5547083401c6SMatthew G. Knepley   Nl   = 0;
5548083401c6SMatthew G. Knepley   Ndef = 0;
5549083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5550083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5551083401c6SMatthew G. Knepley     PetscInt l;
5552083401c6SMatthew G. Knepley 
5553f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5554f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5555f918ec44SMatthew G. Knepley     {
5556f918ec44SMatthew G. Knepley       PetscClassId id;
5557f918ec44SMatthew G. Knepley 
5558f918ec44SMatthew G. Knepley       ierr = PetscObjectGetClassId(dm->fields[f].disc, &id);CHKERRQ(ierr);
5559f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5560f918ec44SMatthew G. Knepley         Ceed ceed;
5561f918ec44SMatthew G. Knepley 
5562f918ec44SMatthew G. Knepley         ierr = DMGetCeed(dm, &ceed);CHKERRQ(ierr);
5563f918ec44SMatthew G. Knepley         ierr = PetscFESetCeed((PetscFE) dm->fields[f].disc, ceed);CHKERRQ(ierr);
5564f918ec44SMatthew G. Knepley       }
5565f918ec44SMatthew G. Knepley     }
5566f918ec44SMatthew G. Knepley #endif
5567083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5568083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5569083401c6SMatthew G. Knepley     if (l < Nl) continue;
5570083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5571083401c6SMatthew G. Knepley   }
5572083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5573083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5574083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5575b3cf3223SMatthew G. Knepley     IS        fields;
5576b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5577b3cf3223SMatthew G. Knepley 
5578b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5579083401c6SMatthew G. Knepley     if (nf) {
5580b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5581b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
558288f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
558388f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
558488f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
558588f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
558688f0c812SMatthew G. Knepley 
558745480ffeSMatthew G. Knepley       ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5588083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5589083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5590b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
55912df9ee95SMatthew G. Knepley     }
5592083401c6SMatthew G. Knepley   }
5593083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5594083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5595083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5596083401c6SMatthew G. Knepley   if (Ndef && Nl) {
55970122748bSMatthew G. Knepley     DM              plex;
5598083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5599083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5600083401c6SMatthew G. Knepley     PetscInt       *fields;
5601083401c6SMatthew G. Knepley     const PetscInt *cells;
5602083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
56030122748bSMatthew G. Knepley 
56040122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
56050122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5606083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5607083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5608083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5609083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5610083401c6SMatthew G. Knepley       IS      pointIS;
5611083401c6SMatthew G. Knepley 
5612083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5613083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5614083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5615083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5616083401c6SMatthew G. Knepley     }
5617083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5618083401c6SMatthew G. Knepley 
5619083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5620083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5621083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5622083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5623083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5624083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5625083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5626083401c6SMatthew G. Knepley 
5627083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5628083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5629083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5630083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5631083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5632083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5633083401c6SMatthew G. Knepley 
563445480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5635083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5636083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5637083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5638083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5639083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
56400122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5641083401c6SMatthew G. Knepley   }
5642083401c6SMatthew G. Knepley   /* Create label DSes
5643083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5644083401c6SMatthew G. Knepley   */
5645083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5646083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5647083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5648083401c6SMatthew G. Knepley     PetscDS   ds;
5649083401c6SMatthew G. Knepley     IS        fields;
5650083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5651083401c6SMatthew G. Knepley 
565245480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5653083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5654083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5655083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5656083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5657083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5658083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5659083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5660083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5661083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5662083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5663083401c6SMatthew G. Knepley     {
5664083401c6SMatthew G. Knepley       DMPolytopeType ct;
5665083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5666083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
56670122748bSMatthew G. Knepley 
5668e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5669665f567fSMatthew G. Knepley       if (lStart >= 0) {
5670412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5671412e9a14SMatthew G. Knepley         switch (ct) {
5672412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5673412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5674412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5675412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5676083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5677083401c6SMatthew G. Knepley           default: break;
5678412e9a14SMatthew G. Knepley         }
5679665f567fSMatthew G. Knepley       }
5680ffc4695bSBarry Smith       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRMPI(ierr);
5681083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5682e5e52638SMatthew G. Knepley     }
5683083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5684e5e52638SMatthew G. Knepley   }
5685083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5686e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5687083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5688083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5689083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5690083401c6SMatthew G. Knepley     const PetscInt *fld;
5691083401c6SMatthew G. Knepley     PetscInt        nf;
5692e5e52638SMatthew G. Knepley 
5693083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5694083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5695083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5696083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5697083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5698e5e52638SMatthew G. Knepley       PetscClassId id;
5699e5e52638SMatthew G. Knepley 
5700083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5701083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5702083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5703083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5704083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5705e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5706e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5707e5e52638SMatthew G. Knepley     }
5708083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5709e5e52638SMatthew G. Knepley   }
5710f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
5711f9244615SMatthew G. Knepley   ierr = PetscOptionsGetInt(NULL, ((PetscObject) dm)->prefix, "-dm_ds_jet_degree", &k, &flg);CHKERRQ(ierr);
5712f9244615SMatthew G. Knepley   if (flg) {
57133b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
57143b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
57153b4aee56SMatthew G. Knepley       PetscInt Nf, f;
57163b4aee56SMatthew G. Knepley 
57173b4aee56SMatthew G. Knepley       ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
57183b4aee56SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {ierr = PetscDSSetJetDegree(ds, f, k);CHKERRQ(ierr);}
57193b4aee56SMatthew G. Knepley     }
5720f9244615SMatthew G. Knepley   }
5721e5e52638SMatthew G. Knepley   /* Setup DSes */
5722e5e52638SMatthew G. Knepley   if (doSetup) {
5723e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5724e5e52638SMatthew G. Knepley   }
5725e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5726e5e52638SMatthew G. Knepley }
5727e5e52638SMatthew G. Knepley 
5728e5e52638SMatthew G. Knepley /*@
57297f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
57307f96f943SMatthew G. Knepley 
5731f2cacb80SMatthew G. Knepley   Collective on DM
5732f2cacb80SMatthew G. Knepley 
57337f96f943SMatthew G. Knepley   Input Parameters:
57347f96f943SMatthew G. Knepley + dm   - The DM
57357f96f943SMatthew G. Knepley - time - The time
57367f96f943SMatthew G. Knepley 
57377f96f943SMatthew G. Knepley   Output Parameters:
5738f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5739f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
57407f96f943SMatthew G. Knepley 
57417f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
57427f96f943SMatthew G. Knepley 
57437f96f943SMatthew G. Knepley   Level: developer
57447f96f943SMatthew G. Knepley 
57457f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
57467f96f943SMatthew G. Knepley @*/
5747f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
57487f96f943SMatthew G. Knepley {
57497f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
57507f96f943SMatthew G. Knepley   void            **ectxs;
57517f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
57527f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
57537f96f943SMatthew G. Knepley 
57547f96f943SMatthew G. Knepley   PetscFunctionBegin;
5755f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5756f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5757f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
57587f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
57597f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
57607f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
57617f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
57627f96f943SMatthew G. Knepley     PetscDS         ds;
57637f96f943SMatthew G. Knepley     DMLabel         label;
57647f96f943SMatthew G. Knepley     IS              fieldIS;
57657f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
57667f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
57677f96f943SMatthew G. Knepley 
57687f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
57697f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
57707f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
57717f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
57727f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5773f2cacb80SMatthew G. Knepley     if (u) {
57747f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
57757f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
57767f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
57777f96f943SMatthew G. Knepley       }
57787f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
57797f96f943SMatthew G. Knepley       if (label) {
57807f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
57817f96f943SMatthew G. Knepley       } else {
57827f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
57837f96f943SMatthew G. Knepley       }
57847f96f943SMatthew G. Knepley     }
5785f2cacb80SMatthew G. Knepley     if (u_t) {
5786f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5787f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5788f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5789f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5790f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5791f2cacb80SMatthew G. Knepley       }
5792f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5793f2cacb80SMatthew G. Knepley       if (label) {
5794f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5795f2cacb80SMatthew G. Knepley       } else {
5796f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5797f2cacb80SMatthew G. Knepley       }
5798f2cacb80SMatthew G. Knepley     }
5799f2cacb80SMatthew G. Knepley   }
5800f2cacb80SMatthew G. Knepley   if (u) {
58017f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
58027f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5803f2cacb80SMatthew G. Knepley   }
5804f2cacb80SMatthew G. Knepley   if (u_t) {
5805f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
5806f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
5807f2cacb80SMatthew G. Knepley   }
58087f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
58097f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
58107f96f943SMatthew G. Knepley }
58117f96f943SMatthew G. Knepley 
581245480ffeSMatthew G. Knepley PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
581345480ffeSMatthew G. Knepley {
581445480ffeSMatthew G. Knepley   PetscDS        dsNew;
581545480ffeSMatthew G. Knepley   DSBoundary     b;
581645480ffeSMatthew G. Knepley   PetscInt       cdim, Nf, f;
5817df911668SMatthew G. Knepley   PetscBool      isHybrid;
581845480ffeSMatthew G. Knepley   void          *ctx;
581945480ffeSMatthew G. Knepley   PetscErrorCode ierr;
582045480ffeSMatthew G. Knepley 
582145480ffeSMatthew G. Knepley   PetscFunctionBegin;
582245480ffeSMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) ds), &dsNew);CHKERRQ(ierr);
582345480ffeSMatthew G. Knepley   ierr = PetscDSCopyConstants(ds, dsNew);CHKERRQ(ierr);
582445480ffeSMatthew G. Knepley   ierr = PetscDSCopyExactSolutions(ds, dsNew);CHKERRQ(ierr);
582545480ffeSMatthew G. Knepley   ierr = PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
582645480ffeSMatthew G. Knepley   ierr = PetscDSCopyEquations(ds, dsNew);CHKERRQ(ierr);
582745480ffeSMatthew G. Knepley   ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
582845480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
582945480ffeSMatthew G. Knepley     ierr = PetscDSGetContext(ds, f, &ctx);CHKERRQ(ierr);
583045480ffeSMatthew G. Knepley     ierr = PetscDSSetContext(dsNew, f, ctx);CHKERRQ(ierr);
583145480ffeSMatthew G. Knepley   }
583245480ffeSMatthew G. Knepley   if (Nf) {
583345480ffeSMatthew G. Knepley     ierr = PetscDSGetCoordinateDimension(ds, &cdim);CHKERRQ(ierr);
583445480ffeSMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsNew, cdim);CHKERRQ(ierr);
583545480ffeSMatthew G. Knepley   }
583645480ffeSMatthew G. Knepley   ierr = PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
583745480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
583845480ffeSMatthew G. Knepley     ierr = DMGetLabel(dm, b->lname, &b->label);CHKERRQ(ierr);
583945480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
584045480ffeSMatthew G. Knepley     //if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
584145480ffeSMatthew G. Knepley   }
5842df911668SMatthew G. Knepley   ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5843df911668SMatthew G. Knepley   ierr = PetscDSSetHybrid(dsNew, isHybrid);CHKERRQ(ierr);
584445480ffeSMatthew G. Knepley 
584545480ffeSMatthew G. Knepley   ierr = DMSetRegionDS(dm, label, fields, dsNew);CHKERRQ(ierr);
584645480ffeSMatthew G. Knepley   ierr = PetscDSDestroy(&dsNew);CHKERRQ(ierr);
584745480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
584845480ffeSMatthew G. Knepley }
584945480ffeSMatthew G. Knepley 
58507f96f943SMatthew G. Knepley /*@
5851e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5852e5e52638SMatthew G. Knepley 
5853d083f849SBarry Smith   Collective on dm
5854e5e52638SMatthew G. Knepley 
5855e5e52638SMatthew G. Knepley   Input Parameter:
5856e5e52638SMatthew G. Knepley . dm - The DM
5857e5e52638SMatthew G. Knepley 
5858e5e52638SMatthew G. Knepley   Output Parameter:
5859e5e52638SMatthew G. Knepley . newdm - The DM
5860e5e52638SMatthew G. Knepley 
5861e5e52638SMatthew G. Knepley   Level: advanced
5862e5e52638SMatthew G. Knepley 
5863e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5864e5e52638SMatthew G. Knepley @*/
5865e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5866e5e52638SMatthew G. Knepley {
5867e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5868e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5869e5e52638SMatthew G. Knepley 
5870e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5871e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5872e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5873e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5874e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5875e5e52638SMatthew G. Knepley     DMLabel  label;
5876b3cf3223SMatthew G. Knepley     IS       fields;
587745480ffeSMatthew G. Knepley     PetscDS  ds, newds;
5878783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5879e5e52638SMatthew G. Knepley 
5880b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5881b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
588245480ffeSMatthew G. Knepley     ierr = DMTransferDS_Internal(newdm, label, fields, ds);CHKERRQ(ierr);
588345480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
588445480ffeSMatthew G. Knepley     ierr = DMGetRegionDS(newdm, label, NULL, &newds);CHKERRQ(ierr);
588545480ffeSMatthew G. Knepley     ierr = PetscDSGetNumBoundary(newds, &Nbd);CHKERRQ(ierr);
5886783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5887b8025e53SMatthew G. Knepley       PetscWeakForm wf;
588845480ffeSMatthew G. Knepley       DMLabel       label;
5889783e2ec8SMatthew G. Knepley       PetscInt      field;
5890783e2ec8SMatthew G. Knepley 
5891b8025e53SMatthew G. Knepley       ierr = PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
589245480ffeSMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, newds, field, bd, label);CHKERRQ(ierr);
5893b8025e53SMatthew G. Knepley       ierr = PetscWeakFormReplaceLabel(wf, label);CHKERRQ(ierr);
5894783e2ec8SMatthew G. Knepley     }
5895e5e52638SMatthew G. Knepley   }
5896e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5897e5e52638SMatthew G. Knepley }
5898e5e52638SMatthew G. Knepley 
5899e5e52638SMatthew G. Knepley /*@
5900e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5901e5e52638SMatthew G. Knepley 
5902d083f849SBarry Smith   Collective on dm
5903e5e52638SMatthew G. Knepley 
5904e5e52638SMatthew G. Knepley   Input Parameter:
5905e5e52638SMatthew G. Knepley . dm - The DM
5906e5e52638SMatthew G. Knepley 
5907e5e52638SMatthew G. Knepley   Output Parameter:
5908e5e52638SMatthew G. Knepley . newdm - The DM
5909e5e52638SMatthew G. Knepley 
5910e5e52638SMatthew G. Knepley   Level: advanced
5911e5e52638SMatthew G. Knepley 
5912e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5913e5e52638SMatthew G. Knepley @*/
5914e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5915e5e52638SMatthew G. Knepley {
5916e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5917e5e52638SMatthew G. Knepley 
5918e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5919e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5920e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5921e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5922e5e52638SMatthew G. Knepley }
5923e5e52638SMatthew G. Knepley 
5924b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5925b64e0483SPeter Brune {
5926b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5927b64e0483SPeter Brune   PetscErrorCode ierr;
5928b64e0483SPeter Brune   Vec coords,ccoords;
59296dbf9973SLawrence Mitchell   Mat inject;
5930b64e0483SPeter Brune   PetscFunctionBegin;
5931b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5932b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5933b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5934b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5935b64e0483SPeter Brune   if (coords && !ccoords) {
5936b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
59376668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
59386dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
59392adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
59406dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5941b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5942b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5943b64e0483SPeter Brune   }
5944b64e0483SPeter Brune   PetscFunctionReturn(0);
5945b64e0483SPeter Brune }
5946b64e0483SPeter Brune 
594703dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
594803dadc2fSPeter Brune {
594903dadc2fSPeter Brune   DM dm_coord,subdm_coord;
595003dadc2fSPeter Brune   PetscErrorCode ierr;
595103dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
595203dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
595303dadc2fSPeter Brune   PetscFunctionBegin;
595403dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
595503dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
595603dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
595703dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
595803dadc2fSPeter Brune   if (coords && !ccoords) {
595903dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
59606668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
596103dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
596224640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
596303dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
596403dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
596503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
59661ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
596703dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
596803dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
596903dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
597003dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
597103dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
597203dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
597303dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
597403dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
597503dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
597603dadc2fSPeter Brune   }
597703dadc2fSPeter Brune   PetscFunctionReturn(0);
597803dadc2fSPeter Brune }
597903dadc2fSPeter Brune 
5980c73cfb54SMatthew G. Knepley /*@
5981c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5982c73cfb54SMatthew G. Knepley 
5983c73cfb54SMatthew G. Knepley   Not collective
5984c73cfb54SMatthew G. Knepley 
5985c73cfb54SMatthew G. Knepley   Input Parameter:
5986c73cfb54SMatthew G. Knepley . dm - The DM
5987c73cfb54SMatthew G. Knepley 
5988c73cfb54SMatthew G. Knepley   Output Parameter:
5989c73cfb54SMatthew G. Knepley . dim - The topological dimension
5990c73cfb54SMatthew G. Knepley 
5991c73cfb54SMatthew G. Knepley   Level: beginner
5992c73cfb54SMatthew G. Knepley 
5993c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5994c73cfb54SMatthew G. Knepley @*/
5995c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5996c73cfb54SMatthew G. Knepley {
5997c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5998c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5999534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6000c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6001c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6002c73cfb54SMatthew G. Knepley }
6003c73cfb54SMatthew G. Knepley 
6004c73cfb54SMatthew G. Knepley /*@
6005c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
6006c73cfb54SMatthew G. Knepley 
6007c73cfb54SMatthew G. Knepley   Collective on dm
6008c73cfb54SMatthew G. Knepley 
6009c73cfb54SMatthew G. Knepley   Input Parameters:
6010c73cfb54SMatthew G. Knepley + dm - The DM
6011c73cfb54SMatthew G. Knepley - dim - The topological dimension
6012c73cfb54SMatthew G. Knepley 
6013c73cfb54SMatthew G. Knepley   Level: beginner
6014c73cfb54SMatthew G. Knepley 
6015c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
6016c73cfb54SMatthew G. Knepley @*/
6017c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6018c73cfb54SMatthew G. Knepley {
6019e5e52638SMatthew G. Knepley   PetscDS        ds;
602045480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6021f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6022f17e8794SMatthew G. Knepley 
6023c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6024c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6025c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6026c73cfb54SMatthew G. Knepley   dm->dim = dim;
602745480ffeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
602845480ffeSMatthew G. Knepley   for (n = 0; n < Nds; ++n) {
602945480ffeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
603045480ffeSMatthew G. Knepley     if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);}
603145480ffeSMatthew G. Knepley   }
6032c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6033c73cfb54SMatthew G. Knepley }
6034c73cfb54SMatthew G. Knepley 
6035793f3fe5SMatthew G. Knepley /*@
6036793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6037793f3fe5SMatthew G. Knepley 
6038d083f849SBarry Smith   Collective on dm
6039793f3fe5SMatthew G. Knepley 
6040793f3fe5SMatthew G. Knepley   Input Parameters:
6041793f3fe5SMatthew G. Knepley + dm - the DM
6042793f3fe5SMatthew G. Knepley - dim - the dimension
6043793f3fe5SMatthew G. Knepley 
6044793f3fe5SMatthew G. Knepley   Output Parameters:
6045793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6046aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6047793f3fe5SMatthew G. Knepley 
6048793f3fe5SMatthew G. Knepley   Note:
6049793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6050a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6051793f3fe5SMatthew G. Knepley   then the interval is empty.
6052793f3fe5SMatthew G. Knepley 
6053793f3fe5SMatthew G. Knepley   Level: intermediate
6054793f3fe5SMatthew G. Knepley 
6055793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
6056793f3fe5SMatthew G. Knepley @*/
6057793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6058793f3fe5SMatthew G. Knepley {
6059793f3fe5SMatthew G. Knepley   PetscInt       d;
6060793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
6061793f3fe5SMatthew G. Knepley 
6062793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6063793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6064793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
6065793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
6066b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
6067793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
6068793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6069793f3fe5SMatthew G. Knepley }
6070793f3fe5SMatthew G. Knepley 
60716636e97aSMatthew G Knepley /*@
60726636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
60736636e97aSMatthew G Knepley 
6074d083f849SBarry Smith   Collective on dm
60756636e97aSMatthew G Knepley 
60766636e97aSMatthew G Knepley   Input Parameters:
60776636e97aSMatthew G Knepley + dm - the DM
60786636e97aSMatthew G Knepley - c - coordinate vector
60796636e97aSMatthew G Knepley 
60802dd40e9bSPatrick Sanan   Notes:
60812dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
60822dd40e9bSPatrick Sanan 
60832dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
60846636e97aSMatthew G Knepley 
60856636e97aSMatthew G Knepley   Level: intermediate
60866636e97aSMatthew G Knepley 
608760c22052SBarry Smith .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
60886636e97aSMatthew G Knepley @*/
60896636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
60906636e97aSMatthew G Knepley {
60916636e97aSMatthew G Knepley   PetscErrorCode ierr;
60926636e97aSMatthew G Knepley 
60936636e97aSMatthew G Knepley   PetscFunctionBegin;
60946636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60956636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
60966636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
60976636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
60986636e97aSMatthew G Knepley   dm->coordinates = c;
60996636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
6100b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
610103dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
61026636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61036636e97aSMatthew G Knepley }
61046636e97aSMatthew G Knepley 
61056636e97aSMatthew G Knepley /*@
61066636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
61076636e97aSMatthew G Knepley 
61087058e716SVaclav Hapla   Not collective
61096636e97aSMatthew G Knepley 
61106636e97aSMatthew G Knepley    Input Parameters:
61116636e97aSMatthew G Knepley +  dm - the DM
61126636e97aSMatthew G Knepley -  c - coordinate vector
61136636e97aSMatthew G Knepley 
61142dd40e9bSPatrick Sanan   Notes:
61156636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
61166636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
61176636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
61186636e97aSMatthew G Knepley 
61192dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
61202dd40e9bSPatrick Sanan 
61216636e97aSMatthew G Knepley   Level: intermediate
61226636e97aSMatthew G Knepley 
61236636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
61246636e97aSMatthew G Knepley @*/
61256636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
61266636e97aSMatthew G Knepley {
61276636e97aSMatthew G Knepley   PetscErrorCode ierr;
61286636e97aSMatthew G Knepley 
61296636e97aSMatthew G Knepley   PetscFunctionBegin;
61306636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61316636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
61326636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
61336636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
61348865f1eaSKarl Rupp 
61356636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
61368865f1eaSKarl Rupp 
61376636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
61386636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61396636e97aSMatthew G Knepley }
61406636e97aSMatthew G Knepley 
61416636e97aSMatthew G Knepley /*@
61426636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
61436636e97aSMatthew G Knepley 
6144d083f849SBarry Smith   Collective on dm
61456636e97aSMatthew G Knepley 
61466636e97aSMatthew G Knepley   Input Parameter:
61476636e97aSMatthew G Knepley . dm - the DM
61486636e97aSMatthew G Knepley 
61496636e97aSMatthew G Knepley   Output Parameter:
61506636e97aSMatthew G Knepley . c - global coordinate vector
61516636e97aSMatthew G Knepley 
61526636e97aSMatthew G Knepley   Note:
615360c22052SBarry Smith   This is a borrowed reference, so the user should NOT destroy this vector. When the DM is
615460c22052SBarry Smith   destroyed the array will no longer be valid.
61556636e97aSMatthew G Knepley 
6156959c7569SPatrick Sanan   Each process has only the locally-owned portion of the global coordinates (does NOT have the ghost coordinates).
61576636e97aSMatthew G Knepley 
61586636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
61596636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
61606636e97aSMatthew G Knepley 
61616636e97aSMatthew G Knepley   Level: intermediate
61626636e97aSMatthew G Knepley 
616360c22052SBarry Smith .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
61646636e97aSMatthew G Knepley @*/
61656636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
61666636e97aSMatthew G Knepley {
61676636e97aSMatthew G Knepley   PetscErrorCode ierr;
61686636e97aSMatthew G Knepley 
61696636e97aSMatthew G Knepley   PetscFunctionBegin;
61706636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61716636e97aSMatthew G Knepley   PetscValidPointer(c,2);
61721f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
61730298fd71SBarry Smith     DM        cdm = NULL;
61741970a576SMatthew G. Knepley     PetscBool localized;
61756636e97aSMatthew G Knepley 
61766636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
61776636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
61781970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
61791970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
61801970a576SMatthew G. Knepley     if (localized) {
61811970a576SMatthew G. Knepley       PetscInt cdim;
61821970a576SMatthew G. Knepley 
61831970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
61841970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
61851970a576SMatthew G. Knepley     }
61866636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
61876636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
61886636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
61896636e97aSMatthew G Knepley   }
61906636e97aSMatthew G Knepley   *c = dm->coordinates;
61916636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61926636e97aSMatthew G Knepley }
61936636e97aSMatthew G Knepley 
61946636e97aSMatthew G Knepley /*@
619581e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
619681e9a530SVaclav Hapla 
6197d083f849SBarry Smith   Collective on dm
619881e9a530SVaclav Hapla 
619981e9a530SVaclav Hapla   Input Parameter:
620081e9a530SVaclav Hapla . dm - the DM
620181e9a530SVaclav Hapla 
620281e9a530SVaclav Hapla   Level: advanced
620381e9a530SVaclav Hapla 
620481e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
620581e9a530SVaclav Hapla @*/
620681e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
620781e9a530SVaclav Hapla {
620881e9a530SVaclav Hapla   PetscErrorCode ierr;
620981e9a530SVaclav Hapla 
621081e9a530SVaclav Hapla   PetscFunctionBegin;
621181e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
621281e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
62131970a576SMatthew G. Knepley     DM        cdm = NULL;
62141970a576SMatthew G. Knepley     PetscBool localized;
62151970a576SMatthew G. Knepley 
621681e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
621781e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
62181970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
62191970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
62201970a576SMatthew G. Knepley     if (localized) {
62211970a576SMatthew G. Knepley       PetscInt cdim;
62221970a576SMatthew G. Knepley 
62231970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
62241970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
62251970a576SMatthew G. Knepley     }
622681e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
622781e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
622881e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
622981e9a530SVaclav Hapla   }
623081e9a530SVaclav Hapla   PetscFunctionReturn(0);
623181e9a530SVaclav Hapla }
623281e9a530SVaclav Hapla 
623381e9a530SVaclav Hapla /*@
62346636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
62356636e97aSMatthew G Knepley 
6236d083f849SBarry Smith   Collective on dm
62376636e97aSMatthew G Knepley 
62386636e97aSMatthew G Knepley   Input Parameter:
62396636e97aSMatthew G Knepley . dm - the DM
62406636e97aSMatthew G Knepley 
62416636e97aSMatthew G Knepley   Output Parameter:
62426636e97aSMatthew G Knepley . c - coordinate vector
62436636e97aSMatthew G Knepley 
62446636e97aSMatthew G Knepley   Note:
62456636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
62466636e97aSMatthew G Knepley 
62476636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
62486636e97aSMatthew G Knepley 
62496636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
62506636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
62516636e97aSMatthew G Knepley 
62526636e97aSMatthew G Knepley   Level: intermediate
62536636e97aSMatthew G Knepley 
625481e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
62556636e97aSMatthew G Knepley @*/
62566636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
62576636e97aSMatthew G Knepley {
62586636e97aSMatthew G Knepley   PetscErrorCode ierr;
62596636e97aSMatthew G Knepley 
62606636e97aSMatthew G Knepley   PetscFunctionBegin;
62616636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62626636e97aSMatthew G Knepley   PetscValidPointer(c,2);
626381e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
62646636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
62656636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62666636e97aSMatthew G Knepley }
62676636e97aSMatthew G Knepley 
626881e9a530SVaclav Hapla /*@
626981e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
627081e9a530SVaclav Hapla 
627181e9a530SVaclav Hapla   Not collective
627281e9a530SVaclav Hapla 
627381e9a530SVaclav Hapla   Input Parameter:
627481e9a530SVaclav Hapla . dm - the DM
627581e9a530SVaclav Hapla 
627681e9a530SVaclav Hapla   Output Parameter:
627781e9a530SVaclav Hapla . c - coordinate vector
627881e9a530SVaclav Hapla 
627981e9a530SVaclav Hapla   Level: advanced
628081e9a530SVaclav Hapla 
628181e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
628281e9a530SVaclav Hapla @*/
628381e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
628481e9a530SVaclav Hapla {
628581e9a530SVaclav Hapla   PetscFunctionBegin;
628681e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
628781e9a530SVaclav Hapla   PetscValidPointer(c,2);
628881e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
62896636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
62906636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62916636e97aSMatthew G Knepley }
62926636e97aSMatthew G Knepley 
62932db98f8dSVaclav Hapla /*@
62942db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
62952db98f8dSVaclav Hapla 
62962db98f8dSVaclav Hapla   Not collective
62972db98f8dSVaclav Hapla 
6298d8d19677SJose E. Roman   Input Parameters:
62992db98f8dSVaclav Hapla + dm - the DM
63002db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
63012db98f8dSVaclav Hapla 
6302d8d19677SJose E. Roman   Output Parameters:
63032db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
63042db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
63052db98f8dSVaclav Hapla 
63062db98f8dSVaclav Hapla   Note:
63072db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
63082db98f8dSVaclav Hapla 
63092db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
63102db98f8dSVaclav Hapla 
63112db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
63122db98f8dSVaclav Hapla 
63132db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
63142db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
63152db98f8dSVaclav Hapla 
63162db98f8dSVaclav Hapla   Level: advanced
63172db98f8dSVaclav Hapla 
63182db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
63192db98f8dSVaclav Hapla @*/
63202db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
63212db98f8dSVaclav Hapla {
63222db98f8dSVaclav Hapla   PetscSection        cs, newcs;
63232db98f8dSVaclav Hapla   Vec                 coords;
63242db98f8dSVaclav Hapla   const PetscScalar   *arr;
63252db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
63262db98f8dSVaclav Hapla   PetscInt            n;
63272db98f8dSVaclav Hapla   PetscErrorCode      ierr;
63282db98f8dSVaclav Hapla 
63292db98f8dSVaclav Hapla   PetscFunctionBegin;
63302db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63312db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
63322db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
63332db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
63342db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
63351bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
63361bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
63372db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
63382db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
63392db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
63402db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
63412db98f8dSVaclav Hapla   if (pCoord) {
63422db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
63432db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
63442db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
63452db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6346ad9ac99dSVaclav Hapla   } else {
6347ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
63482db98f8dSVaclav Hapla   }
6349ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6350ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
63512db98f8dSVaclav Hapla   PetscFunctionReturn(0);
63522db98f8dSVaclav Hapla }
63532db98f8dSVaclav Hapla 
6354f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6355f19dbd58SToby Isaac {
6356f19dbd58SToby Isaac   PetscErrorCode ierr;
6357f19dbd58SToby Isaac 
6358f19dbd58SToby Isaac   PetscFunctionBegin;
6359f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6360f19dbd58SToby Isaac   PetscValidPointer(field,2);
6361f19dbd58SToby Isaac   if (!dm->coordinateField) {
6362f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6363f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6364f19dbd58SToby Isaac     }
6365f19dbd58SToby Isaac   }
6366f19dbd58SToby Isaac   *field = dm->coordinateField;
6367f19dbd58SToby Isaac   PetscFunctionReturn(0);
6368f19dbd58SToby Isaac }
6369f19dbd58SToby Isaac 
6370f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6371f19dbd58SToby Isaac {
6372f19dbd58SToby Isaac   PetscErrorCode ierr;
6373f19dbd58SToby Isaac 
6374f19dbd58SToby Isaac   PetscFunctionBegin;
6375f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6376f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6377c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6378f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6379f19dbd58SToby Isaac   dm->coordinateField = field;
6380f19dbd58SToby Isaac   PetscFunctionReturn(0);
6381f19dbd58SToby Isaac }
6382f19dbd58SToby Isaac 
63836636e97aSMatthew G Knepley /*@
63841cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
63856636e97aSMatthew G Knepley 
6386d083f849SBarry Smith   Collective on dm
63876636e97aSMatthew G Knepley 
63886636e97aSMatthew G Knepley   Input Parameter:
63896636e97aSMatthew G Knepley . dm - the DM
63906636e97aSMatthew G Knepley 
63916636e97aSMatthew G Knepley   Output Parameter:
63926636e97aSMatthew G Knepley . cdm - coordinate DM
63936636e97aSMatthew G Knepley 
63946636e97aSMatthew G Knepley   Level: intermediate
63956636e97aSMatthew G Knepley 
63961cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
63976636e97aSMatthew G Knepley @*/
63986636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
63996636e97aSMatthew G Knepley {
64006636e97aSMatthew G Knepley   PetscErrorCode ierr;
64016636e97aSMatthew G Knepley 
64026636e97aSMatthew G Knepley   PetscFunctionBegin;
64036636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64046636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
64056636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6406308f8a94SToby Isaac     DM cdm;
6407308f8a94SToby Isaac 
640882f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6409308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6410308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6411308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6412308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6413308f8a94SToby Isaac     dm->coordinateDM = cdm;
64146636e97aSMatthew G Knepley   }
64156636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
64166636e97aSMatthew G Knepley   PetscFunctionReturn(0);
64176636e97aSMatthew G Knepley }
6418e87bb0d3SMatthew G Knepley 
64191cfe2091SMatthew G. Knepley /*@
64201cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
64211cfe2091SMatthew G. Knepley 
6422d083f849SBarry Smith   Logically Collective on dm
64231cfe2091SMatthew G. Knepley 
64241cfe2091SMatthew G. Knepley   Input Parameters:
64251cfe2091SMatthew G. Knepley + dm - the DM
64261cfe2091SMatthew G. Knepley - cdm - coordinate DM
64271cfe2091SMatthew G. Knepley 
64281cfe2091SMatthew G. Knepley   Level: intermediate
64291cfe2091SMatthew G. Knepley 
64301cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
64311cfe2091SMatthew G. Knepley @*/
64321cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
64331cfe2091SMatthew G. Knepley {
64341cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
64351cfe2091SMatthew G. Knepley 
64361cfe2091SMatthew G. Knepley   PetscFunctionBegin;
64371cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64381cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6439f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
64401cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
64411cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
64421cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
64431cfe2091SMatthew G. Knepley }
64441cfe2091SMatthew G. Knepley 
644546e270d4SMatthew G. Knepley /*@
644646e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
644746e270d4SMatthew G. Knepley 
644846e270d4SMatthew G. Knepley   Not Collective
644946e270d4SMatthew G. Knepley 
645046e270d4SMatthew G. Knepley   Input Parameter:
645146e270d4SMatthew G. Knepley . dm - The DM object
645246e270d4SMatthew G. Knepley 
645346e270d4SMatthew G. Knepley   Output Parameter:
645446e270d4SMatthew G. Knepley . dim - The embedding dimension
645546e270d4SMatthew G. Knepley 
645646e270d4SMatthew G. Knepley   Level: intermediate
645746e270d4SMatthew G. Knepley 
645892fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
645946e270d4SMatthew G. Knepley @*/
646046e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
646146e270d4SMatthew G. Knepley {
646246e270d4SMatthew G. Knepley   PetscFunctionBegin;
646346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6464534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
64659a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
64669a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
64679a9a41abSToby Isaac   }
646846e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
646946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
647046e270d4SMatthew G. Knepley }
647146e270d4SMatthew G. Knepley 
647246e270d4SMatthew G. Knepley /*@
647346e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
647446e270d4SMatthew G. Knepley 
647546e270d4SMatthew G. Knepley   Not Collective
647646e270d4SMatthew G. Knepley 
647746e270d4SMatthew G. Knepley   Input Parameters:
647846e270d4SMatthew G. Knepley + dm  - The DM object
647946e270d4SMatthew G. Knepley - dim - The embedding dimension
648046e270d4SMatthew G. Knepley 
648146e270d4SMatthew G. Knepley   Level: intermediate
648246e270d4SMatthew G. Knepley 
648392fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
648446e270d4SMatthew G. Knepley @*/
648546e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
648646e270d4SMatthew G. Knepley {
6487e5e52638SMatthew G. Knepley   PetscDS        ds;
648845480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6489f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6490f17e8794SMatthew G. Knepley 
649146e270d4SMatthew G. Knepley   PetscFunctionBegin;
649246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
649346e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
649445480ffeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
649545480ffeSMatthew G. Knepley   for (n = 0; n < Nds; ++n) {
649645480ffeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
6497e5e52638SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
649845480ffeSMatthew G. Knepley   }
649946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
650046e270d4SMatthew G. Knepley }
650146e270d4SMatthew G. Knepley 
6502e8abe2deSMatthew G. Knepley /*@
6503e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6504e8abe2deSMatthew G. Knepley 
6505d083f849SBarry Smith   Collective on dm
6506e8abe2deSMatthew G. Knepley 
6507e8abe2deSMatthew G. Knepley   Input Parameter:
6508e8abe2deSMatthew G. Knepley . dm - The DM object
6509e8abe2deSMatthew G. Knepley 
6510e8abe2deSMatthew G. Knepley   Output Parameter:
6511e8abe2deSMatthew G. Knepley . section - The PetscSection object
6512e8abe2deSMatthew G. Knepley 
6513e8abe2deSMatthew G. Knepley   Level: intermediate
6514e8abe2deSMatthew G. Knepley 
651592fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6516e8abe2deSMatthew G. Knepley @*/
6517e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6518e8abe2deSMatthew G. Knepley {
6519e8abe2deSMatthew G. Knepley   DM             cdm;
6520e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6521e8abe2deSMatthew G. Knepley 
6522e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6523e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6524e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6525e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
652692fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6527e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6528e8abe2deSMatthew G. Knepley }
6529e8abe2deSMatthew G. Knepley 
6530e8abe2deSMatthew G. Knepley /*@
6531e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6532e8abe2deSMatthew G. Knepley 
6533e8abe2deSMatthew G. Knepley   Not Collective
6534e8abe2deSMatthew G. Knepley 
6535e8abe2deSMatthew G. Knepley   Input Parameters:
6536e8abe2deSMatthew G. Knepley + dm      - The DM object
653746e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6538e8abe2deSMatthew G. Knepley - section - The PetscSection object
6539e8abe2deSMatthew G. Knepley 
6540e8abe2deSMatthew G. Knepley   Level: intermediate
6541e8abe2deSMatthew G. Knepley 
654292fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6543e8abe2deSMatthew G. Knepley @*/
654446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6545e8abe2deSMatthew G. Knepley {
6546e8abe2deSMatthew G. Knepley   DM             cdm;
6547e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6548e8abe2deSMatthew G. Knepley 
6549e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6550e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
655146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6552e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
655392fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
655446e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
65554c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
655646e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
655746e270d4SMatthew G. Knepley 
655846e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
655946e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
656046e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
656146e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
656246e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
656346e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
656446e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
656546e270d4SMatthew G. Knepley     }
6566ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
656746e270d4SMatthew G. Knepley   }
6568e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6569e8abe2deSMatthew G. Knepley }
6570e8abe2deSMatthew G. Knepley 
6571d864a3eaSLisandro Dalcin /*@
6572d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6573d864a3eaSLisandro Dalcin 
6574d864a3eaSLisandro Dalcin   Input Parameters:
6575d864a3eaSLisandro Dalcin + dm      - The DM object
6576d864a3eaSLisandro Dalcin - disc    - The new coordinate discretization
6577d864a3eaSLisandro Dalcin 
6578d864a3eaSLisandro Dalcin   Level: intermediate
6579d864a3eaSLisandro Dalcin 
6580d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6581d864a3eaSLisandro Dalcin @*/
6582d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6583d864a3eaSLisandro Dalcin {
6584d864a3eaSLisandro Dalcin   PetscObject    discOld;
6585d864a3eaSLisandro Dalcin   PetscClassId   classid;
6586d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6587d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6588d864a3eaSLisandro Dalcin   Mat            matInterp;
6589d864a3eaSLisandro Dalcin   PetscErrorCode ierr;
6590d864a3eaSLisandro Dalcin 
6591d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6592d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6593d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6594d864a3eaSLisandro Dalcin 
6595d864a3eaSLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdmOld);CHKERRQ(ierr);
6596d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
6597d864a3eaSLisandro Dalcin   ierr = DMGetField(cdmOld, 0, NULL, &discOld);CHKERRQ(ierr);
6598d864a3eaSLisandro Dalcin   ierr = PetscObjectGetClassId(discOld, &classid);CHKERRQ(ierr);
659929ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
660029ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
660129ad44c5SMatthew G. Knepley       PetscFE        feLinear;
660229ad44c5SMatthew G. Knepley       DMPolytopeType ct;
660329ad44c5SMatthew G. Knepley       PetscInt       dim, dE, cStart;
660429ad44c5SMatthew G. Knepley       PetscBool      simplex;
660529ad44c5SMatthew G. Knepley 
660629ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
660729ad44c5SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
660829ad44c5SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
660929ad44c5SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdmOld, 0, &cStart, NULL);CHKERRQ(ierr);
661029ad44c5SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
661129ad44c5SMatthew G. Knepley       switch (ct) {
661229ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM:
661329ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
661429ad44c5SMatthew G. Knepley           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
661529ad44c5SMatthew G. Knepley         default: break;
661629ad44c5SMatthew G. Knepley       }
661729ad44c5SMatthew G. Knepley       simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
661829ad44c5SMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear);CHKERRQ(ierr);
661929ad44c5SMatthew G. Knepley       ierr = DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear);CHKERRQ(ierr);
662029ad44c5SMatthew G. Knepley       ierr = PetscFEDestroy(&feLinear);CHKERRQ(ierr);
662129ad44c5SMatthew G. Knepley       ierr = DMCreateDS(cdmOld);CHKERRQ(ierr);
662229ad44c5SMatthew G. Knepley     } else {
662329ad44c5SMatthew G. Knepley       const char *discname;
662429ad44c5SMatthew G. Knepley 
662529ad44c5SMatthew G. Knepley       ierr = PetscObjectGetType(discOld, &discname);CHKERRQ(ierr);
662629ad44c5SMatthew G. Knepley       SETERRQ1(PetscObjectComm(discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
662729ad44c5SMatthew G. Knepley     }
662829ad44c5SMatthew G. Knepley   }
6629d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6630d864a3eaSLisandro Dalcin   ierr = DMClone(cdmOld, &cdmNew);CHKERRQ(ierr);
6631d864a3eaSLisandro Dalcin   ierr = DMSetField(cdmNew, 0, NULL, (PetscObject) disc);CHKERRQ(ierr);
6632d864a3eaSLisandro Dalcin   ierr = DMCreateDS(cdmNew);CHKERRQ(ierr);
6633d864a3eaSLisandro Dalcin   /* Project the coordinate vector from old to new space  */
6634d864a3eaSLisandro Dalcin   ierr = DMGetCoordinates(dm, &coordsOld);CHKERRQ(ierr);
6635d864a3eaSLisandro Dalcin   ierr = DMCreateGlobalVector(cdmNew, &coordsNew);CHKERRQ(ierr);
6636d864a3eaSLisandro Dalcin   ierr = DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL);CHKERRQ(ierr);
6637d864a3eaSLisandro Dalcin   ierr = MatInterpolate(matInterp, coordsOld, coordsNew);CHKERRQ(ierr);
6638d864a3eaSLisandro Dalcin   ierr = MatDestroy(&matInterp);CHKERRQ(ierr);
6639d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6640d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateField(dm, NULL);CHKERRQ(ierr);
6641d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateDM(dm, cdmNew);CHKERRQ(ierr);
6642d864a3eaSLisandro Dalcin   ierr = DMSetCoordinates(dm, coordsNew);CHKERRQ(ierr);
6643d864a3eaSLisandro Dalcin   ierr = VecDestroy(&coordsNew);CHKERRQ(ierr);
6644d864a3eaSLisandro Dalcin   ierr = DMDestroy(&cdmNew);CHKERRQ(ierr);
6645d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6646d864a3eaSLisandro Dalcin }
6647d864a3eaSLisandro Dalcin 
66485dc8c3f7SMatthew G. Knepley /*@C
664990b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
66505dc8c3f7SMatthew G. Knepley 
6651f899ff85SJose E. Roman   Input Parameter:
665290b157c4SStefano Zampini . dm      - The DM object
665390b157c4SStefano Zampini 
665490b157c4SStefano Zampini   Output Parameters:
665590b157c4SStefano Zampini + per     - Whether the DM is periodic or not
66565dc8c3f7SMatthew 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
66575dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
66585dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
66595dc8c3f7SMatthew G. Knepley 
66605dc8c3f7SMatthew G. Knepley   Level: developer
66615dc8c3f7SMatthew G. Knepley 
66625dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
66635dc8c3f7SMatthew G. Knepley @*/
666490b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6665c6b900c6SMatthew G. Knepley {
6666c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6667c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
666890b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6669c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6670c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
66715dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6672c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6673c6b900c6SMatthew G. Knepley }
6674c6b900c6SMatthew G. Knepley 
66755dc8c3f7SMatthew G. Knepley /*@C
66765dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
66775dc8c3f7SMatthew G. Knepley 
66785dc8c3f7SMatthew G. Knepley   Input Parameters:
66795dc8c3f7SMatthew G. Knepley + dm      - The DM object
6680db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6681db2bf62eSStefano 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.
66825dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
66835dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
66845dc8c3f7SMatthew G. Knepley 
6685db2bf62eSStefano 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.
6686db2bf62eSStefano Zampini 
66875dc8c3f7SMatthew G. Knepley   Level: developer
66885dc8c3f7SMatthew G. Knepley 
66895dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
66905dc8c3f7SMatthew G. Knepley @*/
669190b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6692c6b900c6SMatthew G. Knepley {
6693c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6694c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6695c6b900c6SMatthew G. Knepley 
6696c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6697c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
669890b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6699412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6700412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6701412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
67025dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
670390b157c4SStefano Zampini   if (maxCell) {
6704412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6705412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6706db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6707db2bf62eSStefano Zampini     ierr = PetscFree(dm->maxCell);CHKERRQ(ierr);
6708412e9a14SMatthew G. Knepley   }
6709db2bf62eSStefano Zampini 
6710412e9a14SMatthew G. Knepley   if (L) {
6711412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6712412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6713412e9a14SMatthew G. Knepley   }
6714412e9a14SMatthew G. Knepley   if (bd) {
6715412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6716412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
671790b157c4SStefano Zampini   }
6718072d7d67SStefano Zampini   dm->periodic = per;
6719c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6720c6b900c6SMatthew G. Knepley }
6721c6b900c6SMatthew G. Knepley 
67222e17dfb7SMatthew G. Knepley /*@
67232e17dfb7SMatthew 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.
67242e17dfb7SMatthew G. Knepley 
67252e17dfb7SMatthew G. Knepley   Input Parameters:
67262e17dfb7SMatthew G. Knepley + dm     - The DM
672765da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
672865da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
67292e17dfb7SMatthew G. Knepley 
67302e17dfb7SMatthew G. Knepley   Output Parameter:
67312e17dfb7SMatthew G. Knepley . out - The localized coordinate point
67322e17dfb7SMatthew G. Knepley 
67332e17dfb7SMatthew G. Knepley   Level: developer
67342e17dfb7SMatthew G. Knepley 
67352e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
67362e17dfb7SMatthew G. Knepley @*/
673765da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
67382e17dfb7SMatthew G. Knepley {
67392e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
67402e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
67412e17dfb7SMatthew G. Knepley 
67422e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
67432e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
67442e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
67452e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
67462e17dfb7SMatthew G. Knepley   } else {
674765da65dcSMatthew G. Knepley     if (endpoint) {
674865da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6749da3333bfSMatthew 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)) {
6750da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
675165da65dcSMatthew G. Knepley         } else {
6752da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
675365da65dcSMatthew G. Knepley         }
675465da65dcSMatthew G. Knepley       }
675565da65dcSMatthew G. Knepley     } else {
67562e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
67571118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
67582e17dfb7SMatthew G. Knepley       }
67592e17dfb7SMatthew G. Knepley     }
676065da65dcSMatthew G. Knepley   }
67612e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
67622e17dfb7SMatthew G. Knepley }
67632e17dfb7SMatthew G. Knepley 
67642e17dfb7SMatthew G. Knepley /*
67652e17dfb7SMatthew 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.
67662e17dfb7SMatthew G. Knepley 
67672e17dfb7SMatthew G. Knepley   Input Parameters:
67682e17dfb7SMatthew G. Knepley + dm     - The DM
67692e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
67702e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
67712e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
67722e17dfb7SMatthew G. Knepley 
67732e17dfb7SMatthew G. Knepley   Output Parameter:
67742e17dfb7SMatthew G. Knepley . out - The localized coordinate point
67752e17dfb7SMatthew G. Knepley 
67762e17dfb7SMatthew G. Knepley   Level: developer
67772e17dfb7SMatthew G. Knepley 
67782e17dfb7SMatthew 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
67792e17dfb7SMatthew G. Knepley 
67802e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
67812e17dfb7SMatthew G. Knepley */
67822e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar 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) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
67922e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(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 }
6800a5801f52SStefano Zampini 
68012e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
68022e17dfb7SMatthew G. Knepley {
68032e17dfb7SMatthew G. Knepley   PetscInt d;
68042e17dfb7SMatthew G. Knepley 
68052e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68062e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68072e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68082e17dfb7SMatthew G. Knepley   } else {
68092e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6810908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
68112e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
68122e17dfb7SMatthew G. Knepley       } else {
68132e17dfb7SMatthew G. Knepley         out[d] = in[d];
68142e17dfb7SMatthew G. Knepley       }
68152e17dfb7SMatthew G. Knepley     }
68162e17dfb7SMatthew G. Knepley   }
68172e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68182e17dfb7SMatthew G. Knepley }
68192e17dfb7SMatthew G. Knepley 
68202e17dfb7SMatthew G. Knepley /*
68212e17dfb7SMatthew 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.
68222e17dfb7SMatthew G. Knepley 
68232e17dfb7SMatthew G. Knepley   Input Parameters:
68242e17dfb7SMatthew G. Knepley + dm     - The DM
68252e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
68262e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
68272e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
68282e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
68292e17dfb7SMatthew G. Knepley 
68302e17dfb7SMatthew G. Knepley   Output Parameter:
68312e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
68322e17dfb7SMatthew G. Knepley 
68332e17dfb7SMatthew G. Knepley   Level: developer
68342e17dfb7SMatthew G. Knepley 
68352e17dfb7SMatthew 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
68362e17dfb7SMatthew G. Knepley 
68372e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
68382e17dfb7SMatthew G. Knepley */
68392e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
68402e17dfb7SMatthew G. Knepley {
68412e17dfb7SMatthew G. Knepley   PetscInt d;
68422e17dfb7SMatthew G. Knepley 
68432e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68442e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68452e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
68462e17dfb7SMatthew G. Knepley   } else {
68472e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6848412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6849412e9a14SMatthew G. Knepley 
6850412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6851412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6852412e9a14SMatthew G. Knepley 
6853412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6854412e9a14SMatthew 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]));
6855412e9a14SMatthew G. Knepley         out[d] += newCoord;
68562e17dfb7SMatthew G. Knepley       } else {
68572e17dfb7SMatthew G. Knepley         out[d] += in[d];
68582e17dfb7SMatthew G. Knepley       }
68592e17dfb7SMatthew G. Knepley     }
68602e17dfb7SMatthew G. Knepley   }
68612e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68622e17dfb7SMatthew G. Knepley }
68632e17dfb7SMatthew G. Knepley 
686436447a5eSToby Isaac /*@
68658f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
68668f700142SStefano Zampini 
68678f700142SStefano Zampini   Not collective
686836447a5eSToby Isaac 
686936447a5eSToby Isaac   Input Parameter:
687036447a5eSToby Isaac . dm - The DM
687136447a5eSToby Isaac 
687236447a5eSToby Isaac   Output Parameter:
687336447a5eSToby Isaac   areLocalized - True if localized
687436447a5eSToby Isaac 
687536447a5eSToby Isaac   Level: developer
687636447a5eSToby Isaac 
68778f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
687836447a5eSToby Isaac @*/
68798f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
688036447a5eSToby Isaac {
688136447a5eSToby Isaac   DM             cdm;
688236447a5eSToby Isaac   PetscSection   coordSection;
68832ccac677SMatthew G. Knepley   PetscInt       depth, cStart, cEnd, sStart, sEnd, c, dof;
688446a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
688536447a5eSToby Isaac   PetscErrorCode ierr;
688636447a5eSToby Isaac 
688736447a5eSToby Isaac   PetscFunctionBegin;
688836447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6889534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
68908b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
689146a3a80fSLisandro Dalcin 
689236447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
689336447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
689446a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
68959f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
68962ccac677SMatthew G. Knepley   ierr = DMPlexGetDepth(cdm, &depth);CHKERRQ(ierr);
68972ccac677SMatthew G. Knepley   if (!depth) PetscFunctionReturn(0);
689846a3a80fSLisandro Dalcin 
68999f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
690046a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
690136447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
690236447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
690346a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
690446a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
690536447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
690646a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
690736447a5eSToby Isaac   }
69088f700142SStefano Zampini   *areLocalized = alreadyLocalized;
690936447a5eSToby Isaac   PetscFunctionReturn(0);
691036447a5eSToby Isaac }
691136447a5eSToby Isaac 
69128f700142SStefano Zampini /*@
69138f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
69148f700142SStefano Zampini 
69158f700142SStefano Zampini   Collective on dm
69168f700142SStefano Zampini 
69178f700142SStefano Zampini   Input Parameter:
69188f700142SStefano Zampini . dm - The DM
69198f700142SStefano Zampini 
69208f700142SStefano Zampini   Output Parameter:
69218f700142SStefano Zampini   areLocalized - True if localized
69228f700142SStefano Zampini 
69238f700142SStefano Zampini   Level: developer
69248f700142SStefano Zampini 
69258f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
69268f700142SStefano Zampini @*/
69278f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
69288f700142SStefano Zampini {
69298f700142SStefano Zampini   PetscBool      localized;
69308f700142SStefano Zampini   PetscErrorCode ierr;
69318f700142SStefano Zampini 
69328f700142SStefano Zampini   PetscFunctionBegin;
69338f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6934534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
69358f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
6936820f2d46SBarry Smith   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
69378f700142SStefano Zampini   PetscFunctionReturn(0);
69388f700142SStefano Zampini }
693936447a5eSToby Isaac 
69402e17dfb7SMatthew G. Knepley /*@
6941492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
69422e17dfb7SMatthew G. Knepley 
69438f700142SStefano Zampini   Collective on dm
69448f700142SStefano Zampini 
69452e17dfb7SMatthew G. Knepley   Input Parameter:
69462e17dfb7SMatthew G. Knepley . dm - The DM
69472e17dfb7SMatthew G. Knepley 
69482e17dfb7SMatthew G. Knepley   Level: developer
69492e17dfb7SMatthew G. Knepley 
69508f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
69512e17dfb7SMatthew G. Knepley @*/
69522e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
69532e17dfb7SMatthew G. Knepley {
69542e17dfb7SMatthew G. Knepley   DM             cdm;
69552e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
69562e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
69573e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
69583e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6959e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
69603e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
69613e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
69622e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
69632e17dfb7SMatthew G. Knepley 
69642e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69652e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
696692c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6967f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6968f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6969f7cbd40bSStefano Zampini 
69702e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
69712e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
69722e17dfb7SMatthew G. Knepley   {
69732e17dfb7SMatthew G. Knepley     PetscBool isplex;
69742e17dfb7SMatthew G. Knepley 
69752e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
69762e17dfb7SMatthew G. Knepley     if (isplex) {
69772e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
69783e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
697969291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
69803e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
69813e922f36SToby Isaac       newStart = vStart;
69823e922f36SToby Isaac       newEnd   = vEnd;
69833e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
69843e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
69853e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
69863e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
69873e922f36SToby Isaac       }
69882e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
69892e17dfb7SMatthew G. Knepley   }
69902e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
699143eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
69922e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
69933e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6994e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
69953e922f36SToby Isaac 
69962e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
69972e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
69982e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
69992e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
70003e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
70013e922f36SToby Isaac 
700269291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
70033e922f36SToby Isaac   localized = &anchor[bs];
70043e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
70053e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
70063e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
70073e922f36SToby Isaac 
70083e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
70093e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
70103e922f36SToby Isaac       PetscInt     b;
70113e922f36SToby Isaac 
70123e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
70133e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
70143e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
70153e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
70163e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
70173e922f36SToby Isaac         for (b = 0; b < bs; b++) {
70183e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
70193e922f36SToby Isaac         }
70203e922f36SToby Isaac         if (b < bs) break;
70213e922f36SToby Isaac       }
70223e922f36SToby Isaac       if (d < dof/bs) {
70233e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
70243e922f36SToby Isaac           PetscInt cdof;
70253e922f36SToby Isaac 
70263e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
70273e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
70283e922f36SToby Isaac         }
70293e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
70303e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
70313e922f36SToby Isaac       }
70323e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
70333e922f36SToby Isaac     }
70343e922f36SToby Isaac   }
7035ffc4695bSBarry Smith   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
70363e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
703769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
70383e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
703969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
70403e922f36SToby Isaac     PetscFunctionReturn(0);
70413e922f36SToby Isaac   }
70422e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
70432e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
70442e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
70452e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
70462e17dfb7SMatthew G. Knepley   }
70472e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
70482e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
7049c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
70502e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
70512e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
70522e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
70532e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
7054c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
70552e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
70562e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
70572e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
70582e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
70592e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
70602e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
70612e17dfb7SMatthew G. Knepley   }
70623e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
70633e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
70643e922f36SToby Isaac 
70652e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
70662e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
70673e922f36SToby Isaac       PetscInt     b, cdof;
70682e17dfb7SMatthew G. Knepley 
70693e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
70703e922f36SToby Isaac       if (!cdof) continue;
70712e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
70722e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
70732e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
70742e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
70752e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
70762e17dfb7SMatthew G. Knepley     }
70773e922f36SToby Isaac   }
707869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
707969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
7080c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
70812e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
70822e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
70832e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
70842e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
70852e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
70862e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
70872e17dfb7SMatthew G. Knepley }
70882e17dfb7SMatthew G. Knepley 
7089e87bb0d3SMatthew G Knepley /*@
70903a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
7091e87bb0d3SMatthew G Knepley 
7092d083f849SBarry Smith   Collective on v (see explanation below)
7093e87bb0d3SMatthew G Knepley 
7094e87bb0d3SMatthew G Knepley   Input Parameters:
7095e87bb0d3SMatthew G Knepley + dm - The DM
70966b867d5aSJose E. Roman - ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
7097e87bb0d3SMatthew G Knepley 
70986b867d5aSJose E. Roman   Input/Output Parameters:
70996b867d5aSJose E. Roman + v - The Vec of points, on output contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
71006b867d5aSJose E. Roman - cellSF - Points to either NULL, or a PetscSF with guesses for which cells contain each point;
71016b867d5aSJose E. Roman            on output, the PetscSF containing the ranks and local indices of the containing points
71023a93e3b7SToby Isaac 
7103e87bb0d3SMatthew G Knepley   Level: developer
710461e3bb9bSMatthew G Knepley 
710562a38674SMatthew G. Knepley   Notes:
71063a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
710762a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
71083a93e3b7SToby Isaac 
71093a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
711062a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
71113a93e3b7SToby Isaac 
71123a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
71133a93e3b7SToby Isaac 
711462a38674SMatthew G. Knepley $    const PetscSFNode *cells;
711562a38674SMatthew G. Knepley $    PetscInt           nFound;
7116a6216909SToby Isaac $    const PetscInt    *found;
711762a38674SMatthew G. Knepley $
7118a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
71193a93e3b7SToby Isaac 
71203a93e3b7SToby 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
71213a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
71223a93e3b7SToby Isaac 
712362a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
712461e3bb9bSMatthew G Knepley @*/
712562a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
7126e87bb0d3SMatthew G Knepley {
7127735aa83eSMatthew G Knepley   PetscErrorCode ierr;
7128735aa83eSMatthew G Knepley 
7129e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
7130e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7131e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
7132e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
71333a93e3b7SToby Isaac   if (*cellSF) {
71343a93e3b7SToby Isaac     PetscMPIInt result;
71353a93e3b7SToby Isaac 
7136e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
7137ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRMPI(ierr);
71383a93e3b7SToby 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");
7139e0fc9d1bSMatthew G. Knepley   } else {
71403a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
71413a93e3b7SToby Isaac   }
7142b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
714347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
714462a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
714547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
7146e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
7147e87bb0d3SMatthew G Knepley }
714814f150ffSMatthew G. Knepley 
7149f4d763aaSMatthew G. Knepley /*@
7150f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
7151f4d763aaSMatthew G. Knepley 
71528f700142SStefano Zampini   Collective on dm
71538f700142SStefano Zampini 
7154f4d763aaSMatthew G. Knepley   Input Parameter:
7155f4d763aaSMatthew G. Knepley . dm - The original DM
7156f4d763aaSMatthew G. Knepley 
7157f4d763aaSMatthew G. Knepley   Output Parameter:
7158f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
7159f4d763aaSMatthew G. Knepley 
7160f4d763aaSMatthew G. Knepley   Level: intermediate
7161f4d763aaSMatthew G. Knepley 
7162e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
7163f4d763aaSMatthew G. Knepley @*/
716414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
716514f150ffSMatthew G. Knepley {
7166c26acbdeSMatthew G. Knepley   PetscSection   section;
71672d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
716814f150ffSMatthew G. Knepley   PetscErrorCode ierr;
716914f150ffSMatthew G. Knepley 
717014f150ffSMatthew G. Knepley   PetscFunctionBegin;
717114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
717214f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
717392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
7174c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
7175ffc4695bSBarry Smith   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
71762d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
7177c26acbdeSMatthew G. Knepley     *odm = dm;
7178c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
7179c26acbdeSMatthew G. Knepley   }
718014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
7181c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
718214f150ffSMatthew G. Knepley     PetscSF      sf;
718314f150ffSMatthew G. Knepley 
718414f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
7185e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
718614f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
718792fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
718814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
718914f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
719015b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
7191e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
719214f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
719314f150ffSMatthew G. Knepley   }
719414f150ffSMatthew G. Knepley   *odm = dm->dmBC;
719514f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
719614f150ffSMatthew G. Knepley }
7197f4d763aaSMatthew G. Knepley 
7198f4d763aaSMatthew G. Knepley /*@
7199cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7200f4d763aaSMatthew G. Knepley 
7201f4d763aaSMatthew G. Knepley   Input Parameter:
7202f4d763aaSMatthew G. Knepley . dm - The original DM
7203f4d763aaSMatthew G. Knepley 
7204cdb7a50dSMatthew G. Knepley   Output Parameters:
7205cdb7a50dSMatthew G. Knepley + num - The output sequence number
7206cdb7a50dSMatthew G. Knepley - val - The output sequence value
7207f4d763aaSMatthew G. Knepley 
7208f4d763aaSMatthew G. Knepley   Level: intermediate
7209f4d763aaSMatthew G. Knepley 
7210f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7211f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7212f4d763aaSMatthew G. Knepley 
7213f4d763aaSMatthew G. Knepley .seealso: VecView()
7214f4d763aaSMatthew G. Knepley @*/
7215cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7216f4d763aaSMatthew G. Knepley {
7217f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7218f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7219534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7220534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7221f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7222f4d763aaSMatthew G. Knepley }
7223f4d763aaSMatthew G. Knepley 
7224f4d763aaSMatthew G. Knepley /*@
7225cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7226f4d763aaSMatthew G. Knepley 
7227f4d763aaSMatthew G. Knepley   Input Parameters:
7228f4d763aaSMatthew G. Knepley + dm - The original DM
7229cdb7a50dSMatthew G. Knepley . num - The output sequence number
7230cdb7a50dSMatthew G. Knepley - val - The output sequence value
7231f4d763aaSMatthew G. Knepley 
7232f4d763aaSMatthew G. Knepley   Level: intermediate
7233f4d763aaSMatthew G. Knepley 
7234f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7235f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7236f4d763aaSMatthew G. Knepley 
7237f4d763aaSMatthew G. Knepley .seealso: VecView()
7238f4d763aaSMatthew G. Knepley @*/
7239cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7240f4d763aaSMatthew G. Knepley {
7241f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7242f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7243f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7244cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7245cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7246cdb7a50dSMatthew G. Knepley }
7247cdb7a50dSMatthew G. Knepley 
7248cdb7a50dSMatthew G. Knepley /*@C
7249cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7250cdb7a50dSMatthew G. Knepley 
7251cdb7a50dSMatthew G. Knepley   Input Parameters:
7252cdb7a50dSMatthew G. Knepley + dm   - The original DM
7253cdb7a50dSMatthew G. Knepley . name - The sequence name
7254cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7255cdb7a50dSMatthew G. Knepley 
7256cdb7a50dSMatthew G. Knepley   Output Parameter:
7257cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7258cdb7a50dSMatthew G. Knepley 
7259cdb7a50dSMatthew G. Knepley   Level: intermediate
7260cdb7a50dSMatthew G. Knepley 
7261cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7262cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7263cdb7a50dSMatthew G. Knepley 
7264cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7265cdb7a50dSMatthew G. Knepley @*/
7266cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7267cdb7a50dSMatthew G. Knepley {
7268cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7269cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
7270cdb7a50dSMatthew G. Knepley 
7271cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7272cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7273cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7274064a246eSJacob Faibussowitsch   PetscValidRealPointer(val,5);
7275cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
7276cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7277cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7278cdb7a50dSMatthew G. Knepley     PetscScalar value;
7279cdb7a50dSMatthew G. Knepley 
728039d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
72814aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7282cdb7a50dSMatthew G. Knepley #endif
7283cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7284f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7285f4d763aaSMatthew G. Knepley }
72868e4ac7eaSMatthew G. Knepley 
72878e4ac7eaSMatthew G. Knepley /*@
72888e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
72898e4ac7eaSMatthew G. Knepley 
72908e4ac7eaSMatthew G. Knepley   Not collective
72918e4ac7eaSMatthew G. Knepley 
72928e4ac7eaSMatthew G. Knepley   Input Parameter:
72938e4ac7eaSMatthew G. Knepley . dm - The DM
72948e4ac7eaSMatthew G. Knepley 
72958e4ac7eaSMatthew G. Knepley   Output Parameter:
72968e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
72978e4ac7eaSMatthew G. Knepley 
72988e4ac7eaSMatthew G. Knepley   Level: beginner
72998e4ac7eaSMatthew G. Knepley 
73008e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
73018e4ac7eaSMatthew G. Knepley @*/
73028e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
73038e4ac7eaSMatthew G. Knepley {
73048e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
73058e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7306534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
73078e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
73088e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
73098e4ac7eaSMatthew G. Knepley }
73108e4ac7eaSMatthew G. Knepley 
73118e4ac7eaSMatthew G. Knepley /*@
73125d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
73138e4ac7eaSMatthew G. Knepley 
73148e4ac7eaSMatthew G. Knepley   Collective on dm
73158e4ac7eaSMatthew G. Knepley 
73168e4ac7eaSMatthew G. Knepley   Input Parameters:
73178e4ac7eaSMatthew G. Knepley + dm - The DM
73188e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
73198e4ac7eaSMatthew G. Knepley 
73205d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
73215d3b26e6SMatthew G. Knepley 
73228e4ac7eaSMatthew G. Knepley   Level: beginner
73238e4ac7eaSMatthew G. Knepley 
73245d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
73258e4ac7eaSMatthew G. Knepley @*/
73268e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
73278e4ac7eaSMatthew G. Knepley {
73288e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
73298e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73308833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
73318e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
73328e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
73338e4ac7eaSMatthew G. Knepley }
7334c58f1c22SToby Isaac 
7335c58f1c22SToby Isaac /*@C
7336c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7337c58f1c22SToby Isaac 
7338c58f1c22SToby Isaac   Not Collective
7339c58f1c22SToby Isaac 
7340c58f1c22SToby Isaac   Input Parameters:
7341c58f1c22SToby Isaac + dm   - The DM object
7342c58f1c22SToby Isaac - name - The label name
7343c58f1c22SToby Isaac 
7344c58f1c22SToby Isaac   Level: intermediate
7345c58f1c22SToby Isaac 
7346c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7347c58f1c22SToby Isaac @*/
7348c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7349c58f1c22SToby Isaac {
73505d80c0bfSVaclav Hapla   PetscBool      flg;
73515d80c0bfSVaclav Hapla   DMLabel        label;
7352c58f1c22SToby Isaac   PetscErrorCode ierr;
7353c58f1c22SToby Isaac 
7354c58f1c22SToby Isaac   PetscFunctionBegin;
7355c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7356c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
73575d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7358c58f1c22SToby Isaac   if (!flg) {
73595d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
73605d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
73615d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7362c58f1c22SToby Isaac   }
7363c58f1c22SToby Isaac   PetscFunctionReturn(0);
7364c58f1c22SToby Isaac }
7365c58f1c22SToby Isaac 
7366c58f1c22SToby Isaac /*@C
73670fdc7489SMatthew Knepley   DMCreateLabelAtIndex - Create a label of the given name at the iven index. If it already exists, move it to this index.
73680fdc7489SMatthew Knepley 
73690fdc7489SMatthew Knepley   Not Collective
73700fdc7489SMatthew Knepley 
73710fdc7489SMatthew Knepley   Input Parameters:
73720fdc7489SMatthew Knepley + dm   - The DM object
73730fdc7489SMatthew Knepley . l    - The index for the label
73740fdc7489SMatthew Knepley - name - The label name
73750fdc7489SMatthew Knepley 
73760fdc7489SMatthew Knepley   Level: intermediate
73770fdc7489SMatthew Knepley 
73780fdc7489SMatthew Knepley .seealso: DMCreateLabel(), DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
73790fdc7489SMatthew Knepley @*/
73800fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
73810fdc7489SMatthew Knepley {
73820fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
73830fdc7489SMatthew Knepley   DMLabel        label;
73840fdc7489SMatthew Knepley   PetscInt       Nl, m;
73850fdc7489SMatthew Knepley   PetscBool      flg, match;
73860fdc7489SMatthew Knepley   const char    *lname;
73870fdc7489SMatthew Knepley   PetscErrorCode ierr;
73880fdc7489SMatthew Knepley 
73890fdc7489SMatthew Knepley   PetscFunctionBegin;
73900fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7391064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
73920fdc7489SMatthew Knepley   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
73930fdc7489SMatthew Knepley   if (!flg) {
73940fdc7489SMatthew Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
73950fdc7489SMatthew Knepley     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
73960fdc7489SMatthew Knepley     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
73970fdc7489SMatthew Knepley   }
73980fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
73990fdc7489SMatthew Knepley   if (l >= Nl) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %D must be in [0, %D)", l, Nl);
74000fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
74010fdc7489SMatthew Knepley     ierr = PetscObjectGetName((PetscObject) orig->label, &lname);CHKERRQ(ierr);
74020fdc7489SMatthew Knepley     ierr = PetscStrcmp(name, lname, &match);CHKERRQ(ierr);
74030fdc7489SMatthew Knepley     if (match) break;
74040fdc7489SMatthew Knepley   }
74050fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
74060fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
74070fdc7489SMatthew Knepley   else    prev->next = orig->next;
74080fdc7489SMatthew Knepley   if (!l) {
74090fdc7489SMatthew Knepley     orig->next = dm->labels;
74100fdc7489SMatthew Knepley     dm->labels = orig;
74110fdc7489SMatthew Knepley   } else {
74120fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
74130fdc7489SMatthew Knepley     orig->next = prev->next;
74140fdc7489SMatthew Knepley     prev->next = orig;
74150fdc7489SMatthew Knepley   }
74160fdc7489SMatthew Knepley   PetscFunctionReturn(0);
74170fdc7489SMatthew Knepley }
74180fdc7489SMatthew Knepley 
74190fdc7489SMatthew Knepley /*@C
7420c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7421c58f1c22SToby Isaac 
7422c58f1c22SToby Isaac   Not Collective
7423c58f1c22SToby Isaac 
7424c58f1c22SToby Isaac   Input Parameters:
7425c58f1c22SToby Isaac + dm   - The DM object
7426c58f1c22SToby Isaac . name - The label name
7427c58f1c22SToby Isaac - point - The mesh point
7428c58f1c22SToby Isaac 
7429c58f1c22SToby Isaac   Output Parameter:
7430c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7431c58f1c22SToby Isaac 
7432c58f1c22SToby Isaac   Level: beginner
7433c58f1c22SToby Isaac 
7434c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7435c58f1c22SToby Isaac @*/
7436c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7437c58f1c22SToby Isaac {
7438c58f1c22SToby Isaac   DMLabel        label;
7439c58f1c22SToby Isaac   PetscErrorCode ierr;
7440c58f1c22SToby Isaac 
7441c58f1c22SToby Isaac   PetscFunctionBegin;
7442c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7443c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7444c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
744513903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7446c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7447c58f1c22SToby Isaac   PetscFunctionReturn(0);
7448c58f1c22SToby Isaac }
7449c58f1c22SToby Isaac 
7450c58f1c22SToby Isaac /*@C
7451c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7452c58f1c22SToby Isaac 
7453c58f1c22SToby Isaac   Not Collective
7454c58f1c22SToby Isaac 
7455c58f1c22SToby Isaac   Input Parameters:
7456c58f1c22SToby Isaac + dm   - The DM object
7457c58f1c22SToby Isaac . name - The label name
7458c58f1c22SToby Isaac . point - The mesh point
7459c58f1c22SToby Isaac - value - The label value for this point
7460c58f1c22SToby Isaac 
7461c58f1c22SToby Isaac   Output Parameter:
7462c58f1c22SToby Isaac 
7463c58f1c22SToby Isaac   Level: beginner
7464c58f1c22SToby Isaac 
7465c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7466c58f1c22SToby Isaac @*/
7467c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7468c58f1c22SToby Isaac {
7469c58f1c22SToby Isaac   DMLabel        label;
7470c58f1c22SToby Isaac   PetscErrorCode ierr;
7471c58f1c22SToby Isaac 
7472c58f1c22SToby Isaac   PetscFunctionBegin;
7473c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7474c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7475c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7476c58f1c22SToby Isaac   if (!label) {
7477c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7478c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7479c58f1c22SToby Isaac   }
7480c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7481c58f1c22SToby Isaac   PetscFunctionReturn(0);
7482c58f1c22SToby Isaac }
7483c58f1c22SToby Isaac 
7484c58f1c22SToby Isaac /*@C
7485c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7486c58f1c22SToby Isaac 
7487c58f1c22SToby Isaac   Not Collective
7488c58f1c22SToby Isaac 
7489c58f1c22SToby Isaac   Input Parameters:
7490c58f1c22SToby Isaac + dm   - The DM object
7491c58f1c22SToby Isaac . name - The label name
7492c58f1c22SToby Isaac . point - The mesh point
7493c58f1c22SToby Isaac - value - The label value for this point
7494c58f1c22SToby Isaac 
7495c58f1c22SToby Isaac   Output Parameter:
7496c58f1c22SToby Isaac 
7497c58f1c22SToby Isaac   Level: beginner
7498c58f1c22SToby Isaac 
7499c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7500c58f1c22SToby Isaac @*/
7501c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7502c58f1c22SToby Isaac {
7503c58f1c22SToby Isaac   DMLabel        label;
7504c58f1c22SToby Isaac   PetscErrorCode ierr;
7505c58f1c22SToby Isaac 
7506c58f1c22SToby Isaac   PetscFunctionBegin;
7507c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7508c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7509c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7510c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7511c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7512c58f1c22SToby Isaac   PetscFunctionReturn(0);
7513c58f1c22SToby Isaac }
7514c58f1c22SToby Isaac 
7515c58f1c22SToby Isaac /*@C
7516c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7517c58f1c22SToby Isaac 
7518c58f1c22SToby Isaac   Not Collective
7519c58f1c22SToby Isaac 
7520c58f1c22SToby Isaac   Input Parameters:
7521c58f1c22SToby Isaac + dm   - The DM object
7522c58f1c22SToby Isaac - name - The label name
7523c58f1c22SToby Isaac 
7524c58f1c22SToby Isaac   Output Parameter:
7525c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7526c58f1c22SToby Isaac 
7527c58f1c22SToby Isaac   Level: beginner
7528c58f1c22SToby Isaac 
7529df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7530c58f1c22SToby Isaac @*/
7531c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7532c58f1c22SToby Isaac {
7533c58f1c22SToby Isaac   DMLabel        label;
7534c58f1c22SToby Isaac   PetscErrorCode ierr;
7535c58f1c22SToby Isaac 
7536c58f1c22SToby Isaac   PetscFunctionBegin;
7537c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7538c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7539534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7540c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7541c58f1c22SToby Isaac   *size = 0;
7542c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7543c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7544c58f1c22SToby Isaac   PetscFunctionReturn(0);
7545c58f1c22SToby Isaac }
7546c58f1c22SToby Isaac 
7547c58f1c22SToby Isaac /*@C
7548c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7549c58f1c22SToby Isaac 
7550c58f1c22SToby Isaac   Not Collective
7551c58f1c22SToby Isaac 
7552c58f1c22SToby Isaac   Input Parameters:
7553c58f1c22SToby Isaac + mesh - The DM object
7554c58f1c22SToby Isaac - name - The label name
7555c58f1c22SToby Isaac 
7556c58f1c22SToby Isaac   Output Parameter:
7557c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7558c58f1c22SToby Isaac 
7559c58f1c22SToby Isaac   Level: beginner
7560c58f1c22SToby Isaac 
7561c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7562c58f1c22SToby Isaac @*/
7563c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7564c58f1c22SToby Isaac {
7565c58f1c22SToby Isaac   DMLabel        label;
7566c58f1c22SToby Isaac   PetscErrorCode ierr;
7567c58f1c22SToby Isaac 
7568c58f1c22SToby Isaac   PetscFunctionBegin;
7569c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7570c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7571c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7572c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7573c58f1c22SToby Isaac   *ids = NULL;
7574dab2e251SBlaise Bourdin  if (label) {
7575c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7576dab2e251SBlaise Bourdin   } else {
7577dab2e251SBlaise Bourdin     /* returning an empty IS */
7578dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7579dab2e251SBlaise Bourdin   }
7580c58f1c22SToby Isaac   PetscFunctionReturn(0);
7581c58f1c22SToby Isaac }
7582c58f1c22SToby Isaac 
7583c58f1c22SToby Isaac /*@C
7584c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7585c58f1c22SToby Isaac 
7586c58f1c22SToby Isaac   Not Collective
7587c58f1c22SToby Isaac 
7588c58f1c22SToby Isaac   Input Parameters:
7589c58f1c22SToby Isaac + dm - The DM object
7590c58f1c22SToby Isaac . name - The label name
7591c58f1c22SToby Isaac - value - The stratum value
7592c58f1c22SToby Isaac 
7593c58f1c22SToby Isaac   Output Parameter:
7594c58f1c22SToby Isaac . size - The stratum size
7595c58f1c22SToby Isaac 
7596c58f1c22SToby Isaac   Level: beginner
7597c58f1c22SToby Isaac 
7598c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7599c58f1c22SToby Isaac @*/
7600c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7601c58f1c22SToby Isaac {
7602c58f1c22SToby Isaac   DMLabel        label;
7603c58f1c22SToby Isaac   PetscErrorCode ierr;
7604c58f1c22SToby Isaac 
7605c58f1c22SToby Isaac   PetscFunctionBegin;
7606c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7607c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7608534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7609c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7610c58f1c22SToby Isaac   *size = 0;
7611c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7612c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7613c58f1c22SToby Isaac   PetscFunctionReturn(0);
7614c58f1c22SToby Isaac }
7615c58f1c22SToby Isaac 
7616c58f1c22SToby Isaac /*@C
7617c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7618c58f1c22SToby Isaac 
7619c58f1c22SToby Isaac   Not Collective
7620c58f1c22SToby Isaac 
7621c58f1c22SToby Isaac   Input Parameters:
7622c58f1c22SToby Isaac + dm - The DM object
7623c58f1c22SToby Isaac . name - The label name
7624c58f1c22SToby Isaac - value - The stratum value
7625c58f1c22SToby Isaac 
7626c58f1c22SToby Isaac   Output Parameter:
7627c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7628c58f1c22SToby Isaac 
7629c58f1c22SToby Isaac   Level: beginner
7630c58f1c22SToby Isaac 
7631c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7632c58f1c22SToby Isaac @*/
7633c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7634c58f1c22SToby Isaac {
7635c58f1c22SToby Isaac   DMLabel        label;
7636c58f1c22SToby Isaac   PetscErrorCode ierr;
7637c58f1c22SToby Isaac 
7638c58f1c22SToby Isaac   PetscFunctionBegin;
7639c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7640c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7641c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7642c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7643c58f1c22SToby Isaac   *points = NULL;
7644c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7645c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7646c58f1c22SToby Isaac   PetscFunctionReturn(0);
7647c58f1c22SToby Isaac }
7648c58f1c22SToby Isaac 
76494de306b1SToby Isaac /*@C
76509044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
76514de306b1SToby Isaac 
76524de306b1SToby Isaac   Not Collective
76534de306b1SToby Isaac 
76544de306b1SToby Isaac   Input Parameters:
76554de306b1SToby Isaac + dm - The DM object
76564de306b1SToby Isaac . name - The label name
76574de306b1SToby Isaac . value - The stratum value
76584de306b1SToby Isaac - points - The stratum points
76594de306b1SToby Isaac 
76604de306b1SToby Isaac   Level: beginner
76614de306b1SToby Isaac 
76624de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
76634de306b1SToby Isaac @*/
76644de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
76654de306b1SToby Isaac {
76664de306b1SToby Isaac   DMLabel        label;
76674de306b1SToby Isaac   PetscErrorCode ierr;
76684de306b1SToby Isaac 
76694de306b1SToby Isaac   PetscFunctionBegin;
76704de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
76714de306b1SToby Isaac   PetscValidCharPointer(name, 2);
76724de306b1SToby Isaac   PetscValidPointer(points, 4);
76734de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
76744de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
76754de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
76764de306b1SToby Isaac   PetscFunctionReturn(0);
76774de306b1SToby Isaac }
76784de306b1SToby Isaac 
7679c58f1c22SToby Isaac /*@C
7680c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7681c58f1c22SToby Isaac 
7682c58f1c22SToby Isaac   Not Collective
7683c58f1c22SToby Isaac 
7684c58f1c22SToby Isaac   Input Parameters:
7685c58f1c22SToby Isaac + dm   - The DM object
7686c58f1c22SToby Isaac . name - The label name
7687c58f1c22SToby Isaac - value - The label value for this point
7688c58f1c22SToby Isaac 
7689c58f1c22SToby Isaac   Output Parameter:
7690c58f1c22SToby Isaac 
7691c58f1c22SToby Isaac   Level: beginner
7692c58f1c22SToby Isaac 
7693c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7694c58f1c22SToby Isaac @*/
7695c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7696c58f1c22SToby Isaac {
7697c58f1c22SToby Isaac   DMLabel        label;
7698c58f1c22SToby Isaac   PetscErrorCode ierr;
7699c58f1c22SToby Isaac 
7700c58f1c22SToby Isaac   PetscFunctionBegin;
7701c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7702c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7703c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7704c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7705c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7706c58f1c22SToby Isaac   PetscFunctionReturn(0);
7707c58f1c22SToby Isaac }
7708c58f1c22SToby Isaac 
7709c58f1c22SToby Isaac /*@
7710c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7711c58f1c22SToby Isaac 
7712c58f1c22SToby Isaac   Not Collective
7713c58f1c22SToby Isaac 
7714c58f1c22SToby Isaac   Input Parameter:
7715c58f1c22SToby Isaac . dm   - The DM object
7716c58f1c22SToby Isaac 
7717c58f1c22SToby Isaac   Output Parameter:
7718c58f1c22SToby Isaac . numLabels - the number of Labels
7719c58f1c22SToby Isaac 
7720c58f1c22SToby Isaac   Level: intermediate
7721c58f1c22SToby Isaac 
7722c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7723c58f1c22SToby Isaac @*/
7724c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7725c58f1c22SToby Isaac {
77265d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7727c58f1c22SToby Isaac   PetscInt  n    = 0;
7728c58f1c22SToby Isaac 
7729c58f1c22SToby Isaac   PetscFunctionBegin;
7730c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7731534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7732c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7733c58f1c22SToby Isaac   *numLabels = n;
7734c58f1c22SToby Isaac   PetscFunctionReturn(0);
7735c58f1c22SToby Isaac }
7736c58f1c22SToby Isaac 
7737c58f1c22SToby Isaac /*@C
7738c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7739c58f1c22SToby Isaac 
7740c58f1c22SToby Isaac   Not Collective
7741c58f1c22SToby Isaac 
7742c58f1c22SToby Isaac   Input Parameters:
7743c58f1c22SToby Isaac + dm - The DM object
7744c58f1c22SToby Isaac - n  - the label number
7745c58f1c22SToby Isaac 
7746c58f1c22SToby Isaac   Output Parameter:
7747c58f1c22SToby Isaac . name - the label name
7748c58f1c22SToby Isaac 
7749c58f1c22SToby Isaac   Level: intermediate
7750c58f1c22SToby Isaac 
7751c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7752c58f1c22SToby Isaac @*/
7753c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7754c58f1c22SToby Isaac {
77555d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7756c58f1c22SToby Isaac   PetscInt       l    = 0;
7757d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7758c58f1c22SToby Isaac 
7759c58f1c22SToby Isaac   PetscFunctionBegin;
7760c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7761c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7762c58f1c22SToby Isaac   while (next) {
7763c58f1c22SToby Isaac     if (l == n) {
7764d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7765c58f1c22SToby Isaac       PetscFunctionReturn(0);
7766c58f1c22SToby Isaac     }
7767c58f1c22SToby Isaac     ++l;
7768c58f1c22SToby Isaac     next = next->next;
7769c58f1c22SToby Isaac   }
7770c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7771c58f1c22SToby Isaac }
7772c58f1c22SToby Isaac 
7773c58f1c22SToby Isaac /*@C
7774c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7775c58f1c22SToby Isaac 
7776c58f1c22SToby Isaac   Not Collective
7777c58f1c22SToby Isaac 
7778c58f1c22SToby Isaac   Input Parameters:
7779c58f1c22SToby Isaac + dm   - The DM object
7780c58f1c22SToby Isaac - name - The label name
7781c58f1c22SToby Isaac 
7782c58f1c22SToby Isaac   Output Parameter:
7783c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7784c58f1c22SToby Isaac 
7785c58f1c22SToby Isaac   Level: intermediate
7786c58f1c22SToby Isaac 
7787c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7788c58f1c22SToby Isaac @*/
7789c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7790c58f1c22SToby Isaac {
77915d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7792d67d17b1SMatthew G. Knepley   const char    *lname;
7793c58f1c22SToby Isaac   PetscErrorCode ierr;
7794c58f1c22SToby Isaac 
7795c58f1c22SToby Isaac   PetscFunctionBegin;
7796c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7797c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7798534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7799c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7800c58f1c22SToby Isaac   while (next) {
7801d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7802d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7803c58f1c22SToby Isaac     if (*hasLabel) break;
7804c58f1c22SToby Isaac     next = next->next;
7805c58f1c22SToby Isaac   }
7806c58f1c22SToby Isaac   PetscFunctionReturn(0);
7807c58f1c22SToby Isaac }
7808c58f1c22SToby Isaac 
7809c58f1c22SToby Isaac /*@C
7810c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7811c58f1c22SToby Isaac 
7812c58f1c22SToby Isaac   Not Collective
7813c58f1c22SToby Isaac 
7814c58f1c22SToby Isaac   Input Parameters:
7815c58f1c22SToby Isaac + dm   - The DM object
7816c58f1c22SToby Isaac - name - The label name
7817c58f1c22SToby Isaac 
7818c58f1c22SToby Isaac   Output Parameter:
7819c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7820c58f1c22SToby Isaac 
78216d7c9049SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
78226d7c9049SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
78236d7c9049SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
78246d7c9049SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
78256d7c9049SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
78266d7c9049SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
78276d7c9049SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
78286d7c9049SMatthew G. Knepley 
7829c58f1c22SToby Isaac   Level: intermediate
7830c58f1c22SToby Isaac 
78316d7c9049SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
7832c58f1c22SToby Isaac @*/
7833c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7834c58f1c22SToby Isaac {
78355d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7836c58f1c22SToby Isaac   PetscBool      hasLabel;
7837d67d17b1SMatthew G. Knepley   const char    *lname;
7838c58f1c22SToby Isaac   PetscErrorCode ierr;
7839c58f1c22SToby Isaac 
7840c58f1c22SToby Isaac   PetscFunctionBegin;
7841c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7842c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7843c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7844c58f1c22SToby Isaac   *label = NULL;
7845c58f1c22SToby Isaac   while (next) {
7846d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7847d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7848c58f1c22SToby Isaac     if (hasLabel) {
7849c58f1c22SToby Isaac       *label = next->label;
7850c58f1c22SToby Isaac       break;
7851c58f1c22SToby Isaac     }
7852c58f1c22SToby Isaac     next = next->next;
7853c58f1c22SToby Isaac   }
7854c58f1c22SToby Isaac   PetscFunctionReturn(0);
7855c58f1c22SToby Isaac }
7856c58f1c22SToby Isaac 
7857c58f1c22SToby Isaac /*@C
7858c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7859c58f1c22SToby Isaac 
7860c58f1c22SToby Isaac   Not Collective
7861c58f1c22SToby Isaac 
7862c58f1c22SToby Isaac   Input Parameters:
7863c58f1c22SToby Isaac + dm - The DM object
7864c58f1c22SToby Isaac - n  - the label number
7865c58f1c22SToby Isaac 
7866c58f1c22SToby Isaac   Output Parameter:
7867c58f1c22SToby Isaac . label - the label
7868c58f1c22SToby Isaac 
7869c58f1c22SToby Isaac   Level: intermediate
7870c58f1c22SToby Isaac 
7871c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7872c58f1c22SToby Isaac @*/
7873c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7874c58f1c22SToby Isaac {
78755d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7876c58f1c22SToby Isaac   PetscInt    l    = 0;
7877c58f1c22SToby Isaac 
7878c58f1c22SToby Isaac   PetscFunctionBegin;
7879c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7880c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7881c58f1c22SToby Isaac   while (next) {
7882c58f1c22SToby Isaac     if (l == n) {
7883c58f1c22SToby Isaac       *label = next->label;
7884c58f1c22SToby Isaac       PetscFunctionReturn(0);
7885c58f1c22SToby Isaac     }
7886c58f1c22SToby Isaac     ++l;
7887c58f1c22SToby Isaac     next = next->next;
7888c58f1c22SToby Isaac   }
7889c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7890c58f1c22SToby Isaac }
7891c58f1c22SToby Isaac 
7892c58f1c22SToby Isaac /*@C
7893c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7894c58f1c22SToby Isaac 
7895c58f1c22SToby Isaac   Not Collective
7896c58f1c22SToby Isaac 
7897c58f1c22SToby Isaac   Input Parameters:
7898c58f1c22SToby Isaac + dm   - The DM object
7899c58f1c22SToby Isaac - label - The DMLabel
7900c58f1c22SToby Isaac 
7901c58f1c22SToby Isaac   Level: developer
7902c58f1c22SToby Isaac 
7903c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7904c58f1c22SToby Isaac @*/
7905c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7906c58f1c22SToby Isaac {
79075d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7908c58f1c22SToby Isaac   PetscBool      hasLabel;
7909d67d17b1SMatthew G. Knepley   const char    *lname;
79105d80c0bfSVaclav Hapla   PetscBool      flg;
7911c58f1c22SToby Isaac   PetscErrorCode ierr;
7912c58f1c22SToby Isaac 
7913c58f1c22SToby Isaac   PetscFunctionBegin;
7914c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7915d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7916d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7917d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7918c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7919c58f1c22SToby Isaac   tmpLabel->label  = label;
7920c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
79215d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
79225d80c0bfSVaclav Hapla   *p = tmpLabel;
792308f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
79245d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
79255d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7926ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7927ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7928c58f1c22SToby Isaac   PetscFunctionReturn(0);
7929c58f1c22SToby Isaac }
7930c58f1c22SToby Isaac 
7931c58f1c22SToby Isaac /*@C
79324a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
79334a7ee7d0SMatthew G. Knepley 
79344a7ee7d0SMatthew G. Knepley   Not Collective
79354a7ee7d0SMatthew G. Knepley 
79364a7ee7d0SMatthew G. Knepley   Input Parameters:
79374a7ee7d0SMatthew G. Knepley + dm    - The DM object
79384a7ee7d0SMatthew G. Knepley - label - The DMLabel, having the same name, to substitute
79394a7ee7d0SMatthew G. Knepley 
79404a7ee7d0SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
79414a7ee7d0SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
79424a7ee7d0SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
79434a7ee7d0SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
79444a7ee7d0SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
79454a7ee7d0SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
79464a7ee7d0SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
79474a7ee7d0SMatthew G. Knepley 
79484a7ee7d0SMatthew G. Knepley   Level: intermediate
79494a7ee7d0SMatthew G. Knepley 
79504a7ee7d0SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
79514a7ee7d0SMatthew G. Knepley @*/
79524a7ee7d0SMatthew G. Knepley PetscErrorCode DMSetLabel(DM dm, DMLabel label)
79534a7ee7d0SMatthew G. Knepley {
79544a7ee7d0SMatthew G. Knepley   DMLabelLink    next = dm->labels;
79554a7ee7d0SMatthew G. Knepley   PetscBool      hasLabel, flg;
79564a7ee7d0SMatthew G. Knepley   const char    *name, *lname;
79574a7ee7d0SMatthew G. Knepley   PetscErrorCode ierr;
79584a7ee7d0SMatthew G. Knepley 
79594a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
79604a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79614a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
79624a7ee7d0SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &name);CHKERRQ(ierr);
79634a7ee7d0SMatthew G. Knepley   while (next) {
79644a7ee7d0SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
79654a7ee7d0SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
79664a7ee7d0SMatthew G. Knepley     if (hasLabel) {
79674a7ee7d0SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
79684a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
79694a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
79704a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
79714a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
79724a7ee7d0SMatthew G. Knepley       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
79734a7ee7d0SMatthew G. Knepley       next->label = label;
79744a7ee7d0SMatthew G. Knepley       break;
79754a7ee7d0SMatthew G. Knepley     }
79764a7ee7d0SMatthew G. Knepley     next = next->next;
79774a7ee7d0SMatthew G. Knepley   }
79784a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
79794a7ee7d0SMatthew G. Knepley }
79804a7ee7d0SMatthew G. Knepley 
79814a7ee7d0SMatthew G. Knepley /*@C
7982e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7983c58f1c22SToby Isaac 
7984c58f1c22SToby Isaac   Not Collective
7985c58f1c22SToby Isaac 
7986c58f1c22SToby Isaac   Input Parameters:
7987c58f1c22SToby Isaac + dm   - The DM object
7988c58f1c22SToby Isaac - name - The label name
7989c58f1c22SToby Isaac 
7990c58f1c22SToby Isaac   Output Parameter:
7991c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7992c58f1c22SToby Isaac 
7993c58f1c22SToby Isaac   Level: developer
7994c58f1c22SToby Isaac 
7995e5472504SVaclav Hapla   Notes:
7996e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7997e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7998e5472504SVaclav Hapla 
7999e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
8000e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
8001e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
8002e5472504SVaclav Hapla 
8003e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
8004c58f1c22SToby Isaac @*/
8005c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
8006c58f1c22SToby Isaac {
800795d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
8008c58f1c22SToby Isaac   PetscBool      hasLabel;
8009d67d17b1SMatthew G. Knepley   const char    *lname;
8010c58f1c22SToby Isaac   PetscErrorCode ierr;
8011c58f1c22SToby Isaac 
8012c58f1c22SToby Isaac   PetscFunctionBegin;
8013c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8014e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
8015e5472504SVaclav Hapla   if (label) {
8016e5472504SVaclav Hapla     PetscValidPointer(label, 3);
8017c58f1c22SToby Isaac     *label = NULL;
8018e5472504SVaclav Hapla   }
80195d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
802095d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
8021d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
8022c58f1c22SToby Isaac     if (hasLabel) {
802395d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
8024c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
802595d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
8026ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
8027ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
802895d578d6SVaclav Hapla       if (label) *label = link->label;
802995d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
803095d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8031c58f1c22SToby Isaac       break;
8032c58f1c22SToby Isaac     }
8033c58f1c22SToby Isaac   }
8034c58f1c22SToby Isaac   PetscFunctionReturn(0);
8035c58f1c22SToby Isaac }
8036c58f1c22SToby Isaac 
8037306894acSVaclav Hapla /*@
8038306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
8039306894acSVaclav Hapla 
8040306894acSVaclav Hapla   Not Collective
8041306894acSVaclav Hapla 
8042306894acSVaclav Hapla   Input Parameters:
8043306894acSVaclav Hapla + dm   - The DM object
8044306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
8045306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
8046306894acSVaclav Hapla 
8047306894acSVaclav Hapla   Level: developer
8048306894acSVaclav Hapla 
8049306894acSVaclav Hapla   Notes:
8050306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
8051306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
8052306894acSVaclav Hapla   *label nullified.
8053306894acSVaclav Hapla 
8054306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
8055306894acSVaclav Hapla @*/
8056306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
8057306894acSVaclav Hapla {
805843e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
8059306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
8060306894acSVaclav Hapla   PetscErrorCode ierr;
8061306894acSVaclav Hapla 
8062306894acSVaclav Hapla   PetscFunctionBegin;
8063306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8064306894acSVaclav Hapla   PetscValidPointer(label, 2);
8065f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
8066306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
8067306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
80685d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
806943e45a93SVaclav Hapla     if (*label == link->label) {
8070306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
807143e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
8072306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
8073ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
807443e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
807543e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
807643e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8077306894acSVaclav Hapla       break;
8078306894acSVaclav Hapla     }
8079306894acSVaclav Hapla   }
8080306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
8081306894acSVaclav Hapla   PetscFunctionReturn(0);
8082306894acSVaclav Hapla }
8083306894acSVaclav Hapla 
8084c58f1c22SToby Isaac /*@C
8085c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
8086c58f1c22SToby Isaac 
8087c58f1c22SToby Isaac   Not Collective
8088c58f1c22SToby Isaac 
8089c58f1c22SToby Isaac   Input Parameters:
8090c58f1c22SToby Isaac + dm   - The DM object
8091c58f1c22SToby Isaac - name - The label name
8092c58f1c22SToby Isaac 
8093c58f1c22SToby Isaac   Output Parameter:
8094c58f1c22SToby Isaac . output - The flag for output
8095c58f1c22SToby Isaac 
8096c58f1c22SToby Isaac   Level: developer
8097c58f1c22SToby Isaac 
8098c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8099c58f1c22SToby Isaac @*/
8100c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
8101c58f1c22SToby Isaac {
81025d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8103d67d17b1SMatthew G. Knepley   const char    *lname;
8104c58f1c22SToby Isaac   PetscErrorCode ierr;
8105c58f1c22SToby Isaac 
8106c58f1c22SToby Isaac   PetscFunctionBegin;
8107c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8108c58f1c22SToby Isaac   PetscValidPointer(name, 2);
8109c58f1c22SToby Isaac   PetscValidPointer(output, 3);
8110c58f1c22SToby Isaac   while (next) {
8111c58f1c22SToby Isaac     PetscBool flg;
8112c58f1c22SToby Isaac 
8113d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8114d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8115c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
8116c58f1c22SToby Isaac     next = next->next;
8117c58f1c22SToby Isaac   }
8118c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8119c58f1c22SToby Isaac }
8120c58f1c22SToby Isaac 
8121c58f1c22SToby Isaac /*@C
8122c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
8123c58f1c22SToby Isaac 
8124c58f1c22SToby Isaac   Not Collective
8125c58f1c22SToby Isaac 
8126c58f1c22SToby Isaac   Input Parameters:
8127c58f1c22SToby Isaac + dm     - The DM object
8128c58f1c22SToby Isaac . name   - The label name
8129c58f1c22SToby Isaac - output - The flag for output
8130c58f1c22SToby Isaac 
8131c58f1c22SToby Isaac   Level: developer
8132c58f1c22SToby Isaac 
8133c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8134c58f1c22SToby Isaac @*/
8135c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
8136c58f1c22SToby Isaac {
81375d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8138d67d17b1SMatthew G. Knepley   const char    *lname;
8139c58f1c22SToby Isaac   PetscErrorCode ierr;
8140c58f1c22SToby Isaac 
8141c58f1c22SToby Isaac   PetscFunctionBegin;
8142c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8143534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
8144c58f1c22SToby Isaac   while (next) {
8145c58f1c22SToby Isaac     PetscBool flg;
8146c58f1c22SToby Isaac 
8147d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8148d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8149c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
8150c58f1c22SToby Isaac     next = next->next;
8151c58f1c22SToby Isaac   }
8152c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8153c58f1c22SToby Isaac }
8154c58f1c22SToby Isaac 
8155c58f1c22SToby Isaac /*@
8156c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
8157c58f1c22SToby Isaac 
8158d083f849SBarry Smith   Collective on dmA
8159c58f1c22SToby Isaac 
8160d8d19677SJose E. Roman   Input Parameters:
81615d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
8162*2cbb9b06SVaclav Hapla . dmB - The DM object to which labels are copied
81635d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
8164*2cbb9b06SVaclav Hapla . all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
8165*2cbb9b06SVaclav Hapla - emode - How to behave when a DMLabel in the source and destination DMs with the same name is encountered (see DMCopyLabelsMode)
8166c58f1c22SToby Isaac 
8167c58f1c22SToby Isaac   Level: intermediate
8168c58f1c22SToby Isaac 
8169*2cbb9b06SVaclav Hapla   Notes:
8170*2cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
8171c58f1c22SToby Isaac 
8172*2cbb9b06SVaclav Hapla .seealso: DMAddLabel(), DMCopyLabelsMode
8173c58f1c22SToby Isaac @*/
8174*2cbb9b06SVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
8175c58f1c22SToby Isaac {
8176*2cbb9b06SVaclav Hapla   DMLabel        label, labelNew, labelOld;
8177c58f1c22SToby Isaac   const char    *name;
8178c58f1c22SToby Isaac   PetscBool      flg;
81795d80c0bfSVaclav Hapla   DMLabelLink    link;
81805d80c0bfSVaclav Hapla   PetscErrorCode ierr;
8181c58f1c22SToby Isaac 
81825d80c0bfSVaclav Hapla   PetscFunctionBegin;
81835d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
81845d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
81855d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
81865d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
81875d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
81885d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
81895d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
81905d80c0bfSVaclav Hapla     label=link->label;
81915d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
81925d80c0bfSVaclav Hapla     if (!all) {
8193c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
8194c58f1c22SToby Isaac       if (flg) continue;
81957d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
81967d5acc75SStefano Zampini       if (flg) continue;
8197ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
8198ba2698f1SMatthew G. Knepley       if (flg) continue;
81995d80c0bfSVaclav Hapla     }
8200*2cbb9b06SVaclav Hapla     ierr = DMGetLabel(dmB, name, &labelOld);CHKERRQ(ierr);
8201*2cbb9b06SVaclav Hapla     if (labelOld) {
8202*2cbb9b06SVaclav Hapla       switch (emode) {
8203*2cbb9b06SVaclav Hapla         case DM_COPY_LABELS_KEEP:
8204*2cbb9b06SVaclav Hapla           continue;
8205*2cbb9b06SVaclav Hapla         case DM_COPY_LABELS_REPLACE:
8206*2cbb9b06SVaclav Hapla           ierr = DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE);CHKERRQ(ierr);
8207*2cbb9b06SVaclav Hapla           break;
8208*2cbb9b06SVaclav Hapla         case DM_COPY_LABELS_FAIL:
8209*2cbb9b06SVaclav Hapla           SETERRQ1(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
8210*2cbb9b06SVaclav Hapla         default:
8211*2cbb9b06SVaclav Hapla           SETERRQ1(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", emode);
8212*2cbb9b06SVaclav Hapla       }
8213*2cbb9b06SVaclav Hapla     }
82145d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
8215c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
82165d80c0bfSVaclav Hapla     } else {
82175d80c0bfSVaclav Hapla       labelNew = label;
82185d80c0bfSVaclav Hapla     }
8219c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
82205d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
8221c58f1c22SToby Isaac   }
8222c58f1c22SToby Isaac   PetscFunctionReturn(0);
8223c58f1c22SToby Isaac }
8224461a15a0SLisandro Dalcin 
8225609dae6eSVaclav Hapla /*@C
8226609dae6eSVaclav Hapla   DMCompareLabels - Compare labels of two DMPlex meshes
8227609dae6eSVaclav Hapla 
8228609dae6eSVaclav Hapla   Collective on dmA
8229609dae6eSVaclav Hapla 
8230609dae6eSVaclav Hapla   Input Parameters:
8231609dae6eSVaclav Hapla + dm0 - First DM object
8232609dae6eSVaclav Hapla - dm1 - Second DM object
8233609dae6eSVaclav Hapla 
8234609dae6eSVaclav Hapla   Output Parameters
8235609dae6eSVaclav Hapla + same    - (Optional) Flag whether labels of dm0 and dm1 are the same
8236609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
8237609dae6eSVaclav Hapla 
8238609dae6eSVaclav Hapla   Level: intermediate
8239609dae6eSVaclav Hapla 
8240609dae6eSVaclav Hapla   Notes:
8241609dae6eSVaclav Hapla   If both same and message is passed as NULL, and difference is found, an error is thrown with a message describing the difference.
8242609dae6eSVaclav Hapla 
8243609dae6eSVaclav Hapla   Message must be freed by user.
8244609dae6eSVaclav Hapla 
8245609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
8246609dae6eSVaclav Hapla   DMLabelCompare() is used to compare each pair of labels with the same name.
8247609dae6eSVaclav Hapla 
8248609dae6eSVaclav Hapla   Fortran Notes:
8249609dae6eSVaclav Hapla   This function is currently not available from Fortran.
8250609dae6eSVaclav Hapla 
8251609dae6eSVaclav Hapla .seealso: DMAddLabel(), DMCopyLabelsMode, DMLabelCompare()
8252609dae6eSVaclav Hapla @*/
8253609dae6eSVaclav Hapla PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *same, char **message)
8254609dae6eSVaclav Hapla {
8255609dae6eSVaclav Hapla   PetscInt        n0, n1, i;
8256609dae6eSVaclav Hapla   char            msg[PETSC_MAX_PATH_LEN] = "";
8257609dae6eSVaclav Hapla   MPI_Comm        comm;
8258609dae6eSVaclav Hapla   PetscErrorCode  ierr;
8259609dae6eSVaclav Hapla 
8260609dae6eSVaclav Hapla   PetscFunctionBegin;
8261609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0,DM_CLASSID,1);
8262609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1,DM_CLASSID,2);
8263609dae6eSVaclav Hapla   PetscCheckSameComm(dm0,1,dm1,2);
8264609dae6eSVaclav Hapla   if (same) PetscValidBoolPointer(same,3);
8265609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
8266609dae6eSVaclav Hapla   ierr = PetscObjectGetComm((PetscObject)dm0, &comm);CHKERRQ(ierr);
8267609dae6eSVaclav Hapla   ierr = DMGetNumLabels(dm0, &n0);CHKERRQ(ierr);
8268609dae6eSVaclav Hapla   ierr = DMGetNumLabels(dm1, &n1);CHKERRQ(ierr);
8269609dae6eSVaclav Hapla   if (n0 != n1) {
8270609dae6eSVaclav Hapla     ierr = PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %D != %D = Number of labels in dm1", n0, n1);CHKERRQ(ierr);
8271609dae6eSVaclav Hapla     goto finish;
8272609dae6eSVaclav Hapla   }
8273609dae6eSVaclav Hapla   for (i=0; i<n0; i++) {
8274609dae6eSVaclav Hapla     DMLabel     l0, l1;
8275609dae6eSVaclav Hapla     const char *name;
8276609dae6eSVaclav Hapla     char       *msgInner;
8277609dae6eSVaclav Hapla 
8278609dae6eSVaclav Hapla     /* Ignore label order */
8279609dae6eSVaclav Hapla     ierr = DMGetLabelByNum(dm0, i, &l0);CHKERRQ(ierr);
8280609dae6eSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)l0, &name);CHKERRQ(ierr);
8281609dae6eSVaclav Hapla     ierr = DMGetLabel(dm1, name, &l1);CHKERRQ(ierr);
8282609dae6eSVaclav Hapla     if (!l1) {
8283609dae6eSVaclav Hapla       ierr = PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%D in dm0) not found in dm1", name, i);CHKERRQ(ierr);
8284609dae6eSVaclav Hapla       goto finish;
8285609dae6eSVaclav Hapla     }
8286609dae6eSVaclav Hapla     ierr = DMLabelCompare(l0, l1, NULL, &msgInner);CHKERRQ(ierr);
8287609dae6eSVaclav Hapla     ierr = PetscStrncpy(msg, msgInner, sizeof(msg));CHKERRQ(ierr);
8288609dae6eSVaclav Hapla     ierr = PetscFree(msgInner);CHKERRQ(ierr);
8289609dae6eSVaclav Hapla     if (msg[0]) goto finish;
8290609dae6eSVaclav Hapla   }
8291609dae6eSVaclav Hapla finish:
8292609dae6eSVaclav Hapla   if (msg[0] && !same && !message) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, msg);
8293609dae6eSVaclav Hapla   if (same) *same = (PetscBool) !msg[0];
8294609dae6eSVaclav Hapla   if (message) {
8295609dae6eSVaclav Hapla     *message = NULL;
8296609dae6eSVaclav Hapla     if (msg[0]) {
8297609dae6eSVaclav Hapla       ierr = PetscStrallocpy(msg, message);CHKERRQ(ierr);
8298609dae6eSVaclav Hapla     }
8299609dae6eSVaclav Hapla   }
8300609dae6eSVaclav Hapla   PetscFunctionReturn(0);
8301609dae6eSVaclav Hapla }
8302609dae6eSVaclav Hapla 
8303461a15a0SLisandro Dalcin PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
8304461a15a0SLisandro Dalcin {
8305461a15a0SLisandro Dalcin   PetscErrorCode ierr;
8306609dae6eSVaclav Hapla 
8307461a15a0SLisandro Dalcin   PetscFunctionBegin;
8308461a15a0SLisandro Dalcin   PetscValidPointer(label,2);
8309461a15a0SLisandro Dalcin   if (!*label) {
8310461a15a0SLisandro Dalcin     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
8311461a15a0SLisandro Dalcin     ierr = DMGetLabel(dm, name, label);CHKERRQ(ierr);
8312461a15a0SLisandro Dalcin   }
8313461a15a0SLisandro Dalcin   ierr = DMLabelSetValue(*label, point, value);CHKERRQ(ierr);
8314461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
8315461a15a0SLisandro Dalcin }
8316461a15a0SLisandro Dalcin 
83170fdc7489SMatthew Knepley /*
83180fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
83190fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
83200fdc7489SMatthew Knepley   (label, id) pair in the DM.
83210fdc7489SMatthew Knepley 
83220fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
83230fdc7489SMatthew Knepley   each label.
83240fdc7489SMatthew Knepley */
83250fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
83260fdc7489SMatthew Knepley {
83270fdc7489SMatthew Knepley   DMUniversalLabel ul;
83280fdc7489SMatthew Knepley   PetscBool       *active;
83290fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
83300fdc7489SMatthew Knepley   PetscErrorCode   ierr;
83310fdc7489SMatthew Knepley 
83320fdc7489SMatthew Knepley   PetscFunctionBegin;
83330fdc7489SMatthew Knepley   ierr = PetscMalloc1(1, &ul);CHKERRQ(ierr);
83340fdc7489SMatthew Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label);CHKERRQ(ierr);
83350fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
83360fdc7489SMatthew Knepley   ierr = PetscCalloc1(Nl, &active);CHKERRQ(ierr);
83370fdc7489SMatthew Knepley   ul->Nl = 0;
83380fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
83390fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
83400fdc7489SMatthew Knepley     const char *name;
83410fdc7489SMatthew Knepley 
83420fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
83430fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "depth", 6, &isdepth);CHKERRQ(ierr);
83440fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "celltype", 9, &iscelltype);CHKERRQ(ierr);
83450fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
83460fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
83470fdc7489SMatthew Knepley   }
83480fdc7489SMatthew 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);
83490fdc7489SMatthew Knepley   ul->Nv = 0;
83500fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
83510fdc7489SMatthew Knepley     DMLabel     label;
83520fdc7489SMatthew Knepley     PetscInt    nv;
83530fdc7489SMatthew Knepley     const char *name;
83540fdc7489SMatthew Knepley 
83550fdc7489SMatthew Knepley     if (!active[l]) continue;
83560fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
83570fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
83580fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
83590fdc7489SMatthew Knepley     ierr = PetscStrallocpy(name, &ul->names[m]);CHKERRQ(ierr);
83600fdc7489SMatthew Knepley     ul->indices[m]   = l;
83610fdc7489SMatthew Knepley     ul->Nv          += nv;
83620fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
83630fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
83640fdc7489SMatthew Knepley     ++m;
83650fdc7489SMatthew Knepley   }
83660fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
83670fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
83680fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
83690fdc7489SMatthew Knepley   }
83700fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
83710fdc7489SMatthew Knepley     PetscInt b;
83720fdc7489SMatthew Knepley 
83730fdc7489SMatthew Knepley     ul->masks[l] = 0;
83740fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
83750fdc7489SMatthew Knepley   }
83760fdc7489SMatthew Knepley   ierr = PetscMalloc1(ul->Nv, &ul->values);CHKERRQ(ierr);
83770fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
83780fdc7489SMatthew Knepley     DMLabel         label;
83790fdc7489SMatthew Knepley     IS              valueIS;
83800fdc7489SMatthew Knepley     const PetscInt *varr;
83810fdc7489SMatthew Knepley     PetscInt        nv, v;
83820fdc7489SMatthew Knepley 
83830fdc7489SMatthew Knepley     if (!active[l]) continue;
83840fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
83850fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
83860fdc7489SMatthew Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
83870fdc7489SMatthew Knepley     ierr = ISGetIndices(valueIS, &varr);CHKERRQ(ierr);
83880fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
83890fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
83900fdc7489SMatthew Knepley     }
83910fdc7489SMatthew Knepley     ierr = ISRestoreIndices(valueIS, &varr);CHKERRQ(ierr);
83920fdc7489SMatthew Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
83930fdc7489SMatthew Knepley     ierr = PetscSortInt(nv, &ul->values[ul->offsets[m]]);CHKERRQ(ierr);
83940fdc7489SMatthew Knepley     ++m;
83950fdc7489SMatthew Knepley   }
83960fdc7489SMatthew Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
83970fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
83980fdc7489SMatthew Knepley     PetscInt  uval = 0;
83990fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
84000fdc7489SMatthew Knepley 
84010fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
84020fdc7489SMatthew Knepley       DMLabel  label;
84030649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
84040fdc7489SMatthew Knepley 
84050fdc7489SMatthew Knepley       if (!active[l]) continue;
84060fdc7489SMatthew Knepley       ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
84070fdc7489SMatthew Knepley       ierr = DMLabelGetValue(label, p, &val);CHKERRQ(ierr);
84080fdc7489SMatthew Knepley       ierr = DMLabelGetDefaultValue(label, &defval);CHKERRQ(ierr);
84090fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
84100649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
84110fdc7489SMatthew Knepley       marked = PETSC_TRUE;
84120fdc7489SMatthew Knepley       ierr = PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc);CHKERRQ(ierr);
84130fdc7489SMatthew Knepley       if (loc < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %D not found in compression array", val);
84140fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
84150fdc7489SMatthew Knepley       ++m;
84160fdc7489SMatthew Knepley     }
84170fdc7489SMatthew Knepley     if (marked) {ierr = DMLabelSetValue(ul->label, p, uval);CHKERRQ(ierr);}
84180fdc7489SMatthew Knepley   }
84190fdc7489SMatthew Knepley   ierr = PetscFree(active);CHKERRQ(ierr);
84200fdc7489SMatthew Knepley   *universal = ul;
84210fdc7489SMatthew Knepley   PetscFunctionReturn(0);
84220fdc7489SMatthew Knepley }
84230fdc7489SMatthew Knepley 
84240fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
84250fdc7489SMatthew Knepley {
84260fdc7489SMatthew Knepley   PetscInt       l;
84270fdc7489SMatthew Knepley   PetscErrorCode ierr;
84280fdc7489SMatthew Knepley 
84290fdc7489SMatthew Knepley   PetscFunctionBegin;
84300fdc7489SMatthew Knepley   for (l = 0; l < (*universal)->Nl; ++l) {ierr = PetscFree((*universal)->names[l]);CHKERRQ(ierr);}
84310fdc7489SMatthew Knepley   ierr = DMLabelDestroy(&(*universal)->label);CHKERRQ(ierr);
84320fdc7489SMatthew Knepley   ierr = PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks);CHKERRQ(ierr);
84330fdc7489SMatthew Knepley   ierr = PetscFree((*universal)->values);CHKERRQ(ierr);
84340fdc7489SMatthew Knepley   ierr = PetscFree(*universal);CHKERRQ(ierr);
84350fdc7489SMatthew Knepley   *universal = NULL;
84360fdc7489SMatthew Knepley   PetscFunctionReturn(0);
84370fdc7489SMatthew Knepley }
84380fdc7489SMatthew Knepley 
84390fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
84400fdc7489SMatthew Knepley {
84410fdc7489SMatthew Knepley   PetscFunctionBegin;
84420fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
84430fdc7489SMatthew Knepley   *ulabel = ul->label;
84440fdc7489SMatthew Knepley   PetscFunctionReturn(0);
84450fdc7489SMatthew Knepley }
84460fdc7489SMatthew Knepley 
84470fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
84480fdc7489SMatthew Knepley {
84490fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
84500fdc7489SMatthew Knepley   PetscErrorCode ierr;
84510fdc7489SMatthew Knepley 
84520fdc7489SMatthew Knepley   PetscFunctionBegin;
8453064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
84540fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
84550fdc7489SMatthew Knepley     if (preserveOrder) {ierr = DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]);CHKERRQ(ierr);}
84560fdc7489SMatthew Knepley     else               {ierr = DMCreateLabel(dm, ul->names[l]);CHKERRQ(ierr);}
84570fdc7489SMatthew Knepley   }
84580fdc7489SMatthew Knepley   if (preserveOrder) {
84590fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
84600fdc7489SMatthew Knepley       const char *name;
84610fdc7489SMatthew Knepley       PetscBool   match;
84620fdc7489SMatthew Knepley 
84630fdc7489SMatthew Knepley       ierr = DMGetLabelName(dm, ul->indices[l], &name);CHKERRQ(ierr);
84640fdc7489SMatthew Knepley       ierr = PetscStrcmp(name, ul->names[l], &match);CHKERRQ(ierr);
84650fdc7489SMatthew 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]);
84660fdc7489SMatthew Knepley     }
84670fdc7489SMatthew Knepley   }
84680fdc7489SMatthew Knepley   PetscFunctionReturn(0);
84690fdc7489SMatthew Knepley }
84700fdc7489SMatthew Knepley 
84710fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
84720fdc7489SMatthew Knepley {
84730fdc7489SMatthew Knepley   PetscInt       l;
84740fdc7489SMatthew Knepley   PetscErrorCode ierr;
84750fdc7489SMatthew Knepley 
84760fdc7489SMatthew Knepley   PetscFunctionBegin;
84770fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
84780fdc7489SMatthew Knepley     DMLabel  label;
84790fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
84800fdc7489SMatthew Knepley 
84810fdc7489SMatthew Knepley     if (lval) {
84820fdc7489SMatthew Knepley       if (useIndex) {ierr = DMGetLabelByNum(dm, ul->indices[l], &label);CHKERRQ(ierr);}
84830fdc7489SMatthew Knepley       else          {ierr = DMGetLabel(dm, ul->names[l], &label);CHKERRQ(ierr);}
84840fdc7489SMatthew Knepley       ierr = DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]);CHKERRQ(ierr);
84850fdc7489SMatthew Knepley     }
84860fdc7489SMatthew Knepley   }
84870fdc7489SMatthew Knepley   PetscFunctionReturn(0);
84880fdc7489SMatthew Knepley }
8489a8fb8f29SToby Isaac 
8490a8fb8f29SToby Isaac /*@
8491a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
8492a8fb8f29SToby Isaac 
8493a8fb8f29SToby Isaac   Input Parameter:
8494a8fb8f29SToby Isaac . dm - The DM object
8495a8fb8f29SToby Isaac 
8496a8fb8f29SToby Isaac   Output Parameter:
8497a8fb8f29SToby Isaac . cdm - The coarse DM
8498a8fb8f29SToby Isaac 
8499a8fb8f29SToby Isaac   Level: intermediate
8500a8fb8f29SToby Isaac 
8501a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
8502a8fb8f29SToby Isaac @*/
8503a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
8504a8fb8f29SToby Isaac {
8505a8fb8f29SToby Isaac   PetscFunctionBegin;
8506a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8507a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
8508a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
8509a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8510a8fb8f29SToby Isaac }
8511a8fb8f29SToby Isaac 
8512a8fb8f29SToby Isaac /*@
8513a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
8514a8fb8f29SToby Isaac 
8515a8fb8f29SToby Isaac   Input Parameters:
8516a8fb8f29SToby Isaac + dm - The DM object
8517a8fb8f29SToby Isaac - cdm - The coarse DM
8518a8fb8f29SToby Isaac 
8519a8fb8f29SToby Isaac   Level: intermediate
8520a8fb8f29SToby Isaac 
8521a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
8522a8fb8f29SToby Isaac @*/
8523a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
8524a8fb8f29SToby Isaac {
8525a8fb8f29SToby Isaac   PetscErrorCode ierr;
8526a8fb8f29SToby Isaac 
8527a8fb8f29SToby Isaac   PetscFunctionBegin;
8528a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8529a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
8530a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
8531a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
8532a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
8533a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8534a8fb8f29SToby Isaac }
8535a8fb8f29SToby Isaac 
853688bdff64SToby Isaac /*@
853788bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
853888bdff64SToby Isaac 
853988bdff64SToby Isaac   Input Parameter:
854088bdff64SToby Isaac . dm - The DM object
854188bdff64SToby Isaac 
854288bdff64SToby Isaac   Output Parameter:
854388bdff64SToby Isaac . fdm - The fine DM
854488bdff64SToby Isaac 
854588bdff64SToby Isaac   Level: intermediate
854688bdff64SToby Isaac 
854788bdff64SToby Isaac .seealso: DMSetFineDM()
854888bdff64SToby Isaac @*/
854988bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
855088bdff64SToby Isaac {
855188bdff64SToby Isaac   PetscFunctionBegin;
855288bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
855388bdff64SToby Isaac   PetscValidPointer(fdm, 2);
855488bdff64SToby Isaac   *fdm = dm->fineMesh;
855588bdff64SToby Isaac   PetscFunctionReturn(0);
855688bdff64SToby Isaac }
855788bdff64SToby Isaac 
855888bdff64SToby Isaac /*@
855988bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
856088bdff64SToby Isaac 
856188bdff64SToby Isaac   Input Parameters:
856288bdff64SToby Isaac + dm - The DM object
856388bdff64SToby Isaac - fdm - The fine DM
856488bdff64SToby Isaac 
856588bdff64SToby Isaac   Level: intermediate
856688bdff64SToby Isaac 
856788bdff64SToby Isaac .seealso: DMGetFineDM()
856888bdff64SToby Isaac @*/
856988bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
857088bdff64SToby Isaac {
857188bdff64SToby Isaac   PetscErrorCode ierr;
857288bdff64SToby Isaac 
857388bdff64SToby Isaac   PetscFunctionBegin;
857488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
857588bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
857688bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
857788bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
857888bdff64SToby Isaac   dm->fineMesh = fdm;
857988bdff64SToby Isaac   PetscFunctionReturn(0);
858088bdff64SToby Isaac }
858188bdff64SToby Isaac 
8582a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8583a6ba4734SToby Isaac 
8584a6ba4734SToby Isaac /*@C
8585a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8586a6ba4734SToby Isaac 
8587783e2ec8SMatthew G. Knepley   Collective on dm
8588783e2ec8SMatthew G. Knepley 
8589a6ba4734SToby Isaac   Input Parameters:
85904c258f51SMatthew G. Knepley + dm       - The DM, with a PetscDS that matches the problem being constrained
8591f971fd6bSMatthew G. Knepley . type     - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8592a6ba4734SToby Isaac . name     - The BC name
859345480ffeSMatthew G. Knepley . label    - The label defining constrained points
859445480ffeSMatthew G. Knepley . Nv       - The number of DMLabel values for constrained points
859545480ffeSMatthew G. Knepley . values   - An array of values for constrained points
8596a6ba4734SToby Isaac . field    - The field to constrain
859745480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
8598a6ba4734SToby Isaac . comps    - An array of constrained component numbers
8599a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
860056cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
8601a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
8602a6ba4734SToby Isaac 
860345480ffeSMatthew G. Knepley   Output Parameter:
860445480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
860545480ffeSMatthew G. Knepley 
8606a6ba4734SToby Isaac   Options Database Keys:
8607a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8608a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8609a6ba4734SToby Isaac 
861056cf3b9cSMatthew G. Knepley   Note:
861156cf3b9cSMatthew 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:
861256cf3b9cSMatthew G. Knepley 
861356cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
861456cf3b9cSMatthew G. Knepley 
861556cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
861656cf3b9cSMatthew G. Knepley 
861756cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
861856cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
861956cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
862056cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
862156cf3b9cSMatthew G. Knepley 
862256cf3b9cSMatthew G. Knepley + dim - the spatial dimension
862356cf3b9cSMatthew G. Knepley . Nf - the number of fields
862456cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
862556cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
862656cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
862756cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
862856cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
862956cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
863056cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
863156cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
863256cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
863356cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
863456cf3b9cSMatthew G. Knepley . t - current time
863556cf3b9cSMatthew G. Knepley . x - coordinates of the current point
863656cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
863756cf3b9cSMatthew G. Knepley . constants - constant parameters
863856cf3b9cSMatthew G. Knepley - bcval - output values at the current point
863956cf3b9cSMatthew G. Knepley 
8640a6ba4734SToby Isaac   Level: developer
8641a6ba4734SToby Isaac 
864245480ffeSMatthew G. Knepley .seealso: DSGetBoundary(), PetscDSAddBoundary()
8643a6ba4734SToby Isaac @*/
864445480ffeSMatthew 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)
8645a6ba4734SToby Isaac {
8646e5e52638SMatthew G. Knepley   PetscDS        ds;
8647a6ba4734SToby Isaac   PetscErrorCode ierr;
8648a6ba4734SToby Isaac 
8649a6ba4734SToby Isaac   PetscFunctionBegin;
8650a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8651783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
865245480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
865345480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
865445480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
865545480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
8656e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
865745480ffeSMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, label);CHKERRQ(ierr);
865845480ffeSMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd);CHKERRQ(ierr);
8659a6ba4734SToby Isaac   PetscFunctionReturn(0);
8660a6ba4734SToby Isaac }
8661a6ba4734SToby Isaac 
866245480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8663e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8664e6f8dbb6SToby Isaac {
8665e5e52638SMatthew G. Knepley   PetscDS        ds;
8666dff059c6SToby Isaac   DMBoundary    *lastnext;
8667e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8668e6f8dbb6SToby Isaac   PetscErrorCode ierr;
8669e6f8dbb6SToby Isaac 
8670e6f8dbb6SToby Isaac   PetscFunctionBegin;
8671e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8672e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
867347a1f5adSToby Isaac   if (dm->boundary) {
867447a1f5adSToby Isaac     DMBoundary next = dm->boundary;
867547a1f5adSToby Isaac 
867647a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
867747a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
867847a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
867947a1f5adSToby Isaac     while (next) {
868047a1f5adSToby Isaac       DMBoundary b = next;
868147a1f5adSToby Isaac 
868247a1f5adSToby Isaac       next = b->next;
868347a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
8684a6ba4734SToby Isaac     }
868547a1f5adSToby Isaac     dm->boundary = NULL;
8686a6ba4734SToby Isaac   }
868747a1f5adSToby Isaac 
8688dff059c6SToby Isaac   lastnext = &(dm->boundary);
8689e6f8dbb6SToby Isaac   while (dsbound) {
8690e6f8dbb6SToby Isaac     DMBoundary dmbound;
8691e6f8dbb6SToby Isaac 
8692e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
8693e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
869445480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
869547a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8696dff059c6SToby Isaac     *lastnext = dmbound;
8697dff059c6SToby Isaac     lastnext = &(dmbound->next);
8698dff059c6SToby Isaac     dsbound = dsbound->next;
8699a6ba4734SToby Isaac   }
8700a6ba4734SToby Isaac   PetscFunctionReturn(0);
8701a6ba4734SToby Isaac }
8702a6ba4734SToby Isaac 
8703a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8704a6ba4734SToby Isaac {
8705b95f2879SToby Isaac   DMBoundary     b;
8706a6ba4734SToby Isaac   PetscErrorCode ierr;
8707a6ba4734SToby Isaac 
8708a6ba4734SToby Isaac   PetscFunctionBegin;
8709a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8710534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8711a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8712e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
8713b95f2879SToby Isaac   b = dm->boundary;
8714a6ba4734SToby Isaac   while (b && !(*isBd)) {
8715e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8716e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8717a6ba4734SToby Isaac     PetscInt   i;
8718a6ba4734SToby Isaac 
871945480ffeSMatthew G. Knepley     if (label) {
872045480ffeSMatthew G. Knepley       for (i = 0; i < dsb->Nv && !(*isBd); ++i) {ierr = DMLabelStratumHasPoint(label, dsb->values[i], point, isBd);CHKERRQ(ierr);}
8721a6ba4734SToby Isaac     }
8722a6ba4734SToby Isaac     b = b->next;
8723a6ba4734SToby Isaac   }
8724a6ba4734SToby Isaac   PetscFunctionReturn(0);
8725a6ba4734SToby Isaac }
87264d6f44ffSToby Isaac 
87274d6f44ffSToby Isaac /*@C
8728a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8729a6e0b375SMatthew G. Knepley 
8730a6e0b375SMatthew G. Knepley   Collective on DM
87314d6f44ffSToby Isaac 
87324d6f44ffSToby Isaac   Input Parameters:
87334d6f44ffSToby Isaac + dm      - The DM
87340709b2feSToby Isaac . time    - The time
87354d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
87364d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
87374d6f44ffSToby Isaac - mode    - The insertion mode for values
87384d6f44ffSToby Isaac 
87394d6f44ffSToby Isaac   Output Parameter:
87404d6f44ffSToby Isaac . X - vector
87414d6f44ffSToby Isaac 
87424d6f44ffSToby Isaac    Calling sequence of func:
87430709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
87444d6f44ffSToby Isaac 
87454d6f44ffSToby Isaac +  dim - The spatial dimension
87468ec8862eSJed Brown .  time - The time at which to sample
87474d6f44ffSToby Isaac .  x   - The coordinates
87484d6f44ffSToby Isaac .  Nf  - The number of fields
87494d6f44ffSToby Isaac .  u   - The output field values
87504d6f44ffSToby Isaac -  ctx - optional user-defined function context
87514d6f44ffSToby Isaac 
87524d6f44ffSToby Isaac   Level: developer
87534d6f44ffSToby Isaac 
8754a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
87554d6f44ffSToby Isaac @*/
87560709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
87574d6f44ffSToby Isaac {
87584d6f44ffSToby Isaac   Vec            localX;
87594d6f44ffSToby Isaac   PetscErrorCode ierr;
87604d6f44ffSToby Isaac 
87614d6f44ffSToby Isaac   PetscFunctionBegin;
87624d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87634d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
87640709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
87654d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
87664d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
87674d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
87684d6f44ffSToby Isaac   PetscFunctionReturn(0);
87694d6f44ffSToby Isaac }
87704d6f44ffSToby Isaac 
8771a6e0b375SMatthew G. Knepley /*@C
8772a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8773a6e0b375SMatthew G. Knepley 
8774a6e0b375SMatthew G. Knepley   Not collective
8775a6e0b375SMatthew G. Knepley 
8776a6e0b375SMatthew G. Knepley   Input Parameters:
8777a6e0b375SMatthew G. Knepley + dm      - The DM
8778a6e0b375SMatthew G. Knepley . time    - The time
8779a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8780a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8781a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8782a6e0b375SMatthew G. Knepley 
8783a6e0b375SMatthew G. Knepley   Output Parameter:
8784a6e0b375SMatthew G. Knepley . localX - vector
8785a6e0b375SMatthew G. Knepley 
8786a6e0b375SMatthew G. Knepley    Calling sequence of func:
8787a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8788a6e0b375SMatthew G. Knepley 
8789a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8790a6e0b375SMatthew G. Knepley .  x   - The coordinates
8791a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8792a6e0b375SMatthew G. Knepley .  u   - The output field values
8793a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8794a6e0b375SMatthew G. Knepley 
8795a6e0b375SMatthew G. Knepley   Level: developer
8796a6e0b375SMatthew G. Knepley 
8797a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8798a6e0b375SMatthew G. Knepley @*/
87990709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
88004d6f44ffSToby Isaac {
88014d6f44ffSToby Isaac   PetscErrorCode ierr;
88024d6f44ffSToby Isaac 
88034d6f44ffSToby Isaac   PetscFunctionBegin;
88044d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8805064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
88060918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
88070709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
88084d6f44ffSToby Isaac   PetscFunctionReturn(0);
88094d6f44ffSToby Isaac }
88104d6f44ffSToby Isaac 
8811a6e0b375SMatthew G. Knepley /*@C
8812a6e0b375SMatthew 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.
8813a6e0b375SMatthew G. Knepley 
8814a6e0b375SMatthew G. Knepley   Collective on DM
8815a6e0b375SMatthew G. Knepley 
8816a6e0b375SMatthew G. Knepley   Input Parameters:
8817a6e0b375SMatthew G. Knepley + dm      - The DM
8818a6e0b375SMatthew G. Knepley . time    - The time
8819a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8820a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8821a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8822a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8823a6e0b375SMatthew G. Knepley 
8824a6e0b375SMatthew G. Knepley   Output Parameter:
8825a6e0b375SMatthew G. Knepley . X - vector
8826a6e0b375SMatthew G. Knepley 
8827a6e0b375SMatthew G. Knepley    Calling sequence of func:
8828a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8829a6e0b375SMatthew G. Knepley 
8830a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8831a6e0b375SMatthew G. Knepley .  x   - The coordinates
8832a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8833a6e0b375SMatthew G. Knepley .  u   - The output field values
8834a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8835a6e0b375SMatthew G. Knepley 
8836a6e0b375SMatthew G. Knepley   Level: developer
8837a6e0b375SMatthew G. Knepley 
8838a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8839a6e0b375SMatthew G. Knepley @*/
88402c53366bSMatthew 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)
88412c53366bSMatthew G. Knepley {
88422c53366bSMatthew G. Knepley   Vec            localX;
88432c53366bSMatthew G. Knepley   PetscErrorCode ierr;
88442c53366bSMatthew G. Knepley 
88452c53366bSMatthew G. Knepley   PetscFunctionBegin;
88462c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88472c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
88482c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
88492c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
88502c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
88512c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
88522c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
88532c53366bSMatthew G. Knepley }
88542c53366bSMatthew G. Knepley 
8855a6e0b375SMatthew G. Knepley /*@C
8856a6e0b375SMatthew 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.
8857a6e0b375SMatthew G. Knepley 
8858a6e0b375SMatthew G. Knepley   Not collective
8859a6e0b375SMatthew G. Knepley 
8860a6e0b375SMatthew G. Knepley   Input Parameters:
8861a6e0b375SMatthew G. Knepley + dm      - The DM
8862a6e0b375SMatthew G. Knepley . time    - The time
8863a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8864a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8865a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8866a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8867a6e0b375SMatthew G. Knepley 
8868a6e0b375SMatthew G. Knepley   Output Parameter:
8869a6e0b375SMatthew G. Knepley . localX - vector
8870a6e0b375SMatthew G. Knepley 
8871a6e0b375SMatthew G. Knepley    Calling sequence of func:
8872a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8873a6e0b375SMatthew G. Knepley 
8874a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8875a6e0b375SMatthew G. Knepley .  x   - The coordinates
8876a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8877a6e0b375SMatthew G. Knepley .  u   - The output field values
8878a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8879a6e0b375SMatthew G. Knepley 
8880a6e0b375SMatthew G. Knepley   Level: developer
8881a6e0b375SMatthew G. Knepley 
8882a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8883a6e0b375SMatthew G. Knepley @*/
88841c531cf8SMatthew 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)
88854d6f44ffSToby Isaac {
88864d6f44ffSToby Isaac   PetscErrorCode ierr;
88874d6f44ffSToby Isaac 
88884d6f44ffSToby Isaac   PetscFunctionBegin;
88894d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8890064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
88910918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
88921c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
88934d6f44ffSToby Isaac   PetscFunctionReturn(0);
88944d6f44ffSToby Isaac }
88952716604bSToby Isaac 
8896a6e0b375SMatthew G. Knepley /*@C
8897a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8898a6e0b375SMatthew G. Knepley 
8899a6e0b375SMatthew G. Knepley   Not collective
8900a6e0b375SMatthew G. Knepley 
8901a6e0b375SMatthew G. Knepley   Input Parameters:
8902a6e0b375SMatthew G. Knepley + dm      - The DM
8903a6e0b375SMatthew G. Knepley . time    - The time
8904a6e0b375SMatthew G. Knepley . localU  - The input field vector
8905a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8906a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8907a6e0b375SMatthew G. Knepley 
8908a6e0b375SMatthew G. Knepley   Output Parameter:
8909a6e0b375SMatthew G. Knepley . localX  - The output vector
8910a6e0b375SMatthew G. Knepley 
8911a6e0b375SMatthew G. Knepley    Calling sequence of func:
8912a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8913a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8914a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8915a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8916a6e0b375SMatthew G. Knepley 
8917a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8918a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8919a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8920a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8921a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8922a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8923a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8924a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8925a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8926a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8927a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8928a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8929a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8930a6e0b375SMatthew G. Knepley .  t            - The current time
8931a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8932a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8933a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8934a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8935a6e0b375SMatthew G. Knepley 
8936a6e0b375SMatthew 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.
8937a6e0b375SMatthew 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
8938a6e0b375SMatthew 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
8939a6e0b375SMatthew 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.
8940a6e0b375SMatthew G. Knepley 
8941a6e0b375SMatthew G. Knepley   Level: intermediate
8942a6e0b375SMatthew G. Knepley 
8943a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8944a6e0b375SMatthew G. Knepley @*/
89458c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
89468c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
89478c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
89488c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8949191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
89508c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
89518c6c5593SMatthew G. Knepley {
89528c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
89538c6c5593SMatthew G. Knepley 
89548c6c5593SMatthew G. Knepley   PetscFunctionBegin;
89558c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
89568c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
89578c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
89580918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
89598c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
89608c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
89618c6c5593SMatthew G. Knepley }
89628c6c5593SMatthew G. Knepley 
8963a6e0b375SMatthew G. Knepley /*@C
8964a6e0b375SMatthew 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.
8965a6e0b375SMatthew G. Knepley 
8966a6e0b375SMatthew G. Knepley   Not collective
8967a6e0b375SMatthew G. Knepley 
8968a6e0b375SMatthew G. Knepley   Input Parameters:
8969a6e0b375SMatthew G. Knepley + dm      - The DM
8970a6e0b375SMatthew G. Knepley . time    - The time
8971a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
8972a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8973a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8974a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8975a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8976a6e0b375SMatthew G. Knepley . localU  - The input field vector
8977a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8978a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8979a6e0b375SMatthew G. Knepley 
8980a6e0b375SMatthew G. Knepley   Output Parameter:
8981a6e0b375SMatthew G. Knepley . localX  - The output vector
8982a6e0b375SMatthew G. Knepley 
8983a6e0b375SMatthew G. Knepley    Calling sequence of func:
8984a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8985a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8986a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8987a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8988a6e0b375SMatthew G. Knepley 
8989a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8990a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8991a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8992a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8993a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8994a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8995a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8996a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8997a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8998a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8999a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9000a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9001a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9002a6e0b375SMatthew G. Knepley .  t            - The current time
9003a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9004a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9005a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9006a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9007a6e0b375SMatthew G. Knepley 
9008a6e0b375SMatthew 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.
9009a6e0b375SMatthew 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
9010a6e0b375SMatthew 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
9011a6e0b375SMatthew 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.
9012a6e0b375SMatthew G. Knepley 
9013a6e0b375SMatthew G. Knepley   Level: intermediate
9014a6e0b375SMatthew G. Knepley 
9015a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9016a6e0b375SMatthew G. Knepley @*/
90171c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
90188c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
90198c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
90208c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9021191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
90228c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
90238c6c5593SMatthew G. Knepley {
90248c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
90258c6c5593SMatthew G. Knepley 
90268c6c5593SMatthew G. Knepley   PetscFunctionBegin;
90278c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9028064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9029064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
9030ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
90311c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
90328c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
90338c6c5593SMatthew G. Knepley }
90348c6c5593SMatthew G. Knepley 
90352716604bSToby Isaac /*@C
9036ece3a9fcSMatthew 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.
9037ece3a9fcSMatthew G. Knepley 
9038ece3a9fcSMatthew G. Knepley   Not collective
9039ece3a9fcSMatthew G. Knepley 
9040ece3a9fcSMatthew G. Knepley   Input Parameters:
9041ece3a9fcSMatthew G. Knepley + dm      - The DM
9042ece3a9fcSMatthew G. Knepley . time    - The time
9043ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
9044ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
9045ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
9046ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9047ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9048ece3a9fcSMatthew G. Knepley . localU  - The input field vector
9049ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9050ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
9051ece3a9fcSMatthew G. Knepley 
9052ece3a9fcSMatthew G. Knepley   Output Parameter:
9053ece3a9fcSMatthew G. Knepley . localX  - The output vector
9054ece3a9fcSMatthew G. Knepley 
9055ece3a9fcSMatthew G. Knepley    Calling sequence of func:
9056ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9057ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9058ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9059ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9060ece3a9fcSMatthew G. Knepley 
9061ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
9062ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
9063ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9064ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
9065ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9066ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
9067ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9068ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9069ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9070ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9071ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9072ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9073ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9074ece3a9fcSMatthew G. Knepley .  t            - The current time
9075ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
9076ece3a9fcSMatthew G. Knepley .  n            - The face normal
9077ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
9078ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
9079ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
9080ece3a9fcSMatthew G. Knepley 
9081ece3a9fcSMatthew G. Knepley   Note:
9082ece3a9fcSMatthew 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.
9083ece3a9fcSMatthew 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
9084ece3a9fcSMatthew 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
9085ece3a9fcSMatthew 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.
9086ece3a9fcSMatthew G. Knepley 
9087ece3a9fcSMatthew G. Knepley   Level: intermediate
9088ece3a9fcSMatthew G. Knepley 
9089ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9090ece3a9fcSMatthew G. Knepley @*/
9091ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
9092ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
9093ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9094ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9095ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
9096ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
9097ece3a9fcSMatthew G. Knepley {
9098ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
9099ece3a9fcSMatthew G. Knepley 
9100ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
9101ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9102064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9103064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
9104ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
9105ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
9106ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
9107ece3a9fcSMatthew G. Knepley }
9108ece3a9fcSMatthew G. Knepley 
9109ece3a9fcSMatthew G. Knepley /*@C
91102716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
91112716604bSToby Isaac 
91122716604bSToby Isaac   Input Parameters:
91132716604bSToby Isaac + dm    - The DM
91140709b2feSToby Isaac . time  - The time
91152716604bSToby Isaac . funcs - The functions to evaluate for each field component
91162716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9117574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
91182716604bSToby Isaac 
91192716604bSToby Isaac   Output Parameter:
91202716604bSToby Isaac . diff - The diff ||u - u_h||_2
91212716604bSToby Isaac 
91222716604bSToby Isaac   Level: developer
91232716604bSToby Isaac 
91241189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
91252716604bSToby Isaac @*/
91260709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
91272716604bSToby Isaac {
91282716604bSToby Isaac   PetscErrorCode ierr;
91292716604bSToby Isaac 
91302716604bSToby Isaac   PetscFunctionBegin;
91312716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9132b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
91330918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
91340709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
91352716604bSToby Isaac   PetscFunctionReturn(0);
91362716604bSToby Isaac }
9137b698f381SToby Isaac 
9138b698f381SToby Isaac /*@C
9139b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
9140b698f381SToby Isaac 
9141d083f849SBarry Smith   Collective on dm
9142d083f849SBarry Smith 
9143b698f381SToby Isaac   Input Parameters:
9144b698f381SToby Isaac + dm    - The DM
9145b698f381SToby Isaac , time  - The time
9146b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
9147b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9148574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
9149b698f381SToby Isaac - n     - The vector to project along
9150b698f381SToby Isaac 
9151b698f381SToby Isaac   Output Parameter:
9152b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
9153b698f381SToby Isaac 
9154b698f381SToby Isaac   Level: developer
9155b698f381SToby Isaac 
9156b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
9157b698f381SToby Isaac @*/
9158b698f381SToby 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)
9159b698f381SToby Isaac {
9160b698f381SToby Isaac   PetscErrorCode ierr;
9161b698f381SToby Isaac 
9162b698f381SToby Isaac   PetscFunctionBegin;
9163b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9164b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
9165b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
9166b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
9167b698f381SToby Isaac   PetscFunctionReturn(0);
9168b698f381SToby Isaac }
9169b698f381SToby Isaac 
91702a16baeaSToby Isaac /*@C
91712a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
91722a16baeaSToby Isaac 
9173d083f849SBarry Smith   Collective on dm
9174d083f849SBarry Smith 
91752a16baeaSToby Isaac   Input Parameters:
91762a16baeaSToby Isaac + dm    - The DM
91772a16baeaSToby Isaac . time  - The time
91782a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
91792a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9180574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
91812a16baeaSToby Isaac 
91822a16baeaSToby Isaac   Output Parameter:
91832a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
91842a16baeaSToby Isaac 
91852a16baeaSToby Isaac   Level: developer
91862a16baeaSToby Isaac 
91871189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
91882a16baeaSToby Isaac @*/
91891189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
91902a16baeaSToby Isaac {
91912a16baeaSToby Isaac   PetscErrorCode ierr;
91922a16baeaSToby Isaac 
91932a16baeaSToby Isaac   PetscFunctionBegin;
91942a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
91952a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
91960918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
91972a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
91982a16baeaSToby Isaac   PetscFunctionReturn(0);
91992a16baeaSToby Isaac }
92002a16baeaSToby Isaac 
9201df0b854cSToby Isaac /*@C
9202df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
9203cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
9204df0b854cSToby Isaac 
9205df0b854cSToby Isaac   Collective on dm
9206df0b854cSToby Isaac 
9207df0b854cSToby Isaac   Input parameters:
9208df0b854cSToby Isaac + dm - the pre-adaptation DM object
9209a1b0c543SToby Isaac - label - label with the flags
9210df0b854cSToby Isaac 
9211df0b854cSToby Isaac   Output parameters:
92120d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
9213df0b854cSToby Isaac 
9214df0b854cSToby Isaac   Level: intermediate
92150d1cd5e0SMatthew G. Knepley 
92160d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
9217df0b854cSToby Isaac @*/
92180d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
9219df0b854cSToby Isaac {
9220df0b854cSToby Isaac   PetscErrorCode ierr;
9221df0b854cSToby Isaac 
9222df0b854cSToby Isaac   PetscFunctionBegin;
9223df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9224a1b0c543SToby Isaac   PetscValidPointer(label,2);
92250d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
92260d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
92276f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
92280d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
9229a587d139SMark   if (*dmAdapt) {
9230a587d139SMark     (*dmAdapt)->prealloc_only = dm->prealloc_only;  /* maybe this should go .... */
9231a587d139SMark     ierr = PetscFree((*dmAdapt)->vectype);CHKERRQ(ierr);
9232a587d139SMark     ierr = PetscStrallocpy(dm->vectype,(char**)&(*dmAdapt)->vectype);CHKERRQ(ierr);
9233a587d139SMark     ierr = PetscFree((*dmAdapt)->mattype);CHKERRQ(ierr);
9234a587d139SMark     ierr = PetscStrallocpy(dm->mattype,(char**)&(*dmAdapt)->mattype);CHKERRQ(ierr);
9235a587d139SMark   }
92360d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
92370d1cd5e0SMatthew G. Knepley }
92380d1cd5e0SMatthew G. Knepley 
92390d1cd5e0SMatthew G. Knepley /*@C
92400d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
92410d1cd5e0SMatthew G. Knepley 
92420d1cd5e0SMatthew G. Knepley   Input Parameters:
92430d1cd5e0SMatthew G. Knepley + dm - The DM object
92440d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
92456f25b0d8SLisandro 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_".
92460d1cd5e0SMatthew G. Knepley 
92470d1cd5e0SMatthew G. Knepley   Output Parameter:
92480d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
92490d1cd5e0SMatthew G. Knepley 
92500d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
92510d1cd5e0SMatthew G. Knepley 
92520d1cd5e0SMatthew G. Knepley   Level: advanced
92530d1cd5e0SMatthew G. Knepley 
92540d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
92550d1cd5e0SMatthew G. Knepley @*/
92560d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
92570d1cd5e0SMatthew G. Knepley {
92580d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
92590d1cd5e0SMatthew G. Knepley 
92600d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
92610d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92620d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
92630d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
92640d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
92656f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
92666f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
92670d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
9268df0b854cSToby Isaac   PetscFunctionReturn(0);
9269df0b854cSToby Isaac }
9270c4088d22SMatthew G. Knepley 
9271502a2867SDave May /*@C
9272502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
9273502a2867SDave May 
9274502a2867SDave May  Not Collective
9275502a2867SDave May 
9276502a2867SDave May  Input Parameter:
9277502a2867SDave May .  dm    - The DM
9278502a2867SDave May 
92790a19bb7dSprj-  Output Parameters:
92800a19bb7dSprj- +  nranks - the number of neighbours
92810a19bb7dSprj- -  ranks - the neighbors ranks
9282502a2867SDave May 
9283502a2867SDave May  Notes:
9284502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
9285502a2867SDave May 
9286502a2867SDave May  Level: beginner
9287502a2867SDave May 
9288dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
9289502a2867SDave May @*/
9290502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
9291502a2867SDave May {
9292502a2867SDave May   PetscErrorCode ierr;
9293502a2867SDave May 
9294502a2867SDave May   PetscFunctionBegin;
9295502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
92960918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
9297502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
9298502a2867SDave May   PetscFunctionReturn(0);
9299502a2867SDave May }
9300502a2867SDave May 
9301531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
9302531c7667SBarry Smith 
9303531c7667SBarry Smith /*
9304531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
9305531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
9306531c7667SBarry Smith */
9307531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
9308531c7667SBarry Smith {
9309531c7667SBarry Smith   PetscErrorCode ierr;
9310531c7667SBarry Smith 
9311531c7667SBarry Smith   PetscFunctionBegin;
9312531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9313531c7667SBarry Smith     Vec x1local;
9314531c7667SBarry Smith     DM  dm;
9315531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9316531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
9317531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
9318531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9319531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9320531c7667SBarry Smith     x1   = x1local;
9321531c7667SBarry Smith   }
9322531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
9323531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9324531c7667SBarry Smith     DM  dm;
9325531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9326531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
9327531c7667SBarry Smith   }
9328531c7667SBarry Smith   PetscFunctionReturn(0);
9329531c7667SBarry Smith }
9330531c7667SBarry Smith 
9331531c7667SBarry Smith /*@
9332531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
9333531c7667SBarry Smith 
9334531c7667SBarry Smith     Input Parameter:
9335531c7667SBarry Smith .    coloring - the MatFDColoring object
9336531c7667SBarry Smith 
933795452b02SPatrick Sanan     Developer Notes:
933895452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
9339531c7667SBarry Smith 
93401b266c99SBarry Smith     Level: advanced
93411b266c99SBarry Smith 
9342531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
9343531c7667SBarry Smith @*/
9344531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
9345531c7667SBarry Smith {
9346531c7667SBarry Smith   PetscFunctionBegin;
9347531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
9348531c7667SBarry Smith   PetscFunctionReturn(0);
9349531c7667SBarry Smith }
93508320bc6fSPatrick Sanan 
93518320bc6fSPatrick Sanan /*@
93528320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
93538320bc6fSPatrick Sanan 
93548320bc6fSPatrick Sanan     Collective
93558320bc6fSPatrick Sanan 
93568320bc6fSPatrick Sanan     Input Parameters:
9357a5bc1bf3SBarry Smith +    dm1 - the first DM
93588320bc6fSPatrick Sanan -    dm2 - the second DM
93598320bc6fSPatrick Sanan 
93608320bc6fSPatrick Sanan     Output Parameters:
93618320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
93628320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
93638320bc6fSPatrick Sanan 
93648320bc6fSPatrick Sanan     Notes:
93658320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
93663d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
93678320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
93683d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
93693d862458SPatrick Sanan     decomposition, but hold different data.
93708320bc6fSPatrick Sanan 
93718320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
93728320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
93738320bc6fSPatrick Sanan 
93748320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
93758320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
93768320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
93778320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
93788320bc6fSPatrick Sanan 
93798320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
93808320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
93818320bc6fSPatrick Sanan .vb
93828320bc6fSPatrick Sanan   ...
93838320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
93848320bc6fSPatrick Sanan   if (set && compatible)  {
93858320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
93868320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
93873d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
93888320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
93898320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
93908320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
93918320bc6fSPatrick Sanan       }
93928320bc6fSPatrick Sanan     }
93938320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
93948320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
93958320bc6fSPatrick Sanan   } else {
93968320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
93978320bc6fSPatrick Sanan   }
93988320bc6fSPatrick Sanan   ...
93998320bc6fSPatrick Sanan .ve
94008320bc6fSPatrick Sanan 
94018320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
94028320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
94038320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
94048320bc6fSPatrick Sanan     always check the "set" output parameter.
94058320bc6fSPatrick Sanan 
94068320bc6fSPatrick Sanan     A DM is always compatible with itself.
94078320bc6fSPatrick Sanan 
94088320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
94098320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
94108320bc6fSPatrick Sanan     incompatible.
94118320bc6fSPatrick Sanan 
94128320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
94138320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
94148320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
94158320bc6fSPatrick Sanan 
94168320bc6fSPatrick Sanan     Developer Notes:
94173d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
94183d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
9419a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
94208320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
94218320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
94223d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
94233d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
94248320bc6fSPatrick Sanan 
94258320bc6fSPatrick Sanan     Level: advanced
94268320bc6fSPatrick Sanan 
94273d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
94288320bc6fSPatrick Sanan @*/
94298320bc6fSPatrick Sanan 
9430a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
94318320bc6fSPatrick Sanan {
94328320bc6fSPatrick Sanan   PetscErrorCode ierr;
94338320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
94348320bc6fSPatrick Sanan   DMType         type,type2;
94358320bc6fSPatrick Sanan   PetscBool      sameType;
94368320bc6fSPatrick Sanan 
94378320bc6fSPatrick Sanan   PetscFunctionBegin;
9438a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
94398320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
94408320bc6fSPatrick Sanan 
94418320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
9442a5bc1bf3SBarry Smith   if (dm1 == dm2) {
94438320bc6fSPatrick Sanan     *set = PETSC_TRUE;
94448320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
94458320bc6fSPatrick Sanan     PetscFunctionReturn(0);
94468320bc6fSPatrick Sanan   }
94478320bc6fSPatrick Sanan 
94488320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
94498320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
94508320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
94518320bc6fSPatrick Sanan      determined by the implementation-specific logic */
9452ffc4695bSBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRMPI(ierr);
94538320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
94548320bc6fSPatrick Sanan     *set = PETSC_TRUE;
94558320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
94568320bc6fSPatrick Sanan     PetscFunctionReturn(0);
94578320bc6fSPatrick Sanan   }
94588320bc6fSPatrick Sanan 
94598320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
9460a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
9461a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
9462b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
94638320bc6fSPatrick Sanan   }
94648320bc6fSPatrick Sanan 
9465a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
94668320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
9467a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
94688320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
94698320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
94708320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
9471a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
94728320bc6fSPatrick Sanan   } else {
94738320bc6fSPatrick Sanan     *set = PETSC_FALSE;
94748320bc6fSPatrick Sanan   }
94758320bc6fSPatrick Sanan   PetscFunctionReturn(0);
94768320bc6fSPatrick Sanan }
9477c0f0dcc3SMatthew G. Knepley 
9478c0f0dcc3SMatthew G. Knepley /*@C
9479c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
9480c0f0dcc3SMatthew G. Knepley 
9481c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9482c0f0dcc3SMatthew G. Knepley 
9483c0f0dcc3SMatthew G. Knepley   Input Parameters:
9484c0f0dcc3SMatthew G. Knepley + DM - the DM
9485c0f0dcc3SMatthew G. Knepley . f - the monitor function
9486c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
9487c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
9488c0f0dcc3SMatthew G. Knepley 
9489c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9490c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
9491c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
9492c0f0dcc3SMatthew G. Knepley 
9493c0f0dcc3SMatthew G. Knepley   Notes:
9494c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
9495c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
9496c0f0dcc3SMatthew G. Knepley   order in which they were set.
9497c0f0dcc3SMatthew G. Knepley 
9498c0f0dcc3SMatthew G. Knepley   Fortran Notes:
9499c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
9500c0f0dcc3SMatthew G. Knepley 
9501c0f0dcc3SMatthew G. Knepley   Level: intermediate
9502c0f0dcc3SMatthew G. Knepley 
9503c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
9504c0f0dcc3SMatthew G. Knepley @*/
9505c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
9506c0f0dcc3SMatthew G. Knepley {
9507c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9508c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9509c0f0dcc3SMatthew G. Knepley 
9510c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9511c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9512c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9513c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9514c0f0dcc3SMatthew G. Knepley 
9515c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
9516c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9517c0f0dcc3SMatthew G. Knepley   }
9518c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9519c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9520c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9521c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9522c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9523c0f0dcc3SMatthew G. Knepley }
9524c0f0dcc3SMatthew G. Knepley 
9525c0f0dcc3SMatthew G. Knepley /*@
9526c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9527c0f0dcc3SMatthew G. Knepley 
9528c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9529c0f0dcc3SMatthew G. Knepley 
9530c0f0dcc3SMatthew G. Knepley   Input Parameter:
9531c0f0dcc3SMatthew G. Knepley . dm - the DM
9532c0f0dcc3SMatthew G. Knepley 
9533c0f0dcc3SMatthew G. Knepley   Options Database Key:
9534c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9535c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9536c0f0dcc3SMatthew G. Knepley   set via the options database
9537c0f0dcc3SMatthew G. Knepley 
9538c0f0dcc3SMatthew G. Knepley   Notes:
9539c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9540c0f0dcc3SMatthew G. Knepley 
9541c0f0dcc3SMatthew G. Knepley   Level: intermediate
9542c0f0dcc3SMatthew G. Knepley 
9543c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9544c0f0dcc3SMatthew G. Knepley @*/
9545c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9546c0f0dcc3SMatthew G. Knepley {
9547c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9548c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9549c0f0dcc3SMatthew G. Knepley 
9550c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9551c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9552c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9553c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
9554c0f0dcc3SMatthew G. Knepley   }
9555c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9556c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9557c0f0dcc3SMatthew G. Knepley }
9558c0f0dcc3SMatthew G. Knepley 
9559c0f0dcc3SMatthew G. Knepley /*@C
9560c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9561c0f0dcc3SMatthew G. Knepley 
9562c0f0dcc3SMatthew G. Knepley   Collective on DM
9563c0f0dcc3SMatthew G. Knepley 
9564c0f0dcc3SMatthew G. Knepley   Input Parameters:
9565c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9566c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9567c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9568c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9569c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9570c0f0dcc3SMatthew 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
9571c0f0dcc3SMatthew G. Knepley 
9572c0f0dcc3SMatthew G. Knepley   Output Parameter:
9573c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9574c0f0dcc3SMatthew G. Knepley 
9575c0f0dcc3SMatthew G. Knepley   Level: developer
9576c0f0dcc3SMatthew G. Knepley 
9577c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9578c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9579c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9580c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9581c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9582c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9583c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9584c0f0dcc3SMatthew G. Knepley @*/
9585c0f0dcc3SMatthew 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)
9586c0f0dcc3SMatthew G. Knepley {
9587c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9588c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9589c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
9590c0f0dcc3SMatthew G. Knepley 
9591c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9592c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9593c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
9594c0f0dcc3SMatthew G. Knepley   if (*flg) {
9595c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9596c0f0dcc3SMatthew G. Knepley 
9597c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
9598c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
9599c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
9600c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
9601c0f0dcc3SMatthew G. Knepley   }
9602c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9603c0f0dcc3SMatthew G. Knepley }
9604c0f0dcc3SMatthew G. Knepley 
9605c0f0dcc3SMatthew G. Knepley /*@
9606c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9607c0f0dcc3SMatthew G. Knepley 
9608c0f0dcc3SMatthew G. Knepley    Collective on DM
9609c0f0dcc3SMatthew G. Knepley 
9610c0f0dcc3SMatthew G. Knepley    Input Parameters:
9611c0f0dcc3SMatthew G. Knepley .  dm - The DM
9612c0f0dcc3SMatthew G. Knepley 
9613c0f0dcc3SMatthew G. Knepley    Level: developer
9614c0f0dcc3SMatthew G. Knepley 
9615c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9616c0f0dcc3SMatthew G. Knepley @*/
9617c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9618c0f0dcc3SMatthew G. Knepley {
9619c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9620c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9621c0f0dcc3SMatthew G. Knepley 
9622c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9623c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9624c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9625c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9626c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
9627c0f0dcc3SMatthew G. Knepley   }
9628c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9629c0f0dcc3SMatthew G. Knepley }
96302e4af2aeSMatthew G. Knepley 
96312e4af2aeSMatthew G. Knepley /*@
96322e4af2aeSMatthew G. Knepley   DMComputeError - Computes the error assuming the user has given exact solution functions
96332e4af2aeSMatthew G. Knepley 
96342e4af2aeSMatthew G. Knepley   Collective on DM
96352e4af2aeSMatthew G. Knepley 
96362e4af2aeSMatthew G. Knepley   Input Parameters:
96372e4af2aeSMatthew G. Knepley + dm     - The DM
96386b867d5aSJose E. Roman - sol    - The solution vector
96392e4af2aeSMatthew G. Knepley 
96406b867d5aSJose E. Roman   Input/Output Parameter:
96416b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
96426b867d5aSJose E. Roman            contains the error in each field
96436b867d5aSJose E. Roman 
96446b867d5aSJose E. Roman   Output Parameter:
96456b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
96462e4af2aeSMatthew G. Knepley 
96472e4af2aeSMatthew G. Knepley   Note: The exact solutions come from the PetscDS object, and the time comes from DMGetOutputSequenceNumber().
96482e4af2aeSMatthew G. Knepley 
96492e4af2aeSMatthew G. Knepley   Level: developer
96502e4af2aeSMatthew G. Knepley 
96512e4af2aeSMatthew G. Knepley .seealso: DMMonitorSet(), DMGetRegionNumDS(), PetscDSGetExactSolution(), DMGetOutputSequenceNumber()
96522e4af2aeSMatthew G. Knepley @*/
96532e4af2aeSMatthew G. Knepley PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
96542e4af2aeSMatthew G. Knepley {
96552e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
96562e4af2aeSMatthew G. Knepley   void            **ctxs;
96572e4af2aeSMatthew G. Knepley   PetscReal         time;
96582e4af2aeSMatthew G. Knepley   PetscInt          Nf, f, Nds, s;
96592e4af2aeSMatthew G. Knepley   PetscErrorCode    ierr;
96602e4af2aeSMatthew G. Knepley 
96612e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
96622e4af2aeSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
96632e4af2aeSMatthew G. Knepley   ierr = PetscCalloc2(Nf, &exactSol, Nf, &ctxs);CHKERRQ(ierr);
96642e4af2aeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
96652e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
96662e4af2aeSMatthew G. Knepley     PetscDS         ds;
96672e4af2aeSMatthew G. Knepley     DMLabel         label;
96682e4af2aeSMatthew G. Knepley     IS              fieldIS;
96692e4af2aeSMatthew G. Knepley     const PetscInt *fields;
96702e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
96712e4af2aeSMatthew G. Knepley 
96722e4af2aeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
96732e4af2aeSMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
96742e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);}
96752e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
96762e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
96772e4af2aeSMatthew G. Knepley       ierr = PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]);CHKERRQ(ierr);
96782e4af2aeSMatthew G. Knepley     }
96792e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);}
96802e4af2aeSMatthew G. Knepley   }
96812e4af2aeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
96822e4af2aeSMatthew 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);
96832e4af2aeSMatthew G. Knepley   }
96842e4af2aeSMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
96852e4af2aeSMatthew G. Knepley   if (errors) {ierr = DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors);CHKERRQ(ierr);}
96862e4af2aeSMatthew G. Knepley   if (errorVec) {
96872e4af2aeSMatthew G. Knepley     DM             edm;
96882e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
96892e4af2aeSMatthew G. Knepley     PetscBool      simplex;
96902e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
96912e4af2aeSMatthew G. Knepley 
96922e4af2aeSMatthew G. Knepley     ierr = DMClone(dm, &edm);CHKERRQ(ierr);
96932e4af2aeSMatthew G. Knepley     ierr = DMGetDimension(edm, &dim);CHKERRQ(ierr);
96942e4af2aeSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr);
96952e4af2aeSMatthew G. Knepley     ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
96962e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
96972e4af2aeSMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
96982e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
96992e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
97002e4af2aeSMatthew G. Knepley       PetscQuadrature q;
97012e4af2aeSMatthew G. Knepley       const char     *name;
97022e4af2aeSMatthew G. Knepley 
97032e4af2aeSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
97042e4af2aeSMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe);CHKERRQ(ierr);
97052e4af2aeSMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
97062e4af2aeSMatthew G. Knepley       ierr = PetscObjectSetName((PetscObject) efe, name);CHKERRQ(ierr);
97072e4af2aeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
97082e4af2aeSMatthew G. Knepley       ierr = PetscFESetQuadrature(efe, q);CHKERRQ(ierr);
97092e4af2aeSMatthew G. Knepley       ierr = DMSetField(edm, f, NULL, (PetscObject) efe);CHKERRQ(ierr);
97102e4af2aeSMatthew G. Knepley       ierr = PetscFEDestroy(&efe);CHKERRQ(ierr);
97112e4af2aeSMatthew G. Knepley     }
97122e4af2aeSMatthew G. Knepley     ierr = DMCreateDS(edm);CHKERRQ(ierr);
97132e4af2aeSMatthew G. Knepley 
97141e1ea65dSPierre Jolivet     ierr = DMCreateGlobalVector(edm, errorVec);CHKERRQ(ierr);
97152e4af2aeSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) *errorVec, "Error");CHKERRQ(ierr);
97162e4af2aeSMatthew G. Knepley     ierr = DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec);CHKERRQ(ierr);
97172e4af2aeSMatthew G. Knepley     ierr = DMDestroy(&edm);CHKERRQ(ierr);
97182e4af2aeSMatthew G. Knepley   }
97192e4af2aeSMatthew G. Knepley   ierr = PetscFree2(exactSol, ctxs);CHKERRQ(ierr);
97202e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
97212e4af2aeSMatthew G. Knepley }
97229a2a23afSMatthew G. Knepley 
97239a2a23afSMatthew G. Knepley /*@
97249a2a23afSMatthew G. Knepley   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this DM
97259a2a23afSMatthew G. Knepley 
97269a2a23afSMatthew G. Knepley   Not collective
97279a2a23afSMatthew G. Knepley 
97289a2a23afSMatthew G. Knepley   Input Parameter:
97299a2a23afSMatthew G. Knepley . dm     - The DM
97309a2a23afSMatthew G. Knepley 
97319a2a23afSMatthew G. Knepley   Output Parameter:
9732a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
97339a2a23afSMatthew G. Knepley 
97349a2a23afSMatthew G. Knepley   Level: advanced
97359a2a23afSMatthew G. Knepley 
97369a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryLabels(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
97379a2a23afSMatthew G. Knepley @*/
97389a2a23afSMatthew G. Knepley PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
97399a2a23afSMatthew G. Knepley {
97409a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
97419a2a23afSMatthew G. Knepley 
97429a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97439a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97449a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetSize(dm->auxData, numAux);CHKERRQ(ierr);
97459a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
97469a2a23afSMatthew G. Knepley }
97479a2a23afSMatthew G. Knepley 
97489a2a23afSMatthew G. Knepley /*@
97499a2a23afSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value
97509a2a23afSMatthew G. Knepley 
97519a2a23afSMatthew G. Knepley   Not collective
97529a2a23afSMatthew G. Knepley 
97539a2a23afSMatthew G. Knepley   Input Parameters:
97549a2a23afSMatthew G. Knepley + dm     - The DM
97559a2a23afSMatthew G. Knepley . label  - The DMLabel
97569a2a23afSMatthew G. Knepley - value  - The label value indicating the region
97579a2a23afSMatthew G. Knepley 
97589a2a23afSMatthew G. Knepley   Output Parameter:
97599a2a23afSMatthew G. Knepley . aux    - The Vec holding auxiliary field data
97609a2a23afSMatthew G. Knepley 
976104c51a94SMatthew G. Knepley   Note: If no auxiliary vector is found for this (label, value), (NULL, 0) is checked as well.
976204c51a94SMatthew G. Knepley 
97639a2a23afSMatthew G. Knepley   Level: advanced
97649a2a23afSMatthew G. Knepley 
97659a2a23afSMatthew G. Knepley .seealso: DMSetAuxiliaryVec(), DMGetNumAuxiliaryVec()
97669a2a23afSMatthew G. Knepley @*/
97679a2a23afSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, Vec *aux)
97689a2a23afSMatthew G. Knepley {
976904c51a94SMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0};
977004c51a94SMatthew G. Knepley   PetscBool       has;
97719a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
97729a2a23afSMatthew G. Knepley 
97739a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97749a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97759a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
97769a2a23afSMatthew G. Knepley   key.label = label;
97779a2a23afSMatthew G. Knepley   key.value = value;
977804c51a94SMatthew G. Knepley   ierr = PetscHMapAuxHas(dm->auxData, key, &has);CHKERRQ(ierr);
977904c51a94SMatthew G. Knepley   if (has) {ierr = PetscHMapAuxGet(dm->auxData, key,  aux);CHKERRQ(ierr);}
978004c51a94SMatthew G. Knepley   else     {ierr = PetscHMapAuxGet(dm->auxData, wild, aux);CHKERRQ(ierr);}
97819a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
97829a2a23afSMatthew G. Knepley }
97839a2a23afSMatthew G. Knepley 
97849a2a23afSMatthew G. Knepley /*@
97859a2a23afSMatthew G. Knepley   DMSetAuxiliaryVec - Set the auxiliary vector for region specified by the given label and value
97869a2a23afSMatthew G. Knepley 
97879a2a23afSMatthew G. Knepley   Not collective
97889a2a23afSMatthew G. Knepley 
97899a2a23afSMatthew G. Knepley   Input Parameters:
97909a2a23afSMatthew G. Knepley + dm     - The DM
97919a2a23afSMatthew G. Knepley . label  - The DMLabel
97929a2a23afSMatthew G. Knepley . value  - The label value indicating the region
97939a2a23afSMatthew G. Knepley - aux    - The Vec holding auxiliary field data
97949a2a23afSMatthew G. Knepley 
97959a2a23afSMatthew G. Knepley   Level: advanced
97969a2a23afSMatthew G. Knepley 
97979a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryVec()
97989a2a23afSMatthew G. Knepley @*/
97999a2a23afSMatthew G. Knepley PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, Vec aux)
98009a2a23afSMatthew G. Knepley {
98019a2a23afSMatthew G. Knepley   Vec             old;
98029a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
98039a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
98049a2a23afSMatthew G. Knepley 
98059a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98069a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98079a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
98089a2a23afSMatthew G. Knepley   key.label = label;
98099a2a23afSMatthew G. Knepley   key.value = value;
98109a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGet(dm->auxData, key, &old);CHKERRQ(ierr);
98119a2a23afSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) aux);CHKERRQ(ierr);
98129a2a23afSMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) old);CHKERRQ(ierr);
98139a2a23afSMatthew G. Knepley   if (!aux) {ierr = PetscHMapAuxDel(dm->auxData, key);CHKERRQ(ierr);}
98149a2a23afSMatthew G. Knepley   else      {ierr = PetscHMapAuxSet(dm->auxData, key, aux);CHKERRQ(ierr);}
98159a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98169a2a23afSMatthew G. Knepley }
98179a2a23afSMatthew G. Knepley 
98189a2a23afSMatthew G. Knepley /*@C
98199a2a23afSMatthew G. Knepley   DMGetAuxiliaryLabels - Get the labels and values for all auxiliary vectors in this DM
98209a2a23afSMatthew G. Knepley 
98219a2a23afSMatthew G. Knepley   Not collective
98229a2a23afSMatthew G. Knepley 
98239a2a23afSMatthew G. Knepley   Input Parameter:
98249a2a23afSMatthew G. Knepley . dm      - The DM
98259a2a23afSMatthew G. Knepley 
98269a2a23afSMatthew G. Knepley   Output Parameters:
98279a2a23afSMatthew G. Knepley + labels  - The DMLabels for each Vec
98289a2a23afSMatthew G. Knepley - values  - The label values for each Vec
98299a2a23afSMatthew G. Knepley 
98309a2a23afSMatthew G. Knepley   Note: The arrays passed in must be at least as large as DMGetNumAuxiliaryVec().
98319a2a23afSMatthew G. Knepley 
98329a2a23afSMatthew G. Knepley   Level: advanced
98339a2a23afSMatthew G. Knepley 
98349a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
98359a2a23afSMatthew G. Knepley @*/
98369a2a23afSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[])
98379a2a23afSMatthew G. Knepley {
98389a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
98399a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
98409a2a23afSMatthew G. Knepley   PetscErrorCode   ierr;
98419a2a23afSMatthew G. Knepley 
98429a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98439a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98449a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
98459a2a23afSMatthew G. Knepley   PetscValidPointer(values, 3);
98469a2a23afSMatthew G. Knepley   ierr = DMGetNumAuxiliaryVec(dm, &n);CHKERRQ(ierr);
98479a2a23afSMatthew G. Knepley   ierr = PetscMalloc1(n, &keys);CHKERRQ(ierr);
98489a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetKeys(dm->auxData, &off, keys);CHKERRQ(ierr);
98499a2a23afSMatthew G. Knepley   for (i = 0; i < n; ++i) {labels[i] = keys[i].label; values[i] = keys[i].value;}
98509a2a23afSMatthew G. Knepley   ierr = PetscFree(keys);CHKERRQ(ierr);
98519a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98529a2a23afSMatthew G. Knepley }
98539a2a23afSMatthew G. Knepley 
98549a2a23afSMatthew G. Knepley /*@
98559a2a23afSMatthew G. Knepley   DMCopyAuxiliaryVec - Copy the auxiliary data to a new DM
98569a2a23afSMatthew G. Knepley 
98579a2a23afSMatthew G. Knepley   Not collective
98589a2a23afSMatthew G. Knepley 
98599a2a23afSMatthew G. Knepley   Input Parameter:
98609a2a23afSMatthew G. Knepley . dm    - The DM
98619a2a23afSMatthew G. Knepley 
98629a2a23afSMatthew G. Knepley   Output Parameter:
98639a2a23afSMatthew G. Knepley . dmNew - The new DM, now with the same auxiliary data
98649a2a23afSMatthew G. Knepley 
98659a2a23afSMatthew G. Knepley   Level: advanced
98669a2a23afSMatthew G. Knepley 
98679a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
98689a2a23afSMatthew G. Knepley @*/
98699a2a23afSMatthew G. Knepley PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
98709a2a23afSMatthew G. Knepley {
98719a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
98729a2a23afSMatthew G. Knepley 
98739a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98749a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98759a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDestroy(&dmNew->auxData);CHKERRQ(ierr);
98769a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData);CHKERRQ(ierr);
98779a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98789a2a23afSMatthew G. Knepley }
9879b5a892a1SMatthew G. Knepley 
9880b5a892a1SMatthew G. Knepley /*@C
9881b5a892a1SMatthew G. Knepley   DMPolytopeMatchOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
9882b5a892a1SMatthew G. Knepley 
9883b5a892a1SMatthew G. Knepley   Not collective
9884b5a892a1SMatthew G. Knepley 
9885b5a892a1SMatthew G. Knepley   Input Parameters:
9886b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9887b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9888b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9889b5a892a1SMatthew G. Knepley 
9890b5a892a1SMatthew G. Knepley   Output Parameters:
9891b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
9892b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9893b5a892a1SMatthew G. Knepley 
9894b5a892a1SMatthew G. Knepley   Level: advanced
9895b5a892a1SMatthew G. Knepley 
9896b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchVertexOrientation()
9897b5a892a1SMatthew G. Knepley @*/
9898b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9899b5a892a1SMatthew G. Knepley {
9900b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9901b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
9902b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9903b5a892a1SMatthew G. Knepley 
9904b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9905b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
9906b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9907b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9908b5a892a1SMatthew G. Knepley 
9909b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceCone[arr[c*2]] != targetCone[c]) break;
9910b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
9911b5a892a1SMatthew G. Knepley   }
9912b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9913b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9914b5a892a1SMatthew G. Knepley }
9915b5a892a1SMatthew G. Knepley 
9916b5a892a1SMatthew G. Knepley /*@C
9917b5a892a1SMatthew G. Knepley   DMPolytopeGetOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
9918b5a892a1SMatthew G. Knepley 
9919b5a892a1SMatthew G. Knepley   Not collective
9920b5a892a1SMatthew G. Knepley 
9921b5a892a1SMatthew G. Knepley   Input Parameters:
9922b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9923b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9924b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9925b5a892a1SMatthew G. Knepley 
9926b5a892a1SMatthew G. Knepley   Output Parameters:
9927b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
9928b5a892a1SMatthew G. Knepley 
9929b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
9930b5a892a1SMatthew G. Knepley 
9931b5a892a1SMatthew G. Knepley   Level: advanced
9932b5a892a1SMatthew G. Knepley 
9933b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchOrientation(), DMPolytopeGetVertexOrientation()
9934b5a892a1SMatthew G. Knepley @*/
9935b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9936b5a892a1SMatthew G. Knepley {
9937b5a892a1SMatthew G. Knepley   PetscBool      found;
9938b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
9939b5a892a1SMatthew G. Knepley 
9940b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9941b5a892a1SMatthew G. Knepley   ierr = DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found);CHKERRQ(ierr);
9942b5a892a1SMatthew G. Knepley   if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9943b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9944b5a892a1SMatthew G. Knepley }
9945b5a892a1SMatthew G. Knepley 
9946b5a892a1SMatthew G. Knepley /*@C
9947b5a892a1SMatthew G. Knepley   DMPolytopeMatchVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
9948b5a892a1SMatthew G. Knepley 
9949b5a892a1SMatthew G. Knepley   Not collective
9950b5a892a1SMatthew G. Knepley 
9951b5a892a1SMatthew G. Knepley   Input Parameters:
9952b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9953b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9954b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9955b5a892a1SMatthew G. Knepley 
9956b5a892a1SMatthew G. Knepley   Output Parameters:
9957b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
9958b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9959b5a892a1SMatthew G. Knepley 
9960b5a892a1SMatthew G. Knepley   Level: advanced
9961b5a892a1SMatthew G. Knepley 
9962b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchOrientation()
9963b5a892a1SMatthew G. Knepley @*/
9964b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9965b5a892a1SMatthew G. Knepley {
9966b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9967b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
9968b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9969b5a892a1SMatthew G. Knepley 
9970b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9971b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
9972b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9973b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9974b5a892a1SMatthew G. Knepley 
9975b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceVert[arr[c]] != targetVert[c]) break;
9976b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
9977b5a892a1SMatthew G. Knepley   }
9978b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9979b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9980b5a892a1SMatthew G. Knepley }
9981b5a892a1SMatthew G. Knepley 
9982b5a892a1SMatthew G. Knepley /*@C
9983b5a892a1SMatthew G. Knepley   DMPolytopeGetVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
9984b5a892a1SMatthew G. Knepley 
9985b5a892a1SMatthew G. Knepley   Not collective
9986b5a892a1SMatthew G. Knepley 
9987b5a892a1SMatthew G. Knepley   Input Parameters:
9988b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9989b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9990b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9991b5a892a1SMatthew G. Knepley 
9992b5a892a1SMatthew G. Knepley   Output Parameters:
9993b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
9994b5a892a1SMatthew G. Knepley 
9995b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
9996b5a892a1SMatthew G. Knepley 
9997b5a892a1SMatthew G. Knepley   Level: advanced
9998b5a892a1SMatthew G. Knepley 
9999b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchVertexOrientation(), DMPolytopeGetOrientation()
10000b5a892a1SMatthew G. Knepley @*/
10001b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
10002b5a892a1SMatthew G. Knepley {
10003b5a892a1SMatthew G. Knepley   PetscBool      found;
10004b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
10005b5a892a1SMatthew G. Knepley 
10006b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10007b5a892a1SMatthew G. Knepley   ierr = DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found);CHKERRQ(ierr);
10008b5a892a1SMatthew G. Knepley   if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
10009b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10010b5a892a1SMatthew G. Knepley }
10011012bc364SMatthew G. Knepley 
10012012bc364SMatthew G. Knepley /*@C
10013012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
10014012bc364SMatthew G. Knepley 
10015012bc364SMatthew G. Knepley   Not collective
10016012bc364SMatthew G. Knepley 
10017012bc364SMatthew G. Knepley   Input Parameters:
10018012bc364SMatthew G. Knepley + ct    - The DMPolytopeType
10019012bc364SMatthew G. Knepley - point - Coordinates of the point
10020012bc364SMatthew G. Knepley 
10021012bc364SMatthew G. Knepley   Output Parameters:
10022012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
10023012bc364SMatthew G. Knepley 
10024012bc364SMatthew G. Knepley   Level: advanced
10025012bc364SMatthew G. Knepley 
10026012bc364SMatthew G. Knepley .seealso: DMLocatePoints()
10027012bc364SMatthew G. Knepley @*/
10028012bc364SMatthew G. Knepley PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
10029012bc364SMatthew G. Knepley {
10030012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
10031012bc364SMatthew G. Knepley   PetscInt  d;
10032012bc364SMatthew G. Knepley 
10033012bc364SMatthew G. Knepley   PetscFunctionBegin;
10034012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
10035012bc364SMatthew G. Knepley   switch (ct) {
10036012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
10037012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
10038012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
10039012bc364SMatthew G. Knepley       if (point[d] < -1.0) {*inside = PETSC_FALSE; break;}
10040012bc364SMatthew G. Knepley       sum += point[d];
10041012bc364SMatthew G. Knepley     }
10042012bc364SMatthew G. Knepley     if (sum > PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10043012bc364SMatthew G. Knepley     break;
10044012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
10045012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
10046012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
10047012bc364SMatthew G. Knepley       if (PetscAbsReal(point[d]) > 1.+PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10048012bc364SMatthew G. Knepley     break;
10049012bc364SMatthew G. Knepley   default:
10050012bc364SMatthew G. Knepley     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
10051012bc364SMatthew G. Knepley   }
10052012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
10053012bc364SMatthew G. Knepley }
10054