xref: /petsc/src/dm/interface/dm.c (revision 4f37162b5bb948fc035f35445fadb032839d86d4)
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};
252cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace","keep","fail","DMCopyLabelsMode","DM_COPY_LABELS_", NULL};
2660c22052SBarry Smith 
27a4121054SBarry Smith /*@
28de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
29a4121054SBarry Smith 
30a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
31a4121054SBarry Smith    error when you try to use the vector.
32a4121054SBarry Smith 
33d083f849SBarry Smith   Collective
34a4121054SBarry Smith 
35a4121054SBarry Smith   Input Parameter:
36a4121054SBarry Smith . comm - The communicator for the DM object
37a4121054SBarry Smith 
38a4121054SBarry Smith   Output Parameter:
39a4121054SBarry Smith . dm - The DM object
40a4121054SBarry Smith 
41a4121054SBarry Smith   Level: beginner
42a4121054SBarry Smith 
438472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
44a4121054SBarry Smith @*/
457087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
46a4121054SBarry Smith {
47a4121054SBarry Smith   DM             v;
48e5e52638SMatthew G. Knepley   PetscDS        ds;
49a4121054SBarry Smith   PetscErrorCode ierr;
50a4121054SBarry Smith 
51a4121054SBarry Smith   PetscFunctionBegin;
521411c6eeSJed Brown   PetscValidPointer(dm,2);
530298fd71SBarry Smith   *dm = NULL;
54607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
55a4121054SBarry Smith 
5673107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
57e7c4fc90SDmitry Karpeev 
5849be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
5949be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
600298fd71SBarry Smith   v->ltogmap                  = NULL;
611411c6eeSJed Brown   v->bs                       = 1;
62171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
6388ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
641bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
65c58f1c22SToby Isaac   v->labels                   = NULL;
6634aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
6734aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
68c58f1c22SToby Isaac   v->depthLabel               = NULL;
69ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
701bb6d2a8SBarry Smith   v->localSection             = NULL;
711bb6d2a8SBarry Smith   v->globalSection            = NULL;
72fba222abSToby Isaac   v->defaultConstraintSection = NULL;
73fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
74c6b900c6SMatthew G. Knepley   v->L                        = NULL;
75c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
765dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
779a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7896173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
79435a35e8SMatthew G Knepley   {
80435a35e8SMatthew G Knepley     PetscInt i;
81435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
820298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
83f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
84435a35e8SMatthew G Knepley     }
85435a35e8SMatthew G Knepley   }
8645480ffeSMatthew G. Knepley   ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
87b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
88e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
899a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxCreate(&v->auxData);CHKERRQ(ierr);
9014f150ffSMatthew G. Knepley   v->dmBC = NULL;
91a8fb8f29SToby Isaac   v->coarseMesh = NULL;
92f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
93cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
94c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
95b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
964a7a4c06SLawrence Mitchell 
971411c6eeSJed Brown   *dm = v;
98a4121054SBarry Smith   PetscFunctionReturn(0);
99a4121054SBarry Smith }
100a4121054SBarry Smith 
10138221697SMatthew G. Knepley /*@
10238221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
10338221697SMatthew G. Knepley 
104d083f849SBarry Smith   Collective
10538221697SMatthew G. Knepley 
10638221697SMatthew G. Knepley   Input Parameter:
10738221697SMatthew G. Knepley . dm - The original DM object
10838221697SMatthew G. Knepley 
10938221697SMatthew G. Knepley   Output Parameter:
11038221697SMatthew G. Knepley . newdm  - The new DM object
11138221697SMatthew G. Knepley 
11238221697SMatthew G. Knepley   Level: beginner
11338221697SMatthew G. Knepley 
1141cb8cacdSPatrick Sanan   Notes:
1151cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1161cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1171cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1181bb6d2a8SBarry Smith 
11989706ed2SPatrick Sanan   The clone is considered set up iff the original is.
12089706ed2SPatrick Sanan 
12192cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1221bb6d2a8SBarry Smith 
12338221697SMatthew G. Knepley @*/
12438221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
12538221697SMatthew G. Knepley {
12638221697SMatthew G. Knepley   PetscSF        sf;
12738221697SMatthew G. Knepley   Vec            coords;
12838221697SMatthew G. Knepley   void          *ctx;
129a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
13038221697SMatthew G. Knepley   PetscErrorCode ierr;
13138221697SMatthew G. Knepley 
13238221697SMatthew G. Knepley   PetscFunctionBegin;
13338221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13438221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
13538221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1362cbb9b06SVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL);CHKERRQ(ierr);
137ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
138ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
139c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
140a587d139SMark   ierr = PetscFree((*newdm)->vectype);CHKERRQ(ierr);
141a587d139SMark   ierr = PetscStrallocpy(dm->vectype,(char**)&(*newdm)->vectype);CHKERRQ(ierr);
142a587d139SMark   ierr = PetscFree((*newdm)->mattype);CHKERRQ(ierr);
143a587d139SMark   ierr = PetscStrallocpy(dm->mattype,(char**)&(*newdm)->mattype);CHKERRQ(ierr);
1441de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1451de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
14638221697SMatthew G. Knepley   if (dm->ops->clone) {
14738221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
14838221697SMatthew G. Knepley   }
1493f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
15038221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
15138221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
15238221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
15338221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
154be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
155be4c1c3eSMatthew G. Knepley     DM           ncdm;
156be4c1c3eSMatthew G. Knepley     PetscSection cs;
1575a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
158be4c1c3eSMatthew G. Knepley 
15992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
160be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
161ffc4695bSBarry Smith     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
1625a0206caSToby Isaac     if (pEndMax >= 0) {
163be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
16483bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
16592fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
166a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
167be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
168be4c1c3eSMatthew G. Knepley     }
169be4c1c3eSMatthew G. Knepley   }
170a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
171a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
17238221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
17338221697SMatthew G. Knepley   if (coords) {
17438221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
17538221697SMatthew G. Knepley   } else {
17638221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
17738221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
17838221697SMatthew G. Knepley   }
17990b157c4SStefano Zampini   {
18090b157c4SStefano Zampini     PetscBool             isper;
181c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1825dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
18390b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
18490b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
185c6b900c6SMatthew G. Knepley   }
18634aa8a36SMatthew G. Knepley   {
18734aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
18834aa8a36SMatthew G. Knepley 
18934aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
19034aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
19134aa8a36SMatthew G. Knepley   }
19238221697SMatthew G. Knepley   PetscFunctionReturn(0);
19338221697SMatthew G. Knepley }
19438221697SMatthew G. Knepley 
1959a42bb27SBarry Smith /*@C
196564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1979a42bb27SBarry Smith 
198d083f849SBarry Smith    Logically Collective on da
1999a42bb27SBarry Smith 
2009a42bb27SBarry Smith    Input Parameter:
2019a42bb27SBarry Smith +  da - initial distributed array
202e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
2039a42bb27SBarry Smith 
2049a42bb27SBarry Smith    Options Database:
205dd85299cSBarry Smith .   -dm_vec_type ctype
2069a42bb27SBarry Smith 
2079a42bb27SBarry Smith    Level: intermediate
2089a42bb27SBarry Smith 
209a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
2109a42bb27SBarry Smith @*/
21119fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2129a42bb27SBarry Smith {
2139a42bb27SBarry Smith   PetscErrorCode ierr;
2149a42bb27SBarry Smith 
2159a42bb27SBarry Smith   PetscFunctionBegin;
2169a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2179a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
21819fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2199a42bb27SBarry Smith   PetscFunctionReturn(0);
2209a42bb27SBarry Smith }
2219a42bb27SBarry Smith 
222c0dedaeaSBarry Smith /*@C
223c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
224c0dedaeaSBarry Smith 
225d083f849SBarry Smith    Logically Collective on da
226c0dedaeaSBarry Smith 
227c0dedaeaSBarry Smith    Input Parameter:
228c0dedaeaSBarry Smith .  da - initial distributed array
229c0dedaeaSBarry Smith 
230c0dedaeaSBarry Smith    Output Parameter:
231c0dedaeaSBarry Smith .  ctype - the vector type
232c0dedaeaSBarry Smith 
233c0dedaeaSBarry Smith    Level: intermediate
234c0dedaeaSBarry Smith 
235a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
236c0dedaeaSBarry Smith @*/
237c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
238c0dedaeaSBarry Smith {
239c0dedaeaSBarry Smith   PetscFunctionBegin;
240c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
241c0dedaeaSBarry Smith   *ctype = da->vectype;
242c0dedaeaSBarry Smith   PetscFunctionReturn(0);
243c0dedaeaSBarry Smith }
244c0dedaeaSBarry Smith 
2455f1ad066SMatthew G Knepley /*@
24634f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2475f1ad066SMatthew G Knepley 
2485f1ad066SMatthew G Knepley   Not collective
2495f1ad066SMatthew G Knepley 
2505f1ad066SMatthew G Knepley   Input Parameter:
2515f1ad066SMatthew G Knepley . v - The Vec
2525f1ad066SMatthew G Knepley 
2535f1ad066SMatthew G Knepley   Output Parameter:
2545f1ad066SMatthew G Knepley . dm - The DM
2555f1ad066SMatthew G Knepley 
2565f1ad066SMatthew G Knepley   Level: intermediate
2575f1ad066SMatthew G Knepley 
2585f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2595f1ad066SMatthew G Knepley @*/
2605f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2615f1ad066SMatthew G Knepley {
2625f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley   PetscFunctionBegin;
2655f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2665f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2675f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2685f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2695f1ad066SMatthew G Knepley }
2705f1ad066SMatthew G Knepley 
2715f1ad066SMatthew G Knepley /*@
272d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2735f1ad066SMatthew G Knepley 
2745f1ad066SMatthew G Knepley   Not collective
2755f1ad066SMatthew G Knepley 
2765f1ad066SMatthew G Knepley   Input Parameters:
2775f1ad066SMatthew G Knepley + v - The Vec
2785f1ad066SMatthew G Knepley - dm - The DM
2795f1ad066SMatthew G Knepley 
280d9805387SMatthew G. Knepley   Note: This is NOT the same as DMCreateGlobalVector() since it does not change the view methods or perform other customization, but merely sets the DM member.
281d9805387SMatthew G. Knepley 
2825f1ad066SMatthew G Knepley   Level: intermediate
2835f1ad066SMatthew G Knepley 
2845f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2855f1ad066SMatthew G Knepley @*/
2865f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2875f1ad066SMatthew G Knepley {
2885f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2895f1ad066SMatthew G Knepley 
2905f1ad066SMatthew G Knepley   PetscFunctionBegin;
2915f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
292d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2935f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2945f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2955f1ad066SMatthew G Knepley }
2965f1ad066SMatthew G Knepley 
297521d9a4cSLisandro Dalcin /*@C
2988f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2998f1509bcSBarry Smith 
300d083f849SBarry Smith    Logically Collective on dm
3018f1509bcSBarry Smith 
3028f1509bcSBarry Smith    Input Parameters:
3038f1509bcSBarry Smith +  dm - the DM context
3048f1509bcSBarry Smith -  ctype - the matrix type
3058f1509bcSBarry Smith 
3068f1509bcSBarry Smith    Options Database:
3078f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3088f1509bcSBarry Smith 
3098f1509bcSBarry Smith    Level: intermediate
3108f1509bcSBarry Smith 
3118f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3128f1509bcSBarry Smith           DMGetISColoringType()
3138f1509bcSBarry Smith @*/
3148f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3158f1509bcSBarry Smith {
3168f1509bcSBarry Smith   PetscFunctionBegin;
3178f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3188f1509bcSBarry Smith   dm->coloringtype = ctype;
3198f1509bcSBarry Smith   PetscFunctionReturn(0);
3208f1509bcSBarry Smith }
3218f1509bcSBarry Smith 
3228f1509bcSBarry Smith /*@C
3238f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
324521d9a4cSLisandro Dalcin 
325d083f849SBarry Smith    Logically Collective on dm
326521d9a4cSLisandro Dalcin 
327521d9a4cSLisandro Dalcin    Input Parameter:
3288f1509bcSBarry Smith .  dm - the DM context
3298f1509bcSBarry Smith 
3308f1509bcSBarry Smith    Output Parameter:
3318f1509bcSBarry Smith .  ctype - the matrix type
3328f1509bcSBarry Smith 
3338f1509bcSBarry Smith    Options Database:
3348f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3358f1509bcSBarry Smith 
3368f1509bcSBarry Smith    Level: intermediate
3378f1509bcSBarry Smith 
3388f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3398f1509bcSBarry Smith           DMGetISColoringType()
3408f1509bcSBarry Smith @*/
3418f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3428f1509bcSBarry Smith {
3438f1509bcSBarry Smith   PetscFunctionBegin;
3448f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3458f1509bcSBarry Smith   *ctype = dm->coloringtype;
3468f1509bcSBarry Smith   PetscFunctionReturn(0);
3478f1509bcSBarry Smith }
3488f1509bcSBarry Smith 
3498f1509bcSBarry Smith /*@C
3508f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3518f1509bcSBarry Smith 
352d083f849SBarry Smith    Logically Collective on dm
3538f1509bcSBarry Smith 
3548f1509bcSBarry Smith    Input Parameters:
355521d9a4cSLisandro Dalcin +  dm - the DM context
356a2b5a043SBarry Smith -  ctype - the matrix type
357521d9a4cSLisandro Dalcin 
358521d9a4cSLisandro Dalcin    Options Database:
359521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
360521d9a4cSLisandro Dalcin 
361521d9a4cSLisandro Dalcin    Level: intermediate
362521d9a4cSLisandro Dalcin 
363a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
364521d9a4cSLisandro Dalcin @*/
36519fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
366521d9a4cSLisandro Dalcin {
367521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
36888f0584fSBarry Smith 
369521d9a4cSLisandro Dalcin   PetscFunctionBegin;
370521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
371521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
37219fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
373521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
374521d9a4cSLisandro Dalcin }
375521d9a4cSLisandro Dalcin 
376c0dedaeaSBarry Smith /*@C
377c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
378c0dedaeaSBarry Smith 
379d083f849SBarry Smith    Logically Collective on dm
380c0dedaeaSBarry Smith 
381c0dedaeaSBarry Smith    Input Parameter:
382c0dedaeaSBarry Smith .  dm - the DM context
383c0dedaeaSBarry Smith 
384c0dedaeaSBarry Smith    Output Parameter:
385c0dedaeaSBarry Smith .  ctype - the matrix type
386c0dedaeaSBarry Smith 
387c0dedaeaSBarry Smith    Options Database:
388c0dedaeaSBarry Smith .   -dm_mat_type ctype
389c0dedaeaSBarry Smith 
390c0dedaeaSBarry Smith    Level: intermediate
391c0dedaeaSBarry Smith 
392a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
393c0dedaeaSBarry Smith @*/
394c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
395c0dedaeaSBarry Smith {
396c0dedaeaSBarry Smith   PetscFunctionBegin;
397c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
398c0dedaeaSBarry Smith   *ctype = dm->mattype;
399c0dedaeaSBarry Smith   PetscFunctionReturn(0);
400c0dedaeaSBarry Smith }
401c0dedaeaSBarry Smith 
402c688c046SMatthew G Knepley /*@
40334f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
404c688c046SMatthew G Knepley 
405c688c046SMatthew G Knepley   Not collective
406c688c046SMatthew G Knepley 
407c688c046SMatthew G Knepley   Input Parameter:
408c688c046SMatthew G Knepley . A - The Mat
409c688c046SMatthew G Knepley 
410c688c046SMatthew G Knepley   Output Parameter:
411c688c046SMatthew G Knepley . dm - The DM
412c688c046SMatthew G Knepley 
413c688c046SMatthew G Knepley   Level: intermediate
414c688c046SMatthew G Knepley 
4158f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4168f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4178f1509bcSBarry Smith 
418c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
419c688c046SMatthew G Knepley @*/
420c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
421c688c046SMatthew G Knepley {
422c688c046SMatthew G Knepley   PetscErrorCode ierr;
423c688c046SMatthew G Knepley 
424c688c046SMatthew G Knepley   PetscFunctionBegin;
425c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
426c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
427c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
428c688c046SMatthew G Knepley   PetscFunctionReturn(0);
429c688c046SMatthew G Knepley }
430c688c046SMatthew G Knepley 
431c688c046SMatthew G Knepley /*@
432c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
433c688c046SMatthew G Knepley 
434c688c046SMatthew G Knepley   Not collective
435c688c046SMatthew G Knepley 
436c688c046SMatthew G Knepley   Input Parameters:
437c688c046SMatthew G Knepley + A - The Mat
438c688c046SMatthew G Knepley - dm - The DM
439c688c046SMatthew G Knepley 
440c688c046SMatthew G Knepley   Level: intermediate
441c688c046SMatthew G Knepley 
4428f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4438f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4448f1509bcSBarry Smith 
445c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
446c688c046SMatthew G Knepley @*/
447c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
448c688c046SMatthew G Knepley {
449c688c046SMatthew G Knepley   PetscErrorCode ierr;
450c688c046SMatthew G Knepley 
451c688c046SMatthew G Knepley   PetscFunctionBegin;
452c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4538865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
454c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
455c688c046SMatthew G Knepley   PetscFunctionReturn(0);
456c688c046SMatthew G Knepley }
457c688c046SMatthew G Knepley 
4589a42bb27SBarry Smith /*@C
4599a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4606757b960SDave May    DM options in the database.
4619a42bb27SBarry Smith 
462d083f849SBarry Smith    Logically Collective on dm
4639a42bb27SBarry Smith 
464d8d19677SJose E. Roman    Input Parameters:
4658353ddbbSDave May +  da - the DM context
4669a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4679a42bb27SBarry Smith 
4689a42bb27SBarry Smith    Notes:
4699a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4709a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4719a42bb27SBarry Smith 
4729a42bb27SBarry Smith    Level: advanced
4739a42bb27SBarry Smith 
4749a42bb27SBarry Smith .seealso: DMSetFromOptions()
4759a42bb27SBarry Smith @*/
4767087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4779a42bb27SBarry Smith {
4789a42bb27SBarry Smith   PetscErrorCode ierr;
4799a42bb27SBarry Smith 
4809a42bb27SBarry Smith   PetscFunctionBegin;
4819a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4829a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
483691be533SLawrence Mitchell   if (dm->sf) {
484691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
485691be533SLawrence Mitchell   }
4861bb6d2a8SBarry Smith   if (dm->sectionSF) {
4871bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
488691be533SLawrence Mitchell   }
4899a42bb27SBarry Smith   PetscFunctionReturn(0);
4909a42bb27SBarry Smith }
4919a42bb27SBarry Smith 
49231697293SDave May /*@C
49331697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
49431697293SDave May    DM options in the database.
49531697293SDave May 
496d083f849SBarry Smith    Logically Collective on dm
49731697293SDave May 
49831697293SDave May    Input Parameters:
49931697293SDave May +  dm - the DM context
50031697293SDave May -  prefix - the prefix string to prepend to all DM option requests
50131697293SDave May 
50231697293SDave May    Notes:
50331697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
50431697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
50531697293SDave May 
50631697293SDave May    Level: advanced
50731697293SDave May 
50831697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
50931697293SDave May @*/
51031697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
51131697293SDave May {
51231697293SDave May   PetscErrorCode ierr;
51331697293SDave May 
51431697293SDave May   PetscFunctionBegin;
51531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
51631697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
51731697293SDave May   PetscFunctionReturn(0);
51831697293SDave May }
51931697293SDave May 
52031697293SDave May /*@C
52131697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
52231697293SDave May    DM options in the database.
52331697293SDave May 
52431697293SDave May    Not Collective
52531697293SDave May 
52631697293SDave May    Input Parameters:
52731697293SDave May .  dm - the DM context
52831697293SDave May 
52931697293SDave May    Output Parameters:
53031697293SDave May .  prefix - pointer to the prefix string used is returned
53131697293SDave May 
53295452b02SPatrick Sanan    Notes:
53395452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
53431697293SDave May    sufficient length to hold the prefix.
53531697293SDave May 
53631697293SDave May    Level: advanced
53731697293SDave May 
53831697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
53931697293SDave May @*/
54031697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
54131697293SDave May {
54231697293SDave May   PetscErrorCode ierr;
54331697293SDave May 
54431697293SDave May   PetscFunctionBegin;
54531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54631697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
54731697293SDave May   PetscFunctionReturn(0);
54831697293SDave May }
54931697293SDave May 
55088bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
55188bdff64SToby Isaac {
5526eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
55388bdff64SToby Isaac   PetscErrorCode ierr;
55488bdff64SToby Isaac 
55588bdff64SToby Isaac   PetscFunctionBegin;
556aab5bcd8SJed Brown   *ncrefct = 0;
55788bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55888bdff64SToby Isaac     refct--;
55988bdff64SToby Isaac     if (recurseCoarse) {
56088bdff64SToby Isaac       PetscInt coarseCount;
56188bdff64SToby Isaac 
56288bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
56388bdff64SToby Isaac       refct += coarseCount;
56488bdff64SToby Isaac     }
56588bdff64SToby Isaac   }
56688bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56788bdff64SToby Isaac     refct--;
56888bdff64SToby Isaac     if (recurseFine) {
56988bdff64SToby Isaac       PetscInt fineCount;
57088bdff64SToby Isaac 
57188bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
57288bdff64SToby Isaac       refct += fineCount;
57388bdff64SToby Isaac     }
57488bdff64SToby Isaac   }
57588bdff64SToby Isaac   *ncrefct = refct;
57688bdff64SToby Isaac   PetscFunctionReturn(0);
57788bdff64SToby Isaac }
57888bdff64SToby Isaac 
579f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
580354557abSToby Isaac {
5815d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
582354557abSToby Isaac   PetscErrorCode ierr;
583354557abSToby Isaac 
584354557abSToby Isaac   PetscFunctionBegin;
585354557abSToby Isaac   /* destroy the labels */
586354557abSToby Isaac   while (next) {
587354557abSToby Isaac     DMLabelLink tmp = next->next;
588354557abSToby Isaac 
5895d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
590ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
591354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
592354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
593354557abSToby Isaac     next = tmp;
594354557abSToby Isaac   }
5955d80c0bfSVaclav Hapla   dm->labels = NULL;
596354557abSToby Isaac   PetscFunctionReturn(0);
597354557abSToby Isaac }
598354557abSToby Isaac 
5991fb7b255SJunchao Zhang /*@C
6008472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
60147c6ae99SBarry Smith 
602d083f849SBarry Smith     Collective on dm
60347c6ae99SBarry Smith 
60447c6ae99SBarry Smith     Input Parameter:
60547c6ae99SBarry Smith .   dm - the DM object to destroy
60647c6ae99SBarry Smith 
60747c6ae99SBarry Smith     Level: developer
60847c6ae99SBarry Smith 
609e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
61047c6ae99SBarry Smith 
61147c6ae99SBarry Smith @*/
612fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
61347c6ae99SBarry Smith {
6146eb26441SStefano Zampini   PetscInt       cnt;
615dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61647c6ae99SBarry Smith   PetscErrorCode ierr;
61747c6ae99SBarry Smith 
61847c6ae99SBarry Smith   PetscFunctionBegin;
6196bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6206bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
62187e657c6SBarry Smith 
62288bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
62388bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
62488bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
625ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6266bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6276bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6286eb26441SStefano Zampini 
6296eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6306eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6316eb26441SStefano Zampini 
632f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6330298fd71SBarry Smith   (*dm)->namedglobal = NULL;
634f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6352348bcf4SPeter Brune     nnext = nlink->next;
6362348bcf4SPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6372348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6382348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6392348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6402348bcf4SPeter Brune   }
641f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6420298fd71SBarry Smith   (*dm)->namedlocal = NULL;
643f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
644f490541aSPeter Brune     nnext = nlink->next;
645f490541aSPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
646f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
647f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
648f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
649f490541aSPeter Brune   }
6502348bcf4SPeter Brune 
651b17ce1afSJed Brown   /* Destroy the list of hooks */
652c833c3b5SJed Brown   {
653c833c3b5SJed Brown     DMCoarsenHookLink link,next;
654b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
655b17ce1afSJed Brown       next = link->next;
656b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
657b17ce1afSJed Brown     }
6580298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
659c833c3b5SJed Brown   }
660c833c3b5SJed Brown   {
661c833c3b5SJed Brown     DMRefineHookLink link,next;
662c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
663c833c3b5SJed Brown       next = link->next;
664c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
665c833c3b5SJed Brown     }
6660298fd71SBarry Smith     (*dm)->refinehook = NULL;
667c833c3b5SJed Brown   }
668be081cd6SPeter Brune   {
669be081cd6SPeter Brune     DMSubDomainHookLink link,next;
670be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
671be081cd6SPeter Brune       next = link->next;
672be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
673be081cd6SPeter Brune     }
6740298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
675be081cd6SPeter Brune   }
676baf369e7SPeter Brune   {
677baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
678baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
679baf369e7SPeter Brune       next = link->next;
680baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
681baf369e7SPeter Brune     }
6820298fd71SBarry Smith     (*dm)->gtolhook = NULL;
683baf369e7SPeter Brune   }
684d4d07f1eSToby Isaac   {
685d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
686d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
687d4d07f1eSToby Isaac       next = link->next;
688d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
689d4d07f1eSToby Isaac     }
690d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
691d4d07f1eSToby Isaac   }
692aa1993deSMatthew G Knepley   /* Destroy the work arrays */
693aa1993deSMatthew G Knepley   {
694aa1993deSMatthew G Knepley     DMWorkLink link,next;
695aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
696aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
697aa1993deSMatthew G Knepley       next = link->next;
698aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
699aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
700aa1993deSMatthew G Knepley     }
7010298fd71SBarry Smith     (*dm)->workin = NULL;
702aa1993deSMatthew G Knepley   }
703c58f1c22SToby Isaac   /* destroy the labels */
704f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
705f4cdcedcSVaclav Hapla   /* destroy the fields */
706e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
707f4cdcedcSVaclav Hapla   /* destroy the boundaries */
708e6f8dbb6SToby Isaac   {
709e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
710e6f8dbb6SToby Isaac     while (next) {
711e6f8dbb6SToby Isaac       DMBoundary b = next;
712e6f8dbb6SToby Isaac 
713e6f8dbb6SToby Isaac       next = b->next;
714e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
715e6f8dbb6SToby Isaac     }
716e6f8dbb6SToby Isaac   }
717b17ce1afSJed Brown 
71852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
71952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
72052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
72152536dc3SBarry Smith 
7221a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7231a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7241a266240SBarry Smith   }
72571cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7266bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7276bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
728073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
72988ed4aceSMatthew G Knepley 
7301bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7311bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7328b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
733fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
734fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
73588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7361bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
737736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
738736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7398e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
740736995cdSBlaise Bourdin     }
741736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
742736995cdSBlaise Bourdin   }
7439a2a23afSMatthew G. Knepley   {
7449a2a23afSMatthew G. Knepley     Vec     *auxData;
7459a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7469a2a23afSMatthew G. Knepley 
7479a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxGetSize((*dm)->auxData, &n);CHKERRQ(ierr);
7489a2a23afSMatthew G. Knepley     ierr = PetscMalloc1(n, &auxData);CHKERRQ(ierr);
7499a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxGetVals((*dm)->auxData, &off, auxData);CHKERRQ(ierr);
7509a2a23afSMatthew G. Knepley     for (i = 0; i < n; ++i) {ierr = VecDestroy(&auxData[i]);CHKERRQ(ierr);}
7519a2a23afSMatthew G. Knepley     ierr = PetscFree(auxData);CHKERRQ(ierr);
7529a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxDestroy(&(*dm)->auxData);CHKERRQ(ierr);
7539a2a23afSMatthew G. Knepley   }
75488bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
75588bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
75688bdff64SToby Isaac   }
7576eb26441SStefano Zampini 
758a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
75988bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
76088bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
76188bdff64SToby Isaac   }
76288bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
763f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7646636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7656636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7666636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
767412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
768412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
769412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
770ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
771ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
772ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7736636e97aSMatthew G Knepley 
774e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
77514f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
776e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
777e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
778732e2eb9SMatthew G Knepley 
779ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7806bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
781ed3c66a1SDave May   }
782c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
783f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
784f918ec44SMatthew G. Knepley   ierr = CeedElemRestrictionDestroy(&(*dm)->ceedERestrict);CHKERRQ(ierr);
785f918ec44SMatthew G. Knepley   ierr = CeedDestroy(&(*dm)->ceed);CHKERRQ(ierr);
786f918ec44SMatthew G. Knepley #endif
787435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
788732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
78947c6ae99SBarry Smith   PetscFunctionReturn(0);
79047c6ae99SBarry Smith }
79147c6ae99SBarry Smith 
792d7bf68aeSBarry Smith /*@
793d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
794d7bf68aeSBarry Smith 
795d083f849SBarry Smith     Collective on dm
796d7bf68aeSBarry Smith 
797d7bf68aeSBarry Smith     Input Parameter:
798d7bf68aeSBarry Smith .   dm - the DM object to setup
799d7bf68aeSBarry Smith 
800d7bf68aeSBarry Smith     Level: developer
801d7bf68aeSBarry Smith 
802e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
803d7bf68aeSBarry Smith 
804d7bf68aeSBarry Smith @*/
8057087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
806d7bf68aeSBarry Smith {
807d7bf68aeSBarry Smith   PetscErrorCode ierr;
808d7bf68aeSBarry Smith 
809d7bf68aeSBarry Smith   PetscFunctionBegin;
810171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8118387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
812d7bf68aeSBarry Smith   if (dm->ops->setup) {
813d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
814d7bf68aeSBarry Smith   }
8158387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
816d7bf68aeSBarry Smith   PetscFunctionReturn(0);
817d7bf68aeSBarry Smith }
818d7bf68aeSBarry Smith 
819d7bf68aeSBarry Smith /*@
820d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
821d7bf68aeSBarry Smith 
822d083f849SBarry Smith     Collective on dm
823d7bf68aeSBarry Smith 
824d7bf68aeSBarry Smith     Input Parameter:
825d7bf68aeSBarry Smith .   dm - the DM object to set options for
826d7bf68aeSBarry Smith 
827732e2eb9SMatthew G Knepley     Options Database:
8284164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8294164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8304164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8318f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
832732e2eb9SMatthew G Knepley 
8339318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8349318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8359318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
8369318fe57SMatthew G. Knepley . -dm_plex_shape <shape>            - The domain shape, such as DM_SHAPE_BOX, DM_SHAPE_SPHERE, etc.
8379318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8389318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8399318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
8409318fe57SMatthew G. Knepley . -dm_plex_simplex <bool>           - PETSC_TRUE for simplex elements, PETSC_FALSE for tensor elements
8419318fe57SMatthew G. Knepley . -dm_plex_interpolate <bool>       - PETSC_TRUE turns on topological interpolation (creating edges and faces)
8429318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8439318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8449318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8459318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
8469318fe57SMatthew G. Knepley . -dm_plex_box_bd <bx,by,bz>        - Specify the DMBoundaryType for each direction
8479318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8489318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8499318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8509318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
8519318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8529318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8539318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8549318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
855d410b0cfSMatthew G. Knepley . -dm_extrude <l>                   - Acticate extrusion and apecify the number of layers to extrude
856d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
857d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
858d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
859d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
860d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
861909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
862909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8639318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8649318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8659318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8669318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8679318fe57SMatthew G. Knepley 
868384a6580SVaclav Hapla     DMPLEX Specific Checks
869384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
870384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
871384a6580SVaclav 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()
872384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
873384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
874384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
875384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
876d7bf68aeSBarry Smith 
87795eb5ee5SVaclav Hapla     Level: intermediate
87895eb5ee5SVaclav Hapla 
87995eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
88095eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
881d7bf68aeSBarry Smith 
882d7bf68aeSBarry Smith @*/
8837087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
884d7bf68aeSBarry Smith {
8857781c08eSBarry Smith   char           typeName[256];
886ca266f36SBarry Smith   PetscBool      flg;
887d7bf68aeSBarry Smith   PetscErrorCode ierr;
888d7bf68aeSBarry Smith 
889d7bf68aeSBarry Smith   PetscFunctionBegin;
890171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
89149be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
89249be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8931bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8943194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8950298fd71SBarry 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);
896a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
897f9ba7244SBarry Smith   if (flg) {
898f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
899f9ba7244SBarry Smith   }
900a264d7a6SBarry 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);
901073dac72SJed Brown   if (flg) {
902521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
903073dac72SJed Brown   }
9048f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
905f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
906e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
907f9ba7244SBarry Smith   }
908f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
9090633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
91082fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
911d7bf68aeSBarry Smith   PetscFunctionReturn(0);
912d7bf68aeSBarry Smith }
913d7bf68aeSBarry Smith 
914fc9bc008SSatish Balay /*@C
915fe2efc57SMark    DMViewFromOptions - View from Options
916fe2efc57SMark 
917fe2efc57SMark    Collective on DM
918fe2efc57SMark 
919fe2efc57SMark    Input Parameters:
920fe2efc57SMark +  dm - the DM object
921736c3998SJose E. Roman .  obj - Optional object
922736c3998SJose E. Roman -  name - command line option
923fe2efc57SMark 
924fe2efc57SMark    Level: intermediate
925fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
926fe2efc57SMark @*/
927fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
928fe2efc57SMark {
929fe2efc57SMark   PetscErrorCode ierr;
930fe2efc57SMark 
931fe2efc57SMark   PetscFunctionBegin;
932fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
933fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
934fe2efc57SMark   PetscFunctionReturn(0);
935fe2efc57SMark }
936fe2efc57SMark 
937fe2efc57SMark /*@C
938224748a4SBarry Smith     DMView - Views a DM
93947c6ae99SBarry Smith 
940d083f849SBarry Smith     Collective on dm
94147c6ae99SBarry Smith 
942d8d19677SJose E. Roman     Input Parameters:
94347c6ae99SBarry Smith +   dm - the DM object to view
94447c6ae99SBarry Smith -   v - the viewer
94547c6ae99SBarry Smith 
946224748a4SBarry Smith     Level: beginner
94747c6ae99SBarry Smith 
948e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94947c6ae99SBarry Smith 
95047c6ae99SBarry Smith @*/
9517087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
95247c6ae99SBarry Smith {
95347c6ae99SBarry Smith   PetscErrorCode    ierr;
95432c0f0efSBarry Smith   PetscBool         isbinary;
95576a8abe0SBarry Smith   PetscMPIInt       size;
95676a8abe0SBarry Smith   PetscViewerFormat format;
95747c6ae99SBarry Smith 
95847c6ae99SBarry Smith   PetscFunctionBegin;
959171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9603014e516SBarry Smith   if (!v) {
961ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9623014e516SBarry Smith   }
963b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
96474903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
96574903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
96674903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
96774903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
96874903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
96974903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
97074903a4fSStefano Zampini      in an error here */
97174903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9728bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
973b1b135c8SBarry Smith 
97476a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
975ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRMPI(ierr);
97676a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
97798c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
97832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
97932c0f0efSBarry Smith   if (isbinary) {
98055849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
98132c0f0efSBarry Smith     char     type[256];
98232c0f0efSBarry Smith 
983f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
98432c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
985f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
98632c0f0efSBarry Smith   }
9870c010503SBarry Smith   if (dm->ops->view) {
9880c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
98947c6ae99SBarry Smith   }
99047c6ae99SBarry Smith   PetscFunctionReturn(0);
99147c6ae99SBarry Smith }
99247c6ae99SBarry Smith 
99347c6ae99SBarry Smith /*@
9948472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
99547c6ae99SBarry Smith 
996d083f849SBarry Smith     Collective on dm
99747c6ae99SBarry Smith 
99847c6ae99SBarry Smith     Input Parameter:
99947c6ae99SBarry Smith .   dm - the DM object
100047c6ae99SBarry Smith 
100147c6ae99SBarry Smith     Output Parameter:
100247c6ae99SBarry Smith .   vec - the global vector
100347c6ae99SBarry Smith 
1004073dac72SJed Brown     Level: beginner
100547c6ae99SBarry Smith 
1006ec075b9fSPatrick Sanan .seealso DMCreateLocalVector(), DMGetGlobalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
100747c6ae99SBarry Smith 
100847c6ae99SBarry Smith @*/
10097087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
101047c6ae99SBarry Smith {
101147c6ae99SBarry Smith   PetscErrorCode ierr;
101247c6ae99SBarry Smith 
101347c6ae99SBarry Smith   PetscFunctionBegin;
1014171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1015b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1016b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
101747c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
101876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1019c6b011d8SStefano Zampini     DM vdm;
1020c6b011d8SStefano Zampini 
1021c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1022c6b011d8SStefano 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);
1023c6b011d8SStefano Zampini   }
102447c6ae99SBarry Smith   PetscFunctionReturn(0);
102547c6ae99SBarry Smith }
102647c6ae99SBarry Smith 
102747c6ae99SBarry Smith /*@
10288472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
102947c6ae99SBarry Smith 
103047c6ae99SBarry Smith     Not Collective
103147c6ae99SBarry Smith 
103247c6ae99SBarry Smith     Input Parameter:
103347c6ae99SBarry Smith .   dm - the DM object
103447c6ae99SBarry Smith 
103547c6ae99SBarry Smith     Output Parameter:
103647c6ae99SBarry Smith .   vec - the local vector
103747c6ae99SBarry Smith 
1038073dac72SJed Brown     Level: beginner
103947c6ae99SBarry Smith 
1040ec075b9fSPatrick Sanan .seealso DMCreateGlobalVector(), DMGetLocalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
104147c6ae99SBarry Smith 
104247c6ae99SBarry Smith @*/
10437087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
104447c6ae99SBarry Smith {
104547c6ae99SBarry Smith   PetscErrorCode ierr;
104647c6ae99SBarry Smith 
104747c6ae99SBarry Smith   PetscFunctionBegin;
1048171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1049b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1050b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
105147c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
105276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1053c6b011d8SStefano Zampini     DM vdm;
1054c6b011d8SStefano Zampini 
1055c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1056c6b011d8SStefano 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);
1057c6b011d8SStefano Zampini   }
105847c6ae99SBarry Smith   PetscFunctionReturn(0);
105947c6ae99SBarry Smith }
106047c6ae99SBarry Smith 
10611411c6eeSJed Brown /*@
10621411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10631411c6eeSJed Brown 
1064d083f849SBarry Smith    Collective on dm
10651411c6eeSJed Brown 
10661411c6eeSJed Brown    Input Parameter:
10671411c6eeSJed Brown .  dm - the DM that provides the mapping
10681411c6eeSJed Brown 
10691411c6eeSJed Brown    Output Parameter:
10701411c6eeSJed Brown .  ltog - the mapping
10711411c6eeSJed Brown 
10721411c6eeSJed Brown    Level: intermediate
10731411c6eeSJed Brown 
10741411c6eeSJed Brown    Notes:
10751411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10761411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10771411c6eeSJed Brown 
1078fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10791411c6eeSJed Brown @*/
10807087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10811411c6eeSJed Brown {
10820be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10831411c6eeSJed Brown   PetscErrorCode ierr;
10841411c6eeSJed Brown 
10851411c6eeSJed Brown   PetscFunctionBegin;
10861411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10871411c6eeSJed Brown   PetscValidPointer(ltog,2);
10881411c6eeSJed Brown   if (!dm->ltogmap) {
108937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
109037d0c07bSMatthew G Knepley 
109192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
109237d0c07bSMatthew G Knepley     if (section) {
1093a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
109437d0c07bSMatthew G Knepley       PetscInt       *ltog;
1095ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
109637d0c07bSMatthew G Knepley 
1097e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
109837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1099ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1100ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
110137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1102a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
110337d0c07bSMatthew G Knepley 
110437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
110537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1106a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1107a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
110837d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
11091a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11101a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
11111a7dc684SMatthew G. Knepley         if (dof) {
1112b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1113b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
11141a7dc684SMatthew G. Knepley         }
111537d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1116a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1117a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
111837d0c07bSMatthew G Knepley         }
111937d0c07bSMatthew G Knepley       }
1120bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11210be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11220be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
11230be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11240be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
11257591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11267591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1127ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1128ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1129ccf3bd66SMatthew G. Knepley         n /= bs;
1130ccf3bd66SMatthew G. Knepley       }
1131ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
11323bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
113337d0c07bSMatthew G Knepley     } else {
1134b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1135184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
11361411c6eeSJed Brown     }
113737d0c07bSMatthew G Knepley   }
11381411c6eeSJed Brown   *ltog = dm->ltogmap;
11391411c6eeSJed Brown   PetscFunctionReturn(0);
11401411c6eeSJed Brown }
11411411c6eeSJed Brown 
11421411c6eeSJed Brown /*@
11431411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
11441411c6eeSJed Brown 
11451411c6eeSJed Brown    Not Collective
11461411c6eeSJed Brown 
11471411c6eeSJed Brown    Input Parameter:
11481411c6eeSJed Brown .  dm - the DM with block structure
11491411c6eeSJed Brown 
11501411c6eeSJed Brown    Output Parameter:
11511411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11521411c6eeSJed Brown 
11531411c6eeSJed Brown    Level: intermediate
11541411c6eeSJed Brown 
1155fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
11561411c6eeSJed Brown @*/
11577087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
11581411c6eeSJed Brown {
11591411c6eeSJed Brown   PetscFunctionBegin;
11601411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1161534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11621411c6eeSJed 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");
11631411c6eeSJed Brown   *bs = dm->bs;
11641411c6eeSJed Brown   PetscFunctionReturn(0);
11651411c6eeSJed Brown }
11661411c6eeSJed Brown 
116748eeb7c8SBarry Smith /*@C
11688472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
116947c6ae99SBarry Smith 
1170a5bc1bf3SBarry Smith     Collective on dmc
117147c6ae99SBarry Smith 
1172d8d19677SJose E. Roman     Input Parameters:
1173a5bc1bf3SBarry Smith +   dmc - the DM object
1174a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
117547c6ae99SBarry Smith 
1176d8d19677SJose E. Roman     Output Parameters:
117747c6ae99SBarry Smith +  mat - the interpolation
117847c6ae99SBarry Smith -  vec - the scaling (optional)
117947c6ae99SBarry Smith 
118047c6ae99SBarry Smith     Level: developer
118147c6ae99SBarry Smith 
118295452b02SPatrick Sanan     Notes:
118395452b02SPatrick 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
118485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1185d52bd9f3SBarry Smith 
11861f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1187d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
118885afcc9aSBarry Smith 
11892ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
119047c6ae99SBarry Smith 
119147c6ae99SBarry Smith @*/
1192a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
119347c6ae99SBarry Smith {
119447c6ae99SBarry Smith   PetscErrorCode ierr;
119547c6ae99SBarry Smith 
119647c6ae99SBarry Smith   PetscFunctionBegin;
1197a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1198a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1199c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1200a5bc1bf3SBarry Smith   if (!dmc->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1201a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1202a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1203a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
120447c6ae99SBarry Smith   PetscFunctionReturn(0);
120547c6ae99SBarry Smith }
120647c6ae99SBarry Smith 
12073ad4599aSBarry Smith /*@
1208d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
12092ed6491fSPatrick Sanan 
12102ed6491fSPatrick Sanan   Input Parameters:
12112ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
12122ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
12132ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12142ed6491fSPatrick Sanan 
12152ed6491fSPatrick Sanan   Output Parameter:
12162ed6491fSPatrick Sanan .    scale - the scaled vector
12172ed6491fSPatrick Sanan 
12182ed6491fSPatrick Sanan   Level: developer
12192ed6491fSPatrick Sanan 
12202ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
12212ed6491fSPatrick Sanan 
12222ed6491fSPatrick Sanan @*/
12232ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
12242ed6491fSPatrick Sanan {
12252ed6491fSPatrick Sanan   PetscErrorCode ierr;
12262ed6491fSPatrick Sanan   Vec            fine;
12272ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
12282ed6491fSPatrick Sanan 
12292ed6491fSPatrick Sanan   PetscFunctionBegin;
12302ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
12312ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
12322ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
12332ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
12342ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
12352ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
12362ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12372ed6491fSPatrick Sanan }
12382ed6491fSPatrick Sanan 
12392ed6491fSPatrick Sanan /*@
12403ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
12413ad4599aSBarry Smith 
1242a5bc1bf3SBarry Smith     Collective on dmc
12433ad4599aSBarry Smith 
1244d8d19677SJose E. Roman     Input Parameters:
1245a5bc1bf3SBarry Smith +   dmc - the DM object
1246a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
12473ad4599aSBarry Smith 
12483ad4599aSBarry Smith     Output Parameter:
12493ad4599aSBarry Smith .  mat - the restriction
12503ad4599aSBarry Smith 
12513ad4599aSBarry Smith     Level: developer
12523ad4599aSBarry Smith 
125395452b02SPatrick Sanan     Notes:
125495452b02SPatrick 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
12553ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
12563ad4599aSBarry Smith 
12573ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
12583ad4599aSBarry Smith 
12593ad4599aSBarry Smith @*/
1260a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12613ad4599aSBarry Smith {
12623ad4599aSBarry Smith   PetscErrorCode ierr;
12633ad4599aSBarry Smith 
12643ad4599aSBarry Smith   PetscFunctionBegin;
1265a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1266a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12675a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1268a5bc1bf3SBarry Smith   if (!dmc->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1269a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1270a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1271a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
12723ad4599aSBarry Smith   PetscFunctionReturn(0);
12733ad4599aSBarry Smith }
12743ad4599aSBarry Smith 
127547c6ae99SBarry Smith /*@
12768472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
127747c6ae99SBarry Smith 
1278a5bc1bf3SBarry Smith     Collective on dac
127947c6ae99SBarry Smith 
1280d8d19677SJose E. Roman     Input Parameters:
1281a5bc1bf3SBarry Smith +   dac - the DM object
1282a5bc1bf3SBarry Smith -   daf - the second, finer DM object
128347c6ae99SBarry Smith 
128447c6ae99SBarry Smith     Output Parameter:
12856dbf9973SLawrence Mitchell .   mat - the injection
128647c6ae99SBarry Smith 
128747c6ae99SBarry Smith     Level: developer
128847c6ae99SBarry Smith 
128995452b02SPatrick Sanan    Notes:
129095452b02SPatrick 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
129185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
129285afcc9aSBarry Smith 
1293e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
129447c6ae99SBarry Smith 
129547c6ae99SBarry Smith @*/
1296a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
129747c6ae99SBarry Smith {
129847c6ae99SBarry Smith   PetscErrorCode ierr;
129947c6ae99SBarry Smith 
130047c6ae99SBarry Smith   PetscFunctionBegin;
1301a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1302a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
13035a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1304a5bc1bf3SBarry Smith   if (!dac->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1305a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1306a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1307a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
130847c6ae99SBarry Smith   PetscFunctionReturn(0);
130947c6ae99SBarry Smith }
131047c6ae99SBarry Smith 
1311b412c318SBarry Smith /*@
1312bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1313bd041c0cSMatthew G. Knepley 
1314a5bc1bf3SBarry Smith   Collective on dac
1315bd041c0cSMatthew G. Knepley 
1316d8d19677SJose E. Roman   Input Parameters:
1317a5bc1bf3SBarry Smith + dac - the DM object
1318a5bc1bf3SBarry Smith - daf - the second, finer DM object
1319bd041c0cSMatthew G. Knepley 
1320bd041c0cSMatthew G. Knepley   Output Parameter:
1321bd041c0cSMatthew G. Knepley . mat - the interpolation
1322bd041c0cSMatthew G. Knepley 
1323bd041c0cSMatthew G. Knepley   Level: developer
1324bd041c0cSMatthew G. Knepley 
1325bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1326bd041c0cSMatthew G. Knepley @*/
1327a5bc1bf3SBarry Smith PetscErrorCode DMCreateMassMatrix(DM dac, DM daf, Mat *mat)
1328bd041c0cSMatthew G. Knepley {
1329bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1330bd041c0cSMatthew G. Knepley 
1331bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1332a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1333a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13345a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1335a5bc1bf3SBarry Smith   if (!dac->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dac)->type_name);
1336a5bc1bf3SBarry Smith   ierr = (*dac->ops->createmassmatrix)(dac, daf, mat);CHKERRQ(ierr);
1337bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1338bd041c0cSMatthew G. Knepley }
1339bd041c0cSMatthew G. Knepley 
1340bd041c0cSMatthew G. Knepley /*@
1341b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
134247c6ae99SBarry Smith 
1343d083f849SBarry Smith     Collective on dm
134447c6ae99SBarry Smith 
1345d8d19677SJose E. Roman     Input Parameters:
134647c6ae99SBarry Smith +   dm - the DM object
13475bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
134847c6ae99SBarry Smith 
134947c6ae99SBarry Smith     Output Parameter:
135047c6ae99SBarry Smith .   coloring - the coloring
135147c6ae99SBarry Smith 
1352ec5066bdSBarry Smith     Notes:
1353ec5066bdSBarry 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
1354ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1355ec5066bdSBarry Smith 
1356ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1357ec5066bdSBarry Smith 
135847c6ae99SBarry Smith     Level: developer
135947c6ae99SBarry Smith 
1360ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
136147c6ae99SBarry Smith 
1362aab9d709SJed Brown @*/
1363b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
136447c6ae99SBarry Smith {
136547c6ae99SBarry Smith   PetscErrorCode ierr;
136647c6ae99SBarry Smith 
136747c6ae99SBarry Smith   PetscFunctionBegin;
1368171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13695a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1370b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1371b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
137247c6ae99SBarry Smith   PetscFunctionReturn(0);
137347c6ae99SBarry Smith }
137447c6ae99SBarry Smith 
1375b412c318SBarry Smith /*@
13768472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
137747c6ae99SBarry Smith 
1378d083f849SBarry Smith     Collective on dm
137947c6ae99SBarry Smith 
138047c6ae99SBarry Smith     Input Parameter:
1381b412c318SBarry Smith .   dm - the DM object
138247c6ae99SBarry Smith 
138347c6ae99SBarry Smith     Output Parameter:
138447c6ae99SBarry Smith .   mat - the empty Jacobian
138547c6ae99SBarry Smith 
1386073dac72SJed Brown     Level: beginner
138747c6ae99SBarry Smith 
1388f27dd7c6SMatthew G. Knepley     Options Database Keys:
1389f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1390f27dd7c6SMatthew G. Knepley 
139195452b02SPatrick Sanan     Notes:
139295452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
139394013140SBarry Smith        do not need to do it yourself.
139494013140SBarry Smith 
139594013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13967889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
139794013140SBarry Smith 
139894013140SBarry 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
139994013140SBarry Smith        internally by PETSc.
140094013140SBarry Smith 
140194013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1402aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
140394013140SBarry Smith 
1404b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
140547c6ae99SBarry Smith 
1406aab9d709SJed Brown @*/
1407b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
140847c6ae99SBarry Smith {
140947c6ae99SBarry Smith   PetscErrorCode ierr;
141047c6ae99SBarry Smith 
141147c6ae99SBarry Smith   PetscFunctionBegin;
1412171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1413064a246eSJacob Faibussowitsch   PetscValidPointer(mat,2);
1414b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
14155a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1416fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1417b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
141876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1419c6b011d8SStefano Zampini     DM mdm;
1420c6b011d8SStefano Zampini 
1421c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1422c6b011d8SStefano 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);
1423c6b011d8SStefano Zampini   }
1424e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1425e5e52638SMatthew G. Knepley   if (dm->Nf) {
1426e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1427649ef022SMatthew Knepley     PetscInt     Nf, f;
1428e571a35bSMatthew G. Knepley 
1429e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1430649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1431649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
1432649ef022SMatthew Knepley         ierr = (*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1433e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1434e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1435649ef022SMatthew Knepley         break;
1436e571a35bSMatthew G. Knepley       }
1437649ef022SMatthew Knepley     }
1438649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1439649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
1440649ef022SMatthew Knepley         ierr = (*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1441e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1442e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1443e571a35bSMatthew G. Knepley       }
1444e571a35bSMatthew G. Knepley     }
1445e571a35bSMatthew G. Knepley   }
1446fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
144747c6ae99SBarry Smith   PetscFunctionReturn(0);
144847c6ae99SBarry Smith }
144947c6ae99SBarry Smith 
1450732e2eb9SMatthew G Knepley /*@
1451950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1452732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1453732e2eb9SMatthew G Knepley 
1454d083f849SBarry Smith   Logically Collective on dm
1455732e2eb9SMatthew G Knepley 
1456d8d19677SJose E. Roman   Input Parameters:
1457732e2eb9SMatthew G Knepley + dm - the DM
1458732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1459732e2eb9SMatthew G Knepley 
1460732e2eb9SMatthew G Knepley   Level: developer
1461f27dd7c6SMatthew G. Knepley 
1462f27dd7c6SMatthew G. Knepley   Options Database Keys:
1463f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1464f27dd7c6SMatthew G. Knepley 
1465b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1466732e2eb9SMatthew G Knepley @*/
1467732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1468732e2eb9SMatthew G Knepley {
1469732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1470732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1471732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1472732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1473732e2eb9SMatthew G Knepley }
1474732e2eb9SMatthew G Knepley 
1475b06ff27eSHong Zhang /*@
1476b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1477b06ff27eSHong Zhang     but the array for values will not be allocated.
1478b06ff27eSHong Zhang 
1479d083f849SBarry Smith   Logically Collective on dm
1480b06ff27eSHong Zhang 
1481d8d19677SJose E. Roman   Input Parameters:
1482b06ff27eSHong Zhang + dm - the DM
1483b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1484b06ff27eSHong Zhang 
1485b06ff27eSHong Zhang   Level: developer
1486b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1487b06ff27eSHong Zhang @*/
1488b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1489b06ff27eSHong Zhang {
1490b06ff27eSHong Zhang   PetscFunctionBegin;
1491b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1492b06ff27eSHong Zhang   dm->structure_only = only;
1493b06ff27eSHong Zhang   PetscFunctionReturn(0);
1494b06ff27eSHong Zhang }
1495b06ff27eSHong Zhang 
1496a89ea682SMatthew G Knepley /*@C
1497aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1498a89ea682SMatthew G Knepley 
1499a89ea682SMatthew G Knepley   Not Collective
1500a89ea682SMatthew G Knepley 
1501a89ea682SMatthew G Knepley   Input Parameters:
1502a89ea682SMatthew G Knepley + dm - the DM object
1503a5b23f4aSJose E. Roman . count - The minimum size
150469291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1505a89ea682SMatthew G Knepley 
1506a89ea682SMatthew G Knepley   Output Parameter:
1507a89ea682SMatthew G Knepley . array - the work array
1508a89ea682SMatthew G Knepley 
1509a89ea682SMatthew G Knepley   Level: developer
1510a89ea682SMatthew G Knepley 
1511a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1512a89ea682SMatthew G Knepley @*/
151369291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1514a89ea682SMatthew G Knepley {
1515a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1516aa1993deSMatthew G Knepley   DMWorkLink     link;
151769291d52SBarry Smith   PetscMPIInt    dsize;
1518a89ea682SMatthew G Knepley 
1519a89ea682SMatthew G Knepley   PetscFunctionBegin;
1520a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1521aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1522aa1993deSMatthew G Knepley   if (dm->workin) {
1523aa1993deSMatthew G Knepley     link       = dm->workin;
1524aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1525aa1993deSMatthew G Knepley   } else {
1526b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1527a89ea682SMatthew G Knepley   }
1528ffc4695bSBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRMPI(ierr);
15295056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1530aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1531854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1532854ce69bSBarry Smith     link->bytes = dsize*count;
1533aa1993deSMatthew G Knepley   }
1534aa1993deSMatthew G Knepley   link->next   = dm->workout;
1535aa1993deSMatthew G Knepley   dm->workout  = link;
153600d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
153700d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
153800d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
153900d952a4SJed Brown #endif
1540aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1541a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1542a89ea682SMatthew G Knepley }
1543a89ea682SMatthew G Knepley 
1544aa1993deSMatthew G Knepley /*@C
1545aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1546aa1993deSMatthew G Knepley 
1547aa1993deSMatthew G Knepley   Not Collective
1548aa1993deSMatthew G Knepley 
1549aa1993deSMatthew G Knepley   Input Parameters:
1550aa1993deSMatthew G Knepley + dm - the DM object
1551a5b23f4aSJose E. Roman . count - The minimum size
155269291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1553aa1993deSMatthew G Knepley 
1554aa1993deSMatthew G Knepley   Output Parameter:
1555aa1993deSMatthew G Knepley . array - the work array
1556aa1993deSMatthew G Knepley 
1557aa1993deSMatthew G Knepley   Level: developer
1558aa1993deSMatthew G Knepley 
155995452b02SPatrick Sanan   Developer Notes:
156095452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1561aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1562aa1993deSMatthew G Knepley @*/
156369291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1564aa1993deSMatthew G Knepley {
1565aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1566aa1993deSMatthew G Knepley 
1567aa1993deSMatthew G Knepley   PetscFunctionBegin;
1568aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1569aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1570aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1571aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1572aa1993deSMatthew G Knepley       *p           = link->next;
1573aa1993deSMatthew G Knepley       link->next   = dm->workin;
1574aa1993deSMatthew G Knepley       dm->workin   = link;
15750298fd71SBarry Smith       *(void**)mem = NULL;
1576aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1577aa1993deSMatthew G Knepley     }
1578aa1993deSMatthew G Knepley   }
1579aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1580aa1993deSMatthew G Knepley }
1581e7c4fc90SDmitry Karpeev 
15828cda7954SMatthew G. Knepley /*@C
15838cda7954SMatthew G. Knepley   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field
15848cda7954SMatthew G. Knepley 
15858cda7954SMatthew G. Knepley   Logically collective on DM
15868cda7954SMatthew G. Knepley 
15878cda7954SMatthew G. Knepley   Input Parameters:
15888cda7954SMatthew G. Knepley + dm     - The DM
15898cda7954SMatthew G. Knepley . field  - The field number for the nullspace
15908cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
15918cda7954SMatthew G. Knepley 
15928cda7954SMatthew G. Knepley   Notes:
15938cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15948cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15958cda7954SMatthew G. Knepley $ dm        - The present DM
15968cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15978cda7954SMatthew G. Knepley $ field     - The field number in dm
15988cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15998cda7954SMatthew G. Knepley 
16008cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16018cda7954SMatthew G. Knepley 
16028cda7954SMatthew G. Knepley .seealso: DMGetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16038cda7954SMatthew G. Knepley */
16048cda7954SMatthew G. Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1605435a35e8SMatthew G Knepley {
1606435a35e8SMatthew G Knepley   PetscFunctionBegin;
1607435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
160882f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1609435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1610435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1611435a35e8SMatthew G Knepley }
1612435a35e8SMatthew G Knepley 
16138cda7954SMatthew G. Knepley /*@C
16148cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
16158cda7954SMatthew G. Knepley 
16168cda7954SMatthew G. Knepley   Not collective
16178cda7954SMatthew G. Knepley 
16188cda7954SMatthew G. Knepley   Input Parameters:
16198cda7954SMatthew G. Knepley + dm     - The DM
16208cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16218cda7954SMatthew G. Knepley 
16228cda7954SMatthew G. Knepley   Output Parameter:
16238cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
16248cda7954SMatthew G. Knepley 
16258cda7954SMatthew G. Knepley   Notes:
16268cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16278cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16288cda7954SMatthew G. Knepley $ dm        - The present DM
16298cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16308cda7954SMatthew G. Knepley $ field     - The field number in dm
16318cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16328cda7954SMatthew G. Knepley 
16338cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16348cda7954SMatthew G. Knepley 
16358cda7954SMatthew G. Knepley .seealso: DMSetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16368cda7954SMatthew G. Knepley */
16378cda7954SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
16380a50eb56SMatthew G. Knepley {
16390a50eb56SMatthew G. Knepley   PetscFunctionBegin;
16400a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1641f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
16420a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
16430a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
16440a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
16450a50eb56SMatthew G. Knepley }
16460a50eb56SMatthew G. Knepley 
16478cda7954SMatthew G. Knepley /*@C
16488cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
16498cda7954SMatthew G. Knepley 
16508cda7954SMatthew G. Knepley   Logically collective on DM
16518cda7954SMatthew G. Knepley 
16528cda7954SMatthew G. Knepley   Input Parameters:
16538cda7954SMatthew G. Knepley + dm     - The DM
16548cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16558cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
16568cda7954SMatthew G. Knepley 
16578cda7954SMatthew G. Knepley   Notes:
16588cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16598cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16608cda7954SMatthew G. Knepley $ dm        - The present DM
16618cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16628cda7954SMatthew G. Knepley $ field     - The field number in dm
16638cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16648cda7954SMatthew G. Knepley 
16658cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16668cda7954SMatthew G. Knepley 
16678cda7954SMatthew G. Knepley .seealso: DMGetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16688cda7954SMatthew G. Knepley */
16698cda7954SMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1670f9d4088aSMatthew G. Knepley {
1671f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1672f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1673f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1674f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1675f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1676f9d4088aSMatthew G. Knepley }
1677f9d4088aSMatthew G. Knepley 
16788cda7954SMatthew G. Knepley /*@C
16798cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
16808cda7954SMatthew G. Knepley 
16818cda7954SMatthew G. Knepley   Not collective
16828cda7954SMatthew G. Knepley 
16838cda7954SMatthew G. Knepley   Input Parameters:
16848cda7954SMatthew G. Knepley + dm     - The DM
16858cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16868cda7954SMatthew G. Knepley 
16878cda7954SMatthew G. Knepley   Output Parameter:
16888cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
16898cda7954SMatthew G. Knepley 
16908cda7954SMatthew G. Knepley   Notes:
16918cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16928cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16938cda7954SMatthew G. Knepley $ dm        - The present DM
16948cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16958cda7954SMatthew G. Knepley $ field     - The field number in dm
16968cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16978cda7954SMatthew G. Knepley 
16988cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16998cda7954SMatthew G. Knepley 
17008cda7954SMatthew G. Knepley .seealso: DMSetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
17018cda7954SMatthew G. Knepley */
17028cda7954SMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1703f9d4088aSMatthew G. Knepley {
1704f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1705f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1706f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1707f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1708f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1709f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1710f9d4088aSMatthew G. Knepley }
1711f9d4088aSMatthew G. Knepley 
17124f3b5142SJed Brown /*@C
17134d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
17144d343eeaSMatthew G Knepley 
17154d343eeaSMatthew G Knepley   Not collective
17164d343eeaSMatthew G Knepley 
17174d343eeaSMatthew G Knepley   Input Parameter:
17184d343eeaSMatthew G Knepley . dm - the DM object
17194d343eeaSMatthew G Knepley 
17204d343eeaSMatthew G Knepley   Output Parameters:
17210298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
17220298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
17230298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
17244d343eeaSMatthew G Knepley 
17254d343eeaSMatthew G Knepley   Level: intermediate
17264d343eeaSMatthew G Knepley 
172721c9b008SJed Brown   Notes:
172821c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
172921c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
173021c9b008SJed Brown   PetscFree().
173121c9b008SJed Brown 
17324d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
17334d343eeaSMatthew G Knepley @*/
173437d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
17354d343eeaSMatthew G Knepley {
173637d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
17374d343eeaSMatthew G Knepley   PetscErrorCode ierr;
17384d343eeaSMatthew G Knepley 
17394d343eeaSMatthew G Knepley   PetscFunctionBegin;
17404d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
174169ca1f37SDmitry Karpeev   if (numFields) {
1742534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
174369ca1f37SDmitry Karpeev     *numFields = 0;
174469ca1f37SDmitry Karpeev   }
174537d0c07bSMatthew G Knepley   if (fieldNames) {
174637d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
17470298fd71SBarry Smith     *fieldNames = NULL;
174869ca1f37SDmitry Karpeev   }
174969ca1f37SDmitry Karpeev   if (fields) {
175069ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
17510298fd71SBarry Smith     *fields = NULL;
175269ca1f37SDmitry Karpeev   }
175392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
175437d0c07bSMatthew G Knepley   if (section) {
17553a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
175637d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
175737d0c07bSMatthew G Knepley 
1758e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
175937d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
17603a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
176137d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
176237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
176337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
17643a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
176537d0c07bSMatthew G Knepley     }
176637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
176737d0c07bSMatthew G Knepley       PetscInt gdof;
176837d0c07bSMatthew G Knepley 
176937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
177037d0c07bSMatthew G Knepley       if (gdof > 0) {
177137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
17723a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
177337d0c07bSMatthew G Knepley 
177437d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
177537d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
17763a544194SStefano Zampini           fpdof = fdof-fcdof;
17773a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
17783a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
17793a544194SStefano Zampini             fieldNc[f] = 1;
17803a544194SStefano Zampini           }
17813a544194SStefano Zampini           fieldSizes[f] += fpdof;
178237d0c07bSMatthew G Knepley         }
178337d0c07bSMatthew G Knepley       }
178437d0c07bSMatthew G Knepley     }
178537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1786785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
178737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
178837d0c07bSMatthew G Knepley     }
178937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
179037d0c07bSMatthew G Knepley       PetscInt gdof, goff;
179137d0c07bSMatthew G Knepley 
179237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
179337d0c07bSMatthew G Knepley       if (gdof > 0) {
179437d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
179537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
179637d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
179737d0c07bSMatthew G Knepley 
179837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
179937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
180037d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
180137d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
180237d0c07bSMatthew G Knepley           }
180337d0c07bSMatthew G Knepley         }
180437d0c07bSMatthew G Knepley       }
180537d0c07bSMatthew G Knepley     }
18068865f1eaSKarl Rupp     if (numFields) *numFields = nF;
180737d0c07bSMatthew G Knepley     if (fieldNames) {
1808785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
180937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
181037d0c07bSMatthew G Knepley         const char *fieldName;
181137d0c07bSMatthew G Knepley 
181237d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
181337d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
181437d0c07bSMatthew G Knepley       }
181537d0c07bSMatthew G Knepley     }
181637d0c07bSMatthew G Knepley     if (fields) {
1817785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
181837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
18193a544194SStefano Zampini         PetscInt bs, in[2], out[2];
18203a544194SStefano Zampini 
182182f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
18223a544194SStefano Zampini         in[0] = -fieldNc[f];
18233a544194SStefano Zampini         in[1] = fieldNc[f];
1824820f2d46SBarry Smith         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
18253a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
18263a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
182737d0c07bSMatthew G Knepley       }
182837d0c07bSMatthew G Knepley     }
18293a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
18308865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
18318865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
183269ca1f37SDmitry Karpeev   }
18334d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
18344d343eeaSMatthew G Knepley }
18354d343eeaSMatthew G Knepley 
183616621825SDmitry Karpeev /*@C
183716621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
183816621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
183916621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1840e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1841e7c4fc90SDmitry Karpeev 
1842e7c4fc90SDmitry Karpeev   Not collective
1843e7c4fc90SDmitry Karpeev 
1844e7c4fc90SDmitry Karpeev   Input Parameter:
1845e7c4fc90SDmitry Karpeev . dm - the DM object
1846e7c4fc90SDmitry Karpeev 
1847e7c4fc90SDmitry Karpeev   Output Parameters:
18480298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
18490298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
18500298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
18510298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1852e7c4fc90SDmitry Karpeev 
1853e7c4fc90SDmitry Karpeev   Level: intermediate
1854e7c4fc90SDmitry Karpeev 
1855e7c4fc90SDmitry Karpeev   Notes:
1856e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1857e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1858e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1859e7c4fc90SDmitry Karpeev 
1860e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1861e7c4fc90SDmitry Karpeev @*/
186216621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1863e7c4fc90SDmitry Karpeev {
1864e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1865e7c4fc90SDmitry Karpeev 
1866e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1867e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18688865f1eaSKarl Rupp   if (len) {
1869534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
18708865f1eaSKarl Rupp     *len = 0;
18718865f1eaSKarl Rupp   }
18728865f1eaSKarl Rupp   if (namelist) {
18738865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1874ea78f98cSLisandro Dalcin     *namelist = NULL;
18758865f1eaSKarl Rupp   }
18768865f1eaSKarl Rupp   if (islist) {
18778865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1878ea78f98cSLisandro Dalcin     *islist = NULL;
18798865f1eaSKarl Rupp   }
18808865f1eaSKarl Rupp   if (dmlist) {
18818865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1882ea78f98cSLisandro Dalcin     *dmlist = NULL;
18838865f1eaSKarl Rupp   }
1884f3f0edfdSDmitry Karpeev   /*
1885f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1886f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1887f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1888f3f0edfdSDmitry Karpeev    */
1889ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
189016621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1891435a35e8SMatthew G Knepley     PetscSection section;
1892435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1893435a35e8SMatthew G Knepley 
189492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1895435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1896435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1897f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
189803dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
189903dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
190003dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1901435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1902435a35e8SMatthew G Knepley         const char *fieldName;
1903435a35e8SMatthew G Knepley 
190403dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
190503dc3394SMatthew G. Knepley         if (namelist) {
1906435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1907435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1908435a35e8SMatthew G Knepley         }
190903dc3394SMatthew G. Knepley       }
1910435a35e8SMatthew G Knepley     } else {
191169ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1912e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
19130298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1914e7c4fc90SDmitry Karpeev     }
19158865f1eaSKarl Rupp   } else {
191616621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
191716621825SDmitry Karpeev   }
191816621825SDmitry Karpeev   PetscFunctionReturn(0);
191916621825SDmitry Karpeev }
192016621825SDmitry Karpeev 
1921564cec59SMatthew G. Knepley /*@
1922435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1923435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1924435a35e8SMatthew G Knepley 
1925435a35e8SMatthew G Knepley   Not collective
1926435a35e8SMatthew G Knepley 
1927435a35e8SMatthew G Knepley   Input Parameters:
19282adcc780SMatthew G. Knepley + dm        - The DM object
19292adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
19302adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1931435a35e8SMatthew G Knepley 
1932435a35e8SMatthew G Knepley   Output Parameters:
19332adcc780SMatthew G. Knepley + is - The global indices for the subproblem
19342adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1935435a35e8SMatthew G Knepley 
19365d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19375d3b26e6SMatthew G. Knepley 
1938435a35e8SMatthew G Knepley   Level: intermediate
1939435a35e8SMatthew G Knepley 
19405d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1941435a35e8SMatthew G Knepley @*/
194237bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1943435a35e8SMatthew G Knepley {
1944435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1945435a35e8SMatthew G Knepley 
1946435a35e8SMatthew G Knepley   PetscFunctionBegin;
1947435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1948435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
19498865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
19508865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1951b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1952435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1953435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1954435a35e8SMatthew G Knepley }
1955435a35e8SMatthew G Knepley 
19562adcc780SMatthew G. Knepley /*@C
19572adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
19582adcc780SMatthew G. Knepley 
19592adcc780SMatthew G. Knepley   Not collective
19602adcc780SMatthew G. Knepley 
1961d8d19677SJose E. Roman   Input Parameters:
19622adcc780SMatthew G. Knepley + dms - The DM objects
19632adcc780SMatthew G. Knepley - len - The number of DMs
19642adcc780SMatthew G. Knepley 
19652adcc780SMatthew G. Knepley   Output Parameters:
1966a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
19672adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
19682adcc780SMatthew G. Knepley 
19695d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19705d3b26e6SMatthew G. Knepley 
19712adcc780SMatthew G. Knepley   Level: intermediate
19722adcc780SMatthew G. Knepley 
19735d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
19742adcc780SMatthew G. Knepley @*/
19752adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
19762adcc780SMatthew G. Knepley {
19772adcc780SMatthew G. Knepley   PetscInt       i;
19782adcc780SMatthew G. Knepley   PetscErrorCode ierr;
19792adcc780SMatthew G. Knepley 
19802adcc780SMatthew G. Knepley   PetscFunctionBegin;
19812adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
19822adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
19832adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1984a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
19852adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
19862adcc780SMatthew G. Knepley   if (len) {
1987b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1988b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1989b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
19902adcc780SMatthew G. Knepley   }
19912adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
19922adcc780SMatthew G. Knepley }
19932adcc780SMatthew G. Knepley 
199416621825SDmitry Karpeev /*@C
19958d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
19968d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
19978d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
19988d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
19998d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
200016621825SDmitry Karpeev 
200116621825SDmitry Karpeev   Not collective
200216621825SDmitry Karpeev 
200316621825SDmitry Karpeev   Input Parameter:
200416621825SDmitry Karpeev . dm - the DM object
200516621825SDmitry Karpeev 
200616621825SDmitry Karpeev   Output Parameters:
20070298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
20080298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
20090298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
20100298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
20110298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
201216621825SDmitry Karpeev 
201316621825SDmitry Karpeev   Level: intermediate
201416621825SDmitry Karpeev 
201516621825SDmitry Karpeev   Notes:
201616621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
201716621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
201816621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
201916621825SDmitry Karpeev 
2020245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
202116621825SDmitry Karpeev @*/
20228d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
202316621825SDmitry Karpeev {
202416621825SDmitry Karpeev   PetscErrorCode      ierr;
2025be081cd6SPeter Brune   DMSubDomainHookLink link;
2026be081cd6SPeter Brune   PetscInt            i,l;
202716621825SDmitry Karpeev 
202816621825SDmitry Karpeev   PetscFunctionBegin;
202916621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
203014a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
20310298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
20320298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
20330298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
20340298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
2035f3f0edfdSDmitry Karpeev   /*
2036f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2037f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2038f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2039f3f0edfdSDmitry Karpeev    */
2040ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
204116621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2042be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
204314a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2044f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2045be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2046be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
2047be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
2048be081cd6SPeter Brune         }
2049648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2050e7c4fc90SDmitry Karpeev       }
205114a18fd3SPeter Brune     }
205214a18fd3SPeter Brune     if (len) *len = l;
205314a18fd3SPeter Brune   }
2054e30e807fSPeter Brune   PetscFunctionReturn(0);
2055e30e807fSPeter Brune }
2056e30e807fSPeter Brune 
2057e30e807fSPeter Brune /*@C
2058e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2059e30e807fSPeter Brune 
2060e30e807fSPeter Brune   Not collective
2061e30e807fSPeter Brune 
2062e30e807fSPeter Brune   Input Parameters:
2063e30e807fSPeter Brune + dm - the DM object
2064e30e807fSPeter Brune . n  - the number of subdomain scatters
2065e30e807fSPeter Brune - subdms - the local subdomains
2066e30e807fSPeter Brune 
2067e30e807fSPeter Brune   Output Parameters:
20686b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2069e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2070e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2071e30e807fSPeter Brune 
207295452b02SPatrick Sanan   Notes:
207395452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2074e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2075e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2076e30e807fSPeter Brune   solution and residual data.
2077e30e807fSPeter Brune 
2078e30e807fSPeter Brune   Level: developer
2079e30e807fSPeter Brune 
2080e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2081e30e807fSPeter Brune @*/
2082e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2083e30e807fSPeter Brune {
2084e30e807fSPeter Brune   PetscErrorCode ierr;
2085e30e807fSPeter Brune 
2086e30e807fSPeter Brune   PetscFunctionBegin;
2087e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2088e30e807fSPeter Brune   PetscValidPointer(subdms,3);
2089b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
2090e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
2091e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2092e7c4fc90SDmitry Karpeev }
2093e7c4fc90SDmitry Karpeev 
209447c6ae99SBarry Smith /*@
209547c6ae99SBarry Smith   DMRefine - Refines a DM object
209647c6ae99SBarry Smith 
2097d083f849SBarry Smith   Collective on dm
209847c6ae99SBarry Smith 
2099d8d19677SJose E. Roman   Input Parameters:
210047c6ae99SBarry Smith + dm   - the DM object
210191d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
210247c6ae99SBarry Smith 
210347c6ae99SBarry Smith   Output Parameter:
21040298fd71SBarry Smith . dmf - the refined DM, or NULL
2105ae0a1c52SMatthew G Knepley 
2106f27dd7c6SMatthew G. Knepley   Options Database Keys:
2107412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2108412e9a14SMatthew G. Knepley 
21090298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
211047c6ae99SBarry Smith 
211147c6ae99SBarry Smith   Level: developer
211247c6ae99SBarry Smith 
2113e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
211447c6ae99SBarry Smith @*/
21157087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
211647c6ae99SBarry Smith {
211747c6ae99SBarry Smith   PetscErrorCode   ierr;
2118c833c3b5SJed Brown   DMRefineHookLink link;
211947c6ae99SBarry Smith 
212047c6ae99SBarry Smith   PetscFunctionBegin;
2121732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2122b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
21231ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
212447c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
21254057135bSMatthew G Knepley   if (*dmf) {
212643842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
21278865f1eaSKarl Rupp 
21288cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
21298865f1eaSKarl Rupp 
2130644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
21310598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2132656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
21338865f1eaSKarl Rupp 
2134e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
2135c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
21368865f1eaSKarl Rupp       if (link->refinehook) {
21378865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
21388865f1eaSKarl Rupp       }
2139c833c3b5SJed Brown     }
2140c833c3b5SJed Brown   }
21411ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
2142c833c3b5SJed Brown   PetscFunctionReturn(0);
2143c833c3b5SJed Brown }
2144c833c3b5SJed Brown 
2145bb9467b5SJed Brown /*@C
2146c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2147c833c3b5SJed Brown 
2148c833c3b5SJed Brown    Logically Collective
2149c833c3b5SJed Brown 
21504165533cSJose E. Roman    Input Parameters:
2151c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2152c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2153c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21540298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2155c833c3b5SJed Brown 
2156c833c3b5SJed Brown    Calling sequence of refinehook:
2157c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2158c833c3b5SJed Brown 
2159c833c3b5SJed Brown +  coarse - coarse level DM
2160c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2161c833c3b5SJed Brown -  ctx - optional user-defined function context
2162c833c3b5SJed Brown 
2163c833c3b5SJed Brown    Calling sequence for interphook:
2164c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2165c833c3b5SJed Brown 
2166c833c3b5SJed Brown +  coarse - coarse level DM
2167c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2168c833c3b5SJed Brown .  fine - fine level DM to update
2169c833c3b5SJed Brown -  ctx - optional user-defined function context
2170c833c3b5SJed Brown 
2171c833c3b5SJed Brown    Level: advanced
2172c833c3b5SJed Brown 
2173c833c3b5SJed Brown    Notes:
2174c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2175c833c3b5SJed Brown 
2176c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2177c833c3b5SJed Brown 
2178bb9467b5SJed Brown    This function is currently not available from Fortran.
2179bb9467b5SJed Brown 
2180c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2181c833c3b5SJed Brown @*/
2182c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2183c833c3b5SJed Brown {
2184c833c3b5SJed Brown   PetscErrorCode   ierr;
2185c833c3b5SJed Brown   DMRefineHookLink link,*p;
2186c833c3b5SJed Brown 
2187c833c3b5SJed Brown   PetscFunctionBegin;
2188c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
21893d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
21903d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
21913d8e3701SJed Brown   }
219295dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2193c833c3b5SJed Brown   link->refinehook = refinehook;
2194c833c3b5SJed Brown   link->interphook = interphook;
2195c833c3b5SJed Brown   link->ctx        = ctx;
21960298fd71SBarry Smith   link->next       = NULL;
2197c833c3b5SJed Brown   *p               = link;
2198c833c3b5SJed Brown   PetscFunctionReturn(0);
2199c833c3b5SJed Brown }
2200c833c3b5SJed Brown 
22013d8e3701SJed Brown /*@C
22023d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
22033d8e3701SJed Brown 
22043d8e3701SJed Brown    Logically Collective
22053d8e3701SJed Brown 
22064165533cSJose E. Roman    Input Parameters:
22073d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
22083d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
22093d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
22103d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22113d8e3701SJed Brown 
22123d8e3701SJed Brown    Level: advanced
22133d8e3701SJed Brown 
22143d8e3701SJed Brown    Notes:
22153d8e3701SJed Brown    This function does nothing if the hook is not in the list.
22163d8e3701SJed Brown 
22173d8e3701SJed Brown    This function is currently not available from Fortran.
22183d8e3701SJed Brown 
22193d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
22203d8e3701SJed Brown @*/
22213d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
22223d8e3701SJed Brown {
22233d8e3701SJed Brown   PetscErrorCode   ierr;
22243d8e3701SJed Brown   DMRefineHookLink link,*p;
22253d8e3701SJed Brown 
22263d8e3701SJed Brown   PetscFunctionBegin;
22273d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22283d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
22293d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
22303d8e3701SJed Brown       link = *p;
22313d8e3701SJed Brown       *p = link->next;
22323d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
22333d8e3701SJed Brown       break;
22343d8e3701SJed Brown     }
22353d8e3701SJed Brown   }
22363d8e3701SJed Brown   PetscFunctionReturn(0);
22373d8e3701SJed Brown }
22383d8e3701SJed Brown 
2239c833c3b5SJed Brown /*@
2240c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2241c833c3b5SJed Brown 
2242c833c3b5SJed Brown    Collective if any hooks are
2243c833c3b5SJed Brown 
22444165533cSJose E. Roman    Input Parameters:
2245c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2246e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2247c833c3b5SJed Brown -  fine - finer DM to update
2248c833c3b5SJed Brown 
2249c833c3b5SJed Brown    Level: developer
2250c833c3b5SJed Brown 
2251c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2252c833c3b5SJed Brown @*/
2253c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2254c833c3b5SJed Brown {
2255c833c3b5SJed Brown   PetscErrorCode   ierr;
2256c833c3b5SJed Brown   DMRefineHookLink link;
2257c833c3b5SJed Brown 
2258c833c3b5SJed Brown   PetscFunctionBegin;
2259c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
22608865f1eaSKarl Rupp     if (link->interphook) {
22618865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
22628865f1eaSKarl Rupp     }
22634057135bSMatthew G Knepley   }
226447c6ae99SBarry Smith   PetscFunctionReturn(0);
226547c6ae99SBarry Smith }
226647c6ae99SBarry Smith 
2267eb3f98d2SBarry Smith /*@
22681f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
22691f3379b2SToby Isaac 
22701f3379b2SToby Isaac    Collective on DM
22711f3379b2SToby Isaac 
22724165533cSJose E. Roman    Input Parameters:
22731f3379b2SToby Isaac +  coarse - coarse DM
22741f3379b2SToby Isaac .  fine   - fine DM
22751f3379b2SToby Isaac .  interp - (optional) the matrix computed by DMCreateInterpolation().  Implementations may not need this, but if it
22761f3379b2SToby Isaac             is available it can avoid some recomputation.  If it is provided, MatInterpolate() will be used if
22771f3379b2SToby Isaac             the coarse DM does not have a specialized implementation.
22781f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
22791f3379b2SToby Isaac 
22804165533cSJose E. Roman    Output Parameter:
22811f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
22821f3379b2SToby Isaac 
22831f3379b2SToby Isaac    Level: developer
22841f3379b2SToby Isaac 
22851f3379b2SToby Isaac    Note: This function exists because the interpolation of a solution vector between meshes is not always a linear
22861f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
22871f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
22881f3379b2SToby Isaac    slope-limiting reconstruction.
22891f3379b2SToby Isaac 
22901f3379b2SToby Isaac .seealso DMInterpolate(), DMCreateInterpolation()
22911f3379b2SToby Isaac @*/
22921f3379b2SToby Isaac PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
22931f3379b2SToby Isaac {
22941f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM,DM,Mat,Vec,Vec) = NULL;
22951f3379b2SToby Isaac   PetscErrorCode ierr;
22961f3379b2SToby Isaac 
22971f3379b2SToby Isaac   PetscFunctionBegin;
22981f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22991f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp,MAT_CLASSID,3);
23001f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol,VEC_CLASSID,4);
23011f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol,VEC_CLASSID,5);
23021f3379b2SToby Isaac 
23031f3379b2SToby Isaac   ierr = PetscObjectQueryFunction((PetscObject)coarse,"DMInterpolateSolution_C", &interpsol);CHKERRQ(ierr);
23041f3379b2SToby Isaac   if (interpsol) {
23051f3379b2SToby Isaac     ierr = (*interpsol)(coarse, fine, interp, coarseSol, fineSol);CHKERRQ(ierr);
23061f3379b2SToby Isaac   } else if (interp) {
23071f3379b2SToby Isaac     ierr = MatInterpolate(interp, coarseSol, fineSol);CHKERRQ(ierr);
23081f3379b2SToby Isaac   } else SETERRQ1(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
23091f3379b2SToby Isaac   PetscFunctionReturn(0);
23101f3379b2SToby Isaac }
23111f3379b2SToby Isaac 
23121f3379b2SToby Isaac /*@
2313aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2314eb3f98d2SBarry Smith 
2315eb3f98d2SBarry Smith     Not Collective
2316eb3f98d2SBarry Smith 
2317eb3f98d2SBarry Smith     Input Parameter:
2318eb3f98d2SBarry Smith .   dm - the DM object
2319eb3f98d2SBarry Smith 
2320eb3f98d2SBarry Smith     Output Parameter:
2321eb3f98d2SBarry Smith .   level - number of refinements
2322eb3f98d2SBarry Smith 
2323eb3f98d2SBarry Smith     Level: developer
2324eb3f98d2SBarry Smith 
23256a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2326eb3f98d2SBarry Smith 
2327eb3f98d2SBarry Smith @*/
2328eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2329eb3f98d2SBarry Smith {
2330eb3f98d2SBarry Smith   PetscFunctionBegin;
2331eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2332eb3f98d2SBarry Smith   *level = dm->levelup;
2333eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2334eb3f98d2SBarry Smith }
2335eb3f98d2SBarry Smith 
2336fef3a512SBarry Smith /*@
2337aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2338fef3a512SBarry Smith 
2339fef3a512SBarry Smith     Not Collective
2340fef3a512SBarry Smith 
2341d8d19677SJose E. Roman     Input Parameters:
2342fef3a512SBarry Smith +   dm - the DM object
2343fef3a512SBarry Smith -   level - number of refinements
2344fef3a512SBarry Smith 
2345fef3a512SBarry Smith     Level: advanced
2346fef3a512SBarry Smith 
234795452b02SPatrick Sanan     Notes:
234895452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2349fef3a512SBarry Smith 
2350fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2351fef3a512SBarry Smith 
2352fef3a512SBarry Smith @*/
2353fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2354fef3a512SBarry Smith {
2355fef3a512SBarry Smith   PetscFunctionBegin;
2356fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2357fef3a512SBarry Smith   dm->levelup = level;
2358fef3a512SBarry Smith   PetscFunctionReturn(0);
2359fef3a512SBarry Smith }
2360fef3a512SBarry Smith 
2361d410b0cfSMatthew G. Knepley /*@
2362d410b0cfSMatthew G. Knepley   DMExtrude - Extrude a DM object from a surface
2363d410b0cfSMatthew G. Knepley 
2364d410b0cfSMatthew G. Knepley   Collective on dm
2365d410b0cfSMatthew G. Knepley 
2366d410b0cfSMatthew G. Knepley   Input Parameter:
2367d410b0cfSMatthew G. Knepley + dm     - the DM object
2368d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2369d410b0cfSMatthew G. Knepley 
2370d410b0cfSMatthew G. Knepley   Output Parameter:
2371d410b0cfSMatthew G. Knepley . dme - the extruded DM, or NULL
2372d410b0cfSMatthew G. Knepley 
2373d410b0cfSMatthew G. Knepley   Note: If no extrusion was done, the return value is NULL
2374d410b0cfSMatthew G. Knepley 
2375d410b0cfSMatthew G. Knepley   Level: developer
2376d410b0cfSMatthew G. Knepley 
2377d410b0cfSMatthew G. Knepley .seealso DMRefine(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector()
2378d410b0cfSMatthew G. Knepley @*/
2379d410b0cfSMatthew G. Knepley PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2380d410b0cfSMatthew G. Knepley {
2381d410b0cfSMatthew G. Knepley   PetscErrorCode ierr;
2382d410b0cfSMatthew G. Knepley 
2383d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2384d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2385d410b0cfSMatthew G. Knepley   if (!dm->ops->extrude) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "DM type %s does not implement DMExtrude", ((PetscObject) dm)->type_name);
2386d410b0cfSMatthew G. Knepley   ierr = (*dm->ops->extrude)(dm, layers, dme);CHKERRQ(ierr);
2387d410b0cfSMatthew G. Knepley   if (*dme) {
2388d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
2389d410b0cfSMatthew G. Knepley     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject) dm, (PetscObject) *dme);CHKERRQ(ierr);
2390d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
2391d410b0cfSMatthew G. Knepley     ierr = DMSetMatType(*dme, dm->mattype);CHKERRQ(ierr);
2392d410b0cfSMatthew G. Knepley   }
2393d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2394d410b0cfSMatthew G. Knepley }
2395d410b0cfSMatthew G. Knepley 
2396ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2397ca3d3a14SMatthew G. Knepley {
2398ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2399ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2400ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2401ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2402ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2403ca3d3a14SMatthew G. Knepley }
2404ca3d3a14SMatthew G. Knepley 
2405ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2406ca3d3a14SMatthew G. Knepley {
2407ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2408ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2409ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2410ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2411ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2412ca3d3a14SMatthew G. Knepley }
2413ca3d3a14SMatthew G. Knepley 
2414ca3d3a14SMatthew G. Knepley /*@
2415c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2416ca3d3a14SMatthew G. Knepley 
2417ca3d3a14SMatthew G. Knepley   Input Parameter:
2418ca3d3a14SMatthew G. Knepley . dm - The DM
2419ca3d3a14SMatthew G. Knepley 
2420ca3d3a14SMatthew G. Knepley   Output Parameter:
2421ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2422ca3d3a14SMatthew G. Knepley 
2423ca3d3a14SMatthew G. Knepley   Level: developer
2424ca3d3a14SMatthew G. Knepley 
2425436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2426ca3d3a14SMatthew G. Knepley @*/
2427ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2428ca3d3a14SMatthew G. Knepley {
2429ca3d3a14SMatthew G. Knepley   Vec            tv;
2430ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2431ca3d3a14SMatthew G. Knepley 
2432ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2433ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2434534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2435ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2436ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2437ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2438ca3d3a14SMatthew G. Knepley }
2439ca3d3a14SMatthew G. Knepley 
2440ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2441ca3d3a14SMatthew G. Knepley {
2442ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2443ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2444ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2445ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2446ca3d3a14SMatthew G. Knepley 
2447ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2448ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
244992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2450ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2451ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2452ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
245392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2454ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2455ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2456ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2457ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2458ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2459ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2460ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2461ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2462ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2463ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2464ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2465ca3d3a14SMatthew G. Knepley     }
2466ca3d3a14SMatthew G. Knepley   }
2467ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2468ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2469ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2470ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2471ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2472ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2473ca3d3a14SMatthew G. Knepley       if (dof) {
2474ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2475ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2476ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2477ca3d3a14SMatthew G. Knepley 
2478ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2479ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2480ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2481580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2482ca3d3a14SMatthew G. Knepley       }
2483ca3d3a14SMatthew G. Knepley     }
2484ca3d3a14SMatthew G. Knepley   }
2485ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2486ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2487ca3d3a14SMatthew G. Knepley }
2488ca3d3a14SMatthew G. Knepley 
2489ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2490ca3d3a14SMatthew G. Knepley {
2491ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2492ca3d3a14SMatthew G. Knepley 
2493ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2494ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2495ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2496ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2497ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2498ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2499ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2500ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2501ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2502ca3d3a14SMatthew G. Knepley }
2503ca3d3a14SMatthew G. Knepley 
2504bb9467b5SJed Brown /*@C
2505baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2506baf369e7SPeter Brune 
2507baf369e7SPeter Brune    Logically Collective
2508baf369e7SPeter Brune 
25094165533cSJose E. Roman    Input Parameters:
2510baf369e7SPeter Brune +  dm - the DM
2511baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2512baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
25130298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2514baf369e7SPeter Brune 
2515baf369e7SPeter Brune    Calling sequence for beginhook:
2516baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2517baf369e7SPeter Brune 
2518baf369e7SPeter Brune +  dm - global DM
2519baf369e7SPeter Brune .  g - global vector
2520baf369e7SPeter Brune .  mode - mode
2521baf369e7SPeter Brune .  l - local vector
2522baf369e7SPeter Brune -  ctx - optional user-defined function context
2523baf369e7SPeter Brune 
2524baf369e7SPeter Brune    Calling sequence for endhook:
2525ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2526baf369e7SPeter Brune 
2527baf369e7SPeter Brune +  global - global DM
2528baf369e7SPeter Brune -  ctx - optional user-defined function context
2529baf369e7SPeter Brune 
2530baf369e7SPeter Brune    Level: advanced
2531baf369e7SPeter Brune 
2532baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2533baf369e7SPeter Brune @*/
2534baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2535baf369e7SPeter Brune {
2536baf369e7SPeter Brune   PetscErrorCode          ierr;
2537baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2538baf369e7SPeter Brune 
2539baf369e7SPeter Brune   PetscFunctionBegin;
2540baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2541baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
254295dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2543baf369e7SPeter Brune   link->beginhook = beginhook;
2544baf369e7SPeter Brune   link->endhook   = endhook;
2545baf369e7SPeter Brune   link->ctx       = ctx;
25460298fd71SBarry Smith   link->next      = NULL;
2547baf369e7SPeter Brune   *p              = link;
2548baf369e7SPeter Brune   PetscFunctionReturn(0);
2549baf369e7SPeter Brune }
2550baf369e7SPeter Brune 
25514c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
25524c274da1SToby Isaac {
25534c274da1SToby Isaac   Mat cMat;
25544c274da1SToby Isaac   Vec cVec;
25554c274da1SToby Isaac   PetscSection section, cSec;
25564c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25574c274da1SToby Isaac   PetscErrorCode ierr;
25584c274da1SToby Isaac 
25594c274da1SToby Isaac   PetscFunctionBegin;
25604c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25614c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
25624c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
25635db9a05bSToby Isaac     PetscInt nRows;
25645db9a05bSToby Isaac 
25655db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
25665db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
256792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
25687711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
25694c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
25704c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
25714c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25724c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
25734c274da1SToby Isaac       if (dof) {
25744c274da1SToby Isaac         PetscScalar *vals;
25754c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
25764c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
25774c274da1SToby Isaac       }
25784c274da1SToby Isaac     }
25794c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
25804c274da1SToby Isaac   }
25814c274da1SToby Isaac   PetscFunctionReturn(0);
25824c274da1SToby Isaac }
25834c274da1SToby Isaac 
258447c6ae99SBarry Smith /*@
258501729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
258601729b5cSPatrick Sanan 
2587d083f849SBarry Smith     Neighbor-wise Collective on dm
258801729b5cSPatrick Sanan 
258901729b5cSPatrick Sanan     Input Parameters:
259001729b5cSPatrick Sanan +   dm - the DM object
259101729b5cSPatrick Sanan .   g - the global vector
259201729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
259301729b5cSPatrick Sanan -   l - the local vector
259401729b5cSPatrick Sanan 
259501729b5cSPatrick Sanan     Notes:
259601729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
259701729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
259801729b5cSPatrick Sanan 
259901729b5cSPatrick Sanan     Level: beginner
260001729b5cSPatrick Sanan 
260101729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
260201729b5cSPatrick Sanan 
260301729b5cSPatrick Sanan @*/
260401729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
260501729b5cSPatrick Sanan {
260601729b5cSPatrick Sanan   PetscErrorCode ierr;
260701729b5cSPatrick Sanan 
260801729b5cSPatrick Sanan   PetscFunctionBegin;
260901729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
261001729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
261101729b5cSPatrick Sanan   PetscFunctionReturn(0);
261201729b5cSPatrick Sanan }
261301729b5cSPatrick Sanan 
261401729b5cSPatrick Sanan /*@
261547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
261647c6ae99SBarry Smith 
2617d083f849SBarry Smith     Neighbor-wise Collective on dm
261847c6ae99SBarry Smith 
261947c6ae99SBarry Smith     Input Parameters:
262047c6ae99SBarry Smith +   dm - the DM object
262147c6ae99SBarry Smith .   g - the global vector
262247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
262347c6ae99SBarry Smith -   l - the local vector
262447c6ae99SBarry Smith 
262501729b5cSPatrick Sanan     Level: intermediate
262647c6ae99SBarry Smith 
262701729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
262847c6ae99SBarry Smith 
262947c6ae99SBarry Smith @*/
26307087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
263147c6ae99SBarry Smith {
26327128ae9fSMatthew G Knepley   PetscSF                 sf;
263347c6ae99SBarry Smith   PetscErrorCode          ierr;
2634baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
263547c6ae99SBarry Smith 
263647c6ae99SBarry Smith   PetscFunctionBegin;
2637171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2638baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
26398865f1eaSKarl Rupp     if (link->beginhook) {
26408865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
26418865f1eaSKarl Rupp     }
2642baf369e7SPeter Brune   }
26431bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
26447128ae9fSMatthew G Knepley   if (sf) {
2645ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2646ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2647d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
26487128ae9fSMatthew G Knepley 
264982f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2650a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2651a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2652ad227feaSJunchao Zhang     ierr = PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE);CHKERRQ(ierr);
2653a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2654a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
26557128ae9fSMatthew G Knepley   } else {
265633907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2657843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
26587128ae9fSMatthew G Knepley   }
265947c6ae99SBarry Smith   PetscFunctionReturn(0);
266047c6ae99SBarry Smith }
266147c6ae99SBarry Smith 
266247c6ae99SBarry Smith /*@
266347c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
266447c6ae99SBarry Smith 
2665d083f849SBarry Smith     Neighbor-wise Collective on dm
266647c6ae99SBarry Smith 
266747c6ae99SBarry Smith     Input Parameters:
266847c6ae99SBarry Smith +   dm - the DM object
266947c6ae99SBarry Smith .   g - the global vector
267047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
267147c6ae99SBarry Smith -   l - the local vector
267247c6ae99SBarry Smith 
267301729b5cSPatrick Sanan     Level: intermediate
267447c6ae99SBarry Smith 
267501729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
267647c6ae99SBarry Smith 
267747c6ae99SBarry Smith @*/
26787087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
267947c6ae99SBarry Smith {
26807128ae9fSMatthew G Knepley   PetscSF                 sf;
268147c6ae99SBarry Smith   PetscErrorCode          ierr;
2682ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2683ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2684ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2685baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2686d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
268747c6ae99SBarry Smith 
268847c6ae99SBarry Smith   PetscFunctionBegin;
2689171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26901bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2691ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
26927128ae9fSMatthew G Knepley   if (sf) {
269382f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26947128ae9fSMatthew G Knepley 
2695a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2696a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2697ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray,MPI_REPLACE);CHKERRQ(ierr);
2698a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2699a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
2700ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
27017128ae9fSMatthew G Knepley   } else {
270233907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2703843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
27047128ae9fSMatthew G Knepley   }
27054c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2706baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2707baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2708baf369e7SPeter Brune   }
270947c6ae99SBarry Smith   PetscFunctionReturn(0);
271047c6ae99SBarry Smith }
271147c6ae99SBarry Smith 
2712d4d07f1eSToby Isaac /*@C
2713d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2714d4d07f1eSToby Isaac 
2715d4d07f1eSToby Isaac    Logically Collective
2716d4d07f1eSToby Isaac 
27174165533cSJose E. Roman    Input Parameters:
2718d4d07f1eSToby Isaac +  dm - the DM
2719d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2720d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2721d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2722d4d07f1eSToby Isaac 
2723d4d07f1eSToby Isaac    Calling sequence for beginhook:
2724d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2725d4d07f1eSToby Isaac 
2726d4d07f1eSToby Isaac +  dm - global DM
2727d4d07f1eSToby Isaac .  l - local vector
2728d4d07f1eSToby Isaac .  mode - mode
2729d4d07f1eSToby Isaac .  g - global vector
2730d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2731d4d07f1eSToby Isaac 
2732d4d07f1eSToby Isaac    Calling sequence for endhook:
2733d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2734d4d07f1eSToby Isaac 
2735d4d07f1eSToby Isaac +  global - global DM
2736d4d07f1eSToby Isaac .  l - local vector
2737d4d07f1eSToby Isaac .  mode - mode
2738d4d07f1eSToby Isaac .  g - global vector
2739d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2740d4d07f1eSToby Isaac 
2741d4d07f1eSToby Isaac    Level: advanced
2742d4d07f1eSToby Isaac 
2743d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2744d4d07f1eSToby Isaac @*/
2745d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2746d4d07f1eSToby Isaac {
2747d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2748d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2749d4d07f1eSToby Isaac 
2750d4d07f1eSToby Isaac   PetscFunctionBegin;
2751d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2752d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
275395dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2754d4d07f1eSToby Isaac   link->beginhook = beginhook;
2755d4d07f1eSToby Isaac   link->endhook   = endhook;
2756d4d07f1eSToby Isaac   link->ctx       = ctx;
2757d4d07f1eSToby Isaac   link->next      = NULL;
2758d4d07f1eSToby Isaac   *p              = link;
2759d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2760d4d07f1eSToby Isaac }
2761d4d07f1eSToby Isaac 
27624c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
27634c274da1SToby Isaac {
27644c274da1SToby Isaac   Mat cMat;
27654c274da1SToby Isaac   Vec cVec;
27664c274da1SToby Isaac   PetscSection section, cSec;
27674c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
27684c274da1SToby Isaac   PetscErrorCode ierr;
27694c274da1SToby Isaac 
27704c274da1SToby Isaac   PetscFunctionBegin;
27714c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27724c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
27734c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
27745db9a05bSToby Isaac     PetscInt nRows;
27755db9a05bSToby Isaac 
27765db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
27775db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
277892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
27797711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
27804c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
27814c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27824c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
27834c274da1SToby Isaac       if (dof) {
27844c274da1SToby Isaac         PetscInt d;
27854c274da1SToby Isaac         PetscScalar *vals;
27864c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
27874c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
27884c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
27894c274da1SToby Isaac          * we just extracted */
27904c274da1SToby Isaac         for (d = 0; d < dof; d++) {
27914c274da1SToby Isaac           vals[d] = 0.;
27924c274da1SToby Isaac         }
27934c274da1SToby Isaac       }
27944c274da1SToby Isaac     }
27954c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
27964c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
27974c274da1SToby Isaac   }
27984c274da1SToby Isaac   PetscFunctionReturn(0);
27994c274da1SToby Isaac }
280001729b5cSPatrick Sanan /*@
280101729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
280201729b5cSPatrick Sanan 
2803d083f849SBarry Smith     Neighbor-wise Collective on dm
280401729b5cSPatrick Sanan 
280501729b5cSPatrick Sanan     Input Parameters:
280601729b5cSPatrick Sanan +   dm - the DM object
280701729b5cSPatrick Sanan .   l - the local vector
280801729b5cSPatrick 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.
280901729b5cSPatrick Sanan -   g - the global vector
281001729b5cSPatrick Sanan 
281101729b5cSPatrick Sanan     Notes:
281201729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
281301729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
281401729b5cSPatrick Sanan 
281501729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
281601729b5cSPatrick 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.
281701729b5cSPatrick Sanan 
281801729b5cSPatrick Sanan     Level: beginner
281901729b5cSPatrick Sanan 
282001729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
282101729b5cSPatrick Sanan 
282201729b5cSPatrick Sanan @*/
282301729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
282401729b5cSPatrick Sanan {
282501729b5cSPatrick Sanan   PetscErrorCode ierr;
282601729b5cSPatrick Sanan 
282701729b5cSPatrick Sanan   PetscFunctionBegin;
282801729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
282901729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
283001729b5cSPatrick Sanan   PetscFunctionReturn(0);
283101729b5cSPatrick Sanan }
28324c274da1SToby Isaac 
283347c6ae99SBarry Smith /*@
283401729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
28359a42bb27SBarry Smith 
2836d083f849SBarry Smith     Neighbor-wise Collective on dm
28379a42bb27SBarry Smith 
28389a42bb27SBarry Smith     Input Parameters:
28399a42bb27SBarry Smith +   dm - the DM object
2840f6813fd5SJed Brown .   l - the local vector
28411eb28f2eSBarry 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.
28421eb28f2eSBarry Smith -   g - the global vector
28439a42bb27SBarry Smith 
284495452b02SPatrick Sanan     Notes:
284595452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
284684330215SMatthew 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.
28479a42bb27SBarry Smith 
284801729b5cSPatrick Sanan     Level: intermediate
28499a42bb27SBarry Smith 
285001729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
28519a42bb27SBarry Smith 
28529a42bb27SBarry Smith @*/
28537087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
28549a42bb27SBarry Smith {
28557128ae9fSMatthew G Knepley   PetscSF                 sf;
285684330215SMatthew G. Knepley   PetscSection            s, gs;
2857d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2858ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2859ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2860ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2861fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
286284330215SMatthew G. Knepley   PetscErrorCode          ierr;
2863d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
28649a42bb27SBarry Smith 
28659a42bb27SBarry Smith   PetscFunctionBegin;
2866171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2867d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2868d4d07f1eSToby Isaac     if (link->beginhook) {
2869d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2870d4d07f1eSToby Isaac     }
2871d4d07f1eSToby Isaac   }
28724c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
28731bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
287492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
28757128ae9fSMatthew G Knepley   switch (mode) {
28767128ae9fSMatthew G Knepley   case INSERT_VALUES:
28777128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2878304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
287984330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
28807128ae9fSMatthew G Knepley   case ADD_VALUES:
28817128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2882304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
288384330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
28847128ae9fSMatthew G Knepley   default:
288582f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
28867128ae9fSMatthew G Knepley   }
2887ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2888ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2889ca3d3a14SMatthew G. Knepley     if (transform) {
2890ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2891ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2892ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2893ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2894fa88e482SJed Brown     } else if (isInsert) {
2895ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2896fa88e482SJed Brown     } else {
2897a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2898fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2899ca3d3a14SMatthew G. Knepley     }
2900fa88e482SJed Brown     if (s && isInsert) {
29017128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2902fa88e482SJed Brown     } else {
2903a256111fSJunchao Zhang       ierr = VecGetArrayAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2904fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2905fa88e482SJed Brown     }
2906ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2907d0295fc0SJunchao Zhang       ierr = PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM);CHKERRQ(ierr);
290884330215SMatthew G. Knepley     } else if (s && isInsert) {
290984330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
291084330215SMatthew G. Knepley 
2911e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
291284330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
291384330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
291484330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2915b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
291684330215SMatthew G. Knepley 
291784330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
291803442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
291984330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2920b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
292184330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
292284330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2923b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
292403442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2925b3b16f48SMatthew 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);
2926b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2927b3b16f48SMatthew G. Knepley         if (!gcdof) {
292884330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2929b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2930b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
293184330215SMatthew G. Knepley           const PetscInt *cdofs;
293284330215SMatthew G. Knepley           PetscInt        cind = 0;
293384330215SMatthew G. Knepley 
293484330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2935b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
293684330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2937b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
293884330215SMatthew G. Knepley           }
2939b3b16f48SMatthew 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);
294084330215SMatthew G. Knepley       }
2941ca3d3a14SMatthew G. Knepley     }
2942fa88e482SJed Brown     if (g_inplace) {
2943a256111fSJunchao Zhang       ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
2944fa88e482SJed Brown     } else {
29457128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2946fa88e482SJed Brown     }
2947ca3d3a14SMatthew G. Knepley     if (transform) {
2948ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2949ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2950fa88e482SJed Brown     } else if (l_inplace) {
2951a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
2952ca3d3a14SMatthew G. Knepley     } else {
2953ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2954ca3d3a14SMatthew G. Knepley     }
29557128ae9fSMatthew G Knepley   } else {
2956b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2957843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
29587128ae9fSMatthew G Knepley   }
29599a42bb27SBarry Smith   PetscFunctionReturn(0);
29609a42bb27SBarry Smith }
29619a42bb27SBarry Smith 
29629a42bb27SBarry Smith /*@
29639a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
296447c6ae99SBarry Smith 
2965d083f849SBarry Smith     Neighbor-wise Collective on dm
296647c6ae99SBarry Smith 
296747c6ae99SBarry Smith     Input Parameters:
296847c6ae99SBarry Smith +   dm - the DM object
2969f6813fd5SJed Brown .   l - the local vector
297047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2971f6813fd5SJed Brown -   g - the global vector
297247c6ae99SBarry Smith 
297301729b5cSPatrick Sanan     Level: intermediate
297447c6ae99SBarry Smith 
2975e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
297647c6ae99SBarry Smith 
297747c6ae99SBarry Smith @*/
29787087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
297947c6ae99SBarry Smith {
29807128ae9fSMatthew G Knepley   PetscSF                 sf;
298184330215SMatthew G. Knepley   PetscSection            s;
2982d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2983ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
298484330215SMatthew G. Knepley   PetscErrorCode          ierr;
298547c6ae99SBarry Smith 
298647c6ae99SBarry Smith   PetscFunctionBegin;
2987171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29881bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
298992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
29907128ae9fSMatthew G Knepley   switch (mode) {
29917128ae9fSMatthew G Knepley   case INSERT_VALUES:
29927128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
299384330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
29947128ae9fSMatthew G Knepley   case ADD_VALUES:
29957128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
299684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
29977128ae9fSMatthew G Knepley   default:
299882f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
29997128ae9fSMatthew G Knepley   }
300084330215SMatthew G. Knepley   if (sf && !isInsert) {
3001ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3002ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3003ca3d3a14SMatthew G. Knepley     Vec                tmpl;
300484330215SMatthew G. Knepley 
3005ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
3006ca3d3a14SMatthew G. Knepley     if (transform) {
3007ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
3008ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
3009ca3d3a14SMatthew G. Knepley     } else {
3010a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, NULL);CHKERRQ(ierr);
3011ca3d3a14SMatthew G. Knepley     }
3012a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(g, &gArray, NULL);CHKERRQ(ierr);
3013a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
3014ca3d3a14SMatthew G. Knepley     if (transform) {
3015ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
3016ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
3017ca3d3a14SMatthew G. Knepley     } else {
3018a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
3019ca3d3a14SMatthew G. Knepley     }
3020a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
302184330215SMatthew G. Knepley   } else if (s && isInsert) {
30227128ae9fSMatthew G Knepley   } else {
3023b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
3024843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
30257128ae9fSMatthew G Knepley   }
3026d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
3027d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
3028d4d07f1eSToby Isaac   }
302947c6ae99SBarry Smith   PetscFunctionReturn(0);
303047c6ae99SBarry Smith }
303147c6ae99SBarry Smith 
3032f089877aSRichard Tran Mills /*@
3033bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
3034bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3035d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
3036f089877aSRichard Tran Mills 
3037d083f849SBarry Smith    Neighbor-wise Collective on dm
3038f089877aSRichard Tran Mills 
3039f089877aSRichard Tran Mills    Input Parameters:
3040f089877aSRichard Tran Mills +  dm - the DM object
3041bc0a1609SRichard Tran Mills .  g - the original local vector
3042bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3043f089877aSRichard Tran Mills 
3044bc0a1609SRichard Tran Mills    Output Parameter:
3045bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3046f089877aSRichard Tran Mills 
3047f089877aSRichard Tran Mills    Level: intermediate
3048f089877aSRichard Tran Mills 
3049bc0a1609SRichard Tran Mills    Notes:
3050bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3051bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3052bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3053bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3054bc0a1609SRichard Tran Mills 
3055bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3056f089877aSRichard Tran Mills 
3057f089877aSRichard Tran Mills @*/
3058f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
3059f089877aSRichard Tran Mills {
3060f089877aSRichard Tran Mills   PetscErrorCode          ierr;
3061f089877aSRichard Tran Mills 
3062f089877aSRichard Tran Mills   PetscFunctionBegin;
3063f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3064bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3065f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
3066f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3067f089877aSRichard Tran Mills }
3068f089877aSRichard Tran Mills 
3069f089877aSRichard Tran Mills /*@
3070bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
3071bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3072d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
3073f089877aSRichard Tran Mills 
3074d083f849SBarry Smith    Neighbor-wise Collective on dm
3075f089877aSRichard Tran Mills 
3076f089877aSRichard Tran Mills    Input Parameters:
3077bc0a1609SRichard Tran Mills +  da - the DM object
3078bc0a1609SRichard Tran Mills .  g - the original local vector
3079bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3080f089877aSRichard Tran Mills 
3081bc0a1609SRichard Tran Mills    Output Parameter:
3082bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3083f089877aSRichard Tran Mills 
3084f089877aSRichard Tran Mills    Level: intermediate
3085f089877aSRichard Tran Mills 
3086bc0a1609SRichard Tran Mills    Notes:
3087bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3088bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3089bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3090bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3091bc0a1609SRichard Tran Mills 
3092bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3093f089877aSRichard Tran Mills 
3094f089877aSRichard Tran Mills @*/
3095f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
3096f089877aSRichard Tran Mills {
3097f089877aSRichard Tran Mills   PetscErrorCode          ierr;
3098f089877aSRichard Tran Mills 
3099f089877aSRichard Tran Mills   PetscFunctionBegin;
3100f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3101bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3102f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
3103f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3104f089877aSRichard Tran Mills }
3105f089877aSRichard Tran Mills 
310647c6ae99SBarry Smith /*@
310747c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
310847c6ae99SBarry Smith 
3109d083f849SBarry Smith     Collective on dm
311047c6ae99SBarry Smith 
3111d8d19677SJose E. Roman     Input Parameters:
311247c6ae99SBarry Smith +   dm - the DM object
311391d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
311447c6ae99SBarry Smith 
311547c6ae99SBarry Smith     Output Parameter:
311647c6ae99SBarry Smith .   dmc - the coarsened DM
311747c6ae99SBarry Smith 
311847c6ae99SBarry Smith     Level: developer
311947c6ae99SBarry Smith 
3120e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
312147c6ae99SBarry Smith 
312247c6ae99SBarry Smith @*/
31237087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
312447c6ae99SBarry Smith {
312547c6ae99SBarry Smith   PetscErrorCode    ierr;
3126b17ce1afSJed Brown   DMCoarsenHookLink link;
312747c6ae99SBarry Smith 
312847c6ae99SBarry Smith   PetscFunctionBegin;
3129171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3130b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
313147a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
313247c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
3133b9d85ea2SLisandro Dalcin   if (*dmc) {
3134a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
313543842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
31368cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
3137644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
31380598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3139656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
3140e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
3141b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
3142b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
3143b17ce1afSJed Brown     }
3144b9d85ea2SLisandro Dalcin   }
314547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
3146b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3147b17ce1afSJed Brown   PetscFunctionReturn(0);
3148b17ce1afSJed Brown }
3149b17ce1afSJed Brown 
3150bb9467b5SJed Brown /*@C
3151b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3152b17ce1afSJed Brown 
3153b17ce1afSJed Brown    Logically Collective
3154b17ce1afSJed Brown 
31554165533cSJose E. Roman    Input Parameters:
3156b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3157b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3158b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
31590298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3160b17ce1afSJed Brown 
3161b17ce1afSJed Brown    Calling sequence of coarsenhook:
3162b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3163b17ce1afSJed Brown 
3164b17ce1afSJed Brown +  fine - fine level DM
3165b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3166b17ce1afSJed Brown -  ctx - optional user-defined function context
3167b17ce1afSJed Brown 
3168b17ce1afSJed Brown    Calling sequence for restricthook:
3169c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3170b17ce1afSJed Brown 
3171b17ce1afSJed Brown +  fine - fine level DM
3172b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3173c833c3b5SJed Brown .  rscale - scaling vector for restriction
3174c833c3b5SJed Brown .  inject - matrix restricting by injection
3175b17ce1afSJed Brown .  coarse - coarse level DM to update
3176b17ce1afSJed Brown -  ctx - optional user-defined function context
3177b17ce1afSJed Brown 
3178b17ce1afSJed Brown    Level: advanced
3179b17ce1afSJed Brown 
3180b17ce1afSJed Brown    Notes:
3181b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3182b17ce1afSJed Brown 
3183b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3184b17ce1afSJed Brown 
3185b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3186b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3187b17ce1afSJed Brown 
3188bb9467b5SJed Brown    This function is currently not available from Fortran.
3189bb9467b5SJed Brown 
3190dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3191b17ce1afSJed Brown @*/
3192b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3193b17ce1afSJed Brown {
3194b17ce1afSJed Brown   PetscErrorCode    ierr;
3195b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3196b17ce1afSJed Brown 
3197b17ce1afSJed Brown   PetscFunctionBegin;
3198b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
31991e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
32001e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
32011e3d8eccSJed Brown   }
320295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
3203b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3204b17ce1afSJed Brown   link->restricthook = restricthook;
3205b17ce1afSJed Brown   link->ctx          = ctx;
32060298fd71SBarry Smith   link->next         = NULL;
3207b17ce1afSJed Brown   *p                 = link;
3208b17ce1afSJed Brown   PetscFunctionReturn(0);
3209b17ce1afSJed Brown }
3210b17ce1afSJed Brown 
3211dc822a44SJed Brown /*@C
3212dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3213dc822a44SJed Brown 
3214dc822a44SJed Brown    Logically Collective
3215dc822a44SJed Brown 
32164165533cSJose E. Roman    Input Parameters:
3217dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3218dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3219dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3220dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3221dc822a44SJed Brown 
3222dc822a44SJed Brown    Level: advanced
3223dc822a44SJed Brown 
3224dc822a44SJed Brown    Notes:
3225dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3226dc822a44SJed Brown 
3227dc822a44SJed Brown    This function is currently not available from Fortran.
3228dc822a44SJed Brown 
3229dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3230dc822a44SJed Brown @*/
3231dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3232dc822a44SJed Brown {
3233dc822a44SJed Brown   PetscErrorCode    ierr;
3234dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3235dc822a44SJed Brown 
3236dc822a44SJed Brown   PetscFunctionBegin;
3237dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3238dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3239dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3240dc822a44SJed Brown       link = *p;
3241dc822a44SJed Brown       *p = link->next;
3242dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3243dc822a44SJed Brown       break;
3244dc822a44SJed Brown     }
3245dc822a44SJed Brown   }
3246dc822a44SJed Brown   PetscFunctionReturn(0);
3247dc822a44SJed Brown }
3248dc822a44SJed Brown 
3249b17ce1afSJed Brown /*@
3250b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3251b17ce1afSJed Brown 
3252b17ce1afSJed Brown    Collective if any hooks are
3253b17ce1afSJed Brown 
32544165533cSJose E. Roman    Input Parameters:
3255b17ce1afSJed Brown +  fine - finer DM to use as a base
3256b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3257e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3258b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3259e91eccc2SStefano Zampini -  coarse - coarser DM to update
3260b17ce1afSJed Brown 
3261b17ce1afSJed Brown    Level: developer
3262b17ce1afSJed Brown 
3263b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3264b17ce1afSJed Brown @*/
3265b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3266b17ce1afSJed Brown {
3267b17ce1afSJed Brown   PetscErrorCode    ierr;
3268b17ce1afSJed Brown   DMCoarsenHookLink link;
3269b17ce1afSJed Brown 
3270b17ce1afSJed Brown   PetscFunctionBegin;
3271b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
32728865f1eaSKarl Rupp     if (link->restricthook) {
32738865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
32748865f1eaSKarl Rupp     }
3275b17ce1afSJed Brown   }
327647c6ae99SBarry Smith   PetscFunctionReturn(0);
327747c6ae99SBarry Smith }
327847c6ae99SBarry Smith 
3279bb9467b5SJed Brown /*@C
3280be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
32815dbd56e3SPeter Brune 
3282d083f849SBarry Smith    Logically Collective on global
32835dbd56e3SPeter Brune 
32844165533cSJose E. Roman    Input Parameters:
32855dbd56e3SPeter Brune +  global - global DM
3286ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
32875dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
32880298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
32895dbd56e3SPeter Brune 
3290ec4806b8SPeter Brune    Calling sequence for ddhook:
3291ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3292ec4806b8SPeter Brune 
3293ec4806b8SPeter Brune +  global - global DM
3294ec4806b8SPeter Brune .  block  - block DM
3295ec4806b8SPeter Brune -  ctx - optional user-defined function context
3296ec4806b8SPeter Brune 
32975dbd56e3SPeter Brune    Calling sequence for restricthook:
3298ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
32995dbd56e3SPeter Brune 
33005dbd56e3SPeter Brune +  global - global DM
33015dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
33025dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3303ec4806b8SPeter Brune .  block  - block DM
33045dbd56e3SPeter Brune -  ctx - optional user-defined function context
33055dbd56e3SPeter Brune 
33065dbd56e3SPeter Brune    Level: advanced
33075dbd56e3SPeter Brune 
33085dbd56e3SPeter Brune    Notes:
3309ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
33105dbd56e3SPeter Brune 
33115dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
33125dbd56e3SPeter Brune 
33135dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3314ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
33155dbd56e3SPeter Brune 
3316bb9467b5SJed Brown    This function is currently not available from Fortran.
3317bb9467b5SJed Brown 
33185dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
33195dbd56e3SPeter Brune @*/
3320be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
33215dbd56e3SPeter Brune {
33225dbd56e3SPeter Brune   PetscErrorCode      ierr;
3323be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
33245dbd56e3SPeter Brune 
33255dbd56e3SPeter Brune   PetscFunctionBegin;
33265dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3327b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3328b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3329b3a6b972SJed Brown   }
333095dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
33315dbd56e3SPeter Brune   link->restricthook = restricthook;
3332be081cd6SPeter Brune   link->ddhook       = ddhook;
33335dbd56e3SPeter Brune   link->ctx          = ctx;
33340298fd71SBarry Smith   link->next         = NULL;
33355dbd56e3SPeter Brune   *p                 = link;
33365dbd56e3SPeter Brune   PetscFunctionReturn(0);
33375dbd56e3SPeter Brune }
33385dbd56e3SPeter Brune 
3339b3a6b972SJed Brown /*@C
3340b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3341b3a6b972SJed Brown 
3342b3a6b972SJed Brown    Logically Collective
3343b3a6b972SJed Brown 
33444165533cSJose E. Roman    Input Parameters:
3345b3a6b972SJed Brown +  global - global DM
3346b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3347b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3348b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3349b3a6b972SJed Brown 
3350b3a6b972SJed Brown    Level: advanced
3351b3a6b972SJed Brown 
3352b3a6b972SJed Brown    Notes:
3353b3a6b972SJed Brown 
3354b3a6b972SJed Brown    This function is currently not available from Fortran.
3355b3a6b972SJed Brown 
3356b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3357b3a6b972SJed Brown @*/
3358b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3359b3a6b972SJed Brown {
3360b3a6b972SJed Brown   PetscErrorCode      ierr;
3361b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3362b3a6b972SJed Brown 
3363b3a6b972SJed Brown   PetscFunctionBegin;
3364b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3365b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3366b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3367b3a6b972SJed Brown       link = *p;
3368b3a6b972SJed Brown       *p = link->next;
3369b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3370b3a6b972SJed Brown       break;
3371b3a6b972SJed Brown     }
3372b3a6b972SJed Brown   }
3373b3a6b972SJed Brown   PetscFunctionReturn(0);
3374b3a6b972SJed Brown }
3375b3a6b972SJed Brown 
33765dbd56e3SPeter Brune /*@
3377be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
33785dbd56e3SPeter Brune 
33795dbd56e3SPeter Brune    Collective if any hooks are
33805dbd56e3SPeter Brune 
33814165533cSJose E. Roman    Input Parameters:
33825dbd56e3SPeter Brune +  fine - finer DM to use as a base
3383be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3384be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
33855dbd56e3SPeter Brune -  coarse - coarer DM to update
33865dbd56e3SPeter Brune 
33875dbd56e3SPeter Brune    Level: developer
33885dbd56e3SPeter Brune 
33895dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
33905dbd56e3SPeter Brune @*/
3391be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
33925dbd56e3SPeter Brune {
33935dbd56e3SPeter Brune   PetscErrorCode      ierr;
3394be081cd6SPeter Brune   DMSubDomainHookLink link;
33955dbd56e3SPeter Brune 
33965dbd56e3SPeter Brune   PetscFunctionBegin;
3397be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
33988865f1eaSKarl Rupp     if (link->restricthook) {
33998865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
34008865f1eaSKarl Rupp     }
34015dbd56e3SPeter Brune   }
34025dbd56e3SPeter Brune   PetscFunctionReturn(0);
34035dbd56e3SPeter Brune }
34045dbd56e3SPeter Brune 
34055fe1f584SPeter Brune /*@
34066a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
34075fe1f584SPeter Brune 
34085fe1f584SPeter Brune     Not Collective
34095fe1f584SPeter Brune 
34105fe1f584SPeter Brune     Input Parameter:
34115fe1f584SPeter Brune .   dm - the DM object
34125fe1f584SPeter Brune 
34135fe1f584SPeter Brune     Output Parameter:
34146a7d9d85SPeter Brune .   level - number of coarsenings
34155fe1f584SPeter Brune 
34165fe1f584SPeter Brune     Level: developer
34175fe1f584SPeter Brune 
34186a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
34195fe1f584SPeter Brune 
34205fe1f584SPeter Brune @*/
34215fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
34225fe1f584SPeter Brune {
34235fe1f584SPeter Brune   PetscFunctionBegin;
34245fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3425b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
34265fe1f584SPeter Brune   *level = dm->leveldown;
34275fe1f584SPeter Brune   PetscFunctionReturn(0);
34285fe1f584SPeter Brune }
34295fe1f584SPeter Brune 
34309a64c4a8SMatthew G. Knepley /*@
34319a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
34329a64c4a8SMatthew G. Knepley 
34339a64c4a8SMatthew G. Knepley     Not Collective
34349a64c4a8SMatthew G. Knepley 
34359a64c4a8SMatthew G. Knepley     Input Parameters:
34369a64c4a8SMatthew G. Knepley +   dm - the DM object
34379a64c4a8SMatthew G. Knepley -   level - number of coarsenings
34389a64c4a8SMatthew G. Knepley 
34399a64c4a8SMatthew G. Knepley     Level: developer
34409a64c4a8SMatthew G. Knepley 
34419a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
34429a64c4a8SMatthew G. Knepley @*/
34439a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
34449a64c4a8SMatthew G. Knepley {
34459a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
34469a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34479a64c4a8SMatthew G. Knepley   dm->leveldown = level;
34489a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
34499a64c4a8SMatthew G. Knepley }
34509a64c4a8SMatthew G. Knepley 
345147c6ae99SBarry Smith /*@C
345247c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
345347c6ae99SBarry Smith 
3454d083f849SBarry Smith     Collective on dm
345547c6ae99SBarry Smith 
3456d8d19677SJose E. Roman     Input Parameters:
345747c6ae99SBarry Smith +   dm - the DM object
345847c6ae99SBarry Smith -   nlevels - the number of levels of refinement
345947c6ae99SBarry Smith 
346047c6ae99SBarry Smith     Output Parameter:
346147c6ae99SBarry Smith .   dmf - the refined DM hierarchy
346247c6ae99SBarry Smith 
346347c6ae99SBarry Smith     Level: developer
346447c6ae99SBarry Smith 
3465e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
346647c6ae99SBarry Smith 
346747c6ae99SBarry Smith @*/
34687087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
346947c6ae99SBarry Smith {
347047c6ae99SBarry Smith   PetscErrorCode ierr;
347147c6ae99SBarry Smith 
347247c6ae99SBarry Smith   PetscFunctionBegin;
3473171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3474ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
347547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3476b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
347747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
347847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
347947c6ae99SBarry Smith   } else if (dm->ops->refine) {
348047c6ae99SBarry Smith     PetscInt i;
348147c6ae99SBarry Smith 
3482ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
348347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3484ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
348547c6ae99SBarry Smith     }
3486ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
348747c6ae99SBarry Smith   PetscFunctionReturn(0);
348847c6ae99SBarry Smith }
348947c6ae99SBarry Smith 
349047c6ae99SBarry Smith /*@C
349147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
349247c6ae99SBarry Smith 
3493d083f849SBarry Smith     Collective on dm
349447c6ae99SBarry Smith 
3495d8d19677SJose E. Roman     Input Parameters:
349647c6ae99SBarry Smith +   dm - the DM object
349747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
349847c6ae99SBarry Smith 
349947c6ae99SBarry Smith     Output Parameter:
350047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
350147c6ae99SBarry Smith 
350247c6ae99SBarry Smith     Level: developer
350347c6ae99SBarry Smith 
3504e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
350547c6ae99SBarry Smith 
350647c6ae99SBarry Smith @*/
35077087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
350847c6ae99SBarry Smith {
350947c6ae99SBarry Smith   PetscErrorCode ierr;
351047c6ae99SBarry Smith 
351147c6ae99SBarry Smith   PetscFunctionBegin;
3512171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3513ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
351447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
351547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
351647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
351747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
351847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
351947c6ae99SBarry Smith     PetscInt i;
352047c6ae99SBarry Smith 
3521ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
352247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3523ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
352447c6ae99SBarry Smith     }
3525ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
352647c6ae99SBarry Smith   PetscFunctionReturn(0);
352747c6ae99SBarry Smith }
352847c6ae99SBarry Smith 
35291a266240SBarry Smith /*@C
35301a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
35311a266240SBarry Smith 
35321a266240SBarry Smith     Not Collective
35331a266240SBarry Smith 
35341a266240SBarry Smith     Input Parameters:
35351a266240SBarry Smith +   dm - the DM object
35361a266240SBarry Smith -   destroy - the destroy function
35371a266240SBarry Smith 
35381a266240SBarry Smith     Level: intermediate
35391a266240SBarry Smith 
3540e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
35411a266240SBarry Smith 
3542f07f9ceaSJed Brown @*/
35431a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
35441a266240SBarry Smith {
35451a266240SBarry Smith   PetscFunctionBegin;
3546171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35471a266240SBarry Smith   dm->ctxdestroy = destroy;
35481a266240SBarry Smith   PetscFunctionReturn(0);
35491a266240SBarry Smith }
35501a266240SBarry Smith 
3551b07ff414SBarry Smith /*@
35521b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
355347c6ae99SBarry Smith 
355447c6ae99SBarry Smith     Not Collective
355547c6ae99SBarry Smith 
355647c6ae99SBarry Smith     Input Parameters:
355747c6ae99SBarry Smith +   dm - the DM object
355847c6ae99SBarry Smith -   ctx - the user context
355947c6ae99SBarry Smith 
356047c6ae99SBarry Smith     Level: intermediate
356147c6ae99SBarry Smith 
3562e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
356347c6ae99SBarry Smith 
356447c6ae99SBarry Smith @*/
35651b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
356647c6ae99SBarry Smith {
356747c6ae99SBarry Smith   PetscFunctionBegin;
3568171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
356947c6ae99SBarry Smith   dm->ctx = ctx;
357047c6ae99SBarry Smith   PetscFunctionReturn(0);
357147c6ae99SBarry Smith }
357247c6ae99SBarry Smith 
357347c6ae99SBarry Smith /*@
35741b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
357547c6ae99SBarry Smith 
357647c6ae99SBarry Smith     Not Collective
357747c6ae99SBarry Smith 
357847c6ae99SBarry Smith     Input Parameter:
357947c6ae99SBarry Smith .   dm - the DM object
358047c6ae99SBarry Smith 
358147c6ae99SBarry Smith     Output Parameter:
358247c6ae99SBarry Smith .   ctx - the user context
358347c6ae99SBarry Smith 
358447c6ae99SBarry Smith     Level: intermediate
358547c6ae99SBarry Smith 
3586e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
358747c6ae99SBarry Smith 
358847c6ae99SBarry Smith @*/
35891b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
359047c6ae99SBarry Smith {
359147c6ae99SBarry Smith   PetscFunctionBegin;
3592171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35931b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
359447c6ae99SBarry Smith   PetscFunctionReturn(0);
359547c6ae99SBarry Smith }
359647c6ae99SBarry Smith 
359708da532bSDmitry Karpeev /*@C
3598df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
359908da532bSDmitry Karpeev 
3600d083f849SBarry Smith     Logically Collective on dm
360108da532bSDmitry Karpeev 
3602d8d19677SJose E. Roman     Input Parameters:
360308da532bSDmitry Karpeev +   dm - the DM object
36040298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
360508da532bSDmitry Karpeev 
360608da532bSDmitry Karpeev     Level: intermediate
360708da532bSDmitry Karpeev 
3608835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
360908da532bSDmitry Karpeev          DMSetJacobian()
361008da532bSDmitry Karpeev 
361108da532bSDmitry Karpeev @*/
361208da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
361308da532bSDmitry Karpeev {
361408da532bSDmitry Karpeev   PetscFunctionBegin;
36155a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
361608da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
361708da532bSDmitry Karpeev   PetscFunctionReturn(0);
361808da532bSDmitry Karpeev }
361908da532bSDmitry Karpeev 
362008da532bSDmitry Karpeev /*@
362108da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
362208da532bSDmitry Karpeev 
362308da532bSDmitry Karpeev     Not Collective
362408da532bSDmitry Karpeev 
362508da532bSDmitry Karpeev     Input Parameter:
362608da532bSDmitry Karpeev .   dm - the DM object to destroy
362708da532bSDmitry Karpeev 
362808da532bSDmitry Karpeev     Output Parameter:
362908da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
363008da532bSDmitry Karpeev 
363108da532bSDmitry Karpeev     Level: developer
363208da532bSDmitry Karpeev 
363374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
363408da532bSDmitry Karpeev 
363508da532bSDmitry Karpeev @*/
363608da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
363708da532bSDmitry Karpeev {
363808da532bSDmitry Karpeev   PetscFunctionBegin;
36395a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3640534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
364108da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
364208da532bSDmitry Karpeev   PetscFunctionReturn(0);
364308da532bSDmitry Karpeev }
364408da532bSDmitry Karpeev 
364508da532bSDmitry Karpeev /*@C
364608da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
364708da532bSDmitry Karpeev 
3648d083f849SBarry Smith     Logically Collective on dm
364908da532bSDmitry Karpeev 
3650f899ff85SJose E. Roman     Input Parameter:
3651907376e6SBarry Smith .   dm - the DM object
365208da532bSDmitry Karpeev 
365308da532bSDmitry Karpeev     Output parameters:
365408da532bSDmitry Karpeev +   xl - lower bound
365508da532bSDmitry Karpeev -   xu - upper bound
365608da532bSDmitry Karpeev 
3657907376e6SBarry Smith     Level: advanced
3658907376e6SBarry Smith 
365995452b02SPatrick Sanan     Notes:
366095452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
366108da532bSDmitry Karpeev 
366274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
366308da532bSDmitry Karpeev 
366408da532bSDmitry Karpeev @*/
366508da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
366608da532bSDmitry Karpeev {
366708da532bSDmitry Karpeev   PetscErrorCode ierr;
36685fd66863SKarl Rupp 
366908da532bSDmitry Karpeev   PetscFunctionBegin;
36705a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
367108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
36725a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3673b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
367408da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
367508da532bSDmitry Karpeev   PetscFunctionReturn(0);
367608da532bSDmitry Karpeev }
367708da532bSDmitry Karpeev 
3678b0ae01b7SPeter Brune /*@
3679b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3680b0ae01b7SPeter Brune 
3681b0ae01b7SPeter Brune     Not Collective
3682b0ae01b7SPeter Brune 
3683b0ae01b7SPeter Brune     Input Parameter:
3684b0ae01b7SPeter Brune .   dm - the DM object
3685b0ae01b7SPeter Brune 
3686b0ae01b7SPeter Brune     Output Parameter:
3687b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3688b0ae01b7SPeter Brune 
3689b0ae01b7SPeter Brune     Level: developer
3690b0ae01b7SPeter Brune 
36911565f0a7SPatrick Sanan .seealso DMCreateColoring()
3692b0ae01b7SPeter Brune 
3693b0ae01b7SPeter Brune @*/
3694b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3695b0ae01b7SPeter Brune {
3696b0ae01b7SPeter Brune   PetscFunctionBegin;
36975a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3698534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3699b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3700b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3701b0ae01b7SPeter Brune }
3702b0ae01b7SPeter Brune 
37033ad4599aSBarry Smith /*@
37043ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
37053ad4599aSBarry Smith 
37063ad4599aSBarry Smith     Not Collective
37073ad4599aSBarry Smith 
37083ad4599aSBarry Smith     Input Parameter:
37093ad4599aSBarry Smith .   dm - the DM object
37103ad4599aSBarry Smith 
37113ad4599aSBarry Smith     Output Parameter:
37123ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
37133ad4599aSBarry Smith 
37143ad4599aSBarry Smith     Level: developer
37153ad4599aSBarry Smith 
37161565f0a7SPatrick Sanan .seealso DMCreateRestriction()
37173ad4599aSBarry Smith 
37183ad4599aSBarry Smith @*/
37193ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
37203ad4599aSBarry Smith {
37213ad4599aSBarry Smith   PetscFunctionBegin;
37225a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3723534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37243ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
37253ad4599aSBarry Smith   PetscFunctionReturn(0);
37263ad4599aSBarry Smith }
37273ad4599aSBarry Smith 
3728a7058e45SLawrence Mitchell /*@
3729a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3730a7058e45SLawrence Mitchell 
3731a7058e45SLawrence Mitchell     Not Collective
3732a7058e45SLawrence Mitchell 
3733a7058e45SLawrence Mitchell     Input Parameter:
3734a7058e45SLawrence Mitchell .   dm - the DM object
3735a7058e45SLawrence Mitchell 
3736a7058e45SLawrence Mitchell     Output Parameter:
3737a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3738a7058e45SLawrence Mitchell 
3739a7058e45SLawrence Mitchell     Level: developer
3740a7058e45SLawrence Mitchell 
37411565f0a7SPatrick Sanan .seealso DMCreateInjection()
3742a7058e45SLawrence Mitchell 
3743a7058e45SLawrence Mitchell @*/
3744a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3745a7058e45SLawrence Mitchell {
37464a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
37475a84ad33SLisandro Dalcin 
3748a7058e45SLawrence Mitchell   PetscFunctionBegin;
37495a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3750534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37515a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
37524a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
37535a84ad33SLisandro Dalcin   } else {
37545a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
37555a84ad33SLisandro Dalcin   }
3756a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3757a7058e45SLawrence Mitchell }
3758a7058e45SLawrence Mitchell 
37590298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3760264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3761264ace61SBarry Smith 
3762264ace61SBarry Smith /*@C
3763264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3764264ace61SBarry Smith 
3765d083f849SBarry Smith   Collective on dm
3766264ace61SBarry Smith 
3767264ace61SBarry Smith   Input Parameters:
3768264ace61SBarry Smith + dm     - The DM object
3769264ace61SBarry Smith - method - The name of the DM type
3770264ace61SBarry Smith 
3771264ace61SBarry Smith   Options Database Key:
3772264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3773264ace61SBarry Smith 
3774264ace61SBarry Smith   Notes:
3775e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3776264ace61SBarry Smith 
3777264ace61SBarry Smith   Level: intermediate
3778264ace61SBarry Smith 
3779264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3780264ace61SBarry Smith @*/
378119fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3782264ace61SBarry Smith {
3783264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3784264ace61SBarry Smith   PetscBool      match;
3785264ace61SBarry Smith   PetscErrorCode ierr;
3786264ace61SBarry Smith 
3787264ace61SBarry Smith   PetscFunctionBegin;
3788264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3789251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3790264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3791264ace61SBarry Smith 
37920f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
37931c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3794ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3795264ace61SBarry Smith 
3796264ace61SBarry Smith   if (dm->ops->destroy) {
3797264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3798264ace61SBarry Smith   }
3799d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3800264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3801d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3802264ace61SBarry Smith   PetscFunctionReturn(0);
3803264ace61SBarry Smith }
3804264ace61SBarry Smith 
3805264ace61SBarry Smith /*@C
3806264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3807264ace61SBarry Smith 
3808264ace61SBarry Smith   Not Collective
3809264ace61SBarry Smith 
3810264ace61SBarry Smith   Input Parameter:
3811264ace61SBarry Smith . dm  - The DM
3812264ace61SBarry Smith 
3813264ace61SBarry Smith   Output Parameter:
3814264ace61SBarry Smith . type - The DM type name
3815264ace61SBarry Smith 
3816264ace61SBarry Smith   Level: intermediate
3817264ace61SBarry Smith 
3818264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3819264ace61SBarry Smith @*/
382019fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3821264ace61SBarry Smith {
3822264ace61SBarry Smith   PetscErrorCode ierr;
3823264ace61SBarry Smith 
3824264ace61SBarry Smith   PetscFunctionBegin;
3825264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3826c959eef4SJed Brown   PetscValidPointer(type,2);
3827607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3828264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3829264ace61SBarry Smith   PetscFunctionReturn(0);
3830264ace61SBarry Smith }
3831264ace61SBarry Smith 
383267a56275SMatthew G Knepley /*@C
383367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
383467a56275SMatthew G Knepley 
3835d083f849SBarry Smith   Collective on dm
383667a56275SMatthew G Knepley 
383767a56275SMatthew G Knepley   Input Parameters:
383867a56275SMatthew G Knepley + dm - the DM
383967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
384067a56275SMatthew G Knepley 
384167a56275SMatthew G Knepley   Output Parameter:
384267a56275SMatthew G Knepley . M - pointer to new DM
384367a56275SMatthew G Knepley 
384467a56275SMatthew G Knepley   Notes:
384567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
384667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
384767a56275SMatthew G Knepley   of the input DM.
384867a56275SMatthew G Knepley 
384967a56275SMatthew G Knepley   Level: intermediate
385067a56275SMatthew G Knepley 
385167a56275SMatthew G Knepley .seealso: DMCreate()
385267a56275SMatthew G Knepley @*/
385319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
385467a56275SMatthew G Knepley {
385567a56275SMatthew G Knepley   DM             B;
385667a56275SMatthew G Knepley   char           convname[256];
3857c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
385867a56275SMatthew G Knepley   PetscErrorCode ierr;
385967a56275SMatthew G Knepley 
386067a56275SMatthew G Knepley   PetscFunctionBegin;
386167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
386267a56275SMatthew G Knepley   PetscValidType(dm,1);
386367a56275SMatthew G Knepley   PetscValidPointer(M,3);
3864251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3865c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3866c067b6caSMatthew G. Knepley   if (sametype) {
3867c067b6caSMatthew G. Knepley     *M   = dm;
3868c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3869c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3870c067b6caSMatthew G. Knepley   } else {
38710298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
387267a56275SMatthew G Knepley 
387367a56275SMatthew G Knepley     /*
387467a56275SMatthew G Knepley        Order of precedence:
387567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
387667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
387767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
387867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
387967a56275SMatthew G Knepley        5) Use a really basic converter.
388067a56275SMatthew G Knepley     */
388167a56275SMatthew G Knepley 
388267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3883a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3884a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3885a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3886a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3887a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
38880005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
388967a56275SMatthew G Knepley     if (conv) goto foundconv;
389067a56275SMatthew G Knepley 
389167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
389282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
389367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3894a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3895a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3896a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3897a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3898a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
38990005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
390067a56275SMatthew G Knepley     if (conv) {
3901fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
390267a56275SMatthew G Knepley       goto foundconv;
390367a56275SMatthew G Knepley     }
390467a56275SMatthew G Knepley 
390567a56275SMatthew G Knepley #if 0
390667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
390767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3908fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
390967a56275SMatthew G Knepley     if (conv) goto foundconv;
391067a56275SMatthew G Knepley 
391167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
391267a56275SMatthew G Knepley     if (dm->ops->convert) {
391367a56275SMatthew G Knepley       conv = dm->ops->convert;
391467a56275SMatthew G Knepley     }
391567a56275SMatthew G Knepley     if (conv) goto foundconv;
391667a56275SMatthew G Knepley #endif
391767a56275SMatthew G Knepley 
391867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
391982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
392067a56275SMatthew G Knepley 
392167a56275SMatthew G Knepley foundconv:
392267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
392367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
392412fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
392590b157c4SStefano Zampini     {
392690b157c4SStefano Zampini       PetscBool             isper;
392712fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
392812fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
392990b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
393090b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
3931c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
3932a587d139SMark       ierr = PetscFree((*M)->vectype);CHKERRQ(ierr);
3933a587d139SMark       ierr = PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype);CHKERRQ(ierr);
3934a587d139SMark       ierr = PetscFree((*M)->mattype);CHKERRQ(ierr);
3935a587d139SMark       ierr = PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype);CHKERRQ(ierr);
393612fa691eSMatthew G. Knepley     }
393767a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
393867a56275SMatthew G Knepley   }
393967a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
394067a56275SMatthew G Knepley   PetscFunctionReturn(0);
394167a56275SMatthew G Knepley }
3942264ace61SBarry Smith 
3943264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3944264ace61SBarry Smith 
3945264ace61SBarry Smith /*@C
39461c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
39471c84c290SBarry Smith 
39481c84c290SBarry Smith   Not Collective
39491c84c290SBarry Smith 
39501c84c290SBarry Smith   Input Parameters:
39511c84c290SBarry Smith + name        - The name of a new user-defined creation routine
39521c84c290SBarry Smith - create_func - The creation routine itself
39531c84c290SBarry Smith 
39541c84c290SBarry Smith   Notes:
39551c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
39561c84c290SBarry Smith 
39571c84c290SBarry Smith   Sample usage:
39581c84c290SBarry Smith .vb
3959bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
39601c84c290SBarry Smith .ve
39611c84c290SBarry Smith 
39621c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
39631c84c290SBarry Smith .vb
39641c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
39651c84c290SBarry Smith     DMSetType(DM,"my_da");
39661c84c290SBarry Smith .ve
39671c84c290SBarry Smith    or at runtime via the option
39681c84c290SBarry Smith .vb
39691c84c290SBarry Smith     -da_type my_da
39701c84c290SBarry Smith .ve
3971264ace61SBarry Smith 
3972264ace61SBarry Smith   Level: advanced
39731c84c290SBarry Smith 
3974bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
39751c84c290SBarry Smith 
3976264ace61SBarry Smith @*/
3977bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3978264ace61SBarry Smith {
3979264ace61SBarry Smith   PetscErrorCode ierr;
3980264ace61SBarry Smith 
3981264ace61SBarry Smith   PetscFunctionBegin;
39821d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3983a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3984264ace61SBarry Smith   PetscFunctionReturn(0);
3985264ace61SBarry Smith }
3986264ace61SBarry Smith 
3987b859378eSBarry Smith /*@C
398855849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3989b859378eSBarry Smith 
3990d083f849SBarry Smith   Collective on viewer
3991b859378eSBarry Smith 
3992b859378eSBarry Smith   Input Parameters:
3993b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3994b859378eSBarry Smith            some related function before a call to DMLoad().
3995b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3996b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3997b859378eSBarry Smith 
3998b859378eSBarry Smith    Level: intermediate
3999b859378eSBarry Smith 
4000b859378eSBarry Smith   Notes:
400155849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
4002b859378eSBarry Smith 
4003b859378eSBarry Smith   Notes for advanced users:
4004b859378eSBarry Smith   Most users should not need to know the details of the binary storage
4005b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
4006b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
4007b859378eSBarry Smith   format is
4008b859378eSBarry Smith .vb
4009b859378eSBarry Smith      has not yet been determined
4010b859378eSBarry Smith .ve
4011b859378eSBarry Smith 
4012b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
4013b859378eSBarry Smith @*/
4014b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
4015b859378eSBarry Smith {
40169331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
4017b859378eSBarry Smith   PetscErrorCode ierr;
4018b859378eSBarry Smith 
4019b859378eSBarry Smith   PetscFunctionBegin;
4020b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
4021b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4022fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
402332c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
40249331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
402558cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
40269331c7a4SMatthew G. Knepley   if (isbinary) {
40279331c7a4SMatthew G. Knepley     PetscInt classid;
40289331c7a4SMatthew G. Knepley     char     type[256];
4029b859378eSBarry Smith 
4030060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
40319200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
4032060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
403332c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
40349331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
40359331c7a4SMatthew G. Knepley   } else if (ishdf5) {
40369331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
40379331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
403858cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
4039b859378eSBarry Smith   PetscFunctionReturn(0);
4040b859378eSBarry Smith }
4041b859378eSBarry Smith 
4042b2e4378dSMatthew G. Knepley /*@
4043b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
4044b2e4378dSMatthew G. Knepley 
4045b2e4378dSMatthew G. Knepley   Not collective
4046b2e4378dSMatthew G. Knepley 
4047b2e4378dSMatthew G. Knepley   Input Parameter:
4048b2e4378dSMatthew G. Knepley . dm - the DM
4049b2e4378dSMatthew G. Knepley 
4050b2e4378dSMatthew G. Knepley   Output Parameters:
4051b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
4052b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
4053b2e4378dSMatthew G. Knepley 
4054b2e4378dSMatthew G. Knepley   Level: beginner
4055b2e4378dSMatthew G. Knepley 
4056b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
4057b2e4378dSMatthew G. Knepley 
4058b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
4059b2e4378dSMatthew G. Knepley @*/
4060b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
4061b2e4378dSMatthew G. Knepley {
4062b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
4063b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
4064b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
4065b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
4066b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
4067b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
4068b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
4069b2e4378dSMatthew G. Knepley 
4070b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4071b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4072b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4073b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
4074b2e4378dSMatthew G. Knepley   if (coords) {
4075b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
4076b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
4077b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
4078b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
4079b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
4080b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4081b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4082b2e4378dSMatthew G. Knepley       }
4083b2e4378dSMatthew G. Knepley     }
4084b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
4085b2e4378dSMatthew G. Knepley   } else {
4086b2e4378dSMatthew G. Knepley     PetscBool isda;
4087b2e4378dSMatthew G. Knepley 
4088b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
4089b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
4090b2e4378dSMatthew G. Knepley   }
4091b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
4092b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
4093b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4094b2e4378dSMatthew G. Knepley }
4095b2e4378dSMatthew G. Knepley 
4096b2e4378dSMatthew G. Knepley /*@
4097b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
4098b2e4378dSMatthew G. Knepley 
4099b2e4378dSMatthew G. Knepley   Collective
4100b2e4378dSMatthew G. Knepley 
4101b2e4378dSMatthew G. Knepley   Input Parameter:
4102b2e4378dSMatthew G. Knepley . dm - the DM
4103b2e4378dSMatthew G. Knepley 
4104b2e4378dSMatthew G. Knepley   Output Parameters:
4105b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
4106b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
4107b2e4378dSMatthew G. Knepley 
4108b2e4378dSMatthew G. Knepley   Level: beginner
4109b2e4378dSMatthew G. Knepley 
4110b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
4111b2e4378dSMatthew G. Knepley @*/
4112b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
4113b2e4378dSMatthew G. Knepley {
4114b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
41156ce308c4SMatthew G. Knepley   PetscInt       cdim;
41166ce308c4SMatthew G. Knepley   PetscMPIInt    count;
4117b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
4118b2e4378dSMatthew G. Knepley 
4119b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4120b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4121b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4122b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
4123b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
4124820f2d46SBarry Smith   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4125820f2d46SBarry Smith   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4126b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4127b2e4378dSMatthew G. Knepley }
4128b2e4378dSMatthew G. Knepley 
41297da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41307da65231SMatthew G Knepley 
4131a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4132a6dfd86eSKarl Rupp {
41331d47ebbbSSatish Balay   PetscInt       f;
41341b30c384SMatthew G Knepley   PetscErrorCode ierr;
41351b30c384SMatthew G Knepley 
41367da65231SMatthew G Knepley   PetscFunctionBegin;
413774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
41381d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
413957622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
41407da65231SMatthew G Knepley   }
41417da65231SMatthew G Knepley   PetscFunctionReturn(0);
41427da65231SMatthew G Knepley }
41437da65231SMatthew G Knepley 
4144a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4145a6dfd86eSKarl Rupp {
41461b30c384SMatthew G Knepley   PetscInt       f, g;
41477da65231SMatthew G Knepley   PetscErrorCode ierr;
41487da65231SMatthew G Knepley 
41497da65231SMatthew G Knepley   PetscFunctionBegin;
415074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
41511d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
415274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
41531d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
4154e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
41557da65231SMatthew G Knepley     }
415674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
41577da65231SMatthew G Knepley   }
41587da65231SMatthew G Knepley   PetscFunctionReturn(0);
41597da65231SMatthew G Knepley }
4160e7c4fc90SDmitry Karpeev 
41616113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4162e759306cSMatthew G. Knepley {
41630c5b8624SToby Isaac   PetscInt          localSize, bs;
41640c5b8624SToby Isaac   PetscMPIInt       size;
41650c5b8624SToby Isaac   Vec               x, xglob;
41660c5b8624SToby Isaac   const PetscScalar *xarray;
4167e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
4168e759306cSMatthew G. Knepley 
4169e759306cSMatthew G. Knepley   PetscFunctionBegin;
4170ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRMPI(ierr);
4171e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
4172e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
41736113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
41740c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
41750c5b8624SToby Isaac   if (size > 1) {
41760c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
41770c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
41780c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
41790c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
41800c5b8624SToby Isaac   } else {
41810c5b8624SToby Isaac     xglob = x;
41820c5b8624SToby Isaac   }
41830c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
41840c5b8624SToby Isaac   if (size > 1) {
41850c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
41860c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
41870c5b8624SToby Isaac   }
4188e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
4189e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4190e759306cSMatthew G. Knepley }
4191e759306cSMatthew G. Knepley 
419288ed4aceSMatthew G Knepley /*@
41931bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4194061576a5SJed Brown 
4195061576a5SJed Brown   Input Parameter:
4196061576a5SJed Brown . dm - The DM
4197061576a5SJed Brown 
4198061576a5SJed Brown   Output Parameter:
4199061576a5SJed Brown . section - The PetscSection
4200061576a5SJed Brown 
4201061576a5SJed Brown   Options Database Keys:
4202061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4203061576a5SJed Brown 
4204061576a5SJed Brown   Level: advanced
4205061576a5SJed Brown 
4206061576a5SJed Brown   Notes:
4207061576a5SJed Brown   Use DMGetLocalSection() in new code.
4208061576a5SJed Brown 
4209061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4210061576a5SJed Brown 
4211061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
4212061576a5SJed Brown @*/
4213061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4214061576a5SJed Brown {
4215061576a5SJed Brown   PetscErrorCode ierr;
4216061576a5SJed Brown 
4217061576a5SJed Brown   PetscFunctionBegin;
4218061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
4219061576a5SJed Brown   PetscFunctionReturn(0);
4220061576a5SJed Brown }
4221061576a5SJed Brown 
4222061576a5SJed Brown /*@
4223061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
422488ed4aceSMatthew G Knepley 
422588ed4aceSMatthew G Knepley   Input Parameter:
422688ed4aceSMatthew G Knepley . dm - The DM
422788ed4aceSMatthew G Knepley 
422888ed4aceSMatthew G Knepley   Output Parameter:
422988ed4aceSMatthew G Knepley . section - The PetscSection
423088ed4aceSMatthew G Knepley 
4231e5893cccSMatthew G. Knepley   Options Database Keys:
4232e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4233e5893cccSMatthew G. Knepley 
423488ed4aceSMatthew G Knepley   Level: intermediate
423588ed4aceSMatthew G Knepley 
423688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
423788ed4aceSMatthew G Knepley 
4238061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
423988ed4aceSMatthew G Knepley @*/
4240061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
42410adebc6cSBarry Smith {
4242fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4243fd59a867SMatthew G. Knepley 
424488ed4aceSMatthew G Knepley   PetscFunctionBegin;
424588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
424688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42471bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4248e5e52638SMatthew G. Knepley     PetscInt d;
4249e5e52638SMatthew G. Knepley 
425045480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
425145480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject) dm;
425245480ffeSMatthew G. Knepley       PetscViewer       viewer;
425345480ffeSMatthew G. Knepley       PetscViewerFormat format;
425445480ffeSMatthew G. Knepley       PetscBool         flg;
425545480ffeSMatthew G. Knepley 
425645480ffeSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg);CHKERRQ(ierr);
425745480ffeSMatthew G. Knepley       if (flg) {ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);}
425845480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
425945480ffeSMatthew G. Knepley         ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);
426045480ffeSMatthew G. Knepley         if (flg) {ierr = PetscDSView(dm->probs[d].ds, viewer);CHKERRQ(ierr);}
426145480ffeSMatthew G. Knepley       }
426245480ffeSMatthew G. Knepley       if (flg) {
426345480ffeSMatthew G. Knepley         ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
426445480ffeSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
426545480ffeSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
426645480ffeSMatthew G. Knepley       }
426745480ffeSMatthew G. Knepley     }
42681bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
42691bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
42702f0f8703SMatthew G. Knepley   }
42711bb6d2a8SBarry Smith   *section = dm->localSection;
427288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
427388ed4aceSMatthew G Knepley }
427488ed4aceSMatthew G Knepley 
427588ed4aceSMatthew G Knepley /*@
42761bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4277061576a5SJed Brown 
4278061576a5SJed Brown   Input Parameters:
4279061576a5SJed Brown + dm - The DM
4280061576a5SJed Brown - section - The PetscSection
4281061576a5SJed Brown 
4282061576a5SJed Brown   Level: advanced
4283061576a5SJed Brown 
4284061576a5SJed Brown   Notes:
4285061576a5SJed Brown   Use DMSetLocalSection() in new code.
4286061576a5SJed Brown 
4287061576a5SJed Brown   Any existing Section will be destroyed
4288061576a5SJed Brown 
4289061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4290061576a5SJed Brown @*/
4291061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4292061576a5SJed Brown {
4293061576a5SJed Brown   PetscErrorCode ierr;
4294061576a5SJed Brown 
4295061576a5SJed Brown   PetscFunctionBegin;
4296061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4297061576a5SJed Brown   PetscFunctionReturn(0);
4298061576a5SJed Brown }
4299061576a5SJed Brown 
4300061576a5SJed Brown /*@
4301061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
430288ed4aceSMatthew G Knepley 
430388ed4aceSMatthew G Knepley   Input Parameters:
430488ed4aceSMatthew G Knepley + dm - The DM
430588ed4aceSMatthew G Knepley - section - The PetscSection
430688ed4aceSMatthew G Knepley 
430788ed4aceSMatthew G Knepley   Level: intermediate
430888ed4aceSMatthew G Knepley 
430988ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
431088ed4aceSMatthew G Knepley 
4311061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
431288ed4aceSMatthew G Knepley @*/
4313061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
43140adebc6cSBarry Smith {
4315c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4316af122d2aSMatthew G Knepley   PetscInt       f;
431788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
431888ed4aceSMatthew G Knepley 
431988ed4aceSMatthew G Knepley   PetscFunctionBegin;
432088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4321b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
43221d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
43231bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
43241bb6d2a8SBarry Smith   dm->localSection = section;
43251bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4326af122d2aSMatthew G Knepley   if (numFields) {
4327af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4328af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43290f21e855SMatthew G. Knepley       PetscObject disc;
4330af122d2aSMatthew G Knepley       const char *name;
4331af122d2aSMatthew G Knepley 
43321bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
433344a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
43340f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4335af122d2aSMatthew G Knepley     }
4336af122d2aSMatthew G Knepley   }
4337e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43381bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
433988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
434088ed4aceSMatthew G Knepley }
434188ed4aceSMatthew G Knepley 
43429435951eSToby Isaac /*@
4343b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
43449435951eSToby Isaac 
4345e228b242SToby Isaac   not collective
4346e228b242SToby Isaac 
43479435951eSToby Isaac   Input Parameter:
43489435951eSToby Isaac . dm - The DM
43499435951eSToby Isaac 
4350d8d19677SJose E. Roman   Output Parameters:
43519435951eSToby 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.
43529435951eSToby 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.
43539435951eSToby Isaac 
43549435951eSToby Isaac   Level: advanced
43559435951eSToby Isaac 
43569435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
43579435951eSToby Isaac 
43589435951eSToby Isaac .seealso: DMSetDefaultConstraints()
43599435951eSToby Isaac @*/
43609435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
43619435951eSToby Isaac {
43629435951eSToby Isaac   PetscErrorCode ierr;
43639435951eSToby Isaac 
43649435951eSToby Isaac   PetscFunctionBegin;
43659435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43669435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
436745a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
436845a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
43699435951eSToby Isaac   PetscFunctionReturn(0);
43709435951eSToby Isaac }
43719435951eSToby Isaac 
43729435951eSToby Isaac /*@
4373b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
43749435951eSToby Isaac 
43759435951eSToby 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().
43769435951eSToby Isaac 
43779435951eSToby 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.
43789435951eSToby Isaac 
4379e228b242SToby Isaac   collective on dm
4380e228b242SToby Isaac 
43819435951eSToby Isaac   Input Parameters:
43829435951eSToby Isaac + dm - The DM
4383e228b242SToby 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).
4384e228b242SToby 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).
43859435951eSToby Isaac 
43869435951eSToby Isaac   Level: advanced
43879435951eSToby Isaac 
43889435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
43899435951eSToby Isaac 
43909435951eSToby Isaac .seealso: DMGetDefaultConstraints()
43919435951eSToby Isaac @*/
43929435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
43939435951eSToby Isaac {
4394e228b242SToby Isaac   PetscMPIInt result;
43959435951eSToby Isaac   PetscErrorCode ierr;
43969435951eSToby Isaac 
43979435951eSToby Isaac   PetscFunctionBegin;
43989435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4399e228b242SToby Isaac   if (section) {
4400e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4401ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRMPI(ierr);
4402f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4403e228b242SToby Isaac   }
4404e228b242SToby Isaac   if (mat) {
4405e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4406ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRMPI(ierr);
4407f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4408e228b242SToby Isaac   }
44099435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
44109435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
44119435951eSToby Isaac   dm->defaultConstraintSection = section;
44129435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
44139435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
44149435951eSToby Isaac   dm->defaultConstraintMat = mat;
44159435951eSToby Isaac   PetscFunctionReturn(0);
44169435951eSToby Isaac }
44179435951eSToby Isaac 
4418497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4419507e4973SMatthew G. Knepley /*
4420507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4421507e4973SMatthew G. Knepley 
4422507e4973SMatthew G. Knepley   Input Parameters:
4423507e4973SMatthew G. Knepley + dm - The DM
4424507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4425507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4426507e4973SMatthew G. Knepley 
4427507e4973SMatthew G. Knepley   Level: intermediate
4428507e4973SMatthew G. Knepley 
44291bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4430507e4973SMatthew G. Knepley */
4431f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4432507e4973SMatthew G. Knepley {
4433507e4973SMatthew G. Knepley   MPI_Comm        comm;
4434507e4973SMatthew G. Knepley   PetscLayout     layout;
4435507e4973SMatthew G. Knepley   const PetscInt *ranges;
4436507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4437507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4438507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4439507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4440507e4973SMatthew G. Knepley 
4441507e4973SMatthew G. Knepley   PetscFunctionBegin;
4442507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4443507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4444ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
4445ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
4446507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4447507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4448507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4449507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4450507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4451507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4452507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4453507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4454f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4455507e4973SMatthew G. Knepley 
4456507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4457507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4458507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4459507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4460507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4461507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4462507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4463507e4973SMatthew 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;}
4464507e4973SMatthew 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;}
4465507e4973SMatthew G. Knepley     if (gdof < 0) {
4466507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4467507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4468507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4469507e4973SMatthew G. Knepley 
4470507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4471507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4472507e4973SMatthew 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;}
4473507e4973SMatthew G. Knepley       }
4474507e4973SMatthew G. Knepley     }
4475507e4973SMatthew G. Knepley   }
4476507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4477507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4478820f2d46SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRMPI(ierr);
4479507e4973SMatthew G. Knepley   if (!gvalid) {
4480507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4481507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4482507e4973SMatthew G. Knepley   }
4483507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4484507e4973SMatthew G. Knepley }
4485f741bcd2SMatthew G. Knepley #endif
4486507e4973SMatthew G. Knepley 
448788ed4aceSMatthew G Knepley /*@
4488e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
448988ed4aceSMatthew G Knepley 
4490d083f849SBarry Smith   Collective on dm
44918b1ab98fSJed Brown 
449288ed4aceSMatthew G Knepley   Input Parameter:
449388ed4aceSMatthew G Knepley . dm - The DM
449488ed4aceSMatthew G Knepley 
449588ed4aceSMatthew G Knepley   Output Parameter:
449688ed4aceSMatthew G Knepley . section - The PetscSection
449788ed4aceSMatthew G Knepley 
449888ed4aceSMatthew G Knepley   Level: intermediate
449988ed4aceSMatthew G Knepley 
450088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
450188ed4aceSMatthew G Knepley 
450292fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
450388ed4aceSMatthew G Knepley @*/
4504e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
45050adebc6cSBarry Smith {
450688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
450788ed4aceSMatthew G Knepley 
450888ed4aceSMatthew G Knepley   PetscFunctionBegin;
450988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
451088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
45111bb6d2a8SBarry Smith   if (!dm->globalSection) {
4512fd59a867SMatthew G. Knepley     PetscSection s;
4513fd59a867SMatthew G. Knepley 
451492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4515fd59a867SMatthew 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");
451633907cc2SStefano 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");
45171bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4518cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
45191bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
45201bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
452188ed4aceSMatthew G Knepley   }
45221bb6d2a8SBarry Smith   *section = dm->globalSection;
452388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
452488ed4aceSMatthew G Knepley }
452588ed4aceSMatthew G Knepley 
4526b21d0597SMatthew G Knepley /*@
4527e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4528b21d0597SMatthew G Knepley 
4529b21d0597SMatthew G Knepley   Input Parameters:
4530b21d0597SMatthew G Knepley + dm - The DM
45315080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4532b21d0597SMatthew G Knepley 
4533b21d0597SMatthew G Knepley   Level: intermediate
4534b21d0597SMatthew G Knepley 
4535b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4536b21d0597SMatthew G Knepley 
453792fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4538b21d0597SMatthew G Knepley @*/
4539e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
45400adebc6cSBarry Smith {
4541b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4542b21d0597SMatthew G Knepley 
4543b21d0597SMatthew G Knepley   PetscFunctionBegin;
4544b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45455080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
45461d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
45471bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
45481bb6d2a8SBarry Smith   dm->globalSection = section;
4549497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45501bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4551507e4973SMatthew G. Knepley #endif
4552b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4553b21d0597SMatthew G Knepley }
4554b21d0597SMatthew G Knepley 
455588ed4aceSMatthew G Knepley /*@
45561bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
455788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
455888ed4aceSMatthew G Knepley 
455988ed4aceSMatthew G Knepley   Input Parameter:
456088ed4aceSMatthew G Knepley . dm - The DM
456188ed4aceSMatthew G Knepley 
456288ed4aceSMatthew G Knepley   Output Parameter:
456388ed4aceSMatthew G Knepley . sf - The PetscSF
456488ed4aceSMatthew G Knepley 
456588ed4aceSMatthew G Knepley   Level: intermediate
456688ed4aceSMatthew G Knepley 
456788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
456888ed4aceSMatthew G Knepley 
45691bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
457088ed4aceSMatthew G Knepley @*/
45711bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
45720adebc6cSBarry Smith {
457388ed4aceSMatthew G Knepley   PetscInt       nroots;
457488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
457588ed4aceSMatthew G Knepley 
457688ed4aceSMatthew G Knepley   PetscFunctionBegin;
457788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
457888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
45791bb6d2a8SBarry Smith   if (!dm->sectionSF) {
45801bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
458133907cc2SStefano Zampini   }
45821bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
458388ed4aceSMatthew G Knepley   if (nroots < 0) {
458488ed4aceSMatthew G Knepley     PetscSection section, gSection;
458588ed4aceSMatthew G Knepley 
458692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
458731ea6d37SMatthew G Knepley     if (section) {
4588e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
45891bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
459031ea6d37SMatthew G Knepley     } else {
45910298fd71SBarry Smith       *sf = NULL;
459231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
459331ea6d37SMatthew G Knepley     }
459488ed4aceSMatthew G Knepley   }
45951bb6d2a8SBarry Smith   *sf = dm->sectionSF;
459688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
459788ed4aceSMatthew G Knepley }
459888ed4aceSMatthew G Knepley 
459988ed4aceSMatthew G Knepley /*@
46001bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
460188ed4aceSMatthew G Knepley 
460288ed4aceSMatthew G Knepley   Input Parameters:
460388ed4aceSMatthew G Knepley + dm - The DM
460488ed4aceSMatthew G Knepley - sf - The PetscSF
460588ed4aceSMatthew G Knepley 
460688ed4aceSMatthew G Knepley   Level: intermediate
460788ed4aceSMatthew G Knepley 
460888ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
460988ed4aceSMatthew G Knepley 
46101bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
461188ed4aceSMatthew G Knepley @*/
46121bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
46130adebc6cSBarry Smith {
461488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
461588ed4aceSMatthew G Knepley 
461688ed4aceSMatthew G Knepley   PetscFunctionBegin;
461788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4618b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
461933907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
46201bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
46211bb6d2a8SBarry Smith   dm->sectionSF = sf;
462288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
462388ed4aceSMatthew G Knepley }
462488ed4aceSMatthew G Knepley 
462588ed4aceSMatthew G Knepley /*@C
46261bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
462788ed4aceSMatthew G Knepley   describing the data layout.
462888ed4aceSMatthew G Knepley 
462988ed4aceSMatthew G Knepley   Input Parameters:
463088ed4aceSMatthew G Knepley + dm - The DM
463188ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
463288ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
463388ed4aceSMatthew G Knepley 
46341bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
463588ed4aceSMatthew G Knepley 
46361bb6d2a8SBarry Smith   Level: developer
46371bb6d2a8SBarry Smith 
46381bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
46391bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
46401bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
46411bb6d2a8SBarry Smith 
46421bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
464388ed4aceSMatthew G Knepley @*/
46441bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
464588ed4aceSMatthew G Knepley {
464688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
464788ed4aceSMatthew G Knepley 
464888ed4aceSMatthew G Knepley   PetscFunctionBegin;
464988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4650b0c7db22SLisandro Dalcin   ierr = PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection);CHKERRQ(ierr);
465188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
465288ed4aceSMatthew G Knepley }
4653af122d2aSMatthew G Knepley 
4654b21d0597SMatthew G Knepley /*@
4655b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4656b21d0597SMatthew G Knepley 
4657b21d0597SMatthew G Knepley   Input Parameter:
4658b21d0597SMatthew G Knepley . dm - The DM
4659b21d0597SMatthew G Knepley 
4660b21d0597SMatthew G Knepley   Output Parameter:
4661b21d0597SMatthew G Knepley . sf - The PetscSF
4662b21d0597SMatthew G Knepley 
4663b21d0597SMatthew G Knepley   Level: intermediate
4664b21d0597SMatthew G Knepley 
4665b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4666b21d0597SMatthew G Knepley 
46671bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4668b21d0597SMatthew G Knepley @*/
46690adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
46700adebc6cSBarry Smith {
4671b21d0597SMatthew G Knepley   PetscFunctionBegin;
4672b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4673b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4674b21d0597SMatthew G Knepley   *sf = dm->sf;
4675b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4676b21d0597SMatthew G Knepley }
4677b21d0597SMatthew G Knepley 
4678057b4bcdSMatthew G Knepley /*@
4679057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4680057b4bcdSMatthew G Knepley 
4681057b4bcdSMatthew G Knepley   Input Parameters:
4682057b4bcdSMatthew G Knepley + dm - The DM
4683057b4bcdSMatthew G Knepley - sf - The PetscSF
4684057b4bcdSMatthew G Knepley 
4685057b4bcdSMatthew G Knepley   Level: intermediate
4686057b4bcdSMatthew G Knepley 
46871bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4688057b4bcdSMatthew G Knepley @*/
46890adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
46900adebc6cSBarry Smith {
4691057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4692057b4bcdSMatthew G Knepley 
4693057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4694057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4695b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4696057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
469733907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4698057b4bcdSMatthew G Knepley   dm->sf = sf;
4699057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4700057b4bcdSMatthew G Knepley }
4701057b4bcdSMatthew G Knepley 
4702*4f37162bSMatthew G. Knepley /*@
4703*4f37162bSMatthew G. Knepley   DMGetNaturalSF - Get the PetscSF encoding the map back to the original mesh ordering
4704*4f37162bSMatthew G. Knepley 
4705*4f37162bSMatthew G. Knepley   Input Parameter:
4706*4f37162bSMatthew G. Knepley . dm - The DM
4707*4f37162bSMatthew G. Knepley 
4708*4f37162bSMatthew G. Knepley   Output Parameter:
4709*4f37162bSMatthew G. Knepley . sf - The PetscSF
4710*4f37162bSMatthew G. Knepley 
4711*4f37162bSMatthew G. Knepley   Level: intermediate
4712*4f37162bSMatthew G. Knepley 
4713*4f37162bSMatthew G. Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4714*4f37162bSMatthew G. Knepley 
4715*4f37162bSMatthew G. Knepley .seealso: DMSetNaturalSF(), DMSetUseNatural(), DMGetUseNatural(), DMPlexCreateGlobalToNaturalSF(), DMPlexDistribute()
4716*4f37162bSMatthew G. Knepley @*/
4717*4f37162bSMatthew G. Knepley PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4718*4f37162bSMatthew G. Knepley {
4719*4f37162bSMatthew G. Knepley   PetscFunctionBegin;
4720*4f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4721*4f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
4722*4f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
4723*4f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
4724*4f37162bSMatthew G. Knepley }
4725*4f37162bSMatthew G. Knepley 
4726*4f37162bSMatthew G. Knepley /*@
4727*4f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
4728*4f37162bSMatthew G. Knepley 
4729*4f37162bSMatthew G. Knepley   Input Parameters:
4730*4f37162bSMatthew G. Knepley + dm - The DM
4731*4f37162bSMatthew G. Knepley - sf - The PetscSF
4732*4f37162bSMatthew G. Knepley 
4733*4f37162bSMatthew G. Knepley   Level: intermediate
4734*4f37162bSMatthew G. Knepley 
4735*4f37162bSMatthew G. Knepley .seealso: DMGetNaturalSF(), DMSetUseNatural(), DMGetUseNatural(), DMPlexCreateGlobalToNaturalSF(), DMPlexDistribute()
4736*4f37162bSMatthew G. Knepley @*/
4737*4f37162bSMatthew G. Knepley PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4738*4f37162bSMatthew G. Knepley {
4739*4f37162bSMatthew G. Knepley   PetscErrorCode ierr;
4740*4f37162bSMatthew G. Knepley 
4741*4f37162bSMatthew G. Knepley   PetscFunctionBegin;
4742*4f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4743*4f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4744*4f37162bSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
4745*4f37162bSMatthew G. Knepley   ierr = PetscSFDestroy(&dm->sfNatural);CHKERRQ(ierr);
4746*4f37162bSMatthew G. Knepley   dm->sfNatural = sf;
4747*4f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
4748*4f37162bSMatthew G. Knepley }
4749*4f37162bSMatthew G. Knepley 
475034aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
475134aa8a36SMatthew G. Knepley {
475234aa8a36SMatthew G. Knepley   PetscClassId   id;
475334aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
475434aa8a36SMatthew G. Knepley 
475534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
475634aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
475734aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
475834aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
475934aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
476034aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
476117c1d62eSMatthew G. Knepley   } else {
476217c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
476334aa8a36SMatthew G. Knepley   }
476434aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
476534aa8a36SMatthew G. Knepley }
476634aa8a36SMatthew G. Knepley 
476744a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
476844a7f3ddSMatthew G. Knepley {
476944a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
477044a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
477144a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
477244a7f3ddSMatthew G. Knepley 
477344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
477444a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
477544a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
477644a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4777e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
477844a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
477944a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
478044a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
478144a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
478244a7f3ddSMatthew G. Knepley }
478344a7f3ddSMatthew G. Knepley 
478444a7f3ddSMatthew G. Knepley /*@
478544a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
478644a7f3ddSMatthew G. Knepley 
4787d083f849SBarry Smith   Logically collective on dm
478844a7f3ddSMatthew G. Knepley 
478944a7f3ddSMatthew G. Knepley   Input Parameter:
479044a7f3ddSMatthew G. Knepley . dm - The DM
479144a7f3ddSMatthew G. Knepley 
479244a7f3ddSMatthew G. Knepley   Level: intermediate
479344a7f3ddSMatthew G. Knepley 
479444a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
479544a7f3ddSMatthew G. Knepley @*/
479644a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
479744a7f3ddSMatthew G. Knepley {
479844a7f3ddSMatthew G. Knepley   PetscInt       f;
479944a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
480044a7f3ddSMatthew G. Knepley 
480144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
480244a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
480344a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
480444a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
480544a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
480644a7f3ddSMatthew G. Knepley   }
480744a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
480844a7f3ddSMatthew G. Knepley   dm->fields = NULL;
480944a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
481044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
481144a7f3ddSMatthew G. Knepley }
481244a7f3ddSMatthew G. Knepley 
4813689b5837SMatthew G. Knepley /*@
4814689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4815689b5837SMatthew G. Knepley 
4816689b5837SMatthew G. Knepley   Not collective
4817689b5837SMatthew G. Knepley 
4818689b5837SMatthew G. Knepley   Input Parameter:
4819689b5837SMatthew G. Knepley . dm - The DM
4820689b5837SMatthew G. Knepley 
4821689b5837SMatthew G. Knepley   Output Parameter:
4822689b5837SMatthew G. Knepley . Nf - The number of fields
4823689b5837SMatthew G. Knepley 
4824689b5837SMatthew G. Knepley   Level: intermediate
4825689b5837SMatthew G. Knepley 
4826689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4827689b5837SMatthew G. Knepley @*/
48280f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
48290f21e855SMatthew G. Knepley {
48300f21e855SMatthew G. Knepley   PetscFunctionBegin;
48310f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4832534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
483344a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4834af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4835af122d2aSMatthew G Knepley }
4836af122d2aSMatthew G Knepley 
4837689b5837SMatthew G. Knepley /*@
4838689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4839689b5837SMatthew G. Knepley 
4840d083f849SBarry Smith   Logically collective on dm
4841689b5837SMatthew G. Knepley 
4842689b5837SMatthew G. Knepley   Input Parameters:
4843689b5837SMatthew G. Knepley + dm - The DM
4844689b5837SMatthew G. Knepley - Nf - The number of fields
4845689b5837SMatthew G. Knepley 
4846689b5837SMatthew G. Knepley   Level: intermediate
4847689b5837SMatthew G. Knepley 
4848689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4849689b5837SMatthew G. Knepley @*/
4850af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4851af122d2aSMatthew G Knepley {
48520f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4853af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4854af122d2aSMatthew G Knepley 
4855af122d2aSMatthew G Knepley   PetscFunctionBegin;
4856af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
485744a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
48580f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
48590f21e855SMatthew G. Knepley     PetscContainer obj;
48600f21e855SMatthew G. Knepley 
48610f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
486244a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
48630f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4864af122d2aSMatthew G Knepley   }
4865af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4866af122d2aSMatthew G Knepley }
4867af122d2aSMatthew G Knepley 
4868c1929be8SMatthew G. Knepley /*@
4869c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4870c1929be8SMatthew G. Knepley 
4871c1929be8SMatthew G. Knepley   Not collective
4872c1929be8SMatthew G. Knepley 
4873c1929be8SMatthew G. Knepley   Input Parameters:
4874c1929be8SMatthew G. Knepley + dm - The DM
4875c1929be8SMatthew G. Knepley - f  - The field number
4876c1929be8SMatthew G. Knepley 
487744a7f3ddSMatthew G. Knepley   Output Parameters:
487844a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
487944a7f3ddSMatthew G. Knepley - field - The discretization object
4880c1929be8SMatthew G. Knepley 
488144a7f3ddSMatthew G. Knepley   Level: intermediate
4882c1929be8SMatthew G. Knepley 
488344a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4884c1929be8SMatthew G. Knepley @*/
488544a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4886af122d2aSMatthew G Knepley {
4887af122d2aSMatthew G Knepley   PetscFunctionBegin;
4888af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4889064a246eSJacob Faibussowitsch   PetscValidPointer(field, 4);
489044a7f3ddSMatthew 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);
489144a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
489244a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4893decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4894decb47aaSMatthew G. Knepley }
4895decb47aaSMatthew G. Knepley 
4896083401c6SMatthew G. Knepley /* Does not clear the DS */
4897083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4898083401c6SMatthew G. Knepley {
4899083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4900083401c6SMatthew G. Knepley 
4901083401c6SMatthew G. Knepley   PetscFunctionBegin;
4902083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4903083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4904083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4905083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4906083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4907083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4908083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4909083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4910083401c6SMatthew G. Knepley }
4911083401c6SMatthew G. Knepley 
4912c1929be8SMatthew G. Knepley /*@
4913c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4914c1929be8SMatthew G. Knepley 
4915d083f849SBarry Smith   Logically collective on dm
4916c1929be8SMatthew G. Knepley 
4917c1929be8SMatthew G. Knepley   Input Parameters:
4918c1929be8SMatthew G. Knepley + dm    - The DM
4919c1929be8SMatthew G. Knepley . f     - The field number
492044a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4921c1929be8SMatthew G. Knepley - field - The discretization object
4922c1929be8SMatthew G. Knepley 
492344a7f3ddSMatthew G. Knepley   Level: intermediate
4924c1929be8SMatthew G. Knepley 
492544a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4926c1929be8SMatthew G. Knepley @*/
492744a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4928decb47aaSMatthew G. Knepley {
4929decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4930decb47aaSMatthew G. Knepley 
4931decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4932decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4933e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
493444a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4935e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4936083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
493734aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
49382df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
493944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
494044a7f3ddSMatthew G. Knepley }
494144a7f3ddSMatthew G. Knepley 
494244a7f3ddSMatthew G. Knepley /*@
494344a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
494444a7f3ddSMatthew G. Knepley 
4945d083f849SBarry Smith   Logically collective on dm
494644a7f3ddSMatthew G. Knepley 
494744a7f3ddSMatthew G. Knepley   Input Parameters:
494844a7f3ddSMatthew G. Knepley + dm    - The DM
494944a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
495044a7f3ddSMatthew G. Knepley - field - The discretization object
495144a7f3ddSMatthew G. Knepley 
495244a7f3ddSMatthew G. Knepley   Level: intermediate
495344a7f3ddSMatthew G. Knepley 
495444a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
495544a7f3ddSMatthew G. Knepley @*/
495644a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
495744a7f3ddSMatthew G. Knepley {
495844a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
495944a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
496044a7f3ddSMatthew G. Knepley 
496144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
496244a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4963064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
496444a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
496544a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
496644a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
496744a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
496844a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
496944a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
497034aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
49712df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4972af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4973af122d2aSMatthew G Knepley }
49746636e97aSMatthew G Knepley 
4975e5e52638SMatthew G. Knepley /*@
4976e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4977e0b68406SMatthew Knepley 
4978e0b68406SMatthew Knepley   Logically collective on dm
4979e0b68406SMatthew Knepley 
4980e0b68406SMatthew Knepley   Input Parameters:
4981e0b68406SMatthew Knepley + dm          - The DM
4982e0b68406SMatthew Knepley . f           - The field index
4983e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
4984e0b68406SMatthew Knepley 
4985e0b68406SMatthew Knepley   Level: intermediate
4986e0b68406SMatthew Knepley 
4987e0b68406SMatthew Knepley .seealso: DMGetFieldAvoidTensor(), DMSetField(), DMGetField()
4988e0b68406SMatthew Knepley @*/
4989e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
4990e0b68406SMatthew Knepley {
4991e0b68406SMatthew Knepley   PetscFunctionBegin;
4992e0b68406SMatthew 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);
4993e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
4994e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4995e0b68406SMatthew Knepley }
4996e0b68406SMatthew Knepley 
4997e0b68406SMatthew Knepley /*@
4998e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
4999e0b68406SMatthew Knepley 
5000e0b68406SMatthew Knepley   Logically collective on dm
5001e0b68406SMatthew Knepley 
5002e0b68406SMatthew Knepley   Input Parameters:
5003e0b68406SMatthew Knepley + dm          - The DM
5004e0b68406SMatthew Knepley - f           - The field index
5005e0b68406SMatthew Knepley 
5006e0b68406SMatthew Knepley   Output Parameter:
5007e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5008e0b68406SMatthew Knepley 
5009e0b68406SMatthew Knepley   Level: intermediate
5010e0b68406SMatthew Knepley 
5011e0b68406SMatthew Knepley .seealso: DMSetFieldAvoidTensor(), DMSetField(), DMGetField()
5012e0b68406SMatthew Knepley @*/
5013e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5014e0b68406SMatthew Knepley {
5015e0b68406SMatthew Knepley   PetscFunctionBegin;
5016e0b68406SMatthew 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);
5017e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
5018e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5019e0b68406SMatthew Knepley }
5020e0b68406SMatthew Knepley 
5021e0b68406SMatthew Knepley /*@
5022e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
5023e5e52638SMatthew G. Knepley 
5024d083f849SBarry Smith   Collective on dm
5025e5e52638SMatthew G. Knepley 
5026e5e52638SMatthew G. Knepley   Input Parameter:
5027e5e52638SMatthew G. Knepley . dm - The DM
5028e5e52638SMatthew G. Knepley 
5029e5e52638SMatthew G. Knepley   Output Parameter:
5030e5e52638SMatthew G. Knepley . newdm - The DM
5031e5e52638SMatthew G. Knepley 
5032e5e52638SMatthew G. Knepley   Level: advanced
5033e5e52638SMatthew G. Knepley 
5034e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
5035e5e52638SMatthew G. Knepley @*/
5036e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
5037e5e52638SMatthew G. Knepley {
5038e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
5039e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5040e5e52638SMatthew G. Knepley 
5041e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5042e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5043e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5044e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
5045e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5046e5e52638SMatthew G. Knepley     DMLabel     label;
5047e5e52638SMatthew G. Knepley     PetscObject field;
504834aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5049e5e52638SMatthew G. Knepley 
5050e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
5051e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
505234aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
505334aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
505434aa8a36SMatthew G. Knepley   }
505534aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
505634aa8a36SMatthew G. Knepley }
505734aa8a36SMatthew G. Knepley 
505834aa8a36SMatthew G. Knepley /*@
505934aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
506034aa8a36SMatthew G. Knepley 
506134aa8a36SMatthew G. Knepley   Not collective
506234aa8a36SMatthew G. Knepley 
506334aa8a36SMatthew G. Knepley   Input Parameters:
506434aa8a36SMatthew G. Knepley + dm - The DM object
506534aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
506634aa8a36SMatthew G. Knepley 
5067d8d19677SJose E. Roman   Output Parameters:
506834aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
506934aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
507034aa8a36SMatthew G. Knepley 
507134aa8a36SMatthew G. Knepley   Notes:
507234aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
507334aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
507434aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5075979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
507634aa8a36SMatthew G. Knepley 
507734aa8a36SMatthew G. Knepley   Level: developer
507834aa8a36SMatthew G. Knepley 
507934aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
508034aa8a36SMatthew G. Knepley @*/
508134aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
508234aa8a36SMatthew G. Knepley {
508334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
508434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5085534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
5086534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
508734aa8a36SMatthew G. Knepley   if (f < 0) {
508834aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
508934aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
509034aa8a36SMatthew G. Knepley   } else {
509134aa8a36SMatthew G. Knepley     PetscInt       Nf;
509234aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
509334aa8a36SMatthew G. Knepley 
509434aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
509534aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
509634aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
509734aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
509834aa8a36SMatthew G. Knepley   }
509934aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
510034aa8a36SMatthew G. Knepley }
510134aa8a36SMatthew G. Knepley 
510234aa8a36SMatthew G. Knepley /*@
510334aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
510434aa8a36SMatthew G. Knepley 
510534aa8a36SMatthew G. Knepley   Not collective
510634aa8a36SMatthew G. Knepley 
510734aa8a36SMatthew G. Knepley   Input Parameters:
510834aa8a36SMatthew G. Knepley + dm         - The DM object
510934aa8a36SMatthew G. Knepley . f          - The field number
511034aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
511134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
511234aa8a36SMatthew G. Knepley 
511334aa8a36SMatthew G. Knepley   Notes:
511434aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
511534aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
511634aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5117979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
511834aa8a36SMatthew G. Knepley 
511934aa8a36SMatthew G. Knepley   Level: developer
512034aa8a36SMatthew G. Knepley 
512134aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
512234aa8a36SMatthew G. Knepley @*/
512334aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
512434aa8a36SMatthew G. Knepley {
512534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
512634aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
512734aa8a36SMatthew G. Knepley   if (f < 0) {
512834aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
512934aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
513034aa8a36SMatthew G. Knepley   } else {
513134aa8a36SMatthew G. Knepley     PetscInt       Nf;
513234aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
513334aa8a36SMatthew G. Knepley 
513434aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
513534aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
513634aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
513734aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5138e5e52638SMatthew G. Knepley   }
5139e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5140e5e52638SMatthew G. Knepley }
5141e5e52638SMatthew G. Knepley 
5142b0441da4SMatthew G. Knepley /*@
5143b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5144b0441da4SMatthew G. Knepley 
5145b0441da4SMatthew G. Knepley   Not collective
5146b0441da4SMatthew G. Knepley 
5147f899ff85SJose E. Roman   Input Parameter:
5148b0441da4SMatthew G. Knepley . dm - The DM object
5149b0441da4SMatthew G. Knepley 
5150d8d19677SJose E. Roman   Output Parameters:
5151b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5152b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5153b0441da4SMatthew G. Knepley 
5154b0441da4SMatthew G. Knepley   Notes:
5155b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5156b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5157b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5158b0441da4SMatthew G. Knepley 
5159b0441da4SMatthew G. Knepley   Level: developer
5160b0441da4SMatthew G. Knepley 
5161b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
5162b0441da4SMatthew G. Knepley @*/
5163b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5164b0441da4SMatthew G. Knepley {
5165b0441da4SMatthew G. Knepley   PetscInt       Nf;
5166b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5167b0441da4SMatthew G. Knepley 
5168b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5169b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5170064a246eSJacob Faibussowitsch   if (useCone)    PetscValidBoolPointer(useCone, 2);
5171064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
5172b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5173b0441da4SMatthew G. Knepley   if (!Nf) {
5174b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5175b0441da4SMatthew G. Knepley   } else {
5176b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5177b0441da4SMatthew G. Knepley   }
5178b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5179b0441da4SMatthew G. Knepley }
5180b0441da4SMatthew G. Knepley 
5181b0441da4SMatthew G. Knepley /*@
5182b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5183b0441da4SMatthew G. Knepley 
5184b0441da4SMatthew G. Knepley   Not collective
5185b0441da4SMatthew G. Knepley 
5186b0441da4SMatthew G. Knepley   Input Parameters:
5187b0441da4SMatthew G. Knepley + dm         - The DM object
5188b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5189b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5190b0441da4SMatthew G. Knepley 
5191b0441da4SMatthew G. Knepley   Notes:
5192b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5193b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5194b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5195b0441da4SMatthew G. Knepley 
5196b0441da4SMatthew G. Knepley   Level: developer
5197b0441da4SMatthew G. Knepley 
5198b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
5199b0441da4SMatthew G. Knepley @*/
5200b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5201b0441da4SMatthew G. Knepley {
5202b0441da4SMatthew G. Knepley   PetscInt       Nf;
5203b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5204b0441da4SMatthew G. Knepley 
5205b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5206b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5207b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5208b0441da4SMatthew G. Knepley   if (!Nf) {
5209b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5210b0441da4SMatthew G. Knepley   } else {
5211b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5212e5e52638SMatthew G. Knepley   }
5213e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5214e5e52638SMatthew G. Knepley }
5215e5e52638SMatthew G. Knepley 
5216783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
521745480ffeSMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, DMLabel label)
5218783e2ec8SMatthew G. Knepley {
5219783e2ec8SMatthew G. Knepley   PetscObject    obj;
5220783e2ec8SMatthew G. Knepley   PetscClassId   id;
5221783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
5222783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
5223783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
5224783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
5225783e2ec8SMatthew G. Knepley 
5226783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5227783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
5228783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5229783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
5230783e2ec8SMatthew G. Knepley   if (isFE && label) {
5231783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
5232783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5233783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
523445480ffeSMatthew G. Knepley       DMLabel l;
5235783e2ec8SMatthew G. Knepley 
523645480ffeSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, NULL, &l, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
523745480ffeSMatthew G. Knepley       duplicate = l == label ? PETSC_TRUE : PETSC_FALSE;
5238783e2ec8SMatthew G. Knepley       if (duplicate) break;
5239783e2ec8SMatthew G. Knepley     }
5240783e2ec8SMatthew G. Knepley     if (!duplicate) {
5241783e2ec8SMatthew G. Knepley       DM plex;
5242783e2ec8SMatthew G. Knepley 
5243783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
5244783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
5245783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
5246783e2ec8SMatthew G. Knepley     }
5247783e2ec8SMatthew G. Knepley   }
5248783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5249783e2ec8SMatthew G. Knepley }
5250783e2ec8SMatthew G. Knepley 
5251e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5252e5e52638SMatthew G. Knepley {
5253e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5254e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5255e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5256e5e52638SMatthew G. Knepley 
5257e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5258e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5259e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
5260e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5261b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5262e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5263e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5264e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5265e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5266e5e52638SMatthew G. Knepley }
5267e5e52638SMatthew G. Knepley 
5268e5e52638SMatthew G. Knepley /*@
5269e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5270e5e52638SMatthew G. Knepley 
5271e5e52638SMatthew G. Knepley   Not collective
5272e5e52638SMatthew G. Knepley 
5273e5e52638SMatthew G. Knepley   Input Parameter:
5274e5e52638SMatthew G. Knepley . dm - The DM
5275e5e52638SMatthew G. Knepley 
5276e5e52638SMatthew G. Knepley   Output Parameter:
5277e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5278e5e52638SMatthew G. Knepley 
5279e5e52638SMatthew G. Knepley   Level: intermediate
5280e5e52638SMatthew G. Knepley 
5281e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5282e5e52638SMatthew G. Knepley @*/
5283e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5284e5e52638SMatthew G. Knepley {
5285e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5286e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5287534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5288e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5289e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5290e5e52638SMatthew G. Knepley }
5291e5e52638SMatthew G. Knepley 
5292e5e52638SMatthew G. Knepley /*@
5293e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5294e5e52638SMatthew G. Knepley 
5295d083f849SBarry Smith   Logically collective on dm
5296e5e52638SMatthew G. Knepley 
5297e5e52638SMatthew G. Knepley   Input Parameter:
5298e5e52638SMatthew G. Knepley . dm - The DM
5299e5e52638SMatthew G. Knepley 
5300e5e52638SMatthew G. Knepley   Level: intermediate
5301e5e52638SMatthew G. Knepley 
5302e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5303e5e52638SMatthew G. Knepley @*/
5304e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5305e5e52638SMatthew G. Knepley {
5306e5e52638SMatthew G. Knepley   PetscInt       s;
5307e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5308e5e52638SMatthew G. Knepley 
5309e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5310e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5311e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5312e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5313e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5314b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5315e5e52638SMatthew G. Knepley   }
5316e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5317e5e52638SMatthew G. Knepley   dm->probs = NULL;
5318e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5319e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5320e5e52638SMatthew G. Knepley }
5321e5e52638SMatthew G. Knepley 
5322e5e52638SMatthew G. Knepley /*@
5323e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5324e5e52638SMatthew G. Knepley 
5325e5e52638SMatthew G. Knepley   Not collective
5326e5e52638SMatthew G. Knepley 
5327e5e52638SMatthew G. Knepley   Input Parameter:
5328e5e52638SMatthew G. Knepley . dm    - The DM
5329e5e52638SMatthew G. Knepley 
5330e5e52638SMatthew G. Knepley   Output Parameter:
5331e5e52638SMatthew G. Knepley . prob - The default PetscDS
5332e5e52638SMatthew G. Knepley 
5333e5e52638SMatthew G. Knepley   Level: intermediate
5334e5e52638SMatthew G. Knepley 
5335e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5336e5e52638SMatthew G. Knepley @*/
5337e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5338e5e52638SMatthew G. Knepley {
5339b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5340b0143b4dSMatthew G. Knepley 
5341e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5342e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5343e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5344b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5345b0143b4dSMatthew G. Knepley     PetscDS ds;
5346b0143b4dSMatthew G. Knepley 
534745480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5348b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5349b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5350b0143b4dSMatthew G. Knepley   }
5351b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5352e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5353e5e52638SMatthew G. Knepley }
5354e5e52638SMatthew G. Knepley 
5355e5e52638SMatthew G. Knepley /*@
5356e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5357e5e52638SMatthew G. Knepley 
5358e5e52638SMatthew G. Knepley   Not collective
5359e5e52638SMatthew G. Knepley 
5360e5e52638SMatthew G. Knepley   Input Parameters:
5361e5e52638SMatthew G. Knepley + dm    - The DM
5362e5e52638SMatthew G. Knepley - point - Cell for the DS
5363e5e52638SMatthew G. Knepley 
5364e5e52638SMatthew G. Knepley   Output Parameter:
5365e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5366e5e52638SMatthew G. Knepley 
5367e5e52638SMatthew G. Knepley   Level: developer
5368e5e52638SMatthew G. Knepley 
5369b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5370e5e52638SMatthew G. Knepley @*/
5371e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5372e5e52638SMatthew G. Knepley {
5373e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5374e5e52638SMatthew G. Knepley   PetscInt       s;
5375e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5376e5e52638SMatthew G. Knepley 
5377e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5378e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5379e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5380360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5381e5e52638SMatthew G. Knepley   *prob = NULL;
5382e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5383e5e52638SMatthew G. Knepley     PetscInt val;
5384e5e52638SMatthew G. Knepley 
5385e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5386e5e52638SMatthew G. Knepley     else {
5387e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5388e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5389e5e52638SMatthew G. Knepley     }
5390e5e52638SMatthew G. Knepley   }
5391e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5392e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5393e5e52638SMatthew G. Knepley }
5394e5e52638SMatthew G. Knepley 
5395e5e52638SMatthew G. Knepley /*@
5396e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5397e5e52638SMatthew G. Knepley 
5398e5e52638SMatthew G. Knepley   Not collective
5399e5e52638SMatthew G. Knepley 
5400e5e52638SMatthew G. Knepley   Input Parameters:
5401e5e52638SMatthew G. Knepley + dm    - The DM
5402e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5403e5e52638SMatthew G. Knepley 
5404b3cf3223SMatthew G. Knepley   Output Parameters:
5405b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5406b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5407e5e52638SMatthew G. Knepley 
5408e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5409e5e52638SMatthew G. Knepley 
5410e5e52638SMatthew G. Knepley   Level: advanced
5411e5e52638SMatthew G. Knepley 
5412e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5413e5e52638SMatthew G. Knepley @*/
5414b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5415e5e52638SMatthew G. Knepley {
5416e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5417e5e52638SMatthew G. Knepley 
5418e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5419e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5420e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5421b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5422b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5423e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5424b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5425b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5426b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5427b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5428b3cf3223SMatthew G. Knepley     }
5429e5e52638SMatthew G. Knepley   }
54302df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5431e5e52638SMatthew G. Knepley }
5432e5e52638SMatthew G. Knepley 
5433e5e52638SMatthew G. Knepley /*@
5434083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5435083401c6SMatthew G. Knepley 
5436083401c6SMatthew G. Knepley   Collective on dm
5437083401c6SMatthew G. Knepley 
5438083401c6SMatthew G. Knepley   Input Parameters:
5439083401c6SMatthew G. Knepley + dm     - The DM
5440083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5441083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5442083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5443083401c6SMatthew G. Knepley 
5444083401c6SMatthew 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,
5445083401c6SMatthew G. Knepley   the fields argument is ignored.
5446083401c6SMatthew G. Knepley 
5447083401c6SMatthew G. Knepley   Level: advanced
5448083401c6SMatthew G. Knepley 
5449083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5450083401c6SMatthew G. Knepley @*/
5451083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5452083401c6SMatthew G. Knepley {
5453083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5454083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5455083401c6SMatthew G. Knepley 
5456083401c6SMatthew G. Knepley   PetscFunctionBegin;
5457083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5458083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5459064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5460083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5461083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5462083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5463083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5464083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5465083401c6SMatthew G. Knepley     }
5466083401c6SMatthew G. Knepley   }
5467083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5468083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5469083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5470083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5471083401c6SMatthew G. Knepley   if (!label) {
5472083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5473083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5474083401c6SMatthew G. Knepley     Nds = 0;
5475083401c6SMatthew G. Knepley   }
5476083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5477083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5478083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5479083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5480083401c6SMatthew G. Knepley }
5481083401c6SMatthew G. Knepley 
5482083401c6SMatthew G. Knepley /*@
5483e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5484e5e52638SMatthew G. Knepley 
5485e5e52638SMatthew G. Knepley   Not collective
5486e5e52638SMatthew G. Knepley 
5487e5e52638SMatthew G. Knepley   Input Parameters:
5488e5e52638SMatthew G. Knepley + dm  - The DM
5489e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5490e5e52638SMatthew G. Knepley 
5491e5e52638SMatthew G. Knepley   Output Parameters:
5492b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5493b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5494083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5495e5e52638SMatthew G. Knepley 
5496e5e52638SMatthew G. Knepley   Level: advanced
5497e5e52638SMatthew G. Knepley 
5498e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5499e5e52638SMatthew G. Knepley @*/
5500b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5501e5e52638SMatthew G. Knepley {
5502e5e52638SMatthew G. Knepley   PetscInt       Nds;
5503e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5504e5e52638SMatthew G. Knepley 
5505e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5506e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5507e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5508e5e52638SMatthew 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);
5509e5e52638SMatthew G. Knepley   if (label) {
5510e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5511e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5512e5e52638SMatthew G. Knepley   }
5513b3cf3223SMatthew G. Knepley   if (fields) {
5514b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5515b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5516b3cf3223SMatthew G. Knepley   }
5517e5e52638SMatthew G. Knepley   if (ds) {
5518b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5519e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5520e5e52638SMatthew G. Knepley   }
5521e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5522e5e52638SMatthew G. Knepley }
5523e5e52638SMatthew G. Knepley 
5524e5e52638SMatthew G. Knepley /*@
5525083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5526e5e52638SMatthew G. Knepley 
5527083401c6SMatthew G. Knepley   Not collective
5528e5e52638SMatthew G. Knepley 
5529e5e52638SMatthew G. Knepley   Input Parameters:
5530e5e52638SMatthew G. Knepley + dm     - The DM
5531083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5532083401c6SMatthew G. Knepley . label  - The region label, or NULL
5533083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5534083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5535e5e52638SMatthew G. Knepley 
5536e5e52638SMatthew G. Knepley   Level: advanced
5537e5e52638SMatthew G. Knepley 
5538083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5539e5e52638SMatthew G. Knepley @*/
5540083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5541e5e52638SMatthew G. Knepley {
5542083401c6SMatthew G. Knepley   PetscInt       Nds;
5543e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5544e5e52638SMatthew G. Knepley 
5545e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5546e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5547083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5548083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5549083401c6SMatthew 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);
5550e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5551083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5552083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5553083401c6SMatthew G. Knepley   if (fields) {
5554083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5555b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5556083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5557083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5558e5e52638SMatthew G. Knepley   }
5559083401c6SMatthew G. Knepley   if (ds) {
5560083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5561083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5562083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5563083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5564083401c6SMatthew G. Knepley   }
5565e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5566e5e52638SMatthew G. Knepley }
5567e5e52638SMatthew G. Knepley 
5568e5e52638SMatthew G. Knepley /*@
55691d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
55701d3af9e0SMatthew G. Knepley 
55711d3af9e0SMatthew G. Knepley   Not collective
55721d3af9e0SMatthew G. Knepley 
55731d3af9e0SMatthew G. Knepley   Input Parameters:
55741d3af9e0SMatthew G. Knepley + dm  - The DM
55751d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
55761d3af9e0SMatthew G. Knepley 
55771d3af9e0SMatthew G. Knepley   Output Parameter:
55781d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
55791d3af9e0SMatthew G. Knepley 
55801d3af9e0SMatthew G. Knepley   Level: advanced
55811d3af9e0SMatthew G. Knepley 
55821d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
55831d3af9e0SMatthew G. Knepley @*/
55841d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
55851d3af9e0SMatthew G. Knepley {
55861d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
55871d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
55881d3af9e0SMatthew G. Knepley 
55891d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
55901d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55911d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
55921d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
55931d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
55941d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
55951d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
55961d3af9e0SMatthew G. Knepley   else          *num = n;
55971d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
55981d3af9e0SMatthew G. Knepley }
55991d3af9e0SMatthew G. Knepley 
56001d3af9e0SMatthew G. Knepley /*@
5601e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5602e5e52638SMatthew G. Knepley 
5603d083f849SBarry Smith   Collective on dm
5604e5e52638SMatthew G. Knepley 
5605e5e52638SMatthew G. Knepley   Input Parameter:
5606e5e52638SMatthew G. Knepley . dm - The DM
5607e5e52638SMatthew G. Knepley 
560845480ffeSMatthew G. Knepley   Options Database Keys:
560945480ffeSMatthew G. Knepley . -dm_petscds_view - View all the PetscDS objects in this DM
561045480ffeSMatthew G. Knepley 
5611e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5612e5e52638SMatthew G. Knepley 
5613e5e52638SMatthew G. Knepley   Level: intermediate
5614e5e52638SMatthew G. Knepley 
5615e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5616e5e52638SMatthew G. Knepley @*/
5617e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5618e5e52638SMatthew G. Knepley {
5619e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5620083401c6SMatthew G. Knepley   PetscDS        dsDef;
5621083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5622f9244615SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5623f9244615SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE, flg;
5624e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5625e5e52638SMatthew G. Knepley 
5626e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5627e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5628e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5629e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5630083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5631083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5632083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5633083401c6SMatthew G. Knepley   Nl   = 0;
5634083401c6SMatthew G. Knepley   Ndef = 0;
5635083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5636083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5637083401c6SMatthew G. Knepley     PetscInt l;
5638083401c6SMatthew G. Knepley 
5639f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5640f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5641f918ec44SMatthew G. Knepley     {
5642f918ec44SMatthew G. Knepley       PetscClassId id;
5643f918ec44SMatthew G. Knepley 
5644f918ec44SMatthew G. Knepley       ierr = PetscObjectGetClassId(dm->fields[f].disc, &id);CHKERRQ(ierr);
5645f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5646f918ec44SMatthew G. Knepley         Ceed ceed;
5647f918ec44SMatthew G. Knepley 
5648f918ec44SMatthew G. Knepley         ierr = DMGetCeed(dm, &ceed);CHKERRQ(ierr);
5649f918ec44SMatthew G. Knepley         ierr = PetscFESetCeed((PetscFE) dm->fields[f].disc, ceed);CHKERRQ(ierr);
5650f918ec44SMatthew G. Knepley       }
5651f918ec44SMatthew G. Knepley     }
5652f918ec44SMatthew G. Knepley #endif
5653083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5654083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5655083401c6SMatthew G. Knepley     if (l < Nl) continue;
5656083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5657083401c6SMatthew G. Knepley   }
5658083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5659083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5660083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5661b3cf3223SMatthew G. Knepley     IS        fields;
5662b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5663b3cf3223SMatthew G. Knepley 
5664b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5665083401c6SMatthew G. Knepley     if (nf) {
5666b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5667b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
566888f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
566988f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
567088f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
567188f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
567288f0c812SMatthew G. Knepley 
567345480ffeSMatthew G. Knepley       ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5674083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5675083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5676b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
56772df9ee95SMatthew G. Knepley     }
5678083401c6SMatthew G. Knepley   }
5679083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5680083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5681083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5682083401c6SMatthew G. Knepley   if (Ndef && Nl) {
56830122748bSMatthew G. Knepley     DM              plex;
5684083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5685083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5686083401c6SMatthew G. Knepley     PetscInt       *fields;
5687083401c6SMatthew G. Knepley     const PetscInt *cells;
5688083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
56890122748bSMatthew G. Knepley 
56900122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
56910122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5692083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5693083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5694083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5695083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5696083401c6SMatthew G. Knepley       IS      pointIS;
5697083401c6SMatthew G. Knepley 
5698083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5699083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5700083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5701083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5702083401c6SMatthew G. Knepley     }
5703083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5704083401c6SMatthew G. Knepley 
5705083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5706083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5707083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5708083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5709083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5710083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5711083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5712083401c6SMatthew G. Knepley 
5713083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5714083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5715083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5716083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5717083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5718083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5719083401c6SMatthew G. Knepley 
572045480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5721083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5722083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5723083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5724083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5725083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
57260122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5727083401c6SMatthew G. Knepley   }
5728083401c6SMatthew G. Knepley   /* Create label DSes
5729083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5730083401c6SMatthew G. Knepley   */
5731083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5732083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5733083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5734083401c6SMatthew G. Knepley     PetscDS   ds;
5735083401c6SMatthew G. Knepley     IS        fields;
5736083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5737083401c6SMatthew G. Knepley 
573845480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5739083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5740083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5741083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5742083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5743083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5744083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5745083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5746083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5747083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5748083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5749083401c6SMatthew G. Knepley     {
5750083401c6SMatthew G. Knepley       DMPolytopeType ct;
5751083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5752083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
57530122748bSMatthew G. Knepley 
5754e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5755665f567fSMatthew G. Knepley       if (lStart >= 0) {
5756412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5757412e9a14SMatthew G. Knepley         switch (ct) {
5758412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5759412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5760412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5761412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5762083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5763083401c6SMatthew G. Knepley           default: break;
5764412e9a14SMatthew G. Knepley         }
5765665f567fSMatthew G. Knepley       }
5766ffc4695bSBarry Smith       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRMPI(ierr);
5767083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5768e5e52638SMatthew G. Knepley     }
5769083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5770e5e52638SMatthew G. Knepley   }
5771083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5772e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5773083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5774083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5775083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5776083401c6SMatthew G. Knepley     const PetscInt *fld;
5777083401c6SMatthew G. Knepley     PetscInt        nf;
5778e5e52638SMatthew G. Knepley 
5779083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5780083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5781083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5782083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5783083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5784e5e52638SMatthew G. Knepley       PetscClassId id;
5785e5e52638SMatthew G. Knepley 
5786083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5787083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5788083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5789083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5790083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5791e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5792e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5793e5e52638SMatthew G. Knepley     }
5794083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5795e5e52638SMatthew G. Knepley   }
5796f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
5797f9244615SMatthew G. Knepley   ierr = PetscOptionsGetInt(NULL, ((PetscObject) dm)->prefix, "-dm_ds_jet_degree", &k, &flg);CHKERRQ(ierr);
5798f9244615SMatthew G. Knepley   if (flg) {
57993b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
58003b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
58013b4aee56SMatthew G. Knepley       PetscInt Nf, f;
58023b4aee56SMatthew G. Knepley 
58033b4aee56SMatthew G. Knepley       ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
58043b4aee56SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {ierr = PetscDSSetJetDegree(ds, f, k);CHKERRQ(ierr);}
58053b4aee56SMatthew G. Knepley     }
5806f9244615SMatthew G. Knepley   }
5807e5e52638SMatthew G. Knepley   /* Setup DSes */
5808e5e52638SMatthew G. Knepley   if (doSetup) {
5809e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5810e5e52638SMatthew G. Knepley   }
5811e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5812e5e52638SMatthew G. Knepley }
5813e5e52638SMatthew G. Knepley 
5814e5e52638SMatthew G. Knepley /*@
58157f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
58167f96f943SMatthew G. Knepley 
5817f2cacb80SMatthew G. Knepley   Collective on DM
5818f2cacb80SMatthew G. Knepley 
58197f96f943SMatthew G. Knepley   Input Parameters:
58207f96f943SMatthew G. Knepley + dm   - The DM
58217f96f943SMatthew G. Knepley - time - The time
58227f96f943SMatthew G. Knepley 
58237f96f943SMatthew G. Knepley   Output Parameters:
5824f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5825f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
58267f96f943SMatthew G. Knepley 
58277f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
58287f96f943SMatthew G. Knepley 
58297f96f943SMatthew G. Knepley   Level: developer
58307f96f943SMatthew G. Knepley 
58317f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
58327f96f943SMatthew G. Knepley @*/
5833f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
58347f96f943SMatthew G. Knepley {
58357f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
58367f96f943SMatthew G. Knepley   void            **ectxs;
58377f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
58387f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
58397f96f943SMatthew G. Knepley 
58407f96f943SMatthew G. Knepley   PetscFunctionBegin;
5841f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5842f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5843f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
58447f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
58457f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
58467f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
58477f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
58487f96f943SMatthew G. Knepley     PetscDS         ds;
58497f96f943SMatthew G. Knepley     DMLabel         label;
58507f96f943SMatthew G. Knepley     IS              fieldIS;
58517f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
58527f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
58537f96f943SMatthew G. Knepley 
58547f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
58557f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
58567f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
58577f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
58587f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5859f2cacb80SMatthew G. Knepley     if (u) {
58607f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
58617f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
58627f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
58637f96f943SMatthew G. Knepley       }
58647f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
58657f96f943SMatthew G. Knepley       if (label) {
58667f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
58677f96f943SMatthew G. Knepley       } else {
58687f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
58697f96f943SMatthew G. Knepley       }
58707f96f943SMatthew G. Knepley     }
5871f2cacb80SMatthew G. Knepley     if (u_t) {
5872f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5873f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5874f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5875f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5876f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5877f2cacb80SMatthew G. Knepley       }
5878f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5879f2cacb80SMatthew G. Knepley       if (label) {
5880f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5881f2cacb80SMatthew G. Knepley       } else {
5882f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5883f2cacb80SMatthew G. Knepley       }
5884f2cacb80SMatthew G. Knepley     }
5885f2cacb80SMatthew G. Knepley   }
5886f2cacb80SMatthew G. Knepley   if (u) {
58877f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
58887f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5889f2cacb80SMatthew G. Knepley   }
5890f2cacb80SMatthew G. Knepley   if (u_t) {
5891f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
5892f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
5893f2cacb80SMatthew G. Knepley   }
58947f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
58957f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
58967f96f943SMatthew G. Knepley }
58977f96f943SMatthew G. Knepley 
589845480ffeSMatthew G. Knepley PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
589945480ffeSMatthew G. Knepley {
590045480ffeSMatthew G. Knepley   PetscDS        dsNew;
590145480ffeSMatthew G. Knepley   DSBoundary     b;
590245480ffeSMatthew G. Knepley   PetscInt       cdim, Nf, f;
5903df911668SMatthew G. Knepley   PetscBool      isHybrid;
590445480ffeSMatthew G. Knepley   void          *ctx;
590545480ffeSMatthew G. Knepley   PetscErrorCode ierr;
590645480ffeSMatthew G. Knepley 
590745480ffeSMatthew G. Knepley   PetscFunctionBegin;
590845480ffeSMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) ds), &dsNew);CHKERRQ(ierr);
590945480ffeSMatthew G. Knepley   ierr = PetscDSCopyConstants(ds, dsNew);CHKERRQ(ierr);
591045480ffeSMatthew G. Knepley   ierr = PetscDSCopyExactSolutions(ds, dsNew);CHKERRQ(ierr);
591145480ffeSMatthew G. Knepley   ierr = PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
591245480ffeSMatthew G. Knepley   ierr = PetscDSCopyEquations(ds, dsNew);CHKERRQ(ierr);
591345480ffeSMatthew G. Knepley   ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
591445480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
591545480ffeSMatthew G. Knepley     ierr = PetscDSGetContext(ds, f, &ctx);CHKERRQ(ierr);
591645480ffeSMatthew G. Knepley     ierr = PetscDSSetContext(dsNew, f, ctx);CHKERRQ(ierr);
591745480ffeSMatthew G. Knepley   }
591845480ffeSMatthew G. Knepley   if (Nf) {
591945480ffeSMatthew G. Knepley     ierr = PetscDSGetCoordinateDimension(ds, &cdim);CHKERRQ(ierr);
592045480ffeSMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsNew, cdim);CHKERRQ(ierr);
592145480ffeSMatthew G. Knepley   }
592245480ffeSMatthew G. Knepley   ierr = PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
592345480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
592445480ffeSMatthew G. Knepley     ierr = DMGetLabel(dm, b->lname, &b->label);CHKERRQ(ierr);
592545480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
592645480ffeSMatthew G. Knepley     //if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
592745480ffeSMatthew G. Knepley   }
5928df911668SMatthew G. Knepley   ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5929df911668SMatthew G. Knepley   ierr = PetscDSSetHybrid(dsNew, isHybrid);CHKERRQ(ierr);
593045480ffeSMatthew G. Knepley 
593145480ffeSMatthew G. Knepley   ierr = DMSetRegionDS(dm, label, fields, dsNew);CHKERRQ(ierr);
593245480ffeSMatthew G. Knepley   ierr = PetscDSDestroy(&dsNew);CHKERRQ(ierr);
593345480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
593445480ffeSMatthew G. Knepley }
593545480ffeSMatthew G. Knepley 
59367f96f943SMatthew G. Knepley /*@
5937e5e52638SMatthew G. Knepley   DMCopyDS - Copy the 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(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5950e5e52638SMatthew G. Knepley @*/
5951e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5952e5e52638SMatthew G. Knepley {
5953e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5954e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5955e5e52638SMatthew G. Knepley 
5956e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5957e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5958e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5959e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5960e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5961e5e52638SMatthew G. Knepley     DMLabel  label;
5962b3cf3223SMatthew G. Knepley     IS       fields;
596345480ffeSMatthew G. Knepley     PetscDS  ds, newds;
5964783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5965e5e52638SMatthew G. Knepley 
5966b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5967b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
596845480ffeSMatthew G. Knepley     ierr = DMTransferDS_Internal(newdm, label, fields, ds);CHKERRQ(ierr);
596945480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
597045480ffeSMatthew G. Knepley     ierr = DMGetRegionDS(newdm, label, NULL, &newds);CHKERRQ(ierr);
597145480ffeSMatthew G. Knepley     ierr = PetscDSGetNumBoundary(newds, &Nbd);CHKERRQ(ierr);
5972783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5973b8025e53SMatthew G. Knepley       PetscWeakForm wf;
597445480ffeSMatthew G. Knepley       DMLabel       label;
5975783e2ec8SMatthew G. Knepley       PetscInt      field;
5976783e2ec8SMatthew G. Knepley 
5977b8025e53SMatthew G. Knepley       ierr = PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
597845480ffeSMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, newds, field, bd, label);CHKERRQ(ierr);
5979b8025e53SMatthew G. Knepley       ierr = PetscWeakFormReplaceLabel(wf, label);CHKERRQ(ierr);
5980783e2ec8SMatthew G. Knepley     }
5981e5e52638SMatthew G. Knepley   }
5982e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5983e5e52638SMatthew G. Knepley }
5984e5e52638SMatthew G. Knepley 
5985e5e52638SMatthew G. Knepley /*@
5986e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5987e5e52638SMatthew G. Knepley 
5988d083f849SBarry Smith   Collective on dm
5989e5e52638SMatthew G. Knepley 
5990e5e52638SMatthew G. Knepley   Input Parameter:
5991e5e52638SMatthew G. Knepley . dm - The DM
5992e5e52638SMatthew G. Knepley 
5993e5e52638SMatthew G. Knepley   Output Parameter:
5994e5e52638SMatthew G. Knepley . newdm - The DM
5995e5e52638SMatthew G. Knepley 
5996e5e52638SMatthew G. Knepley   Level: advanced
5997e5e52638SMatthew G. Knepley 
5998e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5999e5e52638SMatthew G. Knepley @*/
6000e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6001e5e52638SMatthew G. Knepley {
6002e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
6003e5e52638SMatthew G. Knepley 
6004e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6005e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
6006e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
6007e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6008e5e52638SMatthew G. Knepley }
6009e5e52638SMatthew G. Knepley 
6010b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
6011b64e0483SPeter Brune {
6012b64e0483SPeter Brune   DM dm_coord,dmc_coord;
6013b64e0483SPeter Brune   PetscErrorCode ierr;
6014b64e0483SPeter Brune   Vec coords,ccoords;
60156dbf9973SLawrence Mitchell   Mat inject;
6016b64e0483SPeter Brune   PetscFunctionBegin;
6017b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
6018b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
6019b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
6020b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
6021b64e0483SPeter Brune   if (coords && !ccoords) {
6022b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
60236668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
60246dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
60252adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
60266dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
6027b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
6028b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
6029b64e0483SPeter Brune   }
6030b64e0483SPeter Brune   PetscFunctionReturn(0);
6031b64e0483SPeter Brune }
6032b64e0483SPeter Brune 
603303dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
603403dadc2fSPeter Brune {
603503dadc2fSPeter Brune   DM dm_coord,subdm_coord;
603603dadc2fSPeter Brune   PetscErrorCode ierr;
603703dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
603803dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
603903dadc2fSPeter Brune   PetscFunctionBegin;
604003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
604103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
604203dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
604303dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
604403dadc2fSPeter Brune   if (coords && !ccoords) {
604503dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
60466668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
604703dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
604824640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
604903dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
605003dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
605103dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
60521ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
605303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
605403dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
605503dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
605603dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
605703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
605803dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
605903dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
606003dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
606103dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
606203dadc2fSPeter Brune   }
606303dadc2fSPeter Brune   PetscFunctionReturn(0);
606403dadc2fSPeter Brune }
606503dadc2fSPeter Brune 
6066c73cfb54SMatthew G. Knepley /*@
6067c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
6068c73cfb54SMatthew G. Knepley 
6069c73cfb54SMatthew G. Knepley   Not collective
6070c73cfb54SMatthew G. Knepley 
6071c73cfb54SMatthew G. Knepley   Input Parameter:
6072c73cfb54SMatthew G. Knepley . dm - The DM
6073c73cfb54SMatthew G. Knepley 
6074c73cfb54SMatthew G. Knepley   Output Parameter:
6075c73cfb54SMatthew G. Knepley . dim - The topological dimension
6076c73cfb54SMatthew G. Knepley 
6077c73cfb54SMatthew G. Knepley   Level: beginner
6078c73cfb54SMatthew G. Knepley 
6079c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
6080c73cfb54SMatthew G. Knepley @*/
6081c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6082c73cfb54SMatthew G. Knepley {
6083c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6084c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6085534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6086c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6087c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6088c73cfb54SMatthew G. Knepley }
6089c73cfb54SMatthew G. Knepley 
6090c73cfb54SMatthew G. Knepley /*@
6091c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
6092c73cfb54SMatthew G. Knepley 
6093c73cfb54SMatthew G. Knepley   Collective on dm
6094c73cfb54SMatthew G. Knepley 
6095c73cfb54SMatthew G. Knepley   Input Parameters:
6096c73cfb54SMatthew G. Knepley + dm - The DM
6097c73cfb54SMatthew G. Knepley - dim - The topological dimension
6098c73cfb54SMatthew G. Knepley 
6099c73cfb54SMatthew G. Knepley   Level: beginner
6100c73cfb54SMatthew G. Knepley 
6101c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
6102c73cfb54SMatthew G. Knepley @*/
6103c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6104c73cfb54SMatthew G. Knepley {
6105e5e52638SMatthew G. Knepley   PetscDS        ds;
610645480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6107f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6108f17e8794SMatthew G. Knepley 
6109c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6110c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6111c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6112c73cfb54SMatthew G. Knepley   dm->dim = dim;
611345480ffeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
611445480ffeSMatthew G. Knepley   for (n = 0; n < Nds; ++n) {
611545480ffeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
611645480ffeSMatthew G. Knepley     if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);}
611745480ffeSMatthew G. Knepley   }
6118c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6119c73cfb54SMatthew G. Knepley }
6120c73cfb54SMatthew G. Knepley 
6121793f3fe5SMatthew G. Knepley /*@
6122793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6123793f3fe5SMatthew G. Knepley 
6124d083f849SBarry Smith   Collective on dm
6125793f3fe5SMatthew G. Knepley 
6126793f3fe5SMatthew G. Knepley   Input Parameters:
6127793f3fe5SMatthew G. Knepley + dm - the DM
6128793f3fe5SMatthew G. Knepley - dim - the dimension
6129793f3fe5SMatthew G. Knepley 
6130793f3fe5SMatthew G. Knepley   Output Parameters:
6131793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6132aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6133793f3fe5SMatthew G. Knepley 
6134793f3fe5SMatthew G. Knepley   Note:
6135793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6136a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6137793f3fe5SMatthew G. Knepley   then the interval is empty.
6138793f3fe5SMatthew G. Knepley 
6139793f3fe5SMatthew G. Knepley   Level: intermediate
6140793f3fe5SMatthew G. Knepley 
6141793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
6142793f3fe5SMatthew G. Knepley @*/
6143793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6144793f3fe5SMatthew G. Knepley {
6145793f3fe5SMatthew G. Knepley   PetscInt       d;
6146793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
6147793f3fe5SMatthew G. Knepley 
6148793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6149793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6150793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
6151793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
6152b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
6153793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
6154793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6155793f3fe5SMatthew G. Knepley }
6156793f3fe5SMatthew G. Knepley 
61576636e97aSMatthew G Knepley /*@
61586636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
61596636e97aSMatthew G Knepley 
6160d083f849SBarry Smith   Collective on dm
61616636e97aSMatthew G Knepley 
61626636e97aSMatthew G Knepley   Input Parameters:
61636636e97aSMatthew G Knepley + dm - the DM
61646636e97aSMatthew G Knepley - c - coordinate vector
61656636e97aSMatthew G Knepley 
61662dd40e9bSPatrick Sanan   Notes:
61672dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
61682dd40e9bSPatrick Sanan 
61692dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
61706636e97aSMatthew G Knepley 
61716636e97aSMatthew G Knepley   Level: intermediate
61726636e97aSMatthew G Knepley 
617360c22052SBarry Smith .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
61746636e97aSMatthew G Knepley @*/
61756636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
61766636e97aSMatthew G Knepley {
61776636e97aSMatthew G Knepley   PetscErrorCode ierr;
61786636e97aSMatthew G Knepley 
61796636e97aSMatthew G Knepley   PetscFunctionBegin;
61806636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61816636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
61826636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
61836636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
61846636e97aSMatthew G Knepley   dm->coordinates = c;
61856636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
6186b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
618703dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
61886636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61896636e97aSMatthew G Knepley }
61906636e97aSMatthew G Knepley 
61916636e97aSMatthew G Knepley /*@
61926636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
61936636e97aSMatthew G Knepley 
61947058e716SVaclav Hapla   Not collective
61956636e97aSMatthew G Knepley 
61966636e97aSMatthew G Knepley    Input Parameters:
61976636e97aSMatthew G Knepley +  dm - the DM
61986636e97aSMatthew G Knepley -  c - coordinate vector
61996636e97aSMatthew G Knepley 
62002dd40e9bSPatrick Sanan   Notes:
62016636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
62026636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
62036636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
62046636e97aSMatthew G Knepley 
62052dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
62062dd40e9bSPatrick Sanan 
62076636e97aSMatthew G Knepley   Level: intermediate
62086636e97aSMatthew G Knepley 
62096636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
62106636e97aSMatthew G Knepley @*/
62116636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
62126636e97aSMatthew G Knepley {
62136636e97aSMatthew G Knepley   PetscErrorCode ierr;
62146636e97aSMatthew G Knepley 
62156636e97aSMatthew G Knepley   PetscFunctionBegin;
62166636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62176636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
62186636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
62196636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
62208865f1eaSKarl Rupp 
62216636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
62228865f1eaSKarl Rupp 
62236636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
62246636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62256636e97aSMatthew G Knepley }
62266636e97aSMatthew G Knepley 
62276636e97aSMatthew G Knepley /*@
62286636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
62296636e97aSMatthew G Knepley 
6230d083f849SBarry Smith   Collective on dm
62316636e97aSMatthew G Knepley 
62326636e97aSMatthew G Knepley   Input Parameter:
62336636e97aSMatthew G Knepley . dm - the DM
62346636e97aSMatthew G Knepley 
62356636e97aSMatthew G Knepley   Output Parameter:
62366636e97aSMatthew G Knepley . c - global coordinate vector
62376636e97aSMatthew G Knepley 
62386636e97aSMatthew G Knepley   Note:
623960c22052SBarry Smith   This is a borrowed reference, so the user should NOT destroy this vector. When the DM is
624060c22052SBarry Smith   destroyed the array will no longer be valid.
62416636e97aSMatthew G Knepley 
6242959c7569SPatrick Sanan   Each process has only the locally-owned portion of the global coordinates (does NOT have the ghost coordinates).
62436636e97aSMatthew G Knepley 
62446636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
62456636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
62466636e97aSMatthew G Knepley 
62476636e97aSMatthew G Knepley   Level: intermediate
62486636e97aSMatthew G Knepley 
624960c22052SBarry Smith .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
62506636e97aSMatthew G Knepley @*/
62516636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
62526636e97aSMatthew G Knepley {
62536636e97aSMatthew G Knepley   PetscErrorCode ierr;
62546636e97aSMatthew G Knepley 
62556636e97aSMatthew G Knepley   PetscFunctionBegin;
62566636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62576636e97aSMatthew G Knepley   PetscValidPointer(c,2);
62581f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
62590298fd71SBarry Smith     DM        cdm = NULL;
62601970a576SMatthew G. Knepley     PetscBool localized;
62616636e97aSMatthew G Knepley 
62626636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
62636636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
62641970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
62651970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
62661970a576SMatthew G. Knepley     if (localized) {
62671970a576SMatthew G. Knepley       PetscInt cdim;
62681970a576SMatthew G. Knepley 
62691970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
62701970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
62711970a576SMatthew G. Knepley     }
62726636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
62736636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
62746636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
62756636e97aSMatthew G Knepley   }
62766636e97aSMatthew G Knepley   *c = dm->coordinates;
62776636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62786636e97aSMatthew G Knepley }
62796636e97aSMatthew G Knepley 
62806636e97aSMatthew G Knepley /*@
628181e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
628281e9a530SVaclav Hapla 
6283d083f849SBarry Smith   Collective on dm
628481e9a530SVaclav Hapla 
628581e9a530SVaclav Hapla   Input Parameter:
628681e9a530SVaclav Hapla . dm - the DM
628781e9a530SVaclav Hapla 
628881e9a530SVaclav Hapla   Level: advanced
628981e9a530SVaclav Hapla 
629081e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
629181e9a530SVaclav Hapla @*/
629281e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
629381e9a530SVaclav Hapla {
629481e9a530SVaclav Hapla   PetscErrorCode ierr;
629581e9a530SVaclav Hapla 
629681e9a530SVaclav Hapla   PetscFunctionBegin;
629781e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
629881e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
62991970a576SMatthew G. Knepley     DM        cdm = NULL;
63001970a576SMatthew G. Knepley     PetscBool localized;
63011970a576SMatthew G. Knepley 
630281e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
630381e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
63041970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
63051970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
63061970a576SMatthew G. Knepley     if (localized) {
63071970a576SMatthew G. Knepley       PetscInt cdim;
63081970a576SMatthew G. Knepley 
63091970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
63101970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
63111970a576SMatthew G. Knepley     }
631281e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
631381e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
631481e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
631581e9a530SVaclav Hapla   }
631681e9a530SVaclav Hapla   PetscFunctionReturn(0);
631781e9a530SVaclav Hapla }
631881e9a530SVaclav Hapla 
631981e9a530SVaclav Hapla /*@
63206636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
63216636e97aSMatthew G Knepley 
6322d083f849SBarry Smith   Collective on dm
63236636e97aSMatthew G Knepley 
63246636e97aSMatthew G Knepley   Input Parameter:
63256636e97aSMatthew G Knepley . dm - the DM
63266636e97aSMatthew G Knepley 
63276636e97aSMatthew G Knepley   Output Parameter:
63286636e97aSMatthew G Knepley . c - coordinate vector
63296636e97aSMatthew G Knepley 
63306636e97aSMatthew G Knepley   Note:
63316636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
63326636e97aSMatthew G Knepley 
63336636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
63346636e97aSMatthew G Knepley 
63356636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
63366636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
63376636e97aSMatthew G Knepley 
63386636e97aSMatthew G Knepley   Level: intermediate
63396636e97aSMatthew G Knepley 
634081e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
63416636e97aSMatthew G Knepley @*/
63426636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
63436636e97aSMatthew G Knepley {
63446636e97aSMatthew G Knepley   PetscErrorCode ierr;
63456636e97aSMatthew G Knepley 
63466636e97aSMatthew G Knepley   PetscFunctionBegin;
63476636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63486636e97aSMatthew G Knepley   PetscValidPointer(c,2);
634981e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
63506636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
63516636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63526636e97aSMatthew G Knepley }
63536636e97aSMatthew G Knepley 
635481e9a530SVaclav Hapla /*@
635581e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
635681e9a530SVaclav Hapla 
635781e9a530SVaclav Hapla   Not collective
635881e9a530SVaclav Hapla 
635981e9a530SVaclav Hapla   Input Parameter:
636081e9a530SVaclav Hapla . dm - the DM
636181e9a530SVaclav Hapla 
636281e9a530SVaclav Hapla   Output Parameter:
636381e9a530SVaclav Hapla . c - coordinate vector
636481e9a530SVaclav Hapla 
636581e9a530SVaclav Hapla   Level: advanced
636681e9a530SVaclav Hapla 
636781e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
636881e9a530SVaclav Hapla @*/
636981e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
637081e9a530SVaclav Hapla {
637181e9a530SVaclav Hapla   PetscFunctionBegin;
637281e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
637381e9a530SVaclav Hapla   PetscValidPointer(c,2);
637481e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
63756636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
63766636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63776636e97aSMatthew G Knepley }
63786636e97aSMatthew G Knepley 
63792db98f8dSVaclav Hapla /*@
63802db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
63812db98f8dSVaclav Hapla 
63822db98f8dSVaclav Hapla   Not collective
63832db98f8dSVaclav Hapla 
6384d8d19677SJose E. Roman   Input Parameters:
63852db98f8dSVaclav Hapla + dm - the DM
63862db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
63872db98f8dSVaclav Hapla 
6388d8d19677SJose E. Roman   Output Parameters:
63892db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
63902db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
63912db98f8dSVaclav Hapla 
63922db98f8dSVaclav Hapla   Note:
63932db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
63942db98f8dSVaclav Hapla 
63952db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
63962db98f8dSVaclav Hapla 
63972db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
63982db98f8dSVaclav Hapla 
63992db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
64002db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
64012db98f8dSVaclav Hapla 
64022db98f8dSVaclav Hapla   Level: advanced
64032db98f8dSVaclav Hapla 
64042db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
64052db98f8dSVaclav Hapla @*/
64062db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
64072db98f8dSVaclav Hapla {
64082db98f8dSVaclav Hapla   PetscSection        cs, newcs;
64092db98f8dSVaclav Hapla   Vec                 coords;
64102db98f8dSVaclav Hapla   const PetscScalar   *arr;
64112db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
64122db98f8dSVaclav Hapla   PetscInt            n;
64132db98f8dSVaclav Hapla   PetscErrorCode      ierr;
64142db98f8dSVaclav Hapla 
64152db98f8dSVaclav Hapla   PetscFunctionBegin;
64162db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64172db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
64182db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
64192db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
64202db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
64211bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
64221bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
64232db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
64242db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
64252db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
64262db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
64272db98f8dSVaclav Hapla   if (pCoord) {
64282db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
64292db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
64302db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
64312db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6432ad9ac99dSVaclav Hapla   } else {
6433ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
64342db98f8dSVaclav Hapla   }
6435ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6436ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
64372db98f8dSVaclav Hapla   PetscFunctionReturn(0);
64382db98f8dSVaclav Hapla }
64392db98f8dSVaclav Hapla 
6440f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6441f19dbd58SToby Isaac {
6442f19dbd58SToby Isaac   PetscErrorCode ierr;
6443f19dbd58SToby Isaac 
6444f19dbd58SToby Isaac   PetscFunctionBegin;
6445f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6446f19dbd58SToby Isaac   PetscValidPointer(field,2);
6447f19dbd58SToby Isaac   if (!dm->coordinateField) {
6448f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6449f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6450f19dbd58SToby Isaac     }
6451f19dbd58SToby Isaac   }
6452f19dbd58SToby Isaac   *field = dm->coordinateField;
6453f19dbd58SToby Isaac   PetscFunctionReturn(0);
6454f19dbd58SToby Isaac }
6455f19dbd58SToby Isaac 
6456f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6457f19dbd58SToby Isaac {
6458f19dbd58SToby Isaac   PetscErrorCode ierr;
6459f19dbd58SToby Isaac 
6460f19dbd58SToby Isaac   PetscFunctionBegin;
6461f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6462f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6463c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6464f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6465f19dbd58SToby Isaac   dm->coordinateField = field;
6466f19dbd58SToby Isaac   PetscFunctionReturn(0);
6467f19dbd58SToby Isaac }
6468f19dbd58SToby Isaac 
64696636e97aSMatthew G Knepley /*@
64701cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
64716636e97aSMatthew G Knepley 
6472d083f849SBarry Smith   Collective on dm
64736636e97aSMatthew G Knepley 
64746636e97aSMatthew G Knepley   Input Parameter:
64756636e97aSMatthew G Knepley . dm - the DM
64766636e97aSMatthew G Knepley 
64776636e97aSMatthew G Knepley   Output Parameter:
64786636e97aSMatthew G Knepley . cdm - coordinate DM
64796636e97aSMatthew G Knepley 
64806636e97aSMatthew G Knepley   Level: intermediate
64816636e97aSMatthew G Knepley 
64821cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
64836636e97aSMatthew G Knepley @*/
64846636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
64856636e97aSMatthew G Knepley {
64866636e97aSMatthew G Knepley   PetscErrorCode ierr;
64876636e97aSMatthew G Knepley 
64886636e97aSMatthew G Knepley   PetscFunctionBegin;
64896636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64906636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
64916636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6492308f8a94SToby Isaac     DM cdm;
6493308f8a94SToby Isaac 
649482f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6495308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6496308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6497308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6498308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6499308f8a94SToby Isaac     dm->coordinateDM = cdm;
65006636e97aSMatthew G Knepley   }
65016636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
65026636e97aSMatthew G Knepley   PetscFunctionReturn(0);
65036636e97aSMatthew G Knepley }
6504e87bb0d3SMatthew G Knepley 
65051cfe2091SMatthew G. Knepley /*@
65061cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
65071cfe2091SMatthew G. Knepley 
6508d083f849SBarry Smith   Logically Collective on dm
65091cfe2091SMatthew G. Knepley 
65101cfe2091SMatthew G. Knepley   Input Parameters:
65111cfe2091SMatthew G. Knepley + dm - the DM
65121cfe2091SMatthew G. Knepley - cdm - coordinate DM
65131cfe2091SMatthew G. Knepley 
65141cfe2091SMatthew G. Knepley   Level: intermediate
65151cfe2091SMatthew G. Knepley 
65161cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
65171cfe2091SMatthew G. Knepley @*/
65181cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
65191cfe2091SMatthew G. Knepley {
65201cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
65211cfe2091SMatthew G. Knepley 
65221cfe2091SMatthew G. Knepley   PetscFunctionBegin;
65231cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65241cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6525f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
65261cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
65271cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
65281cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
65291cfe2091SMatthew G. Knepley }
65301cfe2091SMatthew G. Knepley 
653146e270d4SMatthew G. Knepley /*@
653246e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
653346e270d4SMatthew G. Knepley 
653446e270d4SMatthew G. Knepley   Not Collective
653546e270d4SMatthew G. Knepley 
653646e270d4SMatthew G. Knepley   Input Parameter:
653746e270d4SMatthew G. Knepley . dm - The DM object
653846e270d4SMatthew G. Knepley 
653946e270d4SMatthew G. Knepley   Output Parameter:
654046e270d4SMatthew G. Knepley . dim - The embedding dimension
654146e270d4SMatthew G. Knepley 
654246e270d4SMatthew G. Knepley   Level: intermediate
654346e270d4SMatthew G. Knepley 
654492fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
654546e270d4SMatthew G. Knepley @*/
654646e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
654746e270d4SMatthew G. Knepley {
654846e270d4SMatthew G. Knepley   PetscFunctionBegin;
654946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6550534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
65519a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
65529a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
65539a9a41abSToby Isaac   }
655446e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
655546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
655646e270d4SMatthew G. Knepley }
655746e270d4SMatthew G. Knepley 
655846e270d4SMatthew G. Knepley /*@
655946e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
656046e270d4SMatthew G. Knepley 
656146e270d4SMatthew G. Knepley   Not Collective
656246e270d4SMatthew G. Knepley 
656346e270d4SMatthew G. Knepley   Input Parameters:
656446e270d4SMatthew G. Knepley + dm  - The DM object
656546e270d4SMatthew G. Knepley - dim - The embedding dimension
656646e270d4SMatthew G. Knepley 
656746e270d4SMatthew G. Knepley   Level: intermediate
656846e270d4SMatthew G. Knepley 
656992fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
657046e270d4SMatthew G. Knepley @*/
657146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
657246e270d4SMatthew G. Knepley {
6573e5e52638SMatthew G. Knepley   PetscDS        ds;
657445480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6575f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6576f17e8794SMatthew G. Knepley 
657746e270d4SMatthew G. Knepley   PetscFunctionBegin;
657846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
657946e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
658045480ffeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
658145480ffeSMatthew G. Knepley   for (n = 0; n < Nds; ++n) {
658245480ffeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
6583e5e52638SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
658445480ffeSMatthew G. Knepley   }
658546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
658646e270d4SMatthew G. Knepley }
658746e270d4SMatthew G. Knepley 
6588e8abe2deSMatthew G. Knepley /*@
6589e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6590e8abe2deSMatthew G. Knepley 
6591d083f849SBarry Smith   Collective on dm
6592e8abe2deSMatthew G. Knepley 
6593e8abe2deSMatthew G. Knepley   Input Parameter:
6594e8abe2deSMatthew G. Knepley . dm - The DM object
6595e8abe2deSMatthew G. Knepley 
6596e8abe2deSMatthew G. Knepley   Output Parameter:
6597e8abe2deSMatthew G. Knepley . section - The PetscSection object
6598e8abe2deSMatthew G. Knepley 
6599e8abe2deSMatthew G. Knepley   Level: intermediate
6600e8abe2deSMatthew G. Knepley 
660192fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6602e8abe2deSMatthew G. Knepley @*/
6603e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6604e8abe2deSMatthew G. Knepley {
6605e8abe2deSMatthew G. Knepley   DM             cdm;
6606e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6607e8abe2deSMatthew G. Knepley 
6608e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6609e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6610e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6611e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
661292fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6613e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6614e8abe2deSMatthew G. Knepley }
6615e8abe2deSMatthew G. Knepley 
6616e8abe2deSMatthew G. Knepley /*@
6617e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6618e8abe2deSMatthew G. Knepley 
6619e8abe2deSMatthew G. Knepley   Not Collective
6620e8abe2deSMatthew G. Knepley 
6621e8abe2deSMatthew G. Knepley   Input Parameters:
6622e8abe2deSMatthew G. Knepley + dm      - The DM object
662346e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6624e8abe2deSMatthew G. Knepley - section - The PetscSection object
6625e8abe2deSMatthew G. Knepley 
6626e8abe2deSMatthew G. Knepley   Level: intermediate
6627e8abe2deSMatthew G. Knepley 
662892fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6629e8abe2deSMatthew G. Knepley @*/
663046e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6631e8abe2deSMatthew G. Knepley {
6632e8abe2deSMatthew G. Knepley   DM             cdm;
6633e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6634e8abe2deSMatthew G. Knepley 
6635e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6636e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
663746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6638e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
663992fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
664046e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
66414c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
664246e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
664346e270d4SMatthew G. Knepley 
664446e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
664546e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
664646e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
664746e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
664846e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
664946e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
665046e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
665146e270d4SMatthew G. Knepley     }
6652ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
665346e270d4SMatthew G. Knepley   }
6654e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6655e8abe2deSMatthew G. Knepley }
6656e8abe2deSMatthew G. Knepley 
6657d864a3eaSLisandro Dalcin /*@
6658d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6659d864a3eaSLisandro Dalcin 
6660d864a3eaSLisandro Dalcin   Input Parameters:
6661d864a3eaSLisandro Dalcin + dm      - The DM object
6662d864a3eaSLisandro Dalcin - disc    - The new coordinate discretization
6663d864a3eaSLisandro Dalcin 
6664d864a3eaSLisandro Dalcin   Level: intermediate
6665d864a3eaSLisandro Dalcin 
6666d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6667d864a3eaSLisandro Dalcin @*/
6668d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6669d864a3eaSLisandro Dalcin {
6670d864a3eaSLisandro Dalcin   PetscObject    discOld;
6671d864a3eaSLisandro Dalcin   PetscClassId   classid;
6672d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6673d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6674d864a3eaSLisandro Dalcin   Mat            matInterp;
6675d864a3eaSLisandro Dalcin   PetscErrorCode ierr;
6676d864a3eaSLisandro Dalcin 
6677d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6678d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6679d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6680d864a3eaSLisandro Dalcin 
6681d864a3eaSLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdmOld);CHKERRQ(ierr);
6682d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
6683d864a3eaSLisandro Dalcin   ierr = DMGetField(cdmOld, 0, NULL, &discOld);CHKERRQ(ierr);
6684d864a3eaSLisandro Dalcin   ierr = PetscObjectGetClassId(discOld, &classid);CHKERRQ(ierr);
668529ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
668629ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
668729ad44c5SMatthew G. Knepley       PetscFE        feLinear;
668829ad44c5SMatthew G. Knepley       DMPolytopeType ct;
668929ad44c5SMatthew G. Knepley       PetscInt       dim, dE, cStart;
669029ad44c5SMatthew G. Knepley       PetscBool      simplex;
669129ad44c5SMatthew G. Knepley 
669229ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
669329ad44c5SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
669429ad44c5SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
669529ad44c5SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdmOld, 0, &cStart, NULL);CHKERRQ(ierr);
669629ad44c5SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
669729ad44c5SMatthew G. Knepley       switch (ct) {
669829ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM:
669929ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
670029ad44c5SMatthew G. Knepley           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
670129ad44c5SMatthew G. Knepley         default: break;
670229ad44c5SMatthew G. Knepley       }
670329ad44c5SMatthew G. Knepley       simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
670429ad44c5SMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear);CHKERRQ(ierr);
670529ad44c5SMatthew G. Knepley       ierr = DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear);CHKERRQ(ierr);
670629ad44c5SMatthew G. Knepley       ierr = PetscFEDestroy(&feLinear);CHKERRQ(ierr);
670729ad44c5SMatthew G. Knepley       ierr = DMCreateDS(cdmOld);CHKERRQ(ierr);
670829ad44c5SMatthew G. Knepley     } else {
670929ad44c5SMatthew G. Knepley       const char *discname;
671029ad44c5SMatthew G. Knepley 
671129ad44c5SMatthew G. Knepley       ierr = PetscObjectGetType(discOld, &discname);CHKERRQ(ierr);
671229ad44c5SMatthew G. Knepley       SETERRQ1(PetscObjectComm(discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
671329ad44c5SMatthew G. Knepley     }
671429ad44c5SMatthew G. Knepley   }
6715d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6716d864a3eaSLisandro Dalcin   ierr = DMClone(cdmOld, &cdmNew);CHKERRQ(ierr);
6717d864a3eaSLisandro Dalcin   ierr = DMSetField(cdmNew, 0, NULL, (PetscObject) disc);CHKERRQ(ierr);
6718d864a3eaSLisandro Dalcin   ierr = DMCreateDS(cdmNew);CHKERRQ(ierr);
6719d864a3eaSLisandro Dalcin   /* Project the coordinate vector from old to new space  */
6720d864a3eaSLisandro Dalcin   ierr = DMGetCoordinates(dm, &coordsOld);CHKERRQ(ierr);
6721d864a3eaSLisandro Dalcin   ierr = DMCreateGlobalVector(cdmNew, &coordsNew);CHKERRQ(ierr);
6722d864a3eaSLisandro Dalcin   ierr = DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL);CHKERRQ(ierr);
6723d864a3eaSLisandro Dalcin   ierr = MatInterpolate(matInterp, coordsOld, coordsNew);CHKERRQ(ierr);
6724d864a3eaSLisandro Dalcin   ierr = MatDestroy(&matInterp);CHKERRQ(ierr);
6725d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6726d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateField(dm, NULL);CHKERRQ(ierr);
6727d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateDM(dm, cdmNew);CHKERRQ(ierr);
6728d864a3eaSLisandro Dalcin   ierr = DMSetCoordinates(dm, coordsNew);CHKERRQ(ierr);
6729d864a3eaSLisandro Dalcin   ierr = VecDestroy(&coordsNew);CHKERRQ(ierr);
6730d864a3eaSLisandro Dalcin   ierr = DMDestroy(&cdmNew);CHKERRQ(ierr);
6731d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6732d864a3eaSLisandro Dalcin }
6733d864a3eaSLisandro Dalcin 
67345dc8c3f7SMatthew G. Knepley /*@C
673590b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
67365dc8c3f7SMatthew G. Knepley 
6737f899ff85SJose E. Roman   Input Parameter:
673890b157c4SStefano Zampini . dm      - The DM object
673990b157c4SStefano Zampini 
674090b157c4SStefano Zampini   Output Parameters:
674190b157c4SStefano Zampini + per     - Whether the DM is periodic or not
67425dc8c3f7SMatthew 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
67435dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
67445dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
67455dc8c3f7SMatthew G. Knepley 
67465dc8c3f7SMatthew G. Knepley   Level: developer
67475dc8c3f7SMatthew G. Knepley 
67485dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
67495dc8c3f7SMatthew G. Knepley @*/
675090b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6751c6b900c6SMatthew G. Knepley {
6752c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6753c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
675490b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6755c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6756c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
67575dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6758c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6759c6b900c6SMatthew G. Knepley }
6760c6b900c6SMatthew G. Knepley 
67615dc8c3f7SMatthew G. Knepley /*@C
67625dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
67635dc8c3f7SMatthew G. Knepley 
67645dc8c3f7SMatthew G. Knepley   Input Parameters:
67655dc8c3f7SMatthew G. Knepley + dm      - The DM object
6766db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6767db2bf62eSStefano 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.
67685dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
67695dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
67705dc8c3f7SMatthew G. Knepley 
6771db2bf62eSStefano 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.
6772db2bf62eSStefano Zampini 
67735dc8c3f7SMatthew G. Knepley   Level: developer
67745dc8c3f7SMatthew G. Knepley 
67755dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
67765dc8c3f7SMatthew G. Knepley @*/
677790b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6778c6b900c6SMatthew G. Knepley {
6779c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6780c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6781c6b900c6SMatthew G. Knepley 
6782c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6783c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
678490b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6785412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6786412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6787412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
67885dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
678990b157c4SStefano Zampini   if (maxCell) {
6790412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6791412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6792db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6793db2bf62eSStefano Zampini     ierr = PetscFree(dm->maxCell);CHKERRQ(ierr);
6794412e9a14SMatthew G. Knepley   }
6795db2bf62eSStefano Zampini 
6796412e9a14SMatthew G. Knepley   if (L) {
6797412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6798412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6799412e9a14SMatthew G. Knepley   }
6800412e9a14SMatthew G. Knepley   if (bd) {
6801412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6802412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
680390b157c4SStefano Zampini   }
6804072d7d67SStefano Zampini   dm->periodic = per;
6805c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6806c6b900c6SMatthew G. Knepley }
6807c6b900c6SMatthew G. Knepley 
68082e17dfb7SMatthew G. Knepley /*@
68092e17dfb7SMatthew 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.
68102e17dfb7SMatthew G. Knepley 
68112e17dfb7SMatthew G. Knepley   Input Parameters:
68122e17dfb7SMatthew G. Knepley + dm     - The DM
681365da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
681465da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
68152e17dfb7SMatthew G. Knepley 
68162e17dfb7SMatthew G. Knepley   Output Parameter:
68172e17dfb7SMatthew G. Knepley . out - The localized coordinate point
68182e17dfb7SMatthew G. Knepley 
68192e17dfb7SMatthew G. Knepley   Level: developer
68202e17dfb7SMatthew G. Knepley 
68212e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
68222e17dfb7SMatthew G. Knepley @*/
682365da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
68242e17dfb7SMatthew G. Knepley {
68252e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
68262e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
68272e17dfb7SMatthew G. Knepley 
68282e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68292e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
68302e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68312e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68322e17dfb7SMatthew G. Knepley   } else {
683365da65dcSMatthew G. Knepley     if (endpoint) {
683465da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6835da3333bfSMatthew 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)) {
6836da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
683765da65dcSMatthew G. Knepley         } else {
6838da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
683965da65dcSMatthew G. Knepley         }
684065da65dcSMatthew G. Knepley       }
684165da65dcSMatthew G. Knepley     } else {
68422e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
68431118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
68442e17dfb7SMatthew G. Knepley       }
68452e17dfb7SMatthew G. Knepley     }
684665da65dcSMatthew G. Knepley   }
68472e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68482e17dfb7SMatthew G. Knepley }
68492e17dfb7SMatthew G. Knepley 
68502e17dfb7SMatthew G. Knepley /*
68512e17dfb7SMatthew 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.
68522e17dfb7SMatthew G. Knepley 
68532e17dfb7SMatthew G. Knepley   Input Parameters:
68542e17dfb7SMatthew G. Knepley + dm     - The DM
68552e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
68562e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
68572e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
68582e17dfb7SMatthew G. Knepley 
68592e17dfb7SMatthew G. Knepley   Output Parameter:
68602e17dfb7SMatthew G. Knepley . out - The localized coordinate point
68612e17dfb7SMatthew G. Knepley 
68622e17dfb7SMatthew G. Knepley   Level: developer
68632e17dfb7SMatthew G. Knepley 
68642e17dfb7SMatthew 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
68652e17dfb7SMatthew G. Knepley 
68662e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
68672e17dfb7SMatthew G. Knepley */
68682e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
68692e17dfb7SMatthew G. Knepley {
68702e17dfb7SMatthew G. Knepley   PetscInt d;
68712e17dfb7SMatthew G. Knepley 
68722e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68732e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68742e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68752e17dfb7SMatthew G. Knepley   } else {
68762e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6877908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
68782e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
68792e17dfb7SMatthew G. Knepley       } else {
68802e17dfb7SMatthew G. Knepley         out[d] = in[d];
68812e17dfb7SMatthew G. Knepley       }
68822e17dfb7SMatthew G. Knepley     }
68832e17dfb7SMatthew G. Knepley   }
68842e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68852e17dfb7SMatthew G. Knepley }
6886a5801f52SStefano Zampini 
68872e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
68882e17dfb7SMatthew G. Knepley {
68892e17dfb7SMatthew G. Knepley   PetscInt d;
68902e17dfb7SMatthew G. Knepley 
68912e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68922e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68932e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68942e17dfb7SMatthew G. Knepley   } else {
68952e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6896908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
68972e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
68982e17dfb7SMatthew G. Knepley       } else {
68992e17dfb7SMatthew G. Knepley         out[d] = in[d];
69002e17dfb7SMatthew G. Knepley       }
69012e17dfb7SMatthew G. Knepley     }
69022e17dfb7SMatthew G. Knepley   }
69032e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69042e17dfb7SMatthew G. Knepley }
69052e17dfb7SMatthew G. Knepley 
69062e17dfb7SMatthew G. Knepley /*
69072e17dfb7SMatthew 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.
69082e17dfb7SMatthew G. Knepley 
69092e17dfb7SMatthew G. Knepley   Input Parameters:
69102e17dfb7SMatthew G. Knepley + dm     - The DM
69112e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
69122e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
69132e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
69142e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
69152e17dfb7SMatthew G. Knepley 
69162e17dfb7SMatthew G. Knepley   Output Parameter:
69172e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
69182e17dfb7SMatthew G. Knepley 
69192e17dfb7SMatthew G. Knepley   Level: developer
69202e17dfb7SMatthew G. Knepley 
69212e17dfb7SMatthew 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
69222e17dfb7SMatthew G. Knepley 
69232e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
69242e17dfb7SMatthew G. Knepley */
69252e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
69262e17dfb7SMatthew G. Knepley {
69272e17dfb7SMatthew G. Knepley   PetscInt d;
69282e17dfb7SMatthew G. Knepley 
69292e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69302e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
69312e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
69322e17dfb7SMatthew G. Knepley   } else {
69332e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6934412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6935412e9a14SMatthew G. Knepley 
6936412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6937412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6938412e9a14SMatthew G. Knepley 
6939412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6940412e9a14SMatthew 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]));
6941412e9a14SMatthew G. Knepley         out[d] += newCoord;
69422e17dfb7SMatthew G. Knepley       } else {
69432e17dfb7SMatthew G. Knepley         out[d] += in[d];
69442e17dfb7SMatthew G. Knepley       }
69452e17dfb7SMatthew G. Knepley     }
69462e17dfb7SMatthew G. Knepley   }
69472e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69482e17dfb7SMatthew G. Knepley }
69492e17dfb7SMatthew G. Knepley 
695036447a5eSToby Isaac /*@
69518f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
69528f700142SStefano Zampini 
69538f700142SStefano Zampini   Not collective
695436447a5eSToby Isaac 
695536447a5eSToby Isaac   Input Parameter:
695636447a5eSToby Isaac . dm - The DM
695736447a5eSToby Isaac 
695836447a5eSToby Isaac   Output Parameter:
695936447a5eSToby Isaac   areLocalized - True if localized
696036447a5eSToby Isaac 
696136447a5eSToby Isaac   Level: developer
696236447a5eSToby Isaac 
69638f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
696436447a5eSToby Isaac @*/
69658f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
696636447a5eSToby Isaac {
696736447a5eSToby Isaac   DM             cdm;
696836447a5eSToby Isaac   PetscSection   coordSection;
69692ccac677SMatthew G. Knepley   PetscInt       depth, cStart, cEnd, sStart, sEnd, c, dof;
697046a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
697136447a5eSToby Isaac   PetscErrorCode ierr;
697236447a5eSToby Isaac 
697336447a5eSToby Isaac   PetscFunctionBegin;
697436447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6975534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
69768b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
697746a3a80fSLisandro Dalcin 
697836447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
697936447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
698046a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
69819f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
69822ccac677SMatthew G. Knepley   ierr = DMPlexGetDepth(cdm, &depth);CHKERRQ(ierr);
69832ccac677SMatthew G. Knepley   if (!depth) PetscFunctionReturn(0);
698446a3a80fSLisandro Dalcin 
69859f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
698646a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
698736447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
698836447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
698946a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
699046a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
699136447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
699246a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
699336447a5eSToby Isaac   }
69948f700142SStefano Zampini   *areLocalized = alreadyLocalized;
699536447a5eSToby Isaac   PetscFunctionReturn(0);
699636447a5eSToby Isaac }
699736447a5eSToby Isaac 
69988f700142SStefano Zampini /*@
69998f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
70008f700142SStefano Zampini 
70018f700142SStefano Zampini   Collective on dm
70028f700142SStefano Zampini 
70038f700142SStefano Zampini   Input Parameter:
70048f700142SStefano Zampini . dm - The DM
70058f700142SStefano Zampini 
70068f700142SStefano Zampini   Output Parameter:
70078f700142SStefano Zampini   areLocalized - True if localized
70088f700142SStefano Zampini 
70098f700142SStefano Zampini   Level: developer
70108f700142SStefano Zampini 
70118f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
70128f700142SStefano Zampini @*/
70138f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
70148f700142SStefano Zampini {
70158f700142SStefano Zampini   PetscBool      localized;
70168f700142SStefano Zampini   PetscErrorCode ierr;
70178f700142SStefano Zampini 
70188f700142SStefano Zampini   PetscFunctionBegin;
70198f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7020534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
70218f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
7022820f2d46SBarry Smith   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
70238f700142SStefano Zampini   PetscFunctionReturn(0);
70248f700142SStefano Zampini }
702536447a5eSToby Isaac 
70262e17dfb7SMatthew G. Knepley /*@
7027492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
70282e17dfb7SMatthew G. Knepley 
70298f700142SStefano Zampini   Collective on dm
70308f700142SStefano Zampini 
70312e17dfb7SMatthew G. Knepley   Input Parameter:
70322e17dfb7SMatthew G. Knepley . dm - The DM
70332e17dfb7SMatthew G. Knepley 
70342e17dfb7SMatthew G. Knepley   Level: developer
70352e17dfb7SMatthew G. Knepley 
70368f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
70372e17dfb7SMatthew G. Knepley @*/
70382e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
70392e17dfb7SMatthew G. Knepley {
70402e17dfb7SMatthew G. Knepley   DM             cdm;
70412e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
70422e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
70433e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
70443e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
7045e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
70463e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
70473e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
70482e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
70492e17dfb7SMatthew G. Knepley 
70502e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
70512e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
705292c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
7053f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
7054f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
7055f7cbd40bSStefano Zampini 
70562e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
70572e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
70582e17dfb7SMatthew G. Knepley   {
70592e17dfb7SMatthew G. Knepley     PetscBool isplex;
70602e17dfb7SMatthew G. Knepley 
70612e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
70622e17dfb7SMatthew G. Knepley     if (isplex) {
70632e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
70643e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
706569291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
70663e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
70673e922f36SToby Isaac       newStart = vStart;
70683e922f36SToby Isaac       newEnd   = vEnd;
70693e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
70703e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
70713e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
70723e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
70733e922f36SToby Isaac       }
70742e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
70752e17dfb7SMatthew G. Knepley   }
70762e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
707743eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
70782e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
70793e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
7080e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
70813e922f36SToby Isaac 
70822e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
70832e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
70842e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
70852e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
70863e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
70873e922f36SToby Isaac 
708869291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
70893e922f36SToby Isaac   localized = &anchor[bs];
70903e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
70913e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
70923e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
70933e922f36SToby Isaac 
70943e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
70953e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
70963e922f36SToby Isaac       PetscInt     b;
70973e922f36SToby Isaac 
70983e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
70993e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
71003e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
71013e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
71023e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
71033e922f36SToby Isaac         for (b = 0; b < bs; b++) {
71043e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
71053e922f36SToby Isaac         }
71063e922f36SToby Isaac         if (b < bs) break;
71073e922f36SToby Isaac       }
71083e922f36SToby Isaac       if (d < dof/bs) {
71093e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
71103e922f36SToby Isaac           PetscInt cdof;
71113e922f36SToby Isaac 
71123e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
71133e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
71143e922f36SToby Isaac         }
71153e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
71163e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
71173e922f36SToby Isaac       }
71183e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
71193e922f36SToby Isaac     }
71203e922f36SToby Isaac   }
7121ffc4695bSBarry Smith   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
71223e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
712369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
71243e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
712569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
71263e922f36SToby Isaac     PetscFunctionReturn(0);
71273e922f36SToby Isaac   }
71282e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
71292e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
71302e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
71312e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
71322e17dfb7SMatthew G. Knepley   }
71332e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
71342e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
7135c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
71362e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
71372e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
71382e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
71392e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
7140c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
71412e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
71422e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
71432e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
71442e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
71452e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
71462e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
71472e17dfb7SMatthew G. Knepley   }
71483e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
71493e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
71503e922f36SToby Isaac 
71512e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
71522e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
71533e922f36SToby Isaac       PetscInt     b, cdof;
71542e17dfb7SMatthew G. Knepley 
71553e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
71563e922f36SToby Isaac       if (!cdof) continue;
71572e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
71582e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
71592e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
71602e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
71612e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
71622e17dfb7SMatthew G. Knepley     }
71633e922f36SToby Isaac   }
716469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
716569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
7166c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
71672e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
71682e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
71692e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
71702e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
71712e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
71722e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
71732e17dfb7SMatthew G. Knepley }
71742e17dfb7SMatthew G. Knepley 
7175e87bb0d3SMatthew G Knepley /*@
71763a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
7177e87bb0d3SMatthew G Knepley 
7178d083f849SBarry Smith   Collective on v (see explanation below)
7179e87bb0d3SMatthew G Knepley 
7180e87bb0d3SMatthew G Knepley   Input Parameters:
7181e87bb0d3SMatthew G Knepley + dm - The DM
71826b867d5aSJose E. Roman - ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
7183e87bb0d3SMatthew G Knepley 
71846b867d5aSJose E. Roman   Input/Output Parameters:
71856b867d5aSJose E. Roman + v - The Vec of points, on output contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
71866b867d5aSJose E. Roman - cellSF - Points to either NULL, or a PetscSF with guesses for which cells contain each point;
71876b867d5aSJose E. Roman            on output, the PetscSF containing the ranks and local indices of the containing points
71883a93e3b7SToby Isaac 
7189e87bb0d3SMatthew G Knepley   Level: developer
719061e3bb9bSMatthew G Knepley 
719162a38674SMatthew G. Knepley   Notes:
71923a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
719362a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
71943a93e3b7SToby Isaac 
71953a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
719662a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
71973a93e3b7SToby Isaac 
71983a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
71993a93e3b7SToby Isaac 
720062a38674SMatthew G. Knepley $    const PetscSFNode *cells;
720162a38674SMatthew G. Knepley $    PetscInt           nFound;
7202a6216909SToby Isaac $    const PetscInt    *found;
720362a38674SMatthew G. Knepley $
7204a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
72053a93e3b7SToby Isaac 
72063a93e3b7SToby 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
72073a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
72083a93e3b7SToby Isaac 
720962a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
721061e3bb9bSMatthew G Knepley @*/
721162a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
7212e87bb0d3SMatthew G Knepley {
7213735aa83eSMatthew G Knepley   PetscErrorCode ierr;
7214735aa83eSMatthew G Knepley 
7215e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
7216e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7217e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
7218e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
72193a93e3b7SToby Isaac   if (*cellSF) {
72203a93e3b7SToby Isaac     PetscMPIInt result;
72213a93e3b7SToby Isaac 
7222e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
7223ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRMPI(ierr);
72243a93e3b7SToby 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");
7225e0fc9d1bSMatthew G. Knepley   } else {
72263a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
72273a93e3b7SToby Isaac   }
7228b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
722947a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
723062a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
723147a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
7232e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
7233e87bb0d3SMatthew G Knepley }
723414f150ffSMatthew G. Knepley 
7235f4d763aaSMatthew G. Knepley /*@
7236f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
7237f4d763aaSMatthew G. Knepley 
72388f700142SStefano Zampini   Collective on dm
72398f700142SStefano Zampini 
7240f4d763aaSMatthew G. Knepley   Input Parameter:
7241f4d763aaSMatthew G. Knepley . dm - The original DM
7242f4d763aaSMatthew G. Knepley 
7243f4d763aaSMatthew G. Knepley   Output Parameter:
7244f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
7245f4d763aaSMatthew G. Knepley 
7246f4d763aaSMatthew G. Knepley   Level: intermediate
7247f4d763aaSMatthew G. Knepley 
7248e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
7249f4d763aaSMatthew G. Knepley @*/
725014f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
725114f150ffSMatthew G. Knepley {
7252c26acbdeSMatthew G. Knepley   PetscSection   section;
72532d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
725414f150ffSMatthew G. Knepley   PetscErrorCode ierr;
725514f150ffSMatthew G. Knepley 
725614f150ffSMatthew G. Knepley   PetscFunctionBegin;
725714f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
725814f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
725992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
7260c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
7261ffc4695bSBarry Smith   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
72622d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
7263c26acbdeSMatthew G. Knepley     *odm = dm;
7264c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
7265c26acbdeSMatthew G. Knepley   }
726614f150ffSMatthew G. Knepley   if (!dm->dmBC) {
7267c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
726814f150ffSMatthew G. Knepley     PetscSF      sf;
726914f150ffSMatthew G. Knepley 
727014f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
7271e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
727214f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
727392fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
727414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
727514f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
727615b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
7277e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
727814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
727914f150ffSMatthew G. Knepley   }
728014f150ffSMatthew G. Knepley   *odm = dm->dmBC;
728114f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
728214f150ffSMatthew G. Knepley }
7283f4d763aaSMatthew G. Knepley 
7284f4d763aaSMatthew G. Knepley /*@
7285cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7286f4d763aaSMatthew G. Knepley 
7287f4d763aaSMatthew G. Knepley   Input Parameter:
7288f4d763aaSMatthew G. Knepley . dm - The original DM
7289f4d763aaSMatthew G. Knepley 
7290cdb7a50dSMatthew G. Knepley   Output Parameters:
7291cdb7a50dSMatthew G. Knepley + num - The output sequence number
7292cdb7a50dSMatthew G. Knepley - val - The output sequence value
7293f4d763aaSMatthew G. Knepley 
7294f4d763aaSMatthew G. Knepley   Level: intermediate
7295f4d763aaSMatthew G. Knepley 
7296f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7297f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7298f4d763aaSMatthew G. Knepley 
7299f4d763aaSMatthew G. Knepley .seealso: VecView()
7300f4d763aaSMatthew G. Knepley @*/
7301cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7302f4d763aaSMatthew G. Knepley {
7303f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7304f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7305534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7306534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7307f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7308f4d763aaSMatthew G. Knepley }
7309f4d763aaSMatthew G. Knepley 
7310f4d763aaSMatthew G. Knepley /*@
7311cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7312f4d763aaSMatthew G. Knepley 
7313f4d763aaSMatthew G. Knepley   Input Parameters:
7314f4d763aaSMatthew G. Knepley + dm - The original DM
7315cdb7a50dSMatthew G. Knepley . num - The output sequence number
7316cdb7a50dSMatthew G. Knepley - val - The output sequence value
7317f4d763aaSMatthew G. Knepley 
7318f4d763aaSMatthew G. Knepley   Level: intermediate
7319f4d763aaSMatthew G. Knepley 
7320f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7321f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7322f4d763aaSMatthew G. Knepley 
7323f4d763aaSMatthew G. Knepley .seealso: VecView()
7324f4d763aaSMatthew G. Knepley @*/
7325cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7326f4d763aaSMatthew G. Knepley {
7327f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7328f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7329f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7330cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7331cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7332cdb7a50dSMatthew G. Knepley }
7333cdb7a50dSMatthew G. Knepley 
7334cdb7a50dSMatthew G. Knepley /*@C
7335cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7336cdb7a50dSMatthew G. Knepley 
7337cdb7a50dSMatthew G. Knepley   Input Parameters:
7338cdb7a50dSMatthew G. Knepley + dm   - The original DM
7339cdb7a50dSMatthew G. Knepley . name - The sequence name
7340cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7341cdb7a50dSMatthew G. Knepley 
7342cdb7a50dSMatthew G. Knepley   Output Parameter:
7343cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7344cdb7a50dSMatthew G. Knepley 
7345cdb7a50dSMatthew G. Knepley   Level: intermediate
7346cdb7a50dSMatthew G. Knepley 
7347cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7348cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7349cdb7a50dSMatthew G. Knepley 
7350cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7351cdb7a50dSMatthew G. Knepley @*/
7352cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7353cdb7a50dSMatthew G. Knepley {
7354cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7355cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
7356cdb7a50dSMatthew G. Knepley 
7357cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7358cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7359cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7360064a246eSJacob Faibussowitsch   PetscValidRealPointer(val,5);
7361cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
7362cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7363cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7364cdb7a50dSMatthew G. Knepley     PetscScalar value;
7365cdb7a50dSMatthew G. Knepley 
736639d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
73674aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7368cdb7a50dSMatthew G. Knepley #endif
7369cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7370f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7371f4d763aaSMatthew G. Knepley }
73728e4ac7eaSMatthew G. Knepley 
73738e4ac7eaSMatthew G. Knepley /*@
73748e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
73758e4ac7eaSMatthew G. Knepley 
73768e4ac7eaSMatthew G. Knepley   Not collective
73778e4ac7eaSMatthew G. Knepley 
73788e4ac7eaSMatthew G. Knepley   Input Parameter:
73798e4ac7eaSMatthew G. Knepley . dm - The DM
73808e4ac7eaSMatthew G. Knepley 
73818e4ac7eaSMatthew G. Knepley   Output Parameter:
73828e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
73838e4ac7eaSMatthew G. Knepley 
73848e4ac7eaSMatthew G. Knepley   Level: beginner
73858e4ac7eaSMatthew G. Knepley 
73868e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
73878e4ac7eaSMatthew G. Knepley @*/
73888e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
73898e4ac7eaSMatthew G. Knepley {
73908e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
73918e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7392534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
73938e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
73948e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
73958e4ac7eaSMatthew G. Knepley }
73968e4ac7eaSMatthew G. Knepley 
73978e4ac7eaSMatthew G. Knepley /*@
73985d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
73998e4ac7eaSMatthew G. Knepley 
74008e4ac7eaSMatthew G. Knepley   Collective on dm
74018e4ac7eaSMatthew G. Knepley 
74028e4ac7eaSMatthew G. Knepley   Input Parameters:
74038e4ac7eaSMatthew G. Knepley + dm - The DM
74048e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
74058e4ac7eaSMatthew G. Knepley 
74065d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
74075d3b26e6SMatthew G. Knepley 
74088e4ac7eaSMatthew G. Knepley   Level: beginner
74098e4ac7eaSMatthew G. Knepley 
74105d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
74118e4ac7eaSMatthew G. Knepley @*/
74128e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
74138e4ac7eaSMatthew G. Knepley {
74148e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
74158e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74168833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
74178e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
74188e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
74198e4ac7eaSMatthew G. Knepley }
7420c58f1c22SToby Isaac 
7421c58f1c22SToby Isaac /*@C
7422c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7423c58f1c22SToby Isaac 
7424c58f1c22SToby Isaac   Not Collective
7425c58f1c22SToby Isaac 
7426c58f1c22SToby Isaac   Input Parameters:
7427c58f1c22SToby Isaac + dm   - The DM object
7428c58f1c22SToby Isaac - name - The label name
7429c58f1c22SToby Isaac 
7430c58f1c22SToby Isaac   Level: intermediate
7431c58f1c22SToby Isaac 
7432c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7433c58f1c22SToby Isaac @*/
7434c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7435c58f1c22SToby Isaac {
74365d80c0bfSVaclav Hapla   PetscBool      flg;
74375d80c0bfSVaclav Hapla   DMLabel        label;
7438c58f1c22SToby Isaac   PetscErrorCode ierr;
7439c58f1c22SToby Isaac 
7440c58f1c22SToby Isaac   PetscFunctionBegin;
7441c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7442c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
74435d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7444c58f1c22SToby Isaac   if (!flg) {
74455d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
74465d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
74475d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7448c58f1c22SToby Isaac   }
7449c58f1c22SToby Isaac   PetscFunctionReturn(0);
7450c58f1c22SToby Isaac }
7451c58f1c22SToby Isaac 
7452c58f1c22SToby Isaac /*@C
74530fdc7489SMatthew Knepley   DMCreateLabelAtIndex - Create a label of the given name at the iven index. If it already exists, move it to this index.
74540fdc7489SMatthew Knepley 
74550fdc7489SMatthew Knepley   Not Collective
74560fdc7489SMatthew Knepley 
74570fdc7489SMatthew Knepley   Input Parameters:
74580fdc7489SMatthew Knepley + dm   - The DM object
74590fdc7489SMatthew Knepley . l    - The index for the label
74600fdc7489SMatthew Knepley - name - The label name
74610fdc7489SMatthew Knepley 
74620fdc7489SMatthew Knepley   Level: intermediate
74630fdc7489SMatthew Knepley 
74640fdc7489SMatthew Knepley .seealso: DMCreateLabel(), DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
74650fdc7489SMatthew Knepley @*/
74660fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
74670fdc7489SMatthew Knepley {
74680fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
74690fdc7489SMatthew Knepley   DMLabel        label;
74700fdc7489SMatthew Knepley   PetscInt       Nl, m;
74710fdc7489SMatthew Knepley   PetscBool      flg, match;
74720fdc7489SMatthew Knepley   const char    *lname;
74730fdc7489SMatthew Knepley   PetscErrorCode ierr;
74740fdc7489SMatthew Knepley 
74750fdc7489SMatthew Knepley   PetscFunctionBegin;
74760fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7477064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
74780fdc7489SMatthew Knepley   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
74790fdc7489SMatthew Knepley   if (!flg) {
74800fdc7489SMatthew Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
74810fdc7489SMatthew Knepley     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
74820fdc7489SMatthew Knepley     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
74830fdc7489SMatthew Knepley   }
74840fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
74850fdc7489SMatthew Knepley   if (l >= Nl) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %D must be in [0, %D)", l, Nl);
74860fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
74870fdc7489SMatthew Knepley     ierr = PetscObjectGetName((PetscObject) orig->label, &lname);CHKERRQ(ierr);
74880fdc7489SMatthew Knepley     ierr = PetscStrcmp(name, lname, &match);CHKERRQ(ierr);
74890fdc7489SMatthew Knepley     if (match) break;
74900fdc7489SMatthew Knepley   }
74910fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
74920fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
74930fdc7489SMatthew Knepley   else    prev->next = orig->next;
74940fdc7489SMatthew Knepley   if (!l) {
74950fdc7489SMatthew Knepley     orig->next = dm->labels;
74960fdc7489SMatthew Knepley     dm->labels = orig;
74970fdc7489SMatthew Knepley   } else {
74980fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
74990fdc7489SMatthew Knepley     orig->next = prev->next;
75000fdc7489SMatthew Knepley     prev->next = orig;
75010fdc7489SMatthew Knepley   }
75020fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75030fdc7489SMatthew Knepley }
75040fdc7489SMatthew Knepley 
75050fdc7489SMatthew Knepley /*@C
7506c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7507c58f1c22SToby Isaac 
7508c58f1c22SToby Isaac   Not Collective
7509c58f1c22SToby Isaac 
7510c58f1c22SToby Isaac   Input Parameters:
7511c58f1c22SToby Isaac + dm   - The DM object
7512c58f1c22SToby Isaac . name - The label name
7513c58f1c22SToby Isaac - point - The mesh point
7514c58f1c22SToby Isaac 
7515c58f1c22SToby Isaac   Output Parameter:
7516c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7517c58f1c22SToby Isaac 
7518c58f1c22SToby Isaac   Level: beginner
7519c58f1c22SToby Isaac 
7520c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7521c58f1c22SToby Isaac @*/
7522c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7523c58f1c22SToby Isaac {
7524c58f1c22SToby Isaac   DMLabel        label;
7525c58f1c22SToby Isaac   PetscErrorCode ierr;
7526c58f1c22SToby Isaac 
7527c58f1c22SToby Isaac   PetscFunctionBegin;
7528c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7529c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7530c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
753113903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7532c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7533c58f1c22SToby Isaac   PetscFunctionReturn(0);
7534c58f1c22SToby Isaac }
7535c58f1c22SToby Isaac 
7536c58f1c22SToby Isaac /*@C
7537c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7538c58f1c22SToby Isaac 
7539c58f1c22SToby Isaac   Not Collective
7540c58f1c22SToby Isaac 
7541c58f1c22SToby Isaac   Input Parameters:
7542c58f1c22SToby Isaac + dm   - The DM object
7543c58f1c22SToby Isaac . name - The label name
7544c58f1c22SToby Isaac . point - The mesh point
7545c58f1c22SToby Isaac - value - The label value for this point
7546c58f1c22SToby Isaac 
7547c58f1c22SToby Isaac   Output Parameter:
7548c58f1c22SToby Isaac 
7549c58f1c22SToby Isaac   Level: beginner
7550c58f1c22SToby Isaac 
7551c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7552c58f1c22SToby Isaac @*/
7553c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7554c58f1c22SToby Isaac {
7555c58f1c22SToby Isaac   DMLabel        label;
7556c58f1c22SToby Isaac   PetscErrorCode ierr;
7557c58f1c22SToby Isaac 
7558c58f1c22SToby Isaac   PetscFunctionBegin;
7559c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7560c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7561c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7562c58f1c22SToby Isaac   if (!label) {
7563c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7564c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7565c58f1c22SToby Isaac   }
7566c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7567c58f1c22SToby Isaac   PetscFunctionReturn(0);
7568c58f1c22SToby Isaac }
7569c58f1c22SToby Isaac 
7570c58f1c22SToby Isaac /*@C
7571c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7572c58f1c22SToby Isaac 
7573c58f1c22SToby Isaac   Not Collective
7574c58f1c22SToby Isaac 
7575c58f1c22SToby Isaac   Input Parameters:
7576c58f1c22SToby Isaac + dm   - The DM object
7577c58f1c22SToby Isaac . name - The label name
7578c58f1c22SToby Isaac . point - The mesh point
7579c58f1c22SToby Isaac - value - The label value for this point
7580c58f1c22SToby Isaac 
7581c58f1c22SToby Isaac   Output Parameter:
7582c58f1c22SToby Isaac 
7583c58f1c22SToby Isaac   Level: beginner
7584c58f1c22SToby Isaac 
7585c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7586c58f1c22SToby Isaac @*/
7587c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7588c58f1c22SToby Isaac {
7589c58f1c22SToby Isaac   DMLabel        label;
7590c58f1c22SToby Isaac   PetscErrorCode ierr;
7591c58f1c22SToby Isaac 
7592c58f1c22SToby Isaac   PetscFunctionBegin;
7593c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7594c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7595c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7596c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7597c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7598c58f1c22SToby Isaac   PetscFunctionReturn(0);
7599c58f1c22SToby Isaac }
7600c58f1c22SToby Isaac 
7601c58f1c22SToby Isaac /*@C
7602c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7603c58f1c22SToby Isaac 
7604c58f1c22SToby Isaac   Not Collective
7605c58f1c22SToby Isaac 
7606c58f1c22SToby Isaac   Input Parameters:
7607c58f1c22SToby Isaac + dm   - The DM object
7608c58f1c22SToby Isaac - name - The label name
7609c58f1c22SToby Isaac 
7610c58f1c22SToby Isaac   Output Parameter:
7611c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7612c58f1c22SToby Isaac 
7613c58f1c22SToby Isaac   Level: beginner
7614c58f1c22SToby Isaac 
7615df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7616c58f1c22SToby Isaac @*/
7617c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7618c58f1c22SToby Isaac {
7619c58f1c22SToby Isaac   DMLabel        label;
7620c58f1c22SToby Isaac   PetscErrorCode ierr;
7621c58f1c22SToby Isaac 
7622c58f1c22SToby Isaac   PetscFunctionBegin;
7623c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7624c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7625534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7626c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7627c58f1c22SToby Isaac   *size = 0;
7628c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7629c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7630c58f1c22SToby Isaac   PetscFunctionReturn(0);
7631c58f1c22SToby Isaac }
7632c58f1c22SToby Isaac 
7633c58f1c22SToby Isaac /*@C
7634c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7635c58f1c22SToby Isaac 
7636c58f1c22SToby Isaac   Not Collective
7637c58f1c22SToby Isaac 
7638c58f1c22SToby Isaac   Input Parameters:
7639c58f1c22SToby Isaac + mesh - The DM object
7640c58f1c22SToby Isaac - name - The label name
7641c58f1c22SToby Isaac 
7642c58f1c22SToby Isaac   Output Parameter:
7643c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7644c58f1c22SToby Isaac 
7645c58f1c22SToby Isaac   Level: beginner
7646c58f1c22SToby Isaac 
7647c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7648c58f1c22SToby Isaac @*/
7649c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7650c58f1c22SToby Isaac {
7651c58f1c22SToby Isaac   DMLabel        label;
7652c58f1c22SToby Isaac   PetscErrorCode ierr;
7653c58f1c22SToby Isaac 
7654c58f1c22SToby Isaac   PetscFunctionBegin;
7655c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7656c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7657c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7658c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7659c58f1c22SToby Isaac   *ids = NULL;
7660dab2e251SBlaise Bourdin  if (label) {
7661c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7662dab2e251SBlaise Bourdin   } else {
7663dab2e251SBlaise Bourdin     /* returning an empty IS */
7664dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7665dab2e251SBlaise Bourdin   }
7666c58f1c22SToby Isaac   PetscFunctionReturn(0);
7667c58f1c22SToby Isaac }
7668c58f1c22SToby Isaac 
7669c58f1c22SToby Isaac /*@C
7670c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7671c58f1c22SToby Isaac 
7672c58f1c22SToby Isaac   Not Collective
7673c58f1c22SToby Isaac 
7674c58f1c22SToby Isaac   Input Parameters:
7675c58f1c22SToby Isaac + dm - The DM object
7676c58f1c22SToby Isaac . name - The label name
7677c58f1c22SToby Isaac - value - The stratum value
7678c58f1c22SToby Isaac 
7679c58f1c22SToby Isaac   Output Parameter:
7680c58f1c22SToby Isaac . size - The stratum size
7681c58f1c22SToby Isaac 
7682c58f1c22SToby Isaac   Level: beginner
7683c58f1c22SToby Isaac 
7684c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7685c58f1c22SToby Isaac @*/
7686c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7687c58f1c22SToby Isaac {
7688c58f1c22SToby Isaac   DMLabel        label;
7689c58f1c22SToby Isaac   PetscErrorCode ierr;
7690c58f1c22SToby Isaac 
7691c58f1c22SToby Isaac   PetscFunctionBegin;
7692c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7693c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7694534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7695c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7696c58f1c22SToby Isaac   *size = 0;
7697c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7698c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7699c58f1c22SToby Isaac   PetscFunctionReturn(0);
7700c58f1c22SToby Isaac }
7701c58f1c22SToby Isaac 
7702c58f1c22SToby Isaac /*@C
7703c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7704c58f1c22SToby Isaac 
7705c58f1c22SToby Isaac   Not Collective
7706c58f1c22SToby Isaac 
7707c58f1c22SToby Isaac   Input Parameters:
7708c58f1c22SToby Isaac + dm - The DM object
7709c58f1c22SToby Isaac . name - The label name
7710c58f1c22SToby Isaac - value - The stratum value
7711c58f1c22SToby Isaac 
7712c58f1c22SToby Isaac   Output Parameter:
7713c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7714c58f1c22SToby Isaac 
7715c58f1c22SToby Isaac   Level: beginner
7716c58f1c22SToby Isaac 
7717c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7718c58f1c22SToby Isaac @*/
7719c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7720c58f1c22SToby Isaac {
7721c58f1c22SToby Isaac   DMLabel        label;
7722c58f1c22SToby Isaac   PetscErrorCode ierr;
7723c58f1c22SToby Isaac 
7724c58f1c22SToby Isaac   PetscFunctionBegin;
7725c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7726c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7727c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7728c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7729c58f1c22SToby Isaac   *points = NULL;
7730c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7731c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7732c58f1c22SToby Isaac   PetscFunctionReturn(0);
7733c58f1c22SToby Isaac }
7734c58f1c22SToby Isaac 
77354de306b1SToby Isaac /*@C
77369044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
77374de306b1SToby Isaac 
77384de306b1SToby Isaac   Not Collective
77394de306b1SToby Isaac 
77404de306b1SToby Isaac   Input Parameters:
77414de306b1SToby Isaac + dm - The DM object
77424de306b1SToby Isaac . name - The label name
77434de306b1SToby Isaac . value - The stratum value
77444de306b1SToby Isaac - points - The stratum points
77454de306b1SToby Isaac 
77464de306b1SToby Isaac   Level: beginner
77474de306b1SToby Isaac 
77484de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
77494de306b1SToby Isaac @*/
77504de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
77514de306b1SToby Isaac {
77524de306b1SToby Isaac   DMLabel        label;
77534de306b1SToby Isaac   PetscErrorCode ierr;
77544de306b1SToby Isaac 
77554de306b1SToby Isaac   PetscFunctionBegin;
77564de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77574de306b1SToby Isaac   PetscValidCharPointer(name, 2);
77584de306b1SToby Isaac   PetscValidPointer(points, 4);
77594de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
77604de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
77614de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
77624de306b1SToby Isaac   PetscFunctionReturn(0);
77634de306b1SToby Isaac }
77644de306b1SToby Isaac 
7765c58f1c22SToby Isaac /*@C
7766c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7767c58f1c22SToby Isaac 
7768c58f1c22SToby Isaac   Not Collective
7769c58f1c22SToby Isaac 
7770c58f1c22SToby Isaac   Input Parameters:
7771c58f1c22SToby Isaac + dm   - The DM object
7772c58f1c22SToby Isaac . name - The label name
7773c58f1c22SToby Isaac - value - The label value for this point
7774c58f1c22SToby Isaac 
7775c58f1c22SToby Isaac   Output Parameter:
7776c58f1c22SToby Isaac 
7777c58f1c22SToby Isaac   Level: beginner
7778c58f1c22SToby Isaac 
7779c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7780c58f1c22SToby Isaac @*/
7781c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7782c58f1c22SToby Isaac {
7783c58f1c22SToby Isaac   DMLabel        label;
7784c58f1c22SToby Isaac   PetscErrorCode ierr;
7785c58f1c22SToby Isaac 
7786c58f1c22SToby Isaac   PetscFunctionBegin;
7787c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7788c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7789c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7790c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7791c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7792c58f1c22SToby Isaac   PetscFunctionReturn(0);
7793c58f1c22SToby Isaac }
7794c58f1c22SToby Isaac 
7795c58f1c22SToby Isaac /*@
7796c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7797c58f1c22SToby Isaac 
7798c58f1c22SToby Isaac   Not Collective
7799c58f1c22SToby Isaac 
7800c58f1c22SToby Isaac   Input Parameter:
7801c58f1c22SToby Isaac . dm   - The DM object
7802c58f1c22SToby Isaac 
7803c58f1c22SToby Isaac   Output Parameter:
7804c58f1c22SToby Isaac . numLabels - the number of Labels
7805c58f1c22SToby Isaac 
7806c58f1c22SToby Isaac   Level: intermediate
7807c58f1c22SToby Isaac 
7808c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7809c58f1c22SToby Isaac @*/
7810c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7811c58f1c22SToby Isaac {
78125d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7813c58f1c22SToby Isaac   PetscInt  n    = 0;
7814c58f1c22SToby Isaac 
7815c58f1c22SToby Isaac   PetscFunctionBegin;
7816c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7817534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7818c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7819c58f1c22SToby Isaac   *numLabels = n;
7820c58f1c22SToby Isaac   PetscFunctionReturn(0);
7821c58f1c22SToby Isaac }
7822c58f1c22SToby Isaac 
7823c58f1c22SToby Isaac /*@C
7824c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7825c58f1c22SToby Isaac 
7826c58f1c22SToby Isaac   Not Collective
7827c58f1c22SToby Isaac 
7828c58f1c22SToby Isaac   Input Parameters:
7829c58f1c22SToby Isaac + dm - The DM object
7830c58f1c22SToby Isaac - n  - the label number
7831c58f1c22SToby Isaac 
7832c58f1c22SToby Isaac   Output Parameter:
7833c58f1c22SToby Isaac . name - the label name
7834c58f1c22SToby Isaac 
7835c58f1c22SToby Isaac   Level: intermediate
7836c58f1c22SToby Isaac 
7837c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7838c58f1c22SToby Isaac @*/
7839c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7840c58f1c22SToby Isaac {
78415d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7842c58f1c22SToby Isaac   PetscInt       l    = 0;
7843d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7844c58f1c22SToby Isaac 
7845c58f1c22SToby Isaac   PetscFunctionBegin;
7846c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7847c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7848c58f1c22SToby Isaac   while (next) {
7849c58f1c22SToby Isaac     if (l == n) {
7850d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7851c58f1c22SToby Isaac       PetscFunctionReturn(0);
7852c58f1c22SToby Isaac     }
7853c58f1c22SToby Isaac     ++l;
7854c58f1c22SToby Isaac     next = next->next;
7855c58f1c22SToby Isaac   }
7856c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7857c58f1c22SToby Isaac }
7858c58f1c22SToby Isaac 
7859c58f1c22SToby Isaac /*@C
7860c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7861c58f1c22SToby Isaac 
7862c58f1c22SToby Isaac   Not Collective
7863c58f1c22SToby Isaac 
7864c58f1c22SToby Isaac   Input Parameters:
7865c58f1c22SToby Isaac + dm   - The DM object
7866c58f1c22SToby Isaac - name - The label name
7867c58f1c22SToby Isaac 
7868c58f1c22SToby Isaac   Output Parameter:
7869c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7870c58f1c22SToby Isaac 
7871c58f1c22SToby Isaac   Level: intermediate
7872c58f1c22SToby Isaac 
7873c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7874c58f1c22SToby Isaac @*/
7875c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7876c58f1c22SToby Isaac {
78775d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7878d67d17b1SMatthew G. Knepley   const char    *lname;
7879c58f1c22SToby Isaac   PetscErrorCode ierr;
7880c58f1c22SToby Isaac 
7881c58f1c22SToby Isaac   PetscFunctionBegin;
7882c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7883c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7884534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7885c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7886c58f1c22SToby Isaac   while (next) {
7887d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7888d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7889c58f1c22SToby Isaac     if (*hasLabel) break;
7890c58f1c22SToby Isaac     next = next->next;
7891c58f1c22SToby Isaac   }
7892c58f1c22SToby Isaac   PetscFunctionReturn(0);
7893c58f1c22SToby Isaac }
7894c58f1c22SToby Isaac 
7895c58f1c22SToby Isaac /*@C
7896c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7897c58f1c22SToby Isaac 
7898c58f1c22SToby Isaac   Not Collective
7899c58f1c22SToby Isaac 
7900c58f1c22SToby Isaac   Input Parameters:
7901c58f1c22SToby Isaac + dm   - The DM object
7902c58f1c22SToby Isaac - name - The label name
7903c58f1c22SToby Isaac 
7904c58f1c22SToby Isaac   Output Parameter:
7905c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7906c58f1c22SToby Isaac 
79076d7c9049SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
79086d7c9049SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
79096d7c9049SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
79106d7c9049SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
79116d7c9049SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
79126d7c9049SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
79136d7c9049SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
79146d7c9049SMatthew G. Knepley 
7915c58f1c22SToby Isaac   Level: intermediate
7916c58f1c22SToby Isaac 
79176d7c9049SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
7918c58f1c22SToby Isaac @*/
7919c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7920c58f1c22SToby Isaac {
79215d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7922c58f1c22SToby Isaac   PetscBool      hasLabel;
7923d67d17b1SMatthew G. Knepley   const char    *lname;
7924c58f1c22SToby Isaac   PetscErrorCode ierr;
7925c58f1c22SToby Isaac 
7926c58f1c22SToby Isaac   PetscFunctionBegin;
7927c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7928c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7929c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7930c58f1c22SToby Isaac   *label = NULL;
7931c58f1c22SToby Isaac   while (next) {
7932d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7933d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7934c58f1c22SToby Isaac     if (hasLabel) {
7935c58f1c22SToby Isaac       *label = next->label;
7936c58f1c22SToby Isaac       break;
7937c58f1c22SToby Isaac     }
7938c58f1c22SToby Isaac     next = next->next;
7939c58f1c22SToby Isaac   }
7940c58f1c22SToby Isaac   PetscFunctionReturn(0);
7941c58f1c22SToby Isaac }
7942c58f1c22SToby Isaac 
7943c58f1c22SToby Isaac /*@C
7944c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7945c58f1c22SToby Isaac 
7946c58f1c22SToby Isaac   Not Collective
7947c58f1c22SToby Isaac 
7948c58f1c22SToby Isaac   Input Parameters:
7949c58f1c22SToby Isaac + dm - The DM object
7950c58f1c22SToby Isaac - n  - the label number
7951c58f1c22SToby Isaac 
7952c58f1c22SToby Isaac   Output Parameter:
7953c58f1c22SToby Isaac . label - the label
7954c58f1c22SToby Isaac 
7955c58f1c22SToby Isaac   Level: intermediate
7956c58f1c22SToby Isaac 
7957c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7958c58f1c22SToby Isaac @*/
7959c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7960c58f1c22SToby Isaac {
79615d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7962c58f1c22SToby Isaac   PetscInt    l    = 0;
7963c58f1c22SToby Isaac 
7964c58f1c22SToby Isaac   PetscFunctionBegin;
7965c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7966c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7967c58f1c22SToby Isaac   while (next) {
7968c58f1c22SToby Isaac     if (l == n) {
7969c58f1c22SToby Isaac       *label = next->label;
7970c58f1c22SToby Isaac       PetscFunctionReturn(0);
7971c58f1c22SToby Isaac     }
7972c58f1c22SToby Isaac     ++l;
7973c58f1c22SToby Isaac     next = next->next;
7974c58f1c22SToby Isaac   }
7975c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7976c58f1c22SToby Isaac }
7977c58f1c22SToby Isaac 
7978c58f1c22SToby Isaac /*@C
7979c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7980c58f1c22SToby Isaac 
7981c58f1c22SToby Isaac   Not Collective
7982c58f1c22SToby Isaac 
7983c58f1c22SToby Isaac   Input Parameters:
7984c58f1c22SToby Isaac + dm   - The DM object
7985c58f1c22SToby Isaac - label - The DMLabel
7986c58f1c22SToby Isaac 
7987c58f1c22SToby Isaac   Level: developer
7988c58f1c22SToby Isaac 
7989c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7990c58f1c22SToby Isaac @*/
7991c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7992c58f1c22SToby Isaac {
79935d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7994c58f1c22SToby Isaac   PetscBool      hasLabel;
7995d67d17b1SMatthew G. Knepley   const char    *lname;
79965d80c0bfSVaclav Hapla   PetscBool      flg;
7997c58f1c22SToby Isaac   PetscErrorCode ierr;
7998c58f1c22SToby Isaac 
7999c58f1c22SToby Isaac   PetscFunctionBegin;
8000c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8001d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
8002d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
8003d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
8004c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
8005c58f1c22SToby Isaac   tmpLabel->label  = label;
8006c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
80075d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
80085d80c0bfSVaclav Hapla   *p = tmpLabel;
800908f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
80105d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
80115d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
8012ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
8013ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
8014c58f1c22SToby Isaac   PetscFunctionReturn(0);
8015c58f1c22SToby Isaac }
8016c58f1c22SToby Isaac 
8017c58f1c22SToby Isaac /*@C
80184a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
80194a7ee7d0SMatthew G. Knepley 
80204a7ee7d0SMatthew G. Knepley   Not Collective
80214a7ee7d0SMatthew G. Knepley 
80224a7ee7d0SMatthew G. Knepley   Input Parameters:
80234a7ee7d0SMatthew G. Knepley + dm    - The DM object
80244a7ee7d0SMatthew G. Knepley - label - The DMLabel, having the same name, to substitute
80254a7ee7d0SMatthew G. Knepley 
80264a7ee7d0SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
80274a7ee7d0SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
80284a7ee7d0SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
80294a7ee7d0SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
80304a7ee7d0SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
80314a7ee7d0SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
80324a7ee7d0SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
80334a7ee7d0SMatthew G. Knepley 
80344a7ee7d0SMatthew G. Knepley   Level: intermediate
80354a7ee7d0SMatthew G. Knepley 
80364a7ee7d0SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
80374a7ee7d0SMatthew G. Knepley @*/
80384a7ee7d0SMatthew G. Knepley PetscErrorCode DMSetLabel(DM dm, DMLabel label)
80394a7ee7d0SMatthew G. Knepley {
80404a7ee7d0SMatthew G. Knepley   DMLabelLink    next = dm->labels;
80414a7ee7d0SMatthew G. Knepley   PetscBool      hasLabel, flg;
80424a7ee7d0SMatthew G. Knepley   const char    *name, *lname;
80434a7ee7d0SMatthew G. Knepley   PetscErrorCode ierr;
80444a7ee7d0SMatthew G. Knepley 
80454a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
80464a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80474a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
80484a7ee7d0SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &name);CHKERRQ(ierr);
80494a7ee7d0SMatthew G. Knepley   while (next) {
80504a7ee7d0SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
80514a7ee7d0SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
80524a7ee7d0SMatthew G. Knepley     if (hasLabel) {
80534a7ee7d0SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
80544a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
80554a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
80564a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
80574a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
80584a7ee7d0SMatthew G. Knepley       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
80594a7ee7d0SMatthew G. Knepley       next->label = label;
80604a7ee7d0SMatthew G. Knepley       break;
80614a7ee7d0SMatthew G. Knepley     }
80624a7ee7d0SMatthew G. Knepley     next = next->next;
80634a7ee7d0SMatthew G. Knepley   }
80644a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
80654a7ee7d0SMatthew G. Knepley }
80664a7ee7d0SMatthew G. Knepley 
80674a7ee7d0SMatthew G. Knepley /*@C
8068e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
8069c58f1c22SToby Isaac 
8070c58f1c22SToby Isaac   Not Collective
8071c58f1c22SToby Isaac 
8072c58f1c22SToby Isaac   Input Parameters:
8073c58f1c22SToby Isaac + dm   - The DM object
8074c58f1c22SToby Isaac - name - The label name
8075c58f1c22SToby Isaac 
8076c58f1c22SToby Isaac   Output Parameter:
8077c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
8078c58f1c22SToby Isaac 
8079c58f1c22SToby Isaac   Level: developer
8080c58f1c22SToby Isaac 
8081e5472504SVaclav Hapla   Notes:
8082e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
8083e5472504SVaclav Hapla   DMLabelDestroy() on the label.
8084e5472504SVaclav Hapla 
8085e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
8086e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
8087e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
8088e5472504SVaclav Hapla 
8089e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
8090c58f1c22SToby Isaac @*/
8091c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
8092c58f1c22SToby Isaac {
809395d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
8094c58f1c22SToby Isaac   PetscBool      hasLabel;
8095d67d17b1SMatthew G. Knepley   const char    *lname;
8096c58f1c22SToby Isaac   PetscErrorCode ierr;
8097c58f1c22SToby Isaac 
8098c58f1c22SToby Isaac   PetscFunctionBegin;
8099c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8100e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
8101e5472504SVaclav Hapla   if (label) {
8102e5472504SVaclav Hapla     PetscValidPointer(label, 3);
8103c58f1c22SToby Isaac     *label = NULL;
8104e5472504SVaclav Hapla   }
81055d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
810695d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
8107d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
8108c58f1c22SToby Isaac     if (hasLabel) {
810995d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
8110c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
811195d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
8112ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
8113ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
811495d578d6SVaclav Hapla       if (label) *label = link->label;
811595d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
811695d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8117c58f1c22SToby Isaac       break;
8118c58f1c22SToby Isaac     }
8119c58f1c22SToby Isaac   }
8120c58f1c22SToby Isaac   PetscFunctionReturn(0);
8121c58f1c22SToby Isaac }
8122c58f1c22SToby Isaac 
8123306894acSVaclav Hapla /*@
8124306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
8125306894acSVaclav Hapla 
8126306894acSVaclav Hapla   Not Collective
8127306894acSVaclav Hapla 
8128306894acSVaclav Hapla   Input Parameters:
8129306894acSVaclav Hapla + dm   - The DM object
8130876aa926SVaclav Hapla . label - The DMLabel to be removed from the DM
8131306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
8132306894acSVaclav Hapla 
8133306894acSVaclav Hapla   Level: developer
8134306894acSVaclav Hapla 
8135306894acSVaclav Hapla   Notes:
8136306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
8137306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
8138306894acSVaclav Hapla   *label nullified.
8139306894acSVaclav Hapla 
8140306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
8141306894acSVaclav Hapla @*/
8142306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
8143306894acSVaclav Hapla {
814443e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
8145306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
8146306894acSVaclav Hapla   PetscErrorCode ierr;
8147306894acSVaclav Hapla 
8148306894acSVaclav Hapla   PetscFunctionBegin;
8149306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8150306894acSVaclav Hapla   PetscValidPointer(label, 2);
8151f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
8152306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
8153306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
81545d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
815543e45a93SVaclav Hapla     if (*label == link->label) {
8156306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
815743e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
8158306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
8159ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
816043e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
816143e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
816243e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8163306894acSVaclav Hapla       break;
8164306894acSVaclav Hapla     }
8165306894acSVaclav Hapla   }
8166306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
8167306894acSVaclav Hapla   PetscFunctionReturn(0);
8168306894acSVaclav Hapla }
8169306894acSVaclav Hapla 
8170c58f1c22SToby Isaac /*@C
8171c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
8172c58f1c22SToby Isaac 
8173c58f1c22SToby Isaac   Not Collective
8174c58f1c22SToby Isaac 
8175c58f1c22SToby Isaac   Input Parameters:
8176c58f1c22SToby Isaac + dm   - The DM object
8177c58f1c22SToby Isaac - name - The label name
8178c58f1c22SToby Isaac 
8179c58f1c22SToby Isaac   Output Parameter:
8180c58f1c22SToby Isaac . output - The flag for output
8181c58f1c22SToby Isaac 
8182c58f1c22SToby Isaac   Level: developer
8183c58f1c22SToby Isaac 
8184c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8185c58f1c22SToby Isaac @*/
8186c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
8187c58f1c22SToby Isaac {
81885d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8189d67d17b1SMatthew G. Knepley   const char    *lname;
8190c58f1c22SToby Isaac   PetscErrorCode ierr;
8191c58f1c22SToby Isaac 
8192c58f1c22SToby Isaac   PetscFunctionBegin;
8193c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8194c58f1c22SToby Isaac   PetscValidPointer(name, 2);
8195c58f1c22SToby Isaac   PetscValidPointer(output, 3);
8196c58f1c22SToby Isaac   while (next) {
8197c58f1c22SToby Isaac     PetscBool flg;
8198c58f1c22SToby Isaac 
8199d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8200d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8201c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
8202c58f1c22SToby Isaac     next = next->next;
8203c58f1c22SToby Isaac   }
8204c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8205c58f1c22SToby Isaac }
8206c58f1c22SToby Isaac 
8207c58f1c22SToby Isaac /*@C
8208c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
8209c58f1c22SToby Isaac 
8210c58f1c22SToby Isaac   Not Collective
8211c58f1c22SToby Isaac 
8212c58f1c22SToby Isaac   Input Parameters:
8213c58f1c22SToby Isaac + dm     - The DM object
8214c58f1c22SToby Isaac . name   - The label name
8215c58f1c22SToby Isaac - output - The flag for output
8216c58f1c22SToby Isaac 
8217c58f1c22SToby Isaac   Level: developer
8218c58f1c22SToby Isaac 
8219c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8220c58f1c22SToby Isaac @*/
8221c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
8222c58f1c22SToby Isaac {
82235d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8224d67d17b1SMatthew G. Knepley   const char    *lname;
8225c58f1c22SToby Isaac   PetscErrorCode ierr;
8226c58f1c22SToby Isaac 
8227c58f1c22SToby Isaac   PetscFunctionBegin;
8228c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8229534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
8230c58f1c22SToby Isaac   while (next) {
8231c58f1c22SToby Isaac     PetscBool flg;
8232c58f1c22SToby Isaac 
8233d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8234d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8235c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
8236c58f1c22SToby Isaac     next = next->next;
8237c58f1c22SToby Isaac   }
8238c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8239c58f1c22SToby Isaac }
8240c58f1c22SToby Isaac 
8241c58f1c22SToby Isaac /*@
8242c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
8243c58f1c22SToby Isaac 
8244d083f849SBarry Smith   Collective on dmA
8245c58f1c22SToby Isaac 
8246d8d19677SJose E. Roman   Input Parameters:
82475d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
82482cbb9b06SVaclav Hapla . dmB - The DM object to which labels are copied
82495d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
82502cbb9b06SVaclav Hapla . all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
82512cbb9b06SVaclav Hapla - emode - How to behave when a DMLabel in the source and destination DMs with the same name is encountered (see DMCopyLabelsMode)
8252c58f1c22SToby Isaac 
8253c58f1c22SToby Isaac   Level: intermediate
8254c58f1c22SToby Isaac 
82552cbb9b06SVaclav Hapla   Notes:
82562cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
8257c58f1c22SToby Isaac 
82582cbb9b06SVaclav Hapla .seealso: DMAddLabel(), DMCopyLabelsMode
8259c58f1c22SToby Isaac @*/
82602cbb9b06SVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
8261c58f1c22SToby Isaac {
82622cbb9b06SVaclav Hapla   DMLabel        label, labelNew, labelOld;
8263c58f1c22SToby Isaac   const char    *name;
8264c58f1c22SToby Isaac   PetscBool      flg;
82655d80c0bfSVaclav Hapla   DMLabelLink    link;
82665d80c0bfSVaclav Hapla   PetscErrorCode ierr;
8267c58f1c22SToby Isaac 
82685d80c0bfSVaclav Hapla   PetscFunctionBegin;
82695d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
82705d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
82715d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
82725d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
82735d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
82745d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
82755d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
82765d80c0bfSVaclav Hapla     label=link->label;
82775d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
82785d80c0bfSVaclav Hapla     if (!all) {
8279c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
8280c58f1c22SToby Isaac       if (flg) continue;
82817d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
82827d5acc75SStefano Zampini       if (flg) continue;
8283ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
8284ba2698f1SMatthew G. Knepley       if (flg) continue;
82855d80c0bfSVaclav Hapla     }
82862cbb9b06SVaclav Hapla     ierr = DMGetLabel(dmB, name, &labelOld);CHKERRQ(ierr);
82872cbb9b06SVaclav Hapla     if (labelOld) {
82882cbb9b06SVaclav Hapla       switch (emode) {
82892cbb9b06SVaclav Hapla         case DM_COPY_LABELS_KEEP:
82902cbb9b06SVaclav Hapla           continue;
82912cbb9b06SVaclav Hapla         case DM_COPY_LABELS_REPLACE:
82922cbb9b06SVaclav Hapla           ierr = DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE);CHKERRQ(ierr);
82932cbb9b06SVaclav Hapla           break;
82942cbb9b06SVaclav Hapla         case DM_COPY_LABELS_FAIL:
82952cbb9b06SVaclav Hapla           SETERRQ1(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
82962cbb9b06SVaclav Hapla         default:
82972cbb9b06SVaclav Hapla           SETERRQ1(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", emode);
82982cbb9b06SVaclav Hapla       }
82992cbb9b06SVaclav Hapla     }
83005d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
8301c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
83025d80c0bfSVaclav Hapla     } else {
83035d80c0bfSVaclav Hapla       labelNew = label;
83045d80c0bfSVaclav Hapla     }
8305c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
83065d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
8307c58f1c22SToby Isaac   }
8308c58f1c22SToby Isaac   PetscFunctionReturn(0);
8309c58f1c22SToby Isaac }
8310461a15a0SLisandro Dalcin 
8311609dae6eSVaclav Hapla /*@C
8312609dae6eSVaclav Hapla   DMCompareLabels - Compare labels of two DMPlex meshes
8313609dae6eSVaclav Hapla 
8314609dae6eSVaclav Hapla   Collective on dmA
8315609dae6eSVaclav Hapla 
8316609dae6eSVaclav Hapla   Input Parameters:
8317609dae6eSVaclav Hapla + dm0 - First DM object
8318609dae6eSVaclav Hapla - dm1 - Second DM object
8319609dae6eSVaclav Hapla 
8320609dae6eSVaclav Hapla   Output Parameters
8321609dae6eSVaclav Hapla + same    - (Optional) Flag whether labels of dm0 and dm1 are the same
8322609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
8323609dae6eSVaclav Hapla 
8324609dae6eSVaclav Hapla   Level: intermediate
8325609dae6eSVaclav Hapla 
8326609dae6eSVaclav Hapla   Notes:
8327609dae6eSVaclav Hapla   If both same and message is passed as NULL, and difference is found, an error is thrown with a message describing the difference.
8328609dae6eSVaclav Hapla 
8329609dae6eSVaclav Hapla   Message must be freed by user.
8330609dae6eSVaclav Hapla 
8331609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
8332609dae6eSVaclav Hapla   DMLabelCompare() is used to compare each pair of labels with the same name.
8333609dae6eSVaclav Hapla 
8334609dae6eSVaclav Hapla   Fortran Notes:
8335609dae6eSVaclav Hapla   This function is currently not available from Fortran.
8336609dae6eSVaclav Hapla 
8337609dae6eSVaclav Hapla .seealso: DMAddLabel(), DMCopyLabelsMode, DMLabelCompare()
8338609dae6eSVaclav Hapla @*/
8339609dae6eSVaclav Hapla PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *same, char **message)
8340609dae6eSVaclav Hapla {
8341609dae6eSVaclav Hapla   PetscInt        n0, n1, i;
8342609dae6eSVaclav Hapla   char            msg[PETSC_MAX_PATH_LEN] = "";
8343609dae6eSVaclav Hapla   MPI_Comm        comm;
8344609dae6eSVaclav Hapla   PetscErrorCode  ierr;
8345609dae6eSVaclav Hapla 
8346609dae6eSVaclav Hapla   PetscFunctionBegin;
8347609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0,DM_CLASSID,1);
8348609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1,DM_CLASSID,2);
8349609dae6eSVaclav Hapla   PetscCheckSameComm(dm0,1,dm1,2);
8350609dae6eSVaclav Hapla   if (same) PetscValidBoolPointer(same,3);
8351609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
8352609dae6eSVaclav Hapla   ierr = PetscObjectGetComm((PetscObject)dm0, &comm);CHKERRQ(ierr);
8353609dae6eSVaclav Hapla   ierr = DMGetNumLabels(dm0, &n0);CHKERRQ(ierr);
8354609dae6eSVaclav Hapla   ierr = DMGetNumLabels(dm1, &n1);CHKERRQ(ierr);
8355609dae6eSVaclav Hapla   if (n0 != n1) {
8356609dae6eSVaclav Hapla     ierr = PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %D != %D = Number of labels in dm1", n0, n1);CHKERRQ(ierr);
8357609dae6eSVaclav Hapla     goto finish;
8358609dae6eSVaclav Hapla   }
8359609dae6eSVaclav Hapla   for (i=0; i<n0; i++) {
8360609dae6eSVaclav Hapla     DMLabel     l0, l1;
8361609dae6eSVaclav Hapla     const char *name;
8362609dae6eSVaclav Hapla     char       *msgInner;
8363609dae6eSVaclav Hapla 
8364609dae6eSVaclav Hapla     /* Ignore label order */
8365609dae6eSVaclav Hapla     ierr = DMGetLabelByNum(dm0, i, &l0);CHKERRQ(ierr);
8366609dae6eSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)l0, &name);CHKERRQ(ierr);
8367609dae6eSVaclav Hapla     ierr = DMGetLabel(dm1, name, &l1);CHKERRQ(ierr);
8368609dae6eSVaclav Hapla     if (!l1) {
8369609dae6eSVaclav Hapla       ierr = PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%D in dm0) not found in dm1", name, i);CHKERRQ(ierr);
8370609dae6eSVaclav Hapla       goto finish;
8371609dae6eSVaclav Hapla     }
8372609dae6eSVaclav Hapla     ierr = DMLabelCompare(l0, l1, NULL, &msgInner);CHKERRQ(ierr);
8373609dae6eSVaclav Hapla     ierr = PetscStrncpy(msg, msgInner, sizeof(msg));CHKERRQ(ierr);
8374609dae6eSVaclav Hapla     ierr = PetscFree(msgInner);CHKERRQ(ierr);
8375609dae6eSVaclav Hapla     if (msg[0]) goto finish;
8376609dae6eSVaclav Hapla   }
8377609dae6eSVaclav Hapla finish:
8378609dae6eSVaclav Hapla   if (msg[0] && !same && !message) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, msg);
8379609dae6eSVaclav Hapla   if (same) *same = (PetscBool) !msg[0];
8380609dae6eSVaclav Hapla   if (message) {
8381609dae6eSVaclav Hapla     *message = NULL;
8382609dae6eSVaclav Hapla     if (msg[0]) {
8383609dae6eSVaclav Hapla       ierr = PetscStrallocpy(msg, message);CHKERRQ(ierr);
8384609dae6eSVaclav Hapla     }
8385609dae6eSVaclav Hapla   }
8386609dae6eSVaclav Hapla   PetscFunctionReturn(0);
8387609dae6eSVaclav Hapla }
8388609dae6eSVaclav Hapla 
8389461a15a0SLisandro Dalcin PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
8390461a15a0SLisandro Dalcin {
8391461a15a0SLisandro Dalcin   PetscErrorCode ierr;
8392609dae6eSVaclav Hapla 
8393461a15a0SLisandro Dalcin   PetscFunctionBegin;
8394461a15a0SLisandro Dalcin   PetscValidPointer(label,2);
8395461a15a0SLisandro Dalcin   if (!*label) {
8396461a15a0SLisandro Dalcin     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
8397461a15a0SLisandro Dalcin     ierr = DMGetLabel(dm, name, label);CHKERRQ(ierr);
8398461a15a0SLisandro Dalcin   }
8399461a15a0SLisandro Dalcin   ierr = DMLabelSetValue(*label, point, value);CHKERRQ(ierr);
8400461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
8401461a15a0SLisandro Dalcin }
8402461a15a0SLisandro Dalcin 
84030fdc7489SMatthew Knepley /*
84040fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
84050fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
84060fdc7489SMatthew Knepley   (label, id) pair in the DM.
84070fdc7489SMatthew Knepley 
84080fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
84090fdc7489SMatthew Knepley   each label.
84100fdc7489SMatthew Knepley */
84110fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
84120fdc7489SMatthew Knepley {
84130fdc7489SMatthew Knepley   DMUniversalLabel ul;
84140fdc7489SMatthew Knepley   PetscBool       *active;
84150fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
84160fdc7489SMatthew Knepley   PetscErrorCode   ierr;
84170fdc7489SMatthew Knepley 
84180fdc7489SMatthew Knepley   PetscFunctionBegin;
84190fdc7489SMatthew Knepley   ierr = PetscMalloc1(1, &ul);CHKERRQ(ierr);
84200fdc7489SMatthew Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label);CHKERRQ(ierr);
84210fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
84220fdc7489SMatthew Knepley   ierr = PetscCalloc1(Nl, &active);CHKERRQ(ierr);
84230fdc7489SMatthew Knepley   ul->Nl = 0;
84240fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
84250fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
84260fdc7489SMatthew Knepley     const char *name;
84270fdc7489SMatthew Knepley 
84280fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
84290fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "depth", 6, &isdepth);CHKERRQ(ierr);
84300fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "celltype", 9, &iscelltype);CHKERRQ(ierr);
84310fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
84320fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
84330fdc7489SMatthew Knepley   }
84340fdc7489SMatthew 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);
84350fdc7489SMatthew Knepley   ul->Nv = 0;
84360fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
84370fdc7489SMatthew Knepley     DMLabel     label;
84380fdc7489SMatthew Knepley     PetscInt    nv;
84390fdc7489SMatthew Knepley     const char *name;
84400fdc7489SMatthew Knepley 
84410fdc7489SMatthew Knepley     if (!active[l]) continue;
84420fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
84430fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
84440fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
84450fdc7489SMatthew Knepley     ierr = PetscStrallocpy(name, &ul->names[m]);CHKERRQ(ierr);
84460fdc7489SMatthew Knepley     ul->indices[m]   = l;
84470fdc7489SMatthew Knepley     ul->Nv          += nv;
84480fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
84490fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
84500fdc7489SMatthew Knepley     ++m;
84510fdc7489SMatthew Knepley   }
84520fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
84530fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
84540fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
84550fdc7489SMatthew Knepley   }
84560fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
84570fdc7489SMatthew Knepley     PetscInt b;
84580fdc7489SMatthew Knepley 
84590fdc7489SMatthew Knepley     ul->masks[l] = 0;
84600fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
84610fdc7489SMatthew Knepley   }
84620fdc7489SMatthew Knepley   ierr = PetscMalloc1(ul->Nv, &ul->values);CHKERRQ(ierr);
84630fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
84640fdc7489SMatthew Knepley     DMLabel         label;
84650fdc7489SMatthew Knepley     IS              valueIS;
84660fdc7489SMatthew Knepley     const PetscInt *varr;
84670fdc7489SMatthew Knepley     PetscInt        nv, v;
84680fdc7489SMatthew Knepley 
84690fdc7489SMatthew Knepley     if (!active[l]) continue;
84700fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
84710fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
84720fdc7489SMatthew Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
84730fdc7489SMatthew Knepley     ierr = ISGetIndices(valueIS, &varr);CHKERRQ(ierr);
84740fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
84750fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
84760fdc7489SMatthew Knepley     }
84770fdc7489SMatthew Knepley     ierr = ISRestoreIndices(valueIS, &varr);CHKERRQ(ierr);
84780fdc7489SMatthew Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
84790fdc7489SMatthew Knepley     ierr = PetscSortInt(nv, &ul->values[ul->offsets[m]]);CHKERRQ(ierr);
84800fdc7489SMatthew Knepley     ++m;
84810fdc7489SMatthew Knepley   }
84820fdc7489SMatthew Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
84830fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
84840fdc7489SMatthew Knepley     PetscInt  uval = 0;
84850fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
84860fdc7489SMatthew Knepley 
84870fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
84880fdc7489SMatthew Knepley       DMLabel  label;
84890649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
84900fdc7489SMatthew Knepley 
84910fdc7489SMatthew Knepley       if (!active[l]) continue;
84920fdc7489SMatthew Knepley       ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
84930fdc7489SMatthew Knepley       ierr = DMLabelGetValue(label, p, &val);CHKERRQ(ierr);
84940fdc7489SMatthew Knepley       ierr = DMLabelGetDefaultValue(label, &defval);CHKERRQ(ierr);
84950fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
84960649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
84970fdc7489SMatthew Knepley       marked = PETSC_TRUE;
84980fdc7489SMatthew Knepley       ierr = PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc);CHKERRQ(ierr);
84990fdc7489SMatthew Knepley       if (loc < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %D not found in compression array", val);
85000fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
85010fdc7489SMatthew Knepley       ++m;
85020fdc7489SMatthew Knepley     }
85030fdc7489SMatthew Knepley     if (marked) {ierr = DMLabelSetValue(ul->label, p, uval);CHKERRQ(ierr);}
85040fdc7489SMatthew Knepley   }
85050fdc7489SMatthew Knepley   ierr = PetscFree(active);CHKERRQ(ierr);
85060fdc7489SMatthew Knepley   *universal = ul;
85070fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85080fdc7489SMatthew Knepley }
85090fdc7489SMatthew Knepley 
85100fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
85110fdc7489SMatthew Knepley {
85120fdc7489SMatthew Knepley   PetscInt       l;
85130fdc7489SMatthew Knepley   PetscErrorCode ierr;
85140fdc7489SMatthew Knepley 
85150fdc7489SMatthew Knepley   PetscFunctionBegin;
85160fdc7489SMatthew Knepley   for (l = 0; l < (*universal)->Nl; ++l) {ierr = PetscFree((*universal)->names[l]);CHKERRQ(ierr);}
85170fdc7489SMatthew Knepley   ierr = DMLabelDestroy(&(*universal)->label);CHKERRQ(ierr);
85180fdc7489SMatthew Knepley   ierr = PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks);CHKERRQ(ierr);
85190fdc7489SMatthew Knepley   ierr = PetscFree((*universal)->values);CHKERRQ(ierr);
85200fdc7489SMatthew Knepley   ierr = PetscFree(*universal);CHKERRQ(ierr);
85210fdc7489SMatthew Knepley   *universal = NULL;
85220fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85230fdc7489SMatthew Knepley }
85240fdc7489SMatthew Knepley 
85250fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
85260fdc7489SMatthew Knepley {
85270fdc7489SMatthew Knepley   PetscFunctionBegin;
85280fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
85290fdc7489SMatthew Knepley   *ulabel = ul->label;
85300fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85310fdc7489SMatthew Knepley }
85320fdc7489SMatthew Knepley 
85330fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
85340fdc7489SMatthew Knepley {
85350fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
85360fdc7489SMatthew Knepley   PetscErrorCode ierr;
85370fdc7489SMatthew Knepley 
85380fdc7489SMatthew Knepley   PetscFunctionBegin;
8539064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
85400fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
85410fdc7489SMatthew Knepley     if (preserveOrder) {ierr = DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]);CHKERRQ(ierr);}
85420fdc7489SMatthew Knepley     else               {ierr = DMCreateLabel(dm, ul->names[l]);CHKERRQ(ierr);}
85430fdc7489SMatthew Knepley   }
85440fdc7489SMatthew Knepley   if (preserveOrder) {
85450fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
85460fdc7489SMatthew Knepley       const char *name;
85470fdc7489SMatthew Knepley       PetscBool   match;
85480fdc7489SMatthew Knepley 
85490fdc7489SMatthew Knepley       ierr = DMGetLabelName(dm, ul->indices[l], &name);CHKERRQ(ierr);
85500fdc7489SMatthew Knepley       ierr = PetscStrcmp(name, ul->names[l], &match);CHKERRQ(ierr);
85510fdc7489SMatthew 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]);
85520fdc7489SMatthew Knepley     }
85530fdc7489SMatthew Knepley   }
85540fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85550fdc7489SMatthew Knepley }
85560fdc7489SMatthew Knepley 
85570fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
85580fdc7489SMatthew Knepley {
85590fdc7489SMatthew Knepley   PetscInt       l;
85600fdc7489SMatthew Knepley   PetscErrorCode ierr;
85610fdc7489SMatthew Knepley 
85620fdc7489SMatthew Knepley   PetscFunctionBegin;
85630fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
85640fdc7489SMatthew Knepley     DMLabel  label;
85650fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
85660fdc7489SMatthew Knepley 
85670fdc7489SMatthew Knepley     if (lval) {
85680fdc7489SMatthew Knepley       if (useIndex) {ierr = DMGetLabelByNum(dm, ul->indices[l], &label);CHKERRQ(ierr);}
85690fdc7489SMatthew Knepley       else          {ierr = DMGetLabel(dm, ul->names[l], &label);CHKERRQ(ierr);}
85700fdc7489SMatthew Knepley       ierr = DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]);CHKERRQ(ierr);
85710fdc7489SMatthew Knepley     }
85720fdc7489SMatthew Knepley   }
85730fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85740fdc7489SMatthew Knepley }
8575a8fb8f29SToby Isaac 
8576a8fb8f29SToby Isaac /*@
8577a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
8578a8fb8f29SToby Isaac 
8579a8fb8f29SToby Isaac   Input Parameter:
8580a8fb8f29SToby Isaac . dm - The DM object
8581a8fb8f29SToby Isaac 
8582a8fb8f29SToby Isaac   Output Parameter:
8583a8fb8f29SToby Isaac . cdm - The coarse DM
8584a8fb8f29SToby Isaac 
8585a8fb8f29SToby Isaac   Level: intermediate
8586a8fb8f29SToby Isaac 
8587a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
8588a8fb8f29SToby Isaac @*/
8589a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
8590a8fb8f29SToby Isaac {
8591a8fb8f29SToby Isaac   PetscFunctionBegin;
8592a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8593a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
8594a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
8595a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8596a8fb8f29SToby Isaac }
8597a8fb8f29SToby Isaac 
8598a8fb8f29SToby Isaac /*@
8599a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
8600a8fb8f29SToby Isaac 
8601a8fb8f29SToby Isaac   Input Parameters:
8602a8fb8f29SToby Isaac + dm - The DM object
8603a8fb8f29SToby Isaac - cdm - The coarse DM
8604a8fb8f29SToby Isaac 
8605a8fb8f29SToby Isaac   Level: intermediate
8606a8fb8f29SToby Isaac 
8607a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
8608a8fb8f29SToby Isaac @*/
8609a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
8610a8fb8f29SToby Isaac {
8611a8fb8f29SToby Isaac   PetscErrorCode ierr;
8612a8fb8f29SToby Isaac 
8613a8fb8f29SToby Isaac   PetscFunctionBegin;
8614a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8615a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
8616a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
8617a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
8618a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
8619a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8620a8fb8f29SToby Isaac }
8621a8fb8f29SToby Isaac 
862288bdff64SToby Isaac /*@
862388bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
862488bdff64SToby Isaac 
862588bdff64SToby Isaac   Input Parameter:
862688bdff64SToby Isaac . dm - The DM object
862788bdff64SToby Isaac 
862888bdff64SToby Isaac   Output Parameter:
862988bdff64SToby Isaac . fdm - The fine DM
863088bdff64SToby Isaac 
863188bdff64SToby Isaac   Level: intermediate
863288bdff64SToby Isaac 
863388bdff64SToby Isaac .seealso: DMSetFineDM()
863488bdff64SToby Isaac @*/
863588bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
863688bdff64SToby Isaac {
863788bdff64SToby Isaac   PetscFunctionBegin;
863888bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
863988bdff64SToby Isaac   PetscValidPointer(fdm, 2);
864088bdff64SToby Isaac   *fdm = dm->fineMesh;
864188bdff64SToby Isaac   PetscFunctionReturn(0);
864288bdff64SToby Isaac }
864388bdff64SToby Isaac 
864488bdff64SToby Isaac /*@
864588bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
864688bdff64SToby Isaac 
864788bdff64SToby Isaac   Input Parameters:
864888bdff64SToby Isaac + dm - The DM object
864988bdff64SToby Isaac - fdm - The fine DM
865088bdff64SToby Isaac 
865188bdff64SToby Isaac   Level: intermediate
865288bdff64SToby Isaac 
865388bdff64SToby Isaac .seealso: DMGetFineDM()
865488bdff64SToby Isaac @*/
865588bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
865688bdff64SToby Isaac {
865788bdff64SToby Isaac   PetscErrorCode ierr;
865888bdff64SToby Isaac 
865988bdff64SToby Isaac   PetscFunctionBegin;
866088bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
866188bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
866288bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
866388bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
866488bdff64SToby Isaac   dm->fineMesh = fdm;
866588bdff64SToby Isaac   PetscFunctionReturn(0);
866688bdff64SToby Isaac }
866788bdff64SToby Isaac 
8668a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8669a6ba4734SToby Isaac 
8670a6ba4734SToby Isaac /*@C
8671a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8672a6ba4734SToby Isaac 
8673783e2ec8SMatthew G. Knepley   Collective on dm
8674783e2ec8SMatthew G. Knepley 
8675a6ba4734SToby Isaac   Input Parameters:
86764c258f51SMatthew G. Knepley + dm       - The DM, with a PetscDS that matches the problem being constrained
8677f971fd6bSMatthew G. Knepley . type     - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8678a6ba4734SToby Isaac . name     - The BC name
867945480ffeSMatthew G. Knepley . label    - The label defining constrained points
868045480ffeSMatthew G. Knepley . Nv       - The number of DMLabel values for constrained points
868145480ffeSMatthew G. Knepley . values   - An array of values for constrained points
8682a6ba4734SToby Isaac . field    - The field to constrain
868345480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
8684a6ba4734SToby Isaac . comps    - An array of constrained component numbers
8685a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
868656cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
8687a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
8688a6ba4734SToby Isaac 
868945480ffeSMatthew G. Knepley   Output Parameter:
869045480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
869145480ffeSMatthew G. Knepley 
8692a6ba4734SToby Isaac   Options Database Keys:
8693a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8694a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8695a6ba4734SToby Isaac 
869656cf3b9cSMatthew G. Knepley   Note:
869756cf3b9cSMatthew 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:
869856cf3b9cSMatthew G. Knepley 
869956cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
870056cf3b9cSMatthew G. Knepley 
870156cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
870256cf3b9cSMatthew G. Knepley 
870356cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
870456cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
870556cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
870656cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
870756cf3b9cSMatthew G. Knepley 
870856cf3b9cSMatthew G. Knepley + dim - the spatial dimension
870956cf3b9cSMatthew G. Knepley . Nf - the number of fields
871056cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
871156cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
871256cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
871356cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
871456cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
871556cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
871656cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
871756cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
871856cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
871956cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
872056cf3b9cSMatthew G. Knepley . t - current time
872156cf3b9cSMatthew G. Knepley . x - coordinates of the current point
872256cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
872356cf3b9cSMatthew G. Knepley . constants - constant parameters
872456cf3b9cSMatthew G. Knepley - bcval - output values at the current point
872556cf3b9cSMatthew G. Knepley 
8726a6ba4734SToby Isaac   Level: developer
8727a6ba4734SToby Isaac 
872845480ffeSMatthew G. Knepley .seealso: DSGetBoundary(), PetscDSAddBoundary()
8729a6ba4734SToby Isaac @*/
873045480ffeSMatthew 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)
8731a6ba4734SToby Isaac {
8732e5e52638SMatthew G. Knepley   PetscDS        ds;
8733a6ba4734SToby Isaac   PetscErrorCode ierr;
8734a6ba4734SToby Isaac 
8735a6ba4734SToby Isaac   PetscFunctionBegin;
8736a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8737783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
873845480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
873945480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
874045480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
874145480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
8742e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
874345480ffeSMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, label);CHKERRQ(ierr);
874445480ffeSMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd);CHKERRQ(ierr);
8745a6ba4734SToby Isaac   PetscFunctionReturn(0);
8746a6ba4734SToby Isaac }
8747a6ba4734SToby Isaac 
874845480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8749e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8750e6f8dbb6SToby Isaac {
8751e5e52638SMatthew G. Knepley   PetscDS        ds;
8752dff059c6SToby Isaac   DMBoundary    *lastnext;
8753e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8754e6f8dbb6SToby Isaac   PetscErrorCode ierr;
8755e6f8dbb6SToby Isaac 
8756e6f8dbb6SToby Isaac   PetscFunctionBegin;
8757e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8758e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
875947a1f5adSToby Isaac   if (dm->boundary) {
876047a1f5adSToby Isaac     DMBoundary next = dm->boundary;
876147a1f5adSToby Isaac 
876247a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
876347a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
876447a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
876547a1f5adSToby Isaac     while (next) {
876647a1f5adSToby Isaac       DMBoundary b = next;
876747a1f5adSToby Isaac 
876847a1f5adSToby Isaac       next = b->next;
876947a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
8770a6ba4734SToby Isaac     }
877147a1f5adSToby Isaac     dm->boundary = NULL;
8772a6ba4734SToby Isaac   }
877347a1f5adSToby Isaac 
8774dff059c6SToby Isaac   lastnext = &(dm->boundary);
8775e6f8dbb6SToby Isaac   while (dsbound) {
8776e6f8dbb6SToby Isaac     DMBoundary dmbound;
8777e6f8dbb6SToby Isaac 
8778e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
8779e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
878045480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
878147a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8782dff059c6SToby Isaac     *lastnext = dmbound;
8783dff059c6SToby Isaac     lastnext = &(dmbound->next);
8784dff059c6SToby Isaac     dsbound = dsbound->next;
8785a6ba4734SToby Isaac   }
8786a6ba4734SToby Isaac   PetscFunctionReturn(0);
8787a6ba4734SToby Isaac }
8788a6ba4734SToby Isaac 
8789a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8790a6ba4734SToby Isaac {
8791b95f2879SToby Isaac   DMBoundary     b;
8792a6ba4734SToby Isaac   PetscErrorCode ierr;
8793a6ba4734SToby Isaac 
8794a6ba4734SToby Isaac   PetscFunctionBegin;
8795a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8796534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8797a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8798e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
8799b95f2879SToby Isaac   b = dm->boundary;
8800a6ba4734SToby Isaac   while (b && !(*isBd)) {
8801e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8802e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8803a6ba4734SToby Isaac     PetscInt   i;
8804a6ba4734SToby Isaac 
880545480ffeSMatthew G. Knepley     if (label) {
880645480ffeSMatthew G. Knepley       for (i = 0; i < dsb->Nv && !(*isBd); ++i) {ierr = DMLabelStratumHasPoint(label, dsb->values[i], point, isBd);CHKERRQ(ierr);}
8807a6ba4734SToby Isaac     }
8808a6ba4734SToby Isaac     b = b->next;
8809a6ba4734SToby Isaac   }
8810a6ba4734SToby Isaac   PetscFunctionReturn(0);
8811a6ba4734SToby Isaac }
88124d6f44ffSToby Isaac 
88134d6f44ffSToby Isaac /*@C
8814a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8815a6e0b375SMatthew G. Knepley 
8816a6e0b375SMatthew G. Knepley   Collective on DM
88174d6f44ffSToby Isaac 
88184d6f44ffSToby Isaac   Input Parameters:
88194d6f44ffSToby Isaac + dm      - The DM
88200709b2feSToby Isaac . time    - The time
88214d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
88224d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
88234d6f44ffSToby Isaac - mode    - The insertion mode for values
88244d6f44ffSToby Isaac 
88254d6f44ffSToby Isaac   Output Parameter:
88264d6f44ffSToby Isaac . X - vector
88274d6f44ffSToby Isaac 
88284d6f44ffSToby Isaac    Calling sequence of func:
88290709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
88304d6f44ffSToby Isaac 
88314d6f44ffSToby Isaac +  dim - The spatial dimension
88328ec8862eSJed Brown .  time - The time at which to sample
88334d6f44ffSToby Isaac .  x   - The coordinates
88344d6f44ffSToby Isaac .  Nf  - The number of fields
88354d6f44ffSToby Isaac .  u   - The output field values
88364d6f44ffSToby Isaac -  ctx - optional user-defined function context
88374d6f44ffSToby Isaac 
88384d6f44ffSToby Isaac   Level: developer
88394d6f44ffSToby Isaac 
8840a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
88414d6f44ffSToby Isaac @*/
88420709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
88434d6f44ffSToby Isaac {
88444d6f44ffSToby Isaac   Vec            localX;
88454d6f44ffSToby Isaac   PetscErrorCode ierr;
88464d6f44ffSToby Isaac 
88474d6f44ffSToby Isaac   PetscFunctionBegin;
88484d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88494d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
88500709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
88514d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
88524d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
88534d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
88544d6f44ffSToby Isaac   PetscFunctionReturn(0);
88554d6f44ffSToby Isaac }
88564d6f44ffSToby Isaac 
8857a6e0b375SMatthew G. Knepley /*@C
8858a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8859a6e0b375SMatthew G. Knepley 
8860a6e0b375SMatthew G. Knepley   Not collective
8861a6e0b375SMatthew G. Knepley 
8862a6e0b375SMatthew G. Knepley   Input Parameters:
8863a6e0b375SMatthew G. Knepley + dm      - The DM
8864a6e0b375SMatthew G. Knepley . time    - The time
8865a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8866a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8867a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8868a6e0b375SMatthew G. Knepley 
8869a6e0b375SMatthew G. Knepley   Output Parameter:
8870a6e0b375SMatthew G. Knepley . localX - vector
8871a6e0b375SMatthew G. Knepley 
8872a6e0b375SMatthew G. Knepley    Calling sequence of func:
8873a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8874a6e0b375SMatthew G. Knepley 
8875a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8876a6e0b375SMatthew G. Knepley .  x   - The coordinates
8877a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8878a6e0b375SMatthew G. Knepley .  u   - The output field values
8879a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8880a6e0b375SMatthew G. Knepley 
8881a6e0b375SMatthew G. Knepley   Level: developer
8882a6e0b375SMatthew G. Knepley 
8883a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8884a6e0b375SMatthew G. Knepley @*/
88850709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
88864d6f44ffSToby Isaac {
88874d6f44ffSToby Isaac   PetscErrorCode ierr;
88884d6f44ffSToby Isaac 
88894d6f44ffSToby Isaac   PetscFunctionBegin;
88904d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8891064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
88920918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
88930709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
88944d6f44ffSToby Isaac   PetscFunctionReturn(0);
88954d6f44ffSToby Isaac }
88964d6f44ffSToby Isaac 
8897a6e0b375SMatthew G. Knepley /*@C
8898a6e0b375SMatthew 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.
8899a6e0b375SMatthew G. Knepley 
8900a6e0b375SMatthew G. Knepley   Collective on DM
8901a6e0b375SMatthew G. Knepley 
8902a6e0b375SMatthew G. Knepley   Input Parameters:
8903a6e0b375SMatthew G. Knepley + dm      - The DM
8904a6e0b375SMatthew G. Knepley . time    - The time
8905a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8906a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8907a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8908a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8909a6e0b375SMatthew G. Knepley 
8910a6e0b375SMatthew G. Knepley   Output Parameter:
8911a6e0b375SMatthew G. Knepley . X - vector
8912a6e0b375SMatthew G. Knepley 
8913a6e0b375SMatthew G. Knepley    Calling sequence of func:
8914a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8915a6e0b375SMatthew G. Knepley 
8916a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8917a6e0b375SMatthew G. Knepley .  x   - The coordinates
8918a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8919a6e0b375SMatthew G. Knepley .  u   - The output field values
8920a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8921a6e0b375SMatthew G. Knepley 
8922a6e0b375SMatthew G. Knepley   Level: developer
8923a6e0b375SMatthew G. Knepley 
8924a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8925a6e0b375SMatthew G. Knepley @*/
89262c53366bSMatthew 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)
89272c53366bSMatthew G. Knepley {
89282c53366bSMatthew G. Knepley   Vec            localX;
89292c53366bSMatthew G. Knepley   PetscErrorCode ierr;
89302c53366bSMatthew G. Knepley 
89312c53366bSMatthew G. Knepley   PetscFunctionBegin;
89322c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89332c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
89342c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
89352c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
89362c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
89372c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
89382c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
89392c53366bSMatthew G. Knepley }
89402c53366bSMatthew G. Knepley 
8941a6e0b375SMatthew G. Knepley /*@C
8942a6e0b375SMatthew 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.
8943a6e0b375SMatthew G. Knepley 
8944a6e0b375SMatthew G. Knepley   Not collective
8945a6e0b375SMatthew G. Knepley 
8946a6e0b375SMatthew G. Knepley   Input Parameters:
8947a6e0b375SMatthew G. Knepley + dm      - The DM
8948a6e0b375SMatthew G. Knepley . time    - The time
8949a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8950a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8951a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8952a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8953a6e0b375SMatthew G. Knepley 
8954a6e0b375SMatthew G. Knepley   Output Parameter:
8955a6e0b375SMatthew G. Knepley . localX - vector
8956a6e0b375SMatthew G. Knepley 
8957a6e0b375SMatthew G. Knepley    Calling sequence of func:
8958a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8959a6e0b375SMatthew G. Knepley 
8960a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8961a6e0b375SMatthew G. Knepley .  x   - The coordinates
8962a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8963a6e0b375SMatthew G. Knepley .  u   - The output field values
8964a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8965a6e0b375SMatthew G. Knepley 
8966a6e0b375SMatthew G. Knepley   Level: developer
8967a6e0b375SMatthew G. Knepley 
8968a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8969a6e0b375SMatthew G. Knepley @*/
89701c531cf8SMatthew 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)
89714d6f44ffSToby Isaac {
89724d6f44ffSToby Isaac   PetscErrorCode ierr;
89734d6f44ffSToby Isaac 
89744d6f44ffSToby Isaac   PetscFunctionBegin;
89754d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8976064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
89770918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
89781c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
89794d6f44ffSToby Isaac   PetscFunctionReturn(0);
89804d6f44ffSToby Isaac }
89812716604bSToby Isaac 
8982a6e0b375SMatthew G. Knepley /*@C
8983a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8984a6e0b375SMatthew G. Knepley 
8985a6e0b375SMatthew G. Knepley   Not collective
8986a6e0b375SMatthew G. Knepley 
8987a6e0b375SMatthew G. Knepley   Input Parameters:
8988a6e0b375SMatthew G. Knepley + dm      - The DM
8989a6e0b375SMatthew G. Knepley . time    - The time
8990a6e0b375SMatthew G. Knepley . localU  - The input field vector
8991a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8992a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8993a6e0b375SMatthew G. Knepley 
8994a6e0b375SMatthew G. Knepley   Output Parameter:
8995a6e0b375SMatthew G. Knepley . localX  - The output vector
8996a6e0b375SMatthew G. Knepley 
8997a6e0b375SMatthew G. Knepley    Calling sequence of func:
8998a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8999a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9000a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9001a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9002a6e0b375SMatthew G. Knepley 
9003a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
9004a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
9005a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9006a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
9007a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9008a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
9009a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9010a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9011a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9012a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9013a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9014a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9015a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9016a6e0b375SMatthew G. Knepley .  t            - The current time
9017a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9018a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9019a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9020a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9021a6e0b375SMatthew G. Knepley 
9022a6e0b375SMatthew 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.
9023a6e0b375SMatthew 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
9024a6e0b375SMatthew 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
9025a6e0b375SMatthew 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.
9026a6e0b375SMatthew G. Knepley 
9027a6e0b375SMatthew G. Knepley   Level: intermediate
9028a6e0b375SMatthew G. Knepley 
9029a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9030a6e0b375SMatthew G. Knepley @*/
90318c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
90328c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
90338c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
90348c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9035191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
90368c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
90378c6c5593SMatthew G. Knepley {
90388c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
90398c6c5593SMatthew G. Knepley 
90408c6c5593SMatthew G. Knepley   PetscFunctionBegin;
90418c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
90428c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
90438c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
90440918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
90458c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
90468c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
90478c6c5593SMatthew G. Knepley }
90488c6c5593SMatthew G. Knepley 
9049a6e0b375SMatthew G. Knepley /*@C
9050a6e0b375SMatthew 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.
9051a6e0b375SMatthew G. Knepley 
9052a6e0b375SMatthew G. Knepley   Not collective
9053a6e0b375SMatthew G. Knepley 
9054a6e0b375SMatthew G. Knepley   Input Parameters:
9055a6e0b375SMatthew G. Knepley + dm      - The DM
9056a6e0b375SMatthew G. Knepley . time    - The time
9057a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
9058a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
9059a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
9060a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9061a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9062a6e0b375SMatthew G. Knepley . localU  - The input field vector
9063a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9064a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9065a6e0b375SMatthew G. Knepley 
9066a6e0b375SMatthew G. Knepley   Output Parameter:
9067a6e0b375SMatthew G. Knepley . localX  - The output vector
9068a6e0b375SMatthew G. Knepley 
9069a6e0b375SMatthew G. Knepley    Calling sequence of func:
9070a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9071a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9072a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9073a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9074a6e0b375SMatthew G. Knepley 
9075a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
9076a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
9077a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9078a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
9079a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9080a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
9081a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9082a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9083a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9084a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9085a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9086a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9087a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9088a6e0b375SMatthew G. Knepley .  t            - The current time
9089a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9090a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9091a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9092a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9093a6e0b375SMatthew G. Knepley 
9094a6e0b375SMatthew 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.
9095a6e0b375SMatthew 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
9096a6e0b375SMatthew 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
9097a6e0b375SMatthew 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.
9098a6e0b375SMatthew G. Knepley 
9099a6e0b375SMatthew G. Knepley   Level: intermediate
9100a6e0b375SMatthew G. Knepley 
9101a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9102a6e0b375SMatthew G. Knepley @*/
91031c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
91048c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
91058c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
91068c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9107191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
91088c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
91098c6c5593SMatthew G. Knepley {
91108c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
91118c6c5593SMatthew G. Knepley 
91128c6c5593SMatthew G. Knepley   PetscFunctionBegin;
91138c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9114064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9115064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
9116ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
91171c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
91188c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
91198c6c5593SMatthew G. Knepley }
91208c6c5593SMatthew G. Knepley 
91212716604bSToby Isaac /*@C
9122ece3a9fcSMatthew 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.
9123ece3a9fcSMatthew G. Knepley 
9124ece3a9fcSMatthew G. Knepley   Not collective
9125ece3a9fcSMatthew G. Knepley 
9126ece3a9fcSMatthew G. Knepley   Input Parameters:
9127ece3a9fcSMatthew G. Knepley + dm      - The DM
9128ece3a9fcSMatthew G. Knepley . time    - The time
9129ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
9130ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
9131ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
9132ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9133ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9134ece3a9fcSMatthew G. Knepley . localU  - The input field vector
9135ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9136ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
9137ece3a9fcSMatthew G. Knepley 
9138ece3a9fcSMatthew G. Knepley   Output Parameter:
9139ece3a9fcSMatthew G. Knepley . localX  - The output vector
9140ece3a9fcSMatthew G. Knepley 
9141ece3a9fcSMatthew G. Knepley    Calling sequence of func:
9142ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9143ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9144ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9145ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9146ece3a9fcSMatthew G. Knepley 
9147ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
9148ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
9149ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9150ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
9151ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9152ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
9153ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9154ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9155ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9156ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9157ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9158ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9159ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9160ece3a9fcSMatthew G. Knepley .  t            - The current time
9161ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
9162ece3a9fcSMatthew G. Knepley .  n            - The face normal
9163ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
9164ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
9165ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
9166ece3a9fcSMatthew G. Knepley 
9167ece3a9fcSMatthew G. Knepley   Note:
9168ece3a9fcSMatthew 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.
9169ece3a9fcSMatthew 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
9170ece3a9fcSMatthew 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
9171ece3a9fcSMatthew 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.
9172ece3a9fcSMatthew G. Knepley 
9173ece3a9fcSMatthew G. Knepley   Level: intermediate
9174ece3a9fcSMatthew G. Knepley 
9175ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9176ece3a9fcSMatthew G. Knepley @*/
9177ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
9178ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
9179ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9180ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9181ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
9182ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
9183ece3a9fcSMatthew G. Knepley {
9184ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
9185ece3a9fcSMatthew G. Knepley 
9186ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
9187ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9188064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9189064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
9190ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
9191ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
9192ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
9193ece3a9fcSMatthew G. Knepley }
9194ece3a9fcSMatthew G. Knepley 
9195ece3a9fcSMatthew G. Knepley /*@C
91962716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
91972716604bSToby Isaac 
91982716604bSToby Isaac   Input Parameters:
91992716604bSToby Isaac + dm    - The DM
92000709b2feSToby Isaac . time  - The time
92012716604bSToby Isaac . funcs - The functions to evaluate for each field component
92022716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9203574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
92042716604bSToby Isaac 
92052716604bSToby Isaac   Output Parameter:
92062716604bSToby Isaac . diff - The diff ||u - u_h||_2
92072716604bSToby Isaac 
92082716604bSToby Isaac   Level: developer
92092716604bSToby Isaac 
92101189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
92112716604bSToby Isaac @*/
92120709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
92132716604bSToby Isaac {
92142716604bSToby Isaac   PetscErrorCode ierr;
92152716604bSToby Isaac 
92162716604bSToby Isaac   PetscFunctionBegin;
92172716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9218b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
92190918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
92200709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
92212716604bSToby Isaac   PetscFunctionReturn(0);
92222716604bSToby Isaac }
9223b698f381SToby Isaac 
9224b698f381SToby Isaac /*@C
9225b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
9226b698f381SToby Isaac 
9227d083f849SBarry Smith   Collective on dm
9228d083f849SBarry Smith 
9229b698f381SToby Isaac   Input Parameters:
9230b698f381SToby Isaac + dm    - The DM
9231b698f381SToby Isaac , time  - The time
9232b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
9233b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9234574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
9235b698f381SToby Isaac - n     - The vector to project along
9236b698f381SToby Isaac 
9237b698f381SToby Isaac   Output Parameter:
9238b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
9239b698f381SToby Isaac 
9240b698f381SToby Isaac   Level: developer
9241b698f381SToby Isaac 
9242b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
9243b698f381SToby Isaac @*/
9244b698f381SToby 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)
9245b698f381SToby Isaac {
9246b698f381SToby Isaac   PetscErrorCode ierr;
9247b698f381SToby Isaac 
9248b698f381SToby Isaac   PetscFunctionBegin;
9249b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9250b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
9251b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
9252b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
9253b698f381SToby Isaac   PetscFunctionReturn(0);
9254b698f381SToby Isaac }
9255b698f381SToby Isaac 
92562a16baeaSToby Isaac /*@C
92572a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
92582a16baeaSToby Isaac 
9259d083f849SBarry Smith   Collective on dm
9260d083f849SBarry Smith 
92612a16baeaSToby Isaac   Input Parameters:
92622a16baeaSToby Isaac + dm    - The DM
92632a16baeaSToby Isaac . time  - The time
92642a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
92652a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9266574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
92672a16baeaSToby Isaac 
92682a16baeaSToby Isaac   Output Parameter:
92692a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
92702a16baeaSToby Isaac 
92712a16baeaSToby Isaac   Level: developer
92722a16baeaSToby Isaac 
92731189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
92742a16baeaSToby Isaac @*/
92751189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
92762a16baeaSToby Isaac {
92772a16baeaSToby Isaac   PetscErrorCode ierr;
92782a16baeaSToby Isaac 
92792a16baeaSToby Isaac   PetscFunctionBegin;
92802a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
92812a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
92820918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
92832a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
92842a16baeaSToby Isaac   PetscFunctionReturn(0);
92852a16baeaSToby Isaac }
92862a16baeaSToby Isaac 
9287df0b854cSToby Isaac /*@C
9288df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
9289cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
9290df0b854cSToby Isaac 
9291df0b854cSToby Isaac   Collective on dm
9292df0b854cSToby Isaac 
9293df0b854cSToby Isaac   Input parameters:
9294df0b854cSToby Isaac + dm - the pre-adaptation DM object
9295a1b0c543SToby Isaac - label - label with the flags
9296df0b854cSToby Isaac 
9297df0b854cSToby Isaac   Output parameters:
92980d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
9299df0b854cSToby Isaac 
9300df0b854cSToby Isaac   Level: intermediate
93010d1cd5e0SMatthew G. Knepley 
93020d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
9303df0b854cSToby Isaac @*/
93040d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
9305df0b854cSToby Isaac {
9306df0b854cSToby Isaac   PetscErrorCode ierr;
9307df0b854cSToby Isaac 
9308df0b854cSToby Isaac   PetscFunctionBegin;
9309df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9310a1b0c543SToby Isaac   PetscValidPointer(label,2);
93110d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
93120d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
93136f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
93140d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
9315a587d139SMark   if (*dmAdapt) {
9316a587d139SMark     (*dmAdapt)->prealloc_only = dm->prealloc_only;  /* maybe this should go .... */
9317a587d139SMark     ierr = PetscFree((*dmAdapt)->vectype);CHKERRQ(ierr);
9318a587d139SMark     ierr = PetscStrallocpy(dm->vectype,(char**)&(*dmAdapt)->vectype);CHKERRQ(ierr);
9319a587d139SMark     ierr = PetscFree((*dmAdapt)->mattype);CHKERRQ(ierr);
9320a587d139SMark     ierr = PetscStrallocpy(dm->mattype,(char**)&(*dmAdapt)->mattype);CHKERRQ(ierr);
9321a587d139SMark   }
93220d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
93230d1cd5e0SMatthew G. Knepley }
93240d1cd5e0SMatthew G. Knepley 
93250d1cd5e0SMatthew G. Knepley /*@C
93260d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
93270d1cd5e0SMatthew G. Knepley 
93280d1cd5e0SMatthew G. Knepley   Input Parameters:
93290d1cd5e0SMatthew G. Knepley + dm - The DM object
93300d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
93316f25b0d8SLisandro 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_".
93320d1cd5e0SMatthew G. Knepley 
93330d1cd5e0SMatthew G. Knepley   Output Parameter:
93340d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
93350d1cd5e0SMatthew G. Knepley 
93360d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
93370d1cd5e0SMatthew G. Knepley 
93380d1cd5e0SMatthew G. Knepley   Level: advanced
93390d1cd5e0SMatthew G. Knepley 
93400d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
93410d1cd5e0SMatthew G. Knepley @*/
93420d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
93430d1cd5e0SMatthew G. Knepley {
93440d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
93450d1cd5e0SMatthew G. Knepley 
93460d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
93470d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
93480d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
93490d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
93500d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
93516f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
93526f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
93530d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
9354df0b854cSToby Isaac   PetscFunctionReturn(0);
9355df0b854cSToby Isaac }
9356c4088d22SMatthew G. Knepley 
9357502a2867SDave May /*@C
9358502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
9359502a2867SDave May 
9360502a2867SDave May  Not Collective
9361502a2867SDave May 
9362502a2867SDave May  Input Parameter:
9363502a2867SDave May .  dm    - The DM
9364502a2867SDave May 
93650a19bb7dSprj-  Output Parameters:
93660a19bb7dSprj- +  nranks - the number of neighbours
93670a19bb7dSprj- -  ranks - the neighbors ranks
9368502a2867SDave May 
9369502a2867SDave May  Notes:
9370502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
9371502a2867SDave May 
9372502a2867SDave May  Level: beginner
9373502a2867SDave May 
9374dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
9375502a2867SDave May @*/
9376502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
9377502a2867SDave May {
9378502a2867SDave May   PetscErrorCode ierr;
9379502a2867SDave May 
9380502a2867SDave May   PetscFunctionBegin;
9381502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
93820918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
9383502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
9384502a2867SDave May   PetscFunctionReturn(0);
9385502a2867SDave May }
9386502a2867SDave May 
9387531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
9388531c7667SBarry Smith 
9389531c7667SBarry Smith /*
9390531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
9391531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
9392531c7667SBarry Smith */
9393531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
9394531c7667SBarry Smith {
9395531c7667SBarry Smith   PetscErrorCode ierr;
9396531c7667SBarry Smith 
9397531c7667SBarry Smith   PetscFunctionBegin;
9398531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9399531c7667SBarry Smith     Vec x1local;
9400531c7667SBarry Smith     DM  dm;
9401531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9402531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
9403531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
9404531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9405531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9406531c7667SBarry Smith     x1   = x1local;
9407531c7667SBarry Smith   }
9408531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
9409531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9410531c7667SBarry Smith     DM  dm;
9411531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9412531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
9413531c7667SBarry Smith   }
9414531c7667SBarry Smith   PetscFunctionReturn(0);
9415531c7667SBarry Smith }
9416531c7667SBarry Smith 
9417531c7667SBarry Smith /*@
9418531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
9419531c7667SBarry Smith 
9420531c7667SBarry Smith     Input Parameter:
9421531c7667SBarry Smith .    coloring - the MatFDColoring object
9422531c7667SBarry Smith 
942395452b02SPatrick Sanan     Developer Notes:
942495452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
9425531c7667SBarry Smith 
94261b266c99SBarry Smith     Level: advanced
94271b266c99SBarry Smith 
9428531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
9429531c7667SBarry Smith @*/
9430531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
9431531c7667SBarry Smith {
9432531c7667SBarry Smith   PetscFunctionBegin;
9433531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
9434531c7667SBarry Smith   PetscFunctionReturn(0);
9435531c7667SBarry Smith }
94368320bc6fSPatrick Sanan 
94378320bc6fSPatrick Sanan /*@
94388320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
94398320bc6fSPatrick Sanan 
94408320bc6fSPatrick Sanan     Collective
94418320bc6fSPatrick Sanan 
94428320bc6fSPatrick Sanan     Input Parameters:
9443a5bc1bf3SBarry Smith +    dm1 - the first DM
94448320bc6fSPatrick Sanan -    dm2 - the second DM
94458320bc6fSPatrick Sanan 
94468320bc6fSPatrick Sanan     Output Parameters:
94478320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
94488320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
94498320bc6fSPatrick Sanan 
94508320bc6fSPatrick Sanan     Notes:
94518320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
94523d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
94538320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
94543d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
94553d862458SPatrick Sanan     decomposition, but hold different data.
94568320bc6fSPatrick Sanan 
94578320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
94588320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
94598320bc6fSPatrick Sanan 
94608320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
94618320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
94628320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
94638320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
94648320bc6fSPatrick Sanan 
94658320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
94668320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
94678320bc6fSPatrick Sanan .vb
94688320bc6fSPatrick Sanan   ...
94698320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
94708320bc6fSPatrick Sanan   if (set && compatible)  {
94718320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
94728320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
94733d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
94748320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
94758320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
94768320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
94778320bc6fSPatrick Sanan       }
94788320bc6fSPatrick Sanan     }
94798320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
94808320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
94818320bc6fSPatrick Sanan   } else {
94828320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
94838320bc6fSPatrick Sanan   }
94848320bc6fSPatrick Sanan   ...
94858320bc6fSPatrick Sanan .ve
94868320bc6fSPatrick Sanan 
94878320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
94888320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
94898320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
94908320bc6fSPatrick Sanan     always check the "set" output parameter.
94918320bc6fSPatrick Sanan 
94928320bc6fSPatrick Sanan     A DM is always compatible with itself.
94938320bc6fSPatrick Sanan 
94948320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
94958320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
94968320bc6fSPatrick Sanan     incompatible.
94978320bc6fSPatrick Sanan 
94988320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
94998320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
95008320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
95018320bc6fSPatrick Sanan 
95028320bc6fSPatrick Sanan     Developer Notes:
95033d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
95043d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
9505a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
95068320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
95078320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
95083d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
95093d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
95108320bc6fSPatrick Sanan 
95118320bc6fSPatrick Sanan     Level: advanced
95128320bc6fSPatrick Sanan 
95133d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
95148320bc6fSPatrick Sanan @*/
95158320bc6fSPatrick Sanan 
9516a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
95178320bc6fSPatrick Sanan {
95188320bc6fSPatrick Sanan   PetscErrorCode ierr;
95198320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
95208320bc6fSPatrick Sanan   DMType         type,type2;
95218320bc6fSPatrick Sanan   PetscBool      sameType;
95228320bc6fSPatrick Sanan 
95238320bc6fSPatrick Sanan   PetscFunctionBegin;
9524a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
95258320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
95268320bc6fSPatrick Sanan 
95278320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
9528a5bc1bf3SBarry Smith   if (dm1 == dm2) {
95298320bc6fSPatrick Sanan     *set = PETSC_TRUE;
95308320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
95318320bc6fSPatrick Sanan     PetscFunctionReturn(0);
95328320bc6fSPatrick Sanan   }
95338320bc6fSPatrick Sanan 
95348320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
95358320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
95368320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
95378320bc6fSPatrick Sanan      determined by the implementation-specific logic */
9538ffc4695bSBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRMPI(ierr);
95398320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
95408320bc6fSPatrick Sanan     *set = PETSC_TRUE;
95418320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
95428320bc6fSPatrick Sanan     PetscFunctionReturn(0);
95438320bc6fSPatrick Sanan   }
95448320bc6fSPatrick Sanan 
95458320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
9546a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
9547a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
9548b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
95498320bc6fSPatrick Sanan   }
95508320bc6fSPatrick Sanan 
9551a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
95528320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
9553a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
95548320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
95558320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
95568320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
9557a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
95588320bc6fSPatrick Sanan   } else {
95598320bc6fSPatrick Sanan     *set = PETSC_FALSE;
95608320bc6fSPatrick Sanan   }
95618320bc6fSPatrick Sanan   PetscFunctionReturn(0);
95628320bc6fSPatrick Sanan }
9563c0f0dcc3SMatthew G. Knepley 
9564c0f0dcc3SMatthew G. Knepley /*@C
9565c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
9566c0f0dcc3SMatthew G. Knepley 
9567c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9568c0f0dcc3SMatthew G. Knepley 
9569c0f0dcc3SMatthew G. Knepley   Input Parameters:
9570c0f0dcc3SMatthew G. Knepley + DM - the DM
9571c0f0dcc3SMatthew G. Knepley . f - the monitor function
9572c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
9573c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
9574c0f0dcc3SMatthew G. Knepley 
9575c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9576c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
9577c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
9578c0f0dcc3SMatthew G. Knepley 
9579c0f0dcc3SMatthew G. Knepley   Notes:
9580c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
9581c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
9582c0f0dcc3SMatthew G. Knepley   order in which they were set.
9583c0f0dcc3SMatthew G. Knepley 
9584c0f0dcc3SMatthew G. Knepley   Fortran Notes:
9585c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
9586c0f0dcc3SMatthew G. Knepley 
9587c0f0dcc3SMatthew G. Knepley   Level: intermediate
9588c0f0dcc3SMatthew G. Knepley 
9589c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
9590c0f0dcc3SMatthew G. Knepley @*/
9591c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
9592c0f0dcc3SMatthew G. Knepley {
9593c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9594c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9595c0f0dcc3SMatthew G. Knepley 
9596c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9597c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9598c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9599c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9600c0f0dcc3SMatthew G. Knepley 
9601c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
9602c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9603c0f0dcc3SMatthew G. Knepley   }
9604c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9605c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9606c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9607c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9608c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9609c0f0dcc3SMatthew G. Knepley }
9610c0f0dcc3SMatthew G. Knepley 
9611c0f0dcc3SMatthew G. Knepley /*@
9612c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9613c0f0dcc3SMatthew G. Knepley 
9614c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9615c0f0dcc3SMatthew G. Knepley 
9616c0f0dcc3SMatthew G. Knepley   Input Parameter:
9617c0f0dcc3SMatthew G. Knepley . dm - the DM
9618c0f0dcc3SMatthew G. Knepley 
9619c0f0dcc3SMatthew G. Knepley   Options Database Key:
9620c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9621c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9622c0f0dcc3SMatthew G. Knepley   set via the options database
9623c0f0dcc3SMatthew G. Knepley 
9624c0f0dcc3SMatthew G. Knepley   Notes:
9625c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9626c0f0dcc3SMatthew G. Knepley 
9627c0f0dcc3SMatthew G. Knepley   Level: intermediate
9628c0f0dcc3SMatthew G. Knepley 
9629c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9630c0f0dcc3SMatthew G. Knepley @*/
9631c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9632c0f0dcc3SMatthew G. Knepley {
9633c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9634c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9635c0f0dcc3SMatthew G. Knepley 
9636c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9637c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9638c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9639c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
9640c0f0dcc3SMatthew G. Knepley   }
9641c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9642c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9643c0f0dcc3SMatthew G. Knepley }
9644c0f0dcc3SMatthew G. Knepley 
9645c0f0dcc3SMatthew G. Knepley /*@C
9646c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9647c0f0dcc3SMatthew G. Knepley 
9648c0f0dcc3SMatthew G. Knepley   Collective on DM
9649c0f0dcc3SMatthew G. Knepley 
9650c0f0dcc3SMatthew G. Knepley   Input Parameters:
9651c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9652c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9653c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9654c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9655c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9656c0f0dcc3SMatthew 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
9657c0f0dcc3SMatthew G. Knepley 
9658c0f0dcc3SMatthew G. Knepley   Output Parameter:
9659c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9660c0f0dcc3SMatthew G. Knepley 
9661c0f0dcc3SMatthew G. Knepley   Level: developer
9662c0f0dcc3SMatthew G. Knepley 
9663c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9664c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9665c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9666c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9667c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9668c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9669c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9670c0f0dcc3SMatthew G. Knepley @*/
9671c0f0dcc3SMatthew 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)
9672c0f0dcc3SMatthew G. Knepley {
9673c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9674c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9675c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
9676c0f0dcc3SMatthew G. Knepley 
9677c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9678c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9679c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
9680c0f0dcc3SMatthew G. Knepley   if (*flg) {
9681c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9682c0f0dcc3SMatthew G. Knepley 
9683c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
9684c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
9685c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
9686c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
9687c0f0dcc3SMatthew G. Knepley   }
9688c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9689c0f0dcc3SMatthew G. Knepley }
9690c0f0dcc3SMatthew G. Knepley 
9691c0f0dcc3SMatthew G. Knepley /*@
9692c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9693c0f0dcc3SMatthew G. Knepley 
9694c0f0dcc3SMatthew G. Knepley    Collective on DM
9695c0f0dcc3SMatthew G. Knepley 
9696c0f0dcc3SMatthew G. Knepley    Input Parameters:
9697c0f0dcc3SMatthew G. Knepley .  dm - The DM
9698c0f0dcc3SMatthew G. Knepley 
9699c0f0dcc3SMatthew G. Knepley    Level: developer
9700c0f0dcc3SMatthew G. Knepley 
9701c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9702c0f0dcc3SMatthew G. Knepley @*/
9703c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9704c0f0dcc3SMatthew G. Knepley {
9705c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9706c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9707c0f0dcc3SMatthew G. Knepley 
9708c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9709c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9710c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9711c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9712c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
9713c0f0dcc3SMatthew G. Knepley   }
9714c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9715c0f0dcc3SMatthew G. Knepley }
97162e4af2aeSMatthew G. Knepley 
97172e4af2aeSMatthew G. Knepley /*@
97182e4af2aeSMatthew G. Knepley   DMComputeError - Computes the error assuming the user has given exact solution functions
97192e4af2aeSMatthew G. Knepley 
97202e4af2aeSMatthew G. Knepley   Collective on DM
97212e4af2aeSMatthew G. Knepley 
97222e4af2aeSMatthew G. Knepley   Input Parameters:
97232e4af2aeSMatthew G. Knepley + dm     - The DM
97246b867d5aSJose E. Roman - sol    - The solution vector
97252e4af2aeSMatthew G. Knepley 
97266b867d5aSJose E. Roman   Input/Output Parameter:
97276b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
97286b867d5aSJose E. Roman            contains the error in each field
97296b867d5aSJose E. Roman 
97306b867d5aSJose E. Roman   Output Parameter:
97316b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
97322e4af2aeSMatthew G. Knepley 
97332e4af2aeSMatthew G. Knepley   Note: The exact solutions come from the PetscDS object, and the time comes from DMGetOutputSequenceNumber().
97342e4af2aeSMatthew G. Knepley 
97352e4af2aeSMatthew G. Knepley   Level: developer
97362e4af2aeSMatthew G. Knepley 
97372e4af2aeSMatthew G. Knepley .seealso: DMMonitorSet(), DMGetRegionNumDS(), PetscDSGetExactSolution(), DMGetOutputSequenceNumber()
97382e4af2aeSMatthew G. Knepley @*/
97392e4af2aeSMatthew G. Knepley PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
97402e4af2aeSMatthew G. Knepley {
97412e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
97422e4af2aeSMatthew G. Knepley   void            **ctxs;
97432e4af2aeSMatthew G. Knepley   PetscReal         time;
97442e4af2aeSMatthew G. Knepley   PetscInt          Nf, f, Nds, s;
97452e4af2aeSMatthew G. Knepley   PetscErrorCode    ierr;
97462e4af2aeSMatthew G. Knepley 
97472e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
97482e4af2aeSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
97492e4af2aeSMatthew G. Knepley   ierr = PetscCalloc2(Nf, &exactSol, Nf, &ctxs);CHKERRQ(ierr);
97502e4af2aeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
97512e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
97522e4af2aeSMatthew G. Knepley     PetscDS         ds;
97532e4af2aeSMatthew G. Knepley     DMLabel         label;
97542e4af2aeSMatthew G. Knepley     IS              fieldIS;
97552e4af2aeSMatthew G. Knepley     const PetscInt *fields;
97562e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
97572e4af2aeSMatthew G. Knepley 
97582e4af2aeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
97592e4af2aeSMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
97602e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);}
97612e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
97622e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
97632e4af2aeSMatthew G. Knepley       ierr = PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]);CHKERRQ(ierr);
97642e4af2aeSMatthew G. Knepley     }
97652e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);}
97662e4af2aeSMatthew G. Knepley   }
97672e4af2aeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
97682e4af2aeSMatthew 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);
97692e4af2aeSMatthew G. Knepley   }
97702e4af2aeSMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
97712e4af2aeSMatthew G. Knepley   if (errors) {ierr = DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors);CHKERRQ(ierr);}
97722e4af2aeSMatthew G. Knepley   if (errorVec) {
97732e4af2aeSMatthew G. Knepley     DM             edm;
97742e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
97752e4af2aeSMatthew G. Knepley     PetscBool      simplex;
97762e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
97772e4af2aeSMatthew G. Knepley 
97782e4af2aeSMatthew G. Knepley     ierr = DMClone(dm, &edm);CHKERRQ(ierr);
97792e4af2aeSMatthew G. Knepley     ierr = DMGetDimension(edm, &dim);CHKERRQ(ierr);
97802e4af2aeSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr);
97812e4af2aeSMatthew G. Knepley     ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
97822e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
97832e4af2aeSMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
97842e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
97852e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
97862e4af2aeSMatthew G. Knepley       PetscQuadrature q;
97872e4af2aeSMatthew G. Knepley       const char     *name;
97882e4af2aeSMatthew G. Knepley 
97892e4af2aeSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
97902e4af2aeSMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe);CHKERRQ(ierr);
97912e4af2aeSMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
97922e4af2aeSMatthew G. Knepley       ierr = PetscObjectSetName((PetscObject) efe, name);CHKERRQ(ierr);
97932e4af2aeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
97942e4af2aeSMatthew G. Knepley       ierr = PetscFESetQuadrature(efe, q);CHKERRQ(ierr);
97952e4af2aeSMatthew G. Knepley       ierr = DMSetField(edm, f, NULL, (PetscObject) efe);CHKERRQ(ierr);
97962e4af2aeSMatthew G. Knepley       ierr = PetscFEDestroy(&efe);CHKERRQ(ierr);
97972e4af2aeSMatthew G. Knepley     }
97982e4af2aeSMatthew G. Knepley     ierr = DMCreateDS(edm);CHKERRQ(ierr);
97992e4af2aeSMatthew G. Knepley 
98001e1ea65dSPierre Jolivet     ierr = DMCreateGlobalVector(edm, errorVec);CHKERRQ(ierr);
98012e4af2aeSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) *errorVec, "Error");CHKERRQ(ierr);
98022e4af2aeSMatthew G. Knepley     ierr = DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec);CHKERRQ(ierr);
98032e4af2aeSMatthew G. Knepley     ierr = DMDestroy(&edm);CHKERRQ(ierr);
98042e4af2aeSMatthew G. Knepley   }
98052e4af2aeSMatthew G. Knepley   ierr = PetscFree2(exactSol, ctxs);CHKERRQ(ierr);
98062e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
98072e4af2aeSMatthew G. Knepley }
98089a2a23afSMatthew G. Knepley 
98099a2a23afSMatthew G. Knepley /*@
98109a2a23afSMatthew G. Knepley   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this DM
98119a2a23afSMatthew G. Knepley 
98129a2a23afSMatthew G. Knepley   Not collective
98139a2a23afSMatthew G. Knepley 
98149a2a23afSMatthew G. Knepley   Input Parameter:
98159a2a23afSMatthew G. Knepley . dm     - The DM
98169a2a23afSMatthew G. Knepley 
98179a2a23afSMatthew G. Knepley   Output Parameter:
9818a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
98199a2a23afSMatthew G. Knepley 
98209a2a23afSMatthew G. Knepley   Level: advanced
98219a2a23afSMatthew G. Knepley 
98229a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryLabels(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
98239a2a23afSMatthew G. Knepley @*/
98249a2a23afSMatthew G. Knepley PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
98259a2a23afSMatthew G. Knepley {
98269a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
98279a2a23afSMatthew G. Knepley 
98289a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98299a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98309a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetSize(dm->auxData, numAux);CHKERRQ(ierr);
98319a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98329a2a23afSMatthew G. Knepley }
98339a2a23afSMatthew G. Knepley 
98349a2a23afSMatthew G. Knepley /*@
98359a2a23afSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value
98369a2a23afSMatthew G. Knepley 
98379a2a23afSMatthew G. Knepley   Not collective
98389a2a23afSMatthew G. Knepley 
98399a2a23afSMatthew G. Knepley   Input Parameters:
98409a2a23afSMatthew G. Knepley + dm     - The DM
98419a2a23afSMatthew G. Knepley . label  - The DMLabel
98429a2a23afSMatthew G. Knepley - value  - The label value indicating the region
98439a2a23afSMatthew G. Knepley 
98449a2a23afSMatthew G. Knepley   Output Parameter:
98459a2a23afSMatthew G. Knepley . aux    - The Vec holding auxiliary field data
98469a2a23afSMatthew G. Knepley 
984704c51a94SMatthew G. Knepley   Note: If no auxiliary vector is found for this (label, value), (NULL, 0) is checked as well.
984804c51a94SMatthew G. Knepley 
98499a2a23afSMatthew G. Knepley   Level: advanced
98509a2a23afSMatthew G. Knepley 
98519a2a23afSMatthew G. Knepley .seealso: DMSetAuxiliaryVec(), DMGetNumAuxiliaryVec()
98529a2a23afSMatthew G. Knepley @*/
98539a2a23afSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, Vec *aux)
98549a2a23afSMatthew G. Knepley {
985504c51a94SMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0};
985604c51a94SMatthew G. Knepley   PetscBool       has;
98579a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
98589a2a23afSMatthew G. Knepley 
98599a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98609a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98619a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
98629a2a23afSMatthew G. Knepley   key.label = label;
98639a2a23afSMatthew G. Knepley   key.value = value;
986404c51a94SMatthew G. Knepley   ierr = PetscHMapAuxHas(dm->auxData, key, &has);CHKERRQ(ierr);
986504c51a94SMatthew G. Knepley   if (has) {ierr = PetscHMapAuxGet(dm->auxData, key,  aux);CHKERRQ(ierr);}
986604c51a94SMatthew G. Knepley   else     {ierr = PetscHMapAuxGet(dm->auxData, wild, aux);CHKERRQ(ierr);}
98679a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98689a2a23afSMatthew G. Knepley }
98699a2a23afSMatthew G. Knepley 
98709a2a23afSMatthew G. Knepley /*@
98719a2a23afSMatthew G. Knepley   DMSetAuxiliaryVec - Set the auxiliary vector for region specified by the given label and value
98729a2a23afSMatthew G. Knepley 
98739a2a23afSMatthew G. Knepley   Not collective
98749a2a23afSMatthew G. Knepley 
98759a2a23afSMatthew G. Knepley   Input Parameters:
98769a2a23afSMatthew G. Knepley + dm     - The DM
98779a2a23afSMatthew G. Knepley . label  - The DMLabel
98789a2a23afSMatthew G. Knepley . value  - The label value indicating the region
98799a2a23afSMatthew G. Knepley - aux    - The Vec holding auxiliary field data
98809a2a23afSMatthew G. Knepley 
98819a2a23afSMatthew G. Knepley   Level: advanced
98829a2a23afSMatthew G. Knepley 
98839a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryVec()
98849a2a23afSMatthew G. Knepley @*/
98859a2a23afSMatthew G. Knepley PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, Vec aux)
98869a2a23afSMatthew G. Knepley {
98879a2a23afSMatthew G. Knepley   Vec             old;
98889a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
98899a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
98909a2a23afSMatthew G. Knepley 
98919a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98929a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98939a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
98949a2a23afSMatthew G. Knepley   key.label = label;
98959a2a23afSMatthew G. Knepley   key.value = value;
98969a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGet(dm->auxData, key, &old);CHKERRQ(ierr);
98979a2a23afSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) aux);CHKERRQ(ierr);
98989a2a23afSMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) old);CHKERRQ(ierr);
98999a2a23afSMatthew G. Knepley   if (!aux) {ierr = PetscHMapAuxDel(dm->auxData, key);CHKERRQ(ierr);}
99009a2a23afSMatthew G. Knepley   else      {ierr = PetscHMapAuxSet(dm->auxData, key, aux);CHKERRQ(ierr);}
99019a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
99029a2a23afSMatthew G. Knepley }
99039a2a23afSMatthew G. Knepley 
99049a2a23afSMatthew G. Knepley /*@C
99059a2a23afSMatthew G. Knepley   DMGetAuxiliaryLabels - Get the labels and values for all auxiliary vectors in this DM
99069a2a23afSMatthew G. Knepley 
99079a2a23afSMatthew G. Knepley   Not collective
99089a2a23afSMatthew G. Knepley 
99099a2a23afSMatthew G. Knepley   Input Parameter:
99109a2a23afSMatthew G. Knepley . dm      - The DM
99119a2a23afSMatthew G. Knepley 
99129a2a23afSMatthew G. Knepley   Output Parameters:
99139a2a23afSMatthew G. Knepley + labels  - The DMLabels for each Vec
99149a2a23afSMatthew G. Knepley - values  - The label values for each Vec
99159a2a23afSMatthew G. Knepley 
99169a2a23afSMatthew G. Knepley   Note: The arrays passed in must be at least as large as DMGetNumAuxiliaryVec().
99179a2a23afSMatthew G. Knepley 
99189a2a23afSMatthew G. Knepley   Level: advanced
99199a2a23afSMatthew G. Knepley 
99209a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
99219a2a23afSMatthew G. Knepley @*/
99229a2a23afSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[])
99239a2a23afSMatthew G. Knepley {
99249a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
99259a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
99269a2a23afSMatthew G. Knepley   PetscErrorCode   ierr;
99279a2a23afSMatthew G. Knepley 
99289a2a23afSMatthew G. Knepley   PetscFunctionBegin;
99299a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
99309a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
99319a2a23afSMatthew G. Knepley   PetscValidPointer(values, 3);
99329a2a23afSMatthew G. Knepley   ierr = DMGetNumAuxiliaryVec(dm, &n);CHKERRQ(ierr);
99339a2a23afSMatthew G. Knepley   ierr = PetscMalloc1(n, &keys);CHKERRQ(ierr);
99349a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetKeys(dm->auxData, &off, keys);CHKERRQ(ierr);
99359a2a23afSMatthew G. Knepley   for (i = 0; i < n; ++i) {labels[i] = keys[i].label; values[i] = keys[i].value;}
99369a2a23afSMatthew G. Knepley   ierr = PetscFree(keys);CHKERRQ(ierr);
99379a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
99389a2a23afSMatthew G. Knepley }
99399a2a23afSMatthew G. Knepley 
99409a2a23afSMatthew G. Knepley /*@
99419a2a23afSMatthew G. Knepley   DMCopyAuxiliaryVec - Copy the auxiliary data to a new DM
99429a2a23afSMatthew G. Knepley 
99439a2a23afSMatthew G. Knepley   Not collective
99449a2a23afSMatthew G. Knepley 
99459a2a23afSMatthew G. Knepley   Input Parameter:
99469a2a23afSMatthew G. Knepley . dm    - The DM
99479a2a23afSMatthew G. Knepley 
99489a2a23afSMatthew G. Knepley   Output Parameter:
99499a2a23afSMatthew G. Knepley . dmNew - The new DM, now with the same auxiliary data
99509a2a23afSMatthew G. Knepley 
99519a2a23afSMatthew G. Knepley   Level: advanced
99529a2a23afSMatthew G. Knepley 
99539a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
99549a2a23afSMatthew G. Knepley @*/
99559a2a23afSMatthew G. Knepley PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
99569a2a23afSMatthew G. Knepley {
99579a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
99589a2a23afSMatthew G. Knepley 
99599a2a23afSMatthew G. Knepley   PetscFunctionBegin;
99609a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
99619a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDestroy(&dmNew->auxData);CHKERRQ(ierr);
99629a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData);CHKERRQ(ierr);
99639a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
99649a2a23afSMatthew G. Knepley }
9965b5a892a1SMatthew G. Knepley 
9966b5a892a1SMatthew G. Knepley /*@C
9967b5a892a1SMatthew G. Knepley   DMPolytopeMatchOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
9968b5a892a1SMatthew G. Knepley 
9969b5a892a1SMatthew G. Knepley   Not collective
9970b5a892a1SMatthew G. Knepley 
9971b5a892a1SMatthew G. Knepley   Input Parameters:
9972b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9973b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9974b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9975b5a892a1SMatthew G. Knepley 
9976b5a892a1SMatthew G. Knepley   Output Parameters:
9977b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
9978b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9979b5a892a1SMatthew G. Knepley 
9980b5a892a1SMatthew G. Knepley   Level: advanced
9981b5a892a1SMatthew G. Knepley 
9982b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchVertexOrientation()
9983b5a892a1SMatthew G. Knepley @*/
9984b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9985b5a892a1SMatthew G. Knepley {
9986b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9987b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
9988b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9989b5a892a1SMatthew G. Knepley 
9990b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9991b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
9992b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9993b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9994b5a892a1SMatthew G. Knepley 
9995b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceCone[arr[c*2]] != targetCone[c]) break;
9996b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
9997b5a892a1SMatthew G. Knepley   }
9998b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9999b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10000b5a892a1SMatthew G. Knepley }
10001b5a892a1SMatthew G. Knepley 
10002b5a892a1SMatthew G. Knepley /*@C
10003b5a892a1SMatthew G. Knepley   DMPolytopeGetOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
10004b5a892a1SMatthew G. Knepley 
10005b5a892a1SMatthew G. Knepley   Not collective
10006b5a892a1SMatthew G. Knepley 
10007b5a892a1SMatthew G. Knepley   Input Parameters:
10008b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10009b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
10010b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
10011b5a892a1SMatthew G. Knepley 
10012b5a892a1SMatthew G. Knepley   Output Parameters:
10013b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
10014b5a892a1SMatthew G. Knepley 
10015b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
10016b5a892a1SMatthew G. Knepley 
10017b5a892a1SMatthew G. Knepley   Level: advanced
10018b5a892a1SMatthew G. Knepley 
10019b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchOrientation(), DMPolytopeGetVertexOrientation()
10020b5a892a1SMatthew G. Knepley @*/
10021b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
10022b5a892a1SMatthew G. Knepley {
10023b5a892a1SMatthew G. Knepley   PetscBool      found;
10024b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
10025b5a892a1SMatthew G. Knepley 
10026b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10027b5a892a1SMatthew G. Knepley   ierr = DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found);CHKERRQ(ierr);
10028b5a892a1SMatthew G. Knepley   if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
10029b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10030b5a892a1SMatthew G. Knepley }
10031b5a892a1SMatthew G. Knepley 
10032b5a892a1SMatthew G. Knepley /*@C
10033b5a892a1SMatthew G. Knepley   DMPolytopeMatchVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
10034b5a892a1SMatthew G. Knepley 
10035b5a892a1SMatthew G. Knepley   Not collective
10036b5a892a1SMatthew G. Knepley 
10037b5a892a1SMatthew G. Knepley   Input Parameters:
10038b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10039b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
10040b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
10041b5a892a1SMatthew G. Knepley 
10042b5a892a1SMatthew G. Knepley   Output Parameters:
10043b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
10044b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
10045b5a892a1SMatthew G. Knepley 
10046b5a892a1SMatthew G. Knepley   Level: advanced
10047b5a892a1SMatthew G. Knepley 
10048b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchOrientation()
10049b5a892a1SMatthew G. Knepley @*/
10050b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
10051b5a892a1SMatthew G. Knepley {
10052b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
10053b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
10054b5a892a1SMatthew G. Knepley   PetscInt       o, c;
10055b5a892a1SMatthew G. Knepley 
10056b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10057b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
10058b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
10059b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
10060b5a892a1SMatthew G. Knepley 
10061b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceVert[arr[c]] != targetVert[c]) break;
10062b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
10063b5a892a1SMatthew G. Knepley   }
10064b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
10065b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10066b5a892a1SMatthew G. Knepley }
10067b5a892a1SMatthew G. Knepley 
10068b5a892a1SMatthew G. Knepley /*@C
10069b5a892a1SMatthew G. Knepley   DMPolytopeGetVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
10070b5a892a1SMatthew G. Knepley 
10071b5a892a1SMatthew G. Knepley   Not collective
10072b5a892a1SMatthew G. Knepley 
10073b5a892a1SMatthew G. Knepley   Input Parameters:
10074b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10075b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
10076b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
10077b5a892a1SMatthew G. Knepley 
10078b5a892a1SMatthew G. Knepley   Output Parameters:
10079b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
10080b5a892a1SMatthew G. Knepley 
10081b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
10082b5a892a1SMatthew G. Knepley 
10083b5a892a1SMatthew G. Knepley   Level: advanced
10084b5a892a1SMatthew G. Knepley 
10085b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchVertexOrientation(), DMPolytopeGetOrientation()
10086b5a892a1SMatthew G. Knepley @*/
10087b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
10088b5a892a1SMatthew G. Knepley {
10089b5a892a1SMatthew G. Knepley   PetscBool      found;
10090b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
10091b5a892a1SMatthew G. Knepley 
10092b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10093b5a892a1SMatthew G. Knepley   ierr = DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found);CHKERRQ(ierr);
10094b5a892a1SMatthew G. Knepley   if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
10095b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10096b5a892a1SMatthew G. Knepley }
10097012bc364SMatthew G. Knepley 
10098012bc364SMatthew G. Knepley /*@C
10099012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
10100012bc364SMatthew G. Knepley 
10101012bc364SMatthew G. Knepley   Not collective
10102012bc364SMatthew G. Knepley 
10103012bc364SMatthew G. Knepley   Input Parameters:
10104012bc364SMatthew G. Knepley + ct    - The DMPolytopeType
10105012bc364SMatthew G. Knepley - point - Coordinates of the point
10106012bc364SMatthew G. Knepley 
10107012bc364SMatthew G. Knepley   Output Parameters:
10108012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
10109012bc364SMatthew G. Knepley 
10110012bc364SMatthew G. Knepley   Level: advanced
10111012bc364SMatthew G. Knepley 
10112012bc364SMatthew G. Knepley .seealso: DMLocatePoints()
10113012bc364SMatthew G. Knepley @*/
10114012bc364SMatthew G. Knepley PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
10115012bc364SMatthew G. Knepley {
10116012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
10117012bc364SMatthew G. Knepley   PetscInt  d;
10118012bc364SMatthew G. Knepley 
10119012bc364SMatthew G. Knepley   PetscFunctionBegin;
10120012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
10121012bc364SMatthew G. Knepley   switch (ct) {
10122012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
10123012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
10124012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
10125012bc364SMatthew G. Knepley       if (point[d] < -1.0) {*inside = PETSC_FALSE; break;}
10126012bc364SMatthew G. Knepley       sum += point[d];
10127012bc364SMatthew G. Knepley     }
10128012bc364SMatthew G. Knepley     if (sum > PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10129012bc364SMatthew G. Knepley     break;
10130012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
10131012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
10132012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
10133012bc364SMatthew G. Knepley       if (PetscAbsReal(point[d]) > 1.+PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10134012bc364SMatthew G. Knepley     break;
10135012bc364SMatthew G. Knepley   default:
10136012bc364SMatthew G. Knepley     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
10137012bc364SMatthew G. Knepley   }
10138012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
10139012bc364SMatthew G. Knepley }
10140