xref: /petsc/src/dm/interface/dm.c (revision d410b0cf18e1798d3d4c14858e0c2ffdbe2fea69)
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};
2560c22052SBarry Smith 
26a4121054SBarry Smith /*@
27de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
28a4121054SBarry Smith 
29a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
30a4121054SBarry Smith    error when you try to use the vector.
31a4121054SBarry Smith 
32d083f849SBarry Smith   Collective
33a4121054SBarry Smith 
34a4121054SBarry Smith   Input Parameter:
35a4121054SBarry Smith . comm - The communicator for the DM object
36a4121054SBarry Smith 
37a4121054SBarry Smith   Output Parameter:
38a4121054SBarry Smith . dm - The DM object
39a4121054SBarry Smith 
40a4121054SBarry Smith   Level: beginner
41a4121054SBarry Smith 
428472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
43a4121054SBarry Smith @*/
447087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
45a4121054SBarry Smith {
46a4121054SBarry Smith   DM             v;
47e5e52638SMatthew G. Knepley   PetscDS        ds;
48a4121054SBarry Smith   PetscErrorCode ierr;
49a4121054SBarry Smith 
50a4121054SBarry Smith   PetscFunctionBegin;
511411c6eeSJed Brown   PetscValidPointer(dm,2);
520298fd71SBarry Smith   *dm = NULL;
53607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
54a4121054SBarry Smith 
5573107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
56e7c4fc90SDmitry Karpeev 
5749be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
5849be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
590298fd71SBarry Smith   v->ltogmap                  = NULL;
601411c6eeSJed Brown   v->bs                       = 1;
61171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
6288ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
631bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
64c58f1c22SToby Isaac   v->labels                   = NULL;
6534aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
6634aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
67c58f1c22SToby Isaac   v->depthLabel               = NULL;
68ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
691bb6d2a8SBarry Smith   v->localSection             = NULL;
701bb6d2a8SBarry Smith   v->globalSection            = NULL;
71fba222abSToby Isaac   v->defaultConstraintSection = NULL;
72fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
73c6b900c6SMatthew G. Knepley   v->L                        = NULL;
74c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
755dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
769a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7796173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
78435a35e8SMatthew G Knepley   {
79435a35e8SMatthew G Knepley     PetscInt i;
80435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
810298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
82f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
83435a35e8SMatthew G Knepley     }
84435a35e8SMatthew G Knepley   }
8545480ffeSMatthew G. Knepley   ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
86b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
87e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
889a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxCreate(&v->auxData);CHKERRQ(ierr);
8914f150ffSMatthew G. Knepley   v->dmBC = NULL;
90a8fb8f29SToby Isaac   v->coarseMesh = NULL;
91f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
92cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
93c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
94b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
954a7a4c06SLawrence Mitchell 
961411c6eeSJed Brown   *dm = v;
97a4121054SBarry Smith   PetscFunctionReturn(0);
98a4121054SBarry Smith }
99a4121054SBarry Smith 
10038221697SMatthew G. Knepley /*@
10138221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
10238221697SMatthew G. Knepley 
103d083f849SBarry Smith   Collective
10438221697SMatthew G. Knepley 
10538221697SMatthew G. Knepley   Input Parameter:
10638221697SMatthew G. Knepley . dm - The original DM object
10738221697SMatthew G. Knepley 
10838221697SMatthew G. Knepley   Output Parameter:
10938221697SMatthew G. Knepley . newdm  - The new DM object
11038221697SMatthew G. Knepley 
11138221697SMatthew G. Knepley   Level: beginner
11238221697SMatthew G. Knepley 
1131cb8cacdSPatrick Sanan   Notes:
1141cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1151cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1161cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1171bb6d2a8SBarry Smith 
11889706ed2SPatrick Sanan   The clone is considered set up iff the original is.
11989706ed2SPatrick Sanan 
12092cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1211bb6d2a8SBarry Smith 
12238221697SMatthew G. Knepley @*/
12338221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
12438221697SMatthew G. Knepley {
12538221697SMatthew G. Knepley   PetscSF        sf;
12638221697SMatthew G. Knepley   Vec            coords;
12738221697SMatthew G. Knepley   void          *ctx;
128a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
12938221697SMatthew G. Knepley   PetscErrorCode ierr;
13038221697SMatthew G. Knepley 
13138221697SMatthew G. Knepley   PetscFunctionBegin;
13238221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13338221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
13438221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1355d80c0bfSVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE);CHKERRQ(ierr);
136ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
137ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
138c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
139a587d139SMark   ierr = PetscFree((*newdm)->vectype);CHKERRQ(ierr);
140a587d139SMark   ierr = PetscStrallocpy(dm->vectype,(char**)&(*newdm)->vectype);CHKERRQ(ierr);
141a587d139SMark   ierr = PetscFree((*newdm)->mattype);CHKERRQ(ierr);
142a587d139SMark   ierr = PetscStrallocpy(dm->mattype,(char**)&(*newdm)->mattype);CHKERRQ(ierr);
1431de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1441de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
14538221697SMatthew G. Knepley   if (dm->ops->clone) {
14638221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
14738221697SMatthew G. Knepley   }
1483f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
14938221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
15038221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
15138221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
15238221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
153be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
154be4c1c3eSMatthew G. Knepley     DM           ncdm;
155be4c1c3eSMatthew G. Knepley     PetscSection cs;
1565a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
157be4c1c3eSMatthew G. Knepley 
15892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
159be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
160ffc4695bSBarry Smith     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
1615a0206caSToby Isaac     if (pEndMax >= 0) {
162be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
16383bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
16492fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
165a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
166be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
167be4c1c3eSMatthew G. Knepley     }
168be4c1c3eSMatthew G. Knepley   }
169a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
170a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
17138221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
17238221697SMatthew G. Knepley   if (coords) {
17338221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
17438221697SMatthew G. Knepley   } else {
17538221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
17638221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
17738221697SMatthew G. Knepley   }
17890b157c4SStefano Zampini   {
17990b157c4SStefano Zampini     PetscBool             isper;
180c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1815dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
18290b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
18390b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
184c6b900c6SMatthew G. Knepley   }
18534aa8a36SMatthew G. Knepley   {
18634aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
18734aa8a36SMatthew G. Knepley 
18834aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
18934aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
19034aa8a36SMatthew G. Knepley   }
19138221697SMatthew G. Knepley   PetscFunctionReturn(0);
19238221697SMatthew G. Knepley }
19338221697SMatthew G. Knepley 
1949a42bb27SBarry Smith /*@C
195564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1969a42bb27SBarry Smith 
197d083f849SBarry Smith    Logically Collective on da
1989a42bb27SBarry Smith 
1999a42bb27SBarry Smith    Input Parameter:
2009a42bb27SBarry Smith +  da - initial distributed array
201e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
2029a42bb27SBarry Smith 
2039a42bb27SBarry Smith    Options Database:
204dd85299cSBarry Smith .   -dm_vec_type ctype
2059a42bb27SBarry Smith 
2069a42bb27SBarry Smith    Level: intermediate
2079a42bb27SBarry Smith 
208a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
2099a42bb27SBarry Smith @*/
21019fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2119a42bb27SBarry Smith {
2129a42bb27SBarry Smith   PetscErrorCode ierr;
2139a42bb27SBarry Smith 
2149a42bb27SBarry Smith   PetscFunctionBegin;
2159a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2169a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
21719fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2189a42bb27SBarry Smith   PetscFunctionReturn(0);
2199a42bb27SBarry Smith }
2209a42bb27SBarry Smith 
221c0dedaeaSBarry Smith /*@C
222c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
223c0dedaeaSBarry Smith 
224d083f849SBarry Smith    Logically Collective on da
225c0dedaeaSBarry Smith 
226c0dedaeaSBarry Smith    Input Parameter:
227c0dedaeaSBarry Smith .  da - initial distributed array
228c0dedaeaSBarry Smith 
229c0dedaeaSBarry Smith    Output Parameter:
230c0dedaeaSBarry Smith .  ctype - the vector type
231c0dedaeaSBarry Smith 
232c0dedaeaSBarry Smith    Level: intermediate
233c0dedaeaSBarry Smith 
234a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
235c0dedaeaSBarry Smith @*/
236c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
237c0dedaeaSBarry Smith {
238c0dedaeaSBarry Smith   PetscFunctionBegin;
239c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
240c0dedaeaSBarry Smith   *ctype = da->vectype;
241c0dedaeaSBarry Smith   PetscFunctionReturn(0);
242c0dedaeaSBarry Smith }
243c0dedaeaSBarry Smith 
2445f1ad066SMatthew G Knepley /*@
24534f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2465f1ad066SMatthew G Knepley 
2475f1ad066SMatthew G Knepley   Not collective
2485f1ad066SMatthew G Knepley 
2495f1ad066SMatthew G Knepley   Input Parameter:
2505f1ad066SMatthew G Knepley . v - The Vec
2515f1ad066SMatthew G Knepley 
2525f1ad066SMatthew G Knepley   Output Parameter:
2535f1ad066SMatthew G Knepley . dm - The DM
2545f1ad066SMatthew G Knepley 
2555f1ad066SMatthew G Knepley   Level: intermediate
2565f1ad066SMatthew G Knepley 
2575f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2585f1ad066SMatthew G Knepley @*/
2595f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2605f1ad066SMatthew G Knepley {
2615f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2625f1ad066SMatthew G Knepley 
2635f1ad066SMatthew G Knepley   PetscFunctionBegin;
2645f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2655f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2665f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2675f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2685f1ad066SMatthew G Knepley }
2695f1ad066SMatthew G Knepley 
2705f1ad066SMatthew G Knepley /*@
271d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2725f1ad066SMatthew G Knepley 
2735f1ad066SMatthew G Knepley   Not collective
2745f1ad066SMatthew G Knepley 
2755f1ad066SMatthew G Knepley   Input Parameters:
2765f1ad066SMatthew G Knepley + v - The Vec
2775f1ad066SMatthew G Knepley - dm - The DM
2785f1ad066SMatthew G Knepley 
279d9805387SMatthew 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.
280d9805387SMatthew G. Knepley 
2815f1ad066SMatthew G Knepley   Level: intermediate
2825f1ad066SMatthew G Knepley 
2835f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2845f1ad066SMatthew G Knepley @*/
2855f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2865f1ad066SMatthew G Knepley {
2875f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2885f1ad066SMatthew G Knepley 
2895f1ad066SMatthew G Knepley   PetscFunctionBegin;
2905f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
291d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2925f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2935f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2945f1ad066SMatthew G Knepley }
2955f1ad066SMatthew G Knepley 
296521d9a4cSLisandro Dalcin /*@C
2978f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2988f1509bcSBarry Smith 
299d083f849SBarry Smith    Logically Collective on dm
3008f1509bcSBarry Smith 
3018f1509bcSBarry Smith    Input Parameters:
3028f1509bcSBarry Smith +  dm - the DM context
3038f1509bcSBarry Smith -  ctype - the matrix type
3048f1509bcSBarry Smith 
3058f1509bcSBarry Smith    Options Database:
3068f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3078f1509bcSBarry Smith 
3088f1509bcSBarry Smith    Level: intermediate
3098f1509bcSBarry Smith 
3108f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3118f1509bcSBarry Smith           DMGetISColoringType()
3128f1509bcSBarry Smith @*/
3138f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3148f1509bcSBarry Smith {
3158f1509bcSBarry Smith   PetscFunctionBegin;
3168f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3178f1509bcSBarry Smith   dm->coloringtype = ctype;
3188f1509bcSBarry Smith   PetscFunctionReturn(0);
3198f1509bcSBarry Smith }
3208f1509bcSBarry Smith 
3218f1509bcSBarry Smith /*@C
3228f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
323521d9a4cSLisandro Dalcin 
324d083f849SBarry Smith    Logically Collective on dm
325521d9a4cSLisandro Dalcin 
326521d9a4cSLisandro Dalcin    Input Parameter:
3278f1509bcSBarry Smith .  dm - the DM context
3288f1509bcSBarry Smith 
3298f1509bcSBarry Smith    Output Parameter:
3308f1509bcSBarry Smith .  ctype - the matrix type
3318f1509bcSBarry Smith 
3328f1509bcSBarry Smith    Options Database:
3338f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3348f1509bcSBarry Smith 
3358f1509bcSBarry Smith    Level: intermediate
3368f1509bcSBarry Smith 
3378f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3388f1509bcSBarry Smith           DMGetISColoringType()
3398f1509bcSBarry Smith @*/
3408f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3418f1509bcSBarry Smith {
3428f1509bcSBarry Smith   PetscFunctionBegin;
3438f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3448f1509bcSBarry Smith   *ctype = dm->coloringtype;
3458f1509bcSBarry Smith   PetscFunctionReturn(0);
3468f1509bcSBarry Smith }
3478f1509bcSBarry Smith 
3488f1509bcSBarry Smith /*@C
3498f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3508f1509bcSBarry Smith 
351d083f849SBarry Smith    Logically Collective on dm
3528f1509bcSBarry Smith 
3538f1509bcSBarry Smith    Input Parameters:
354521d9a4cSLisandro Dalcin +  dm - the DM context
355a2b5a043SBarry Smith -  ctype - the matrix type
356521d9a4cSLisandro Dalcin 
357521d9a4cSLisandro Dalcin    Options Database:
358521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
359521d9a4cSLisandro Dalcin 
360521d9a4cSLisandro Dalcin    Level: intermediate
361521d9a4cSLisandro Dalcin 
362a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
363521d9a4cSLisandro Dalcin @*/
36419fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
365521d9a4cSLisandro Dalcin {
366521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
36788f0584fSBarry Smith 
368521d9a4cSLisandro Dalcin   PetscFunctionBegin;
369521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
370521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
37119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
372521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
373521d9a4cSLisandro Dalcin }
374521d9a4cSLisandro Dalcin 
375c0dedaeaSBarry Smith /*@C
376c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
377c0dedaeaSBarry Smith 
378d083f849SBarry Smith    Logically Collective on dm
379c0dedaeaSBarry Smith 
380c0dedaeaSBarry Smith    Input Parameter:
381c0dedaeaSBarry Smith .  dm - the DM context
382c0dedaeaSBarry Smith 
383c0dedaeaSBarry Smith    Output Parameter:
384c0dedaeaSBarry Smith .  ctype - the matrix type
385c0dedaeaSBarry Smith 
386c0dedaeaSBarry Smith    Options Database:
387c0dedaeaSBarry Smith .   -dm_mat_type ctype
388c0dedaeaSBarry Smith 
389c0dedaeaSBarry Smith    Level: intermediate
390c0dedaeaSBarry Smith 
391a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
392c0dedaeaSBarry Smith @*/
393c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
394c0dedaeaSBarry Smith {
395c0dedaeaSBarry Smith   PetscFunctionBegin;
396c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
397c0dedaeaSBarry Smith   *ctype = dm->mattype;
398c0dedaeaSBarry Smith   PetscFunctionReturn(0);
399c0dedaeaSBarry Smith }
400c0dedaeaSBarry Smith 
401c688c046SMatthew G Knepley /*@
40234f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
403c688c046SMatthew G Knepley 
404c688c046SMatthew G Knepley   Not collective
405c688c046SMatthew G Knepley 
406c688c046SMatthew G Knepley   Input Parameter:
407c688c046SMatthew G Knepley . A - The Mat
408c688c046SMatthew G Knepley 
409c688c046SMatthew G Knepley   Output Parameter:
410c688c046SMatthew G Knepley . dm - The DM
411c688c046SMatthew G Knepley 
412c688c046SMatthew G Knepley   Level: intermediate
413c688c046SMatthew G Knepley 
4148f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4158f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4168f1509bcSBarry Smith 
417c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
418c688c046SMatthew G Knepley @*/
419c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
420c688c046SMatthew G Knepley {
421c688c046SMatthew G Knepley   PetscErrorCode ierr;
422c688c046SMatthew G Knepley 
423c688c046SMatthew G Knepley   PetscFunctionBegin;
424c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
425c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
426c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
427c688c046SMatthew G Knepley   PetscFunctionReturn(0);
428c688c046SMatthew G Knepley }
429c688c046SMatthew G Knepley 
430c688c046SMatthew G Knepley /*@
431c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
432c688c046SMatthew G Knepley 
433c688c046SMatthew G Knepley   Not collective
434c688c046SMatthew G Knepley 
435c688c046SMatthew G Knepley   Input Parameters:
436c688c046SMatthew G Knepley + A - The Mat
437c688c046SMatthew G Knepley - dm - The DM
438c688c046SMatthew G Knepley 
439c688c046SMatthew G Knepley   Level: intermediate
440c688c046SMatthew G Knepley 
4418f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4428f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4438f1509bcSBarry Smith 
444c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
445c688c046SMatthew G Knepley @*/
446c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
447c688c046SMatthew G Knepley {
448c688c046SMatthew G Knepley   PetscErrorCode ierr;
449c688c046SMatthew G Knepley 
450c688c046SMatthew G Knepley   PetscFunctionBegin;
451c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4528865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
453c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
454c688c046SMatthew G Knepley   PetscFunctionReturn(0);
455c688c046SMatthew G Knepley }
456c688c046SMatthew G Knepley 
4579a42bb27SBarry Smith /*@C
4589a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4596757b960SDave May    DM options in the database.
4609a42bb27SBarry Smith 
461d083f849SBarry Smith    Logically Collective on dm
4629a42bb27SBarry Smith 
463d8d19677SJose E. Roman    Input Parameters:
4648353ddbbSDave May +  da - the DM context
4659a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4669a42bb27SBarry Smith 
4679a42bb27SBarry Smith    Notes:
4689a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4699a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4709a42bb27SBarry Smith 
4719a42bb27SBarry Smith    Level: advanced
4729a42bb27SBarry Smith 
4739a42bb27SBarry Smith .seealso: DMSetFromOptions()
4749a42bb27SBarry Smith @*/
4757087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4769a42bb27SBarry Smith {
4779a42bb27SBarry Smith   PetscErrorCode ierr;
4789a42bb27SBarry Smith 
4799a42bb27SBarry Smith   PetscFunctionBegin;
4809a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4819a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
482691be533SLawrence Mitchell   if (dm->sf) {
483691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
484691be533SLawrence Mitchell   }
4851bb6d2a8SBarry Smith   if (dm->sectionSF) {
4861bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
487691be533SLawrence Mitchell   }
4889a42bb27SBarry Smith   PetscFunctionReturn(0);
4899a42bb27SBarry Smith }
4909a42bb27SBarry Smith 
49131697293SDave May /*@C
49231697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
49331697293SDave May    DM options in the database.
49431697293SDave May 
495d083f849SBarry Smith    Logically Collective on dm
49631697293SDave May 
49731697293SDave May    Input Parameters:
49831697293SDave May +  dm - the DM context
49931697293SDave May -  prefix - the prefix string to prepend to all DM option requests
50031697293SDave May 
50131697293SDave May    Notes:
50231697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
50331697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
50431697293SDave May 
50531697293SDave May    Level: advanced
50631697293SDave May 
50731697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
50831697293SDave May @*/
50931697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
51031697293SDave May {
51131697293SDave May   PetscErrorCode ierr;
51231697293SDave May 
51331697293SDave May   PetscFunctionBegin;
51431697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
51531697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
51631697293SDave May   PetscFunctionReturn(0);
51731697293SDave May }
51831697293SDave May 
51931697293SDave May /*@C
52031697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
52131697293SDave May    DM options in the database.
52231697293SDave May 
52331697293SDave May    Not Collective
52431697293SDave May 
52531697293SDave May    Input Parameters:
52631697293SDave May .  dm - the DM context
52731697293SDave May 
52831697293SDave May    Output Parameters:
52931697293SDave May .  prefix - pointer to the prefix string used is returned
53031697293SDave May 
53195452b02SPatrick Sanan    Notes:
53295452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
53331697293SDave May    sufficient length to hold the prefix.
53431697293SDave May 
53531697293SDave May    Level: advanced
53631697293SDave May 
53731697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
53831697293SDave May @*/
53931697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
54031697293SDave May {
54131697293SDave May   PetscErrorCode ierr;
54231697293SDave May 
54331697293SDave May   PetscFunctionBegin;
54431697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54531697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
54631697293SDave May   PetscFunctionReturn(0);
54731697293SDave May }
54831697293SDave May 
54988bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
55088bdff64SToby Isaac {
5516eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
55288bdff64SToby Isaac   PetscErrorCode ierr;
55388bdff64SToby Isaac 
55488bdff64SToby Isaac   PetscFunctionBegin;
555aab5bcd8SJed Brown   *ncrefct = 0;
55688bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55788bdff64SToby Isaac     refct--;
55888bdff64SToby Isaac     if (recurseCoarse) {
55988bdff64SToby Isaac       PetscInt coarseCount;
56088bdff64SToby Isaac 
56188bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
56288bdff64SToby Isaac       refct += coarseCount;
56388bdff64SToby Isaac     }
56488bdff64SToby Isaac   }
56588bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56688bdff64SToby Isaac     refct--;
56788bdff64SToby Isaac     if (recurseFine) {
56888bdff64SToby Isaac       PetscInt fineCount;
56988bdff64SToby Isaac 
57088bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
57188bdff64SToby Isaac       refct += fineCount;
57288bdff64SToby Isaac     }
57388bdff64SToby Isaac   }
57488bdff64SToby Isaac   *ncrefct = refct;
57588bdff64SToby Isaac   PetscFunctionReturn(0);
57688bdff64SToby Isaac }
57788bdff64SToby Isaac 
578f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
579354557abSToby Isaac {
5805d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
581354557abSToby Isaac   PetscErrorCode ierr;
582354557abSToby Isaac 
583354557abSToby Isaac   PetscFunctionBegin;
584354557abSToby Isaac   /* destroy the labels */
585354557abSToby Isaac   while (next) {
586354557abSToby Isaac     DMLabelLink tmp = next->next;
587354557abSToby Isaac 
5885d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
589ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
590354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
591354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
592354557abSToby Isaac     next = tmp;
593354557abSToby Isaac   }
5945d80c0bfSVaclav Hapla   dm->labels = NULL;
595354557abSToby Isaac   PetscFunctionReturn(0);
596354557abSToby Isaac }
597354557abSToby Isaac 
5981fb7b255SJunchao Zhang /*@C
5998472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
60047c6ae99SBarry Smith 
601d083f849SBarry Smith     Collective on dm
60247c6ae99SBarry Smith 
60347c6ae99SBarry Smith     Input Parameter:
60447c6ae99SBarry Smith .   dm - the DM object to destroy
60547c6ae99SBarry Smith 
60647c6ae99SBarry Smith     Level: developer
60747c6ae99SBarry Smith 
608e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60947c6ae99SBarry Smith 
61047c6ae99SBarry Smith @*/
611fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
61247c6ae99SBarry Smith {
6136eb26441SStefano Zampini   PetscInt       cnt;
614dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61547c6ae99SBarry Smith   PetscErrorCode ierr;
61647c6ae99SBarry Smith 
61747c6ae99SBarry Smith   PetscFunctionBegin;
6186bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6196bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
62087e657c6SBarry Smith 
62188bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
62288bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
62388bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
624ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6256bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6266bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6276eb26441SStefano Zampini 
6286eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6296eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6306eb26441SStefano Zampini 
631f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6320298fd71SBarry Smith   (*dm)->namedglobal = NULL;
633f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6342348bcf4SPeter Brune     nnext = nlink->next;
6352348bcf4SPeter 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);
6362348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6372348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6382348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6392348bcf4SPeter Brune   }
640f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6410298fd71SBarry Smith   (*dm)->namedlocal = NULL;
642f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
643f490541aSPeter Brune     nnext = nlink->next;
644f490541aSPeter 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);
645f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
646f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
647f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
648f490541aSPeter Brune   }
6492348bcf4SPeter Brune 
650b17ce1afSJed Brown   /* Destroy the list of hooks */
651c833c3b5SJed Brown   {
652c833c3b5SJed Brown     DMCoarsenHookLink link,next;
653b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
654b17ce1afSJed Brown       next = link->next;
655b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
656b17ce1afSJed Brown     }
6570298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
658c833c3b5SJed Brown   }
659c833c3b5SJed Brown   {
660c833c3b5SJed Brown     DMRefineHookLink link,next;
661c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
662c833c3b5SJed Brown       next = link->next;
663c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
664c833c3b5SJed Brown     }
6650298fd71SBarry Smith     (*dm)->refinehook = NULL;
666c833c3b5SJed Brown   }
667be081cd6SPeter Brune   {
668be081cd6SPeter Brune     DMSubDomainHookLink link,next;
669be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
670be081cd6SPeter Brune       next = link->next;
671be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
672be081cd6SPeter Brune     }
6730298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
674be081cd6SPeter Brune   }
675baf369e7SPeter Brune   {
676baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
677baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
678baf369e7SPeter Brune       next = link->next;
679baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
680baf369e7SPeter Brune     }
6810298fd71SBarry Smith     (*dm)->gtolhook = NULL;
682baf369e7SPeter Brune   }
683d4d07f1eSToby Isaac   {
684d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
685d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
686d4d07f1eSToby Isaac       next = link->next;
687d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
688d4d07f1eSToby Isaac     }
689d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
690d4d07f1eSToby Isaac   }
691aa1993deSMatthew G Knepley   /* Destroy the work arrays */
692aa1993deSMatthew G Knepley   {
693aa1993deSMatthew G Knepley     DMWorkLink link,next;
694aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
695aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
696aa1993deSMatthew G Knepley       next = link->next;
697aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
698aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
699aa1993deSMatthew G Knepley     }
7000298fd71SBarry Smith     (*dm)->workin = NULL;
701aa1993deSMatthew G Knepley   }
702c58f1c22SToby Isaac   /* destroy the labels */
703f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
704f4cdcedcSVaclav Hapla   /* destroy the fields */
705e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
706f4cdcedcSVaclav Hapla   /* destroy the boundaries */
707e6f8dbb6SToby Isaac   {
708e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
709e6f8dbb6SToby Isaac     while (next) {
710e6f8dbb6SToby Isaac       DMBoundary b = next;
711e6f8dbb6SToby Isaac 
712e6f8dbb6SToby Isaac       next = b->next;
713e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
714e6f8dbb6SToby Isaac     }
715e6f8dbb6SToby Isaac   }
716b17ce1afSJed Brown 
71752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
71852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
71952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
72052536dc3SBarry Smith 
7211a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7221a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7231a266240SBarry Smith   }
72471cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7256bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7266bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
727073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
72888ed4aceSMatthew G Knepley 
7291bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7301bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7318b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
732fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
733fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
73488ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7351bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
736736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
737736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7388e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
739736995cdSBlaise Bourdin     }
740736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
741736995cdSBlaise Bourdin   }
7429a2a23afSMatthew G. Knepley   {
7439a2a23afSMatthew G. Knepley     Vec     *auxData;
7449a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7459a2a23afSMatthew G. Knepley 
7469a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxGetSize((*dm)->auxData, &n);CHKERRQ(ierr);
7479a2a23afSMatthew G. Knepley     ierr = PetscMalloc1(n, &auxData);CHKERRQ(ierr);
7489a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxGetVals((*dm)->auxData, &off, auxData);CHKERRQ(ierr);
7499a2a23afSMatthew G. Knepley     for (i = 0; i < n; ++i) {ierr = VecDestroy(&auxData[i]);CHKERRQ(ierr);}
7509a2a23afSMatthew G. Knepley     ierr = PetscFree(auxData);CHKERRQ(ierr);
7519a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxDestroy(&(*dm)->auxData);CHKERRQ(ierr);
7529a2a23afSMatthew G. Knepley   }
75388bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
75488bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
75588bdff64SToby Isaac   }
7566eb26441SStefano Zampini 
757a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
75888bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
75988bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
76088bdff64SToby Isaac   }
76188bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
762f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7636636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7646636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7656636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
766412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
767412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
768412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
769ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
770ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
771ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7726636e97aSMatthew G Knepley 
773e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
77414f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
775e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
776e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
777732e2eb9SMatthew G Knepley 
778ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7796bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
780ed3c66a1SDave May   }
781c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
782f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
783f918ec44SMatthew G. Knepley   ierr = CeedElemRestrictionDestroy(&(*dm)->ceedERestrict);CHKERRQ(ierr);
784f918ec44SMatthew G. Knepley   ierr = CeedDestroy(&(*dm)->ceed);CHKERRQ(ierr);
785f918ec44SMatthew G. Knepley #endif
786435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
787732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
78847c6ae99SBarry Smith   PetscFunctionReturn(0);
78947c6ae99SBarry Smith }
79047c6ae99SBarry Smith 
791d7bf68aeSBarry Smith /*@
792d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
793d7bf68aeSBarry Smith 
794d083f849SBarry Smith     Collective on dm
795d7bf68aeSBarry Smith 
796d7bf68aeSBarry Smith     Input Parameter:
797d7bf68aeSBarry Smith .   dm - the DM object to setup
798d7bf68aeSBarry Smith 
799d7bf68aeSBarry Smith     Level: developer
800d7bf68aeSBarry Smith 
801e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
802d7bf68aeSBarry Smith 
803d7bf68aeSBarry Smith @*/
8047087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
805d7bf68aeSBarry Smith {
806d7bf68aeSBarry Smith   PetscErrorCode ierr;
807d7bf68aeSBarry Smith 
808d7bf68aeSBarry Smith   PetscFunctionBegin;
809171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8108387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
811d7bf68aeSBarry Smith   if (dm->ops->setup) {
812d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
813d7bf68aeSBarry Smith   }
8148387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
815d7bf68aeSBarry Smith   PetscFunctionReturn(0);
816d7bf68aeSBarry Smith }
817d7bf68aeSBarry Smith 
818d7bf68aeSBarry Smith /*@
819d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
820d7bf68aeSBarry Smith 
821d083f849SBarry Smith     Collective on dm
822d7bf68aeSBarry Smith 
823d7bf68aeSBarry Smith     Input Parameter:
824d7bf68aeSBarry Smith .   dm - the DM object to set options for
825d7bf68aeSBarry Smith 
826732e2eb9SMatthew G Knepley     Options Database:
8274164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8284164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8294164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8308f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
831732e2eb9SMatthew G Knepley 
8329318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8339318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8349318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
8359318fe57SMatthew G. Knepley . -dm_plex_shape <shape>            - The domain shape, such as DM_SHAPE_BOX, DM_SHAPE_SPHERE, etc.
8369318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8379318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8389318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
8399318fe57SMatthew G. Knepley . -dm_plex_simplex <bool>           - PETSC_TRUE for simplex elements, PETSC_FALSE for tensor elements
8409318fe57SMatthew G. Knepley . -dm_plex_interpolate <bool>       - PETSC_TRUE turns on topological interpolation (creating edges and faces)
8419318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8429318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8439318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8449318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
8459318fe57SMatthew G. Knepley . -dm_plex_box_bd <bx,by,bz>        - Specify the DMBoundaryType for each direction
8469318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8479318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8489318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8499318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
8509318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8519318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8529318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8539318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
854*d410b0cfSMatthew G. Knepley . -dm_extrude <l>                   - Acticate extrusion and apecify the number of layers to extrude
855*d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
856*d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
857*d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
858*d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
859*d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
860909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
861909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8629318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8639318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8649318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8659318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8669318fe57SMatthew G. Knepley 
867384a6580SVaclav Hapla     DMPLEX Specific Checks
868384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
869384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
870384a6580SVaclav 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()
871384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
872384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
873384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
874384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
875d7bf68aeSBarry Smith 
87695eb5ee5SVaclav Hapla     Level: intermediate
87795eb5ee5SVaclav Hapla 
87895eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
87995eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
880d7bf68aeSBarry Smith 
881d7bf68aeSBarry Smith @*/
8827087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
883d7bf68aeSBarry Smith {
8847781c08eSBarry Smith   char           typeName[256];
885ca266f36SBarry Smith   PetscBool      flg;
886d7bf68aeSBarry Smith   PetscErrorCode ierr;
887d7bf68aeSBarry Smith 
888d7bf68aeSBarry Smith   PetscFunctionBegin;
889171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
89049be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
89149be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8921bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8933194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8940298fd71SBarry 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);
895a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
896f9ba7244SBarry Smith   if (flg) {
897f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
898f9ba7244SBarry Smith   }
899a264d7a6SBarry 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);
900073dac72SJed Brown   if (flg) {
901521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
902073dac72SJed Brown   }
9038f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
904f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
905e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
906f9ba7244SBarry Smith   }
907f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
9080633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
90982fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
910d7bf68aeSBarry Smith   PetscFunctionReturn(0);
911d7bf68aeSBarry Smith }
912d7bf68aeSBarry Smith 
913fc9bc008SSatish Balay /*@C
914fe2efc57SMark    DMViewFromOptions - View from Options
915fe2efc57SMark 
916fe2efc57SMark    Collective on DM
917fe2efc57SMark 
918fe2efc57SMark    Input Parameters:
919fe2efc57SMark +  dm - the DM object
920736c3998SJose E. Roman .  obj - Optional object
921736c3998SJose E. Roman -  name - command line option
922fe2efc57SMark 
923fe2efc57SMark    Level: intermediate
924fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
925fe2efc57SMark @*/
926fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
927fe2efc57SMark {
928fe2efc57SMark   PetscErrorCode ierr;
929fe2efc57SMark 
930fe2efc57SMark   PetscFunctionBegin;
931fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
932fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
933fe2efc57SMark   PetscFunctionReturn(0);
934fe2efc57SMark }
935fe2efc57SMark 
936fe2efc57SMark /*@C
937224748a4SBarry Smith     DMView - Views a DM
93847c6ae99SBarry Smith 
939d083f849SBarry Smith     Collective on dm
94047c6ae99SBarry Smith 
941d8d19677SJose E. Roman     Input Parameters:
94247c6ae99SBarry Smith +   dm - the DM object to view
94347c6ae99SBarry Smith -   v - the viewer
94447c6ae99SBarry Smith 
945224748a4SBarry Smith     Level: beginner
94647c6ae99SBarry Smith 
947e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94847c6ae99SBarry Smith 
94947c6ae99SBarry Smith @*/
9507087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
95147c6ae99SBarry Smith {
95247c6ae99SBarry Smith   PetscErrorCode    ierr;
95332c0f0efSBarry Smith   PetscBool         isbinary;
95476a8abe0SBarry Smith   PetscMPIInt       size;
95576a8abe0SBarry Smith   PetscViewerFormat format;
95647c6ae99SBarry Smith 
95747c6ae99SBarry Smith   PetscFunctionBegin;
958171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9593014e516SBarry Smith   if (!v) {
960ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9613014e516SBarry Smith   }
962b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
96374903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
96474903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
96574903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
96674903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
96774903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
96874903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
96974903a4fSStefano Zampini      in an error here */
97074903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9718bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
972b1b135c8SBarry Smith 
97376a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
974ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRMPI(ierr);
97576a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
97698c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
97732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
97832c0f0efSBarry Smith   if (isbinary) {
97955849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
98032c0f0efSBarry Smith     char     type[256];
98132c0f0efSBarry Smith 
982f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
98332c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
984f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
98532c0f0efSBarry Smith   }
9860c010503SBarry Smith   if (dm->ops->view) {
9870c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
98847c6ae99SBarry Smith   }
98947c6ae99SBarry Smith   PetscFunctionReturn(0);
99047c6ae99SBarry Smith }
99147c6ae99SBarry Smith 
99247c6ae99SBarry Smith /*@
9938472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
99447c6ae99SBarry Smith 
995d083f849SBarry Smith     Collective on dm
99647c6ae99SBarry Smith 
99747c6ae99SBarry Smith     Input Parameter:
99847c6ae99SBarry Smith .   dm - the DM object
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith     Output Parameter:
100147c6ae99SBarry Smith .   vec - the global vector
100247c6ae99SBarry Smith 
1003073dac72SJed Brown     Level: beginner
100447c6ae99SBarry Smith 
1005ec075b9fSPatrick Sanan .seealso DMCreateLocalVector(), DMGetGlobalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
100647c6ae99SBarry Smith 
100747c6ae99SBarry Smith @*/
10087087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
100947c6ae99SBarry Smith {
101047c6ae99SBarry Smith   PetscErrorCode ierr;
101147c6ae99SBarry Smith 
101247c6ae99SBarry Smith   PetscFunctionBegin;
1013171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1014b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1015b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
101647c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
101776bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1018c6b011d8SStefano Zampini     DM vdm;
1019c6b011d8SStefano Zampini 
1020c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1021c6b011d8SStefano 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);
1022c6b011d8SStefano Zampini   }
102347c6ae99SBarry Smith   PetscFunctionReturn(0);
102447c6ae99SBarry Smith }
102547c6ae99SBarry Smith 
102647c6ae99SBarry Smith /*@
10278472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
102847c6ae99SBarry Smith 
102947c6ae99SBarry Smith     Not Collective
103047c6ae99SBarry Smith 
103147c6ae99SBarry Smith     Input Parameter:
103247c6ae99SBarry Smith .   dm - the DM object
103347c6ae99SBarry Smith 
103447c6ae99SBarry Smith     Output Parameter:
103547c6ae99SBarry Smith .   vec - the local vector
103647c6ae99SBarry Smith 
1037073dac72SJed Brown     Level: beginner
103847c6ae99SBarry Smith 
1039ec075b9fSPatrick Sanan .seealso DMCreateGlobalVector(), DMGetLocalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
104047c6ae99SBarry Smith 
104147c6ae99SBarry Smith @*/
10427087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
104347c6ae99SBarry Smith {
104447c6ae99SBarry Smith   PetscErrorCode ierr;
104547c6ae99SBarry Smith 
104647c6ae99SBarry Smith   PetscFunctionBegin;
1047171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1048b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1049b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
105047c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
105176bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1052c6b011d8SStefano Zampini     DM vdm;
1053c6b011d8SStefano Zampini 
1054c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1055c6b011d8SStefano 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);
1056c6b011d8SStefano Zampini   }
105747c6ae99SBarry Smith   PetscFunctionReturn(0);
105847c6ae99SBarry Smith }
105947c6ae99SBarry Smith 
10601411c6eeSJed Brown /*@
10611411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10621411c6eeSJed Brown 
1063d083f849SBarry Smith    Collective on dm
10641411c6eeSJed Brown 
10651411c6eeSJed Brown    Input Parameter:
10661411c6eeSJed Brown .  dm - the DM that provides the mapping
10671411c6eeSJed Brown 
10681411c6eeSJed Brown    Output Parameter:
10691411c6eeSJed Brown .  ltog - the mapping
10701411c6eeSJed Brown 
10711411c6eeSJed Brown    Level: intermediate
10721411c6eeSJed Brown 
10731411c6eeSJed Brown    Notes:
10741411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10751411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10761411c6eeSJed Brown 
1077fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10781411c6eeSJed Brown @*/
10797087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10801411c6eeSJed Brown {
10810be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10821411c6eeSJed Brown   PetscErrorCode ierr;
10831411c6eeSJed Brown 
10841411c6eeSJed Brown   PetscFunctionBegin;
10851411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10861411c6eeSJed Brown   PetscValidPointer(ltog,2);
10871411c6eeSJed Brown   if (!dm->ltogmap) {
108837d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
108937d0c07bSMatthew G Knepley 
109092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
109137d0c07bSMatthew G Knepley     if (section) {
1092a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
109337d0c07bSMatthew G Knepley       PetscInt       *ltog;
1094ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
109537d0c07bSMatthew G Knepley 
1096e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
109737d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1098ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1099ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
110037d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1101a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
110237d0c07bSMatthew G Knepley 
110337d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
110437d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1105a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1106a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
110737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
11081a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11091a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
11101a7dc684SMatthew G. Knepley         if (dof) {
1111b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1112b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
11131a7dc684SMatthew G. Knepley         }
111437d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1115a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1116a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
111737d0c07bSMatthew G Knepley         }
111837d0c07bSMatthew G Knepley       }
1119bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11200be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11210be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
11220be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11230be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
11247591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11257591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1126ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1127ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1128ccf3bd66SMatthew G. Knepley         n /= bs;
1129ccf3bd66SMatthew G. Knepley       }
1130ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
11313bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
113237d0c07bSMatthew G Knepley     } else {
1133b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1134184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
11351411c6eeSJed Brown     }
113637d0c07bSMatthew G Knepley   }
11371411c6eeSJed Brown   *ltog = dm->ltogmap;
11381411c6eeSJed Brown   PetscFunctionReturn(0);
11391411c6eeSJed Brown }
11401411c6eeSJed Brown 
11411411c6eeSJed Brown /*@
11421411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
11431411c6eeSJed Brown 
11441411c6eeSJed Brown    Not Collective
11451411c6eeSJed Brown 
11461411c6eeSJed Brown    Input Parameter:
11471411c6eeSJed Brown .  dm - the DM with block structure
11481411c6eeSJed Brown 
11491411c6eeSJed Brown    Output Parameter:
11501411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11511411c6eeSJed Brown 
11521411c6eeSJed Brown    Level: intermediate
11531411c6eeSJed Brown 
1154fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
11551411c6eeSJed Brown @*/
11567087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
11571411c6eeSJed Brown {
11581411c6eeSJed Brown   PetscFunctionBegin;
11591411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1160534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11611411c6eeSJed 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");
11621411c6eeSJed Brown   *bs = dm->bs;
11631411c6eeSJed Brown   PetscFunctionReturn(0);
11641411c6eeSJed Brown }
11651411c6eeSJed Brown 
116648eeb7c8SBarry Smith /*@C
11678472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
116847c6ae99SBarry Smith 
1169a5bc1bf3SBarry Smith     Collective on dmc
117047c6ae99SBarry Smith 
1171d8d19677SJose E. Roman     Input Parameters:
1172a5bc1bf3SBarry Smith +   dmc - the DM object
1173a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
117447c6ae99SBarry Smith 
1175d8d19677SJose E. Roman     Output Parameters:
117647c6ae99SBarry Smith +  mat - the interpolation
117747c6ae99SBarry Smith -  vec - the scaling (optional)
117847c6ae99SBarry Smith 
117947c6ae99SBarry Smith     Level: developer
118047c6ae99SBarry Smith 
118195452b02SPatrick Sanan     Notes:
118295452b02SPatrick 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
118385afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1184d52bd9f3SBarry Smith 
11851f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1186d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
118785afcc9aSBarry Smith 
11882ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
118947c6ae99SBarry Smith 
119047c6ae99SBarry Smith @*/
1191a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
119247c6ae99SBarry Smith {
119347c6ae99SBarry Smith   PetscErrorCode ierr;
119447c6ae99SBarry Smith 
119547c6ae99SBarry Smith   PetscFunctionBegin;
1196a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1197a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1198c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1199a5bc1bf3SBarry Smith   if (!dmc->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1200a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1201a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1202a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
120347c6ae99SBarry Smith   PetscFunctionReturn(0);
120447c6ae99SBarry Smith }
120547c6ae99SBarry Smith 
12063ad4599aSBarry Smith /*@
1207d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
12082ed6491fSPatrick Sanan 
12092ed6491fSPatrick Sanan   Input Parameters:
12102ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
12112ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
12122ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12132ed6491fSPatrick Sanan 
12142ed6491fSPatrick Sanan   Output Parameter:
12152ed6491fSPatrick Sanan .    scale - the scaled vector
12162ed6491fSPatrick Sanan 
12172ed6491fSPatrick Sanan   Level: developer
12182ed6491fSPatrick Sanan 
12192ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
12202ed6491fSPatrick Sanan 
12212ed6491fSPatrick Sanan @*/
12222ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
12232ed6491fSPatrick Sanan {
12242ed6491fSPatrick Sanan   PetscErrorCode ierr;
12252ed6491fSPatrick Sanan   Vec            fine;
12262ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
12272ed6491fSPatrick Sanan 
12282ed6491fSPatrick Sanan   PetscFunctionBegin;
12292ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
12302ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
12312ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
12322ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
12332ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
12342ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
12352ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12362ed6491fSPatrick Sanan }
12372ed6491fSPatrick Sanan 
12382ed6491fSPatrick Sanan /*@
12393ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
12403ad4599aSBarry Smith 
1241a5bc1bf3SBarry Smith     Collective on dmc
12423ad4599aSBarry Smith 
1243d8d19677SJose E. Roman     Input Parameters:
1244a5bc1bf3SBarry Smith +   dmc - the DM object
1245a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
12463ad4599aSBarry Smith 
12473ad4599aSBarry Smith     Output Parameter:
12483ad4599aSBarry Smith .  mat - the restriction
12493ad4599aSBarry Smith 
12503ad4599aSBarry Smith     Level: developer
12513ad4599aSBarry Smith 
125295452b02SPatrick Sanan     Notes:
125395452b02SPatrick 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
12543ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
12553ad4599aSBarry Smith 
12563ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
12573ad4599aSBarry Smith 
12583ad4599aSBarry Smith @*/
1259a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12603ad4599aSBarry Smith {
12613ad4599aSBarry Smith   PetscErrorCode ierr;
12623ad4599aSBarry Smith 
12633ad4599aSBarry Smith   PetscFunctionBegin;
1264a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1265a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12665a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1267a5bc1bf3SBarry Smith   if (!dmc->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1268a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1269a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1270a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
12713ad4599aSBarry Smith   PetscFunctionReturn(0);
12723ad4599aSBarry Smith }
12733ad4599aSBarry Smith 
127447c6ae99SBarry Smith /*@
12758472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
127647c6ae99SBarry Smith 
1277a5bc1bf3SBarry Smith     Collective on dac
127847c6ae99SBarry Smith 
1279d8d19677SJose E. Roman     Input Parameters:
1280a5bc1bf3SBarry Smith +   dac - the DM object
1281a5bc1bf3SBarry Smith -   daf - the second, finer DM object
128247c6ae99SBarry Smith 
128347c6ae99SBarry Smith     Output Parameter:
12846dbf9973SLawrence Mitchell .   mat - the injection
128547c6ae99SBarry Smith 
128647c6ae99SBarry Smith     Level: developer
128747c6ae99SBarry Smith 
128895452b02SPatrick Sanan    Notes:
128995452b02SPatrick 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
129085afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
129185afcc9aSBarry Smith 
1292e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
129347c6ae99SBarry Smith 
129447c6ae99SBarry Smith @*/
1295a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
129647c6ae99SBarry Smith {
129747c6ae99SBarry Smith   PetscErrorCode ierr;
129847c6ae99SBarry Smith 
129947c6ae99SBarry Smith   PetscFunctionBegin;
1300a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1301a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
13025a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1303a5bc1bf3SBarry Smith   if (!dac->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1304a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1305a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1306a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
130747c6ae99SBarry Smith   PetscFunctionReturn(0);
130847c6ae99SBarry Smith }
130947c6ae99SBarry Smith 
1310b412c318SBarry Smith /*@
1311bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1312bd041c0cSMatthew G. Knepley 
1313a5bc1bf3SBarry Smith   Collective on dac
1314bd041c0cSMatthew G. Knepley 
1315d8d19677SJose E. Roman   Input Parameters:
1316a5bc1bf3SBarry Smith + dac - the DM object
1317a5bc1bf3SBarry Smith - daf - the second, finer DM object
1318bd041c0cSMatthew G. Knepley 
1319bd041c0cSMatthew G. Knepley   Output Parameter:
1320bd041c0cSMatthew G. Knepley . mat - the interpolation
1321bd041c0cSMatthew G. Knepley 
1322bd041c0cSMatthew G. Knepley   Level: developer
1323bd041c0cSMatthew G. Knepley 
1324bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1325bd041c0cSMatthew G. Knepley @*/
1326a5bc1bf3SBarry Smith PetscErrorCode DMCreateMassMatrix(DM dac, DM daf, Mat *mat)
1327bd041c0cSMatthew G. Knepley {
1328bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1329bd041c0cSMatthew G. Knepley 
1330bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1331a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1332a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13335a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1334a5bc1bf3SBarry Smith   if (!dac->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dac)->type_name);
1335a5bc1bf3SBarry Smith   ierr = (*dac->ops->createmassmatrix)(dac, daf, mat);CHKERRQ(ierr);
1336bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1337bd041c0cSMatthew G. Knepley }
1338bd041c0cSMatthew G. Knepley 
1339bd041c0cSMatthew G. Knepley /*@
1340b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
134147c6ae99SBarry Smith 
1342d083f849SBarry Smith     Collective on dm
134347c6ae99SBarry Smith 
1344d8d19677SJose E. Roman     Input Parameters:
134547c6ae99SBarry Smith +   dm - the DM object
13465bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
134747c6ae99SBarry Smith 
134847c6ae99SBarry Smith     Output Parameter:
134947c6ae99SBarry Smith .   coloring - the coloring
135047c6ae99SBarry Smith 
1351ec5066bdSBarry Smith     Notes:
1352ec5066bdSBarry 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
1353ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1354ec5066bdSBarry Smith 
1355ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1356ec5066bdSBarry Smith 
135747c6ae99SBarry Smith     Level: developer
135847c6ae99SBarry Smith 
1359ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
136047c6ae99SBarry Smith 
1361aab9d709SJed Brown @*/
1362b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
136347c6ae99SBarry Smith {
136447c6ae99SBarry Smith   PetscErrorCode ierr;
136547c6ae99SBarry Smith 
136647c6ae99SBarry Smith   PetscFunctionBegin;
1367171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13685a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1369b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1370b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
137147c6ae99SBarry Smith   PetscFunctionReturn(0);
137247c6ae99SBarry Smith }
137347c6ae99SBarry Smith 
1374b412c318SBarry Smith /*@
13758472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
137647c6ae99SBarry Smith 
1377d083f849SBarry Smith     Collective on dm
137847c6ae99SBarry Smith 
137947c6ae99SBarry Smith     Input Parameter:
1380b412c318SBarry Smith .   dm - the DM object
138147c6ae99SBarry Smith 
138247c6ae99SBarry Smith     Output Parameter:
138347c6ae99SBarry Smith .   mat - the empty Jacobian
138447c6ae99SBarry Smith 
1385073dac72SJed Brown     Level: beginner
138647c6ae99SBarry Smith 
1387f27dd7c6SMatthew G. Knepley     Options Database Keys:
1388f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1389f27dd7c6SMatthew G. Knepley 
139095452b02SPatrick Sanan     Notes:
139195452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
139294013140SBarry Smith        do not need to do it yourself.
139394013140SBarry Smith 
139494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13957889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
139694013140SBarry Smith 
139794013140SBarry 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
139894013140SBarry Smith        internally by PETSc.
139994013140SBarry Smith 
140094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1401aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
140294013140SBarry Smith 
1403b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
140447c6ae99SBarry Smith 
1405aab9d709SJed Brown @*/
1406b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
140747c6ae99SBarry Smith {
140847c6ae99SBarry Smith   PetscErrorCode ierr;
140947c6ae99SBarry Smith 
141047c6ae99SBarry Smith   PetscFunctionBegin;
1411171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1412064a246eSJacob Faibussowitsch   PetscValidPointer(mat,2);
1413b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
14145a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1415fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1416b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
141776bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1418c6b011d8SStefano Zampini     DM mdm;
1419c6b011d8SStefano Zampini 
1420c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1421c6b011d8SStefano 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);
1422c6b011d8SStefano Zampini   }
1423e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1424e5e52638SMatthew G. Knepley   if (dm->Nf) {
1425e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1426649ef022SMatthew Knepley     PetscInt     Nf, f;
1427e571a35bSMatthew G. Knepley 
1428e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1429649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1430649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
1431649ef022SMatthew Knepley         ierr = (*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1432e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1433e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1434649ef022SMatthew Knepley         break;
1435e571a35bSMatthew G. Knepley       }
1436649ef022SMatthew Knepley     }
1437649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1438649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
1439649ef022SMatthew Knepley         ierr = (*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1440e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1441e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1442e571a35bSMatthew G. Knepley       }
1443e571a35bSMatthew G. Knepley     }
1444e571a35bSMatthew G. Knepley   }
1445fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
144647c6ae99SBarry Smith   PetscFunctionReturn(0);
144747c6ae99SBarry Smith }
144847c6ae99SBarry Smith 
1449732e2eb9SMatthew G Knepley /*@
1450950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1451732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1452732e2eb9SMatthew G Knepley 
1453d083f849SBarry Smith   Logically Collective on dm
1454732e2eb9SMatthew G Knepley 
1455d8d19677SJose E. Roman   Input Parameters:
1456732e2eb9SMatthew G Knepley + dm - the DM
1457732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1458732e2eb9SMatthew G Knepley 
1459732e2eb9SMatthew G Knepley   Level: developer
1460f27dd7c6SMatthew G. Knepley 
1461f27dd7c6SMatthew G. Knepley   Options Database Keys:
1462f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1463f27dd7c6SMatthew G. Knepley 
1464b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1465732e2eb9SMatthew G Knepley @*/
1466732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1467732e2eb9SMatthew G Knepley {
1468732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1469732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1470732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1471732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1472732e2eb9SMatthew G Knepley }
1473732e2eb9SMatthew G Knepley 
1474b06ff27eSHong Zhang /*@
1475b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1476b06ff27eSHong Zhang     but the array for values will not be allocated.
1477b06ff27eSHong Zhang 
1478d083f849SBarry Smith   Logically Collective on dm
1479b06ff27eSHong Zhang 
1480d8d19677SJose E. Roman   Input Parameters:
1481b06ff27eSHong Zhang + dm - the DM
1482b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1483b06ff27eSHong Zhang 
1484b06ff27eSHong Zhang   Level: developer
1485b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1486b06ff27eSHong Zhang @*/
1487b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1488b06ff27eSHong Zhang {
1489b06ff27eSHong Zhang   PetscFunctionBegin;
1490b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1491b06ff27eSHong Zhang   dm->structure_only = only;
1492b06ff27eSHong Zhang   PetscFunctionReturn(0);
1493b06ff27eSHong Zhang }
1494b06ff27eSHong Zhang 
1495a89ea682SMatthew G Knepley /*@C
1496aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1497a89ea682SMatthew G Knepley 
1498a89ea682SMatthew G Knepley   Not Collective
1499a89ea682SMatthew G Knepley 
1500a89ea682SMatthew G Knepley   Input Parameters:
1501a89ea682SMatthew G Knepley + dm - the DM object
1502a5b23f4aSJose E. Roman . count - The minimum size
150369291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1504a89ea682SMatthew G Knepley 
1505a89ea682SMatthew G Knepley   Output Parameter:
1506a89ea682SMatthew G Knepley . array - the work array
1507a89ea682SMatthew G Knepley 
1508a89ea682SMatthew G Knepley   Level: developer
1509a89ea682SMatthew G Knepley 
1510a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1511a89ea682SMatthew G Knepley @*/
151269291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1513a89ea682SMatthew G Knepley {
1514a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1515aa1993deSMatthew G Knepley   DMWorkLink     link;
151669291d52SBarry Smith   PetscMPIInt    dsize;
1517a89ea682SMatthew G Knepley 
1518a89ea682SMatthew G Knepley   PetscFunctionBegin;
1519a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1520aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1521aa1993deSMatthew G Knepley   if (dm->workin) {
1522aa1993deSMatthew G Knepley     link       = dm->workin;
1523aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1524aa1993deSMatthew G Knepley   } else {
1525b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1526a89ea682SMatthew G Knepley   }
1527ffc4695bSBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRMPI(ierr);
15285056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1529aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1530854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1531854ce69bSBarry Smith     link->bytes = dsize*count;
1532aa1993deSMatthew G Knepley   }
1533aa1993deSMatthew G Knepley   link->next   = dm->workout;
1534aa1993deSMatthew G Knepley   dm->workout  = link;
153500d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
153600d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
153700d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
153800d952a4SJed Brown #endif
1539aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1540a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1541a89ea682SMatthew G Knepley }
1542a89ea682SMatthew G Knepley 
1543aa1993deSMatthew G Knepley /*@C
1544aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1545aa1993deSMatthew G Knepley 
1546aa1993deSMatthew G Knepley   Not Collective
1547aa1993deSMatthew G Knepley 
1548aa1993deSMatthew G Knepley   Input Parameters:
1549aa1993deSMatthew G Knepley + dm - the DM object
1550a5b23f4aSJose E. Roman . count - The minimum size
155169291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1552aa1993deSMatthew G Knepley 
1553aa1993deSMatthew G Knepley   Output Parameter:
1554aa1993deSMatthew G Knepley . array - the work array
1555aa1993deSMatthew G Knepley 
1556aa1993deSMatthew G Knepley   Level: developer
1557aa1993deSMatthew G Knepley 
155895452b02SPatrick Sanan   Developer Notes:
155995452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1560aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1561aa1993deSMatthew G Knepley @*/
156269291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1563aa1993deSMatthew G Knepley {
1564aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1565aa1993deSMatthew G Knepley 
1566aa1993deSMatthew G Knepley   PetscFunctionBegin;
1567aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1568aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1569aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1570aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1571aa1993deSMatthew G Knepley       *p           = link->next;
1572aa1993deSMatthew G Knepley       link->next   = dm->workin;
1573aa1993deSMatthew G Knepley       dm->workin   = link;
15740298fd71SBarry Smith       *(void**)mem = NULL;
1575aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1576aa1993deSMatthew G Knepley     }
1577aa1993deSMatthew G Knepley   }
1578aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1579aa1993deSMatthew G Knepley }
1580e7c4fc90SDmitry Karpeev 
15818cda7954SMatthew G. Knepley /*@C
15828cda7954SMatthew G. Knepley   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field
15838cda7954SMatthew G. Knepley 
15848cda7954SMatthew G. Knepley   Logically collective on DM
15858cda7954SMatthew G. Knepley 
15868cda7954SMatthew G. Knepley   Input Parameters:
15878cda7954SMatthew G. Knepley + dm     - The DM
15888cda7954SMatthew G. Knepley . field  - The field number for the nullspace
15898cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
15908cda7954SMatthew G. Knepley 
15918cda7954SMatthew G. Knepley   Notes:
15928cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15938cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15948cda7954SMatthew G. Knepley $ dm        - The present DM
15958cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15968cda7954SMatthew G. Knepley $ field     - The field number in dm
15978cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15988cda7954SMatthew G. Knepley 
15998cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16008cda7954SMatthew G. Knepley 
16018cda7954SMatthew G. Knepley .seealso: DMGetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16028cda7954SMatthew G. Knepley */
16038cda7954SMatthew G. Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1604435a35e8SMatthew G Knepley {
1605435a35e8SMatthew G Knepley   PetscFunctionBegin;
1606435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
160782f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1608435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1609435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1610435a35e8SMatthew G Knepley }
1611435a35e8SMatthew G Knepley 
16128cda7954SMatthew G. Knepley /*@C
16138cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
16148cda7954SMatthew G. Knepley 
16158cda7954SMatthew G. Knepley   Not collective
16168cda7954SMatthew G. Knepley 
16178cda7954SMatthew G. Knepley   Input Parameters:
16188cda7954SMatthew G. Knepley + dm     - The DM
16198cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16208cda7954SMatthew G. Knepley 
16218cda7954SMatthew G. Knepley   Output Parameter:
16228cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
16238cda7954SMatthew G. Knepley 
16248cda7954SMatthew G. Knepley   Notes:
16258cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16268cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16278cda7954SMatthew G. Knepley $ dm        - The present DM
16288cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16298cda7954SMatthew G. Knepley $ field     - The field number in dm
16308cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16318cda7954SMatthew G. Knepley 
16328cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16338cda7954SMatthew G. Knepley 
16348cda7954SMatthew G. Knepley .seealso: DMSetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16358cda7954SMatthew G. Knepley */
16368cda7954SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
16370a50eb56SMatthew G. Knepley {
16380a50eb56SMatthew G. Knepley   PetscFunctionBegin;
16390a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1640f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
16410a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
16420a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
16430a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
16440a50eb56SMatthew G. Knepley }
16450a50eb56SMatthew G. Knepley 
16468cda7954SMatthew G. Knepley /*@C
16478cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
16488cda7954SMatthew G. Knepley 
16498cda7954SMatthew G. Knepley   Logically collective on DM
16508cda7954SMatthew G. Knepley 
16518cda7954SMatthew G. Knepley   Input Parameters:
16528cda7954SMatthew G. Knepley + dm     - The DM
16538cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16548cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
16558cda7954SMatthew G. Knepley 
16568cda7954SMatthew G. Knepley   Notes:
16578cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16588cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16598cda7954SMatthew G. Knepley $ dm        - The present DM
16608cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16618cda7954SMatthew G. Knepley $ field     - The field number in dm
16628cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16638cda7954SMatthew G. Knepley 
16648cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16658cda7954SMatthew G. Knepley 
16668cda7954SMatthew G. Knepley .seealso: DMGetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16678cda7954SMatthew G. Knepley */
16688cda7954SMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1669f9d4088aSMatthew G. Knepley {
1670f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1671f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1672f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1673f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1674f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1675f9d4088aSMatthew G. Knepley }
1676f9d4088aSMatthew G. Knepley 
16778cda7954SMatthew G. Knepley /*@C
16788cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
16798cda7954SMatthew G. Knepley 
16808cda7954SMatthew G. Knepley   Not collective
16818cda7954SMatthew G. Knepley 
16828cda7954SMatthew G. Knepley   Input Parameters:
16838cda7954SMatthew G. Knepley + dm     - The DM
16848cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16858cda7954SMatthew G. Knepley 
16868cda7954SMatthew G. Knepley   Output Parameter:
16878cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
16888cda7954SMatthew G. Knepley 
16898cda7954SMatthew G. Knepley   Notes:
16908cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16918cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16928cda7954SMatthew G. Knepley $ dm        - The present DM
16938cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16948cda7954SMatthew G. Knepley $ field     - The field number in dm
16958cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16968cda7954SMatthew G. Knepley 
16978cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16988cda7954SMatthew G. Knepley 
16998cda7954SMatthew G. Knepley .seealso: DMSetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
17008cda7954SMatthew G. Knepley */
17018cda7954SMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1702f9d4088aSMatthew G. Knepley {
1703f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1704f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1705f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1706f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1707f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1708f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1709f9d4088aSMatthew G. Knepley }
1710f9d4088aSMatthew G. Knepley 
17114f3b5142SJed Brown /*@C
17124d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
17134d343eeaSMatthew G Knepley 
17144d343eeaSMatthew G Knepley   Not collective
17154d343eeaSMatthew G Knepley 
17164d343eeaSMatthew G Knepley   Input Parameter:
17174d343eeaSMatthew G Knepley . dm - the DM object
17184d343eeaSMatthew G Knepley 
17194d343eeaSMatthew G Knepley   Output Parameters:
17200298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
17210298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
17220298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
17234d343eeaSMatthew G Knepley 
17244d343eeaSMatthew G Knepley   Level: intermediate
17254d343eeaSMatthew G Knepley 
172621c9b008SJed Brown   Notes:
172721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
172821c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
172921c9b008SJed Brown   PetscFree().
173021c9b008SJed Brown 
17314d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
17324d343eeaSMatthew G Knepley @*/
173337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
17344d343eeaSMatthew G Knepley {
173537d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
17364d343eeaSMatthew G Knepley   PetscErrorCode ierr;
17374d343eeaSMatthew G Knepley 
17384d343eeaSMatthew G Knepley   PetscFunctionBegin;
17394d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
174069ca1f37SDmitry Karpeev   if (numFields) {
1741534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
174269ca1f37SDmitry Karpeev     *numFields = 0;
174369ca1f37SDmitry Karpeev   }
174437d0c07bSMatthew G Knepley   if (fieldNames) {
174537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
17460298fd71SBarry Smith     *fieldNames = NULL;
174769ca1f37SDmitry Karpeev   }
174869ca1f37SDmitry Karpeev   if (fields) {
174969ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
17500298fd71SBarry Smith     *fields = NULL;
175169ca1f37SDmitry Karpeev   }
175292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
175337d0c07bSMatthew G Knepley   if (section) {
17543a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
175537d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
175637d0c07bSMatthew G Knepley 
1757e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
175837d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
17593a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
176037d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
176137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
176237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
17633a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
176437d0c07bSMatthew G Knepley     }
176537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
176637d0c07bSMatthew G Knepley       PetscInt gdof;
176737d0c07bSMatthew G Knepley 
176837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
176937d0c07bSMatthew G Knepley       if (gdof > 0) {
177037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
17713a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
177237d0c07bSMatthew G Knepley 
177337d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
177437d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
17753a544194SStefano Zampini           fpdof = fdof-fcdof;
17763a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
17773a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
17783a544194SStefano Zampini             fieldNc[f] = 1;
17793a544194SStefano Zampini           }
17803a544194SStefano Zampini           fieldSizes[f] += fpdof;
178137d0c07bSMatthew G Knepley         }
178237d0c07bSMatthew G Knepley       }
178337d0c07bSMatthew G Knepley     }
178437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1785785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
178637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
178737d0c07bSMatthew G Knepley     }
178837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
178937d0c07bSMatthew G Knepley       PetscInt gdof, goff;
179037d0c07bSMatthew G Knepley 
179137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
179237d0c07bSMatthew G Knepley       if (gdof > 0) {
179337d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
179437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
179537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
179637d0c07bSMatthew G Knepley 
179737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
179837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
179937d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
180037d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
180137d0c07bSMatthew G Knepley           }
180237d0c07bSMatthew G Knepley         }
180337d0c07bSMatthew G Knepley       }
180437d0c07bSMatthew G Knepley     }
18058865f1eaSKarl Rupp     if (numFields) *numFields = nF;
180637d0c07bSMatthew G Knepley     if (fieldNames) {
1807785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
180837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
180937d0c07bSMatthew G Knepley         const char *fieldName;
181037d0c07bSMatthew G Knepley 
181137d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
181237d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
181337d0c07bSMatthew G Knepley       }
181437d0c07bSMatthew G Knepley     }
181537d0c07bSMatthew G Knepley     if (fields) {
1816785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
181737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
18183a544194SStefano Zampini         PetscInt bs, in[2], out[2];
18193a544194SStefano Zampini 
182082f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
18213a544194SStefano Zampini         in[0] = -fieldNc[f];
18223a544194SStefano Zampini         in[1] = fieldNc[f];
1823820f2d46SBarry Smith         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
18243a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
18253a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
182637d0c07bSMatthew G Knepley       }
182737d0c07bSMatthew G Knepley     }
18283a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
18298865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
18308865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
183169ca1f37SDmitry Karpeev   }
18324d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
18334d343eeaSMatthew G Knepley }
18344d343eeaSMatthew G Knepley 
183516621825SDmitry Karpeev /*@C
183616621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
183716621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
183816621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1839e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1840e7c4fc90SDmitry Karpeev 
1841e7c4fc90SDmitry Karpeev   Not collective
1842e7c4fc90SDmitry Karpeev 
1843e7c4fc90SDmitry Karpeev   Input Parameter:
1844e7c4fc90SDmitry Karpeev . dm - the DM object
1845e7c4fc90SDmitry Karpeev 
1846e7c4fc90SDmitry Karpeev   Output Parameters:
18470298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
18480298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
18490298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
18500298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1851e7c4fc90SDmitry Karpeev 
1852e7c4fc90SDmitry Karpeev   Level: intermediate
1853e7c4fc90SDmitry Karpeev 
1854e7c4fc90SDmitry Karpeev   Notes:
1855e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1856e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1857e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1858e7c4fc90SDmitry Karpeev 
1859e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1860e7c4fc90SDmitry Karpeev @*/
186116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1862e7c4fc90SDmitry Karpeev {
1863e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1864e7c4fc90SDmitry Karpeev 
1865e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1866e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18678865f1eaSKarl Rupp   if (len) {
1868534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
18698865f1eaSKarl Rupp     *len = 0;
18708865f1eaSKarl Rupp   }
18718865f1eaSKarl Rupp   if (namelist) {
18728865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1873ea78f98cSLisandro Dalcin     *namelist = NULL;
18748865f1eaSKarl Rupp   }
18758865f1eaSKarl Rupp   if (islist) {
18768865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1877ea78f98cSLisandro Dalcin     *islist = NULL;
18788865f1eaSKarl Rupp   }
18798865f1eaSKarl Rupp   if (dmlist) {
18808865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1881ea78f98cSLisandro Dalcin     *dmlist = NULL;
18828865f1eaSKarl Rupp   }
1883f3f0edfdSDmitry Karpeev   /*
1884f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1885f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1886f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1887f3f0edfdSDmitry Karpeev    */
1888ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
188916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1890435a35e8SMatthew G Knepley     PetscSection section;
1891435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1892435a35e8SMatthew G Knepley 
189392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1894435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1895435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1896f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
189703dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
189803dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
189903dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1900435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1901435a35e8SMatthew G Knepley         const char *fieldName;
1902435a35e8SMatthew G Knepley 
190303dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
190403dc3394SMatthew G. Knepley         if (namelist) {
1905435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1906435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1907435a35e8SMatthew G Knepley         }
190803dc3394SMatthew G. Knepley       }
1909435a35e8SMatthew G Knepley     } else {
191069ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1911e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
19120298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1913e7c4fc90SDmitry Karpeev     }
19148865f1eaSKarl Rupp   } else {
191516621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
191616621825SDmitry Karpeev   }
191716621825SDmitry Karpeev   PetscFunctionReturn(0);
191816621825SDmitry Karpeev }
191916621825SDmitry Karpeev 
1920564cec59SMatthew G. Knepley /*@
1921435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1922435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1923435a35e8SMatthew G Knepley 
1924435a35e8SMatthew G Knepley   Not collective
1925435a35e8SMatthew G Knepley 
1926435a35e8SMatthew G Knepley   Input Parameters:
19272adcc780SMatthew G. Knepley + dm        - The DM object
19282adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
19292adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1930435a35e8SMatthew G Knepley 
1931435a35e8SMatthew G Knepley   Output Parameters:
19322adcc780SMatthew G. Knepley + is - The global indices for the subproblem
19332adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1934435a35e8SMatthew G Knepley 
19355d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19365d3b26e6SMatthew G. Knepley 
1937435a35e8SMatthew G Knepley   Level: intermediate
1938435a35e8SMatthew G Knepley 
19395d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1940435a35e8SMatthew G Knepley @*/
194137bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1942435a35e8SMatthew G Knepley {
1943435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1944435a35e8SMatthew G Knepley 
1945435a35e8SMatthew G Knepley   PetscFunctionBegin;
1946435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1947435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
19488865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
19498865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1950b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1951435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1952435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1953435a35e8SMatthew G Knepley }
1954435a35e8SMatthew G Knepley 
19552adcc780SMatthew G. Knepley /*@C
19562adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
19572adcc780SMatthew G. Knepley 
19582adcc780SMatthew G. Knepley   Not collective
19592adcc780SMatthew G. Knepley 
1960d8d19677SJose E. Roman   Input Parameters:
19612adcc780SMatthew G. Knepley + dms - The DM objects
19622adcc780SMatthew G. Knepley - len - The number of DMs
19632adcc780SMatthew G. Knepley 
19642adcc780SMatthew G. Knepley   Output Parameters:
1965a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
19662adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
19672adcc780SMatthew G. Knepley 
19685d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19695d3b26e6SMatthew G. Knepley 
19702adcc780SMatthew G. Knepley   Level: intermediate
19712adcc780SMatthew G. Knepley 
19725d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
19732adcc780SMatthew G. Knepley @*/
19742adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
19752adcc780SMatthew G. Knepley {
19762adcc780SMatthew G. Knepley   PetscInt       i;
19772adcc780SMatthew G. Knepley   PetscErrorCode ierr;
19782adcc780SMatthew G. Knepley 
19792adcc780SMatthew G. Knepley   PetscFunctionBegin;
19802adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
19812adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
19822adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1983a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
19842adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
19852adcc780SMatthew G. Knepley   if (len) {
1986b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1987b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1988b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
19892adcc780SMatthew G. Knepley   }
19902adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
19912adcc780SMatthew G. Knepley }
19922adcc780SMatthew G. Knepley 
199316621825SDmitry Karpeev /*@C
19948d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
19958d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
19968d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
19978d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
19988d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
199916621825SDmitry Karpeev 
200016621825SDmitry Karpeev   Not collective
200116621825SDmitry Karpeev 
200216621825SDmitry Karpeev   Input Parameter:
200316621825SDmitry Karpeev . dm - the DM object
200416621825SDmitry Karpeev 
200516621825SDmitry Karpeev   Output Parameters:
20060298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
20070298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
20080298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
20090298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
20100298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
201116621825SDmitry Karpeev 
201216621825SDmitry Karpeev   Level: intermediate
201316621825SDmitry Karpeev 
201416621825SDmitry Karpeev   Notes:
201516621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
201616621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
201716621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
201816621825SDmitry Karpeev 
2019245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
202016621825SDmitry Karpeev @*/
20218d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
202216621825SDmitry Karpeev {
202316621825SDmitry Karpeev   PetscErrorCode      ierr;
2024be081cd6SPeter Brune   DMSubDomainHookLink link;
2025be081cd6SPeter Brune   PetscInt            i,l;
202616621825SDmitry Karpeev 
202716621825SDmitry Karpeev   PetscFunctionBegin;
202816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
202914a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
20300298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
20310298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
20320298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
20330298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
2034f3f0edfdSDmitry Karpeev   /*
2035f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2036f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2037f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2038f3f0edfdSDmitry Karpeev    */
2039ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
204016621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2041be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
204214a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2043f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2044be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2045be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
2046be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
2047be081cd6SPeter Brune         }
2048648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2049e7c4fc90SDmitry Karpeev       }
205014a18fd3SPeter Brune     }
205114a18fd3SPeter Brune     if (len) *len = l;
205214a18fd3SPeter Brune   }
2053e30e807fSPeter Brune   PetscFunctionReturn(0);
2054e30e807fSPeter Brune }
2055e30e807fSPeter Brune 
2056e30e807fSPeter Brune /*@C
2057e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2058e30e807fSPeter Brune 
2059e30e807fSPeter Brune   Not collective
2060e30e807fSPeter Brune 
2061e30e807fSPeter Brune   Input Parameters:
2062e30e807fSPeter Brune + dm - the DM object
2063e30e807fSPeter Brune . n  - the number of subdomain scatters
2064e30e807fSPeter Brune - subdms - the local subdomains
2065e30e807fSPeter Brune 
2066e30e807fSPeter Brune   Output Parameters:
20676b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2068e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2069e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2070e30e807fSPeter Brune 
207195452b02SPatrick Sanan   Notes:
207295452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2073e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2074e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2075e30e807fSPeter Brune   solution and residual data.
2076e30e807fSPeter Brune 
2077e30e807fSPeter Brune   Level: developer
2078e30e807fSPeter Brune 
2079e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2080e30e807fSPeter Brune @*/
2081e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2082e30e807fSPeter Brune {
2083e30e807fSPeter Brune   PetscErrorCode ierr;
2084e30e807fSPeter Brune 
2085e30e807fSPeter Brune   PetscFunctionBegin;
2086e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2087e30e807fSPeter Brune   PetscValidPointer(subdms,3);
2088b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
2089e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
2090e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2091e7c4fc90SDmitry Karpeev }
2092e7c4fc90SDmitry Karpeev 
209347c6ae99SBarry Smith /*@
209447c6ae99SBarry Smith   DMRefine - Refines a DM object
209547c6ae99SBarry Smith 
2096d083f849SBarry Smith   Collective on dm
209747c6ae99SBarry Smith 
2098d8d19677SJose E. Roman   Input Parameters:
209947c6ae99SBarry Smith + dm   - the DM object
210091d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
210147c6ae99SBarry Smith 
210247c6ae99SBarry Smith   Output Parameter:
21030298fd71SBarry Smith . dmf - the refined DM, or NULL
2104ae0a1c52SMatthew G Knepley 
2105f27dd7c6SMatthew G. Knepley   Options Database Keys:
2106412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2107412e9a14SMatthew G. Knepley 
21080298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
210947c6ae99SBarry Smith 
211047c6ae99SBarry Smith   Level: developer
211147c6ae99SBarry Smith 
2112e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
211347c6ae99SBarry Smith @*/
21147087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
211547c6ae99SBarry Smith {
211647c6ae99SBarry Smith   PetscErrorCode   ierr;
2117c833c3b5SJed Brown   DMRefineHookLink link;
211847c6ae99SBarry Smith 
211947c6ae99SBarry Smith   PetscFunctionBegin;
2120732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2121b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
21221ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
212347c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
21244057135bSMatthew G Knepley   if (*dmf) {
212543842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
21268865f1eaSKarl Rupp 
21278cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
21288865f1eaSKarl Rupp 
2129644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
21300598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2131656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
21328865f1eaSKarl Rupp 
2133e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
2134c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
21358865f1eaSKarl Rupp       if (link->refinehook) {
21368865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
21378865f1eaSKarl Rupp       }
2138c833c3b5SJed Brown     }
2139c833c3b5SJed Brown   }
21401ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
2141c833c3b5SJed Brown   PetscFunctionReturn(0);
2142c833c3b5SJed Brown }
2143c833c3b5SJed Brown 
2144bb9467b5SJed Brown /*@C
2145c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2146c833c3b5SJed Brown 
2147c833c3b5SJed Brown    Logically Collective
2148c833c3b5SJed Brown 
21494165533cSJose E. Roman    Input Parameters:
2150c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2151c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2152c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21530298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2154c833c3b5SJed Brown 
2155c833c3b5SJed Brown    Calling sequence of refinehook:
2156c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2157c833c3b5SJed Brown 
2158c833c3b5SJed Brown +  coarse - coarse level DM
2159c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2160c833c3b5SJed Brown -  ctx - optional user-defined function context
2161c833c3b5SJed Brown 
2162c833c3b5SJed Brown    Calling sequence for interphook:
2163c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2164c833c3b5SJed Brown 
2165c833c3b5SJed Brown +  coarse - coarse level DM
2166c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2167c833c3b5SJed Brown .  fine - fine level DM to update
2168c833c3b5SJed Brown -  ctx - optional user-defined function context
2169c833c3b5SJed Brown 
2170c833c3b5SJed Brown    Level: advanced
2171c833c3b5SJed Brown 
2172c833c3b5SJed Brown    Notes:
2173c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2174c833c3b5SJed Brown 
2175c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2176c833c3b5SJed Brown 
2177bb9467b5SJed Brown    This function is currently not available from Fortran.
2178bb9467b5SJed Brown 
2179c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2180c833c3b5SJed Brown @*/
2181c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2182c833c3b5SJed Brown {
2183c833c3b5SJed Brown   PetscErrorCode   ierr;
2184c833c3b5SJed Brown   DMRefineHookLink link,*p;
2185c833c3b5SJed Brown 
2186c833c3b5SJed Brown   PetscFunctionBegin;
2187c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
21883d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
21893d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
21903d8e3701SJed Brown   }
219195dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2192c833c3b5SJed Brown   link->refinehook = refinehook;
2193c833c3b5SJed Brown   link->interphook = interphook;
2194c833c3b5SJed Brown   link->ctx        = ctx;
21950298fd71SBarry Smith   link->next       = NULL;
2196c833c3b5SJed Brown   *p               = link;
2197c833c3b5SJed Brown   PetscFunctionReturn(0);
2198c833c3b5SJed Brown }
2199c833c3b5SJed Brown 
22003d8e3701SJed Brown /*@C
22013d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
22023d8e3701SJed Brown 
22033d8e3701SJed Brown    Logically Collective
22043d8e3701SJed Brown 
22054165533cSJose E. Roman    Input Parameters:
22063d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
22073d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
22083d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
22093d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22103d8e3701SJed Brown 
22113d8e3701SJed Brown    Level: advanced
22123d8e3701SJed Brown 
22133d8e3701SJed Brown    Notes:
22143d8e3701SJed Brown    This function does nothing if the hook is not in the list.
22153d8e3701SJed Brown 
22163d8e3701SJed Brown    This function is currently not available from Fortran.
22173d8e3701SJed Brown 
22183d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
22193d8e3701SJed Brown @*/
22203d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
22213d8e3701SJed Brown {
22223d8e3701SJed Brown   PetscErrorCode   ierr;
22233d8e3701SJed Brown   DMRefineHookLink link,*p;
22243d8e3701SJed Brown 
22253d8e3701SJed Brown   PetscFunctionBegin;
22263d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22273d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
22283d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
22293d8e3701SJed Brown       link = *p;
22303d8e3701SJed Brown       *p = link->next;
22313d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
22323d8e3701SJed Brown       break;
22333d8e3701SJed Brown     }
22343d8e3701SJed Brown   }
22353d8e3701SJed Brown   PetscFunctionReturn(0);
22363d8e3701SJed Brown }
22373d8e3701SJed Brown 
2238c833c3b5SJed Brown /*@
2239c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2240c833c3b5SJed Brown 
2241c833c3b5SJed Brown    Collective if any hooks are
2242c833c3b5SJed Brown 
22434165533cSJose E. Roman    Input Parameters:
2244c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2245e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2246c833c3b5SJed Brown -  fine - finer DM to update
2247c833c3b5SJed Brown 
2248c833c3b5SJed Brown    Level: developer
2249c833c3b5SJed Brown 
2250c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2251c833c3b5SJed Brown @*/
2252c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2253c833c3b5SJed Brown {
2254c833c3b5SJed Brown   PetscErrorCode   ierr;
2255c833c3b5SJed Brown   DMRefineHookLink link;
2256c833c3b5SJed Brown 
2257c833c3b5SJed Brown   PetscFunctionBegin;
2258c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
22598865f1eaSKarl Rupp     if (link->interphook) {
22608865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
22618865f1eaSKarl Rupp     }
22624057135bSMatthew G Knepley   }
226347c6ae99SBarry Smith   PetscFunctionReturn(0);
226447c6ae99SBarry Smith }
226547c6ae99SBarry Smith 
2266eb3f98d2SBarry Smith /*@
22671f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
22681f3379b2SToby Isaac 
22691f3379b2SToby Isaac    Collective on DM
22701f3379b2SToby Isaac 
22714165533cSJose E. Roman    Input Parameters:
22721f3379b2SToby Isaac +  coarse - coarse DM
22731f3379b2SToby Isaac .  fine   - fine DM
22741f3379b2SToby Isaac .  interp - (optional) the matrix computed by DMCreateInterpolation().  Implementations may not need this, but if it
22751f3379b2SToby Isaac             is available it can avoid some recomputation.  If it is provided, MatInterpolate() will be used if
22761f3379b2SToby Isaac             the coarse DM does not have a specialized implementation.
22771f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
22781f3379b2SToby Isaac 
22794165533cSJose E. Roman    Output Parameter:
22801f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
22811f3379b2SToby Isaac 
22821f3379b2SToby Isaac    Level: developer
22831f3379b2SToby Isaac 
22841f3379b2SToby Isaac    Note: This function exists because the interpolation of a solution vector between meshes is not always a linear
22851f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
22861f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
22871f3379b2SToby Isaac    slope-limiting reconstruction.
22881f3379b2SToby Isaac 
22891f3379b2SToby Isaac .seealso DMInterpolate(), DMCreateInterpolation()
22901f3379b2SToby Isaac @*/
22911f3379b2SToby Isaac PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
22921f3379b2SToby Isaac {
22931f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM,DM,Mat,Vec,Vec) = NULL;
22941f3379b2SToby Isaac   PetscErrorCode ierr;
22951f3379b2SToby Isaac 
22961f3379b2SToby Isaac   PetscFunctionBegin;
22971f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22981f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp,MAT_CLASSID,3);
22991f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol,VEC_CLASSID,4);
23001f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol,VEC_CLASSID,5);
23011f3379b2SToby Isaac 
23021f3379b2SToby Isaac   ierr = PetscObjectQueryFunction((PetscObject)coarse,"DMInterpolateSolution_C", &interpsol);CHKERRQ(ierr);
23031f3379b2SToby Isaac   if (interpsol) {
23041f3379b2SToby Isaac     ierr = (*interpsol)(coarse, fine, interp, coarseSol, fineSol);CHKERRQ(ierr);
23051f3379b2SToby Isaac   } else if (interp) {
23061f3379b2SToby Isaac     ierr = MatInterpolate(interp, coarseSol, fineSol);CHKERRQ(ierr);
23071f3379b2SToby Isaac   } else SETERRQ1(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
23081f3379b2SToby Isaac   PetscFunctionReturn(0);
23091f3379b2SToby Isaac }
23101f3379b2SToby Isaac 
23111f3379b2SToby Isaac /*@
2312aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2313eb3f98d2SBarry Smith 
2314eb3f98d2SBarry Smith     Not Collective
2315eb3f98d2SBarry Smith 
2316eb3f98d2SBarry Smith     Input Parameter:
2317eb3f98d2SBarry Smith .   dm - the DM object
2318eb3f98d2SBarry Smith 
2319eb3f98d2SBarry Smith     Output Parameter:
2320eb3f98d2SBarry Smith .   level - number of refinements
2321eb3f98d2SBarry Smith 
2322eb3f98d2SBarry Smith     Level: developer
2323eb3f98d2SBarry Smith 
23246a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2325eb3f98d2SBarry Smith 
2326eb3f98d2SBarry Smith @*/
2327eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2328eb3f98d2SBarry Smith {
2329eb3f98d2SBarry Smith   PetscFunctionBegin;
2330eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2331eb3f98d2SBarry Smith   *level = dm->levelup;
2332eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2333eb3f98d2SBarry Smith }
2334eb3f98d2SBarry Smith 
2335fef3a512SBarry Smith /*@
2336aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2337fef3a512SBarry Smith 
2338fef3a512SBarry Smith     Not Collective
2339fef3a512SBarry Smith 
2340d8d19677SJose E. Roman     Input Parameters:
2341fef3a512SBarry Smith +   dm - the DM object
2342fef3a512SBarry Smith -   level - number of refinements
2343fef3a512SBarry Smith 
2344fef3a512SBarry Smith     Level: advanced
2345fef3a512SBarry Smith 
234695452b02SPatrick Sanan     Notes:
234795452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2348fef3a512SBarry Smith 
2349fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2350fef3a512SBarry Smith 
2351fef3a512SBarry Smith @*/
2352fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2353fef3a512SBarry Smith {
2354fef3a512SBarry Smith   PetscFunctionBegin;
2355fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2356fef3a512SBarry Smith   dm->levelup = level;
2357fef3a512SBarry Smith   PetscFunctionReturn(0);
2358fef3a512SBarry Smith }
2359fef3a512SBarry Smith 
2360*d410b0cfSMatthew G. Knepley /*@
2361*d410b0cfSMatthew G. Knepley   DMExtrude - Extrude a DM object from a surface
2362*d410b0cfSMatthew G. Knepley 
2363*d410b0cfSMatthew G. Knepley   Collective on dm
2364*d410b0cfSMatthew G. Knepley 
2365*d410b0cfSMatthew G. Knepley   Input Parameter:
2366*d410b0cfSMatthew G. Knepley + dm     - the DM object
2367*d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2368*d410b0cfSMatthew G. Knepley 
2369*d410b0cfSMatthew G. Knepley   Output Parameter:
2370*d410b0cfSMatthew G. Knepley . dme - the extruded DM, or NULL
2371*d410b0cfSMatthew G. Knepley 
2372*d410b0cfSMatthew G. Knepley   Note: If no extrusion was done, the return value is NULL
2373*d410b0cfSMatthew G. Knepley 
2374*d410b0cfSMatthew G. Knepley   Level: developer
2375*d410b0cfSMatthew G. Knepley 
2376*d410b0cfSMatthew G. Knepley .seealso DMRefine(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector()
2377*d410b0cfSMatthew G. Knepley @*/
2378*d410b0cfSMatthew G. Knepley PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2379*d410b0cfSMatthew G. Knepley {
2380*d410b0cfSMatthew G. Knepley   PetscErrorCode ierr;
2381*d410b0cfSMatthew G. Knepley 
2382*d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2383*d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2384*d410b0cfSMatthew G. Knepley   if (!dm->ops->extrude) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "DM type %s does not implement DMExtrude", ((PetscObject) dm)->type_name);
2385*d410b0cfSMatthew G. Knepley   ierr = (*dm->ops->extrude)(dm, layers, dme);CHKERRQ(ierr);
2386*d410b0cfSMatthew G. Knepley   if (*dme) {
2387*d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
2388*d410b0cfSMatthew G. Knepley     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject) dm, (PetscObject) *dme);CHKERRQ(ierr);
2389*d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
2390*d410b0cfSMatthew G. Knepley     ierr = DMSetMatType(*dme, dm->mattype);CHKERRQ(ierr);
2391*d410b0cfSMatthew G. Knepley   }
2392*d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2393*d410b0cfSMatthew G. Knepley }
2394*d410b0cfSMatthew G. Knepley 
2395ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2396ca3d3a14SMatthew G. Knepley {
2397ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2398ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2399ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2400ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2401ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2402ca3d3a14SMatthew G. Knepley }
2403ca3d3a14SMatthew G. Knepley 
2404ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2405ca3d3a14SMatthew G. Knepley {
2406ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2407ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2408ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2409ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2410ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2411ca3d3a14SMatthew G. Knepley }
2412ca3d3a14SMatthew G. Knepley 
2413ca3d3a14SMatthew G. Knepley /*@
2414c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2415ca3d3a14SMatthew G. Knepley 
2416ca3d3a14SMatthew G. Knepley   Input Parameter:
2417ca3d3a14SMatthew G. Knepley . dm - The DM
2418ca3d3a14SMatthew G. Knepley 
2419ca3d3a14SMatthew G. Knepley   Output Parameter:
2420ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2421ca3d3a14SMatthew G. Knepley 
2422ca3d3a14SMatthew G. Knepley   Level: developer
2423ca3d3a14SMatthew G. Knepley 
2424436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2425ca3d3a14SMatthew G. Knepley @*/
2426ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2427ca3d3a14SMatthew G. Knepley {
2428ca3d3a14SMatthew G. Knepley   Vec            tv;
2429ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2430ca3d3a14SMatthew G. Knepley 
2431ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2432ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2433534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2434ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2435ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2436ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2437ca3d3a14SMatthew G. Knepley }
2438ca3d3a14SMatthew G. Knepley 
2439ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2440ca3d3a14SMatthew G. Knepley {
2441ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2442ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2443ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2444ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2445ca3d3a14SMatthew G. Knepley 
2446ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2447ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
244892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2449ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2450ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2451ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
245292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2453ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2454ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2455ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2456ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2457ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2458ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2459ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2460ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2461ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2462ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2463ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2464ca3d3a14SMatthew G. Knepley     }
2465ca3d3a14SMatthew G. Knepley   }
2466ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2467ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2468ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2469ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2470ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2471ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2472ca3d3a14SMatthew G. Knepley       if (dof) {
2473ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2474ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2475ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2476ca3d3a14SMatthew G. Knepley 
2477ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2478ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2479ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2480580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2481ca3d3a14SMatthew G. Knepley       }
2482ca3d3a14SMatthew G. Knepley     }
2483ca3d3a14SMatthew G. Knepley   }
2484ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2485ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2486ca3d3a14SMatthew G. Knepley }
2487ca3d3a14SMatthew G. Knepley 
2488ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2489ca3d3a14SMatthew G. Knepley {
2490ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2491ca3d3a14SMatthew G. Knepley 
2492ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2493ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2494ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2495ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2496ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2497ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2498ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2499ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2500ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2501ca3d3a14SMatthew G. Knepley }
2502ca3d3a14SMatthew G. Knepley 
2503bb9467b5SJed Brown /*@C
2504baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2505baf369e7SPeter Brune 
2506baf369e7SPeter Brune    Logically Collective
2507baf369e7SPeter Brune 
25084165533cSJose E. Roman    Input Parameters:
2509baf369e7SPeter Brune +  dm - the DM
2510baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2511baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
25120298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2513baf369e7SPeter Brune 
2514baf369e7SPeter Brune    Calling sequence for beginhook:
2515baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2516baf369e7SPeter Brune 
2517baf369e7SPeter Brune +  dm - global DM
2518baf369e7SPeter Brune .  g - global vector
2519baf369e7SPeter Brune .  mode - mode
2520baf369e7SPeter Brune .  l - local vector
2521baf369e7SPeter Brune -  ctx - optional user-defined function context
2522baf369e7SPeter Brune 
2523baf369e7SPeter Brune    Calling sequence for endhook:
2524ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2525baf369e7SPeter Brune 
2526baf369e7SPeter Brune +  global - global DM
2527baf369e7SPeter Brune -  ctx - optional user-defined function context
2528baf369e7SPeter Brune 
2529baf369e7SPeter Brune    Level: advanced
2530baf369e7SPeter Brune 
2531baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2532baf369e7SPeter Brune @*/
2533baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2534baf369e7SPeter Brune {
2535baf369e7SPeter Brune   PetscErrorCode          ierr;
2536baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2537baf369e7SPeter Brune 
2538baf369e7SPeter Brune   PetscFunctionBegin;
2539baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2540baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
254195dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2542baf369e7SPeter Brune   link->beginhook = beginhook;
2543baf369e7SPeter Brune   link->endhook   = endhook;
2544baf369e7SPeter Brune   link->ctx       = ctx;
25450298fd71SBarry Smith   link->next      = NULL;
2546baf369e7SPeter Brune   *p              = link;
2547baf369e7SPeter Brune   PetscFunctionReturn(0);
2548baf369e7SPeter Brune }
2549baf369e7SPeter Brune 
25504c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
25514c274da1SToby Isaac {
25524c274da1SToby Isaac   Mat cMat;
25534c274da1SToby Isaac   Vec cVec;
25544c274da1SToby Isaac   PetscSection section, cSec;
25554c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25564c274da1SToby Isaac   PetscErrorCode ierr;
25574c274da1SToby Isaac 
25584c274da1SToby Isaac   PetscFunctionBegin;
25594c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25604c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
25614c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
25625db9a05bSToby Isaac     PetscInt nRows;
25635db9a05bSToby Isaac 
25645db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
25655db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
256692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
25677711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
25684c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
25694c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
25704c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25714c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
25724c274da1SToby Isaac       if (dof) {
25734c274da1SToby Isaac         PetscScalar *vals;
25744c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
25754c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
25764c274da1SToby Isaac       }
25774c274da1SToby Isaac     }
25784c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
25794c274da1SToby Isaac   }
25804c274da1SToby Isaac   PetscFunctionReturn(0);
25814c274da1SToby Isaac }
25824c274da1SToby Isaac 
258347c6ae99SBarry Smith /*@
258401729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
258501729b5cSPatrick Sanan 
2586d083f849SBarry Smith     Neighbor-wise Collective on dm
258701729b5cSPatrick Sanan 
258801729b5cSPatrick Sanan     Input Parameters:
258901729b5cSPatrick Sanan +   dm - the DM object
259001729b5cSPatrick Sanan .   g - the global vector
259101729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
259201729b5cSPatrick Sanan -   l - the local vector
259301729b5cSPatrick Sanan 
259401729b5cSPatrick Sanan     Notes:
259501729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
259601729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
259701729b5cSPatrick Sanan 
259801729b5cSPatrick Sanan     Level: beginner
259901729b5cSPatrick Sanan 
260001729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
260101729b5cSPatrick Sanan 
260201729b5cSPatrick Sanan @*/
260301729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
260401729b5cSPatrick Sanan {
260501729b5cSPatrick Sanan   PetscErrorCode ierr;
260601729b5cSPatrick Sanan 
260701729b5cSPatrick Sanan   PetscFunctionBegin;
260801729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
260901729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
261001729b5cSPatrick Sanan   PetscFunctionReturn(0);
261101729b5cSPatrick Sanan }
261201729b5cSPatrick Sanan 
261301729b5cSPatrick Sanan /*@
261447c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
261547c6ae99SBarry Smith 
2616d083f849SBarry Smith     Neighbor-wise Collective on dm
261747c6ae99SBarry Smith 
261847c6ae99SBarry Smith     Input Parameters:
261947c6ae99SBarry Smith +   dm - the DM object
262047c6ae99SBarry Smith .   g - the global vector
262147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
262247c6ae99SBarry Smith -   l - the local vector
262347c6ae99SBarry Smith 
262401729b5cSPatrick Sanan     Level: intermediate
262547c6ae99SBarry Smith 
262601729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
262747c6ae99SBarry Smith 
262847c6ae99SBarry Smith @*/
26297087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
263047c6ae99SBarry Smith {
26317128ae9fSMatthew G Knepley   PetscSF                 sf;
263247c6ae99SBarry Smith   PetscErrorCode          ierr;
2633baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
263447c6ae99SBarry Smith 
263547c6ae99SBarry Smith   PetscFunctionBegin;
2636171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2637baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
26388865f1eaSKarl Rupp     if (link->beginhook) {
26398865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
26408865f1eaSKarl Rupp     }
2641baf369e7SPeter Brune   }
26421bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
26437128ae9fSMatthew G Knepley   if (sf) {
2644ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2645ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2646d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
26477128ae9fSMatthew G Knepley 
264882f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2649a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2650a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2651ad227feaSJunchao Zhang     ierr = PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE);CHKERRQ(ierr);
2652a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2653a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
26547128ae9fSMatthew G Knepley   } else {
265533907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2656843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
26577128ae9fSMatthew G Knepley   }
265847c6ae99SBarry Smith   PetscFunctionReturn(0);
265947c6ae99SBarry Smith }
266047c6ae99SBarry Smith 
266147c6ae99SBarry Smith /*@
266247c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
266347c6ae99SBarry Smith 
2664d083f849SBarry Smith     Neighbor-wise Collective on dm
266547c6ae99SBarry Smith 
266647c6ae99SBarry Smith     Input Parameters:
266747c6ae99SBarry Smith +   dm - the DM object
266847c6ae99SBarry Smith .   g - the global vector
266947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
267047c6ae99SBarry Smith -   l - the local vector
267147c6ae99SBarry Smith 
267201729b5cSPatrick Sanan     Level: intermediate
267347c6ae99SBarry Smith 
267401729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
267547c6ae99SBarry Smith 
267647c6ae99SBarry Smith @*/
26777087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
267847c6ae99SBarry Smith {
26797128ae9fSMatthew G Knepley   PetscSF                 sf;
268047c6ae99SBarry Smith   PetscErrorCode          ierr;
2681ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2682ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2683ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2684baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2685d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
268647c6ae99SBarry Smith 
268747c6ae99SBarry Smith   PetscFunctionBegin;
2688171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26891bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2690ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
26917128ae9fSMatthew G Knepley   if (sf) {
269282f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26937128ae9fSMatthew G Knepley 
2694a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2695a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2696ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray,MPI_REPLACE);CHKERRQ(ierr);
2697a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2698a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
2699ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
27007128ae9fSMatthew G Knepley   } else {
270133907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2702843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
27037128ae9fSMatthew G Knepley   }
27044c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2705baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2706baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2707baf369e7SPeter Brune   }
270847c6ae99SBarry Smith   PetscFunctionReturn(0);
270947c6ae99SBarry Smith }
271047c6ae99SBarry Smith 
2711d4d07f1eSToby Isaac /*@C
2712d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2713d4d07f1eSToby Isaac 
2714d4d07f1eSToby Isaac    Logically Collective
2715d4d07f1eSToby Isaac 
27164165533cSJose E. Roman    Input Parameters:
2717d4d07f1eSToby Isaac +  dm - the DM
2718d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2719d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2720d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2721d4d07f1eSToby Isaac 
2722d4d07f1eSToby Isaac    Calling sequence for beginhook:
2723d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2724d4d07f1eSToby Isaac 
2725d4d07f1eSToby Isaac +  dm - global DM
2726d4d07f1eSToby Isaac .  l - local vector
2727d4d07f1eSToby Isaac .  mode - mode
2728d4d07f1eSToby Isaac .  g - global vector
2729d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2730d4d07f1eSToby Isaac 
2731d4d07f1eSToby Isaac    Calling sequence for endhook:
2732d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2733d4d07f1eSToby Isaac 
2734d4d07f1eSToby Isaac +  global - global DM
2735d4d07f1eSToby Isaac .  l - local vector
2736d4d07f1eSToby Isaac .  mode - mode
2737d4d07f1eSToby Isaac .  g - global vector
2738d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2739d4d07f1eSToby Isaac 
2740d4d07f1eSToby Isaac    Level: advanced
2741d4d07f1eSToby Isaac 
2742d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2743d4d07f1eSToby Isaac @*/
2744d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2745d4d07f1eSToby Isaac {
2746d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2747d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2748d4d07f1eSToby Isaac 
2749d4d07f1eSToby Isaac   PetscFunctionBegin;
2750d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2751d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
275295dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2753d4d07f1eSToby Isaac   link->beginhook = beginhook;
2754d4d07f1eSToby Isaac   link->endhook   = endhook;
2755d4d07f1eSToby Isaac   link->ctx       = ctx;
2756d4d07f1eSToby Isaac   link->next      = NULL;
2757d4d07f1eSToby Isaac   *p              = link;
2758d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2759d4d07f1eSToby Isaac }
2760d4d07f1eSToby Isaac 
27614c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
27624c274da1SToby Isaac {
27634c274da1SToby Isaac   Mat cMat;
27644c274da1SToby Isaac   Vec cVec;
27654c274da1SToby Isaac   PetscSection section, cSec;
27664c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
27674c274da1SToby Isaac   PetscErrorCode ierr;
27684c274da1SToby Isaac 
27694c274da1SToby Isaac   PetscFunctionBegin;
27704c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27714c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
27724c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
27735db9a05bSToby Isaac     PetscInt nRows;
27745db9a05bSToby Isaac 
27755db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
27765db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
277792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
27787711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
27794c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
27804c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27814c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
27824c274da1SToby Isaac       if (dof) {
27834c274da1SToby Isaac         PetscInt d;
27844c274da1SToby Isaac         PetscScalar *vals;
27854c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
27864c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
27874c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
27884c274da1SToby Isaac          * we just extracted */
27894c274da1SToby Isaac         for (d = 0; d < dof; d++) {
27904c274da1SToby Isaac           vals[d] = 0.;
27914c274da1SToby Isaac         }
27924c274da1SToby Isaac       }
27934c274da1SToby Isaac     }
27944c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
27954c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
27964c274da1SToby Isaac   }
27974c274da1SToby Isaac   PetscFunctionReturn(0);
27984c274da1SToby Isaac }
279901729b5cSPatrick Sanan /*@
280001729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
280101729b5cSPatrick Sanan 
2802d083f849SBarry Smith     Neighbor-wise Collective on dm
280301729b5cSPatrick Sanan 
280401729b5cSPatrick Sanan     Input Parameters:
280501729b5cSPatrick Sanan +   dm - the DM object
280601729b5cSPatrick Sanan .   l - the local vector
280701729b5cSPatrick 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.
280801729b5cSPatrick Sanan -   g - the global vector
280901729b5cSPatrick Sanan 
281001729b5cSPatrick Sanan     Notes:
281101729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
281201729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
281301729b5cSPatrick Sanan 
281401729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
281501729b5cSPatrick 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.
281601729b5cSPatrick Sanan 
281701729b5cSPatrick Sanan     Level: beginner
281801729b5cSPatrick Sanan 
281901729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
282001729b5cSPatrick Sanan 
282101729b5cSPatrick Sanan @*/
282201729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
282301729b5cSPatrick Sanan {
282401729b5cSPatrick Sanan   PetscErrorCode ierr;
282501729b5cSPatrick Sanan 
282601729b5cSPatrick Sanan   PetscFunctionBegin;
282701729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
282801729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
282901729b5cSPatrick Sanan   PetscFunctionReturn(0);
283001729b5cSPatrick Sanan }
28314c274da1SToby Isaac 
283247c6ae99SBarry Smith /*@
283301729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
28349a42bb27SBarry Smith 
2835d083f849SBarry Smith     Neighbor-wise Collective on dm
28369a42bb27SBarry Smith 
28379a42bb27SBarry Smith     Input Parameters:
28389a42bb27SBarry Smith +   dm - the DM object
2839f6813fd5SJed Brown .   l - the local vector
28401eb28f2eSBarry 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.
28411eb28f2eSBarry Smith -   g - the global vector
28429a42bb27SBarry Smith 
284395452b02SPatrick Sanan     Notes:
284495452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
284584330215SMatthew 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.
28469a42bb27SBarry Smith 
284701729b5cSPatrick Sanan     Level: intermediate
28489a42bb27SBarry Smith 
284901729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
28509a42bb27SBarry Smith 
28519a42bb27SBarry Smith @*/
28527087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
28539a42bb27SBarry Smith {
28547128ae9fSMatthew G Knepley   PetscSF                 sf;
285584330215SMatthew G. Knepley   PetscSection            s, gs;
2856d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2857ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2858ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2859ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2860fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
286184330215SMatthew G. Knepley   PetscErrorCode          ierr;
2862d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
28639a42bb27SBarry Smith 
28649a42bb27SBarry Smith   PetscFunctionBegin;
2865171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2866d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2867d4d07f1eSToby Isaac     if (link->beginhook) {
2868d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2869d4d07f1eSToby Isaac     }
2870d4d07f1eSToby Isaac   }
28714c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
28721bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
287392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
28747128ae9fSMatthew G Knepley   switch (mode) {
28757128ae9fSMatthew G Knepley   case INSERT_VALUES:
28767128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2877304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
287884330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
28797128ae9fSMatthew G Knepley   case ADD_VALUES:
28807128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2881304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
288284330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
28837128ae9fSMatthew G Knepley   default:
288482f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
28857128ae9fSMatthew G Knepley   }
2886ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2887ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2888ca3d3a14SMatthew G. Knepley     if (transform) {
2889ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2890ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2891ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2892ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2893fa88e482SJed Brown     } else if (isInsert) {
2894ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2895fa88e482SJed Brown     } else {
2896a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2897fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2898ca3d3a14SMatthew G. Knepley     }
2899fa88e482SJed Brown     if (s && isInsert) {
29007128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2901fa88e482SJed Brown     } else {
2902a256111fSJunchao Zhang       ierr = VecGetArrayAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2903fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2904fa88e482SJed Brown     }
2905ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2906d0295fc0SJunchao Zhang       ierr = PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM);CHKERRQ(ierr);
290784330215SMatthew G. Knepley     } else if (s && isInsert) {
290884330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
290984330215SMatthew G. Knepley 
2910e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
291184330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
291284330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
291384330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2914b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
291584330215SMatthew G. Knepley 
291684330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
291703442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
291884330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2919b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
292084330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
292184330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2922b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
292303442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2924b3b16f48SMatthew 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);
2925b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2926b3b16f48SMatthew G. Knepley         if (!gcdof) {
292784330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2928b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2929b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
293084330215SMatthew G. Knepley           const PetscInt *cdofs;
293184330215SMatthew G. Knepley           PetscInt        cind = 0;
293284330215SMatthew G. Knepley 
293384330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2934b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
293584330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2936b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
293784330215SMatthew G. Knepley           }
2938b3b16f48SMatthew 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);
293984330215SMatthew G. Knepley       }
2940ca3d3a14SMatthew G. Knepley     }
2941fa88e482SJed Brown     if (g_inplace) {
2942a256111fSJunchao Zhang       ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
2943fa88e482SJed Brown     } else {
29447128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2945fa88e482SJed Brown     }
2946ca3d3a14SMatthew G. Knepley     if (transform) {
2947ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2948ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2949fa88e482SJed Brown     } else if (l_inplace) {
2950a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
2951ca3d3a14SMatthew G. Knepley     } else {
2952ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2953ca3d3a14SMatthew G. Knepley     }
29547128ae9fSMatthew G Knepley   } else {
2955b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2956843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
29577128ae9fSMatthew G Knepley   }
29589a42bb27SBarry Smith   PetscFunctionReturn(0);
29599a42bb27SBarry Smith }
29609a42bb27SBarry Smith 
29619a42bb27SBarry Smith /*@
29629a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
296347c6ae99SBarry Smith 
2964d083f849SBarry Smith     Neighbor-wise Collective on dm
296547c6ae99SBarry Smith 
296647c6ae99SBarry Smith     Input Parameters:
296747c6ae99SBarry Smith +   dm - the DM object
2968f6813fd5SJed Brown .   l - the local vector
296947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2970f6813fd5SJed Brown -   g - the global vector
297147c6ae99SBarry Smith 
297201729b5cSPatrick Sanan     Level: intermediate
297347c6ae99SBarry Smith 
2974e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
297547c6ae99SBarry Smith 
297647c6ae99SBarry Smith @*/
29777087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
297847c6ae99SBarry Smith {
29797128ae9fSMatthew G Knepley   PetscSF                 sf;
298084330215SMatthew G. Knepley   PetscSection            s;
2981d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2982ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
298384330215SMatthew G. Knepley   PetscErrorCode          ierr;
298447c6ae99SBarry Smith 
298547c6ae99SBarry Smith   PetscFunctionBegin;
2986171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29871bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
298892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
29897128ae9fSMatthew G Knepley   switch (mode) {
29907128ae9fSMatthew G Knepley   case INSERT_VALUES:
29917128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
299284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
29937128ae9fSMatthew G Knepley   case ADD_VALUES:
29947128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
299584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
29967128ae9fSMatthew G Knepley   default:
299782f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
29987128ae9fSMatthew G Knepley   }
299984330215SMatthew G. Knepley   if (sf && !isInsert) {
3000ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3001ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3002ca3d3a14SMatthew G. Knepley     Vec                tmpl;
300384330215SMatthew G. Knepley 
3004ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
3005ca3d3a14SMatthew G. Knepley     if (transform) {
3006ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
3007ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
3008ca3d3a14SMatthew G. Knepley     } else {
3009a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, NULL);CHKERRQ(ierr);
3010ca3d3a14SMatthew G. Knepley     }
3011a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(g, &gArray, NULL);CHKERRQ(ierr);
3012a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
3013ca3d3a14SMatthew G. Knepley     if (transform) {
3014ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
3015ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
3016ca3d3a14SMatthew G. Knepley     } else {
3017a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
3018ca3d3a14SMatthew G. Knepley     }
3019a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
302084330215SMatthew G. Knepley   } else if (s && isInsert) {
30217128ae9fSMatthew G Knepley   } else {
3022b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
3023843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
30247128ae9fSMatthew G Knepley   }
3025d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
3026d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
3027d4d07f1eSToby Isaac   }
302847c6ae99SBarry Smith   PetscFunctionReturn(0);
302947c6ae99SBarry Smith }
303047c6ae99SBarry Smith 
3031f089877aSRichard Tran Mills /*@
3032bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - 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 followed by DMLocalToLocalEnd().
3035f089877aSRichard Tran Mills 
3036d083f849SBarry Smith    Neighbor-wise Collective on dm
3037f089877aSRichard Tran Mills 
3038f089877aSRichard Tran Mills    Input Parameters:
3039f089877aSRichard Tran Mills +  dm - 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(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3055f089877aSRichard Tran Mills 
3056f089877aSRichard Tran Mills @*/
3057f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(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->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3064f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(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 
3068f089877aSRichard Tran Mills /*@
3069bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
3070bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3071d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
3072f089877aSRichard Tran Mills 
3073d083f849SBarry Smith    Neighbor-wise Collective on dm
3074f089877aSRichard Tran Mills 
3075f089877aSRichard Tran Mills    Input Parameters:
3076bc0a1609SRichard Tran Mills +  da - the DM object
3077bc0a1609SRichard Tran Mills .  g - the original local vector
3078bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3079f089877aSRichard Tran Mills 
3080bc0a1609SRichard Tran Mills    Output Parameter:
3081bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3082f089877aSRichard Tran Mills 
3083f089877aSRichard Tran Mills    Level: intermediate
3084f089877aSRichard Tran Mills 
3085bc0a1609SRichard Tran Mills    Notes:
3086bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3087bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3088bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3089bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3090bc0a1609SRichard Tran Mills 
3091bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3092f089877aSRichard Tran Mills 
3093f089877aSRichard Tran Mills @*/
3094f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
3095f089877aSRichard Tran Mills {
3096f089877aSRichard Tran Mills   PetscErrorCode          ierr;
3097f089877aSRichard Tran Mills 
3098f089877aSRichard Tran Mills   PetscFunctionBegin;
3099f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3100bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3101f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
3102f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3103f089877aSRichard Tran Mills }
3104f089877aSRichard Tran Mills 
310547c6ae99SBarry Smith /*@
310647c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
310747c6ae99SBarry Smith 
3108d083f849SBarry Smith     Collective on dm
310947c6ae99SBarry Smith 
3110d8d19677SJose E. Roman     Input Parameters:
311147c6ae99SBarry Smith +   dm - the DM object
311291d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
311347c6ae99SBarry Smith 
311447c6ae99SBarry Smith     Output Parameter:
311547c6ae99SBarry Smith .   dmc - the coarsened DM
311647c6ae99SBarry Smith 
311747c6ae99SBarry Smith     Level: developer
311847c6ae99SBarry Smith 
3119e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
312047c6ae99SBarry Smith 
312147c6ae99SBarry Smith @*/
31227087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
312347c6ae99SBarry Smith {
312447c6ae99SBarry Smith   PetscErrorCode    ierr;
3125b17ce1afSJed Brown   DMCoarsenHookLink link;
312647c6ae99SBarry Smith 
312747c6ae99SBarry Smith   PetscFunctionBegin;
3128171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3129b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
313047a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
313147c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
3132b9d85ea2SLisandro Dalcin   if (*dmc) {
3133a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
313443842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
31358cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
3136644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
31370598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3138656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
3139e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
3140b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
3141b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
3142b17ce1afSJed Brown     }
3143b9d85ea2SLisandro Dalcin   }
314447a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
3145b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3146b17ce1afSJed Brown   PetscFunctionReturn(0);
3147b17ce1afSJed Brown }
3148b17ce1afSJed Brown 
3149bb9467b5SJed Brown /*@C
3150b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3151b17ce1afSJed Brown 
3152b17ce1afSJed Brown    Logically Collective
3153b17ce1afSJed Brown 
31544165533cSJose E. Roman    Input Parameters:
3155b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3156b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3157b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
31580298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3159b17ce1afSJed Brown 
3160b17ce1afSJed Brown    Calling sequence of coarsenhook:
3161b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3162b17ce1afSJed Brown 
3163b17ce1afSJed Brown +  fine - fine level DM
3164b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3165b17ce1afSJed Brown -  ctx - optional user-defined function context
3166b17ce1afSJed Brown 
3167b17ce1afSJed Brown    Calling sequence for restricthook:
3168c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3169b17ce1afSJed Brown 
3170b17ce1afSJed Brown +  fine - fine level DM
3171b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3172c833c3b5SJed Brown .  rscale - scaling vector for restriction
3173c833c3b5SJed Brown .  inject - matrix restricting by injection
3174b17ce1afSJed Brown .  coarse - coarse level DM to update
3175b17ce1afSJed Brown -  ctx - optional user-defined function context
3176b17ce1afSJed Brown 
3177b17ce1afSJed Brown    Level: advanced
3178b17ce1afSJed Brown 
3179b17ce1afSJed Brown    Notes:
3180b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3181b17ce1afSJed Brown 
3182b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3183b17ce1afSJed Brown 
3184b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3185b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3186b17ce1afSJed Brown 
3187bb9467b5SJed Brown    This function is currently not available from Fortran.
3188bb9467b5SJed Brown 
3189dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3190b17ce1afSJed Brown @*/
3191b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3192b17ce1afSJed Brown {
3193b17ce1afSJed Brown   PetscErrorCode    ierr;
3194b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3195b17ce1afSJed Brown 
3196b17ce1afSJed Brown   PetscFunctionBegin;
3197b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
31981e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
31991e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
32001e3d8eccSJed Brown   }
320195dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
3202b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3203b17ce1afSJed Brown   link->restricthook = restricthook;
3204b17ce1afSJed Brown   link->ctx          = ctx;
32050298fd71SBarry Smith   link->next         = NULL;
3206b17ce1afSJed Brown   *p                 = link;
3207b17ce1afSJed Brown   PetscFunctionReturn(0);
3208b17ce1afSJed Brown }
3209b17ce1afSJed Brown 
3210dc822a44SJed Brown /*@C
3211dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3212dc822a44SJed Brown 
3213dc822a44SJed Brown    Logically Collective
3214dc822a44SJed Brown 
32154165533cSJose E. Roman    Input Parameters:
3216dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3217dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3218dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3219dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3220dc822a44SJed Brown 
3221dc822a44SJed Brown    Level: advanced
3222dc822a44SJed Brown 
3223dc822a44SJed Brown    Notes:
3224dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3225dc822a44SJed Brown 
3226dc822a44SJed Brown    This function is currently not available from Fortran.
3227dc822a44SJed Brown 
3228dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3229dc822a44SJed Brown @*/
3230dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3231dc822a44SJed Brown {
3232dc822a44SJed Brown   PetscErrorCode    ierr;
3233dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3234dc822a44SJed Brown 
3235dc822a44SJed Brown   PetscFunctionBegin;
3236dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3237dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3238dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3239dc822a44SJed Brown       link = *p;
3240dc822a44SJed Brown       *p = link->next;
3241dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3242dc822a44SJed Brown       break;
3243dc822a44SJed Brown     }
3244dc822a44SJed Brown   }
3245dc822a44SJed Brown   PetscFunctionReturn(0);
3246dc822a44SJed Brown }
3247dc822a44SJed Brown 
3248b17ce1afSJed Brown /*@
3249b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3250b17ce1afSJed Brown 
3251b17ce1afSJed Brown    Collective if any hooks are
3252b17ce1afSJed Brown 
32534165533cSJose E. Roman    Input Parameters:
3254b17ce1afSJed Brown +  fine - finer DM to use as a base
3255b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3256e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3257b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3258e91eccc2SStefano Zampini -  coarse - coarser DM to update
3259b17ce1afSJed Brown 
3260b17ce1afSJed Brown    Level: developer
3261b17ce1afSJed Brown 
3262b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3263b17ce1afSJed Brown @*/
3264b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3265b17ce1afSJed Brown {
3266b17ce1afSJed Brown   PetscErrorCode    ierr;
3267b17ce1afSJed Brown   DMCoarsenHookLink link;
3268b17ce1afSJed Brown 
3269b17ce1afSJed Brown   PetscFunctionBegin;
3270b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
32718865f1eaSKarl Rupp     if (link->restricthook) {
32728865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
32738865f1eaSKarl Rupp     }
3274b17ce1afSJed Brown   }
327547c6ae99SBarry Smith   PetscFunctionReturn(0);
327647c6ae99SBarry Smith }
327747c6ae99SBarry Smith 
3278bb9467b5SJed Brown /*@C
3279be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
32805dbd56e3SPeter Brune 
3281d083f849SBarry Smith    Logically Collective on global
32825dbd56e3SPeter Brune 
32834165533cSJose E. Roman    Input Parameters:
32845dbd56e3SPeter Brune +  global - global DM
3285ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
32865dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
32870298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
32885dbd56e3SPeter Brune 
3289ec4806b8SPeter Brune    Calling sequence for ddhook:
3290ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3291ec4806b8SPeter Brune 
3292ec4806b8SPeter Brune +  global - global DM
3293ec4806b8SPeter Brune .  block  - block DM
3294ec4806b8SPeter Brune -  ctx - optional user-defined function context
3295ec4806b8SPeter Brune 
32965dbd56e3SPeter Brune    Calling sequence for restricthook:
3297ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
32985dbd56e3SPeter Brune 
32995dbd56e3SPeter Brune +  global - global DM
33005dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
33015dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3302ec4806b8SPeter Brune .  block  - block DM
33035dbd56e3SPeter Brune -  ctx - optional user-defined function context
33045dbd56e3SPeter Brune 
33055dbd56e3SPeter Brune    Level: advanced
33065dbd56e3SPeter Brune 
33075dbd56e3SPeter Brune    Notes:
3308ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
33095dbd56e3SPeter Brune 
33105dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
33115dbd56e3SPeter Brune 
33125dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3313ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
33145dbd56e3SPeter Brune 
3315bb9467b5SJed Brown    This function is currently not available from Fortran.
3316bb9467b5SJed Brown 
33175dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
33185dbd56e3SPeter Brune @*/
3319be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
33205dbd56e3SPeter Brune {
33215dbd56e3SPeter Brune   PetscErrorCode      ierr;
3322be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
33235dbd56e3SPeter Brune 
33245dbd56e3SPeter Brune   PetscFunctionBegin;
33255dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3326b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3327b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3328b3a6b972SJed Brown   }
332995dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
33305dbd56e3SPeter Brune   link->restricthook = restricthook;
3331be081cd6SPeter Brune   link->ddhook       = ddhook;
33325dbd56e3SPeter Brune   link->ctx          = ctx;
33330298fd71SBarry Smith   link->next         = NULL;
33345dbd56e3SPeter Brune   *p                 = link;
33355dbd56e3SPeter Brune   PetscFunctionReturn(0);
33365dbd56e3SPeter Brune }
33375dbd56e3SPeter Brune 
3338b3a6b972SJed Brown /*@C
3339b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3340b3a6b972SJed Brown 
3341b3a6b972SJed Brown    Logically Collective
3342b3a6b972SJed Brown 
33434165533cSJose E. Roman    Input Parameters:
3344b3a6b972SJed Brown +  global - global DM
3345b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3346b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3347b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3348b3a6b972SJed Brown 
3349b3a6b972SJed Brown    Level: advanced
3350b3a6b972SJed Brown 
3351b3a6b972SJed Brown    Notes:
3352b3a6b972SJed Brown 
3353b3a6b972SJed Brown    This function is currently not available from Fortran.
3354b3a6b972SJed Brown 
3355b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3356b3a6b972SJed Brown @*/
3357b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3358b3a6b972SJed Brown {
3359b3a6b972SJed Brown   PetscErrorCode      ierr;
3360b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3361b3a6b972SJed Brown 
3362b3a6b972SJed Brown   PetscFunctionBegin;
3363b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3364b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3365b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3366b3a6b972SJed Brown       link = *p;
3367b3a6b972SJed Brown       *p = link->next;
3368b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3369b3a6b972SJed Brown       break;
3370b3a6b972SJed Brown     }
3371b3a6b972SJed Brown   }
3372b3a6b972SJed Brown   PetscFunctionReturn(0);
3373b3a6b972SJed Brown }
3374b3a6b972SJed Brown 
33755dbd56e3SPeter Brune /*@
3376be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
33775dbd56e3SPeter Brune 
33785dbd56e3SPeter Brune    Collective if any hooks are
33795dbd56e3SPeter Brune 
33804165533cSJose E. Roman    Input Parameters:
33815dbd56e3SPeter Brune +  fine - finer DM to use as a base
3382be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3383be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
33845dbd56e3SPeter Brune -  coarse - coarer DM to update
33855dbd56e3SPeter Brune 
33865dbd56e3SPeter Brune    Level: developer
33875dbd56e3SPeter Brune 
33885dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
33895dbd56e3SPeter Brune @*/
3390be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
33915dbd56e3SPeter Brune {
33925dbd56e3SPeter Brune   PetscErrorCode      ierr;
3393be081cd6SPeter Brune   DMSubDomainHookLink link;
33945dbd56e3SPeter Brune 
33955dbd56e3SPeter Brune   PetscFunctionBegin;
3396be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
33978865f1eaSKarl Rupp     if (link->restricthook) {
33988865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
33998865f1eaSKarl Rupp     }
34005dbd56e3SPeter Brune   }
34015dbd56e3SPeter Brune   PetscFunctionReturn(0);
34025dbd56e3SPeter Brune }
34035dbd56e3SPeter Brune 
34045fe1f584SPeter Brune /*@
34056a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
34065fe1f584SPeter Brune 
34075fe1f584SPeter Brune     Not Collective
34085fe1f584SPeter Brune 
34095fe1f584SPeter Brune     Input Parameter:
34105fe1f584SPeter Brune .   dm - the DM object
34115fe1f584SPeter Brune 
34125fe1f584SPeter Brune     Output Parameter:
34136a7d9d85SPeter Brune .   level - number of coarsenings
34145fe1f584SPeter Brune 
34155fe1f584SPeter Brune     Level: developer
34165fe1f584SPeter Brune 
34176a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
34185fe1f584SPeter Brune 
34195fe1f584SPeter Brune @*/
34205fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
34215fe1f584SPeter Brune {
34225fe1f584SPeter Brune   PetscFunctionBegin;
34235fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3424b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
34255fe1f584SPeter Brune   *level = dm->leveldown;
34265fe1f584SPeter Brune   PetscFunctionReturn(0);
34275fe1f584SPeter Brune }
34285fe1f584SPeter Brune 
34299a64c4a8SMatthew G. Knepley /*@
34309a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
34319a64c4a8SMatthew G. Knepley 
34329a64c4a8SMatthew G. Knepley     Not Collective
34339a64c4a8SMatthew G. Knepley 
34349a64c4a8SMatthew G. Knepley     Input Parameters:
34359a64c4a8SMatthew G. Knepley +   dm - the DM object
34369a64c4a8SMatthew G. Knepley -   level - number of coarsenings
34379a64c4a8SMatthew G. Knepley 
34389a64c4a8SMatthew G. Knepley     Level: developer
34399a64c4a8SMatthew G. Knepley 
34409a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
34419a64c4a8SMatthew G. Knepley @*/
34429a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
34439a64c4a8SMatthew G. Knepley {
34449a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
34459a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34469a64c4a8SMatthew G. Knepley   dm->leveldown = level;
34479a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
34489a64c4a8SMatthew G. Knepley }
34499a64c4a8SMatthew G. Knepley 
345047c6ae99SBarry Smith /*@C
345147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
345247c6ae99SBarry Smith 
3453d083f849SBarry Smith     Collective on dm
345447c6ae99SBarry Smith 
3455d8d19677SJose E. Roman     Input Parameters:
345647c6ae99SBarry Smith +   dm - the DM object
345747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
345847c6ae99SBarry Smith 
345947c6ae99SBarry Smith     Output Parameter:
346047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
346147c6ae99SBarry Smith 
346247c6ae99SBarry Smith     Level: developer
346347c6ae99SBarry Smith 
3464e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
346547c6ae99SBarry Smith 
346647c6ae99SBarry Smith @*/
34677087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
346847c6ae99SBarry Smith {
346947c6ae99SBarry Smith   PetscErrorCode ierr;
347047c6ae99SBarry Smith 
347147c6ae99SBarry Smith   PetscFunctionBegin;
3472171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3473ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
347447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3475b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
347647c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
347747c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
347847c6ae99SBarry Smith   } else if (dm->ops->refine) {
347947c6ae99SBarry Smith     PetscInt i;
348047c6ae99SBarry Smith 
3481ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
348247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3483ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
348447c6ae99SBarry Smith     }
3485ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
348647c6ae99SBarry Smith   PetscFunctionReturn(0);
348747c6ae99SBarry Smith }
348847c6ae99SBarry Smith 
348947c6ae99SBarry Smith /*@C
349047c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
349147c6ae99SBarry Smith 
3492d083f849SBarry Smith     Collective on dm
349347c6ae99SBarry Smith 
3494d8d19677SJose E. Roman     Input Parameters:
349547c6ae99SBarry Smith +   dm - the DM object
349647c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
349747c6ae99SBarry Smith 
349847c6ae99SBarry Smith     Output Parameter:
349947c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
350047c6ae99SBarry Smith 
350147c6ae99SBarry Smith     Level: developer
350247c6ae99SBarry Smith 
3503e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
350447c6ae99SBarry Smith 
350547c6ae99SBarry Smith @*/
35067087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
350747c6ae99SBarry Smith {
350847c6ae99SBarry Smith   PetscErrorCode ierr;
350947c6ae99SBarry Smith 
351047c6ae99SBarry Smith   PetscFunctionBegin;
3511171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3512ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
351347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
351447c6ae99SBarry Smith   PetscValidPointer(dmc,3);
351547c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
351647c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
351747c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
351847c6ae99SBarry Smith     PetscInt i;
351947c6ae99SBarry Smith 
3520ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
352147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3522ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
352347c6ae99SBarry Smith     }
3524ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
352547c6ae99SBarry Smith   PetscFunctionReturn(0);
352647c6ae99SBarry Smith }
352747c6ae99SBarry Smith 
35281a266240SBarry Smith /*@C
35291a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
35301a266240SBarry Smith 
35311a266240SBarry Smith     Not Collective
35321a266240SBarry Smith 
35331a266240SBarry Smith     Input Parameters:
35341a266240SBarry Smith +   dm - the DM object
35351a266240SBarry Smith -   destroy - the destroy function
35361a266240SBarry Smith 
35371a266240SBarry Smith     Level: intermediate
35381a266240SBarry Smith 
3539e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
35401a266240SBarry Smith 
3541f07f9ceaSJed Brown @*/
35421a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
35431a266240SBarry Smith {
35441a266240SBarry Smith   PetscFunctionBegin;
3545171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35461a266240SBarry Smith   dm->ctxdestroy = destroy;
35471a266240SBarry Smith   PetscFunctionReturn(0);
35481a266240SBarry Smith }
35491a266240SBarry Smith 
3550b07ff414SBarry Smith /*@
35511b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
355247c6ae99SBarry Smith 
355347c6ae99SBarry Smith     Not Collective
355447c6ae99SBarry Smith 
355547c6ae99SBarry Smith     Input Parameters:
355647c6ae99SBarry Smith +   dm - the DM object
355747c6ae99SBarry Smith -   ctx - the user context
355847c6ae99SBarry Smith 
355947c6ae99SBarry Smith     Level: intermediate
356047c6ae99SBarry Smith 
3561e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
356247c6ae99SBarry Smith 
356347c6ae99SBarry Smith @*/
35641b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
356547c6ae99SBarry Smith {
356647c6ae99SBarry Smith   PetscFunctionBegin;
3567171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
356847c6ae99SBarry Smith   dm->ctx = ctx;
356947c6ae99SBarry Smith   PetscFunctionReturn(0);
357047c6ae99SBarry Smith }
357147c6ae99SBarry Smith 
357247c6ae99SBarry Smith /*@
35731b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
357447c6ae99SBarry Smith 
357547c6ae99SBarry Smith     Not Collective
357647c6ae99SBarry Smith 
357747c6ae99SBarry Smith     Input Parameter:
357847c6ae99SBarry Smith .   dm - the DM object
357947c6ae99SBarry Smith 
358047c6ae99SBarry Smith     Output Parameter:
358147c6ae99SBarry Smith .   ctx - the user context
358247c6ae99SBarry Smith 
358347c6ae99SBarry Smith     Level: intermediate
358447c6ae99SBarry Smith 
3585e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
358647c6ae99SBarry Smith 
358747c6ae99SBarry Smith @*/
35881b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
358947c6ae99SBarry Smith {
359047c6ae99SBarry Smith   PetscFunctionBegin;
3591171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35921b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
359347c6ae99SBarry Smith   PetscFunctionReturn(0);
359447c6ae99SBarry Smith }
359547c6ae99SBarry Smith 
359608da532bSDmitry Karpeev /*@C
3597df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
359808da532bSDmitry Karpeev 
3599d083f849SBarry Smith     Logically Collective on dm
360008da532bSDmitry Karpeev 
3601d8d19677SJose E. Roman     Input Parameters:
360208da532bSDmitry Karpeev +   dm - the DM object
36030298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
360408da532bSDmitry Karpeev 
360508da532bSDmitry Karpeev     Level: intermediate
360608da532bSDmitry Karpeev 
3607835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
360808da532bSDmitry Karpeev          DMSetJacobian()
360908da532bSDmitry Karpeev 
361008da532bSDmitry Karpeev @*/
361108da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
361208da532bSDmitry Karpeev {
361308da532bSDmitry Karpeev   PetscFunctionBegin;
36145a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
361508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
361608da532bSDmitry Karpeev   PetscFunctionReturn(0);
361708da532bSDmitry Karpeev }
361808da532bSDmitry Karpeev 
361908da532bSDmitry Karpeev /*@
362008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
362108da532bSDmitry Karpeev 
362208da532bSDmitry Karpeev     Not Collective
362308da532bSDmitry Karpeev 
362408da532bSDmitry Karpeev     Input Parameter:
362508da532bSDmitry Karpeev .   dm - the DM object to destroy
362608da532bSDmitry Karpeev 
362708da532bSDmitry Karpeev     Output Parameter:
362808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
362908da532bSDmitry Karpeev 
363008da532bSDmitry Karpeev     Level: developer
363108da532bSDmitry Karpeev 
363274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
363308da532bSDmitry Karpeev 
363408da532bSDmitry Karpeev @*/
363508da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
363608da532bSDmitry Karpeev {
363708da532bSDmitry Karpeev   PetscFunctionBegin;
36385a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3639534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
364008da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
364108da532bSDmitry Karpeev   PetscFunctionReturn(0);
364208da532bSDmitry Karpeev }
364308da532bSDmitry Karpeev 
364408da532bSDmitry Karpeev /*@C
364508da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
364608da532bSDmitry Karpeev 
3647d083f849SBarry Smith     Logically Collective on dm
364808da532bSDmitry Karpeev 
3649f899ff85SJose E. Roman     Input Parameter:
3650907376e6SBarry Smith .   dm - the DM object
365108da532bSDmitry Karpeev 
365208da532bSDmitry Karpeev     Output parameters:
365308da532bSDmitry Karpeev +   xl - lower bound
365408da532bSDmitry Karpeev -   xu - upper bound
365508da532bSDmitry Karpeev 
3656907376e6SBarry Smith     Level: advanced
3657907376e6SBarry Smith 
365895452b02SPatrick Sanan     Notes:
365995452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
366008da532bSDmitry Karpeev 
366174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
366208da532bSDmitry Karpeev 
366308da532bSDmitry Karpeev @*/
366408da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
366508da532bSDmitry Karpeev {
366608da532bSDmitry Karpeev   PetscErrorCode ierr;
36675fd66863SKarl Rupp 
366808da532bSDmitry Karpeev   PetscFunctionBegin;
36695a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
367008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
36715a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3672b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
367308da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
367408da532bSDmitry Karpeev   PetscFunctionReturn(0);
367508da532bSDmitry Karpeev }
367608da532bSDmitry Karpeev 
3677b0ae01b7SPeter Brune /*@
3678b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3679b0ae01b7SPeter Brune 
3680b0ae01b7SPeter Brune     Not Collective
3681b0ae01b7SPeter Brune 
3682b0ae01b7SPeter Brune     Input Parameter:
3683b0ae01b7SPeter Brune .   dm - the DM object
3684b0ae01b7SPeter Brune 
3685b0ae01b7SPeter Brune     Output Parameter:
3686b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3687b0ae01b7SPeter Brune 
3688b0ae01b7SPeter Brune     Level: developer
3689b0ae01b7SPeter Brune 
36901565f0a7SPatrick Sanan .seealso DMCreateColoring()
3691b0ae01b7SPeter Brune 
3692b0ae01b7SPeter Brune @*/
3693b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3694b0ae01b7SPeter Brune {
3695b0ae01b7SPeter Brune   PetscFunctionBegin;
36965a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3697534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3698b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3699b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3700b0ae01b7SPeter Brune }
3701b0ae01b7SPeter Brune 
37023ad4599aSBarry Smith /*@
37033ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
37043ad4599aSBarry Smith 
37053ad4599aSBarry Smith     Not Collective
37063ad4599aSBarry Smith 
37073ad4599aSBarry Smith     Input Parameter:
37083ad4599aSBarry Smith .   dm - the DM object
37093ad4599aSBarry Smith 
37103ad4599aSBarry Smith     Output Parameter:
37113ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
37123ad4599aSBarry Smith 
37133ad4599aSBarry Smith     Level: developer
37143ad4599aSBarry Smith 
37151565f0a7SPatrick Sanan .seealso DMCreateRestriction()
37163ad4599aSBarry Smith 
37173ad4599aSBarry Smith @*/
37183ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
37193ad4599aSBarry Smith {
37203ad4599aSBarry Smith   PetscFunctionBegin;
37215a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3722534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37233ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
37243ad4599aSBarry Smith   PetscFunctionReturn(0);
37253ad4599aSBarry Smith }
37263ad4599aSBarry Smith 
3727a7058e45SLawrence Mitchell /*@
3728a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3729a7058e45SLawrence Mitchell 
3730a7058e45SLawrence Mitchell     Not Collective
3731a7058e45SLawrence Mitchell 
3732a7058e45SLawrence Mitchell     Input Parameter:
3733a7058e45SLawrence Mitchell .   dm - the DM object
3734a7058e45SLawrence Mitchell 
3735a7058e45SLawrence Mitchell     Output Parameter:
3736a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3737a7058e45SLawrence Mitchell 
3738a7058e45SLawrence Mitchell     Level: developer
3739a7058e45SLawrence Mitchell 
37401565f0a7SPatrick Sanan .seealso DMCreateInjection()
3741a7058e45SLawrence Mitchell 
3742a7058e45SLawrence Mitchell @*/
3743a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3744a7058e45SLawrence Mitchell {
37454a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
37465a84ad33SLisandro Dalcin 
3747a7058e45SLawrence Mitchell   PetscFunctionBegin;
37485a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3749534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37505a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
37514a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
37525a84ad33SLisandro Dalcin   } else {
37535a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
37545a84ad33SLisandro Dalcin   }
3755a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3756a7058e45SLawrence Mitchell }
3757a7058e45SLawrence Mitchell 
37580298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3759264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3760264ace61SBarry Smith 
3761264ace61SBarry Smith /*@C
3762264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3763264ace61SBarry Smith 
3764d083f849SBarry Smith   Collective on dm
3765264ace61SBarry Smith 
3766264ace61SBarry Smith   Input Parameters:
3767264ace61SBarry Smith + dm     - The DM object
3768264ace61SBarry Smith - method - The name of the DM type
3769264ace61SBarry Smith 
3770264ace61SBarry Smith   Options Database Key:
3771264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3772264ace61SBarry Smith 
3773264ace61SBarry Smith   Notes:
3774e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3775264ace61SBarry Smith 
3776264ace61SBarry Smith   Level: intermediate
3777264ace61SBarry Smith 
3778264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3779264ace61SBarry Smith @*/
378019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3781264ace61SBarry Smith {
3782264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3783264ace61SBarry Smith   PetscBool      match;
3784264ace61SBarry Smith   PetscErrorCode ierr;
3785264ace61SBarry Smith 
3786264ace61SBarry Smith   PetscFunctionBegin;
3787264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3788251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3789264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3790264ace61SBarry Smith 
37910f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
37921c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3793ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3794264ace61SBarry Smith 
3795264ace61SBarry Smith   if (dm->ops->destroy) {
3796264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3797264ace61SBarry Smith   }
3798d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3799264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3800d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3801264ace61SBarry Smith   PetscFunctionReturn(0);
3802264ace61SBarry Smith }
3803264ace61SBarry Smith 
3804264ace61SBarry Smith /*@C
3805264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3806264ace61SBarry Smith 
3807264ace61SBarry Smith   Not Collective
3808264ace61SBarry Smith 
3809264ace61SBarry Smith   Input Parameter:
3810264ace61SBarry Smith . dm  - The DM
3811264ace61SBarry Smith 
3812264ace61SBarry Smith   Output Parameter:
3813264ace61SBarry Smith . type - The DM type name
3814264ace61SBarry Smith 
3815264ace61SBarry Smith   Level: intermediate
3816264ace61SBarry Smith 
3817264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3818264ace61SBarry Smith @*/
381919fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3820264ace61SBarry Smith {
3821264ace61SBarry Smith   PetscErrorCode ierr;
3822264ace61SBarry Smith 
3823264ace61SBarry Smith   PetscFunctionBegin;
3824264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3825c959eef4SJed Brown   PetscValidPointer(type,2);
3826607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3827264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3828264ace61SBarry Smith   PetscFunctionReturn(0);
3829264ace61SBarry Smith }
3830264ace61SBarry Smith 
383167a56275SMatthew G Knepley /*@C
383267a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
383367a56275SMatthew G Knepley 
3834d083f849SBarry Smith   Collective on dm
383567a56275SMatthew G Knepley 
383667a56275SMatthew G Knepley   Input Parameters:
383767a56275SMatthew G Knepley + dm - the DM
383867a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
383967a56275SMatthew G Knepley 
384067a56275SMatthew G Knepley   Output Parameter:
384167a56275SMatthew G Knepley . M - pointer to new DM
384267a56275SMatthew G Knepley 
384367a56275SMatthew G Knepley   Notes:
384467a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
384567a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
384667a56275SMatthew G Knepley   of the input DM.
384767a56275SMatthew G Knepley 
384867a56275SMatthew G Knepley   Level: intermediate
384967a56275SMatthew G Knepley 
385067a56275SMatthew G Knepley .seealso: DMCreate()
385167a56275SMatthew G Knepley @*/
385219fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
385367a56275SMatthew G Knepley {
385467a56275SMatthew G Knepley   DM             B;
385567a56275SMatthew G Knepley   char           convname[256];
3856c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
385767a56275SMatthew G Knepley   PetscErrorCode ierr;
385867a56275SMatthew G Knepley 
385967a56275SMatthew G Knepley   PetscFunctionBegin;
386067a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
386167a56275SMatthew G Knepley   PetscValidType(dm,1);
386267a56275SMatthew G Knepley   PetscValidPointer(M,3);
3863251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3864c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3865c067b6caSMatthew G. Knepley   if (sametype) {
3866c067b6caSMatthew G. Knepley     *M   = dm;
3867c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3868c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3869c067b6caSMatthew G. Knepley   } else {
38700298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
387167a56275SMatthew G Knepley 
387267a56275SMatthew G Knepley     /*
387367a56275SMatthew G Knepley        Order of precedence:
387467a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
387567a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
387667a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
387767a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
387867a56275SMatthew G Knepley        5) Use a really basic converter.
387967a56275SMatthew G Knepley     */
388067a56275SMatthew G Knepley 
388167a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3882a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3883a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3884a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3885a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3886a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
38870005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
388867a56275SMatthew G Knepley     if (conv) goto foundconv;
388967a56275SMatthew G Knepley 
389067a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
389182f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
389267a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3893a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3894a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3895a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3896a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3897a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
38980005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
389967a56275SMatthew G Knepley     if (conv) {
3900fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
390167a56275SMatthew G Knepley       goto foundconv;
390267a56275SMatthew G Knepley     }
390367a56275SMatthew G Knepley 
390467a56275SMatthew G Knepley #if 0
390567a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
390667a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3907fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
390867a56275SMatthew G Knepley     if (conv) goto foundconv;
390967a56275SMatthew G Knepley 
391067a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
391167a56275SMatthew G Knepley     if (dm->ops->convert) {
391267a56275SMatthew G Knepley       conv = dm->ops->convert;
391367a56275SMatthew G Knepley     }
391467a56275SMatthew G Knepley     if (conv) goto foundconv;
391567a56275SMatthew G Knepley #endif
391667a56275SMatthew G Knepley 
391767a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
391882f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
391967a56275SMatthew G Knepley 
392067a56275SMatthew G Knepley foundconv:
392167a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
392267a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
392312fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
392490b157c4SStefano Zampini     {
392590b157c4SStefano Zampini       PetscBool             isper;
392612fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
392712fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
392890b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
392990b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
3930c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
3931a587d139SMark       ierr = PetscFree((*M)->vectype);CHKERRQ(ierr);
3932a587d139SMark       ierr = PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype);CHKERRQ(ierr);
3933a587d139SMark       ierr = PetscFree((*M)->mattype);CHKERRQ(ierr);
3934a587d139SMark       ierr = PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype);CHKERRQ(ierr);
393512fa691eSMatthew G. Knepley     }
393667a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
393767a56275SMatthew G Knepley   }
393867a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
393967a56275SMatthew G Knepley   PetscFunctionReturn(0);
394067a56275SMatthew G Knepley }
3941264ace61SBarry Smith 
3942264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3943264ace61SBarry Smith 
3944264ace61SBarry Smith /*@C
39451c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
39461c84c290SBarry Smith 
39471c84c290SBarry Smith   Not Collective
39481c84c290SBarry Smith 
39491c84c290SBarry Smith   Input Parameters:
39501c84c290SBarry Smith + name        - The name of a new user-defined creation routine
39511c84c290SBarry Smith - create_func - The creation routine itself
39521c84c290SBarry Smith 
39531c84c290SBarry Smith   Notes:
39541c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
39551c84c290SBarry Smith 
39561c84c290SBarry Smith   Sample usage:
39571c84c290SBarry Smith .vb
3958bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
39591c84c290SBarry Smith .ve
39601c84c290SBarry Smith 
39611c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
39621c84c290SBarry Smith .vb
39631c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
39641c84c290SBarry Smith     DMSetType(DM,"my_da");
39651c84c290SBarry Smith .ve
39661c84c290SBarry Smith    or at runtime via the option
39671c84c290SBarry Smith .vb
39681c84c290SBarry Smith     -da_type my_da
39691c84c290SBarry Smith .ve
3970264ace61SBarry Smith 
3971264ace61SBarry Smith   Level: advanced
39721c84c290SBarry Smith 
3973bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
39741c84c290SBarry Smith 
3975264ace61SBarry Smith @*/
3976bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3977264ace61SBarry Smith {
3978264ace61SBarry Smith   PetscErrorCode ierr;
3979264ace61SBarry Smith 
3980264ace61SBarry Smith   PetscFunctionBegin;
39811d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3982a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3983264ace61SBarry Smith   PetscFunctionReturn(0);
3984264ace61SBarry Smith }
3985264ace61SBarry Smith 
3986b859378eSBarry Smith /*@C
398755849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3988b859378eSBarry Smith 
3989d083f849SBarry Smith   Collective on viewer
3990b859378eSBarry Smith 
3991b859378eSBarry Smith   Input Parameters:
3992b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3993b859378eSBarry Smith            some related function before a call to DMLoad().
3994b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3995b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3996b859378eSBarry Smith 
3997b859378eSBarry Smith    Level: intermediate
3998b859378eSBarry Smith 
3999b859378eSBarry Smith   Notes:
400055849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
4001b859378eSBarry Smith 
4002b859378eSBarry Smith   Notes for advanced users:
4003b859378eSBarry Smith   Most users should not need to know the details of the binary storage
4004b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
4005b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
4006b859378eSBarry Smith   format is
4007b859378eSBarry Smith .vb
4008b859378eSBarry Smith      has not yet been determined
4009b859378eSBarry Smith .ve
4010b859378eSBarry Smith 
4011b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
4012b859378eSBarry Smith @*/
4013b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
4014b859378eSBarry Smith {
40159331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
4016b859378eSBarry Smith   PetscErrorCode ierr;
4017b859378eSBarry Smith 
4018b859378eSBarry Smith   PetscFunctionBegin;
4019b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
4020b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4021fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
402232c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
40239331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
402458cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
40259331c7a4SMatthew G. Knepley   if (isbinary) {
40269331c7a4SMatthew G. Knepley     PetscInt classid;
40279331c7a4SMatthew G. Knepley     char     type[256];
4028b859378eSBarry Smith 
4029060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
40309200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
4031060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
403232c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
40339331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
40349331c7a4SMatthew G. Knepley   } else if (ishdf5) {
40359331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
40369331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
403758cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
4038b859378eSBarry Smith   PetscFunctionReturn(0);
4039b859378eSBarry Smith }
4040b859378eSBarry Smith 
4041b2e4378dSMatthew G. Knepley /*@
4042b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
4043b2e4378dSMatthew G. Knepley 
4044b2e4378dSMatthew G. Knepley   Not collective
4045b2e4378dSMatthew G. Knepley 
4046b2e4378dSMatthew G. Knepley   Input Parameter:
4047b2e4378dSMatthew G. Knepley . dm - the DM
4048b2e4378dSMatthew G. Knepley 
4049b2e4378dSMatthew G. Knepley   Output Parameters:
4050b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
4051b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
4052b2e4378dSMatthew G. Knepley 
4053b2e4378dSMatthew G. Knepley   Level: beginner
4054b2e4378dSMatthew G. Knepley 
4055b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
4056b2e4378dSMatthew G. Knepley 
4057b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
4058b2e4378dSMatthew G. Knepley @*/
4059b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
4060b2e4378dSMatthew G. Knepley {
4061b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
4062b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
4063b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
4064b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
4065b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
4066b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
4067b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
4068b2e4378dSMatthew G. Knepley 
4069b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4070b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4071b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4072b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
4073b2e4378dSMatthew G. Knepley   if (coords) {
4074b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
4075b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
4076b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
4077b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
4078b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
4079b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4080b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4081b2e4378dSMatthew G. Knepley       }
4082b2e4378dSMatthew G. Knepley     }
4083b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
4084b2e4378dSMatthew G. Knepley   } else {
4085b2e4378dSMatthew G. Knepley     PetscBool isda;
4086b2e4378dSMatthew G. Knepley 
4087b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
4088b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
4089b2e4378dSMatthew G. Knepley   }
4090b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
4091b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
4092b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4093b2e4378dSMatthew G. Knepley }
4094b2e4378dSMatthew G. Knepley 
4095b2e4378dSMatthew G. Knepley /*@
4096b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
4097b2e4378dSMatthew G. Knepley 
4098b2e4378dSMatthew G. Knepley   Collective
4099b2e4378dSMatthew G. Knepley 
4100b2e4378dSMatthew G. Knepley   Input Parameter:
4101b2e4378dSMatthew G. Knepley . dm - the DM
4102b2e4378dSMatthew G. Knepley 
4103b2e4378dSMatthew G. Knepley   Output Parameters:
4104b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
4105b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
4106b2e4378dSMatthew G. Knepley 
4107b2e4378dSMatthew G. Knepley   Level: beginner
4108b2e4378dSMatthew G. Knepley 
4109b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
4110b2e4378dSMatthew G. Knepley @*/
4111b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
4112b2e4378dSMatthew G. Knepley {
4113b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
41146ce308c4SMatthew G. Knepley   PetscInt       cdim;
41156ce308c4SMatthew G. Knepley   PetscMPIInt    count;
4116b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
4117b2e4378dSMatthew G. Knepley 
4118b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4119b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4120b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4121b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
4122b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
4123820f2d46SBarry Smith   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4124820f2d46SBarry Smith   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4125b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4126b2e4378dSMatthew G. Knepley }
4127b2e4378dSMatthew G. Knepley 
41287da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41297da65231SMatthew G Knepley 
4130a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4131a6dfd86eSKarl Rupp {
41321d47ebbbSSatish Balay   PetscInt       f;
41331b30c384SMatthew G Knepley   PetscErrorCode ierr;
41341b30c384SMatthew G Knepley 
41357da65231SMatthew G Knepley   PetscFunctionBegin;
413674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
41371d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
413857622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
41397da65231SMatthew G Knepley   }
41407da65231SMatthew G Knepley   PetscFunctionReturn(0);
41417da65231SMatthew G Knepley }
41427da65231SMatthew G Knepley 
4143a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4144a6dfd86eSKarl Rupp {
41451b30c384SMatthew G Knepley   PetscInt       f, g;
41467da65231SMatthew G Knepley   PetscErrorCode ierr;
41477da65231SMatthew G Knepley 
41487da65231SMatthew G Knepley   PetscFunctionBegin;
414974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
41501d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
415174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
41521d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
4153e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
41547da65231SMatthew G Knepley     }
415574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
41567da65231SMatthew G Knepley   }
41577da65231SMatthew G Knepley   PetscFunctionReturn(0);
41587da65231SMatthew G Knepley }
4159e7c4fc90SDmitry Karpeev 
41606113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4161e759306cSMatthew G. Knepley {
41620c5b8624SToby Isaac   PetscInt          localSize, bs;
41630c5b8624SToby Isaac   PetscMPIInt       size;
41640c5b8624SToby Isaac   Vec               x, xglob;
41650c5b8624SToby Isaac   const PetscScalar *xarray;
4166e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
4167e759306cSMatthew G. Knepley 
4168e759306cSMatthew G. Knepley   PetscFunctionBegin;
4169ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRMPI(ierr);
4170e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
4171e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
41726113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
41730c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
41740c5b8624SToby Isaac   if (size > 1) {
41750c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
41760c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
41770c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
41780c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
41790c5b8624SToby Isaac   } else {
41800c5b8624SToby Isaac     xglob = x;
41810c5b8624SToby Isaac   }
41820c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
41830c5b8624SToby Isaac   if (size > 1) {
41840c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
41850c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
41860c5b8624SToby Isaac   }
4187e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
4188e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4189e759306cSMatthew G. Knepley }
4190e759306cSMatthew G. Knepley 
419188ed4aceSMatthew G Knepley /*@
41921bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4193061576a5SJed Brown 
4194061576a5SJed Brown   Input Parameter:
4195061576a5SJed Brown . dm - The DM
4196061576a5SJed Brown 
4197061576a5SJed Brown   Output Parameter:
4198061576a5SJed Brown . section - The PetscSection
4199061576a5SJed Brown 
4200061576a5SJed Brown   Options Database Keys:
4201061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4202061576a5SJed Brown 
4203061576a5SJed Brown   Level: advanced
4204061576a5SJed Brown 
4205061576a5SJed Brown   Notes:
4206061576a5SJed Brown   Use DMGetLocalSection() in new code.
4207061576a5SJed Brown 
4208061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4209061576a5SJed Brown 
4210061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
4211061576a5SJed Brown @*/
4212061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4213061576a5SJed Brown {
4214061576a5SJed Brown   PetscErrorCode ierr;
4215061576a5SJed Brown 
4216061576a5SJed Brown   PetscFunctionBegin;
4217061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
4218061576a5SJed Brown   PetscFunctionReturn(0);
4219061576a5SJed Brown }
4220061576a5SJed Brown 
4221061576a5SJed Brown /*@
4222061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
422388ed4aceSMatthew G Knepley 
422488ed4aceSMatthew G Knepley   Input Parameter:
422588ed4aceSMatthew G Knepley . dm - The DM
422688ed4aceSMatthew G Knepley 
422788ed4aceSMatthew G Knepley   Output Parameter:
422888ed4aceSMatthew G Knepley . section - The PetscSection
422988ed4aceSMatthew G Knepley 
4230e5893cccSMatthew G. Knepley   Options Database Keys:
4231e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4232e5893cccSMatthew G. Knepley 
423388ed4aceSMatthew G Knepley   Level: intermediate
423488ed4aceSMatthew G Knepley 
423588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
423688ed4aceSMatthew G Knepley 
4237061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
423888ed4aceSMatthew G Knepley @*/
4239061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
42400adebc6cSBarry Smith {
4241fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4242fd59a867SMatthew G. Knepley 
424388ed4aceSMatthew G Knepley   PetscFunctionBegin;
424488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
424588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42461bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4247e5e52638SMatthew G. Knepley     PetscInt d;
4248e5e52638SMatthew G. Knepley 
424945480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
425045480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject) dm;
425145480ffeSMatthew G. Knepley       PetscViewer       viewer;
425245480ffeSMatthew G. Knepley       PetscViewerFormat format;
425345480ffeSMatthew G. Knepley       PetscBool         flg;
425445480ffeSMatthew G. Knepley 
425545480ffeSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg);CHKERRQ(ierr);
425645480ffeSMatthew G. Knepley       if (flg) {ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);}
425745480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
425845480ffeSMatthew G. Knepley         ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);
425945480ffeSMatthew G. Knepley         if (flg) {ierr = PetscDSView(dm->probs[d].ds, viewer);CHKERRQ(ierr);}
426045480ffeSMatthew G. Knepley       }
426145480ffeSMatthew G. Knepley       if (flg) {
426245480ffeSMatthew G. Knepley         ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
426345480ffeSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
426445480ffeSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
426545480ffeSMatthew G. Knepley       }
426645480ffeSMatthew G. Knepley     }
42671bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
42681bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
42692f0f8703SMatthew G. Knepley   }
42701bb6d2a8SBarry Smith   *section = dm->localSection;
427188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
427288ed4aceSMatthew G Knepley }
427388ed4aceSMatthew G Knepley 
427488ed4aceSMatthew G Knepley /*@
42751bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4276061576a5SJed Brown 
4277061576a5SJed Brown   Input Parameters:
4278061576a5SJed Brown + dm - The DM
4279061576a5SJed Brown - section - The PetscSection
4280061576a5SJed Brown 
4281061576a5SJed Brown   Level: advanced
4282061576a5SJed Brown 
4283061576a5SJed Brown   Notes:
4284061576a5SJed Brown   Use DMSetLocalSection() in new code.
4285061576a5SJed Brown 
4286061576a5SJed Brown   Any existing Section will be destroyed
4287061576a5SJed Brown 
4288061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4289061576a5SJed Brown @*/
4290061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4291061576a5SJed Brown {
4292061576a5SJed Brown   PetscErrorCode ierr;
4293061576a5SJed Brown 
4294061576a5SJed Brown   PetscFunctionBegin;
4295061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4296061576a5SJed Brown   PetscFunctionReturn(0);
4297061576a5SJed Brown }
4298061576a5SJed Brown 
4299061576a5SJed Brown /*@
4300061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
430188ed4aceSMatthew G Knepley 
430288ed4aceSMatthew G Knepley   Input Parameters:
430388ed4aceSMatthew G Knepley + dm - The DM
430488ed4aceSMatthew G Knepley - section - The PetscSection
430588ed4aceSMatthew G Knepley 
430688ed4aceSMatthew G Knepley   Level: intermediate
430788ed4aceSMatthew G Knepley 
430888ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
430988ed4aceSMatthew G Knepley 
4310061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
431188ed4aceSMatthew G Knepley @*/
4312061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
43130adebc6cSBarry Smith {
4314c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4315af122d2aSMatthew G Knepley   PetscInt       f;
431688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
431788ed4aceSMatthew G Knepley 
431888ed4aceSMatthew G Knepley   PetscFunctionBegin;
431988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4320b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
43211d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
43221bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
43231bb6d2a8SBarry Smith   dm->localSection = section;
43241bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4325af122d2aSMatthew G Knepley   if (numFields) {
4326af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4327af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43280f21e855SMatthew G. Knepley       PetscObject disc;
4329af122d2aSMatthew G Knepley       const char *name;
4330af122d2aSMatthew G Knepley 
43311bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
433244a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
43330f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4334af122d2aSMatthew G Knepley     }
4335af122d2aSMatthew G Knepley   }
4336e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43371bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
433888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
433988ed4aceSMatthew G Knepley }
434088ed4aceSMatthew G Knepley 
43419435951eSToby Isaac /*@
4342b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
43439435951eSToby Isaac 
4344e228b242SToby Isaac   not collective
4345e228b242SToby Isaac 
43469435951eSToby Isaac   Input Parameter:
43479435951eSToby Isaac . dm - The DM
43489435951eSToby Isaac 
4349d8d19677SJose E. Roman   Output Parameters:
43509435951eSToby 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.
43519435951eSToby 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.
43529435951eSToby Isaac 
43539435951eSToby Isaac   Level: advanced
43549435951eSToby Isaac 
43559435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
43569435951eSToby Isaac 
43579435951eSToby Isaac .seealso: DMSetDefaultConstraints()
43589435951eSToby Isaac @*/
43599435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
43609435951eSToby Isaac {
43619435951eSToby Isaac   PetscErrorCode ierr;
43629435951eSToby Isaac 
43639435951eSToby Isaac   PetscFunctionBegin;
43649435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43659435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
436645a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
436745a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
43689435951eSToby Isaac   PetscFunctionReturn(0);
43699435951eSToby Isaac }
43709435951eSToby Isaac 
43719435951eSToby Isaac /*@
4372b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
43739435951eSToby Isaac 
43749435951eSToby 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().
43759435951eSToby Isaac 
43769435951eSToby 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.
43779435951eSToby Isaac 
4378e228b242SToby Isaac   collective on dm
4379e228b242SToby Isaac 
43809435951eSToby Isaac   Input Parameters:
43819435951eSToby Isaac + dm - The DM
4382e228b242SToby 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).
4383e228b242SToby 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).
43849435951eSToby Isaac 
43859435951eSToby Isaac   Level: advanced
43869435951eSToby Isaac 
43879435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
43889435951eSToby Isaac 
43899435951eSToby Isaac .seealso: DMGetDefaultConstraints()
43909435951eSToby Isaac @*/
43919435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
43929435951eSToby Isaac {
4393e228b242SToby Isaac   PetscMPIInt result;
43949435951eSToby Isaac   PetscErrorCode ierr;
43959435951eSToby Isaac 
43969435951eSToby Isaac   PetscFunctionBegin;
43979435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4398e228b242SToby Isaac   if (section) {
4399e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4400ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRMPI(ierr);
4401f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4402e228b242SToby Isaac   }
4403e228b242SToby Isaac   if (mat) {
4404e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4405ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRMPI(ierr);
4406f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4407e228b242SToby Isaac   }
44089435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
44099435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
44109435951eSToby Isaac   dm->defaultConstraintSection = section;
44119435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
44129435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
44139435951eSToby Isaac   dm->defaultConstraintMat = mat;
44149435951eSToby Isaac   PetscFunctionReturn(0);
44159435951eSToby Isaac }
44169435951eSToby Isaac 
4417497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4418507e4973SMatthew G. Knepley /*
4419507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4420507e4973SMatthew G. Knepley 
4421507e4973SMatthew G. Knepley   Input Parameters:
4422507e4973SMatthew G. Knepley + dm - The DM
4423507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4424507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4425507e4973SMatthew G. Knepley 
4426507e4973SMatthew G. Knepley   Level: intermediate
4427507e4973SMatthew G. Knepley 
44281bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4429507e4973SMatthew G. Knepley */
4430f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4431507e4973SMatthew G. Knepley {
4432507e4973SMatthew G. Knepley   MPI_Comm        comm;
4433507e4973SMatthew G. Knepley   PetscLayout     layout;
4434507e4973SMatthew G. Knepley   const PetscInt *ranges;
4435507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4436507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4437507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4438507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4439507e4973SMatthew G. Knepley 
4440507e4973SMatthew G. Knepley   PetscFunctionBegin;
4441507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4442507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4443ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
4444ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
4445507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4446507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4447507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4448507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4449507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4450507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4451507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4452507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4453f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4454507e4973SMatthew G. Knepley 
4455507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4456507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4457507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4458507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4459507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4460507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4461507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4462507e4973SMatthew 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;}
4463507e4973SMatthew 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;}
4464507e4973SMatthew G. Knepley     if (gdof < 0) {
4465507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4466507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4467507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4468507e4973SMatthew G. Knepley 
4469507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4470507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4471507e4973SMatthew 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;}
4472507e4973SMatthew G. Knepley       }
4473507e4973SMatthew G. Knepley     }
4474507e4973SMatthew G. Knepley   }
4475507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4476507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4477820f2d46SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRMPI(ierr);
4478507e4973SMatthew G. Knepley   if (!gvalid) {
4479507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4480507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4481507e4973SMatthew G. Knepley   }
4482507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4483507e4973SMatthew G. Knepley }
4484f741bcd2SMatthew G. Knepley #endif
4485507e4973SMatthew G. Knepley 
448688ed4aceSMatthew G Knepley /*@
4487e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
448888ed4aceSMatthew G Knepley 
4489d083f849SBarry Smith   Collective on dm
44908b1ab98fSJed Brown 
449188ed4aceSMatthew G Knepley   Input Parameter:
449288ed4aceSMatthew G Knepley . dm - The DM
449388ed4aceSMatthew G Knepley 
449488ed4aceSMatthew G Knepley   Output Parameter:
449588ed4aceSMatthew G Knepley . section - The PetscSection
449688ed4aceSMatthew G Knepley 
449788ed4aceSMatthew G Knepley   Level: intermediate
449888ed4aceSMatthew G Knepley 
449988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
450088ed4aceSMatthew G Knepley 
450192fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
450288ed4aceSMatthew G Knepley @*/
4503e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
45040adebc6cSBarry Smith {
450588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
450688ed4aceSMatthew G Knepley 
450788ed4aceSMatthew G Knepley   PetscFunctionBegin;
450888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
450988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
45101bb6d2a8SBarry Smith   if (!dm->globalSection) {
4511fd59a867SMatthew G. Knepley     PetscSection s;
4512fd59a867SMatthew G. Knepley 
451392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4514fd59a867SMatthew 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");
451533907cc2SStefano 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");
45161bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4517cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
45181bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
45191bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
452088ed4aceSMatthew G Knepley   }
45211bb6d2a8SBarry Smith   *section = dm->globalSection;
452288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
452388ed4aceSMatthew G Knepley }
452488ed4aceSMatthew G Knepley 
4525b21d0597SMatthew G Knepley /*@
4526e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4527b21d0597SMatthew G Knepley 
4528b21d0597SMatthew G Knepley   Input Parameters:
4529b21d0597SMatthew G Knepley + dm - The DM
45305080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4531b21d0597SMatthew G Knepley 
4532b21d0597SMatthew G Knepley   Level: intermediate
4533b21d0597SMatthew G Knepley 
4534b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4535b21d0597SMatthew G Knepley 
453692fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4537b21d0597SMatthew G Knepley @*/
4538e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
45390adebc6cSBarry Smith {
4540b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4541b21d0597SMatthew G Knepley 
4542b21d0597SMatthew G Knepley   PetscFunctionBegin;
4543b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45445080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
45451d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
45461bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
45471bb6d2a8SBarry Smith   dm->globalSection = section;
4548497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45491bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4550507e4973SMatthew G. Knepley #endif
4551b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4552b21d0597SMatthew G Knepley }
4553b21d0597SMatthew G Knepley 
455488ed4aceSMatthew G Knepley /*@
45551bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
455688ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
455788ed4aceSMatthew G Knepley 
455888ed4aceSMatthew G Knepley   Input Parameter:
455988ed4aceSMatthew G Knepley . dm - The DM
456088ed4aceSMatthew G Knepley 
456188ed4aceSMatthew G Knepley   Output Parameter:
456288ed4aceSMatthew G Knepley . sf - The PetscSF
456388ed4aceSMatthew G Knepley 
456488ed4aceSMatthew G Knepley   Level: intermediate
456588ed4aceSMatthew G Knepley 
456688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
456788ed4aceSMatthew G Knepley 
45681bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
456988ed4aceSMatthew G Knepley @*/
45701bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
45710adebc6cSBarry Smith {
457288ed4aceSMatthew G Knepley   PetscInt       nroots;
457388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
457488ed4aceSMatthew G Knepley 
457588ed4aceSMatthew G Knepley   PetscFunctionBegin;
457688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
457788ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
45781bb6d2a8SBarry Smith   if (!dm->sectionSF) {
45791bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
458033907cc2SStefano Zampini   }
45811bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
458288ed4aceSMatthew G Knepley   if (nroots < 0) {
458388ed4aceSMatthew G Knepley     PetscSection section, gSection;
458488ed4aceSMatthew G Knepley 
458592fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
458631ea6d37SMatthew G Knepley     if (section) {
4587e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
45881bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
458931ea6d37SMatthew G Knepley     } else {
45900298fd71SBarry Smith       *sf = NULL;
459131ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
459231ea6d37SMatthew G Knepley     }
459388ed4aceSMatthew G Knepley   }
45941bb6d2a8SBarry Smith   *sf = dm->sectionSF;
459588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
459688ed4aceSMatthew G Knepley }
459788ed4aceSMatthew G Knepley 
459888ed4aceSMatthew G Knepley /*@
45991bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
460088ed4aceSMatthew G Knepley 
460188ed4aceSMatthew G Knepley   Input Parameters:
460288ed4aceSMatthew G Knepley + dm - The DM
460388ed4aceSMatthew G Knepley - sf - The PetscSF
460488ed4aceSMatthew G Knepley 
460588ed4aceSMatthew G Knepley   Level: intermediate
460688ed4aceSMatthew G Knepley 
460788ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
460888ed4aceSMatthew G Knepley 
46091bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
461088ed4aceSMatthew G Knepley @*/
46111bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
46120adebc6cSBarry Smith {
461388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
461488ed4aceSMatthew G Knepley 
461588ed4aceSMatthew G Knepley   PetscFunctionBegin;
461688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4617b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
461833907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
46191bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
46201bb6d2a8SBarry Smith   dm->sectionSF = sf;
462188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
462288ed4aceSMatthew G Knepley }
462388ed4aceSMatthew G Knepley 
462488ed4aceSMatthew G Knepley /*@C
46251bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
462688ed4aceSMatthew G Knepley   describing the data layout.
462788ed4aceSMatthew G Knepley 
462888ed4aceSMatthew G Knepley   Input Parameters:
462988ed4aceSMatthew G Knepley + dm - The DM
463088ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
463188ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
463288ed4aceSMatthew G Knepley 
46331bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
463488ed4aceSMatthew G Knepley 
46351bb6d2a8SBarry Smith   Level: developer
46361bb6d2a8SBarry Smith 
46371bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
46381bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
46391bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
46401bb6d2a8SBarry Smith 
46411bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
464288ed4aceSMatthew G Knepley @*/
46431bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
464488ed4aceSMatthew G Knepley {
464588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
464688ed4aceSMatthew G Knepley 
464788ed4aceSMatthew G Knepley   PetscFunctionBegin;
464888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4649b0c7db22SLisandro Dalcin   ierr = PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection);CHKERRQ(ierr);
465088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
465188ed4aceSMatthew G Knepley }
4652af122d2aSMatthew G Knepley 
4653b21d0597SMatthew G Knepley /*@
4654b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4655b21d0597SMatthew G Knepley 
4656b21d0597SMatthew G Knepley   Input Parameter:
4657b21d0597SMatthew G Knepley . dm - The DM
4658b21d0597SMatthew G Knepley 
4659b21d0597SMatthew G Knepley   Output Parameter:
4660b21d0597SMatthew G Knepley . sf - The PetscSF
4661b21d0597SMatthew G Knepley 
4662b21d0597SMatthew G Knepley   Level: intermediate
4663b21d0597SMatthew G Knepley 
4664b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4665b21d0597SMatthew G Knepley 
46661bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4667b21d0597SMatthew G Knepley @*/
46680adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
46690adebc6cSBarry Smith {
4670b21d0597SMatthew G Knepley   PetscFunctionBegin;
4671b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4672b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4673b21d0597SMatthew G Knepley   *sf = dm->sf;
4674b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4675b21d0597SMatthew G Knepley }
4676b21d0597SMatthew G Knepley 
4677057b4bcdSMatthew G Knepley /*@
4678057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4679057b4bcdSMatthew G Knepley 
4680057b4bcdSMatthew G Knepley   Input Parameters:
4681057b4bcdSMatthew G Knepley + dm - The DM
4682057b4bcdSMatthew G Knepley - sf - The PetscSF
4683057b4bcdSMatthew G Knepley 
4684057b4bcdSMatthew G Knepley   Level: intermediate
4685057b4bcdSMatthew G Knepley 
46861bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4687057b4bcdSMatthew G Knepley @*/
46880adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
46890adebc6cSBarry Smith {
4690057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4691057b4bcdSMatthew G Knepley 
4692057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4693057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4694b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4695057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
469633907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4697057b4bcdSMatthew G Knepley   dm->sf = sf;
4698057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4699057b4bcdSMatthew G Knepley }
4700057b4bcdSMatthew G Knepley 
470134aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
470234aa8a36SMatthew G. Knepley {
470334aa8a36SMatthew G. Knepley   PetscClassId   id;
470434aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
470534aa8a36SMatthew G. Knepley 
470634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
470734aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
470834aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
470934aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
471034aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
471134aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
471217c1d62eSMatthew G. Knepley   } else {
471317c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
471434aa8a36SMatthew G. Knepley   }
471534aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
471634aa8a36SMatthew G. Knepley }
471734aa8a36SMatthew G. Knepley 
471844a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
471944a7f3ddSMatthew G. Knepley {
472044a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
472144a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
472244a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
472344a7f3ddSMatthew G. Knepley 
472444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
472544a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
472644a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
472744a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4728e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
472944a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
473044a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
473144a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
473244a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
473344a7f3ddSMatthew G. Knepley }
473444a7f3ddSMatthew G. Knepley 
473544a7f3ddSMatthew G. Knepley /*@
473644a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
473744a7f3ddSMatthew G. Knepley 
4738d083f849SBarry Smith   Logically collective on dm
473944a7f3ddSMatthew G. Knepley 
474044a7f3ddSMatthew G. Knepley   Input Parameter:
474144a7f3ddSMatthew G. Knepley . dm - The DM
474244a7f3ddSMatthew G. Knepley 
474344a7f3ddSMatthew G. Knepley   Level: intermediate
474444a7f3ddSMatthew G. Knepley 
474544a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
474644a7f3ddSMatthew G. Knepley @*/
474744a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
474844a7f3ddSMatthew G. Knepley {
474944a7f3ddSMatthew G. Knepley   PetscInt       f;
475044a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
475144a7f3ddSMatthew G. Knepley 
475244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
475344a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
475444a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
475544a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
475644a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
475744a7f3ddSMatthew G. Knepley   }
475844a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
475944a7f3ddSMatthew G. Knepley   dm->fields = NULL;
476044a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
476144a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
476244a7f3ddSMatthew G. Knepley }
476344a7f3ddSMatthew G. Knepley 
4764689b5837SMatthew G. Knepley /*@
4765689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4766689b5837SMatthew G. Knepley 
4767689b5837SMatthew G. Knepley   Not collective
4768689b5837SMatthew G. Knepley 
4769689b5837SMatthew G. Knepley   Input Parameter:
4770689b5837SMatthew G. Knepley . dm - The DM
4771689b5837SMatthew G. Knepley 
4772689b5837SMatthew G. Knepley   Output Parameter:
4773689b5837SMatthew G. Knepley . Nf - The number of fields
4774689b5837SMatthew G. Knepley 
4775689b5837SMatthew G. Knepley   Level: intermediate
4776689b5837SMatthew G. Knepley 
4777689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4778689b5837SMatthew G. Knepley @*/
47790f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
47800f21e855SMatthew G. Knepley {
47810f21e855SMatthew G. Knepley   PetscFunctionBegin;
47820f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4783534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
478444a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4785af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4786af122d2aSMatthew G Knepley }
4787af122d2aSMatthew G Knepley 
4788689b5837SMatthew G. Knepley /*@
4789689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4790689b5837SMatthew G. Knepley 
4791d083f849SBarry Smith   Logically collective on dm
4792689b5837SMatthew G. Knepley 
4793689b5837SMatthew G. Knepley   Input Parameters:
4794689b5837SMatthew G. Knepley + dm - The DM
4795689b5837SMatthew G. Knepley - Nf - The number of fields
4796689b5837SMatthew G. Knepley 
4797689b5837SMatthew G. Knepley   Level: intermediate
4798689b5837SMatthew G. Knepley 
4799689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4800689b5837SMatthew G. Knepley @*/
4801af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4802af122d2aSMatthew G Knepley {
48030f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4804af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4805af122d2aSMatthew G Knepley 
4806af122d2aSMatthew G Knepley   PetscFunctionBegin;
4807af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
480844a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
48090f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
48100f21e855SMatthew G. Knepley     PetscContainer obj;
48110f21e855SMatthew G. Knepley 
48120f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
481344a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
48140f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4815af122d2aSMatthew G Knepley   }
4816af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4817af122d2aSMatthew G Knepley }
4818af122d2aSMatthew G Knepley 
4819c1929be8SMatthew G. Knepley /*@
4820c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4821c1929be8SMatthew G. Knepley 
4822c1929be8SMatthew G. Knepley   Not collective
4823c1929be8SMatthew G. Knepley 
4824c1929be8SMatthew G. Knepley   Input Parameters:
4825c1929be8SMatthew G. Knepley + dm - The DM
4826c1929be8SMatthew G. Knepley - f  - The field number
4827c1929be8SMatthew G. Knepley 
482844a7f3ddSMatthew G. Knepley   Output Parameters:
482944a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
483044a7f3ddSMatthew G. Knepley - field - The discretization object
4831c1929be8SMatthew G. Knepley 
483244a7f3ddSMatthew G. Knepley   Level: intermediate
4833c1929be8SMatthew G. Knepley 
483444a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4835c1929be8SMatthew G. Knepley @*/
483644a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4837af122d2aSMatthew G Knepley {
4838af122d2aSMatthew G Knepley   PetscFunctionBegin;
4839af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4840064a246eSJacob Faibussowitsch   PetscValidPointer(field, 4);
484144a7f3ddSMatthew 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);
484244a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
484344a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4844decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4845decb47aaSMatthew G. Knepley }
4846decb47aaSMatthew G. Knepley 
4847083401c6SMatthew G. Knepley /* Does not clear the DS */
4848083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4849083401c6SMatthew G. Knepley {
4850083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4851083401c6SMatthew G. Knepley 
4852083401c6SMatthew G. Knepley   PetscFunctionBegin;
4853083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4854083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4855083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4856083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4857083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4858083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4859083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4860083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4861083401c6SMatthew G. Knepley }
4862083401c6SMatthew G. Knepley 
4863c1929be8SMatthew G. Knepley /*@
4864c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4865c1929be8SMatthew G. Knepley 
4866d083f849SBarry Smith   Logically collective on dm
4867c1929be8SMatthew G. Knepley 
4868c1929be8SMatthew G. Knepley   Input Parameters:
4869c1929be8SMatthew G. Knepley + dm    - The DM
4870c1929be8SMatthew G. Knepley . f     - The field number
487144a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4872c1929be8SMatthew G. Knepley - field - The discretization object
4873c1929be8SMatthew G. Knepley 
487444a7f3ddSMatthew G. Knepley   Level: intermediate
4875c1929be8SMatthew G. Knepley 
487644a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4877c1929be8SMatthew G. Knepley @*/
487844a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4879decb47aaSMatthew G. Knepley {
4880decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4881decb47aaSMatthew G. Knepley 
4882decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4883decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4884e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
488544a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4886e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4887083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
488834aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
48892df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
489044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
489144a7f3ddSMatthew G. Knepley }
489244a7f3ddSMatthew G. Knepley 
489344a7f3ddSMatthew G. Knepley /*@
489444a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
489544a7f3ddSMatthew G. Knepley 
4896d083f849SBarry Smith   Logically collective on dm
489744a7f3ddSMatthew G. Knepley 
489844a7f3ddSMatthew G. Knepley   Input Parameters:
489944a7f3ddSMatthew G. Knepley + dm    - The DM
490044a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
490144a7f3ddSMatthew G. Knepley - field - The discretization object
490244a7f3ddSMatthew G. Knepley 
490344a7f3ddSMatthew G. Knepley   Level: intermediate
490444a7f3ddSMatthew G. Knepley 
490544a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
490644a7f3ddSMatthew G. Knepley @*/
490744a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
490844a7f3ddSMatthew G. Knepley {
490944a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
491044a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
491144a7f3ddSMatthew G. Knepley 
491244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
491344a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4914064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
491544a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
491644a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
491744a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
491844a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
491944a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
492044a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
492134aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
49222df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4923af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4924af122d2aSMatthew G Knepley }
49256636e97aSMatthew G Knepley 
4926e5e52638SMatthew G. Knepley /*@
4927e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4928e0b68406SMatthew Knepley 
4929e0b68406SMatthew Knepley   Logically collective on dm
4930e0b68406SMatthew Knepley 
4931e0b68406SMatthew Knepley   Input Parameters:
4932e0b68406SMatthew Knepley + dm          - The DM
4933e0b68406SMatthew Knepley . f           - The field index
4934e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
4935e0b68406SMatthew Knepley 
4936e0b68406SMatthew Knepley   Level: intermediate
4937e0b68406SMatthew Knepley 
4938e0b68406SMatthew Knepley .seealso: DMGetFieldAvoidTensor(), DMSetField(), DMGetField()
4939e0b68406SMatthew Knepley @*/
4940e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
4941e0b68406SMatthew Knepley {
4942e0b68406SMatthew Knepley   PetscFunctionBegin;
4943e0b68406SMatthew 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);
4944e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
4945e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4946e0b68406SMatthew Knepley }
4947e0b68406SMatthew Knepley 
4948e0b68406SMatthew Knepley /*@
4949e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
4950e0b68406SMatthew Knepley 
4951e0b68406SMatthew Knepley   Logically collective on dm
4952e0b68406SMatthew Knepley 
4953e0b68406SMatthew Knepley   Input Parameters:
4954e0b68406SMatthew Knepley + dm          - The DM
4955e0b68406SMatthew Knepley - f           - The field index
4956e0b68406SMatthew Knepley 
4957e0b68406SMatthew Knepley   Output Parameter:
4958e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
4959e0b68406SMatthew Knepley 
4960e0b68406SMatthew Knepley   Level: intermediate
4961e0b68406SMatthew Knepley 
4962e0b68406SMatthew Knepley .seealso: DMSetFieldAvoidTensor(), DMSetField(), DMGetField()
4963e0b68406SMatthew Knepley @*/
4964e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
4965e0b68406SMatthew Knepley {
4966e0b68406SMatthew Knepley   PetscFunctionBegin;
4967e0b68406SMatthew 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);
4968e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
4969e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4970e0b68406SMatthew Knepley }
4971e0b68406SMatthew Knepley 
4972e0b68406SMatthew Knepley /*@
4973e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4974e5e52638SMatthew G. Knepley 
4975d083f849SBarry Smith   Collective on dm
4976e5e52638SMatthew G. Knepley 
4977e5e52638SMatthew G. Knepley   Input Parameter:
4978e5e52638SMatthew G. Knepley . dm - The DM
4979e5e52638SMatthew G. Knepley 
4980e5e52638SMatthew G. Knepley   Output Parameter:
4981e5e52638SMatthew G. Knepley . newdm - The DM
4982e5e52638SMatthew G. Knepley 
4983e5e52638SMatthew G. Knepley   Level: advanced
4984e5e52638SMatthew G. Knepley 
4985e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4986e5e52638SMatthew G. Knepley @*/
4987e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4988e5e52638SMatthew G. Knepley {
4989e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4990e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4991e5e52638SMatthew G. Knepley 
4992e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4993e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4994e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4995e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4996e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4997e5e52638SMatthew G. Knepley     DMLabel     label;
4998e5e52638SMatthew G. Knepley     PetscObject field;
499934aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5000e5e52638SMatthew G. Knepley 
5001e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
5002e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
500334aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
500434aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
500534aa8a36SMatthew G. Knepley   }
500634aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
500734aa8a36SMatthew G. Knepley }
500834aa8a36SMatthew G. Knepley 
500934aa8a36SMatthew G. Knepley /*@
501034aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
501134aa8a36SMatthew G. Knepley 
501234aa8a36SMatthew G. Knepley   Not collective
501334aa8a36SMatthew G. Knepley 
501434aa8a36SMatthew G. Knepley   Input Parameters:
501534aa8a36SMatthew G. Knepley + dm - The DM object
501634aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
501734aa8a36SMatthew G. Knepley 
5018d8d19677SJose E. Roman   Output Parameters:
501934aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
502034aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
502134aa8a36SMatthew G. Knepley 
502234aa8a36SMatthew G. Knepley   Notes:
502334aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
502434aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
502534aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5026979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
502734aa8a36SMatthew G. Knepley 
502834aa8a36SMatthew G. Knepley   Level: developer
502934aa8a36SMatthew G. Knepley 
503034aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
503134aa8a36SMatthew G. Knepley @*/
503234aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
503334aa8a36SMatthew G. Knepley {
503434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
503534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5036534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
5037534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
503834aa8a36SMatthew G. Knepley   if (f < 0) {
503934aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
504034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
504134aa8a36SMatthew G. Knepley   } else {
504234aa8a36SMatthew G. Knepley     PetscInt       Nf;
504334aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
504434aa8a36SMatthew G. Knepley 
504534aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
504634aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
504734aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
504834aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
504934aa8a36SMatthew G. Knepley   }
505034aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
505134aa8a36SMatthew G. Knepley }
505234aa8a36SMatthew G. Knepley 
505334aa8a36SMatthew G. Knepley /*@
505434aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
505534aa8a36SMatthew G. Knepley 
505634aa8a36SMatthew G. Knepley   Not collective
505734aa8a36SMatthew G. Knepley 
505834aa8a36SMatthew G. Knepley   Input Parameters:
505934aa8a36SMatthew G. Knepley + dm         - The DM object
506034aa8a36SMatthew G. Knepley . f          - The field number
506134aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
506234aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
506334aa8a36SMatthew G. Knepley 
506434aa8a36SMatthew G. Knepley   Notes:
506534aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
506634aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
506734aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5068979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
506934aa8a36SMatthew G. Knepley 
507034aa8a36SMatthew G. Knepley   Level: developer
507134aa8a36SMatthew G. Knepley 
507234aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
507334aa8a36SMatthew G. Knepley @*/
507434aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
507534aa8a36SMatthew G. Knepley {
507634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
507734aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
507834aa8a36SMatthew G. Knepley   if (f < 0) {
507934aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
508034aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
508134aa8a36SMatthew G. Knepley   } else {
508234aa8a36SMatthew G. Knepley     PetscInt       Nf;
508334aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
508434aa8a36SMatthew G. Knepley 
508534aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
508634aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
508734aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
508834aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5089e5e52638SMatthew G. Knepley   }
5090e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5091e5e52638SMatthew G. Knepley }
5092e5e52638SMatthew G. Knepley 
5093b0441da4SMatthew G. Knepley /*@
5094b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5095b0441da4SMatthew G. Knepley 
5096b0441da4SMatthew G. Knepley   Not collective
5097b0441da4SMatthew G. Knepley 
5098f899ff85SJose E. Roman   Input Parameter:
5099b0441da4SMatthew G. Knepley . dm - The DM object
5100b0441da4SMatthew G. Knepley 
5101d8d19677SJose E. Roman   Output Parameters:
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: DMSetBasicAdjacency(), DMGetField(), DMSetField()
5113b0441da4SMatthew G. Knepley @*/
5114b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(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);
5121064a246eSJacob Faibussowitsch   if (useCone)    PetscValidBoolPointer(useCone, 2);
5122064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
5123b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5124b0441da4SMatthew G. Knepley   if (!Nf) {
5125b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5126b0441da4SMatthew G. Knepley   } else {
5127b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5128b0441da4SMatthew G. Knepley   }
5129b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5130b0441da4SMatthew G. Knepley }
5131b0441da4SMatthew G. Knepley 
5132b0441da4SMatthew G. Knepley /*@
5133b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5134b0441da4SMatthew G. Knepley 
5135b0441da4SMatthew G. Knepley   Not collective
5136b0441da4SMatthew G. Knepley 
5137b0441da4SMatthew G. Knepley   Input Parameters:
5138b0441da4SMatthew G. Knepley + dm         - The DM object
5139b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5140b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5141b0441da4SMatthew G. Knepley 
5142b0441da4SMatthew G. Knepley   Notes:
5143b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5144b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5145b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5146b0441da4SMatthew G. Knepley 
5147b0441da4SMatthew G. Knepley   Level: developer
5148b0441da4SMatthew G. Knepley 
5149b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
5150b0441da4SMatthew G. Knepley @*/
5151b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5152b0441da4SMatthew G. Knepley {
5153b0441da4SMatthew G. Knepley   PetscInt       Nf;
5154b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5155b0441da4SMatthew G. Knepley 
5156b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5157b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5158b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5159b0441da4SMatthew G. Knepley   if (!Nf) {
5160b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5161b0441da4SMatthew G. Knepley   } else {
5162b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5163e5e52638SMatthew G. Knepley   }
5164e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5165e5e52638SMatthew G. Knepley }
5166e5e52638SMatthew G. Knepley 
5167783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
516845480ffeSMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, DMLabel label)
5169783e2ec8SMatthew G. Knepley {
5170783e2ec8SMatthew G. Knepley   PetscObject    obj;
5171783e2ec8SMatthew G. Knepley   PetscClassId   id;
5172783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
5173783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
5174783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
5175783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
5176783e2ec8SMatthew G. Knepley 
5177783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5178783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
5179783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5180783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
5181783e2ec8SMatthew G. Knepley   if (isFE && label) {
5182783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
5183783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5184783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
518545480ffeSMatthew G. Knepley       DMLabel l;
5186783e2ec8SMatthew G. Knepley 
518745480ffeSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, NULL, &l, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
518845480ffeSMatthew G. Knepley       duplicate = l == label ? PETSC_TRUE : PETSC_FALSE;
5189783e2ec8SMatthew G. Knepley       if (duplicate) break;
5190783e2ec8SMatthew G. Knepley     }
5191783e2ec8SMatthew G. Knepley     if (!duplicate) {
5192783e2ec8SMatthew G. Knepley       DM plex;
5193783e2ec8SMatthew G. Knepley 
5194783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
5195783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
5196783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
5197783e2ec8SMatthew G. Knepley     }
5198783e2ec8SMatthew G. Knepley   }
5199783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5200783e2ec8SMatthew G. Knepley }
5201783e2ec8SMatthew G. Knepley 
5202e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5203e5e52638SMatthew G. Knepley {
5204e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5205e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5206e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5207e5e52638SMatthew G. Knepley 
5208e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5209e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5210e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
5211e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5212b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5213e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5214e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5215e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5216e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5217e5e52638SMatthew G. Knepley }
5218e5e52638SMatthew G. Knepley 
5219e5e52638SMatthew G. Knepley /*@
5220e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5221e5e52638SMatthew G. Knepley 
5222e5e52638SMatthew G. Knepley   Not collective
5223e5e52638SMatthew G. Knepley 
5224e5e52638SMatthew G. Knepley   Input Parameter:
5225e5e52638SMatthew G. Knepley . dm - The DM
5226e5e52638SMatthew G. Knepley 
5227e5e52638SMatthew G. Knepley   Output Parameter:
5228e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5229e5e52638SMatthew G. Knepley 
5230e5e52638SMatthew G. Knepley   Level: intermediate
5231e5e52638SMatthew G. Knepley 
5232e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5233e5e52638SMatthew G. Knepley @*/
5234e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5235e5e52638SMatthew G. Knepley {
5236e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5237e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5238534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5239e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5240e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5241e5e52638SMatthew G. Knepley }
5242e5e52638SMatthew G. Knepley 
5243e5e52638SMatthew G. Knepley /*@
5244e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5245e5e52638SMatthew G. Knepley 
5246d083f849SBarry Smith   Logically collective on dm
5247e5e52638SMatthew G. Knepley 
5248e5e52638SMatthew G. Knepley   Input Parameter:
5249e5e52638SMatthew G. Knepley . dm - The DM
5250e5e52638SMatthew G. Knepley 
5251e5e52638SMatthew G. Knepley   Level: intermediate
5252e5e52638SMatthew G. Knepley 
5253e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5254e5e52638SMatthew G. Knepley @*/
5255e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5256e5e52638SMatthew G. Knepley {
5257e5e52638SMatthew G. Knepley   PetscInt       s;
5258e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5259e5e52638SMatthew G. Knepley 
5260e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5261e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5262e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5263e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5264e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5265b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5266e5e52638SMatthew G. Knepley   }
5267e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5268e5e52638SMatthew G. Knepley   dm->probs = NULL;
5269e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5270e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5271e5e52638SMatthew G. Knepley }
5272e5e52638SMatthew G. Knepley 
5273e5e52638SMatthew G. Knepley /*@
5274e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5275e5e52638SMatthew G. Knepley 
5276e5e52638SMatthew G. Knepley   Not collective
5277e5e52638SMatthew G. Knepley 
5278e5e52638SMatthew G. Knepley   Input Parameter:
5279e5e52638SMatthew G. Knepley . dm    - The DM
5280e5e52638SMatthew G. Knepley 
5281e5e52638SMatthew G. Knepley   Output Parameter:
5282e5e52638SMatthew G. Knepley . prob - The default PetscDS
5283e5e52638SMatthew G. Knepley 
5284e5e52638SMatthew G. Knepley   Level: intermediate
5285e5e52638SMatthew G. Knepley 
5286e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5287e5e52638SMatthew G. Knepley @*/
5288e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5289e5e52638SMatthew G. Knepley {
5290b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5291b0143b4dSMatthew G. Knepley 
5292e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5293e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5294e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5295b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5296b0143b4dSMatthew G. Knepley     PetscDS ds;
5297b0143b4dSMatthew G. Knepley 
529845480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5299b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5300b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5301b0143b4dSMatthew G. Knepley   }
5302b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5303e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5304e5e52638SMatthew G. Knepley }
5305e5e52638SMatthew G. Knepley 
5306e5e52638SMatthew G. Knepley /*@
5307e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5308e5e52638SMatthew G. Knepley 
5309e5e52638SMatthew G. Knepley   Not collective
5310e5e52638SMatthew G. Knepley 
5311e5e52638SMatthew G. Knepley   Input Parameters:
5312e5e52638SMatthew G. Knepley + dm    - The DM
5313e5e52638SMatthew G. Knepley - point - Cell for the DS
5314e5e52638SMatthew G. Knepley 
5315e5e52638SMatthew G. Knepley   Output Parameter:
5316e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5317e5e52638SMatthew G. Knepley 
5318e5e52638SMatthew G. Knepley   Level: developer
5319e5e52638SMatthew G. Knepley 
5320b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5321e5e52638SMatthew G. Knepley @*/
5322e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5323e5e52638SMatthew G. Knepley {
5324e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5325e5e52638SMatthew G. Knepley   PetscInt       s;
5326e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5327e5e52638SMatthew G. Knepley 
5328e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5329e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5330e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5331360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5332e5e52638SMatthew G. Knepley   *prob = NULL;
5333e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5334e5e52638SMatthew G. Knepley     PetscInt val;
5335e5e52638SMatthew G. Knepley 
5336e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5337e5e52638SMatthew G. Knepley     else {
5338e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5339e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5340e5e52638SMatthew G. Knepley     }
5341e5e52638SMatthew G. Knepley   }
5342e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5343e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5344e5e52638SMatthew G. Knepley }
5345e5e52638SMatthew G. Knepley 
5346e5e52638SMatthew G. Knepley /*@
5347e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5348e5e52638SMatthew G. Knepley 
5349e5e52638SMatthew G. Knepley   Not collective
5350e5e52638SMatthew G. Knepley 
5351e5e52638SMatthew G. Knepley   Input Parameters:
5352e5e52638SMatthew G. Knepley + dm    - The DM
5353e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5354e5e52638SMatthew G. Knepley 
5355b3cf3223SMatthew G. Knepley   Output Parameters:
5356b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5357b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5358e5e52638SMatthew G. Knepley 
5359e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5360e5e52638SMatthew G. Knepley 
5361e5e52638SMatthew G. Knepley   Level: advanced
5362e5e52638SMatthew G. Knepley 
5363e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5364e5e52638SMatthew G. Knepley @*/
5365b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5366e5e52638SMatthew G. Knepley {
5367e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5368e5e52638SMatthew G. Knepley 
5369e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5370e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5371e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5372b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5373b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5374e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5375b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5376b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5377b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5378b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5379b3cf3223SMatthew G. Knepley     }
5380e5e52638SMatthew G. Knepley   }
53812df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5382e5e52638SMatthew G. Knepley }
5383e5e52638SMatthew G. Knepley 
5384e5e52638SMatthew G. Knepley /*@
5385083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5386083401c6SMatthew G. Knepley 
5387083401c6SMatthew G. Knepley   Collective on dm
5388083401c6SMatthew G. Knepley 
5389083401c6SMatthew G. Knepley   Input Parameters:
5390083401c6SMatthew G. Knepley + dm     - The DM
5391083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5392083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5393083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5394083401c6SMatthew G. Knepley 
5395083401c6SMatthew 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,
5396083401c6SMatthew G. Knepley   the fields argument is ignored.
5397083401c6SMatthew G. Knepley 
5398083401c6SMatthew G. Knepley   Level: advanced
5399083401c6SMatthew G. Knepley 
5400083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5401083401c6SMatthew G. Knepley @*/
5402083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5403083401c6SMatthew G. Knepley {
5404083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5405083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5406083401c6SMatthew G. Knepley 
5407083401c6SMatthew G. Knepley   PetscFunctionBegin;
5408083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5409083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5410064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5411083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5412083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5413083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5414083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5415083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5416083401c6SMatthew G. Knepley     }
5417083401c6SMatthew G. Knepley   }
5418083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5419083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5420083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5421083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5422083401c6SMatthew G. Knepley   if (!label) {
5423083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5424083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5425083401c6SMatthew G. Knepley     Nds = 0;
5426083401c6SMatthew G. Knepley   }
5427083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5428083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5429083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5430083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5431083401c6SMatthew G. Knepley }
5432083401c6SMatthew G. Knepley 
5433083401c6SMatthew G. Knepley /*@
5434e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5435e5e52638SMatthew G. Knepley 
5436e5e52638SMatthew G. Knepley   Not collective
5437e5e52638SMatthew G. Knepley 
5438e5e52638SMatthew G. Knepley   Input Parameters:
5439e5e52638SMatthew G. Knepley + dm  - The DM
5440e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5441e5e52638SMatthew G. Knepley 
5442e5e52638SMatthew G. Knepley   Output Parameters:
5443b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5444b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5445083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5446e5e52638SMatthew G. Knepley 
5447e5e52638SMatthew G. Knepley   Level: advanced
5448e5e52638SMatthew G. Knepley 
5449e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5450e5e52638SMatthew G. Knepley @*/
5451b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5452e5e52638SMatthew G. Knepley {
5453e5e52638SMatthew G. Knepley   PetscInt       Nds;
5454e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5455e5e52638SMatthew G. Knepley 
5456e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5457e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5458e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5459e5e52638SMatthew 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);
5460e5e52638SMatthew G. Knepley   if (label) {
5461e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5462e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5463e5e52638SMatthew G. Knepley   }
5464b3cf3223SMatthew G. Knepley   if (fields) {
5465b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5466b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5467b3cf3223SMatthew G. Knepley   }
5468e5e52638SMatthew G. Knepley   if (ds) {
5469b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5470e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5471e5e52638SMatthew G. Knepley   }
5472e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5473e5e52638SMatthew G. Knepley }
5474e5e52638SMatthew G. Knepley 
5475e5e52638SMatthew G. Knepley /*@
5476083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5477e5e52638SMatthew G. Knepley 
5478083401c6SMatthew G. Knepley   Not collective
5479e5e52638SMatthew G. Knepley 
5480e5e52638SMatthew G. Knepley   Input Parameters:
5481e5e52638SMatthew G. Knepley + dm     - The DM
5482083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5483083401c6SMatthew G. Knepley . label  - The region label, or NULL
5484083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5485083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5486e5e52638SMatthew G. Knepley 
5487e5e52638SMatthew G. Knepley   Level: advanced
5488e5e52638SMatthew G. Knepley 
5489083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5490e5e52638SMatthew G. Knepley @*/
5491083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5492e5e52638SMatthew G. Knepley {
5493083401c6SMatthew G. Knepley   PetscInt       Nds;
5494e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5495e5e52638SMatthew G. Knepley 
5496e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5497e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5498083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5499083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5500083401c6SMatthew 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);
5501e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5502083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5503083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5504083401c6SMatthew G. Knepley   if (fields) {
5505083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5506b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5507083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5508083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5509e5e52638SMatthew G. Knepley   }
5510083401c6SMatthew G. Knepley   if (ds) {
5511083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5512083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5513083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5514083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5515083401c6SMatthew G. Knepley   }
5516e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5517e5e52638SMatthew G. Knepley }
5518e5e52638SMatthew G. Knepley 
5519e5e52638SMatthew G. Knepley /*@
55201d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
55211d3af9e0SMatthew G. Knepley 
55221d3af9e0SMatthew G. Knepley   Not collective
55231d3af9e0SMatthew G. Knepley 
55241d3af9e0SMatthew G. Knepley   Input Parameters:
55251d3af9e0SMatthew G. Knepley + dm  - The DM
55261d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
55271d3af9e0SMatthew G. Knepley 
55281d3af9e0SMatthew G. Knepley   Output Parameter:
55291d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
55301d3af9e0SMatthew G. Knepley 
55311d3af9e0SMatthew G. Knepley   Level: advanced
55321d3af9e0SMatthew G. Knepley 
55331d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
55341d3af9e0SMatthew G. Knepley @*/
55351d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
55361d3af9e0SMatthew G. Knepley {
55371d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
55381d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
55391d3af9e0SMatthew G. Knepley 
55401d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
55411d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55421d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
55431d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
55441d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
55451d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
55461d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
55471d3af9e0SMatthew G. Knepley   else          *num = n;
55481d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
55491d3af9e0SMatthew G. Knepley }
55501d3af9e0SMatthew G. Knepley 
55511d3af9e0SMatthew G. Knepley /*@
5552e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5553e5e52638SMatthew G. Knepley 
5554d083f849SBarry Smith   Collective on dm
5555e5e52638SMatthew G. Knepley 
5556e5e52638SMatthew G. Knepley   Input Parameter:
5557e5e52638SMatthew G. Knepley . dm - The DM
5558e5e52638SMatthew G. Knepley 
555945480ffeSMatthew G. Knepley   Options Database Keys:
556045480ffeSMatthew G. Knepley . -dm_petscds_view - View all the PetscDS objects in this DM
556145480ffeSMatthew G. Knepley 
5562e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5563e5e52638SMatthew G. Knepley 
5564e5e52638SMatthew G. Knepley   Level: intermediate
5565e5e52638SMatthew G. Knepley 
5566e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5567e5e52638SMatthew G. Knepley @*/
5568e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5569e5e52638SMatthew G. Knepley {
5570e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5571083401c6SMatthew G. Knepley   PetscDS        dsDef;
5572083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5573f9244615SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5574f9244615SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE, flg;
5575e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5576e5e52638SMatthew G. Knepley 
5577e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5578e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5579e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5580e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5581083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5582083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5583083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5584083401c6SMatthew G. Knepley   Nl   = 0;
5585083401c6SMatthew G. Knepley   Ndef = 0;
5586083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5587083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5588083401c6SMatthew G. Knepley     PetscInt l;
5589083401c6SMatthew G. Knepley 
5590f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5591f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5592f918ec44SMatthew G. Knepley     {
5593f918ec44SMatthew G. Knepley       PetscClassId id;
5594f918ec44SMatthew G. Knepley 
5595f918ec44SMatthew G. Knepley       ierr = PetscObjectGetClassId(dm->fields[f].disc, &id);CHKERRQ(ierr);
5596f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5597f918ec44SMatthew G. Knepley         Ceed ceed;
5598f918ec44SMatthew G. Knepley 
5599f918ec44SMatthew G. Knepley         ierr = DMGetCeed(dm, &ceed);CHKERRQ(ierr);
5600f918ec44SMatthew G. Knepley         ierr = PetscFESetCeed((PetscFE) dm->fields[f].disc, ceed);CHKERRQ(ierr);
5601f918ec44SMatthew G. Knepley       }
5602f918ec44SMatthew G. Knepley     }
5603f918ec44SMatthew G. Knepley #endif
5604083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5605083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5606083401c6SMatthew G. Knepley     if (l < Nl) continue;
5607083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5608083401c6SMatthew G. Knepley   }
5609083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5610083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5611083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5612b3cf3223SMatthew G. Knepley     IS        fields;
5613b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5614b3cf3223SMatthew G. Knepley 
5615b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5616083401c6SMatthew G. Knepley     if (nf) {
5617b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5618b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
561988f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
562088f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
562188f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
562288f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
562388f0c812SMatthew G. Knepley 
562445480ffeSMatthew G. Knepley       ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5625083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5626083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5627b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
56282df9ee95SMatthew G. Knepley     }
5629083401c6SMatthew G. Knepley   }
5630083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5631083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5632083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5633083401c6SMatthew G. Knepley   if (Ndef && Nl) {
56340122748bSMatthew G. Knepley     DM              plex;
5635083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5636083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5637083401c6SMatthew G. Knepley     PetscInt       *fields;
5638083401c6SMatthew G. Knepley     const PetscInt *cells;
5639083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
56400122748bSMatthew G. Knepley 
56410122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
56420122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5643083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5644083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5645083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5646083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5647083401c6SMatthew G. Knepley       IS      pointIS;
5648083401c6SMatthew G. Knepley 
5649083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5650083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5651083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5652083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5653083401c6SMatthew G. Knepley     }
5654083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5655083401c6SMatthew G. Knepley 
5656083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5657083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5658083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5659083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5660083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5661083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5662083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5663083401c6SMatthew G. Knepley 
5664083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5665083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5666083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5667083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5668083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5669083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5670083401c6SMatthew G. Knepley 
567145480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5672083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5673083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5674083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5675083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5676083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
56770122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5678083401c6SMatthew G. Knepley   }
5679083401c6SMatthew G. Knepley   /* Create label DSes
5680083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5681083401c6SMatthew G. Knepley   */
5682083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5683083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5684083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5685083401c6SMatthew G. Knepley     PetscDS   ds;
5686083401c6SMatthew G. Knepley     IS        fields;
5687083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5688083401c6SMatthew G. Knepley 
568945480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5690083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5691083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5692083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5693083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5694083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5695083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5696083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5697083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5698083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5699083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5700083401c6SMatthew G. Knepley     {
5701083401c6SMatthew G. Knepley       DMPolytopeType ct;
5702083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5703083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
57040122748bSMatthew G. Knepley 
5705e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5706665f567fSMatthew G. Knepley       if (lStart >= 0) {
5707412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5708412e9a14SMatthew G. Knepley         switch (ct) {
5709412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5710412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5711412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5712412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5713083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5714083401c6SMatthew G. Knepley           default: break;
5715412e9a14SMatthew G. Knepley         }
5716665f567fSMatthew G. Knepley       }
5717ffc4695bSBarry Smith       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRMPI(ierr);
5718083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5719e5e52638SMatthew G. Knepley     }
5720083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5721e5e52638SMatthew G. Knepley   }
5722083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5723e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5724083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5725083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5726083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5727083401c6SMatthew G. Knepley     const PetscInt *fld;
5728083401c6SMatthew G. Knepley     PetscInt        nf;
5729e5e52638SMatthew G. Knepley 
5730083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5731083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5732083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5733083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5734083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5735e5e52638SMatthew G. Knepley       PetscClassId id;
5736e5e52638SMatthew G. Knepley 
5737083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5738083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5739083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5740083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5741083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5742e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5743e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5744e5e52638SMatthew G. Knepley     }
5745083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5746e5e52638SMatthew G. Knepley   }
5747f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
5748f9244615SMatthew G. Knepley   ierr = PetscOptionsGetInt(NULL, ((PetscObject) dm)->prefix, "-dm_ds_jet_degree", &k, &flg);CHKERRQ(ierr);
5749f9244615SMatthew G. Knepley   if (flg) {
57503b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
57513b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
57523b4aee56SMatthew G. Knepley       PetscInt Nf, f;
57533b4aee56SMatthew G. Knepley 
57543b4aee56SMatthew G. Knepley       ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
57553b4aee56SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {ierr = PetscDSSetJetDegree(ds, f, k);CHKERRQ(ierr);}
57563b4aee56SMatthew G. Knepley     }
5757f9244615SMatthew G. Knepley   }
5758e5e52638SMatthew G. Knepley   /* Setup DSes */
5759e5e52638SMatthew G. Knepley   if (doSetup) {
5760e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5761e5e52638SMatthew G. Knepley   }
5762e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5763e5e52638SMatthew G. Knepley }
5764e5e52638SMatthew G. Knepley 
5765e5e52638SMatthew G. Knepley /*@
57667f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
57677f96f943SMatthew G. Knepley 
5768f2cacb80SMatthew G. Knepley   Collective on DM
5769f2cacb80SMatthew G. Knepley 
57707f96f943SMatthew G. Knepley   Input Parameters:
57717f96f943SMatthew G. Knepley + dm   - The DM
57727f96f943SMatthew G. Knepley - time - The time
57737f96f943SMatthew G. Knepley 
57747f96f943SMatthew G. Knepley   Output Parameters:
5775f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5776f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
57777f96f943SMatthew G. Knepley 
57787f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
57797f96f943SMatthew G. Knepley 
57807f96f943SMatthew G. Knepley   Level: developer
57817f96f943SMatthew G. Knepley 
57827f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
57837f96f943SMatthew G. Knepley @*/
5784f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
57857f96f943SMatthew G. Knepley {
57867f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
57877f96f943SMatthew G. Knepley   void            **ectxs;
57887f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
57897f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
57907f96f943SMatthew G. Knepley 
57917f96f943SMatthew G. Knepley   PetscFunctionBegin;
5792f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5793f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5794f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
57957f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
57967f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
57977f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
57987f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
57997f96f943SMatthew G. Knepley     PetscDS         ds;
58007f96f943SMatthew G. Knepley     DMLabel         label;
58017f96f943SMatthew G. Knepley     IS              fieldIS;
58027f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
58037f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
58047f96f943SMatthew G. Knepley 
58057f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
58067f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
58077f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
58087f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
58097f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5810f2cacb80SMatthew G. Knepley     if (u) {
58117f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
58127f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
58137f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
58147f96f943SMatthew G. Knepley       }
58157f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
58167f96f943SMatthew G. Knepley       if (label) {
58177f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
58187f96f943SMatthew G. Knepley       } else {
58197f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
58207f96f943SMatthew G. Knepley       }
58217f96f943SMatthew G. Knepley     }
5822f2cacb80SMatthew G. Knepley     if (u_t) {
5823f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5824f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5825f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5826f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5827f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5828f2cacb80SMatthew G. Knepley       }
5829f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5830f2cacb80SMatthew G. Knepley       if (label) {
5831f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5832f2cacb80SMatthew G. Knepley       } else {
5833f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5834f2cacb80SMatthew G. Knepley       }
5835f2cacb80SMatthew G. Knepley     }
5836f2cacb80SMatthew G. Knepley   }
5837f2cacb80SMatthew G. Knepley   if (u) {
58387f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
58397f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5840f2cacb80SMatthew G. Knepley   }
5841f2cacb80SMatthew G. Knepley   if (u_t) {
5842f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
5843f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
5844f2cacb80SMatthew G. Knepley   }
58457f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
58467f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
58477f96f943SMatthew G. Knepley }
58487f96f943SMatthew G. Knepley 
584945480ffeSMatthew G. Knepley PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
585045480ffeSMatthew G. Knepley {
585145480ffeSMatthew G. Knepley   PetscDS        dsNew;
585245480ffeSMatthew G. Knepley   DSBoundary     b;
585345480ffeSMatthew G. Knepley   PetscInt       cdim, Nf, f;
5854df911668SMatthew G. Knepley   PetscBool      isHybrid;
585545480ffeSMatthew G. Knepley   void          *ctx;
585645480ffeSMatthew G. Knepley   PetscErrorCode ierr;
585745480ffeSMatthew G. Knepley 
585845480ffeSMatthew G. Knepley   PetscFunctionBegin;
585945480ffeSMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) ds), &dsNew);CHKERRQ(ierr);
586045480ffeSMatthew G. Knepley   ierr = PetscDSCopyConstants(ds, dsNew);CHKERRQ(ierr);
586145480ffeSMatthew G. Knepley   ierr = PetscDSCopyExactSolutions(ds, dsNew);CHKERRQ(ierr);
586245480ffeSMatthew G. Knepley   ierr = PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
586345480ffeSMatthew G. Knepley   ierr = PetscDSCopyEquations(ds, dsNew);CHKERRQ(ierr);
586445480ffeSMatthew G. Knepley   ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
586545480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
586645480ffeSMatthew G. Knepley     ierr = PetscDSGetContext(ds, f, &ctx);CHKERRQ(ierr);
586745480ffeSMatthew G. Knepley     ierr = PetscDSSetContext(dsNew, f, ctx);CHKERRQ(ierr);
586845480ffeSMatthew G. Knepley   }
586945480ffeSMatthew G. Knepley   if (Nf) {
587045480ffeSMatthew G. Knepley     ierr = PetscDSGetCoordinateDimension(ds, &cdim);CHKERRQ(ierr);
587145480ffeSMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsNew, cdim);CHKERRQ(ierr);
587245480ffeSMatthew G. Knepley   }
587345480ffeSMatthew G. Knepley   ierr = PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
587445480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
587545480ffeSMatthew G. Knepley     ierr = DMGetLabel(dm, b->lname, &b->label);CHKERRQ(ierr);
587645480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
587745480ffeSMatthew G. Knepley     //if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
587845480ffeSMatthew G. Knepley   }
5879df911668SMatthew G. Knepley   ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5880df911668SMatthew G. Knepley   ierr = PetscDSSetHybrid(dsNew, isHybrid);CHKERRQ(ierr);
588145480ffeSMatthew G. Knepley 
588245480ffeSMatthew G. Knepley   ierr = DMSetRegionDS(dm, label, fields, dsNew);CHKERRQ(ierr);
588345480ffeSMatthew G. Knepley   ierr = PetscDSDestroy(&dsNew);CHKERRQ(ierr);
588445480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
588545480ffeSMatthew G. Knepley }
588645480ffeSMatthew G. Knepley 
58877f96f943SMatthew G. Knepley /*@
5888e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5889e5e52638SMatthew G. Knepley 
5890d083f849SBarry Smith   Collective on dm
5891e5e52638SMatthew G. Knepley 
5892e5e52638SMatthew G. Knepley   Input Parameter:
5893e5e52638SMatthew G. Knepley . dm - The DM
5894e5e52638SMatthew G. Knepley 
5895e5e52638SMatthew G. Knepley   Output Parameter:
5896e5e52638SMatthew G. Knepley . newdm - The DM
5897e5e52638SMatthew G. Knepley 
5898e5e52638SMatthew G. Knepley   Level: advanced
5899e5e52638SMatthew G. Knepley 
5900e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5901e5e52638SMatthew G. Knepley @*/
5902e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5903e5e52638SMatthew G. Knepley {
5904e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5905e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5906e5e52638SMatthew G. Knepley 
5907e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5908e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5909e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5910e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5911e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5912e5e52638SMatthew G. Knepley     DMLabel  label;
5913b3cf3223SMatthew G. Knepley     IS       fields;
591445480ffeSMatthew G. Knepley     PetscDS  ds, newds;
5915783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5916e5e52638SMatthew G. Knepley 
5917b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5918b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
591945480ffeSMatthew G. Knepley     ierr = DMTransferDS_Internal(newdm, label, fields, ds);CHKERRQ(ierr);
592045480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
592145480ffeSMatthew G. Knepley     ierr = DMGetRegionDS(newdm, label, NULL, &newds);CHKERRQ(ierr);
592245480ffeSMatthew G. Knepley     ierr = PetscDSGetNumBoundary(newds, &Nbd);CHKERRQ(ierr);
5923783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5924b8025e53SMatthew G. Knepley       PetscWeakForm wf;
592545480ffeSMatthew G. Knepley       DMLabel       label;
5926783e2ec8SMatthew G. Knepley       PetscInt      field;
5927783e2ec8SMatthew G. Knepley 
5928b8025e53SMatthew G. Knepley       ierr = PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
592945480ffeSMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, newds, field, bd, label);CHKERRQ(ierr);
5930b8025e53SMatthew G. Knepley       ierr = PetscWeakFormReplaceLabel(wf, label);CHKERRQ(ierr);
5931783e2ec8SMatthew G. Knepley     }
5932e5e52638SMatthew G. Knepley   }
5933e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5934e5e52638SMatthew G. Knepley }
5935e5e52638SMatthew G. Knepley 
5936e5e52638SMatthew G. Knepley /*@
5937e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5938e5e52638SMatthew G. Knepley 
5939d083f849SBarry Smith   Collective on dm
5940e5e52638SMatthew G. Knepley 
5941e5e52638SMatthew G. Knepley   Input Parameter:
5942e5e52638SMatthew G. Knepley . dm - The DM
5943e5e52638SMatthew G. Knepley 
5944e5e52638SMatthew G. Knepley   Output Parameter:
5945e5e52638SMatthew G. Knepley . newdm - The DM
5946e5e52638SMatthew G. Knepley 
5947e5e52638SMatthew G. Knepley   Level: advanced
5948e5e52638SMatthew G. Knepley 
5949e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5950e5e52638SMatthew G. Knepley @*/
5951e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5952e5e52638SMatthew G. Knepley {
5953e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5954e5e52638SMatthew G. Knepley 
5955e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5956e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5957e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5958e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5959e5e52638SMatthew G. Knepley }
5960e5e52638SMatthew G. Knepley 
5961b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5962b64e0483SPeter Brune {
5963b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5964b64e0483SPeter Brune   PetscErrorCode ierr;
5965b64e0483SPeter Brune   Vec coords,ccoords;
59666dbf9973SLawrence Mitchell   Mat inject;
5967b64e0483SPeter Brune   PetscFunctionBegin;
5968b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5969b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5970b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5971b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5972b64e0483SPeter Brune   if (coords && !ccoords) {
5973b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
59746668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
59756dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
59762adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
59776dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5978b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5979b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5980b64e0483SPeter Brune   }
5981b64e0483SPeter Brune   PetscFunctionReturn(0);
5982b64e0483SPeter Brune }
5983b64e0483SPeter Brune 
598403dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
598503dadc2fSPeter Brune {
598603dadc2fSPeter Brune   DM dm_coord,subdm_coord;
598703dadc2fSPeter Brune   PetscErrorCode ierr;
598803dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
598903dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
599003dadc2fSPeter Brune   PetscFunctionBegin;
599103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
599203dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
599303dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
599403dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
599503dadc2fSPeter Brune   if (coords && !ccoords) {
599603dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
59976668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
599803dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
599924640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
600003dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
600103dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
600203dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
60031ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
600403dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
600503dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
600603dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
600703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
600803dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
600903dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
601003dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
601103dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
601203dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
601303dadc2fSPeter Brune   }
601403dadc2fSPeter Brune   PetscFunctionReturn(0);
601503dadc2fSPeter Brune }
601603dadc2fSPeter Brune 
6017c73cfb54SMatthew G. Knepley /*@
6018c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
6019c73cfb54SMatthew G. Knepley 
6020c73cfb54SMatthew G. Knepley   Not collective
6021c73cfb54SMatthew G. Knepley 
6022c73cfb54SMatthew G. Knepley   Input Parameter:
6023c73cfb54SMatthew G. Knepley . dm - The DM
6024c73cfb54SMatthew G. Knepley 
6025c73cfb54SMatthew G. Knepley   Output Parameter:
6026c73cfb54SMatthew G. Knepley . dim - The topological dimension
6027c73cfb54SMatthew G. Knepley 
6028c73cfb54SMatthew G. Knepley   Level: beginner
6029c73cfb54SMatthew G. Knepley 
6030c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
6031c73cfb54SMatthew G. Knepley @*/
6032c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6033c73cfb54SMatthew G. Knepley {
6034c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6035c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6036534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6037c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6038c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6039c73cfb54SMatthew G. Knepley }
6040c73cfb54SMatthew G. Knepley 
6041c73cfb54SMatthew G. Knepley /*@
6042c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
6043c73cfb54SMatthew G. Knepley 
6044c73cfb54SMatthew G. Knepley   Collective on dm
6045c73cfb54SMatthew G. Knepley 
6046c73cfb54SMatthew G. Knepley   Input Parameters:
6047c73cfb54SMatthew G. Knepley + dm - The DM
6048c73cfb54SMatthew G. Knepley - dim - The topological dimension
6049c73cfb54SMatthew G. Knepley 
6050c73cfb54SMatthew G. Knepley   Level: beginner
6051c73cfb54SMatthew G. Knepley 
6052c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
6053c73cfb54SMatthew G. Knepley @*/
6054c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6055c73cfb54SMatthew G. Knepley {
6056e5e52638SMatthew G. Knepley   PetscDS        ds;
605745480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6058f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6059f17e8794SMatthew G. Knepley 
6060c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6061c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6062c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6063c73cfb54SMatthew G. Knepley   dm->dim = dim;
606445480ffeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
606545480ffeSMatthew G. Knepley   for (n = 0; n < Nds; ++n) {
606645480ffeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
606745480ffeSMatthew G. Knepley     if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);}
606845480ffeSMatthew G. Knepley   }
6069c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6070c73cfb54SMatthew G. Knepley }
6071c73cfb54SMatthew G. Knepley 
6072793f3fe5SMatthew G. Knepley /*@
6073793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6074793f3fe5SMatthew G. Knepley 
6075d083f849SBarry Smith   Collective on dm
6076793f3fe5SMatthew G. Knepley 
6077793f3fe5SMatthew G. Knepley   Input Parameters:
6078793f3fe5SMatthew G. Knepley + dm - the DM
6079793f3fe5SMatthew G. Knepley - dim - the dimension
6080793f3fe5SMatthew G. Knepley 
6081793f3fe5SMatthew G. Knepley   Output Parameters:
6082793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6083aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6084793f3fe5SMatthew G. Knepley 
6085793f3fe5SMatthew G. Knepley   Note:
6086793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6087a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6088793f3fe5SMatthew G. Knepley   then the interval is empty.
6089793f3fe5SMatthew G. Knepley 
6090793f3fe5SMatthew G. Knepley   Level: intermediate
6091793f3fe5SMatthew G. Knepley 
6092793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
6093793f3fe5SMatthew G. Knepley @*/
6094793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6095793f3fe5SMatthew G. Knepley {
6096793f3fe5SMatthew G. Knepley   PetscInt       d;
6097793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
6098793f3fe5SMatthew G. Knepley 
6099793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6100793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6101793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
6102793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
6103b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
6104793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
6105793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6106793f3fe5SMatthew G. Knepley }
6107793f3fe5SMatthew G. Knepley 
61086636e97aSMatthew G Knepley /*@
61096636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
61106636e97aSMatthew G Knepley 
6111d083f849SBarry Smith   Collective on dm
61126636e97aSMatthew G Knepley 
61136636e97aSMatthew G Knepley   Input Parameters:
61146636e97aSMatthew G Knepley + dm - the DM
61156636e97aSMatthew G Knepley - c - coordinate vector
61166636e97aSMatthew G Knepley 
61172dd40e9bSPatrick Sanan   Notes:
61182dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
61192dd40e9bSPatrick Sanan 
61202dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
61216636e97aSMatthew G Knepley 
61226636e97aSMatthew G Knepley   Level: intermediate
61236636e97aSMatthew G Knepley 
612460c22052SBarry Smith .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
61256636e97aSMatthew G Knepley @*/
61266636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
61276636e97aSMatthew G Knepley {
61286636e97aSMatthew G Knepley   PetscErrorCode ierr;
61296636e97aSMatthew G Knepley 
61306636e97aSMatthew G Knepley   PetscFunctionBegin;
61316636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61326636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
61336636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
61346636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
61356636e97aSMatthew G Knepley   dm->coordinates = c;
61366636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
6137b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
613803dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
61396636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61406636e97aSMatthew G Knepley }
61416636e97aSMatthew G Knepley 
61426636e97aSMatthew G Knepley /*@
61436636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
61446636e97aSMatthew G Knepley 
61457058e716SVaclav Hapla   Not collective
61466636e97aSMatthew G Knepley 
61476636e97aSMatthew G Knepley    Input Parameters:
61486636e97aSMatthew G Knepley +  dm - the DM
61496636e97aSMatthew G Knepley -  c - coordinate vector
61506636e97aSMatthew G Knepley 
61512dd40e9bSPatrick Sanan   Notes:
61526636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
61536636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
61546636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
61556636e97aSMatthew G Knepley 
61562dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
61572dd40e9bSPatrick Sanan 
61586636e97aSMatthew G Knepley   Level: intermediate
61596636e97aSMatthew G Knepley 
61606636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
61616636e97aSMatthew G Knepley @*/
61626636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
61636636e97aSMatthew G Knepley {
61646636e97aSMatthew G Knepley   PetscErrorCode ierr;
61656636e97aSMatthew G Knepley 
61666636e97aSMatthew G Knepley   PetscFunctionBegin;
61676636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61686636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
61696636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
61706636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
61718865f1eaSKarl Rupp 
61726636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
61738865f1eaSKarl Rupp 
61746636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
61756636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61766636e97aSMatthew G Knepley }
61776636e97aSMatthew G Knepley 
61786636e97aSMatthew G Knepley /*@
61796636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
61806636e97aSMatthew G Knepley 
6181d083f849SBarry Smith   Collective on dm
61826636e97aSMatthew G Knepley 
61836636e97aSMatthew G Knepley   Input Parameter:
61846636e97aSMatthew G Knepley . dm - the DM
61856636e97aSMatthew G Knepley 
61866636e97aSMatthew G Knepley   Output Parameter:
61876636e97aSMatthew G Knepley . c - global coordinate vector
61886636e97aSMatthew G Knepley 
61896636e97aSMatthew G Knepley   Note:
619060c22052SBarry Smith   This is a borrowed reference, so the user should NOT destroy this vector. When the DM is
619160c22052SBarry Smith   destroyed the array will no longer be valid.
61926636e97aSMatthew G Knepley 
6193959c7569SPatrick Sanan   Each process has only the locally-owned portion of the global coordinates (does NOT have the ghost coordinates).
61946636e97aSMatthew G Knepley 
61956636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
61966636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
61976636e97aSMatthew G Knepley 
61986636e97aSMatthew G Knepley   Level: intermediate
61996636e97aSMatthew G Knepley 
620060c22052SBarry Smith .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
62016636e97aSMatthew G Knepley @*/
62026636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
62036636e97aSMatthew G Knepley {
62046636e97aSMatthew G Knepley   PetscErrorCode ierr;
62056636e97aSMatthew G Knepley 
62066636e97aSMatthew G Knepley   PetscFunctionBegin;
62076636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62086636e97aSMatthew G Knepley   PetscValidPointer(c,2);
62091f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
62100298fd71SBarry Smith     DM        cdm = NULL;
62111970a576SMatthew G. Knepley     PetscBool localized;
62126636e97aSMatthew G Knepley 
62136636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
62146636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
62151970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
62161970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
62171970a576SMatthew G. Knepley     if (localized) {
62181970a576SMatthew G. Knepley       PetscInt cdim;
62191970a576SMatthew G. Knepley 
62201970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
62211970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
62221970a576SMatthew G. Knepley     }
62236636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
62246636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
62256636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
62266636e97aSMatthew G Knepley   }
62276636e97aSMatthew G Knepley   *c = dm->coordinates;
62286636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62296636e97aSMatthew G Knepley }
62306636e97aSMatthew G Knepley 
62316636e97aSMatthew G Knepley /*@
623281e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
623381e9a530SVaclav Hapla 
6234d083f849SBarry Smith   Collective on dm
623581e9a530SVaclav Hapla 
623681e9a530SVaclav Hapla   Input Parameter:
623781e9a530SVaclav Hapla . dm - the DM
623881e9a530SVaclav Hapla 
623981e9a530SVaclav Hapla   Level: advanced
624081e9a530SVaclav Hapla 
624181e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
624281e9a530SVaclav Hapla @*/
624381e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
624481e9a530SVaclav Hapla {
624581e9a530SVaclav Hapla   PetscErrorCode ierr;
624681e9a530SVaclav Hapla 
624781e9a530SVaclav Hapla   PetscFunctionBegin;
624881e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
624981e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
62501970a576SMatthew G. Knepley     DM        cdm = NULL;
62511970a576SMatthew G. Knepley     PetscBool localized;
62521970a576SMatthew G. Knepley 
625381e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
625481e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
62551970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
62561970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
62571970a576SMatthew G. Knepley     if (localized) {
62581970a576SMatthew G. Knepley       PetscInt cdim;
62591970a576SMatthew G. Knepley 
62601970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
62611970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
62621970a576SMatthew G. Knepley     }
626381e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
626481e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
626581e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
626681e9a530SVaclav Hapla   }
626781e9a530SVaclav Hapla   PetscFunctionReturn(0);
626881e9a530SVaclav Hapla }
626981e9a530SVaclav Hapla 
627081e9a530SVaclav Hapla /*@
62716636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
62726636e97aSMatthew G Knepley 
6273d083f849SBarry Smith   Collective on dm
62746636e97aSMatthew G Knepley 
62756636e97aSMatthew G Knepley   Input Parameter:
62766636e97aSMatthew G Knepley . dm - the DM
62776636e97aSMatthew G Knepley 
62786636e97aSMatthew G Knepley   Output Parameter:
62796636e97aSMatthew G Knepley . c - coordinate vector
62806636e97aSMatthew G Knepley 
62816636e97aSMatthew G Knepley   Note:
62826636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
62836636e97aSMatthew G Knepley 
62846636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
62856636e97aSMatthew G Knepley 
62866636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
62876636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
62886636e97aSMatthew G Knepley 
62896636e97aSMatthew G Knepley   Level: intermediate
62906636e97aSMatthew G Knepley 
629181e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
62926636e97aSMatthew G Knepley @*/
62936636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
62946636e97aSMatthew G Knepley {
62956636e97aSMatthew G Knepley   PetscErrorCode ierr;
62966636e97aSMatthew G Knepley 
62976636e97aSMatthew G Knepley   PetscFunctionBegin;
62986636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62996636e97aSMatthew G Knepley   PetscValidPointer(c,2);
630081e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
63016636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
63026636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63036636e97aSMatthew G Knepley }
63046636e97aSMatthew G Knepley 
630581e9a530SVaclav Hapla /*@
630681e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
630781e9a530SVaclav Hapla 
630881e9a530SVaclav Hapla   Not collective
630981e9a530SVaclav Hapla 
631081e9a530SVaclav Hapla   Input Parameter:
631181e9a530SVaclav Hapla . dm - the DM
631281e9a530SVaclav Hapla 
631381e9a530SVaclav Hapla   Output Parameter:
631481e9a530SVaclav Hapla . c - coordinate vector
631581e9a530SVaclav Hapla 
631681e9a530SVaclav Hapla   Level: advanced
631781e9a530SVaclav Hapla 
631881e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
631981e9a530SVaclav Hapla @*/
632081e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
632181e9a530SVaclav Hapla {
632281e9a530SVaclav Hapla   PetscFunctionBegin;
632381e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
632481e9a530SVaclav Hapla   PetscValidPointer(c,2);
632581e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
63266636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
63276636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63286636e97aSMatthew G Knepley }
63296636e97aSMatthew G Knepley 
63302db98f8dSVaclav Hapla /*@
63312db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
63322db98f8dSVaclav Hapla 
63332db98f8dSVaclav Hapla   Not collective
63342db98f8dSVaclav Hapla 
6335d8d19677SJose E. Roman   Input Parameters:
63362db98f8dSVaclav Hapla + dm - the DM
63372db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
63382db98f8dSVaclav Hapla 
6339d8d19677SJose E. Roman   Output Parameters:
63402db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
63412db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
63422db98f8dSVaclav Hapla 
63432db98f8dSVaclav Hapla   Note:
63442db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
63452db98f8dSVaclav Hapla 
63462db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
63472db98f8dSVaclav Hapla 
63482db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
63492db98f8dSVaclav Hapla 
63502db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
63512db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
63522db98f8dSVaclav Hapla 
63532db98f8dSVaclav Hapla   Level: advanced
63542db98f8dSVaclav Hapla 
63552db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
63562db98f8dSVaclav Hapla @*/
63572db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
63582db98f8dSVaclav Hapla {
63592db98f8dSVaclav Hapla   PetscSection        cs, newcs;
63602db98f8dSVaclav Hapla   Vec                 coords;
63612db98f8dSVaclav Hapla   const PetscScalar   *arr;
63622db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
63632db98f8dSVaclav Hapla   PetscInt            n;
63642db98f8dSVaclav Hapla   PetscErrorCode      ierr;
63652db98f8dSVaclav Hapla 
63662db98f8dSVaclav Hapla   PetscFunctionBegin;
63672db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63682db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
63692db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
63702db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
63712db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
63721bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
63731bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
63742db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
63752db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
63762db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
63772db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
63782db98f8dSVaclav Hapla   if (pCoord) {
63792db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
63802db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
63812db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
63822db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6383ad9ac99dSVaclav Hapla   } else {
6384ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
63852db98f8dSVaclav Hapla   }
6386ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6387ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
63882db98f8dSVaclav Hapla   PetscFunctionReturn(0);
63892db98f8dSVaclav Hapla }
63902db98f8dSVaclav Hapla 
6391f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6392f19dbd58SToby Isaac {
6393f19dbd58SToby Isaac   PetscErrorCode ierr;
6394f19dbd58SToby Isaac 
6395f19dbd58SToby Isaac   PetscFunctionBegin;
6396f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6397f19dbd58SToby Isaac   PetscValidPointer(field,2);
6398f19dbd58SToby Isaac   if (!dm->coordinateField) {
6399f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6400f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6401f19dbd58SToby Isaac     }
6402f19dbd58SToby Isaac   }
6403f19dbd58SToby Isaac   *field = dm->coordinateField;
6404f19dbd58SToby Isaac   PetscFunctionReturn(0);
6405f19dbd58SToby Isaac }
6406f19dbd58SToby Isaac 
6407f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6408f19dbd58SToby Isaac {
6409f19dbd58SToby Isaac   PetscErrorCode ierr;
6410f19dbd58SToby Isaac 
6411f19dbd58SToby Isaac   PetscFunctionBegin;
6412f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6413f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6414c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6415f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6416f19dbd58SToby Isaac   dm->coordinateField = field;
6417f19dbd58SToby Isaac   PetscFunctionReturn(0);
6418f19dbd58SToby Isaac }
6419f19dbd58SToby Isaac 
64206636e97aSMatthew G Knepley /*@
64211cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
64226636e97aSMatthew G Knepley 
6423d083f849SBarry Smith   Collective on dm
64246636e97aSMatthew G Knepley 
64256636e97aSMatthew G Knepley   Input Parameter:
64266636e97aSMatthew G Knepley . dm - the DM
64276636e97aSMatthew G Knepley 
64286636e97aSMatthew G Knepley   Output Parameter:
64296636e97aSMatthew G Knepley . cdm - coordinate DM
64306636e97aSMatthew G Knepley 
64316636e97aSMatthew G Knepley   Level: intermediate
64326636e97aSMatthew G Knepley 
64331cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
64346636e97aSMatthew G Knepley @*/
64356636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
64366636e97aSMatthew G Knepley {
64376636e97aSMatthew G Knepley   PetscErrorCode ierr;
64386636e97aSMatthew G Knepley 
64396636e97aSMatthew G Knepley   PetscFunctionBegin;
64406636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64416636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
64426636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6443308f8a94SToby Isaac     DM cdm;
6444308f8a94SToby Isaac 
644582f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6446308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6447308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6448308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6449308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6450308f8a94SToby Isaac     dm->coordinateDM = cdm;
64516636e97aSMatthew G Knepley   }
64526636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
64536636e97aSMatthew G Knepley   PetscFunctionReturn(0);
64546636e97aSMatthew G Knepley }
6455e87bb0d3SMatthew G Knepley 
64561cfe2091SMatthew G. Knepley /*@
64571cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
64581cfe2091SMatthew G. Knepley 
6459d083f849SBarry Smith   Logically Collective on dm
64601cfe2091SMatthew G. Knepley 
64611cfe2091SMatthew G. Knepley   Input Parameters:
64621cfe2091SMatthew G. Knepley + dm - the DM
64631cfe2091SMatthew G. Knepley - cdm - coordinate DM
64641cfe2091SMatthew G. Knepley 
64651cfe2091SMatthew G. Knepley   Level: intermediate
64661cfe2091SMatthew G. Knepley 
64671cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
64681cfe2091SMatthew G. Knepley @*/
64691cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
64701cfe2091SMatthew G. Knepley {
64711cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
64721cfe2091SMatthew G. Knepley 
64731cfe2091SMatthew G. Knepley   PetscFunctionBegin;
64741cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64751cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6476f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
64771cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
64781cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
64791cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
64801cfe2091SMatthew G. Knepley }
64811cfe2091SMatthew G. Knepley 
648246e270d4SMatthew G. Knepley /*@
648346e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
648446e270d4SMatthew G. Knepley 
648546e270d4SMatthew G. Knepley   Not Collective
648646e270d4SMatthew G. Knepley 
648746e270d4SMatthew G. Knepley   Input Parameter:
648846e270d4SMatthew G. Knepley . dm - The DM object
648946e270d4SMatthew G. Knepley 
649046e270d4SMatthew G. Knepley   Output Parameter:
649146e270d4SMatthew G. Knepley . dim - The embedding dimension
649246e270d4SMatthew G. Knepley 
649346e270d4SMatthew G. Knepley   Level: intermediate
649446e270d4SMatthew G. Knepley 
649592fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
649646e270d4SMatthew G. Knepley @*/
649746e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
649846e270d4SMatthew G. Knepley {
649946e270d4SMatthew G. Knepley   PetscFunctionBegin;
650046e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6501534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
65029a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
65039a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
65049a9a41abSToby Isaac   }
650546e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
650646e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
650746e270d4SMatthew G. Knepley }
650846e270d4SMatthew G. Knepley 
650946e270d4SMatthew G. Knepley /*@
651046e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
651146e270d4SMatthew G. Knepley 
651246e270d4SMatthew G. Knepley   Not Collective
651346e270d4SMatthew G. Knepley 
651446e270d4SMatthew G. Knepley   Input Parameters:
651546e270d4SMatthew G. Knepley + dm  - The DM object
651646e270d4SMatthew G. Knepley - dim - The embedding dimension
651746e270d4SMatthew G. Knepley 
651846e270d4SMatthew G. Knepley   Level: intermediate
651946e270d4SMatthew G. Knepley 
652092fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
652146e270d4SMatthew G. Knepley @*/
652246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
652346e270d4SMatthew G. Knepley {
6524e5e52638SMatthew G. Knepley   PetscDS        ds;
652545480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6526f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6527f17e8794SMatthew G. Knepley 
652846e270d4SMatthew G. Knepley   PetscFunctionBegin;
652946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
653046e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
653145480ffeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
653245480ffeSMatthew G. Knepley   for (n = 0; n < Nds; ++n) {
653345480ffeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
6534e5e52638SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
653545480ffeSMatthew G. Knepley   }
653646e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
653746e270d4SMatthew G. Knepley }
653846e270d4SMatthew G. Knepley 
6539e8abe2deSMatthew G. Knepley /*@
6540e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6541e8abe2deSMatthew G. Knepley 
6542d083f849SBarry Smith   Collective on dm
6543e8abe2deSMatthew G. Knepley 
6544e8abe2deSMatthew G. Knepley   Input Parameter:
6545e8abe2deSMatthew G. Knepley . dm - The DM object
6546e8abe2deSMatthew G. Knepley 
6547e8abe2deSMatthew G. Knepley   Output Parameter:
6548e8abe2deSMatthew G. Knepley . section - The PetscSection object
6549e8abe2deSMatthew G. Knepley 
6550e8abe2deSMatthew G. Knepley   Level: intermediate
6551e8abe2deSMatthew G. Knepley 
655292fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6553e8abe2deSMatthew G. Knepley @*/
6554e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6555e8abe2deSMatthew G. Knepley {
6556e8abe2deSMatthew G. Knepley   DM             cdm;
6557e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6558e8abe2deSMatthew G. Knepley 
6559e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6560e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6561e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6562e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
656392fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6564e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6565e8abe2deSMatthew G. Knepley }
6566e8abe2deSMatthew G. Knepley 
6567e8abe2deSMatthew G. Knepley /*@
6568e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6569e8abe2deSMatthew G. Knepley 
6570e8abe2deSMatthew G. Knepley   Not Collective
6571e8abe2deSMatthew G. Knepley 
6572e8abe2deSMatthew G. Knepley   Input Parameters:
6573e8abe2deSMatthew G. Knepley + dm      - The DM object
657446e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6575e8abe2deSMatthew G. Knepley - section - The PetscSection object
6576e8abe2deSMatthew G. Knepley 
6577e8abe2deSMatthew G. Knepley   Level: intermediate
6578e8abe2deSMatthew G. Knepley 
657992fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6580e8abe2deSMatthew G. Knepley @*/
658146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6582e8abe2deSMatthew G. Knepley {
6583e8abe2deSMatthew G. Knepley   DM             cdm;
6584e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6585e8abe2deSMatthew G. Knepley 
6586e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6587e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
658846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6589e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
659092fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
659146e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
65924c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
659346e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
659446e270d4SMatthew G. Knepley 
659546e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
659646e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
659746e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
659846e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
659946e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
660046e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
660146e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
660246e270d4SMatthew G. Knepley     }
6603ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
660446e270d4SMatthew G. Knepley   }
6605e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6606e8abe2deSMatthew G. Knepley }
6607e8abe2deSMatthew G. Knepley 
6608d864a3eaSLisandro Dalcin /*@
6609d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6610d864a3eaSLisandro Dalcin 
6611d864a3eaSLisandro Dalcin   Input Parameters:
6612d864a3eaSLisandro Dalcin + dm      - The DM object
6613d864a3eaSLisandro Dalcin - disc    - The new coordinate discretization
6614d864a3eaSLisandro Dalcin 
6615d864a3eaSLisandro Dalcin   Level: intermediate
6616d864a3eaSLisandro Dalcin 
6617d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6618d864a3eaSLisandro Dalcin @*/
6619d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6620d864a3eaSLisandro Dalcin {
6621d864a3eaSLisandro Dalcin   PetscObject    discOld;
6622d864a3eaSLisandro Dalcin   PetscClassId   classid;
6623d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6624d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6625d864a3eaSLisandro Dalcin   Mat            matInterp;
6626d864a3eaSLisandro Dalcin   PetscErrorCode ierr;
6627d864a3eaSLisandro Dalcin 
6628d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6629d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6630d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6631d864a3eaSLisandro Dalcin 
6632d864a3eaSLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdmOld);CHKERRQ(ierr);
6633d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
6634d864a3eaSLisandro Dalcin   ierr = DMGetField(cdmOld, 0, NULL, &discOld);CHKERRQ(ierr);
6635d864a3eaSLisandro Dalcin   ierr = PetscObjectGetClassId(discOld, &classid);CHKERRQ(ierr);
663629ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
663729ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
663829ad44c5SMatthew G. Knepley       PetscFE        feLinear;
663929ad44c5SMatthew G. Knepley       DMPolytopeType ct;
664029ad44c5SMatthew G. Knepley       PetscInt       dim, dE, cStart;
664129ad44c5SMatthew G. Knepley       PetscBool      simplex;
664229ad44c5SMatthew G. Knepley 
664329ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
664429ad44c5SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
664529ad44c5SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
664629ad44c5SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdmOld, 0, &cStart, NULL);CHKERRQ(ierr);
664729ad44c5SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
664829ad44c5SMatthew G. Knepley       switch (ct) {
664929ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM:
665029ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
665129ad44c5SMatthew G. Knepley           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
665229ad44c5SMatthew G. Knepley         default: break;
665329ad44c5SMatthew G. Knepley       }
665429ad44c5SMatthew G. Knepley       simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
665529ad44c5SMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear);CHKERRQ(ierr);
665629ad44c5SMatthew G. Knepley       ierr = DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear);CHKERRQ(ierr);
665729ad44c5SMatthew G. Knepley       ierr = PetscFEDestroy(&feLinear);CHKERRQ(ierr);
665829ad44c5SMatthew G. Knepley       ierr = DMCreateDS(cdmOld);CHKERRQ(ierr);
665929ad44c5SMatthew G. Knepley     } else {
666029ad44c5SMatthew G. Knepley       const char *discname;
666129ad44c5SMatthew G. Knepley 
666229ad44c5SMatthew G. Knepley       ierr = PetscObjectGetType(discOld, &discname);CHKERRQ(ierr);
666329ad44c5SMatthew G. Knepley       SETERRQ1(PetscObjectComm(discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
666429ad44c5SMatthew G. Knepley     }
666529ad44c5SMatthew G. Knepley   }
6666d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6667d864a3eaSLisandro Dalcin   ierr = DMClone(cdmOld, &cdmNew);CHKERRQ(ierr);
6668d864a3eaSLisandro Dalcin   ierr = DMSetField(cdmNew, 0, NULL, (PetscObject) disc);CHKERRQ(ierr);
6669d864a3eaSLisandro Dalcin   ierr = DMCreateDS(cdmNew);CHKERRQ(ierr);
6670d864a3eaSLisandro Dalcin   /* Project the coordinate vector from old to new space  */
6671d864a3eaSLisandro Dalcin   ierr = DMGetCoordinates(dm, &coordsOld);CHKERRQ(ierr);
6672d864a3eaSLisandro Dalcin   ierr = DMCreateGlobalVector(cdmNew, &coordsNew);CHKERRQ(ierr);
6673d864a3eaSLisandro Dalcin   ierr = DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL);CHKERRQ(ierr);
6674d864a3eaSLisandro Dalcin   ierr = MatInterpolate(matInterp, coordsOld, coordsNew);CHKERRQ(ierr);
6675d864a3eaSLisandro Dalcin   ierr = MatDestroy(&matInterp);CHKERRQ(ierr);
6676d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6677d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateField(dm, NULL);CHKERRQ(ierr);
6678d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateDM(dm, cdmNew);CHKERRQ(ierr);
6679d864a3eaSLisandro Dalcin   ierr = DMSetCoordinates(dm, coordsNew);CHKERRQ(ierr);
6680d864a3eaSLisandro Dalcin   ierr = VecDestroy(&coordsNew);CHKERRQ(ierr);
6681d864a3eaSLisandro Dalcin   ierr = DMDestroy(&cdmNew);CHKERRQ(ierr);
6682d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6683d864a3eaSLisandro Dalcin }
6684d864a3eaSLisandro Dalcin 
66855dc8c3f7SMatthew G. Knepley /*@C
668690b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
66875dc8c3f7SMatthew G. Knepley 
6688f899ff85SJose E. Roman   Input Parameter:
668990b157c4SStefano Zampini . dm      - The DM object
669090b157c4SStefano Zampini 
669190b157c4SStefano Zampini   Output Parameters:
669290b157c4SStefano Zampini + per     - Whether the DM is periodic or not
66935dc8c3f7SMatthew 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
66945dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
66955dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
66965dc8c3f7SMatthew G. Knepley 
66975dc8c3f7SMatthew G. Knepley   Level: developer
66985dc8c3f7SMatthew G. Knepley 
66995dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
67005dc8c3f7SMatthew G. Knepley @*/
670190b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6702c6b900c6SMatthew G. Knepley {
6703c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6704c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
670590b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6706c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6707c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
67085dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6709c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6710c6b900c6SMatthew G. Knepley }
6711c6b900c6SMatthew G. Knepley 
67125dc8c3f7SMatthew G. Knepley /*@C
67135dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
67145dc8c3f7SMatthew G. Knepley 
67155dc8c3f7SMatthew G. Knepley   Input Parameters:
67165dc8c3f7SMatthew G. Knepley + dm      - The DM object
6717db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6718db2bf62eSStefano 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.
67195dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
67205dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
67215dc8c3f7SMatthew G. Knepley 
6722db2bf62eSStefano 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.
6723db2bf62eSStefano Zampini 
67245dc8c3f7SMatthew G. Knepley   Level: developer
67255dc8c3f7SMatthew G. Knepley 
67265dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
67275dc8c3f7SMatthew G. Knepley @*/
672890b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6729c6b900c6SMatthew G. Knepley {
6730c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6731c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6732c6b900c6SMatthew G. Knepley 
6733c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6734c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
673590b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6736412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6737412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6738412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
67395dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
674090b157c4SStefano Zampini   if (maxCell) {
6741412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6742412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6743db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6744db2bf62eSStefano Zampini     ierr = PetscFree(dm->maxCell);CHKERRQ(ierr);
6745412e9a14SMatthew G. Knepley   }
6746db2bf62eSStefano Zampini 
6747412e9a14SMatthew G. Knepley   if (L) {
6748412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6749412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6750412e9a14SMatthew G. Knepley   }
6751412e9a14SMatthew G. Knepley   if (bd) {
6752412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6753412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
675490b157c4SStefano Zampini   }
6755072d7d67SStefano Zampini   dm->periodic = per;
6756c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6757c6b900c6SMatthew G. Knepley }
6758c6b900c6SMatthew G. Knepley 
67592e17dfb7SMatthew G. Knepley /*@
67602e17dfb7SMatthew 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.
67612e17dfb7SMatthew G. Knepley 
67622e17dfb7SMatthew G. Knepley   Input Parameters:
67632e17dfb7SMatthew G. Knepley + dm     - The DM
676465da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
676565da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
67662e17dfb7SMatthew G. Knepley 
67672e17dfb7SMatthew G. Knepley   Output Parameter:
67682e17dfb7SMatthew G. Knepley . out - The localized coordinate point
67692e17dfb7SMatthew G. Knepley 
67702e17dfb7SMatthew G. Knepley   Level: developer
67712e17dfb7SMatthew G. Knepley 
67722e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
67732e17dfb7SMatthew G. Knepley @*/
677465da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
67752e17dfb7SMatthew G. Knepley {
67762e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
67772e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
67782e17dfb7SMatthew G. Knepley 
67792e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
67802e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
67812e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
67822e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
67832e17dfb7SMatthew G. Knepley   } else {
678465da65dcSMatthew G. Knepley     if (endpoint) {
678565da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6786da3333bfSMatthew 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)) {
6787da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
678865da65dcSMatthew G. Knepley         } else {
6789da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
679065da65dcSMatthew G. Knepley         }
679165da65dcSMatthew G. Knepley       }
679265da65dcSMatthew G. Knepley     } else {
67932e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
67941118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
67952e17dfb7SMatthew G. Knepley       }
67962e17dfb7SMatthew G. Knepley     }
679765da65dcSMatthew G. Knepley   }
67982e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
67992e17dfb7SMatthew G. Knepley }
68002e17dfb7SMatthew G. Knepley 
68012e17dfb7SMatthew G. Knepley /*
68022e17dfb7SMatthew 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.
68032e17dfb7SMatthew G. Knepley 
68042e17dfb7SMatthew G. Knepley   Input Parameters:
68052e17dfb7SMatthew G. Knepley + dm     - The DM
68062e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
68072e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
68082e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
68092e17dfb7SMatthew G. Knepley 
68102e17dfb7SMatthew G. Knepley   Output Parameter:
68112e17dfb7SMatthew G. Knepley . out - The localized coordinate point
68122e17dfb7SMatthew G. Knepley 
68132e17dfb7SMatthew G. Knepley   Level: developer
68142e17dfb7SMatthew G. Knepley 
68152e17dfb7SMatthew 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
68162e17dfb7SMatthew G. Knepley 
68172e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
68182e17dfb7SMatthew G. Knepley */
68192e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
68202e17dfb7SMatthew G. Knepley {
68212e17dfb7SMatthew G. Knepley   PetscInt d;
68222e17dfb7SMatthew G. Knepley 
68232e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68242e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68252e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68262e17dfb7SMatthew G. Knepley   } else {
68272e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6828908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
68292e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
68302e17dfb7SMatthew G. Knepley       } else {
68312e17dfb7SMatthew G. Knepley         out[d] = in[d];
68322e17dfb7SMatthew G. Knepley       }
68332e17dfb7SMatthew G. Knepley     }
68342e17dfb7SMatthew G. Knepley   }
68352e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68362e17dfb7SMatthew G. Knepley }
6837a5801f52SStefano Zampini 
68382e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
68392e17dfb7SMatthew G. Knepley {
68402e17dfb7SMatthew G. Knepley   PetscInt d;
68412e17dfb7SMatthew G. Knepley 
68422e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68432e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68442e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68452e17dfb7SMatthew G. Knepley   } else {
68462e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6847908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
68482e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
68492e17dfb7SMatthew G. Knepley       } else {
68502e17dfb7SMatthew G. Knepley         out[d] = in[d];
68512e17dfb7SMatthew G. Knepley       }
68522e17dfb7SMatthew G. Knepley     }
68532e17dfb7SMatthew G. Knepley   }
68542e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68552e17dfb7SMatthew G. Knepley }
68562e17dfb7SMatthew G. Knepley 
68572e17dfb7SMatthew G. Knepley /*
68582e17dfb7SMatthew 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.
68592e17dfb7SMatthew G. Knepley 
68602e17dfb7SMatthew G. Knepley   Input Parameters:
68612e17dfb7SMatthew G. Knepley + dm     - The DM
68622e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
68632e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
68642e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
68652e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
68662e17dfb7SMatthew G. Knepley 
68672e17dfb7SMatthew G. Knepley   Output Parameter:
68682e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
68692e17dfb7SMatthew G. Knepley 
68702e17dfb7SMatthew G. Knepley   Level: developer
68712e17dfb7SMatthew G. Knepley 
68722e17dfb7SMatthew 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
68732e17dfb7SMatthew G. Knepley 
68742e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
68752e17dfb7SMatthew G. Knepley */
68762e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
68772e17dfb7SMatthew G. Knepley {
68782e17dfb7SMatthew G. Knepley   PetscInt d;
68792e17dfb7SMatthew G. Knepley 
68802e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68812e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68822e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
68832e17dfb7SMatthew G. Knepley   } else {
68842e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6885412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6886412e9a14SMatthew G. Knepley 
6887412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6888412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6889412e9a14SMatthew G. Knepley 
6890412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6891412e9a14SMatthew 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]));
6892412e9a14SMatthew G. Knepley         out[d] += newCoord;
68932e17dfb7SMatthew G. Knepley       } else {
68942e17dfb7SMatthew G. Knepley         out[d] += in[d];
68952e17dfb7SMatthew G. Knepley       }
68962e17dfb7SMatthew G. Knepley     }
68972e17dfb7SMatthew G. Knepley   }
68982e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68992e17dfb7SMatthew G. Knepley }
69002e17dfb7SMatthew G. Knepley 
690136447a5eSToby Isaac /*@
69028f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
69038f700142SStefano Zampini 
69048f700142SStefano Zampini   Not collective
690536447a5eSToby Isaac 
690636447a5eSToby Isaac   Input Parameter:
690736447a5eSToby Isaac . dm - The DM
690836447a5eSToby Isaac 
690936447a5eSToby Isaac   Output Parameter:
691036447a5eSToby Isaac   areLocalized - True if localized
691136447a5eSToby Isaac 
691236447a5eSToby Isaac   Level: developer
691336447a5eSToby Isaac 
69148f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
691536447a5eSToby Isaac @*/
69168f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
691736447a5eSToby Isaac {
691836447a5eSToby Isaac   DM             cdm;
691936447a5eSToby Isaac   PetscSection   coordSection;
69202ccac677SMatthew G. Knepley   PetscInt       depth, cStart, cEnd, sStart, sEnd, c, dof;
692146a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
692236447a5eSToby Isaac   PetscErrorCode ierr;
692336447a5eSToby Isaac 
692436447a5eSToby Isaac   PetscFunctionBegin;
692536447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6926534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
69278b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
692846a3a80fSLisandro Dalcin 
692936447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
693036447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
693146a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
69329f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
69332ccac677SMatthew G. Knepley   ierr = DMPlexGetDepth(cdm, &depth);CHKERRQ(ierr);
69342ccac677SMatthew G. Knepley   if (!depth) PetscFunctionReturn(0);
693546a3a80fSLisandro Dalcin 
69369f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
693746a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
693836447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
693936447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
694046a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
694146a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
694236447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
694346a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
694436447a5eSToby Isaac   }
69458f700142SStefano Zampini   *areLocalized = alreadyLocalized;
694636447a5eSToby Isaac   PetscFunctionReturn(0);
694736447a5eSToby Isaac }
694836447a5eSToby Isaac 
69498f700142SStefano Zampini /*@
69508f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
69518f700142SStefano Zampini 
69528f700142SStefano Zampini   Collective on dm
69538f700142SStefano Zampini 
69548f700142SStefano Zampini   Input Parameter:
69558f700142SStefano Zampini . dm - The DM
69568f700142SStefano Zampini 
69578f700142SStefano Zampini   Output Parameter:
69588f700142SStefano Zampini   areLocalized - True if localized
69598f700142SStefano Zampini 
69608f700142SStefano Zampini   Level: developer
69618f700142SStefano Zampini 
69628f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
69638f700142SStefano Zampini @*/
69648f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
69658f700142SStefano Zampini {
69668f700142SStefano Zampini   PetscBool      localized;
69678f700142SStefano Zampini   PetscErrorCode ierr;
69688f700142SStefano Zampini 
69698f700142SStefano Zampini   PetscFunctionBegin;
69708f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6971534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
69728f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
6973820f2d46SBarry Smith   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
69748f700142SStefano Zampini   PetscFunctionReturn(0);
69758f700142SStefano Zampini }
697636447a5eSToby Isaac 
69772e17dfb7SMatthew G. Knepley /*@
6978492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
69792e17dfb7SMatthew G. Knepley 
69808f700142SStefano Zampini   Collective on dm
69818f700142SStefano Zampini 
69822e17dfb7SMatthew G. Knepley   Input Parameter:
69832e17dfb7SMatthew G. Knepley . dm - The DM
69842e17dfb7SMatthew G. Knepley 
69852e17dfb7SMatthew G. Knepley   Level: developer
69862e17dfb7SMatthew G. Knepley 
69878f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
69882e17dfb7SMatthew G. Knepley @*/
69892e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
69902e17dfb7SMatthew G. Knepley {
69912e17dfb7SMatthew G. Knepley   DM             cdm;
69922e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
69932e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
69943e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
69953e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6996e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
69973e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
69983e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
69992e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
70002e17dfb7SMatthew G. Knepley 
70012e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
70022e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
700392c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
7004f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
7005f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
7006f7cbd40bSStefano Zampini 
70072e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
70082e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
70092e17dfb7SMatthew G. Knepley   {
70102e17dfb7SMatthew G. Knepley     PetscBool isplex;
70112e17dfb7SMatthew G. Knepley 
70122e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
70132e17dfb7SMatthew G. Knepley     if (isplex) {
70142e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
70153e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
701669291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
70173e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
70183e922f36SToby Isaac       newStart = vStart;
70193e922f36SToby Isaac       newEnd   = vEnd;
70203e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
70213e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
70223e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
70233e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
70243e922f36SToby Isaac       }
70252e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
70262e17dfb7SMatthew G. Knepley   }
70272e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
702843eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
70292e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
70303e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
7031e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
70323e922f36SToby Isaac 
70332e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
70342e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
70352e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
70362e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
70373e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
70383e922f36SToby Isaac 
703969291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
70403e922f36SToby Isaac   localized = &anchor[bs];
70413e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
70423e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
70433e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
70443e922f36SToby Isaac 
70453e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
70463e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
70473e922f36SToby Isaac       PetscInt     b;
70483e922f36SToby Isaac 
70493e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
70503e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
70513e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
70523e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
70533e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
70543e922f36SToby Isaac         for (b = 0; b < bs; b++) {
70553e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
70563e922f36SToby Isaac         }
70573e922f36SToby Isaac         if (b < bs) break;
70583e922f36SToby Isaac       }
70593e922f36SToby Isaac       if (d < dof/bs) {
70603e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
70613e922f36SToby Isaac           PetscInt cdof;
70623e922f36SToby Isaac 
70633e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
70643e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
70653e922f36SToby Isaac         }
70663e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
70673e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
70683e922f36SToby Isaac       }
70693e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
70703e922f36SToby Isaac     }
70713e922f36SToby Isaac   }
7072ffc4695bSBarry Smith   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
70733e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
707469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
70753e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
707669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
70773e922f36SToby Isaac     PetscFunctionReturn(0);
70783e922f36SToby Isaac   }
70792e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
70802e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
70812e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
70822e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
70832e17dfb7SMatthew G. Knepley   }
70842e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
70852e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
7086c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
70872e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
70882e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
70892e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
70902e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
7091c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
70922e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
70932e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
70942e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
70952e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
70962e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
70972e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
70982e17dfb7SMatthew G. Knepley   }
70993e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
71003e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
71013e922f36SToby Isaac 
71022e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
71032e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
71043e922f36SToby Isaac       PetscInt     b, cdof;
71052e17dfb7SMatthew G. Knepley 
71063e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
71073e922f36SToby Isaac       if (!cdof) continue;
71082e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
71092e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
71102e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
71112e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
71122e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
71132e17dfb7SMatthew G. Knepley     }
71143e922f36SToby Isaac   }
711569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
711669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
7117c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
71182e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
71192e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
71202e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
71212e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
71222e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
71232e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
71242e17dfb7SMatthew G. Knepley }
71252e17dfb7SMatthew G. Knepley 
7126e87bb0d3SMatthew G Knepley /*@
71273a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
7128e87bb0d3SMatthew G Knepley 
7129d083f849SBarry Smith   Collective on v (see explanation below)
7130e87bb0d3SMatthew G Knepley 
7131e87bb0d3SMatthew G Knepley   Input Parameters:
7132e87bb0d3SMatthew G Knepley + dm - The DM
71336b867d5aSJose E. Roman - ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
7134e87bb0d3SMatthew G Knepley 
71356b867d5aSJose E. Roman   Input/Output Parameters:
71366b867d5aSJose E. Roman + v - The Vec of points, on output contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
71376b867d5aSJose E. Roman - cellSF - Points to either NULL, or a PetscSF with guesses for which cells contain each point;
71386b867d5aSJose E. Roman            on output, the PetscSF containing the ranks and local indices of the containing points
71393a93e3b7SToby Isaac 
7140e87bb0d3SMatthew G Knepley   Level: developer
714161e3bb9bSMatthew G Knepley 
714262a38674SMatthew G. Knepley   Notes:
71433a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
714462a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
71453a93e3b7SToby Isaac 
71463a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
714762a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
71483a93e3b7SToby Isaac 
71493a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
71503a93e3b7SToby Isaac 
715162a38674SMatthew G. Knepley $    const PetscSFNode *cells;
715262a38674SMatthew G. Knepley $    PetscInt           nFound;
7153a6216909SToby Isaac $    const PetscInt    *found;
715462a38674SMatthew G. Knepley $
7155a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
71563a93e3b7SToby Isaac 
71573a93e3b7SToby 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
71583a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
71593a93e3b7SToby Isaac 
716062a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
716161e3bb9bSMatthew G Knepley @*/
716262a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
7163e87bb0d3SMatthew G Knepley {
7164735aa83eSMatthew G Knepley   PetscErrorCode ierr;
7165735aa83eSMatthew G Knepley 
7166e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
7167e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7168e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
7169e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
71703a93e3b7SToby Isaac   if (*cellSF) {
71713a93e3b7SToby Isaac     PetscMPIInt result;
71723a93e3b7SToby Isaac 
7173e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
7174ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRMPI(ierr);
71753a93e3b7SToby 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");
7176e0fc9d1bSMatthew G. Knepley   } else {
71773a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
71783a93e3b7SToby Isaac   }
7179b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
718047a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
718162a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
718247a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
7183e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
7184e87bb0d3SMatthew G Knepley }
718514f150ffSMatthew G. Knepley 
7186f4d763aaSMatthew G. Knepley /*@
7187f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
7188f4d763aaSMatthew G. Knepley 
71898f700142SStefano Zampini   Collective on dm
71908f700142SStefano Zampini 
7191f4d763aaSMatthew G. Knepley   Input Parameter:
7192f4d763aaSMatthew G. Knepley . dm - The original DM
7193f4d763aaSMatthew G. Knepley 
7194f4d763aaSMatthew G. Knepley   Output Parameter:
7195f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
7196f4d763aaSMatthew G. Knepley 
7197f4d763aaSMatthew G. Knepley   Level: intermediate
7198f4d763aaSMatthew G. Knepley 
7199e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
7200f4d763aaSMatthew G. Knepley @*/
720114f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
720214f150ffSMatthew G. Knepley {
7203c26acbdeSMatthew G. Knepley   PetscSection   section;
72042d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
720514f150ffSMatthew G. Knepley   PetscErrorCode ierr;
720614f150ffSMatthew G. Knepley 
720714f150ffSMatthew G. Knepley   PetscFunctionBegin;
720814f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
720914f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
721092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
7211c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
7212ffc4695bSBarry Smith   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
72132d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
7214c26acbdeSMatthew G. Knepley     *odm = dm;
7215c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
7216c26acbdeSMatthew G. Knepley   }
721714f150ffSMatthew G. Knepley   if (!dm->dmBC) {
7218c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
721914f150ffSMatthew G. Knepley     PetscSF      sf;
722014f150ffSMatthew G. Knepley 
722114f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
7222e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
722314f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
722492fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
722514f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
722614f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
722715b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
7228e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
722914f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
723014f150ffSMatthew G. Knepley   }
723114f150ffSMatthew G. Knepley   *odm = dm->dmBC;
723214f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
723314f150ffSMatthew G. Knepley }
7234f4d763aaSMatthew G. Knepley 
7235f4d763aaSMatthew G. Knepley /*@
7236cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7237f4d763aaSMatthew G. Knepley 
7238f4d763aaSMatthew G. Knepley   Input Parameter:
7239f4d763aaSMatthew G. Knepley . dm - The original DM
7240f4d763aaSMatthew G. Knepley 
7241cdb7a50dSMatthew G. Knepley   Output Parameters:
7242cdb7a50dSMatthew G. Knepley + num - The output sequence number
7243cdb7a50dSMatthew G. Knepley - val - The output sequence value
7244f4d763aaSMatthew G. Knepley 
7245f4d763aaSMatthew G. Knepley   Level: intermediate
7246f4d763aaSMatthew G. Knepley 
7247f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7248f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7249f4d763aaSMatthew G. Knepley 
7250f4d763aaSMatthew G. Knepley .seealso: VecView()
7251f4d763aaSMatthew G. Knepley @*/
7252cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7253f4d763aaSMatthew G. Knepley {
7254f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7255f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7256534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7257534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7258f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7259f4d763aaSMatthew G. Knepley }
7260f4d763aaSMatthew G. Knepley 
7261f4d763aaSMatthew G. Knepley /*@
7262cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7263f4d763aaSMatthew G. Knepley 
7264f4d763aaSMatthew G. Knepley   Input Parameters:
7265f4d763aaSMatthew G. Knepley + dm - The original DM
7266cdb7a50dSMatthew G. Knepley . num - The output sequence number
7267cdb7a50dSMatthew G. Knepley - val - The output sequence value
7268f4d763aaSMatthew G. Knepley 
7269f4d763aaSMatthew G. Knepley   Level: intermediate
7270f4d763aaSMatthew G. Knepley 
7271f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7272f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7273f4d763aaSMatthew G. Knepley 
7274f4d763aaSMatthew G. Knepley .seealso: VecView()
7275f4d763aaSMatthew G. Knepley @*/
7276cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7277f4d763aaSMatthew G. Knepley {
7278f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7279f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7280f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7281cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7282cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7283cdb7a50dSMatthew G. Knepley }
7284cdb7a50dSMatthew G. Knepley 
7285cdb7a50dSMatthew G. Knepley /*@C
7286cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7287cdb7a50dSMatthew G. Knepley 
7288cdb7a50dSMatthew G. Knepley   Input Parameters:
7289cdb7a50dSMatthew G. Knepley + dm   - The original DM
7290cdb7a50dSMatthew G. Knepley . name - The sequence name
7291cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7292cdb7a50dSMatthew G. Knepley 
7293cdb7a50dSMatthew G. Knepley   Output Parameter:
7294cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7295cdb7a50dSMatthew G. Knepley 
7296cdb7a50dSMatthew G. Knepley   Level: intermediate
7297cdb7a50dSMatthew G. Knepley 
7298cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7299cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7300cdb7a50dSMatthew G. Knepley 
7301cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7302cdb7a50dSMatthew G. Knepley @*/
7303cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7304cdb7a50dSMatthew G. Knepley {
7305cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7306cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
7307cdb7a50dSMatthew G. Knepley 
7308cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7309cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7310cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7311064a246eSJacob Faibussowitsch   PetscValidRealPointer(val,5);
7312cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
7313cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7314cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7315cdb7a50dSMatthew G. Knepley     PetscScalar value;
7316cdb7a50dSMatthew G. Knepley 
731739d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
73184aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7319cdb7a50dSMatthew G. Knepley #endif
7320cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7321f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7322f4d763aaSMatthew G. Knepley }
73238e4ac7eaSMatthew G. Knepley 
73248e4ac7eaSMatthew G. Knepley /*@
73258e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
73268e4ac7eaSMatthew G. Knepley 
73278e4ac7eaSMatthew G. Knepley   Not collective
73288e4ac7eaSMatthew G. Knepley 
73298e4ac7eaSMatthew G. Knepley   Input Parameter:
73308e4ac7eaSMatthew G. Knepley . dm - The DM
73318e4ac7eaSMatthew G. Knepley 
73328e4ac7eaSMatthew G. Knepley   Output Parameter:
73338e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
73348e4ac7eaSMatthew G. Knepley 
73358e4ac7eaSMatthew G. Knepley   Level: beginner
73368e4ac7eaSMatthew G. Knepley 
73378e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
73388e4ac7eaSMatthew G. Knepley @*/
73398e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
73408e4ac7eaSMatthew G. Knepley {
73418e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
73428e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7343534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
73448e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
73458e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
73468e4ac7eaSMatthew G. Knepley }
73478e4ac7eaSMatthew G. Knepley 
73488e4ac7eaSMatthew G. Knepley /*@
73495d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
73508e4ac7eaSMatthew G. Knepley 
73518e4ac7eaSMatthew G. Knepley   Collective on dm
73528e4ac7eaSMatthew G. Knepley 
73538e4ac7eaSMatthew G. Knepley   Input Parameters:
73548e4ac7eaSMatthew G. Knepley + dm - The DM
73558e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
73568e4ac7eaSMatthew G. Knepley 
73575d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
73585d3b26e6SMatthew G. Knepley 
73598e4ac7eaSMatthew G. Knepley   Level: beginner
73608e4ac7eaSMatthew G. Knepley 
73615d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
73628e4ac7eaSMatthew G. Knepley @*/
73638e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
73648e4ac7eaSMatthew G. Knepley {
73658e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
73668e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73678833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
73688e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
73698e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
73708e4ac7eaSMatthew G. Knepley }
7371c58f1c22SToby Isaac 
7372c58f1c22SToby Isaac /*@C
7373c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7374c58f1c22SToby Isaac 
7375c58f1c22SToby Isaac   Not Collective
7376c58f1c22SToby Isaac 
7377c58f1c22SToby Isaac   Input Parameters:
7378c58f1c22SToby Isaac + dm   - The DM object
7379c58f1c22SToby Isaac - name - The label name
7380c58f1c22SToby Isaac 
7381c58f1c22SToby Isaac   Level: intermediate
7382c58f1c22SToby Isaac 
7383c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7384c58f1c22SToby Isaac @*/
7385c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7386c58f1c22SToby Isaac {
73875d80c0bfSVaclav Hapla   PetscBool      flg;
73885d80c0bfSVaclav Hapla   DMLabel        label;
7389c58f1c22SToby Isaac   PetscErrorCode ierr;
7390c58f1c22SToby Isaac 
7391c58f1c22SToby Isaac   PetscFunctionBegin;
7392c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7393c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
73945d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7395c58f1c22SToby Isaac   if (!flg) {
73965d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
73975d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
73985d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7399c58f1c22SToby Isaac   }
7400c58f1c22SToby Isaac   PetscFunctionReturn(0);
7401c58f1c22SToby Isaac }
7402c58f1c22SToby Isaac 
7403c58f1c22SToby Isaac /*@C
74040fdc7489SMatthew Knepley   DMCreateLabelAtIndex - Create a label of the given name at the iven index. If it already exists, move it to this index.
74050fdc7489SMatthew Knepley 
74060fdc7489SMatthew Knepley   Not Collective
74070fdc7489SMatthew Knepley 
74080fdc7489SMatthew Knepley   Input Parameters:
74090fdc7489SMatthew Knepley + dm   - The DM object
74100fdc7489SMatthew Knepley . l    - The index for the label
74110fdc7489SMatthew Knepley - name - The label name
74120fdc7489SMatthew Knepley 
74130fdc7489SMatthew Knepley   Level: intermediate
74140fdc7489SMatthew Knepley 
74150fdc7489SMatthew Knepley .seealso: DMCreateLabel(), DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
74160fdc7489SMatthew Knepley @*/
74170fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
74180fdc7489SMatthew Knepley {
74190fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
74200fdc7489SMatthew Knepley   DMLabel        label;
74210fdc7489SMatthew Knepley   PetscInt       Nl, m;
74220fdc7489SMatthew Knepley   PetscBool      flg, match;
74230fdc7489SMatthew Knepley   const char    *lname;
74240fdc7489SMatthew Knepley   PetscErrorCode ierr;
74250fdc7489SMatthew Knepley 
74260fdc7489SMatthew Knepley   PetscFunctionBegin;
74270fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7428064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
74290fdc7489SMatthew Knepley   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
74300fdc7489SMatthew Knepley   if (!flg) {
74310fdc7489SMatthew Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
74320fdc7489SMatthew Knepley     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
74330fdc7489SMatthew Knepley     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
74340fdc7489SMatthew Knepley   }
74350fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
74360fdc7489SMatthew Knepley   if (l >= Nl) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %D must be in [0, %D)", l, Nl);
74370fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
74380fdc7489SMatthew Knepley     ierr = PetscObjectGetName((PetscObject) orig->label, &lname);CHKERRQ(ierr);
74390fdc7489SMatthew Knepley     ierr = PetscStrcmp(name, lname, &match);CHKERRQ(ierr);
74400fdc7489SMatthew Knepley     if (match) break;
74410fdc7489SMatthew Knepley   }
74420fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
74430fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
74440fdc7489SMatthew Knepley   else    prev->next = orig->next;
74450fdc7489SMatthew Knepley   if (!l) {
74460fdc7489SMatthew Knepley     orig->next = dm->labels;
74470fdc7489SMatthew Knepley     dm->labels = orig;
74480fdc7489SMatthew Knepley   } else {
74490fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
74500fdc7489SMatthew Knepley     orig->next = prev->next;
74510fdc7489SMatthew Knepley     prev->next = orig;
74520fdc7489SMatthew Knepley   }
74530fdc7489SMatthew Knepley   PetscFunctionReturn(0);
74540fdc7489SMatthew Knepley }
74550fdc7489SMatthew Knepley 
74560fdc7489SMatthew Knepley /*@C
7457c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7458c58f1c22SToby Isaac 
7459c58f1c22SToby Isaac   Not Collective
7460c58f1c22SToby Isaac 
7461c58f1c22SToby Isaac   Input Parameters:
7462c58f1c22SToby Isaac + dm   - The DM object
7463c58f1c22SToby Isaac . name - The label name
7464c58f1c22SToby Isaac - point - The mesh point
7465c58f1c22SToby Isaac 
7466c58f1c22SToby Isaac   Output Parameter:
7467c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7468c58f1c22SToby Isaac 
7469c58f1c22SToby Isaac   Level: beginner
7470c58f1c22SToby Isaac 
7471c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7472c58f1c22SToby Isaac @*/
7473c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7474c58f1c22SToby Isaac {
7475c58f1c22SToby Isaac   DMLabel        label;
7476c58f1c22SToby Isaac   PetscErrorCode ierr;
7477c58f1c22SToby Isaac 
7478c58f1c22SToby Isaac   PetscFunctionBegin;
7479c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7480c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7481c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
748213903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7483c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7484c58f1c22SToby Isaac   PetscFunctionReturn(0);
7485c58f1c22SToby Isaac }
7486c58f1c22SToby Isaac 
7487c58f1c22SToby Isaac /*@C
7488c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7489c58f1c22SToby Isaac 
7490c58f1c22SToby Isaac   Not Collective
7491c58f1c22SToby Isaac 
7492c58f1c22SToby Isaac   Input Parameters:
7493c58f1c22SToby Isaac + dm   - The DM object
7494c58f1c22SToby Isaac . name - The label name
7495c58f1c22SToby Isaac . point - The mesh point
7496c58f1c22SToby Isaac - value - The label value for this point
7497c58f1c22SToby Isaac 
7498c58f1c22SToby Isaac   Output Parameter:
7499c58f1c22SToby Isaac 
7500c58f1c22SToby Isaac   Level: beginner
7501c58f1c22SToby Isaac 
7502c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7503c58f1c22SToby Isaac @*/
7504c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7505c58f1c22SToby Isaac {
7506c58f1c22SToby Isaac   DMLabel        label;
7507c58f1c22SToby Isaac   PetscErrorCode ierr;
7508c58f1c22SToby Isaac 
7509c58f1c22SToby Isaac   PetscFunctionBegin;
7510c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7511c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7512c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7513c58f1c22SToby Isaac   if (!label) {
7514c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7515c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7516c58f1c22SToby Isaac   }
7517c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7518c58f1c22SToby Isaac   PetscFunctionReturn(0);
7519c58f1c22SToby Isaac }
7520c58f1c22SToby Isaac 
7521c58f1c22SToby Isaac /*@C
7522c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7523c58f1c22SToby Isaac 
7524c58f1c22SToby Isaac   Not Collective
7525c58f1c22SToby Isaac 
7526c58f1c22SToby Isaac   Input Parameters:
7527c58f1c22SToby Isaac + dm   - The DM object
7528c58f1c22SToby Isaac . name - The label name
7529c58f1c22SToby Isaac . point - The mesh point
7530c58f1c22SToby Isaac - value - The label value for this point
7531c58f1c22SToby Isaac 
7532c58f1c22SToby Isaac   Output Parameter:
7533c58f1c22SToby Isaac 
7534c58f1c22SToby Isaac   Level: beginner
7535c58f1c22SToby Isaac 
7536c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7537c58f1c22SToby Isaac @*/
7538c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7539c58f1c22SToby Isaac {
7540c58f1c22SToby Isaac   DMLabel        label;
7541c58f1c22SToby Isaac   PetscErrorCode ierr;
7542c58f1c22SToby Isaac 
7543c58f1c22SToby Isaac   PetscFunctionBegin;
7544c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7545c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7546c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7547c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7548c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7549c58f1c22SToby Isaac   PetscFunctionReturn(0);
7550c58f1c22SToby Isaac }
7551c58f1c22SToby Isaac 
7552c58f1c22SToby Isaac /*@C
7553c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7554c58f1c22SToby Isaac 
7555c58f1c22SToby Isaac   Not Collective
7556c58f1c22SToby Isaac 
7557c58f1c22SToby Isaac   Input Parameters:
7558c58f1c22SToby Isaac + dm   - The DM object
7559c58f1c22SToby Isaac - name - The label name
7560c58f1c22SToby Isaac 
7561c58f1c22SToby Isaac   Output Parameter:
7562c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7563c58f1c22SToby Isaac 
7564c58f1c22SToby Isaac   Level: beginner
7565c58f1c22SToby Isaac 
7566df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7567c58f1c22SToby Isaac @*/
7568c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7569c58f1c22SToby Isaac {
7570c58f1c22SToby Isaac   DMLabel        label;
7571c58f1c22SToby Isaac   PetscErrorCode ierr;
7572c58f1c22SToby Isaac 
7573c58f1c22SToby Isaac   PetscFunctionBegin;
7574c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7575c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7576534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7577c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7578c58f1c22SToby Isaac   *size = 0;
7579c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7580c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7581c58f1c22SToby Isaac   PetscFunctionReturn(0);
7582c58f1c22SToby Isaac }
7583c58f1c22SToby Isaac 
7584c58f1c22SToby Isaac /*@C
7585c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7586c58f1c22SToby Isaac 
7587c58f1c22SToby Isaac   Not Collective
7588c58f1c22SToby Isaac 
7589c58f1c22SToby Isaac   Input Parameters:
7590c58f1c22SToby Isaac + mesh - The DM object
7591c58f1c22SToby Isaac - name - The label name
7592c58f1c22SToby Isaac 
7593c58f1c22SToby Isaac   Output Parameter:
7594c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7595c58f1c22SToby Isaac 
7596c58f1c22SToby Isaac   Level: beginner
7597c58f1c22SToby Isaac 
7598c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7599c58f1c22SToby Isaac @*/
7600c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
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);
7608c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7609c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7610c58f1c22SToby Isaac   *ids = NULL;
7611dab2e251SBlaise Bourdin  if (label) {
7612c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7613dab2e251SBlaise Bourdin   } else {
7614dab2e251SBlaise Bourdin     /* returning an empty IS */
7615dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7616dab2e251SBlaise Bourdin   }
7617c58f1c22SToby Isaac   PetscFunctionReturn(0);
7618c58f1c22SToby Isaac }
7619c58f1c22SToby Isaac 
7620c58f1c22SToby Isaac /*@C
7621c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7622c58f1c22SToby Isaac 
7623c58f1c22SToby Isaac   Not Collective
7624c58f1c22SToby Isaac 
7625c58f1c22SToby Isaac   Input Parameters:
7626c58f1c22SToby Isaac + dm - The DM object
7627c58f1c22SToby Isaac . name - The label name
7628c58f1c22SToby Isaac - value - The stratum value
7629c58f1c22SToby Isaac 
7630c58f1c22SToby Isaac   Output Parameter:
7631c58f1c22SToby Isaac . size - The stratum size
7632c58f1c22SToby Isaac 
7633c58f1c22SToby Isaac   Level: beginner
7634c58f1c22SToby Isaac 
7635c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7636c58f1c22SToby Isaac @*/
7637c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7638c58f1c22SToby Isaac {
7639c58f1c22SToby Isaac   DMLabel        label;
7640c58f1c22SToby Isaac   PetscErrorCode ierr;
7641c58f1c22SToby Isaac 
7642c58f1c22SToby Isaac   PetscFunctionBegin;
7643c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7644c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7645534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7646c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7647c58f1c22SToby Isaac   *size = 0;
7648c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7649c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7650c58f1c22SToby Isaac   PetscFunctionReturn(0);
7651c58f1c22SToby Isaac }
7652c58f1c22SToby Isaac 
7653c58f1c22SToby Isaac /*@C
7654c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7655c58f1c22SToby Isaac 
7656c58f1c22SToby Isaac   Not Collective
7657c58f1c22SToby Isaac 
7658c58f1c22SToby Isaac   Input Parameters:
7659c58f1c22SToby Isaac + dm - The DM object
7660c58f1c22SToby Isaac . name - The label name
7661c58f1c22SToby Isaac - value - The stratum value
7662c58f1c22SToby Isaac 
7663c58f1c22SToby Isaac   Output Parameter:
7664c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7665c58f1c22SToby Isaac 
7666c58f1c22SToby Isaac   Level: beginner
7667c58f1c22SToby Isaac 
7668c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7669c58f1c22SToby Isaac @*/
7670c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7671c58f1c22SToby Isaac {
7672c58f1c22SToby Isaac   DMLabel        label;
7673c58f1c22SToby Isaac   PetscErrorCode ierr;
7674c58f1c22SToby Isaac 
7675c58f1c22SToby Isaac   PetscFunctionBegin;
7676c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7677c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7678c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7679c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7680c58f1c22SToby Isaac   *points = NULL;
7681c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7682c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7683c58f1c22SToby Isaac   PetscFunctionReturn(0);
7684c58f1c22SToby Isaac }
7685c58f1c22SToby Isaac 
76864de306b1SToby Isaac /*@C
76879044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
76884de306b1SToby Isaac 
76894de306b1SToby Isaac   Not Collective
76904de306b1SToby Isaac 
76914de306b1SToby Isaac   Input Parameters:
76924de306b1SToby Isaac + dm - The DM object
76934de306b1SToby Isaac . name - The label name
76944de306b1SToby Isaac . value - The stratum value
76954de306b1SToby Isaac - points - The stratum points
76964de306b1SToby Isaac 
76974de306b1SToby Isaac   Level: beginner
76984de306b1SToby Isaac 
76994de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
77004de306b1SToby Isaac @*/
77014de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
77024de306b1SToby Isaac {
77034de306b1SToby Isaac   DMLabel        label;
77044de306b1SToby Isaac   PetscErrorCode ierr;
77054de306b1SToby Isaac 
77064de306b1SToby Isaac   PetscFunctionBegin;
77074de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77084de306b1SToby Isaac   PetscValidCharPointer(name, 2);
77094de306b1SToby Isaac   PetscValidPointer(points, 4);
77104de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
77114de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
77124de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
77134de306b1SToby Isaac   PetscFunctionReturn(0);
77144de306b1SToby Isaac }
77154de306b1SToby Isaac 
7716c58f1c22SToby Isaac /*@C
7717c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7718c58f1c22SToby Isaac 
7719c58f1c22SToby Isaac   Not Collective
7720c58f1c22SToby Isaac 
7721c58f1c22SToby Isaac   Input Parameters:
7722c58f1c22SToby Isaac + dm   - The DM object
7723c58f1c22SToby Isaac . name - The label name
7724c58f1c22SToby Isaac - value - The label value for this point
7725c58f1c22SToby Isaac 
7726c58f1c22SToby Isaac   Output Parameter:
7727c58f1c22SToby Isaac 
7728c58f1c22SToby Isaac   Level: beginner
7729c58f1c22SToby Isaac 
7730c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7731c58f1c22SToby Isaac @*/
7732c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7733c58f1c22SToby Isaac {
7734c58f1c22SToby Isaac   DMLabel        label;
7735c58f1c22SToby Isaac   PetscErrorCode ierr;
7736c58f1c22SToby Isaac 
7737c58f1c22SToby Isaac   PetscFunctionBegin;
7738c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7739c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7740c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7741c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7742c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7743c58f1c22SToby Isaac   PetscFunctionReturn(0);
7744c58f1c22SToby Isaac }
7745c58f1c22SToby Isaac 
7746c58f1c22SToby Isaac /*@
7747c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7748c58f1c22SToby Isaac 
7749c58f1c22SToby Isaac   Not Collective
7750c58f1c22SToby Isaac 
7751c58f1c22SToby Isaac   Input Parameter:
7752c58f1c22SToby Isaac . dm   - The DM object
7753c58f1c22SToby Isaac 
7754c58f1c22SToby Isaac   Output Parameter:
7755c58f1c22SToby Isaac . numLabels - the number of Labels
7756c58f1c22SToby Isaac 
7757c58f1c22SToby Isaac   Level: intermediate
7758c58f1c22SToby Isaac 
7759c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7760c58f1c22SToby Isaac @*/
7761c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7762c58f1c22SToby Isaac {
77635d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7764c58f1c22SToby Isaac   PetscInt  n    = 0;
7765c58f1c22SToby Isaac 
7766c58f1c22SToby Isaac   PetscFunctionBegin;
7767c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7768534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7769c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7770c58f1c22SToby Isaac   *numLabels = n;
7771c58f1c22SToby Isaac   PetscFunctionReturn(0);
7772c58f1c22SToby Isaac }
7773c58f1c22SToby Isaac 
7774c58f1c22SToby Isaac /*@C
7775c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7776c58f1c22SToby Isaac 
7777c58f1c22SToby Isaac   Not Collective
7778c58f1c22SToby Isaac 
7779c58f1c22SToby Isaac   Input Parameters:
7780c58f1c22SToby Isaac + dm - The DM object
7781c58f1c22SToby Isaac - n  - the label number
7782c58f1c22SToby Isaac 
7783c58f1c22SToby Isaac   Output Parameter:
7784c58f1c22SToby Isaac . name - the label name
7785c58f1c22SToby Isaac 
7786c58f1c22SToby Isaac   Level: intermediate
7787c58f1c22SToby Isaac 
7788c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7789c58f1c22SToby Isaac @*/
7790c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7791c58f1c22SToby Isaac {
77925d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7793c58f1c22SToby Isaac   PetscInt       l    = 0;
7794d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7795c58f1c22SToby Isaac 
7796c58f1c22SToby Isaac   PetscFunctionBegin;
7797c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7798c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7799c58f1c22SToby Isaac   while (next) {
7800c58f1c22SToby Isaac     if (l == n) {
7801d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7802c58f1c22SToby Isaac       PetscFunctionReturn(0);
7803c58f1c22SToby Isaac     }
7804c58f1c22SToby Isaac     ++l;
7805c58f1c22SToby Isaac     next = next->next;
7806c58f1c22SToby Isaac   }
7807c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7808c58f1c22SToby Isaac }
7809c58f1c22SToby Isaac 
7810c58f1c22SToby Isaac /*@C
7811c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7812c58f1c22SToby Isaac 
7813c58f1c22SToby Isaac   Not Collective
7814c58f1c22SToby Isaac 
7815c58f1c22SToby Isaac   Input Parameters:
7816c58f1c22SToby Isaac + dm   - The DM object
7817c58f1c22SToby Isaac - name - The label name
7818c58f1c22SToby Isaac 
7819c58f1c22SToby Isaac   Output Parameter:
7820c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7821c58f1c22SToby Isaac 
7822c58f1c22SToby Isaac   Level: intermediate
7823c58f1c22SToby Isaac 
7824c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7825c58f1c22SToby Isaac @*/
7826c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7827c58f1c22SToby Isaac {
78285d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7829d67d17b1SMatthew G. Knepley   const char    *lname;
7830c58f1c22SToby Isaac   PetscErrorCode ierr;
7831c58f1c22SToby Isaac 
7832c58f1c22SToby Isaac   PetscFunctionBegin;
7833c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7834c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7835534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7836c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7837c58f1c22SToby Isaac   while (next) {
7838d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7839d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7840c58f1c22SToby Isaac     if (*hasLabel) break;
7841c58f1c22SToby Isaac     next = next->next;
7842c58f1c22SToby Isaac   }
7843c58f1c22SToby Isaac   PetscFunctionReturn(0);
7844c58f1c22SToby Isaac }
7845c58f1c22SToby Isaac 
7846c58f1c22SToby Isaac /*@C
7847c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7848c58f1c22SToby Isaac 
7849c58f1c22SToby Isaac   Not Collective
7850c58f1c22SToby Isaac 
7851c58f1c22SToby Isaac   Input Parameters:
7852c58f1c22SToby Isaac + dm   - The DM object
7853c58f1c22SToby Isaac - name - The label name
7854c58f1c22SToby Isaac 
7855c58f1c22SToby Isaac   Output Parameter:
7856c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7857c58f1c22SToby Isaac 
78586d7c9049SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
78596d7c9049SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
78606d7c9049SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
78616d7c9049SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
78626d7c9049SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
78636d7c9049SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
78646d7c9049SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
78656d7c9049SMatthew G. Knepley 
7866c58f1c22SToby Isaac   Level: intermediate
7867c58f1c22SToby Isaac 
78686d7c9049SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
7869c58f1c22SToby Isaac @*/
7870c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7871c58f1c22SToby Isaac {
78725d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7873c58f1c22SToby Isaac   PetscBool      hasLabel;
7874d67d17b1SMatthew G. Knepley   const char    *lname;
7875c58f1c22SToby Isaac   PetscErrorCode ierr;
7876c58f1c22SToby Isaac 
7877c58f1c22SToby Isaac   PetscFunctionBegin;
7878c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7879c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7880c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7881c58f1c22SToby Isaac   *label = NULL;
7882c58f1c22SToby Isaac   while (next) {
7883d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7884d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7885c58f1c22SToby Isaac     if (hasLabel) {
7886c58f1c22SToby Isaac       *label = next->label;
7887c58f1c22SToby Isaac       break;
7888c58f1c22SToby Isaac     }
7889c58f1c22SToby Isaac     next = next->next;
7890c58f1c22SToby Isaac   }
7891c58f1c22SToby Isaac   PetscFunctionReturn(0);
7892c58f1c22SToby Isaac }
7893c58f1c22SToby Isaac 
7894c58f1c22SToby Isaac /*@C
7895c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7896c58f1c22SToby Isaac 
7897c58f1c22SToby Isaac   Not Collective
7898c58f1c22SToby Isaac 
7899c58f1c22SToby Isaac   Input Parameters:
7900c58f1c22SToby Isaac + dm - The DM object
7901c58f1c22SToby Isaac - n  - the label number
7902c58f1c22SToby Isaac 
7903c58f1c22SToby Isaac   Output Parameter:
7904c58f1c22SToby Isaac . label - the label
7905c58f1c22SToby Isaac 
7906c58f1c22SToby Isaac   Level: intermediate
7907c58f1c22SToby Isaac 
7908c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7909c58f1c22SToby Isaac @*/
7910c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7911c58f1c22SToby Isaac {
79125d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7913c58f1c22SToby Isaac   PetscInt    l    = 0;
7914c58f1c22SToby Isaac 
7915c58f1c22SToby Isaac   PetscFunctionBegin;
7916c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7917c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7918c58f1c22SToby Isaac   while (next) {
7919c58f1c22SToby Isaac     if (l == n) {
7920c58f1c22SToby Isaac       *label = next->label;
7921c58f1c22SToby Isaac       PetscFunctionReturn(0);
7922c58f1c22SToby Isaac     }
7923c58f1c22SToby Isaac     ++l;
7924c58f1c22SToby Isaac     next = next->next;
7925c58f1c22SToby Isaac   }
7926c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7927c58f1c22SToby Isaac }
7928c58f1c22SToby Isaac 
7929c58f1c22SToby Isaac /*@C
7930c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7931c58f1c22SToby Isaac 
7932c58f1c22SToby Isaac   Not Collective
7933c58f1c22SToby Isaac 
7934c58f1c22SToby Isaac   Input Parameters:
7935c58f1c22SToby Isaac + dm   - The DM object
7936c58f1c22SToby Isaac - label - The DMLabel
7937c58f1c22SToby Isaac 
7938c58f1c22SToby Isaac   Level: developer
7939c58f1c22SToby Isaac 
7940c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7941c58f1c22SToby Isaac @*/
7942c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7943c58f1c22SToby Isaac {
79445d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7945c58f1c22SToby Isaac   PetscBool      hasLabel;
7946d67d17b1SMatthew G. Knepley   const char    *lname;
79475d80c0bfSVaclav Hapla   PetscBool      flg;
7948c58f1c22SToby Isaac   PetscErrorCode ierr;
7949c58f1c22SToby Isaac 
7950c58f1c22SToby Isaac   PetscFunctionBegin;
7951c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7952d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7953d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7954d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7955c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7956c58f1c22SToby Isaac   tmpLabel->label  = label;
7957c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
79585d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
79595d80c0bfSVaclav Hapla   *p = tmpLabel;
796008f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
79615d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
79625d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7963ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7964ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7965c58f1c22SToby Isaac   PetscFunctionReturn(0);
7966c58f1c22SToby Isaac }
7967c58f1c22SToby Isaac 
7968c58f1c22SToby Isaac /*@C
79694a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
79704a7ee7d0SMatthew G. Knepley 
79714a7ee7d0SMatthew G. Knepley   Not Collective
79724a7ee7d0SMatthew G. Knepley 
79734a7ee7d0SMatthew G. Knepley   Input Parameters:
79744a7ee7d0SMatthew G. Knepley + dm    - The DM object
79754a7ee7d0SMatthew G. Knepley - label - The DMLabel, having the same name, to substitute
79764a7ee7d0SMatthew G. Knepley 
79774a7ee7d0SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
79784a7ee7d0SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
79794a7ee7d0SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
79804a7ee7d0SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
79814a7ee7d0SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
79824a7ee7d0SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
79834a7ee7d0SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
79844a7ee7d0SMatthew G. Knepley 
79854a7ee7d0SMatthew G. Knepley   Level: intermediate
79864a7ee7d0SMatthew G. Knepley 
79874a7ee7d0SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
79884a7ee7d0SMatthew G. Knepley @*/
79894a7ee7d0SMatthew G. Knepley PetscErrorCode DMSetLabel(DM dm, DMLabel label)
79904a7ee7d0SMatthew G. Knepley {
79914a7ee7d0SMatthew G. Knepley   DMLabelLink    next = dm->labels;
79924a7ee7d0SMatthew G. Knepley   PetscBool      hasLabel, flg;
79934a7ee7d0SMatthew G. Knepley   const char    *name, *lname;
79944a7ee7d0SMatthew G. Knepley   PetscErrorCode ierr;
79954a7ee7d0SMatthew G. Knepley 
79964a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
79974a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79984a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
79994a7ee7d0SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &name);CHKERRQ(ierr);
80004a7ee7d0SMatthew G. Knepley   while (next) {
80014a7ee7d0SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
80024a7ee7d0SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
80034a7ee7d0SMatthew G. Knepley     if (hasLabel) {
80044a7ee7d0SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
80054a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
80064a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
80074a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
80084a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
80094a7ee7d0SMatthew G. Knepley       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
80104a7ee7d0SMatthew G. Knepley       next->label = label;
80114a7ee7d0SMatthew G. Knepley       break;
80124a7ee7d0SMatthew G. Knepley     }
80134a7ee7d0SMatthew G. Knepley     next = next->next;
80144a7ee7d0SMatthew G. Knepley   }
80154a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
80164a7ee7d0SMatthew G. Knepley }
80174a7ee7d0SMatthew G. Knepley 
80184a7ee7d0SMatthew G. Knepley /*@C
8019e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
8020c58f1c22SToby Isaac 
8021c58f1c22SToby Isaac   Not Collective
8022c58f1c22SToby Isaac 
8023c58f1c22SToby Isaac   Input Parameters:
8024c58f1c22SToby Isaac + dm   - The DM object
8025c58f1c22SToby Isaac - name - The label name
8026c58f1c22SToby Isaac 
8027c58f1c22SToby Isaac   Output Parameter:
8028c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
8029c58f1c22SToby Isaac 
8030c58f1c22SToby Isaac   Level: developer
8031c58f1c22SToby Isaac 
8032e5472504SVaclav Hapla   Notes:
8033e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
8034e5472504SVaclav Hapla   DMLabelDestroy() on the label.
8035e5472504SVaclav Hapla 
8036e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
8037e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
8038e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
8039e5472504SVaclav Hapla 
8040e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
8041c58f1c22SToby Isaac @*/
8042c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
8043c58f1c22SToby Isaac {
804495d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
8045c58f1c22SToby Isaac   PetscBool      hasLabel;
8046d67d17b1SMatthew G. Knepley   const char    *lname;
8047c58f1c22SToby Isaac   PetscErrorCode ierr;
8048c58f1c22SToby Isaac 
8049c58f1c22SToby Isaac   PetscFunctionBegin;
8050c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8051e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
8052e5472504SVaclav Hapla   if (label) {
8053e5472504SVaclav Hapla     PetscValidPointer(label, 3);
8054c58f1c22SToby Isaac     *label = NULL;
8055e5472504SVaclav Hapla   }
80565d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
805795d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
8058d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
8059c58f1c22SToby Isaac     if (hasLabel) {
806095d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
8061c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
806295d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
8063ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
8064ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
806595d578d6SVaclav Hapla       if (label) *label = link->label;
806695d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
806795d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8068c58f1c22SToby Isaac       break;
8069c58f1c22SToby Isaac     }
8070c58f1c22SToby Isaac   }
8071c58f1c22SToby Isaac   PetscFunctionReturn(0);
8072c58f1c22SToby Isaac }
8073c58f1c22SToby Isaac 
8074306894acSVaclav Hapla /*@
8075306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
8076306894acSVaclav Hapla 
8077306894acSVaclav Hapla   Not Collective
8078306894acSVaclav Hapla 
8079306894acSVaclav Hapla   Input Parameters:
8080306894acSVaclav Hapla + dm   - The DM object
8081306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
8082306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
8083306894acSVaclav Hapla 
8084306894acSVaclav Hapla   Level: developer
8085306894acSVaclav Hapla 
8086306894acSVaclav Hapla   Notes:
8087306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
8088306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
8089306894acSVaclav Hapla   *label nullified.
8090306894acSVaclav Hapla 
8091306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
8092306894acSVaclav Hapla @*/
8093306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
8094306894acSVaclav Hapla {
809543e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
8096306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
8097306894acSVaclav Hapla   PetscErrorCode ierr;
8098306894acSVaclav Hapla 
8099306894acSVaclav Hapla   PetscFunctionBegin;
8100306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8101306894acSVaclav Hapla   PetscValidPointer(label, 2);
8102f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
8103306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
8104306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
81055d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
810643e45a93SVaclav Hapla     if (*label == link->label) {
8107306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
810843e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
8109306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
8110ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
811143e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
811243e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
811343e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8114306894acSVaclav Hapla       break;
8115306894acSVaclav Hapla     }
8116306894acSVaclav Hapla   }
8117306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
8118306894acSVaclav Hapla   PetscFunctionReturn(0);
8119306894acSVaclav Hapla }
8120306894acSVaclav Hapla 
8121c58f1c22SToby Isaac /*@C
8122c58f1c22SToby Isaac   DMGetLabelOutput - Get 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 
8130c58f1c22SToby Isaac   Output Parameter:
8131c58f1c22SToby Isaac . output - The flag for output
8132c58f1c22SToby Isaac 
8133c58f1c22SToby Isaac   Level: developer
8134c58f1c22SToby Isaac 
8135c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8136c58f1c22SToby Isaac @*/
8137c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
8138c58f1c22SToby Isaac {
81395d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8140d67d17b1SMatthew G. Knepley   const char    *lname;
8141c58f1c22SToby Isaac   PetscErrorCode ierr;
8142c58f1c22SToby Isaac 
8143c58f1c22SToby Isaac   PetscFunctionBegin;
8144c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8145c58f1c22SToby Isaac   PetscValidPointer(name, 2);
8146c58f1c22SToby Isaac   PetscValidPointer(output, 3);
8147c58f1c22SToby Isaac   while (next) {
8148c58f1c22SToby Isaac     PetscBool flg;
8149c58f1c22SToby Isaac 
8150d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8151d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8152c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
8153c58f1c22SToby Isaac     next = next->next;
8154c58f1c22SToby Isaac   }
8155c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8156c58f1c22SToby Isaac }
8157c58f1c22SToby Isaac 
8158c58f1c22SToby Isaac /*@C
8159c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
8160c58f1c22SToby Isaac 
8161c58f1c22SToby Isaac   Not Collective
8162c58f1c22SToby Isaac 
8163c58f1c22SToby Isaac   Input Parameters:
8164c58f1c22SToby Isaac + dm     - The DM object
8165c58f1c22SToby Isaac . name   - The label name
8166c58f1c22SToby Isaac - output - The flag for output
8167c58f1c22SToby Isaac 
8168c58f1c22SToby Isaac   Level: developer
8169c58f1c22SToby Isaac 
8170c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8171c58f1c22SToby Isaac @*/
8172c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
8173c58f1c22SToby Isaac {
81745d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8175d67d17b1SMatthew G. Knepley   const char    *lname;
8176c58f1c22SToby Isaac   PetscErrorCode ierr;
8177c58f1c22SToby Isaac 
8178c58f1c22SToby Isaac   PetscFunctionBegin;
8179c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8180534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
8181c58f1c22SToby Isaac   while (next) {
8182c58f1c22SToby Isaac     PetscBool flg;
8183c58f1c22SToby Isaac 
8184d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8185d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8186c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
8187c58f1c22SToby Isaac     next = next->next;
8188c58f1c22SToby Isaac   }
8189c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8190c58f1c22SToby Isaac }
8191c58f1c22SToby Isaac 
8192c58f1c22SToby Isaac /*@
8193c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
8194c58f1c22SToby Isaac 
8195d083f849SBarry Smith   Collective on dmA
8196c58f1c22SToby Isaac 
8197d8d19677SJose E. Roman   Input Parameters:
81985d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
81992e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
82005d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
8201ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
8202c58f1c22SToby Isaac 
8203c58f1c22SToby Isaac   Level: intermediate
8204c58f1c22SToby Isaac 
8205c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
8206c58f1c22SToby Isaac 
82075d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
8208c58f1c22SToby Isaac @*/
82095d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
8210c58f1c22SToby Isaac {
8211c58f1c22SToby Isaac   DMLabel        label, labelNew;
8212c58f1c22SToby Isaac   const char    *name;
8213c58f1c22SToby Isaac   PetscBool      flg;
82145d80c0bfSVaclav Hapla   DMLabelLink    link;
82155d80c0bfSVaclav Hapla   PetscErrorCode ierr;
8216c58f1c22SToby Isaac 
82175d80c0bfSVaclav Hapla   PetscFunctionBegin;
82185d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
82195d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
82205d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
82215d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
82225d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
82235d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
82245d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
82255d80c0bfSVaclav Hapla     label=link->label;
82265d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
82275d80c0bfSVaclav Hapla     if (!all) {
8228c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
8229c58f1c22SToby Isaac       if (flg) continue;
82307d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
82317d5acc75SStefano Zampini       if (flg) continue;
8232ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
8233ba2698f1SMatthew G. Knepley       if (flg) continue;
82345d80c0bfSVaclav Hapla     }
82355d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
8236c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
82375d80c0bfSVaclav Hapla     } else {
82385d80c0bfSVaclav Hapla       labelNew = label;
82395d80c0bfSVaclav Hapla     }
8240c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
82415d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
8242c58f1c22SToby Isaac   }
8243c58f1c22SToby Isaac   PetscFunctionReturn(0);
8244c58f1c22SToby Isaac }
8245461a15a0SLisandro Dalcin 
8246461a15a0SLisandro Dalcin PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
8247461a15a0SLisandro Dalcin {
8248461a15a0SLisandro Dalcin   PetscErrorCode ierr;
8249461a15a0SLisandro Dalcin   PetscFunctionBegin;
8250461a15a0SLisandro Dalcin   PetscValidPointer(label,2);
8251461a15a0SLisandro Dalcin   if (!*label) {
8252461a15a0SLisandro Dalcin     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
8253461a15a0SLisandro Dalcin     ierr = DMGetLabel(dm, name, label);CHKERRQ(ierr);
8254461a15a0SLisandro Dalcin   }
8255461a15a0SLisandro Dalcin   ierr = DMLabelSetValue(*label, point, value);CHKERRQ(ierr);
8256461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
8257461a15a0SLisandro Dalcin }
8258461a15a0SLisandro Dalcin 
82590fdc7489SMatthew Knepley /*
82600fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
82610fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
82620fdc7489SMatthew Knepley   (label, id) pair in the DM.
82630fdc7489SMatthew Knepley 
82640fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
82650fdc7489SMatthew Knepley   each label.
82660fdc7489SMatthew Knepley */
82670fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
82680fdc7489SMatthew Knepley {
82690fdc7489SMatthew Knepley   DMUniversalLabel ul;
82700fdc7489SMatthew Knepley   PetscBool       *active;
82710fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
82720fdc7489SMatthew Knepley   PetscErrorCode   ierr;
82730fdc7489SMatthew Knepley 
82740fdc7489SMatthew Knepley   PetscFunctionBegin;
82750fdc7489SMatthew Knepley   ierr = PetscMalloc1(1, &ul);CHKERRQ(ierr);
82760fdc7489SMatthew Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label);CHKERRQ(ierr);
82770fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
82780fdc7489SMatthew Knepley   ierr = PetscCalloc1(Nl, &active);CHKERRQ(ierr);
82790fdc7489SMatthew Knepley   ul->Nl = 0;
82800fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
82810fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
82820fdc7489SMatthew Knepley     const char *name;
82830fdc7489SMatthew Knepley 
82840fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
82850fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "depth", 6, &isdepth);CHKERRQ(ierr);
82860fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "celltype", 9, &iscelltype);CHKERRQ(ierr);
82870fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
82880fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
82890fdc7489SMatthew Knepley   }
82900fdc7489SMatthew 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);
82910fdc7489SMatthew Knepley   ul->Nv = 0;
82920fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
82930fdc7489SMatthew Knepley     DMLabel     label;
82940fdc7489SMatthew Knepley     PetscInt    nv;
82950fdc7489SMatthew Knepley     const char *name;
82960fdc7489SMatthew Knepley 
82970fdc7489SMatthew Knepley     if (!active[l]) continue;
82980fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
82990fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
83000fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
83010fdc7489SMatthew Knepley     ierr = PetscStrallocpy(name, &ul->names[m]);CHKERRQ(ierr);
83020fdc7489SMatthew Knepley     ul->indices[m]   = l;
83030fdc7489SMatthew Knepley     ul->Nv          += nv;
83040fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
83050fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
83060fdc7489SMatthew Knepley     ++m;
83070fdc7489SMatthew Knepley   }
83080fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
83090fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
83100fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
83110fdc7489SMatthew Knepley   }
83120fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
83130fdc7489SMatthew Knepley     PetscInt b;
83140fdc7489SMatthew Knepley 
83150fdc7489SMatthew Knepley     ul->masks[l] = 0;
83160fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
83170fdc7489SMatthew Knepley   }
83180fdc7489SMatthew Knepley   ierr = PetscMalloc1(ul->Nv, &ul->values);CHKERRQ(ierr);
83190fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
83200fdc7489SMatthew Knepley     DMLabel         label;
83210fdc7489SMatthew Knepley     IS              valueIS;
83220fdc7489SMatthew Knepley     const PetscInt *varr;
83230fdc7489SMatthew Knepley     PetscInt        nv, v;
83240fdc7489SMatthew Knepley 
83250fdc7489SMatthew Knepley     if (!active[l]) continue;
83260fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
83270fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
83280fdc7489SMatthew Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
83290fdc7489SMatthew Knepley     ierr = ISGetIndices(valueIS, &varr);CHKERRQ(ierr);
83300fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
83310fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
83320fdc7489SMatthew Knepley     }
83330fdc7489SMatthew Knepley     ierr = ISRestoreIndices(valueIS, &varr);CHKERRQ(ierr);
83340fdc7489SMatthew Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
83350fdc7489SMatthew Knepley     ierr = PetscSortInt(nv, &ul->values[ul->offsets[m]]);CHKERRQ(ierr);
83360fdc7489SMatthew Knepley     ++m;
83370fdc7489SMatthew Knepley   }
83380fdc7489SMatthew Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
83390fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
83400fdc7489SMatthew Knepley     PetscInt  uval = 0;
83410fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
83420fdc7489SMatthew Knepley 
83430fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
83440fdc7489SMatthew Knepley       DMLabel  label;
83450649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
83460fdc7489SMatthew Knepley 
83470fdc7489SMatthew Knepley       if (!active[l]) continue;
83480fdc7489SMatthew Knepley       ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
83490fdc7489SMatthew Knepley       ierr = DMLabelGetValue(label, p, &val);CHKERRQ(ierr);
83500fdc7489SMatthew Knepley       ierr = DMLabelGetDefaultValue(label, &defval);CHKERRQ(ierr);
83510fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
83520649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
83530fdc7489SMatthew Knepley       marked = PETSC_TRUE;
83540fdc7489SMatthew Knepley       ierr = PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc);CHKERRQ(ierr);
83550fdc7489SMatthew Knepley       if (loc < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %D not found in compression array", val);
83560fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
83570fdc7489SMatthew Knepley       ++m;
83580fdc7489SMatthew Knepley     }
83590fdc7489SMatthew Knepley     if (marked) {ierr = DMLabelSetValue(ul->label, p, uval);CHKERRQ(ierr);}
83600fdc7489SMatthew Knepley   }
83610fdc7489SMatthew Knepley   ierr = PetscFree(active);CHKERRQ(ierr);
83620fdc7489SMatthew Knepley   *universal = ul;
83630fdc7489SMatthew Knepley   PetscFunctionReturn(0);
83640fdc7489SMatthew Knepley }
83650fdc7489SMatthew Knepley 
83660fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
83670fdc7489SMatthew Knepley {
83680fdc7489SMatthew Knepley   PetscInt       l;
83690fdc7489SMatthew Knepley   PetscErrorCode ierr;
83700fdc7489SMatthew Knepley 
83710fdc7489SMatthew Knepley   PetscFunctionBegin;
83720fdc7489SMatthew Knepley   for (l = 0; l < (*universal)->Nl; ++l) {ierr = PetscFree((*universal)->names[l]);CHKERRQ(ierr);}
83730fdc7489SMatthew Knepley   ierr = DMLabelDestroy(&(*universal)->label);CHKERRQ(ierr);
83740fdc7489SMatthew Knepley   ierr = PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks);CHKERRQ(ierr);
83750fdc7489SMatthew Knepley   ierr = PetscFree((*universal)->values);CHKERRQ(ierr);
83760fdc7489SMatthew Knepley   ierr = PetscFree(*universal);CHKERRQ(ierr);
83770fdc7489SMatthew Knepley   *universal = NULL;
83780fdc7489SMatthew Knepley   PetscFunctionReturn(0);
83790fdc7489SMatthew Knepley }
83800fdc7489SMatthew Knepley 
83810fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
83820fdc7489SMatthew Knepley {
83830fdc7489SMatthew Knepley   PetscFunctionBegin;
83840fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
83850fdc7489SMatthew Knepley   *ulabel = ul->label;
83860fdc7489SMatthew Knepley   PetscFunctionReturn(0);
83870fdc7489SMatthew Knepley }
83880fdc7489SMatthew Knepley 
83890fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
83900fdc7489SMatthew Knepley {
83910fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
83920fdc7489SMatthew Knepley   PetscErrorCode ierr;
83930fdc7489SMatthew Knepley 
83940fdc7489SMatthew Knepley   PetscFunctionBegin;
8395064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
83960fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
83970fdc7489SMatthew Knepley     if (preserveOrder) {ierr = DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]);CHKERRQ(ierr);}
83980fdc7489SMatthew Knepley     else               {ierr = DMCreateLabel(dm, ul->names[l]);CHKERRQ(ierr);}
83990fdc7489SMatthew Knepley   }
84000fdc7489SMatthew Knepley   if (preserveOrder) {
84010fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
84020fdc7489SMatthew Knepley       const char *name;
84030fdc7489SMatthew Knepley       PetscBool   match;
84040fdc7489SMatthew Knepley 
84050fdc7489SMatthew Knepley       ierr = DMGetLabelName(dm, ul->indices[l], &name);CHKERRQ(ierr);
84060fdc7489SMatthew Knepley       ierr = PetscStrcmp(name, ul->names[l], &match);CHKERRQ(ierr);
84070fdc7489SMatthew 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]);
84080fdc7489SMatthew Knepley     }
84090fdc7489SMatthew Knepley   }
84100fdc7489SMatthew Knepley   PetscFunctionReturn(0);
84110fdc7489SMatthew Knepley }
84120fdc7489SMatthew Knepley 
84130fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
84140fdc7489SMatthew Knepley {
84150fdc7489SMatthew Knepley   PetscInt       l;
84160fdc7489SMatthew Knepley   PetscErrorCode ierr;
84170fdc7489SMatthew Knepley 
84180fdc7489SMatthew Knepley   PetscFunctionBegin;
84190fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
84200fdc7489SMatthew Knepley     DMLabel  label;
84210fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
84220fdc7489SMatthew Knepley 
84230fdc7489SMatthew Knepley     if (lval) {
84240fdc7489SMatthew Knepley       if (useIndex) {ierr = DMGetLabelByNum(dm, ul->indices[l], &label);CHKERRQ(ierr);}
84250fdc7489SMatthew Knepley       else          {ierr = DMGetLabel(dm, ul->names[l], &label);CHKERRQ(ierr);}
84260fdc7489SMatthew Knepley       ierr = DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]);CHKERRQ(ierr);
84270fdc7489SMatthew Knepley     }
84280fdc7489SMatthew Knepley   }
84290fdc7489SMatthew Knepley   PetscFunctionReturn(0);
84300fdc7489SMatthew Knepley }
8431a8fb8f29SToby Isaac 
8432a8fb8f29SToby Isaac /*@
8433a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
8434a8fb8f29SToby Isaac 
8435a8fb8f29SToby Isaac   Input Parameter:
8436a8fb8f29SToby Isaac . dm - The DM object
8437a8fb8f29SToby Isaac 
8438a8fb8f29SToby Isaac   Output Parameter:
8439a8fb8f29SToby Isaac . cdm - The coarse DM
8440a8fb8f29SToby Isaac 
8441a8fb8f29SToby Isaac   Level: intermediate
8442a8fb8f29SToby Isaac 
8443a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
8444a8fb8f29SToby Isaac @*/
8445a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
8446a8fb8f29SToby Isaac {
8447a8fb8f29SToby Isaac   PetscFunctionBegin;
8448a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8449a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
8450a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
8451a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8452a8fb8f29SToby Isaac }
8453a8fb8f29SToby Isaac 
8454a8fb8f29SToby Isaac /*@
8455a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
8456a8fb8f29SToby Isaac 
8457a8fb8f29SToby Isaac   Input Parameters:
8458a8fb8f29SToby Isaac + dm - The DM object
8459a8fb8f29SToby Isaac - cdm - The coarse DM
8460a8fb8f29SToby Isaac 
8461a8fb8f29SToby Isaac   Level: intermediate
8462a8fb8f29SToby Isaac 
8463a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
8464a8fb8f29SToby Isaac @*/
8465a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
8466a8fb8f29SToby Isaac {
8467a8fb8f29SToby Isaac   PetscErrorCode ierr;
8468a8fb8f29SToby Isaac 
8469a8fb8f29SToby Isaac   PetscFunctionBegin;
8470a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8471a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
8472a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
8473a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
8474a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
8475a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8476a8fb8f29SToby Isaac }
8477a8fb8f29SToby Isaac 
847888bdff64SToby Isaac /*@
847988bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
848088bdff64SToby Isaac 
848188bdff64SToby Isaac   Input Parameter:
848288bdff64SToby Isaac . dm - The DM object
848388bdff64SToby Isaac 
848488bdff64SToby Isaac   Output Parameter:
848588bdff64SToby Isaac . fdm - The fine DM
848688bdff64SToby Isaac 
848788bdff64SToby Isaac   Level: intermediate
848888bdff64SToby Isaac 
848988bdff64SToby Isaac .seealso: DMSetFineDM()
849088bdff64SToby Isaac @*/
849188bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
849288bdff64SToby Isaac {
849388bdff64SToby Isaac   PetscFunctionBegin;
849488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
849588bdff64SToby Isaac   PetscValidPointer(fdm, 2);
849688bdff64SToby Isaac   *fdm = dm->fineMesh;
849788bdff64SToby Isaac   PetscFunctionReturn(0);
849888bdff64SToby Isaac }
849988bdff64SToby Isaac 
850088bdff64SToby Isaac /*@
850188bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
850288bdff64SToby Isaac 
850388bdff64SToby Isaac   Input Parameters:
850488bdff64SToby Isaac + dm - The DM object
850588bdff64SToby Isaac - fdm - The fine DM
850688bdff64SToby Isaac 
850788bdff64SToby Isaac   Level: intermediate
850888bdff64SToby Isaac 
850988bdff64SToby Isaac .seealso: DMGetFineDM()
851088bdff64SToby Isaac @*/
851188bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
851288bdff64SToby Isaac {
851388bdff64SToby Isaac   PetscErrorCode ierr;
851488bdff64SToby Isaac 
851588bdff64SToby Isaac   PetscFunctionBegin;
851688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
851788bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
851888bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
851988bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
852088bdff64SToby Isaac   dm->fineMesh = fdm;
852188bdff64SToby Isaac   PetscFunctionReturn(0);
852288bdff64SToby Isaac }
852388bdff64SToby Isaac 
8524a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8525a6ba4734SToby Isaac 
8526a6ba4734SToby Isaac /*@C
8527a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8528a6ba4734SToby Isaac 
8529783e2ec8SMatthew G. Knepley   Collective on dm
8530783e2ec8SMatthew G. Knepley 
8531a6ba4734SToby Isaac   Input Parameters:
85324c258f51SMatthew G. Knepley + dm       - The DM, with a PetscDS that matches the problem being constrained
8533f971fd6bSMatthew G. Knepley . type     - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8534a6ba4734SToby Isaac . name     - The BC name
853545480ffeSMatthew G. Knepley . label    - The label defining constrained points
853645480ffeSMatthew G. Knepley . Nv       - The number of DMLabel values for constrained points
853745480ffeSMatthew G. Knepley . values   - An array of values for constrained points
8538a6ba4734SToby Isaac . field    - The field to constrain
853945480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
8540a6ba4734SToby Isaac . comps    - An array of constrained component numbers
8541a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
854256cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
8543a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
8544a6ba4734SToby Isaac 
854545480ffeSMatthew G. Knepley   Output Parameter:
854645480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
854745480ffeSMatthew G. Knepley 
8548a6ba4734SToby Isaac   Options Database Keys:
8549a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8550a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8551a6ba4734SToby Isaac 
855256cf3b9cSMatthew G. Knepley   Note:
855356cf3b9cSMatthew 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:
855456cf3b9cSMatthew G. Knepley 
855556cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
855656cf3b9cSMatthew G. Knepley 
855756cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
855856cf3b9cSMatthew G. Knepley 
855956cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
856056cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
856156cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
856256cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
856356cf3b9cSMatthew G. Knepley 
856456cf3b9cSMatthew G. Knepley + dim - the spatial dimension
856556cf3b9cSMatthew G. Knepley . Nf - the number of fields
856656cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
856756cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
856856cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
856956cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
857056cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
857156cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
857256cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
857356cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
857456cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
857556cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
857656cf3b9cSMatthew G. Knepley . t - current time
857756cf3b9cSMatthew G. Knepley . x - coordinates of the current point
857856cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
857956cf3b9cSMatthew G. Knepley . constants - constant parameters
858056cf3b9cSMatthew G. Knepley - bcval - output values at the current point
858156cf3b9cSMatthew G. Knepley 
8582a6ba4734SToby Isaac   Level: developer
8583a6ba4734SToby Isaac 
858445480ffeSMatthew G. Knepley .seealso: DSGetBoundary(), PetscDSAddBoundary()
8585a6ba4734SToby Isaac @*/
858645480ffeSMatthew 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)
8587a6ba4734SToby Isaac {
8588e5e52638SMatthew G. Knepley   PetscDS        ds;
8589a6ba4734SToby Isaac   PetscErrorCode ierr;
8590a6ba4734SToby Isaac 
8591a6ba4734SToby Isaac   PetscFunctionBegin;
8592a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8593783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
859445480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
859545480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
859645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
859745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
8598e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
859945480ffeSMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, label);CHKERRQ(ierr);
860045480ffeSMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd);CHKERRQ(ierr);
8601a6ba4734SToby Isaac   PetscFunctionReturn(0);
8602a6ba4734SToby Isaac }
8603a6ba4734SToby Isaac 
860445480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8605e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8606e6f8dbb6SToby Isaac {
8607e5e52638SMatthew G. Knepley   PetscDS        ds;
8608dff059c6SToby Isaac   DMBoundary    *lastnext;
8609e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8610e6f8dbb6SToby Isaac   PetscErrorCode ierr;
8611e6f8dbb6SToby Isaac 
8612e6f8dbb6SToby Isaac   PetscFunctionBegin;
8613e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8614e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
861547a1f5adSToby Isaac   if (dm->boundary) {
861647a1f5adSToby Isaac     DMBoundary next = dm->boundary;
861747a1f5adSToby Isaac 
861847a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
861947a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
862047a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
862147a1f5adSToby Isaac     while (next) {
862247a1f5adSToby Isaac       DMBoundary b = next;
862347a1f5adSToby Isaac 
862447a1f5adSToby Isaac       next = b->next;
862547a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
8626a6ba4734SToby Isaac     }
862747a1f5adSToby Isaac     dm->boundary = NULL;
8628a6ba4734SToby Isaac   }
862947a1f5adSToby Isaac 
8630dff059c6SToby Isaac   lastnext = &(dm->boundary);
8631e6f8dbb6SToby Isaac   while (dsbound) {
8632e6f8dbb6SToby Isaac     DMBoundary dmbound;
8633e6f8dbb6SToby Isaac 
8634e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
8635e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
863645480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
863747a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8638dff059c6SToby Isaac     *lastnext = dmbound;
8639dff059c6SToby Isaac     lastnext = &(dmbound->next);
8640dff059c6SToby Isaac     dsbound = dsbound->next;
8641a6ba4734SToby Isaac   }
8642a6ba4734SToby Isaac   PetscFunctionReturn(0);
8643a6ba4734SToby Isaac }
8644a6ba4734SToby Isaac 
8645a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8646a6ba4734SToby Isaac {
8647b95f2879SToby Isaac   DMBoundary     b;
8648a6ba4734SToby Isaac   PetscErrorCode ierr;
8649a6ba4734SToby Isaac 
8650a6ba4734SToby Isaac   PetscFunctionBegin;
8651a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8652534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8653a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8654e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
8655b95f2879SToby Isaac   b = dm->boundary;
8656a6ba4734SToby Isaac   while (b && !(*isBd)) {
8657e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8658e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8659a6ba4734SToby Isaac     PetscInt   i;
8660a6ba4734SToby Isaac 
866145480ffeSMatthew G. Knepley     if (label) {
866245480ffeSMatthew G. Knepley       for (i = 0; i < dsb->Nv && !(*isBd); ++i) {ierr = DMLabelStratumHasPoint(label, dsb->values[i], point, isBd);CHKERRQ(ierr);}
8663a6ba4734SToby Isaac     }
8664a6ba4734SToby Isaac     b = b->next;
8665a6ba4734SToby Isaac   }
8666a6ba4734SToby Isaac   PetscFunctionReturn(0);
8667a6ba4734SToby Isaac }
86684d6f44ffSToby Isaac 
86694d6f44ffSToby Isaac /*@C
8670a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8671a6e0b375SMatthew G. Knepley 
8672a6e0b375SMatthew G. Knepley   Collective on DM
86734d6f44ffSToby Isaac 
86744d6f44ffSToby Isaac   Input Parameters:
86754d6f44ffSToby Isaac + dm      - The DM
86760709b2feSToby Isaac . time    - The time
86774d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
86784d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
86794d6f44ffSToby Isaac - mode    - The insertion mode for values
86804d6f44ffSToby Isaac 
86814d6f44ffSToby Isaac   Output Parameter:
86824d6f44ffSToby Isaac . X - vector
86834d6f44ffSToby Isaac 
86844d6f44ffSToby Isaac    Calling sequence of func:
86850709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
86864d6f44ffSToby Isaac 
86874d6f44ffSToby Isaac +  dim - The spatial dimension
86888ec8862eSJed Brown .  time - The time at which to sample
86894d6f44ffSToby Isaac .  x   - The coordinates
86904d6f44ffSToby Isaac .  Nf  - The number of fields
86914d6f44ffSToby Isaac .  u   - The output field values
86924d6f44ffSToby Isaac -  ctx - optional user-defined function context
86934d6f44ffSToby Isaac 
86944d6f44ffSToby Isaac   Level: developer
86954d6f44ffSToby Isaac 
8696a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
86974d6f44ffSToby Isaac @*/
86980709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
86994d6f44ffSToby Isaac {
87004d6f44ffSToby Isaac   Vec            localX;
87014d6f44ffSToby Isaac   PetscErrorCode ierr;
87024d6f44ffSToby Isaac 
87034d6f44ffSToby Isaac   PetscFunctionBegin;
87044d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87054d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
87060709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
87074d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
87084d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
87094d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
87104d6f44ffSToby Isaac   PetscFunctionReturn(0);
87114d6f44ffSToby Isaac }
87124d6f44ffSToby Isaac 
8713a6e0b375SMatthew G. Knepley /*@C
8714a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8715a6e0b375SMatthew G. Knepley 
8716a6e0b375SMatthew G. Knepley   Not collective
8717a6e0b375SMatthew G. Knepley 
8718a6e0b375SMatthew G. Knepley   Input Parameters:
8719a6e0b375SMatthew G. Knepley + dm      - The DM
8720a6e0b375SMatthew G. Knepley . time    - The time
8721a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8722a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8723a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8724a6e0b375SMatthew G. Knepley 
8725a6e0b375SMatthew G. Knepley   Output Parameter:
8726a6e0b375SMatthew G. Knepley . localX - vector
8727a6e0b375SMatthew G. Knepley 
8728a6e0b375SMatthew G. Knepley    Calling sequence of func:
8729a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8730a6e0b375SMatthew G. Knepley 
8731a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8732a6e0b375SMatthew G. Knepley .  x   - The coordinates
8733a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8734a6e0b375SMatthew G. Knepley .  u   - The output field values
8735a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8736a6e0b375SMatthew G. Knepley 
8737a6e0b375SMatthew G. Knepley   Level: developer
8738a6e0b375SMatthew G. Knepley 
8739a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8740a6e0b375SMatthew G. Knepley @*/
87410709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
87424d6f44ffSToby Isaac {
87434d6f44ffSToby Isaac   PetscErrorCode ierr;
87444d6f44ffSToby Isaac 
87454d6f44ffSToby Isaac   PetscFunctionBegin;
87464d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8747064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
87480918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
87490709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
87504d6f44ffSToby Isaac   PetscFunctionReturn(0);
87514d6f44ffSToby Isaac }
87524d6f44ffSToby Isaac 
8753a6e0b375SMatthew G. Knepley /*@C
8754a6e0b375SMatthew 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.
8755a6e0b375SMatthew G. Knepley 
8756a6e0b375SMatthew G. Knepley   Collective on DM
8757a6e0b375SMatthew G. Knepley 
8758a6e0b375SMatthew G. Knepley   Input Parameters:
8759a6e0b375SMatthew G. Knepley + dm      - The DM
8760a6e0b375SMatthew G. Knepley . time    - The time
8761a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8762a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8763a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8764a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8765a6e0b375SMatthew G. Knepley 
8766a6e0b375SMatthew G. Knepley   Output Parameter:
8767a6e0b375SMatthew G. Knepley . X - vector
8768a6e0b375SMatthew G. Knepley 
8769a6e0b375SMatthew G. Knepley    Calling sequence of func:
8770a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8771a6e0b375SMatthew G. Knepley 
8772a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8773a6e0b375SMatthew G. Knepley .  x   - The coordinates
8774a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8775a6e0b375SMatthew G. Knepley .  u   - The output field values
8776a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8777a6e0b375SMatthew G. Knepley 
8778a6e0b375SMatthew G. Knepley   Level: developer
8779a6e0b375SMatthew G. Knepley 
8780a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8781a6e0b375SMatthew G. Knepley @*/
87822c53366bSMatthew 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)
87832c53366bSMatthew G. Knepley {
87842c53366bSMatthew G. Knepley   Vec            localX;
87852c53366bSMatthew G. Knepley   PetscErrorCode ierr;
87862c53366bSMatthew G. Knepley 
87872c53366bSMatthew G. Knepley   PetscFunctionBegin;
87882c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87892c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
87902c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
87912c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
87922c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
87932c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
87942c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
87952c53366bSMatthew G. Knepley }
87962c53366bSMatthew G. Knepley 
8797a6e0b375SMatthew G. Knepley /*@C
8798a6e0b375SMatthew 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.
8799a6e0b375SMatthew G. Knepley 
8800a6e0b375SMatthew G. Knepley   Not collective
8801a6e0b375SMatthew G. Knepley 
8802a6e0b375SMatthew G. Knepley   Input Parameters:
8803a6e0b375SMatthew G. Knepley + dm      - The DM
8804a6e0b375SMatthew G. Knepley . time    - The time
8805a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8806a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8807a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8808a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8809a6e0b375SMatthew G. Knepley 
8810a6e0b375SMatthew G. Knepley   Output Parameter:
8811a6e0b375SMatthew G. Knepley . localX - vector
8812a6e0b375SMatthew G. Knepley 
8813a6e0b375SMatthew G. Knepley    Calling sequence of func:
8814a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8815a6e0b375SMatthew G. Knepley 
8816a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8817a6e0b375SMatthew G. Knepley .  x   - The coordinates
8818a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8819a6e0b375SMatthew G. Knepley .  u   - The output field values
8820a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8821a6e0b375SMatthew G. Knepley 
8822a6e0b375SMatthew G. Knepley   Level: developer
8823a6e0b375SMatthew G. Knepley 
8824a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8825a6e0b375SMatthew G. Knepley @*/
88261c531cf8SMatthew 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)
88274d6f44ffSToby Isaac {
88284d6f44ffSToby Isaac   PetscErrorCode ierr;
88294d6f44ffSToby Isaac 
88304d6f44ffSToby Isaac   PetscFunctionBegin;
88314d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8832064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
88330918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
88341c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
88354d6f44ffSToby Isaac   PetscFunctionReturn(0);
88364d6f44ffSToby Isaac }
88372716604bSToby Isaac 
8838a6e0b375SMatthew G. Knepley /*@C
8839a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8840a6e0b375SMatthew G. Knepley 
8841a6e0b375SMatthew G. Knepley   Not collective
8842a6e0b375SMatthew G. Knepley 
8843a6e0b375SMatthew G. Knepley   Input Parameters:
8844a6e0b375SMatthew G. Knepley + dm      - The DM
8845a6e0b375SMatthew G. Knepley . time    - The time
8846a6e0b375SMatthew G. Knepley . localU  - The input field vector
8847a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8848a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8849a6e0b375SMatthew G. Knepley 
8850a6e0b375SMatthew G. Knepley   Output Parameter:
8851a6e0b375SMatthew G. Knepley . localX  - The output vector
8852a6e0b375SMatthew G. Knepley 
8853a6e0b375SMatthew G. Knepley    Calling sequence of func:
8854a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8855a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8856a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8857a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8858a6e0b375SMatthew G. Knepley 
8859a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8860a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8861a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8862a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8863a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8864a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8865a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8866a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8867a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8868a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8869a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8870a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8871a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8872a6e0b375SMatthew G. Knepley .  t            - The current time
8873a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8874a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8875a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8876a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8877a6e0b375SMatthew G. Knepley 
8878a6e0b375SMatthew 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.
8879a6e0b375SMatthew 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
8880a6e0b375SMatthew 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
8881a6e0b375SMatthew 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.
8882a6e0b375SMatthew G. Knepley 
8883a6e0b375SMatthew G. Knepley   Level: intermediate
8884a6e0b375SMatthew G. Knepley 
8885a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8886a6e0b375SMatthew G. Knepley @*/
88878c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
88888c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
88898c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
88908c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8891191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
88928c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
88938c6c5593SMatthew G. Knepley {
88948c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
88958c6c5593SMatthew G. Knepley 
88968c6c5593SMatthew G. Knepley   PetscFunctionBegin;
88978c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
88988c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
88998c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
89000918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
89018c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
89028c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
89038c6c5593SMatthew G. Knepley }
89048c6c5593SMatthew G. Knepley 
8905a6e0b375SMatthew G. Knepley /*@C
8906a6e0b375SMatthew 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.
8907a6e0b375SMatthew G. Knepley 
8908a6e0b375SMatthew G. Knepley   Not collective
8909a6e0b375SMatthew G. Knepley 
8910a6e0b375SMatthew G. Knepley   Input Parameters:
8911a6e0b375SMatthew G. Knepley + dm      - The DM
8912a6e0b375SMatthew G. Knepley . time    - The time
8913a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
8914a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8915a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8916a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8917a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8918a6e0b375SMatthew G. Knepley . localU  - The input field vector
8919a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8920a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8921a6e0b375SMatthew G. Knepley 
8922a6e0b375SMatthew G. Knepley   Output Parameter:
8923a6e0b375SMatthew G. Knepley . localX  - The output vector
8924a6e0b375SMatthew G. Knepley 
8925a6e0b375SMatthew G. Knepley    Calling sequence of func:
8926a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8927a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8928a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8929a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8930a6e0b375SMatthew G. Knepley 
8931a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8932a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8933a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8934a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8935a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8936a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8937a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8938a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8939a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8940a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8941a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8942a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8943a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8944a6e0b375SMatthew G. Knepley .  t            - The current time
8945a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8946a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8947a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8948a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8949a6e0b375SMatthew G. Knepley 
8950a6e0b375SMatthew 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.
8951a6e0b375SMatthew 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
8952a6e0b375SMatthew 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
8953a6e0b375SMatthew 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.
8954a6e0b375SMatthew G. Knepley 
8955a6e0b375SMatthew G. Knepley   Level: intermediate
8956a6e0b375SMatthew G. Knepley 
8957a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8958a6e0b375SMatthew G. Knepley @*/
89591c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
89608c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
89618c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
89628c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8963191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
89648c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
89658c6c5593SMatthew G. Knepley {
89668c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
89678c6c5593SMatthew G. Knepley 
89688c6c5593SMatthew G. Knepley   PetscFunctionBegin;
89698c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8970064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
8971064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
8972ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
89731c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
89748c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
89758c6c5593SMatthew G. Knepley }
89768c6c5593SMatthew G. Knepley 
89772716604bSToby Isaac /*@C
8978ece3a9fcSMatthew 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.
8979ece3a9fcSMatthew G. Knepley 
8980ece3a9fcSMatthew G. Knepley   Not collective
8981ece3a9fcSMatthew G. Knepley 
8982ece3a9fcSMatthew G. Knepley   Input Parameters:
8983ece3a9fcSMatthew G. Knepley + dm      - The DM
8984ece3a9fcSMatthew G. Knepley . time    - The time
8985ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
8986ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8987ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8988ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8989ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8990ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8991ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8992ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8993ece3a9fcSMatthew G. Knepley 
8994ece3a9fcSMatthew G. Knepley   Output Parameter:
8995ece3a9fcSMatthew G. Knepley . localX  - The output vector
8996ece3a9fcSMatthew G. Knepley 
8997ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8998ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8999ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9000ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9001ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9002ece3a9fcSMatthew G. Knepley 
9003ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
9004ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
9005ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9006ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
9007ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9008ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
9009ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9010ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9011ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9012ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9013ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9014ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9015ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9016ece3a9fcSMatthew G. Knepley .  t            - The current time
9017ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
9018ece3a9fcSMatthew G. Knepley .  n            - The face normal
9019ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
9020ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
9021ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
9022ece3a9fcSMatthew G. Knepley 
9023ece3a9fcSMatthew G. Knepley   Note:
9024ece3a9fcSMatthew 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.
9025ece3a9fcSMatthew 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
9026ece3a9fcSMatthew 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
9027ece3a9fcSMatthew 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.
9028ece3a9fcSMatthew G. Knepley 
9029ece3a9fcSMatthew G. Knepley   Level: intermediate
9030ece3a9fcSMatthew G. Knepley 
9031ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9032ece3a9fcSMatthew G. Knepley @*/
9033ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
9034ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
9035ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9036ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9037ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
9038ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
9039ece3a9fcSMatthew G. Knepley {
9040ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
9041ece3a9fcSMatthew G. Knepley 
9042ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
9043ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9044064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9045064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
9046ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
9047ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
9048ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
9049ece3a9fcSMatthew G. Knepley }
9050ece3a9fcSMatthew G. Knepley 
9051ece3a9fcSMatthew G. Knepley /*@C
90522716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
90532716604bSToby Isaac 
90542716604bSToby Isaac   Input Parameters:
90552716604bSToby Isaac + dm    - The DM
90560709b2feSToby Isaac . time  - The time
90572716604bSToby Isaac . funcs - The functions to evaluate for each field component
90582716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9059574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
90602716604bSToby Isaac 
90612716604bSToby Isaac   Output Parameter:
90622716604bSToby Isaac . diff - The diff ||u - u_h||_2
90632716604bSToby Isaac 
90642716604bSToby Isaac   Level: developer
90652716604bSToby Isaac 
90661189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
90672716604bSToby Isaac @*/
90680709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
90692716604bSToby Isaac {
90702716604bSToby Isaac   PetscErrorCode ierr;
90712716604bSToby Isaac 
90722716604bSToby Isaac   PetscFunctionBegin;
90732716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9074b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
90750918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
90760709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
90772716604bSToby Isaac   PetscFunctionReturn(0);
90782716604bSToby Isaac }
9079b698f381SToby Isaac 
9080b698f381SToby Isaac /*@C
9081b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
9082b698f381SToby Isaac 
9083d083f849SBarry Smith   Collective on dm
9084d083f849SBarry Smith 
9085b698f381SToby Isaac   Input Parameters:
9086b698f381SToby Isaac + dm    - The DM
9087b698f381SToby Isaac , time  - The time
9088b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
9089b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9090574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
9091b698f381SToby Isaac - n     - The vector to project along
9092b698f381SToby Isaac 
9093b698f381SToby Isaac   Output Parameter:
9094b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
9095b698f381SToby Isaac 
9096b698f381SToby Isaac   Level: developer
9097b698f381SToby Isaac 
9098b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
9099b698f381SToby Isaac @*/
9100b698f381SToby 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)
9101b698f381SToby Isaac {
9102b698f381SToby Isaac   PetscErrorCode ierr;
9103b698f381SToby Isaac 
9104b698f381SToby Isaac   PetscFunctionBegin;
9105b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9106b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
9107b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
9108b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
9109b698f381SToby Isaac   PetscFunctionReturn(0);
9110b698f381SToby Isaac }
9111b698f381SToby Isaac 
91122a16baeaSToby Isaac /*@C
91132a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
91142a16baeaSToby Isaac 
9115d083f849SBarry Smith   Collective on dm
9116d083f849SBarry Smith 
91172a16baeaSToby Isaac   Input Parameters:
91182a16baeaSToby Isaac + dm    - The DM
91192a16baeaSToby Isaac . time  - The time
91202a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
91212a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9122574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
91232a16baeaSToby Isaac 
91242a16baeaSToby Isaac   Output Parameter:
91252a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
91262a16baeaSToby Isaac 
91272a16baeaSToby Isaac   Level: developer
91282a16baeaSToby Isaac 
91291189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
91302a16baeaSToby Isaac @*/
91311189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
91322a16baeaSToby Isaac {
91332a16baeaSToby Isaac   PetscErrorCode ierr;
91342a16baeaSToby Isaac 
91352a16baeaSToby Isaac   PetscFunctionBegin;
91362a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
91372a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
91380918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
91392a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
91402a16baeaSToby Isaac   PetscFunctionReturn(0);
91412a16baeaSToby Isaac }
91422a16baeaSToby Isaac 
9143df0b854cSToby Isaac /*@C
9144df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
9145cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
9146df0b854cSToby Isaac 
9147df0b854cSToby Isaac   Collective on dm
9148df0b854cSToby Isaac 
9149df0b854cSToby Isaac   Input parameters:
9150df0b854cSToby Isaac + dm - the pre-adaptation DM object
9151a1b0c543SToby Isaac - label - label with the flags
9152df0b854cSToby Isaac 
9153df0b854cSToby Isaac   Output parameters:
91540d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
9155df0b854cSToby Isaac 
9156df0b854cSToby Isaac   Level: intermediate
91570d1cd5e0SMatthew G. Knepley 
91580d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
9159df0b854cSToby Isaac @*/
91600d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
9161df0b854cSToby Isaac {
9162df0b854cSToby Isaac   PetscErrorCode ierr;
9163df0b854cSToby Isaac 
9164df0b854cSToby Isaac   PetscFunctionBegin;
9165df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9166a1b0c543SToby Isaac   PetscValidPointer(label,2);
91670d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
91680d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
91696f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
91700d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
9171a587d139SMark   if (*dmAdapt) {
9172a587d139SMark     (*dmAdapt)->prealloc_only = dm->prealloc_only;  /* maybe this should go .... */
9173a587d139SMark     ierr = PetscFree((*dmAdapt)->vectype);CHKERRQ(ierr);
9174a587d139SMark     ierr = PetscStrallocpy(dm->vectype,(char**)&(*dmAdapt)->vectype);CHKERRQ(ierr);
9175a587d139SMark     ierr = PetscFree((*dmAdapt)->mattype);CHKERRQ(ierr);
9176a587d139SMark     ierr = PetscStrallocpy(dm->mattype,(char**)&(*dmAdapt)->mattype);CHKERRQ(ierr);
9177a587d139SMark   }
91780d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
91790d1cd5e0SMatthew G. Knepley }
91800d1cd5e0SMatthew G. Knepley 
91810d1cd5e0SMatthew G. Knepley /*@C
91820d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
91830d1cd5e0SMatthew G. Knepley 
91840d1cd5e0SMatthew G. Knepley   Input Parameters:
91850d1cd5e0SMatthew G. Knepley + dm - The DM object
91860d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
91876f25b0d8SLisandro 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_".
91880d1cd5e0SMatthew G. Knepley 
91890d1cd5e0SMatthew G. Knepley   Output Parameter:
91900d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
91910d1cd5e0SMatthew G. Knepley 
91920d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
91930d1cd5e0SMatthew G. Knepley 
91940d1cd5e0SMatthew G. Knepley   Level: advanced
91950d1cd5e0SMatthew G. Knepley 
91960d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
91970d1cd5e0SMatthew G. Knepley @*/
91980d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
91990d1cd5e0SMatthew G. Knepley {
92000d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
92010d1cd5e0SMatthew G. Knepley 
92020d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
92030d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92040d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
92050d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
92060d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
92076f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
92086f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
92090d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
9210df0b854cSToby Isaac   PetscFunctionReturn(0);
9211df0b854cSToby Isaac }
9212c4088d22SMatthew G. Knepley 
9213502a2867SDave May /*@C
9214502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
9215502a2867SDave May 
9216502a2867SDave May  Not Collective
9217502a2867SDave May 
9218502a2867SDave May  Input Parameter:
9219502a2867SDave May .  dm    - The DM
9220502a2867SDave May 
92210a19bb7dSprj-  Output Parameters:
92220a19bb7dSprj- +  nranks - the number of neighbours
92230a19bb7dSprj- -  ranks - the neighbors ranks
9224502a2867SDave May 
9225502a2867SDave May  Notes:
9226502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
9227502a2867SDave May 
9228502a2867SDave May  Level: beginner
9229502a2867SDave May 
9230dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
9231502a2867SDave May @*/
9232502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
9233502a2867SDave May {
9234502a2867SDave May   PetscErrorCode ierr;
9235502a2867SDave May 
9236502a2867SDave May   PetscFunctionBegin;
9237502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
92380918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
9239502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
9240502a2867SDave May   PetscFunctionReturn(0);
9241502a2867SDave May }
9242502a2867SDave May 
9243531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
9244531c7667SBarry Smith 
9245531c7667SBarry Smith /*
9246531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
9247531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
9248531c7667SBarry Smith */
9249531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
9250531c7667SBarry Smith {
9251531c7667SBarry Smith   PetscErrorCode ierr;
9252531c7667SBarry Smith 
9253531c7667SBarry Smith   PetscFunctionBegin;
9254531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9255531c7667SBarry Smith     Vec x1local;
9256531c7667SBarry Smith     DM  dm;
9257531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9258531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
9259531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
9260531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9261531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9262531c7667SBarry Smith     x1   = x1local;
9263531c7667SBarry Smith   }
9264531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
9265531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9266531c7667SBarry Smith     DM  dm;
9267531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9268531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
9269531c7667SBarry Smith   }
9270531c7667SBarry Smith   PetscFunctionReturn(0);
9271531c7667SBarry Smith }
9272531c7667SBarry Smith 
9273531c7667SBarry Smith /*@
9274531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
9275531c7667SBarry Smith 
9276531c7667SBarry Smith     Input Parameter:
9277531c7667SBarry Smith .    coloring - the MatFDColoring object
9278531c7667SBarry Smith 
927995452b02SPatrick Sanan     Developer Notes:
928095452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
9281531c7667SBarry Smith 
92821b266c99SBarry Smith     Level: advanced
92831b266c99SBarry Smith 
9284531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
9285531c7667SBarry Smith @*/
9286531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
9287531c7667SBarry Smith {
9288531c7667SBarry Smith   PetscFunctionBegin;
9289531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
9290531c7667SBarry Smith   PetscFunctionReturn(0);
9291531c7667SBarry Smith }
92928320bc6fSPatrick Sanan 
92938320bc6fSPatrick Sanan /*@
92948320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
92958320bc6fSPatrick Sanan 
92968320bc6fSPatrick Sanan     Collective
92978320bc6fSPatrick Sanan 
92988320bc6fSPatrick Sanan     Input Parameters:
9299a5bc1bf3SBarry Smith +    dm1 - the first DM
93008320bc6fSPatrick Sanan -    dm2 - the second DM
93018320bc6fSPatrick Sanan 
93028320bc6fSPatrick Sanan     Output Parameters:
93038320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
93048320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
93058320bc6fSPatrick Sanan 
93068320bc6fSPatrick Sanan     Notes:
93078320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
93083d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
93098320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
93103d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
93113d862458SPatrick Sanan     decomposition, but hold different data.
93128320bc6fSPatrick Sanan 
93138320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
93148320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
93158320bc6fSPatrick Sanan 
93168320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
93178320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
93188320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
93198320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
93208320bc6fSPatrick Sanan 
93218320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
93228320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
93238320bc6fSPatrick Sanan .vb
93248320bc6fSPatrick Sanan   ...
93258320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
93268320bc6fSPatrick Sanan   if (set && compatible)  {
93278320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
93288320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
93293d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
93308320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
93318320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
93328320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
93338320bc6fSPatrick Sanan       }
93348320bc6fSPatrick Sanan     }
93358320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
93368320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
93378320bc6fSPatrick Sanan   } else {
93388320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
93398320bc6fSPatrick Sanan   }
93408320bc6fSPatrick Sanan   ...
93418320bc6fSPatrick Sanan .ve
93428320bc6fSPatrick Sanan 
93438320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
93448320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
93458320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
93468320bc6fSPatrick Sanan     always check the "set" output parameter.
93478320bc6fSPatrick Sanan 
93488320bc6fSPatrick Sanan     A DM is always compatible with itself.
93498320bc6fSPatrick Sanan 
93508320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
93518320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
93528320bc6fSPatrick Sanan     incompatible.
93538320bc6fSPatrick Sanan 
93548320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
93558320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
93568320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
93578320bc6fSPatrick Sanan 
93588320bc6fSPatrick Sanan     Developer Notes:
93593d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
93603d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
9361a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
93628320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
93638320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
93643d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
93653d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
93668320bc6fSPatrick Sanan 
93678320bc6fSPatrick Sanan     Level: advanced
93688320bc6fSPatrick Sanan 
93693d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
93708320bc6fSPatrick Sanan @*/
93718320bc6fSPatrick Sanan 
9372a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
93738320bc6fSPatrick Sanan {
93748320bc6fSPatrick Sanan   PetscErrorCode ierr;
93758320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
93768320bc6fSPatrick Sanan   DMType         type,type2;
93778320bc6fSPatrick Sanan   PetscBool      sameType;
93788320bc6fSPatrick Sanan 
93798320bc6fSPatrick Sanan   PetscFunctionBegin;
9380a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
93818320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
93828320bc6fSPatrick Sanan 
93838320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
9384a5bc1bf3SBarry Smith   if (dm1 == dm2) {
93858320bc6fSPatrick Sanan     *set = PETSC_TRUE;
93868320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
93878320bc6fSPatrick Sanan     PetscFunctionReturn(0);
93888320bc6fSPatrick Sanan   }
93898320bc6fSPatrick Sanan 
93908320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
93918320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
93928320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
93938320bc6fSPatrick Sanan      determined by the implementation-specific logic */
9394ffc4695bSBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRMPI(ierr);
93958320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
93968320bc6fSPatrick Sanan     *set = PETSC_TRUE;
93978320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
93988320bc6fSPatrick Sanan     PetscFunctionReturn(0);
93998320bc6fSPatrick Sanan   }
94008320bc6fSPatrick Sanan 
94018320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
9402a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
9403a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
9404b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
94058320bc6fSPatrick Sanan   }
94068320bc6fSPatrick Sanan 
9407a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
94088320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
9409a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
94108320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
94118320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
94128320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
9413a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
94148320bc6fSPatrick Sanan   } else {
94158320bc6fSPatrick Sanan     *set = PETSC_FALSE;
94168320bc6fSPatrick Sanan   }
94178320bc6fSPatrick Sanan   PetscFunctionReturn(0);
94188320bc6fSPatrick Sanan }
9419c0f0dcc3SMatthew G. Knepley 
9420c0f0dcc3SMatthew G. Knepley /*@C
9421c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
9422c0f0dcc3SMatthew G. Knepley 
9423c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9424c0f0dcc3SMatthew G. Knepley 
9425c0f0dcc3SMatthew G. Knepley   Input Parameters:
9426c0f0dcc3SMatthew G. Knepley + DM - the DM
9427c0f0dcc3SMatthew G. Knepley . f - the monitor function
9428c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
9429c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
9430c0f0dcc3SMatthew G. Knepley 
9431c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9432c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
9433c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
9434c0f0dcc3SMatthew G. Knepley 
9435c0f0dcc3SMatthew G. Knepley   Notes:
9436c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
9437c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
9438c0f0dcc3SMatthew G. Knepley   order in which they were set.
9439c0f0dcc3SMatthew G. Knepley 
9440c0f0dcc3SMatthew G. Knepley   Fortran Notes:
9441c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
9442c0f0dcc3SMatthew G. Knepley 
9443c0f0dcc3SMatthew G. Knepley   Level: intermediate
9444c0f0dcc3SMatthew G. Knepley 
9445c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
9446c0f0dcc3SMatthew G. Knepley @*/
9447c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
9448c0f0dcc3SMatthew G. Knepley {
9449c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9450c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9451c0f0dcc3SMatthew G. Knepley 
9452c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9453c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9454c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9455c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9456c0f0dcc3SMatthew G. Knepley 
9457c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
9458c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9459c0f0dcc3SMatthew G. Knepley   }
9460c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9461c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9462c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9463c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9464c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9465c0f0dcc3SMatthew G. Knepley }
9466c0f0dcc3SMatthew G. Knepley 
9467c0f0dcc3SMatthew G. Knepley /*@
9468c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9469c0f0dcc3SMatthew G. Knepley 
9470c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9471c0f0dcc3SMatthew G. Knepley 
9472c0f0dcc3SMatthew G. Knepley   Input Parameter:
9473c0f0dcc3SMatthew G. Knepley . dm - the DM
9474c0f0dcc3SMatthew G. Knepley 
9475c0f0dcc3SMatthew G. Knepley   Options Database Key:
9476c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9477c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9478c0f0dcc3SMatthew G. Knepley   set via the options database
9479c0f0dcc3SMatthew G. Knepley 
9480c0f0dcc3SMatthew G. Knepley   Notes:
9481c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9482c0f0dcc3SMatthew G. Knepley 
9483c0f0dcc3SMatthew G. Knepley   Level: intermediate
9484c0f0dcc3SMatthew G. Knepley 
9485c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9486c0f0dcc3SMatthew G. Knepley @*/
9487c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9488c0f0dcc3SMatthew G. Knepley {
9489c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9490c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9491c0f0dcc3SMatthew G. Knepley 
9492c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9493c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9494c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9495c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
9496c0f0dcc3SMatthew G. Knepley   }
9497c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9498c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9499c0f0dcc3SMatthew G. Knepley }
9500c0f0dcc3SMatthew G. Knepley 
9501c0f0dcc3SMatthew G. Knepley /*@C
9502c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9503c0f0dcc3SMatthew G. Knepley 
9504c0f0dcc3SMatthew G. Knepley   Collective on DM
9505c0f0dcc3SMatthew G. Knepley 
9506c0f0dcc3SMatthew G. Knepley   Input Parameters:
9507c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9508c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9509c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9510c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9511c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9512c0f0dcc3SMatthew 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
9513c0f0dcc3SMatthew G. Knepley 
9514c0f0dcc3SMatthew G. Knepley   Output Parameter:
9515c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9516c0f0dcc3SMatthew G. Knepley 
9517c0f0dcc3SMatthew G. Knepley   Level: developer
9518c0f0dcc3SMatthew G. Knepley 
9519c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9520c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9521c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9522c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9523c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9524c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9525c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9526c0f0dcc3SMatthew G. Knepley @*/
9527c0f0dcc3SMatthew 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)
9528c0f0dcc3SMatthew G. Knepley {
9529c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9530c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9531c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
9532c0f0dcc3SMatthew G. Knepley 
9533c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9534c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9535c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
9536c0f0dcc3SMatthew G. Knepley   if (*flg) {
9537c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9538c0f0dcc3SMatthew G. Knepley 
9539c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
9540c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
9541c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
9542c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
9543c0f0dcc3SMatthew G. Knepley   }
9544c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9545c0f0dcc3SMatthew G. Knepley }
9546c0f0dcc3SMatthew G. Knepley 
9547c0f0dcc3SMatthew G. Knepley /*@
9548c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9549c0f0dcc3SMatthew G. Knepley 
9550c0f0dcc3SMatthew G. Knepley    Collective on DM
9551c0f0dcc3SMatthew G. Knepley 
9552c0f0dcc3SMatthew G. Knepley    Input Parameters:
9553c0f0dcc3SMatthew G. Knepley .  dm - The DM
9554c0f0dcc3SMatthew G. Knepley 
9555c0f0dcc3SMatthew G. Knepley    Level: developer
9556c0f0dcc3SMatthew G. Knepley 
9557c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9558c0f0dcc3SMatthew G. Knepley @*/
9559c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9560c0f0dcc3SMatthew G. Knepley {
9561c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9562c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9563c0f0dcc3SMatthew G. Knepley 
9564c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9565c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9566c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9567c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9568c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
9569c0f0dcc3SMatthew G. Knepley   }
9570c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9571c0f0dcc3SMatthew G. Knepley }
95722e4af2aeSMatthew G. Knepley 
95732e4af2aeSMatthew G. Knepley /*@
95742e4af2aeSMatthew G. Knepley   DMComputeError - Computes the error assuming the user has given exact solution functions
95752e4af2aeSMatthew G. Knepley 
95762e4af2aeSMatthew G. Knepley   Collective on DM
95772e4af2aeSMatthew G. Knepley 
95782e4af2aeSMatthew G. Knepley   Input Parameters:
95792e4af2aeSMatthew G. Knepley + dm     - The DM
95806b867d5aSJose E. Roman - sol    - The solution vector
95812e4af2aeSMatthew G. Knepley 
95826b867d5aSJose E. Roman   Input/Output Parameter:
95836b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
95846b867d5aSJose E. Roman            contains the error in each field
95856b867d5aSJose E. Roman 
95866b867d5aSJose E. Roman   Output Parameter:
95876b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
95882e4af2aeSMatthew G. Knepley 
95892e4af2aeSMatthew G. Knepley   Note: The exact solutions come from the PetscDS object, and the time comes from DMGetOutputSequenceNumber().
95902e4af2aeSMatthew G. Knepley 
95912e4af2aeSMatthew G. Knepley   Level: developer
95922e4af2aeSMatthew G. Knepley 
95932e4af2aeSMatthew G. Knepley .seealso: DMMonitorSet(), DMGetRegionNumDS(), PetscDSGetExactSolution(), DMGetOutputSequenceNumber()
95942e4af2aeSMatthew G. Knepley @*/
95952e4af2aeSMatthew G. Knepley PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
95962e4af2aeSMatthew G. Knepley {
95972e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
95982e4af2aeSMatthew G. Knepley   void            **ctxs;
95992e4af2aeSMatthew G. Knepley   PetscReal         time;
96002e4af2aeSMatthew G. Knepley   PetscInt          Nf, f, Nds, s;
96012e4af2aeSMatthew G. Knepley   PetscErrorCode    ierr;
96022e4af2aeSMatthew G. Knepley 
96032e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
96042e4af2aeSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
96052e4af2aeSMatthew G. Knepley   ierr = PetscCalloc2(Nf, &exactSol, Nf, &ctxs);CHKERRQ(ierr);
96062e4af2aeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
96072e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
96082e4af2aeSMatthew G. Knepley     PetscDS         ds;
96092e4af2aeSMatthew G. Knepley     DMLabel         label;
96102e4af2aeSMatthew G. Knepley     IS              fieldIS;
96112e4af2aeSMatthew G. Knepley     const PetscInt *fields;
96122e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
96132e4af2aeSMatthew G. Knepley 
96142e4af2aeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
96152e4af2aeSMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
96162e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);}
96172e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
96182e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
96192e4af2aeSMatthew G. Knepley       ierr = PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]);CHKERRQ(ierr);
96202e4af2aeSMatthew G. Knepley     }
96212e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);}
96222e4af2aeSMatthew G. Knepley   }
96232e4af2aeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
96242e4af2aeSMatthew 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);
96252e4af2aeSMatthew G. Knepley   }
96262e4af2aeSMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
96272e4af2aeSMatthew G. Knepley   if (errors) {ierr = DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors);CHKERRQ(ierr);}
96282e4af2aeSMatthew G. Knepley   if (errorVec) {
96292e4af2aeSMatthew G. Knepley     DM             edm;
96302e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
96312e4af2aeSMatthew G. Knepley     PetscBool      simplex;
96322e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
96332e4af2aeSMatthew G. Knepley 
96342e4af2aeSMatthew G. Knepley     ierr = DMClone(dm, &edm);CHKERRQ(ierr);
96352e4af2aeSMatthew G. Knepley     ierr = DMGetDimension(edm, &dim);CHKERRQ(ierr);
96362e4af2aeSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr);
96372e4af2aeSMatthew G. Knepley     ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
96382e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
96392e4af2aeSMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
96402e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
96412e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
96422e4af2aeSMatthew G. Knepley       PetscQuadrature q;
96432e4af2aeSMatthew G. Knepley       const char     *name;
96442e4af2aeSMatthew G. Knepley 
96452e4af2aeSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
96462e4af2aeSMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe);CHKERRQ(ierr);
96472e4af2aeSMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
96482e4af2aeSMatthew G. Knepley       ierr = PetscObjectSetName((PetscObject) efe, name);CHKERRQ(ierr);
96492e4af2aeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
96502e4af2aeSMatthew G. Knepley       ierr = PetscFESetQuadrature(efe, q);CHKERRQ(ierr);
96512e4af2aeSMatthew G. Knepley       ierr = DMSetField(edm, f, NULL, (PetscObject) efe);CHKERRQ(ierr);
96522e4af2aeSMatthew G. Knepley       ierr = PetscFEDestroy(&efe);CHKERRQ(ierr);
96532e4af2aeSMatthew G. Knepley     }
96542e4af2aeSMatthew G. Knepley     ierr = DMCreateDS(edm);CHKERRQ(ierr);
96552e4af2aeSMatthew G. Knepley 
96561e1ea65dSPierre Jolivet     ierr = DMCreateGlobalVector(edm, errorVec);CHKERRQ(ierr);
96572e4af2aeSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) *errorVec, "Error");CHKERRQ(ierr);
96582e4af2aeSMatthew G. Knepley     ierr = DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec);CHKERRQ(ierr);
96592e4af2aeSMatthew G. Knepley     ierr = DMDestroy(&edm);CHKERRQ(ierr);
96602e4af2aeSMatthew G. Knepley   }
96612e4af2aeSMatthew G. Knepley   ierr = PetscFree2(exactSol, ctxs);CHKERRQ(ierr);
96622e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
96632e4af2aeSMatthew G. Knepley }
96649a2a23afSMatthew G. Knepley 
96659a2a23afSMatthew G. Knepley /*@
96669a2a23afSMatthew G. Knepley   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this DM
96679a2a23afSMatthew G. Knepley 
96689a2a23afSMatthew G. Knepley   Not collective
96699a2a23afSMatthew G. Knepley 
96709a2a23afSMatthew G. Knepley   Input Parameter:
96719a2a23afSMatthew G. Knepley . dm     - The DM
96729a2a23afSMatthew G. Knepley 
96739a2a23afSMatthew G. Knepley   Output Parameter:
9674a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
96759a2a23afSMatthew G. Knepley 
96769a2a23afSMatthew G. Knepley   Level: advanced
96779a2a23afSMatthew G. Knepley 
96789a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryLabels(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
96799a2a23afSMatthew G. Knepley @*/
96809a2a23afSMatthew G. Knepley PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
96819a2a23afSMatthew G. Knepley {
96829a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
96839a2a23afSMatthew G. Knepley 
96849a2a23afSMatthew G. Knepley   PetscFunctionBegin;
96859a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
96869a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetSize(dm->auxData, numAux);CHKERRQ(ierr);
96879a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
96889a2a23afSMatthew G. Knepley }
96899a2a23afSMatthew G. Knepley 
96909a2a23afSMatthew G. Knepley /*@
96919a2a23afSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value
96929a2a23afSMatthew G. Knepley 
96939a2a23afSMatthew G. Knepley   Not collective
96949a2a23afSMatthew G. Knepley 
96959a2a23afSMatthew G. Knepley   Input Parameters:
96969a2a23afSMatthew G. Knepley + dm     - The DM
96979a2a23afSMatthew G. Knepley . label  - The DMLabel
96989a2a23afSMatthew G. Knepley - value  - The label value indicating the region
96999a2a23afSMatthew G. Knepley 
97009a2a23afSMatthew G. Knepley   Output Parameter:
97019a2a23afSMatthew G. Knepley . aux    - The Vec holding auxiliary field data
97029a2a23afSMatthew G. Knepley 
970304c51a94SMatthew G. Knepley   Note: If no auxiliary vector is found for this (label, value), (NULL, 0) is checked as well.
970404c51a94SMatthew G. Knepley 
97059a2a23afSMatthew G. Knepley   Level: advanced
97069a2a23afSMatthew G. Knepley 
97079a2a23afSMatthew G. Knepley .seealso: DMSetAuxiliaryVec(), DMGetNumAuxiliaryVec()
97089a2a23afSMatthew G. Knepley @*/
97099a2a23afSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, Vec *aux)
97109a2a23afSMatthew G. Knepley {
971104c51a94SMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0};
971204c51a94SMatthew G. Knepley   PetscBool       has;
97139a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
97149a2a23afSMatthew G. Knepley 
97159a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97169a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97179a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
97189a2a23afSMatthew G. Knepley   key.label = label;
97199a2a23afSMatthew G. Knepley   key.value = value;
972004c51a94SMatthew G. Knepley   ierr = PetscHMapAuxHas(dm->auxData, key, &has);CHKERRQ(ierr);
972104c51a94SMatthew G. Knepley   if (has) {ierr = PetscHMapAuxGet(dm->auxData, key,  aux);CHKERRQ(ierr);}
972204c51a94SMatthew G. Knepley   else     {ierr = PetscHMapAuxGet(dm->auxData, wild, aux);CHKERRQ(ierr);}
97239a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
97249a2a23afSMatthew G. Knepley }
97259a2a23afSMatthew G. Knepley 
97269a2a23afSMatthew G. Knepley /*@
97279a2a23afSMatthew G. Knepley   DMSetAuxiliaryVec - Set the auxiliary vector for region specified by the given label and value
97289a2a23afSMatthew G. Knepley 
97299a2a23afSMatthew G. Knepley   Not collective
97309a2a23afSMatthew G. Knepley 
97319a2a23afSMatthew G. Knepley   Input Parameters:
97329a2a23afSMatthew G. Knepley + dm     - The DM
97339a2a23afSMatthew G. Knepley . label  - The DMLabel
97349a2a23afSMatthew G. Knepley . value  - The label value indicating the region
97359a2a23afSMatthew G. Knepley - aux    - The Vec holding auxiliary field data
97369a2a23afSMatthew G. Knepley 
97379a2a23afSMatthew G. Knepley   Level: advanced
97389a2a23afSMatthew G. Knepley 
97399a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryVec()
97409a2a23afSMatthew G. Knepley @*/
97419a2a23afSMatthew G. Knepley PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, Vec aux)
97429a2a23afSMatthew G. Knepley {
97439a2a23afSMatthew G. Knepley   Vec             old;
97449a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
97459a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
97469a2a23afSMatthew G. Knepley 
97479a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97489a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97499a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
97509a2a23afSMatthew G. Knepley   key.label = label;
97519a2a23afSMatthew G. Knepley   key.value = value;
97529a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGet(dm->auxData, key, &old);CHKERRQ(ierr);
97539a2a23afSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) aux);CHKERRQ(ierr);
97549a2a23afSMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) old);CHKERRQ(ierr);
97559a2a23afSMatthew G. Knepley   if (!aux) {ierr = PetscHMapAuxDel(dm->auxData, key);CHKERRQ(ierr);}
97569a2a23afSMatthew G. Knepley   else      {ierr = PetscHMapAuxSet(dm->auxData, key, aux);CHKERRQ(ierr);}
97579a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
97589a2a23afSMatthew G. Knepley }
97599a2a23afSMatthew G. Knepley 
97609a2a23afSMatthew G. Knepley /*@C
97619a2a23afSMatthew G. Knepley   DMGetAuxiliaryLabels - Get the labels and values for all auxiliary vectors in this DM
97629a2a23afSMatthew G. Knepley 
97639a2a23afSMatthew G. Knepley   Not collective
97649a2a23afSMatthew G. Knepley 
97659a2a23afSMatthew G. Knepley   Input Parameter:
97669a2a23afSMatthew G. Knepley . dm      - The DM
97679a2a23afSMatthew G. Knepley 
97689a2a23afSMatthew G. Knepley   Output Parameters:
97699a2a23afSMatthew G. Knepley + labels  - The DMLabels for each Vec
97709a2a23afSMatthew G. Knepley - values  - The label values for each Vec
97719a2a23afSMatthew G. Knepley 
97729a2a23afSMatthew G. Knepley   Note: The arrays passed in must be at least as large as DMGetNumAuxiliaryVec().
97739a2a23afSMatthew G. Knepley 
97749a2a23afSMatthew G. Knepley   Level: advanced
97759a2a23afSMatthew G. Knepley 
97769a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
97779a2a23afSMatthew G. Knepley @*/
97789a2a23afSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[])
97799a2a23afSMatthew G. Knepley {
97809a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
97819a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
97829a2a23afSMatthew G. Knepley   PetscErrorCode   ierr;
97839a2a23afSMatthew G. Knepley 
97849a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97859a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97869a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
97879a2a23afSMatthew G. Knepley   PetscValidPointer(values, 3);
97889a2a23afSMatthew G. Knepley   ierr = DMGetNumAuxiliaryVec(dm, &n);CHKERRQ(ierr);
97899a2a23afSMatthew G. Knepley   ierr = PetscMalloc1(n, &keys);CHKERRQ(ierr);
97909a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetKeys(dm->auxData, &off, keys);CHKERRQ(ierr);
97919a2a23afSMatthew G. Knepley   for (i = 0; i < n; ++i) {labels[i] = keys[i].label; values[i] = keys[i].value;}
97929a2a23afSMatthew G. Knepley   ierr = PetscFree(keys);CHKERRQ(ierr);
97939a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
97949a2a23afSMatthew G. Knepley }
97959a2a23afSMatthew G. Knepley 
97969a2a23afSMatthew G. Knepley /*@
97979a2a23afSMatthew G. Knepley   DMCopyAuxiliaryVec - Copy the auxiliary data to a new DM
97989a2a23afSMatthew G. Knepley 
97999a2a23afSMatthew G. Knepley   Not collective
98009a2a23afSMatthew G. Knepley 
98019a2a23afSMatthew G. Knepley   Input Parameter:
98029a2a23afSMatthew G. Knepley . dm    - The DM
98039a2a23afSMatthew G. Knepley 
98049a2a23afSMatthew G. Knepley   Output Parameter:
98059a2a23afSMatthew G. Knepley . dmNew - The new DM, now with the same auxiliary data
98069a2a23afSMatthew G. Knepley 
98079a2a23afSMatthew G. Knepley   Level: advanced
98089a2a23afSMatthew G. Knepley 
98099a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
98109a2a23afSMatthew G. Knepley @*/
98119a2a23afSMatthew G. Knepley PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
98129a2a23afSMatthew G. Knepley {
98139a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
98149a2a23afSMatthew G. Knepley 
98159a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98169a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98179a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDestroy(&dmNew->auxData);CHKERRQ(ierr);
98189a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData);CHKERRQ(ierr);
98199a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98209a2a23afSMatthew G. Knepley }
9821b5a892a1SMatthew G. Knepley 
9822b5a892a1SMatthew G. Knepley /*@C
9823b5a892a1SMatthew G. Knepley   DMPolytopeMatchOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
9824b5a892a1SMatthew G. Knepley 
9825b5a892a1SMatthew G. Knepley   Not collective
9826b5a892a1SMatthew G. Knepley 
9827b5a892a1SMatthew G. Knepley   Input Parameters:
9828b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9829b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9830b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9831b5a892a1SMatthew G. Knepley 
9832b5a892a1SMatthew G. Knepley   Output Parameters:
9833b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
9834b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9835b5a892a1SMatthew G. Knepley 
9836b5a892a1SMatthew G. Knepley   Level: advanced
9837b5a892a1SMatthew G. Knepley 
9838b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchVertexOrientation()
9839b5a892a1SMatthew G. Knepley @*/
9840b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9841b5a892a1SMatthew G. Knepley {
9842b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9843b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
9844b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9845b5a892a1SMatthew G. Knepley 
9846b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9847b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
9848b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9849b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9850b5a892a1SMatthew G. Knepley 
9851b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceCone[arr[c*2]] != targetCone[c]) break;
9852b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
9853b5a892a1SMatthew G. Knepley   }
9854b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9855b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9856b5a892a1SMatthew G. Knepley }
9857b5a892a1SMatthew G. Knepley 
9858b5a892a1SMatthew G. Knepley /*@C
9859b5a892a1SMatthew G. Knepley   DMPolytopeGetOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
9860b5a892a1SMatthew G. Knepley 
9861b5a892a1SMatthew G. Knepley   Not collective
9862b5a892a1SMatthew G. Knepley 
9863b5a892a1SMatthew G. Knepley   Input Parameters:
9864b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9865b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9866b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9867b5a892a1SMatthew G. Knepley 
9868b5a892a1SMatthew G. Knepley   Output Parameters:
9869b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
9870b5a892a1SMatthew G. Knepley 
9871b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
9872b5a892a1SMatthew G. Knepley 
9873b5a892a1SMatthew G. Knepley   Level: advanced
9874b5a892a1SMatthew G. Knepley 
9875b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchOrientation(), DMPolytopeGetVertexOrientation()
9876b5a892a1SMatthew G. Knepley @*/
9877b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9878b5a892a1SMatthew G. Knepley {
9879b5a892a1SMatthew G. Knepley   PetscBool      found;
9880b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
9881b5a892a1SMatthew G. Knepley 
9882b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9883b5a892a1SMatthew G. Knepley   ierr = DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found);CHKERRQ(ierr);
9884b5a892a1SMatthew G. Knepley   if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9885b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9886b5a892a1SMatthew G. Knepley }
9887b5a892a1SMatthew G. Knepley 
9888b5a892a1SMatthew G. Knepley /*@C
9889b5a892a1SMatthew G. Knepley   DMPolytopeMatchVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
9890b5a892a1SMatthew G. Knepley 
9891b5a892a1SMatthew G. Knepley   Not collective
9892b5a892a1SMatthew G. Knepley 
9893b5a892a1SMatthew G. Knepley   Input Parameters:
9894b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9895b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9896b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9897b5a892a1SMatthew G. Knepley 
9898b5a892a1SMatthew G. Knepley   Output Parameters:
9899b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
9900b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9901b5a892a1SMatthew G. Knepley 
9902b5a892a1SMatthew G. Knepley   Level: advanced
9903b5a892a1SMatthew G. Knepley 
9904b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchOrientation()
9905b5a892a1SMatthew G. Knepley @*/
9906b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9907b5a892a1SMatthew G. Knepley {
9908b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9909b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
9910b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9911b5a892a1SMatthew G. Knepley 
9912b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9913b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
9914b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9915b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9916b5a892a1SMatthew G. Knepley 
9917b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceVert[arr[c]] != targetVert[c]) break;
9918b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
9919b5a892a1SMatthew G. Knepley   }
9920b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9921b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9922b5a892a1SMatthew G. Knepley }
9923b5a892a1SMatthew G. Knepley 
9924b5a892a1SMatthew G. Knepley /*@C
9925b5a892a1SMatthew G. Knepley   DMPolytopeGetVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
9926b5a892a1SMatthew G. Knepley 
9927b5a892a1SMatthew G. Knepley   Not collective
9928b5a892a1SMatthew G. Knepley 
9929b5a892a1SMatthew G. Knepley   Input Parameters:
9930b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9931b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9932b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9933b5a892a1SMatthew G. Knepley 
9934b5a892a1SMatthew G. Knepley   Output Parameters:
9935b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
9936b5a892a1SMatthew G. Knepley 
9937b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
9938b5a892a1SMatthew G. Knepley 
9939b5a892a1SMatthew G. Knepley   Level: advanced
9940b5a892a1SMatthew G. Knepley 
9941b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchVertexOrientation(), DMPolytopeGetOrientation()
9942b5a892a1SMatthew G. Knepley @*/
9943b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9944b5a892a1SMatthew G. Knepley {
9945b5a892a1SMatthew G. Knepley   PetscBool      found;
9946b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
9947b5a892a1SMatthew G. Knepley 
9948b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9949b5a892a1SMatthew G. Knepley   ierr = DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found);CHKERRQ(ierr);
9950b5a892a1SMatthew G. Knepley   if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9951b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9952b5a892a1SMatthew G. Knepley }
9953012bc364SMatthew G. Knepley 
9954012bc364SMatthew G. Knepley /*@C
9955012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9956012bc364SMatthew G. Knepley 
9957012bc364SMatthew G. Knepley   Not collective
9958012bc364SMatthew G. Knepley 
9959012bc364SMatthew G. Knepley   Input Parameters:
9960012bc364SMatthew G. Knepley + ct    - The DMPolytopeType
9961012bc364SMatthew G. Knepley - point - Coordinates of the point
9962012bc364SMatthew G. Knepley 
9963012bc364SMatthew G. Knepley   Output Parameters:
9964012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
9965012bc364SMatthew G. Knepley 
9966012bc364SMatthew G. Knepley   Level: advanced
9967012bc364SMatthew G. Knepley 
9968012bc364SMatthew G. Knepley .seealso: DMLocatePoints()
9969012bc364SMatthew G. Knepley @*/
9970012bc364SMatthew G. Knepley PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9971012bc364SMatthew G. Knepley {
9972012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9973012bc364SMatthew G. Knepley   PetscInt  d;
9974012bc364SMatthew G. Knepley 
9975012bc364SMatthew G. Knepley   PetscFunctionBegin;
9976012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9977012bc364SMatthew G. Knepley   switch (ct) {
9978012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9979012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9980012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
9981012bc364SMatthew G. Knepley       if (point[d] < -1.0) {*inside = PETSC_FALSE; break;}
9982012bc364SMatthew G. Knepley       sum += point[d];
9983012bc364SMatthew G. Knepley     }
9984012bc364SMatthew G. Knepley     if (sum > PETSC_SMALL) {*inside = PETSC_FALSE; break;}
9985012bc364SMatthew G. Knepley     break;
9986012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9987012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9988012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
9989012bc364SMatthew G. Knepley       if (PetscAbsReal(point[d]) > 1.+PETSC_SMALL) {*inside = PETSC_FALSE; break;}
9990012bc364SMatthew G. Knepley     break;
9991012bc364SMatthew G. Knepley   default:
9992012bc364SMatthew G. Knepley     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9993012bc364SMatthew G. Knepley   }
9994012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
9995012bc364SMatthew G. Knepley }
9996