xref: /petsc/src/dm/interface/dm.c (revision 2c71b3e237ead271e4f3aa1505f92bf476e3413d)
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;
61a4ea9b21SRichard Tran Mills   v->bind_below               = 0;
621411c6eeSJed Brown   v->bs                       = 1;
63171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
6488ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
651bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
66c58f1c22SToby Isaac   v->labels                   = NULL;
6734aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
6834aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
69c58f1c22SToby Isaac   v->depthLabel               = NULL;
70ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
711bb6d2a8SBarry Smith   v->localSection             = NULL;
721bb6d2a8SBarry Smith   v->globalSection            = NULL;
73fba222abSToby Isaac   v->defaultConstraintSection = NULL;
74fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
75c6b900c6SMatthew G. Knepley   v->L                        = NULL;
76c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
775dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
789a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7996173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
80435a35e8SMatthew G Knepley   {
81435a35e8SMatthew G Knepley     PetscInt i;
82435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
830298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
84f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
85435a35e8SMatthew G Knepley     }
86435a35e8SMatthew G Knepley   }
8745480ffeSMatthew G. Knepley   ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
88b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
89e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
909a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxCreate(&v->auxData);CHKERRQ(ierr);
9114f150ffSMatthew G. Knepley   v->dmBC = NULL;
92a8fb8f29SToby Isaac   v->coarseMesh = NULL;
93f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
94cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
95c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
96b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
974a7a4c06SLawrence Mitchell 
981411c6eeSJed Brown   *dm = v;
99a4121054SBarry Smith   PetscFunctionReturn(0);
100a4121054SBarry Smith }
101a4121054SBarry Smith 
10238221697SMatthew G. Knepley /*@
10338221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
10438221697SMatthew G. Knepley 
105d083f849SBarry Smith   Collective
10638221697SMatthew G. Knepley 
10738221697SMatthew G. Knepley   Input Parameter:
10838221697SMatthew G. Knepley . dm - The original DM object
10938221697SMatthew G. Knepley 
11038221697SMatthew G. Knepley   Output Parameter:
11138221697SMatthew G. Knepley . newdm  - The new DM object
11238221697SMatthew G. Knepley 
11338221697SMatthew G. Knepley   Level: beginner
11438221697SMatthew G. Knepley 
1151cb8cacdSPatrick Sanan   Notes:
1161cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1171cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1181cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1191bb6d2a8SBarry Smith 
12089706ed2SPatrick Sanan   The clone is considered set up iff the original is.
12189706ed2SPatrick Sanan 
12292cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1231bb6d2a8SBarry Smith 
12438221697SMatthew G. Knepley @*/
12538221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
12638221697SMatthew G. Knepley {
12738221697SMatthew G. Knepley   PetscSF        sf;
12838221697SMatthew G. Knepley   Vec            coords;
12938221697SMatthew G. Knepley   void          *ctx;
130a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
13138221697SMatthew G. Knepley   PetscErrorCode ierr;
13238221697SMatthew G. Knepley 
13338221697SMatthew G. Knepley   PetscFunctionBegin;
13438221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13538221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
13638221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1372cbb9b06SVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL);CHKERRQ(ierr);
138ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
139ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
140c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
141a587d139SMark   ierr = PetscFree((*newdm)->vectype);CHKERRQ(ierr);
142a587d139SMark   ierr = PetscStrallocpy(dm->vectype,(char**)&(*newdm)->vectype);CHKERRQ(ierr);
143a587d139SMark   ierr = PetscFree((*newdm)->mattype);CHKERRQ(ierr);
144a587d139SMark   ierr = PetscStrallocpy(dm->mattype,(char**)&(*newdm)->mattype);CHKERRQ(ierr);
1451de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1461de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
14738221697SMatthew G. Knepley   if (dm->ops->clone) {
14838221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
14938221697SMatthew G. Knepley   }
1503f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
15138221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
15238221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
15338221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
15438221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
155be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
156be4c1c3eSMatthew G. Knepley     DM           ncdm;
157be4c1c3eSMatthew G. Knepley     PetscSection cs;
1585a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
159be4c1c3eSMatthew G. Knepley 
16092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
161be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
162ffc4695bSBarry Smith     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
1635a0206caSToby Isaac     if (pEndMax >= 0) {
164be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
16583bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
16692fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
167a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
168be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
169be4c1c3eSMatthew G. Knepley     }
170be4c1c3eSMatthew G. Knepley   }
171a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
172a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
17338221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
17438221697SMatthew G. Knepley   if (coords) {
17538221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
17638221697SMatthew G. Knepley   } else {
17738221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
17838221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
17938221697SMatthew G. Knepley   }
18090b157c4SStefano Zampini   {
18190b157c4SStefano Zampini     PetscBool             isper;
182c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1835dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
18490b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
18590b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
186c6b900c6SMatthew G. Knepley   }
18734aa8a36SMatthew G. Knepley   {
18834aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
18934aa8a36SMatthew G. Knepley 
19034aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
19134aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
19234aa8a36SMatthew G. Knepley   }
19338221697SMatthew G. Knepley   PetscFunctionReturn(0);
19438221697SMatthew G. Knepley }
19538221697SMatthew G. Knepley 
1969a42bb27SBarry Smith /*@C
197564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1989a42bb27SBarry Smith 
199d083f849SBarry Smith    Logically Collective on da
2009a42bb27SBarry Smith 
2019a42bb27SBarry Smith    Input Parameter:
2029a42bb27SBarry Smith +  da - initial distributed array
203e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
2049a42bb27SBarry Smith 
2059a42bb27SBarry Smith    Options Database:
206dd85299cSBarry Smith .   -dm_vec_type ctype
2079a42bb27SBarry Smith 
2089a42bb27SBarry Smith    Level: intermediate
2099a42bb27SBarry Smith 
210a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
2119a42bb27SBarry Smith @*/
21219fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2139a42bb27SBarry Smith {
2149a42bb27SBarry Smith   PetscErrorCode ierr;
2159a42bb27SBarry Smith 
2169a42bb27SBarry Smith   PetscFunctionBegin;
2179a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2189a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
21919fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2209a42bb27SBarry Smith   PetscFunctionReturn(0);
2219a42bb27SBarry Smith }
2229a42bb27SBarry Smith 
223c0dedaeaSBarry Smith /*@C
224c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
225c0dedaeaSBarry Smith 
226d083f849SBarry Smith    Logically Collective on da
227c0dedaeaSBarry Smith 
228c0dedaeaSBarry Smith    Input Parameter:
229c0dedaeaSBarry Smith .  da - initial distributed array
230c0dedaeaSBarry Smith 
231c0dedaeaSBarry Smith    Output Parameter:
232c0dedaeaSBarry Smith .  ctype - the vector type
233c0dedaeaSBarry Smith 
234c0dedaeaSBarry Smith    Level: intermediate
235c0dedaeaSBarry Smith 
236a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
237c0dedaeaSBarry Smith @*/
238c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
239c0dedaeaSBarry Smith {
240c0dedaeaSBarry Smith   PetscFunctionBegin;
241c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
242c0dedaeaSBarry Smith   *ctype = da->vectype;
243c0dedaeaSBarry Smith   PetscFunctionReturn(0);
244c0dedaeaSBarry Smith }
245c0dedaeaSBarry Smith 
2465f1ad066SMatthew G Knepley /*@
24734f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2485f1ad066SMatthew G Knepley 
2495f1ad066SMatthew G Knepley   Not collective
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley   Input Parameter:
2525f1ad066SMatthew G Knepley . v - The Vec
2535f1ad066SMatthew G Knepley 
2545f1ad066SMatthew G Knepley   Output Parameter:
2555f1ad066SMatthew G Knepley . dm - The DM
2565f1ad066SMatthew G Knepley 
2575f1ad066SMatthew G Knepley   Level: intermediate
2585f1ad066SMatthew G Knepley 
2595f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2605f1ad066SMatthew G Knepley @*/
2615f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2625f1ad066SMatthew G Knepley {
2635f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2645f1ad066SMatthew G Knepley 
2655f1ad066SMatthew G Knepley   PetscFunctionBegin;
2665f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2675f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2685f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2695f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2705f1ad066SMatthew G Knepley }
2715f1ad066SMatthew G Knepley 
2725f1ad066SMatthew G Knepley /*@
273d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2745f1ad066SMatthew G Knepley 
2755f1ad066SMatthew G Knepley   Not collective
2765f1ad066SMatthew G Knepley 
2775f1ad066SMatthew G Knepley   Input Parameters:
2785f1ad066SMatthew G Knepley + v - The Vec
2795f1ad066SMatthew G Knepley - dm - The DM
2805f1ad066SMatthew G Knepley 
281d9805387SMatthew 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.
282d9805387SMatthew G. Knepley 
2835f1ad066SMatthew G Knepley   Level: intermediate
2845f1ad066SMatthew G Knepley 
2855f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2865f1ad066SMatthew G Knepley @*/
2875f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2885f1ad066SMatthew G Knepley {
2895f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2905f1ad066SMatthew G Knepley 
2915f1ad066SMatthew G Knepley   PetscFunctionBegin;
2925f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
293d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2945f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2955f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2965f1ad066SMatthew G Knepley }
2975f1ad066SMatthew G Knepley 
298521d9a4cSLisandro Dalcin /*@C
2998f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
3008f1509bcSBarry Smith 
301d083f849SBarry Smith    Logically Collective on dm
3028f1509bcSBarry Smith 
3038f1509bcSBarry Smith    Input Parameters:
3048f1509bcSBarry Smith +  dm - the DM context
3058f1509bcSBarry Smith -  ctype - the matrix type
3068f1509bcSBarry Smith 
3078f1509bcSBarry Smith    Options Database:
3088f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3098f1509bcSBarry Smith 
3108f1509bcSBarry Smith    Level: intermediate
3118f1509bcSBarry Smith 
3128f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3138f1509bcSBarry Smith           DMGetISColoringType()
3148f1509bcSBarry Smith @*/
3158f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3168f1509bcSBarry Smith {
3178f1509bcSBarry Smith   PetscFunctionBegin;
3188f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3198f1509bcSBarry Smith   dm->coloringtype = ctype;
3208f1509bcSBarry Smith   PetscFunctionReturn(0);
3218f1509bcSBarry Smith }
3228f1509bcSBarry Smith 
3238f1509bcSBarry Smith /*@C
3248f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
325521d9a4cSLisandro Dalcin 
326d083f849SBarry Smith    Logically Collective on dm
327521d9a4cSLisandro Dalcin 
328521d9a4cSLisandro Dalcin    Input Parameter:
3298f1509bcSBarry Smith .  dm - the DM context
3308f1509bcSBarry Smith 
3318f1509bcSBarry Smith    Output Parameter:
3328f1509bcSBarry Smith .  ctype - the matrix type
3338f1509bcSBarry Smith 
3348f1509bcSBarry Smith    Options Database:
3358f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3368f1509bcSBarry Smith 
3378f1509bcSBarry Smith    Level: intermediate
3388f1509bcSBarry Smith 
3398f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3408f1509bcSBarry Smith           DMGetISColoringType()
3418f1509bcSBarry Smith @*/
3428f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3438f1509bcSBarry Smith {
3448f1509bcSBarry Smith   PetscFunctionBegin;
3458f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3468f1509bcSBarry Smith   *ctype = dm->coloringtype;
3478f1509bcSBarry Smith   PetscFunctionReturn(0);
3488f1509bcSBarry Smith }
3498f1509bcSBarry Smith 
3508f1509bcSBarry Smith /*@C
3518f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3528f1509bcSBarry Smith 
353d083f849SBarry Smith    Logically Collective on dm
3548f1509bcSBarry Smith 
3558f1509bcSBarry Smith    Input Parameters:
356521d9a4cSLisandro Dalcin +  dm - the DM context
357a2b5a043SBarry Smith -  ctype - the matrix type
358521d9a4cSLisandro Dalcin 
359521d9a4cSLisandro Dalcin    Options Database:
360521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
361521d9a4cSLisandro Dalcin 
362521d9a4cSLisandro Dalcin    Level: intermediate
363521d9a4cSLisandro Dalcin 
364a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
365521d9a4cSLisandro Dalcin @*/
36619fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
367521d9a4cSLisandro Dalcin {
368521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
36988f0584fSBarry Smith 
370521d9a4cSLisandro Dalcin   PetscFunctionBegin;
371521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
372521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
37319fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
374521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
375521d9a4cSLisandro Dalcin }
376521d9a4cSLisandro Dalcin 
377c0dedaeaSBarry Smith /*@C
378c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
379c0dedaeaSBarry Smith 
380d083f849SBarry Smith    Logically Collective on dm
381c0dedaeaSBarry Smith 
382c0dedaeaSBarry Smith    Input Parameter:
383c0dedaeaSBarry Smith .  dm - the DM context
384c0dedaeaSBarry Smith 
385c0dedaeaSBarry Smith    Output Parameter:
386c0dedaeaSBarry Smith .  ctype - the matrix type
387c0dedaeaSBarry Smith 
388c0dedaeaSBarry Smith    Options Database:
389c0dedaeaSBarry Smith .   -dm_mat_type ctype
390c0dedaeaSBarry Smith 
391c0dedaeaSBarry Smith    Level: intermediate
392c0dedaeaSBarry Smith 
393a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
394c0dedaeaSBarry Smith @*/
395c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
396c0dedaeaSBarry Smith {
397c0dedaeaSBarry Smith   PetscFunctionBegin;
398c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
399c0dedaeaSBarry Smith   *ctype = dm->mattype;
400c0dedaeaSBarry Smith   PetscFunctionReturn(0);
401c0dedaeaSBarry Smith }
402c0dedaeaSBarry Smith 
403c688c046SMatthew G Knepley /*@
40434f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
405c688c046SMatthew G Knepley 
406c688c046SMatthew G Knepley   Not collective
407c688c046SMatthew G Knepley 
408c688c046SMatthew G Knepley   Input Parameter:
409c688c046SMatthew G Knepley . A - The Mat
410c688c046SMatthew G Knepley 
411c688c046SMatthew G Knepley   Output Parameter:
412c688c046SMatthew G Knepley . dm - The DM
413c688c046SMatthew G Knepley 
414c688c046SMatthew G Knepley   Level: intermediate
415c688c046SMatthew G Knepley 
4168f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4178f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4188f1509bcSBarry Smith 
419c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
420c688c046SMatthew G Knepley @*/
421c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
422c688c046SMatthew G Knepley {
423c688c046SMatthew G Knepley   PetscErrorCode ierr;
424c688c046SMatthew G Knepley 
425c688c046SMatthew G Knepley   PetscFunctionBegin;
426c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
427c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
428c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
429c688c046SMatthew G Knepley   PetscFunctionReturn(0);
430c688c046SMatthew G Knepley }
431c688c046SMatthew G Knepley 
432c688c046SMatthew G Knepley /*@
433c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
434c688c046SMatthew G Knepley 
435c688c046SMatthew G Knepley   Not collective
436c688c046SMatthew G Knepley 
437c688c046SMatthew G Knepley   Input Parameters:
438c688c046SMatthew G Knepley + A - The Mat
439c688c046SMatthew G Knepley - dm - The DM
440c688c046SMatthew G Knepley 
441c688c046SMatthew G Knepley   Level: intermediate
442c688c046SMatthew G Knepley 
4438f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4448f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4458f1509bcSBarry Smith 
446c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
447c688c046SMatthew G Knepley @*/
448c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
449c688c046SMatthew G Knepley {
450c688c046SMatthew G Knepley   PetscErrorCode ierr;
451c688c046SMatthew G Knepley 
452c688c046SMatthew G Knepley   PetscFunctionBegin;
453c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4548865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
455c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
456c688c046SMatthew G Knepley   PetscFunctionReturn(0);
457c688c046SMatthew G Knepley }
458c688c046SMatthew G Knepley 
4599a42bb27SBarry Smith /*@C
4609a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4616757b960SDave May    DM options in the database.
4629a42bb27SBarry Smith 
463d083f849SBarry Smith    Logically Collective on dm
4649a42bb27SBarry Smith 
465d8d19677SJose E. Roman    Input Parameters:
4668353ddbbSDave May +  da - the DM context
4679a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4689a42bb27SBarry Smith 
4699a42bb27SBarry Smith    Notes:
4709a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4719a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4729a42bb27SBarry Smith 
4739a42bb27SBarry Smith    Level: advanced
4749a42bb27SBarry Smith 
4759a42bb27SBarry Smith .seealso: DMSetFromOptions()
4769a42bb27SBarry Smith @*/
4777087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4789a42bb27SBarry Smith {
4799a42bb27SBarry Smith   PetscErrorCode ierr;
4809a42bb27SBarry Smith 
4819a42bb27SBarry Smith   PetscFunctionBegin;
4829a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4839a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
484691be533SLawrence Mitchell   if (dm->sf) {
485691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
486691be533SLawrence Mitchell   }
4871bb6d2a8SBarry Smith   if (dm->sectionSF) {
4881bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
489691be533SLawrence Mitchell   }
4909a42bb27SBarry Smith   PetscFunctionReturn(0);
4919a42bb27SBarry Smith }
4929a42bb27SBarry Smith 
49331697293SDave May /*@C
49431697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
49531697293SDave May    DM options in the database.
49631697293SDave May 
497d083f849SBarry Smith    Logically Collective on dm
49831697293SDave May 
49931697293SDave May    Input Parameters:
50031697293SDave May +  dm - the DM context
50131697293SDave May -  prefix - the prefix string to prepend to all DM option requests
50231697293SDave May 
50331697293SDave May    Notes:
50431697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
50531697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
50631697293SDave May 
50731697293SDave May    Level: advanced
50831697293SDave May 
50931697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
51031697293SDave May @*/
51131697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
51231697293SDave May {
51331697293SDave May   PetscErrorCode ierr;
51431697293SDave May 
51531697293SDave May   PetscFunctionBegin;
51631697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
51731697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
51831697293SDave May   PetscFunctionReturn(0);
51931697293SDave May }
52031697293SDave May 
52131697293SDave May /*@C
52231697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
52331697293SDave May    DM options in the database.
52431697293SDave May 
52531697293SDave May    Not Collective
52631697293SDave May 
52731697293SDave May    Input Parameters:
52831697293SDave May .  dm - the DM context
52931697293SDave May 
53031697293SDave May    Output Parameters:
53131697293SDave May .  prefix - pointer to the prefix string used is returned
53231697293SDave May 
53395452b02SPatrick Sanan    Notes:
53495452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
53531697293SDave May    sufficient length to hold the prefix.
53631697293SDave May 
53731697293SDave May    Level: advanced
53831697293SDave May 
53931697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
54031697293SDave May @*/
54131697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
54231697293SDave May {
54331697293SDave May   PetscErrorCode ierr;
54431697293SDave May 
54531697293SDave May   PetscFunctionBegin;
54631697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54731697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
54831697293SDave May   PetscFunctionReturn(0);
54931697293SDave May }
55031697293SDave May 
55188bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
55288bdff64SToby Isaac {
5536eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
55488bdff64SToby Isaac   PetscErrorCode ierr;
55588bdff64SToby Isaac 
55688bdff64SToby Isaac   PetscFunctionBegin;
557aab5bcd8SJed Brown   *ncrefct = 0;
55888bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55988bdff64SToby Isaac     refct--;
56088bdff64SToby Isaac     if (recurseCoarse) {
56188bdff64SToby Isaac       PetscInt coarseCount;
56288bdff64SToby Isaac 
56388bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
56488bdff64SToby Isaac       refct += coarseCount;
56588bdff64SToby Isaac     }
56688bdff64SToby Isaac   }
56788bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56888bdff64SToby Isaac     refct--;
56988bdff64SToby Isaac     if (recurseFine) {
57088bdff64SToby Isaac       PetscInt fineCount;
57188bdff64SToby Isaac 
57288bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
57388bdff64SToby Isaac       refct += fineCount;
57488bdff64SToby Isaac     }
57588bdff64SToby Isaac   }
57688bdff64SToby Isaac   *ncrefct = refct;
57788bdff64SToby Isaac   PetscFunctionReturn(0);
57888bdff64SToby Isaac }
57988bdff64SToby Isaac 
580f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
581354557abSToby Isaac {
5825d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
583354557abSToby Isaac   PetscErrorCode ierr;
584354557abSToby Isaac 
585354557abSToby Isaac   PetscFunctionBegin;
586354557abSToby Isaac   /* destroy the labels */
587354557abSToby Isaac   while (next) {
588354557abSToby Isaac     DMLabelLink tmp = next->next;
589354557abSToby Isaac 
5905d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
591ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
592354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
593354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
594354557abSToby Isaac     next = tmp;
595354557abSToby Isaac   }
5965d80c0bfSVaclav Hapla   dm->labels = NULL;
597354557abSToby Isaac   PetscFunctionReturn(0);
598354557abSToby Isaac }
599354557abSToby Isaac 
6001fb7b255SJunchao Zhang /*@C
6018472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
60247c6ae99SBarry Smith 
603d083f849SBarry Smith     Collective on dm
60447c6ae99SBarry Smith 
60547c6ae99SBarry Smith     Input Parameter:
60647c6ae99SBarry Smith .   dm - the DM object to destroy
60747c6ae99SBarry Smith 
60847c6ae99SBarry Smith     Level: developer
60947c6ae99SBarry Smith 
610e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
61147c6ae99SBarry Smith 
61247c6ae99SBarry Smith @*/
613fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
61447c6ae99SBarry Smith {
6156eb26441SStefano Zampini   PetscInt       cnt;
616dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61747c6ae99SBarry Smith   PetscErrorCode ierr;
61847c6ae99SBarry Smith 
61947c6ae99SBarry Smith   PetscFunctionBegin;
6206bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6216bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
62287e657c6SBarry Smith 
62388bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
62488bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
62588bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
626ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6276bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6286bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6296eb26441SStefano Zampini 
6306eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6316eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6326eb26441SStefano Zampini 
633f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6340298fd71SBarry Smith   (*dm)->namedglobal = NULL;
635f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6362348bcf4SPeter Brune     nnext = nlink->next;
637*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(nlink->status != DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6382348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6392348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6402348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6412348bcf4SPeter Brune   }
642f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6430298fd71SBarry Smith   (*dm)->namedlocal = NULL;
644f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
645f490541aSPeter Brune     nnext = nlink->next;
646*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(nlink->status != DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
647f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
648f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
649f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
650f490541aSPeter Brune   }
6512348bcf4SPeter Brune 
652b17ce1afSJed Brown   /* Destroy the list of hooks */
653c833c3b5SJed Brown   {
654c833c3b5SJed Brown     DMCoarsenHookLink link,next;
655b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
656b17ce1afSJed Brown       next = link->next;
657b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
658b17ce1afSJed Brown     }
6590298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
660c833c3b5SJed Brown   }
661c833c3b5SJed Brown   {
662c833c3b5SJed Brown     DMRefineHookLink link,next;
663c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
664c833c3b5SJed Brown       next = link->next;
665c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
666c833c3b5SJed Brown     }
6670298fd71SBarry Smith     (*dm)->refinehook = NULL;
668c833c3b5SJed Brown   }
669be081cd6SPeter Brune   {
670be081cd6SPeter Brune     DMSubDomainHookLink link,next;
671be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
672be081cd6SPeter Brune       next = link->next;
673be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
674be081cd6SPeter Brune     }
6750298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
676be081cd6SPeter Brune   }
677baf369e7SPeter Brune   {
678baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
679baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
680baf369e7SPeter Brune       next = link->next;
681baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
682baf369e7SPeter Brune     }
6830298fd71SBarry Smith     (*dm)->gtolhook = NULL;
684baf369e7SPeter Brune   }
685d4d07f1eSToby Isaac   {
686d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
687d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
688d4d07f1eSToby Isaac       next = link->next;
689d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
690d4d07f1eSToby Isaac     }
691d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
692d4d07f1eSToby Isaac   }
693aa1993deSMatthew G Knepley   /* Destroy the work arrays */
694aa1993deSMatthew G Knepley   {
695aa1993deSMatthew G Knepley     DMWorkLink link,next;
696*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse((*dm)->workout,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
697aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
698aa1993deSMatthew G Knepley       next = link->next;
699aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
700aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
701aa1993deSMatthew G Knepley     }
7020298fd71SBarry Smith     (*dm)->workin = NULL;
703aa1993deSMatthew G Knepley   }
704c58f1c22SToby Isaac   /* destroy the labels */
705f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
706f4cdcedcSVaclav Hapla   /* destroy the fields */
707e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
708f4cdcedcSVaclav Hapla   /* destroy the boundaries */
709e6f8dbb6SToby Isaac   {
710e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
711e6f8dbb6SToby Isaac     while (next) {
712e6f8dbb6SToby Isaac       DMBoundary b = next;
713e6f8dbb6SToby Isaac 
714e6f8dbb6SToby Isaac       next = b->next;
715e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
716e6f8dbb6SToby Isaac     }
717e6f8dbb6SToby Isaac   }
718b17ce1afSJed Brown 
71952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
72052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
72152536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
72252536dc3SBarry Smith 
7231a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7241a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7251a266240SBarry Smith   }
72671cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7276bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7286bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
729073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
73088ed4aceSMatthew G Knepley 
7311bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7321bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7338b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
734fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
735fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
73688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7371bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
738736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
739736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7408e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
741736995cdSBlaise Bourdin     }
742736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
743736995cdSBlaise Bourdin   }
7449a2a23afSMatthew G. Knepley   {
7459a2a23afSMatthew G. Knepley     Vec     *auxData;
7469a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7479a2a23afSMatthew G. Knepley 
7489a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxGetSize((*dm)->auxData, &n);CHKERRQ(ierr);
7499a2a23afSMatthew G. Knepley     ierr = PetscMalloc1(n, &auxData);CHKERRQ(ierr);
7509a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxGetVals((*dm)->auxData, &off, auxData);CHKERRQ(ierr);
7519a2a23afSMatthew G. Knepley     for (i = 0; i < n; ++i) {ierr = VecDestroy(&auxData[i]);CHKERRQ(ierr);}
7529a2a23afSMatthew G. Knepley     ierr = PetscFree(auxData);CHKERRQ(ierr);
7539a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxDestroy(&(*dm)->auxData);CHKERRQ(ierr);
7549a2a23afSMatthew G. Knepley   }
75588bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
75688bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
75788bdff64SToby Isaac   }
7586eb26441SStefano Zampini 
759a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
76088bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
76188bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
76288bdff64SToby Isaac   }
76388bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
764f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7656636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7666636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7676636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
768412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
769412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
770412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
771ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
772ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
773ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7746636e97aSMatthew G Knepley 
775e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
77614f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
777e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
778e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
779732e2eb9SMatthew G Knepley 
780ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7816bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
782ed3c66a1SDave May   }
783c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
784f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
785a2c9b50fSJeremy L Thompson   ierr = CeedElemRestrictionDestroy(&(*dm)->ceedERestrict);CHKERRQ_CEED(ierr);
786a2c9b50fSJeremy L Thompson   ierr = CeedDestroy(&(*dm)->ceed);CHKERRQ_CEED(ierr);
787f918ec44SMatthew G. Knepley #endif
788435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
789732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
79047c6ae99SBarry Smith   PetscFunctionReturn(0);
79147c6ae99SBarry Smith }
79247c6ae99SBarry Smith 
793d7bf68aeSBarry Smith /*@
794d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
795d7bf68aeSBarry Smith 
796d083f849SBarry Smith     Collective on dm
797d7bf68aeSBarry Smith 
798d7bf68aeSBarry Smith     Input Parameter:
799d7bf68aeSBarry Smith .   dm - the DM object to setup
800d7bf68aeSBarry Smith 
801d7bf68aeSBarry Smith     Level: developer
802d7bf68aeSBarry Smith 
803e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
804d7bf68aeSBarry Smith 
805d7bf68aeSBarry Smith @*/
8067087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
807d7bf68aeSBarry Smith {
808d7bf68aeSBarry Smith   PetscErrorCode ierr;
809d7bf68aeSBarry Smith 
810d7bf68aeSBarry Smith   PetscFunctionBegin;
811171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8128387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
813d7bf68aeSBarry Smith   if (dm->ops->setup) {
814d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
815d7bf68aeSBarry Smith   }
8168387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
817d7bf68aeSBarry Smith   PetscFunctionReturn(0);
818d7bf68aeSBarry Smith }
819d7bf68aeSBarry Smith 
820d7bf68aeSBarry Smith /*@
821d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
822d7bf68aeSBarry Smith 
823d083f849SBarry Smith     Collective on dm
824d7bf68aeSBarry Smith 
825d7bf68aeSBarry Smith     Input Parameter:
826d7bf68aeSBarry Smith .   dm - the DM object to set options for
827d7bf68aeSBarry Smith 
828732e2eb9SMatthew G Knepley     Options Database:
8294164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8304164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8314164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
832a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
833a4ea9b21SRichard Tran Mills -   -dm_bind_below <n>   - bind (force execution on CPU) for Vec and Mat objects with local size (number of vector entries or matrix rows) below n; currently only supported for DMDA
834732e2eb9SMatthew G Knepley 
8359318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8369318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8379318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
838cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
8399318fe57SMatthew G. Knepley . -dm_plex_shape <shape>            - The domain shape, such as DM_SHAPE_BOX, DM_SHAPE_SPHERE, etc.
8409318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8419318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8429318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
8439318fe57SMatthew G. Knepley . -dm_plex_simplex <bool>           - PETSC_TRUE for simplex elements, PETSC_FALSE for tensor elements
8449318fe57SMatthew G. Knepley . -dm_plex_interpolate <bool>       - PETSC_TRUE turns on topological interpolation (creating edges and faces)
8459318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8469318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8479318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8489318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
8499318fe57SMatthew G. Knepley . -dm_plex_box_bd <bx,by,bz>        - Specify the DMBoundaryType for each direction
8509318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8519318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8529318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8539318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
854bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8559318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8569318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8579318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8589318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
859bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
860d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
861d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
862d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
863d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
864d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
865909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
866909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8679318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8689318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8699318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8709318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8719318fe57SMatthew G. Knepley 
872384a6580SVaclav Hapla     DMPLEX Specific Checks
873384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
874384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
875384a6580SVaclav 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()
876384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
877384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
878384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
879384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
880d7bf68aeSBarry Smith 
88195eb5ee5SVaclav Hapla     Level: intermediate
88295eb5ee5SVaclav Hapla 
88395eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
88495eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
885d7bf68aeSBarry Smith 
886d7bf68aeSBarry Smith @*/
8877087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
888d7bf68aeSBarry Smith {
8897781c08eSBarry Smith   char           typeName[256];
890ca266f36SBarry Smith   PetscBool      flg;
891d7bf68aeSBarry Smith   PetscErrorCode ierr;
892d7bf68aeSBarry Smith 
893d7bf68aeSBarry Smith   PetscFunctionBegin;
894171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
89549be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
89649be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8971bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8983194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8990298fd71SBarry 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);
900a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
901f9ba7244SBarry Smith   if (flg) {
902f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
903f9ba7244SBarry Smith   }
904a264d7a6SBarry 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);
905073dac72SJed Brown   if (flg) {
906521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
907073dac72SJed Brown   }
9088f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
909a4ea9b21SRichard Tran Mills   ierr = PetscOptionsInt("-dm_bind_below","Set the size threshold (in entries) below which the Vec is bound to the CPU","VecBindToCPU",dm->bind_below,&dm->bind_below,&flg);CHKERRQ(ierr);
910f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
911e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
912f9ba7244SBarry Smith   }
913f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
9140633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
91582fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
916d7bf68aeSBarry Smith   PetscFunctionReturn(0);
917d7bf68aeSBarry Smith }
918d7bf68aeSBarry Smith 
919fc9bc008SSatish Balay /*@C
920fe2efc57SMark    DMViewFromOptions - View from Options
921fe2efc57SMark 
922fe2efc57SMark    Collective on DM
923fe2efc57SMark 
924fe2efc57SMark    Input Parameters:
925fe2efc57SMark +  dm - the DM object
926736c3998SJose E. Roman .  obj - Optional object
927736c3998SJose E. Roman -  name - command line option
928fe2efc57SMark 
929fe2efc57SMark    Level: intermediate
930fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
931fe2efc57SMark @*/
932fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
933fe2efc57SMark {
934fe2efc57SMark   PetscErrorCode ierr;
935fe2efc57SMark 
936fe2efc57SMark   PetscFunctionBegin;
937fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
938fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
939fe2efc57SMark   PetscFunctionReturn(0);
940fe2efc57SMark }
941fe2efc57SMark 
942fe2efc57SMark /*@C
943224748a4SBarry Smith     DMView - Views a DM
94447c6ae99SBarry Smith 
945d083f849SBarry Smith     Collective on dm
94647c6ae99SBarry Smith 
947d8d19677SJose E. Roman     Input Parameters:
94847c6ae99SBarry Smith +   dm - the DM object to view
94947c6ae99SBarry Smith -   v - the viewer
95047c6ae99SBarry Smith 
951cd7e8a5eSksagiyam     Notes:
952cd7e8a5eSksagiyam     Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
953cd7e8a5eSksagiyam     meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
954cd7e8a5eSksagiyam     before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
955cd7e8a5eSksagiyam 
956224748a4SBarry Smith     Level: beginner
95747c6ae99SBarry Smith 
958cd7e8a5eSksagiyam .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMLoad(), PetscObjectSetName()
95947c6ae99SBarry Smith 
96047c6ae99SBarry Smith @*/
9617087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
96247c6ae99SBarry Smith {
96347c6ae99SBarry Smith   PetscErrorCode    ierr;
96432c0f0efSBarry Smith   PetscBool         isbinary;
96576a8abe0SBarry Smith   PetscMPIInt       size;
96676a8abe0SBarry Smith   PetscViewerFormat format;
96747c6ae99SBarry Smith 
96847c6ae99SBarry Smith   PetscFunctionBegin;
969171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9703014e516SBarry Smith   if (!v) {
971ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9723014e516SBarry Smith   }
973b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
97474903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
97574903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
97674903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97774903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97874903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
97974903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
98074903a4fSStefano Zampini      in an error here */
98174903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9828bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
983b1b135c8SBarry Smith 
98476a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
985ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRMPI(ierr);
98676a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
98798c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
98832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
98932c0f0efSBarry Smith   if (isbinary) {
99055849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
99132c0f0efSBarry Smith     char     type[256];
99232c0f0efSBarry Smith 
993f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
99432c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
995f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
99632c0f0efSBarry Smith   }
9970c010503SBarry Smith   if (dm->ops->view) {
9980c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
99947c6ae99SBarry Smith   }
100047c6ae99SBarry Smith   PetscFunctionReturn(0);
100147c6ae99SBarry Smith }
100247c6ae99SBarry Smith 
100347c6ae99SBarry Smith /*@
10048472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
100547c6ae99SBarry Smith 
1006d083f849SBarry Smith     Collective on dm
100747c6ae99SBarry Smith 
100847c6ae99SBarry Smith     Input Parameter:
100947c6ae99SBarry Smith .   dm - the DM object
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith     Output Parameter:
101247c6ae99SBarry Smith .   vec - the global vector
101347c6ae99SBarry Smith 
1014073dac72SJed Brown     Level: beginner
101547c6ae99SBarry Smith 
1016ec075b9fSPatrick Sanan .seealso DMCreateLocalVector(), DMGetGlobalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
101747c6ae99SBarry Smith 
101847c6ae99SBarry Smith @*/
10197087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
102047c6ae99SBarry Smith {
102147c6ae99SBarry Smith   PetscErrorCode ierr;
102247c6ae99SBarry Smith 
102347c6ae99SBarry Smith   PetscFunctionBegin;
1024171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1025b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1026*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->createglobalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
102747c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
102876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1029c6b011d8SStefano Zampini     DM vdm;
1030c6b011d8SStefano Zampini 
1031c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1032*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!vdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
1033c6b011d8SStefano Zampini   }
103447c6ae99SBarry Smith   PetscFunctionReturn(0);
103547c6ae99SBarry Smith }
103647c6ae99SBarry Smith 
103747c6ae99SBarry Smith /*@
10388472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
103947c6ae99SBarry Smith 
104047c6ae99SBarry Smith     Not Collective
104147c6ae99SBarry Smith 
104247c6ae99SBarry Smith     Input Parameter:
104347c6ae99SBarry Smith .   dm - the DM object
104447c6ae99SBarry Smith 
104547c6ae99SBarry Smith     Output Parameter:
104647c6ae99SBarry Smith .   vec - the local vector
104747c6ae99SBarry Smith 
1048073dac72SJed Brown     Level: beginner
104947c6ae99SBarry Smith 
1050ec075b9fSPatrick Sanan .seealso DMCreateGlobalVector(), DMGetLocalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
105147c6ae99SBarry Smith 
105247c6ae99SBarry Smith @*/
10537087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
105447c6ae99SBarry Smith {
105547c6ae99SBarry Smith   PetscErrorCode ierr;
105647c6ae99SBarry Smith 
105747c6ae99SBarry Smith   PetscFunctionBegin;
1058171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1059b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1060*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->createlocalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
106147c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
106276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1063c6b011d8SStefano Zampini     DM vdm;
1064c6b011d8SStefano Zampini 
1065c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1066*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!vdm,PETSC_COMM_SELF,PETSC_ERR_LIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
1067c6b011d8SStefano Zampini   }
106847c6ae99SBarry Smith   PetscFunctionReturn(0);
106947c6ae99SBarry Smith }
107047c6ae99SBarry Smith 
10711411c6eeSJed Brown /*@
10721411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10731411c6eeSJed Brown 
1074d083f849SBarry Smith    Collective on dm
10751411c6eeSJed Brown 
10761411c6eeSJed Brown    Input Parameter:
10771411c6eeSJed Brown .  dm - the DM that provides the mapping
10781411c6eeSJed Brown 
10791411c6eeSJed Brown    Output Parameter:
10801411c6eeSJed Brown .  ltog - the mapping
10811411c6eeSJed Brown 
10821411c6eeSJed Brown    Level: intermediate
10831411c6eeSJed Brown 
10841411c6eeSJed Brown    Notes:
10851411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10861411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10871411c6eeSJed Brown 
1088fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10891411c6eeSJed Brown @*/
10907087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10911411c6eeSJed Brown {
10920be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10931411c6eeSJed Brown   PetscErrorCode ierr;
10941411c6eeSJed Brown 
10951411c6eeSJed Brown   PetscFunctionBegin;
10961411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10971411c6eeSJed Brown   PetscValidPointer(ltog,2);
10981411c6eeSJed Brown   if (!dm->ltogmap) {
109937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
110037d0c07bSMatthew G Knepley 
110192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
110237d0c07bSMatthew G Knepley     if (section) {
1103a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
110437d0c07bSMatthew G Knepley       PetscInt       *ltog;
1105ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
110637d0c07bSMatthew G Knepley 
1107e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
110837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1109ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1110ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
111137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1112e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
111337d0c07bSMatthew G Knepley 
111437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
111537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1116a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1117a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
111837d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
11191a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11201a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
11211a7dc684SMatthew G. Knepley         if (dof) {
1122b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1123b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
11241a7dc684SMatthew G. Knepley         }
1125e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
1126e6befd46SJed Brown           if ((cind < cdof) && (c == cdofs[cind])) {
1127e6befd46SJed Brown             ltog[l] = off < 0 ? off-c : -(off+c+1);
1128e6befd46SJed Brown             cind++;
1129e6befd46SJed Brown           } else {
1130e6befd46SJed Brown             ltog[l] = (off < 0 ? -(off+1) : off) + c;
1131e6befd46SJed Brown           }
113237d0c07bSMatthew G Knepley         }
113337d0c07bSMatthew G Knepley       }
1134bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11350be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11360be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
11370be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11380be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
11397591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11407591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1141ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1142ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1143ccf3bd66SMatthew G. Knepley         n /= bs;
1144ccf3bd66SMatthew G. Knepley       }
1145ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
11463bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
114737d0c07bSMatthew G Knepley     } else {
1148*2c71b3e2SJacob Faibussowitsch       PetscCheckFalse(!dm->ops->getlocaltoglobalmapping,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1149184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
11501411c6eeSJed Brown     }
115137d0c07bSMatthew G Knepley   }
11521411c6eeSJed Brown   *ltog = dm->ltogmap;
11531411c6eeSJed Brown   PetscFunctionReturn(0);
11541411c6eeSJed Brown }
11551411c6eeSJed Brown 
11561411c6eeSJed Brown /*@
11571411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
11581411c6eeSJed Brown 
11591411c6eeSJed Brown    Not Collective
11601411c6eeSJed Brown 
11611411c6eeSJed Brown    Input Parameter:
11621411c6eeSJed Brown .  dm - the DM with block structure
11631411c6eeSJed Brown 
11641411c6eeSJed Brown    Output Parameter:
11651411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11661411c6eeSJed Brown 
11671411c6eeSJed Brown    Level: intermediate
11681411c6eeSJed Brown 
1169fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
11701411c6eeSJed Brown @*/
11717087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
11721411c6eeSJed Brown {
11731411c6eeSJed Brown   PetscFunctionBegin;
11741411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1175534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
1176*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(dm->bs < 1,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
11771411c6eeSJed Brown   *bs = dm->bs;
11781411c6eeSJed Brown   PetscFunctionReturn(0);
11791411c6eeSJed Brown }
11801411c6eeSJed Brown 
118148eeb7c8SBarry Smith /*@C
11828472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
118347c6ae99SBarry Smith 
1184a5bc1bf3SBarry Smith     Collective on dmc
118547c6ae99SBarry Smith 
1186d8d19677SJose E. Roman     Input Parameters:
1187a5bc1bf3SBarry Smith +   dmc - the DM object
1188a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
118947c6ae99SBarry Smith 
1190d8d19677SJose E. Roman     Output Parameters:
119147c6ae99SBarry Smith +  mat - the interpolation
119247c6ae99SBarry Smith -  vec - the scaling (optional)
119347c6ae99SBarry Smith 
119447c6ae99SBarry Smith     Level: developer
119547c6ae99SBarry Smith 
119695452b02SPatrick Sanan     Notes:
119795452b02SPatrick 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
119885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1199d52bd9f3SBarry Smith 
12001f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1201d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
120285afcc9aSBarry Smith 
12032ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
120447c6ae99SBarry Smith 
120547c6ae99SBarry Smith @*/
1206a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
120747c6ae99SBarry Smith {
120847c6ae99SBarry Smith   PetscErrorCode ierr;
120947c6ae99SBarry Smith 
121047c6ae99SBarry Smith   PetscFunctionBegin;
1211a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1212a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1213c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1214*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dmc->ops->createinterpolation,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1215a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1216a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1217a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
121847c6ae99SBarry Smith   PetscFunctionReturn(0);
121947c6ae99SBarry Smith }
122047c6ae99SBarry Smith 
12213ad4599aSBarry Smith /*@
1222d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
12232ed6491fSPatrick Sanan 
12242ed6491fSPatrick Sanan   Input Parameters:
12252ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
12262ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
12272ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12282ed6491fSPatrick Sanan 
12292ed6491fSPatrick Sanan   Output Parameter:
12302ed6491fSPatrick Sanan .    scale - the scaled vector
12312ed6491fSPatrick Sanan 
12322ed6491fSPatrick Sanan   Level: developer
12332ed6491fSPatrick Sanan 
1234e9c74fd6SRichard Tran Mills   Developer Notes:
1235e9c74fd6SRichard Tran Mills   If the fine-scale DMDA has the -dm_bind_below option set to true, then DMCreateInterpolationScale() calls MatSetBindingPropagates()
1236e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1237e9c74fd6SRichard Tran Mills 
12382ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
12392ed6491fSPatrick Sanan 
12402ed6491fSPatrick Sanan @*/
12412ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
12422ed6491fSPatrick Sanan {
12432ed6491fSPatrick Sanan   PetscErrorCode ierr;
12442ed6491fSPatrick Sanan   Vec            fine;
12452ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
12469704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1247e9c74fd6SRichard Tran Mills   PetscBool      bindingpropagates,isbound;
12489704db99SRichard Tran Mills #endif
12492ed6491fSPatrick Sanan 
12502ed6491fSPatrick Sanan   PetscFunctionBegin;
12512ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
12522ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
12532ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
12549704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12559704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12569704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12579704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
1258e9c74fd6SRichard Tran Mills   ierr = VecGetBindingPropagates(fine,&bindingpropagates);CHKERRQ(ierr);
1259e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
1260b1538989SRichard Tran Mills     ierr = MatSetBindingPropagates(mat,PETSC_TRUE);CHKERRQ(ierr);
1261e9c74fd6SRichard Tran Mills     ierr = VecBoundToCPU(fine,&isbound);CHKERRQ(ierr);
1262e9c74fd6SRichard Tran Mills     ierr = MatBindToCPU(mat,isbound);CHKERRQ(ierr);
126383aa49f4SRichard Tran Mills   }
12649704db99SRichard Tran Mills #endif
12652ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
12662ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
12672ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
12682ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12692ed6491fSPatrick Sanan }
12702ed6491fSPatrick Sanan 
12712ed6491fSPatrick Sanan /*@
12723ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
12733ad4599aSBarry Smith 
1274a5bc1bf3SBarry Smith     Collective on dmc
12753ad4599aSBarry Smith 
1276d8d19677SJose E. Roman     Input Parameters:
1277a5bc1bf3SBarry Smith +   dmc - the DM object
1278a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
12793ad4599aSBarry Smith 
12803ad4599aSBarry Smith     Output Parameter:
12813ad4599aSBarry Smith .  mat - the restriction
12823ad4599aSBarry Smith 
12833ad4599aSBarry Smith     Level: developer
12843ad4599aSBarry Smith 
128595452b02SPatrick Sanan     Notes:
128695452b02SPatrick 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
12873ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
12883ad4599aSBarry Smith 
12893ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
12903ad4599aSBarry Smith 
12913ad4599aSBarry Smith @*/
1292a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12933ad4599aSBarry Smith {
12943ad4599aSBarry Smith   PetscErrorCode ierr;
12953ad4599aSBarry Smith 
12963ad4599aSBarry Smith   PetscFunctionBegin;
1297a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1298a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12995a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1300*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dmc->ops->createrestriction,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1301a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1302a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1303a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
13043ad4599aSBarry Smith   PetscFunctionReturn(0);
13053ad4599aSBarry Smith }
13063ad4599aSBarry Smith 
130747c6ae99SBarry Smith /*@
13088472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
130947c6ae99SBarry Smith 
1310a5bc1bf3SBarry Smith     Collective on dac
131147c6ae99SBarry Smith 
1312d8d19677SJose E. Roman     Input Parameters:
1313a5bc1bf3SBarry Smith +   dac - the DM object
1314a5bc1bf3SBarry Smith -   daf - the second, finer DM object
131547c6ae99SBarry Smith 
131647c6ae99SBarry Smith     Output Parameter:
13176dbf9973SLawrence Mitchell .   mat - the injection
131847c6ae99SBarry Smith 
131947c6ae99SBarry Smith     Level: developer
132047c6ae99SBarry Smith 
132195452b02SPatrick Sanan    Notes:
132295452b02SPatrick 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
132385afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
132485afcc9aSBarry Smith 
1325e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
132647c6ae99SBarry Smith 
132747c6ae99SBarry Smith @*/
1328a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
132947c6ae99SBarry Smith {
133047c6ae99SBarry Smith   PetscErrorCode ierr;
133147c6ae99SBarry Smith 
133247c6ae99SBarry Smith   PetscFunctionBegin;
1333a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1334a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
13355a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1336*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dac->ops->createinjection,PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1337a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1338a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1339a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
134047c6ae99SBarry Smith   PetscFunctionReturn(0);
134147c6ae99SBarry Smith }
134247c6ae99SBarry Smith 
1343b412c318SBarry Smith /*@
1344bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1345bd041c0cSMatthew G. Knepley 
1346a5bc1bf3SBarry Smith   Collective on dac
1347bd041c0cSMatthew G. Knepley 
1348d8d19677SJose E. Roman   Input Parameters:
1349b4937a87SMatthew G. Knepley + dmc - the target DM object
1350b4937a87SMatthew G. Knepley - dmf - the source DM object
1351bd041c0cSMatthew G. Knepley 
1352bd041c0cSMatthew G. Knepley   Output Parameter:
1353b4937a87SMatthew G. Knepley . mat - the mass matrix
1354bd041c0cSMatthew G. Knepley 
1355bd041c0cSMatthew G. Knepley   Level: developer
1356bd041c0cSMatthew G. Knepley 
1357b4937a87SMatthew G. Knepley .seealso DMCreateMassMatrixLumped(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1358bd041c0cSMatthew G. Knepley @*/
1359b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1360bd041c0cSMatthew G. Knepley {
1361bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1362bd041c0cSMatthew G. Knepley 
1363bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1364b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1365b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13665a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1367*2c71b3e2SJacob Faibussowitsch   PetscCheck(dmc->ops->createmassmatrix,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dmc)->type_name);
1368b4937a87SMatthew G. Knepley   ierr = (*dmc->ops->createmassmatrix)(dmc, dmf, mat);CHKERRQ(ierr);
1369b4937a87SMatthew G. Knepley   PetscFunctionReturn(0);
1370b4937a87SMatthew G. Knepley }
1371b4937a87SMatthew G. Knepley 
1372b4937a87SMatthew G. Knepley /*@
1373b4937a87SMatthew G. Knepley   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given DM
1374b4937a87SMatthew G. Knepley 
1375b4937a87SMatthew G. Knepley   Collective on dm
1376b4937a87SMatthew G. Knepley 
1377b4937a87SMatthew G. Knepley   Input Parameter:
1378b4937a87SMatthew G. Knepley . dm - the DM object
1379b4937a87SMatthew G. Knepley 
1380b4937a87SMatthew G. Knepley   Output Parameter:
1381b4937a87SMatthew G. Knepley . lm - the lumped mass matrix
1382b4937a87SMatthew G. Knepley 
1383b4937a87SMatthew G. Knepley   Level: developer
1384b4937a87SMatthew G. Knepley 
1385b4937a87SMatthew G. Knepley .seealso DMCreateMassMatrix(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1386b4937a87SMatthew G. Knepley @*/
1387b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1388b4937a87SMatthew G. Knepley {
1389b4937a87SMatthew G. Knepley   PetscErrorCode ierr;
1390b4937a87SMatthew G. Knepley 
1391b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1392b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1393b4937a87SMatthew G. Knepley   PetscValidPointer(lm,2);
1394*2c71b3e2SJacob Faibussowitsch   PetscCheck(dm->ops->createmassmatrixlumped,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrixLumped",((PetscObject)dm)->type_name);
1395b4937a87SMatthew G. Knepley   ierr = (*dm->ops->createmassmatrixlumped)(dm, lm);CHKERRQ(ierr);
1396bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1397bd041c0cSMatthew G. Knepley }
1398bd041c0cSMatthew G. Knepley 
1399bd041c0cSMatthew G. Knepley /*@
1400b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
140147c6ae99SBarry Smith 
1402d083f849SBarry Smith     Collective on dm
140347c6ae99SBarry Smith 
1404d8d19677SJose E. Roman     Input Parameters:
140547c6ae99SBarry Smith +   dm - the DM object
14065bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
140747c6ae99SBarry Smith 
140847c6ae99SBarry Smith     Output Parameter:
140947c6ae99SBarry Smith .   coloring - the coloring
141047c6ae99SBarry Smith 
1411ec5066bdSBarry Smith     Notes:
1412ec5066bdSBarry 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
1413ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1414ec5066bdSBarry Smith 
1415ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1416ec5066bdSBarry Smith 
141747c6ae99SBarry Smith     Level: developer
141847c6ae99SBarry Smith 
1419ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
142047c6ae99SBarry Smith 
1421aab9d709SJed Brown @*/
1422b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
142347c6ae99SBarry Smith {
142447c6ae99SBarry Smith   PetscErrorCode ierr;
142547c6ae99SBarry Smith 
142647c6ae99SBarry Smith   PetscFunctionBegin;
1427171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14285a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1429*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->getcoloring,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1430b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
143147c6ae99SBarry Smith   PetscFunctionReturn(0);
143247c6ae99SBarry Smith }
143347c6ae99SBarry Smith 
1434b412c318SBarry Smith /*@
14358472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
143647c6ae99SBarry Smith 
1437d083f849SBarry Smith     Collective on dm
143847c6ae99SBarry Smith 
143947c6ae99SBarry Smith     Input Parameter:
1440b412c318SBarry Smith .   dm - the DM object
144147c6ae99SBarry Smith 
144247c6ae99SBarry Smith     Output Parameter:
144347c6ae99SBarry Smith .   mat - the empty Jacobian
144447c6ae99SBarry Smith 
1445073dac72SJed Brown     Level: beginner
144647c6ae99SBarry Smith 
1447f27dd7c6SMatthew G. Knepley     Options Database Keys:
1448f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1449f27dd7c6SMatthew G. Knepley 
145095452b02SPatrick Sanan     Notes:
145195452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
145294013140SBarry Smith        do not need to do it yourself.
145394013140SBarry Smith 
145494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
14557889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
145694013140SBarry Smith 
145794013140SBarry 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
145894013140SBarry Smith        internally by PETSc.
145994013140SBarry Smith 
146094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1461aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
146294013140SBarry Smith 
1463b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
146447c6ae99SBarry Smith 
1465aab9d709SJed Brown @*/
1466b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
146747c6ae99SBarry Smith {
146847c6ae99SBarry Smith   PetscErrorCode ierr;
146947c6ae99SBarry Smith 
147047c6ae99SBarry Smith   PetscFunctionBegin;
1471171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1472064a246eSJacob Faibussowitsch   PetscValidPointer(mat,2);
1473*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->creatematrix,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
14745a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1475fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1476b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
147776bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1478c6b011d8SStefano Zampini     DM mdm;
1479c6b011d8SStefano Zampini 
1480c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1481*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!mdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the matrix",((PetscObject)dm)->type_name);
1482c6b011d8SStefano Zampini   }
1483e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1484e5e52638SMatthew G. Knepley   if (dm->Nf) {
1485e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1486649ef022SMatthew Knepley     PetscInt     Nf, f;
1487e571a35bSMatthew G. Knepley 
1488e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1489649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1490649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
1491649ef022SMatthew Knepley         ierr = (*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1492e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1493e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1494649ef022SMatthew Knepley         break;
1495e571a35bSMatthew G. Knepley       }
1496649ef022SMatthew Knepley     }
1497649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1498649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
1499649ef022SMatthew Knepley         ierr = (*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1500e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1501e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1502e571a35bSMatthew G. Knepley       }
1503e571a35bSMatthew G. Knepley     }
1504e571a35bSMatthew G. Knepley   }
1505fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
150647c6ae99SBarry Smith   PetscFunctionReturn(0);
150747c6ae99SBarry Smith }
150847c6ae99SBarry Smith 
1509732e2eb9SMatthew G Knepley /*@
1510950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1511732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1512732e2eb9SMatthew G Knepley 
1513d083f849SBarry Smith   Logically Collective on dm
1514732e2eb9SMatthew G Knepley 
1515d8d19677SJose E. Roman   Input Parameters:
1516732e2eb9SMatthew G Knepley + dm - the DM
1517732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1518732e2eb9SMatthew G Knepley 
1519732e2eb9SMatthew G Knepley   Level: developer
1520f27dd7c6SMatthew G. Knepley 
1521f27dd7c6SMatthew G. Knepley   Options Database Keys:
1522f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1523f27dd7c6SMatthew G. Knepley 
1524b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1525732e2eb9SMatthew G Knepley @*/
1526732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1527732e2eb9SMatthew G Knepley {
1528732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1529732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1530732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1531732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1532732e2eb9SMatthew G Knepley }
1533732e2eb9SMatthew G Knepley 
1534b06ff27eSHong Zhang /*@
1535b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1536b06ff27eSHong Zhang     but the array for values will not be allocated.
1537b06ff27eSHong Zhang 
1538d083f849SBarry Smith   Logically Collective on dm
1539b06ff27eSHong Zhang 
1540d8d19677SJose E. Roman   Input Parameters:
1541b06ff27eSHong Zhang + dm - the DM
1542b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1543b06ff27eSHong Zhang 
1544b06ff27eSHong Zhang   Level: developer
1545b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1546b06ff27eSHong Zhang @*/
1547b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1548b06ff27eSHong Zhang {
1549b06ff27eSHong Zhang   PetscFunctionBegin;
1550b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1551b06ff27eSHong Zhang   dm->structure_only = only;
1552b06ff27eSHong Zhang   PetscFunctionReturn(0);
1553b06ff27eSHong Zhang }
1554b06ff27eSHong Zhang 
1555a89ea682SMatthew G Knepley /*@C
1556aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1557a89ea682SMatthew G Knepley 
1558a89ea682SMatthew G Knepley   Not Collective
1559a89ea682SMatthew G Knepley 
1560a89ea682SMatthew G Knepley   Input Parameters:
1561a89ea682SMatthew G Knepley + dm - the DM object
1562a5b23f4aSJose E. Roman . count - The minimum size
156369291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1564a89ea682SMatthew G Knepley 
1565a89ea682SMatthew G Knepley   Output Parameter:
1566a89ea682SMatthew G Knepley . array - the work array
1567a89ea682SMatthew G Knepley 
1568a89ea682SMatthew G Knepley   Level: developer
1569a89ea682SMatthew G Knepley 
1570a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1571a89ea682SMatthew G Knepley @*/
157269291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1573a89ea682SMatthew G Knepley {
1574a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1575aa1993deSMatthew G Knepley   DMWorkLink     link;
157669291d52SBarry Smith   PetscMPIInt    dsize;
1577a89ea682SMatthew G Knepley 
1578a89ea682SMatthew G Knepley   PetscFunctionBegin;
1579a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1580aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1581aa1993deSMatthew G Knepley   if (dm->workin) {
1582aa1993deSMatthew G Knepley     link       = dm->workin;
1583aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1584aa1993deSMatthew G Knepley   } else {
1585b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1586a89ea682SMatthew G Knepley   }
1587ffc4695bSBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRMPI(ierr);
15885056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1589aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1590854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1591854ce69bSBarry Smith     link->bytes = dsize*count;
1592aa1993deSMatthew G Knepley   }
1593aa1993deSMatthew G Knepley   link->next   = dm->workout;
1594aa1993deSMatthew G Knepley   dm->workout  = link;
159500d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
159600d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
159700d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
159800d952a4SJed Brown #endif
1599aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1600a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1601a89ea682SMatthew G Knepley }
1602a89ea682SMatthew G Knepley 
1603aa1993deSMatthew G Knepley /*@C
1604aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1605aa1993deSMatthew G Knepley 
1606aa1993deSMatthew G Knepley   Not Collective
1607aa1993deSMatthew G Knepley 
1608aa1993deSMatthew G Knepley   Input Parameters:
1609aa1993deSMatthew G Knepley + dm - the DM object
1610a5b23f4aSJose E. Roman . count - The minimum size
161169291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1612aa1993deSMatthew G Knepley 
1613aa1993deSMatthew G Knepley   Output Parameter:
1614aa1993deSMatthew G Knepley . array - the work array
1615aa1993deSMatthew G Knepley 
1616aa1993deSMatthew G Knepley   Level: developer
1617aa1993deSMatthew G Knepley 
161895452b02SPatrick Sanan   Developer Notes:
161995452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1620aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1621aa1993deSMatthew G Knepley @*/
162269291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1623aa1993deSMatthew G Knepley {
1624aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1625aa1993deSMatthew G Knepley 
1626aa1993deSMatthew G Knepley   PetscFunctionBegin;
1627aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1628aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1629aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1630aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1631aa1993deSMatthew G Knepley       *p           = link->next;
1632aa1993deSMatthew G Knepley       link->next   = dm->workin;
1633aa1993deSMatthew G Knepley       dm->workin   = link;
16340298fd71SBarry Smith       *(void**)mem = NULL;
1635aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1636aa1993deSMatthew G Knepley     }
1637aa1993deSMatthew G Knepley   }
1638aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1639aa1993deSMatthew G Knepley }
1640e7c4fc90SDmitry Karpeev 
16418cda7954SMatthew G. Knepley /*@C
16428cda7954SMatthew G. Knepley   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field
16438cda7954SMatthew G. Knepley 
16448cda7954SMatthew G. Knepley   Logically collective on DM
16458cda7954SMatthew G. Knepley 
16468cda7954SMatthew G. Knepley   Input Parameters:
16478cda7954SMatthew G. Knepley + dm     - The DM
16488cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16498cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
16508cda7954SMatthew G. Knepley 
16518cda7954SMatthew G. Knepley   Notes:
16528cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16538cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16548cda7954SMatthew G. Knepley $ dm        - The present DM
16558cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16568cda7954SMatthew G. Knepley $ field     - The field number in dm
16578cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16588cda7954SMatthew G. Knepley 
16598cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16608cda7954SMatthew G. Knepley 
16618cda7954SMatthew G. Knepley .seealso: DMGetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16628cda7954SMatthew G. Knepley */
16638cda7954SMatthew G. Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1664435a35e8SMatthew G Knepley {
1665435a35e8SMatthew G Knepley   PetscFunctionBegin;
1666435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1667*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(field >= 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1668435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1669435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1670435a35e8SMatthew G Knepley }
1671435a35e8SMatthew G Knepley 
16728cda7954SMatthew G. Knepley /*@C
16738cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
16748cda7954SMatthew G. Knepley 
16758cda7954SMatthew G. Knepley   Not collective
16768cda7954SMatthew G. Knepley 
16778cda7954SMatthew G. Knepley   Input Parameters:
16788cda7954SMatthew G. Knepley + dm     - The DM
16798cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16808cda7954SMatthew G. Knepley 
16818cda7954SMatthew G. Knepley   Output Parameter:
16828cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
16838cda7954SMatthew G. Knepley 
16848cda7954SMatthew G. Knepley   Notes:
16858cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16868cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16878cda7954SMatthew G. Knepley $ dm        - The present DM
16888cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16898cda7954SMatthew G. Knepley $ field     - The field number in dm
16908cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16918cda7954SMatthew G. Knepley 
16928cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16938cda7954SMatthew G. Knepley 
16948cda7954SMatthew G. Knepley .seealso: DMSetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16958cda7954SMatthew G. Knepley */
16968cda7954SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
16970a50eb56SMatthew G. Knepley {
16980a50eb56SMatthew G. Knepley   PetscFunctionBegin;
16990a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1700f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1701*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(field >= 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
17020a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
17030a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
17040a50eb56SMatthew G. Knepley }
17050a50eb56SMatthew G. Knepley 
17068cda7954SMatthew G. Knepley /*@C
17078cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
17088cda7954SMatthew G. Knepley 
17098cda7954SMatthew G. Knepley   Logically collective on DM
17108cda7954SMatthew G. Knepley 
17118cda7954SMatthew G. Knepley   Input Parameters:
17128cda7954SMatthew G. Knepley + dm     - The DM
17138cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17148cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
17158cda7954SMatthew G. Knepley 
17168cda7954SMatthew G. Knepley   Notes:
17178cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
17188cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
17198cda7954SMatthew G. Knepley $ dm        - The present DM
17208cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
17218cda7954SMatthew G. Knepley $ field     - The field number in dm
17228cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
17238cda7954SMatthew G. Knepley 
17248cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
17258cda7954SMatthew G. Knepley 
17268cda7954SMatthew G. Knepley .seealso: DMGetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
17278cda7954SMatthew G. Knepley */
17288cda7954SMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1729f9d4088aSMatthew G. Knepley {
1730f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1731f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1732*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(field >= 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1733f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1734f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1735f9d4088aSMatthew G. Knepley }
1736f9d4088aSMatthew G. Knepley 
17378cda7954SMatthew G. Knepley /*@C
17388cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
17398cda7954SMatthew G. Knepley 
17408cda7954SMatthew G. Knepley   Not collective
17418cda7954SMatthew G. Knepley 
17428cda7954SMatthew G. Knepley   Input Parameters:
17438cda7954SMatthew G. Knepley + dm     - The DM
17448cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17458cda7954SMatthew G. Knepley 
17468cda7954SMatthew G. Knepley   Output Parameter:
17478cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
17488cda7954SMatthew G. Knepley 
17498cda7954SMatthew G. Knepley   Notes:
17508cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
17518cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
17528cda7954SMatthew G. Knepley $ dm        - The present DM
17538cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
17548cda7954SMatthew G. Knepley $ field     - The field number in dm
17558cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
17568cda7954SMatthew G. Knepley 
17578cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
17588cda7954SMatthew G. Knepley 
17598cda7954SMatthew G. Knepley .seealso: DMSetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
17608cda7954SMatthew G. Knepley */
17618cda7954SMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1762f9d4088aSMatthew G. Knepley {
1763f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1764f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1765f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1766*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(field >= 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1767f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1768f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1769f9d4088aSMatthew G. Knepley }
1770f9d4088aSMatthew G. Knepley 
17714f3b5142SJed Brown /*@C
17724d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
17734d343eeaSMatthew G Knepley 
17744d343eeaSMatthew G Knepley   Not collective
17754d343eeaSMatthew G Knepley 
17764d343eeaSMatthew G Knepley   Input Parameter:
17774d343eeaSMatthew G Knepley . dm - the DM object
17784d343eeaSMatthew G Knepley 
17794d343eeaSMatthew G Knepley   Output Parameters:
17800298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
17810298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
17820298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
17834d343eeaSMatthew G Knepley 
17844d343eeaSMatthew G Knepley   Level: intermediate
17854d343eeaSMatthew G Knepley 
178621c9b008SJed Brown   Notes:
178721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
178821c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
178921c9b008SJed Brown   PetscFree().
179021c9b008SJed Brown 
17914d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
17924d343eeaSMatthew G Knepley @*/
179337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
17944d343eeaSMatthew G Knepley {
179537d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
17964d343eeaSMatthew G Knepley   PetscErrorCode ierr;
17974d343eeaSMatthew G Knepley 
17984d343eeaSMatthew G Knepley   PetscFunctionBegin;
17994d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
180069ca1f37SDmitry Karpeev   if (numFields) {
1801534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
180269ca1f37SDmitry Karpeev     *numFields = 0;
180369ca1f37SDmitry Karpeev   }
180437d0c07bSMatthew G Knepley   if (fieldNames) {
180537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
18060298fd71SBarry Smith     *fieldNames = NULL;
180769ca1f37SDmitry Karpeev   }
180869ca1f37SDmitry Karpeev   if (fields) {
180969ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
18100298fd71SBarry Smith     *fields = NULL;
181169ca1f37SDmitry Karpeev   }
181292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
181337d0c07bSMatthew G Knepley   if (section) {
18143a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
181537d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
181637d0c07bSMatthew G Knepley 
1817e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
181837d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
18193a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
182037d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
182137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
182237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
18233a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
182437d0c07bSMatthew G Knepley     }
182537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
182637d0c07bSMatthew G Knepley       PetscInt gdof;
182737d0c07bSMatthew G Knepley 
182837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
182937d0c07bSMatthew G Knepley       if (gdof > 0) {
183037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
18313a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
183237d0c07bSMatthew G Knepley 
183337d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
183437d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
18353a544194SStefano Zampini           fpdof = fdof-fcdof;
18363a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
18373a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
18383a544194SStefano Zampini             fieldNc[f] = 1;
18393a544194SStefano Zampini           }
18403a544194SStefano Zampini           fieldSizes[f] += fpdof;
184137d0c07bSMatthew G Knepley         }
184237d0c07bSMatthew G Knepley       }
184337d0c07bSMatthew G Knepley     }
184437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1845785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
184637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
184737d0c07bSMatthew G Knepley     }
184837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
184937d0c07bSMatthew G Knepley       PetscInt gdof, goff;
185037d0c07bSMatthew G Knepley 
185137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
185237d0c07bSMatthew G Knepley       if (gdof > 0) {
185337d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
185437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
185537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
185637d0c07bSMatthew G Knepley 
185737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
185837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
185937d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
186037d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
186137d0c07bSMatthew G Knepley           }
186237d0c07bSMatthew G Knepley         }
186337d0c07bSMatthew G Knepley       }
186437d0c07bSMatthew G Knepley     }
18658865f1eaSKarl Rupp     if (numFields) *numFields = nF;
186637d0c07bSMatthew G Knepley     if (fieldNames) {
1867785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
186837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
186937d0c07bSMatthew G Knepley         const char *fieldName;
187037d0c07bSMatthew G Knepley 
187137d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
187237d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
187337d0c07bSMatthew G Knepley       }
187437d0c07bSMatthew G Knepley     }
187537d0c07bSMatthew G Knepley     if (fields) {
1876785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
187737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
18783a544194SStefano Zampini         PetscInt bs, in[2], out[2];
18793a544194SStefano Zampini 
188082f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
18813a544194SStefano Zampini         in[0] = -fieldNc[f];
18823a544194SStefano Zampini         in[1] = fieldNc[f];
1883820f2d46SBarry Smith         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
18843a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
18853a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
188637d0c07bSMatthew G Knepley       }
188737d0c07bSMatthew G Knepley     }
18883a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
18898865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
18908865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
189169ca1f37SDmitry Karpeev   }
18924d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
18934d343eeaSMatthew G Knepley }
18944d343eeaSMatthew G Knepley 
189516621825SDmitry Karpeev /*@C
189616621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
189716621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
189816621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1899e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1900e7c4fc90SDmitry Karpeev 
1901e7c4fc90SDmitry Karpeev   Not collective
1902e7c4fc90SDmitry Karpeev 
1903e7c4fc90SDmitry Karpeev   Input Parameter:
1904e7c4fc90SDmitry Karpeev . dm - the DM object
1905e7c4fc90SDmitry Karpeev 
1906e7c4fc90SDmitry Karpeev   Output Parameters:
19070298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
19080298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
19090298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
19100298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1911e7c4fc90SDmitry Karpeev 
1912e7c4fc90SDmitry Karpeev   Level: intermediate
1913e7c4fc90SDmitry Karpeev 
1914e7c4fc90SDmitry Karpeev   Notes:
1915e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1916e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1917e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1918e7c4fc90SDmitry Karpeev 
1919e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1920e7c4fc90SDmitry Karpeev @*/
192116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1922e7c4fc90SDmitry Karpeev {
1923e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1924e7c4fc90SDmitry Karpeev 
1925e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1926e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19278865f1eaSKarl Rupp   if (len) {
1928534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
19298865f1eaSKarl Rupp     *len = 0;
19308865f1eaSKarl Rupp   }
19318865f1eaSKarl Rupp   if (namelist) {
19328865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1933ea78f98cSLisandro Dalcin     *namelist = NULL;
19348865f1eaSKarl Rupp   }
19358865f1eaSKarl Rupp   if (islist) {
19368865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1937ea78f98cSLisandro Dalcin     *islist = NULL;
19388865f1eaSKarl Rupp   }
19398865f1eaSKarl Rupp   if (dmlist) {
19408865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1941ea78f98cSLisandro Dalcin     *dmlist = NULL;
19428865f1eaSKarl Rupp   }
1943f3f0edfdSDmitry Karpeev   /*
1944f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1945f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1946f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1947f3f0edfdSDmitry Karpeev    */
1948*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
194916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1950435a35e8SMatthew G Knepley     PetscSection section;
1951435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1952435a35e8SMatthew G Knepley 
195392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1954435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1955435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1956f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
195703dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
195803dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
195903dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1960435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1961435a35e8SMatthew G Knepley         const char *fieldName;
1962435a35e8SMatthew G Knepley 
196303dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
196403dc3394SMatthew G. Knepley         if (namelist) {
1965435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1966435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1967435a35e8SMatthew G Knepley         }
196803dc3394SMatthew G. Knepley       }
1969435a35e8SMatthew G Knepley     } else {
197069ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1971e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
19720298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1973e7c4fc90SDmitry Karpeev     }
19748865f1eaSKarl Rupp   } else {
197516621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
197616621825SDmitry Karpeev   }
197716621825SDmitry Karpeev   PetscFunctionReturn(0);
197816621825SDmitry Karpeev }
197916621825SDmitry Karpeev 
1980564cec59SMatthew G. Knepley /*@
1981435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1982435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1983435a35e8SMatthew G Knepley 
1984435a35e8SMatthew G Knepley   Not collective
1985435a35e8SMatthew G Knepley 
1986435a35e8SMatthew G Knepley   Input Parameters:
19872adcc780SMatthew G. Knepley + dm        - The DM object
19882adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
19892adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1990435a35e8SMatthew G Knepley 
1991435a35e8SMatthew G Knepley   Output Parameters:
19922adcc780SMatthew G. Knepley + is - The global indices for the subproblem
19932adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1994435a35e8SMatthew G Knepley 
19955d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19965d3b26e6SMatthew G. Knepley 
1997435a35e8SMatthew G Knepley   Level: intermediate
1998435a35e8SMatthew G Knepley 
19995d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2000435a35e8SMatthew G Knepley @*/
200137bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2002435a35e8SMatthew G Knepley {
2003435a35e8SMatthew G Knepley   PetscErrorCode ierr;
2004435a35e8SMatthew G Knepley 
2005435a35e8SMatthew G Knepley   PetscFunctionBegin;
2006435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2007435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
20088865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
20098865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
2010*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->createsubdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
2011435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2012435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
2013435a35e8SMatthew G Knepley }
2014435a35e8SMatthew G Knepley 
20152adcc780SMatthew G. Knepley /*@C
20162adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
20172adcc780SMatthew G. Knepley 
20182adcc780SMatthew G. Knepley   Not collective
20192adcc780SMatthew G. Knepley 
2020d8d19677SJose E. Roman   Input Parameters:
20212adcc780SMatthew G. Knepley + dms - The DM objects
20222adcc780SMatthew G. Knepley - len - The number of DMs
20232adcc780SMatthew G. Knepley 
20242adcc780SMatthew G. Knepley   Output Parameters:
2025a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
20262adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
20272adcc780SMatthew G. Knepley 
20285d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
20295d3b26e6SMatthew G. Knepley 
20302adcc780SMatthew G. Knepley   Level: intermediate
20312adcc780SMatthew G. Knepley 
20325d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
20332adcc780SMatthew G. Knepley @*/
20342adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
20352adcc780SMatthew G. Knepley {
20362adcc780SMatthew G. Knepley   PetscInt       i;
20372adcc780SMatthew G. Knepley   PetscErrorCode ierr;
20382adcc780SMatthew G. Knepley 
20392adcc780SMatthew G. Knepley   PetscFunctionBegin;
20402adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
20412adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
20422adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
2043a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
2044*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(len < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
20452adcc780SMatthew G. Knepley   if (len) {
2046b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2047*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!dm->ops->createsuperdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
2048b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
20492adcc780SMatthew G. Knepley   }
20502adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
20512adcc780SMatthew G. Knepley }
20522adcc780SMatthew G. Knepley 
205316621825SDmitry Karpeev /*@C
20548d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
20558d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
20568d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
20578d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
20588d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
205916621825SDmitry Karpeev 
206016621825SDmitry Karpeev   Not collective
206116621825SDmitry Karpeev 
206216621825SDmitry Karpeev   Input Parameter:
206316621825SDmitry Karpeev . dm - the DM object
206416621825SDmitry Karpeev 
206516621825SDmitry Karpeev   Output Parameters:
20660298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
20670298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
20680298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
20690298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
20700298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
207116621825SDmitry Karpeev 
207216621825SDmitry Karpeev   Level: intermediate
207316621825SDmitry Karpeev 
207416621825SDmitry Karpeev   Notes:
207516621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
207616621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
207716621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
207816621825SDmitry Karpeev 
2079245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
208016621825SDmitry Karpeev @*/
20818d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
208216621825SDmitry Karpeev {
208316621825SDmitry Karpeev   PetscErrorCode      ierr;
2084be081cd6SPeter Brune   DMSubDomainHookLink link;
2085be081cd6SPeter Brune   PetscInt            i,l;
208616621825SDmitry Karpeev 
208716621825SDmitry Karpeev   PetscFunctionBegin;
208816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
208914a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
20900298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
20910298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
20920298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
20930298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
2094f3f0edfdSDmitry Karpeev   /*
2095f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2096f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2097f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2098f3f0edfdSDmitry Karpeev    */
2099*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
210016621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2101be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
210214a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2103f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2104be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2105be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
2106be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
2107be081cd6SPeter Brune         }
2108648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2109e7c4fc90SDmitry Karpeev       }
211014a18fd3SPeter Brune     }
211114a18fd3SPeter Brune     if (len) *len = l;
211214a18fd3SPeter Brune   }
2113e30e807fSPeter Brune   PetscFunctionReturn(0);
2114e30e807fSPeter Brune }
2115e30e807fSPeter Brune 
2116e30e807fSPeter Brune /*@C
2117e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2118e30e807fSPeter Brune 
2119e30e807fSPeter Brune   Not collective
2120e30e807fSPeter Brune 
2121e30e807fSPeter Brune   Input Parameters:
2122e30e807fSPeter Brune + dm - the DM object
2123e30e807fSPeter Brune . n  - the number of subdomain scatters
2124e30e807fSPeter Brune - subdms - the local subdomains
2125e30e807fSPeter Brune 
2126e30e807fSPeter Brune   Output Parameters:
21276b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2128e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2129e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2130e30e807fSPeter Brune 
213195452b02SPatrick Sanan   Notes:
213295452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2133e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2134e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2135e30e807fSPeter Brune   solution and residual data.
2136e30e807fSPeter Brune 
2137e30e807fSPeter Brune   Level: developer
2138e30e807fSPeter Brune 
2139e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2140e30e807fSPeter Brune @*/
2141e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2142e30e807fSPeter Brune {
2143e30e807fSPeter Brune   PetscErrorCode ierr;
2144e30e807fSPeter Brune 
2145e30e807fSPeter Brune   PetscFunctionBegin;
2146e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2147e30e807fSPeter Brune   PetscValidPointer(subdms,3);
2148*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->createddscatters,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
2149e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
2150e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2151e7c4fc90SDmitry Karpeev }
2152e7c4fc90SDmitry Karpeev 
215347c6ae99SBarry Smith /*@
215447c6ae99SBarry Smith   DMRefine - Refines a DM object
215547c6ae99SBarry Smith 
2156d083f849SBarry Smith   Collective on dm
215747c6ae99SBarry Smith 
2158d8d19677SJose E. Roman   Input Parameters:
215947c6ae99SBarry Smith + dm   - the DM object
216091d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
216147c6ae99SBarry Smith 
216247c6ae99SBarry Smith   Output Parameter:
21630298fd71SBarry Smith . dmf - the refined DM, or NULL
2164ae0a1c52SMatthew G Knepley 
2165f27dd7c6SMatthew G. Knepley   Options Database Keys:
2166412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2167412e9a14SMatthew G. Knepley 
21680298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
216947c6ae99SBarry Smith 
217047c6ae99SBarry Smith   Level: developer
217147c6ae99SBarry Smith 
2172e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
217347c6ae99SBarry Smith @*/
21747087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
217547c6ae99SBarry Smith {
217647c6ae99SBarry Smith   PetscErrorCode   ierr;
2177c833c3b5SJed Brown   DMRefineHookLink link;
217847c6ae99SBarry Smith 
217947c6ae99SBarry Smith   PetscFunctionBegin;
2180732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2181*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->refine,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
21821ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
218347c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
21844057135bSMatthew G Knepley   if (*dmf) {
218543842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
21868865f1eaSKarl Rupp 
21878cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
21888865f1eaSKarl Rupp 
2189644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
21900598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2191656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
21928865f1eaSKarl Rupp 
2193e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
2194c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
21958865f1eaSKarl Rupp       if (link->refinehook) {
21968865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
21978865f1eaSKarl Rupp       }
2198c833c3b5SJed Brown     }
2199c833c3b5SJed Brown   }
22001ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
2201c833c3b5SJed Brown   PetscFunctionReturn(0);
2202c833c3b5SJed Brown }
2203c833c3b5SJed Brown 
2204bb9467b5SJed Brown /*@C
2205c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2206c833c3b5SJed Brown 
2207c833c3b5SJed Brown    Logically Collective
2208c833c3b5SJed Brown 
22094165533cSJose E. Roman    Input Parameters:
2210c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2211c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2212c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
22130298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2214c833c3b5SJed Brown 
2215c833c3b5SJed Brown    Calling sequence of refinehook:
2216c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2217c833c3b5SJed Brown 
2218c833c3b5SJed Brown +  coarse - coarse level DM
2219c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2220c833c3b5SJed Brown -  ctx - optional user-defined function context
2221c833c3b5SJed Brown 
2222c833c3b5SJed Brown    Calling sequence for interphook:
2223c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2224c833c3b5SJed Brown 
2225c833c3b5SJed Brown +  coarse - coarse level DM
2226c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2227c833c3b5SJed Brown .  fine - fine level DM to update
2228c833c3b5SJed Brown -  ctx - optional user-defined function context
2229c833c3b5SJed Brown 
2230c833c3b5SJed Brown    Level: advanced
2231c833c3b5SJed Brown 
2232c833c3b5SJed Brown    Notes:
2233c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2234c833c3b5SJed Brown 
2235c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2236c833c3b5SJed Brown 
2237bb9467b5SJed Brown    This function is currently not available from Fortran.
2238bb9467b5SJed Brown 
2239c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2240c833c3b5SJed Brown @*/
2241c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2242c833c3b5SJed Brown {
2243c833c3b5SJed Brown   PetscErrorCode   ierr;
2244c833c3b5SJed Brown   DMRefineHookLink link,*p;
2245c833c3b5SJed Brown 
2246c833c3b5SJed Brown   PetscFunctionBegin;
2247c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22483d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
22493d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
22503d8e3701SJed Brown   }
225195dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2252c833c3b5SJed Brown   link->refinehook = refinehook;
2253c833c3b5SJed Brown   link->interphook = interphook;
2254c833c3b5SJed Brown   link->ctx        = ctx;
22550298fd71SBarry Smith   link->next       = NULL;
2256c833c3b5SJed Brown   *p               = link;
2257c833c3b5SJed Brown   PetscFunctionReturn(0);
2258c833c3b5SJed Brown }
2259c833c3b5SJed Brown 
22603d8e3701SJed Brown /*@C
22613d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
22623d8e3701SJed Brown 
22633d8e3701SJed Brown    Logically Collective
22643d8e3701SJed Brown 
22654165533cSJose E. Roman    Input Parameters:
22663d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
22673d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
22683d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
22693d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22703d8e3701SJed Brown 
22713d8e3701SJed Brown    Level: advanced
22723d8e3701SJed Brown 
22733d8e3701SJed Brown    Notes:
22743d8e3701SJed Brown    This function does nothing if the hook is not in the list.
22753d8e3701SJed Brown 
22763d8e3701SJed Brown    This function is currently not available from Fortran.
22773d8e3701SJed Brown 
22783d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
22793d8e3701SJed Brown @*/
22803d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
22813d8e3701SJed Brown {
22823d8e3701SJed Brown   PetscErrorCode   ierr;
22833d8e3701SJed Brown   DMRefineHookLink link,*p;
22843d8e3701SJed Brown 
22853d8e3701SJed Brown   PetscFunctionBegin;
22863d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22873d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
22883d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
22893d8e3701SJed Brown       link = *p;
22903d8e3701SJed Brown       *p = link->next;
22913d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
22923d8e3701SJed Brown       break;
22933d8e3701SJed Brown     }
22943d8e3701SJed Brown   }
22953d8e3701SJed Brown   PetscFunctionReturn(0);
22963d8e3701SJed Brown }
22973d8e3701SJed Brown 
2298c833c3b5SJed Brown /*@
2299c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2300c833c3b5SJed Brown 
2301c833c3b5SJed Brown    Collective if any hooks are
2302c833c3b5SJed Brown 
23034165533cSJose E. Roman    Input Parameters:
2304c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2305e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2306c833c3b5SJed Brown -  fine - finer DM to update
2307c833c3b5SJed Brown 
2308c833c3b5SJed Brown    Level: developer
2309c833c3b5SJed Brown 
2310c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2311c833c3b5SJed Brown @*/
2312c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2313c833c3b5SJed Brown {
2314c833c3b5SJed Brown   PetscErrorCode   ierr;
2315c833c3b5SJed Brown   DMRefineHookLink link;
2316c833c3b5SJed Brown 
2317c833c3b5SJed Brown   PetscFunctionBegin;
2318c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
23198865f1eaSKarl Rupp     if (link->interphook) {
23208865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
23218865f1eaSKarl Rupp     }
23224057135bSMatthew G Knepley   }
232347c6ae99SBarry Smith   PetscFunctionReturn(0);
232447c6ae99SBarry Smith }
232547c6ae99SBarry Smith 
2326eb3f98d2SBarry Smith /*@
23271f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
23281f3379b2SToby Isaac 
23291f3379b2SToby Isaac    Collective on DM
23301f3379b2SToby Isaac 
23314165533cSJose E. Roman    Input Parameters:
23321f3379b2SToby Isaac +  coarse - coarse DM
23331f3379b2SToby Isaac .  fine   - fine DM
23341f3379b2SToby Isaac .  interp - (optional) the matrix computed by DMCreateInterpolation().  Implementations may not need this, but if it
23351f3379b2SToby Isaac             is available it can avoid some recomputation.  If it is provided, MatInterpolate() will be used if
23361f3379b2SToby Isaac             the coarse DM does not have a specialized implementation.
23371f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
23381f3379b2SToby Isaac 
23394165533cSJose E. Roman    Output Parameter:
23401f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
23411f3379b2SToby Isaac 
23421f3379b2SToby Isaac    Level: developer
23431f3379b2SToby Isaac 
23441f3379b2SToby Isaac    Note: This function exists because the interpolation of a solution vector between meshes is not always a linear
23451f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
23461f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
23471f3379b2SToby Isaac    slope-limiting reconstruction.
23481f3379b2SToby Isaac 
23491f3379b2SToby Isaac .seealso DMInterpolate(), DMCreateInterpolation()
23501f3379b2SToby Isaac @*/
23511f3379b2SToby Isaac PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
23521f3379b2SToby Isaac {
23531f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM,DM,Mat,Vec,Vec) = NULL;
23541f3379b2SToby Isaac   PetscErrorCode ierr;
23551f3379b2SToby Isaac 
23561f3379b2SToby Isaac   PetscFunctionBegin;
23571f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
23581f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp,MAT_CLASSID,3);
23591f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol,VEC_CLASSID,4);
23601f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol,VEC_CLASSID,5);
23611f3379b2SToby Isaac 
23621f3379b2SToby Isaac   ierr = PetscObjectQueryFunction((PetscObject)coarse,"DMInterpolateSolution_C", &interpsol);CHKERRQ(ierr);
23631f3379b2SToby Isaac   if (interpsol) {
23641f3379b2SToby Isaac     ierr = (*interpsol)(coarse, fine, interp, coarseSol, fineSol);CHKERRQ(ierr);
23651f3379b2SToby Isaac   } else if (interp) {
23661f3379b2SToby Isaac     ierr = MatInterpolate(interp, coarseSol, fineSol);CHKERRQ(ierr);
236798921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
23681f3379b2SToby Isaac   PetscFunctionReturn(0);
23691f3379b2SToby Isaac }
23701f3379b2SToby Isaac 
23711f3379b2SToby Isaac /*@
2372aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2373eb3f98d2SBarry Smith 
2374eb3f98d2SBarry Smith     Not Collective
2375eb3f98d2SBarry Smith 
2376eb3f98d2SBarry Smith     Input Parameter:
2377eb3f98d2SBarry Smith .   dm - the DM object
2378eb3f98d2SBarry Smith 
2379eb3f98d2SBarry Smith     Output Parameter:
2380eb3f98d2SBarry Smith .   level - number of refinements
2381eb3f98d2SBarry Smith 
2382eb3f98d2SBarry Smith     Level: developer
2383eb3f98d2SBarry Smith 
23846a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2385eb3f98d2SBarry Smith 
2386eb3f98d2SBarry Smith @*/
2387eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2388eb3f98d2SBarry Smith {
2389eb3f98d2SBarry Smith   PetscFunctionBegin;
2390eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2391eb3f98d2SBarry Smith   *level = dm->levelup;
2392eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2393eb3f98d2SBarry Smith }
2394eb3f98d2SBarry Smith 
2395fef3a512SBarry Smith /*@
2396aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2397fef3a512SBarry Smith 
2398fef3a512SBarry Smith     Not Collective
2399fef3a512SBarry Smith 
2400d8d19677SJose E. Roman     Input Parameters:
2401fef3a512SBarry Smith +   dm - the DM object
2402fef3a512SBarry Smith -   level - number of refinements
2403fef3a512SBarry Smith 
2404fef3a512SBarry Smith     Level: advanced
2405fef3a512SBarry Smith 
240695452b02SPatrick Sanan     Notes:
240795452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2408fef3a512SBarry Smith 
2409fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2410fef3a512SBarry Smith 
2411fef3a512SBarry Smith @*/
2412fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2413fef3a512SBarry Smith {
2414fef3a512SBarry Smith   PetscFunctionBegin;
2415fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2416fef3a512SBarry Smith   dm->levelup = level;
2417fef3a512SBarry Smith   PetscFunctionReturn(0);
2418fef3a512SBarry Smith }
2419fef3a512SBarry Smith 
2420d410b0cfSMatthew G. Knepley /*@
2421d410b0cfSMatthew G. Knepley   DMExtrude - Extrude a DM object from a surface
2422d410b0cfSMatthew G. Knepley 
2423d410b0cfSMatthew G. Knepley   Collective on dm
2424d410b0cfSMatthew G. Knepley 
2425d410b0cfSMatthew G. Knepley   Input Parameter:
2426d410b0cfSMatthew G. Knepley + dm     - the DM object
2427d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2428d410b0cfSMatthew G. Knepley 
2429d410b0cfSMatthew G. Knepley   Output Parameter:
2430d410b0cfSMatthew G. Knepley . dme - the extruded DM, or NULL
2431d410b0cfSMatthew G. Knepley 
2432d410b0cfSMatthew G. Knepley   Note: If no extrusion was done, the return value is NULL
2433d410b0cfSMatthew G. Knepley 
2434d410b0cfSMatthew G. Knepley   Level: developer
2435d410b0cfSMatthew G. Knepley 
2436d410b0cfSMatthew G. Knepley .seealso DMRefine(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector()
2437d410b0cfSMatthew G. Knepley @*/
2438d410b0cfSMatthew G. Knepley PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2439d410b0cfSMatthew G. Knepley {
2440d410b0cfSMatthew G. Knepley   PetscErrorCode ierr;
2441d410b0cfSMatthew G. Knepley 
2442d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2443d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2444*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->extrude,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "DM type %s does not implement DMExtrude", ((PetscObject) dm)->type_name);
2445d410b0cfSMatthew G. Knepley   ierr = (*dm->ops->extrude)(dm, layers, dme);CHKERRQ(ierr);
2446d410b0cfSMatthew G. Knepley   if (*dme) {
2447d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
2448d410b0cfSMatthew G. Knepley     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject) dm, (PetscObject) *dme);CHKERRQ(ierr);
2449d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
2450d410b0cfSMatthew G. Knepley     ierr = DMSetMatType(*dme, dm->mattype);CHKERRQ(ierr);
2451d410b0cfSMatthew G. Knepley   }
2452d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2453d410b0cfSMatthew G. Knepley }
2454d410b0cfSMatthew G. Knepley 
2455ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2456ca3d3a14SMatthew G. Knepley {
2457ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2458ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2459ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2460ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2461ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2462ca3d3a14SMatthew G. Knepley }
2463ca3d3a14SMatthew G. Knepley 
2464ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2465ca3d3a14SMatthew G. Knepley {
2466ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2467ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2468ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2469ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2470ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2471ca3d3a14SMatthew G. Knepley }
2472ca3d3a14SMatthew G. Knepley 
2473ca3d3a14SMatthew G. Knepley /*@
2474c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2475ca3d3a14SMatthew G. Knepley 
2476ca3d3a14SMatthew G. Knepley   Input Parameter:
2477ca3d3a14SMatthew G. Knepley . dm - The DM
2478ca3d3a14SMatthew G. Knepley 
2479ca3d3a14SMatthew G. Knepley   Output Parameter:
2480ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2481ca3d3a14SMatthew G. Knepley 
2482ca3d3a14SMatthew G. Knepley   Level: developer
2483ca3d3a14SMatthew G. Knepley 
2484436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2485ca3d3a14SMatthew G. Knepley @*/
2486ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2487ca3d3a14SMatthew G. Knepley {
2488ca3d3a14SMatthew G. Knepley   Vec            tv;
2489ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2490ca3d3a14SMatthew G. Knepley 
2491ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2492ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2493534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2494ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2495ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2496ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2497ca3d3a14SMatthew G. Knepley }
2498ca3d3a14SMatthew G. Knepley 
2499ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2500ca3d3a14SMatthew G. Knepley {
2501ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2502ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2503ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2504ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2505ca3d3a14SMatthew G. Knepley 
2506ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2507ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
250892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2509ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2510ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2511ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
251292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2513ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2514ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2515ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2516ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2517ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2518ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2519ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2520ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2521ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2522ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2523ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2524ca3d3a14SMatthew G. Knepley     }
2525ca3d3a14SMatthew G. Knepley   }
2526ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2527ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2528ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2529ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2530ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2531ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2532ca3d3a14SMatthew G. Knepley       if (dof) {
2533ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2534ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2535ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2536ca3d3a14SMatthew G. Knepley 
2537ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2538ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2539ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2540580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2541ca3d3a14SMatthew G. Knepley       }
2542ca3d3a14SMatthew G. Knepley     }
2543ca3d3a14SMatthew G. Knepley   }
2544ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2545ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2546ca3d3a14SMatthew G. Knepley }
2547ca3d3a14SMatthew G. Knepley 
2548ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2549ca3d3a14SMatthew G. Knepley {
2550ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2551ca3d3a14SMatthew G. Knepley 
2552ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2553ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2554ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2555ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2556ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2557ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2558ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2559ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2560ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2561ca3d3a14SMatthew G. Knepley }
2562ca3d3a14SMatthew G. Knepley 
2563bb9467b5SJed Brown /*@C
2564baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2565baf369e7SPeter Brune 
2566baf369e7SPeter Brune    Logically Collective
2567baf369e7SPeter Brune 
25684165533cSJose E. Roman    Input Parameters:
2569baf369e7SPeter Brune +  dm - the DM
2570baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2571baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
25720298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2573baf369e7SPeter Brune 
2574baf369e7SPeter Brune    Calling sequence for beginhook:
2575baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2576baf369e7SPeter Brune 
2577baf369e7SPeter Brune +  dm - global DM
2578baf369e7SPeter Brune .  g - global vector
2579baf369e7SPeter Brune .  mode - mode
2580baf369e7SPeter Brune .  l - local vector
2581baf369e7SPeter Brune -  ctx - optional user-defined function context
2582baf369e7SPeter Brune 
2583baf369e7SPeter Brune    Calling sequence for endhook:
2584ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2585baf369e7SPeter Brune 
2586baf369e7SPeter Brune +  global - global DM
2587baf369e7SPeter Brune -  ctx - optional user-defined function context
2588baf369e7SPeter Brune 
2589baf369e7SPeter Brune    Level: advanced
2590baf369e7SPeter Brune 
2591baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2592baf369e7SPeter Brune @*/
2593baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2594baf369e7SPeter Brune {
2595baf369e7SPeter Brune   PetscErrorCode          ierr;
2596baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2597baf369e7SPeter Brune 
2598baf369e7SPeter Brune   PetscFunctionBegin;
2599baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2600baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
260195dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2602baf369e7SPeter Brune   link->beginhook = beginhook;
2603baf369e7SPeter Brune   link->endhook   = endhook;
2604baf369e7SPeter Brune   link->ctx       = ctx;
26050298fd71SBarry Smith   link->next      = NULL;
2606baf369e7SPeter Brune   *p              = link;
2607baf369e7SPeter Brune   PetscFunctionReturn(0);
2608baf369e7SPeter Brune }
2609baf369e7SPeter Brune 
26104c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
26114c274da1SToby Isaac {
26124c274da1SToby Isaac   Mat cMat;
26134c274da1SToby Isaac   Vec cVec;
26144c274da1SToby Isaac   PetscSection section, cSec;
26154c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
26164c274da1SToby Isaac   PetscErrorCode ierr;
26174c274da1SToby Isaac 
26184c274da1SToby Isaac   PetscFunctionBegin;
26194c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26204c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
26214c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
26225db9a05bSToby Isaac     PetscInt nRows;
26235db9a05bSToby Isaac 
26245db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
26255db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
262692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
26277711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
26284c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
26294c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
26304c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
26314c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
26324c274da1SToby Isaac       if (dof) {
26334c274da1SToby Isaac         PetscScalar *vals;
26344c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
26354c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
26364c274da1SToby Isaac       }
26374c274da1SToby Isaac     }
26384c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
26394c274da1SToby Isaac   }
26404c274da1SToby Isaac   PetscFunctionReturn(0);
26414c274da1SToby Isaac }
26424c274da1SToby Isaac 
264347c6ae99SBarry Smith /*@
264401729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
264501729b5cSPatrick Sanan 
2646d083f849SBarry Smith     Neighbor-wise Collective on dm
264701729b5cSPatrick Sanan 
264801729b5cSPatrick Sanan     Input Parameters:
264901729b5cSPatrick Sanan +   dm - the DM object
265001729b5cSPatrick Sanan .   g - the global vector
265101729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
265201729b5cSPatrick Sanan -   l - the local vector
265301729b5cSPatrick Sanan 
265401729b5cSPatrick Sanan     Notes:
265501729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
265601729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
265701729b5cSPatrick Sanan 
265801729b5cSPatrick Sanan     Level: beginner
265901729b5cSPatrick Sanan 
266001729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
266101729b5cSPatrick Sanan 
266201729b5cSPatrick Sanan @*/
266301729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
266401729b5cSPatrick Sanan {
266501729b5cSPatrick Sanan   PetscErrorCode ierr;
266601729b5cSPatrick Sanan 
266701729b5cSPatrick Sanan   PetscFunctionBegin;
266801729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
266901729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
267001729b5cSPatrick Sanan   PetscFunctionReturn(0);
267101729b5cSPatrick Sanan }
267201729b5cSPatrick Sanan 
267301729b5cSPatrick Sanan /*@
267447c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
267547c6ae99SBarry Smith 
2676d083f849SBarry Smith     Neighbor-wise Collective on dm
267747c6ae99SBarry Smith 
267847c6ae99SBarry Smith     Input Parameters:
267947c6ae99SBarry Smith +   dm - the DM object
268047c6ae99SBarry Smith .   g - the global vector
268147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
268247c6ae99SBarry Smith -   l - the local vector
268347c6ae99SBarry Smith 
268401729b5cSPatrick Sanan     Level: intermediate
268547c6ae99SBarry Smith 
268601729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
268747c6ae99SBarry Smith 
268847c6ae99SBarry Smith @*/
26897087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
269047c6ae99SBarry Smith {
26917128ae9fSMatthew G Knepley   PetscSF                 sf;
269247c6ae99SBarry Smith   PetscErrorCode          ierr;
2693baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
269447c6ae99SBarry Smith 
269547c6ae99SBarry Smith   PetscFunctionBegin;
2696171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2697baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
26988865f1eaSKarl Rupp     if (link->beginhook) {
26998865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
27008865f1eaSKarl Rupp     }
2701baf369e7SPeter Brune   }
27021bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
27037128ae9fSMatthew G Knepley   if (sf) {
2704ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2705ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2706d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
27077128ae9fSMatthew G Knepley 
2708*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(mode == ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2709a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2710a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2711ad227feaSJunchao Zhang     ierr = PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE);CHKERRQ(ierr);
2712a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2713a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
27147128ae9fSMatthew G Knepley   } else {
2715*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!dm->ops->globaltolocalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2716843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
27177128ae9fSMatthew G Knepley   }
271847c6ae99SBarry Smith   PetscFunctionReturn(0);
271947c6ae99SBarry Smith }
272047c6ae99SBarry Smith 
272147c6ae99SBarry Smith /*@
272247c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
272347c6ae99SBarry Smith 
2724d083f849SBarry Smith     Neighbor-wise Collective on dm
272547c6ae99SBarry Smith 
272647c6ae99SBarry Smith     Input Parameters:
272747c6ae99SBarry Smith +   dm - the DM object
272847c6ae99SBarry Smith .   g - the global vector
272947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
273047c6ae99SBarry Smith -   l - the local vector
273147c6ae99SBarry Smith 
273201729b5cSPatrick Sanan     Level: intermediate
273347c6ae99SBarry Smith 
273401729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
273547c6ae99SBarry Smith 
273647c6ae99SBarry Smith @*/
27377087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
273847c6ae99SBarry Smith {
27397128ae9fSMatthew G Knepley   PetscSF                 sf;
274047c6ae99SBarry Smith   PetscErrorCode          ierr;
2741ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2742ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2743ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2744baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2745d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
274647c6ae99SBarry Smith 
274747c6ae99SBarry Smith   PetscFunctionBegin;
2748171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27491bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2750ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
27517128ae9fSMatthew G Knepley   if (sf) {
2752*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(mode == ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
27537128ae9fSMatthew G Knepley 
2754a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2755a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2756ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray,MPI_REPLACE);CHKERRQ(ierr);
2757a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2758a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
2759ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
27607128ae9fSMatthew G Knepley   } else {
2761*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!dm->ops->globaltolocalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2762843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
27637128ae9fSMatthew G Knepley   }
27644c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2765baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2766baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2767baf369e7SPeter Brune   }
276847c6ae99SBarry Smith   PetscFunctionReturn(0);
276947c6ae99SBarry Smith }
277047c6ae99SBarry Smith 
2771d4d07f1eSToby Isaac /*@C
2772d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2773d4d07f1eSToby Isaac 
2774d4d07f1eSToby Isaac    Logically Collective
2775d4d07f1eSToby Isaac 
27764165533cSJose E. Roman    Input Parameters:
2777d4d07f1eSToby Isaac +  dm - the DM
2778d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2779d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2780d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2781d4d07f1eSToby Isaac 
2782d4d07f1eSToby Isaac    Calling sequence for beginhook:
2783d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2784d4d07f1eSToby Isaac 
2785d4d07f1eSToby Isaac +  dm - global DM
2786d4d07f1eSToby Isaac .  l - local vector
2787d4d07f1eSToby Isaac .  mode - mode
2788d4d07f1eSToby Isaac .  g - global vector
2789d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2790d4d07f1eSToby Isaac 
2791d4d07f1eSToby Isaac    Calling sequence for endhook:
2792d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2793d4d07f1eSToby Isaac 
2794d4d07f1eSToby Isaac +  global - global DM
2795d4d07f1eSToby Isaac .  l - local vector
2796d4d07f1eSToby Isaac .  mode - mode
2797d4d07f1eSToby Isaac .  g - global vector
2798d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2799d4d07f1eSToby Isaac 
2800d4d07f1eSToby Isaac    Level: advanced
2801d4d07f1eSToby Isaac 
2802d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2803d4d07f1eSToby Isaac @*/
2804d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2805d4d07f1eSToby Isaac {
2806d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2807d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2808d4d07f1eSToby Isaac 
2809d4d07f1eSToby Isaac   PetscFunctionBegin;
2810d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2811d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
281295dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2813d4d07f1eSToby Isaac   link->beginhook = beginhook;
2814d4d07f1eSToby Isaac   link->endhook   = endhook;
2815d4d07f1eSToby Isaac   link->ctx       = ctx;
2816d4d07f1eSToby Isaac   link->next      = NULL;
2817d4d07f1eSToby Isaac   *p              = link;
2818d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2819d4d07f1eSToby Isaac }
2820d4d07f1eSToby Isaac 
28214c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
28224c274da1SToby Isaac {
28234c274da1SToby Isaac   Mat cMat;
28244c274da1SToby Isaac   Vec cVec;
28254c274da1SToby Isaac   PetscSection section, cSec;
28264c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
28274c274da1SToby Isaac   PetscErrorCode ierr;
28284c274da1SToby Isaac 
28294c274da1SToby Isaac   PetscFunctionBegin;
28304c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28314c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
28324c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
28335db9a05bSToby Isaac     PetscInt nRows;
28345db9a05bSToby Isaac 
28355db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
28365db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
283792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
28387711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
28394c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
28404c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
28414c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
28424c274da1SToby Isaac       if (dof) {
28434c274da1SToby Isaac         PetscInt d;
28444c274da1SToby Isaac         PetscScalar *vals;
28454c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
28464c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
28474c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
28484c274da1SToby Isaac          * we just extracted */
28494c274da1SToby Isaac         for (d = 0; d < dof; d++) {
28504c274da1SToby Isaac           vals[d] = 0.;
28514c274da1SToby Isaac         }
28524c274da1SToby Isaac       }
28534c274da1SToby Isaac     }
28544c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
28554c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
28564c274da1SToby Isaac   }
28574c274da1SToby Isaac   PetscFunctionReturn(0);
28584c274da1SToby Isaac }
285901729b5cSPatrick Sanan /*@
286001729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
286101729b5cSPatrick Sanan 
2862d083f849SBarry Smith     Neighbor-wise Collective on dm
286301729b5cSPatrick Sanan 
286401729b5cSPatrick Sanan     Input Parameters:
286501729b5cSPatrick Sanan +   dm - the DM object
286601729b5cSPatrick Sanan .   l - the local vector
286701729b5cSPatrick 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.
286801729b5cSPatrick Sanan -   g - the global vector
286901729b5cSPatrick Sanan 
287001729b5cSPatrick Sanan     Notes:
287101729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
287201729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
287301729b5cSPatrick Sanan 
287401729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
287501729b5cSPatrick 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.
287601729b5cSPatrick Sanan 
287701729b5cSPatrick Sanan     Level: beginner
287801729b5cSPatrick Sanan 
287901729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
288001729b5cSPatrick Sanan 
288101729b5cSPatrick Sanan @*/
288201729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
288301729b5cSPatrick Sanan {
288401729b5cSPatrick Sanan   PetscErrorCode ierr;
288501729b5cSPatrick Sanan 
288601729b5cSPatrick Sanan   PetscFunctionBegin;
288701729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
288801729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
288901729b5cSPatrick Sanan   PetscFunctionReturn(0);
289001729b5cSPatrick Sanan }
28914c274da1SToby Isaac 
289247c6ae99SBarry Smith /*@
289301729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
28949a42bb27SBarry Smith 
2895d083f849SBarry Smith     Neighbor-wise Collective on dm
28969a42bb27SBarry Smith 
28979a42bb27SBarry Smith     Input Parameters:
28989a42bb27SBarry Smith +   dm - the DM object
2899f6813fd5SJed Brown .   l - the local vector
29001eb28f2eSBarry 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.
29011eb28f2eSBarry Smith -   g - the global vector
29029a42bb27SBarry Smith 
290395452b02SPatrick Sanan     Notes:
290495452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
290584330215SMatthew 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.
29069a42bb27SBarry Smith 
290701729b5cSPatrick Sanan     Level: intermediate
29089a42bb27SBarry Smith 
290901729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
29109a42bb27SBarry Smith 
29119a42bb27SBarry Smith @*/
29127087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
29139a42bb27SBarry Smith {
29147128ae9fSMatthew G Knepley   PetscSF                 sf;
291584330215SMatthew G. Knepley   PetscSection            s, gs;
2916d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2917ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2918ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2919ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2920fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
292184330215SMatthew G. Knepley   PetscErrorCode          ierr;
2922d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
29239a42bb27SBarry Smith 
29249a42bb27SBarry Smith   PetscFunctionBegin;
2925171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2926d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2927d4d07f1eSToby Isaac     if (link->beginhook) {
2928d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2929d4d07f1eSToby Isaac     }
2930d4d07f1eSToby Isaac   }
29314c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
29321bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
293392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
29347128ae9fSMatthew G Knepley   switch (mode) {
29357128ae9fSMatthew G Knepley   case INSERT_VALUES:
29367128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2937304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
293884330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
29397128ae9fSMatthew G Knepley   case ADD_VALUES:
29407128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2941304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
294284330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
29437128ae9fSMatthew G Knepley   default:
294498921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
29457128ae9fSMatthew G Knepley   }
2946ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2947ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2948ca3d3a14SMatthew G. Knepley     if (transform) {
2949ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2950ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2951ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2952ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2953fa88e482SJed Brown     } else if (isInsert) {
2954ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2955fa88e482SJed Brown     } else {
2956a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2957fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2958ca3d3a14SMatthew G. Knepley     }
2959fa88e482SJed Brown     if (s && isInsert) {
29607128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2961fa88e482SJed Brown     } else {
2962a256111fSJunchao Zhang       ierr = VecGetArrayAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2963fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2964fa88e482SJed Brown     }
2965ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2966d0295fc0SJunchao Zhang       ierr = PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM);CHKERRQ(ierr);
296784330215SMatthew G. Knepley     } else if (s && isInsert) {
296884330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
296984330215SMatthew G. Knepley 
2970e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
297184330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
297284330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
297384330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2974b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
297584330215SMatthew G. Knepley 
297684330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
297703442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
297884330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2979b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
298084330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
298184330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2982b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
298303442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2984*2c71b3e2SJacob Faibussowitsch         PetscCheckFalse(dof != gdof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %d dof: %d gdof: %d cdof: %d gcdof: %d", p, dof, gdof, cdof, gcdof);
2985b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2986b3b16f48SMatthew G. Knepley         if (!gcdof) {
298784330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2988b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2989b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
299084330215SMatthew G. Knepley           const PetscInt *cdofs;
299184330215SMatthew G. Knepley           PetscInt        cind = 0;
299284330215SMatthew G. Knepley 
299384330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2994b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
299584330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2996b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
299784330215SMatthew G. Knepley           }
299898921bdaSJacob Faibussowitsch         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %d dof: %d gdof: %d cdof: %d gcdof: %d", p, dof, gdof, cdof, gcdof);
299984330215SMatthew G. Knepley       }
3000ca3d3a14SMatthew G. Knepley     }
3001fa88e482SJed Brown     if (g_inplace) {
3002a256111fSJunchao Zhang       ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
3003fa88e482SJed Brown     } else {
30047128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
3005fa88e482SJed Brown     }
3006ca3d3a14SMatthew G. Knepley     if (transform) {
3007ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
3008ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
3009fa88e482SJed Brown     } else if (l_inplace) {
3010a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
3011ca3d3a14SMatthew G. Knepley     } else {
3012ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
3013ca3d3a14SMatthew G. Knepley     }
30147128ae9fSMatthew G Knepley   } else {
3015*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!dm->ops->localtoglobalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
3016843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
30177128ae9fSMatthew G Knepley   }
30189a42bb27SBarry Smith   PetscFunctionReturn(0);
30199a42bb27SBarry Smith }
30209a42bb27SBarry Smith 
30219a42bb27SBarry Smith /*@
30229a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
302347c6ae99SBarry Smith 
3024d083f849SBarry Smith     Neighbor-wise Collective on dm
302547c6ae99SBarry Smith 
302647c6ae99SBarry Smith     Input Parameters:
302747c6ae99SBarry Smith +   dm - the DM object
3028f6813fd5SJed Brown .   l - the local vector
302947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
3030f6813fd5SJed Brown -   g - the global vector
303147c6ae99SBarry Smith 
303201729b5cSPatrick Sanan     Level: intermediate
303347c6ae99SBarry Smith 
3034e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
303547c6ae99SBarry Smith 
303647c6ae99SBarry Smith @*/
30377087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
303847c6ae99SBarry Smith {
30397128ae9fSMatthew G Knepley   PetscSF                 sf;
304084330215SMatthew G. Knepley   PetscSection            s;
3041d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3042ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
304384330215SMatthew G. Knepley   PetscErrorCode          ierr;
304447c6ae99SBarry Smith 
304547c6ae99SBarry Smith   PetscFunctionBegin;
3046171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30471bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
304892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
30497128ae9fSMatthew G Knepley   switch (mode) {
30507128ae9fSMatthew G Knepley   case INSERT_VALUES:
30517128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
305284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
30537128ae9fSMatthew G Knepley   case ADD_VALUES:
30547128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
305584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
30567128ae9fSMatthew G Knepley   default:
305798921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
30587128ae9fSMatthew G Knepley   }
305984330215SMatthew G. Knepley   if (sf && !isInsert) {
3060ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3061ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3062ca3d3a14SMatthew G. Knepley     Vec                tmpl;
306384330215SMatthew G. Knepley 
3064ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
3065ca3d3a14SMatthew G. Knepley     if (transform) {
3066ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
3067ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
3068ca3d3a14SMatthew G. Knepley     } else {
3069a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, NULL);CHKERRQ(ierr);
3070ca3d3a14SMatthew G. Knepley     }
3071a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(g, &gArray, NULL);CHKERRQ(ierr);
3072a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
3073ca3d3a14SMatthew G. Knepley     if (transform) {
3074ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
3075ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
3076ca3d3a14SMatthew G. Knepley     } else {
3077a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
3078ca3d3a14SMatthew G. Knepley     }
3079a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
308084330215SMatthew G. Knepley   } else if (s && isInsert) {
30817128ae9fSMatthew G Knepley   } else {
3082*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!dm->ops->localtoglobalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
3083843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
30847128ae9fSMatthew G Knepley   }
3085d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
3086d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
3087d4d07f1eSToby Isaac   }
308847c6ae99SBarry Smith   PetscFunctionReturn(0);
308947c6ae99SBarry Smith }
309047c6ae99SBarry Smith 
3091f089877aSRichard Tran Mills /*@
3092bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
3093bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3094d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
3095f089877aSRichard Tran Mills 
3096d083f849SBarry Smith    Neighbor-wise Collective on dm
3097f089877aSRichard Tran Mills 
3098f089877aSRichard Tran Mills    Input Parameters:
3099f089877aSRichard Tran Mills +  dm - the DM object
3100bc0a1609SRichard Tran Mills .  g - the original local vector
3101bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3102f089877aSRichard Tran Mills 
3103bc0a1609SRichard Tran Mills    Output Parameter:
3104bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3105f089877aSRichard Tran Mills 
3106f089877aSRichard Tran Mills    Level: intermediate
3107f089877aSRichard Tran Mills 
3108bc0a1609SRichard Tran Mills    Notes:
3109bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3110bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3111bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3112bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3113bc0a1609SRichard Tran Mills 
3114bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3115f089877aSRichard Tran Mills 
3116f089877aSRichard Tran Mills @*/
3117f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
3118f089877aSRichard Tran Mills {
3119f089877aSRichard Tran Mills   PetscErrorCode          ierr;
3120f089877aSRichard Tran Mills 
3121f089877aSRichard Tran Mills   PetscFunctionBegin;
3122f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3123*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->localtolocalbegin,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3124f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
3125f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3126f089877aSRichard Tran Mills }
3127f089877aSRichard Tran Mills 
3128f089877aSRichard Tran Mills /*@
3129bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
3130bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3131d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
3132f089877aSRichard Tran Mills 
3133d083f849SBarry Smith    Neighbor-wise Collective on dm
3134f089877aSRichard Tran Mills 
3135f089877aSRichard Tran Mills    Input Parameters:
3136bc0a1609SRichard Tran Mills +  da - the DM object
3137bc0a1609SRichard Tran Mills .  g - the original local vector
3138bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3139f089877aSRichard Tran Mills 
3140bc0a1609SRichard Tran Mills    Output Parameter:
3141bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3142f089877aSRichard Tran Mills 
3143f089877aSRichard Tran Mills    Level: intermediate
3144f089877aSRichard Tran Mills 
3145bc0a1609SRichard Tran Mills    Notes:
3146bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3147bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3148bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3149bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3150bc0a1609SRichard Tran Mills 
3151bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3152f089877aSRichard Tran Mills 
3153f089877aSRichard Tran Mills @*/
3154f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
3155f089877aSRichard Tran Mills {
3156f089877aSRichard Tran Mills   PetscErrorCode          ierr;
3157f089877aSRichard Tran Mills 
3158f089877aSRichard Tran Mills   PetscFunctionBegin;
3159f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3160*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->localtolocalend,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3161f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
3162f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3163f089877aSRichard Tran Mills }
3164f089877aSRichard Tran Mills 
316547c6ae99SBarry Smith /*@
316647c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
316747c6ae99SBarry Smith 
3168d083f849SBarry Smith     Collective on dm
316947c6ae99SBarry Smith 
3170d8d19677SJose E. Roman     Input Parameters:
317147c6ae99SBarry Smith +   dm - the DM object
317291d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
317347c6ae99SBarry Smith 
317447c6ae99SBarry Smith     Output Parameter:
317547c6ae99SBarry Smith .   dmc - the coarsened DM
317647c6ae99SBarry Smith 
317747c6ae99SBarry Smith     Level: developer
317847c6ae99SBarry Smith 
3179e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
318047c6ae99SBarry Smith 
318147c6ae99SBarry Smith @*/
31827087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
318347c6ae99SBarry Smith {
318447c6ae99SBarry Smith   PetscErrorCode    ierr;
3185b17ce1afSJed Brown   DMCoarsenHookLink link;
318647c6ae99SBarry Smith 
318747c6ae99SBarry Smith   PetscFunctionBegin;
3188171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3189*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->coarsen,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
319047a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
319147c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
3192b9d85ea2SLisandro Dalcin   if (*dmc) {
3193a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
3194a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
319543842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
31968cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
3197644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
31980598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3199656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
3200e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
3201b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
3202b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
3203b17ce1afSJed Brown     }
3204b9d85ea2SLisandro Dalcin   }
320547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
3206*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!(*dmc),PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3207b17ce1afSJed Brown   PetscFunctionReturn(0);
3208b17ce1afSJed Brown }
3209b17ce1afSJed Brown 
3210bb9467b5SJed Brown /*@C
3211b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3212b17ce1afSJed Brown 
3213b17ce1afSJed Brown    Logically Collective
3214b17ce1afSJed Brown 
32154165533cSJose E. Roman    Input Parameters:
3216b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3217b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3218b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
32190298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3220b17ce1afSJed Brown 
3221b17ce1afSJed Brown    Calling sequence of coarsenhook:
3222b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3223b17ce1afSJed Brown 
3224b17ce1afSJed Brown +  fine - fine level DM
3225b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3226b17ce1afSJed Brown -  ctx - optional user-defined function context
3227b17ce1afSJed Brown 
3228b17ce1afSJed Brown    Calling sequence for restricthook:
3229c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3230b17ce1afSJed Brown 
3231b17ce1afSJed Brown +  fine - fine level DM
3232b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3233c833c3b5SJed Brown .  rscale - scaling vector for restriction
3234c833c3b5SJed Brown .  inject - matrix restricting by injection
3235b17ce1afSJed Brown .  coarse - coarse level DM to update
3236b17ce1afSJed Brown -  ctx - optional user-defined function context
3237b17ce1afSJed Brown 
3238b17ce1afSJed Brown    Level: advanced
3239b17ce1afSJed Brown 
3240b17ce1afSJed Brown    Notes:
3241b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3242b17ce1afSJed Brown 
3243b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3244b17ce1afSJed Brown 
3245b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3246b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3247b17ce1afSJed Brown 
3248bb9467b5SJed Brown    This function is currently not available from Fortran.
3249bb9467b5SJed Brown 
3250dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3251b17ce1afSJed Brown @*/
3252b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3253b17ce1afSJed Brown {
3254b17ce1afSJed Brown   PetscErrorCode    ierr;
3255b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3256b17ce1afSJed Brown 
3257b17ce1afSJed Brown   PetscFunctionBegin;
3258b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
32591e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
32601e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
32611e3d8eccSJed Brown   }
326295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
3263b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3264b17ce1afSJed Brown   link->restricthook = restricthook;
3265b17ce1afSJed Brown   link->ctx          = ctx;
32660298fd71SBarry Smith   link->next         = NULL;
3267b17ce1afSJed Brown   *p                 = link;
3268b17ce1afSJed Brown   PetscFunctionReturn(0);
3269b17ce1afSJed Brown }
3270b17ce1afSJed Brown 
3271dc822a44SJed Brown /*@C
3272dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3273dc822a44SJed Brown 
3274dc822a44SJed Brown    Logically Collective
3275dc822a44SJed Brown 
32764165533cSJose E. Roman    Input Parameters:
3277dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3278dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3279dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3280dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3281dc822a44SJed Brown 
3282dc822a44SJed Brown    Level: advanced
3283dc822a44SJed Brown 
3284dc822a44SJed Brown    Notes:
3285dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3286dc822a44SJed Brown 
3287dc822a44SJed Brown    This function is currently not available from Fortran.
3288dc822a44SJed Brown 
3289dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3290dc822a44SJed Brown @*/
3291dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3292dc822a44SJed Brown {
3293dc822a44SJed Brown   PetscErrorCode    ierr;
3294dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3295dc822a44SJed Brown 
3296dc822a44SJed Brown   PetscFunctionBegin;
3297dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3298dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3299dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3300dc822a44SJed Brown       link = *p;
3301dc822a44SJed Brown       *p = link->next;
3302dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3303dc822a44SJed Brown       break;
3304dc822a44SJed Brown     }
3305dc822a44SJed Brown   }
3306dc822a44SJed Brown   PetscFunctionReturn(0);
3307dc822a44SJed Brown }
3308dc822a44SJed Brown 
3309b17ce1afSJed Brown /*@
3310b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3311b17ce1afSJed Brown 
3312b17ce1afSJed Brown    Collective if any hooks are
3313b17ce1afSJed Brown 
33144165533cSJose E. Roman    Input Parameters:
3315b17ce1afSJed Brown +  fine - finer DM to use as a base
3316b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3317e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3318b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3319e91eccc2SStefano Zampini -  coarse - coarser DM to update
3320b17ce1afSJed Brown 
3321b17ce1afSJed Brown    Level: developer
3322b17ce1afSJed Brown 
3323b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3324b17ce1afSJed Brown @*/
3325b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3326b17ce1afSJed Brown {
3327b17ce1afSJed Brown   PetscErrorCode    ierr;
3328b17ce1afSJed Brown   DMCoarsenHookLink link;
3329b17ce1afSJed Brown 
3330b17ce1afSJed Brown   PetscFunctionBegin;
3331b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
33328865f1eaSKarl Rupp     if (link->restricthook) {
33338865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
33348865f1eaSKarl Rupp     }
3335b17ce1afSJed Brown   }
333647c6ae99SBarry Smith   PetscFunctionReturn(0);
333747c6ae99SBarry Smith }
333847c6ae99SBarry Smith 
3339bb9467b5SJed Brown /*@C
3340be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
33415dbd56e3SPeter Brune 
3342d083f849SBarry Smith    Logically Collective on global
33435dbd56e3SPeter Brune 
33444165533cSJose E. Roman    Input Parameters:
33455dbd56e3SPeter Brune +  global - global DM
3346ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
33475dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
33480298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
33495dbd56e3SPeter Brune 
3350ec4806b8SPeter Brune    Calling sequence for ddhook:
3351ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3352ec4806b8SPeter Brune 
3353ec4806b8SPeter Brune +  global - global DM
3354ec4806b8SPeter Brune .  block  - block DM
3355ec4806b8SPeter Brune -  ctx - optional user-defined function context
3356ec4806b8SPeter Brune 
33575dbd56e3SPeter Brune    Calling sequence for restricthook:
3358ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
33595dbd56e3SPeter Brune 
33605dbd56e3SPeter Brune +  global - global DM
33615dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
33625dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3363ec4806b8SPeter Brune .  block  - block DM
33645dbd56e3SPeter Brune -  ctx - optional user-defined function context
33655dbd56e3SPeter Brune 
33665dbd56e3SPeter Brune    Level: advanced
33675dbd56e3SPeter Brune 
33685dbd56e3SPeter Brune    Notes:
3369ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
33705dbd56e3SPeter Brune 
33715dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
33725dbd56e3SPeter Brune 
33735dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3374ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
33755dbd56e3SPeter Brune 
3376bb9467b5SJed Brown    This function is currently not available from Fortran.
3377bb9467b5SJed Brown 
33785dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
33795dbd56e3SPeter Brune @*/
3380be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
33815dbd56e3SPeter Brune {
33825dbd56e3SPeter Brune   PetscErrorCode      ierr;
3383be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
33845dbd56e3SPeter Brune 
33855dbd56e3SPeter Brune   PetscFunctionBegin;
33865dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3387b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3388b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3389b3a6b972SJed Brown   }
339095dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
33915dbd56e3SPeter Brune   link->restricthook = restricthook;
3392be081cd6SPeter Brune   link->ddhook       = ddhook;
33935dbd56e3SPeter Brune   link->ctx          = ctx;
33940298fd71SBarry Smith   link->next         = NULL;
33955dbd56e3SPeter Brune   *p                 = link;
33965dbd56e3SPeter Brune   PetscFunctionReturn(0);
33975dbd56e3SPeter Brune }
33985dbd56e3SPeter Brune 
3399b3a6b972SJed Brown /*@C
3400b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3401b3a6b972SJed Brown 
3402b3a6b972SJed Brown    Logically Collective
3403b3a6b972SJed Brown 
34044165533cSJose E. Roman    Input Parameters:
3405b3a6b972SJed Brown +  global - global DM
3406b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3407b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3408b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3409b3a6b972SJed Brown 
3410b3a6b972SJed Brown    Level: advanced
3411b3a6b972SJed Brown 
3412b3a6b972SJed Brown    Notes:
3413b3a6b972SJed Brown 
3414b3a6b972SJed Brown    This function is currently not available from Fortran.
3415b3a6b972SJed Brown 
3416b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3417b3a6b972SJed Brown @*/
3418b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3419b3a6b972SJed Brown {
3420b3a6b972SJed Brown   PetscErrorCode      ierr;
3421b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3422b3a6b972SJed Brown 
3423b3a6b972SJed Brown   PetscFunctionBegin;
3424b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3425b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3426b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3427b3a6b972SJed Brown       link = *p;
3428b3a6b972SJed Brown       *p = link->next;
3429b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3430b3a6b972SJed Brown       break;
3431b3a6b972SJed Brown     }
3432b3a6b972SJed Brown   }
3433b3a6b972SJed Brown   PetscFunctionReturn(0);
3434b3a6b972SJed Brown }
3435b3a6b972SJed Brown 
34365dbd56e3SPeter Brune /*@
3437be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
34385dbd56e3SPeter Brune 
34395dbd56e3SPeter Brune    Collective if any hooks are
34405dbd56e3SPeter Brune 
34414165533cSJose E. Roman    Input Parameters:
34425dbd56e3SPeter Brune +  fine - finer DM to use as a base
3443be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3444be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
34455dbd56e3SPeter Brune -  coarse - coarer DM to update
34465dbd56e3SPeter Brune 
34475dbd56e3SPeter Brune    Level: developer
34485dbd56e3SPeter Brune 
34495dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
34505dbd56e3SPeter Brune @*/
3451be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
34525dbd56e3SPeter Brune {
34535dbd56e3SPeter Brune   PetscErrorCode      ierr;
3454be081cd6SPeter Brune   DMSubDomainHookLink link;
34555dbd56e3SPeter Brune 
34565dbd56e3SPeter Brune   PetscFunctionBegin;
3457be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
34588865f1eaSKarl Rupp     if (link->restricthook) {
34598865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
34608865f1eaSKarl Rupp     }
34615dbd56e3SPeter Brune   }
34625dbd56e3SPeter Brune   PetscFunctionReturn(0);
34635dbd56e3SPeter Brune }
34645dbd56e3SPeter Brune 
34655fe1f584SPeter Brune /*@
34666a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
34675fe1f584SPeter Brune 
34685fe1f584SPeter Brune     Not Collective
34695fe1f584SPeter Brune 
34705fe1f584SPeter Brune     Input Parameter:
34715fe1f584SPeter Brune .   dm - the DM object
34725fe1f584SPeter Brune 
34735fe1f584SPeter Brune     Output Parameter:
34746a7d9d85SPeter Brune .   level - number of coarsenings
34755fe1f584SPeter Brune 
34765fe1f584SPeter Brune     Level: developer
34775fe1f584SPeter Brune 
34786a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
34795fe1f584SPeter Brune 
34805fe1f584SPeter Brune @*/
34815fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
34825fe1f584SPeter Brune {
34835fe1f584SPeter Brune   PetscFunctionBegin;
34845fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3485b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
34865fe1f584SPeter Brune   *level = dm->leveldown;
34875fe1f584SPeter Brune   PetscFunctionReturn(0);
34885fe1f584SPeter Brune }
34895fe1f584SPeter Brune 
34909a64c4a8SMatthew G. Knepley /*@
34919a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
34929a64c4a8SMatthew G. Knepley 
34939a64c4a8SMatthew G. Knepley     Not Collective
34949a64c4a8SMatthew G. Knepley 
34959a64c4a8SMatthew G. Knepley     Input Parameters:
34969a64c4a8SMatthew G. Knepley +   dm - the DM object
34979a64c4a8SMatthew G. Knepley -   level - number of coarsenings
34989a64c4a8SMatthew G. Knepley 
34999a64c4a8SMatthew G. Knepley     Level: developer
35009a64c4a8SMatthew G. Knepley 
35019a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
35029a64c4a8SMatthew G. Knepley @*/
35039a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
35049a64c4a8SMatthew G. Knepley {
35059a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
35069a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35079a64c4a8SMatthew G. Knepley   dm->leveldown = level;
35089a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
35099a64c4a8SMatthew G. Knepley }
35109a64c4a8SMatthew G. Knepley 
351147c6ae99SBarry Smith /*@C
351247c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
351347c6ae99SBarry Smith 
3514d083f849SBarry Smith     Collective on dm
351547c6ae99SBarry Smith 
3516d8d19677SJose E. Roman     Input Parameters:
351747c6ae99SBarry Smith +   dm - the DM object
351847c6ae99SBarry Smith -   nlevels - the number of levels of refinement
351947c6ae99SBarry Smith 
352047c6ae99SBarry Smith     Output Parameter:
352147c6ae99SBarry Smith .   dmf - the refined DM hierarchy
352247c6ae99SBarry Smith 
352347c6ae99SBarry Smith     Level: developer
352447c6ae99SBarry Smith 
3525e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
352647c6ae99SBarry Smith 
352747c6ae99SBarry Smith @*/
35287087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
352947c6ae99SBarry Smith {
353047c6ae99SBarry Smith   PetscErrorCode ierr;
353147c6ae99SBarry Smith 
353247c6ae99SBarry Smith   PetscFunctionBegin;
3533171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3534*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(nlevels < 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
353547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3536b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
353747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
353847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
353947c6ae99SBarry Smith   } else if (dm->ops->refine) {
354047c6ae99SBarry Smith     PetscInt i;
354147c6ae99SBarry Smith 
3542ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
354347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3544ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
354547c6ae99SBarry Smith     }
3546ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
354747c6ae99SBarry Smith   PetscFunctionReturn(0);
354847c6ae99SBarry Smith }
354947c6ae99SBarry Smith 
355047c6ae99SBarry Smith /*@C
355147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
355247c6ae99SBarry Smith 
3553d083f849SBarry Smith     Collective on dm
355447c6ae99SBarry Smith 
3555d8d19677SJose E. Roman     Input Parameters:
355647c6ae99SBarry Smith +   dm - the DM object
355747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
355847c6ae99SBarry Smith 
355947c6ae99SBarry Smith     Output Parameter:
356047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
356147c6ae99SBarry Smith 
356247c6ae99SBarry Smith     Level: developer
356347c6ae99SBarry Smith 
3564e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
356547c6ae99SBarry Smith 
356647c6ae99SBarry Smith @*/
35677087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
356847c6ae99SBarry Smith {
356947c6ae99SBarry Smith   PetscErrorCode ierr;
357047c6ae99SBarry Smith 
357147c6ae99SBarry Smith   PetscFunctionBegin;
3572171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3573*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(nlevels < 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
357447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
357547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
357647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
357747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
357847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
357947c6ae99SBarry Smith     PetscInt i;
358047c6ae99SBarry Smith 
3581ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
358247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3583ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
358447c6ae99SBarry Smith     }
3585ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
358647c6ae99SBarry Smith   PetscFunctionReturn(0);
358747c6ae99SBarry Smith }
358847c6ae99SBarry Smith 
35891a266240SBarry Smith /*@C
35901a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
35911a266240SBarry Smith 
35921a266240SBarry Smith     Not Collective
35931a266240SBarry Smith 
35941a266240SBarry Smith     Input Parameters:
35951a266240SBarry Smith +   dm - the DM object
35961a266240SBarry Smith -   destroy - the destroy function
35971a266240SBarry Smith 
35981a266240SBarry Smith     Level: intermediate
35991a266240SBarry Smith 
3600e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
36011a266240SBarry Smith 
3602f07f9ceaSJed Brown @*/
36031a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
36041a266240SBarry Smith {
36051a266240SBarry Smith   PetscFunctionBegin;
3606171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36071a266240SBarry Smith   dm->ctxdestroy = destroy;
36081a266240SBarry Smith   PetscFunctionReturn(0);
36091a266240SBarry Smith }
36101a266240SBarry Smith 
3611b07ff414SBarry Smith /*@
36121b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
361347c6ae99SBarry Smith 
361447c6ae99SBarry Smith     Not Collective
361547c6ae99SBarry Smith 
361647c6ae99SBarry Smith     Input Parameters:
361747c6ae99SBarry Smith +   dm - the DM object
361847c6ae99SBarry Smith -   ctx - the user context
361947c6ae99SBarry Smith 
362047c6ae99SBarry Smith     Level: intermediate
362147c6ae99SBarry Smith 
3622e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
362347c6ae99SBarry Smith 
362447c6ae99SBarry Smith @*/
36251b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
362647c6ae99SBarry Smith {
362747c6ae99SBarry Smith   PetscFunctionBegin;
3628171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
362947c6ae99SBarry Smith   dm->ctx = ctx;
363047c6ae99SBarry Smith   PetscFunctionReturn(0);
363147c6ae99SBarry Smith }
363247c6ae99SBarry Smith 
363347c6ae99SBarry Smith /*@
36341b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
363547c6ae99SBarry Smith 
363647c6ae99SBarry Smith     Not Collective
363747c6ae99SBarry Smith 
363847c6ae99SBarry Smith     Input Parameter:
363947c6ae99SBarry Smith .   dm - the DM object
364047c6ae99SBarry Smith 
364147c6ae99SBarry Smith     Output Parameter:
364247c6ae99SBarry Smith .   ctx - the user context
364347c6ae99SBarry Smith 
364447c6ae99SBarry Smith     Level: intermediate
364547c6ae99SBarry Smith 
3646e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
364747c6ae99SBarry Smith 
364847c6ae99SBarry Smith @*/
36491b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
365047c6ae99SBarry Smith {
365147c6ae99SBarry Smith   PetscFunctionBegin;
3652171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36531b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
365447c6ae99SBarry Smith   PetscFunctionReturn(0);
365547c6ae99SBarry Smith }
365647c6ae99SBarry Smith 
365708da532bSDmitry Karpeev /*@C
3658df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
365908da532bSDmitry Karpeev 
3660d083f849SBarry Smith     Logically Collective on dm
366108da532bSDmitry Karpeev 
3662d8d19677SJose E. Roman     Input Parameters:
366308da532bSDmitry Karpeev +   dm - the DM object
36640298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
366508da532bSDmitry Karpeev 
366608da532bSDmitry Karpeev     Level: intermediate
366708da532bSDmitry Karpeev 
3668835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
366908da532bSDmitry Karpeev          DMSetJacobian()
367008da532bSDmitry Karpeev 
367108da532bSDmitry Karpeev @*/
367208da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
367308da532bSDmitry Karpeev {
367408da532bSDmitry Karpeev   PetscFunctionBegin;
36755a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
367608da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
367708da532bSDmitry Karpeev   PetscFunctionReturn(0);
367808da532bSDmitry Karpeev }
367908da532bSDmitry Karpeev 
368008da532bSDmitry Karpeev /*@
368108da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
368208da532bSDmitry Karpeev 
368308da532bSDmitry Karpeev     Not Collective
368408da532bSDmitry Karpeev 
368508da532bSDmitry Karpeev     Input Parameter:
368608da532bSDmitry Karpeev .   dm - the DM object to destroy
368708da532bSDmitry Karpeev 
368808da532bSDmitry Karpeev     Output Parameter:
368908da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
369008da532bSDmitry Karpeev 
369108da532bSDmitry Karpeev     Level: developer
369208da532bSDmitry Karpeev 
369374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
369408da532bSDmitry Karpeev 
369508da532bSDmitry Karpeev @*/
369608da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
369708da532bSDmitry Karpeev {
369808da532bSDmitry Karpeev   PetscFunctionBegin;
36995a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3700534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
370108da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
370208da532bSDmitry Karpeev   PetscFunctionReturn(0);
370308da532bSDmitry Karpeev }
370408da532bSDmitry Karpeev 
370508da532bSDmitry Karpeev /*@C
370608da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
370708da532bSDmitry Karpeev 
3708d083f849SBarry Smith     Logically Collective on dm
370908da532bSDmitry Karpeev 
3710f899ff85SJose E. Roman     Input Parameter:
3711907376e6SBarry Smith .   dm - the DM object
371208da532bSDmitry Karpeev 
371308da532bSDmitry Karpeev     Output parameters:
371408da532bSDmitry Karpeev +   xl - lower bound
371508da532bSDmitry Karpeev -   xu - upper bound
371608da532bSDmitry Karpeev 
3717907376e6SBarry Smith     Level: advanced
3718907376e6SBarry Smith 
371995452b02SPatrick Sanan     Notes:
372095452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
372108da532bSDmitry Karpeev 
372274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
372308da532bSDmitry Karpeev 
372408da532bSDmitry Karpeev @*/
372508da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
372608da532bSDmitry Karpeev {
372708da532bSDmitry Karpeev   PetscErrorCode ierr;
37285fd66863SKarl Rupp 
372908da532bSDmitry Karpeev   PetscFunctionBegin;
37305a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
373108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
37325a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3733*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->computevariablebounds,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
373408da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
373508da532bSDmitry Karpeev   PetscFunctionReturn(0);
373608da532bSDmitry Karpeev }
373708da532bSDmitry Karpeev 
3738b0ae01b7SPeter Brune /*@
3739b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3740b0ae01b7SPeter Brune 
3741b0ae01b7SPeter Brune     Not Collective
3742b0ae01b7SPeter Brune 
3743b0ae01b7SPeter Brune     Input Parameter:
3744b0ae01b7SPeter Brune .   dm - the DM object
3745b0ae01b7SPeter Brune 
3746b0ae01b7SPeter Brune     Output Parameter:
3747b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3748b0ae01b7SPeter Brune 
3749b0ae01b7SPeter Brune     Level: developer
3750b0ae01b7SPeter Brune 
37511565f0a7SPatrick Sanan .seealso DMCreateColoring()
3752b0ae01b7SPeter Brune 
3753b0ae01b7SPeter Brune @*/
3754b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3755b0ae01b7SPeter Brune {
3756b0ae01b7SPeter Brune   PetscFunctionBegin;
37575a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3758534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3759b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3760b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3761b0ae01b7SPeter Brune }
3762b0ae01b7SPeter Brune 
37633ad4599aSBarry Smith /*@
37643ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
37653ad4599aSBarry Smith 
37663ad4599aSBarry Smith     Not Collective
37673ad4599aSBarry Smith 
37683ad4599aSBarry Smith     Input Parameter:
37693ad4599aSBarry Smith .   dm - the DM object
37703ad4599aSBarry Smith 
37713ad4599aSBarry Smith     Output Parameter:
37723ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
37733ad4599aSBarry Smith 
37743ad4599aSBarry Smith     Level: developer
37753ad4599aSBarry Smith 
37761565f0a7SPatrick Sanan .seealso DMCreateRestriction()
37773ad4599aSBarry Smith 
37783ad4599aSBarry Smith @*/
37793ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
37803ad4599aSBarry Smith {
37813ad4599aSBarry Smith   PetscFunctionBegin;
37825a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3783534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37843ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
37853ad4599aSBarry Smith   PetscFunctionReturn(0);
37863ad4599aSBarry Smith }
37873ad4599aSBarry Smith 
3788a7058e45SLawrence Mitchell /*@
3789a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3790a7058e45SLawrence Mitchell 
3791a7058e45SLawrence Mitchell     Not Collective
3792a7058e45SLawrence Mitchell 
3793a7058e45SLawrence Mitchell     Input Parameter:
3794a7058e45SLawrence Mitchell .   dm - the DM object
3795a7058e45SLawrence Mitchell 
3796a7058e45SLawrence Mitchell     Output Parameter:
3797a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3798a7058e45SLawrence Mitchell 
3799a7058e45SLawrence Mitchell     Level: developer
3800a7058e45SLawrence Mitchell 
38011565f0a7SPatrick Sanan .seealso DMCreateInjection()
3802a7058e45SLawrence Mitchell 
3803a7058e45SLawrence Mitchell @*/
3804a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3805a7058e45SLawrence Mitchell {
38064a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
38075a84ad33SLisandro Dalcin 
3808a7058e45SLawrence Mitchell   PetscFunctionBegin;
38095a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3810534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
38115a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
38124a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
38135a84ad33SLisandro Dalcin   } else {
38145a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
38155a84ad33SLisandro Dalcin   }
3816a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3817a7058e45SLawrence Mitchell }
3818a7058e45SLawrence Mitchell 
38190298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3820264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3821264ace61SBarry Smith 
3822264ace61SBarry Smith /*@C
3823264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3824264ace61SBarry Smith 
3825d083f849SBarry Smith   Collective on dm
3826264ace61SBarry Smith 
3827264ace61SBarry Smith   Input Parameters:
3828264ace61SBarry Smith + dm     - The DM object
3829264ace61SBarry Smith - method - The name of the DM type
3830264ace61SBarry Smith 
3831264ace61SBarry Smith   Options Database Key:
3832264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3833264ace61SBarry Smith 
3834264ace61SBarry Smith   Notes:
3835e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3836264ace61SBarry Smith 
3837264ace61SBarry Smith   Level: intermediate
3838264ace61SBarry Smith 
3839264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3840264ace61SBarry Smith @*/
384119fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3842264ace61SBarry Smith {
3843264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3844264ace61SBarry Smith   PetscBool      match;
3845264ace61SBarry Smith   PetscErrorCode ierr;
3846264ace61SBarry Smith 
3847264ace61SBarry Smith   PetscFunctionBegin;
3848264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3849251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3850264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3851264ace61SBarry Smith 
38520f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
38531c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3854*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!r,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3855264ace61SBarry Smith 
3856264ace61SBarry Smith   if (dm->ops->destroy) {
3857264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3858264ace61SBarry Smith   }
3859d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3860264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3861d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3862264ace61SBarry Smith   PetscFunctionReturn(0);
3863264ace61SBarry Smith }
3864264ace61SBarry Smith 
3865264ace61SBarry Smith /*@C
3866264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3867264ace61SBarry Smith 
3868264ace61SBarry Smith   Not Collective
3869264ace61SBarry Smith 
3870264ace61SBarry Smith   Input Parameter:
3871264ace61SBarry Smith . dm  - The DM
3872264ace61SBarry Smith 
3873264ace61SBarry Smith   Output Parameter:
3874264ace61SBarry Smith . type - The DM type name
3875264ace61SBarry Smith 
3876264ace61SBarry Smith   Level: intermediate
3877264ace61SBarry Smith 
3878264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3879264ace61SBarry Smith @*/
388019fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3881264ace61SBarry Smith {
3882264ace61SBarry Smith   PetscErrorCode ierr;
3883264ace61SBarry Smith 
3884264ace61SBarry Smith   PetscFunctionBegin;
3885264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3886c959eef4SJed Brown   PetscValidPointer(type,2);
3887607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3888264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3889264ace61SBarry Smith   PetscFunctionReturn(0);
3890264ace61SBarry Smith }
3891264ace61SBarry Smith 
389267a56275SMatthew G Knepley /*@C
389367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
389467a56275SMatthew G Knepley 
3895d083f849SBarry Smith   Collective on dm
389667a56275SMatthew G Knepley 
389767a56275SMatthew G Knepley   Input Parameters:
389867a56275SMatthew G Knepley + dm - the DM
389967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
390067a56275SMatthew G Knepley 
390167a56275SMatthew G Knepley   Output Parameter:
390267a56275SMatthew G Knepley . M - pointer to new DM
390367a56275SMatthew G Knepley 
390467a56275SMatthew G Knepley   Notes:
390567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
390667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
390767a56275SMatthew G Knepley   of the input DM.
390867a56275SMatthew G Knepley 
390967a56275SMatthew G Knepley   Level: intermediate
391067a56275SMatthew G Knepley 
391167a56275SMatthew G Knepley .seealso: DMCreate()
391267a56275SMatthew G Knepley @*/
391319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
391467a56275SMatthew G Knepley {
391567a56275SMatthew G Knepley   DM             B;
391667a56275SMatthew G Knepley   char           convname[256];
3917c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
391867a56275SMatthew G Knepley   PetscErrorCode ierr;
391967a56275SMatthew G Knepley 
392067a56275SMatthew G Knepley   PetscFunctionBegin;
392167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
392267a56275SMatthew G Knepley   PetscValidType(dm,1);
392367a56275SMatthew G Knepley   PetscValidPointer(M,3);
3924251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3925c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3926c067b6caSMatthew G. Knepley   if (sametype) {
3927c067b6caSMatthew G. Knepley     *M   = dm;
3928c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3929c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3930c067b6caSMatthew G. Knepley   } else {
39310298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
393267a56275SMatthew G Knepley 
393367a56275SMatthew G Knepley     /*
393467a56275SMatthew G Knepley        Order of precedence:
393567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
393667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
393767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
393867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
393967a56275SMatthew G Knepley        5) Use a really basic converter.
394067a56275SMatthew G Knepley     */
394167a56275SMatthew G Knepley 
394267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3943a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3944a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3945a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3946a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3947a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
39480005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
394967a56275SMatthew G Knepley     if (conv) goto foundconv;
395067a56275SMatthew G Knepley 
395167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
395282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
395367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3954a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3955a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3956a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3957a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3958a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
39590005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
396067a56275SMatthew G Knepley     if (conv) {
3961fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
396267a56275SMatthew G Knepley       goto foundconv;
396367a56275SMatthew G Knepley     }
396467a56275SMatthew G Knepley 
396567a56275SMatthew G Knepley #if 0
396667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
396767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3968fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
396967a56275SMatthew G Knepley     if (conv) goto foundconv;
397067a56275SMatthew G Knepley 
397167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
397267a56275SMatthew G Knepley     if (dm->ops->convert) {
397367a56275SMatthew G Knepley       conv = dm->ops->convert;
397467a56275SMatthew G Knepley     }
397567a56275SMatthew G Knepley     if (conv) goto foundconv;
397667a56275SMatthew G Knepley #endif
397767a56275SMatthew G Knepley 
397867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
397998921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
398067a56275SMatthew G Knepley 
398167a56275SMatthew G Knepley foundconv:
398267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
398367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
398412fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
398590b157c4SStefano Zampini     {
398690b157c4SStefano Zampini       PetscBool             isper;
398712fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
398812fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
398990b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
399090b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
3991c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
3992a587d139SMark       ierr = PetscFree((*M)->vectype);CHKERRQ(ierr);
3993a587d139SMark       ierr = PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype);CHKERRQ(ierr);
3994a587d139SMark       ierr = PetscFree((*M)->mattype);CHKERRQ(ierr);
3995a587d139SMark       ierr = PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype);CHKERRQ(ierr);
399612fa691eSMatthew G. Knepley     }
399767a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
399867a56275SMatthew G Knepley   }
399967a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
400067a56275SMatthew G Knepley   PetscFunctionReturn(0);
400167a56275SMatthew G Knepley }
4002264ace61SBarry Smith 
4003264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4004264ace61SBarry Smith 
4005264ace61SBarry Smith /*@C
40061c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
40071c84c290SBarry Smith 
40081c84c290SBarry Smith   Not Collective
40091c84c290SBarry Smith 
40101c84c290SBarry Smith   Input Parameters:
40111c84c290SBarry Smith + name        - The name of a new user-defined creation routine
40121c84c290SBarry Smith - create_func - The creation routine itself
40131c84c290SBarry Smith 
40141c84c290SBarry Smith   Notes:
40151c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
40161c84c290SBarry Smith 
40171c84c290SBarry Smith   Sample usage:
40181c84c290SBarry Smith .vb
4019bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40201c84c290SBarry Smith .ve
40211c84c290SBarry Smith 
40221c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
40231c84c290SBarry Smith .vb
40241c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40251c84c290SBarry Smith     DMSetType(DM,"my_da");
40261c84c290SBarry Smith .ve
40271c84c290SBarry Smith    or at runtime via the option
40281c84c290SBarry Smith .vb
40291c84c290SBarry Smith     -da_type my_da
40301c84c290SBarry Smith .ve
4031264ace61SBarry Smith 
4032264ace61SBarry Smith   Level: advanced
40331c84c290SBarry Smith 
4034bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
40351c84c290SBarry Smith 
4036264ace61SBarry Smith @*/
4037bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
4038264ace61SBarry Smith {
4039264ace61SBarry Smith   PetscErrorCode ierr;
4040264ace61SBarry Smith 
4041264ace61SBarry Smith   PetscFunctionBegin;
40421d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
4043a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
4044264ace61SBarry Smith   PetscFunctionReturn(0);
4045264ace61SBarry Smith }
4046264ace61SBarry Smith 
4047b859378eSBarry Smith /*@C
404855849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
4049b859378eSBarry Smith 
4050d083f849SBarry Smith   Collective on viewer
4051b859378eSBarry Smith 
4052b859378eSBarry Smith   Input Parameters:
4053b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
4054b859378eSBarry Smith            some related function before a call to DMLoad().
4055b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
4056b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
4057b859378eSBarry Smith 
4058b859378eSBarry Smith    Level: intermediate
4059b859378eSBarry Smith 
4060b859378eSBarry Smith   Notes:
406155849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4062b859378eSBarry Smith 
4063cd7e8a5eSksagiyam   Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
4064cd7e8a5eSksagiyam   meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
4065cd7e8a5eSksagiyam   before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
4066cd7e8a5eSksagiyam 
4067b859378eSBarry Smith   Notes for advanced users:
4068b859378eSBarry Smith   Most users should not need to know the details of the binary storage
4069b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
4070b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
4071b859378eSBarry Smith   format is
4072b859378eSBarry Smith .vb
4073b859378eSBarry Smith      has not yet been determined
4074b859378eSBarry Smith .ve
4075b859378eSBarry Smith 
4076b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
4077b859378eSBarry Smith @*/
4078b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
4079b859378eSBarry Smith {
40809331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
4081b859378eSBarry Smith   PetscErrorCode ierr;
4082b859378eSBarry Smith 
4083b859378eSBarry Smith   PetscFunctionBegin;
4084b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
4085b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4086fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
408732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
40889331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
408958cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
40909331c7a4SMatthew G. Knepley   if (isbinary) {
40919331c7a4SMatthew G. Knepley     PetscInt classid;
40929331c7a4SMatthew G. Knepley     char     type[256];
4093b859378eSBarry Smith 
4094060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
4095*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(classid != DM_FILE_CLASSID,PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
4096060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
409732c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
40989331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
40999331c7a4SMatthew G. Knepley   } else if (ishdf5) {
41009331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
41019331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
410258cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
4103b859378eSBarry Smith   PetscFunctionReturn(0);
4104b859378eSBarry Smith }
4105b859378eSBarry Smith 
4106b2e4378dSMatthew G. Knepley /*@
4107b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
4108b2e4378dSMatthew G. Knepley 
4109b2e4378dSMatthew G. Knepley   Not collective
4110b2e4378dSMatthew G. Knepley 
4111b2e4378dSMatthew G. Knepley   Input Parameter:
4112b2e4378dSMatthew G. Knepley . dm - the DM
4113b2e4378dSMatthew G. Knepley 
4114b2e4378dSMatthew G. Knepley   Output Parameters:
4115b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
4116b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
4117b2e4378dSMatthew G. Knepley 
4118b2e4378dSMatthew G. Knepley   Level: beginner
4119b2e4378dSMatthew G. Knepley 
4120b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
4121b2e4378dSMatthew G. Knepley 
4122b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
4123b2e4378dSMatthew G. Knepley @*/
4124b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
4125b2e4378dSMatthew G. Knepley {
4126b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
4127b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
4128b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
4129b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
4130b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
4131b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
4132b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
4133b2e4378dSMatthew G. Knepley 
4134b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4135b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4136b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4137b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
4138b2e4378dSMatthew G. Knepley   if (coords) {
4139b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
4140b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
4141b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
4142b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
4143b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
4144b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4145b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4146b2e4378dSMatthew G. Knepley       }
4147b2e4378dSMatthew G. Knepley     }
4148b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
4149b2e4378dSMatthew G. Knepley   } else {
4150b2e4378dSMatthew G. Knepley     PetscBool isda;
4151b2e4378dSMatthew G. Knepley 
4152b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
4153b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
4154b2e4378dSMatthew G. Knepley   }
4155b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
4156b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
4157b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4158b2e4378dSMatthew G. Knepley }
4159b2e4378dSMatthew G. Knepley 
4160b2e4378dSMatthew G. Knepley /*@
4161b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
4162b2e4378dSMatthew G. Knepley 
4163b2e4378dSMatthew G. Knepley   Collective
4164b2e4378dSMatthew G. Knepley 
4165b2e4378dSMatthew G. Knepley   Input Parameter:
4166b2e4378dSMatthew G. Knepley . dm - the DM
4167b2e4378dSMatthew G. Knepley 
4168b2e4378dSMatthew G. Knepley   Output Parameters:
4169b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
4170b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
4171b2e4378dSMatthew G. Knepley 
4172b2e4378dSMatthew G. Knepley   Level: beginner
4173b2e4378dSMatthew G. Knepley 
4174b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
4175b2e4378dSMatthew G. Knepley @*/
4176b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
4177b2e4378dSMatthew G. Knepley {
4178b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
41796ce308c4SMatthew G. Knepley   PetscInt       cdim;
41806ce308c4SMatthew G. Knepley   PetscMPIInt    count;
4181b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
4182b2e4378dSMatthew G. Knepley 
4183b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4184b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4185b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4186b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
4187b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
4188820f2d46SBarry Smith   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4189820f2d46SBarry Smith   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4190b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4191b2e4378dSMatthew G. Knepley }
4192b2e4378dSMatthew G. Knepley 
41937da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41947da65231SMatthew G Knepley 
4195a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4196a6dfd86eSKarl Rupp {
41971d47ebbbSSatish Balay   PetscInt       f;
41981b30c384SMatthew G Knepley   PetscErrorCode ierr;
41991b30c384SMatthew G Knepley 
42007da65231SMatthew G Knepley   PetscFunctionBegin;
420174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
42021d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
420357622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
42047da65231SMatthew G Knepley   }
42057da65231SMatthew G Knepley   PetscFunctionReturn(0);
42067da65231SMatthew G Knepley }
42077da65231SMatthew G Knepley 
4208a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4209a6dfd86eSKarl Rupp {
42101b30c384SMatthew G Knepley   PetscInt       f, g;
42117da65231SMatthew G Knepley   PetscErrorCode ierr;
42127da65231SMatthew G Knepley 
42137da65231SMatthew G Knepley   PetscFunctionBegin;
421474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
42151d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
421674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
42171d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
4218e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
42197da65231SMatthew G Knepley     }
422074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
42217da65231SMatthew G Knepley   }
42227da65231SMatthew G Knepley   PetscFunctionReturn(0);
42237da65231SMatthew G Knepley }
4224e7c4fc90SDmitry Karpeev 
42256113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4226e759306cSMatthew G. Knepley {
42270c5b8624SToby Isaac   PetscInt          localSize, bs;
42280c5b8624SToby Isaac   PetscMPIInt       size;
42290c5b8624SToby Isaac   Vec               x, xglob;
42300c5b8624SToby Isaac   const PetscScalar *xarray;
4231e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
4232e759306cSMatthew G. Knepley 
4233e759306cSMatthew G. Knepley   PetscFunctionBegin;
4234ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRMPI(ierr);
4235e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
4236e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
42376113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
42380c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
42390c5b8624SToby Isaac   if (size > 1) {
42400c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
42410c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
42420c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
42430c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
42440c5b8624SToby Isaac   } else {
42450c5b8624SToby Isaac     xglob = x;
42460c5b8624SToby Isaac   }
42470c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
42480c5b8624SToby Isaac   if (size > 1) {
42490c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
42500c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
42510c5b8624SToby Isaac   }
4252e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
4253e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4254e759306cSMatthew G. Knepley }
4255e759306cSMatthew G. Knepley 
425688ed4aceSMatthew G Knepley /*@
42571bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4258061576a5SJed Brown 
4259061576a5SJed Brown   Input Parameter:
4260061576a5SJed Brown . dm - The DM
4261061576a5SJed Brown 
4262061576a5SJed Brown   Output Parameter:
4263061576a5SJed Brown . section - The PetscSection
4264061576a5SJed Brown 
4265061576a5SJed Brown   Options Database Keys:
4266061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4267061576a5SJed Brown 
4268061576a5SJed Brown   Level: advanced
4269061576a5SJed Brown 
4270061576a5SJed Brown   Notes:
4271061576a5SJed Brown   Use DMGetLocalSection() in new code.
4272061576a5SJed Brown 
4273061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4274061576a5SJed Brown 
4275061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
4276061576a5SJed Brown @*/
4277061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4278061576a5SJed Brown {
4279061576a5SJed Brown   PetscErrorCode ierr;
4280061576a5SJed Brown 
4281061576a5SJed Brown   PetscFunctionBegin;
4282061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
4283061576a5SJed Brown   PetscFunctionReturn(0);
4284061576a5SJed Brown }
4285061576a5SJed Brown 
4286061576a5SJed Brown /*@
4287061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
428888ed4aceSMatthew G Knepley 
428988ed4aceSMatthew G Knepley   Input Parameter:
429088ed4aceSMatthew G Knepley . dm - The DM
429188ed4aceSMatthew G Knepley 
429288ed4aceSMatthew G Knepley   Output Parameter:
429388ed4aceSMatthew G Knepley . section - The PetscSection
429488ed4aceSMatthew G Knepley 
4295e5893cccSMatthew G. Knepley   Options Database Keys:
4296e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4297e5893cccSMatthew G. Knepley 
429888ed4aceSMatthew G Knepley   Level: intermediate
429988ed4aceSMatthew G Knepley 
430088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
430188ed4aceSMatthew G Knepley 
4302061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
430388ed4aceSMatthew G Knepley @*/
4304061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
43050adebc6cSBarry Smith {
4306fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4307fd59a867SMatthew G. Knepley 
430888ed4aceSMatthew G Knepley   PetscFunctionBegin;
430988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
431088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
43111bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4312e5e52638SMatthew G. Knepley     PetscInt d;
4313e5e52638SMatthew G. Knepley 
431445480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
431545480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject) dm;
431645480ffeSMatthew G. Knepley       PetscViewer       viewer;
431745480ffeSMatthew G. Knepley       PetscViewerFormat format;
431845480ffeSMatthew G. Knepley       PetscBool         flg;
431945480ffeSMatthew G. Knepley 
432045480ffeSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg);CHKERRQ(ierr);
432145480ffeSMatthew G. Knepley       if (flg) {ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);}
432245480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
432345480ffeSMatthew G. Knepley         ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);
432445480ffeSMatthew G. Knepley         if (flg) {ierr = PetscDSView(dm->probs[d].ds, viewer);CHKERRQ(ierr);}
432545480ffeSMatthew G. Knepley       }
432645480ffeSMatthew G. Knepley       if (flg) {
432745480ffeSMatthew G. Knepley         ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
432845480ffeSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
432945480ffeSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
433045480ffeSMatthew G. Knepley       }
433145480ffeSMatthew G. Knepley     }
43321bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
43331bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
43342f0f8703SMatthew G. Knepley   }
43351bb6d2a8SBarry Smith   *section = dm->localSection;
433688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
433788ed4aceSMatthew G Knepley }
433888ed4aceSMatthew G Knepley 
433988ed4aceSMatthew G Knepley /*@
43401bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4341061576a5SJed Brown 
4342061576a5SJed Brown   Input Parameters:
4343061576a5SJed Brown + dm - The DM
4344061576a5SJed Brown - section - The PetscSection
4345061576a5SJed Brown 
4346061576a5SJed Brown   Level: advanced
4347061576a5SJed Brown 
4348061576a5SJed Brown   Notes:
4349061576a5SJed Brown   Use DMSetLocalSection() in new code.
4350061576a5SJed Brown 
4351061576a5SJed Brown   Any existing Section will be destroyed
4352061576a5SJed Brown 
4353061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4354061576a5SJed Brown @*/
4355061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4356061576a5SJed Brown {
4357061576a5SJed Brown   PetscErrorCode ierr;
4358061576a5SJed Brown 
4359061576a5SJed Brown   PetscFunctionBegin;
4360061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4361061576a5SJed Brown   PetscFunctionReturn(0);
4362061576a5SJed Brown }
4363061576a5SJed Brown 
4364061576a5SJed Brown /*@
4365061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
436688ed4aceSMatthew G Knepley 
436788ed4aceSMatthew G Knepley   Input Parameters:
436888ed4aceSMatthew G Knepley + dm - The DM
436988ed4aceSMatthew G Knepley - section - The PetscSection
437088ed4aceSMatthew G Knepley 
437188ed4aceSMatthew G Knepley   Level: intermediate
437288ed4aceSMatthew G Knepley 
437388ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
437488ed4aceSMatthew G Knepley 
4375061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
437688ed4aceSMatthew G Knepley @*/
4377061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
43780adebc6cSBarry Smith {
4379c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4380af122d2aSMatthew G Knepley   PetscInt       f;
438188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
438288ed4aceSMatthew G Knepley 
438388ed4aceSMatthew G Knepley   PetscFunctionBegin;
438488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4385b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
43861d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
43871bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
43881bb6d2a8SBarry Smith   dm->localSection = section;
43891bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4390af122d2aSMatthew G Knepley   if (numFields) {
4391af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4392af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43930f21e855SMatthew G. Knepley       PetscObject disc;
4394af122d2aSMatthew G Knepley       const char *name;
4395af122d2aSMatthew G Knepley 
43961bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
439744a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
43980f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4399af122d2aSMatthew G Knepley     }
4400af122d2aSMatthew G Knepley   }
4401e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
44021bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
440388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
440488ed4aceSMatthew G Knepley }
440588ed4aceSMatthew G Knepley 
44069435951eSToby Isaac /*@
4407b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
44089435951eSToby Isaac 
4409e228b242SToby Isaac   not collective
4410e228b242SToby Isaac 
44119435951eSToby Isaac   Input Parameter:
44129435951eSToby Isaac . dm - The DM
44139435951eSToby Isaac 
4414d8d19677SJose E. Roman   Output Parameters:
44159435951eSToby 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.
44169435951eSToby 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.
44179435951eSToby Isaac 
44189435951eSToby Isaac   Level: advanced
44199435951eSToby Isaac 
44209435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
44219435951eSToby Isaac 
44229435951eSToby Isaac .seealso: DMSetDefaultConstraints()
44239435951eSToby Isaac @*/
44249435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
44259435951eSToby Isaac {
44269435951eSToby Isaac   PetscErrorCode ierr;
44279435951eSToby Isaac 
44289435951eSToby Isaac   PetscFunctionBegin;
44299435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44309435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
443145a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
443245a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
44339435951eSToby Isaac   PetscFunctionReturn(0);
44349435951eSToby Isaac }
44359435951eSToby Isaac 
44369435951eSToby Isaac /*@
4437b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
44389435951eSToby Isaac 
44399435951eSToby 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().
44409435951eSToby Isaac 
44419435951eSToby 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.
44429435951eSToby Isaac 
4443e228b242SToby Isaac   collective on dm
4444e228b242SToby Isaac 
44459435951eSToby Isaac   Input Parameters:
44469435951eSToby Isaac + dm - The DM
4447e228b242SToby 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).
4448e228b242SToby 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).
44499435951eSToby Isaac 
44509435951eSToby Isaac   Level: advanced
44519435951eSToby Isaac 
44529435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
44539435951eSToby Isaac 
44549435951eSToby Isaac .seealso: DMGetDefaultConstraints()
44559435951eSToby Isaac @*/
44569435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
44579435951eSToby Isaac {
4458e228b242SToby Isaac   PetscMPIInt result;
44599435951eSToby Isaac   PetscErrorCode ierr;
44609435951eSToby Isaac 
44619435951eSToby Isaac   PetscFunctionBegin;
44629435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4463e228b242SToby Isaac   if (section) {
4464e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4465ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRMPI(ierr);
4466*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(result != MPI_CONGRUENT && result != MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4467e228b242SToby Isaac   }
4468e228b242SToby Isaac   if (mat) {
4469e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4470ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRMPI(ierr);
4471*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(result != MPI_CONGRUENT && result != MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4472e228b242SToby Isaac   }
44739435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
44749435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
44759435951eSToby Isaac   dm->defaultConstraintSection = section;
44769435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
44779435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
44789435951eSToby Isaac   dm->defaultConstraintMat = mat;
44799435951eSToby Isaac   PetscFunctionReturn(0);
44809435951eSToby Isaac }
44819435951eSToby Isaac 
4482497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4483507e4973SMatthew G. Knepley /*
4484507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4485507e4973SMatthew G. Knepley 
4486507e4973SMatthew G. Knepley   Input Parameters:
4487507e4973SMatthew G. Knepley + dm - The DM
4488507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4489507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4490507e4973SMatthew G. Knepley 
4491507e4973SMatthew G. Knepley   Level: intermediate
4492507e4973SMatthew G. Knepley 
44931bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4494507e4973SMatthew G. Knepley */
4495f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4496507e4973SMatthew G. Knepley {
4497507e4973SMatthew G. Knepley   MPI_Comm        comm;
4498507e4973SMatthew G. Knepley   PetscLayout     layout;
4499507e4973SMatthew G. Knepley   const PetscInt *ranges;
4500507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4501507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4502507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4503507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4504507e4973SMatthew G. Knepley 
4505507e4973SMatthew G. Knepley   PetscFunctionBegin;
4506507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4507507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4508ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
4509ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
4510507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4511507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4512507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4513507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4514507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4515507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4516507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4517507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4518f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4519507e4973SMatthew G. Knepley 
4520507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4521507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4522507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4523507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4524507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4525507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4526507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4527507e4973SMatthew 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;}
4528507e4973SMatthew 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;}
4529507e4973SMatthew G. Knepley     if (gdof < 0) {
4530507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4531507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4532507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4533507e4973SMatthew G. Knepley 
4534507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4535507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4536507e4973SMatthew 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;}
4537507e4973SMatthew G. Knepley       }
4538507e4973SMatthew G. Knepley     }
4539507e4973SMatthew G. Knepley   }
4540507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4541507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4542820f2d46SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRMPI(ierr);
4543507e4973SMatthew G. Knepley   if (!gvalid) {
4544507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4545507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4546507e4973SMatthew G. Knepley   }
4547507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4548507e4973SMatthew G. Knepley }
4549f741bcd2SMatthew G. Knepley #endif
4550507e4973SMatthew G. Knepley 
455188ed4aceSMatthew G Knepley /*@
4552e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
455388ed4aceSMatthew G Knepley 
4554d083f849SBarry Smith   Collective on dm
45558b1ab98fSJed Brown 
455688ed4aceSMatthew G Knepley   Input Parameter:
455788ed4aceSMatthew G Knepley . dm - The DM
455888ed4aceSMatthew G Knepley 
455988ed4aceSMatthew G Knepley   Output Parameter:
456088ed4aceSMatthew G Knepley . section - The PetscSection
456188ed4aceSMatthew G Knepley 
456288ed4aceSMatthew G Knepley   Level: intermediate
456388ed4aceSMatthew G Knepley 
456488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
456588ed4aceSMatthew G Knepley 
456692fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
456788ed4aceSMatthew G Knepley @*/
4568e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
45690adebc6cSBarry Smith {
457088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
457188ed4aceSMatthew G Knepley 
457288ed4aceSMatthew G Knepley   PetscFunctionBegin;
457388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
457488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
45751bb6d2a8SBarry Smith   if (!dm->globalSection) {
4576fd59a867SMatthew G. Knepley     PetscSection s;
4577fd59a867SMatthew G. Knepley 
457892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4579*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!s,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
4580*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!dm->sf,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45811bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4582cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
45831bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
45841bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
458588ed4aceSMatthew G Knepley   }
45861bb6d2a8SBarry Smith   *section = dm->globalSection;
458788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
458888ed4aceSMatthew G Knepley }
458988ed4aceSMatthew G Knepley 
4590b21d0597SMatthew G Knepley /*@
4591e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4592b21d0597SMatthew G Knepley 
4593b21d0597SMatthew G Knepley   Input Parameters:
4594b21d0597SMatthew G Knepley + dm - The DM
45955080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4596b21d0597SMatthew G Knepley 
4597b21d0597SMatthew G Knepley   Level: intermediate
4598b21d0597SMatthew G Knepley 
4599b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4600b21d0597SMatthew G Knepley 
460192fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4602b21d0597SMatthew G Knepley @*/
4603e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
46040adebc6cSBarry Smith {
4605b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4606b21d0597SMatthew G Knepley 
4607b21d0597SMatthew G Knepley   PetscFunctionBegin;
4608b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46095080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
46101d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
46111bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
46121bb6d2a8SBarry Smith   dm->globalSection = section;
4613497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46141bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4615507e4973SMatthew G. Knepley #endif
4616b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4617b21d0597SMatthew G Knepley }
4618b21d0597SMatthew G Knepley 
461988ed4aceSMatthew G Knepley /*@
46201bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
462188ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
462288ed4aceSMatthew G Knepley 
462388ed4aceSMatthew G Knepley   Input Parameter:
462488ed4aceSMatthew G Knepley . dm - The DM
462588ed4aceSMatthew G Knepley 
462688ed4aceSMatthew G Knepley   Output Parameter:
462788ed4aceSMatthew G Knepley . sf - The PetscSF
462888ed4aceSMatthew G Knepley 
462988ed4aceSMatthew G Knepley   Level: intermediate
463088ed4aceSMatthew G Knepley 
463188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
463288ed4aceSMatthew G Knepley 
46331bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
463488ed4aceSMatthew G Knepley @*/
46351bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
46360adebc6cSBarry Smith {
463788ed4aceSMatthew G Knepley   PetscInt       nroots;
463888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
463988ed4aceSMatthew G Knepley 
464088ed4aceSMatthew G Knepley   PetscFunctionBegin;
464188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
464288ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
46431bb6d2a8SBarry Smith   if (!dm->sectionSF) {
46441bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
464533907cc2SStefano Zampini   }
46461bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
464788ed4aceSMatthew G Knepley   if (nroots < 0) {
464888ed4aceSMatthew G Knepley     PetscSection section, gSection;
464988ed4aceSMatthew G Knepley 
465092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
465131ea6d37SMatthew G Knepley     if (section) {
4652e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
46531bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
465431ea6d37SMatthew G Knepley     } else {
46550298fd71SBarry Smith       *sf = NULL;
465631ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
465731ea6d37SMatthew G Knepley     }
465888ed4aceSMatthew G Knepley   }
46591bb6d2a8SBarry Smith   *sf = dm->sectionSF;
466088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
466188ed4aceSMatthew G Knepley }
466288ed4aceSMatthew G Knepley 
466388ed4aceSMatthew G Knepley /*@
46641bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
466588ed4aceSMatthew G Knepley 
466688ed4aceSMatthew G Knepley   Input Parameters:
466788ed4aceSMatthew G Knepley + dm - The DM
466888ed4aceSMatthew G Knepley - sf - The PetscSF
466988ed4aceSMatthew G Knepley 
467088ed4aceSMatthew G Knepley   Level: intermediate
467188ed4aceSMatthew G Knepley 
467288ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
467388ed4aceSMatthew G Knepley 
46741bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
467588ed4aceSMatthew G Knepley @*/
46761bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
46770adebc6cSBarry Smith {
467888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
467988ed4aceSMatthew G Knepley 
468088ed4aceSMatthew G Knepley   PetscFunctionBegin;
468188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4682b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
468333907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
46841bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
46851bb6d2a8SBarry Smith   dm->sectionSF = sf;
468688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
468788ed4aceSMatthew G Knepley }
468888ed4aceSMatthew G Knepley 
468988ed4aceSMatthew G Knepley /*@C
46901bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
469188ed4aceSMatthew G Knepley   describing the data layout.
469288ed4aceSMatthew G Knepley 
469388ed4aceSMatthew G Knepley   Input Parameters:
469488ed4aceSMatthew G Knepley + dm - The DM
469588ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
469688ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
469788ed4aceSMatthew G Knepley 
46981bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
469988ed4aceSMatthew G Knepley 
47001bb6d2a8SBarry Smith   Level: developer
47011bb6d2a8SBarry Smith 
47021bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
47031bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
47041bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
47051bb6d2a8SBarry Smith 
47061bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
470788ed4aceSMatthew G Knepley @*/
47081bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
470988ed4aceSMatthew G Knepley {
471088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
471188ed4aceSMatthew G Knepley 
471288ed4aceSMatthew G Knepley   PetscFunctionBegin;
471388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4714b0c7db22SLisandro Dalcin   ierr = PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection);CHKERRQ(ierr);
471588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
471688ed4aceSMatthew G Knepley }
4717af122d2aSMatthew G Knepley 
4718b21d0597SMatthew G Knepley /*@
4719b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4720b21d0597SMatthew G Knepley 
4721b21d0597SMatthew G Knepley   Input Parameter:
4722b21d0597SMatthew G Knepley . dm - The DM
4723b21d0597SMatthew G Knepley 
4724b21d0597SMatthew G Knepley   Output Parameter:
4725b21d0597SMatthew G Knepley . sf - The PetscSF
4726b21d0597SMatthew G Knepley 
4727b21d0597SMatthew G Knepley   Level: intermediate
4728b21d0597SMatthew G Knepley 
4729b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4730b21d0597SMatthew G Knepley 
47311bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4732b21d0597SMatthew G Knepley @*/
47330adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
47340adebc6cSBarry Smith {
4735b21d0597SMatthew G Knepley   PetscFunctionBegin;
4736b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4737b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4738b21d0597SMatthew G Knepley   *sf = dm->sf;
4739b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4740b21d0597SMatthew G Knepley }
4741b21d0597SMatthew G Knepley 
4742057b4bcdSMatthew G Knepley /*@
4743057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4744057b4bcdSMatthew G Knepley 
4745057b4bcdSMatthew G Knepley   Input Parameters:
4746057b4bcdSMatthew G Knepley + dm - The DM
4747057b4bcdSMatthew G Knepley - sf - The PetscSF
4748057b4bcdSMatthew G Knepley 
4749057b4bcdSMatthew G Knepley   Level: intermediate
4750057b4bcdSMatthew G Knepley 
47511bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4752057b4bcdSMatthew G Knepley @*/
47530adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
47540adebc6cSBarry Smith {
4755057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4756057b4bcdSMatthew G Knepley 
4757057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4758057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4759b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4760057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
476133907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4762057b4bcdSMatthew G Knepley   dm->sf = sf;
4763057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4764057b4bcdSMatthew G Knepley }
4765057b4bcdSMatthew G Knepley 
47664f37162bSMatthew G. Knepley /*@
47674f37162bSMatthew G. Knepley   DMGetNaturalSF - Get the PetscSF encoding the map back to the original mesh ordering
47684f37162bSMatthew G. Knepley 
47694f37162bSMatthew G. Knepley   Input Parameter:
47704f37162bSMatthew G. Knepley . dm - The DM
47714f37162bSMatthew G. Knepley 
47724f37162bSMatthew G. Knepley   Output Parameter:
47734f37162bSMatthew G. Knepley . sf - The PetscSF
47744f37162bSMatthew G. Knepley 
47754f37162bSMatthew G. Knepley   Level: intermediate
47764f37162bSMatthew G. Knepley 
47774f37162bSMatthew G. Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
47784f37162bSMatthew G. Knepley 
47794f37162bSMatthew G. Knepley .seealso: DMSetNaturalSF(), DMSetUseNatural(), DMGetUseNatural(), DMPlexCreateGlobalToNaturalSF(), DMPlexDistribute()
47804f37162bSMatthew G. Knepley @*/
47814f37162bSMatthew G. Knepley PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
47824f37162bSMatthew G. Knepley {
47834f37162bSMatthew G. Knepley   PetscFunctionBegin;
47844f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47854f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
47864f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
47874f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
47884f37162bSMatthew G. Knepley }
47894f37162bSMatthew G. Knepley 
47904f37162bSMatthew G. Knepley /*@
47914f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
47924f37162bSMatthew G. Knepley 
47934f37162bSMatthew G. Knepley   Input Parameters:
47944f37162bSMatthew G. Knepley + dm - The DM
47954f37162bSMatthew G. Knepley - sf - The PetscSF
47964f37162bSMatthew G. Knepley 
47974f37162bSMatthew G. Knepley   Level: intermediate
47984f37162bSMatthew G. Knepley 
47994f37162bSMatthew G. Knepley .seealso: DMGetNaturalSF(), DMSetUseNatural(), DMGetUseNatural(), DMPlexCreateGlobalToNaturalSF(), DMPlexDistribute()
48004f37162bSMatthew G. Knepley @*/
48014f37162bSMatthew G. Knepley PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
48024f37162bSMatthew G. Knepley {
48034f37162bSMatthew G. Knepley   PetscErrorCode ierr;
48044f37162bSMatthew G. Knepley 
48054f37162bSMatthew G. Knepley   PetscFunctionBegin;
48064f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48074f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48084f37162bSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
48094f37162bSMatthew G. Knepley   ierr = PetscSFDestroy(&dm->sfNatural);CHKERRQ(ierr);
48104f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48114f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
48124f37162bSMatthew G. Knepley }
48134f37162bSMatthew G. Knepley 
481434aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
481534aa8a36SMatthew G. Knepley {
481634aa8a36SMatthew G. Knepley   PetscClassId   id;
481734aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
481834aa8a36SMatthew G. Knepley 
481934aa8a36SMatthew G. Knepley   PetscFunctionBegin;
482034aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
482134aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
482234aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
482334aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
482434aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
482517c1d62eSMatthew G. Knepley   } else {
482617c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
482734aa8a36SMatthew G. Knepley   }
482834aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
482934aa8a36SMatthew G. Knepley }
483034aa8a36SMatthew G. Knepley 
483144a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
483244a7f3ddSMatthew G. Knepley {
483344a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
483444a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
483544a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
483644a7f3ddSMatthew G. Knepley 
483744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
483844a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
483944a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
484044a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4841e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
484244a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
484344a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
484444a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
484544a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
484644a7f3ddSMatthew G. Knepley }
484744a7f3ddSMatthew G. Knepley 
484844a7f3ddSMatthew G. Knepley /*@
484944a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
485044a7f3ddSMatthew G. Knepley 
4851d083f849SBarry Smith   Logically collective on dm
485244a7f3ddSMatthew G. Knepley 
485344a7f3ddSMatthew G. Knepley   Input Parameter:
485444a7f3ddSMatthew G. Knepley . dm - The DM
485544a7f3ddSMatthew G. Knepley 
485644a7f3ddSMatthew G. Knepley   Level: intermediate
485744a7f3ddSMatthew G. Knepley 
485844a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
485944a7f3ddSMatthew G. Knepley @*/
486044a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
486144a7f3ddSMatthew G. Knepley {
486244a7f3ddSMatthew G. Knepley   PetscInt       f;
486344a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
486444a7f3ddSMatthew G. Knepley 
486544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
486644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
486744a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
486844a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
486944a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
487044a7f3ddSMatthew G. Knepley   }
487144a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
487244a7f3ddSMatthew G. Knepley   dm->fields = NULL;
487344a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
487444a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
487544a7f3ddSMatthew G. Knepley }
487644a7f3ddSMatthew G. Knepley 
4877689b5837SMatthew G. Knepley /*@
4878689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4879689b5837SMatthew G. Knepley 
4880689b5837SMatthew G. Knepley   Not collective
4881689b5837SMatthew G. Knepley 
4882689b5837SMatthew G. Knepley   Input Parameter:
4883689b5837SMatthew G. Knepley . dm - The DM
4884689b5837SMatthew G. Knepley 
4885689b5837SMatthew G. Knepley   Output Parameter:
4886689b5837SMatthew G. Knepley . Nf - The number of fields
4887689b5837SMatthew G. Knepley 
4888689b5837SMatthew G. Knepley   Level: intermediate
4889689b5837SMatthew G. Knepley 
4890689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4891689b5837SMatthew G. Knepley @*/
48920f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
48930f21e855SMatthew G. Knepley {
48940f21e855SMatthew G. Knepley   PetscFunctionBegin;
48950f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4896534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
489744a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4898af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4899af122d2aSMatthew G Knepley }
4900af122d2aSMatthew G Knepley 
4901689b5837SMatthew G. Knepley /*@
4902689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4903689b5837SMatthew G. Knepley 
4904d083f849SBarry Smith   Logically collective on dm
4905689b5837SMatthew G. Knepley 
4906689b5837SMatthew G. Knepley   Input Parameters:
4907689b5837SMatthew G. Knepley + dm - The DM
4908689b5837SMatthew G. Knepley - Nf - The number of fields
4909689b5837SMatthew G. Knepley 
4910689b5837SMatthew G. Knepley   Level: intermediate
4911689b5837SMatthew G. Knepley 
4912689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4913689b5837SMatthew G. Knepley @*/
4914af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4915af122d2aSMatthew G Knepley {
49160f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4917af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4918af122d2aSMatthew G Knepley 
4919af122d2aSMatthew G Knepley   PetscFunctionBegin;
4920af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
492144a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
49220f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49230f21e855SMatthew G. Knepley     PetscContainer obj;
49240f21e855SMatthew G. Knepley 
49250f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
492644a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
49270f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4928af122d2aSMatthew G Knepley   }
4929af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4930af122d2aSMatthew G Knepley }
4931af122d2aSMatthew G Knepley 
4932c1929be8SMatthew G. Knepley /*@
4933c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4934c1929be8SMatthew G. Knepley 
4935c1929be8SMatthew G. Knepley   Not collective
4936c1929be8SMatthew G. Knepley 
4937c1929be8SMatthew G. Knepley   Input Parameters:
4938c1929be8SMatthew G. Knepley + dm - The DM
4939c1929be8SMatthew G. Knepley - f  - The field number
4940c1929be8SMatthew G. Knepley 
494144a7f3ddSMatthew G. Knepley   Output Parameters:
494244a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
494344a7f3ddSMatthew G. Knepley - field - The discretization object
4944c1929be8SMatthew G. Knepley 
494544a7f3ddSMatthew G. Knepley   Level: intermediate
4946c1929be8SMatthew G. Knepley 
494744a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4948c1929be8SMatthew G. Knepley @*/
494944a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4950af122d2aSMatthew G Knepley {
4951af122d2aSMatthew G Knepley   PetscFunctionBegin;
4952af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4953064a246eSJacob Faibussowitsch   PetscValidPointer(field, 4);
4954*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse((f < 0) || (f >= dm->Nf),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, dm->Nf);
495544a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
495644a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4957decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4958decb47aaSMatthew G. Knepley }
4959decb47aaSMatthew G. Knepley 
4960083401c6SMatthew G. Knepley /* Does not clear the DS */
4961083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4962083401c6SMatthew G. Knepley {
4963083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4964083401c6SMatthew G. Knepley 
4965083401c6SMatthew G. Knepley   PetscFunctionBegin;
4966083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4967083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4968083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4969083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4970083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4971083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4972083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4973083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4974083401c6SMatthew G. Knepley }
4975083401c6SMatthew G. Knepley 
4976c1929be8SMatthew G. Knepley /*@
4977c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4978c1929be8SMatthew G. Knepley 
4979d083f849SBarry Smith   Logically collective on dm
4980c1929be8SMatthew G. Knepley 
4981c1929be8SMatthew G. Knepley   Input Parameters:
4982c1929be8SMatthew G. Knepley + dm    - The DM
4983c1929be8SMatthew G. Knepley . f     - The field number
498444a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4985c1929be8SMatthew G. Knepley - field - The discretization object
4986c1929be8SMatthew G. Knepley 
498744a7f3ddSMatthew G. Knepley   Level: intermediate
4988c1929be8SMatthew G. Knepley 
498944a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4990c1929be8SMatthew G. Knepley @*/
499144a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4992decb47aaSMatthew G. Knepley {
4993decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4994decb47aaSMatthew G. Knepley 
4995decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4996decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4997e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
499844a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4999*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(f < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
5000083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
500134aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
50022df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
500344a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
500444a7f3ddSMatthew G. Knepley }
500544a7f3ddSMatthew G. Knepley 
500644a7f3ddSMatthew G. Knepley /*@
500744a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
500844a7f3ddSMatthew G. Knepley 
5009d083f849SBarry Smith   Logically collective on dm
501044a7f3ddSMatthew G. Knepley 
501144a7f3ddSMatthew G. Knepley   Input Parameters:
501244a7f3ddSMatthew G. Knepley + dm    - The DM
501344a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
501444a7f3ddSMatthew G. Knepley - field - The discretization object
501544a7f3ddSMatthew G. Knepley 
501644a7f3ddSMatthew G. Knepley   Level: intermediate
501744a7f3ddSMatthew G. Knepley 
501844a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
501944a7f3ddSMatthew G. Knepley @*/
502044a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
502144a7f3ddSMatthew G. Knepley {
502244a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
502344a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
502444a7f3ddSMatthew G. Knepley 
502544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
502644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5027064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
502844a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
502944a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
503044a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
503144a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
503244a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
503344a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
503434aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
50352df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
5036af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
5037af122d2aSMatthew G Knepley }
50386636e97aSMatthew G Knepley 
5039e5e52638SMatthew G. Knepley /*@
5040e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5041e0b68406SMatthew Knepley 
5042e0b68406SMatthew Knepley   Logically collective on dm
5043e0b68406SMatthew Knepley 
5044e0b68406SMatthew Knepley   Input Parameters:
5045e0b68406SMatthew Knepley + dm          - The DM
5046e0b68406SMatthew Knepley . f           - The field index
5047e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
5048e0b68406SMatthew Knepley 
5049e0b68406SMatthew Knepley   Level: intermediate
5050e0b68406SMatthew Knepley 
5051e0b68406SMatthew Knepley .seealso: DMGetFieldAvoidTensor(), DMSetField(), DMGetField()
5052e0b68406SMatthew Knepley @*/
5053e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5054e0b68406SMatthew Knepley {
5055e0b68406SMatthew Knepley   PetscFunctionBegin;
5056*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse((f < 0) || (f >= dm->Nf),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
5057e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
5058e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5059e0b68406SMatthew Knepley }
5060e0b68406SMatthew Knepley 
5061e0b68406SMatthew Knepley /*@
5062e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5063e0b68406SMatthew Knepley 
5064e0b68406SMatthew Knepley   Logically collective on dm
5065e0b68406SMatthew Knepley 
5066e0b68406SMatthew Knepley   Input Parameters:
5067e0b68406SMatthew Knepley + dm          - The DM
5068e0b68406SMatthew Knepley - f           - The field index
5069e0b68406SMatthew Knepley 
5070e0b68406SMatthew Knepley   Output Parameter:
5071e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5072e0b68406SMatthew Knepley 
5073e0b68406SMatthew Knepley   Level: intermediate
5074e0b68406SMatthew Knepley 
5075e0b68406SMatthew Knepley .seealso: DMSetFieldAvoidTensor(), DMSetField(), DMGetField()
5076e0b68406SMatthew Knepley @*/
5077e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5078e0b68406SMatthew Knepley {
5079e0b68406SMatthew Knepley   PetscFunctionBegin;
5080*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse((f < 0) || (f >= dm->Nf),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
5081e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
5082e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5083e0b68406SMatthew Knepley }
5084e0b68406SMatthew Knepley 
5085e0b68406SMatthew Knepley /*@
5086e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
5087e5e52638SMatthew G. Knepley 
5088d083f849SBarry Smith   Collective on dm
5089e5e52638SMatthew G. Knepley 
5090e5e52638SMatthew G. Knepley   Input Parameter:
5091e5e52638SMatthew G. Knepley . dm - The DM
5092e5e52638SMatthew G. Knepley 
5093e5e52638SMatthew G. Knepley   Output Parameter:
5094e5e52638SMatthew G. Knepley . newdm - The DM
5095e5e52638SMatthew G. Knepley 
5096e5e52638SMatthew G. Knepley   Level: advanced
5097e5e52638SMatthew G. Knepley 
5098e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
5099e5e52638SMatthew G. Knepley @*/
5100e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
5101e5e52638SMatthew G. Knepley {
5102e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
5103e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5104e5e52638SMatthew G. Knepley 
5105e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5106e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5107e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5108e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
5109e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5110e5e52638SMatthew G. Knepley     DMLabel     label;
5111e5e52638SMatthew G. Knepley     PetscObject field;
511234aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5113e5e52638SMatthew G. Knepley 
5114e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
5115e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
511634aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
511734aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
511834aa8a36SMatthew G. Knepley   }
511934aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
512034aa8a36SMatthew G. Knepley }
512134aa8a36SMatthew G. Knepley 
512234aa8a36SMatthew G. Knepley /*@
512334aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
512434aa8a36SMatthew G. Knepley 
512534aa8a36SMatthew G. Knepley   Not collective
512634aa8a36SMatthew G. Knepley 
512734aa8a36SMatthew G. Knepley   Input Parameters:
512834aa8a36SMatthew G. Knepley + dm - The DM object
512934aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
513034aa8a36SMatthew G. Knepley 
5131d8d19677SJose E. Roman   Output Parameters:
513234aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
513334aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
513434aa8a36SMatthew G. Knepley 
513534aa8a36SMatthew G. Knepley   Notes:
513634aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
513734aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
513834aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5139979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
514034aa8a36SMatthew G. Knepley 
514134aa8a36SMatthew G. Knepley   Level: developer
514234aa8a36SMatthew G. Knepley 
514334aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
514434aa8a36SMatthew G. Knepley @*/
514534aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
514634aa8a36SMatthew G. Knepley {
514734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
514834aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5149534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
5150534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
515134aa8a36SMatthew G. Knepley   if (f < 0) {
515234aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
515334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
515434aa8a36SMatthew G. Knepley   } else {
515534aa8a36SMatthew G. Knepley     PetscInt       Nf;
515634aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
515734aa8a36SMatthew G. Knepley 
515834aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5159*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(f >= Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
516034aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
516134aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
516234aa8a36SMatthew G. Knepley   }
516334aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
516434aa8a36SMatthew G. Knepley }
516534aa8a36SMatthew G. Knepley 
516634aa8a36SMatthew G. Knepley /*@
516734aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
516834aa8a36SMatthew G. Knepley 
516934aa8a36SMatthew G. Knepley   Not collective
517034aa8a36SMatthew G. Knepley 
517134aa8a36SMatthew G. Knepley   Input Parameters:
517234aa8a36SMatthew G. Knepley + dm         - The DM object
517334aa8a36SMatthew G. Knepley . f          - The field number
517434aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
517534aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
517634aa8a36SMatthew G. Knepley 
517734aa8a36SMatthew G. Knepley   Notes:
517834aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
517934aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
518034aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5181979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
518234aa8a36SMatthew G. Knepley 
518334aa8a36SMatthew G. Knepley   Level: developer
518434aa8a36SMatthew G. Knepley 
518534aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
518634aa8a36SMatthew G. Knepley @*/
518734aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
518834aa8a36SMatthew G. Knepley {
518934aa8a36SMatthew G. Knepley   PetscFunctionBegin;
519034aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
519134aa8a36SMatthew G. Knepley   if (f < 0) {
519234aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
519334aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
519434aa8a36SMatthew G. Knepley   } else {
519534aa8a36SMatthew G. Knepley     PetscInt       Nf;
519634aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
519734aa8a36SMatthew G. Knepley 
519834aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5199*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(f >= Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
520034aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
520134aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5202e5e52638SMatthew G. Knepley   }
5203e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5204e5e52638SMatthew G. Knepley }
5205e5e52638SMatthew G. Knepley 
5206b0441da4SMatthew G. Knepley /*@
5207b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5208b0441da4SMatthew G. Knepley 
5209b0441da4SMatthew G. Knepley   Not collective
5210b0441da4SMatthew G. Knepley 
5211f899ff85SJose E. Roman   Input Parameter:
5212b0441da4SMatthew G. Knepley . dm - The DM object
5213b0441da4SMatthew G. Knepley 
5214d8d19677SJose E. Roman   Output Parameters:
5215b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5216b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5217b0441da4SMatthew G. Knepley 
5218b0441da4SMatthew G. Knepley   Notes:
5219b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5220b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5221b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5222b0441da4SMatthew G. Knepley 
5223b0441da4SMatthew G. Knepley   Level: developer
5224b0441da4SMatthew G. Knepley 
5225b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
5226b0441da4SMatthew G. Knepley @*/
5227b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5228b0441da4SMatthew G. Knepley {
5229b0441da4SMatthew G. Knepley   PetscInt       Nf;
5230b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5231b0441da4SMatthew G. Knepley 
5232b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5233b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5234064a246eSJacob Faibussowitsch   if (useCone)    PetscValidBoolPointer(useCone, 2);
5235064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
5236b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5237b0441da4SMatthew G. Knepley   if (!Nf) {
5238b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5239b0441da4SMatthew G. Knepley   } else {
5240b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5241b0441da4SMatthew G. Knepley   }
5242b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5243b0441da4SMatthew G. Knepley }
5244b0441da4SMatthew G. Knepley 
5245b0441da4SMatthew G. Knepley /*@
5246b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5247b0441da4SMatthew G. Knepley 
5248b0441da4SMatthew G. Knepley   Not collective
5249b0441da4SMatthew G. Knepley 
5250b0441da4SMatthew G. Knepley   Input Parameters:
5251b0441da4SMatthew G. Knepley + dm         - The DM object
5252b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5253b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5254b0441da4SMatthew G. Knepley 
5255b0441da4SMatthew G. Knepley   Notes:
5256b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5257b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5258b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5259b0441da4SMatthew G. Knepley 
5260b0441da4SMatthew G. Knepley   Level: developer
5261b0441da4SMatthew G. Knepley 
5262b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
5263b0441da4SMatthew G. Knepley @*/
5264b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5265b0441da4SMatthew G. Knepley {
5266b0441da4SMatthew G. Knepley   PetscInt       Nf;
5267b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5268b0441da4SMatthew G. Knepley 
5269b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5270b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5271b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5272b0441da4SMatthew G. Knepley   if (!Nf) {
5273b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5274b0441da4SMatthew G. Knepley   } else {
5275b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5276e5e52638SMatthew G. Knepley   }
5277e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5278e5e52638SMatthew G. Knepley }
5279e5e52638SMatthew G. Knepley 
5280783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
528145480ffeSMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, DMLabel label)
5282783e2ec8SMatthew G. Knepley {
5283783e2ec8SMatthew G. Knepley   PetscObject    obj;
5284783e2ec8SMatthew G. Knepley   PetscClassId   id;
5285783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
5286783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
5287783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
5288783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
5289783e2ec8SMatthew G. Knepley 
5290783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5291783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
5292783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5293783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
5294783e2ec8SMatthew G. Knepley   if (isFE && label) {
5295783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
5296783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5297783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
529845480ffeSMatthew G. Knepley       DMLabel l;
5299783e2ec8SMatthew G. Knepley 
530045480ffeSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, NULL, &l, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
530145480ffeSMatthew G. Knepley       duplicate = l == label ? PETSC_TRUE : PETSC_FALSE;
5302783e2ec8SMatthew G. Knepley       if (duplicate) break;
5303783e2ec8SMatthew G. Knepley     }
5304783e2ec8SMatthew G. Knepley     if (!duplicate) {
5305783e2ec8SMatthew G. Knepley       DM plex;
5306783e2ec8SMatthew G. Knepley 
5307783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
5308783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
5309783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
5310783e2ec8SMatthew G. Knepley     }
5311783e2ec8SMatthew G. Knepley   }
5312783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5313783e2ec8SMatthew G. Knepley }
5314783e2ec8SMatthew G. Knepley 
5315e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5316e5e52638SMatthew G. Knepley {
5317e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5318e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5319e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5320e5e52638SMatthew G. Knepley 
5321e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5322e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5323e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
5324e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5325b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5326e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5327e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5328e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5329e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5330e5e52638SMatthew G. Knepley }
5331e5e52638SMatthew G. Knepley 
5332e5e52638SMatthew G. Knepley /*@
5333e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5334e5e52638SMatthew G. Knepley 
5335e5e52638SMatthew G. Knepley   Not collective
5336e5e52638SMatthew G. Knepley 
5337e5e52638SMatthew G. Knepley   Input Parameter:
5338e5e52638SMatthew G. Knepley . dm - The DM
5339e5e52638SMatthew G. Knepley 
5340e5e52638SMatthew G. Knepley   Output Parameter:
5341e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5342e5e52638SMatthew G. Knepley 
5343e5e52638SMatthew G. Knepley   Level: intermediate
5344e5e52638SMatthew G. Knepley 
5345e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5346e5e52638SMatthew G. Knepley @*/
5347e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5348e5e52638SMatthew G. Knepley {
5349e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5350e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5351534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5352e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5353e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5354e5e52638SMatthew G. Knepley }
5355e5e52638SMatthew G. Knepley 
5356e5e52638SMatthew G. Knepley /*@
5357e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5358e5e52638SMatthew G. Knepley 
5359d083f849SBarry Smith   Logically collective on dm
5360e5e52638SMatthew G. Knepley 
5361e5e52638SMatthew G. Knepley   Input Parameter:
5362e5e52638SMatthew G. Knepley . dm - The DM
5363e5e52638SMatthew G. Knepley 
5364e5e52638SMatthew G. Knepley   Level: intermediate
5365e5e52638SMatthew G. Knepley 
5366e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5367e5e52638SMatthew G. Knepley @*/
5368e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5369e5e52638SMatthew G. Knepley {
5370e5e52638SMatthew G. Knepley   PetscInt       s;
5371e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5372e5e52638SMatthew G. Knepley 
5373e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5374e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5375e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5376e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5377e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5378b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5379e5e52638SMatthew G. Knepley   }
5380e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5381e5e52638SMatthew G. Knepley   dm->probs = NULL;
5382e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5383e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5384e5e52638SMatthew G. Knepley }
5385e5e52638SMatthew G. Knepley 
5386e5e52638SMatthew G. Knepley /*@
5387e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5388e5e52638SMatthew G. Knepley 
5389e5e52638SMatthew G. Knepley   Not collective
5390e5e52638SMatthew G. Knepley 
5391e5e52638SMatthew G. Knepley   Input Parameter:
5392e5e52638SMatthew G. Knepley . dm    - The DM
5393e5e52638SMatthew G. Knepley 
5394e5e52638SMatthew G. Knepley   Output Parameter:
5395e5e52638SMatthew G. Knepley . prob - The default PetscDS
5396e5e52638SMatthew G. Knepley 
5397e5e52638SMatthew G. Knepley   Level: intermediate
5398e5e52638SMatthew G. Knepley 
5399e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5400e5e52638SMatthew G. Knepley @*/
5401e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5402e5e52638SMatthew G. Knepley {
5403b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5404b0143b4dSMatthew G. Knepley 
5405e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5406e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5407e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5408b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5409b0143b4dSMatthew G. Knepley     PetscDS ds;
5410b0143b4dSMatthew G. Knepley 
541145480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5412b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5413b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5414b0143b4dSMatthew G. Knepley   }
5415b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5416e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5417e5e52638SMatthew G. Knepley }
5418e5e52638SMatthew G. Knepley 
5419e5e52638SMatthew G. Knepley /*@
5420e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5421e5e52638SMatthew G. Knepley 
5422e5e52638SMatthew G. Knepley   Not collective
5423e5e52638SMatthew G. Knepley 
5424e5e52638SMatthew G. Knepley   Input Parameters:
5425e5e52638SMatthew G. Knepley + dm    - The DM
5426e5e52638SMatthew G. Knepley - point - Cell for the DS
5427e5e52638SMatthew G. Knepley 
5428e5e52638SMatthew G. Knepley   Output Parameter:
5429e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5430e5e52638SMatthew G. Knepley 
5431e5e52638SMatthew G. Knepley   Level: developer
5432e5e52638SMatthew G. Knepley 
5433b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5434e5e52638SMatthew G. Knepley @*/
5435e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5436e5e52638SMatthew G. Knepley {
5437e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5438e5e52638SMatthew G. Knepley   PetscInt       s;
5439e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5440e5e52638SMatthew G. Knepley 
5441e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5442e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5443e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5444*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(point < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5445e5e52638SMatthew G. Knepley   *prob = NULL;
5446e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5447e5e52638SMatthew G. Knepley     PetscInt val;
5448e5e52638SMatthew G. Knepley 
5449e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5450e5e52638SMatthew G. Knepley     else {
5451e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5452e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5453e5e52638SMatthew G. Knepley     }
5454e5e52638SMatthew G. Knepley   }
5455e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5456e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5457e5e52638SMatthew G. Knepley }
5458e5e52638SMatthew G. Knepley 
5459e5e52638SMatthew G. Knepley /*@
5460e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5461e5e52638SMatthew G. Knepley 
5462e5e52638SMatthew G. Knepley   Not collective
5463e5e52638SMatthew G. Knepley 
5464e5e52638SMatthew G. Knepley   Input Parameters:
5465e5e52638SMatthew G. Knepley + dm    - The DM
5466e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5467e5e52638SMatthew G. Knepley 
5468b3cf3223SMatthew G. Knepley   Output Parameters:
5469b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5470b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5471e5e52638SMatthew G. Knepley 
5472154ca461SJed Brown   Note:
5473154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5474154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5475154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5476e5e52638SMatthew G. Knepley 
5477e5e52638SMatthew G. Knepley   Level: advanced
5478e5e52638SMatthew G. Knepley 
5479e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5480e5e52638SMatthew G. Knepley @*/
5481b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5482e5e52638SMatthew G. Knepley {
5483e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5484e5e52638SMatthew G. Knepley 
5485e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5486e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5487e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5488b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5489b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5490e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5491154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5492b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5493b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5494154ca461SJed Brown       if (dm->probs[s].label) PetscFunctionReturn(0);
5495b3cf3223SMatthew G. Knepley     }
5496e5e52638SMatthew G. Knepley   }
54972df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5498e5e52638SMatthew G. Knepley }
5499e5e52638SMatthew G. Knepley 
5500e5e52638SMatthew G. Knepley /*@
5501083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5502083401c6SMatthew G. Knepley 
5503083401c6SMatthew G. Knepley   Collective on dm
5504083401c6SMatthew G. Knepley 
5505083401c6SMatthew G. Knepley   Input Parameters:
5506083401c6SMatthew G. Knepley + dm     - The DM
5507083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5508083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5509083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5510083401c6SMatthew G. Knepley 
5511083401c6SMatthew 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,
5512083401c6SMatthew G. Knepley   the fields argument is ignored.
5513083401c6SMatthew G. Knepley 
5514083401c6SMatthew G. Knepley   Level: advanced
5515083401c6SMatthew G. Knepley 
5516083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5517083401c6SMatthew G. Knepley @*/
5518083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5519083401c6SMatthew G. Knepley {
5520083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5521083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5522083401c6SMatthew G. Knepley 
5523083401c6SMatthew G. Knepley   PetscFunctionBegin;
5524083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5525083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5526064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5527083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5528083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5529083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5530083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5531083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5532083401c6SMatthew G. Knepley     }
5533083401c6SMatthew G. Knepley   }
5534083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5535083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5536083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5537083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5538083401c6SMatthew G. Knepley   if (!label) {
5539083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5540083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5541083401c6SMatthew G. Knepley     Nds = 0;
5542083401c6SMatthew G. Knepley   }
5543083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5544083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5545083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5546083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5547083401c6SMatthew G. Knepley }
5548083401c6SMatthew G. Knepley 
5549083401c6SMatthew G. Knepley /*@
5550e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5551e5e52638SMatthew G. Knepley 
5552e5e52638SMatthew G. Knepley   Not collective
5553e5e52638SMatthew G. Knepley 
5554e5e52638SMatthew G. Knepley   Input Parameters:
5555e5e52638SMatthew G. Knepley + dm  - The DM
5556e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5557e5e52638SMatthew G. Knepley 
5558e5e52638SMatthew G. Knepley   Output Parameters:
5559b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5560b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5561083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5562e5e52638SMatthew G. Knepley 
5563e5e52638SMatthew G. Knepley   Level: advanced
5564e5e52638SMatthew G. Knepley 
5565e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5566e5e52638SMatthew G. Knepley @*/
5567b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5568e5e52638SMatthew G. Knepley {
5569e5e52638SMatthew G. Knepley   PetscInt       Nds;
5570e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5571e5e52638SMatthew G. Knepley 
5572e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5573e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5574e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5575*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse((num < 0) || (num >= Nds),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
5576e5e52638SMatthew G. Knepley   if (label) {
5577e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5578e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5579e5e52638SMatthew G. Knepley   }
5580b3cf3223SMatthew G. Knepley   if (fields) {
5581b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5582b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5583b3cf3223SMatthew G. Knepley   }
5584e5e52638SMatthew G. Knepley   if (ds) {
5585b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5586e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5587e5e52638SMatthew G. Knepley   }
5588e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5589e5e52638SMatthew G. Knepley }
5590e5e52638SMatthew G. Knepley 
5591e5e52638SMatthew G. Knepley /*@
5592083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5593e5e52638SMatthew G. Knepley 
5594083401c6SMatthew G. Knepley   Not collective
5595e5e52638SMatthew G. Knepley 
5596e5e52638SMatthew G. Knepley   Input Parameters:
5597e5e52638SMatthew G. Knepley + dm     - The DM
5598083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5599083401c6SMatthew G. Knepley . label  - The region label, or NULL
5600083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5601083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5602e5e52638SMatthew G. Knepley 
5603e5e52638SMatthew G. Knepley   Level: advanced
5604e5e52638SMatthew G. Knepley 
5605083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5606e5e52638SMatthew G. Knepley @*/
5607083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5608e5e52638SMatthew G. Knepley {
5609083401c6SMatthew G. Knepley   PetscInt       Nds;
5610e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5611e5e52638SMatthew G. Knepley 
5612e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5613e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5614083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5615083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5616*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse((num < 0) || (num >= Nds),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
5617e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5618083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5619083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5620083401c6SMatthew G. Knepley   if (fields) {
5621083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5622b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5623083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5624083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5625e5e52638SMatthew G. Knepley   }
5626083401c6SMatthew G. Knepley   if (ds) {
5627083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5628083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5629083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5630083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5631083401c6SMatthew G. Knepley   }
5632e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5633e5e52638SMatthew G. Knepley }
5634e5e52638SMatthew G. Knepley 
5635e5e52638SMatthew G. Knepley /*@
56361d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
56371d3af9e0SMatthew G. Knepley 
56381d3af9e0SMatthew G. Knepley   Not collective
56391d3af9e0SMatthew G. Knepley 
56401d3af9e0SMatthew G. Knepley   Input Parameters:
56411d3af9e0SMatthew G. Knepley + dm  - The DM
56421d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
56431d3af9e0SMatthew G. Knepley 
56441d3af9e0SMatthew G. Knepley   Output Parameter:
56451d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
56461d3af9e0SMatthew G. Knepley 
56471d3af9e0SMatthew G. Knepley   Level: advanced
56481d3af9e0SMatthew G. Knepley 
56491d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
56501d3af9e0SMatthew G. Knepley @*/
56511d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
56521d3af9e0SMatthew G. Knepley {
56531d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
56541d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
56551d3af9e0SMatthew G. Knepley 
56561d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
56571d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56581d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
56591d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
56601d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
56611d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
56621d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
56631d3af9e0SMatthew G. Knepley   else          *num = n;
56641d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
56651d3af9e0SMatthew G. Knepley }
56661d3af9e0SMatthew G. Knepley 
56671d3af9e0SMatthew G. Knepley /*@
5668e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5669e5e52638SMatthew G. Knepley 
5670d083f849SBarry Smith   Collective on dm
5671e5e52638SMatthew G. Knepley 
5672e5e52638SMatthew G. Knepley   Input Parameter:
5673e5e52638SMatthew G. Knepley . dm - The DM
5674e5e52638SMatthew G. Knepley 
567545480ffeSMatthew G. Knepley   Options Database Keys:
567645480ffeSMatthew G. Knepley . -dm_petscds_view - View all the PetscDS objects in this DM
567745480ffeSMatthew G. Knepley 
5678e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5679e5e52638SMatthew G. Knepley 
5680e5e52638SMatthew G. Knepley   Level: intermediate
5681e5e52638SMatthew G. Knepley 
5682e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5683e5e52638SMatthew G. Knepley @*/
5684e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5685e5e52638SMatthew G. Knepley {
5686e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5687083401c6SMatthew G. Knepley   PetscDS        dsDef;
5688083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5689f9244615SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5690f9244615SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE, flg;
5691e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5692e5e52638SMatthew G. Knepley 
5693e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5694e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5695e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5696e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5697083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5698083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5699083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5700083401c6SMatthew G. Knepley   Nl   = 0;
5701083401c6SMatthew G. Knepley   Ndef = 0;
5702083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5703083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5704083401c6SMatthew G. Knepley     PetscInt l;
5705083401c6SMatthew G. Knepley 
5706f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5707f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5708f918ec44SMatthew G. Knepley     {
5709f918ec44SMatthew G. Knepley       PetscClassId id;
5710f918ec44SMatthew G. Knepley 
5711f918ec44SMatthew G. Knepley       ierr = PetscObjectGetClassId(dm->fields[f].disc, &id);CHKERRQ(ierr);
5712f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5713f918ec44SMatthew G. Knepley         Ceed ceed;
5714f918ec44SMatthew G. Knepley 
5715f918ec44SMatthew G. Knepley         ierr = DMGetCeed(dm, &ceed);CHKERRQ(ierr);
5716f918ec44SMatthew G. Knepley         ierr = PetscFESetCeed((PetscFE) dm->fields[f].disc, ceed);CHKERRQ(ierr);
5717f918ec44SMatthew G. Knepley       }
5718f918ec44SMatthew G. Knepley     }
5719f918ec44SMatthew G. Knepley #endif
5720083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5721083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5722083401c6SMatthew G. Knepley     if (l < Nl) continue;
5723083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5724083401c6SMatthew G. Knepley   }
5725083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5726083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5727083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5728b3cf3223SMatthew G. Knepley     IS        fields;
5729b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5730b3cf3223SMatthew G. Knepley 
5731b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5732*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!nf,comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
5733b3cf3223SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5734b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
573588f0c812SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
573688f0c812SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
573788f0c812SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
573888f0c812SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
573988f0c812SMatthew G. Knepley 
574045480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5741083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5742083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5743b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
57442df9ee95SMatthew G. Knepley   }
5745083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5746083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5747083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5748083401c6SMatthew G. Knepley   if (Ndef && Nl) {
57490122748bSMatthew G. Knepley     DM              plex;
5750083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5751083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5752083401c6SMatthew G. Knepley     PetscInt       *fields;
5753083401c6SMatthew G. Knepley     const PetscInt *cells;
5754083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
57550122748bSMatthew G. Knepley 
57560122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
57570122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5758083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5759083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
57605fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5761083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5762083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5763083401c6SMatthew G. Knepley       IS      pointIS;
5764083401c6SMatthew G. Knepley 
5765083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5766083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5767083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5768083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5769083401c6SMatthew G. Knepley     }
5770083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5771083401c6SMatthew G. Knepley 
5772083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5773083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5774083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5775083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5776083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5777083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5778083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5779083401c6SMatthew G. Knepley 
5780083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5781083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5782083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5783083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5784083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5785083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5786083401c6SMatthew G. Knepley 
578745480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5788083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5789083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
57905fedec97SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5791083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5792083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
57930122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5794083401c6SMatthew G. Knepley   }
5795083401c6SMatthew G. Knepley   /* Create label DSes
5796083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5797083401c6SMatthew G. Knepley   */
5798083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5799083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5800083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5801083401c6SMatthew G. Knepley     PetscDS   ds;
5802083401c6SMatthew G. Knepley     IS        fields;
5803083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5804083401c6SMatthew G. Knepley 
580545480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5806083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5807083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5808083401c6SMatthew G. Knepley     for (f = 0, nf  = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5809083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5810083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5811083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5812083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5813083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5814083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5815083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5816083401c6SMatthew G. Knepley     {
5817083401c6SMatthew G. Knepley       DMPolytopeType ct;
5818083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
58195fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
58200122748bSMatthew G. Knepley 
5821e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5822665f567fSMatthew G. Knepley       if (lStart >= 0) {
5823412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5824412e9a14SMatthew G. Knepley         switch (ct) {
5825412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5826412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5827412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5828412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
58295fedec97SMatthew G. Knepley             isCohesiveLocal = PETSC_TRUE;break;
5830083401c6SMatthew G. Knepley           default: break;
5831412e9a14SMatthew G. Knepley         }
5832665f567fSMatthew G. Knepley       }
58335fedec97SMatthew G. Knepley       ierr = MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRMPI(ierr);
58345fedec97SMatthew G. Knepley       for (f = 0, nf  = 0; f < Nf; ++f) {
58355fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
58365fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
58375fedec97SMatthew G. Knepley             ierr = PetscDSSetDiscretization(ds, nf, NULL);CHKERRQ(ierr);
58385fedec97SMatthew G. Knepley             ierr = PetscDSSetCohesive(ds, nf, isCohesive);CHKERRQ(ierr);
58395fedec97SMatthew G. Knepley           }
58405fedec97SMatthew G. Knepley           ++nf;
58415fedec97SMatthew G. Knepley         }
58425fedec97SMatthew G. Knepley       }
5843e5e52638SMatthew G. Knepley     }
5844083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5845e5e52638SMatthew G. Knepley   }
5846083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5847e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5848083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5849083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5850083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5851083401c6SMatthew G. Knepley     const PetscInt *fld;
58525fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
58535fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5854e5e52638SMatthew G. Knepley 
58555fedec97SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsnf);CHKERRQ(ierr);
58565fedec97SMatthew G. Knepley     ierr = PetscDSIsCohesive(ds, &isCohesive);CHKERRQ(ierr);
5857083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5858083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5859083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5860083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
58615fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5862e5e52638SMatthew G. Knepley       PetscClassId id;
5863e5e52638SMatthew G. Knepley 
58645fedec97SMatthew G. Knepley       /* Handle DS with no fields */
58655fedec97SMatthew G. Knepley       if (dsnf) {ierr = PetscDSGetCohesive(ds, f, &isCohesiveField);CHKERRQ(ierr);}
58665fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
58675fedec97SMatthew G. Knepley       if (isCohesive && !isCohesiveField) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5868083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5869083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5870e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5871e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5872e5e52638SMatthew G. Knepley     }
5873083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5874e5e52638SMatthew G. Knepley   }
5875f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
5876f9244615SMatthew G. Knepley   ierr = PetscOptionsGetInt(NULL, ((PetscObject) dm)->prefix, "-dm_ds_jet_degree", &k, &flg);CHKERRQ(ierr);
5877f9244615SMatthew G. Knepley   if (flg) {
58783b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
58793b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
58803b4aee56SMatthew G. Knepley       PetscInt Nf, f;
58813b4aee56SMatthew G. Knepley 
58823b4aee56SMatthew G. Knepley       ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
58833b4aee56SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {ierr = PetscDSSetJetDegree(ds, f, k);CHKERRQ(ierr);}
58843b4aee56SMatthew G. Knepley     }
5885f9244615SMatthew G. Knepley   }
5886e5e52638SMatthew G. Knepley   /* Setup DSes */
5887e5e52638SMatthew G. Knepley   if (doSetup) {
5888e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5889e5e52638SMatthew G. Knepley   }
5890e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5891e5e52638SMatthew G. Knepley }
5892e5e52638SMatthew G. Knepley 
5893e5e52638SMatthew G. Knepley /*@
58947f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
58957f96f943SMatthew G. Knepley 
5896f2cacb80SMatthew G. Knepley   Collective on DM
5897f2cacb80SMatthew G. Knepley 
58987f96f943SMatthew G. Knepley   Input Parameters:
58997f96f943SMatthew G. Knepley + dm   - The DM
59007f96f943SMatthew G. Knepley - time - The time
59017f96f943SMatthew G. Knepley 
59027f96f943SMatthew G. Knepley   Output Parameters:
5903f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5904f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
59057f96f943SMatthew G. Knepley 
59067f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
59077f96f943SMatthew G. Knepley 
59087f96f943SMatthew G. Knepley   Level: developer
59097f96f943SMatthew G. Knepley 
59107f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
59117f96f943SMatthew G. Knepley @*/
5912f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
59137f96f943SMatthew G. Knepley {
59147f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
59157f96f943SMatthew G. Knepley   void            **ectxs;
59167f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
59177f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
59187f96f943SMatthew G. Knepley 
59197f96f943SMatthew G. Knepley   PetscFunctionBegin;
5920f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5921f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5922f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
59237f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
59247f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
59257f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
59267f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
59277f96f943SMatthew G. Knepley     PetscDS         ds;
59287f96f943SMatthew G. Knepley     DMLabel         label;
59297f96f943SMatthew G. Knepley     IS              fieldIS;
59307f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
59317f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
59327f96f943SMatthew G. Knepley 
59337f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
59347f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
59357f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
59367f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
59377f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5938f2cacb80SMatthew G. Knepley     if (u) {
59397f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
59407f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
59417f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
59427f96f943SMatthew G. Knepley       }
59437f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
59447f96f943SMatthew G. Knepley       if (label) {
59457f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
59467f96f943SMatthew G. Knepley       } else {
59477f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
59487f96f943SMatthew G. Knepley       }
59497f96f943SMatthew G. Knepley     }
5950f2cacb80SMatthew G. Knepley     if (u_t) {
5951f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5952f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5953f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5954f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5955f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5956f2cacb80SMatthew G. Knepley       }
5957f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5958f2cacb80SMatthew G. Knepley       if (label) {
5959f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5960f2cacb80SMatthew G. Knepley       } else {
5961f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5962f2cacb80SMatthew G. Knepley       }
5963f2cacb80SMatthew G. Knepley     }
5964f2cacb80SMatthew G. Knepley   }
5965f2cacb80SMatthew G. Knepley   if (u) {
59667f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
59677f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5968f2cacb80SMatthew G. Knepley   }
5969f2cacb80SMatthew G. Knepley   if (u_t) {
5970f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
5971f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
5972f2cacb80SMatthew G. Knepley   }
59737f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
59747f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
59757f96f943SMatthew G. Knepley }
59767f96f943SMatthew G. Knepley 
597745480ffeSMatthew G. Knepley PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
597845480ffeSMatthew G. Knepley {
597945480ffeSMatthew G. Knepley   PetscDS        dsNew;
598045480ffeSMatthew G. Knepley   DSBoundary     b;
598145480ffeSMatthew G. Knepley   PetscInt       cdim, Nf, f;
59825fedec97SMatthew G. Knepley   PetscBool      isCohesive;
598345480ffeSMatthew G. Knepley   void          *ctx;
598445480ffeSMatthew G. Knepley   PetscErrorCode ierr;
598545480ffeSMatthew G. Knepley 
598645480ffeSMatthew G. Knepley   PetscFunctionBegin;
598745480ffeSMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) ds), &dsNew);CHKERRQ(ierr);
598845480ffeSMatthew G. Knepley   ierr = PetscDSCopyConstants(ds, dsNew);CHKERRQ(ierr);
598945480ffeSMatthew G. Knepley   ierr = PetscDSCopyExactSolutions(ds, dsNew);CHKERRQ(ierr);
599045480ffeSMatthew G. Knepley   ierr = PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
599145480ffeSMatthew G. Knepley   ierr = PetscDSCopyEquations(ds, dsNew);CHKERRQ(ierr);
599245480ffeSMatthew G. Knepley   ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
599345480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
599445480ffeSMatthew G. Knepley     ierr = PetscDSGetContext(ds, f, &ctx);CHKERRQ(ierr);
599545480ffeSMatthew G. Knepley     ierr = PetscDSSetContext(dsNew, f, ctx);CHKERRQ(ierr);
59965fedec97SMatthew G. Knepley     ierr = PetscDSGetCohesive(ds, f, &isCohesive);CHKERRQ(ierr);
59975fedec97SMatthew G. Knepley     ierr = PetscDSSetCohesive(dsNew, f, isCohesive);CHKERRQ(ierr);
599845480ffeSMatthew G. Knepley   }
599945480ffeSMatthew G. Knepley   if (Nf) {
600045480ffeSMatthew G. Knepley     ierr = PetscDSGetCoordinateDimension(ds, &cdim);CHKERRQ(ierr);
600145480ffeSMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsNew, cdim);CHKERRQ(ierr);
600245480ffeSMatthew G. Knepley   }
600345480ffeSMatthew G. Knepley   ierr = PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
600445480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
600545480ffeSMatthew G. Knepley     ierr = DMGetLabel(dm, b->lname, &b->label);CHKERRQ(ierr);
600645480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
6007*2c71b3e2SJacob Faibussowitsch     //PetscCheckFalse(!b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
600845480ffeSMatthew G. Knepley   }
600945480ffeSMatthew G. Knepley 
601045480ffeSMatthew G. Knepley   ierr = DMSetRegionDS(dm, label, fields, dsNew);CHKERRQ(ierr);
601145480ffeSMatthew G. Knepley   ierr = PetscDSDestroy(&dsNew);CHKERRQ(ierr);
601245480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
601345480ffeSMatthew G. Knepley }
601445480ffeSMatthew G. Knepley 
60157f96f943SMatthew G. Knepley /*@
6016e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
6017e5e52638SMatthew G. Knepley 
6018d083f849SBarry Smith   Collective on dm
6019e5e52638SMatthew G. Knepley 
6020e5e52638SMatthew G. Knepley   Input Parameter:
6021e5e52638SMatthew G. Knepley . dm - The DM
6022e5e52638SMatthew G. Knepley 
6023e5e52638SMatthew G. Knepley   Output Parameter:
6024e5e52638SMatthew G. Knepley . newdm - The DM
6025e5e52638SMatthew G. Knepley 
6026e5e52638SMatthew G. Knepley   Level: advanced
6027e5e52638SMatthew G. Knepley 
6028e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
6029e5e52638SMatthew G. Knepley @*/
6030e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
6031e5e52638SMatthew G. Knepley {
6032e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
6033e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
6034e5e52638SMatthew G. Knepley 
6035e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6036e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
6037e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
6038e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
6039e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6040e5e52638SMatthew G. Knepley     DMLabel  label;
6041b3cf3223SMatthew G. Knepley     IS       fields;
604245480ffeSMatthew G. Knepley     PetscDS  ds, newds;
6043783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6044e5e52638SMatthew G. Knepley 
6045b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
6046b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
604745480ffeSMatthew G. Knepley     ierr = DMTransferDS_Internal(newdm, label, fields, ds);CHKERRQ(ierr);
604845480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
604945480ffeSMatthew G. Knepley     ierr = DMGetRegionDS(newdm, label, NULL, &newds);CHKERRQ(ierr);
605045480ffeSMatthew G. Knepley     ierr = PetscDSGetNumBoundary(newds, &Nbd);CHKERRQ(ierr);
6051783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6052b8025e53SMatthew G. Knepley       PetscWeakForm wf;
605345480ffeSMatthew G. Knepley       DMLabel       label;
6054783e2ec8SMatthew G. Knepley       PetscInt      field;
6055783e2ec8SMatthew G. Knepley 
6056b8025e53SMatthew G. Knepley       ierr = PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
605745480ffeSMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, newds, field, bd, label);CHKERRQ(ierr);
6058b8025e53SMatthew G. Knepley       ierr = PetscWeakFormReplaceLabel(wf, label);CHKERRQ(ierr);
6059783e2ec8SMatthew G. Knepley     }
6060e5e52638SMatthew G. Knepley   }
6061e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6062e5e52638SMatthew G. Knepley }
6063e5e52638SMatthew G. Knepley 
6064e5e52638SMatthew G. Knepley /*@
6065e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
6066e5e52638SMatthew G. Knepley 
6067d083f849SBarry Smith   Collective on dm
6068e5e52638SMatthew G. Knepley 
6069e5e52638SMatthew G. Knepley   Input Parameter:
6070e5e52638SMatthew G. Knepley . dm - The DM
6071e5e52638SMatthew G. Knepley 
6072e5e52638SMatthew G. Knepley   Output Parameter:
6073e5e52638SMatthew G. Knepley . newdm - The DM
6074e5e52638SMatthew G. Knepley 
6075e5e52638SMatthew G. Knepley   Level: advanced
6076e5e52638SMatthew G. Knepley 
6077e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
6078e5e52638SMatthew G. Knepley @*/
6079e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6080e5e52638SMatthew G. Knepley {
6081e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
6082e5e52638SMatthew G. Knepley 
6083e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6084e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
6085e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
6086e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6087e5e52638SMatthew G. Knepley }
6088e5e52638SMatthew G. Knepley 
6089b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
6090b64e0483SPeter Brune {
6091b64e0483SPeter Brune   DM dm_coord,dmc_coord;
6092b64e0483SPeter Brune   PetscErrorCode ierr;
6093b64e0483SPeter Brune   Vec coords,ccoords;
60946dbf9973SLawrence Mitchell   Mat inject;
6095b64e0483SPeter Brune   PetscFunctionBegin;
6096b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
6097b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
6098b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
6099b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
6100b64e0483SPeter Brune   if (coords && !ccoords) {
6101b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
61026668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
61036dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
61042adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
61056dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
6106b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
6107b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
6108b64e0483SPeter Brune   }
6109b64e0483SPeter Brune   PetscFunctionReturn(0);
6110b64e0483SPeter Brune }
6111b64e0483SPeter Brune 
611203dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
611303dadc2fSPeter Brune {
611403dadc2fSPeter Brune   DM dm_coord,subdm_coord;
611503dadc2fSPeter Brune   PetscErrorCode ierr;
611603dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
611703dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
611803dadc2fSPeter Brune   PetscFunctionBegin;
611903dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
612003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
612103dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
612203dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
612303dadc2fSPeter Brune   if (coords && !ccoords) {
612403dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
61256668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
612603dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
612724640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
612803dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
612903dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
613003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
61311ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
613203dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
613303dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
613403dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
613503dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
613603dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
613703dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
613803dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
613903dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
614003dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
614103dadc2fSPeter Brune   }
614203dadc2fSPeter Brune   PetscFunctionReturn(0);
614303dadc2fSPeter Brune }
614403dadc2fSPeter Brune 
6145c73cfb54SMatthew G. Knepley /*@
6146c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
6147c73cfb54SMatthew G. Knepley 
6148c73cfb54SMatthew G. Knepley   Not collective
6149c73cfb54SMatthew G. Knepley 
6150c73cfb54SMatthew G. Knepley   Input Parameter:
6151c73cfb54SMatthew G. Knepley . dm - The DM
6152c73cfb54SMatthew G. Knepley 
6153c73cfb54SMatthew G. Knepley   Output Parameter:
6154c73cfb54SMatthew G. Knepley . dim - The topological dimension
6155c73cfb54SMatthew G. Knepley 
6156c73cfb54SMatthew G. Knepley   Level: beginner
6157c73cfb54SMatthew G. Knepley 
6158c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
6159c73cfb54SMatthew G. Knepley @*/
6160c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6161c73cfb54SMatthew G. Knepley {
6162c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6163c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6164534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6165c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6166c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6167c73cfb54SMatthew G. Knepley }
6168c73cfb54SMatthew G. Knepley 
6169c73cfb54SMatthew G. Knepley /*@
6170c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
6171c73cfb54SMatthew G. Knepley 
6172c73cfb54SMatthew G. Knepley   Collective on dm
6173c73cfb54SMatthew G. Knepley 
6174c73cfb54SMatthew G. Knepley   Input Parameters:
6175c73cfb54SMatthew G. Knepley + dm - The DM
6176c73cfb54SMatthew G. Knepley - dim - The topological dimension
6177c73cfb54SMatthew G. Knepley 
6178c73cfb54SMatthew G. Knepley   Level: beginner
6179c73cfb54SMatthew G. Knepley 
6180c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
6181c73cfb54SMatthew G. Knepley @*/
6182c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6183c73cfb54SMatthew G. Knepley {
6184e5e52638SMatthew G. Knepley   PetscDS        ds;
618545480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6186f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6187f17e8794SMatthew G. Knepley 
6188c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6189c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6190c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6191c73cfb54SMatthew G. Knepley   dm->dim = dim;
6192d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
619345480ffeSMatthew G. Knepley     ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
619445480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
619545480ffeSMatthew G. Knepley       ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
619645480ffeSMatthew G. Knepley       if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);}
619745480ffeSMatthew G. Knepley     }
6198d17bd122SMatthew G. Knepley   }
6199c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6200c73cfb54SMatthew G. Knepley }
6201c73cfb54SMatthew G. Knepley 
6202793f3fe5SMatthew G. Knepley /*@
6203793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6204793f3fe5SMatthew G. Knepley 
6205d083f849SBarry Smith   Collective on dm
6206793f3fe5SMatthew G. Knepley 
6207793f3fe5SMatthew G. Knepley   Input Parameters:
6208793f3fe5SMatthew G. Knepley + dm - the DM
6209793f3fe5SMatthew G. Knepley - dim - the dimension
6210793f3fe5SMatthew G. Knepley 
6211793f3fe5SMatthew G. Knepley   Output Parameters:
6212793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6213aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6214793f3fe5SMatthew G. Knepley 
6215793f3fe5SMatthew G. Knepley   Note:
6216793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6217a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6218793f3fe5SMatthew G. Knepley   then the interval is empty.
6219793f3fe5SMatthew G. Knepley 
6220793f3fe5SMatthew G. Knepley   Level: intermediate
6221793f3fe5SMatthew G. Knepley 
6222793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
6223793f3fe5SMatthew G. Knepley @*/
6224793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6225793f3fe5SMatthew G. Knepley {
6226793f3fe5SMatthew G. Knepley   PetscInt       d;
6227793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
6228793f3fe5SMatthew G. Knepley 
6229793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6230793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6231793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
6232*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse((dim < 0) || (dim > d),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
6233*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->getdimpoints,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
6234793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
6235793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6236793f3fe5SMatthew G. Knepley }
6237793f3fe5SMatthew G. Knepley 
62386636e97aSMatthew G Knepley /*@
62396636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
62406636e97aSMatthew G Knepley 
6241d083f849SBarry Smith   Collective on dm
62426636e97aSMatthew G Knepley 
62436636e97aSMatthew G Knepley   Input Parameters:
62446636e97aSMatthew G Knepley + dm - the DM
62456636e97aSMatthew G Knepley - c - coordinate vector
62466636e97aSMatthew G Knepley 
62472dd40e9bSPatrick Sanan   Notes:
62482dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
62492dd40e9bSPatrick Sanan 
62502dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
62516636e97aSMatthew G Knepley 
62526636e97aSMatthew G Knepley   Level: intermediate
62536636e97aSMatthew G Knepley 
625460c22052SBarry Smith .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
62556636e97aSMatthew G Knepley @*/
62566636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
62576636e97aSMatthew G Knepley {
62586636e97aSMatthew G Knepley   PetscErrorCode ierr;
62596636e97aSMatthew G Knepley 
62606636e97aSMatthew G Knepley   PetscFunctionBegin;
62616636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62626636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
62636636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
62646636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
62656636e97aSMatthew G Knepley   dm->coordinates = c;
62666636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
6267b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
626803dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
62696636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62706636e97aSMatthew G Knepley }
62716636e97aSMatthew G Knepley 
62726636e97aSMatthew G Knepley /*@
62736636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
62746636e97aSMatthew G Knepley 
62757058e716SVaclav Hapla   Not collective
62766636e97aSMatthew G Knepley 
62776636e97aSMatthew G Knepley    Input Parameters:
62786636e97aSMatthew G Knepley +  dm - the DM
62796636e97aSMatthew G Knepley -  c - coordinate vector
62806636e97aSMatthew G Knepley 
62812dd40e9bSPatrick Sanan   Notes:
62826636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
62836636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
62846636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
62856636e97aSMatthew G Knepley 
62862dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
62872dd40e9bSPatrick Sanan 
62886636e97aSMatthew G Knepley   Level: intermediate
62896636e97aSMatthew G Knepley 
62906636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
62916636e97aSMatthew G Knepley @*/
62926636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
62936636e97aSMatthew G Knepley {
62946636e97aSMatthew G Knepley   PetscErrorCode ierr;
62956636e97aSMatthew G Knepley 
62966636e97aSMatthew G Knepley   PetscFunctionBegin;
62976636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62986636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
62996636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
63006636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
63018865f1eaSKarl Rupp 
63026636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
63038865f1eaSKarl Rupp 
63046636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
63056636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63066636e97aSMatthew G Knepley }
63076636e97aSMatthew G Knepley 
63086636e97aSMatthew G Knepley /*@
63096636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
63106636e97aSMatthew G Knepley 
6311d083f849SBarry Smith   Collective on dm
63126636e97aSMatthew G Knepley 
63136636e97aSMatthew G Knepley   Input Parameter:
63146636e97aSMatthew G Knepley . dm - the DM
63156636e97aSMatthew G Knepley 
63166636e97aSMatthew G Knepley   Output Parameter:
63176636e97aSMatthew G Knepley . c - global coordinate vector
63186636e97aSMatthew G Knepley 
63196636e97aSMatthew G Knepley   Note:
632060c22052SBarry Smith   This is a borrowed reference, so the user should NOT destroy this vector. When the DM is
632160c22052SBarry Smith   destroyed the array will no longer be valid.
63226636e97aSMatthew G Knepley 
6323959c7569SPatrick Sanan   Each process has only the locally-owned portion of the global coordinates (does NOT have the ghost coordinates).
63246636e97aSMatthew G Knepley 
63256636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
63266636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
63276636e97aSMatthew G Knepley 
63286636e97aSMatthew G Knepley   Level: intermediate
63296636e97aSMatthew G Knepley 
633060c22052SBarry Smith .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
63316636e97aSMatthew G Knepley @*/
63326636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
63336636e97aSMatthew G Knepley {
63346636e97aSMatthew G Knepley   PetscErrorCode ierr;
63356636e97aSMatthew G Knepley 
63366636e97aSMatthew G Knepley   PetscFunctionBegin;
63376636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63386636e97aSMatthew G Knepley   PetscValidPointer(c,2);
63391f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
63400298fd71SBarry Smith     DM        cdm = NULL;
63411970a576SMatthew G. Knepley     PetscBool localized;
63426636e97aSMatthew G Knepley 
63436636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
63446636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
63451970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
63461970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
63471970a576SMatthew G. Knepley     if (localized) {
63481970a576SMatthew G. Knepley       PetscInt cdim;
63491970a576SMatthew G. Knepley 
63501970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
63511970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
63521970a576SMatthew G. Knepley     }
63536636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
63546636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
63556636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
63566636e97aSMatthew G Knepley   }
63576636e97aSMatthew G Knepley   *c = dm->coordinates;
63586636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63596636e97aSMatthew G Knepley }
63606636e97aSMatthew G Knepley 
63616636e97aSMatthew G Knepley /*@
636281e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
636381e9a530SVaclav Hapla 
6364d083f849SBarry Smith   Collective on dm
636581e9a530SVaclav Hapla 
636681e9a530SVaclav Hapla   Input Parameter:
636781e9a530SVaclav Hapla . dm - the DM
636881e9a530SVaclav Hapla 
636981e9a530SVaclav Hapla   Level: advanced
637081e9a530SVaclav Hapla 
637181e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
637281e9a530SVaclav Hapla @*/
637381e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
637481e9a530SVaclav Hapla {
637581e9a530SVaclav Hapla   PetscErrorCode ierr;
637681e9a530SVaclav Hapla 
637781e9a530SVaclav Hapla   PetscFunctionBegin;
637881e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
637981e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
63801970a576SMatthew G. Knepley     DM        cdm = NULL;
63811970a576SMatthew G. Knepley     PetscBool localized;
63821970a576SMatthew G. Knepley 
638381e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
638481e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
63851970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
63861970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
63871970a576SMatthew G. Knepley     if (localized) {
63881970a576SMatthew G. Knepley       PetscInt cdim;
63891970a576SMatthew G. Knepley 
63901970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
63911970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
63921970a576SMatthew G. Knepley     }
639381e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
639481e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
639581e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
639681e9a530SVaclav Hapla   }
639781e9a530SVaclav Hapla   PetscFunctionReturn(0);
639881e9a530SVaclav Hapla }
639981e9a530SVaclav Hapla 
640081e9a530SVaclav Hapla /*@
64016636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
64026636e97aSMatthew G Knepley 
6403d083f849SBarry Smith   Collective on dm
64046636e97aSMatthew G Knepley 
64056636e97aSMatthew G Knepley   Input Parameter:
64066636e97aSMatthew G Knepley . dm - the DM
64076636e97aSMatthew G Knepley 
64086636e97aSMatthew G Knepley   Output Parameter:
64096636e97aSMatthew G Knepley . c - coordinate vector
64106636e97aSMatthew G Knepley 
64116636e97aSMatthew G Knepley   Note:
64126636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
64136636e97aSMatthew G Knepley 
64146636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
64156636e97aSMatthew G Knepley 
64166636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
64176636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
64186636e97aSMatthew G Knepley 
64196636e97aSMatthew G Knepley   Level: intermediate
64206636e97aSMatthew G Knepley 
642181e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
64226636e97aSMatthew G Knepley @*/
64236636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
64246636e97aSMatthew G Knepley {
64256636e97aSMatthew G Knepley   PetscErrorCode ierr;
64266636e97aSMatthew G Knepley 
64276636e97aSMatthew G Knepley   PetscFunctionBegin;
64286636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64296636e97aSMatthew G Knepley   PetscValidPointer(c,2);
643081e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
64316636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
64326636e97aSMatthew G Knepley   PetscFunctionReturn(0);
64336636e97aSMatthew G Knepley }
64346636e97aSMatthew G Knepley 
643581e9a530SVaclav Hapla /*@
643681e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
643781e9a530SVaclav Hapla 
643881e9a530SVaclav Hapla   Not collective
643981e9a530SVaclav Hapla 
644081e9a530SVaclav Hapla   Input Parameter:
644181e9a530SVaclav Hapla . dm - the DM
644281e9a530SVaclav Hapla 
644381e9a530SVaclav Hapla   Output Parameter:
644481e9a530SVaclav Hapla . c - coordinate vector
644581e9a530SVaclav Hapla 
644681e9a530SVaclav Hapla   Level: advanced
644781e9a530SVaclav Hapla 
644881e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
644981e9a530SVaclav Hapla @*/
645081e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
645181e9a530SVaclav Hapla {
645281e9a530SVaclav Hapla   PetscFunctionBegin;
645381e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
645481e9a530SVaclav Hapla   PetscValidPointer(c,2);
6455*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->coordinatesLocal && dm->coordinates,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
64566636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
64576636e97aSMatthew G Knepley   PetscFunctionReturn(0);
64586636e97aSMatthew G Knepley }
64596636e97aSMatthew G Knepley 
64602db98f8dSVaclav Hapla /*@
64612db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
64622db98f8dSVaclav Hapla 
64632db98f8dSVaclav Hapla   Not collective
64642db98f8dSVaclav Hapla 
6465d8d19677SJose E. Roman   Input Parameters:
64662db98f8dSVaclav Hapla + dm - the DM
64672db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
64682db98f8dSVaclav Hapla 
6469d8d19677SJose E. Roman   Output Parameters:
64702db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
64712db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
64722db98f8dSVaclav Hapla 
64732db98f8dSVaclav Hapla   Note:
64742db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
64752db98f8dSVaclav Hapla 
64762db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
64772db98f8dSVaclav Hapla 
64782db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
64792db98f8dSVaclav Hapla 
64802db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
64812db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
64822db98f8dSVaclav Hapla 
64832db98f8dSVaclav Hapla   Level: advanced
64842db98f8dSVaclav Hapla 
64852db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
64862db98f8dSVaclav Hapla @*/
64872db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
64882db98f8dSVaclav Hapla {
64892db98f8dSVaclav Hapla   PetscSection        cs, newcs;
64902db98f8dSVaclav Hapla   Vec                 coords;
64912db98f8dSVaclav Hapla   const PetscScalar   *arr;
64922db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
64932db98f8dSVaclav Hapla   PetscInt            n;
64942db98f8dSVaclav Hapla   PetscErrorCode      ierr;
64952db98f8dSVaclav Hapla 
64962db98f8dSVaclav Hapla   PetscFunctionBegin;
64972db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64982db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
64992db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
65002db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
6501*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->coordinatesLocal,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
6502*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->coordinateDM || !dm->coordinateDM->localSection,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
65031bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
65042db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
65052db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
65062db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
65072db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
65082db98f8dSVaclav Hapla   if (pCoord) {
65092db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
65102db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
65112db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
65122db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6513ad9ac99dSVaclav Hapla   } else {
6514ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
65152db98f8dSVaclav Hapla   }
6516ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6517ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
65182db98f8dSVaclav Hapla   PetscFunctionReturn(0);
65192db98f8dSVaclav Hapla }
65202db98f8dSVaclav Hapla 
6521f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6522f19dbd58SToby Isaac {
6523f19dbd58SToby Isaac   PetscErrorCode ierr;
6524f19dbd58SToby Isaac 
6525f19dbd58SToby Isaac   PetscFunctionBegin;
6526f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6527f19dbd58SToby Isaac   PetscValidPointer(field,2);
6528f19dbd58SToby Isaac   if (!dm->coordinateField) {
6529f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6530f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6531f19dbd58SToby Isaac     }
6532f19dbd58SToby Isaac   }
6533f19dbd58SToby Isaac   *field = dm->coordinateField;
6534f19dbd58SToby Isaac   PetscFunctionReturn(0);
6535f19dbd58SToby Isaac }
6536f19dbd58SToby Isaac 
6537f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6538f19dbd58SToby Isaac {
6539f19dbd58SToby Isaac   PetscErrorCode ierr;
6540f19dbd58SToby Isaac 
6541f19dbd58SToby Isaac   PetscFunctionBegin;
6542f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6543f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6544c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6545f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6546f19dbd58SToby Isaac   dm->coordinateField = field;
6547f19dbd58SToby Isaac   PetscFunctionReturn(0);
6548f19dbd58SToby Isaac }
6549f19dbd58SToby Isaac 
65506636e97aSMatthew G Knepley /*@
65511cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
65526636e97aSMatthew G Knepley 
6553d083f849SBarry Smith   Collective on dm
65546636e97aSMatthew G Knepley 
65556636e97aSMatthew G Knepley   Input Parameter:
65566636e97aSMatthew G Knepley . dm - the DM
65576636e97aSMatthew G Knepley 
65586636e97aSMatthew G Knepley   Output Parameter:
65596636e97aSMatthew G Knepley . cdm - coordinate DM
65606636e97aSMatthew G Knepley 
65616636e97aSMatthew G Knepley   Level: intermediate
65626636e97aSMatthew G Knepley 
65631cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
65646636e97aSMatthew G Knepley @*/
65656636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
65666636e97aSMatthew G Knepley {
65676636e97aSMatthew G Knepley   PetscErrorCode ierr;
65686636e97aSMatthew G Knepley 
65696636e97aSMatthew G Knepley   PetscFunctionBegin;
65706636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65716636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
65726636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6573308f8a94SToby Isaac     DM cdm;
6574308f8a94SToby Isaac 
6575*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!dm->ops->createcoordinatedm,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6576308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6577c9ad657eSksagiyam     ierr = PetscObjectSetName((PetscObject)cdm, "coordinateDM");CHKERRQ(ierr);
6578308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6579308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6580308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6581308f8a94SToby Isaac     dm->coordinateDM = cdm;
65826636e97aSMatthew G Knepley   }
65836636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
65846636e97aSMatthew G Knepley   PetscFunctionReturn(0);
65856636e97aSMatthew G Knepley }
6586e87bb0d3SMatthew G Knepley 
65871cfe2091SMatthew G. Knepley /*@
65881cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
65891cfe2091SMatthew G. Knepley 
6590d083f849SBarry Smith   Logically Collective on dm
65911cfe2091SMatthew G. Knepley 
65921cfe2091SMatthew G. Knepley   Input Parameters:
65931cfe2091SMatthew G. Knepley + dm - the DM
65941cfe2091SMatthew G. Knepley - cdm - coordinate DM
65951cfe2091SMatthew G. Knepley 
65961cfe2091SMatthew G. Knepley   Level: intermediate
65971cfe2091SMatthew G. Knepley 
65981cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
65991cfe2091SMatthew G. Knepley @*/
66001cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
66011cfe2091SMatthew G. Knepley {
66021cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
66031cfe2091SMatthew G. Knepley 
66041cfe2091SMatthew G. Knepley   PetscFunctionBegin;
66051cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
66061cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6607f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
66081cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
66091cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
66101cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
66111cfe2091SMatthew G. Knepley }
66121cfe2091SMatthew G. Knepley 
661346e270d4SMatthew G. Knepley /*@
661446e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
661546e270d4SMatthew G. Knepley 
661646e270d4SMatthew G. Knepley   Not Collective
661746e270d4SMatthew G. Knepley 
661846e270d4SMatthew G. Knepley   Input Parameter:
661946e270d4SMatthew G. Knepley . dm - The DM object
662046e270d4SMatthew G. Knepley 
662146e270d4SMatthew G. Knepley   Output Parameter:
662246e270d4SMatthew G. Knepley . dim - The embedding dimension
662346e270d4SMatthew G. Knepley 
662446e270d4SMatthew G. Knepley   Level: intermediate
662546e270d4SMatthew G. Knepley 
662692fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
662746e270d4SMatthew G. Knepley @*/
662846e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
662946e270d4SMatthew G. Knepley {
663046e270d4SMatthew G. Knepley   PetscFunctionBegin;
663146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6632534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
66339a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
66349a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
66359a9a41abSToby Isaac   }
663646e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
663746e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
663846e270d4SMatthew G. Knepley }
663946e270d4SMatthew G. Knepley 
664046e270d4SMatthew G. Knepley /*@
664146e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
664246e270d4SMatthew G. Knepley 
664346e270d4SMatthew G. Knepley   Not Collective
664446e270d4SMatthew G. Knepley 
664546e270d4SMatthew G. Knepley   Input Parameters:
664646e270d4SMatthew G. Knepley + dm  - The DM object
664746e270d4SMatthew G. Knepley - dim - The embedding dimension
664846e270d4SMatthew G. Knepley 
664946e270d4SMatthew G. Knepley   Level: intermediate
665046e270d4SMatthew G. Knepley 
665192fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
665246e270d4SMatthew G. Knepley @*/
665346e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
665446e270d4SMatthew G. Knepley {
6655e5e52638SMatthew G. Knepley   PetscDS        ds;
665645480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6657f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6658f17e8794SMatthew G. Knepley 
665946e270d4SMatthew G. Knepley   PetscFunctionBegin;
666046e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
666146e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6662d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
666345480ffeSMatthew G. Knepley     ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
666445480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
666545480ffeSMatthew G. Knepley       ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
6666e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
666745480ffeSMatthew G. Knepley     }
6668d17bd122SMatthew G. Knepley   }
666946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
667046e270d4SMatthew G. Knepley }
667146e270d4SMatthew G. Knepley 
6672e8abe2deSMatthew G. Knepley /*@
6673e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6674e8abe2deSMatthew G. Knepley 
6675d083f849SBarry Smith   Collective on dm
6676e8abe2deSMatthew G. Knepley 
6677e8abe2deSMatthew G. Knepley   Input Parameter:
6678e8abe2deSMatthew G. Knepley . dm - The DM object
6679e8abe2deSMatthew G. Knepley 
6680e8abe2deSMatthew G. Knepley   Output Parameter:
6681e8abe2deSMatthew G. Knepley . section - The PetscSection object
6682e8abe2deSMatthew G. Knepley 
6683e8abe2deSMatthew G. Knepley   Level: intermediate
6684e8abe2deSMatthew G. Knepley 
668592fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6686e8abe2deSMatthew G. Knepley @*/
6687e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6688e8abe2deSMatthew G. Knepley {
6689e8abe2deSMatthew G. Knepley   DM             cdm;
6690e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6691e8abe2deSMatthew G. Knepley 
6692e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6693e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6694e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6695e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
669692fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6697e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6698e8abe2deSMatthew G. Knepley }
6699e8abe2deSMatthew G. Knepley 
6700e8abe2deSMatthew G. Knepley /*@
6701e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6702e8abe2deSMatthew G. Knepley 
6703e8abe2deSMatthew G. Knepley   Not Collective
6704e8abe2deSMatthew G. Knepley 
6705e8abe2deSMatthew G. Knepley   Input Parameters:
6706e8abe2deSMatthew G. Knepley + dm      - The DM object
670746e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6708e8abe2deSMatthew G. Knepley - section - The PetscSection object
6709e8abe2deSMatthew G. Knepley 
6710e8abe2deSMatthew G. Knepley   Level: intermediate
6711e8abe2deSMatthew G. Knepley 
671292fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6713e8abe2deSMatthew G. Knepley @*/
671446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6715e8abe2deSMatthew G. Knepley {
6716e8abe2deSMatthew G. Knepley   DM             cdm;
6717e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6718e8abe2deSMatthew G. Knepley 
6719e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6720e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
672146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6722e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
672392fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
672446e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
67254c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
672646e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
672746e270d4SMatthew G. Knepley 
672846e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
672946e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
673046e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
673146e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
673246e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
673346e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
673446e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
673546e270d4SMatthew G. Knepley     }
6736ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
673746e270d4SMatthew G. Knepley   }
6738e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6739e8abe2deSMatthew G. Knepley }
6740e8abe2deSMatthew G. Knepley 
6741d864a3eaSLisandro Dalcin /*@
6742d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6743d864a3eaSLisandro Dalcin 
6744d864a3eaSLisandro Dalcin   Input Parameters:
6745d864a3eaSLisandro Dalcin + dm      - The DM object
6746d864a3eaSLisandro Dalcin - disc    - The new coordinate discretization
6747d864a3eaSLisandro Dalcin 
6748d864a3eaSLisandro Dalcin   Level: intermediate
6749d864a3eaSLisandro Dalcin 
6750d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6751d864a3eaSLisandro Dalcin @*/
6752d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6753d864a3eaSLisandro Dalcin {
6754d864a3eaSLisandro Dalcin   PetscObject    discOld;
6755d864a3eaSLisandro Dalcin   PetscClassId   classid;
6756d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6757d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6758d864a3eaSLisandro Dalcin   Mat            matInterp;
6759d864a3eaSLisandro Dalcin   PetscErrorCode ierr;
6760d864a3eaSLisandro Dalcin 
6761d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6762d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6763d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6764d864a3eaSLisandro Dalcin 
6765d864a3eaSLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdmOld);CHKERRQ(ierr);
6766d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
6767d864a3eaSLisandro Dalcin   ierr = DMGetField(cdmOld, 0, NULL, &discOld);CHKERRQ(ierr);
6768d864a3eaSLisandro Dalcin   ierr = PetscObjectGetClassId(discOld, &classid);CHKERRQ(ierr);
676929ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
677029ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
677129ad44c5SMatthew G. Knepley       PetscFE        feLinear;
677229ad44c5SMatthew G. Knepley       DMPolytopeType ct;
677329ad44c5SMatthew G. Knepley       PetscInt       dim, dE, cStart;
677429ad44c5SMatthew G. Knepley       PetscBool      simplex;
677529ad44c5SMatthew G. Knepley 
677629ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
677729ad44c5SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
677829ad44c5SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
677929ad44c5SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdmOld, 0, &cStart, NULL);CHKERRQ(ierr);
678029ad44c5SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
678129ad44c5SMatthew G. Knepley       switch (ct) {
678229ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM:
678329ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
678429ad44c5SMatthew G. Knepley           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
678529ad44c5SMatthew G. Knepley         default: break;
678629ad44c5SMatthew G. Knepley       }
678729ad44c5SMatthew G. Knepley       simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
678829ad44c5SMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear);CHKERRQ(ierr);
678929ad44c5SMatthew G. Knepley       ierr = DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear);CHKERRQ(ierr);
679029ad44c5SMatthew G. Knepley       ierr = PetscFEDestroy(&feLinear);CHKERRQ(ierr);
679129ad44c5SMatthew G. Knepley       ierr = DMCreateDS(cdmOld);CHKERRQ(ierr);
679229ad44c5SMatthew G. Knepley     } else {
679329ad44c5SMatthew G. Knepley       const char *discname;
679429ad44c5SMatthew G. Knepley 
679529ad44c5SMatthew G. Knepley       ierr = PetscObjectGetType(discOld, &discname);CHKERRQ(ierr);
679698921bdaSJacob Faibussowitsch       SETERRQ(PetscObjectComm(discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
679729ad44c5SMatthew G. Knepley     }
679829ad44c5SMatthew G. Knepley   }
6799d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6800d864a3eaSLisandro Dalcin   ierr = DMClone(cdmOld, &cdmNew);CHKERRQ(ierr);
6801d864a3eaSLisandro Dalcin   ierr = DMSetField(cdmNew, 0, NULL, (PetscObject) disc);CHKERRQ(ierr);
6802d864a3eaSLisandro Dalcin   ierr = DMCreateDS(cdmNew);CHKERRQ(ierr);
6803d864a3eaSLisandro Dalcin   /* Project the coordinate vector from old to new space  */
6804d864a3eaSLisandro Dalcin   ierr = DMGetCoordinates(dm, &coordsOld);CHKERRQ(ierr);
6805d864a3eaSLisandro Dalcin   ierr = DMCreateGlobalVector(cdmNew, &coordsNew);CHKERRQ(ierr);
6806d864a3eaSLisandro Dalcin   ierr = DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL);CHKERRQ(ierr);
6807d864a3eaSLisandro Dalcin   ierr = MatInterpolate(matInterp, coordsOld, coordsNew);CHKERRQ(ierr);
6808d864a3eaSLisandro Dalcin   ierr = MatDestroy(&matInterp);CHKERRQ(ierr);
6809d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6810d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateField(dm, NULL);CHKERRQ(ierr);
6811d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateDM(dm, cdmNew);CHKERRQ(ierr);
6812d864a3eaSLisandro Dalcin   ierr = DMSetCoordinates(dm, coordsNew);CHKERRQ(ierr);
6813d864a3eaSLisandro Dalcin   ierr = VecDestroy(&coordsNew);CHKERRQ(ierr);
6814d864a3eaSLisandro Dalcin   ierr = DMDestroy(&cdmNew);CHKERRQ(ierr);
6815d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6816d864a3eaSLisandro Dalcin }
6817d864a3eaSLisandro Dalcin 
68185dc8c3f7SMatthew G. Knepley /*@C
681990b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
68205dc8c3f7SMatthew G. Knepley 
6821f899ff85SJose E. Roman   Input Parameter:
682290b157c4SStefano Zampini . dm      - The DM object
682390b157c4SStefano Zampini 
682490b157c4SStefano Zampini   Output Parameters:
682590b157c4SStefano Zampini + per     - Whether the DM is periodic or not
68265dc8c3f7SMatthew 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
68275dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
68285dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
68295dc8c3f7SMatthew G. Knepley 
68305dc8c3f7SMatthew G. Knepley   Level: developer
68315dc8c3f7SMatthew G. Knepley 
68325dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
68335dc8c3f7SMatthew G. Knepley @*/
683490b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6835c6b900c6SMatthew G. Knepley {
6836c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6837c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
683890b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6839c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6840c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
68415dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6842c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6843c6b900c6SMatthew G. Knepley }
6844c6b900c6SMatthew G. Knepley 
68455dc8c3f7SMatthew G. Knepley /*@C
68465dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
68475dc8c3f7SMatthew G. Knepley 
68485dc8c3f7SMatthew G. Knepley   Input Parameters:
68495dc8c3f7SMatthew G. Knepley + dm      - The DM object
6850db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6851db2bf62eSStefano 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.
68525dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
68535dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
68545dc8c3f7SMatthew G. Knepley 
6855db2bf62eSStefano 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.
6856db2bf62eSStefano Zampini 
68575dc8c3f7SMatthew G. Knepley   Level: developer
68585dc8c3f7SMatthew G. Knepley 
68595dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
68605dc8c3f7SMatthew G. Knepley @*/
686190b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6862c6b900c6SMatthew G. Knepley {
6863c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6864c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6865c6b900c6SMatthew G. Knepley 
6866c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6867c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
686890b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6869412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6870412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6871412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
68725dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
687390b157c4SStefano Zampini   if (maxCell) {
6874412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6875412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6876db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6877db2bf62eSStefano Zampini     ierr = PetscFree(dm->maxCell);CHKERRQ(ierr);
6878412e9a14SMatthew G. Knepley   }
6879db2bf62eSStefano Zampini 
6880412e9a14SMatthew G. Knepley   if (L) {
6881412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6882412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6883412e9a14SMatthew G. Knepley   }
6884412e9a14SMatthew G. Knepley   if (bd) {
6885412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6886412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
688790b157c4SStefano Zampini   }
6888072d7d67SStefano Zampini   dm->periodic = per;
6889c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6890c6b900c6SMatthew G. Knepley }
6891c6b900c6SMatthew G. Knepley 
68922e17dfb7SMatthew G. Knepley /*@
68932e17dfb7SMatthew 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.
68942e17dfb7SMatthew G. Knepley 
68952e17dfb7SMatthew G. Knepley   Input Parameters:
68962e17dfb7SMatthew G. Knepley + dm     - The DM
689765da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
689865da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
68992e17dfb7SMatthew G. Knepley 
69002e17dfb7SMatthew G. Knepley   Output Parameter:
69012e17dfb7SMatthew G. Knepley . out - The localized coordinate point
69022e17dfb7SMatthew G. Knepley 
69032e17dfb7SMatthew G. Knepley   Level: developer
69042e17dfb7SMatthew G. Knepley 
69052e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
69062e17dfb7SMatthew G. Knepley @*/
690765da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
69082e17dfb7SMatthew G. Knepley {
69092e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
69102e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
69112e17dfb7SMatthew G. Knepley 
69122e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69132e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
69142e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
69152e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
69162e17dfb7SMatthew G. Knepley   } else {
691765da65dcSMatthew G. Knepley     if (endpoint) {
691865da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6919da3333bfSMatthew 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)) {
6920da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
692165da65dcSMatthew G. Knepley         } else {
6922da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
692365da65dcSMatthew G. Knepley         }
692465da65dcSMatthew G. Knepley       }
692565da65dcSMatthew G. Knepley     } else {
69262e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
69271118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
69282e17dfb7SMatthew G. Knepley       }
69292e17dfb7SMatthew G. Knepley     }
693065da65dcSMatthew G. Knepley   }
69312e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69322e17dfb7SMatthew G. Knepley }
69332e17dfb7SMatthew G. Knepley 
69342e17dfb7SMatthew G. Knepley /*
69352e17dfb7SMatthew 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.
69362e17dfb7SMatthew G. Knepley 
69372e17dfb7SMatthew G. Knepley   Input Parameters:
69382e17dfb7SMatthew G. Knepley + dm     - The DM
69392e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
69402e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
69412e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
69422e17dfb7SMatthew G. Knepley 
69432e17dfb7SMatthew G. Knepley   Output Parameter:
69442e17dfb7SMatthew G. Knepley . out - The localized coordinate point
69452e17dfb7SMatthew G. Knepley 
69462e17dfb7SMatthew G. Knepley   Level: developer
69472e17dfb7SMatthew G. Knepley 
69482e17dfb7SMatthew 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
69492e17dfb7SMatthew G. Knepley 
69502e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
69512e17dfb7SMatthew G. Knepley */
69522e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
69532e17dfb7SMatthew G. Knepley {
69542e17dfb7SMatthew G. Knepley   PetscInt d;
69552e17dfb7SMatthew G. Knepley 
69562e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69572e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
69582e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
69592e17dfb7SMatthew G. Knepley   } else {
69602e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6961908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
69622e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
69632e17dfb7SMatthew G. Knepley       } else {
69642e17dfb7SMatthew G. Knepley         out[d] = in[d];
69652e17dfb7SMatthew G. Knepley       }
69662e17dfb7SMatthew G. Knepley     }
69672e17dfb7SMatthew G. Knepley   }
69682e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69692e17dfb7SMatthew G. Knepley }
6970a5801f52SStefano Zampini 
69712e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
69722e17dfb7SMatthew G. Knepley {
69732e17dfb7SMatthew G. Knepley   PetscInt d;
69742e17dfb7SMatthew G. Knepley 
69752e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69762e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
69772e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
69782e17dfb7SMatthew G. Knepley   } else {
69792e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6980908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
69812e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
69822e17dfb7SMatthew G. Knepley       } else {
69832e17dfb7SMatthew G. Knepley         out[d] = in[d];
69842e17dfb7SMatthew G. Knepley       }
69852e17dfb7SMatthew G. Knepley     }
69862e17dfb7SMatthew G. Knepley   }
69872e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69882e17dfb7SMatthew G. Knepley }
69892e17dfb7SMatthew G. Knepley 
69902e17dfb7SMatthew G. Knepley /*
69912e17dfb7SMatthew 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.
69922e17dfb7SMatthew G. Knepley 
69932e17dfb7SMatthew G. Knepley   Input Parameters:
69942e17dfb7SMatthew G. Knepley + dm     - The DM
69952e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
69962e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
69972e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
69982e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
69992e17dfb7SMatthew G. Knepley 
70002e17dfb7SMatthew G. Knepley   Output Parameter:
70012e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
70022e17dfb7SMatthew G. Knepley 
70032e17dfb7SMatthew G. Knepley   Level: developer
70042e17dfb7SMatthew G. Knepley 
70052e17dfb7SMatthew 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
70062e17dfb7SMatthew G. Knepley 
70072e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
70082e17dfb7SMatthew G. Knepley */
70092e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
70102e17dfb7SMatthew G. Knepley {
70112e17dfb7SMatthew G. Knepley   PetscInt d;
70122e17dfb7SMatthew G. Knepley 
70132e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
70142e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
70152e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
70162e17dfb7SMatthew G. Knepley   } else {
70172e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
7018412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
7019412e9a14SMatthew G. Knepley 
7020412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
7021412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
7022412e9a14SMatthew G. Knepley 
7023412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
702498921bdaSJacob Faibussowitsch           SETERRQ(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]));
7025412e9a14SMatthew G. Knepley         out[d] += newCoord;
70262e17dfb7SMatthew G. Knepley       } else {
70272e17dfb7SMatthew G. Knepley         out[d] += in[d];
70282e17dfb7SMatthew G. Knepley       }
70292e17dfb7SMatthew G. Knepley     }
70302e17dfb7SMatthew G. Knepley   }
70312e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
70322e17dfb7SMatthew G. Knepley }
70332e17dfb7SMatthew G. Knepley 
703436447a5eSToby Isaac /*@
70358f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
70368f700142SStefano Zampini 
70378f700142SStefano Zampini   Not collective
703836447a5eSToby Isaac 
703936447a5eSToby Isaac   Input Parameter:
704036447a5eSToby Isaac . dm - The DM
704136447a5eSToby Isaac 
704236447a5eSToby Isaac   Output Parameter:
704336447a5eSToby Isaac   areLocalized - True if localized
704436447a5eSToby Isaac 
704536447a5eSToby Isaac   Level: developer
704636447a5eSToby Isaac 
70478f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
704836447a5eSToby Isaac @*/
70498f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
705036447a5eSToby Isaac {
705136447a5eSToby Isaac   DM             cdm;
705236447a5eSToby Isaac   PetscSection   coordSection;
70532ccac677SMatthew G. Knepley   PetscInt       depth, cStart, cEnd, sStart, sEnd, c, dof;
705446a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
705536447a5eSToby Isaac   PetscErrorCode ierr;
705636447a5eSToby Isaac 
705736447a5eSToby Isaac   PetscFunctionBegin;
705836447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7059534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
70608b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
706146a3a80fSLisandro Dalcin 
706236447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
706336447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
706446a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
70659f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
70662ccac677SMatthew G. Knepley   ierr = DMPlexGetDepth(cdm, &depth);CHKERRQ(ierr);
70672ccac677SMatthew G. Knepley   if (!depth) PetscFunctionReturn(0);
706846a3a80fSLisandro Dalcin 
70699f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
707046a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
707136447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
707236447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
707346a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
707446a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
707536447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
707646a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
707736447a5eSToby Isaac   }
70788f700142SStefano Zampini   *areLocalized = alreadyLocalized;
707936447a5eSToby Isaac   PetscFunctionReturn(0);
708036447a5eSToby Isaac }
708136447a5eSToby Isaac 
70828f700142SStefano Zampini /*@
70838f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
70848f700142SStefano Zampini 
70858f700142SStefano Zampini   Collective on dm
70868f700142SStefano Zampini 
70878f700142SStefano Zampini   Input Parameter:
70888f700142SStefano Zampini . dm - The DM
70898f700142SStefano Zampini 
70908f700142SStefano Zampini   Output Parameter:
70918f700142SStefano Zampini   areLocalized - True if localized
70928f700142SStefano Zampini 
70938f700142SStefano Zampini   Level: developer
70948f700142SStefano Zampini 
70958f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
70968f700142SStefano Zampini @*/
70978f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
70988f700142SStefano Zampini {
70998f700142SStefano Zampini   PetscBool      localized;
71008f700142SStefano Zampini   PetscErrorCode ierr;
71018f700142SStefano Zampini 
71028f700142SStefano Zampini   PetscFunctionBegin;
71038f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7104534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
71058f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
7106820f2d46SBarry Smith   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
71078f700142SStefano Zampini   PetscFunctionReturn(0);
71088f700142SStefano Zampini }
710936447a5eSToby Isaac 
71102e17dfb7SMatthew G. Knepley /*@
7111492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
71122e17dfb7SMatthew G. Knepley 
71138f700142SStefano Zampini   Collective on dm
71148f700142SStefano Zampini 
71152e17dfb7SMatthew G. Knepley   Input Parameter:
71162e17dfb7SMatthew G. Knepley . dm - The DM
71172e17dfb7SMatthew G. Knepley 
71182e17dfb7SMatthew G. Knepley   Level: developer
71192e17dfb7SMatthew G. Knepley 
71208f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
71212e17dfb7SMatthew G. Knepley @*/
71222e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
71232e17dfb7SMatthew G. Knepley {
71242e17dfb7SMatthew G. Knepley   DM             cdm;
71252e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
71262e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
71273e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
71283e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
7129e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
71303e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
71313e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
71322e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
71332e17dfb7SMatthew G. Knepley 
71342e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
71352e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
713692c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
7137f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
7138f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
7139f7cbd40bSStefano Zampini 
71402e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
71412e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
71422e17dfb7SMatthew G. Knepley   {
71432e17dfb7SMatthew G. Knepley     PetscBool isplex;
71442e17dfb7SMatthew G. Knepley 
71452e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
71462e17dfb7SMatthew G. Knepley     if (isplex) {
71472e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
71483e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
714969291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
71503e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
71513e922f36SToby Isaac       newStart = vStart;
71523e922f36SToby Isaac       newEnd   = vEnd;
71533e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
71543e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
71553e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
71563e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
71573e922f36SToby Isaac       }
71582e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
71592e17dfb7SMatthew G. Knepley   }
71602e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
7161*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!coordinates,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
71622e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
71633e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
7164e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
71653e922f36SToby Isaac 
71662e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
71672e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
71682e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
71692e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
71703e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
71713e922f36SToby Isaac 
717269291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
71733e922f36SToby Isaac   localized = &anchor[bs];
71743e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
71753e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
71763e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
71773e922f36SToby Isaac 
71783e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
71793e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
71803e922f36SToby Isaac       PetscInt     b;
71813e922f36SToby Isaac 
71823e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
71833e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
71843e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
71853e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
71863e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
71873e922f36SToby Isaac         for (b = 0; b < bs; b++) {
71883e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
71893e922f36SToby Isaac         }
71903e922f36SToby Isaac         if (b < bs) break;
71913e922f36SToby Isaac       }
71923e922f36SToby Isaac       if (d < dof/bs) {
71933e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
71943e922f36SToby Isaac           PetscInt cdof;
71953e922f36SToby Isaac 
71963e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
71973e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
71983e922f36SToby Isaac         }
71993e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
72003e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
72013e922f36SToby Isaac       }
72023e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
72033e922f36SToby Isaac     }
72043e922f36SToby Isaac   }
7205ffc4695bSBarry Smith   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
72063e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
720769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
72083e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
720969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
72103e922f36SToby Isaac     PetscFunctionReturn(0);
72113e922f36SToby Isaac   }
72122e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
72132e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
72142e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
72152e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
72162e17dfb7SMatthew G. Knepley   }
72172e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
72182e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
7219c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
72202e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
72212e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
72222e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
72232e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
7224c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
72252e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
72262e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
72272e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
72282e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
72292e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
72302e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
72312e17dfb7SMatthew G. Knepley   }
72323e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
72333e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
72343e922f36SToby Isaac 
72352e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
72362e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
72373e922f36SToby Isaac       PetscInt     b, cdof;
72382e17dfb7SMatthew G. Knepley 
72393e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
72403e922f36SToby Isaac       if (!cdof) continue;
72412e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
72422e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
72432e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
72442e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
72452e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
72462e17dfb7SMatthew G. Knepley     }
72473e922f36SToby Isaac   }
724869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
724969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
7250c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
72512e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
72522e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
72532e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
72542e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
72552e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
72562e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
72572e17dfb7SMatthew G. Knepley }
72582e17dfb7SMatthew G. Knepley 
7259e87bb0d3SMatthew G Knepley /*@
72603a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
7261e87bb0d3SMatthew G Knepley 
7262d083f849SBarry Smith   Collective on v (see explanation below)
7263e87bb0d3SMatthew G Knepley 
7264e87bb0d3SMatthew G Knepley   Input Parameters:
7265e87bb0d3SMatthew G Knepley + dm - The DM
72666b867d5aSJose E. Roman - ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
7267e87bb0d3SMatthew G Knepley 
72686b867d5aSJose E. Roman   Input/Output Parameters:
72696b867d5aSJose E. Roman + v - The Vec of points, on output contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
72706b867d5aSJose E. Roman - cellSF - Points to either NULL, or a PetscSF with guesses for which cells contain each point;
72716b867d5aSJose E. Roman            on output, the PetscSF containing the ranks and local indices of the containing points
72723a93e3b7SToby Isaac 
7273e87bb0d3SMatthew G Knepley   Level: developer
727461e3bb9bSMatthew G Knepley 
727562a38674SMatthew G. Knepley   Notes:
72763a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
727762a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
72783a93e3b7SToby Isaac 
72793a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
728062a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
72813a93e3b7SToby Isaac 
72823a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
72833a93e3b7SToby Isaac 
728462a38674SMatthew G. Knepley $    const PetscSFNode *cells;
728562a38674SMatthew G. Knepley $    PetscInt           nFound;
7286a6216909SToby Isaac $    const PetscInt    *found;
728762a38674SMatthew G. Knepley $
7288a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
72893a93e3b7SToby Isaac 
72903a93e3b7SToby 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
72913a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
72923a93e3b7SToby Isaac 
729362a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
729461e3bb9bSMatthew G Knepley @*/
729562a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
7296e87bb0d3SMatthew G Knepley {
7297735aa83eSMatthew G Knepley   PetscErrorCode ierr;
7298735aa83eSMatthew G Knepley 
7299e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
7300e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7301e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
7302e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
73033a93e3b7SToby Isaac   if (*cellSF) {
73043a93e3b7SToby Isaac     PetscMPIInt result;
73053a93e3b7SToby Isaac 
7306e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
7307ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRMPI(ierr);
7308*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(result != MPI_IDENT && result != MPI_CONGRUENT,PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"cellSF must have a communicator congruent to v's");
7309e0fc9d1bSMatthew G. Knepley   } else {
73103a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
73113a93e3b7SToby Isaac   }
7312*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->locatepoints,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
731347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
731462a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
731547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
7316e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
7317e87bb0d3SMatthew G Knepley }
731814f150ffSMatthew G. Knepley 
7319f4d763aaSMatthew G. Knepley /*@
7320f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
7321f4d763aaSMatthew G. Knepley 
73228f700142SStefano Zampini   Collective on dm
73238f700142SStefano Zampini 
7324f4d763aaSMatthew G. Knepley   Input Parameter:
7325f4d763aaSMatthew G. Knepley . dm - The original DM
7326f4d763aaSMatthew G. Knepley 
7327f4d763aaSMatthew G. Knepley   Output Parameter:
7328f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
7329f4d763aaSMatthew G. Knepley 
7330f4d763aaSMatthew G. Knepley   Level: intermediate
7331f4d763aaSMatthew G. Knepley 
7332e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
7333f4d763aaSMatthew G. Knepley @*/
733414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
733514f150ffSMatthew G. Knepley {
7336c26acbdeSMatthew G. Knepley   PetscSection   section;
73372d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
733814f150ffSMatthew G. Knepley   PetscErrorCode ierr;
733914f150ffSMatthew G. Knepley 
734014f150ffSMatthew G. Knepley   PetscFunctionBegin;
734114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
734214f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
734392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
7344c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
7345ffc4695bSBarry Smith   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
73462d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
7347c26acbdeSMatthew G. Knepley     *odm = dm;
7348c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
7349c26acbdeSMatthew G. Knepley   }
735014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
7351c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
735214f150ffSMatthew G. Knepley     PetscSF      sf;
735314f150ffSMatthew G. Knepley 
735414f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
7355e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
735614f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
735792fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
735814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
735914f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
736015b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
7361e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
736214f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
736314f150ffSMatthew G. Knepley   }
736414f150ffSMatthew G. Knepley   *odm = dm->dmBC;
736514f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
736614f150ffSMatthew G. Knepley }
7367f4d763aaSMatthew G. Knepley 
7368f4d763aaSMatthew G. Knepley /*@
7369cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7370f4d763aaSMatthew G. Knepley 
7371f4d763aaSMatthew G. Knepley   Input Parameter:
7372f4d763aaSMatthew G. Knepley . dm - The original DM
7373f4d763aaSMatthew G. Knepley 
7374cdb7a50dSMatthew G. Knepley   Output Parameters:
7375cdb7a50dSMatthew G. Knepley + num - The output sequence number
7376cdb7a50dSMatthew G. Knepley - val - The output sequence value
7377f4d763aaSMatthew G. Knepley 
7378f4d763aaSMatthew G. Knepley   Level: intermediate
7379f4d763aaSMatthew G. Knepley 
7380f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7381f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7382f4d763aaSMatthew G. Knepley 
7383f4d763aaSMatthew G. Knepley .seealso: VecView()
7384f4d763aaSMatthew G. Knepley @*/
7385cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7386f4d763aaSMatthew G. Knepley {
7387f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7388f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7389534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7390534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7391f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7392f4d763aaSMatthew G. Knepley }
7393f4d763aaSMatthew G. Knepley 
7394f4d763aaSMatthew G. Knepley /*@
7395cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7396f4d763aaSMatthew G. Knepley 
7397f4d763aaSMatthew G. Knepley   Input Parameters:
7398f4d763aaSMatthew G. Knepley + dm - The original DM
7399cdb7a50dSMatthew G. Knepley . num - The output sequence number
7400cdb7a50dSMatthew G. Knepley - val - The output sequence value
7401f4d763aaSMatthew G. Knepley 
7402f4d763aaSMatthew G. Knepley   Level: intermediate
7403f4d763aaSMatthew G. Knepley 
7404f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7405f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7406f4d763aaSMatthew G. Knepley 
7407f4d763aaSMatthew G. Knepley .seealso: VecView()
7408f4d763aaSMatthew G. Knepley @*/
7409cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7410f4d763aaSMatthew G. Knepley {
7411f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7412f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7413f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7414cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7415cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7416cdb7a50dSMatthew G. Knepley }
7417cdb7a50dSMatthew G. Knepley 
7418cdb7a50dSMatthew G. Knepley /*@C
7419cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7420cdb7a50dSMatthew G. Knepley 
7421cdb7a50dSMatthew G. Knepley   Input Parameters:
7422cdb7a50dSMatthew G. Knepley + dm   - The original DM
7423cdb7a50dSMatthew G. Knepley . name - The sequence name
7424cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7425cdb7a50dSMatthew G. Knepley 
7426cdb7a50dSMatthew G. Knepley   Output Parameter:
7427cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7428cdb7a50dSMatthew G. Knepley 
7429cdb7a50dSMatthew G. Knepley   Level: intermediate
7430cdb7a50dSMatthew G. Knepley 
7431cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7432cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7433cdb7a50dSMatthew G. Knepley 
7434cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7435cdb7a50dSMatthew G. Knepley @*/
7436cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7437cdb7a50dSMatthew G. Knepley {
7438cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7439cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
7440cdb7a50dSMatthew G. Knepley 
7441cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7442cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7443cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7444064a246eSJacob Faibussowitsch   PetscValidRealPointer(val,5);
7445cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
7446cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7447cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7448cdb7a50dSMatthew G. Knepley     PetscScalar value;
7449cdb7a50dSMatthew G. Knepley 
745039d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
74514aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7452cdb7a50dSMatthew G. Knepley #endif
7453cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7454f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7455f4d763aaSMatthew G. Knepley }
74568e4ac7eaSMatthew G. Knepley 
74578e4ac7eaSMatthew G. Knepley /*@
74588e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
74598e4ac7eaSMatthew G. Knepley 
74608e4ac7eaSMatthew G. Knepley   Not collective
74618e4ac7eaSMatthew G. Knepley 
74628e4ac7eaSMatthew G. Knepley   Input Parameter:
74638e4ac7eaSMatthew G. Knepley . dm - The DM
74648e4ac7eaSMatthew G. Knepley 
74658e4ac7eaSMatthew G. Knepley   Output Parameter:
74668e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
74678e4ac7eaSMatthew G. Knepley 
74688e4ac7eaSMatthew G. Knepley   Level: beginner
74698e4ac7eaSMatthew G. Knepley 
74708e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
74718e4ac7eaSMatthew G. Knepley @*/
74728e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
74738e4ac7eaSMatthew G. Knepley {
74748e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
74758e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7476534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
74778e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
74788e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
74798e4ac7eaSMatthew G. Knepley }
74808e4ac7eaSMatthew G. Knepley 
74818e4ac7eaSMatthew G. Knepley /*@
74825d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
74838e4ac7eaSMatthew G. Knepley 
74848e4ac7eaSMatthew G. Knepley   Collective on dm
74858e4ac7eaSMatthew G. Knepley 
74868e4ac7eaSMatthew G. Knepley   Input Parameters:
74878e4ac7eaSMatthew G. Knepley + dm - The DM
74888e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
74898e4ac7eaSMatthew G. Knepley 
74905d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
74915d3b26e6SMatthew G. Knepley 
74928e4ac7eaSMatthew G. Knepley   Level: beginner
74938e4ac7eaSMatthew G. Knepley 
74945d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
74958e4ac7eaSMatthew G. Knepley @*/
74968e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
74978e4ac7eaSMatthew G. Knepley {
74988e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
74998e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75008833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
75018e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
75028e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
75038e4ac7eaSMatthew G. Knepley }
7504c58f1c22SToby Isaac 
7505c58f1c22SToby Isaac /*@C
7506c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
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 
7514c58f1c22SToby Isaac   Level: intermediate
7515c58f1c22SToby Isaac 
7516c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7517c58f1c22SToby Isaac @*/
7518c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7519c58f1c22SToby Isaac {
75205d80c0bfSVaclav Hapla   PetscBool      flg;
75215d80c0bfSVaclav Hapla   DMLabel        label;
7522c58f1c22SToby Isaac   PetscErrorCode ierr;
7523c58f1c22SToby Isaac 
7524c58f1c22SToby Isaac   PetscFunctionBegin;
7525c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7526c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
75275d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7528c58f1c22SToby Isaac   if (!flg) {
75295d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
75305d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
75315d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7532c58f1c22SToby Isaac   }
7533c58f1c22SToby Isaac   PetscFunctionReturn(0);
7534c58f1c22SToby Isaac }
7535c58f1c22SToby Isaac 
7536c58f1c22SToby Isaac /*@C
75370fdc7489SMatthew Knepley   DMCreateLabelAtIndex - Create a label of the given name at the iven index. If it already exists, move it to this index.
75380fdc7489SMatthew Knepley 
75390fdc7489SMatthew Knepley   Not Collective
75400fdc7489SMatthew Knepley 
75410fdc7489SMatthew Knepley   Input Parameters:
75420fdc7489SMatthew Knepley + dm   - The DM object
75430fdc7489SMatthew Knepley . l    - The index for the label
75440fdc7489SMatthew Knepley - name - The label name
75450fdc7489SMatthew Knepley 
75460fdc7489SMatthew Knepley   Level: intermediate
75470fdc7489SMatthew Knepley 
75480fdc7489SMatthew Knepley .seealso: DMCreateLabel(), DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
75490fdc7489SMatthew Knepley @*/
75500fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
75510fdc7489SMatthew Knepley {
75520fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
75530fdc7489SMatthew Knepley   DMLabel        label;
75540fdc7489SMatthew Knepley   PetscInt       Nl, m;
75550fdc7489SMatthew Knepley   PetscBool      flg, match;
75560fdc7489SMatthew Knepley   const char    *lname;
75570fdc7489SMatthew Knepley   PetscErrorCode ierr;
75580fdc7489SMatthew Knepley 
75590fdc7489SMatthew Knepley   PetscFunctionBegin;
75600fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7561064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
75620fdc7489SMatthew Knepley   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
75630fdc7489SMatthew Knepley   if (!flg) {
75640fdc7489SMatthew Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
75650fdc7489SMatthew Knepley     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
75660fdc7489SMatthew Knepley     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
75670fdc7489SMatthew Knepley   }
75680fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
7569*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(l >= Nl,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %D must be in [0, %D)", l, Nl);
75700fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
75710fdc7489SMatthew Knepley     ierr = PetscObjectGetName((PetscObject) orig->label, &lname);CHKERRQ(ierr);
75720fdc7489SMatthew Knepley     ierr = PetscStrcmp(name, lname, &match);CHKERRQ(ierr);
75730fdc7489SMatthew Knepley     if (match) break;
75740fdc7489SMatthew Knepley   }
75750fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
75760fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
75770fdc7489SMatthew Knepley   else    prev->next = orig->next;
75780fdc7489SMatthew Knepley   if (!l) {
75790fdc7489SMatthew Knepley     orig->next = dm->labels;
75800fdc7489SMatthew Knepley     dm->labels = orig;
75810fdc7489SMatthew Knepley   } else {
75820fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
75830fdc7489SMatthew Knepley     orig->next = prev->next;
75840fdc7489SMatthew Knepley     prev->next = orig;
75850fdc7489SMatthew Knepley   }
75860fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75870fdc7489SMatthew Knepley }
75880fdc7489SMatthew Knepley 
75890fdc7489SMatthew Knepley /*@C
7590c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7591c58f1c22SToby Isaac 
7592c58f1c22SToby Isaac   Not Collective
7593c58f1c22SToby Isaac 
7594c58f1c22SToby Isaac   Input Parameters:
7595c58f1c22SToby Isaac + dm   - The DM object
7596c58f1c22SToby Isaac . name - The label name
7597c58f1c22SToby Isaac - point - The mesh point
7598c58f1c22SToby Isaac 
7599c58f1c22SToby Isaac   Output Parameter:
7600c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7601c58f1c22SToby Isaac 
7602c58f1c22SToby Isaac   Level: beginner
7603c58f1c22SToby Isaac 
7604c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7605c58f1c22SToby Isaac @*/
7606c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7607c58f1c22SToby Isaac {
7608c58f1c22SToby Isaac   DMLabel        label;
7609c58f1c22SToby Isaac   PetscErrorCode ierr;
7610c58f1c22SToby Isaac 
7611c58f1c22SToby Isaac   PetscFunctionBegin;
7612c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7613c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7614c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7615*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7616c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7617c58f1c22SToby Isaac   PetscFunctionReturn(0);
7618c58f1c22SToby Isaac }
7619c58f1c22SToby Isaac 
7620c58f1c22SToby Isaac /*@C
7621c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7622c58f1c22SToby Isaac 
7623c58f1c22SToby Isaac   Not Collective
7624c58f1c22SToby Isaac 
7625c58f1c22SToby Isaac   Input Parameters:
7626c58f1c22SToby Isaac + dm   - The DM object
7627c58f1c22SToby Isaac . name - The label name
7628c58f1c22SToby Isaac . point - The mesh point
7629c58f1c22SToby Isaac - value - The label value for this point
7630c58f1c22SToby Isaac 
7631c58f1c22SToby Isaac   Output Parameter:
7632c58f1c22SToby Isaac 
7633c58f1c22SToby Isaac   Level: beginner
7634c58f1c22SToby Isaac 
7635c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7636c58f1c22SToby Isaac @*/
7637c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7638c58f1c22SToby Isaac {
7639c58f1c22SToby Isaac   DMLabel        label;
7640c58f1c22SToby Isaac   PetscErrorCode ierr;
7641c58f1c22SToby Isaac 
7642c58f1c22SToby Isaac   PetscFunctionBegin;
7643c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7644c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7645c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7646c58f1c22SToby Isaac   if (!label) {
7647c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7648c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7649c58f1c22SToby Isaac   }
7650c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7651c58f1c22SToby Isaac   PetscFunctionReturn(0);
7652c58f1c22SToby Isaac }
7653c58f1c22SToby Isaac 
7654c58f1c22SToby Isaac /*@C
7655c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7656c58f1c22SToby Isaac 
7657c58f1c22SToby Isaac   Not Collective
7658c58f1c22SToby Isaac 
7659c58f1c22SToby Isaac   Input Parameters:
7660c58f1c22SToby Isaac + dm   - The DM object
7661c58f1c22SToby Isaac . name - The label name
7662c58f1c22SToby Isaac . point - The mesh point
7663c58f1c22SToby Isaac - value - The label value for this point
7664c58f1c22SToby Isaac 
7665c58f1c22SToby Isaac   Output Parameter:
7666c58f1c22SToby Isaac 
7667c58f1c22SToby Isaac   Level: beginner
7668c58f1c22SToby Isaac 
7669c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7670c58f1c22SToby Isaac @*/
7671c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7672c58f1c22SToby Isaac {
7673c58f1c22SToby Isaac   DMLabel        label;
7674c58f1c22SToby Isaac   PetscErrorCode ierr;
7675c58f1c22SToby Isaac 
7676c58f1c22SToby Isaac   PetscFunctionBegin;
7677c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7678c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7679c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7680c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7681c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7682c58f1c22SToby Isaac   PetscFunctionReturn(0);
7683c58f1c22SToby Isaac }
7684c58f1c22SToby Isaac 
7685c58f1c22SToby Isaac /*@C
7686c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7687c58f1c22SToby Isaac 
7688c58f1c22SToby Isaac   Not Collective
7689c58f1c22SToby Isaac 
7690c58f1c22SToby Isaac   Input Parameters:
7691c58f1c22SToby Isaac + dm   - The DM object
7692c58f1c22SToby Isaac - name - The label name
7693c58f1c22SToby Isaac 
7694c58f1c22SToby Isaac   Output Parameter:
7695c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7696c58f1c22SToby Isaac 
7697c58f1c22SToby Isaac   Level: beginner
7698c58f1c22SToby Isaac 
7699df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7700c58f1c22SToby Isaac @*/
7701c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7702c58f1c22SToby Isaac {
7703c58f1c22SToby Isaac   DMLabel        label;
7704c58f1c22SToby Isaac   PetscErrorCode ierr;
7705c58f1c22SToby Isaac 
7706c58f1c22SToby Isaac   PetscFunctionBegin;
7707c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7708c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7709534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7710c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7711c58f1c22SToby Isaac   *size = 0;
7712c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7713c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7714c58f1c22SToby Isaac   PetscFunctionReturn(0);
7715c58f1c22SToby Isaac }
7716c58f1c22SToby Isaac 
7717c58f1c22SToby Isaac /*@C
7718c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7719c58f1c22SToby Isaac 
7720c58f1c22SToby Isaac   Not Collective
7721c58f1c22SToby Isaac 
7722c58f1c22SToby Isaac   Input Parameters:
7723c58f1c22SToby Isaac + mesh - The DM object
7724c58f1c22SToby Isaac - name - The label name
7725c58f1c22SToby Isaac 
7726c58f1c22SToby Isaac   Output Parameter:
7727c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7728c58f1c22SToby Isaac 
7729c58f1c22SToby Isaac   Level: beginner
7730c58f1c22SToby Isaac 
7731c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7732c58f1c22SToby Isaac @*/
7733c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7734c58f1c22SToby Isaac {
7735c58f1c22SToby Isaac   DMLabel        label;
7736c58f1c22SToby Isaac   PetscErrorCode ierr;
7737c58f1c22SToby Isaac 
7738c58f1c22SToby Isaac   PetscFunctionBegin;
7739c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7740c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7741c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7742c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7743c58f1c22SToby Isaac   *ids = NULL;
7744dab2e251SBlaise Bourdin  if (label) {
7745c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7746dab2e251SBlaise Bourdin   } else {
7747dab2e251SBlaise Bourdin     /* returning an empty IS */
7748dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7749dab2e251SBlaise Bourdin   }
7750c58f1c22SToby Isaac   PetscFunctionReturn(0);
7751c58f1c22SToby Isaac }
7752c58f1c22SToby Isaac 
7753c58f1c22SToby Isaac /*@C
7754c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7755c58f1c22SToby Isaac 
7756c58f1c22SToby Isaac   Not Collective
7757c58f1c22SToby Isaac 
7758c58f1c22SToby Isaac   Input Parameters:
7759c58f1c22SToby Isaac + dm - The DM object
7760c58f1c22SToby Isaac . name - The label name
7761c58f1c22SToby Isaac - value - The stratum value
7762c58f1c22SToby Isaac 
7763c58f1c22SToby Isaac   Output Parameter:
7764c58f1c22SToby Isaac . size - The stratum size
7765c58f1c22SToby Isaac 
7766c58f1c22SToby Isaac   Level: beginner
7767c58f1c22SToby Isaac 
7768c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7769c58f1c22SToby Isaac @*/
7770c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7771c58f1c22SToby Isaac {
7772c58f1c22SToby Isaac   DMLabel        label;
7773c58f1c22SToby Isaac   PetscErrorCode ierr;
7774c58f1c22SToby Isaac 
7775c58f1c22SToby Isaac   PetscFunctionBegin;
7776c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7777c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7778534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7779c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7780c58f1c22SToby Isaac   *size = 0;
7781c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7782c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7783c58f1c22SToby Isaac   PetscFunctionReturn(0);
7784c58f1c22SToby Isaac }
7785c58f1c22SToby Isaac 
7786c58f1c22SToby Isaac /*@C
7787c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7788c58f1c22SToby Isaac 
7789c58f1c22SToby Isaac   Not Collective
7790c58f1c22SToby Isaac 
7791c58f1c22SToby Isaac   Input Parameters:
7792c58f1c22SToby Isaac + dm - The DM object
7793c58f1c22SToby Isaac . name - The label name
7794c58f1c22SToby Isaac - value - The stratum value
7795c58f1c22SToby Isaac 
7796c58f1c22SToby Isaac   Output Parameter:
7797c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7798c58f1c22SToby Isaac 
7799c58f1c22SToby Isaac   Level: beginner
7800c58f1c22SToby Isaac 
7801c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7802c58f1c22SToby Isaac @*/
7803c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7804c58f1c22SToby Isaac {
7805c58f1c22SToby Isaac   DMLabel        label;
7806c58f1c22SToby Isaac   PetscErrorCode ierr;
7807c58f1c22SToby Isaac 
7808c58f1c22SToby Isaac   PetscFunctionBegin;
7809c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7810c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7811c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7812c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7813c58f1c22SToby Isaac   *points = NULL;
7814c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7815c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7816c58f1c22SToby Isaac   PetscFunctionReturn(0);
7817c58f1c22SToby Isaac }
7818c58f1c22SToby Isaac 
78194de306b1SToby Isaac /*@C
78209044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
78214de306b1SToby Isaac 
78224de306b1SToby Isaac   Not Collective
78234de306b1SToby Isaac 
78244de306b1SToby Isaac   Input Parameters:
78254de306b1SToby Isaac + dm - The DM object
78264de306b1SToby Isaac . name - The label name
78274de306b1SToby Isaac . value - The stratum value
78284de306b1SToby Isaac - points - The stratum points
78294de306b1SToby Isaac 
78304de306b1SToby Isaac   Level: beginner
78314de306b1SToby Isaac 
78324de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
78334de306b1SToby Isaac @*/
78344de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
78354de306b1SToby Isaac {
78364de306b1SToby Isaac   DMLabel        label;
78374de306b1SToby Isaac   PetscErrorCode ierr;
78384de306b1SToby Isaac 
78394de306b1SToby Isaac   PetscFunctionBegin;
78404de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78414de306b1SToby Isaac   PetscValidCharPointer(name, 2);
78424de306b1SToby Isaac   PetscValidPointer(points, 4);
78434de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
78444de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
78454de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
78464de306b1SToby Isaac   PetscFunctionReturn(0);
78474de306b1SToby Isaac }
78484de306b1SToby Isaac 
7849c58f1c22SToby Isaac /*@C
7850c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7851c58f1c22SToby Isaac 
7852c58f1c22SToby Isaac   Not Collective
7853c58f1c22SToby Isaac 
7854c58f1c22SToby Isaac   Input Parameters:
7855c58f1c22SToby Isaac + dm   - The DM object
7856c58f1c22SToby Isaac . name - The label name
7857c58f1c22SToby Isaac - value - The label value for this point
7858c58f1c22SToby Isaac 
7859c58f1c22SToby Isaac   Output Parameter:
7860c58f1c22SToby Isaac 
7861c58f1c22SToby Isaac   Level: beginner
7862c58f1c22SToby Isaac 
7863c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7864c58f1c22SToby Isaac @*/
7865c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7866c58f1c22SToby Isaac {
7867c58f1c22SToby Isaac   DMLabel        label;
7868c58f1c22SToby Isaac   PetscErrorCode ierr;
7869c58f1c22SToby Isaac 
7870c58f1c22SToby Isaac   PetscFunctionBegin;
7871c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7872c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7873c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7874c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7875c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7876c58f1c22SToby Isaac   PetscFunctionReturn(0);
7877c58f1c22SToby Isaac }
7878c58f1c22SToby Isaac 
7879c58f1c22SToby Isaac /*@
7880c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7881c58f1c22SToby Isaac 
7882c58f1c22SToby Isaac   Not Collective
7883c58f1c22SToby Isaac 
7884c58f1c22SToby Isaac   Input Parameter:
7885c58f1c22SToby Isaac . dm   - The DM object
7886c58f1c22SToby Isaac 
7887c58f1c22SToby Isaac   Output Parameter:
7888c58f1c22SToby Isaac . numLabels - the number of Labels
7889c58f1c22SToby Isaac 
7890c58f1c22SToby Isaac   Level: intermediate
7891c58f1c22SToby Isaac 
7892c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7893c58f1c22SToby Isaac @*/
7894c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7895c58f1c22SToby Isaac {
78965d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7897c58f1c22SToby Isaac   PetscInt  n    = 0;
7898c58f1c22SToby Isaac 
7899c58f1c22SToby Isaac   PetscFunctionBegin;
7900c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7901534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7902c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7903c58f1c22SToby Isaac   *numLabels = n;
7904c58f1c22SToby Isaac   PetscFunctionReturn(0);
7905c58f1c22SToby Isaac }
7906c58f1c22SToby Isaac 
7907c58f1c22SToby Isaac /*@C
7908c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7909c58f1c22SToby Isaac 
7910c58f1c22SToby Isaac   Not Collective
7911c58f1c22SToby Isaac 
7912c58f1c22SToby Isaac   Input Parameters:
7913c58f1c22SToby Isaac + dm - The DM object
7914c58f1c22SToby Isaac - n  - the label number
7915c58f1c22SToby Isaac 
7916c58f1c22SToby Isaac   Output Parameter:
7917c58f1c22SToby Isaac . name - the label name
7918c58f1c22SToby Isaac 
7919c58f1c22SToby Isaac   Level: intermediate
7920c58f1c22SToby Isaac 
7921c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7922c58f1c22SToby Isaac @*/
7923c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7924c58f1c22SToby Isaac {
79255d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7926c58f1c22SToby Isaac   PetscInt       l    = 0;
7927d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7928c58f1c22SToby Isaac 
7929c58f1c22SToby Isaac   PetscFunctionBegin;
7930c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7931c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7932c58f1c22SToby Isaac   while (next) {
7933c58f1c22SToby Isaac     if (l == n) {
7934d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7935c58f1c22SToby Isaac       PetscFunctionReturn(0);
7936c58f1c22SToby Isaac     }
7937c58f1c22SToby Isaac     ++l;
7938c58f1c22SToby Isaac     next = next->next;
7939c58f1c22SToby Isaac   }
794098921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7941c58f1c22SToby Isaac }
7942c58f1c22SToby Isaac 
7943c58f1c22SToby Isaac /*@C
7944c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7945c58f1c22SToby Isaac 
7946c58f1c22SToby Isaac   Not Collective
7947c58f1c22SToby Isaac 
7948c58f1c22SToby Isaac   Input Parameters:
7949c58f1c22SToby Isaac + dm   - The DM object
7950c58f1c22SToby Isaac - name - The label name
7951c58f1c22SToby Isaac 
7952c58f1c22SToby Isaac   Output Parameter:
7953c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7954c58f1c22SToby Isaac 
7955c58f1c22SToby Isaac   Level: intermediate
7956c58f1c22SToby Isaac 
7957c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7958c58f1c22SToby Isaac @*/
7959c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7960c58f1c22SToby Isaac {
79615d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7962d67d17b1SMatthew G. Knepley   const char    *lname;
7963c58f1c22SToby Isaac   PetscErrorCode ierr;
7964c58f1c22SToby Isaac 
7965c58f1c22SToby Isaac   PetscFunctionBegin;
7966c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7967c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7968534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7969c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7970c58f1c22SToby Isaac   while (next) {
7971d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7972d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7973c58f1c22SToby Isaac     if (*hasLabel) break;
7974c58f1c22SToby Isaac     next = next->next;
7975c58f1c22SToby Isaac   }
7976c58f1c22SToby Isaac   PetscFunctionReturn(0);
7977c58f1c22SToby Isaac }
7978c58f1c22SToby Isaac 
7979c58f1c22SToby Isaac /*@C
7980c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7981c58f1c22SToby Isaac 
7982c58f1c22SToby Isaac   Not Collective
7983c58f1c22SToby Isaac 
7984c58f1c22SToby Isaac   Input Parameters:
7985c58f1c22SToby Isaac + dm   - The DM object
7986c58f1c22SToby Isaac - name - The label name
7987c58f1c22SToby Isaac 
7988c58f1c22SToby Isaac   Output Parameter:
7989c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7990c58f1c22SToby Isaac 
79916d7c9049SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
79926d7c9049SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
79936d7c9049SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
79946d7c9049SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
79956d7c9049SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
79966d7c9049SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
79976d7c9049SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
79986d7c9049SMatthew G. Knepley 
7999c58f1c22SToby Isaac   Level: intermediate
8000c58f1c22SToby Isaac 
80016d7c9049SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
8002c58f1c22SToby Isaac @*/
8003c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
8004c58f1c22SToby Isaac {
80055d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8006c58f1c22SToby Isaac   PetscBool      hasLabel;
8007d67d17b1SMatthew G. Knepley   const char    *lname;
8008c58f1c22SToby Isaac   PetscErrorCode ierr;
8009c58f1c22SToby Isaac 
8010c58f1c22SToby Isaac   PetscFunctionBegin;
8011c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8012c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
8013c58f1c22SToby Isaac   PetscValidPointer(label, 3);
8014c58f1c22SToby Isaac   *label = NULL;
8015c58f1c22SToby Isaac   while (next) {
8016d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8017d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
8018c58f1c22SToby Isaac     if (hasLabel) {
8019c58f1c22SToby Isaac       *label = next->label;
8020c58f1c22SToby Isaac       break;
8021c58f1c22SToby Isaac     }
8022c58f1c22SToby Isaac     next = next->next;
8023c58f1c22SToby Isaac   }
8024c58f1c22SToby Isaac   PetscFunctionReturn(0);
8025c58f1c22SToby Isaac }
8026c58f1c22SToby Isaac 
8027c58f1c22SToby Isaac /*@C
8028c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
8029c58f1c22SToby Isaac 
8030c58f1c22SToby Isaac   Not Collective
8031c58f1c22SToby Isaac 
8032c58f1c22SToby Isaac   Input Parameters:
8033c58f1c22SToby Isaac + dm - The DM object
8034c58f1c22SToby Isaac - n  - the label number
8035c58f1c22SToby Isaac 
8036c58f1c22SToby Isaac   Output Parameter:
8037c58f1c22SToby Isaac . label - the label
8038c58f1c22SToby Isaac 
8039c58f1c22SToby Isaac   Level: intermediate
8040c58f1c22SToby Isaac 
8041c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8042c58f1c22SToby Isaac @*/
8043c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
8044c58f1c22SToby Isaac {
80455d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
8046c58f1c22SToby Isaac   PetscInt    l    = 0;
8047c58f1c22SToby Isaac 
8048c58f1c22SToby Isaac   PetscFunctionBegin;
8049c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8050c58f1c22SToby Isaac   PetscValidPointer(label, 3);
8051c58f1c22SToby Isaac   while (next) {
8052c58f1c22SToby Isaac     if (l == n) {
8053c58f1c22SToby Isaac       *label = next->label;
8054c58f1c22SToby Isaac       PetscFunctionReturn(0);
8055c58f1c22SToby Isaac     }
8056c58f1c22SToby Isaac     ++l;
8057c58f1c22SToby Isaac     next = next->next;
8058c58f1c22SToby Isaac   }
805998921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
8060c58f1c22SToby Isaac }
8061c58f1c22SToby Isaac 
8062c58f1c22SToby Isaac /*@C
8063c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
8064c58f1c22SToby Isaac 
8065c58f1c22SToby Isaac   Not Collective
8066c58f1c22SToby Isaac 
8067c58f1c22SToby Isaac   Input Parameters:
8068c58f1c22SToby Isaac + dm   - The DM object
8069c58f1c22SToby Isaac - label - The DMLabel
8070c58f1c22SToby Isaac 
8071c58f1c22SToby Isaac   Level: developer
8072c58f1c22SToby Isaac 
8073c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8074c58f1c22SToby Isaac @*/
8075c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
8076c58f1c22SToby Isaac {
80775d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
8078c58f1c22SToby Isaac   PetscBool      hasLabel;
8079d67d17b1SMatthew G. Knepley   const char    *lname;
80805d80c0bfSVaclav Hapla   PetscBool      flg;
8081c58f1c22SToby Isaac   PetscErrorCode ierr;
8082c58f1c22SToby Isaac 
8083c58f1c22SToby Isaac   PetscFunctionBegin;
8084c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8085d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
8086d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
8087*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(hasLabel,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
8088c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
8089c58f1c22SToby Isaac   tmpLabel->label  = label;
8090c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
80915d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
80925d80c0bfSVaclav Hapla   *p = tmpLabel;
809308f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
80945d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
80955d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
8096ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
8097ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
8098c58f1c22SToby Isaac   PetscFunctionReturn(0);
8099c58f1c22SToby Isaac }
8100c58f1c22SToby Isaac 
8101c58f1c22SToby Isaac /*@C
81024a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
81034a7ee7d0SMatthew G. Knepley 
81044a7ee7d0SMatthew G. Knepley   Not Collective
81054a7ee7d0SMatthew G. Knepley 
81064a7ee7d0SMatthew G. Knepley   Input Parameters:
81074a7ee7d0SMatthew G. Knepley + dm    - The DM object
81084a7ee7d0SMatthew G. Knepley - label - The DMLabel, having the same name, to substitute
81094a7ee7d0SMatthew G. Knepley 
81104a7ee7d0SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
81114a7ee7d0SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
81124a7ee7d0SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
81134a7ee7d0SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
81144a7ee7d0SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
81154a7ee7d0SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
81164a7ee7d0SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
81174a7ee7d0SMatthew G. Knepley 
81184a7ee7d0SMatthew G. Knepley   Level: intermediate
81194a7ee7d0SMatthew G. Knepley 
81204a7ee7d0SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
81214a7ee7d0SMatthew G. Knepley @*/
81224a7ee7d0SMatthew G. Knepley PetscErrorCode DMSetLabel(DM dm, DMLabel label)
81234a7ee7d0SMatthew G. Knepley {
81244a7ee7d0SMatthew G. Knepley   DMLabelLink    next = dm->labels;
81254a7ee7d0SMatthew G. Knepley   PetscBool      hasLabel, flg;
81264a7ee7d0SMatthew G. Knepley   const char    *name, *lname;
81274a7ee7d0SMatthew G. Knepley   PetscErrorCode ierr;
81284a7ee7d0SMatthew G. Knepley 
81294a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
81304a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81314a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
81324a7ee7d0SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &name);CHKERRQ(ierr);
81334a7ee7d0SMatthew G. Knepley   while (next) {
81344a7ee7d0SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
81354a7ee7d0SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
81364a7ee7d0SMatthew G. Knepley     if (hasLabel) {
81374a7ee7d0SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
81384a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
81394a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
81404a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
81414a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
81424a7ee7d0SMatthew G. Knepley       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
81434a7ee7d0SMatthew G. Knepley       next->label = label;
81444a7ee7d0SMatthew G. Knepley       break;
81454a7ee7d0SMatthew G. Knepley     }
81464a7ee7d0SMatthew G. Knepley     next = next->next;
81474a7ee7d0SMatthew G. Knepley   }
81484a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
81494a7ee7d0SMatthew G. Knepley }
81504a7ee7d0SMatthew G. Knepley 
81514a7ee7d0SMatthew G. Knepley /*@C
8152e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
8153c58f1c22SToby Isaac 
8154c58f1c22SToby Isaac   Not Collective
8155c58f1c22SToby Isaac 
8156c58f1c22SToby Isaac   Input Parameters:
8157c58f1c22SToby Isaac + dm   - The DM object
8158c58f1c22SToby Isaac - name - The label name
8159c58f1c22SToby Isaac 
8160c58f1c22SToby Isaac   Output Parameter:
8161c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
8162c58f1c22SToby Isaac 
8163c58f1c22SToby Isaac   Level: developer
8164c58f1c22SToby Isaac 
8165e5472504SVaclav Hapla   Notes:
8166e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
8167e5472504SVaclav Hapla   DMLabelDestroy() on the label.
8168e5472504SVaclav Hapla 
8169e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
8170e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
8171e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
8172e5472504SVaclav Hapla 
8173e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
8174c58f1c22SToby Isaac @*/
8175c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
8176c58f1c22SToby Isaac {
817795d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
8178c58f1c22SToby Isaac   PetscBool      hasLabel;
8179d67d17b1SMatthew G. Knepley   const char    *lname;
8180c58f1c22SToby Isaac   PetscErrorCode ierr;
8181c58f1c22SToby Isaac 
8182c58f1c22SToby Isaac   PetscFunctionBegin;
8183c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8184e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
8185e5472504SVaclav Hapla   if (label) {
8186e5472504SVaclav Hapla     PetscValidPointer(label, 3);
8187c58f1c22SToby Isaac     *label = NULL;
8188e5472504SVaclav Hapla   }
81895d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
819095d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
8191d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
8192c58f1c22SToby Isaac     if (hasLabel) {
819395d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
8194c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
819595d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
8196ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
8197ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
819895d578d6SVaclav Hapla       if (label) *label = link->label;
819995d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
820095d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8201c58f1c22SToby Isaac       break;
8202c58f1c22SToby Isaac     }
8203c58f1c22SToby Isaac   }
8204c58f1c22SToby Isaac   PetscFunctionReturn(0);
8205c58f1c22SToby Isaac }
8206c58f1c22SToby Isaac 
8207306894acSVaclav Hapla /*@
8208306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
8209306894acSVaclav Hapla 
8210306894acSVaclav Hapla   Not Collective
8211306894acSVaclav Hapla 
8212306894acSVaclav Hapla   Input Parameters:
8213306894acSVaclav Hapla + dm   - The DM object
8214876aa926SVaclav Hapla . label - The DMLabel to be removed from the DM
8215306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
8216306894acSVaclav Hapla 
8217306894acSVaclav Hapla   Level: developer
8218306894acSVaclav Hapla 
8219306894acSVaclav Hapla   Notes:
8220306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
8221306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
8222306894acSVaclav Hapla   *label nullified.
8223306894acSVaclav Hapla 
8224306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
8225306894acSVaclav Hapla @*/
8226306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
8227306894acSVaclav Hapla {
822843e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
8229306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
8230306894acSVaclav Hapla   PetscErrorCode ierr;
8231306894acSVaclav Hapla 
8232306894acSVaclav Hapla   PetscFunctionBegin;
8233306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8234306894acSVaclav Hapla   PetscValidPointer(label, 2);
8235f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
8236306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
8237306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
82385d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
823943e45a93SVaclav Hapla     if (*label == link->label) {
8240306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
824143e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
8242306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
8243ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
824443e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
824543e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
824643e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8247306894acSVaclav Hapla       break;
8248306894acSVaclav Hapla     }
8249306894acSVaclav Hapla   }
8250*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!hasLabel && failNotFound,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
8251306894acSVaclav Hapla   PetscFunctionReturn(0);
8252306894acSVaclav Hapla }
8253306894acSVaclav Hapla 
8254c58f1c22SToby Isaac /*@C
8255c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
8256c58f1c22SToby Isaac 
8257c58f1c22SToby Isaac   Not Collective
8258c58f1c22SToby Isaac 
8259c58f1c22SToby Isaac   Input Parameters:
8260c58f1c22SToby Isaac + dm   - The DM object
8261c58f1c22SToby Isaac - name - The label name
8262c58f1c22SToby Isaac 
8263c58f1c22SToby Isaac   Output Parameter:
8264c58f1c22SToby Isaac . output - The flag for output
8265c58f1c22SToby Isaac 
8266c58f1c22SToby Isaac   Level: developer
8267c58f1c22SToby Isaac 
8268c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8269c58f1c22SToby Isaac @*/
8270c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
8271c58f1c22SToby Isaac {
82725d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8273d67d17b1SMatthew G. Knepley   const char    *lname;
8274c58f1c22SToby Isaac   PetscErrorCode ierr;
8275c58f1c22SToby Isaac 
8276c58f1c22SToby Isaac   PetscFunctionBegin;
8277c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8278c58f1c22SToby Isaac   PetscValidPointer(name, 2);
8279c58f1c22SToby Isaac   PetscValidPointer(output, 3);
8280c58f1c22SToby Isaac   while (next) {
8281c58f1c22SToby Isaac     PetscBool flg;
8282c58f1c22SToby Isaac 
8283d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8284d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8285c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
8286c58f1c22SToby Isaac     next = next->next;
8287c58f1c22SToby Isaac   }
828898921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8289c58f1c22SToby Isaac }
8290c58f1c22SToby Isaac 
8291c58f1c22SToby Isaac /*@C
8292c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
8293c58f1c22SToby Isaac 
8294c58f1c22SToby Isaac   Not Collective
8295c58f1c22SToby Isaac 
8296c58f1c22SToby Isaac   Input Parameters:
8297c58f1c22SToby Isaac + dm     - The DM object
8298c58f1c22SToby Isaac . name   - The label name
8299c58f1c22SToby Isaac - output - The flag for output
8300c58f1c22SToby Isaac 
8301c58f1c22SToby Isaac   Level: developer
8302c58f1c22SToby Isaac 
8303c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8304c58f1c22SToby Isaac @*/
8305c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
8306c58f1c22SToby Isaac {
83075d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8308d67d17b1SMatthew G. Knepley   const char    *lname;
8309c58f1c22SToby Isaac   PetscErrorCode ierr;
8310c58f1c22SToby Isaac 
8311c58f1c22SToby Isaac   PetscFunctionBegin;
8312c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8313534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
8314c58f1c22SToby Isaac   while (next) {
8315c58f1c22SToby Isaac     PetscBool flg;
8316c58f1c22SToby Isaac 
8317d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8318d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8319c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
8320c58f1c22SToby Isaac     next = next->next;
8321c58f1c22SToby Isaac   }
832298921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8323c58f1c22SToby Isaac }
8324c58f1c22SToby Isaac 
8325c58f1c22SToby Isaac /*@
8326c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
8327c58f1c22SToby Isaac 
8328d083f849SBarry Smith   Collective on dmA
8329c58f1c22SToby Isaac 
8330d8d19677SJose E. Roman   Input Parameters:
83315d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
83322cbb9b06SVaclav Hapla . dmB - The DM object to which labels are copied
83335d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
83342cbb9b06SVaclav Hapla . all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
83352cbb9b06SVaclav Hapla - emode - How to behave when a DMLabel in the source and destination DMs with the same name is encountered (see DMCopyLabelsMode)
8336c58f1c22SToby Isaac 
8337c58f1c22SToby Isaac   Level: intermediate
8338c58f1c22SToby Isaac 
83392cbb9b06SVaclav Hapla   Notes:
83402cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
8341c58f1c22SToby Isaac 
83422cbb9b06SVaclav Hapla .seealso: DMAddLabel(), DMCopyLabelsMode
8343c58f1c22SToby Isaac @*/
83442cbb9b06SVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
8345c58f1c22SToby Isaac {
83462cbb9b06SVaclav Hapla   DMLabel        label, labelNew, labelOld;
8347c58f1c22SToby Isaac   const char    *name;
8348c58f1c22SToby Isaac   PetscBool      flg;
83495d80c0bfSVaclav Hapla   DMLabelLink    link;
83505d80c0bfSVaclav Hapla   PetscErrorCode ierr;
8351c58f1c22SToby Isaac 
83525d80c0bfSVaclav Hapla   PetscFunctionBegin;
83535d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
83545d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
83555d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
83565d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
8357*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(mode==PETSC_USE_POINTER,PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
83585d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
83595d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
83605d80c0bfSVaclav Hapla     label=link->label;
83615d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
83625d80c0bfSVaclav Hapla     if (!all) {
8363c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
8364c58f1c22SToby Isaac       if (flg) continue;
83657d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
83667d5acc75SStefano Zampini       if (flg) continue;
8367ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
8368ba2698f1SMatthew G. Knepley       if (flg) continue;
83695d80c0bfSVaclav Hapla     }
83702cbb9b06SVaclav Hapla     ierr = DMGetLabel(dmB, name, &labelOld);CHKERRQ(ierr);
83712cbb9b06SVaclav Hapla     if (labelOld) {
83722cbb9b06SVaclav Hapla       switch (emode) {
83732cbb9b06SVaclav Hapla         case DM_COPY_LABELS_KEEP:
83742cbb9b06SVaclav Hapla           continue;
83752cbb9b06SVaclav Hapla         case DM_COPY_LABELS_REPLACE:
83762cbb9b06SVaclav Hapla           ierr = DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE);CHKERRQ(ierr);
83772cbb9b06SVaclav Hapla           break;
83782cbb9b06SVaclav Hapla         case DM_COPY_LABELS_FAIL:
837998921bdaSJacob Faibussowitsch           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
83802cbb9b06SVaclav Hapla         default:
838198921bdaSJacob Faibussowitsch           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", emode);
83822cbb9b06SVaclav Hapla       }
83832cbb9b06SVaclav Hapla     }
83845d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
8385c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
83865d80c0bfSVaclav Hapla     } else {
83875d80c0bfSVaclav Hapla       labelNew = label;
83885d80c0bfSVaclav Hapla     }
8389c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
83905d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
8391c58f1c22SToby Isaac   }
8392c58f1c22SToby Isaac   PetscFunctionReturn(0);
8393c58f1c22SToby Isaac }
8394461a15a0SLisandro Dalcin 
8395609dae6eSVaclav Hapla /*@C
8396609dae6eSVaclav Hapla   DMCompareLabels - Compare labels of two DMPlex meshes
8397609dae6eSVaclav Hapla 
83985efe38ccSVaclav Hapla   Collective
8399609dae6eSVaclav Hapla 
8400609dae6eSVaclav Hapla   Input Parameters:
8401609dae6eSVaclav Hapla + dm0 - First DM object
8402609dae6eSVaclav Hapla - dm1 - Second DM object
8403609dae6eSVaclav Hapla 
8404609dae6eSVaclav Hapla   Output Parameters
84055efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
8406609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
8407609dae6eSVaclav Hapla 
8408609dae6eSVaclav Hapla   Level: intermediate
8409609dae6eSVaclav Hapla 
8410609dae6eSVaclav Hapla   Notes:
84115efe38ccSVaclav Hapla   The output flag equal is the same on all processes.
84125efe38ccSVaclav Hapla   If it is passed as NULL and difference is found, an error is thrown on all processes.
84135efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
8414609dae6eSVaclav Hapla 
84155efe38ccSVaclav Hapla   The output message is set independently on each rank.
84165efe38ccSVaclav Hapla   It is set to NULL if no difference was found on the current rank. It must be freed by user.
84175efe38ccSVaclav Hapla   If message is passed as NULL and difference is found, the difference description is printed to stderr in synchronized manner.
84185efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
8419609dae6eSVaclav Hapla 
8420609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
8421609dae6eSVaclav Hapla   DMLabelCompare() is used to compare each pair of labels with the same name.
8422609dae6eSVaclav Hapla 
8423609dae6eSVaclav Hapla   Fortran Notes:
8424609dae6eSVaclav Hapla   This function is currently not available from Fortran.
8425609dae6eSVaclav Hapla 
8426609dae6eSVaclav Hapla .seealso: DMAddLabel(), DMCopyLabelsMode, DMLabelCompare()
8427609dae6eSVaclav Hapla @*/
84285efe38ccSVaclav Hapla PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
8429609dae6eSVaclav Hapla {
84305efe38ccSVaclav Hapla   PetscInt        n, i;
8431609dae6eSVaclav Hapla   char            msg[PETSC_MAX_PATH_LEN] = "";
84325efe38ccSVaclav Hapla   PetscBool       eq;
8433609dae6eSVaclav Hapla   MPI_Comm        comm;
84345efe38ccSVaclav Hapla   PetscMPIInt     rank;
8435609dae6eSVaclav Hapla   PetscErrorCode  ierr;
8436609dae6eSVaclav Hapla 
8437609dae6eSVaclav Hapla   PetscFunctionBegin;
8438609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0,DM_CLASSID,1);
8439609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1,DM_CLASSID,2);
8440609dae6eSVaclav Hapla   PetscCheckSameComm(dm0,1,dm1,2);
84415efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal,3);
8442609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
8443609dae6eSVaclav Hapla   ierr = PetscObjectGetComm((PetscObject)dm0, &comm);CHKERRQ(ierr);
84445efe38ccSVaclav Hapla   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
84455efe38ccSVaclav Hapla   {
84465efe38ccSVaclav Hapla     PetscInt n1;
84475efe38ccSVaclav Hapla 
84485efe38ccSVaclav Hapla     ierr = DMGetNumLabels(dm0, &n);CHKERRQ(ierr);
8449609dae6eSVaclav Hapla     ierr = DMGetNumLabels(dm1, &n1);CHKERRQ(ierr);
84505efe38ccSVaclav Hapla     eq = (PetscBool) (n == n1);
84515efe38ccSVaclav Hapla     if (!eq) {
84525efe38ccSVaclav Hapla       ierr = PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %D != %D = Number of labels in dm1", n, n1);CHKERRQ(ierr);
8453609dae6eSVaclav Hapla     }
84545efe38ccSVaclav Hapla     ierr = MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRMPI(ierr);
84555efe38ccSVaclav Hapla     if (!eq) goto finish;
84565efe38ccSVaclav Hapla   }
84575efe38ccSVaclav Hapla   for (i=0; i<n; i++) {
8458609dae6eSVaclav Hapla     DMLabel     l0, l1;
8459609dae6eSVaclav Hapla     const char *name;
8460609dae6eSVaclav Hapla     char       *msgInner;
8461609dae6eSVaclav Hapla 
8462609dae6eSVaclav Hapla     /* Ignore label order */
8463609dae6eSVaclav Hapla     ierr = DMGetLabelByNum(dm0, i, &l0);CHKERRQ(ierr);
8464609dae6eSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)l0, &name);CHKERRQ(ierr);
8465609dae6eSVaclav Hapla     ierr = DMGetLabel(dm1, name, &l1);CHKERRQ(ierr);
8466609dae6eSVaclav Hapla     if (!l1) {
8467609dae6eSVaclav Hapla       ierr = PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%D in dm0) not found in dm1", name, i);CHKERRQ(ierr);
84685efe38ccSVaclav Hapla       eq = PETSC_FALSE;
84695efe38ccSVaclav Hapla       break;
8470609dae6eSVaclav Hapla     }
84715efe38ccSVaclav Hapla     ierr = DMLabelCompare(comm, l0, l1, &eq, &msgInner);CHKERRQ(ierr);
8472609dae6eSVaclav Hapla     ierr = PetscStrncpy(msg, msgInner, sizeof(msg));CHKERRQ(ierr);
8473609dae6eSVaclav Hapla     ierr = PetscFree(msgInner);CHKERRQ(ierr);
84745efe38ccSVaclav Hapla     if (!eq) break;
8475609dae6eSVaclav Hapla   }
84765efe38ccSVaclav Hapla   ierr = MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRMPI(ierr);
8477609dae6eSVaclav Hapla finish:
84785efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
8479609dae6eSVaclav Hapla   if (message) {
8480609dae6eSVaclav Hapla     *message = NULL;
8481609dae6eSVaclav Hapla     if (msg[0]) {
8482609dae6eSVaclav Hapla       ierr = PetscStrallocpy(msg, message);CHKERRQ(ierr);
8483609dae6eSVaclav Hapla     }
84845efe38ccSVaclav Hapla   } else {
84855efe38ccSVaclav Hapla     if (msg[0]) {
84865efe38ccSVaclav Hapla       ierr = PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg);CHKERRQ(ierr);
8487609dae6eSVaclav Hapla     }
84885efe38ccSVaclav Hapla     ierr = PetscSynchronizedFlush(comm, PETSC_STDERR);CHKERRQ(ierr);
84895efe38ccSVaclav Hapla   }
84905efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
84915efe38ccSVaclav Hapla   if (equal) *equal = eq;
8492*2c71b3e2SJacob Faibussowitsch   else PetscCheckFalse(!eq,comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
8493609dae6eSVaclav Hapla   PetscFunctionReturn(0);
8494609dae6eSVaclav Hapla }
8495609dae6eSVaclav Hapla 
8496461a15a0SLisandro Dalcin PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
8497461a15a0SLisandro Dalcin {
8498461a15a0SLisandro Dalcin   PetscErrorCode ierr;
8499609dae6eSVaclav Hapla 
8500461a15a0SLisandro Dalcin   PetscFunctionBegin;
8501461a15a0SLisandro Dalcin   PetscValidPointer(label,2);
8502461a15a0SLisandro Dalcin   if (!*label) {
8503461a15a0SLisandro Dalcin     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
8504461a15a0SLisandro Dalcin     ierr = DMGetLabel(dm, name, label);CHKERRQ(ierr);
8505461a15a0SLisandro Dalcin   }
8506461a15a0SLisandro Dalcin   ierr = DMLabelSetValue(*label, point, value);CHKERRQ(ierr);
8507461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
8508461a15a0SLisandro Dalcin }
8509461a15a0SLisandro Dalcin 
85100fdc7489SMatthew Knepley /*
85110fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
85120fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
85130fdc7489SMatthew Knepley   (label, id) pair in the DM.
85140fdc7489SMatthew Knepley 
85150fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
85160fdc7489SMatthew Knepley   each label.
85170fdc7489SMatthew Knepley */
85180fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
85190fdc7489SMatthew Knepley {
85200fdc7489SMatthew Knepley   DMUniversalLabel ul;
85210fdc7489SMatthew Knepley   PetscBool       *active;
85220fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
85230fdc7489SMatthew Knepley   PetscErrorCode   ierr;
85240fdc7489SMatthew Knepley 
85250fdc7489SMatthew Knepley   PetscFunctionBegin;
85260fdc7489SMatthew Knepley   ierr = PetscMalloc1(1, &ul);CHKERRQ(ierr);
85270fdc7489SMatthew Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label);CHKERRQ(ierr);
85280fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
85290fdc7489SMatthew Knepley   ierr = PetscCalloc1(Nl, &active);CHKERRQ(ierr);
85300fdc7489SMatthew Knepley   ul->Nl = 0;
85310fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
85320fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
85330fdc7489SMatthew Knepley     const char *name;
85340fdc7489SMatthew Knepley 
85350fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
85360fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "depth", 6, &isdepth);CHKERRQ(ierr);
85370fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "celltype", 9, &iscelltype);CHKERRQ(ierr);
85380fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
85390fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
85400fdc7489SMatthew Knepley   }
85410fdc7489SMatthew 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);
85420fdc7489SMatthew Knepley   ul->Nv = 0;
85430fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
85440fdc7489SMatthew Knepley     DMLabel     label;
85450fdc7489SMatthew Knepley     PetscInt    nv;
85460fdc7489SMatthew Knepley     const char *name;
85470fdc7489SMatthew Knepley 
85480fdc7489SMatthew Knepley     if (!active[l]) continue;
85490fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
85500fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
85510fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
85520fdc7489SMatthew Knepley     ierr = PetscStrallocpy(name, &ul->names[m]);CHKERRQ(ierr);
85530fdc7489SMatthew Knepley     ul->indices[m]   = l;
85540fdc7489SMatthew Knepley     ul->Nv          += nv;
85550fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
85560fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
85570fdc7489SMatthew Knepley     ++m;
85580fdc7489SMatthew Knepley   }
85590fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
85600fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
85610fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
85620fdc7489SMatthew Knepley   }
85630fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
85640fdc7489SMatthew Knepley     PetscInt b;
85650fdc7489SMatthew Knepley 
85660fdc7489SMatthew Knepley     ul->masks[l] = 0;
85670fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
85680fdc7489SMatthew Knepley   }
85690fdc7489SMatthew Knepley   ierr = PetscMalloc1(ul->Nv, &ul->values);CHKERRQ(ierr);
85700fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
85710fdc7489SMatthew Knepley     DMLabel         label;
85720fdc7489SMatthew Knepley     IS              valueIS;
85730fdc7489SMatthew Knepley     const PetscInt *varr;
85740fdc7489SMatthew Knepley     PetscInt        nv, v;
85750fdc7489SMatthew Knepley 
85760fdc7489SMatthew Knepley     if (!active[l]) continue;
85770fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
85780fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
85790fdc7489SMatthew Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
85800fdc7489SMatthew Knepley     ierr = ISGetIndices(valueIS, &varr);CHKERRQ(ierr);
85810fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
85820fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
85830fdc7489SMatthew Knepley     }
85840fdc7489SMatthew Knepley     ierr = ISRestoreIndices(valueIS, &varr);CHKERRQ(ierr);
85850fdc7489SMatthew Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
85860fdc7489SMatthew Knepley     ierr = PetscSortInt(nv, &ul->values[ul->offsets[m]]);CHKERRQ(ierr);
85870fdc7489SMatthew Knepley     ++m;
85880fdc7489SMatthew Knepley   }
85890fdc7489SMatthew Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
85900fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
85910fdc7489SMatthew Knepley     PetscInt  uval = 0;
85920fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
85930fdc7489SMatthew Knepley 
85940fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
85950fdc7489SMatthew Knepley       DMLabel  label;
85960649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
85970fdc7489SMatthew Knepley 
85980fdc7489SMatthew Knepley       if (!active[l]) continue;
85990fdc7489SMatthew Knepley       ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
86000fdc7489SMatthew Knepley       ierr = DMLabelGetValue(label, p, &val);CHKERRQ(ierr);
86010fdc7489SMatthew Knepley       ierr = DMLabelGetDefaultValue(label, &defval);CHKERRQ(ierr);
86020fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
86030649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
86040fdc7489SMatthew Knepley       marked = PETSC_TRUE;
86050fdc7489SMatthew Knepley       ierr = PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc);CHKERRQ(ierr);
8606*2c71b3e2SJacob Faibussowitsch       PetscCheckFalse(loc < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %D not found in compression array", val);
86070fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
86080fdc7489SMatthew Knepley       ++m;
86090fdc7489SMatthew Knepley     }
86100fdc7489SMatthew Knepley     if (marked) {ierr = DMLabelSetValue(ul->label, p, uval);CHKERRQ(ierr);}
86110fdc7489SMatthew Knepley   }
86120fdc7489SMatthew Knepley   ierr = PetscFree(active);CHKERRQ(ierr);
86130fdc7489SMatthew Knepley   *universal = ul;
86140fdc7489SMatthew Knepley   PetscFunctionReturn(0);
86150fdc7489SMatthew Knepley }
86160fdc7489SMatthew Knepley 
86170fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
86180fdc7489SMatthew Knepley {
86190fdc7489SMatthew Knepley   PetscInt       l;
86200fdc7489SMatthew Knepley   PetscErrorCode ierr;
86210fdc7489SMatthew Knepley 
86220fdc7489SMatthew Knepley   PetscFunctionBegin;
86230fdc7489SMatthew Knepley   for (l = 0; l < (*universal)->Nl; ++l) {ierr = PetscFree((*universal)->names[l]);CHKERRQ(ierr);}
86240fdc7489SMatthew Knepley   ierr = DMLabelDestroy(&(*universal)->label);CHKERRQ(ierr);
86250fdc7489SMatthew Knepley   ierr = PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks);CHKERRQ(ierr);
86260fdc7489SMatthew Knepley   ierr = PetscFree((*universal)->values);CHKERRQ(ierr);
86270fdc7489SMatthew Knepley   ierr = PetscFree(*universal);CHKERRQ(ierr);
86280fdc7489SMatthew Knepley   *universal = NULL;
86290fdc7489SMatthew Knepley   PetscFunctionReturn(0);
86300fdc7489SMatthew Knepley }
86310fdc7489SMatthew Knepley 
86320fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
86330fdc7489SMatthew Knepley {
86340fdc7489SMatthew Knepley   PetscFunctionBegin;
86350fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
86360fdc7489SMatthew Knepley   *ulabel = ul->label;
86370fdc7489SMatthew Knepley   PetscFunctionReturn(0);
86380fdc7489SMatthew Knepley }
86390fdc7489SMatthew Knepley 
86400fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
86410fdc7489SMatthew Knepley {
86420fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
86430fdc7489SMatthew Knepley   PetscErrorCode ierr;
86440fdc7489SMatthew Knepley 
86450fdc7489SMatthew Knepley   PetscFunctionBegin;
8646064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
86470fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
86480fdc7489SMatthew Knepley     if (preserveOrder) {ierr = DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]);CHKERRQ(ierr);}
86490fdc7489SMatthew Knepley     else               {ierr = DMCreateLabel(dm, ul->names[l]);CHKERRQ(ierr);}
86500fdc7489SMatthew Knepley   }
86510fdc7489SMatthew Knepley   if (preserveOrder) {
86520fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
86530fdc7489SMatthew Knepley       const char *name;
86540fdc7489SMatthew Knepley       PetscBool   match;
86550fdc7489SMatthew Knepley 
86560fdc7489SMatthew Knepley       ierr = DMGetLabelName(dm, ul->indices[l], &name);CHKERRQ(ierr);
86570fdc7489SMatthew Knepley       ierr = PetscStrcmp(name, ul->names[l], &match);CHKERRQ(ierr);
8658*2c71b3e2SJacob Faibussowitsch       PetscCheckFalse(!match,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Label %D name %s does not match new name %s", l, name, ul->names[l]);
86590fdc7489SMatthew Knepley     }
86600fdc7489SMatthew Knepley   }
86610fdc7489SMatthew Knepley   PetscFunctionReturn(0);
86620fdc7489SMatthew Knepley }
86630fdc7489SMatthew Knepley 
86640fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
86650fdc7489SMatthew Knepley {
86660fdc7489SMatthew Knepley   PetscInt       l;
86670fdc7489SMatthew Knepley   PetscErrorCode ierr;
86680fdc7489SMatthew Knepley 
86690fdc7489SMatthew Knepley   PetscFunctionBegin;
86700fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
86710fdc7489SMatthew Knepley     DMLabel  label;
86720fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
86730fdc7489SMatthew Knepley 
86740fdc7489SMatthew Knepley     if (lval) {
86750fdc7489SMatthew Knepley       if (useIndex) {ierr = DMGetLabelByNum(dm, ul->indices[l], &label);CHKERRQ(ierr);}
86760fdc7489SMatthew Knepley       else          {ierr = DMGetLabel(dm, ul->names[l], &label);CHKERRQ(ierr);}
86770fdc7489SMatthew Knepley       ierr = DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]);CHKERRQ(ierr);
86780fdc7489SMatthew Knepley     }
86790fdc7489SMatthew Knepley   }
86800fdc7489SMatthew Knepley   PetscFunctionReturn(0);
86810fdc7489SMatthew Knepley }
8682a8fb8f29SToby Isaac 
8683a8fb8f29SToby Isaac /*@
8684a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
8685a8fb8f29SToby Isaac 
8686a8fb8f29SToby Isaac   Input Parameter:
8687a8fb8f29SToby Isaac . dm - The DM object
8688a8fb8f29SToby Isaac 
8689a8fb8f29SToby Isaac   Output Parameter:
8690a8fb8f29SToby Isaac . cdm - The coarse DM
8691a8fb8f29SToby Isaac 
8692a8fb8f29SToby Isaac   Level: intermediate
8693a8fb8f29SToby Isaac 
8694a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
8695a8fb8f29SToby Isaac @*/
8696a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
8697a8fb8f29SToby Isaac {
8698a8fb8f29SToby Isaac   PetscFunctionBegin;
8699a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8700a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
8701a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
8702a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8703a8fb8f29SToby Isaac }
8704a8fb8f29SToby Isaac 
8705a8fb8f29SToby Isaac /*@
8706a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
8707a8fb8f29SToby Isaac 
8708a8fb8f29SToby Isaac   Input Parameters:
8709a8fb8f29SToby Isaac + dm - The DM object
8710a8fb8f29SToby Isaac - cdm - The coarse DM
8711a8fb8f29SToby Isaac 
8712a8fb8f29SToby Isaac   Level: intermediate
8713a8fb8f29SToby Isaac 
8714a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
8715a8fb8f29SToby Isaac @*/
8716a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
8717a8fb8f29SToby Isaac {
8718a8fb8f29SToby Isaac   PetscErrorCode ierr;
8719a8fb8f29SToby Isaac 
8720a8fb8f29SToby Isaac   PetscFunctionBegin;
8721a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8722a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
8723a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
8724a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
8725a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
8726a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8727a8fb8f29SToby Isaac }
8728a8fb8f29SToby Isaac 
872988bdff64SToby Isaac /*@
873088bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
873188bdff64SToby Isaac 
873288bdff64SToby Isaac   Input Parameter:
873388bdff64SToby Isaac . dm - The DM object
873488bdff64SToby Isaac 
873588bdff64SToby Isaac   Output Parameter:
873688bdff64SToby Isaac . fdm - The fine DM
873788bdff64SToby Isaac 
873888bdff64SToby Isaac   Level: intermediate
873988bdff64SToby Isaac 
874088bdff64SToby Isaac .seealso: DMSetFineDM()
874188bdff64SToby Isaac @*/
874288bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
874388bdff64SToby Isaac {
874488bdff64SToby Isaac   PetscFunctionBegin;
874588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
874688bdff64SToby Isaac   PetscValidPointer(fdm, 2);
874788bdff64SToby Isaac   *fdm = dm->fineMesh;
874888bdff64SToby Isaac   PetscFunctionReturn(0);
874988bdff64SToby Isaac }
875088bdff64SToby Isaac 
875188bdff64SToby Isaac /*@
875288bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
875388bdff64SToby Isaac 
875488bdff64SToby Isaac   Input Parameters:
875588bdff64SToby Isaac + dm - The DM object
875688bdff64SToby Isaac - fdm - The fine DM
875788bdff64SToby Isaac 
875888bdff64SToby Isaac   Level: intermediate
875988bdff64SToby Isaac 
876088bdff64SToby Isaac .seealso: DMGetFineDM()
876188bdff64SToby Isaac @*/
876288bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
876388bdff64SToby Isaac {
876488bdff64SToby Isaac   PetscErrorCode ierr;
876588bdff64SToby Isaac 
876688bdff64SToby Isaac   PetscFunctionBegin;
876788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
876888bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
876988bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
877088bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
877188bdff64SToby Isaac   dm->fineMesh = fdm;
877288bdff64SToby Isaac   PetscFunctionReturn(0);
877388bdff64SToby Isaac }
877488bdff64SToby Isaac 
8775a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8776a6ba4734SToby Isaac 
8777a6ba4734SToby Isaac /*@C
8778a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8779a6ba4734SToby Isaac 
8780783e2ec8SMatthew G. Knepley   Collective on dm
8781783e2ec8SMatthew G. Knepley 
8782a6ba4734SToby Isaac   Input Parameters:
87834c258f51SMatthew G. Knepley + dm       - The DM, with a PetscDS that matches the problem being constrained
8784f971fd6bSMatthew G. Knepley . type     - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8785a6ba4734SToby Isaac . name     - The BC name
878645480ffeSMatthew G. Knepley . label    - The label defining constrained points
878745480ffeSMatthew G. Knepley . Nv       - The number of DMLabel values for constrained points
878845480ffeSMatthew G. Knepley . values   - An array of values for constrained points
8789a6ba4734SToby Isaac . field    - The field to constrain
879045480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
8791a6ba4734SToby Isaac . comps    - An array of constrained component numbers
8792a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
879356cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
8794a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
8795a6ba4734SToby Isaac 
879645480ffeSMatthew G. Knepley   Output Parameter:
879745480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
879845480ffeSMatthew G. Knepley 
8799a6ba4734SToby Isaac   Options Database Keys:
8800a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8801a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8802a6ba4734SToby Isaac 
880356cf3b9cSMatthew G. Knepley   Note:
880456cf3b9cSMatthew 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:
880556cf3b9cSMatthew G. Knepley 
880656cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
880756cf3b9cSMatthew G. Knepley 
880856cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
880956cf3b9cSMatthew G. Knepley 
881056cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
881156cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
881256cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
881356cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
881456cf3b9cSMatthew G. Knepley 
881556cf3b9cSMatthew G. Knepley + dim - the spatial dimension
881656cf3b9cSMatthew G. Knepley . Nf - the number of fields
881756cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
881856cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
881956cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
882056cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
882156cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
882256cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
882356cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
882456cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
882556cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
882656cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
882756cf3b9cSMatthew G. Knepley . t - current time
882856cf3b9cSMatthew G. Knepley . x - coordinates of the current point
882956cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
883056cf3b9cSMatthew G. Knepley . constants - constant parameters
883156cf3b9cSMatthew G. Knepley - bcval - output values at the current point
883256cf3b9cSMatthew G. Knepley 
8833a6ba4734SToby Isaac   Level: developer
8834a6ba4734SToby Isaac 
883545480ffeSMatthew G. Knepley .seealso: DSGetBoundary(), PetscDSAddBoundary()
8836a6ba4734SToby Isaac @*/
883745480ffeSMatthew 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)
8838a6ba4734SToby Isaac {
8839e5e52638SMatthew G. Knepley   PetscDS        ds;
8840a6ba4734SToby Isaac   PetscErrorCode ierr;
8841a6ba4734SToby Isaac 
8842a6ba4734SToby Isaac   PetscFunctionBegin;
8843a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8844783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
884545480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
884645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
884745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
884845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
8849e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
885045480ffeSMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, label);CHKERRQ(ierr);
885145480ffeSMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd);CHKERRQ(ierr);
8852a6ba4734SToby Isaac   PetscFunctionReturn(0);
8853a6ba4734SToby Isaac }
8854a6ba4734SToby Isaac 
885545480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8856e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8857e6f8dbb6SToby Isaac {
8858e5e52638SMatthew G. Knepley   PetscDS        ds;
8859dff059c6SToby Isaac   DMBoundary    *lastnext;
8860e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8861e6f8dbb6SToby Isaac   PetscErrorCode ierr;
8862e6f8dbb6SToby Isaac 
8863e6f8dbb6SToby Isaac   PetscFunctionBegin;
8864e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8865e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
886647a1f5adSToby Isaac   if (dm->boundary) {
886747a1f5adSToby Isaac     DMBoundary next = dm->boundary;
886847a1f5adSToby Isaac 
886947a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
887047a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
887147a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
887247a1f5adSToby Isaac     while (next) {
887347a1f5adSToby Isaac       DMBoundary b = next;
887447a1f5adSToby Isaac 
887547a1f5adSToby Isaac       next = b->next;
887647a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
8877a6ba4734SToby Isaac     }
887847a1f5adSToby Isaac     dm->boundary = NULL;
8879a6ba4734SToby Isaac   }
888047a1f5adSToby Isaac 
8881dff059c6SToby Isaac   lastnext = &(dm->boundary);
8882e6f8dbb6SToby Isaac   while (dsbound) {
8883e6f8dbb6SToby Isaac     DMBoundary dmbound;
8884e6f8dbb6SToby Isaac 
8885e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
8886e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
888745480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
888847a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8889dff059c6SToby Isaac     *lastnext = dmbound;
8890dff059c6SToby Isaac     lastnext = &(dmbound->next);
8891dff059c6SToby Isaac     dsbound = dsbound->next;
8892a6ba4734SToby Isaac   }
8893a6ba4734SToby Isaac   PetscFunctionReturn(0);
8894a6ba4734SToby Isaac }
8895a6ba4734SToby Isaac 
8896a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8897a6ba4734SToby Isaac {
8898b95f2879SToby Isaac   DMBoundary     b;
8899a6ba4734SToby Isaac   PetscErrorCode ierr;
8900a6ba4734SToby Isaac 
8901a6ba4734SToby Isaac   PetscFunctionBegin;
8902a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8903534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8904a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8905e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
8906b95f2879SToby Isaac   b = dm->boundary;
8907a6ba4734SToby Isaac   while (b && !(*isBd)) {
8908e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8909e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8910a6ba4734SToby Isaac     PetscInt   i;
8911a6ba4734SToby Isaac 
891245480ffeSMatthew G. Knepley     if (label) {
891345480ffeSMatthew G. Knepley       for (i = 0; i < dsb->Nv && !(*isBd); ++i) {ierr = DMLabelStratumHasPoint(label, dsb->values[i], point, isBd);CHKERRQ(ierr);}
8914a6ba4734SToby Isaac     }
8915a6ba4734SToby Isaac     b = b->next;
8916a6ba4734SToby Isaac   }
8917a6ba4734SToby Isaac   PetscFunctionReturn(0);
8918a6ba4734SToby Isaac }
89194d6f44ffSToby Isaac 
89204d6f44ffSToby Isaac /*@C
8921a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8922a6e0b375SMatthew G. Knepley 
8923a6e0b375SMatthew G. Knepley   Collective on DM
89244d6f44ffSToby Isaac 
89254d6f44ffSToby Isaac   Input Parameters:
89264d6f44ffSToby Isaac + dm      - The DM
89270709b2feSToby Isaac . time    - The time
89284d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
89294d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
89304d6f44ffSToby Isaac - mode    - The insertion mode for values
89314d6f44ffSToby Isaac 
89324d6f44ffSToby Isaac   Output Parameter:
89334d6f44ffSToby Isaac . X - vector
89344d6f44ffSToby Isaac 
89354d6f44ffSToby Isaac    Calling sequence of func:
893677b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
89374d6f44ffSToby Isaac 
89384d6f44ffSToby Isaac +  dim - The spatial dimension
89398ec8862eSJed Brown .  time - The time at which to sample
89404d6f44ffSToby Isaac .  x   - The coordinates
894177b739a6SMatthew Knepley .  Nc  - The number of components
89424d6f44ffSToby Isaac .  u   - The output field values
89434d6f44ffSToby Isaac -  ctx - optional user-defined function context
89444d6f44ffSToby Isaac 
89454d6f44ffSToby Isaac   Level: developer
89464d6f44ffSToby Isaac 
8947a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
89484d6f44ffSToby Isaac @*/
89490709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
89504d6f44ffSToby Isaac {
89514d6f44ffSToby Isaac   Vec            localX;
89524d6f44ffSToby Isaac   PetscErrorCode ierr;
89534d6f44ffSToby Isaac 
89544d6f44ffSToby Isaac   PetscFunctionBegin;
89554d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89564d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
89570709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
89584d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
89594d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
89604d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
89614d6f44ffSToby Isaac   PetscFunctionReturn(0);
89624d6f44ffSToby Isaac }
89634d6f44ffSToby Isaac 
8964a6e0b375SMatthew G. Knepley /*@C
8965a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8966a6e0b375SMatthew G. Knepley 
8967a6e0b375SMatthew G. Knepley   Not collective
8968a6e0b375SMatthew G. Knepley 
8969a6e0b375SMatthew G. Knepley   Input Parameters:
8970a6e0b375SMatthew G. Knepley + dm      - The DM
8971a6e0b375SMatthew G. Knepley . time    - The time
8972a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8973a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8974a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8975a6e0b375SMatthew G. Knepley 
8976a6e0b375SMatthew G. Knepley   Output Parameter:
8977a6e0b375SMatthew G. Knepley . localX - vector
8978a6e0b375SMatthew G. Knepley 
8979a6e0b375SMatthew G. Knepley    Calling sequence of func:
898077b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8981a6e0b375SMatthew G. Knepley 
8982a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8983a6e0b375SMatthew G. Knepley .  x   - The coordinates
898477b739a6SMatthew Knepley .  Nc  - The number of components
8985a6e0b375SMatthew G. Knepley .  u   - The output field values
8986a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8987a6e0b375SMatthew G. Knepley 
8988a6e0b375SMatthew G. Knepley   Level: developer
8989a6e0b375SMatthew G. Knepley 
8990a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8991a6e0b375SMatthew G. Knepley @*/
89920709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
89934d6f44ffSToby Isaac {
89944d6f44ffSToby Isaac   PetscErrorCode ierr;
89954d6f44ffSToby Isaac 
89964d6f44ffSToby Isaac   PetscFunctionBegin;
89974d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8998064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
8999*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->projectfunctionlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
90000709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
90014d6f44ffSToby Isaac   PetscFunctionReturn(0);
90024d6f44ffSToby Isaac }
90034d6f44ffSToby Isaac 
9004a6e0b375SMatthew G. Knepley /*@C
9005a6e0b375SMatthew 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.
9006a6e0b375SMatthew G. Knepley 
9007a6e0b375SMatthew G. Knepley   Collective on DM
9008a6e0b375SMatthew G. Knepley 
9009a6e0b375SMatthew G. Knepley   Input Parameters:
9010a6e0b375SMatthew G. Knepley + dm      - The DM
9011a6e0b375SMatthew G. Knepley . time    - The time
9012a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
9013a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
9014a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
9015a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9016a6e0b375SMatthew G. Knepley 
9017a6e0b375SMatthew G. Knepley   Output Parameter:
9018a6e0b375SMatthew G. Knepley . X - vector
9019a6e0b375SMatthew G. Knepley 
9020a6e0b375SMatthew G. Knepley    Calling sequence of func:
902177b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
9022a6e0b375SMatthew G. Knepley 
9023a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
9024a6e0b375SMatthew G. Knepley .  x   - The coordinates
902577b739a6SMatthew Knepley .  Nc  - The number of components
9026a6e0b375SMatthew G. Knepley .  u   - The output field values
9027a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
9028a6e0b375SMatthew G. Knepley 
9029a6e0b375SMatthew G. Knepley   Level: developer
9030a6e0b375SMatthew G. Knepley 
9031a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
9032a6e0b375SMatthew G. Knepley @*/
90332c53366bSMatthew 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)
90342c53366bSMatthew G. Knepley {
90352c53366bSMatthew G. Knepley   Vec            localX;
90362c53366bSMatthew G. Knepley   PetscErrorCode ierr;
90372c53366bSMatthew G. Knepley 
90382c53366bSMatthew G. Knepley   PetscFunctionBegin;
90392c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90402c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
90412c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
90422c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
90432c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
90442c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
90452c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
90462c53366bSMatthew G. Knepley }
90472c53366bSMatthew G. Knepley 
9048a6e0b375SMatthew G. Knepley /*@C
9049a6e0b375SMatthew 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.
9050a6e0b375SMatthew G. Knepley 
9051a6e0b375SMatthew G. Knepley   Not collective
9052a6e0b375SMatthew G. Knepley 
9053a6e0b375SMatthew G. Knepley   Input Parameters:
9054a6e0b375SMatthew G. Knepley + dm      - The DM
9055a6e0b375SMatthew G. Knepley . time    - The time
9056a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
9057a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
9058a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
9059a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9060a6e0b375SMatthew G. Knepley 
9061a6e0b375SMatthew G. Knepley   Output Parameter:
9062a6e0b375SMatthew G. Knepley . localX - vector
9063a6e0b375SMatthew G. Knepley 
9064a6e0b375SMatthew G. Knepley    Calling sequence of func:
906577b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
9066a6e0b375SMatthew G. Knepley 
9067a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
9068a6e0b375SMatthew G. Knepley .  x   - The coordinates
906977b739a6SMatthew Knepley .  Nc  - The number of components
9070a6e0b375SMatthew G. Knepley .  u   - The output field values
9071a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
9072a6e0b375SMatthew G. Knepley 
9073a6e0b375SMatthew G. Knepley   Level: developer
9074a6e0b375SMatthew G. Knepley 
9075a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
9076a6e0b375SMatthew G. Knepley @*/
90771c531cf8SMatthew 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)
90784d6f44ffSToby Isaac {
90794d6f44ffSToby Isaac   PetscErrorCode ierr;
90804d6f44ffSToby Isaac 
90814d6f44ffSToby Isaac   PetscFunctionBegin;
90824d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9083064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
9084*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->projectfunctionlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
90851c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
90864d6f44ffSToby Isaac   PetscFunctionReturn(0);
90874d6f44ffSToby Isaac }
90882716604bSToby Isaac 
9089a6e0b375SMatthew G. Knepley /*@C
9090a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
9091a6e0b375SMatthew G. Knepley 
9092a6e0b375SMatthew G. Knepley   Not collective
9093a6e0b375SMatthew G. Knepley 
9094a6e0b375SMatthew G. Knepley   Input Parameters:
9095a6e0b375SMatthew G. Knepley + dm      - The DM
9096a6e0b375SMatthew G. Knepley . time    - The time
9097a6e0b375SMatthew G. Knepley . localU  - The input field vector
9098a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9099a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9100a6e0b375SMatthew G. Knepley 
9101a6e0b375SMatthew G. Knepley   Output Parameter:
9102a6e0b375SMatthew G. Knepley . localX  - The output vector
9103a6e0b375SMatthew G. Knepley 
9104a6e0b375SMatthew G. Knepley    Calling sequence of func:
9105a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9106a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9107a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9108a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9109a6e0b375SMatthew G. Knepley 
9110a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
9111a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
9112a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9113a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
9114a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9115a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
9116a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9117a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9118a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9119a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9120a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9121a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9122a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9123a6e0b375SMatthew G. Knepley .  t            - The current time
9124a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9125a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9126a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9127a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9128a6e0b375SMatthew G. Knepley 
9129a6e0b375SMatthew 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.
9130a6e0b375SMatthew 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
9131a6e0b375SMatthew 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
9132a6e0b375SMatthew 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.
9133a6e0b375SMatthew G. Knepley 
9134a6e0b375SMatthew G. Knepley   Level: intermediate
9135a6e0b375SMatthew G. Knepley 
9136a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9137a6e0b375SMatthew G. Knepley @*/
91388c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
91398c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
91408c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
91418c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9142191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
91438c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
91448c6c5593SMatthew G. Knepley {
91458c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
91468c6c5593SMatthew G. Knepley 
91478c6c5593SMatthew G. Knepley   PetscFunctionBegin;
91488c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
91498c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
91508c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
9151*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->projectfieldlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
91528c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
91538c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
91548c6c5593SMatthew G. Knepley }
91558c6c5593SMatthew G. Knepley 
9156a6e0b375SMatthew G. Knepley /*@C
9157a6e0b375SMatthew 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.
9158a6e0b375SMatthew G. Knepley 
9159a6e0b375SMatthew G. Knepley   Not collective
9160a6e0b375SMatthew G. Knepley 
9161a6e0b375SMatthew G. Knepley   Input Parameters:
9162a6e0b375SMatthew G. Knepley + dm      - The DM
9163a6e0b375SMatthew G. Knepley . time    - The time
9164a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
9165a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
9166a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
9167a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9168a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9169a6e0b375SMatthew G. Knepley . localU  - The input field vector
9170a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9171a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9172a6e0b375SMatthew G. Knepley 
9173a6e0b375SMatthew G. Knepley   Output Parameter:
9174a6e0b375SMatthew G. Knepley . localX  - The output vector
9175a6e0b375SMatthew G. Knepley 
9176a6e0b375SMatthew G. Knepley    Calling sequence of func:
9177a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9178a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9179a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9180a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9181a6e0b375SMatthew G. Knepley 
9182a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
9183a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
9184a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9185a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
9186a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9187a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
9188a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9189a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9190a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9191a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9192a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9193a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9194a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9195a6e0b375SMatthew G. Knepley .  t            - The current time
9196a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9197a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9198a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9199a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9200a6e0b375SMatthew G. Knepley 
9201a6e0b375SMatthew 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.
9202a6e0b375SMatthew 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
9203a6e0b375SMatthew 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
9204a6e0b375SMatthew 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.
9205a6e0b375SMatthew G. Knepley 
9206a6e0b375SMatthew G. Knepley   Level: intermediate
9207a6e0b375SMatthew G. Knepley 
9208a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9209a6e0b375SMatthew G. Knepley @*/
92101c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
92118c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
92128c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
92138c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9214191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
92158c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
92168c6c5593SMatthew G. Knepley {
92178c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
92188c6c5593SMatthew G. Knepley 
92198c6c5593SMatthew G. Knepley   PetscFunctionBegin;
92208c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9221064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9222064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
9223*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->projectfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
92241c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
92258c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
92268c6c5593SMatthew G. Knepley }
92278c6c5593SMatthew G. Knepley 
92282716604bSToby Isaac /*@C
9229ece3a9fcSMatthew 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.
9230ece3a9fcSMatthew G. Knepley 
9231ece3a9fcSMatthew G. Knepley   Not collective
9232ece3a9fcSMatthew G. Knepley 
9233ece3a9fcSMatthew G. Knepley   Input Parameters:
9234ece3a9fcSMatthew G. Knepley + dm      - The DM
9235ece3a9fcSMatthew G. Knepley . time    - The time
9236ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
9237ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
9238ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
9239ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9240ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9241ece3a9fcSMatthew G. Knepley . localU  - The input field vector
9242ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9243ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
9244ece3a9fcSMatthew G. Knepley 
9245ece3a9fcSMatthew G. Knepley   Output Parameter:
9246ece3a9fcSMatthew G. Knepley . localX  - The output vector
9247ece3a9fcSMatthew G. Knepley 
9248ece3a9fcSMatthew G. Knepley    Calling sequence of func:
9249ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9250ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9251ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9252ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9253ece3a9fcSMatthew G. Knepley 
9254ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
9255ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
9256ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9257ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
9258ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9259ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
9260ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9261ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9262ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9263ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9264ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9265ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9266ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9267ece3a9fcSMatthew G. Knepley .  t            - The current time
9268ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
9269ece3a9fcSMatthew G. Knepley .  n            - The face normal
9270ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
9271ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
9272ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
9273ece3a9fcSMatthew G. Knepley 
9274ece3a9fcSMatthew G. Knepley   Note:
9275ece3a9fcSMatthew 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.
9276ece3a9fcSMatthew 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
9277ece3a9fcSMatthew 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
9278ece3a9fcSMatthew 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.
9279ece3a9fcSMatthew G. Knepley 
9280ece3a9fcSMatthew G. Knepley   Level: intermediate
9281ece3a9fcSMatthew G. Knepley 
9282ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9283ece3a9fcSMatthew G. Knepley @*/
9284ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
9285ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
9286ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9287ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9288ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
9289ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
9290ece3a9fcSMatthew G. Knepley {
9291ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
9292ece3a9fcSMatthew G. Knepley 
9293ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
9294ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9295064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9296064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
9297*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->projectbdfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
9298ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
9299ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
9300ece3a9fcSMatthew G. Knepley }
9301ece3a9fcSMatthew G. Knepley 
9302ece3a9fcSMatthew G. Knepley /*@C
93032716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
93042716604bSToby Isaac 
93052716604bSToby Isaac   Input Parameters:
93062716604bSToby Isaac + dm    - The DM
93070709b2feSToby Isaac . time  - The time
93082716604bSToby Isaac . funcs - The functions to evaluate for each field component
93092716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9310574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
93112716604bSToby Isaac 
93122716604bSToby Isaac   Output Parameter:
93132716604bSToby Isaac . diff - The diff ||u - u_h||_2
93142716604bSToby Isaac 
93152716604bSToby Isaac   Level: developer
93162716604bSToby Isaac 
93171189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
93182716604bSToby Isaac @*/
93190709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
93202716604bSToby Isaac {
93212716604bSToby Isaac   PetscErrorCode ierr;
93222716604bSToby Isaac 
93232716604bSToby Isaac   PetscFunctionBegin;
93242716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9325b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
9326*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->computel2diff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
93270709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
93282716604bSToby Isaac   PetscFunctionReturn(0);
93292716604bSToby Isaac }
9330b698f381SToby Isaac 
9331b698f381SToby Isaac /*@C
9332b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
9333b698f381SToby Isaac 
9334d083f849SBarry Smith   Collective on dm
9335d083f849SBarry Smith 
9336b698f381SToby Isaac   Input Parameters:
9337b698f381SToby Isaac + dm    - The DM
9338b698f381SToby Isaac , time  - The time
9339b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
9340b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9341574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
9342b698f381SToby Isaac - n     - The vector to project along
9343b698f381SToby Isaac 
9344b698f381SToby Isaac   Output Parameter:
9345b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
9346b698f381SToby Isaac 
9347b698f381SToby Isaac   Level: developer
9348b698f381SToby Isaac 
9349b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
9350b698f381SToby Isaac @*/
9351b698f381SToby 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)
9352b698f381SToby Isaac {
9353b698f381SToby Isaac   PetscErrorCode ierr;
9354b698f381SToby Isaac 
9355b698f381SToby Isaac   PetscFunctionBegin;
9356b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9357b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
9358*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->computel2gradientdiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
9359b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
9360b698f381SToby Isaac   PetscFunctionReturn(0);
9361b698f381SToby Isaac }
9362b698f381SToby Isaac 
93632a16baeaSToby Isaac /*@C
93642a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
93652a16baeaSToby Isaac 
9366d083f849SBarry Smith   Collective on dm
9367d083f849SBarry Smith 
93682a16baeaSToby Isaac   Input Parameters:
93692a16baeaSToby Isaac + dm    - The DM
93702a16baeaSToby Isaac . time  - The time
93712a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
93722a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9373574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
93742a16baeaSToby Isaac 
93752a16baeaSToby Isaac   Output Parameter:
93762a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
93772a16baeaSToby Isaac 
93782a16baeaSToby Isaac   Level: developer
93792a16baeaSToby Isaac 
93801189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
93812a16baeaSToby Isaac @*/
93821189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
93832a16baeaSToby Isaac {
93842a16baeaSToby Isaac   PetscErrorCode ierr;
93852a16baeaSToby Isaac 
93862a16baeaSToby Isaac   PetscFunctionBegin;
93872a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
93882a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
9389*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->computel2fielddiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
93902a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
93912a16baeaSToby Isaac   PetscFunctionReturn(0);
93922a16baeaSToby Isaac }
93932a16baeaSToby Isaac 
9394df0b854cSToby Isaac /*@C
9395502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
9396502a2867SDave May 
9397502a2867SDave May  Not Collective
9398502a2867SDave May 
9399502a2867SDave May  Input Parameter:
9400502a2867SDave May .  dm    - The DM
9401502a2867SDave May 
94020a19bb7dSprj-  Output Parameters:
94030a19bb7dSprj- +  nranks - the number of neighbours
94040a19bb7dSprj- -  ranks - the neighbors ranks
9405502a2867SDave May 
9406502a2867SDave May  Notes:
9407502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
9408502a2867SDave May 
9409502a2867SDave May  Level: beginner
9410502a2867SDave May 
9411dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
9412502a2867SDave May @*/
9413502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
9414502a2867SDave May {
9415502a2867SDave May   PetscErrorCode ierr;
9416502a2867SDave May 
9417502a2867SDave May   PetscFunctionBegin;
9418502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9419*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dm->ops->getneighbors,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
9420502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
9421502a2867SDave May   PetscFunctionReturn(0);
9422502a2867SDave May }
9423502a2867SDave May 
9424531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
9425531c7667SBarry Smith 
9426531c7667SBarry Smith /*
9427531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
9428531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
9429531c7667SBarry Smith */
9430531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
9431531c7667SBarry Smith {
9432531c7667SBarry Smith   PetscErrorCode ierr;
9433531c7667SBarry Smith 
9434531c7667SBarry Smith   PetscFunctionBegin;
9435531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9436531c7667SBarry Smith     Vec x1local;
9437531c7667SBarry Smith     DM  dm;
9438531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9439*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!dm,PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
9440531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
9441531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9442531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9443531c7667SBarry Smith     x1   = x1local;
9444531c7667SBarry Smith   }
9445531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
9446531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9447531c7667SBarry Smith     DM  dm;
9448531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9449531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
9450531c7667SBarry Smith   }
9451531c7667SBarry Smith   PetscFunctionReturn(0);
9452531c7667SBarry Smith }
9453531c7667SBarry Smith 
9454531c7667SBarry Smith /*@
9455531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
9456531c7667SBarry Smith 
9457531c7667SBarry Smith     Input Parameter:
9458531c7667SBarry Smith .    coloring - the MatFDColoring object
9459531c7667SBarry Smith 
946095452b02SPatrick Sanan     Developer Notes:
946195452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
9462531c7667SBarry Smith 
94631b266c99SBarry Smith     Level: advanced
94641b266c99SBarry Smith 
9465531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
9466531c7667SBarry Smith @*/
9467531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
9468531c7667SBarry Smith {
9469531c7667SBarry Smith   PetscFunctionBegin;
9470531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
9471531c7667SBarry Smith   PetscFunctionReturn(0);
9472531c7667SBarry Smith }
94738320bc6fSPatrick Sanan 
94748320bc6fSPatrick Sanan /*@
94758320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
94768320bc6fSPatrick Sanan 
94778320bc6fSPatrick Sanan     Collective
94788320bc6fSPatrick Sanan 
94798320bc6fSPatrick Sanan     Input Parameters:
9480a5bc1bf3SBarry Smith +    dm1 - the first DM
94818320bc6fSPatrick Sanan -    dm2 - the second DM
94828320bc6fSPatrick Sanan 
94838320bc6fSPatrick Sanan     Output Parameters:
94848320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
94858320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
94868320bc6fSPatrick Sanan 
94878320bc6fSPatrick Sanan     Notes:
94888320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
94893d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
94908320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
94913d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
94923d862458SPatrick Sanan     decomposition, but hold different data.
94938320bc6fSPatrick Sanan 
94948320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
94958320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
94968320bc6fSPatrick Sanan 
94978320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
94988320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
94998320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
95008320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
95018320bc6fSPatrick Sanan 
95028320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
95038320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
95048320bc6fSPatrick Sanan .vb
95058320bc6fSPatrick Sanan   ...
95068320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
95078320bc6fSPatrick Sanan   if (set && compatible)  {
95088320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
95098320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
95103d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
95118320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
95128320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
95138320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
95148320bc6fSPatrick Sanan       }
95158320bc6fSPatrick Sanan     }
95168320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
95178320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
95188320bc6fSPatrick Sanan   } else {
95198320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
95208320bc6fSPatrick Sanan   }
95218320bc6fSPatrick Sanan   ...
95228320bc6fSPatrick Sanan .ve
95238320bc6fSPatrick Sanan 
95248320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
95258320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
95268320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
95278320bc6fSPatrick Sanan     always check the "set" output parameter.
95288320bc6fSPatrick Sanan 
95298320bc6fSPatrick Sanan     A DM is always compatible with itself.
95308320bc6fSPatrick Sanan 
95318320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
95328320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
95338320bc6fSPatrick Sanan     incompatible.
95348320bc6fSPatrick Sanan 
95358320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
95368320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
95378320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
95388320bc6fSPatrick Sanan 
95398320bc6fSPatrick Sanan     Developer Notes:
95403d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
95413d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
9542a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
95438320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
95448320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
95453d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
95463d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
95478320bc6fSPatrick Sanan 
95488320bc6fSPatrick Sanan     Level: advanced
95498320bc6fSPatrick Sanan 
95503d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
95518320bc6fSPatrick Sanan @*/
95528320bc6fSPatrick Sanan 
9553a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
95548320bc6fSPatrick Sanan {
95558320bc6fSPatrick Sanan   PetscErrorCode ierr;
95568320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
95578320bc6fSPatrick Sanan   DMType         type,type2;
95588320bc6fSPatrick Sanan   PetscBool      sameType;
95598320bc6fSPatrick Sanan 
95608320bc6fSPatrick Sanan   PetscFunctionBegin;
9561a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
95628320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
95638320bc6fSPatrick Sanan 
95648320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
9565a5bc1bf3SBarry Smith   if (dm1 == dm2) {
95668320bc6fSPatrick Sanan     *set = PETSC_TRUE;
95678320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
95688320bc6fSPatrick Sanan     PetscFunctionReturn(0);
95698320bc6fSPatrick Sanan   }
95708320bc6fSPatrick Sanan 
95718320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
95728320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
95738320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
95748320bc6fSPatrick Sanan      determined by the implementation-specific logic */
9575ffc4695bSBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRMPI(ierr);
95768320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
95778320bc6fSPatrick Sanan     *set = PETSC_TRUE;
95788320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
95798320bc6fSPatrick Sanan     PetscFunctionReturn(0);
95808320bc6fSPatrick Sanan   }
95818320bc6fSPatrick Sanan 
95828320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
9583a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
9584a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
9585b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
95868320bc6fSPatrick Sanan   }
95878320bc6fSPatrick Sanan 
9588a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
95898320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
9590a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
95918320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
95928320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
95938320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
9594a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
95958320bc6fSPatrick Sanan   } else {
95968320bc6fSPatrick Sanan     *set = PETSC_FALSE;
95978320bc6fSPatrick Sanan   }
95988320bc6fSPatrick Sanan   PetscFunctionReturn(0);
95998320bc6fSPatrick Sanan }
9600c0f0dcc3SMatthew G. Knepley 
9601c0f0dcc3SMatthew G. Knepley /*@C
9602c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
9603c0f0dcc3SMatthew G. Knepley 
9604c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9605c0f0dcc3SMatthew G. Knepley 
9606c0f0dcc3SMatthew G. Knepley   Input Parameters:
9607c0f0dcc3SMatthew G. Knepley + DM - the DM
9608c0f0dcc3SMatthew G. Knepley . f - the monitor function
9609c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
9610c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
9611c0f0dcc3SMatthew G. Knepley 
9612c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9613c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
9614c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
9615c0f0dcc3SMatthew G. Knepley 
9616c0f0dcc3SMatthew G. Knepley   Notes:
9617c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
9618c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
9619c0f0dcc3SMatthew G. Knepley   order in which they were set.
9620c0f0dcc3SMatthew G. Knepley 
9621c0f0dcc3SMatthew G. Knepley   Fortran Notes:
9622c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
9623c0f0dcc3SMatthew G. Knepley 
9624c0f0dcc3SMatthew G. Knepley   Level: intermediate
9625c0f0dcc3SMatthew G. Knepley 
9626c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
9627c0f0dcc3SMatthew G. Knepley @*/
9628c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
9629c0f0dcc3SMatthew G. Knepley {
9630c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9631c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9632c0f0dcc3SMatthew G. Knepley 
9633c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9634c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9635c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9636c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9637c0f0dcc3SMatthew G. Knepley 
9638c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
9639c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9640c0f0dcc3SMatthew G. Knepley   }
9641*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(dm->numbermonitors >= MAXDMMONITORS,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9642c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9643c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9644c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9645c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9646c0f0dcc3SMatthew G. Knepley }
9647c0f0dcc3SMatthew G. Knepley 
9648c0f0dcc3SMatthew G. Knepley /*@
9649c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9650c0f0dcc3SMatthew G. Knepley 
9651c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9652c0f0dcc3SMatthew G. Knepley 
9653c0f0dcc3SMatthew G. Knepley   Input Parameter:
9654c0f0dcc3SMatthew G. Knepley . dm - the DM
9655c0f0dcc3SMatthew G. Knepley 
9656c0f0dcc3SMatthew G. Knepley   Options Database Key:
9657c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9658c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9659c0f0dcc3SMatthew G. Knepley   set via the options database
9660c0f0dcc3SMatthew G. Knepley 
9661c0f0dcc3SMatthew G. Knepley   Notes:
9662c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9663c0f0dcc3SMatthew G. Knepley 
9664c0f0dcc3SMatthew G. Knepley   Level: intermediate
9665c0f0dcc3SMatthew G. Knepley 
9666c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9667c0f0dcc3SMatthew G. Knepley @*/
9668c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9669c0f0dcc3SMatthew G. Knepley {
9670c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9671c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9672c0f0dcc3SMatthew G. Knepley 
9673c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9674c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9675c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9676c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
9677c0f0dcc3SMatthew G. Knepley   }
9678c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9679c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9680c0f0dcc3SMatthew G. Knepley }
9681c0f0dcc3SMatthew G. Knepley 
9682c0f0dcc3SMatthew G. Knepley /*@C
9683c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9684c0f0dcc3SMatthew G. Knepley 
9685c0f0dcc3SMatthew G. Knepley   Collective on DM
9686c0f0dcc3SMatthew G. Knepley 
9687c0f0dcc3SMatthew G. Knepley   Input Parameters:
9688c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9689c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9690c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9691c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9692c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9693c0f0dcc3SMatthew 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
9694c0f0dcc3SMatthew G. Knepley 
9695c0f0dcc3SMatthew G. Knepley   Output Parameter:
9696c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9697c0f0dcc3SMatthew G. Knepley 
9698c0f0dcc3SMatthew G. Knepley   Level: developer
9699c0f0dcc3SMatthew G. Knepley 
9700c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9701c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9702c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9703c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9704c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9705c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9706c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9707c0f0dcc3SMatthew G. Knepley @*/
9708c0f0dcc3SMatthew 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)
9709c0f0dcc3SMatthew G. Knepley {
9710c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9711c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9712c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
9713c0f0dcc3SMatthew G. Knepley 
9714c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9715c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9716c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
9717c0f0dcc3SMatthew G. Knepley   if (*flg) {
9718c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9719c0f0dcc3SMatthew G. Knepley 
9720c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
9721c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
9722c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
9723c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
9724c0f0dcc3SMatthew G. Knepley   }
9725c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9726c0f0dcc3SMatthew G. Knepley }
9727c0f0dcc3SMatthew G. Knepley 
9728c0f0dcc3SMatthew G. Knepley /*@
9729c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9730c0f0dcc3SMatthew G. Knepley 
9731c0f0dcc3SMatthew G. Knepley    Collective on DM
9732c0f0dcc3SMatthew G. Knepley 
9733c0f0dcc3SMatthew G. Knepley    Input Parameters:
9734c0f0dcc3SMatthew G. Knepley .  dm - The DM
9735c0f0dcc3SMatthew G. Knepley 
9736c0f0dcc3SMatthew G. Knepley    Level: developer
9737c0f0dcc3SMatthew G. Knepley 
9738c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9739c0f0dcc3SMatthew G. Knepley @*/
9740c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9741c0f0dcc3SMatthew G. Knepley {
9742c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9743c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9744c0f0dcc3SMatthew G. Knepley 
9745c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9746c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9747c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9748c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9749c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
9750c0f0dcc3SMatthew G. Knepley   }
9751c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9752c0f0dcc3SMatthew G. Knepley }
97532e4af2aeSMatthew G. Knepley 
97542e4af2aeSMatthew G. Knepley /*@
97552e4af2aeSMatthew G. Knepley   DMComputeError - Computes the error assuming the user has given exact solution functions
97562e4af2aeSMatthew G. Knepley 
97572e4af2aeSMatthew G. Knepley   Collective on DM
97582e4af2aeSMatthew G. Knepley 
97592e4af2aeSMatthew G. Knepley   Input Parameters:
97602e4af2aeSMatthew G. Knepley + dm     - The DM
97616b867d5aSJose E. Roman - sol    - The solution vector
97622e4af2aeSMatthew G. Knepley 
97636b867d5aSJose E. Roman   Input/Output Parameter:
97646b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
97656b867d5aSJose E. Roman            contains the error in each field
97666b867d5aSJose E. Roman 
97676b867d5aSJose E. Roman   Output Parameter:
97686b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
97692e4af2aeSMatthew G. Knepley 
97702e4af2aeSMatthew G. Knepley   Note: The exact solutions come from the PetscDS object, and the time comes from DMGetOutputSequenceNumber().
97712e4af2aeSMatthew G. Knepley 
97722e4af2aeSMatthew G. Knepley   Level: developer
97732e4af2aeSMatthew G. Knepley 
97742e4af2aeSMatthew G. Knepley .seealso: DMMonitorSet(), DMGetRegionNumDS(), PetscDSGetExactSolution(), DMGetOutputSequenceNumber()
97752e4af2aeSMatthew G. Knepley @*/
97762e4af2aeSMatthew G. Knepley PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
97772e4af2aeSMatthew G. Knepley {
97782e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
97792e4af2aeSMatthew G. Knepley   void            **ctxs;
97802e4af2aeSMatthew G. Knepley   PetscReal         time;
97812e4af2aeSMatthew G. Knepley   PetscInt          Nf, f, Nds, s;
97822e4af2aeSMatthew G. Knepley   PetscErrorCode    ierr;
97832e4af2aeSMatthew G. Knepley 
97842e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
97852e4af2aeSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
97862e4af2aeSMatthew G. Knepley   ierr = PetscCalloc2(Nf, &exactSol, Nf, &ctxs);CHKERRQ(ierr);
97872e4af2aeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
97882e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
97892e4af2aeSMatthew G. Knepley     PetscDS         ds;
97902e4af2aeSMatthew G. Knepley     DMLabel         label;
97912e4af2aeSMatthew G. Knepley     IS              fieldIS;
97922e4af2aeSMatthew G. Knepley     const PetscInt *fields;
97932e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
97942e4af2aeSMatthew G. Knepley 
97952e4af2aeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
97962e4af2aeSMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
97972e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);}
97982e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
97992e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
98002e4af2aeSMatthew G. Knepley       ierr = PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]);CHKERRQ(ierr);
98012e4af2aeSMatthew G. Knepley     }
98022e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);}
98032e4af2aeSMatthew G. Knepley   }
98042e4af2aeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
9805*2c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!exactSol[f],PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "DS must contain exact solution functions in order to calculate error, missing for field %D", f);
98062e4af2aeSMatthew G. Knepley   }
98072e4af2aeSMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
98082e4af2aeSMatthew G. Knepley   if (errors) {ierr = DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors);CHKERRQ(ierr);}
98092e4af2aeSMatthew G. Knepley   if (errorVec) {
98102e4af2aeSMatthew G. Knepley     DM             edm;
98112e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
98122e4af2aeSMatthew G. Knepley     PetscBool      simplex;
98132e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
98142e4af2aeSMatthew G. Knepley 
98152e4af2aeSMatthew G. Knepley     ierr = DMClone(dm, &edm);CHKERRQ(ierr);
98162e4af2aeSMatthew G. Knepley     ierr = DMGetDimension(edm, &dim);CHKERRQ(ierr);
98172e4af2aeSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr);
98182e4af2aeSMatthew G. Knepley     ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
98192e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
98202e4af2aeSMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
98212e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
98222e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
98232e4af2aeSMatthew G. Knepley       PetscQuadrature q;
98242e4af2aeSMatthew G. Knepley       const char     *name;
98252e4af2aeSMatthew G. Knepley 
98262e4af2aeSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
98272e4af2aeSMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe);CHKERRQ(ierr);
98282e4af2aeSMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
98292e4af2aeSMatthew G. Knepley       ierr = PetscObjectSetName((PetscObject) efe, name);CHKERRQ(ierr);
98302e4af2aeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
98312e4af2aeSMatthew G. Knepley       ierr = PetscFESetQuadrature(efe, q);CHKERRQ(ierr);
98322e4af2aeSMatthew G. Knepley       ierr = DMSetField(edm, f, NULL, (PetscObject) efe);CHKERRQ(ierr);
98332e4af2aeSMatthew G. Knepley       ierr = PetscFEDestroy(&efe);CHKERRQ(ierr);
98342e4af2aeSMatthew G. Knepley     }
98352e4af2aeSMatthew G. Knepley     ierr = DMCreateDS(edm);CHKERRQ(ierr);
98362e4af2aeSMatthew G. Knepley 
98371e1ea65dSPierre Jolivet     ierr = DMCreateGlobalVector(edm, errorVec);CHKERRQ(ierr);
98382e4af2aeSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) *errorVec, "Error");CHKERRQ(ierr);
98392e4af2aeSMatthew G. Knepley     ierr = DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec);CHKERRQ(ierr);
98402e4af2aeSMatthew G. Knepley     ierr = DMDestroy(&edm);CHKERRQ(ierr);
98412e4af2aeSMatthew G. Knepley   }
98422e4af2aeSMatthew G. Knepley   ierr = PetscFree2(exactSol, ctxs);CHKERRQ(ierr);
98432e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
98442e4af2aeSMatthew G. Knepley }
98459a2a23afSMatthew G. Knepley 
98469a2a23afSMatthew G. Knepley /*@
98479a2a23afSMatthew G. Knepley   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this DM
98489a2a23afSMatthew G. Knepley 
98499a2a23afSMatthew G. Knepley   Not collective
98509a2a23afSMatthew G. Knepley 
98519a2a23afSMatthew G. Knepley   Input Parameter:
98529a2a23afSMatthew G. Knepley . dm     - The DM
98539a2a23afSMatthew G. Knepley 
98549a2a23afSMatthew G. Knepley   Output Parameter:
9855a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
98569a2a23afSMatthew G. Knepley 
98579a2a23afSMatthew G. Knepley   Level: advanced
98589a2a23afSMatthew G. Knepley 
98599a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryLabels(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
98609a2a23afSMatthew G. Knepley @*/
98619a2a23afSMatthew G. Knepley PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
98629a2a23afSMatthew G. Knepley {
98639a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
98649a2a23afSMatthew G. Knepley 
98659a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98669a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98679a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetSize(dm->auxData, numAux);CHKERRQ(ierr);
98689a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98699a2a23afSMatthew G. Knepley }
98709a2a23afSMatthew G. Knepley 
98719a2a23afSMatthew G. Knepley /*@
98729a2a23afSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value
98739a2a23afSMatthew G. Knepley 
98749a2a23afSMatthew G. Knepley   Not collective
98759a2a23afSMatthew G. Knepley 
98769a2a23afSMatthew G. Knepley   Input Parameters:
98779a2a23afSMatthew G. Knepley + dm     - The DM
98789a2a23afSMatthew G. Knepley . label  - The DMLabel
98799a2a23afSMatthew G. Knepley - value  - The label value indicating the region
98809a2a23afSMatthew G. Knepley 
98819a2a23afSMatthew G. Knepley   Output Parameter:
98829a2a23afSMatthew G. Knepley . aux    - The Vec holding auxiliary field data
98839a2a23afSMatthew G. Knepley 
988404c51a94SMatthew G. Knepley   Note: If no auxiliary vector is found for this (label, value), (NULL, 0) is checked as well.
988504c51a94SMatthew G. Knepley 
98869a2a23afSMatthew G. Knepley   Level: advanced
98879a2a23afSMatthew G. Knepley 
98889a2a23afSMatthew G. Knepley .seealso: DMSetAuxiliaryVec(), DMGetNumAuxiliaryVec()
98899a2a23afSMatthew G. Knepley @*/
98909a2a23afSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, Vec *aux)
98919a2a23afSMatthew G. Knepley {
989204c51a94SMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0};
989304c51a94SMatthew G. Knepley   PetscBool       has;
98949a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
98959a2a23afSMatthew G. Knepley 
98969a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98979a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98989a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
98999a2a23afSMatthew G. Knepley   key.label = label;
99009a2a23afSMatthew G. Knepley   key.value = value;
990104c51a94SMatthew G. Knepley   ierr = PetscHMapAuxHas(dm->auxData, key, &has);CHKERRQ(ierr);
990204c51a94SMatthew G. Knepley   if (has) {ierr = PetscHMapAuxGet(dm->auxData, key,  aux);CHKERRQ(ierr);}
990304c51a94SMatthew G. Knepley   else     {ierr = PetscHMapAuxGet(dm->auxData, wild, aux);CHKERRQ(ierr);}
99049a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
99059a2a23afSMatthew G. Knepley }
99069a2a23afSMatthew G. Knepley 
99079a2a23afSMatthew G. Knepley /*@
99089a2a23afSMatthew G. Knepley   DMSetAuxiliaryVec - Set the auxiliary vector for region specified by the given label and value
99099a2a23afSMatthew G. Knepley 
99109a2a23afSMatthew G. Knepley   Not collective
99119a2a23afSMatthew G. Knepley 
99129a2a23afSMatthew G. Knepley   Input Parameters:
99139a2a23afSMatthew G. Knepley + dm     - The DM
99149a2a23afSMatthew G. Knepley . label  - The DMLabel
99159a2a23afSMatthew G. Knepley . value  - The label value indicating the region
99169a2a23afSMatthew G. Knepley - aux    - The Vec holding auxiliary field data
99179a2a23afSMatthew G. Knepley 
99189a2a23afSMatthew G. Knepley   Level: advanced
99199a2a23afSMatthew G. Knepley 
99209a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryVec()
99219a2a23afSMatthew G. Knepley @*/
99229a2a23afSMatthew G. Knepley PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, Vec aux)
99239a2a23afSMatthew G. Knepley {
99249a2a23afSMatthew G. Knepley   Vec             old;
99259a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
99269a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
99279a2a23afSMatthew G. Knepley 
99289a2a23afSMatthew G. Knepley   PetscFunctionBegin;
99299a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
99309a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
99319a2a23afSMatthew G. Knepley   key.label = label;
99329a2a23afSMatthew G. Knepley   key.value = value;
99339a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGet(dm->auxData, key, &old);CHKERRQ(ierr);
99349a2a23afSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) aux);CHKERRQ(ierr);
99359a2a23afSMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) old);CHKERRQ(ierr);
99369a2a23afSMatthew G. Knepley   if (!aux) {ierr = PetscHMapAuxDel(dm->auxData, key);CHKERRQ(ierr);}
99379a2a23afSMatthew G. Knepley   else      {ierr = PetscHMapAuxSet(dm->auxData, key, aux);CHKERRQ(ierr);}
99389a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
99399a2a23afSMatthew G. Knepley }
99409a2a23afSMatthew G. Knepley 
99419a2a23afSMatthew G. Knepley /*@C
99429a2a23afSMatthew G. Knepley   DMGetAuxiliaryLabels - Get the labels and values for all auxiliary vectors in this DM
99439a2a23afSMatthew G. Knepley 
99449a2a23afSMatthew G. Knepley   Not collective
99459a2a23afSMatthew G. Knepley 
99469a2a23afSMatthew G. Knepley   Input Parameter:
99479a2a23afSMatthew G. Knepley . dm      - The DM
99489a2a23afSMatthew G. Knepley 
99499a2a23afSMatthew G. Knepley   Output Parameters:
99509a2a23afSMatthew G. Knepley + labels  - The DMLabels for each Vec
99519a2a23afSMatthew G. Knepley - values  - The label values for each Vec
99529a2a23afSMatthew G. Knepley 
99539a2a23afSMatthew G. Knepley   Note: The arrays passed in must be at least as large as DMGetNumAuxiliaryVec().
99549a2a23afSMatthew G. Knepley 
99559a2a23afSMatthew G. Knepley   Level: advanced
99569a2a23afSMatthew G. Knepley 
99579a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
99589a2a23afSMatthew G. Knepley @*/
99599a2a23afSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[])
99609a2a23afSMatthew G. Knepley {
99619a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
99629a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
99639a2a23afSMatthew G. Knepley   PetscErrorCode   ierr;
99649a2a23afSMatthew G. Knepley 
99659a2a23afSMatthew G. Knepley   PetscFunctionBegin;
99669a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
99679a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
99689a2a23afSMatthew G. Knepley   PetscValidPointer(values, 3);
99699a2a23afSMatthew G. Knepley   ierr = DMGetNumAuxiliaryVec(dm, &n);CHKERRQ(ierr);
99709a2a23afSMatthew G. Knepley   ierr = PetscMalloc1(n, &keys);CHKERRQ(ierr);
99719a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetKeys(dm->auxData, &off, keys);CHKERRQ(ierr);
99729a2a23afSMatthew G. Knepley   for (i = 0; i < n; ++i) {labels[i] = keys[i].label; values[i] = keys[i].value;}
99739a2a23afSMatthew G. Knepley   ierr = PetscFree(keys);CHKERRQ(ierr);
99749a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
99759a2a23afSMatthew G. Knepley }
99769a2a23afSMatthew G. Knepley 
99779a2a23afSMatthew G. Knepley /*@
99789a2a23afSMatthew G. Knepley   DMCopyAuxiliaryVec - Copy the auxiliary data to a new DM
99799a2a23afSMatthew G. Knepley 
99809a2a23afSMatthew G. Knepley   Not collective
99819a2a23afSMatthew G. Knepley 
99829a2a23afSMatthew G. Knepley   Input Parameter:
99839a2a23afSMatthew G. Knepley . dm    - The DM
99849a2a23afSMatthew G. Knepley 
99859a2a23afSMatthew G. Knepley   Output Parameter:
99869a2a23afSMatthew G. Knepley . dmNew - The new DM, now with the same auxiliary data
99879a2a23afSMatthew G. Knepley 
99889a2a23afSMatthew G. Knepley   Level: advanced
99899a2a23afSMatthew G. Knepley 
99909a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
99919a2a23afSMatthew G. Knepley @*/
99929a2a23afSMatthew G. Knepley PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
99939a2a23afSMatthew G. Knepley {
99949a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
99959a2a23afSMatthew G. Knepley 
99969a2a23afSMatthew G. Knepley   PetscFunctionBegin;
99979a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
99989a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDestroy(&dmNew->auxData);CHKERRQ(ierr);
99999a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData);CHKERRQ(ierr);
100009a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
100019a2a23afSMatthew G. Knepley }
10002b5a892a1SMatthew G. Knepley 
10003b5a892a1SMatthew G. Knepley /*@C
10004b5a892a1SMatthew G. Knepley   DMPolytopeMatchOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
10005b5a892a1SMatthew G. Knepley 
10006b5a892a1SMatthew G. Knepley   Not collective
10007b5a892a1SMatthew G. Knepley 
10008b5a892a1SMatthew G. Knepley   Input Parameters:
10009b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10010b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
10011b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
10012b5a892a1SMatthew G. Knepley 
10013b5a892a1SMatthew G. Knepley   Output Parameters:
10014b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
10015b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
10016b5a892a1SMatthew G. Knepley 
10017b5a892a1SMatthew G. Knepley   Level: advanced
10018b5a892a1SMatthew G. Knepley 
10019b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchVertexOrientation()
10020b5a892a1SMatthew G. Knepley @*/
10021b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
10022b5a892a1SMatthew G. Knepley {
10023b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
10024b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
10025b5a892a1SMatthew G. Knepley   PetscInt       o, c;
10026b5a892a1SMatthew G. Knepley 
10027b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10028b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
10029b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
10030b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
10031b5a892a1SMatthew G. Knepley 
10032b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceCone[arr[c*2]] != targetCone[c]) break;
10033b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
10034b5a892a1SMatthew G. Knepley   }
10035b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
10036b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10037b5a892a1SMatthew G. Knepley }
10038b5a892a1SMatthew G. Knepley 
10039b5a892a1SMatthew G. Knepley /*@C
10040b5a892a1SMatthew G. Knepley   DMPolytopeGetOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
10041b5a892a1SMatthew G. Knepley 
10042b5a892a1SMatthew G. Knepley   Not collective
10043b5a892a1SMatthew G. Knepley 
10044b5a892a1SMatthew G. Knepley   Input Parameters:
10045b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10046b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
10047b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
10048b5a892a1SMatthew G. Knepley 
10049b5a892a1SMatthew G. Knepley   Output Parameters:
10050b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
10051b5a892a1SMatthew G. Knepley 
10052b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
10053b5a892a1SMatthew G. Knepley 
10054b5a892a1SMatthew G. Knepley   Level: advanced
10055b5a892a1SMatthew G. Knepley 
10056b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchOrientation(), DMPolytopeGetVertexOrientation()
10057b5a892a1SMatthew G. Knepley @*/
10058b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
10059b5a892a1SMatthew G. Knepley {
10060b5a892a1SMatthew G. Knepley   PetscBool      found;
10061b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
10062b5a892a1SMatthew G. Knepley 
10063b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10064b5a892a1SMatthew G. Knepley   ierr = DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found);CHKERRQ(ierr);
10065*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
10066b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10067b5a892a1SMatthew G. Knepley }
10068b5a892a1SMatthew G. Knepley 
10069b5a892a1SMatthew G. Knepley /*@C
10070b5a892a1SMatthew G. Knepley   DMPolytopeMatchVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
10071b5a892a1SMatthew G. Knepley 
10072b5a892a1SMatthew G. Knepley   Not collective
10073b5a892a1SMatthew G. Knepley 
10074b5a892a1SMatthew G. Knepley   Input Parameters:
10075b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10076b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
10077b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
10078b5a892a1SMatthew G. Knepley 
10079b5a892a1SMatthew G. Knepley   Output Parameters:
10080b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
10081b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
10082b5a892a1SMatthew G. Knepley 
10083b5a892a1SMatthew G. Knepley   Level: advanced
10084b5a892a1SMatthew G. Knepley 
10085b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchOrientation()
10086b5a892a1SMatthew G. Knepley @*/
10087b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
10088b5a892a1SMatthew G. Knepley {
10089b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
10090b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
10091b5a892a1SMatthew G. Knepley   PetscInt       o, c;
10092b5a892a1SMatthew G. Knepley 
10093b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10094b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
10095b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
10096b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
10097b5a892a1SMatthew G. Knepley 
10098b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceVert[arr[c]] != targetVert[c]) break;
10099b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
10100b5a892a1SMatthew G. Knepley   }
10101b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
10102b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10103b5a892a1SMatthew G. Knepley }
10104b5a892a1SMatthew G. Knepley 
10105b5a892a1SMatthew G. Knepley /*@C
10106b5a892a1SMatthew G. Knepley   DMPolytopeGetVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
10107b5a892a1SMatthew G. Knepley 
10108b5a892a1SMatthew G. Knepley   Not collective
10109b5a892a1SMatthew G. Knepley 
10110b5a892a1SMatthew G. Knepley   Input Parameters:
10111b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10112b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
10113b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
10114b5a892a1SMatthew G. Knepley 
10115b5a892a1SMatthew G. Knepley   Output Parameters:
10116b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
10117b5a892a1SMatthew G. Knepley 
10118b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
10119b5a892a1SMatthew G. Knepley 
10120b5a892a1SMatthew G. Knepley   Level: advanced
10121b5a892a1SMatthew G. Knepley 
10122b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchVertexOrientation(), DMPolytopeGetOrientation()
10123b5a892a1SMatthew G. Knepley @*/
10124b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
10125b5a892a1SMatthew G. Knepley {
10126b5a892a1SMatthew G. Knepley   PetscBool      found;
10127b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
10128b5a892a1SMatthew G. Knepley 
10129b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10130b5a892a1SMatthew G. Knepley   ierr = DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found);CHKERRQ(ierr);
10131*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
10132b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10133b5a892a1SMatthew G. Knepley }
10134012bc364SMatthew G. Knepley 
10135012bc364SMatthew G. Knepley /*@C
10136012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
10137012bc364SMatthew G. Knepley 
10138012bc364SMatthew G. Knepley   Not collective
10139012bc364SMatthew G. Knepley 
10140012bc364SMatthew G. Knepley   Input Parameters:
10141012bc364SMatthew G. Knepley + ct    - The DMPolytopeType
10142012bc364SMatthew G. Knepley - point - Coordinates of the point
10143012bc364SMatthew G. Knepley 
10144012bc364SMatthew G. Knepley   Output Parameters:
10145012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
10146012bc364SMatthew G. Knepley 
10147012bc364SMatthew G. Knepley   Level: advanced
10148012bc364SMatthew G. Knepley 
10149012bc364SMatthew G. Knepley .seealso: DMLocatePoints()
10150012bc364SMatthew G. Knepley @*/
10151012bc364SMatthew G. Knepley PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
10152012bc364SMatthew G. Knepley {
10153012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
10154012bc364SMatthew G. Knepley   PetscInt  d;
10155012bc364SMatthew G. Knepley 
10156012bc364SMatthew G. Knepley   PetscFunctionBegin;
10157012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
10158012bc364SMatthew G. Knepley   switch (ct) {
10159012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
10160012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
10161012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
10162012bc364SMatthew G. Knepley       if (point[d] < -1.0) {*inside = PETSC_FALSE; break;}
10163012bc364SMatthew G. Knepley       sum += point[d];
10164012bc364SMatthew G. Knepley     }
10165012bc364SMatthew G. Knepley     if (sum > PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10166012bc364SMatthew G. Knepley     break;
10167012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
10168012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
10169012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
10170012bc364SMatthew G. Knepley       if (PetscAbsReal(point[d]) > 1.+PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10171012bc364SMatthew G. Knepley     break;
10172012bc364SMatthew G. Knepley   default:
1017398921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
10174012bc364SMatthew G. Knepley   }
10175012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
10176012bc364SMatthew G. Knepley }
10177