xref: /petsc/src/dm/interface/dm.c (revision b1538989ce3467600cc0aac7ce74e2ea248c7f12)
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;
6372348bcf4SPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
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;
646f490541aSPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
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;
696aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(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
785f918ec44SMatthew G. Knepley   ierr = CeedElemRestrictionDestroy(&(*dm)->ceedERestrict);CHKERRQ(ierr);
786f918ec44SMatthew G. Knepley   ierr = CeedDestroy(&(*dm)->ceed);CHKERRQ(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
8389318fe57SMatthew G. Knepley . -dm_plex_shape <shape>            - The domain shape, such as DM_SHAPE_BOX, DM_SHAPE_SPHERE, etc.
8399318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8409318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8419318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
8429318fe57SMatthew G. Knepley . -dm_plex_simplex <bool>           - PETSC_TRUE for simplex elements, PETSC_FALSE for tensor elements
8439318fe57SMatthew G. Knepley . -dm_plex_interpolate <bool>       - PETSC_TRUE turns on topological interpolation (creating edges and faces)
8449318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8459318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8469318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8479318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
8489318fe57SMatthew G. Knepley . -dm_plex_box_bd <bx,by,bz>        - Specify the DMBoundaryType for each direction
8499318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8509318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8519318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8529318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
8539318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8549318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8559318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8569318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
857d410b0cfSMatthew G. Knepley . -dm_extrude <l>                   - Acticate extrusion and apecify the number of layers to extrude
858d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
859d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
860d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
861d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
862d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
863909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
864909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8659318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8669318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8679318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8689318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8699318fe57SMatthew G. Knepley 
870384a6580SVaclav Hapla     DMPLEX Specific Checks
871384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
872384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
873384a6580SVaclav 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()
874384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
875384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
876384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
877384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
878d7bf68aeSBarry Smith 
87995eb5ee5SVaclav Hapla     Level: intermediate
88095eb5ee5SVaclav Hapla 
88195eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
88295eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
883d7bf68aeSBarry Smith 
884d7bf68aeSBarry Smith @*/
8857087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
886d7bf68aeSBarry Smith {
8877781c08eSBarry Smith   char           typeName[256];
888ca266f36SBarry Smith   PetscBool      flg;
889d7bf68aeSBarry Smith   PetscErrorCode ierr;
890d7bf68aeSBarry Smith 
891d7bf68aeSBarry Smith   PetscFunctionBegin;
892171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
89349be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
89449be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8951bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8963194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8970298fd71SBarry 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);
898a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
899f9ba7244SBarry Smith   if (flg) {
900f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
901f9ba7244SBarry Smith   }
902a264d7a6SBarry 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);
903073dac72SJed Brown   if (flg) {
904521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
905073dac72SJed Brown   }
9068f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
907a4ea9b21SRichard 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);
908f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
909e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
910f9ba7244SBarry Smith   }
911f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
9120633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
91382fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
914d7bf68aeSBarry Smith   PetscFunctionReturn(0);
915d7bf68aeSBarry Smith }
916d7bf68aeSBarry Smith 
917fc9bc008SSatish Balay /*@C
918fe2efc57SMark    DMViewFromOptions - View from Options
919fe2efc57SMark 
920fe2efc57SMark    Collective on DM
921fe2efc57SMark 
922fe2efc57SMark    Input Parameters:
923fe2efc57SMark +  dm - the DM object
924736c3998SJose E. Roman .  obj - Optional object
925736c3998SJose E. Roman -  name - command line option
926fe2efc57SMark 
927fe2efc57SMark    Level: intermediate
928fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
929fe2efc57SMark @*/
930fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
931fe2efc57SMark {
932fe2efc57SMark   PetscErrorCode ierr;
933fe2efc57SMark 
934fe2efc57SMark   PetscFunctionBegin;
935fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
936fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
937fe2efc57SMark   PetscFunctionReturn(0);
938fe2efc57SMark }
939fe2efc57SMark 
940fe2efc57SMark /*@C
941224748a4SBarry Smith     DMView - Views a DM
94247c6ae99SBarry Smith 
943d083f849SBarry Smith     Collective on dm
94447c6ae99SBarry Smith 
945d8d19677SJose E. Roman     Input Parameters:
94647c6ae99SBarry Smith +   dm - the DM object to view
94747c6ae99SBarry Smith -   v - the viewer
94847c6ae99SBarry Smith 
949224748a4SBarry Smith     Level: beginner
95047c6ae99SBarry Smith 
951e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
95247c6ae99SBarry Smith 
95347c6ae99SBarry Smith @*/
9547087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
95547c6ae99SBarry Smith {
95647c6ae99SBarry Smith   PetscErrorCode    ierr;
95732c0f0efSBarry Smith   PetscBool         isbinary;
95876a8abe0SBarry Smith   PetscMPIInt       size;
95976a8abe0SBarry Smith   PetscViewerFormat format;
96047c6ae99SBarry Smith 
96147c6ae99SBarry Smith   PetscFunctionBegin;
962171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9633014e516SBarry Smith   if (!v) {
964ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9653014e516SBarry Smith   }
966b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
96774903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
96874903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
96974903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97074903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97174903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
97274903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
97374903a4fSStefano Zampini      in an error here */
97474903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9758bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
976b1b135c8SBarry Smith 
97776a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
978ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRMPI(ierr);
97976a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
98098c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
98132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
98232c0f0efSBarry Smith   if (isbinary) {
98355849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
98432c0f0efSBarry Smith     char     type[256];
98532c0f0efSBarry Smith 
986f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
98732c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
988f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
98932c0f0efSBarry Smith   }
9900c010503SBarry Smith   if (dm->ops->view) {
9910c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
99247c6ae99SBarry Smith   }
99347c6ae99SBarry Smith   PetscFunctionReturn(0);
99447c6ae99SBarry Smith }
99547c6ae99SBarry Smith 
99647c6ae99SBarry Smith /*@
9978472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
99847c6ae99SBarry Smith 
999d083f849SBarry Smith     Collective on dm
100047c6ae99SBarry Smith 
100147c6ae99SBarry Smith     Input Parameter:
100247c6ae99SBarry Smith .   dm - the DM object
100347c6ae99SBarry Smith 
100447c6ae99SBarry Smith     Output Parameter:
100547c6ae99SBarry Smith .   vec - the global vector
100647c6ae99SBarry Smith 
1007073dac72SJed Brown     Level: beginner
100847c6ae99SBarry Smith 
1009ec075b9fSPatrick Sanan .seealso DMCreateLocalVector(), DMGetGlobalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith @*/
10127087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
101347c6ae99SBarry Smith {
101447c6ae99SBarry Smith   PetscErrorCode ierr;
101547c6ae99SBarry Smith 
101647c6ae99SBarry Smith   PetscFunctionBegin;
1017171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1018b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1019b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
102047c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
102176bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1022c6b011d8SStefano Zampini     DM vdm;
1023c6b011d8SStefano Zampini 
1024c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1025c6b011d8SStefano Zampini     if (!vdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the vector\n",((PetscObject)dm)->type_name);
1026c6b011d8SStefano Zampini   }
102747c6ae99SBarry Smith   PetscFunctionReturn(0);
102847c6ae99SBarry Smith }
102947c6ae99SBarry Smith 
103047c6ae99SBarry Smith /*@
10318472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
103247c6ae99SBarry Smith 
103347c6ae99SBarry Smith     Not Collective
103447c6ae99SBarry Smith 
103547c6ae99SBarry Smith     Input Parameter:
103647c6ae99SBarry Smith .   dm - the DM object
103747c6ae99SBarry Smith 
103847c6ae99SBarry Smith     Output Parameter:
103947c6ae99SBarry Smith .   vec - the local vector
104047c6ae99SBarry Smith 
1041073dac72SJed Brown     Level: beginner
104247c6ae99SBarry Smith 
1043ec075b9fSPatrick Sanan .seealso DMCreateGlobalVector(), DMGetLocalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
104447c6ae99SBarry Smith 
104547c6ae99SBarry Smith @*/
10467087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
104747c6ae99SBarry Smith {
104847c6ae99SBarry Smith   PetscErrorCode ierr;
104947c6ae99SBarry Smith 
105047c6ae99SBarry Smith   PetscFunctionBegin;
1051171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1052b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1053b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
105447c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
105576bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1056c6b011d8SStefano Zampini     DM vdm;
1057c6b011d8SStefano Zampini 
1058c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1059c6b011d8SStefano Zampini     if (!vdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"DM type '%s' did not attach the DM to the vector\n",((PetscObject)dm)->type_name);
1060c6b011d8SStefano Zampini   }
106147c6ae99SBarry Smith   PetscFunctionReturn(0);
106247c6ae99SBarry Smith }
106347c6ae99SBarry Smith 
10641411c6eeSJed Brown /*@
10651411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10661411c6eeSJed Brown 
1067d083f849SBarry Smith    Collective on dm
10681411c6eeSJed Brown 
10691411c6eeSJed Brown    Input Parameter:
10701411c6eeSJed Brown .  dm - the DM that provides the mapping
10711411c6eeSJed Brown 
10721411c6eeSJed Brown    Output Parameter:
10731411c6eeSJed Brown .  ltog - the mapping
10741411c6eeSJed Brown 
10751411c6eeSJed Brown    Level: intermediate
10761411c6eeSJed Brown 
10771411c6eeSJed Brown    Notes:
10781411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10791411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10801411c6eeSJed Brown 
1081fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10821411c6eeSJed Brown @*/
10837087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10841411c6eeSJed Brown {
10850be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10861411c6eeSJed Brown   PetscErrorCode ierr;
10871411c6eeSJed Brown 
10881411c6eeSJed Brown   PetscFunctionBegin;
10891411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10901411c6eeSJed Brown   PetscValidPointer(ltog,2);
10911411c6eeSJed Brown   if (!dm->ltogmap) {
109237d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
109337d0c07bSMatthew G Knepley 
109492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
109537d0c07bSMatthew G Knepley     if (section) {
1096a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
109737d0c07bSMatthew G Knepley       PetscInt       *ltog;
1098ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
109937d0c07bSMatthew G Knepley 
1100e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
110137d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1102ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1103ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
110437d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1105a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
110637d0c07bSMatthew G Knepley 
110737d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
110837d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1109a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1110a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
111137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
11121a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11131a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
11141a7dc684SMatthew G. Knepley         if (dof) {
1115b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1116b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
11171a7dc684SMatthew G. Knepley         }
111837d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1119a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1120a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
112137d0c07bSMatthew G Knepley         }
112237d0c07bSMatthew G Knepley       }
1123bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11240be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11250be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
11260be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11270be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
11287591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11297591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1130ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1131ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1132ccf3bd66SMatthew G. Knepley         n /= bs;
1133ccf3bd66SMatthew G. Knepley       }
1134ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
11353bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
113637d0c07bSMatthew G Knepley     } else {
1137b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1138184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
11391411c6eeSJed Brown     }
114037d0c07bSMatthew G Knepley   }
11411411c6eeSJed Brown   *ltog = dm->ltogmap;
11421411c6eeSJed Brown   PetscFunctionReturn(0);
11431411c6eeSJed Brown }
11441411c6eeSJed Brown 
11451411c6eeSJed Brown /*@
11461411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
11471411c6eeSJed Brown 
11481411c6eeSJed Brown    Not Collective
11491411c6eeSJed Brown 
11501411c6eeSJed Brown    Input Parameter:
11511411c6eeSJed Brown .  dm - the DM with block structure
11521411c6eeSJed Brown 
11531411c6eeSJed Brown    Output Parameter:
11541411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11551411c6eeSJed Brown 
11561411c6eeSJed Brown    Level: intermediate
11571411c6eeSJed Brown 
1158fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
11591411c6eeSJed Brown @*/
11607087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
11611411c6eeSJed Brown {
11621411c6eeSJed Brown   PetscFunctionBegin;
11631411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1164534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11651411c6eeSJed Brown   if (dm->bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
11661411c6eeSJed Brown   *bs = dm->bs;
11671411c6eeSJed Brown   PetscFunctionReturn(0);
11681411c6eeSJed Brown }
11691411c6eeSJed Brown 
117048eeb7c8SBarry Smith /*@C
11718472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
117247c6ae99SBarry Smith 
1173a5bc1bf3SBarry Smith     Collective on dmc
117447c6ae99SBarry Smith 
1175d8d19677SJose E. Roman     Input Parameters:
1176a5bc1bf3SBarry Smith +   dmc - the DM object
1177a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
117847c6ae99SBarry Smith 
1179d8d19677SJose E. Roman     Output Parameters:
118047c6ae99SBarry Smith +  mat - the interpolation
118147c6ae99SBarry Smith -  vec - the scaling (optional)
118247c6ae99SBarry Smith 
118347c6ae99SBarry Smith     Level: developer
118447c6ae99SBarry Smith 
118595452b02SPatrick Sanan     Notes:
118695452b02SPatrick 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
118785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1188d52bd9f3SBarry Smith 
11891f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1190d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
119185afcc9aSBarry Smith 
11922ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
119347c6ae99SBarry Smith 
119447c6ae99SBarry Smith @*/
1195a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
119647c6ae99SBarry Smith {
119747c6ae99SBarry Smith   PetscErrorCode ierr;
119847c6ae99SBarry Smith 
119947c6ae99SBarry Smith   PetscFunctionBegin;
1200a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1201a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1202c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1203a5bc1bf3SBarry Smith   if (!dmc->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1204a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1205a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1206a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
120747c6ae99SBarry Smith   PetscFunctionReturn(0);
120847c6ae99SBarry Smith }
120947c6ae99SBarry Smith 
12103ad4599aSBarry Smith /*@
1211d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
12122ed6491fSPatrick Sanan 
12132ed6491fSPatrick Sanan   Input Parameters:
12142ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
12152ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
12162ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12172ed6491fSPatrick Sanan 
12182ed6491fSPatrick Sanan   Output Parameter:
12192ed6491fSPatrick Sanan .    scale - the scaled vector
12202ed6491fSPatrick Sanan 
12212ed6491fSPatrick Sanan   Level: developer
12222ed6491fSPatrick Sanan 
12232ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
12242ed6491fSPatrick Sanan 
12252ed6491fSPatrick Sanan @*/
12262ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
12272ed6491fSPatrick Sanan {
12282ed6491fSPatrick Sanan   PetscErrorCode ierr;
12292ed6491fSPatrick Sanan   Vec            fine;
12302ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
12319704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12329704db99SRichard Tran Mills   PetscBool      flg;
12339704db99SRichard Tran Mills #endif
12342ed6491fSPatrick Sanan 
12352ed6491fSPatrick Sanan   PetscFunctionBegin;
12362ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
12372ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
12382ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
12399704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12409704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12419704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12429704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
124383aa49f4SRichard Tran Mills   if (fine->bindingpropagates) {
12449704db99SRichard Tran Mills     ierr = VecBoundToCPU(fine,&flg);CHKERRQ(ierr);
1245*b1538989SRichard Tran Mills     ierr = MatSetBindingPropagates(mat,PETSC_TRUE);CHKERRQ(ierr);
124683aa49f4SRichard Tran Mills     ierr = MatBindToCPU(mat,flg);CHKERRQ(ierr);
124783aa49f4SRichard Tran Mills   }
12489704db99SRichard Tran Mills #endif
12492ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
12502ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
12512ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
12522ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12532ed6491fSPatrick Sanan }
12542ed6491fSPatrick Sanan 
12552ed6491fSPatrick Sanan /*@
12563ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
12573ad4599aSBarry Smith 
1258a5bc1bf3SBarry Smith     Collective on dmc
12593ad4599aSBarry Smith 
1260d8d19677SJose E. Roman     Input Parameters:
1261a5bc1bf3SBarry Smith +   dmc - the DM object
1262a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
12633ad4599aSBarry Smith 
12643ad4599aSBarry Smith     Output Parameter:
12653ad4599aSBarry Smith .  mat - the restriction
12663ad4599aSBarry Smith 
12673ad4599aSBarry Smith     Level: developer
12683ad4599aSBarry Smith 
126995452b02SPatrick Sanan     Notes:
127095452b02SPatrick 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
12713ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
12723ad4599aSBarry Smith 
12733ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
12743ad4599aSBarry Smith 
12753ad4599aSBarry Smith @*/
1276a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12773ad4599aSBarry Smith {
12783ad4599aSBarry Smith   PetscErrorCode ierr;
12793ad4599aSBarry Smith 
12803ad4599aSBarry Smith   PetscFunctionBegin;
1281a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1282a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12835a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1284a5bc1bf3SBarry Smith   if (!dmc->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1285a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1286a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1287a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
12883ad4599aSBarry Smith   PetscFunctionReturn(0);
12893ad4599aSBarry Smith }
12903ad4599aSBarry Smith 
129147c6ae99SBarry Smith /*@
12928472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
129347c6ae99SBarry Smith 
1294a5bc1bf3SBarry Smith     Collective on dac
129547c6ae99SBarry Smith 
1296d8d19677SJose E. Roman     Input Parameters:
1297a5bc1bf3SBarry Smith +   dac - the DM object
1298a5bc1bf3SBarry Smith -   daf - the second, finer DM object
129947c6ae99SBarry Smith 
130047c6ae99SBarry Smith     Output Parameter:
13016dbf9973SLawrence Mitchell .   mat - the injection
130247c6ae99SBarry Smith 
130347c6ae99SBarry Smith     Level: developer
130447c6ae99SBarry Smith 
130595452b02SPatrick Sanan    Notes:
130695452b02SPatrick 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
130785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
130885afcc9aSBarry Smith 
1309e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
131047c6ae99SBarry Smith 
131147c6ae99SBarry Smith @*/
1312a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
131347c6ae99SBarry Smith {
131447c6ae99SBarry Smith   PetscErrorCode ierr;
131547c6ae99SBarry Smith 
131647c6ae99SBarry Smith   PetscFunctionBegin;
1317a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1318a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
13195a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1320a5bc1bf3SBarry Smith   if (!dac->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1321a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1322a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1323a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
132447c6ae99SBarry Smith   PetscFunctionReturn(0);
132547c6ae99SBarry Smith }
132647c6ae99SBarry Smith 
1327b412c318SBarry Smith /*@
1328bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1329bd041c0cSMatthew G. Knepley 
1330a5bc1bf3SBarry Smith   Collective on dac
1331bd041c0cSMatthew G. Knepley 
1332d8d19677SJose E. Roman   Input Parameters:
1333a5bc1bf3SBarry Smith + dac - the DM object
1334a5bc1bf3SBarry Smith - daf - the second, finer DM object
1335bd041c0cSMatthew G. Knepley 
1336bd041c0cSMatthew G. Knepley   Output Parameter:
1337bd041c0cSMatthew G. Knepley . mat - the interpolation
1338bd041c0cSMatthew G. Knepley 
1339bd041c0cSMatthew G. Knepley   Level: developer
1340bd041c0cSMatthew G. Knepley 
1341bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1342bd041c0cSMatthew G. Knepley @*/
1343a5bc1bf3SBarry Smith PetscErrorCode DMCreateMassMatrix(DM dac, DM daf, Mat *mat)
1344bd041c0cSMatthew G. Knepley {
1345bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1346bd041c0cSMatthew G. Knepley 
1347bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1348a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1349a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13505a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1351a5bc1bf3SBarry Smith   if (!dac->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dac)->type_name);
1352a5bc1bf3SBarry Smith   ierr = (*dac->ops->createmassmatrix)(dac, daf, mat);CHKERRQ(ierr);
1353bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1354bd041c0cSMatthew G. Knepley }
1355bd041c0cSMatthew G. Knepley 
1356bd041c0cSMatthew G. Knepley /*@
1357b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
135847c6ae99SBarry Smith 
1359d083f849SBarry Smith     Collective on dm
136047c6ae99SBarry Smith 
1361d8d19677SJose E. Roman     Input Parameters:
136247c6ae99SBarry Smith +   dm - the DM object
13635bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
136447c6ae99SBarry Smith 
136547c6ae99SBarry Smith     Output Parameter:
136647c6ae99SBarry Smith .   coloring - the coloring
136747c6ae99SBarry Smith 
1368ec5066bdSBarry Smith     Notes:
1369ec5066bdSBarry 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
1370ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1371ec5066bdSBarry Smith 
1372ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1373ec5066bdSBarry Smith 
137447c6ae99SBarry Smith     Level: developer
137547c6ae99SBarry Smith 
1376ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
137747c6ae99SBarry Smith 
1378aab9d709SJed Brown @*/
1379b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
138047c6ae99SBarry Smith {
138147c6ae99SBarry Smith   PetscErrorCode ierr;
138247c6ae99SBarry Smith 
138347c6ae99SBarry Smith   PetscFunctionBegin;
1384171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13855a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1386b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1387b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
138847c6ae99SBarry Smith   PetscFunctionReturn(0);
138947c6ae99SBarry Smith }
139047c6ae99SBarry Smith 
1391b412c318SBarry Smith /*@
13928472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
139347c6ae99SBarry Smith 
1394d083f849SBarry Smith     Collective on dm
139547c6ae99SBarry Smith 
139647c6ae99SBarry Smith     Input Parameter:
1397b412c318SBarry Smith .   dm - the DM object
139847c6ae99SBarry Smith 
139947c6ae99SBarry Smith     Output Parameter:
140047c6ae99SBarry Smith .   mat - the empty Jacobian
140147c6ae99SBarry Smith 
1402073dac72SJed Brown     Level: beginner
140347c6ae99SBarry Smith 
1404f27dd7c6SMatthew G. Knepley     Options Database Keys:
1405f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1406f27dd7c6SMatthew G. Knepley 
140795452b02SPatrick Sanan     Notes:
140895452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
140994013140SBarry Smith        do not need to do it yourself.
141094013140SBarry Smith 
141194013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
14127889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
141394013140SBarry Smith 
141494013140SBarry 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
141594013140SBarry Smith        internally by PETSc.
141694013140SBarry Smith 
141794013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1418aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
141994013140SBarry Smith 
1420b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
142147c6ae99SBarry Smith 
1422aab9d709SJed Brown @*/
1423b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
142447c6ae99SBarry Smith {
142547c6ae99SBarry Smith   PetscErrorCode ierr;
142647c6ae99SBarry Smith 
142747c6ae99SBarry Smith   PetscFunctionBegin;
1428171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1429064a246eSJacob Faibussowitsch   PetscValidPointer(mat,2);
1430b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
14315a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1432fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1433b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
143476bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1435c6b011d8SStefano Zampini     DM mdm;
1436c6b011d8SStefano Zampini 
1437c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1438c6b011d8SStefano Zampini     if (!mdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the matrix\n",((PetscObject)dm)->type_name);
1439c6b011d8SStefano Zampini   }
1440e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1441e5e52638SMatthew G. Knepley   if (dm->Nf) {
1442e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1443649ef022SMatthew Knepley     PetscInt     Nf, f;
1444e571a35bSMatthew G. Knepley 
1445e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1446649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1447649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
1448649ef022SMatthew Knepley         ierr = (*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1449e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1450e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1451649ef022SMatthew Knepley         break;
1452e571a35bSMatthew G. Knepley       }
1453649ef022SMatthew Knepley     }
1454649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1455649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
1456649ef022SMatthew Knepley         ierr = (*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1457e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1458e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1459e571a35bSMatthew G. Knepley       }
1460e571a35bSMatthew G. Knepley     }
1461e571a35bSMatthew G. Knepley   }
1462fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
146347c6ae99SBarry Smith   PetscFunctionReturn(0);
146447c6ae99SBarry Smith }
146547c6ae99SBarry Smith 
1466732e2eb9SMatthew G Knepley /*@
1467950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1468732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1469732e2eb9SMatthew G Knepley 
1470d083f849SBarry Smith   Logically Collective on dm
1471732e2eb9SMatthew G Knepley 
1472d8d19677SJose E. Roman   Input Parameters:
1473732e2eb9SMatthew G Knepley + dm - the DM
1474732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1475732e2eb9SMatthew G Knepley 
1476732e2eb9SMatthew G Knepley   Level: developer
1477f27dd7c6SMatthew G. Knepley 
1478f27dd7c6SMatthew G. Knepley   Options Database Keys:
1479f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1480f27dd7c6SMatthew G. Knepley 
1481b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1482732e2eb9SMatthew G Knepley @*/
1483732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1484732e2eb9SMatthew G Knepley {
1485732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1486732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1487732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1488732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1489732e2eb9SMatthew G Knepley }
1490732e2eb9SMatthew G Knepley 
1491b06ff27eSHong Zhang /*@
1492b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1493b06ff27eSHong Zhang     but the array for values will not be allocated.
1494b06ff27eSHong Zhang 
1495d083f849SBarry Smith   Logically Collective on dm
1496b06ff27eSHong Zhang 
1497d8d19677SJose E. Roman   Input Parameters:
1498b06ff27eSHong Zhang + dm - the DM
1499b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1500b06ff27eSHong Zhang 
1501b06ff27eSHong Zhang   Level: developer
1502b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1503b06ff27eSHong Zhang @*/
1504b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1505b06ff27eSHong Zhang {
1506b06ff27eSHong Zhang   PetscFunctionBegin;
1507b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1508b06ff27eSHong Zhang   dm->structure_only = only;
1509b06ff27eSHong Zhang   PetscFunctionReturn(0);
1510b06ff27eSHong Zhang }
1511b06ff27eSHong Zhang 
1512a89ea682SMatthew G Knepley /*@C
1513aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1514a89ea682SMatthew G Knepley 
1515a89ea682SMatthew G Knepley   Not Collective
1516a89ea682SMatthew G Knepley 
1517a89ea682SMatthew G Knepley   Input Parameters:
1518a89ea682SMatthew G Knepley + dm - the DM object
1519a5b23f4aSJose E. Roman . count - The minimum size
152069291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1521a89ea682SMatthew G Knepley 
1522a89ea682SMatthew G Knepley   Output Parameter:
1523a89ea682SMatthew G Knepley . array - the work array
1524a89ea682SMatthew G Knepley 
1525a89ea682SMatthew G Knepley   Level: developer
1526a89ea682SMatthew G Knepley 
1527a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1528a89ea682SMatthew G Knepley @*/
152969291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1530a89ea682SMatthew G Knepley {
1531a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1532aa1993deSMatthew G Knepley   DMWorkLink     link;
153369291d52SBarry Smith   PetscMPIInt    dsize;
1534a89ea682SMatthew G Knepley 
1535a89ea682SMatthew G Knepley   PetscFunctionBegin;
1536a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1537aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1538aa1993deSMatthew G Knepley   if (dm->workin) {
1539aa1993deSMatthew G Knepley     link       = dm->workin;
1540aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1541aa1993deSMatthew G Knepley   } else {
1542b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1543a89ea682SMatthew G Knepley   }
1544ffc4695bSBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRMPI(ierr);
15455056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1546aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1547854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1548854ce69bSBarry Smith     link->bytes = dsize*count;
1549aa1993deSMatthew G Knepley   }
1550aa1993deSMatthew G Knepley   link->next   = dm->workout;
1551aa1993deSMatthew G Knepley   dm->workout  = link;
155200d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
155300d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
155400d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
155500d952a4SJed Brown #endif
1556aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1557a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1558a89ea682SMatthew G Knepley }
1559a89ea682SMatthew G Knepley 
1560aa1993deSMatthew G Knepley /*@C
1561aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1562aa1993deSMatthew G Knepley 
1563aa1993deSMatthew G Knepley   Not Collective
1564aa1993deSMatthew G Knepley 
1565aa1993deSMatthew G Knepley   Input Parameters:
1566aa1993deSMatthew G Knepley + dm - the DM object
1567a5b23f4aSJose E. Roman . count - The minimum size
156869291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1569aa1993deSMatthew G Knepley 
1570aa1993deSMatthew G Knepley   Output Parameter:
1571aa1993deSMatthew G Knepley . array - the work array
1572aa1993deSMatthew G Knepley 
1573aa1993deSMatthew G Knepley   Level: developer
1574aa1993deSMatthew G Knepley 
157595452b02SPatrick Sanan   Developer Notes:
157695452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1577aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1578aa1993deSMatthew G Knepley @*/
157969291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1580aa1993deSMatthew G Knepley {
1581aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1582aa1993deSMatthew G Knepley 
1583aa1993deSMatthew G Knepley   PetscFunctionBegin;
1584aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1585aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1586aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1587aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1588aa1993deSMatthew G Knepley       *p           = link->next;
1589aa1993deSMatthew G Knepley       link->next   = dm->workin;
1590aa1993deSMatthew G Knepley       dm->workin   = link;
15910298fd71SBarry Smith       *(void**)mem = NULL;
1592aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1593aa1993deSMatthew G Knepley     }
1594aa1993deSMatthew G Knepley   }
1595aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1596aa1993deSMatthew G Knepley }
1597e7c4fc90SDmitry Karpeev 
15988cda7954SMatthew G. Knepley /*@C
15998cda7954SMatthew G. Knepley   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field
16008cda7954SMatthew G. Knepley 
16018cda7954SMatthew G. Knepley   Logically collective on DM
16028cda7954SMatthew G. Knepley 
16038cda7954SMatthew G. Knepley   Input Parameters:
16048cda7954SMatthew G. Knepley + dm     - The DM
16058cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16068cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
16078cda7954SMatthew G. Knepley 
16088cda7954SMatthew G. Knepley   Notes:
16098cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16108cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16118cda7954SMatthew G. Knepley $ dm        - The present DM
16128cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16138cda7954SMatthew G. Knepley $ field     - The field number in dm
16148cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16158cda7954SMatthew G. Knepley 
16168cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16178cda7954SMatthew G. Knepley 
16188cda7954SMatthew G. Knepley .seealso: DMGetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16198cda7954SMatthew G. Knepley */
16208cda7954SMatthew G. Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1621435a35e8SMatthew G Knepley {
1622435a35e8SMatthew G Knepley   PetscFunctionBegin;
1623435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
162482f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1625435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1626435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1627435a35e8SMatthew G Knepley }
1628435a35e8SMatthew G Knepley 
16298cda7954SMatthew G. Knepley /*@C
16308cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
16318cda7954SMatthew G. Knepley 
16328cda7954SMatthew G. Knepley   Not collective
16338cda7954SMatthew G. Knepley 
16348cda7954SMatthew G. Knepley   Input Parameters:
16358cda7954SMatthew G. Knepley + dm     - The DM
16368cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16378cda7954SMatthew G. Knepley 
16388cda7954SMatthew G. Knepley   Output Parameter:
16398cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
16408cda7954SMatthew G. Knepley 
16418cda7954SMatthew G. Knepley   Notes:
16428cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16438cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16448cda7954SMatthew G. Knepley $ dm        - The present DM
16458cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16468cda7954SMatthew G. Knepley $ field     - The field number in dm
16478cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16488cda7954SMatthew G. Knepley 
16498cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16508cda7954SMatthew G. Knepley 
16518cda7954SMatthew G. Knepley .seealso: DMSetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16528cda7954SMatthew G. Knepley */
16538cda7954SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
16540a50eb56SMatthew G. Knepley {
16550a50eb56SMatthew G. Knepley   PetscFunctionBegin;
16560a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1657f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
16580a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
16590a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
16600a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
16610a50eb56SMatthew G. Knepley }
16620a50eb56SMatthew G. Knepley 
16638cda7954SMatthew G. Knepley /*@C
16648cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
16658cda7954SMatthew G. Knepley 
16668cda7954SMatthew G. Knepley   Logically collective on DM
16678cda7954SMatthew G. Knepley 
16688cda7954SMatthew G. Knepley   Input Parameters:
16698cda7954SMatthew G. Knepley + dm     - The DM
16708cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16718cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
16728cda7954SMatthew G. Knepley 
16738cda7954SMatthew G. Knepley   Notes:
16748cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16758cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16768cda7954SMatthew G. Knepley $ dm        - The present DM
16778cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16788cda7954SMatthew G. Knepley $ field     - The field number in dm
16798cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16808cda7954SMatthew G. Knepley 
16818cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16828cda7954SMatthew G. Knepley 
16838cda7954SMatthew G. Knepley .seealso: DMGetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16848cda7954SMatthew G. Knepley */
16858cda7954SMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1686f9d4088aSMatthew G. Knepley {
1687f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1688f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1689f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1690f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1691f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1692f9d4088aSMatthew G. Knepley }
1693f9d4088aSMatthew G. Knepley 
16948cda7954SMatthew G. Knepley /*@C
16958cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
16968cda7954SMatthew G. Knepley 
16978cda7954SMatthew G. Knepley   Not collective
16988cda7954SMatthew G. Knepley 
16998cda7954SMatthew G. Knepley   Input Parameters:
17008cda7954SMatthew G. Knepley + dm     - The DM
17018cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17028cda7954SMatthew G. Knepley 
17038cda7954SMatthew G. Knepley   Output Parameter:
17048cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
17058cda7954SMatthew G. Knepley 
17068cda7954SMatthew G. Knepley   Notes:
17078cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
17088cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
17098cda7954SMatthew G. Knepley $ dm        - The present DM
17108cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
17118cda7954SMatthew G. Knepley $ field     - The field number in dm
17128cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
17138cda7954SMatthew G. Knepley 
17148cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
17158cda7954SMatthew G. Knepley 
17168cda7954SMatthew G. Knepley .seealso: DMSetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
17178cda7954SMatthew G. Knepley */
17188cda7954SMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1719f9d4088aSMatthew G. Knepley {
1720f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1721f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1722f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1723f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1724f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1725f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1726f9d4088aSMatthew G. Knepley }
1727f9d4088aSMatthew G. Knepley 
17284f3b5142SJed Brown /*@C
17294d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
17304d343eeaSMatthew G Knepley 
17314d343eeaSMatthew G Knepley   Not collective
17324d343eeaSMatthew G Knepley 
17334d343eeaSMatthew G Knepley   Input Parameter:
17344d343eeaSMatthew G Knepley . dm - the DM object
17354d343eeaSMatthew G Knepley 
17364d343eeaSMatthew G Knepley   Output Parameters:
17370298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
17380298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
17390298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
17404d343eeaSMatthew G Knepley 
17414d343eeaSMatthew G Knepley   Level: intermediate
17424d343eeaSMatthew G Knepley 
174321c9b008SJed Brown   Notes:
174421c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
174521c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
174621c9b008SJed Brown   PetscFree().
174721c9b008SJed Brown 
17484d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
17494d343eeaSMatthew G Knepley @*/
175037d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
17514d343eeaSMatthew G Knepley {
175237d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
17534d343eeaSMatthew G Knepley   PetscErrorCode ierr;
17544d343eeaSMatthew G Knepley 
17554d343eeaSMatthew G Knepley   PetscFunctionBegin;
17564d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
175769ca1f37SDmitry Karpeev   if (numFields) {
1758534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
175969ca1f37SDmitry Karpeev     *numFields = 0;
176069ca1f37SDmitry Karpeev   }
176137d0c07bSMatthew G Knepley   if (fieldNames) {
176237d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
17630298fd71SBarry Smith     *fieldNames = NULL;
176469ca1f37SDmitry Karpeev   }
176569ca1f37SDmitry Karpeev   if (fields) {
176669ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
17670298fd71SBarry Smith     *fields = NULL;
176869ca1f37SDmitry Karpeev   }
176992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
177037d0c07bSMatthew G Knepley   if (section) {
17713a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
177237d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
177337d0c07bSMatthew G Knepley 
1774e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
177537d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
17763a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
177737d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
177837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
177937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
17803a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
178137d0c07bSMatthew G Knepley     }
178237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
178337d0c07bSMatthew G Knepley       PetscInt gdof;
178437d0c07bSMatthew G Knepley 
178537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
178637d0c07bSMatthew G Knepley       if (gdof > 0) {
178737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
17883a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
178937d0c07bSMatthew G Knepley 
179037d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
179137d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
17923a544194SStefano Zampini           fpdof = fdof-fcdof;
17933a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
17943a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
17953a544194SStefano Zampini             fieldNc[f] = 1;
17963a544194SStefano Zampini           }
17973a544194SStefano Zampini           fieldSizes[f] += fpdof;
179837d0c07bSMatthew G Knepley         }
179937d0c07bSMatthew G Knepley       }
180037d0c07bSMatthew G Knepley     }
180137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1802785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
180337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
180437d0c07bSMatthew G Knepley     }
180537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
180637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
180737d0c07bSMatthew G Knepley 
180837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
180937d0c07bSMatthew G Knepley       if (gdof > 0) {
181037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
181137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
181237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
181337d0c07bSMatthew G Knepley 
181437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
181537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
181637d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
181737d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
181837d0c07bSMatthew G Knepley           }
181937d0c07bSMatthew G Knepley         }
182037d0c07bSMatthew G Knepley       }
182137d0c07bSMatthew G Knepley     }
18228865f1eaSKarl Rupp     if (numFields) *numFields = nF;
182337d0c07bSMatthew G Knepley     if (fieldNames) {
1824785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
182537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
182637d0c07bSMatthew G Knepley         const char *fieldName;
182737d0c07bSMatthew G Knepley 
182837d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
182937d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
183037d0c07bSMatthew G Knepley       }
183137d0c07bSMatthew G Knepley     }
183237d0c07bSMatthew G Knepley     if (fields) {
1833785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
183437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
18353a544194SStefano Zampini         PetscInt bs, in[2], out[2];
18363a544194SStefano Zampini 
183782f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
18383a544194SStefano Zampini         in[0] = -fieldNc[f];
18393a544194SStefano Zampini         in[1] = fieldNc[f];
1840820f2d46SBarry Smith         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
18413a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
18423a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
184337d0c07bSMatthew G Knepley       }
184437d0c07bSMatthew G Knepley     }
18453a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
18468865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
18478865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
184869ca1f37SDmitry Karpeev   }
18494d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
18504d343eeaSMatthew G Knepley }
18514d343eeaSMatthew G Knepley 
185216621825SDmitry Karpeev /*@C
185316621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
185416621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
185516621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1856e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1857e7c4fc90SDmitry Karpeev 
1858e7c4fc90SDmitry Karpeev   Not collective
1859e7c4fc90SDmitry Karpeev 
1860e7c4fc90SDmitry Karpeev   Input Parameter:
1861e7c4fc90SDmitry Karpeev . dm - the DM object
1862e7c4fc90SDmitry Karpeev 
1863e7c4fc90SDmitry Karpeev   Output Parameters:
18640298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
18650298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
18660298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
18670298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1868e7c4fc90SDmitry Karpeev 
1869e7c4fc90SDmitry Karpeev   Level: intermediate
1870e7c4fc90SDmitry Karpeev 
1871e7c4fc90SDmitry Karpeev   Notes:
1872e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1873e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1874e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1875e7c4fc90SDmitry Karpeev 
1876e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1877e7c4fc90SDmitry Karpeev @*/
187816621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1879e7c4fc90SDmitry Karpeev {
1880e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1881e7c4fc90SDmitry Karpeev 
1882e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1883e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18848865f1eaSKarl Rupp   if (len) {
1885534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
18868865f1eaSKarl Rupp     *len = 0;
18878865f1eaSKarl Rupp   }
18888865f1eaSKarl Rupp   if (namelist) {
18898865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1890ea78f98cSLisandro Dalcin     *namelist = NULL;
18918865f1eaSKarl Rupp   }
18928865f1eaSKarl Rupp   if (islist) {
18938865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1894ea78f98cSLisandro Dalcin     *islist = NULL;
18958865f1eaSKarl Rupp   }
18968865f1eaSKarl Rupp   if (dmlist) {
18978865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1898ea78f98cSLisandro Dalcin     *dmlist = NULL;
18998865f1eaSKarl Rupp   }
1900f3f0edfdSDmitry Karpeev   /*
1901f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1902f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1903f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1904f3f0edfdSDmitry Karpeev    */
1905ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
190616621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1907435a35e8SMatthew G Knepley     PetscSection section;
1908435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1909435a35e8SMatthew G Knepley 
191092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1911435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1912435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1913f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
191403dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
191503dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
191603dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1917435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1918435a35e8SMatthew G Knepley         const char *fieldName;
1919435a35e8SMatthew G Knepley 
192003dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
192103dc3394SMatthew G. Knepley         if (namelist) {
1922435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1923435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1924435a35e8SMatthew G Knepley         }
192503dc3394SMatthew G. Knepley       }
1926435a35e8SMatthew G Knepley     } else {
192769ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1928e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
19290298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1930e7c4fc90SDmitry Karpeev     }
19318865f1eaSKarl Rupp   } else {
193216621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
193316621825SDmitry Karpeev   }
193416621825SDmitry Karpeev   PetscFunctionReturn(0);
193516621825SDmitry Karpeev }
193616621825SDmitry Karpeev 
1937564cec59SMatthew G. Knepley /*@
1938435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1939435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1940435a35e8SMatthew G Knepley 
1941435a35e8SMatthew G Knepley   Not collective
1942435a35e8SMatthew G Knepley 
1943435a35e8SMatthew G Knepley   Input Parameters:
19442adcc780SMatthew G. Knepley + dm        - The DM object
19452adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
19462adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1947435a35e8SMatthew G Knepley 
1948435a35e8SMatthew G Knepley   Output Parameters:
19492adcc780SMatthew G. Knepley + is - The global indices for the subproblem
19502adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1951435a35e8SMatthew G Knepley 
19525d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19535d3b26e6SMatthew G. Knepley 
1954435a35e8SMatthew G Knepley   Level: intermediate
1955435a35e8SMatthew G Knepley 
19565d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1957435a35e8SMatthew G Knepley @*/
195837bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1959435a35e8SMatthew G Knepley {
1960435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1961435a35e8SMatthew G Knepley 
1962435a35e8SMatthew G Knepley   PetscFunctionBegin;
1963435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1964435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
19658865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
19668865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1967b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1968435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1969435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1970435a35e8SMatthew G Knepley }
1971435a35e8SMatthew G Knepley 
19722adcc780SMatthew G. Knepley /*@C
19732adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
19742adcc780SMatthew G. Knepley 
19752adcc780SMatthew G. Knepley   Not collective
19762adcc780SMatthew G. Knepley 
1977d8d19677SJose E. Roman   Input Parameters:
19782adcc780SMatthew G. Knepley + dms - The DM objects
19792adcc780SMatthew G. Knepley - len - The number of DMs
19802adcc780SMatthew G. Knepley 
19812adcc780SMatthew G. Knepley   Output Parameters:
1982a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
19832adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
19842adcc780SMatthew G. Knepley 
19855d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19865d3b26e6SMatthew G. Knepley 
19872adcc780SMatthew G. Knepley   Level: intermediate
19882adcc780SMatthew G. Knepley 
19895d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
19902adcc780SMatthew G. Knepley @*/
19912adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
19922adcc780SMatthew G. Knepley {
19932adcc780SMatthew G. Knepley   PetscInt       i;
19942adcc780SMatthew G. Knepley   PetscErrorCode ierr;
19952adcc780SMatthew G. Knepley 
19962adcc780SMatthew G. Knepley   PetscFunctionBegin;
19972adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
19982adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
19992adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
2000a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
20012adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
20022adcc780SMatthew G. Knepley   if (len) {
2003b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2004b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
2005b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
20062adcc780SMatthew G. Knepley   }
20072adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
20082adcc780SMatthew G. Knepley }
20092adcc780SMatthew G. Knepley 
201016621825SDmitry Karpeev /*@C
20118d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
20128d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
20138d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
20148d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
20158d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
201616621825SDmitry Karpeev 
201716621825SDmitry Karpeev   Not collective
201816621825SDmitry Karpeev 
201916621825SDmitry Karpeev   Input Parameter:
202016621825SDmitry Karpeev . dm - the DM object
202116621825SDmitry Karpeev 
202216621825SDmitry Karpeev   Output Parameters:
20230298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
20240298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
20250298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
20260298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
20270298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
202816621825SDmitry Karpeev 
202916621825SDmitry Karpeev   Level: intermediate
203016621825SDmitry Karpeev 
203116621825SDmitry Karpeev   Notes:
203216621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
203316621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
203416621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
203516621825SDmitry Karpeev 
2036245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
203716621825SDmitry Karpeev @*/
20388d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
203916621825SDmitry Karpeev {
204016621825SDmitry Karpeev   PetscErrorCode      ierr;
2041be081cd6SPeter Brune   DMSubDomainHookLink link;
2042be081cd6SPeter Brune   PetscInt            i,l;
204316621825SDmitry Karpeev 
204416621825SDmitry Karpeev   PetscFunctionBegin;
204516621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
204614a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
20470298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
20480298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
20490298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
20500298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
2051f3f0edfdSDmitry Karpeev   /*
2052f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2053f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2054f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2055f3f0edfdSDmitry Karpeev    */
2056ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
205716621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2058be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
205914a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2060f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2061be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2062be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
2063be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
2064be081cd6SPeter Brune         }
2065648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2066e7c4fc90SDmitry Karpeev       }
206714a18fd3SPeter Brune     }
206814a18fd3SPeter Brune     if (len) *len = l;
206914a18fd3SPeter Brune   }
2070e30e807fSPeter Brune   PetscFunctionReturn(0);
2071e30e807fSPeter Brune }
2072e30e807fSPeter Brune 
2073e30e807fSPeter Brune /*@C
2074e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2075e30e807fSPeter Brune 
2076e30e807fSPeter Brune   Not collective
2077e30e807fSPeter Brune 
2078e30e807fSPeter Brune   Input Parameters:
2079e30e807fSPeter Brune + dm - the DM object
2080e30e807fSPeter Brune . n  - the number of subdomain scatters
2081e30e807fSPeter Brune - subdms - the local subdomains
2082e30e807fSPeter Brune 
2083e30e807fSPeter Brune   Output Parameters:
20846b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2085e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2086e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2087e30e807fSPeter Brune 
208895452b02SPatrick Sanan   Notes:
208995452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2090e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2091e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2092e30e807fSPeter Brune   solution and residual data.
2093e30e807fSPeter Brune 
2094e30e807fSPeter Brune   Level: developer
2095e30e807fSPeter Brune 
2096e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2097e30e807fSPeter Brune @*/
2098e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2099e30e807fSPeter Brune {
2100e30e807fSPeter Brune   PetscErrorCode ierr;
2101e30e807fSPeter Brune 
2102e30e807fSPeter Brune   PetscFunctionBegin;
2103e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2104e30e807fSPeter Brune   PetscValidPointer(subdms,3);
2105b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
2106e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
2107e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2108e7c4fc90SDmitry Karpeev }
2109e7c4fc90SDmitry Karpeev 
211047c6ae99SBarry Smith /*@
211147c6ae99SBarry Smith   DMRefine - Refines a DM object
211247c6ae99SBarry Smith 
2113d083f849SBarry Smith   Collective on dm
211447c6ae99SBarry Smith 
2115d8d19677SJose E. Roman   Input Parameters:
211647c6ae99SBarry Smith + dm   - the DM object
211791d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
211847c6ae99SBarry Smith 
211947c6ae99SBarry Smith   Output Parameter:
21200298fd71SBarry Smith . dmf - the refined DM, or NULL
2121ae0a1c52SMatthew G Knepley 
2122f27dd7c6SMatthew G. Knepley   Options Database Keys:
2123412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2124412e9a14SMatthew G. Knepley 
21250298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
212647c6ae99SBarry Smith 
212747c6ae99SBarry Smith   Level: developer
212847c6ae99SBarry Smith 
2129e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
213047c6ae99SBarry Smith @*/
21317087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
213247c6ae99SBarry Smith {
213347c6ae99SBarry Smith   PetscErrorCode   ierr;
2134c833c3b5SJed Brown   DMRefineHookLink link;
213547c6ae99SBarry Smith 
213647c6ae99SBarry Smith   PetscFunctionBegin;
2137732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2138b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
21391ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
214047c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
21414057135bSMatthew G Knepley   if (*dmf) {
214243842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
21438865f1eaSKarl Rupp 
21448cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
21458865f1eaSKarl Rupp 
2146644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
21470598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2148656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
21498865f1eaSKarl Rupp 
2150e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
2151c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
21528865f1eaSKarl Rupp       if (link->refinehook) {
21538865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
21548865f1eaSKarl Rupp       }
2155c833c3b5SJed Brown     }
2156c833c3b5SJed Brown   }
21571ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
2158c833c3b5SJed Brown   PetscFunctionReturn(0);
2159c833c3b5SJed Brown }
2160c833c3b5SJed Brown 
2161bb9467b5SJed Brown /*@C
2162c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2163c833c3b5SJed Brown 
2164c833c3b5SJed Brown    Logically Collective
2165c833c3b5SJed Brown 
21664165533cSJose E. Roman    Input Parameters:
2167c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2168c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2169c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21700298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2171c833c3b5SJed Brown 
2172c833c3b5SJed Brown    Calling sequence of refinehook:
2173c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2174c833c3b5SJed Brown 
2175c833c3b5SJed Brown +  coarse - coarse level DM
2176c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2177c833c3b5SJed Brown -  ctx - optional user-defined function context
2178c833c3b5SJed Brown 
2179c833c3b5SJed Brown    Calling sequence for interphook:
2180c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2181c833c3b5SJed Brown 
2182c833c3b5SJed Brown +  coarse - coarse level DM
2183c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2184c833c3b5SJed Brown .  fine - fine level DM to update
2185c833c3b5SJed Brown -  ctx - optional user-defined function context
2186c833c3b5SJed Brown 
2187c833c3b5SJed Brown    Level: advanced
2188c833c3b5SJed Brown 
2189c833c3b5SJed Brown    Notes:
2190c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2191c833c3b5SJed Brown 
2192c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2193c833c3b5SJed Brown 
2194bb9467b5SJed Brown    This function is currently not available from Fortran.
2195bb9467b5SJed Brown 
2196c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2197c833c3b5SJed Brown @*/
2198c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2199c833c3b5SJed Brown {
2200c833c3b5SJed Brown   PetscErrorCode   ierr;
2201c833c3b5SJed Brown   DMRefineHookLink link,*p;
2202c833c3b5SJed Brown 
2203c833c3b5SJed Brown   PetscFunctionBegin;
2204c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22053d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
22063d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
22073d8e3701SJed Brown   }
220895dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2209c833c3b5SJed Brown   link->refinehook = refinehook;
2210c833c3b5SJed Brown   link->interphook = interphook;
2211c833c3b5SJed Brown   link->ctx        = ctx;
22120298fd71SBarry Smith   link->next       = NULL;
2213c833c3b5SJed Brown   *p               = link;
2214c833c3b5SJed Brown   PetscFunctionReturn(0);
2215c833c3b5SJed Brown }
2216c833c3b5SJed Brown 
22173d8e3701SJed Brown /*@C
22183d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
22193d8e3701SJed Brown 
22203d8e3701SJed Brown    Logically Collective
22213d8e3701SJed Brown 
22224165533cSJose E. Roman    Input Parameters:
22233d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
22243d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
22253d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
22263d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22273d8e3701SJed Brown 
22283d8e3701SJed Brown    Level: advanced
22293d8e3701SJed Brown 
22303d8e3701SJed Brown    Notes:
22313d8e3701SJed Brown    This function does nothing if the hook is not in the list.
22323d8e3701SJed Brown 
22333d8e3701SJed Brown    This function is currently not available from Fortran.
22343d8e3701SJed Brown 
22353d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
22363d8e3701SJed Brown @*/
22373d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
22383d8e3701SJed Brown {
22393d8e3701SJed Brown   PetscErrorCode   ierr;
22403d8e3701SJed Brown   DMRefineHookLink link,*p;
22413d8e3701SJed Brown 
22423d8e3701SJed Brown   PetscFunctionBegin;
22433d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22443d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
22453d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
22463d8e3701SJed Brown       link = *p;
22473d8e3701SJed Brown       *p = link->next;
22483d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
22493d8e3701SJed Brown       break;
22503d8e3701SJed Brown     }
22513d8e3701SJed Brown   }
22523d8e3701SJed Brown   PetscFunctionReturn(0);
22533d8e3701SJed Brown }
22543d8e3701SJed Brown 
2255c833c3b5SJed Brown /*@
2256c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2257c833c3b5SJed Brown 
2258c833c3b5SJed Brown    Collective if any hooks are
2259c833c3b5SJed Brown 
22604165533cSJose E. Roman    Input Parameters:
2261c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2262e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2263c833c3b5SJed Brown -  fine - finer DM to update
2264c833c3b5SJed Brown 
2265c833c3b5SJed Brown    Level: developer
2266c833c3b5SJed Brown 
2267c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2268c833c3b5SJed Brown @*/
2269c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2270c833c3b5SJed Brown {
2271c833c3b5SJed Brown   PetscErrorCode   ierr;
2272c833c3b5SJed Brown   DMRefineHookLink link;
2273c833c3b5SJed Brown 
2274c833c3b5SJed Brown   PetscFunctionBegin;
2275c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
22768865f1eaSKarl Rupp     if (link->interphook) {
22778865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
22788865f1eaSKarl Rupp     }
22794057135bSMatthew G Knepley   }
228047c6ae99SBarry Smith   PetscFunctionReturn(0);
228147c6ae99SBarry Smith }
228247c6ae99SBarry Smith 
2283eb3f98d2SBarry Smith /*@
22841f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
22851f3379b2SToby Isaac 
22861f3379b2SToby Isaac    Collective on DM
22871f3379b2SToby Isaac 
22884165533cSJose E. Roman    Input Parameters:
22891f3379b2SToby Isaac +  coarse - coarse DM
22901f3379b2SToby Isaac .  fine   - fine DM
22911f3379b2SToby Isaac .  interp - (optional) the matrix computed by DMCreateInterpolation().  Implementations may not need this, but if it
22921f3379b2SToby Isaac             is available it can avoid some recomputation.  If it is provided, MatInterpolate() will be used if
22931f3379b2SToby Isaac             the coarse DM does not have a specialized implementation.
22941f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
22951f3379b2SToby Isaac 
22964165533cSJose E. Roman    Output Parameter:
22971f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
22981f3379b2SToby Isaac 
22991f3379b2SToby Isaac    Level: developer
23001f3379b2SToby Isaac 
23011f3379b2SToby Isaac    Note: This function exists because the interpolation of a solution vector between meshes is not always a linear
23021f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
23031f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
23041f3379b2SToby Isaac    slope-limiting reconstruction.
23051f3379b2SToby Isaac 
23061f3379b2SToby Isaac .seealso DMInterpolate(), DMCreateInterpolation()
23071f3379b2SToby Isaac @*/
23081f3379b2SToby Isaac PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
23091f3379b2SToby Isaac {
23101f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM,DM,Mat,Vec,Vec) = NULL;
23111f3379b2SToby Isaac   PetscErrorCode ierr;
23121f3379b2SToby Isaac 
23131f3379b2SToby Isaac   PetscFunctionBegin;
23141f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
23151f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp,MAT_CLASSID,3);
23161f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol,VEC_CLASSID,4);
23171f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol,VEC_CLASSID,5);
23181f3379b2SToby Isaac 
23191f3379b2SToby Isaac   ierr = PetscObjectQueryFunction((PetscObject)coarse,"DMInterpolateSolution_C", &interpsol);CHKERRQ(ierr);
23201f3379b2SToby Isaac   if (interpsol) {
23211f3379b2SToby Isaac     ierr = (*interpsol)(coarse, fine, interp, coarseSol, fineSol);CHKERRQ(ierr);
23221f3379b2SToby Isaac   } else if (interp) {
23231f3379b2SToby Isaac     ierr = MatInterpolate(interp, coarseSol, fineSol);CHKERRQ(ierr);
23241f3379b2SToby Isaac   } else SETERRQ1(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
23251f3379b2SToby Isaac   PetscFunctionReturn(0);
23261f3379b2SToby Isaac }
23271f3379b2SToby Isaac 
23281f3379b2SToby Isaac /*@
2329aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2330eb3f98d2SBarry Smith 
2331eb3f98d2SBarry Smith     Not Collective
2332eb3f98d2SBarry Smith 
2333eb3f98d2SBarry Smith     Input Parameter:
2334eb3f98d2SBarry Smith .   dm - the DM object
2335eb3f98d2SBarry Smith 
2336eb3f98d2SBarry Smith     Output Parameter:
2337eb3f98d2SBarry Smith .   level - number of refinements
2338eb3f98d2SBarry Smith 
2339eb3f98d2SBarry Smith     Level: developer
2340eb3f98d2SBarry Smith 
23416a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2342eb3f98d2SBarry Smith 
2343eb3f98d2SBarry Smith @*/
2344eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2345eb3f98d2SBarry Smith {
2346eb3f98d2SBarry Smith   PetscFunctionBegin;
2347eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2348eb3f98d2SBarry Smith   *level = dm->levelup;
2349eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2350eb3f98d2SBarry Smith }
2351eb3f98d2SBarry Smith 
2352fef3a512SBarry Smith /*@
2353aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2354fef3a512SBarry Smith 
2355fef3a512SBarry Smith     Not Collective
2356fef3a512SBarry Smith 
2357d8d19677SJose E. Roman     Input Parameters:
2358fef3a512SBarry Smith +   dm - the DM object
2359fef3a512SBarry Smith -   level - number of refinements
2360fef3a512SBarry Smith 
2361fef3a512SBarry Smith     Level: advanced
2362fef3a512SBarry Smith 
236395452b02SPatrick Sanan     Notes:
236495452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2365fef3a512SBarry Smith 
2366fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2367fef3a512SBarry Smith 
2368fef3a512SBarry Smith @*/
2369fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2370fef3a512SBarry Smith {
2371fef3a512SBarry Smith   PetscFunctionBegin;
2372fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2373fef3a512SBarry Smith   dm->levelup = level;
2374fef3a512SBarry Smith   PetscFunctionReturn(0);
2375fef3a512SBarry Smith }
2376fef3a512SBarry Smith 
2377d410b0cfSMatthew G. Knepley /*@
2378d410b0cfSMatthew G. Knepley   DMExtrude - Extrude a DM object from a surface
2379d410b0cfSMatthew G. Knepley 
2380d410b0cfSMatthew G. Knepley   Collective on dm
2381d410b0cfSMatthew G. Knepley 
2382d410b0cfSMatthew G. Knepley   Input Parameter:
2383d410b0cfSMatthew G. Knepley + dm     - the DM object
2384d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2385d410b0cfSMatthew G. Knepley 
2386d410b0cfSMatthew G. Knepley   Output Parameter:
2387d410b0cfSMatthew G. Knepley . dme - the extruded DM, or NULL
2388d410b0cfSMatthew G. Knepley 
2389d410b0cfSMatthew G. Knepley   Note: If no extrusion was done, the return value is NULL
2390d410b0cfSMatthew G. Knepley 
2391d410b0cfSMatthew G. Knepley   Level: developer
2392d410b0cfSMatthew G. Knepley 
2393d410b0cfSMatthew G. Knepley .seealso DMRefine(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector()
2394d410b0cfSMatthew G. Knepley @*/
2395d410b0cfSMatthew G. Knepley PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2396d410b0cfSMatthew G. Knepley {
2397d410b0cfSMatthew G. Knepley   PetscErrorCode ierr;
2398d410b0cfSMatthew G. Knepley 
2399d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2400d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2401d410b0cfSMatthew G. Knepley   if (!dm->ops->extrude) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "DM type %s does not implement DMExtrude", ((PetscObject) dm)->type_name);
2402d410b0cfSMatthew G. Knepley   ierr = (*dm->ops->extrude)(dm, layers, dme);CHKERRQ(ierr);
2403d410b0cfSMatthew G. Knepley   if (*dme) {
2404d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
2405d410b0cfSMatthew G. Knepley     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject) dm, (PetscObject) *dme);CHKERRQ(ierr);
2406d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
2407d410b0cfSMatthew G. Knepley     ierr = DMSetMatType(*dme, dm->mattype);CHKERRQ(ierr);
2408d410b0cfSMatthew G. Knepley   }
2409d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2410d410b0cfSMatthew G. Knepley }
2411d410b0cfSMatthew G. Knepley 
2412ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2413ca3d3a14SMatthew G. Knepley {
2414ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2415ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2416ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2417ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2418ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2419ca3d3a14SMatthew G. Knepley }
2420ca3d3a14SMatthew G. Knepley 
2421ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2422ca3d3a14SMatthew G. Knepley {
2423ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2424ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2425ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2426ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2427ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2428ca3d3a14SMatthew G. Knepley }
2429ca3d3a14SMatthew G. Knepley 
2430ca3d3a14SMatthew G. Knepley /*@
2431c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2432ca3d3a14SMatthew G. Knepley 
2433ca3d3a14SMatthew G. Knepley   Input Parameter:
2434ca3d3a14SMatthew G. Knepley . dm - The DM
2435ca3d3a14SMatthew G. Knepley 
2436ca3d3a14SMatthew G. Knepley   Output Parameter:
2437ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2438ca3d3a14SMatthew G. Knepley 
2439ca3d3a14SMatthew G. Knepley   Level: developer
2440ca3d3a14SMatthew G. Knepley 
2441436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2442ca3d3a14SMatthew G. Knepley @*/
2443ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2444ca3d3a14SMatthew G. Knepley {
2445ca3d3a14SMatthew G. Knepley   Vec            tv;
2446ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2447ca3d3a14SMatthew G. Knepley 
2448ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2449ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2450534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2451ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2452ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2453ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2454ca3d3a14SMatthew G. Knepley }
2455ca3d3a14SMatthew G. Knepley 
2456ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2457ca3d3a14SMatthew G. Knepley {
2458ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2459ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2460ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2461ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2462ca3d3a14SMatthew G. Knepley 
2463ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2464ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
246592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2466ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2467ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2468ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
246992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2470ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2471ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2472ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2473ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2474ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2475ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2476ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2477ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2478ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2479ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2480ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2481ca3d3a14SMatthew G. Knepley     }
2482ca3d3a14SMatthew G. Knepley   }
2483ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2484ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2485ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2486ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2487ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2488ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2489ca3d3a14SMatthew G. Knepley       if (dof) {
2490ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2491ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2492ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2493ca3d3a14SMatthew G. Knepley 
2494ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2495ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2496ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2497580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2498ca3d3a14SMatthew G. Knepley       }
2499ca3d3a14SMatthew G. Knepley     }
2500ca3d3a14SMatthew G. Knepley   }
2501ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2502ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2503ca3d3a14SMatthew G. Knepley }
2504ca3d3a14SMatthew G. Knepley 
2505ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2506ca3d3a14SMatthew G. Knepley {
2507ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2508ca3d3a14SMatthew G. Knepley 
2509ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2510ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2511ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2512ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2513ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2514ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2515ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2516ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2517ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2518ca3d3a14SMatthew G. Knepley }
2519ca3d3a14SMatthew G. Knepley 
2520bb9467b5SJed Brown /*@C
2521baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2522baf369e7SPeter Brune 
2523baf369e7SPeter Brune    Logically Collective
2524baf369e7SPeter Brune 
25254165533cSJose E. Roman    Input Parameters:
2526baf369e7SPeter Brune +  dm - the DM
2527baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2528baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
25290298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2530baf369e7SPeter Brune 
2531baf369e7SPeter Brune    Calling sequence for beginhook:
2532baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2533baf369e7SPeter Brune 
2534baf369e7SPeter Brune +  dm - global DM
2535baf369e7SPeter Brune .  g - global vector
2536baf369e7SPeter Brune .  mode - mode
2537baf369e7SPeter Brune .  l - local vector
2538baf369e7SPeter Brune -  ctx - optional user-defined function context
2539baf369e7SPeter Brune 
2540baf369e7SPeter Brune    Calling sequence for endhook:
2541ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2542baf369e7SPeter Brune 
2543baf369e7SPeter Brune +  global - global DM
2544baf369e7SPeter Brune -  ctx - optional user-defined function context
2545baf369e7SPeter Brune 
2546baf369e7SPeter Brune    Level: advanced
2547baf369e7SPeter Brune 
2548baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2549baf369e7SPeter Brune @*/
2550baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2551baf369e7SPeter Brune {
2552baf369e7SPeter Brune   PetscErrorCode          ierr;
2553baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2554baf369e7SPeter Brune 
2555baf369e7SPeter Brune   PetscFunctionBegin;
2556baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2557baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
255895dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2559baf369e7SPeter Brune   link->beginhook = beginhook;
2560baf369e7SPeter Brune   link->endhook   = endhook;
2561baf369e7SPeter Brune   link->ctx       = ctx;
25620298fd71SBarry Smith   link->next      = NULL;
2563baf369e7SPeter Brune   *p              = link;
2564baf369e7SPeter Brune   PetscFunctionReturn(0);
2565baf369e7SPeter Brune }
2566baf369e7SPeter Brune 
25674c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
25684c274da1SToby Isaac {
25694c274da1SToby Isaac   Mat cMat;
25704c274da1SToby Isaac   Vec cVec;
25714c274da1SToby Isaac   PetscSection section, cSec;
25724c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25734c274da1SToby Isaac   PetscErrorCode ierr;
25744c274da1SToby Isaac 
25754c274da1SToby Isaac   PetscFunctionBegin;
25764c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25774c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
25784c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
25795db9a05bSToby Isaac     PetscInt nRows;
25805db9a05bSToby Isaac 
25815db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
25825db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
258392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
25847711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
25854c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
25864c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
25874c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
25884c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
25894c274da1SToby Isaac       if (dof) {
25904c274da1SToby Isaac         PetscScalar *vals;
25914c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
25924c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
25934c274da1SToby Isaac       }
25944c274da1SToby Isaac     }
25954c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
25964c274da1SToby Isaac   }
25974c274da1SToby Isaac   PetscFunctionReturn(0);
25984c274da1SToby Isaac }
25994c274da1SToby Isaac 
260047c6ae99SBarry Smith /*@
260101729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
260201729b5cSPatrick Sanan 
2603d083f849SBarry Smith     Neighbor-wise Collective on dm
260401729b5cSPatrick Sanan 
260501729b5cSPatrick Sanan     Input Parameters:
260601729b5cSPatrick Sanan +   dm - the DM object
260701729b5cSPatrick Sanan .   g - the global vector
260801729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
260901729b5cSPatrick Sanan -   l - the local vector
261001729b5cSPatrick Sanan 
261101729b5cSPatrick Sanan     Notes:
261201729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
261301729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
261401729b5cSPatrick Sanan 
261501729b5cSPatrick Sanan     Level: beginner
261601729b5cSPatrick Sanan 
261701729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
261801729b5cSPatrick Sanan 
261901729b5cSPatrick Sanan @*/
262001729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
262101729b5cSPatrick Sanan {
262201729b5cSPatrick Sanan   PetscErrorCode ierr;
262301729b5cSPatrick Sanan 
262401729b5cSPatrick Sanan   PetscFunctionBegin;
262501729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
262601729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
262701729b5cSPatrick Sanan   PetscFunctionReturn(0);
262801729b5cSPatrick Sanan }
262901729b5cSPatrick Sanan 
263001729b5cSPatrick Sanan /*@
263147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
263247c6ae99SBarry Smith 
2633d083f849SBarry Smith     Neighbor-wise Collective on dm
263447c6ae99SBarry Smith 
263547c6ae99SBarry Smith     Input Parameters:
263647c6ae99SBarry Smith +   dm - the DM object
263747c6ae99SBarry Smith .   g - the global vector
263847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
263947c6ae99SBarry Smith -   l - the local vector
264047c6ae99SBarry Smith 
264101729b5cSPatrick Sanan     Level: intermediate
264247c6ae99SBarry Smith 
264301729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
264447c6ae99SBarry Smith 
264547c6ae99SBarry Smith @*/
26467087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
264747c6ae99SBarry Smith {
26487128ae9fSMatthew G Knepley   PetscSF                 sf;
264947c6ae99SBarry Smith   PetscErrorCode          ierr;
2650baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
265147c6ae99SBarry Smith 
265247c6ae99SBarry Smith   PetscFunctionBegin;
2653171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2654baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
26558865f1eaSKarl Rupp     if (link->beginhook) {
26568865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
26578865f1eaSKarl Rupp     }
2658baf369e7SPeter Brune   }
26591bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
26607128ae9fSMatthew G Knepley   if (sf) {
2661ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2662ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2663d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
26647128ae9fSMatthew G Knepley 
266582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2666a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2667a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2668ad227feaSJunchao Zhang     ierr = PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE);CHKERRQ(ierr);
2669a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2670a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
26717128ae9fSMatthew G Knepley   } else {
267233907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2673843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
26747128ae9fSMatthew G Knepley   }
267547c6ae99SBarry Smith   PetscFunctionReturn(0);
267647c6ae99SBarry Smith }
267747c6ae99SBarry Smith 
267847c6ae99SBarry Smith /*@
267947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
268047c6ae99SBarry Smith 
2681d083f849SBarry Smith     Neighbor-wise Collective on dm
268247c6ae99SBarry Smith 
268347c6ae99SBarry Smith     Input Parameters:
268447c6ae99SBarry Smith +   dm - the DM object
268547c6ae99SBarry Smith .   g - the global vector
268647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
268747c6ae99SBarry Smith -   l - the local vector
268847c6ae99SBarry Smith 
268901729b5cSPatrick Sanan     Level: intermediate
269047c6ae99SBarry Smith 
269101729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
269247c6ae99SBarry Smith 
269347c6ae99SBarry Smith @*/
26947087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
269547c6ae99SBarry Smith {
26967128ae9fSMatthew G Knepley   PetscSF                 sf;
269747c6ae99SBarry Smith   PetscErrorCode          ierr;
2698ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2699ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2700ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2701baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2702d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
270347c6ae99SBarry Smith 
270447c6ae99SBarry Smith   PetscFunctionBegin;
2705171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27061bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2707ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
27087128ae9fSMatthew G Knepley   if (sf) {
270982f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
27107128ae9fSMatthew G Knepley 
2711a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2712a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2713ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray,MPI_REPLACE);CHKERRQ(ierr);
2714a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2715a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
2716ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
27177128ae9fSMatthew G Knepley   } else {
271833907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2719843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
27207128ae9fSMatthew G Knepley   }
27214c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2722baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2723baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2724baf369e7SPeter Brune   }
272547c6ae99SBarry Smith   PetscFunctionReturn(0);
272647c6ae99SBarry Smith }
272747c6ae99SBarry Smith 
2728d4d07f1eSToby Isaac /*@C
2729d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2730d4d07f1eSToby Isaac 
2731d4d07f1eSToby Isaac    Logically Collective
2732d4d07f1eSToby Isaac 
27334165533cSJose E. Roman    Input Parameters:
2734d4d07f1eSToby Isaac +  dm - the DM
2735d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2736d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2737d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2738d4d07f1eSToby Isaac 
2739d4d07f1eSToby Isaac    Calling sequence for beginhook:
2740d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2741d4d07f1eSToby Isaac 
2742d4d07f1eSToby Isaac +  dm - global DM
2743d4d07f1eSToby Isaac .  l - local vector
2744d4d07f1eSToby Isaac .  mode - mode
2745d4d07f1eSToby Isaac .  g - global vector
2746d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2747d4d07f1eSToby Isaac 
2748d4d07f1eSToby Isaac    Calling sequence for endhook:
2749d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2750d4d07f1eSToby Isaac 
2751d4d07f1eSToby Isaac +  global - global DM
2752d4d07f1eSToby Isaac .  l - local vector
2753d4d07f1eSToby Isaac .  mode - mode
2754d4d07f1eSToby Isaac .  g - global vector
2755d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2756d4d07f1eSToby Isaac 
2757d4d07f1eSToby Isaac    Level: advanced
2758d4d07f1eSToby Isaac 
2759d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2760d4d07f1eSToby Isaac @*/
2761d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2762d4d07f1eSToby Isaac {
2763d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2764d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2765d4d07f1eSToby Isaac 
2766d4d07f1eSToby Isaac   PetscFunctionBegin;
2767d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2768d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
276995dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2770d4d07f1eSToby Isaac   link->beginhook = beginhook;
2771d4d07f1eSToby Isaac   link->endhook   = endhook;
2772d4d07f1eSToby Isaac   link->ctx       = ctx;
2773d4d07f1eSToby Isaac   link->next      = NULL;
2774d4d07f1eSToby Isaac   *p              = link;
2775d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2776d4d07f1eSToby Isaac }
2777d4d07f1eSToby Isaac 
27784c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
27794c274da1SToby Isaac {
27804c274da1SToby Isaac   Mat cMat;
27814c274da1SToby Isaac   Vec cVec;
27824c274da1SToby Isaac   PetscSection section, cSec;
27834c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
27844c274da1SToby Isaac   PetscErrorCode ierr;
27854c274da1SToby Isaac 
27864c274da1SToby Isaac   PetscFunctionBegin;
27874c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27884c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
27894c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
27905db9a05bSToby Isaac     PetscInt nRows;
27915db9a05bSToby Isaac 
27925db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
27935db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
279492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
27957711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
27964c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
27974c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27984c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
27994c274da1SToby Isaac       if (dof) {
28004c274da1SToby Isaac         PetscInt d;
28014c274da1SToby Isaac         PetscScalar *vals;
28024c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
28034c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
28044c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
28054c274da1SToby Isaac          * we just extracted */
28064c274da1SToby Isaac         for (d = 0; d < dof; d++) {
28074c274da1SToby Isaac           vals[d] = 0.;
28084c274da1SToby Isaac         }
28094c274da1SToby Isaac       }
28104c274da1SToby Isaac     }
28114c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
28124c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
28134c274da1SToby Isaac   }
28144c274da1SToby Isaac   PetscFunctionReturn(0);
28154c274da1SToby Isaac }
281601729b5cSPatrick Sanan /*@
281701729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
281801729b5cSPatrick Sanan 
2819d083f849SBarry Smith     Neighbor-wise Collective on dm
282001729b5cSPatrick Sanan 
282101729b5cSPatrick Sanan     Input Parameters:
282201729b5cSPatrick Sanan +   dm - the DM object
282301729b5cSPatrick Sanan .   l - the local vector
282401729b5cSPatrick 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.
282501729b5cSPatrick Sanan -   g - the global vector
282601729b5cSPatrick Sanan 
282701729b5cSPatrick Sanan     Notes:
282801729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
282901729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
283001729b5cSPatrick Sanan 
283101729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
283201729b5cSPatrick 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.
283301729b5cSPatrick Sanan 
283401729b5cSPatrick Sanan     Level: beginner
283501729b5cSPatrick Sanan 
283601729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
283701729b5cSPatrick Sanan 
283801729b5cSPatrick Sanan @*/
283901729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
284001729b5cSPatrick Sanan {
284101729b5cSPatrick Sanan   PetscErrorCode ierr;
284201729b5cSPatrick Sanan 
284301729b5cSPatrick Sanan   PetscFunctionBegin;
284401729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
284501729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
284601729b5cSPatrick Sanan   PetscFunctionReturn(0);
284701729b5cSPatrick Sanan }
28484c274da1SToby Isaac 
284947c6ae99SBarry Smith /*@
285001729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
28519a42bb27SBarry Smith 
2852d083f849SBarry Smith     Neighbor-wise Collective on dm
28539a42bb27SBarry Smith 
28549a42bb27SBarry Smith     Input Parameters:
28559a42bb27SBarry Smith +   dm - the DM object
2856f6813fd5SJed Brown .   l - the local vector
28571eb28f2eSBarry 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.
28581eb28f2eSBarry Smith -   g - the global vector
28599a42bb27SBarry Smith 
286095452b02SPatrick Sanan     Notes:
286195452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
286284330215SMatthew 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.
28639a42bb27SBarry Smith 
286401729b5cSPatrick Sanan     Level: intermediate
28659a42bb27SBarry Smith 
286601729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
28679a42bb27SBarry Smith 
28689a42bb27SBarry Smith @*/
28697087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
28709a42bb27SBarry Smith {
28717128ae9fSMatthew G Knepley   PetscSF                 sf;
287284330215SMatthew G. Knepley   PetscSection            s, gs;
2873d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2874ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2875ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2876ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2877fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
287884330215SMatthew G. Knepley   PetscErrorCode          ierr;
2879d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
28809a42bb27SBarry Smith 
28819a42bb27SBarry Smith   PetscFunctionBegin;
2882171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2883d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2884d4d07f1eSToby Isaac     if (link->beginhook) {
2885d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2886d4d07f1eSToby Isaac     }
2887d4d07f1eSToby Isaac   }
28884c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
28891bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
289092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
28917128ae9fSMatthew G Knepley   switch (mode) {
28927128ae9fSMatthew G Knepley   case INSERT_VALUES:
28937128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2894304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
289584330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
28967128ae9fSMatthew G Knepley   case ADD_VALUES:
28977128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2898304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
289984330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
29007128ae9fSMatthew G Knepley   default:
290182f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
29027128ae9fSMatthew G Knepley   }
2903ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2904ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2905ca3d3a14SMatthew G. Knepley     if (transform) {
2906ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2907ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2908ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2909ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2910fa88e482SJed Brown     } else if (isInsert) {
2911ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2912fa88e482SJed Brown     } else {
2913a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2914fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2915ca3d3a14SMatthew G. Knepley     }
2916fa88e482SJed Brown     if (s && isInsert) {
29177128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2918fa88e482SJed Brown     } else {
2919a256111fSJunchao Zhang       ierr = VecGetArrayAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2920fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2921fa88e482SJed Brown     }
2922ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2923d0295fc0SJunchao Zhang       ierr = PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM);CHKERRQ(ierr);
292484330215SMatthew G. Knepley     } else if (s && isInsert) {
292584330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
292684330215SMatthew G. Knepley 
2927e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
292884330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
292984330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
293084330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2931b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
293284330215SMatthew G. Knepley 
293384330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
293403442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
293584330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2936b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
293784330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
293884330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2939b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
294003442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2941b3b16f48SMatthew G. Knepley         if (dof != gdof) SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %d dof: %d gdof: %d cdof: %d gcdof: %d", p, dof, gdof, cdof, gcdof);
2942b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2943b3b16f48SMatthew G. Knepley         if (!gcdof) {
294484330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2945b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2946b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
294784330215SMatthew G. Knepley           const PetscInt *cdofs;
294884330215SMatthew G. Knepley           PetscInt        cind = 0;
294984330215SMatthew G. Knepley 
295084330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2951b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
295284330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2953b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
295484330215SMatthew G. Knepley           }
2955b3b16f48SMatthew G. Knepley         } else SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %d dof: %d gdof: %d cdof: %d gcdof: %d", p, dof, gdof, cdof, gcdof);
295684330215SMatthew G. Knepley       }
2957ca3d3a14SMatthew G. Knepley     }
2958fa88e482SJed Brown     if (g_inplace) {
2959a256111fSJunchao Zhang       ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
2960fa88e482SJed Brown     } else {
29617128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2962fa88e482SJed Brown     }
2963ca3d3a14SMatthew G. Knepley     if (transform) {
2964ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2965ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2966fa88e482SJed Brown     } else if (l_inplace) {
2967a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
2968ca3d3a14SMatthew G. Knepley     } else {
2969ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2970ca3d3a14SMatthew G. Knepley     }
29717128ae9fSMatthew G Knepley   } else {
2972b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2973843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
29747128ae9fSMatthew G Knepley   }
29759a42bb27SBarry Smith   PetscFunctionReturn(0);
29769a42bb27SBarry Smith }
29779a42bb27SBarry Smith 
29789a42bb27SBarry Smith /*@
29799a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
298047c6ae99SBarry Smith 
2981d083f849SBarry Smith     Neighbor-wise Collective on dm
298247c6ae99SBarry Smith 
298347c6ae99SBarry Smith     Input Parameters:
298447c6ae99SBarry Smith +   dm - the DM object
2985f6813fd5SJed Brown .   l - the local vector
298647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2987f6813fd5SJed Brown -   g - the global vector
298847c6ae99SBarry Smith 
298901729b5cSPatrick Sanan     Level: intermediate
299047c6ae99SBarry Smith 
2991e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
299247c6ae99SBarry Smith 
299347c6ae99SBarry Smith @*/
29947087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
299547c6ae99SBarry Smith {
29967128ae9fSMatthew G Knepley   PetscSF                 sf;
299784330215SMatthew G. Knepley   PetscSection            s;
2998d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2999ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
300084330215SMatthew G. Knepley   PetscErrorCode          ierr;
300147c6ae99SBarry Smith 
300247c6ae99SBarry Smith   PetscFunctionBegin;
3003171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30041bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
300592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
30067128ae9fSMatthew G Knepley   switch (mode) {
30077128ae9fSMatthew G Knepley   case INSERT_VALUES:
30087128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
300984330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
30107128ae9fSMatthew G Knepley   case ADD_VALUES:
30117128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
301284330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
30137128ae9fSMatthew G Knepley   default:
301482f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
30157128ae9fSMatthew G Knepley   }
301684330215SMatthew G. Knepley   if (sf && !isInsert) {
3017ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3018ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3019ca3d3a14SMatthew G. Knepley     Vec                tmpl;
302084330215SMatthew G. Knepley 
3021ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
3022ca3d3a14SMatthew G. Knepley     if (transform) {
3023ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
3024ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
3025ca3d3a14SMatthew G. Knepley     } else {
3026a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, NULL);CHKERRQ(ierr);
3027ca3d3a14SMatthew G. Knepley     }
3028a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(g, &gArray, NULL);CHKERRQ(ierr);
3029a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
3030ca3d3a14SMatthew G. Knepley     if (transform) {
3031ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
3032ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
3033ca3d3a14SMatthew G. Knepley     } else {
3034a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
3035ca3d3a14SMatthew G. Knepley     }
3036a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
303784330215SMatthew G. Knepley   } else if (s && isInsert) {
30387128ae9fSMatthew G Knepley   } else {
3039b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
3040843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
30417128ae9fSMatthew G Knepley   }
3042d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
3043d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
3044d4d07f1eSToby Isaac   }
304547c6ae99SBarry Smith   PetscFunctionReturn(0);
304647c6ae99SBarry Smith }
304747c6ae99SBarry Smith 
3048f089877aSRichard Tran Mills /*@
3049bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
3050bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3051d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
3052f089877aSRichard Tran Mills 
3053d083f849SBarry Smith    Neighbor-wise Collective on dm
3054f089877aSRichard Tran Mills 
3055f089877aSRichard Tran Mills    Input Parameters:
3056f089877aSRichard Tran Mills +  dm - the DM object
3057bc0a1609SRichard Tran Mills .  g - the original local vector
3058bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3059f089877aSRichard Tran Mills 
3060bc0a1609SRichard Tran Mills    Output Parameter:
3061bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3062f089877aSRichard Tran Mills 
3063f089877aSRichard Tran Mills    Level: intermediate
3064f089877aSRichard Tran Mills 
3065bc0a1609SRichard Tran Mills    Notes:
3066bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3067bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3068bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3069bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3070bc0a1609SRichard Tran Mills 
3071bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3072f089877aSRichard Tran Mills 
3073f089877aSRichard Tran Mills @*/
3074f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
3075f089877aSRichard Tran Mills {
3076f089877aSRichard Tran Mills   PetscErrorCode          ierr;
3077f089877aSRichard Tran Mills 
3078f089877aSRichard Tran Mills   PetscFunctionBegin;
3079f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3080bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3081f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
3082f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3083f089877aSRichard Tran Mills }
3084f089877aSRichard Tran Mills 
3085f089877aSRichard Tran Mills /*@
3086bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
3087bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3088d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
3089f089877aSRichard Tran Mills 
3090d083f849SBarry Smith    Neighbor-wise Collective on dm
3091f089877aSRichard Tran Mills 
3092f089877aSRichard Tran Mills    Input Parameters:
3093bc0a1609SRichard Tran Mills +  da - the DM object
3094bc0a1609SRichard Tran Mills .  g - the original local vector
3095bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3096f089877aSRichard Tran Mills 
3097bc0a1609SRichard Tran Mills    Output Parameter:
3098bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3099f089877aSRichard Tran Mills 
3100f089877aSRichard Tran Mills    Level: intermediate
3101f089877aSRichard Tran Mills 
3102bc0a1609SRichard Tran Mills    Notes:
3103bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3104bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3105bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3106bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3107bc0a1609SRichard Tran Mills 
3108bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3109f089877aSRichard Tran Mills 
3110f089877aSRichard Tran Mills @*/
3111f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
3112f089877aSRichard Tran Mills {
3113f089877aSRichard Tran Mills   PetscErrorCode          ierr;
3114f089877aSRichard Tran Mills 
3115f089877aSRichard Tran Mills   PetscFunctionBegin;
3116f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3117bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3118f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
3119f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3120f089877aSRichard Tran Mills }
3121f089877aSRichard Tran Mills 
312247c6ae99SBarry Smith /*@
312347c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
312447c6ae99SBarry Smith 
3125d083f849SBarry Smith     Collective on dm
312647c6ae99SBarry Smith 
3127d8d19677SJose E. Roman     Input Parameters:
312847c6ae99SBarry Smith +   dm - the DM object
312991d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
313047c6ae99SBarry Smith 
313147c6ae99SBarry Smith     Output Parameter:
313247c6ae99SBarry Smith .   dmc - the coarsened DM
313347c6ae99SBarry Smith 
313447c6ae99SBarry Smith     Level: developer
313547c6ae99SBarry Smith 
3136e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
313747c6ae99SBarry Smith 
313847c6ae99SBarry Smith @*/
31397087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
314047c6ae99SBarry Smith {
314147c6ae99SBarry Smith   PetscErrorCode    ierr;
3142b17ce1afSJed Brown   DMCoarsenHookLink link;
314347c6ae99SBarry Smith 
314447c6ae99SBarry Smith   PetscFunctionBegin;
3145171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3146b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
314747a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
314847c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
3149b9d85ea2SLisandro Dalcin   if (*dmc) {
3150a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
3151a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
315243842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
31538cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
3154644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
31550598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3156656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
3157e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
3158b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
3159b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
3160b17ce1afSJed Brown     }
3161b9d85ea2SLisandro Dalcin   }
316247a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
3163b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3164b17ce1afSJed Brown   PetscFunctionReturn(0);
3165b17ce1afSJed Brown }
3166b17ce1afSJed Brown 
3167bb9467b5SJed Brown /*@C
3168b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3169b17ce1afSJed Brown 
3170b17ce1afSJed Brown    Logically Collective
3171b17ce1afSJed Brown 
31724165533cSJose E. Roman    Input Parameters:
3173b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3174b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3175b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
31760298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3177b17ce1afSJed Brown 
3178b17ce1afSJed Brown    Calling sequence of coarsenhook:
3179b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3180b17ce1afSJed Brown 
3181b17ce1afSJed Brown +  fine - fine level DM
3182b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3183b17ce1afSJed Brown -  ctx - optional user-defined function context
3184b17ce1afSJed Brown 
3185b17ce1afSJed Brown    Calling sequence for restricthook:
3186c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3187b17ce1afSJed Brown 
3188b17ce1afSJed Brown +  fine - fine level DM
3189b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3190c833c3b5SJed Brown .  rscale - scaling vector for restriction
3191c833c3b5SJed Brown .  inject - matrix restricting by injection
3192b17ce1afSJed Brown .  coarse - coarse level DM to update
3193b17ce1afSJed Brown -  ctx - optional user-defined function context
3194b17ce1afSJed Brown 
3195b17ce1afSJed Brown    Level: advanced
3196b17ce1afSJed Brown 
3197b17ce1afSJed Brown    Notes:
3198b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3199b17ce1afSJed Brown 
3200b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3201b17ce1afSJed Brown 
3202b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3203b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3204b17ce1afSJed Brown 
3205bb9467b5SJed Brown    This function is currently not available from Fortran.
3206bb9467b5SJed Brown 
3207dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3208b17ce1afSJed Brown @*/
3209b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3210b17ce1afSJed Brown {
3211b17ce1afSJed Brown   PetscErrorCode    ierr;
3212b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3213b17ce1afSJed Brown 
3214b17ce1afSJed Brown   PetscFunctionBegin;
3215b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
32161e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
32171e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
32181e3d8eccSJed Brown   }
321995dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
3220b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3221b17ce1afSJed Brown   link->restricthook = restricthook;
3222b17ce1afSJed Brown   link->ctx          = ctx;
32230298fd71SBarry Smith   link->next         = NULL;
3224b17ce1afSJed Brown   *p                 = link;
3225b17ce1afSJed Brown   PetscFunctionReturn(0);
3226b17ce1afSJed Brown }
3227b17ce1afSJed Brown 
3228dc822a44SJed Brown /*@C
3229dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3230dc822a44SJed Brown 
3231dc822a44SJed Brown    Logically Collective
3232dc822a44SJed Brown 
32334165533cSJose E. Roman    Input Parameters:
3234dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3235dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3236dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3237dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3238dc822a44SJed Brown 
3239dc822a44SJed Brown    Level: advanced
3240dc822a44SJed Brown 
3241dc822a44SJed Brown    Notes:
3242dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3243dc822a44SJed Brown 
3244dc822a44SJed Brown    This function is currently not available from Fortran.
3245dc822a44SJed Brown 
3246dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3247dc822a44SJed Brown @*/
3248dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3249dc822a44SJed Brown {
3250dc822a44SJed Brown   PetscErrorCode    ierr;
3251dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3252dc822a44SJed Brown 
3253dc822a44SJed Brown   PetscFunctionBegin;
3254dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3255dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3256dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3257dc822a44SJed Brown       link = *p;
3258dc822a44SJed Brown       *p = link->next;
3259dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3260dc822a44SJed Brown       break;
3261dc822a44SJed Brown     }
3262dc822a44SJed Brown   }
3263dc822a44SJed Brown   PetscFunctionReturn(0);
3264dc822a44SJed Brown }
3265dc822a44SJed Brown 
3266b17ce1afSJed Brown /*@
3267b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3268b17ce1afSJed Brown 
3269b17ce1afSJed Brown    Collective if any hooks are
3270b17ce1afSJed Brown 
32714165533cSJose E. Roman    Input Parameters:
3272b17ce1afSJed Brown +  fine - finer DM to use as a base
3273b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3274e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3275b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3276e91eccc2SStefano Zampini -  coarse - coarser DM to update
3277b17ce1afSJed Brown 
3278b17ce1afSJed Brown    Level: developer
3279b17ce1afSJed Brown 
3280b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3281b17ce1afSJed Brown @*/
3282b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3283b17ce1afSJed Brown {
3284b17ce1afSJed Brown   PetscErrorCode    ierr;
3285b17ce1afSJed Brown   DMCoarsenHookLink link;
3286b17ce1afSJed Brown 
3287b17ce1afSJed Brown   PetscFunctionBegin;
3288b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
32898865f1eaSKarl Rupp     if (link->restricthook) {
32908865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
32918865f1eaSKarl Rupp     }
3292b17ce1afSJed Brown   }
329347c6ae99SBarry Smith   PetscFunctionReturn(0);
329447c6ae99SBarry Smith }
329547c6ae99SBarry Smith 
3296bb9467b5SJed Brown /*@C
3297be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
32985dbd56e3SPeter Brune 
3299d083f849SBarry Smith    Logically Collective on global
33005dbd56e3SPeter Brune 
33014165533cSJose E. Roman    Input Parameters:
33025dbd56e3SPeter Brune +  global - global DM
3303ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
33045dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
33050298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
33065dbd56e3SPeter Brune 
3307ec4806b8SPeter Brune    Calling sequence for ddhook:
3308ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3309ec4806b8SPeter Brune 
3310ec4806b8SPeter Brune +  global - global DM
3311ec4806b8SPeter Brune .  block  - block DM
3312ec4806b8SPeter Brune -  ctx - optional user-defined function context
3313ec4806b8SPeter Brune 
33145dbd56e3SPeter Brune    Calling sequence for restricthook:
3315ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
33165dbd56e3SPeter Brune 
33175dbd56e3SPeter Brune +  global - global DM
33185dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
33195dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3320ec4806b8SPeter Brune .  block  - block DM
33215dbd56e3SPeter Brune -  ctx - optional user-defined function context
33225dbd56e3SPeter Brune 
33235dbd56e3SPeter Brune    Level: advanced
33245dbd56e3SPeter Brune 
33255dbd56e3SPeter Brune    Notes:
3326ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
33275dbd56e3SPeter Brune 
33285dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
33295dbd56e3SPeter Brune 
33305dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3331ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
33325dbd56e3SPeter Brune 
3333bb9467b5SJed Brown    This function is currently not available from Fortran.
3334bb9467b5SJed Brown 
33355dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
33365dbd56e3SPeter Brune @*/
3337be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
33385dbd56e3SPeter Brune {
33395dbd56e3SPeter Brune   PetscErrorCode      ierr;
3340be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
33415dbd56e3SPeter Brune 
33425dbd56e3SPeter Brune   PetscFunctionBegin;
33435dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3344b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3345b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3346b3a6b972SJed Brown   }
334795dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
33485dbd56e3SPeter Brune   link->restricthook = restricthook;
3349be081cd6SPeter Brune   link->ddhook       = ddhook;
33505dbd56e3SPeter Brune   link->ctx          = ctx;
33510298fd71SBarry Smith   link->next         = NULL;
33525dbd56e3SPeter Brune   *p                 = link;
33535dbd56e3SPeter Brune   PetscFunctionReturn(0);
33545dbd56e3SPeter Brune }
33555dbd56e3SPeter Brune 
3356b3a6b972SJed Brown /*@C
3357b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3358b3a6b972SJed Brown 
3359b3a6b972SJed Brown    Logically Collective
3360b3a6b972SJed Brown 
33614165533cSJose E. Roman    Input Parameters:
3362b3a6b972SJed Brown +  global - global DM
3363b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3364b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3365b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3366b3a6b972SJed Brown 
3367b3a6b972SJed Brown    Level: advanced
3368b3a6b972SJed Brown 
3369b3a6b972SJed Brown    Notes:
3370b3a6b972SJed Brown 
3371b3a6b972SJed Brown    This function is currently not available from Fortran.
3372b3a6b972SJed Brown 
3373b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3374b3a6b972SJed Brown @*/
3375b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3376b3a6b972SJed Brown {
3377b3a6b972SJed Brown   PetscErrorCode      ierr;
3378b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3379b3a6b972SJed Brown 
3380b3a6b972SJed Brown   PetscFunctionBegin;
3381b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3382b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3383b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3384b3a6b972SJed Brown       link = *p;
3385b3a6b972SJed Brown       *p = link->next;
3386b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3387b3a6b972SJed Brown       break;
3388b3a6b972SJed Brown     }
3389b3a6b972SJed Brown   }
3390b3a6b972SJed Brown   PetscFunctionReturn(0);
3391b3a6b972SJed Brown }
3392b3a6b972SJed Brown 
33935dbd56e3SPeter Brune /*@
3394be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
33955dbd56e3SPeter Brune 
33965dbd56e3SPeter Brune    Collective if any hooks are
33975dbd56e3SPeter Brune 
33984165533cSJose E. Roman    Input Parameters:
33995dbd56e3SPeter Brune +  fine - finer DM to use as a base
3400be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3401be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
34025dbd56e3SPeter Brune -  coarse - coarer DM to update
34035dbd56e3SPeter Brune 
34045dbd56e3SPeter Brune    Level: developer
34055dbd56e3SPeter Brune 
34065dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
34075dbd56e3SPeter Brune @*/
3408be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
34095dbd56e3SPeter Brune {
34105dbd56e3SPeter Brune   PetscErrorCode      ierr;
3411be081cd6SPeter Brune   DMSubDomainHookLink link;
34125dbd56e3SPeter Brune 
34135dbd56e3SPeter Brune   PetscFunctionBegin;
3414be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
34158865f1eaSKarl Rupp     if (link->restricthook) {
34168865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
34178865f1eaSKarl Rupp     }
34185dbd56e3SPeter Brune   }
34195dbd56e3SPeter Brune   PetscFunctionReturn(0);
34205dbd56e3SPeter Brune }
34215dbd56e3SPeter Brune 
34225fe1f584SPeter Brune /*@
34236a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
34245fe1f584SPeter Brune 
34255fe1f584SPeter Brune     Not Collective
34265fe1f584SPeter Brune 
34275fe1f584SPeter Brune     Input Parameter:
34285fe1f584SPeter Brune .   dm - the DM object
34295fe1f584SPeter Brune 
34305fe1f584SPeter Brune     Output Parameter:
34316a7d9d85SPeter Brune .   level - number of coarsenings
34325fe1f584SPeter Brune 
34335fe1f584SPeter Brune     Level: developer
34345fe1f584SPeter Brune 
34356a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
34365fe1f584SPeter Brune 
34375fe1f584SPeter Brune @*/
34385fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
34395fe1f584SPeter Brune {
34405fe1f584SPeter Brune   PetscFunctionBegin;
34415fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3442b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
34435fe1f584SPeter Brune   *level = dm->leveldown;
34445fe1f584SPeter Brune   PetscFunctionReturn(0);
34455fe1f584SPeter Brune }
34465fe1f584SPeter Brune 
34479a64c4a8SMatthew G. Knepley /*@
34489a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
34499a64c4a8SMatthew G. Knepley 
34509a64c4a8SMatthew G. Knepley     Not Collective
34519a64c4a8SMatthew G. Knepley 
34529a64c4a8SMatthew G. Knepley     Input Parameters:
34539a64c4a8SMatthew G. Knepley +   dm - the DM object
34549a64c4a8SMatthew G. Knepley -   level - number of coarsenings
34559a64c4a8SMatthew G. Knepley 
34569a64c4a8SMatthew G. Knepley     Level: developer
34579a64c4a8SMatthew G. Knepley 
34589a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
34599a64c4a8SMatthew G. Knepley @*/
34609a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
34619a64c4a8SMatthew G. Knepley {
34629a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
34639a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34649a64c4a8SMatthew G. Knepley   dm->leveldown = level;
34659a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
34669a64c4a8SMatthew G. Knepley }
34679a64c4a8SMatthew G. Knepley 
346847c6ae99SBarry Smith /*@C
346947c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
347047c6ae99SBarry Smith 
3471d083f849SBarry Smith     Collective on dm
347247c6ae99SBarry Smith 
3473d8d19677SJose E. Roman     Input Parameters:
347447c6ae99SBarry Smith +   dm - the DM object
347547c6ae99SBarry Smith -   nlevels - the number of levels of refinement
347647c6ae99SBarry Smith 
347747c6ae99SBarry Smith     Output Parameter:
347847c6ae99SBarry Smith .   dmf - the refined DM hierarchy
347947c6ae99SBarry Smith 
348047c6ae99SBarry Smith     Level: developer
348147c6ae99SBarry Smith 
3482e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
348347c6ae99SBarry Smith 
348447c6ae99SBarry Smith @*/
34857087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
348647c6ae99SBarry Smith {
348747c6ae99SBarry Smith   PetscErrorCode ierr;
348847c6ae99SBarry Smith 
348947c6ae99SBarry Smith   PetscFunctionBegin;
3490171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3491ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
349247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3493b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
349447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
349547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
349647c6ae99SBarry Smith   } else if (dm->ops->refine) {
349747c6ae99SBarry Smith     PetscInt i;
349847c6ae99SBarry Smith 
3499ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
350047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3501ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
350247c6ae99SBarry Smith     }
3503ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
350447c6ae99SBarry Smith   PetscFunctionReturn(0);
350547c6ae99SBarry Smith }
350647c6ae99SBarry Smith 
350747c6ae99SBarry Smith /*@C
350847c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
350947c6ae99SBarry Smith 
3510d083f849SBarry Smith     Collective on dm
351147c6ae99SBarry Smith 
3512d8d19677SJose E. Roman     Input Parameters:
351347c6ae99SBarry Smith +   dm - the DM object
351447c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
351547c6ae99SBarry Smith 
351647c6ae99SBarry Smith     Output Parameter:
351747c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
351847c6ae99SBarry Smith 
351947c6ae99SBarry Smith     Level: developer
352047c6ae99SBarry Smith 
3521e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
352247c6ae99SBarry Smith 
352347c6ae99SBarry Smith @*/
35247087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
352547c6ae99SBarry Smith {
352647c6ae99SBarry Smith   PetscErrorCode ierr;
352747c6ae99SBarry Smith 
352847c6ae99SBarry Smith   PetscFunctionBegin;
3529171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3530ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
353147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
353247c6ae99SBarry Smith   PetscValidPointer(dmc,3);
353347c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
353447c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
353547c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
353647c6ae99SBarry Smith     PetscInt i;
353747c6ae99SBarry Smith 
3538ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
353947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3540ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
354147c6ae99SBarry Smith     }
3542ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
354347c6ae99SBarry Smith   PetscFunctionReturn(0);
354447c6ae99SBarry Smith }
354547c6ae99SBarry Smith 
35461a266240SBarry Smith /*@C
35471a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
35481a266240SBarry Smith 
35491a266240SBarry Smith     Not Collective
35501a266240SBarry Smith 
35511a266240SBarry Smith     Input Parameters:
35521a266240SBarry Smith +   dm - the DM object
35531a266240SBarry Smith -   destroy - the destroy function
35541a266240SBarry Smith 
35551a266240SBarry Smith     Level: intermediate
35561a266240SBarry Smith 
3557e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
35581a266240SBarry Smith 
3559f07f9ceaSJed Brown @*/
35601a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
35611a266240SBarry Smith {
35621a266240SBarry Smith   PetscFunctionBegin;
3563171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35641a266240SBarry Smith   dm->ctxdestroy = destroy;
35651a266240SBarry Smith   PetscFunctionReturn(0);
35661a266240SBarry Smith }
35671a266240SBarry Smith 
3568b07ff414SBarry Smith /*@
35691b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
357047c6ae99SBarry Smith 
357147c6ae99SBarry Smith     Not Collective
357247c6ae99SBarry Smith 
357347c6ae99SBarry Smith     Input Parameters:
357447c6ae99SBarry Smith +   dm - the DM object
357547c6ae99SBarry Smith -   ctx - the user context
357647c6ae99SBarry Smith 
357747c6ae99SBarry Smith     Level: intermediate
357847c6ae99SBarry Smith 
3579e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
358047c6ae99SBarry Smith 
358147c6ae99SBarry Smith @*/
35821b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
358347c6ae99SBarry Smith {
358447c6ae99SBarry Smith   PetscFunctionBegin;
3585171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
358647c6ae99SBarry Smith   dm->ctx = ctx;
358747c6ae99SBarry Smith   PetscFunctionReturn(0);
358847c6ae99SBarry Smith }
358947c6ae99SBarry Smith 
359047c6ae99SBarry Smith /*@
35911b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
359247c6ae99SBarry Smith 
359347c6ae99SBarry Smith     Not Collective
359447c6ae99SBarry Smith 
359547c6ae99SBarry Smith     Input Parameter:
359647c6ae99SBarry Smith .   dm - the DM object
359747c6ae99SBarry Smith 
359847c6ae99SBarry Smith     Output Parameter:
359947c6ae99SBarry Smith .   ctx - the user context
360047c6ae99SBarry Smith 
360147c6ae99SBarry Smith     Level: intermediate
360247c6ae99SBarry Smith 
3603e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
360447c6ae99SBarry Smith 
360547c6ae99SBarry Smith @*/
36061b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
360747c6ae99SBarry Smith {
360847c6ae99SBarry Smith   PetscFunctionBegin;
3609171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36101b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
361147c6ae99SBarry Smith   PetscFunctionReturn(0);
361247c6ae99SBarry Smith }
361347c6ae99SBarry Smith 
361408da532bSDmitry Karpeev /*@C
3615df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
361608da532bSDmitry Karpeev 
3617d083f849SBarry Smith     Logically Collective on dm
361808da532bSDmitry Karpeev 
3619d8d19677SJose E. Roman     Input Parameters:
362008da532bSDmitry Karpeev +   dm - the DM object
36210298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
362208da532bSDmitry Karpeev 
362308da532bSDmitry Karpeev     Level: intermediate
362408da532bSDmitry Karpeev 
3625835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
362608da532bSDmitry Karpeev          DMSetJacobian()
362708da532bSDmitry Karpeev 
362808da532bSDmitry Karpeev @*/
362908da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
363008da532bSDmitry Karpeev {
363108da532bSDmitry Karpeev   PetscFunctionBegin;
36325a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
363308da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
363408da532bSDmitry Karpeev   PetscFunctionReturn(0);
363508da532bSDmitry Karpeev }
363608da532bSDmitry Karpeev 
363708da532bSDmitry Karpeev /*@
363808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
363908da532bSDmitry Karpeev 
364008da532bSDmitry Karpeev     Not Collective
364108da532bSDmitry Karpeev 
364208da532bSDmitry Karpeev     Input Parameter:
364308da532bSDmitry Karpeev .   dm - the DM object to destroy
364408da532bSDmitry Karpeev 
364508da532bSDmitry Karpeev     Output Parameter:
364608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
364708da532bSDmitry Karpeev 
364808da532bSDmitry Karpeev     Level: developer
364908da532bSDmitry Karpeev 
365074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
365108da532bSDmitry Karpeev 
365208da532bSDmitry Karpeev @*/
365308da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
365408da532bSDmitry Karpeev {
365508da532bSDmitry Karpeev   PetscFunctionBegin;
36565a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3657534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
365808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
365908da532bSDmitry Karpeev   PetscFunctionReturn(0);
366008da532bSDmitry Karpeev }
366108da532bSDmitry Karpeev 
366208da532bSDmitry Karpeev /*@C
366308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
366408da532bSDmitry Karpeev 
3665d083f849SBarry Smith     Logically Collective on dm
366608da532bSDmitry Karpeev 
3667f899ff85SJose E. Roman     Input Parameter:
3668907376e6SBarry Smith .   dm - the DM object
366908da532bSDmitry Karpeev 
367008da532bSDmitry Karpeev     Output parameters:
367108da532bSDmitry Karpeev +   xl - lower bound
367208da532bSDmitry Karpeev -   xu - upper bound
367308da532bSDmitry Karpeev 
3674907376e6SBarry Smith     Level: advanced
3675907376e6SBarry Smith 
367695452b02SPatrick Sanan     Notes:
367795452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
367808da532bSDmitry Karpeev 
367974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
368008da532bSDmitry Karpeev 
368108da532bSDmitry Karpeev @*/
368208da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
368308da532bSDmitry Karpeev {
368408da532bSDmitry Karpeev   PetscErrorCode ierr;
36855fd66863SKarl Rupp 
368608da532bSDmitry Karpeev   PetscFunctionBegin;
36875a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
368808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
36895a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3690b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
369108da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
369208da532bSDmitry Karpeev   PetscFunctionReturn(0);
369308da532bSDmitry Karpeev }
369408da532bSDmitry Karpeev 
3695b0ae01b7SPeter Brune /*@
3696b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3697b0ae01b7SPeter Brune 
3698b0ae01b7SPeter Brune     Not Collective
3699b0ae01b7SPeter Brune 
3700b0ae01b7SPeter Brune     Input Parameter:
3701b0ae01b7SPeter Brune .   dm - the DM object
3702b0ae01b7SPeter Brune 
3703b0ae01b7SPeter Brune     Output Parameter:
3704b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3705b0ae01b7SPeter Brune 
3706b0ae01b7SPeter Brune     Level: developer
3707b0ae01b7SPeter Brune 
37081565f0a7SPatrick Sanan .seealso DMCreateColoring()
3709b0ae01b7SPeter Brune 
3710b0ae01b7SPeter Brune @*/
3711b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3712b0ae01b7SPeter Brune {
3713b0ae01b7SPeter Brune   PetscFunctionBegin;
37145a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3715534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3716b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3717b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3718b0ae01b7SPeter Brune }
3719b0ae01b7SPeter Brune 
37203ad4599aSBarry Smith /*@
37213ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
37223ad4599aSBarry Smith 
37233ad4599aSBarry Smith     Not Collective
37243ad4599aSBarry Smith 
37253ad4599aSBarry Smith     Input Parameter:
37263ad4599aSBarry Smith .   dm - the DM object
37273ad4599aSBarry Smith 
37283ad4599aSBarry Smith     Output Parameter:
37293ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
37303ad4599aSBarry Smith 
37313ad4599aSBarry Smith     Level: developer
37323ad4599aSBarry Smith 
37331565f0a7SPatrick Sanan .seealso DMCreateRestriction()
37343ad4599aSBarry Smith 
37353ad4599aSBarry Smith @*/
37363ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
37373ad4599aSBarry Smith {
37383ad4599aSBarry Smith   PetscFunctionBegin;
37395a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3740534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37413ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
37423ad4599aSBarry Smith   PetscFunctionReturn(0);
37433ad4599aSBarry Smith }
37443ad4599aSBarry Smith 
3745a7058e45SLawrence Mitchell /*@
3746a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3747a7058e45SLawrence Mitchell 
3748a7058e45SLawrence Mitchell     Not Collective
3749a7058e45SLawrence Mitchell 
3750a7058e45SLawrence Mitchell     Input Parameter:
3751a7058e45SLawrence Mitchell .   dm - the DM object
3752a7058e45SLawrence Mitchell 
3753a7058e45SLawrence Mitchell     Output Parameter:
3754a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3755a7058e45SLawrence Mitchell 
3756a7058e45SLawrence Mitchell     Level: developer
3757a7058e45SLawrence Mitchell 
37581565f0a7SPatrick Sanan .seealso DMCreateInjection()
3759a7058e45SLawrence Mitchell 
3760a7058e45SLawrence Mitchell @*/
3761a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3762a7058e45SLawrence Mitchell {
37634a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
37645a84ad33SLisandro Dalcin 
3765a7058e45SLawrence Mitchell   PetscFunctionBegin;
37665a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3767534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37685a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
37694a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
37705a84ad33SLisandro Dalcin   } else {
37715a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
37725a84ad33SLisandro Dalcin   }
3773a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3774a7058e45SLawrence Mitchell }
3775a7058e45SLawrence Mitchell 
37760298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3777264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3778264ace61SBarry Smith 
3779264ace61SBarry Smith /*@C
3780264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3781264ace61SBarry Smith 
3782d083f849SBarry Smith   Collective on dm
3783264ace61SBarry Smith 
3784264ace61SBarry Smith   Input Parameters:
3785264ace61SBarry Smith + dm     - The DM object
3786264ace61SBarry Smith - method - The name of the DM type
3787264ace61SBarry Smith 
3788264ace61SBarry Smith   Options Database Key:
3789264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3790264ace61SBarry Smith 
3791264ace61SBarry Smith   Notes:
3792e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3793264ace61SBarry Smith 
3794264ace61SBarry Smith   Level: intermediate
3795264ace61SBarry Smith 
3796264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3797264ace61SBarry Smith @*/
379819fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3799264ace61SBarry Smith {
3800264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3801264ace61SBarry Smith   PetscBool      match;
3802264ace61SBarry Smith   PetscErrorCode ierr;
3803264ace61SBarry Smith 
3804264ace61SBarry Smith   PetscFunctionBegin;
3805264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3806251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3807264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3808264ace61SBarry Smith 
38090f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
38101c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3811ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3812264ace61SBarry Smith 
3813264ace61SBarry Smith   if (dm->ops->destroy) {
3814264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3815264ace61SBarry Smith   }
3816d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3817264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3818d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3819264ace61SBarry Smith   PetscFunctionReturn(0);
3820264ace61SBarry Smith }
3821264ace61SBarry Smith 
3822264ace61SBarry Smith /*@C
3823264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3824264ace61SBarry Smith 
3825264ace61SBarry Smith   Not Collective
3826264ace61SBarry Smith 
3827264ace61SBarry Smith   Input Parameter:
3828264ace61SBarry Smith . dm  - The DM
3829264ace61SBarry Smith 
3830264ace61SBarry Smith   Output Parameter:
3831264ace61SBarry Smith . type - The DM type name
3832264ace61SBarry Smith 
3833264ace61SBarry Smith   Level: intermediate
3834264ace61SBarry Smith 
3835264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3836264ace61SBarry Smith @*/
383719fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3838264ace61SBarry Smith {
3839264ace61SBarry Smith   PetscErrorCode ierr;
3840264ace61SBarry Smith 
3841264ace61SBarry Smith   PetscFunctionBegin;
3842264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3843c959eef4SJed Brown   PetscValidPointer(type,2);
3844607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3845264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3846264ace61SBarry Smith   PetscFunctionReturn(0);
3847264ace61SBarry Smith }
3848264ace61SBarry Smith 
384967a56275SMatthew G Knepley /*@C
385067a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
385167a56275SMatthew G Knepley 
3852d083f849SBarry Smith   Collective on dm
385367a56275SMatthew G Knepley 
385467a56275SMatthew G Knepley   Input Parameters:
385567a56275SMatthew G Knepley + dm - the DM
385667a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
385767a56275SMatthew G Knepley 
385867a56275SMatthew G Knepley   Output Parameter:
385967a56275SMatthew G Knepley . M - pointer to new DM
386067a56275SMatthew G Knepley 
386167a56275SMatthew G Knepley   Notes:
386267a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
386367a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
386467a56275SMatthew G Knepley   of the input DM.
386567a56275SMatthew G Knepley 
386667a56275SMatthew G Knepley   Level: intermediate
386767a56275SMatthew G Knepley 
386867a56275SMatthew G Knepley .seealso: DMCreate()
386967a56275SMatthew G Knepley @*/
387019fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
387167a56275SMatthew G Knepley {
387267a56275SMatthew G Knepley   DM             B;
387367a56275SMatthew G Knepley   char           convname[256];
3874c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
387567a56275SMatthew G Knepley   PetscErrorCode ierr;
387667a56275SMatthew G Knepley 
387767a56275SMatthew G Knepley   PetscFunctionBegin;
387867a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
387967a56275SMatthew G Knepley   PetscValidType(dm,1);
388067a56275SMatthew G Knepley   PetscValidPointer(M,3);
3881251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3882c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3883c067b6caSMatthew G. Knepley   if (sametype) {
3884c067b6caSMatthew G. Knepley     *M   = dm;
3885c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3886c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3887c067b6caSMatthew G. Knepley   } else {
38880298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
388967a56275SMatthew G Knepley 
389067a56275SMatthew G Knepley     /*
389167a56275SMatthew G Knepley        Order of precedence:
389267a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
389367a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
389467a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
389567a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
389667a56275SMatthew G Knepley        5) Use a really basic converter.
389767a56275SMatthew G Knepley     */
389867a56275SMatthew G Knepley 
389967a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3900a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3901a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3902a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3903a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3904a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
39050005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
390667a56275SMatthew G Knepley     if (conv) goto foundconv;
390767a56275SMatthew G Knepley 
390867a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
390982f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
391067a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3911a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3912a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3913a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3914a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3915a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
39160005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
391767a56275SMatthew G Knepley     if (conv) {
3918fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
391967a56275SMatthew G Knepley       goto foundconv;
392067a56275SMatthew G Knepley     }
392167a56275SMatthew G Knepley 
392267a56275SMatthew G Knepley #if 0
392367a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
392467a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3925fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
392667a56275SMatthew G Knepley     if (conv) goto foundconv;
392767a56275SMatthew G Knepley 
392867a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
392967a56275SMatthew G Knepley     if (dm->ops->convert) {
393067a56275SMatthew G Knepley       conv = dm->ops->convert;
393167a56275SMatthew G Knepley     }
393267a56275SMatthew G Knepley     if (conv) goto foundconv;
393367a56275SMatthew G Knepley #endif
393467a56275SMatthew G Knepley 
393567a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
393682f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
393767a56275SMatthew G Knepley 
393867a56275SMatthew G Knepley foundconv:
393967a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
394067a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
394112fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
394290b157c4SStefano Zampini     {
394390b157c4SStefano Zampini       PetscBool             isper;
394412fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
394512fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
394690b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
394790b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
3948c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
3949a587d139SMark       ierr = PetscFree((*M)->vectype);CHKERRQ(ierr);
3950a587d139SMark       ierr = PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype);CHKERRQ(ierr);
3951a587d139SMark       ierr = PetscFree((*M)->mattype);CHKERRQ(ierr);
3952a587d139SMark       ierr = PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype);CHKERRQ(ierr);
395312fa691eSMatthew G. Knepley     }
395467a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
395567a56275SMatthew G Knepley   }
395667a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
395767a56275SMatthew G Knepley   PetscFunctionReturn(0);
395867a56275SMatthew G Knepley }
3959264ace61SBarry Smith 
3960264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3961264ace61SBarry Smith 
3962264ace61SBarry Smith /*@C
39631c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
39641c84c290SBarry Smith 
39651c84c290SBarry Smith   Not Collective
39661c84c290SBarry Smith 
39671c84c290SBarry Smith   Input Parameters:
39681c84c290SBarry Smith + name        - The name of a new user-defined creation routine
39691c84c290SBarry Smith - create_func - The creation routine itself
39701c84c290SBarry Smith 
39711c84c290SBarry Smith   Notes:
39721c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
39731c84c290SBarry Smith 
39741c84c290SBarry Smith   Sample usage:
39751c84c290SBarry Smith .vb
3976bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
39771c84c290SBarry Smith .ve
39781c84c290SBarry Smith 
39791c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
39801c84c290SBarry Smith .vb
39811c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
39821c84c290SBarry Smith     DMSetType(DM,"my_da");
39831c84c290SBarry Smith .ve
39841c84c290SBarry Smith    or at runtime via the option
39851c84c290SBarry Smith .vb
39861c84c290SBarry Smith     -da_type my_da
39871c84c290SBarry Smith .ve
3988264ace61SBarry Smith 
3989264ace61SBarry Smith   Level: advanced
39901c84c290SBarry Smith 
3991bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
39921c84c290SBarry Smith 
3993264ace61SBarry Smith @*/
3994bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3995264ace61SBarry Smith {
3996264ace61SBarry Smith   PetscErrorCode ierr;
3997264ace61SBarry Smith 
3998264ace61SBarry Smith   PetscFunctionBegin;
39991d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
4000a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
4001264ace61SBarry Smith   PetscFunctionReturn(0);
4002264ace61SBarry Smith }
4003264ace61SBarry Smith 
4004b859378eSBarry Smith /*@C
400555849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
4006b859378eSBarry Smith 
4007d083f849SBarry Smith   Collective on viewer
4008b859378eSBarry Smith 
4009b859378eSBarry Smith   Input Parameters:
4010b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
4011b859378eSBarry Smith            some related function before a call to DMLoad().
4012b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
4013b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
4014b859378eSBarry Smith 
4015b859378eSBarry Smith    Level: intermediate
4016b859378eSBarry Smith 
4017b859378eSBarry Smith   Notes:
401855849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
4019b859378eSBarry Smith 
4020b859378eSBarry Smith   Notes for advanced users:
4021b859378eSBarry Smith   Most users should not need to know the details of the binary storage
4022b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
4023b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
4024b859378eSBarry Smith   format is
4025b859378eSBarry Smith .vb
4026b859378eSBarry Smith      has not yet been determined
4027b859378eSBarry Smith .ve
4028b859378eSBarry Smith 
4029b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
4030b859378eSBarry Smith @*/
4031b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
4032b859378eSBarry Smith {
40339331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
4034b859378eSBarry Smith   PetscErrorCode ierr;
4035b859378eSBarry Smith 
4036b859378eSBarry Smith   PetscFunctionBegin;
4037b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
4038b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4039fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
404032c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
40419331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
404258cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
40439331c7a4SMatthew G. Knepley   if (isbinary) {
40449331c7a4SMatthew G. Knepley     PetscInt classid;
40459331c7a4SMatthew G. Knepley     char     type[256];
4046b859378eSBarry Smith 
4047060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
40489200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
4049060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
405032c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
40519331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
40529331c7a4SMatthew G. Knepley   } else if (ishdf5) {
40539331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
40549331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
405558cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
4056b859378eSBarry Smith   PetscFunctionReturn(0);
4057b859378eSBarry Smith }
4058b859378eSBarry Smith 
4059b2e4378dSMatthew G. Knepley /*@
4060b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
4061b2e4378dSMatthew G. Knepley 
4062b2e4378dSMatthew G. Knepley   Not collective
4063b2e4378dSMatthew G. Knepley 
4064b2e4378dSMatthew G. Knepley   Input Parameter:
4065b2e4378dSMatthew G. Knepley . dm - the DM
4066b2e4378dSMatthew G. Knepley 
4067b2e4378dSMatthew G. Knepley   Output Parameters:
4068b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
4069b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
4070b2e4378dSMatthew G. Knepley 
4071b2e4378dSMatthew G. Knepley   Level: beginner
4072b2e4378dSMatthew G. Knepley 
4073b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
4074b2e4378dSMatthew G. Knepley 
4075b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
4076b2e4378dSMatthew G. Knepley @*/
4077b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
4078b2e4378dSMatthew G. Knepley {
4079b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
4080b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
4081b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
4082b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
4083b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
4084b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
4085b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
4086b2e4378dSMatthew G. Knepley 
4087b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4088b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4089b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4090b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
4091b2e4378dSMatthew G. Knepley   if (coords) {
4092b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
4093b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
4094b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
4095b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
4096b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
4097b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4098b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4099b2e4378dSMatthew G. Knepley       }
4100b2e4378dSMatthew G. Knepley     }
4101b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
4102b2e4378dSMatthew G. Knepley   } else {
4103b2e4378dSMatthew G. Knepley     PetscBool isda;
4104b2e4378dSMatthew G. Knepley 
4105b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
4106b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
4107b2e4378dSMatthew G. Knepley   }
4108b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
4109b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
4110b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4111b2e4378dSMatthew G. Knepley }
4112b2e4378dSMatthew G. Knepley 
4113b2e4378dSMatthew G. Knepley /*@
4114b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
4115b2e4378dSMatthew G. Knepley 
4116b2e4378dSMatthew G. Knepley   Collective
4117b2e4378dSMatthew G. Knepley 
4118b2e4378dSMatthew G. Knepley   Input Parameter:
4119b2e4378dSMatthew G. Knepley . dm - the DM
4120b2e4378dSMatthew G. Knepley 
4121b2e4378dSMatthew G. Knepley   Output Parameters:
4122b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
4123b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
4124b2e4378dSMatthew G. Knepley 
4125b2e4378dSMatthew G. Knepley   Level: beginner
4126b2e4378dSMatthew G. Knepley 
4127b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
4128b2e4378dSMatthew G. Knepley @*/
4129b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
4130b2e4378dSMatthew G. Knepley {
4131b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
41326ce308c4SMatthew G. Knepley   PetscInt       cdim;
41336ce308c4SMatthew G. Knepley   PetscMPIInt    count;
4134b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
4135b2e4378dSMatthew G. Knepley 
4136b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4137b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4138b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4139b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
4140b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
4141820f2d46SBarry Smith   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4142820f2d46SBarry Smith   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4143b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4144b2e4378dSMatthew G. Knepley }
4145b2e4378dSMatthew G. Knepley 
41467da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41477da65231SMatthew G Knepley 
4148a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4149a6dfd86eSKarl Rupp {
41501d47ebbbSSatish Balay   PetscInt       f;
41511b30c384SMatthew G Knepley   PetscErrorCode ierr;
41521b30c384SMatthew G Knepley 
41537da65231SMatthew G Knepley   PetscFunctionBegin;
415474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
41551d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
415657622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
41577da65231SMatthew G Knepley   }
41587da65231SMatthew G Knepley   PetscFunctionReturn(0);
41597da65231SMatthew G Knepley }
41607da65231SMatthew G Knepley 
4161a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4162a6dfd86eSKarl Rupp {
41631b30c384SMatthew G Knepley   PetscInt       f, g;
41647da65231SMatthew G Knepley   PetscErrorCode ierr;
41657da65231SMatthew G Knepley 
41667da65231SMatthew G Knepley   PetscFunctionBegin;
416774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
41681d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
416974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
41701d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
4171e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
41727da65231SMatthew G Knepley     }
417374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
41747da65231SMatthew G Knepley   }
41757da65231SMatthew G Knepley   PetscFunctionReturn(0);
41767da65231SMatthew G Knepley }
4177e7c4fc90SDmitry Karpeev 
41786113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4179e759306cSMatthew G. Knepley {
41800c5b8624SToby Isaac   PetscInt          localSize, bs;
41810c5b8624SToby Isaac   PetscMPIInt       size;
41820c5b8624SToby Isaac   Vec               x, xglob;
41830c5b8624SToby Isaac   const PetscScalar *xarray;
4184e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
4185e759306cSMatthew G. Knepley 
4186e759306cSMatthew G. Knepley   PetscFunctionBegin;
4187ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRMPI(ierr);
4188e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
4189e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
41906113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
41910c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
41920c5b8624SToby Isaac   if (size > 1) {
41930c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
41940c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
41950c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
41960c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
41970c5b8624SToby Isaac   } else {
41980c5b8624SToby Isaac     xglob = x;
41990c5b8624SToby Isaac   }
42000c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
42010c5b8624SToby Isaac   if (size > 1) {
42020c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
42030c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
42040c5b8624SToby Isaac   }
4205e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
4206e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4207e759306cSMatthew G. Knepley }
4208e759306cSMatthew G. Knepley 
420988ed4aceSMatthew G Knepley /*@
42101bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4211061576a5SJed Brown 
4212061576a5SJed Brown   Input Parameter:
4213061576a5SJed Brown . dm - The DM
4214061576a5SJed Brown 
4215061576a5SJed Brown   Output Parameter:
4216061576a5SJed Brown . section - The PetscSection
4217061576a5SJed Brown 
4218061576a5SJed Brown   Options Database Keys:
4219061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4220061576a5SJed Brown 
4221061576a5SJed Brown   Level: advanced
4222061576a5SJed Brown 
4223061576a5SJed Brown   Notes:
4224061576a5SJed Brown   Use DMGetLocalSection() in new code.
4225061576a5SJed Brown 
4226061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4227061576a5SJed Brown 
4228061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
4229061576a5SJed Brown @*/
4230061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4231061576a5SJed Brown {
4232061576a5SJed Brown   PetscErrorCode ierr;
4233061576a5SJed Brown 
4234061576a5SJed Brown   PetscFunctionBegin;
4235061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
4236061576a5SJed Brown   PetscFunctionReturn(0);
4237061576a5SJed Brown }
4238061576a5SJed Brown 
4239061576a5SJed Brown /*@
4240061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
424188ed4aceSMatthew G Knepley 
424288ed4aceSMatthew G Knepley   Input Parameter:
424388ed4aceSMatthew G Knepley . dm - The DM
424488ed4aceSMatthew G Knepley 
424588ed4aceSMatthew G Knepley   Output Parameter:
424688ed4aceSMatthew G Knepley . section - The PetscSection
424788ed4aceSMatthew G Knepley 
4248e5893cccSMatthew G. Knepley   Options Database Keys:
4249e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4250e5893cccSMatthew G. Knepley 
425188ed4aceSMatthew G Knepley   Level: intermediate
425288ed4aceSMatthew G Knepley 
425388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
425488ed4aceSMatthew G Knepley 
4255061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
425688ed4aceSMatthew G Knepley @*/
4257061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
42580adebc6cSBarry Smith {
4259fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4260fd59a867SMatthew G. Knepley 
426188ed4aceSMatthew G Knepley   PetscFunctionBegin;
426288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
426388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42641bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4265e5e52638SMatthew G. Knepley     PetscInt d;
4266e5e52638SMatthew G. Knepley 
426745480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
426845480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject) dm;
426945480ffeSMatthew G. Knepley       PetscViewer       viewer;
427045480ffeSMatthew G. Knepley       PetscViewerFormat format;
427145480ffeSMatthew G. Knepley       PetscBool         flg;
427245480ffeSMatthew G. Knepley 
427345480ffeSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg);CHKERRQ(ierr);
427445480ffeSMatthew G. Knepley       if (flg) {ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);}
427545480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
427645480ffeSMatthew G. Knepley         ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);
427745480ffeSMatthew G. Knepley         if (flg) {ierr = PetscDSView(dm->probs[d].ds, viewer);CHKERRQ(ierr);}
427845480ffeSMatthew G. Knepley       }
427945480ffeSMatthew G. Knepley       if (flg) {
428045480ffeSMatthew G. Knepley         ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
428145480ffeSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
428245480ffeSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
428345480ffeSMatthew G. Knepley       }
428445480ffeSMatthew G. Knepley     }
42851bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
42861bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
42872f0f8703SMatthew G. Knepley   }
42881bb6d2a8SBarry Smith   *section = dm->localSection;
428988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
429088ed4aceSMatthew G Knepley }
429188ed4aceSMatthew G Knepley 
429288ed4aceSMatthew G Knepley /*@
42931bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4294061576a5SJed Brown 
4295061576a5SJed Brown   Input Parameters:
4296061576a5SJed Brown + dm - The DM
4297061576a5SJed Brown - section - The PetscSection
4298061576a5SJed Brown 
4299061576a5SJed Brown   Level: advanced
4300061576a5SJed Brown 
4301061576a5SJed Brown   Notes:
4302061576a5SJed Brown   Use DMSetLocalSection() in new code.
4303061576a5SJed Brown 
4304061576a5SJed Brown   Any existing Section will be destroyed
4305061576a5SJed Brown 
4306061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4307061576a5SJed Brown @*/
4308061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4309061576a5SJed Brown {
4310061576a5SJed Brown   PetscErrorCode ierr;
4311061576a5SJed Brown 
4312061576a5SJed Brown   PetscFunctionBegin;
4313061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4314061576a5SJed Brown   PetscFunctionReturn(0);
4315061576a5SJed Brown }
4316061576a5SJed Brown 
4317061576a5SJed Brown /*@
4318061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
431988ed4aceSMatthew G Knepley 
432088ed4aceSMatthew G Knepley   Input Parameters:
432188ed4aceSMatthew G Knepley + dm - The DM
432288ed4aceSMatthew G Knepley - section - The PetscSection
432388ed4aceSMatthew G Knepley 
432488ed4aceSMatthew G Knepley   Level: intermediate
432588ed4aceSMatthew G Knepley 
432688ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
432788ed4aceSMatthew G Knepley 
4328061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
432988ed4aceSMatthew G Knepley @*/
4330061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
43310adebc6cSBarry Smith {
4332c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4333af122d2aSMatthew G Knepley   PetscInt       f;
433488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
433588ed4aceSMatthew G Knepley 
433688ed4aceSMatthew G Knepley   PetscFunctionBegin;
433788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4338b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
43391d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
43401bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
43411bb6d2a8SBarry Smith   dm->localSection = section;
43421bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4343af122d2aSMatthew G Knepley   if (numFields) {
4344af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4345af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43460f21e855SMatthew G. Knepley       PetscObject disc;
4347af122d2aSMatthew G Knepley       const char *name;
4348af122d2aSMatthew G Knepley 
43491bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
435044a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
43510f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4352af122d2aSMatthew G Knepley     }
4353af122d2aSMatthew G Knepley   }
4354e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43551bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
435688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
435788ed4aceSMatthew G Knepley }
435888ed4aceSMatthew G Knepley 
43599435951eSToby Isaac /*@
4360b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
43619435951eSToby Isaac 
4362e228b242SToby Isaac   not collective
4363e228b242SToby Isaac 
43649435951eSToby Isaac   Input Parameter:
43659435951eSToby Isaac . dm - The DM
43669435951eSToby Isaac 
4367d8d19677SJose E. Roman   Output Parameters:
43689435951eSToby 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.
43699435951eSToby 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.
43709435951eSToby Isaac 
43719435951eSToby Isaac   Level: advanced
43729435951eSToby Isaac 
43739435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
43749435951eSToby Isaac 
43759435951eSToby Isaac .seealso: DMSetDefaultConstraints()
43769435951eSToby Isaac @*/
43779435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
43789435951eSToby Isaac {
43799435951eSToby Isaac   PetscErrorCode ierr;
43809435951eSToby Isaac 
43819435951eSToby Isaac   PetscFunctionBegin;
43829435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43839435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
438445a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
438545a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
43869435951eSToby Isaac   PetscFunctionReturn(0);
43879435951eSToby Isaac }
43889435951eSToby Isaac 
43899435951eSToby Isaac /*@
4390b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
43919435951eSToby Isaac 
43929435951eSToby 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().
43939435951eSToby Isaac 
43949435951eSToby 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.
43959435951eSToby Isaac 
4396e228b242SToby Isaac   collective on dm
4397e228b242SToby Isaac 
43989435951eSToby Isaac   Input Parameters:
43999435951eSToby Isaac + dm - The DM
4400e228b242SToby 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).
4401e228b242SToby 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).
44029435951eSToby Isaac 
44039435951eSToby Isaac   Level: advanced
44049435951eSToby Isaac 
44059435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
44069435951eSToby Isaac 
44079435951eSToby Isaac .seealso: DMGetDefaultConstraints()
44089435951eSToby Isaac @*/
44099435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
44109435951eSToby Isaac {
4411e228b242SToby Isaac   PetscMPIInt result;
44129435951eSToby Isaac   PetscErrorCode ierr;
44139435951eSToby Isaac 
44149435951eSToby Isaac   PetscFunctionBegin;
44159435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4416e228b242SToby Isaac   if (section) {
4417e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4418ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRMPI(ierr);
4419f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4420e228b242SToby Isaac   }
4421e228b242SToby Isaac   if (mat) {
4422e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4423ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRMPI(ierr);
4424f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4425e228b242SToby Isaac   }
44269435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
44279435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
44289435951eSToby Isaac   dm->defaultConstraintSection = section;
44299435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
44309435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
44319435951eSToby Isaac   dm->defaultConstraintMat = mat;
44329435951eSToby Isaac   PetscFunctionReturn(0);
44339435951eSToby Isaac }
44349435951eSToby Isaac 
4435497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4436507e4973SMatthew G. Knepley /*
4437507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4438507e4973SMatthew G. Knepley 
4439507e4973SMatthew G. Knepley   Input Parameters:
4440507e4973SMatthew G. Knepley + dm - The DM
4441507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4442507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4443507e4973SMatthew G. Knepley 
4444507e4973SMatthew G. Knepley   Level: intermediate
4445507e4973SMatthew G. Knepley 
44461bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4447507e4973SMatthew G. Knepley */
4448f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4449507e4973SMatthew G. Knepley {
4450507e4973SMatthew G. Knepley   MPI_Comm        comm;
4451507e4973SMatthew G. Knepley   PetscLayout     layout;
4452507e4973SMatthew G. Knepley   const PetscInt *ranges;
4453507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4454507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4455507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4456507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4457507e4973SMatthew G. Knepley 
4458507e4973SMatthew G. Knepley   PetscFunctionBegin;
4459507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4460507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4461ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
4462ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
4463507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4464507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4465507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4466507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4467507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4468507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4469507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4470507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4471f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4472507e4973SMatthew G. Knepley 
4473507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4474507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4475507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4476507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4477507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4478507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4479507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4480507e4973SMatthew 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;}
4481507e4973SMatthew 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;}
4482507e4973SMatthew G. Knepley     if (gdof < 0) {
4483507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4484507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4485507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4486507e4973SMatthew G. Knepley 
4487507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4488507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4489507e4973SMatthew 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;}
4490507e4973SMatthew G. Knepley       }
4491507e4973SMatthew G. Knepley     }
4492507e4973SMatthew G. Knepley   }
4493507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4494507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4495820f2d46SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRMPI(ierr);
4496507e4973SMatthew G. Knepley   if (!gvalid) {
4497507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4498507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4499507e4973SMatthew G. Knepley   }
4500507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4501507e4973SMatthew G. Knepley }
4502f741bcd2SMatthew G. Knepley #endif
4503507e4973SMatthew G. Knepley 
450488ed4aceSMatthew G Knepley /*@
4505e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
450688ed4aceSMatthew G Knepley 
4507d083f849SBarry Smith   Collective on dm
45088b1ab98fSJed Brown 
450988ed4aceSMatthew G Knepley   Input Parameter:
451088ed4aceSMatthew G Knepley . dm - The DM
451188ed4aceSMatthew G Knepley 
451288ed4aceSMatthew G Knepley   Output Parameter:
451388ed4aceSMatthew G Knepley . section - The PetscSection
451488ed4aceSMatthew G Knepley 
451588ed4aceSMatthew G Knepley   Level: intermediate
451688ed4aceSMatthew G Knepley 
451788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
451888ed4aceSMatthew G Knepley 
451992fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
452088ed4aceSMatthew G Knepley @*/
4521e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
45220adebc6cSBarry Smith {
452388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
452488ed4aceSMatthew G Knepley 
452588ed4aceSMatthew G Knepley   PetscFunctionBegin;
452688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
452788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
45281bb6d2a8SBarry Smith   if (!dm->globalSection) {
4529fd59a867SMatthew G. Knepley     PetscSection s;
4530fd59a867SMatthew G. Knepley 
453192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4532fd59a867SMatthew G. Knepley     if (!s)  SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
453333907cc2SStefano Zampini     if (!dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45341bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4535cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
45361bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
45371bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
453888ed4aceSMatthew G Knepley   }
45391bb6d2a8SBarry Smith   *section = dm->globalSection;
454088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
454188ed4aceSMatthew G Knepley }
454288ed4aceSMatthew G Knepley 
4543b21d0597SMatthew G Knepley /*@
4544e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4545b21d0597SMatthew G Knepley 
4546b21d0597SMatthew G Knepley   Input Parameters:
4547b21d0597SMatthew G Knepley + dm - The DM
45485080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4549b21d0597SMatthew G Knepley 
4550b21d0597SMatthew G Knepley   Level: intermediate
4551b21d0597SMatthew G Knepley 
4552b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4553b21d0597SMatthew G Knepley 
455492fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4555b21d0597SMatthew G Knepley @*/
4556e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
45570adebc6cSBarry Smith {
4558b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4559b21d0597SMatthew G Knepley 
4560b21d0597SMatthew G Knepley   PetscFunctionBegin;
4561b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45625080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
45631d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
45641bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
45651bb6d2a8SBarry Smith   dm->globalSection = section;
4566497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45671bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4568507e4973SMatthew G. Knepley #endif
4569b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4570b21d0597SMatthew G Knepley }
4571b21d0597SMatthew G Knepley 
457288ed4aceSMatthew G Knepley /*@
45731bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
457488ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
457588ed4aceSMatthew G Knepley 
457688ed4aceSMatthew G Knepley   Input Parameter:
457788ed4aceSMatthew G Knepley . dm - The DM
457888ed4aceSMatthew G Knepley 
457988ed4aceSMatthew G Knepley   Output Parameter:
458088ed4aceSMatthew G Knepley . sf - The PetscSF
458188ed4aceSMatthew G Knepley 
458288ed4aceSMatthew G Knepley   Level: intermediate
458388ed4aceSMatthew G Knepley 
458488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
458588ed4aceSMatthew G Knepley 
45861bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
458788ed4aceSMatthew G Knepley @*/
45881bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
45890adebc6cSBarry Smith {
459088ed4aceSMatthew G Knepley   PetscInt       nroots;
459188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
459288ed4aceSMatthew G Knepley 
459388ed4aceSMatthew G Knepley   PetscFunctionBegin;
459488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
459588ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
45961bb6d2a8SBarry Smith   if (!dm->sectionSF) {
45971bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
459833907cc2SStefano Zampini   }
45991bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
460088ed4aceSMatthew G Knepley   if (nroots < 0) {
460188ed4aceSMatthew G Knepley     PetscSection section, gSection;
460288ed4aceSMatthew G Knepley 
460392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
460431ea6d37SMatthew G Knepley     if (section) {
4605e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
46061bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
460731ea6d37SMatthew G Knepley     } else {
46080298fd71SBarry Smith       *sf = NULL;
460931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
461031ea6d37SMatthew G Knepley     }
461188ed4aceSMatthew G Knepley   }
46121bb6d2a8SBarry Smith   *sf = dm->sectionSF;
461388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
461488ed4aceSMatthew G Knepley }
461588ed4aceSMatthew G Knepley 
461688ed4aceSMatthew G Knepley /*@
46171bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
461888ed4aceSMatthew G Knepley 
461988ed4aceSMatthew G Knepley   Input Parameters:
462088ed4aceSMatthew G Knepley + dm - The DM
462188ed4aceSMatthew G Knepley - sf - The PetscSF
462288ed4aceSMatthew G Knepley 
462388ed4aceSMatthew G Knepley   Level: intermediate
462488ed4aceSMatthew G Knepley 
462588ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
462688ed4aceSMatthew G Knepley 
46271bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
462888ed4aceSMatthew G Knepley @*/
46291bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
46300adebc6cSBarry Smith {
463188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
463288ed4aceSMatthew G Knepley 
463388ed4aceSMatthew G Knepley   PetscFunctionBegin;
463488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4635b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
463633907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
46371bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
46381bb6d2a8SBarry Smith   dm->sectionSF = sf;
463988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
464088ed4aceSMatthew G Knepley }
464188ed4aceSMatthew G Knepley 
464288ed4aceSMatthew G Knepley /*@C
46431bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
464488ed4aceSMatthew G Knepley   describing the data layout.
464588ed4aceSMatthew G Knepley 
464688ed4aceSMatthew G Knepley   Input Parameters:
464788ed4aceSMatthew G Knepley + dm - The DM
464888ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
464988ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
465088ed4aceSMatthew G Knepley 
46511bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
465288ed4aceSMatthew G Knepley 
46531bb6d2a8SBarry Smith   Level: developer
46541bb6d2a8SBarry Smith 
46551bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
46561bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
46571bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
46581bb6d2a8SBarry Smith 
46591bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
466088ed4aceSMatthew G Knepley @*/
46611bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
466288ed4aceSMatthew G Knepley {
466388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
466488ed4aceSMatthew G Knepley 
466588ed4aceSMatthew G Knepley   PetscFunctionBegin;
466688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4667b0c7db22SLisandro Dalcin   ierr = PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection);CHKERRQ(ierr);
466888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
466988ed4aceSMatthew G Knepley }
4670af122d2aSMatthew G Knepley 
4671b21d0597SMatthew G Knepley /*@
4672b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4673b21d0597SMatthew G Knepley 
4674b21d0597SMatthew G Knepley   Input Parameter:
4675b21d0597SMatthew G Knepley . dm - The DM
4676b21d0597SMatthew G Knepley 
4677b21d0597SMatthew G Knepley   Output Parameter:
4678b21d0597SMatthew G Knepley . sf - The PetscSF
4679b21d0597SMatthew G Knepley 
4680b21d0597SMatthew G Knepley   Level: intermediate
4681b21d0597SMatthew G Knepley 
4682b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4683b21d0597SMatthew G Knepley 
46841bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4685b21d0597SMatthew G Knepley @*/
46860adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
46870adebc6cSBarry Smith {
4688b21d0597SMatthew G Knepley   PetscFunctionBegin;
4689b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4690b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4691b21d0597SMatthew G Knepley   *sf = dm->sf;
4692b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4693b21d0597SMatthew G Knepley }
4694b21d0597SMatthew G Knepley 
4695057b4bcdSMatthew G Knepley /*@
4696057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4697057b4bcdSMatthew G Knepley 
4698057b4bcdSMatthew G Knepley   Input Parameters:
4699057b4bcdSMatthew G Knepley + dm - The DM
4700057b4bcdSMatthew G Knepley - sf - The PetscSF
4701057b4bcdSMatthew G Knepley 
4702057b4bcdSMatthew G Knepley   Level: intermediate
4703057b4bcdSMatthew G Knepley 
47041bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4705057b4bcdSMatthew G Knepley @*/
47060adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
47070adebc6cSBarry Smith {
4708057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4709057b4bcdSMatthew G Knepley 
4710057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4711057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4712b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4713057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
471433907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4715057b4bcdSMatthew G Knepley   dm->sf = sf;
4716057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4717057b4bcdSMatthew G Knepley }
4718057b4bcdSMatthew G Knepley 
47194f37162bSMatthew G. Knepley /*@
47204f37162bSMatthew G. Knepley   DMGetNaturalSF - Get the PetscSF encoding the map back to the original mesh ordering
47214f37162bSMatthew G. Knepley 
47224f37162bSMatthew G. Knepley   Input Parameter:
47234f37162bSMatthew G. Knepley . dm - The DM
47244f37162bSMatthew G. Knepley 
47254f37162bSMatthew G. Knepley   Output Parameter:
47264f37162bSMatthew G. Knepley . sf - The PetscSF
47274f37162bSMatthew G. Knepley 
47284f37162bSMatthew G. Knepley   Level: intermediate
47294f37162bSMatthew G. Knepley 
47304f37162bSMatthew G. Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
47314f37162bSMatthew G. Knepley 
47324f37162bSMatthew G. Knepley .seealso: DMSetNaturalSF(), DMSetUseNatural(), DMGetUseNatural(), DMPlexCreateGlobalToNaturalSF(), DMPlexDistribute()
47334f37162bSMatthew G. Knepley @*/
47344f37162bSMatthew G. Knepley PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
47354f37162bSMatthew G. Knepley {
47364f37162bSMatthew G. Knepley   PetscFunctionBegin;
47374f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47384f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
47394f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
47404f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
47414f37162bSMatthew G. Knepley }
47424f37162bSMatthew G. Knepley 
47434f37162bSMatthew G. Knepley /*@
47444f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
47454f37162bSMatthew G. Knepley 
47464f37162bSMatthew G. Knepley   Input Parameters:
47474f37162bSMatthew G. Knepley + dm - The DM
47484f37162bSMatthew G. Knepley - sf - The PetscSF
47494f37162bSMatthew G. Knepley 
47504f37162bSMatthew G. Knepley   Level: intermediate
47514f37162bSMatthew G. Knepley 
47524f37162bSMatthew G. Knepley .seealso: DMGetNaturalSF(), DMSetUseNatural(), DMGetUseNatural(), DMPlexCreateGlobalToNaturalSF(), DMPlexDistribute()
47534f37162bSMatthew G. Knepley @*/
47544f37162bSMatthew G. Knepley PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
47554f37162bSMatthew G. Knepley {
47564f37162bSMatthew G. Knepley   PetscErrorCode ierr;
47574f37162bSMatthew G. Knepley 
47584f37162bSMatthew G. Knepley   PetscFunctionBegin;
47594f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47604f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47614f37162bSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
47624f37162bSMatthew G. Knepley   ierr = PetscSFDestroy(&dm->sfNatural);CHKERRQ(ierr);
47634f37162bSMatthew G. Knepley   dm->sfNatural = sf;
47644f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
47654f37162bSMatthew G. Knepley }
47664f37162bSMatthew G. Knepley 
476734aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
476834aa8a36SMatthew G. Knepley {
476934aa8a36SMatthew G. Knepley   PetscClassId   id;
477034aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
477134aa8a36SMatthew G. Knepley 
477234aa8a36SMatthew G. Knepley   PetscFunctionBegin;
477334aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
477434aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
477534aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
477634aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
477734aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
477817c1d62eSMatthew G. Knepley   } else {
477917c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
478034aa8a36SMatthew G. Knepley   }
478134aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
478234aa8a36SMatthew G. Knepley }
478334aa8a36SMatthew G. Knepley 
478444a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
478544a7f3ddSMatthew G. Knepley {
478644a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
478744a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
478844a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
478944a7f3ddSMatthew G. Knepley 
479044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
479144a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
479244a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
479344a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4794e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
479544a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
479644a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
479744a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
479844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
479944a7f3ddSMatthew G. Knepley }
480044a7f3ddSMatthew G. Knepley 
480144a7f3ddSMatthew G. Knepley /*@
480244a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
480344a7f3ddSMatthew G. Knepley 
4804d083f849SBarry Smith   Logically collective on dm
480544a7f3ddSMatthew G. Knepley 
480644a7f3ddSMatthew G. Knepley   Input Parameter:
480744a7f3ddSMatthew G. Knepley . dm - The DM
480844a7f3ddSMatthew G. Knepley 
480944a7f3ddSMatthew G. Knepley   Level: intermediate
481044a7f3ddSMatthew G. Knepley 
481144a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
481244a7f3ddSMatthew G. Knepley @*/
481344a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
481444a7f3ddSMatthew G. Knepley {
481544a7f3ddSMatthew G. Knepley   PetscInt       f;
481644a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
481744a7f3ddSMatthew G. Knepley 
481844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
481944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
482044a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
482144a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
482244a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
482344a7f3ddSMatthew G. Knepley   }
482444a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
482544a7f3ddSMatthew G. Knepley   dm->fields = NULL;
482644a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
482744a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
482844a7f3ddSMatthew G. Knepley }
482944a7f3ddSMatthew G. Knepley 
4830689b5837SMatthew G. Knepley /*@
4831689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4832689b5837SMatthew G. Knepley 
4833689b5837SMatthew G. Knepley   Not collective
4834689b5837SMatthew G. Knepley 
4835689b5837SMatthew G. Knepley   Input Parameter:
4836689b5837SMatthew G. Knepley . dm - The DM
4837689b5837SMatthew G. Knepley 
4838689b5837SMatthew G. Knepley   Output Parameter:
4839689b5837SMatthew G. Knepley . Nf - The number of fields
4840689b5837SMatthew G. Knepley 
4841689b5837SMatthew G. Knepley   Level: intermediate
4842689b5837SMatthew G. Knepley 
4843689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4844689b5837SMatthew G. Knepley @*/
48450f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
48460f21e855SMatthew G. Knepley {
48470f21e855SMatthew G. Knepley   PetscFunctionBegin;
48480f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4849534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
485044a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4851af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4852af122d2aSMatthew G Knepley }
4853af122d2aSMatthew G Knepley 
4854689b5837SMatthew G. Knepley /*@
4855689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4856689b5837SMatthew G. Knepley 
4857d083f849SBarry Smith   Logically collective on dm
4858689b5837SMatthew G. Knepley 
4859689b5837SMatthew G. Knepley   Input Parameters:
4860689b5837SMatthew G. Knepley + dm - The DM
4861689b5837SMatthew G. Knepley - Nf - The number of fields
4862689b5837SMatthew G. Knepley 
4863689b5837SMatthew G. Knepley   Level: intermediate
4864689b5837SMatthew G. Knepley 
4865689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4866689b5837SMatthew G. Knepley @*/
4867af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4868af122d2aSMatthew G Knepley {
48690f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4870af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4871af122d2aSMatthew G Knepley 
4872af122d2aSMatthew G Knepley   PetscFunctionBegin;
4873af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
487444a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
48750f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
48760f21e855SMatthew G. Knepley     PetscContainer obj;
48770f21e855SMatthew G. Knepley 
48780f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
487944a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
48800f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4881af122d2aSMatthew G Knepley   }
4882af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4883af122d2aSMatthew G Knepley }
4884af122d2aSMatthew G Knepley 
4885c1929be8SMatthew G. Knepley /*@
4886c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4887c1929be8SMatthew G. Knepley 
4888c1929be8SMatthew G. Knepley   Not collective
4889c1929be8SMatthew G. Knepley 
4890c1929be8SMatthew G. Knepley   Input Parameters:
4891c1929be8SMatthew G. Knepley + dm - The DM
4892c1929be8SMatthew G. Knepley - f  - The field number
4893c1929be8SMatthew G. Knepley 
489444a7f3ddSMatthew G. Knepley   Output Parameters:
489544a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
489644a7f3ddSMatthew G. Knepley - field - The discretization object
4897c1929be8SMatthew G. Knepley 
489844a7f3ddSMatthew G. Knepley   Level: intermediate
4899c1929be8SMatthew G. Knepley 
490044a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4901c1929be8SMatthew G. Knepley @*/
490244a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4903af122d2aSMatthew G Knepley {
4904af122d2aSMatthew G Knepley   PetscFunctionBegin;
4905af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4906064a246eSJacob Faibussowitsch   PetscValidPointer(field, 4);
490744a7f3ddSMatthew G. Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, dm->Nf);
490844a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
490944a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4910decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4911decb47aaSMatthew G. Knepley }
4912decb47aaSMatthew G. Knepley 
4913083401c6SMatthew G. Knepley /* Does not clear the DS */
4914083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4915083401c6SMatthew G. Knepley {
4916083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4917083401c6SMatthew G. Knepley 
4918083401c6SMatthew G. Knepley   PetscFunctionBegin;
4919083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4920083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4921083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4922083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4923083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4924083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4925083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4926083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4927083401c6SMatthew G. Knepley }
4928083401c6SMatthew G. Knepley 
4929c1929be8SMatthew G. Knepley /*@
4930c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4931c1929be8SMatthew G. Knepley 
4932d083f849SBarry Smith   Logically collective on dm
4933c1929be8SMatthew G. Knepley 
4934c1929be8SMatthew G. Knepley   Input Parameters:
4935c1929be8SMatthew G. Knepley + dm    - The DM
4936c1929be8SMatthew G. Knepley . f     - The field number
493744a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4938c1929be8SMatthew G. Knepley - field - The discretization object
4939c1929be8SMatthew G. Knepley 
494044a7f3ddSMatthew G. Knepley   Level: intermediate
4941c1929be8SMatthew G. Knepley 
494244a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4943c1929be8SMatthew G. Knepley @*/
494444a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4945decb47aaSMatthew G. Knepley {
4946decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4947decb47aaSMatthew G. Knepley 
4948decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4949decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4950e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
495144a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4952e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4953083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
495434aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
49552df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
495644a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
495744a7f3ddSMatthew G. Knepley }
495844a7f3ddSMatthew G. Knepley 
495944a7f3ddSMatthew G. Knepley /*@
496044a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
496144a7f3ddSMatthew G. Knepley 
4962d083f849SBarry Smith   Logically collective on dm
496344a7f3ddSMatthew G. Knepley 
496444a7f3ddSMatthew G. Knepley   Input Parameters:
496544a7f3ddSMatthew G. Knepley + dm    - The DM
496644a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
496744a7f3ddSMatthew G. Knepley - field - The discretization object
496844a7f3ddSMatthew G. Knepley 
496944a7f3ddSMatthew G. Knepley   Level: intermediate
497044a7f3ddSMatthew G. Knepley 
497144a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
497244a7f3ddSMatthew G. Knepley @*/
497344a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
497444a7f3ddSMatthew G. Knepley {
497544a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
497644a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
497744a7f3ddSMatthew G. Knepley 
497844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
497944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4980064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
498144a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
498244a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
498344a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
498444a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
498544a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
498644a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
498734aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
49882df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4989af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4990af122d2aSMatthew G Knepley }
49916636e97aSMatthew G Knepley 
4992e5e52638SMatthew G. Knepley /*@
4993e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4994e0b68406SMatthew Knepley 
4995e0b68406SMatthew Knepley   Logically collective on dm
4996e0b68406SMatthew Knepley 
4997e0b68406SMatthew Knepley   Input Parameters:
4998e0b68406SMatthew Knepley + dm          - The DM
4999e0b68406SMatthew Knepley . f           - The field index
5000e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
5001e0b68406SMatthew Knepley 
5002e0b68406SMatthew Knepley   Level: intermediate
5003e0b68406SMatthew Knepley 
5004e0b68406SMatthew Knepley .seealso: DMGetFieldAvoidTensor(), DMSetField(), DMGetField()
5005e0b68406SMatthew Knepley @*/
5006e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5007e0b68406SMatthew Knepley {
5008e0b68406SMatthew Knepley   PetscFunctionBegin;
5009e0b68406SMatthew Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
5010e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
5011e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5012e0b68406SMatthew Knepley }
5013e0b68406SMatthew Knepley 
5014e0b68406SMatthew Knepley /*@
5015e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5016e0b68406SMatthew Knepley 
5017e0b68406SMatthew Knepley   Logically collective on dm
5018e0b68406SMatthew Knepley 
5019e0b68406SMatthew Knepley   Input Parameters:
5020e0b68406SMatthew Knepley + dm          - The DM
5021e0b68406SMatthew Knepley - f           - The field index
5022e0b68406SMatthew Knepley 
5023e0b68406SMatthew Knepley   Output Parameter:
5024e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5025e0b68406SMatthew Knepley 
5026e0b68406SMatthew Knepley   Level: intermediate
5027e0b68406SMatthew Knepley 
5028e0b68406SMatthew Knepley .seealso: DMSetFieldAvoidTensor(), DMSetField(), DMGetField()
5029e0b68406SMatthew Knepley @*/
5030e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5031e0b68406SMatthew Knepley {
5032e0b68406SMatthew Knepley   PetscFunctionBegin;
5033e0b68406SMatthew Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
5034e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
5035e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5036e0b68406SMatthew Knepley }
5037e0b68406SMatthew Knepley 
5038e0b68406SMatthew Knepley /*@
5039e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
5040e5e52638SMatthew G. Knepley 
5041d083f849SBarry Smith   Collective on dm
5042e5e52638SMatthew G. Knepley 
5043e5e52638SMatthew G. Knepley   Input Parameter:
5044e5e52638SMatthew G. Knepley . dm - The DM
5045e5e52638SMatthew G. Knepley 
5046e5e52638SMatthew G. Knepley   Output Parameter:
5047e5e52638SMatthew G. Knepley . newdm - The DM
5048e5e52638SMatthew G. Knepley 
5049e5e52638SMatthew G. Knepley   Level: advanced
5050e5e52638SMatthew G. Knepley 
5051e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
5052e5e52638SMatthew G. Knepley @*/
5053e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
5054e5e52638SMatthew G. Knepley {
5055e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
5056e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5057e5e52638SMatthew G. Knepley 
5058e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5059e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5060e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5061e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
5062e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5063e5e52638SMatthew G. Knepley     DMLabel     label;
5064e5e52638SMatthew G. Knepley     PetscObject field;
506534aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5066e5e52638SMatthew G. Knepley 
5067e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
5068e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
506934aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
507034aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
507134aa8a36SMatthew G. Knepley   }
507234aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
507334aa8a36SMatthew G. Knepley }
507434aa8a36SMatthew G. Knepley 
507534aa8a36SMatthew G. Knepley /*@
507634aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
507734aa8a36SMatthew G. Knepley 
507834aa8a36SMatthew G. Knepley   Not collective
507934aa8a36SMatthew G. Knepley 
508034aa8a36SMatthew G. Knepley   Input Parameters:
508134aa8a36SMatthew G. Knepley + dm - The DM object
508234aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
508334aa8a36SMatthew G. Knepley 
5084d8d19677SJose E. Roman   Output Parameters:
508534aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
508634aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
508734aa8a36SMatthew G. Knepley 
508834aa8a36SMatthew G. Knepley   Notes:
508934aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
509034aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
509134aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5092979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
509334aa8a36SMatthew G. Knepley 
509434aa8a36SMatthew G. Knepley   Level: developer
509534aa8a36SMatthew G. Knepley 
509634aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
509734aa8a36SMatthew G. Knepley @*/
509834aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
509934aa8a36SMatthew G. Knepley {
510034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
510134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5102534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
5103534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
510434aa8a36SMatthew G. Knepley   if (f < 0) {
510534aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
510634aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
510734aa8a36SMatthew G. Knepley   } else {
510834aa8a36SMatthew G. Knepley     PetscInt       Nf;
510934aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
511034aa8a36SMatthew G. Knepley 
511134aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
511234aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
511334aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
511434aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
511534aa8a36SMatthew G. Knepley   }
511634aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
511734aa8a36SMatthew G. Knepley }
511834aa8a36SMatthew G. Knepley 
511934aa8a36SMatthew G. Knepley /*@
512034aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
512134aa8a36SMatthew G. Knepley 
512234aa8a36SMatthew G. Knepley   Not collective
512334aa8a36SMatthew G. Knepley 
512434aa8a36SMatthew G. Knepley   Input Parameters:
512534aa8a36SMatthew G. Knepley + dm         - The DM object
512634aa8a36SMatthew G. Knepley . f          - The field number
512734aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
512834aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
512934aa8a36SMatthew G. Knepley 
513034aa8a36SMatthew G. Knepley   Notes:
513134aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
513234aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
513334aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5134979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
513534aa8a36SMatthew G. Knepley 
513634aa8a36SMatthew G. Knepley   Level: developer
513734aa8a36SMatthew G. Knepley 
513834aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
513934aa8a36SMatthew G. Knepley @*/
514034aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
514134aa8a36SMatthew G. Knepley {
514234aa8a36SMatthew G. Knepley   PetscFunctionBegin;
514334aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
514434aa8a36SMatthew G. Knepley   if (f < 0) {
514534aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
514634aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
514734aa8a36SMatthew G. Knepley   } else {
514834aa8a36SMatthew G. Knepley     PetscInt       Nf;
514934aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
515034aa8a36SMatthew G. Knepley 
515134aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
515234aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
515334aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
515434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5155e5e52638SMatthew G. Knepley   }
5156e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5157e5e52638SMatthew G. Knepley }
5158e5e52638SMatthew G. Knepley 
5159b0441da4SMatthew G. Knepley /*@
5160b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5161b0441da4SMatthew G. Knepley 
5162b0441da4SMatthew G. Knepley   Not collective
5163b0441da4SMatthew G. Knepley 
5164f899ff85SJose E. Roman   Input Parameter:
5165b0441da4SMatthew G. Knepley . dm - The DM object
5166b0441da4SMatthew G. Knepley 
5167d8d19677SJose E. Roman   Output Parameters:
5168b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5169b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5170b0441da4SMatthew G. Knepley 
5171b0441da4SMatthew G. Knepley   Notes:
5172b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5173b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5174b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5175b0441da4SMatthew G. Knepley 
5176b0441da4SMatthew G. Knepley   Level: developer
5177b0441da4SMatthew G. Knepley 
5178b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
5179b0441da4SMatthew G. Knepley @*/
5180b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5181b0441da4SMatthew G. Knepley {
5182b0441da4SMatthew G. Knepley   PetscInt       Nf;
5183b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5184b0441da4SMatthew G. Knepley 
5185b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5186b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5187064a246eSJacob Faibussowitsch   if (useCone)    PetscValidBoolPointer(useCone, 2);
5188064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
5189b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5190b0441da4SMatthew G. Knepley   if (!Nf) {
5191b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5192b0441da4SMatthew G. Knepley   } else {
5193b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5194b0441da4SMatthew G. Knepley   }
5195b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5196b0441da4SMatthew G. Knepley }
5197b0441da4SMatthew G. Knepley 
5198b0441da4SMatthew G. Knepley /*@
5199b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5200b0441da4SMatthew G. Knepley 
5201b0441da4SMatthew G. Knepley   Not collective
5202b0441da4SMatthew G. Knepley 
5203b0441da4SMatthew G. Knepley   Input Parameters:
5204b0441da4SMatthew G. Knepley + dm         - The DM object
5205b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5206b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5207b0441da4SMatthew G. Knepley 
5208b0441da4SMatthew G. Knepley   Notes:
5209b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5210b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5211b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5212b0441da4SMatthew G. Knepley 
5213b0441da4SMatthew G. Knepley   Level: developer
5214b0441da4SMatthew G. Knepley 
5215b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
5216b0441da4SMatthew G. Knepley @*/
5217b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5218b0441da4SMatthew G. Knepley {
5219b0441da4SMatthew G. Knepley   PetscInt       Nf;
5220b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5221b0441da4SMatthew G. Knepley 
5222b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5223b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5224b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5225b0441da4SMatthew G. Knepley   if (!Nf) {
5226b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5227b0441da4SMatthew G. Knepley   } else {
5228b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5229e5e52638SMatthew G. Knepley   }
5230e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5231e5e52638SMatthew G. Knepley }
5232e5e52638SMatthew G. Knepley 
5233783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
523445480ffeSMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, DMLabel label)
5235783e2ec8SMatthew G. Knepley {
5236783e2ec8SMatthew G. Knepley   PetscObject    obj;
5237783e2ec8SMatthew G. Knepley   PetscClassId   id;
5238783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
5239783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
5240783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
5241783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
5242783e2ec8SMatthew G. Knepley 
5243783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5244783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
5245783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5246783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
5247783e2ec8SMatthew G. Knepley   if (isFE && label) {
5248783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
5249783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5250783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
525145480ffeSMatthew G. Knepley       DMLabel l;
5252783e2ec8SMatthew G. Knepley 
525345480ffeSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, NULL, &l, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
525445480ffeSMatthew G. Knepley       duplicate = l == label ? PETSC_TRUE : PETSC_FALSE;
5255783e2ec8SMatthew G. Knepley       if (duplicate) break;
5256783e2ec8SMatthew G. Knepley     }
5257783e2ec8SMatthew G. Knepley     if (!duplicate) {
5258783e2ec8SMatthew G. Knepley       DM plex;
5259783e2ec8SMatthew G. Knepley 
5260783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
5261783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
5262783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
5263783e2ec8SMatthew G. Knepley     }
5264783e2ec8SMatthew G. Knepley   }
5265783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5266783e2ec8SMatthew G. Knepley }
5267783e2ec8SMatthew G. Knepley 
5268e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5269e5e52638SMatthew G. Knepley {
5270e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5271e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5272e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5273e5e52638SMatthew G. Knepley 
5274e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5275e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5276e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
5277e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5278b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5279e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5280e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5281e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5282e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5283e5e52638SMatthew G. Knepley }
5284e5e52638SMatthew G. Knepley 
5285e5e52638SMatthew G. Knepley /*@
5286e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5287e5e52638SMatthew G. Knepley 
5288e5e52638SMatthew G. Knepley   Not collective
5289e5e52638SMatthew G. Knepley 
5290e5e52638SMatthew G. Knepley   Input Parameter:
5291e5e52638SMatthew G. Knepley . dm - The DM
5292e5e52638SMatthew G. Knepley 
5293e5e52638SMatthew G. Knepley   Output Parameter:
5294e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5295e5e52638SMatthew G. Knepley 
5296e5e52638SMatthew G. Knepley   Level: intermediate
5297e5e52638SMatthew G. Knepley 
5298e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5299e5e52638SMatthew G. Knepley @*/
5300e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5301e5e52638SMatthew G. Knepley {
5302e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5303e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5304534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5305e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5306e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5307e5e52638SMatthew G. Knepley }
5308e5e52638SMatthew G. Knepley 
5309e5e52638SMatthew G. Knepley /*@
5310e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5311e5e52638SMatthew G. Knepley 
5312d083f849SBarry Smith   Logically collective on dm
5313e5e52638SMatthew G. Knepley 
5314e5e52638SMatthew G. Knepley   Input Parameter:
5315e5e52638SMatthew G. Knepley . dm - The DM
5316e5e52638SMatthew G. Knepley 
5317e5e52638SMatthew G. Knepley   Level: intermediate
5318e5e52638SMatthew G. Knepley 
5319e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5320e5e52638SMatthew G. Knepley @*/
5321e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5322e5e52638SMatthew G. Knepley {
5323e5e52638SMatthew G. Knepley   PetscInt       s;
5324e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5325e5e52638SMatthew G. Knepley 
5326e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5327e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5328e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5329e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5330e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5331b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5332e5e52638SMatthew G. Knepley   }
5333e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5334e5e52638SMatthew G. Knepley   dm->probs = NULL;
5335e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5336e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5337e5e52638SMatthew G. Knepley }
5338e5e52638SMatthew G. Knepley 
5339e5e52638SMatthew G. Knepley /*@
5340e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5341e5e52638SMatthew G. Knepley 
5342e5e52638SMatthew G. Knepley   Not collective
5343e5e52638SMatthew G. Knepley 
5344e5e52638SMatthew G. Knepley   Input Parameter:
5345e5e52638SMatthew G. Knepley . dm    - The DM
5346e5e52638SMatthew G. Knepley 
5347e5e52638SMatthew G. Knepley   Output Parameter:
5348e5e52638SMatthew G. Knepley . prob - The default PetscDS
5349e5e52638SMatthew G. Knepley 
5350e5e52638SMatthew G. Knepley   Level: intermediate
5351e5e52638SMatthew G. Knepley 
5352e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5353e5e52638SMatthew G. Knepley @*/
5354e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5355e5e52638SMatthew G. Knepley {
5356b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5357b0143b4dSMatthew G. Knepley 
5358e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5359e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5360e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5361b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5362b0143b4dSMatthew G. Knepley     PetscDS ds;
5363b0143b4dSMatthew G. Knepley 
536445480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5365b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5366b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5367b0143b4dSMatthew G. Knepley   }
5368b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5369e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5370e5e52638SMatthew G. Knepley }
5371e5e52638SMatthew G. Knepley 
5372e5e52638SMatthew G. Knepley /*@
5373e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5374e5e52638SMatthew G. Knepley 
5375e5e52638SMatthew G. Knepley   Not collective
5376e5e52638SMatthew G. Knepley 
5377e5e52638SMatthew G. Knepley   Input Parameters:
5378e5e52638SMatthew G. Knepley + dm    - The DM
5379e5e52638SMatthew G. Knepley - point - Cell for the DS
5380e5e52638SMatthew G. Knepley 
5381e5e52638SMatthew G. Knepley   Output Parameter:
5382e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5383e5e52638SMatthew G. Knepley 
5384e5e52638SMatthew G. Knepley   Level: developer
5385e5e52638SMatthew G. Knepley 
5386b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5387e5e52638SMatthew G. Knepley @*/
5388e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5389e5e52638SMatthew G. Knepley {
5390e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5391e5e52638SMatthew G. Knepley   PetscInt       s;
5392e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5393e5e52638SMatthew G. Knepley 
5394e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5395e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5396e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5397360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5398e5e52638SMatthew G. Knepley   *prob = NULL;
5399e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5400e5e52638SMatthew G. Knepley     PetscInt val;
5401e5e52638SMatthew G. Knepley 
5402e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5403e5e52638SMatthew G. Knepley     else {
5404e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5405e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5406e5e52638SMatthew G. Knepley     }
5407e5e52638SMatthew G. Knepley   }
5408e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5409e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5410e5e52638SMatthew G. Knepley }
5411e5e52638SMatthew G. Knepley 
5412e5e52638SMatthew G. Knepley /*@
5413e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5414e5e52638SMatthew G. Knepley 
5415e5e52638SMatthew G. Knepley   Not collective
5416e5e52638SMatthew G. Knepley 
5417e5e52638SMatthew G. Knepley   Input Parameters:
5418e5e52638SMatthew G. Knepley + dm    - The DM
5419e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5420e5e52638SMatthew G. Knepley 
5421b3cf3223SMatthew G. Knepley   Output Parameters:
5422b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5423b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5424e5e52638SMatthew G. Knepley 
5425e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5426e5e52638SMatthew G. Knepley 
5427e5e52638SMatthew G. Knepley   Level: advanced
5428e5e52638SMatthew G. Knepley 
5429e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5430e5e52638SMatthew G. Knepley @*/
5431b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5432e5e52638SMatthew G. Knepley {
5433e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5434e5e52638SMatthew G. Knepley 
5435e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5436e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5437e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5438b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5439b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5440e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5441b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5442b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5443b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5444b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5445b3cf3223SMatthew G. Knepley     }
5446e5e52638SMatthew G. Knepley   }
54472df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5448e5e52638SMatthew G. Knepley }
5449e5e52638SMatthew G. Knepley 
5450e5e52638SMatthew G. Knepley /*@
5451083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5452083401c6SMatthew G. Knepley 
5453083401c6SMatthew G. Knepley   Collective on dm
5454083401c6SMatthew G. Knepley 
5455083401c6SMatthew G. Knepley   Input Parameters:
5456083401c6SMatthew G. Knepley + dm     - The DM
5457083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5458083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5459083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5460083401c6SMatthew G. Knepley 
5461083401c6SMatthew 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,
5462083401c6SMatthew G. Knepley   the fields argument is ignored.
5463083401c6SMatthew G. Knepley 
5464083401c6SMatthew G. Knepley   Level: advanced
5465083401c6SMatthew G. Knepley 
5466083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5467083401c6SMatthew G. Knepley @*/
5468083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5469083401c6SMatthew G. Knepley {
5470083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5471083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5472083401c6SMatthew G. Knepley 
5473083401c6SMatthew G. Knepley   PetscFunctionBegin;
5474083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5475083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5476064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5477083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5478083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5479083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5480083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5481083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5482083401c6SMatthew G. Knepley     }
5483083401c6SMatthew G. Knepley   }
5484083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5485083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5486083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5487083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5488083401c6SMatthew G. Knepley   if (!label) {
5489083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5490083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5491083401c6SMatthew G. Knepley     Nds = 0;
5492083401c6SMatthew G. Knepley   }
5493083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5494083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5495083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5496083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5497083401c6SMatthew G. Knepley }
5498083401c6SMatthew G. Knepley 
5499083401c6SMatthew G. Knepley /*@
5500e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5501e5e52638SMatthew G. Knepley 
5502e5e52638SMatthew G. Knepley   Not collective
5503e5e52638SMatthew G. Knepley 
5504e5e52638SMatthew G. Knepley   Input Parameters:
5505e5e52638SMatthew G. Knepley + dm  - The DM
5506e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5507e5e52638SMatthew G. Knepley 
5508e5e52638SMatthew G. Knepley   Output Parameters:
5509b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5510b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5511083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5512e5e52638SMatthew G. Knepley 
5513e5e52638SMatthew G. Knepley   Level: advanced
5514e5e52638SMatthew G. Knepley 
5515e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5516e5e52638SMatthew G. Knepley @*/
5517b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5518e5e52638SMatthew G. Knepley {
5519e5e52638SMatthew G. Knepley   PetscInt       Nds;
5520e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5521e5e52638SMatthew G. Knepley 
5522e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5523e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5524e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5525e5e52638SMatthew G. Knepley   if ((num < 0) || (num >= Nds)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
5526e5e52638SMatthew G. Knepley   if (label) {
5527e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5528e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5529e5e52638SMatthew G. Knepley   }
5530b3cf3223SMatthew G. Knepley   if (fields) {
5531b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5532b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5533b3cf3223SMatthew G. Knepley   }
5534e5e52638SMatthew G. Knepley   if (ds) {
5535b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5536e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5537e5e52638SMatthew G. Knepley   }
5538e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5539e5e52638SMatthew G. Knepley }
5540e5e52638SMatthew G. Knepley 
5541e5e52638SMatthew G. Knepley /*@
5542083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5543e5e52638SMatthew G. Knepley 
5544083401c6SMatthew G. Knepley   Not collective
5545e5e52638SMatthew G. Knepley 
5546e5e52638SMatthew G. Knepley   Input Parameters:
5547e5e52638SMatthew G. Knepley + dm     - The DM
5548083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5549083401c6SMatthew G. Knepley . label  - The region label, or NULL
5550083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5551083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5552e5e52638SMatthew G. Knepley 
5553e5e52638SMatthew G. Knepley   Level: advanced
5554e5e52638SMatthew G. Knepley 
5555083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5556e5e52638SMatthew G. Knepley @*/
5557083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5558e5e52638SMatthew G. Knepley {
5559083401c6SMatthew G. Knepley   PetscInt       Nds;
5560e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5561e5e52638SMatthew G. Knepley 
5562e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5563e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5564083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5565083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5566083401c6SMatthew G. Knepley   if ((num < 0) || (num >= Nds)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
5567e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5568083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5569083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5570083401c6SMatthew G. Knepley   if (fields) {
5571083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5572b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5573083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5574083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5575e5e52638SMatthew G. Knepley   }
5576083401c6SMatthew G. Knepley   if (ds) {
5577083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5578083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5579083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5580083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5581083401c6SMatthew G. Knepley   }
5582e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5583e5e52638SMatthew G. Knepley }
5584e5e52638SMatthew G. Knepley 
5585e5e52638SMatthew G. Knepley /*@
55861d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
55871d3af9e0SMatthew G. Knepley 
55881d3af9e0SMatthew G. Knepley   Not collective
55891d3af9e0SMatthew G. Knepley 
55901d3af9e0SMatthew G. Knepley   Input Parameters:
55911d3af9e0SMatthew G. Knepley + dm  - The DM
55921d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
55931d3af9e0SMatthew G. Knepley 
55941d3af9e0SMatthew G. Knepley   Output Parameter:
55951d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
55961d3af9e0SMatthew G. Knepley 
55971d3af9e0SMatthew G. Knepley   Level: advanced
55981d3af9e0SMatthew G. Knepley 
55991d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
56001d3af9e0SMatthew G. Knepley @*/
56011d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
56021d3af9e0SMatthew G. Knepley {
56031d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
56041d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
56051d3af9e0SMatthew G. Knepley 
56061d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
56071d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56081d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
56091d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
56101d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
56111d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
56121d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
56131d3af9e0SMatthew G. Knepley   else          *num = n;
56141d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
56151d3af9e0SMatthew G. Knepley }
56161d3af9e0SMatthew G. Knepley 
56171d3af9e0SMatthew G. Knepley /*@
5618e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5619e5e52638SMatthew G. Knepley 
5620d083f849SBarry Smith   Collective on dm
5621e5e52638SMatthew G. Knepley 
5622e5e52638SMatthew G. Knepley   Input Parameter:
5623e5e52638SMatthew G. Knepley . dm - The DM
5624e5e52638SMatthew G. Knepley 
562545480ffeSMatthew G. Knepley   Options Database Keys:
562645480ffeSMatthew G. Knepley . -dm_petscds_view - View all the PetscDS objects in this DM
562745480ffeSMatthew G. Knepley 
5628e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5629e5e52638SMatthew G. Knepley 
5630e5e52638SMatthew G. Knepley   Level: intermediate
5631e5e52638SMatthew G. Knepley 
5632e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5633e5e52638SMatthew G. Knepley @*/
5634e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5635e5e52638SMatthew G. Knepley {
5636e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5637083401c6SMatthew G. Knepley   PetscDS        dsDef;
5638083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5639f9244615SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5640f9244615SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE, flg;
5641e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5642e5e52638SMatthew G. Knepley 
5643e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5644e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5645e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5646e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5647083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5648083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5649083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5650083401c6SMatthew G. Knepley   Nl   = 0;
5651083401c6SMatthew G. Knepley   Ndef = 0;
5652083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5653083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5654083401c6SMatthew G. Knepley     PetscInt l;
5655083401c6SMatthew G. Knepley 
5656f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5657f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5658f918ec44SMatthew G. Knepley     {
5659f918ec44SMatthew G. Knepley       PetscClassId id;
5660f918ec44SMatthew G. Knepley 
5661f918ec44SMatthew G. Knepley       ierr = PetscObjectGetClassId(dm->fields[f].disc, &id);CHKERRQ(ierr);
5662f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5663f918ec44SMatthew G. Knepley         Ceed ceed;
5664f918ec44SMatthew G. Knepley 
5665f918ec44SMatthew G. Knepley         ierr = DMGetCeed(dm, &ceed);CHKERRQ(ierr);
5666f918ec44SMatthew G. Knepley         ierr = PetscFESetCeed((PetscFE) dm->fields[f].disc, ceed);CHKERRQ(ierr);
5667f918ec44SMatthew G. Knepley       }
5668f918ec44SMatthew G. Knepley     }
5669f918ec44SMatthew G. Knepley #endif
5670083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5671083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5672083401c6SMatthew G. Knepley     if (l < Nl) continue;
5673083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5674083401c6SMatthew G. Knepley   }
5675083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5676083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5677083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5678b3cf3223SMatthew G. Knepley     IS        fields;
5679b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5680b3cf3223SMatthew G. Knepley 
5681b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5682083401c6SMatthew G. Knepley     if (nf) {
5683b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5684b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
568588f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
568688f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
568788f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
568888f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
568988f0c812SMatthew G. Knepley 
569045480ffeSMatthew G. Knepley       ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5691083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5692083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5693b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
56942df9ee95SMatthew G. Knepley     }
5695083401c6SMatthew G. Knepley   }
5696083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5697083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5698083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5699083401c6SMatthew G. Knepley   if (Ndef && Nl) {
57000122748bSMatthew G. Knepley     DM              plex;
5701083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5702083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5703083401c6SMatthew G. Knepley     PetscInt       *fields;
5704083401c6SMatthew G. Knepley     const PetscInt *cells;
5705083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
57060122748bSMatthew G. Knepley 
57070122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
57080122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5709083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5710083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5711083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5712083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5713083401c6SMatthew G. Knepley       IS      pointIS;
5714083401c6SMatthew G. Knepley 
5715083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5716083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5717083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5718083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5719083401c6SMatthew G. Knepley     }
5720083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5721083401c6SMatthew G. Knepley 
5722083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5723083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5724083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5725083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5726083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5727083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5728083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5729083401c6SMatthew G. Knepley 
5730083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5731083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5732083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5733083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5734083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5735083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5736083401c6SMatthew G. Knepley 
573745480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5738083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5739083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5740083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5741083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5742083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
57430122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5744083401c6SMatthew G. Knepley   }
5745083401c6SMatthew G. Knepley   /* Create label DSes
5746083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5747083401c6SMatthew G. Knepley   */
5748083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5749083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5750083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5751083401c6SMatthew G. Knepley     PetscDS   ds;
5752083401c6SMatthew G. Knepley     IS        fields;
5753083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5754083401c6SMatthew G. Knepley 
575545480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5756083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5757083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5758083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5759083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5760083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5761083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5762083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5763083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5764083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5765083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5766083401c6SMatthew G. Knepley     {
5767083401c6SMatthew G. Knepley       DMPolytopeType ct;
5768083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5769083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
57700122748bSMatthew G. Knepley 
5771e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5772665f567fSMatthew G. Knepley       if (lStart >= 0) {
5773412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5774412e9a14SMatthew G. Knepley         switch (ct) {
5775412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5776412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5777412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5778412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5779083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5780083401c6SMatthew G. Knepley           default: break;
5781412e9a14SMatthew G. Knepley         }
5782665f567fSMatthew G. Knepley       }
5783ffc4695bSBarry Smith       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRMPI(ierr);
5784083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5785e5e52638SMatthew G. Knepley     }
5786083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5787e5e52638SMatthew G. Knepley   }
5788083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5789e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5790083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5791083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5792083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5793083401c6SMatthew G. Knepley     const PetscInt *fld;
5794083401c6SMatthew G. Knepley     PetscInt        nf;
5795e5e52638SMatthew G. Knepley 
5796083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5797083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5798083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5799083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5800083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5801e5e52638SMatthew G. Knepley       PetscClassId id;
5802e5e52638SMatthew G. Knepley 
5803083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5804083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5805083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5806083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5807083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5808e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5809e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5810e5e52638SMatthew G. Knepley     }
5811083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5812e5e52638SMatthew G. Knepley   }
5813f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
5814f9244615SMatthew G. Knepley   ierr = PetscOptionsGetInt(NULL, ((PetscObject) dm)->prefix, "-dm_ds_jet_degree", &k, &flg);CHKERRQ(ierr);
5815f9244615SMatthew G. Knepley   if (flg) {
58163b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
58173b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
58183b4aee56SMatthew G. Knepley       PetscInt Nf, f;
58193b4aee56SMatthew G. Knepley 
58203b4aee56SMatthew G. Knepley       ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
58213b4aee56SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {ierr = PetscDSSetJetDegree(ds, f, k);CHKERRQ(ierr);}
58223b4aee56SMatthew G. Knepley     }
5823f9244615SMatthew G. Knepley   }
5824e5e52638SMatthew G. Knepley   /* Setup DSes */
5825e5e52638SMatthew G. Knepley   if (doSetup) {
5826e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5827e5e52638SMatthew G. Knepley   }
5828e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5829e5e52638SMatthew G. Knepley }
5830e5e52638SMatthew G. Knepley 
5831e5e52638SMatthew G. Knepley /*@
58327f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
58337f96f943SMatthew G. Knepley 
5834f2cacb80SMatthew G. Knepley   Collective on DM
5835f2cacb80SMatthew G. Knepley 
58367f96f943SMatthew G. Knepley   Input Parameters:
58377f96f943SMatthew G. Knepley + dm   - The DM
58387f96f943SMatthew G. Knepley - time - The time
58397f96f943SMatthew G. Knepley 
58407f96f943SMatthew G. Knepley   Output Parameters:
5841f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5842f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
58437f96f943SMatthew G. Knepley 
58447f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
58457f96f943SMatthew G. Knepley 
58467f96f943SMatthew G. Knepley   Level: developer
58477f96f943SMatthew G. Knepley 
58487f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
58497f96f943SMatthew G. Knepley @*/
5850f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
58517f96f943SMatthew G. Knepley {
58527f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
58537f96f943SMatthew G. Knepley   void            **ectxs;
58547f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
58557f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
58567f96f943SMatthew G. Knepley 
58577f96f943SMatthew G. Knepley   PetscFunctionBegin;
5858f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5859f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5860f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
58617f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
58627f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
58637f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
58647f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
58657f96f943SMatthew G. Knepley     PetscDS         ds;
58667f96f943SMatthew G. Knepley     DMLabel         label;
58677f96f943SMatthew G. Knepley     IS              fieldIS;
58687f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
58697f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
58707f96f943SMatthew G. Knepley 
58717f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
58727f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
58737f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
58747f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
58757f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5876f2cacb80SMatthew G. Knepley     if (u) {
58777f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
58787f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
58797f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
58807f96f943SMatthew G. Knepley       }
58817f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
58827f96f943SMatthew G. Knepley       if (label) {
58837f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
58847f96f943SMatthew G. Knepley       } else {
58857f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
58867f96f943SMatthew G. Knepley       }
58877f96f943SMatthew G. Knepley     }
5888f2cacb80SMatthew G. Knepley     if (u_t) {
5889f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5890f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5891f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5892f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5893f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5894f2cacb80SMatthew G. Knepley       }
5895f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5896f2cacb80SMatthew G. Knepley       if (label) {
5897f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5898f2cacb80SMatthew G. Knepley       } else {
5899f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5900f2cacb80SMatthew G. Knepley       }
5901f2cacb80SMatthew G. Knepley     }
5902f2cacb80SMatthew G. Knepley   }
5903f2cacb80SMatthew G. Knepley   if (u) {
59047f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
59057f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5906f2cacb80SMatthew G. Knepley   }
5907f2cacb80SMatthew G. Knepley   if (u_t) {
5908f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
5909f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
5910f2cacb80SMatthew G. Knepley   }
59117f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
59127f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
59137f96f943SMatthew G. Knepley }
59147f96f943SMatthew G. Knepley 
591545480ffeSMatthew G. Knepley PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
591645480ffeSMatthew G. Knepley {
591745480ffeSMatthew G. Knepley   PetscDS        dsNew;
591845480ffeSMatthew G. Knepley   DSBoundary     b;
591945480ffeSMatthew G. Knepley   PetscInt       cdim, Nf, f;
5920df911668SMatthew G. Knepley   PetscBool      isHybrid;
592145480ffeSMatthew G. Knepley   void          *ctx;
592245480ffeSMatthew G. Knepley   PetscErrorCode ierr;
592345480ffeSMatthew G. Knepley 
592445480ffeSMatthew G. Knepley   PetscFunctionBegin;
592545480ffeSMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) ds), &dsNew);CHKERRQ(ierr);
592645480ffeSMatthew G. Knepley   ierr = PetscDSCopyConstants(ds, dsNew);CHKERRQ(ierr);
592745480ffeSMatthew G. Knepley   ierr = PetscDSCopyExactSolutions(ds, dsNew);CHKERRQ(ierr);
592845480ffeSMatthew G. Knepley   ierr = PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
592945480ffeSMatthew G. Knepley   ierr = PetscDSCopyEquations(ds, dsNew);CHKERRQ(ierr);
593045480ffeSMatthew G. Knepley   ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
593145480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
593245480ffeSMatthew G. Knepley     ierr = PetscDSGetContext(ds, f, &ctx);CHKERRQ(ierr);
593345480ffeSMatthew G. Knepley     ierr = PetscDSSetContext(dsNew, f, ctx);CHKERRQ(ierr);
593445480ffeSMatthew G. Knepley   }
593545480ffeSMatthew G. Knepley   if (Nf) {
593645480ffeSMatthew G. Knepley     ierr = PetscDSGetCoordinateDimension(ds, &cdim);CHKERRQ(ierr);
593745480ffeSMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsNew, cdim);CHKERRQ(ierr);
593845480ffeSMatthew G. Knepley   }
593945480ffeSMatthew G. Knepley   ierr = PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
594045480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
594145480ffeSMatthew G. Knepley     ierr = DMGetLabel(dm, b->lname, &b->label);CHKERRQ(ierr);
594245480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
594345480ffeSMatthew G. Knepley     //if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
594445480ffeSMatthew G. Knepley   }
5945df911668SMatthew G. Knepley   ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5946df911668SMatthew G. Knepley   ierr = PetscDSSetHybrid(dsNew, isHybrid);CHKERRQ(ierr);
594745480ffeSMatthew G. Knepley 
594845480ffeSMatthew G. Knepley   ierr = DMSetRegionDS(dm, label, fields, dsNew);CHKERRQ(ierr);
594945480ffeSMatthew G. Knepley   ierr = PetscDSDestroy(&dsNew);CHKERRQ(ierr);
595045480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
595145480ffeSMatthew G. Knepley }
595245480ffeSMatthew G. Knepley 
59537f96f943SMatthew G. Knepley /*@
5954e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5955e5e52638SMatthew G. Knepley 
5956d083f849SBarry Smith   Collective on dm
5957e5e52638SMatthew G. Knepley 
5958e5e52638SMatthew G. Knepley   Input Parameter:
5959e5e52638SMatthew G. Knepley . dm - The DM
5960e5e52638SMatthew G. Knepley 
5961e5e52638SMatthew G. Knepley   Output Parameter:
5962e5e52638SMatthew G. Knepley . newdm - The DM
5963e5e52638SMatthew G. Knepley 
5964e5e52638SMatthew G. Knepley   Level: advanced
5965e5e52638SMatthew G. Knepley 
5966e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5967e5e52638SMatthew G. Knepley @*/
5968e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5969e5e52638SMatthew G. Knepley {
5970e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5971e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5972e5e52638SMatthew G. Knepley 
5973e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5974e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5975e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5976e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5977e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5978e5e52638SMatthew G. Knepley     DMLabel  label;
5979b3cf3223SMatthew G. Knepley     IS       fields;
598045480ffeSMatthew G. Knepley     PetscDS  ds, newds;
5981783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5982e5e52638SMatthew G. Knepley 
5983b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5984b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
598545480ffeSMatthew G. Knepley     ierr = DMTransferDS_Internal(newdm, label, fields, ds);CHKERRQ(ierr);
598645480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
598745480ffeSMatthew G. Knepley     ierr = DMGetRegionDS(newdm, label, NULL, &newds);CHKERRQ(ierr);
598845480ffeSMatthew G. Knepley     ierr = PetscDSGetNumBoundary(newds, &Nbd);CHKERRQ(ierr);
5989783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5990b8025e53SMatthew G. Knepley       PetscWeakForm wf;
599145480ffeSMatthew G. Knepley       DMLabel       label;
5992783e2ec8SMatthew G. Knepley       PetscInt      field;
5993783e2ec8SMatthew G. Knepley 
5994b8025e53SMatthew G. Knepley       ierr = PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
599545480ffeSMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, newds, field, bd, label);CHKERRQ(ierr);
5996b8025e53SMatthew G. Knepley       ierr = PetscWeakFormReplaceLabel(wf, label);CHKERRQ(ierr);
5997783e2ec8SMatthew G. Knepley     }
5998e5e52638SMatthew G. Knepley   }
5999e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6000e5e52638SMatthew G. Knepley }
6001e5e52638SMatthew G. Knepley 
6002e5e52638SMatthew G. Knepley /*@
6003e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
6004e5e52638SMatthew G. Knepley 
6005d083f849SBarry Smith   Collective on dm
6006e5e52638SMatthew G. Knepley 
6007e5e52638SMatthew G. Knepley   Input Parameter:
6008e5e52638SMatthew G. Knepley . dm - The DM
6009e5e52638SMatthew G. Knepley 
6010e5e52638SMatthew G. Knepley   Output Parameter:
6011e5e52638SMatthew G. Knepley . newdm - The DM
6012e5e52638SMatthew G. Knepley 
6013e5e52638SMatthew G. Knepley   Level: advanced
6014e5e52638SMatthew G. Knepley 
6015e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
6016e5e52638SMatthew G. Knepley @*/
6017e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6018e5e52638SMatthew G. Knepley {
6019e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
6020e5e52638SMatthew G. Knepley 
6021e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6022e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
6023e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
6024e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6025e5e52638SMatthew G. Knepley }
6026e5e52638SMatthew G. Knepley 
6027b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
6028b64e0483SPeter Brune {
6029b64e0483SPeter Brune   DM dm_coord,dmc_coord;
6030b64e0483SPeter Brune   PetscErrorCode ierr;
6031b64e0483SPeter Brune   Vec coords,ccoords;
60326dbf9973SLawrence Mitchell   Mat inject;
6033b64e0483SPeter Brune   PetscFunctionBegin;
6034b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
6035b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
6036b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
6037b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
6038b64e0483SPeter Brune   if (coords && !ccoords) {
6039b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
60406668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
60416dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
60422adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
60436dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
6044b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
6045b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
6046b64e0483SPeter Brune   }
6047b64e0483SPeter Brune   PetscFunctionReturn(0);
6048b64e0483SPeter Brune }
6049b64e0483SPeter Brune 
605003dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
605103dadc2fSPeter Brune {
605203dadc2fSPeter Brune   DM dm_coord,subdm_coord;
605303dadc2fSPeter Brune   PetscErrorCode ierr;
605403dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
605503dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
605603dadc2fSPeter Brune   PetscFunctionBegin;
605703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
605803dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
605903dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
606003dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
606103dadc2fSPeter Brune   if (coords && !ccoords) {
606203dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
60636668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
606403dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
606524640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
606603dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
606703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
606803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
60691ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
607003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
607103dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
607203dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
607303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
607403dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
607503dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
607603dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
607703dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
607803dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
607903dadc2fSPeter Brune   }
608003dadc2fSPeter Brune   PetscFunctionReturn(0);
608103dadc2fSPeter Brune }
608203dadc2fSPeter Brune 
6083c73cfb54SMatthew G. Knepley /*@
6084c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
6085c73cfb54SMatthew G. Knepley 
6086c73cfb54SMatthew G. Knepley   Not collective
6087c73cfb54SMatthew G. Knepley 
6088c73cfb54SMatthew G. Knepley   Input Parameter:
6089c73cfb54SMatthew G. Knepley . dm - The DM
6090c73cfb54SMatthew G. Knepley 
6091c73cfb54SMatthew G. Knepley   Output Parameter:
6092c73cfb54SMatthew G. Knepley . dim - The topological dimension
6093c73cfb54SMatthew G. Knepley 
6094c73cfb54SMatthew G. Knepley   Level: beginner
6095c73cfb54SMatthew G. Knepley 
6096c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
6097c73cfb54SMatthew G. Knepley @*/
6098c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6099c73cfb54SMatthew G. Knepley {
6100c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6101c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6102534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6103c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6104c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6105c73cfb54SMatthew G. Knepley }
6106c73cfb54SMatthew G. Knepley 
6107c73cfb54SMatthew G. Knepley /*@
6108c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
6109c73cfb54SMatthew G. Knepley 
6110c73cfb54SMatthew G. Knepley   Collective on dm
6111c73cfb54SMatthew G. Knepley 
6112c73cfb54SMatthew G. Knepley   Input Parameters:
6113c73cfb54SMatthew G. Knepley + dm - The DM
6114c73cfb54SMatthew G. Knepley - dim - The topological dimension
6115c73cfb54SMatthew G. Knepley 
6116c73cfb54SMatthew G. Knepley   Level: beginner
6117c73cfb54SMatthew G. Knepley 
6118c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
6119c73cfb54SMatthew G. Knepley @*/
6120c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6121c73cfb54SMatthew G. Knepley {
6122e5e52638SMatthew G. Knepley   PetscDS        ds;
612345480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6124f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6125f17e8794SMatthew G. Knepley 
6126c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6127c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6128c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6129c73cfb54SMatthew G. Knepley   dm->dim = dim;
613045480ffeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
613145480ffeSMatthew G. Knepley   for (n = 0; n < Nds; ++n) {
613245480ffeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
613345480ffeSMatthew G. Knepley     if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);}
613445480ffeSMatthew G. Knepley   }
6135c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6136c73cfb54SMatthew G. Knepley }
6137c73cfb54SMatthew G. Knepley 
6138793f3fe5SMatthew G. Knepley /*@
6139793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6140793f3fe5SMatthew G. Knepley 
6141d083f849SBarry Smith   Collective on dm
6142793f3fe5SMatthew G. Knepley 
6143793f3fe5SMatthew G. Knepley   Input Parameters:
6144793f3fe5SMatthew G. Knepley + dm - the DM
6145793f3fe5SMatthew G. Knepley - dim - the dimension
6146793f3fe5SMatthew G. Knepley 
6147793f3fe5SMatthew G. Knepley   Output Parameters:
6148793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6149aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6150793f3fe5SMatthew G. Knepley 
6151793f3fe5SMatthew G. Knepley   Note:
6152793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6153a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6154793f3fe5SMatthew G. Knepley   then the interval is empty.
6155793f3fe5SMatthew G. Knepley 
6156793f3fe5SMatthew G. Knepley   Level: intermediate
6157793f3fe5SMatthew G. Knepley 
6158793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
6159793f3fe5SMatthew G. Knepley @*/
6160793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6161793f3fe5SMatthew G. Knepley {
6162793f3fe5SMatthew G. Knepley   PetscInt       d;
6163793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
6164793f3fe5SMatthew G. Knepley 
6165793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6166793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6167793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
6168793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
6169b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
6170793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
6171793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6172793f3fe5SMatthew G. Knepley }
6173793f3fe5SMatthew G. Knepley 
61746636e97aSMatthew G Knepley /*@
61756636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
61766636e97aSMatthew G Knepley 
6177d083f849SBarry Smith   Collective on dm
61786636e97aSMatthew G Knepley 
61796636e97aSMatthew G Knepley   Input Parameters:
61806636e97aSMatthew G Knepley + dm - the DM
61816636e97aSMatthew G Knepley - c - coordinate vector
61826636e97aSMatthew G Knepley 
61832dd40e9bSPatrick Sanan   Notes:
61842dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
61852dd40e9bSPatrick Sanan 
61862dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
61876636e97aSMatthew G Knepley 
61886636e97aSMatthew G Knepley   Level: intermediate
61896636e97aSMatthew G Knepley 
619060c22052SBarry Smith .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
61916636e97aSMatthew G Knepley @*/
61926636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
61936636e97aSMatthew G Knepley {
61946636e97aSMatthew G Knepley   PetscErrorCode ierr;
61956636e97aSMatthew G Knepley 
61966636e97aSMatthew G Knepley   PetscFunctionBegin;
61976636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61986636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
61996636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
62006636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
62016636e97aSMatthew G Knepley   dm->coordinates = c;
62026636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
6203b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
620403dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
62056636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62066636e97aSMatthew G Knepley }
62076636e97aSMatthew G Knepley 
62086636e97aSMatthew G Knepley /*@
62096636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
62106636e97aSMatthew G Knepley 
62117058e716SVaclav Hapla   Not collective
62126636e97aSMatthew G Knepley 
62136636e97aSMatthew G Knepley    Input Parameters:
62146636e97aSMatthew G Knepley +  dm - the DM
62156636e97aSMatthew G Knepley -  c - coordinate vector
62166636e97aSMatthew G Knepley 
62172dd40e9bSPatrick Sanan   Notes:
62186636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
62196636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
62206636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
62216636e97aSMatthew G Knepley 
62222dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
62232dd40e9bSPatrick Sanan 
62246636e97aSMatthew G Knepley   Level: intermediate
62256636e97aSMatthew G Knepley 
62266636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
62276636e97aSMatthew G Knepley @*/
62286636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
62296636e97aSMatthew G Knepley {
62306636e97aSMatthew G Knepley   PetscErrorCode ierr;
62316636e97aSMatthew G Knepley 
62326636e97aSMatthew G Knepley   PetscFunctionBegin;
62336636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62346636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
62356636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
62366636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
62378865f1eaSKarl Rupp 
62386636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
62398865f1eaSKarl Rupp 
62406636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
62416636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62426636e97aSMatthew G Knepley }
62436636e97aSMatthew G Knepley 
62446636e97aSMatthew G Knepley /*@
62456636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
62466636e97aSMatthew G Knepley 
6247d083f849SBarry Smith   Collective on dm
62486636e97aSMatthew G Knepley 
62496636e97aSMatthew G Knepley   Input Parameter:
62506636e97aSMatthew G Knepley . dm - the DM
62516636e97aSMatthew G Knepley 
62526636e97aSMatthew G Knepley   Output Parameter:
62536636e97aSMatthew G Knepley . c - global coordinate vector
62546636e97aSMatthew G Knepley 
62556636e97aSMatthew G Knepley   Note:
625660c22052SBarry Smith   This is a borrowed reference, so the user should NOT destroy this vector. When the DM is
625760c22052SBarry Smith   destroyed the array will no longer be valid.
62586636e97aSMatthew G Knepley 
6259959c7569SPatrick Sanan   Each process has only the locally-owned portion of the global coordinates (does NOT have the ghost coordinates).
62606636e97aSMatthew G Knepley 
62616636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
62626636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
62636636e97aSMatthew G Knepley 
62646636e97aSMatthew G Knepley   Level: intermediate
62656636e97aSMatthew G Knepley 
626660c22052SBarry Smith .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
62676636e97aSMatthew G Knepley @*/
62686636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
62696636e97aSMatthew G Knepley {
62706636e97aSMatthew G Knepley   PetscErrorCode ierr;
62716636e97aSMatthew G Knepley 
62726636e97aSMatthew G Knepley   PetscFunctionBegin;
62736636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62746636e97aSMatthew G Knepley   PetscValidPointer(c,2);
62751f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
62760298fd71SBarry Smith     DM        cdm = NULL;
62771970a576SMatthew G. Knepley     PetscBool localized;
62786636e97aSMatthew G Knepley 
62796636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
62806636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
62811970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
62821970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
62831970a576SMatthew G. Knepley     if (localized) {
62841970a576SMatthew G. Knepley       PetscInt cdim;
62851970a576SMatthew G. Knepley 
62861970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
62871970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
62881970a576SMatthew G. Knepley     }
62896636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
62906636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
62916636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
62926636e97aSMatthew G Knepley   }
62936636e97aSMatthew G Knepley   *c = dm->coordinates;
62946636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62956636e97aSMatthew G Knepley }
62966636e97aSMatthew G Knepley 
62976636e97aSMatthew G Knepley /*@
629881e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
629981e9a530SVaclav Hapla 
6300d083f849SBarry Smith   Collective on dm
630181e9a530SVaclav Hapla 
630281e9a530SVaclav Hapla   Input Parameter:
630381e9a530SVaclav Hapla . dm - the DM
630481e9a530SVaclav Hapla 
630581e9a530SVaclav Hapla   Level: advanced
630681e9a530SVaclav Hapla 
630781e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
630881e9a530SVaclav Hapla @*/
630981e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
631081e9a530SVaclav Hapla {
631181e9a530SVaclav Hapla   PetscErrorCode ierr;
631281e9a530SVaclav Hapla 
631381e9a530SVaclav Hapla   PetscFunctionBegin;
631481e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
631581e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
63161970a576SMatthew G. Knepley     DM        cdm = NULL;
63171970a576SMatthew G. Knepley     PetscBool localized;
63181970a576SMatthew G. Knepley 
631981e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
632081e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
63211970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
63221970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
63231970a576SMatthew G. Knepley     if (localized) {
63241970a576SMatthew G. Knepley       PetscInt cdim;
63251970a576SMatthew G. Knepley 
63261970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
63271970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
63281970a576SMatthew G. Knepley     }
632981e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
633081e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
633181e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
633281e9a530SVaclav Hapla   }
633381e9a530SVaclav Hapla   PetscFunctionReturn(0);
633481e9a530SVaclav Hapla }
633581e9a530SVaclav Hapla 
633681e9a530SVaclav Hapla /*@
63376636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
63386636e97aSMatthew G Knepley 
6339d083f849SBarry Smith   Collective on dm
63406636e97aSMatthew G Knepley 
63416636e97aSMatthew G Knepley   Input Parameter:
63426636e97aSMatthew G Knepley . dm - the DM
63436636e97aSMatthew G Knepley 
63446636e97aSMatthew G Knepley   Output Parameter:
63456636e97aSMatthew G Knepley . c - coordinate vector
63466636e97aSMatthew G Knepley 
63476636e97aSMatthew G Knepley   Note:
63486636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
63496636e97aSMatthew G Knepley 
63506636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
63516636e97aSMatthew G Knepley 
63526636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
63536636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
63546636e97aSMatthew G Knepley 
63556636e97aSMatthew G Knepley   Level: intermediate
63566636e97aSMatthew G Knepley 
635781e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
63586636e97aSMatthew G Knepley @*/
63596636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
63606636e97aSMatthew G Knepley {
63616636e97aSMatthew G Knepley   PetscErrorCode ierr;
63626636e97aSMatthew G Knepley 
63636636e97aSMatthew G Knepley   PetscFunctionBegin;
63646636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63656636e97aSMatthew G Knepley   PetscValidPointer(c,2);
636681e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
63676636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
63686636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63696636e97aSMatthew G Knepley }
63706636e97aSMatthew G Knepley 
637181e9a530SVaclav Hapla /*@
637281e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
637381e9a530SVaclav Hapla 
637481e9a530SVaclav Hapla   Not collective
637581e9a530SVaclav Hapla 
637681e9a530SVaclav Hapla   Input Parameter:
637781e9a530SVaclav Hapla . dm - the DM
637881e9a530SVaclav Hapla 
637981e9a530SVaclav Hapla   Output Parameter:
638081e9a530SVaclav Hapla . c - coordinate vector
638181e9a530SVaclav Hapla 
638281e9a530SVaclav Hapla   Level: advanced
638381e9a530SVaclav Hapla 
638481e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
638581e9a530SVaclav Hapla @*/
638681e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
638781e9a530SVaclav Hapla {
638881e9a530SVaclav Hapla   PetscFunctionBegin;
638981e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
639081e9a530SVaclav Hapla   PetscValidPointer(c,2);
639181e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
63926636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
63936636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63946636e97aSMatthew G Knepley }
63956636e97aSMatthew G Knepley 
63962db98f8dSVaclav Hapla /*@
63972db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
63982db98f8dSVaclav Hapla 
63992db98f8dSVaclav Hapla   Not collective
64002db98f8dSVaclav Hapla 
6401d8d19677SJose E. Roman   Input Parameters:
64022db98f8dSVaclav Hapla + dm - the DM
64032db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
64042db98f8dSVaclav Hapla 
6405d8d19677SJose E. Roman   Output Parameters:
64062db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
64072db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
64082db98f8dSVaclav Hapla 
64092db98f8dSVaclav Hapla   Note:
64102db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
64112db98f8dSVaclav Hapla 
64122db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
64132db98f8dSVaclav Hapla 
64142db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
64152db98f8dSVaclav Hapla 
64162db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
64172db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
64182db98f8dSVaclav Hapla 
64192db98f8dSVaclav Hapla   Level: advanced
64202db98f8dSVaclav Hapla 
64212db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
64222db98f8dSVaclav Hapla @*/
64232db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
64242db98f8dSVaclav Hapla {
64252db98f8dSVaclav Hapla   PetscSection        cs, newcs;
64262db98f8dSVaclav Hapla   Vec                 coords;
64272db98f8dSVaclav Hapla   const PetscScalar   *arr;
64282db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
64292db98f8dSVaclav Hapla   PetscInt            n;
64302db98f8dSVaclav Hapla   PetscErrorCode      ierr;
64312db98f8dSVaclav Hapla 
64322db98f8dSVaclav Hapla   PetscFunctionBegin;
64332db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64342db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
64352db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
64362db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
64372db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
64381bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
64391bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
64402db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
64412db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
64422db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
64432db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
64442db98f8dSVaclav Hapla   if (pCoord) {
64452db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
64462db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
64472db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
64482db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6449ad9ac99dSVaclav Hapla   } else {
6450ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
64512db98f8dSVaclav Hapla   }
6452ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6453ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
64542db98f8dSVaclav Hapla   PetscFunctionReturn(0);
64552db98f8dSVaclav Hapla }
64562db98f8dSVaclav Hapla 
6457f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6458f19dbd58SToby Isaac {
6459f19dbd58SToby Isaac   PetscErrorCode ierr;
6460f19dbd58SToby Isaac 
6461f19dbd58SToby Isaac   PetscFunctionBegin;
6462f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6463f19dbd58SToby Isaac   PetscValidPointer(field,2);
6464f19dbd58SToby Isaac   if (!dm->coordinateField) {
6465f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6466f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6467f19dbd58SToby Isaac     }
6468f19dbd58SToby Isaac   }
6469f19dbd58SToby Isaac   *field = dm->coordinateField;
6470f19dbd58SToby Isaac   PetscFunctionReturn(0);
6471f19dbd58SToby Isaac }
6472f19dbd58SToby Isaac 
6473f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6474f19dbd58SToby Isaac {
6475f19dbd58SToby Isaac   PetscErrorCode ierr;
6476f19dbd58SToby Isaac 
6477f19dbd58SToby Isaac   PetscFunctionBegin;
6478f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6479f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6480c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6481f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6482f19dbd58SToby Isaac   dm->coordinateField = field;
6483f19dbd58SToby Isaac   PetscFunctionReturn(0);
6484f19dbd58SToby Isaac }
6485f19dbd58SToby Isaac 
64866636e97aSMatthew G Knepley /*@
64871cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
64886636e97aSMatthew G Knepley 
6489d083f849SBarry Smith   Collective on dm
64906636e97aSMatthew G Knepley 
64916636e97aSMatthew G Knepley   Input Parameter:
64926636e97aSMatthew G Knepley . dm - the DM
64936636e97aSMatthew G Knepley 
64946636e97aSMatthew G Knepley   Output Parameter:
64956636e97aSMatthew G Knepley . cdm - coordinate DM
64966636e97aSMatthew G Knepley 
64976636e97aSMatthew G Knepley   Level: intermediate
64986636e97aSMatthew G Knepley 
64991cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
65006636e97aSMatthew G Knepley @*/
65016636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
65026636e97aSMatthew G Knepley {
65036636e97aSMatthew G Knepley   PetscErrorCode ierr;
65046636e97aSMatthew G Knepley 
65056636e97aSMatthew G Knepley   PetscFunctionBegin;
65066636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65076636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
65086636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6509308f8a94SToby Isaac     DM cdm;
6510308f8a94SToby Isaac 
651182f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6512308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6513308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6514308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6515308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6516308f8a94SToby Isaac     dm->coordinateDM = cdm;
65176636e97aSMatthew G Knepley   }
65186636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
65196636e97aSMatthew G Knepley   PetscFunctionReturn(0);
65206636e97aSMatthew G Knepley }
6521e87bb0d3SMatthew G Knepley 
65221cfe2091SMatthew G. Knepley /*@
65231cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
65241cfe2091SMatthew G. Knepley 
6525d083f849SBarry Smith   Logically Collective on dm
65261cfe2091SMatthew G. Knepley 
65271cfe2091SMatthew G. Knepley   Input Parameters:
65281cfe2091SMatthew G. Knepley + dm - the DM
65291cfe2091SMatthew G. Knepley - cdm - coordinate DM
65301cfe2091SMatthew G. Knepley 
65311cfe2091SMatthew G. Knepley   Level: intermediate
65321cfe2091SMatthew G. Knepley 
65331cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
65341cfe2091SMatthew G. Knepley @*/
65351cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
65361cfe2091SMatthew G. Knepley {
65371cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
65381cfe2091SMatthew G. Knepley 
65391cfe2091SMatthew G. Knepley   PetscFunctionBegin;
65401cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65411cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6542f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
65431cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
65441cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
65451cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
65461cfe2091SMatthew G. Knepley }
65471cfe2091SMatthew G. Knepley 
654846e270d4SMatthew G. Knepley /*@
654946e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
655046e270d4SMatthew G. Knepley 
655146e270d4SMatthew G. Knepley   Not Collective
655246e270d4SMatthew G. Knepley 
655346e270d4SMatthew G. Knepley   Input Parameter:
655446e270d4SMatthew G. Knepley . dm - The DM object
655546e270d4SMatthew G. Knepley 
655646e270d4SMatthew G. Knepley   Output Parameter:
655746e270d4SMatthew G. Knepley . dim - The embedding dimension
655846e270d4SMatthew G. Knepley 
655946e270d4SMatthew G. Knepley   Level: intermediate
656046e270d4SMatthew G. Knepley 
656192fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
656246e270d4SMatthew G. Knepley @*/
656346e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
656446e270d4SMatthew G. Knepley {
656546e270d4SMatthew G. Knepley   PetscFunctionBegin;
656646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6567534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
65689a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
65699a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
65709a9a41abSToby Isaac   }
657146e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
657246e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
657346e270d4SMatthew G. Knepley }
657446e270d4SMatthew G. Knepley 
657546e270d4SMatthew G. Knepley /*@
657646e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
657746e270d4SMatthew G. Knepley 
657846e270d4SMatthew G. Knepley   Not Collective
657946e270d4SMatthew G. Knepley 
658046e270d4SMatthew G. Knepley   Input Parameters:
658146e270d4SMatthew G. Knepley + dm  - The DM object
658246e270d4SMatthew G. Knepley - dim - The embedding dimension
658346e270d4SMatthew G. Knepley 
658446e270d4SMatthew G. Knepley   Level: intermediate
658546e270d4SMatthew G. Knepley 
658692fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
658746e270d4SMatthew G. Knepley @*/
658846e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
658946e270d4SMatthew G. Knepley {
6590e5e52638SMatthew G. Knepley   PetscDS        ds;
659145480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6592f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6593f17e8794SMatthew G. Knepley 
659446e270d4SMatthew G. Knepley   PetscFunctionBegin;
659546e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
659646e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
659745480ffeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
659845480ffeSMatthew G. Knepley   for (n = 0; n < Nds; ++n) {
659945480ffeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
6600e5e52638SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
660145480ffeSMatthew G. Knepley   }
660246e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
660346e270d4SMatthew G. Knepley }
660446e270d4SMatthew G. Knepley 
6605e8abe2deSMatthew G. Knepley /*@
6606e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6607e8abe2deSMatthew G. Knepley 
6608d083f849SBarry Smith   Collective on dm
6609e8abe2deSMatthew G. Knepley 
6610e8abe2deSMatthew G. Knepley   Input Parameter:
6611e8abe2deSMatthew G. Knepley . dm - The DM object
6612e8abe2deSMatthew G. Knepley 
6613e8abe2deSMatthew G. Knepley   Output Parameter:
6614e8abe2deSMatthew G. Knepley . section - The PetscSection object
6615e8abe2deSMatthew G. Knepley 
6616e8abe2deSMatthew G. Knepley   Level: intermediate
6617e8abe2deSMatthew G. Knepley 
661892fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6619e8abe2deSMatthew G. Knepley @*/
6620e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6621e8abe2deSMatthew G. Knepley {
6622e8abe2deSMatthew G. Knepley   DM             cdm;
6623e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6624e8abe2deSMatthew G. Knepley 
6625e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6626e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6627e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6628e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
662992fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6630e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6631e8abe2deSMatthew G. Knepley }
6632e8abe2deSMatthew G. Knepley 
6633e8abe2deSMatthew G. Knepley /*@
6634e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6635e8abe2deSMatthew G. Knepley 
6636e8abe2deSMatthew G. Knepley   Not Collective
6637e8abe2deSMatthew G. Knepley 
6638e8abe2deSMatthew G. Knepley   Input Parameters:
6639e8abe2deSMatthew G. Knepley + dm      - The DM object
664046e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6641e8abe2deSMatthew G. Knepley - section - The PetscSection object
6642e8abe2deSMatthew G. Knepley 
6643e8abe2deSMatthew G. Knepley   Level: intermediate
6644e8abe2deSMatthew G. Knepley 
664592fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6646e8abe2deSMatthew G. Knepley @*/
664746e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6648e8abe2deSMatthew G. Knepley {
6649e8abe2deSMatthew G. Knepley   DM             cdm;
6650e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6651e8abe2deSMatthew G. Knepley 
6652e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6653e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
665446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6655e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
665692fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
665746e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
66584c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
665946e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
666046e270d4SMatthew G. Knepley 
666146e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
666246e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
666346e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
666446e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
666546e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
666646e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
666746e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
666846e270d4SMatthew G. Knepley     }
6669ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
667046e270d4SMatthew G. Knepley   }
6671e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6672e8abe2deSMatthew G. Knepley }
6673e8abe2deSMatthew G. Knepley 
6674d864a3eaSLisandro Dalcin /*@
6675d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6676d864a3eaSLisandro Dalcin 
6677d864a3eaSLisandro Dalcin   Input Parameters:
6678d864a3eaSLisandro Dalcin + dm      - The DM object
6679d864a3eaSLisandro Dalcin - disc    - The new coordinate discretization
6680d864a3eaSLisandro Dalcin 
6681d864a3eaSLisandro Dalcin   Level: intermediate
6682d864a3eaSLisandro Dalcin 
6683d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6684d864a3eaSLisandro Dalcin @*/
6685d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6686d864a3eaSLisandro Dalcin {
6687d864a3eaSLisandro Dalcin   PetscObject    discOld;
6688d864a3eaSLisandro Dalcin   PetscClassId   classid;
6689d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6690d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6691d864a3eaSLisandro Dalcin   Mat            matInterp;
6692d864a3eaSLisandro Dalcin   PetscErrorCode ierr;
6693d864a3eaSLisandro Dalcin 
6694d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6695d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6696d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6697d864a3eaSLisandro Dalcin 
6698d864a3eaSLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdmOld);CHKERRQ(ierr);
6699d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
6700d864a3eaSLisandro Dalcin   ierr = DMGetField(cdmOld, 0, NULL, &discOld);CHKERRQ(ierr);
6701d864a3eaSLisandro Dalcin   ierr = PetscObjectGetClassId(discOld, &classid);CHKERRQ(ierr);
670229ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
670329ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
670429ad44c5SMatthew G. Knepley       PetscFE        feLinear;
670529ad44c5SMatthew G. Knepley       DMPolytopeType ct;
670629ad44c5SMatthew G. Knepley       PetscInt       dim, dE, cStart;
670729ad44c5SMatthew G. Knepley       PetscBool      simplex;
670829ad44c5SMatthew G. Knepley 
670929ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
671029ad44c5SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
671129ad44c5SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
671229ad44c5SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdmOld, 0, &cStart, NULL);CHKERRQ(ierr);
671329ad44c5SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
671429ad44c5SMatthew G. Knepley       switch (ct) {
671529ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM:
671629ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
671729ad44c5SMatthew G. Knepley           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
671829ad44c5SMatthew G. Knepley         default: break;
671929ad44c5SMatthew G. Knepley       }
672029ad44c5SMatthew G. Knepley       simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
672129ad44c5SMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear);CHKERRQ(ierr);
672229ad44c5SMatthew G. Knepley       ierr = DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear);CHKERRQ(ierr);
672329ad44c5SMatthew G. Knepley       ierr = PetscFEDestroy(&feLinear);CHKERRQ(ierr);
672429ad44c5SMatthew G. Knepley       ierr = DMCreateDS(cdmOld);CHKERRQ(ierr);
672529ad44c5SMatthew G. Knepley     } else {
672629ad44c5SMatthew G. Knepley       const char *discname;
672729ad44c5SMatthew G. Knepley 
672829ad44c5SMatthew G. Knepley       ierr = PetscObjectGetType(discOld, &discname);CHKERRQ(ierr);
672929ad44c5SMatthew G. Knepley       SETERRQ1(PetscObjectComm(discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
673029ad44c5SMatthew G. Knepley     }
673129ad44c5SMatthew G. Knepley   }
6732d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6733d864a3eaSLisandro Dalcin   ierr = DMClone(cdmOld, &cdmNew);CHKERRQ(ierr);
6734d864a3eaSLisandro Dalcin   ierr = DMSetField(cdmNew, 0, NULL, (PetscObject) disc);CHKERRQ(ierr);
6735d864a3eaSLisandro Dalcin   ierr = DMCreateDS(cdmNew);CHKERRQ(ierr);
6736d864a3eaSLisandro Dalcin   /* Project the coordinate vector from old to new space  */
6737d864a3eaSLisandro Dalcin   ierr = DMGetCoordinates(dm, &coordsOld);CHKERRQ(ierr);
6738d864a3eaSLisandro Dalcin   ierr = DMCreateGlobalVector(cdmNew, &coordsNew);CHKERRQ(ierr);
6739d864a3eaSLisandro Dalcin   ierr = DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL);CHKERRQ(ierr);
6740d864a3eaSLisandro Dalcin   ierr = MatInterpolate(matInterp, coordsOld, coordsNew);CHKERRQ(ierr);
6741d864a3eaSLisandro Dalcin   ierr = MatDestroy(&matInterp);CHKERRQ(ierr);
6742d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6743d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateField(dm, NULL);CHKERRQ(ierr);
6744d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateDM(dm, cdmNew);CHKERRQ(ierr);
6745d864a3eaSLisandro Dalcin   ierr = DMSetCoordinates(dm, coordsNew);CHKERRQ(ierr);
6746d864a3eaSLisandro Dalcin   ierr = VecDestroy(&coordsNew);CHKERRQ(ierr);
6747d864a3eaSLisandro Dalcin   ierr = DMDestroy(&cdmNew);CHKERRQ(ierr);
6748d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6749d864a3eaSLisandro Dalcin }
6750d864a3eaSLisandro Dalcin 
67515dc8c3f7SMatthew G. Knepley /*@C
675290b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
67535dc8c3f7SMatthew G. Knepley 
6754f899ff85SJose E. Roman   Input Parameter:
675590b157c4SStefano Zampini . dm      - The DM object
675690b157c4SStefano Zampini 
675790b157c4SStefano Zampini   Output Parameters:
675890b157c4SStefano Zampini + per     - Whether the DM is periodic or not
67595dc8c3f7SMatthew 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
67605dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
67615dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
67625dc8c3f7SMatthew G. Knepley 
67635dc8c3f7SMatthew G. Knepley   Level: developer
67645dc8c3f7SMatthew G. Knepley 
67655dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
67665dc8c3f7SMatthew G. Knepley @*/
676790b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6768c6b900c6SMatthew G. Knepley {
6769c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6770c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
677190b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6772c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6773c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
67745dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6775c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6776c6b900c6SMatthew G. Knepley }
6777c6b900c6SMatthew G. Knepley 
67785dc8c3f7SMatthew G. Knepley /*@C
67795dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
67805dc8c3f7SMatthew G. Knepley 
67815dc8c3f7SMatthew G. Knepley   Input Parameters:
67825dc8c3f7SMatthew G. Knepley + dm      - The DM object
6783db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6784db2bf62eSStefano 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.
67855dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
67865dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
67875dc8c3f7SMatthew G. Knepley 
6788db2bf62eSStefano 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.
6789db2bf62eSStefano Zampini 
67905dc8c3f7SMatthew G. Knepley   Level: developer
67915dc8c3f7SMatthew G. Knepley 
67925dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
67935dc8c3f7SMatthew G. Knepley @*/
679490b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6795c6b900c6SMatthew G. Knepley {
6796c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6797c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6798c6b900c6SMatthew G. Knepley 
6799c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6800c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
680190b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6802412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6803412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6804412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
68055dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
680690b157c4SStefano Zampini   if (maxCell) {
6807412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6808412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6809db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6810db2bf62eSStefano Zampini     ierr = PetscFree(dm->maxCell);CHKERRQ(ierr);
6811412e9a14SMatthew G. Knepley   }
6812db2bf62eSStefano Zampini 
6813412e9a14SMatthew G. Knepley   if (L) {
6814412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6815412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6816412e9a14SMatthew G. Knepley   }
6817412e9a14SMatthew G. Knepley   if (bd) {
6818412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6819412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
682090b157c4SStefano Zampini   }
6821072d7d67SStefano Zampini   dm->periodic = per;
6822c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6823c6b900c6SMatthew G. Knepley }
6824c6b900c6SMatthew G. Knepley 
68252e17dfb7SMatthew G. Knepley /*@
68262e17dfb7SMatthew 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.
68272e17dfb7SMatthew G. Knepley 
68282e17dfb7SMatthew G. Knepley   Input Parameters:
68292e17dfb7SMatthew G. Knepley + dm     - The DM
683065da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
683165da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
68322e17dfb7SMatthew G. Knepley 
68332e17dfb7SMatthew G. Knepley   Output Parameter:
68342e17dfb7SMatthew G. Knepley . out - The localized coordinate point
68352e17dfb7SMatthew G. Knepley 
68362e17dfb7SMatthew G. Knepley   Level: developer
68372e17dfb7SMatthew G. Knepley 
68382e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
68392e17dfb7SMatthew G. Knepley @*/
684065da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
68412e17dfb7SMatthew G. Knepley {
68422e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
68432e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
68442e17dfb7SMatthew G. Knepley 
68452e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68462e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
68472e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68482e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68492e17dfb7SMatthew G. Knepley   } else {
685065da65dcSMatthew G. Knepley     if (endpoint) {
685165da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6852da3333bfSMatthew 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)) {
6853da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
685465da65dcSMatthew G. Knepley         } else {
6855da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
685665da65dcSMatthew G. Knepley         }
685765da65dcSMatthew G. Knepley       }
685865da65dcSMatthew G. Knepley     } else {
68592e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
68601118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
68612e17dfb7SMatthew G. Knepley       }
68622e17dfb7SMatthew G. Knepley     }
686365da65dcSMatthew G. Knepley   }
68642e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68652e17dfb7SMatthew G. Knepley }
68662e17dfb7SMatthew G. Knepley 
68672e17dfb7SMatthew G. Knepley /*
68682e17dfb7SMatthew 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.
68692e17dfb7SMatthew G. Knepley 
68702e17dfb7SMatthew G. Knepley   Input Parameters:
68712e17dfb7SMatthew G. Knepley + dm     - The DM
68722e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
68732e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
68742e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
68752e17dfb7SMatthew G. Knepley 
68762e17dfb7SMatthew G. Knepley   Output Parameter:
68772e17dfb7SMatthew G. Knepley . out - The localized coordinate point
68782e17dfb7SMatthew G. Knepley 
68792e17dfb7SMatthew G. Knepley   Level: developer
68802e17dfb7SMatthew G. Knepley 
68812e17dfb7SMatthew 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
68822e17dfb7SMatthew G. Knepley 
68832e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
68842e17dfb7SMatthew G. Knepley */
68852e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
68862e17dfb7SMatthew G. Knepley {
68872e17dfb7SMatthew G. Knepley   PetscInt d;
68882e17dfb7SMatthew G. Knepley 
68892e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68902e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68912e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68922e17dfb7SMatthew G. Knepley   } else {
68932e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6894908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
68952e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
68962e17dfb7SMatthew G. Knepley       } else {
68972e17dfb7SMatthew G. Knepley         out[d] = in[d];
68982e17dfb7SMatthew G. Knepley       }
68992e17dfb7SMatthew G. Knepley     }
69002e17dfb7SMatthew G. Knepley   }
69012e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69022e17dfb7SMatthew G. Knepley }
6903a5801f52SStefano Zampini 
69042e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
69052e17dfb7SMatthew G. Knepley {
69062e17dfb7SMatthew G. Knepley   PetscInt d;
69072e17dfb7SMatthew G. Knepley 
69082e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69092e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
69102e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
69112e17dfb7SMatthew G. Knepley   } else {
69122e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6913908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
69142e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
69152e17dfb7SMatthew G. Knepley       } else {
69162e17dfb7SMatthew G. Knepley         out[d] = in[d];
69172e17dfb7SMatthew G. Knepley       }
69182e17dfb7SMatthew G. Knepley     }
69192e17dfb7SMatthew G. Knepley   }
69202e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69212e17dfb7SMatthew G. Knepley }
69222e17dfb7SMatthew G. Knepley 
69232e17dfb7SMatthew G. Knepley /*
69242e17dfb7SMatthew 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.
69252e17dfb7SMatthew G. Knepley 
69262e17dfb7SMatthew G. Knepley   Input Parameters:
69272e17dfb7SMatthew G. Knepley + dm     - The DM
69282e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
69292e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
69302e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
69312e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
69322e17dfb7SMatthew G. Knepley 
69332e17dfb7SMatthew G. Knepley   Output Parameter:
69342e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
69352e17dfb7SMatthew G. Knepley 
69362e17dfb7SMatthew G. Knepley   Level: developer
69372e17dfb7SMatthew G. Knepley 
69382e17dfb7SMatthew 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
69392e17dfb7SMatthew G. Knepley 
69402e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
69412e17dfb7SMatthew G. Knepley */
69422e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
69432e17dfb7SMatthew G. Knepley {
69442e17dfb7SMatthew G. Knepley   PetscInt d;
69452e17dfb7SMatthew G. Knepley 
69462e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69472e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
69482e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
69492e17dfb7SMatthew G. Knepley   } else {
69502e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6951412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6952412e9a14SMatthew G. Knepley 
6953412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6954412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6955412e9a14SMatthew G. Knepley 
6956412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6957412e9a14SMatthew G. Knepley           SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%D-Coordinate %g more than %g away from anchor %g", d, (double) PetscRealPart(in[d]), (double) maxC, (double) PetscRealPart(anchor[d]));
6958412e9a14SMatthew G. Knepley         out[d] += newCoord;
69592e17dfb7SMatthew G. Knepley       } else {
69602e17dfb7SMatthew G. Knepley         out[d] += in[d];
69612e17dfb7SMatthew G. Knepley       }
69622e17dfb7SMatthew G. Knepley     }
69632e17dfb7SMatthew G. Knepley   }
69642e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69652e17dfb7SMatthew G. Knepley }
69662e17dfb7SMatthew G. Knepley 
696736447a5eSToby Isaac /*@
69688f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
69698f700142SStefano Zampini 
69708f700142SStefano Zampini   Not collective
697136447a5eSToby Isaac 
697236447a5eSToby Isaac   Input Parameter:
697336447a5eSToby Isaac . dm - The DM
697436447a5eSToby Isaac 
697536447a5eSToby Isaac   Output Parameter:
697636447a5eSToby Isaac   areLocalized - True if localized
697736447a5eSToby Isaac 
697836447a5eSToby Isaac   Level: developer
697936447a5eSToby Isaac 
69808f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
698136447a5eSToby Isaac @*/
69828f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
698336447a5eSToby Isaac {
698436447a5eSToby Isaac   DM             cdm;
698536447a5eSToby Isaac   PetscSection   coordSection;
69862ccac677SMatthew G. Knepley   PetscInt       depth, cStart, cEnd, sStart, sEnd, c, dof;
698746a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
698836447a5eSToby Isaac   PetscErrorCode ierr;
698936447a5eSToby Isaac 
699036447a5eSToby Isaac   PetscFunctionBegin;
699136447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6992534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
69938b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
699446a3a80fSLisandro Dalcin 
699536447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
699636447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
699746a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
69989f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
69992ccac677SMatthew G. Knepley   ierr = DMPlexGetDepth(cdm, &depth);CHKERRQ(ierr);
70002ccac677SMatthew G. Knepley   if (!depth) PetscFunctionReturn(0);
700146a3a80fSLisandro Dalcin 
70029f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
700346a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
700436447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
700536447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
700646a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
700746a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
700836447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
700946a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
701036447a5eSToby Isaac   }
70118f700142SStefano Zampini   *areLocalized = alreadyLocalized;
701236447a5eSToby Isaac   PetscFunctionReturn(0);
701336447a5eSToby Isaac }
701436447a5eSToby Isaac 
70158f700142SStefano Zampini /*@
70168f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
70178f700142SStefano Zampini 
70188f700142SStefano Zampini   Collective on dm
70198f700142SStefano Zampini 
70208f700142SStefano Zampini   Input Parameter:
70218f700142SStefano Zampini . dm - The DM
70228f700142SStefano Zampini 
70238f700142SStefano Zampini   Output Parameter:
70248f700142SStefano Zampini   areLocalized - True if localized
70258f700142SStefano Zampini 
70268f700142SStefano Zampini   Level: developer
70278f700142SStefano Zampini 
70288f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
70298f700142SStefano Zampini @*/
70308f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
70318f700142SStefano Zampini {
70328f700142SStefano Zampini   PetscBool      localized;
70338f700142SStefano Zampini   PetscErrorCode ierr;
70348f700142SStefano Zampini 
70358f700142SStefano Zampini   PetscFunctionBegin;
70368f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7037534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
70388f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
7039820f2d46SBarry Smith   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
70408f700142SStefano Zampini   PetscFunctionReturn(0);
70418f700142SStefano Zampini }
704236447a5eSToby Isaac 
70432e17dfb7SMatthew G. Knepley /*@
7044492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
70452e17dfb7SMatthew G. Knepley 
70468f700142SStefano Zampini   Collective on dm
70478f700142SStefano Zampini 
70482e17dfb7SMatthew G. Knepley   Input Parameter:
70492e17dfb7SMatthew G. Knepley . dm - The DM
70502e17dfb7SMatthew G. Knepley 
70512e17dfb7SMatthew G. Knepley   Level: developer
70522e17dfb7SMatthew G. Knepley 
70538f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
70542e17dfb7SMatthew G. Knepley @*/
70552e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
70562e17dfb7SMatthew G. Knepley {
70572e17dfb7SMatthew G. Knepley   DM             cdm;
70582e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
70592e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
70603e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
70613e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
7062e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
70633e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
70643e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
70652e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
70662e17dfb7SMatthew G. Knepley 
70672e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
70682e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
706992c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
7070f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
7071f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
7072f7cbd40bSStefano Zampini 
70732e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
70742e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
70752e17dfb7SMatthew G. Knepley   {
70762e17dfb7SMatthew G. Knepley     PetscBool isplex;
70772e17dfb7SMatthew G. Knepley 
70782e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
70792e17dfb7SMatthew G. Knepley     if (isplex) {
70802e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
70813e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
708269291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
70833e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
70843e922f36SToby Isaac       newStart = vStart;
70853e922f36SToby Isaac       newEnd   = vEnd;
70863e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
70873e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
70883e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
70893e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
70903e922f36SToby Isaac       }
70912e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
70922e17dfb7SMatthew G. Knepley   }
70932e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
709443eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
70952e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
70963e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
7097e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
70983e922f36SToby Isaac 
70992e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
71002e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
71012e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
71022e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
71033e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
71043e922f36SToby Isaac 
710569291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
71063e922f36SToby Isaac   localized = &anchor[bs];
71073e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
71083e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
71093e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
71103e922f36SToby Isaac 
71113e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
71123e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
71133e922f36SToby Isaac       PetscInt     b;
71143e922f36SToby Isaac 
71153e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
71163e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
71173e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
71183e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
71193e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
71203e922f36SToby Isaac         for (b = 0; b < bs; b++) {
71213e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
71223e922f36SToby Isaac         }
71233e922f36SToby Isaac         if (b < bs) break;
71243e922f36SToby Isaac       }
71253e922f36SToby Isaac       if (d < dof/bs) {
71263e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
71273e922f36SToby Isaac           PetscInt cdof;
71283e922f36SToby Isaac 
71293e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
71303e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
71313e922f36SToby Isaac         }
71323e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
71333e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
71343e922f36SToby Isaac       }
71353e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
71363e922f36SToby Isaac     }
71373e922f36SToby Isaac   }
7138ffc4695bSBarry Smith   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
71393e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
714069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
71413e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
714269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
71433e922f36SToby Isaac     PetscFunctionReturn(0);
71443e922f36SToby Isaac   }
71452e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
71462e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
71472e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
71482e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
71492e17dfb7SMatthew G. Knepley   }
71502e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
71512e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
7152c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
71532e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
71542e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
71552e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
71562e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
7157c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
71582e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
71592e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
71602e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
71612e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
71622e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
71632e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
71642e17dfb7SMatthew G. Knepley   }
71653e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
71663e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
71673e922f36SToby Isaac 
71682e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
71692e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
71703e922f36SToby Isaac       PetscInt     b, cdof;
71712e17dfb7SMatthew G. Knepley 
71723e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
71733e922f36SToby Isaac       if (!cdof) continue;
71742e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
71752e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
71762e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
71772e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
71782e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
71792e17dfb7SMatthew G. Knepley     }
71803e922f36SToby Isaac   }
718169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
718269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
7183c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
71842e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
71852e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
71862e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
71872e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
71882e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
71892e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
71902e17dfb7SMatthew G. Knepley }
71912e17dfb7SMatthew G. Knepley 
7192e87bb0d3SMatthew G Knepley /*@
71933a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
7194e87bb0d3SMatthew G Knepley 
7195d083f849SBarry Smith   Collective on v (see explanation below)
7196e87bb0d3SMatthew G Knepley 
7197e87bb0d3SMatthew G Knepley   Input Parameters:
7198e87bb0d3SMatthew G Knepley + dm - The DM
71996b867d5aSJose E. Roman - ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
7200e87bb0d3SMatthew G Knepley 
72016b867d5aSJose E. Roman   Input/Output Parameters:
72026b867d5aSJose E. Roman + v - The Vec of points, on output contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
72036b867d5aSJose E. Roman - cellSF - Points to either NULL, or a PetscSF with guesses for which cells contain each point;
72046b867d5aSJose E. Roman            on output, the PetscSF containing the ranks and local indices of the containing points
72053a93e3b7SToby Isaac 
7206e87bb0d3SMatthew G Knepley   Level: developer
720761e3bb9bSMatthew G Knepley 
720862a38674SMatthew G. Knepley   Notes:
72093a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
721062a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
72113a93e3b7SToby Isaac 
72123a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
721362a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
72143a93e3b7SToby Isaac 
72153a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
72163a93e3b7SToby Isaac 
721762a38674SMatthew G. Knepley $    const PetscSFNode *cells;
721862a38674SMatthew G. Knepley $    PetscInt           nFound;
7219a6216909SToby Isaac $    const PetscInt    *found;
722062a38674SMatthew G. Knepley $
7221a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
72223a93e3b7SToby Isaac 
72233a93e3b7SToby 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
72243a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
72253a93e3b7SToby Isaac 
722662a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
722761e3bb9bSMatthew G Knepley @*/
722862a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
7229e87bb0d3SMatthew G Knepley {
7230735aa83eSMatthew G Knepley   PetscErrorCode ierr;
7231735aa83eSMatthew G Knepley 
7232e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
7233e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7234e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
7235e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
72363a93e3b7SToby Isaac   if (*cellSF) {
72373a93e3b7SToby Isaac     PetscMPIInt result;
72383a93e3b7SToby Isaac 
7239e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
7240ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRMPI(ierr);
72413a93e3b7SToby Isaac     if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"cellSF must have a communicator congruent to v's");
7242e0fc9d1bSMatthew G. Knepley   } else {
72433a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
72443a93e3b7SToby Isaac   }
7245b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
724647a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
724762a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
724847a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
7249e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
7250e87bb0d3SMatthew G Knepley }
725114f150ffSMatthew G. Knepley 
7252f4d763aaSMatthew G. Knepley /*@
7253f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
7254f4d763aaSMatthew G. Knepley 
72558f700142SStefano Zampini   Collective on dm
72568f700142SStefano Zampini 
7257f4d763aaSMatthew G. Knepley   Input Parameter:
7258f4d763aaSMatthew G. Knepley . dm - The original DM
7259f4d763aaSMatthew G. Knepley 
7260f4d763aaSMatthew G. Knepley   Output Parameter:
7261f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
7262f4d763aaSMatthew G. Knepley 
7263f4d763aaSMatthew G. Knepley   Level: intermediate
7264f4d763aaSMatthew G. Knepley 
7265e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
7266f4d763aaSMatthew G. Knepley @*/
726714f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
726814f150ffSMatthew G. Knepley {
7269c26acbdeSMatthew G. Knepley   PetscSection   section;
72702d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
727114f150ffSMatthew G. Knepley   PetscErrorCode ierr;
727214f150ffSMatthew G. Knepley 
727314f150ffSMatthew G. Knepley   PetscFunctionBegin;
727414f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
727514f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
727692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
7277c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
7278ffc4695bSBarry Smith   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
72792d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
7280c26acbdeSMatthew G. Knepley     *odm = dm;
7281c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
7282c26acbdeSMatthew G. Knepley   }
728314f150ffSMatthew G. Knepley   if (!dm->dmBC) {
7284c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
728514f150ffSMatthew G. Knepley     PetscSF      sf;
728614f150ffSMatthew G. Knepley 
728714f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
7288e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
728914f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
729092fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
729114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
729214f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
729315b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
7294e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
729514f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
729614f150ffSMatthew G. Knepley   }
729714f150ffSMatthew G. Knepley   *odm = dm->dmBC;
729814f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
729914f150ffSMatthew G. Knepley }
7300f4d763aaSMatthew G. Knepley 
7301f4d763aaSMatthew G. Knepley /*@
7302cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7303f4d763aaSMatthew G. Knepley 
7304f4d763aaSMatthew G. Knepley   Input Parameter:
7305f4d763aaSMatthew G. Knepley . dm - The original DM
7306f4d763aaSMatthew G. Knepley 
7307cdb7a50dSMatthew G. Knepley   Output Parameters:
7308cdb7a50dSMatthew G. Knepley + num - The output sequence number
7309cdb7a50dSMatthew G. Knepley - val - The output sequence value
7310f4d763aaSMatthew G. Knepley 
7311f4d763aaSMatthew G. Knepley   Level: intermediate
7312f4d763aaSMatthew G. Knepley 
7313f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7314f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7315f4d763aaSMatthew G. Knepley 
7316f4d763aaSMatthew G. Knepley .seealso: VecView()
7317f4d763aaSMatthew G. Knepley @*/
7318cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7319f4d763aaSMatthew G. Knepley {
7320f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7321f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7322534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7323534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7324f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7325f4d763aaSMatthew G. Knepley }
7326f4d763aaSMatthew G. Knepley 
7327f4d763aaSMatthew G. Knepley /*@
7328cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7329f4d763aaSMatthew G. Knepley 
7330f4d763aaSMatthew G. Knepley   Input Parameters:
7331f4d763aaSMatthew G. Knepley + dm - The original DM
7332cdb7a50dSMatthew G. Knepley . num - The output sequence number
7333cdb7a50dSMatthew G. Knepley - val - The output sequence value
7334f4d763aaSMatthew G. Knepley 
7335f4d763aaSMatthew G. Knepley   Level: intermediate
7336f4d763aaSMatthew G. Knepley 
7337f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7338f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7339f4d763aaSMatthew G. Knepley 
7340f4d763aaSMatthew G. Knepley .seealso: VecView()
7341f4d763aaSMatthew G. Knepley @*/
7342cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7343f4d763aaSMatthew G. Knepley {
7344f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7345f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7346f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7347cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7348cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7349cdb7a50dSMatthew G. Knepley }
7350cdb7a50dSMatthew G. Knepley 
7351cdb7a50dSMatthew G. Knepley /*@C
7352cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7353cdb7a50dSMatthew G. Knepley 
7354cdb7a50dSMatthew G. Knepley   Input Parameters:
7355cdb7a50dSMatthew G. Knepley + dm   - The original DM
7356cdb7a50dSMatthew G. Knepley . name - The sequence name
7357cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7358cdb7a50dSMatthew G. Knepley 
7359cdb7a50dSMatthew G. Knepley   Output Parameter:
7360cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7361cdb7a50dSMatthew G. Knepley 
7362cdb7a50dSMatthew G. Knepley   Level: intermediate
7363cdb7a50dSMatthew G. Knepley 
7364cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7365cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7366cdb7a50dSMatthew G. Knepley 
7367cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7368cdb7a50dSMatthew G. Knepley @*/
7369cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7370cdb7a50dSMatthew G. Knepley {
7371cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7372cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
7373cdb7a50dSMatthew G. Knepley 
7374cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7375cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7376cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7377064a246eSJacob Faibussowitsch   PetscValidRealPointer(val,5);
7378cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
7379cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7380cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7381cdb7a50dSMatthew G. Knepley     PetscScalar value;
7382cdb7a50dSMatthew G. Knepley 
738339d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
73844aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7385cdb7a50dSMatthew G. Knepley #endif
7386cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7387f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7388f4d763aaSMatthew G. Knepley }
73898e4ac7eaSMatthew G. Knepley 
73908e4ac7eaSMatthew G. Knepley /*@
73918e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
73928e4ac7eaSMatthew G. Knepley 
73938e4ac7eaSMatthew G. Knepley   Not collective
73948e4ac7eaSMatthew G. Knepley 
73958e4ac7eaSMatthew G. Knepley   Input Parameter:
73968e4ac7eaSMatthew G. Knepley . dm - The DM
73978e4ac7eaSMatthew G. Knepley 
73988e4ac7eaSMatthew G. Knepley   Output Parameter:
73998e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
74008e4ac7eaSMatthew G. Knepley 
74018e4ac7eaSMatthew G. Knepley   Level: beginner
74028e4ac7eaSMatthew G. Knepley 
74038e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
74048e4ac7eaSMatthew G. Knepley @*/
74058e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
74068e4ac7eaSMatthew G. Knepley {
74078e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
74088e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7409534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
74108e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
74118e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
74128e4ac7eaSMatthew G. Knepley }
74138e4ac7eaSMatthew G. Knepley 
74148e4ac7eaSMatthew G. Knepley /*@
74155d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
74168e4ac7eaSMatthew G. Knepley 
74178e4ac7eaSMatthew G. Knepley   Collective on dm
74188e4ac7eaSMatthew G. Knepley 
74198e4ac7eaSMatthew G. Knepley   Input Parameters:
74208e4ac7eaSMatthew G. Knepley + dm - The DM
74218e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
74228e4ac7eaSMatthew G. Knepley 
74235d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
74245d3b26e6SMatthew G. Knepley 
74258e4ac7eaSMatthew G. Knepley   Level: beginner
74268e4ac7eaSMatthew G. Knepley 
74275d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
74288e4ac7eaSMatthew G. Knepley @*/
74298e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
74308e4ac7eaSMatthew G. Knepley {
74318e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
74328e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74338833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
74348e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
74358e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
74368e4ac7eaSMatthew G. Knepley }
7437c58f1c22SToby Isaac 
7438c58f1c22SToby Isaac /*@C
7439c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7440c58f1c22SToby Isaac 
7441c58f1c22SToby Isaac   Not Collective
7442c58f1c22SToby Isaac 
7443c58f1c22SToby Isaac   Input Parameters:
7444c58f1c22SToby Isaac + dm   - The DM object
7445c58f1c22SToby Isaac - name - The label name
7446c58f1c22SToby Isaac 
7447c58f1c22SToby Isaac   Level: intermediate
7448c58f1c22SToby Isaac 
7449c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7450c58f1c22SToby Isaac @*/
7451c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7452c58f1c22SToby Isaac {
74535d80c0bfSVaclav Hapla   PetscBool      flg;
74545d80c0bfSVaclav Hapla   DMLabel        label;
7455c58f1c22SToby Isaac   PetscErrorCode ierr;
7456c58f1c22SToby Isaac 
7457c58f1c22SToby Isaac   PetscFunctionBegin;
7458c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7459c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
74605d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7461c58f1c22SToby Isaac   if (!flg) {
74625d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
74635d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
74645d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7465c58f1c22SToby Isaac   }
7466c58f1c22SToby Isaac   PetscFunctionReturn(0);
7467c58f1c22SToby Isaac }
7468c58f1c22SToby Isaac 
7469c58f1c22SToby Isaac /*@C
74700fdc7489SMatthew Knepley   DMCreateLabelAtIndex - Create a label of the given name at the iven index. If it already exists, move it to this index.
74710fdc7489SMatthew Knepley 
74720fdc7489SMatthew Knepley   Not Collective
74730fdc7489SMatthew Knepley 
74740fdc7489SMatthew Knepley   Input Parameters:
74750fdc7489SMatthew Knepley + dm   - The DM object
74760fdc7489SMatthew Knepley . l    - The index for the label
74770fdc7489SMatthew Knepley - name - The label name
74780fdc7489SMatthew Knepley 
74790fdc7489SMatthew Knepley   Level: intermediate
74800fdc7489SMatthew Knepley 
74810fdc7489SMatthew Knepley .seealso: DMCreateLabel(), DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
74820fdc7489SMatthew Knepley @*/
74830fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
74840fdc7489SMatthew Knepley {
74850fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
74860fdc7489SMatthew Knepley   DMLabel        label;
74870fdc7489SMatthew Knepley   PetscInt       Nl, m;
74880fdc7489SMatthew Knepley   PetscBool      flg, match;
74890fdc7489SMatthew Knepley   const char    *lname;
74900fdc7489SMatthew Knepley   PetscErrorCode ierr;
74910fdc7489SMatthew Knepley 
74920fdc7489SMatthew Knepley   PetscFunctionBegin;
74930fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7494064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
74950fdc7489SMatthew Knepley   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
74960fdc7489SMatthew Knepley   if (!flg) {
74970fdc7489SMatthew Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
74980fdc7489SMatthew Knepley     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
74990fdc7489SMatthew Knepley     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
75000fdc7489SMatthew Knepley   }
75010fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
75020fdc7489SMatthew Knepley   if (l >= Nl) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %D must be in [0, %D)", l, Nl);
75030fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
75040fdc7489SMatthew Knepley     ierr = PetscObjectGetName((PetscObject) orig->label, &lname);CHKERRQ(ierr);
75050fdc7489SMatthew Knepley     ierr = PetscStrcmp(name, lname, &match);CHKERRQ(ierr);
75060fdc7489SMatthew Knepley     if (match) break;
75070fdc7489SMatthew Knepley   }
75080fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
75090fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
75100fdc7489SMatthew Knepley   else    prev->next = orig->next;
75110fdc7489SMatthew Knepley   if (!l) {
75120fdc7489SMatthew Knepley     orig->next = dm->labels;
75130fdc7489SMatthew Knepley     dm->labels = orig;
75140fdc7489SMatthew Knepley   } else {
75150fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
75160fdc7489SMatthew Knepley     orig->next = prev->next;
75170fdc7489SMatthew Knepley     prev->next = orig;
75180fdc7489SMatthew Knepley   }
75190fdc7489SMatthew Knepley   PetscFunctionReturn(0);
75200fdc7489SMatthew Knepley }
75210fdc7489SMatthew Knepley 
75220fdc7489SMatthew Knepley /*@C
7523c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7524c58f1c22SToby Isaac 
7525c58f1c22SToby Isaac   Not Collective
7526c58f1c22SToby Isaac 
7527c58f1c22SToby Isaac   Input Parameters:
7528c58f1c22SToby Isaac + dm   - The DM object
7529c58f1c22SToby Isaac . name - The label name
7530c58f1c22SToby Isaac - point - The mesh point
7531c58f1c22SToby Isaac 
7532c58f1c22SToby Isaac   Output Parameter:
7533c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7534c58f1c22SToby Isaac 
7535c58f1c22SToby Isaac   Level: beginner
7536c58f1c22SToby Isaac 
7537c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7538c58f1c22SToby Isaac @*/
7539c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7540c58f1c22SToby Isaac {
7541c58f1c22SToby Isaac   DMLabel        label;
7542c58f1c22SToby Isaac   PetscErrorCode ierr;
7543c58f1c22SToby Isaac 
7544c58f1c22SToby Isaac   PetscFunctionBegin;
7545c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7546c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7547c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
754813903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7549c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7550c58f1c22SToby Isaac   PetscFunctionReturn(0);
7551c58f1c22SToby Isaac }
7552c58f1c22SToby Isaac 
7553c58f1c22SToby Isaac /*@C
7554c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7555c58f1c22SToby Isaac 
7556c58f1c22SToby Isaac   Not Collective
7557c58f1c22SToby Isaac 
7558c58f1c22SToby Isaac   Input Parameters:
7559c58f1c22SToby Isaac + dm   - The DM object
7560c58f1c22SToby Isaac . name - The label name
7561c58f1c22SToby Isaac . point - The mesh point
7562c58f1c22SToby Isaac - value - The label value for this point
7563c58f1c22SToby Isaac 
7564c58f1c22SToby Isaac   Output Parameter:
7565c58f1c22SToby Isaac 
7566c58f1c22SToby Isaac   Level: beginner
7567c58f1c22SToby Isaac 
7568c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7569c58f1c22SToby Isaac @*/
7570c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7571c58f1c22SToby Isaac {
7572c58f1c22SToby Isaac   DMLabel        label;
7573c58f1c22SToby Isaac   PetscErrorCode ierr;
7574c58f1c22SToby Isaac 
7575c58f1c22SToby Isaac   PetscFunctionBegin;
7576c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7577c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7578c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7579c58f1c22SToby Isaac   if (!label) {
7580c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7581c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7582c58f1c22SToby Isaac   }
7583c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7584c58f1c22SToby Isaac   PetscFunctionReturn(0);
7585c58f1c22SToby Isaac }
7586c58f1c22SToby Isaac 
7587c58f1c22SToby Isaac /*@C
7588c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7589c58f1c22SToby Isaac 
7590c58f1c22SToby Isaac   Not Collective
7591c58f1c22SToby Isaac 
7592c58f1c22SToby Isaac   Input Parameters:
7593c58f1c22SToby Isaac + dm   - The DM object
7594c58f1c22SToby Isaac . name - The label name
7595c58f1c22SToby Isaac . point - The mesh point
7596c58f1c22SToby Isaac - value - The label value for this point
7597c58f1c22SToby Isaac 
7598c58f1c22SToby Isaac   Output Parameter:
7599c58f1c22SToby Isaac 
7600c58f1c22SToby Isaac   Level: beginner
7601c58f1c22SToby Isaac 
7602c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7603c58f1c22SToby Isaac @*/
7604c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7605c58f1c22SToby Isaac {
7606c58f1c22SToby Isaac   DMLabel        label;
7607c58f1c22SToby Isaac   PetscErrorCode ierr;
7608c58f1c22SToby Isaac 
7609c58f1c22SToby Isaac   PetscFunctionBegin;
7610c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7611c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7612c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7613c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7614c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7615c58f1c22SToby Isaac   PetscFunctionReturn(0);
7616c58f1c22SToby Isaac }
7617c58f1c22SToby Isaac 
7618c58f1c22SToby Isaac /*@C
7619c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7620c58f1c22SToby Isaac 
7621c58f1c22SToby Isaac   Not Collective
7622c58f1c22SToby Isaac 
7623c58f1c22SToby Isaac   Input Parameters:
7624c58f1c22SToby Isaac + dm   - The DM object
7625c58f1c22SToby Isaac - name - The label name
7626c58f1c22SToby Isaac 
7627c58f1c22SToby Isaac   Output Parameter:
7628c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7629c58f1c22SToby Isaac 
7630c58f1c22SToby Isaac   Level: beginner
7631c58f1c22SToby Isaac 
7632df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7633c58f1c22SToby Isaac @*/
7634c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7635c58f1c22SToby Isaac {
7636c58f1c22SToby Isaac   DMLabel        label;
7637c58f1c22SToby Isaac   PetscErrorCode ierr;
7638c58f1c22SToby Isaac 
7639c58f1c22SToby Isaac   PetscFunctionBegin;
7640c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7641c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7642534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7643c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7644c58f1c22SToby Isaac   *size = 0;
7645c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7646c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7647c58f1c22SToby Isaac   PetscFunctionReturn(0);
7648c58f1c22SToby Isaac }
7649c58f1c22SToby Isaac 
7650c58f1c22SToby Isaac /*@C
7651c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7652c58f1c22SToby Isaac 
7653c58f1c22SToby Isaac   Not Collective
7654c58f1c22SToby Isaac 
7655c58f1c22SToby Isaac   Input Parameters:
7656c58f1c22SToby Isaac + mesh - The DM object
7657c58f1c22SToby Isaac - name - The label name
7658c58f1c22SToby Isaac 
7659c58f1c22SToby Isaac   Output Parameter:
7660c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7661c58f1c22SToby Isaac 
7662c58f1c22SToby Isaac   Level: beginner
7663c58f1c22SToby Isaac 
7664c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7665c58f1c22SToby Isaac @*/
7666c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7667c58f1c22SToby Isaac {
7668c58f1c22SToby Isaac   DMLabel        label;
7669c58f1c22SToby Isaac   PetscErrorCode ierr;
7670c58f1c22SToby Isaac 
7671c58f1c22SToby Isaac   PetscFunctionBegin;
7672c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7673c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7674c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7675c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7676c58f1c22SToby Isaac   *ids = NULL;
7677dab2e251SBlaise Bourdin  if (label) {
7678c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7679dab2e251SBlaise Bourdin   } else {
7680dab2e251SBlaise Bourdin     /* returning an empty IS */
7681dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7682dab2e251SBlaise Bourdin   }
7683c58f1c22SToby Isaac   PetscFunctionReturn(0);
7684c58f1c22SToby Isaac }
7685c58f1c22SToby Isaac 
7686c58f1c22SToby Isaac /*@C
7687c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7688c58f1c22SToby Isaac 
7689c58f1c22SToby Isaac   Not Collective
7690c58f1c22SToby Isaac 
7691c58f1c22SToby Isaac   Input Parameters:
7692c58f1c22SToby Isaac + dm - The DM object
7693c58f1c22SToby Isaac . name - The label name
7694c58f1c22SToby Isaac - value - The stratum value
7695c58f1c22SToby Isaac 
7696c58f1c22SToby Isaac   Output Parameter:
7697c58f1c22SToby Isaac . size - The stratum size
7698c58f1c22SToby Isaac 
7699c58f1c22SToby Isaac   Level: beginner
7700c58f1c22SToby Isaac 
7701c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7702c58f1c22SToby Isaac @*/
7703c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7704c58f1c22SToby Isaac {
7705c58f1c22SToby Isaac   DMLabel        label;
7706c58f1c22SToby Isaac   PetscErrorCode ierr;
7707c58f1c22SToby Isaac 
7708c58f1c22SToby Isaac   PetscFunctionBegin;
7709c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7710c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7711534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7712c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7713c58f1c22SToby Isaac   *size = 0;
7714c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7715c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7716c58f1c22SToby Isaac   PetscFunctionReturn(0);
7717c58f1c22SToby Isaac }
7718c58f1c22SToby Isaac 
7719c58f1c22SToby Isaac /*@C
7720c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7721c58f1c22SToby Isaac 
7722c58f1c22SToby Isaac   Not Collective
7723c58f1c22SToby Isaac 
7724c58f1c22SToby Isaac   Input Parameters:
7725c58f1c22SToby Isaac + dm - The DM object
7726c58f1c22SToby Isaac . name - The label name
7727c58f1c22SToby Isaac - value - The stratum value
7728c58f1c22SToby Isaac 
7729c58f1c22SToby Isaac   Output Parameter:
7730c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7731c58f1c22SToby Isaac 
7732c58f1c22SToby Isaac   Level: beginner
7733c58f1c22SToby Isaac 
7734c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7735c58f1c22SToby Isaac @*/
7736c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7737c58f1c22SToby Isaac {
7738c58f1c22SToby Isaac   DMLabel        label;
7739c58f1c22SToby Isaac   PetscErrorCode ierr;
7740c58f1c22SToby Isaac 
7741c58f1c22SToby Isaac   PetscFunctionBegin;
7742c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7743c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7744c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7745c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7746c58f1c22SToby Isaac   *points = NULL;
7747c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7748c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7749c58f1c22SToby Isaac   PetscFunctionReturn(0);
7750c58f1c22SToby Isaac }
7751c58f1c22SToby Isaac 
77524de306b1SToby Isaac /*@C
77539044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
77544de306b1SToby Isaac 
77554de306b1SToby Isaac   Not Collective
77564de306b1SToby Isaac 
77574de306b1SToby Isaac   Input Parameters:
77584de306b1SToby Isaac + dm - The DM object
77594de306b1SToby Isaac . name - The label name
77604de306b1SToby Isaac . value - The stratum value
77614de306b1SToby Isaac - points - The stratum points
77624de306b1SToby Isaac 
77634de306b1SToby Isaac   Level: beginner
77644de306b1SToby Isaac 
77654de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
77664de306b1SToby Isaac @*/
77674de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
77684de306b1SToby Isaac {
77694de306b1SToby Isaac   DMLabel        label;
77704de306b1SToby Isaac   PetscErrorCode ierr;
77714de306b1SToby Isaac 
77724de306b1SToby Isaac   PetscFunctionBegin;
77734de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77744de306b1SToby Isaac   PetscValidCharPointer(name, 2);
77754de306b1SToby Isaac   PetscValidPointer(points, 4);
77764de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
77774de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
77784de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
77794de306b1SToby Isaac   PetscFunctionReturn(0);
77804de306b1SToby Isaac }
77814de306b1SToby Isaac 
7782c58f1c22SToby Isaac /*@C
7783c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7784c58f1c22SToby Isaac 
7785c58f1c22SToby Isaac   Not Collective
7786c58f1c22SToby Isaac 
7787c58f1c22SToby Isaac   Input Parameters:
7788c58f1c22SToby Isaac + dm   - The DM object
7789c58f1c22SToby Isaac . name - The label name
7790c58f1c22SToby Isaac - value - The label value for this point
7791c58f1c22SToby Isaac 
7792c58f1c22SToby Isaac   Output Parameter:
7793c58f1c22SToby Isaac 
7794c58f1c22SToby Isaac   Level: beginner
7795c58f1c22SToby Isaac 
7796c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7797c58f1c22SToby Isaac @*/
7798c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7799c58f1c22SToby Isaac {
7800c58f1c22SToby Isaac   DMLabel        label;
7801c58f1c22SToby Isaac   PetscErrorCode ierr;
7802c58f1c22SToby Isaac 
7803c58f1c22SToby Isaac   PetscFunctionBegin;
7804c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7805c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7806c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7807c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7808c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7809c58f1c22SToby Isaac   PetscFunctionReturn(0);
7810c58f1c22SToby Isaac }
7811c58f1c22SToby Isaac 
7812c58f1c22SToby Isaac /*@
7813c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7814c58f1c22SToby Isaac 
7815c58f1c22SToby Isaac   Not Collective
7816c58f1c22SToby Isaac 
7817c58f1c22SToby Isaac   Input Parameter:
7818c58f1c22SToby Isaac . dm   - The DM object
7819c58f1c22SToby Isaac 
7820c58f1c22SToby Isaac   Output Parameter:
7821c58f1c22SToby Isaac . numLabels - the number of Labels
7822c58f1c22SToby Isaac 
7823c58f1c22SToby Isaac   Level: intermediate
7824c58f1c22SToby Isaac 
7825c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7826c58f1c22SToby Isaac @*/
7827c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7828c58f1c22SToby Isaac {
78295d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7830c58f1c22SToby Isaac   PetscInt  n    = 0;
7831c58f1c22SToby Isaac 
7832c58f1c22SToby Isaac   PetscFunctionBegin;
7833c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7834534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7835c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7836c58f1c22SToby Isaac   *numLabels = n;
7837c58f1c22SToby Isaac   PetscFunctionReturn(0);
7838c58f1c22SToby Isaac }
7839c58f1c22SToby Isaac 
7840c58f1c22SToby Isaac /*@C
7841c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7842c58f1c22SToby Isaac 
7843c58f1c22SToby Isaac   Not Collective
7844c58f1c22SToby Isaac 
7845c58f1c22SToby Isaac   Input Parameters:
7846c58f1c22SToby Isaac + dm - The DM object
7847c58f1c22SToby Isaac - n  - the label number
7848c58f1c22SToby Isaac 
7849c58f1c22SToby Isaac   Output Parameter:
7850c58f1c22SToby Isaac . name - the label name
7851c58f1c22SToby Isaac 
7852c58f1c22SToby Isaac   Level: intermediate
7853c58f1c22SToby Isaac 
7854c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7855c58f1c22SToby Isaac @*/
7856c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7857c58f1c22SToby Isaac {
78585d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7859c58f1c22SToby Isaac   PetscInt       l    = 0;
7860d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7861c58f1c22SToby Isaac 
7862c58f1c22SToby Isaac   PetscFunctionBegin;
7863c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7864c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7865c58f1c22SToby Isaac   while (next) {
7866c58f1c22SToby Isaac     if (l == n) {
7867d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7868c58f1c22SToby Isaac       PetscFunctionReturn(0);
7869c58f1c22SToby Isaac     }
7870c58f1c22SToby Isaac     ++l;
7871c58f1c22SToby Isaac     next = next->next;
7872c58f1c22SToby Isaac   }
7873c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7874c58f1c22SToby Isaac }
7875c58f1c22SToby Isaac 
7876c58f1c22SToby Isaac /*@C
7877c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7878c58f1c22SToby Isaac 
7879c58f1c22SToby Isaac   Not Collective
7880c58f1c22SToby Isaac 
7881c58f1c22SToby Isaac   Input Parameters:
7882c58f1c22SToby Isaac + dm   - The DM object
7883c58f1c22SToby Isaac - name - The label name
7884c58f1c22SToby Isaac 
7885c58f1c22SToby Isaac   Output Parameter:
7886c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7887c58f1c22SToby Isaac 
7888c58f1c22SToby Isaac   Level: intermediate
7889c58f1c22SToby Isaac 
7890c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7891c58f1c22SToby Isaac @*/
7892c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7893c58f1c22SToby Isaac {
78945d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7895d67d17b1SMatthew G. Knepley   const char    *lname;
7896c58f1c22SToby Isaac   PetscErrorCode ierr;
7897c58f1c22SToby Isaac 
7898c58f1c22SToby Isaac   PetscFunctionBegin;
7899c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7900c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7901534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7902c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7903c58f1c22SToby Isaac   while (next) {
7904d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7905d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7906c58f1c22SToby Isaac     if (*hasLabel) break;
7907c58f1c22SToby Isaac     next = next->next;
7908c58f1c22SToby Isaac   }
7909c58f1c22SToby Isaac   PetscFunctionReturn(0);
7910c58f1c22SToby Isaac }
7911c58f1c22SToby Isaac 
7912c58f1c22SToby Isaac /*@C
7913c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7914c58f1c22SToby Isaac 
7915c58f1c22SToby Isaac   Not Collective
7916c58f1c22SToby Isaac 
7917c58f1c22SToby Isaac   Input Parameters:
7918c58f1c22SToby Isaac + dm   - The DM object
7919c58f1c22SToby Isaac - name - The label name
7920c58f1c22SToby Isaac 
7921c58f1c22SToby Isaac   Output Parameter:
7922c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7923c58f1c22SToby Isaac 
79246d7c9049SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
79256d7c9049SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
79266d7c9049SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
79276d7c9049SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
79286d7c9049SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
79296d7c9049SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
79306d7c9049SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
79316d7c9049SMatthew G. Knepley 
7932c58f1c22SToby Isaac   Level: intermediate
7933c58f1c22SToby Isaac 
79346d7c9049SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
7935c58f1c22SToby Isaac @*/
7936c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7937c58f1c22SToby Isaac {
79385d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7939c58f1c22SToby Isaac   PetscBool      hasLabel;
7940d67d17b1SMatthew G. Knepley   const char    *lname;
7941c58f1c22SToby Isaac   PetscErrorCode ierr;
7942c58f1c22SToby Isaac 
7943c58f1c22SToby Isaac   PetscFunctionBegin;
7944c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7945c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7946c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7947c58f1c22SToby Isaac   *label = NULL;
7948c58f1c22SToby Isaac   while (next) {
7949d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7950d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7951c58f1c22SToby Isaac     if (hasLabel) {
7952c58f1c22SToby Isaac       *label = next->label;
7953c58f1c22SToby Isaac       break;
7954c58f1c22SToby Isaac     }
7955c58f1c22SToby Isaac     next = next->next;
7956c58f1c22SToby Isaac   }
7957c58f1c22SToby Isaac   PetscFunctionReturn(0);
7958c58f1c22SToby Isaac }
7959c58f1c22SToby Isaac 
7960c58f1c22SToby Isaac /*@C
7961c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7962c58f1c22SToby Isaac 
7963c58f1c22SToby Isaac   Not Collective
7964c58f1c22SToby Isaac 
7965c58f1c22SToby Isaac   Input Parameters:
7966c58f1c22SToby Isaac + dm - The DM object
7967c58f1c22SToby Isaac - n  - the label number
7968c58f1c22SToby Isaac 
7969c58f1c22SToby Isaac   Output Parameter:
7970c58f1c22SToby Isaac . label - the label
7971c58f1c22SToby Isaac 
7972c58f1c22SToby Isaac   Level: intermediate
7973c58f1c22SToby Isaac 
7974c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7975c58f1c22SToby Isaac @*/
7976c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7977c58f1c22SToby Isaac {
79785d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7979c58f1c22SToby Isaac   PetscInt    l    = 0;
7980c58f1c22SToby Isaac 
7981c58f1c22SToby Isaac   PetscFunctionBegin;
7982c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7983c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7984c58f1c22SToby Isaac   while (next) {
7985c58f1c22SToby Isaac     if (l == n) {
7986c58f1c22SToby Isaac       *label = next->label;
7987c58f1c22SToby Isaac       PetscFunctionReturn(0);
7988c58f1c22SToby Isaac     }
7989c58f1c22SToby Isaac     ++l;
7990c58f1c22SToby Isaac     next = next->next;
7991c58f1c22SToby Isaac   }
7992c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7993c58f1c22SToby Isaac }
7994c58f1c22SToby Isaac 
7995c58f1c22SToby Isaac /*@C
7996c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7997c58f1c22SToby Isaac 
7998c58f1c22SToby Isaac   Not Collective
7999c58f1c22SToby Isaac 
8000c58f1c22SToby Isaac   Input Parameters:
8001c58f1c22SToby Isaac + dm   - The DM object
8002c58f1c22SToby Isaac - label - The DMLabel
8003c58f1c22SToby Isaac 
8004c58f1c22SToby Isaac   Level: developer
8005c58f1c22SToby Isaac 
8006c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8007c58f1c22SToby Isaac @*/
8008c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
8009c58f1c22SToby Isaac {
80105d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
8011c58f1c22SToby Isaac   PetscBool      hasLabel;
8012d67d17b1SMatthew G. Knepley   const char    *lname;
80135d80c0bfSVaclav Hapla   PetscBool      flg;
8014c58f1c22SToby Isaac   PetscErrorCode ierr;
8015c58f1c22SToby Isaac 
8016c58f1c22SToby Isaac   PetscFunctionBegin;
8017c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8018d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
8019d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
8020d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
8021c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
8022c58f1c22SToby Isaac   tmpLabel->label  = label;
8023c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
80245d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
80255d80c0bfSVaclav Hapla   *p = tmpLabel;
802608f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
80275d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
80285d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
8029ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
8030ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
8031c58f1c22SToby Isaac   PetscFunctionReturn(0);
8032c58f1c22SToby Isaac }
8033c58f1c22SToby Isaac 
8034c58f1c22SToby Isaac /*@C
80354a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
80364a7ee7d0SMatthew G. Knepley 
80374a7ee7d0SMatthew G. Knepley   Not Collective
80384a7ee7d0SMatthew G. Knepley 
80394a7ee7d0SMatthew G. Knepley   Input Parameters:
80404a7ee7d0SMatthew G. Knepley + dm    - The DM object
80414a7ee7d0SMatthew G. Knepley - label - The DMLabel, having the same name, to substitute
80424a7ee7d0SMatthew G. Knepley 
80434a7ee7d0SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
80444a7ee7d0SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
80454a7ee7d0SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
80464a7ee7d0SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
80474a7ee7d0SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
80484a7ee7d0SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
80494a7ee7d0SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
80504a7ee7d0SMatthew G. Knepley 
80514a7ee7d0SMatthew G. Knepley   Level: intermediate
80524a7ee7d0SMatthew G. Knepley 
80534a7ee7d0SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
80544a7ee7d0SMatthew G. Knepley @*/
80554a7ee7d0SMatthew G. Knepley PetscErrorCode DMSetLabel(DM dm, DMLabel label)
80564a7ee7d0SMatthew G. Knepley {
80574a7ee7d0SMatthew G. Knepley   DMLabelLink    next = dm->labels;
80584a7ee7d0SMatthew G. Knepley   PetscBool      hasLabel, flg;
80594a7ee7d0SMatthew G. Knepley   const char    *name, *lname;
80604a7ee7d0SMatthew G. Knepley   PetscErrorCode ierr;
80614a7ee7d0SMatthew G. Knepley 
80624a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
80634a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80644a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
80654a7ee7d0SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &name);CHKERRQ(ierr);
80664a7ee7d0SMatthew G. Knepley   while (next) {
80674a7ee7d0SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
80684a7ee7d0SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
80694a7ee7d0SMatthew G. Knepley     if (hasLabel) {
80704a7ee7d0SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
80714a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
80724a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
80734a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
80744a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
80754a7ee7d0SMatthew G. Knepley       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
80764a7ee7d0SMatthew G. Knepley       next->label = label;
80774a7ee7d0SMatthew G. Knepley       break;
80784a7ee7d0SMatthew G. Knepley     }
80794a7ee7d0SMatthew G. Knepley     next = next->next;
80804a7ee7d0SMatthew G. Knepley   }
80814a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
80824a7ee7d0SMatthew G. Knepley }
80834a7ee7d0SMatthew G. Knepley 
80844a7ee7d0SMatthew G. Knepley /*@C
8085e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
8086c58f1c22SToby Isaac 
8087c58f1c22SToby Isaac   Not Collective
8088c58f1c22SToby Isaac 
8089c58f1c22SToby Isaac   Input Parameters:
8090c58f1c22SToby Isaac + dm   - The DM object
8091c58f1c22SToby Isaac - name - The label name
8092c58f1c22SToby Isaac 
8093c58f1c22SToby Isaac   Output Parameter:
8094c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
8095c58f1c22SToby Isaac 
8096c58f1c22SToby Isaac   Level: developer
8097c58f1c22SToby Isaac 
8098e5472504SVaclav Hapla   Notes:
8099e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
8100e5472504SVaclav Hapla   DMLabelDestroy() on the label.
8101e5472504SVaclav Hapla 
8102e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
8103e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
8104e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
8105e5472504SVaclav Hapla 
8106e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
8107c58f1c22SToby Isaac @*/
8108c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
8109c58f1c22SToby Isaac {
811095d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
8111c58f1c22SToby Isaac   PetscBool      hasLabel;
8112d67d17b1SMatthew G. Knepley   const char    *lname;
8113c58f1c22SToby Isaac   PetscErrorCode ierr;
8114c58f1c22SToby Isaac 
8115c58f1c22SToby Isaac   PetscFunctionBegin;
8116c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8117e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
8118e5472504SVaclav Hapla   if (label) {
8119e5472504SVaclav Hapla     PetscValidPointer(label, 3);
8120c58f1c22SToby Isaac     *label = NULL;
8121e5472504SVaclav Hapla   }
81225d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
812395d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
8124d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
8125c58f1c22SToby Isaac     if (hasLabel) {
812695d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
8127c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
812895d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
8129ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
8130ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
813195d578d6SVaclav Hapla       if (label) *label = link->label;
813295d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
813395d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8134c58f1c22SToby Isaac       break;
8135c58f1c22SToby Isaac     }
8136c58f1c22SToby Isaac   }
8137c58f1c22SToby Isaac   PetscFunctionReturn(0);
8138c58f1c22SToby Isaac }
8139c58f1c22SToby Isaac 
8140306894acSVaclav Hapla /*@
8141306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
8142306894acSVaclav Hapla 
8143306894acSVaclav Hapla   Not Collective
8144306894acSVaclav Hapla 
8145306894acSVaclav Hapla   Input Parameters:
8146306894acSVaclav Hapla + dm   - The DM object
8147876aa926SVaclav Hapla . label - The DMLabel to be removed from the DM
8148306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
8149306894acSVaclav Hapla 
8150306894acSVaclav Hapla   Level: developer
8151306894acSVaclav Hapla 
8152306894acSVaclav Hapla   Notes:
8153306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
8154306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
8155306894acSVaclav Hapla   *label nullified.
8156306894acSVaclav Hapla 
8157306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
8158306894acSVaclav Hapla @*/
8159306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
8160306894acSVaclav Hapla {
816143e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
8162306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
8163306894acSVaclav Hapla   PetscErrorCode ierr;
8164306894acSVaclav Hapla 
8165306894acSVaclav Hapla   PetscFunctionBegin;
8166306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8167306894acSVaclav Hapla   PetscValidPointer(label, 2);
8168f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
8169306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
8170306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
81715d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
817243e45a93SVaclav Hapla     if (*label == link->label) {
8173306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
817443e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
8175306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
8176ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
817743e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
817843e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
817943e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8180306894acSVaclav Hapla       break;
8181306894acSVaclav Hapla     }
8182306894acSVaclav Hapla   }
8183306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
8184306894acSVaclav Hapla   PetscFunctionReturn(0);
8185306894acSVaclav Hapla }
8186306894acSVaclav Hapla 
8187c58f1c22SToby Isaac /*@C
8188c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
8189c58f1c22SToby Isaac 
8190c58f1c22SToby Isaac   Not Collective
8191c58f1c22SToby Isaac 
8192c58f1c22SToby Isaac   Input Parameters:
8193c58f1c22SToby Isaac + dm   - The DM object
8194c58f1c22SToby Isaac - name - The label name
8195c58f1c22SToby Isaac 
8196c58f1c22SToby Isaac   Output Parameter:
8197c58f1c22SToby Isaac . output - The flag for output
8198c58f1c22SToby Isaac 
8199c58f1c22SToby Isaac   Level: developer
8200c58f1c22SToby Isaac 
8201c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8202c58f1c22SToby Isaac @*/
8203c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
8204c58f1c22SToby Isaac {
82055d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8206d67d17b1SMatthew G. Knepley   const char    *lname;
8207c58f1c22SToby Isaac   PetscErrorCode ierr;
8208c58f1c22SToby Isaac 
8209c58f1c22SToby Isaac   PetscFunctionBegin;
8210c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8211c58f1c22SToby Isaac   PetscValidPointer(name, 2);
8212c58f1c22SToby Isaac   PetscValidPointer(output, 3);
8213c58f1c22SToby Isaac   while (next) {
8214c58f1c22SToby Isaac     PetscBool flg;
8215c58f1c22SToby Isaac 
8216d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8217d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8218c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
8219c58f1c22SToby Isaac     next = next->next;
8220c58f1c22SToby Isaac   }
8221c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8222c58f1c22SToby Isaac }
8223c58f1c22SToby Isaac 
8224c58f1c22SToby Isaac /*@C
8225c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
8226c58f1c22SToby Isaac 
8227c58f1c22SToby Isaac   Not Collective
8228c58f1c22SToby Isaac 
8229c58f1c22SToby Isaac   Input Parameters:
8230c58f1c22SToby Isaac + dm     - The DM object
8231c58f1c22SToby Isaac . name   - The label name
8232c58f1c22SToby Isaac - output - The flag for output
8233c58f1c22SToby Isaac 
8234c58f1c22SToby Isaac   Level: developer
8235c58f1c22SToby Isaac 
8236c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8237c58f1c22SToby Isaac @*/
8238c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
8239c58f1c22SToby Isaac {
82405d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8241d67d17b1SMatthew G. Knepley   const char    *lname;
8242c58f1c22SToby Isaac   PetscErrorCode ierr;
8243c58f1c22SToby Isaac 
8244c58f1c22SToby Isaac   PetscFunctionBegin;
8245c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8246534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
8247c58f1c22SToby Isaac   while (next) {
8248c58f1c22SToby Isaac     PetscBool flg;
8249c58f1c22SToby Isaac 
8250d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8251d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8252c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
8253c58f1c22SToby Isaac     next = next->next;
8254c58f1c22SToby Isaac   }
8255c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8256c58f1c22SToby Isaac }
8257c58f1c22SToby Isaac 
8258c58f1c22SToby Isaac /*@
8259c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
8260c58f1c22SToby Isaac 
8261d083f849SBarry Smith   Collective on dmA
8262c58f1c22SToby Isaac 
8263d8d19677SJose E. Roman   Input Parameters:
82645d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
82652cbb9b06SVaclav Hapla . dmB - The DM object to which labels are copied
82665d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
82672cbb9b06SVaclav Hapla . all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
82682cbb9b06SVaclav Hapla - emode - How to behave when a DMLabel in the source and destination DMs with the same name is encountered (see DMCopyLabelsMode)
8269c58f1c22SToby Isaac 
8270c58f1c22SToby Isaac   Level: intermediate
8271c58f1c22SToby Isaac 
82722cbb9b06SVaclav Hapla   Notes:
82732cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
8274c58f1c22SToby Isaac 
82752cbb9b06SVaclav Hapla .seealso: DMAddLabel(), DMCopyLabelsMode
8276c58f1c22SToby Isaac @*/
82772cbb9b06SVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
8278c58f1c22SToby Isaac {
82792cbb9b06SVaclav Hapla   DMLabel        label, labelNew, labelOld;
8280c58f1c22SToby Isaac   const char    *name;
8281c58f1c22SToby Isaac   PetscBool      flg;
82825d80c0bfSVaclav Hapla   DMLabelLink    link;
82835d80c0bfSVaclav Hapla   PetscErrorCode ierr;
8284c58f1c22SToby Isaac 
82855d80c0bfSVaclav Hapla   PetscFunctionBegin;
82865d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
82875d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
82885d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
82895d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
82905d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
82915d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
82925d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
82935d80c0bfSVaclav Hapla     label=link->label;
82945d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
82955d80c0bfSVaclav Hapla     if (!all) {
8296c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
8297c58f1c22SToby Isaac       if (flg) continue;
82987d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
82997d5acc75SStefano Zampini       if (flg) continue;
8300ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
8301ba2698f1SMatthew G. Knepley       if (flg) continue;
83025d80c0bfSVaclav Hapla     }
83032cbb9b06SVaclav Hapla     ierr = DMGetLabel(dmB, name, &labelOld);CHKERRQ(ierr);
83042cbb9b06SVaclav Hapla     if (labelOld) {
83052cbb9b06SVaclav Hapla       switch (emode) {
83062cbb9b06SVaclav Hapla         case DM_COPY_LABELS_KEEP:
83072cbb9b06SVaclav Hapla           continue;
83082cbb9b06SVaclav Hapla         case DM_COPY_LABELS_REPLACE:
83092cbb9b06SVaclav Hapla           ierr = DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE);CHKERRQ(ierr);
83102cbb9b06SVaclav Hapla           break;
83112cbb9b06SVaclav Hapla         case DM_COPY_LABELS_FAIL:
83122cbb9b06SVaclav Hapla           SETERRQ1(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
83132cbb9b06SVaclav Hapla         default:
83142cbb9b06SVaclav Hapla           SETERRQ1(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", emode);
83152cbb9b06SVaclav Hapla       }
83162cbb9b06SVaclav Hapla     }
83175d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
8318c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
83195d80c0bfSVaclav Hapla     } else {
83205d80c0bfSVaclav Hapla       labelNew = label;
83215d80c0bfSVaclav Hapla     }
8322c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
83235d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
8324c58f1c22SToby Isaac   }
8325c58f1c22SToby Isaac   PetscFunctionReturn(0);
8326c58f1c22SToby Isaac }
8327461a15a0SLisandro Dalcin 
8328609dae6eSVaclav Hapla /*@C
8329609dae6eSVaclav Hapla   DMCompareLabels - Compare labels of two DMPlex meshes
8330609dae6eSVaclav Hapla 
8331609dae6eSVaclav Hapla   Collective on dmA
8332609dae6eSVaclav Hapla 
8333609dae6eSVaclav Hapla   Input Parameters:
8334609dae6eSVaclav Hapla + dm0 - First DM object
8335609dae6eSVaclav Hapla - dm1 - Second DM object
8336609dae6eSVaclav Hapla 
8337609dae6eSVaclav Hapla   Output Parameters
8338609dae6eSVaclav Hapla + same    - (Optional) Flag whether labels of dm0 and dm1 are the same
8339609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
8340609dae6eSVaclav Hapla 
8341609dae6eSVaclav Hapla   Level: intermediate
8342609dae6eSVaclav Hapla 
8343609dae6eSVaclav Hapla   Notes:
8344609dae6eSVaclav Hapla   If both same and message is passed as NULL, and difference is found, an error is thrown with a message describing the difference.
8345609dae6eSVaclav Hapla 
8346609dae6eSVaclav Hapla   Message must be freed by user.
8347609dae6eSVaclav Hapla 
8348609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
8349609dae6eSVaclav Hapla   DMLabelCompare() is used to compare each pair of labels with the same name.
8350609dae6eSVaclav Hapla 
8351609dae6eSVaclav Hapla   Fortran Notes:
8352609dae6eSVaclav Hapla   This function is currently not available from Fortran.
8353609dae6eSVaclav Hapla 
8354609dae6eSVaclav Hapla .seealso: DMAddLabel(), DMCopyLabelsMode, DMLabelCompare()
8355609dae6eSVaclav Hapla @*/
8356609dae6eSVaclav Hapla PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *same, char **message)
8357609dae6eSVaclav Hapla {
8358609dae6eSVaclav Hapla   PetscInt        n0, n1, i;
8359609dae6eSVaclav Hapla   char            msg[PETSC_MAX_PATH_LEN] = "";
8360609dae6eSVaclav Hapla   MPI_Comm        comm;
8361609dae6eSVaclav Hapla   PetscErrorCode  ierr;
8362609dae6eSVaclav Hapla 
8363609dae6eSVaclav Hapla   PetscFunctionBegin;
8364609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0,DM_CLASSID,1);
8365609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1,DM_CLASSID,2);
8366609dae6eSVaclav Hapla   PetscCheckSameComm(dm0,1,dm1,2);
8367609dae6eSVaclav Hapla   if (same) PetscValidBoolPointer(same,3);
8368609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
8369609dae6eSVaclav Hapla   ierr = PetscObjectGetComm((PetscObject)dm0, &comm);CHKERRQ(ierr);
8370609dae6eSVaclav Hapla   ierr = DMGetNumLabels(dm0, &n0);CHKERRQ(ierr);
8371609dae6eSVaclav Hapla   ierr = DMGetNumLabels(dm1, &n1);CHKERRQ(ierr);
8372609dae6eSVaclav Hapla   if (n0 != n1) {
8373609dae6eSVaclav Hapla     ierr = PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %D != %D = Number of labels in dm1", n0, n1);CHKERRQ(ierr);
8374609dae6eSVaclav Hapla     goto finish;
8375609dae6eSVaclav Hapla   }
8376609dae6eSVaclav Hapla   for (i=0; i<n0; i++) {
8377609dae6eSVaclav Hapla     DMLabel     l0, l1;
8378609dae6eSVaclav Hapla     const char *name;
8379609dae6eSVaclav Hapla     char       *msgInner;
8380609dae6eSVaclav Hapla 
8381609dae6eSVaclav Hapla     /* Ignore label order */
8382609dae6eSVaclav Hapla     ierr = DMGetLabelByNum(dm0, i, &l0);CHKERRQ(ierr);
8383609dae6eSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)l0, &name);CHKERRQ(ierr);
8384609dae6eSVaclav Hapla     ierr = DMGetLabel(dm1, name, &l1);CHKERRQ(ierr);
8385609dae6eSVaclav Hapla     if (!l1) {
8386609dae6eSVaclav Hapla       ierr = PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%D in dm0) not found in dm1", name, i);CHKERRQ(ierr);
8387609dae6eSVaclav Hapla       goto finish;
8388609dae6eSVaclav Hapla     }
8389609dae6eSVaclav Hapla     ierr = DMLabelCompare(l0, l1, NULL, &msgInner);CHKERRQ(ierr);
8390609dae6eSVaclav Hapla     ierr = PetscStrncpy(msg, msgInner, sizeof(msg));CHKERRQ(ierr);
8391609dae6eSVaclav Hapla     ierr = PetscFree(msgInner);CHKERRQ(ierr);
8392609dae6eSVaclav Hapla     if (msg[0]) goto finish;
8393609dae6eSVaclav Hapla   }
8394609dae6eSVaclav Hapla finish:
8395609dae6eSVaclav Hapla   if (msg[0] && !same && !message) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, msg);
8396609dae6eSVaclav Hapla   if (same) *same = (PetscBool) !msg[0];
8397609dae6eSVaclav Hapla   if (message) {
8398609dae6eSVaclav Hapla     *message = NULL;
8399609dae6eSVaclav Hapla     if (msg[0]) {
8400609dae6eSVaclav Hapla       ierr = PetscStrallocpy(msg, message);CHKERRQ(ierr);
8401609dae6eSVaclav Hapla     }
8402609dae6eSVaclav Hapla   }
8403609dae6eSVaclav Hapla   PetscFunctionReturn(0);
8404609dae6eSVaclav Hapla }
8405609dae6eSVaclav Hapla 
8406461a15a0SLisandro Dalcin PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
8407461a15a0SLisandro Dalcin {
8408461a15a0SLisandro Dalcin   PetscErrorCode ierr;
8409609dae6eSVaclav Hapla 
8410461a15a0SLisandro Dalcin   PetscFunctionBegin;
8411461a15a0SLisandro Dalcin   PetscValidPointer(label,2);
8412461a15a0SLisandro Dalcin   if (!*label) {
8413461a15a0SLisandro Dalcin     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
8414461a15a0SLisandro Dalcin     ierr = DMGetLabel(dm, name, label);CHKERRQ(ierr);
8415461a15a0SLisandro Dalcin   }
8416461a15a0SLisandro Dalcin   ierr = DMLabelSetValue(*label, point, value);CHKERRQ(ierr);
8417461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
8418461a15a0SLisandro Dalcin }
8419461a15a0SLisandro Dalcin 
84200fdc7489SMatthew Knepley /*
84210fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
84220fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
84230fdc7489SMatthew Knepley   (label, id) pair in the DM.
84240fdc7489SMatthew Knepley 
84250fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
84260fdc7489SMatthew Knepley   each label.
84270fdc7489SMatthew Knepley */
84280fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
84290fdc7489SMatthew Knepley {
84300fdc7489SMatthew Knepley   DMUniversalLabel ul;
84310fdc7489SMatthew Knepley   PetscBool       *active;
84320fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
84330fdc7489SMatthew Knepley   PetscErrorCode   ierr;
84340fdc7489SMatthew Knepley 
84350fdc7489SMatthew Knepley   PetscFunctionBegin;
84360fdc7489SMatthew Knepley   ierr = PetscMalloc1(1, &ul);CHKERRQ(ierr);
84370fdc7489SMatthew Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label);CHKERRQ(ierr);
84380fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
84390fdc7489SMatthew Knepley   ierr = PetscCalloc1(Nl, &active);CHKERRQ(ierr);
84400fdc7489SMatthew Knepley   ul->Nl = 0;
84410fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
84420fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
84430fdc7489SMatthew Knepley     const char *name;
84440fdc7489SMatthew Knepley 
84450fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
84460fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "depth", 6, &isdepth);CHKERRQ(ierr);
84470fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "celltype", 9, &iscelltype);CHKERRQ(ierr);
84480fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
84490fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
84500fdc7489SMatthew Knepley   }
84510fdc7489SMatthew 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);
84520fdc7489SMatthew Knepley   ul->Nv = 0;
84530fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
84540fdc7489SMatthew Knepley     DMLabel     label;
84550fdc7489SMatthew Knepley     PetscInt    nv;
84560fdc7489SMatthew Knepley     const char *name;
84570fdc7489SMatthew Knepley 
84580fdc7489SMatthew Knepley     if (!active[l]) continue;
84590fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
84600fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
84610fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
84620fdc7489SMatthew Knepley     ierr = PetscStrallocpy(name, &ul->names[m]);CHKERRQ(ierr);
84630fdc7489SMatthew Knepley     ul->indices[m]   = l;
84640fdc7489SMatthew Knepley     ul->Nv          += nv;
84650fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
84660fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
84670fdc7489SMatthew Knepley     ++m;
84680fdc7489SMatthew Knepley   }
84690fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
84700fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
84710fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
84720fdc7489SMatthew Knepley   }
84730fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
84740fdc7489SMatthew Knepley     PetscInt b;
84750fdc7489SMatthew Knepley 
84760fdc7489SMatthew Knepley     ul->masks[l] = 0;
84770fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
84780fdc7489SMatthew Knepley   }
84790fdc7489SMatthew Knepley   ierr = PetscMalloc1(ul->Nv, &ul->values);CHKERRQ(ierr);
84800fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
84810fdc7489SMatthew Knepley     DMLabel         label;
84820fdc7489SMatthew Knepley     IS              valueIS;
84830fdc7489SMatthew Knepley     const PetscInt *varr;
84840fdc7489SMatthew Knepley     PetscInt        nv, v;
84850fdc7489SMatthew Knepley 
84860fdc7489SMatthew Knepley     if (!active[l]) continue;
84870fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
84880fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
84890fdc7489SMatthew Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
84900fdc7489SMatthew Knepley     ierr = ISGetIndices(valueIS, &varr);CHKERRQ(ierr);
84910fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
84920fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
84930fdc7489SMatthew Knepley     }
84940fdc7489SMatthew Knepley     ierr = ISRestoreIndices(valueIS, &varr);CHKERRQ(ierr);
84950fdc7489SMatthew Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
84960fdc7489SMatthew Knepley     ierr = PetscSortInt(nv, &ul->values[ul->offsets[m]]);CHKERRQ(ierr);
84970fdc7489SMatthew Knepley     ++m;
84980fdc7489SMatthew Knepley   }
84990fdc7489SMatthew Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
85000fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
85010fdc7489SMatthew Knepley     PetscInt  uval = 0;
85020fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
85030fdc7489SMatthew Knepley 
85040fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
85050fdc7489SMatthew Knepley       DMLabel  label;
85060649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
85070fdc7489SMatthew Knepley 
85080fdc7489SMatthew Knepley       if (!active[l]) continue;
85090fdc7489SMatthew Knepley       ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
85100fdc7489SMatthew Knepley       ierr = DMLabelGetValue(label, p, &val);CHKERRQ(ierr);
85110fdc7489SMatthew Knepley       ierr = DMLabelGetDefaultValue(label, &defval);CHKERRQ(ierr);
85120fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
85130649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
85140fdc7489SMatthew Knepley       marked = PETSC_TRUE;
85150fdc7489SMatthew Knepley       ierr = PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc);CHKERRQ(ierr);
85160fdc7489SMatthew Knepley       if (loc < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %D not found in compression array", val);
85170fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
85180fdc7489SMatthew Knepley       ++m;
85190fdc7489SMatthew Knepley     }
85200fdc7489SMatthew Knepley     if (marked) {ierr = DMLabelSetValue(ul->label, p, uval);CHKERRQ(ierr);}
85210fdc7489SMatthew Knepley   }
85220fdc7489SMatthew Knepley   ierr = PetscFree(active);CHKERRQ(ierr);
85230fdc7489SMatthew Knepley   *universal = ul;
85240fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85250fdc7489SMatthew Knepley }
85260fdc7489SMatthew Knepley 
85270fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
85280fdc7489SMatthew Knepley {
85290fdc7489SMatthew Knepley   PetscInt       l;
85300fdc7489SMatthew Knepley   PetscErrorCode ierr;
85310fdc7489SMatthew Knepley 
85320fdc7489SMatthew Knepley   PetscFunctionBegin;
85330fdc7489SMatthew Knepley   for (l = 0; l < (*universal)->Nl; ++l) {ierr = PetscFree((*universal)->names[l]);CHKERRQ(ierr);}
85340fdc7489SMatthew Knepley   ierr = DMLabelDestroy(&(*universal)->label);CHKERRQ(ierr);
85350fdc7489SMatthew Knepley   ierr = PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks);CHKERRQ(ierr);
85360fdc7489SMatthew Knepley   ierr = PetscFree((*universal)->values);CHKERRQ(ierr);
85370fdc7489SMatthew Knepley   ierr = PetscFree(*universal);CHKERRQ(ierr);
85380fdc7489SMatthew Knepley   *universal = NULL;
85390fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85400fdc7489SMatthew Knepley }
85410fdc7489SMatthew Knepley 
85420fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
85430fdc7489SMatthew Knepley {
85440fdc7489SMatthew Knepley   PetscFunctionBegin;
85450fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
85460fdc7489SMatthew Knepley   *ulabel = ul->label;
85470fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85480fdc7489SMatthew Knepley }
85490fdc7489SMatthew Knepley 
85500fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
85510fdc7489SMatthew Knepley {
85520fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
85530fdc7489SMatthew Knepley   PetscErrorCode ierr;
85540fdc7489SMatthew Knepley 
85550fdc7489SMatthew Knepley   PetscFunctionBegin;
8556064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
85570fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
85580fdc7489SMatthew Knepley     if (preserveOrder) {ierr = DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]);CHKERRQ(ierr);}
85590fdc7489SMatthew Knepley     else               {ierr = DMCreateLabel(dm, ul->names[l]);CHKERRQ(ierr);}
85600fdc7489SMatthew Knepley   }
85610fdc7489SMatthew Knepley   if (preserveOrder) {
85620fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
85630fdc7489SMatthew Knepley       const char *name;
85640fdc7489SMatthew Knepley       PetscBool   match;
85650fdc7489SMatthew Knepley 
85660fdc7489SMatthew Knepley       ierr = DMGetLabelName(dm, ul->indices[l], &name);CHKERRQ(ierr);
85670fdc7489SMatthew Knepley       ierr = PetscStrcmp(name, ul->names[l], &match);CHKERRQ(ierr);
85680fdc7489SMatthew Knepley       if (!match) SETERRQ3(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Label %D name %s does not match new name %s", l, name, ul->names[l]);
85690fdc7489SMatthew Knepley     }
85700fdc7489SMatthew Knepley   }
85710fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85720fdc7489SMatthew Knepley }
85730fdc7489SMatthew Knepley 
85740fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
85750fdc7489SMatthew Knepley {
85760fdc7489SMatthew Knepley   PetscInt       l;
85770fdc7489SMatthew Knepley   PetscErrorCode ierr;
85780fdc7489SMatthew Knepley 
85790fdc7489SMatthew Knepley   PetscFunctionBegin;
85800fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
85810fdc7489SMatthew Knepley     DMLabel  label;
85820fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
85830fdc7489SMatthew Knepley 
85840fdc7489SMatthew Knepley     if (lval) {
85850fdc7489SMatthew Knepley       if (useIndex) {ierr = DMGetLabelByNum(dm, ul->indices[l], &label);CHKERRQ(ierr);}
85860fdc7489SMatthew Knepley       else          {ierr = DMGetLabel(dm, ul->names[l], &label);CHKERRQ(ierr);}
85870fdc7489SMatthew Knepley       ierr = DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]);CHKERRQ(ierr);
85880fdc7489SMatthew Knepley     }
85890fdc7489SMatthew Knepley   }
85900fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85910fdc7489SMatthew Knepley }
8592a8fb8f29SToby Isaac 
8593a8fb8f29SToby Isaac /*@
8594a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
8595a8fb8f29SToby Isaac 
8596a8fb8f29SToby Isaac   Input Parameter:
8597a8fb8f29SToby Isaac . dm - The DM object
8598a8fb8f29SToby Isaac 
8599a8fb8f29SToby Isaac   Output Parameter:
8600a8fb8f29SToby Isaac . cdm - The coarse DM
8601a8fb8f29SToby Isaac 
8602a8fb8f29SToby Isaac   Level: intermediate
8603a8fb8f29SToby Isaac 
8604a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
8605a8fb8f29SToby Isaac @*/
8606a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
8607a8fb8f29SToby Isaac {
8608a8fb8f29SToby Isaac   PetscFunctionBegin;
8609a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8610a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
8611a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
8612a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8613a8fb8f29SToby Isaac }
8614a8fb8f29SToby Isaac 
8615a8fb8f29SToby Isaac /*@
8616a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
8617a8fb8f29SToby Isaac 
8618a8fb8f29SToby Isaac   Input Parameters:
8619a8fb8f29SToby Isaac + dm - The DM object
8620a8fb8f29SToby Isaac - cdm - The coarse DM
8621a8fb8f29SToby Isaac 
8622a8fb8f29SToby Isaac   Level: intermediate
8623a8fb8f29SToby Isaac 
8624a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
8625a8fb8f29SToby Isaac @*/
8626a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
8627a8fb8f29SToby Isaac {
8628a8fb8f29SToby Isaac   PetscErrorCode ierr;
8629a8fb8f29SToby Isaac 
8630a8fb8f29SToby Isaac   PetscFunctionBegin;
8631a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8632a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
8633a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
8634a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
8635a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
8636a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8637a8fb8f29SToby Isaac }
8638a8fb8f29SToby Isaac 
863988bdff64SToby Isaac /*@
864088bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
864188bdff64SToby Isaac 
864288bdff64SToby Isaac   Input Parameter:
864388bdff64SToby Isaac . dm - The DM object
864488bdff64SToby Isaac 
864588bdff64SToby Isaac   Output Parameter:
864688bdff64SToby Isaac . fdm - The fine DM
864788bdff64SToby Isaac 
864888bdff64SToby Isaac   Level: intermediate
864988bdff64SToby Isaac 
865088bdff64SToby Isaac .seealso: DMSetFineDM()
865188bdff64SToby Isaac @*/
865288bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
865388bdff64SToby Isaac {
865488bdff64SToby Isaac   PetscFunctionBegin;
865588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
865688bdff64SToby Isaac   PetscValidPointer(fdm, 2);
865788bdff64SToby Isaac   *fdm = dm->fineMesh;
865888bdff64SToby Isaac   PetscFunctionReturn(0);
865988bdff64SToby Isaac }
866088bdff64SToby Isaac 
866188bdff64SToby Isaac /*@
866288bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
866388bdff64SToby Isaac 
866488bdff64SToby Isaac   Input Parameters:
866588bdff64SToby Isaac + dm - The DM object
866688bdff64SToby Isaac - fdm - The fine DM
866788bdff64SToby Isaac 
866888bdff64SToby Isaac   Level: intermediate
866988bdff64SToby Isaac 
867088bdff64SToby Isaac .seealso: DMGetFineDM()
867188bdff64SToby Isaac @*/
867288bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
867388bdff64SToby Isaac {
867488bdff64SToby Isaac   PetscErrorCode ierr;
867588bdff64SToby Isaac 
867688bdff64SToby Isaac   PetscFunctionBegin;
867788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
867888bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
867988bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
868088bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
868188bdff64SToby Isaac   dm->fineMesh = fdm;
868288bdff64SToby Isaac   PetscFunctionReturn(0);
868388bdff64SToby Isaac }
868488bdff64SToby Isaac 
8685a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8686a6ba4734SToby Isaac 
8687a6ba4734SToby Isaac /*@C
8688a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8689a6ba4734SToby Isaac 
8690783e2ec8SMatthew G. Knepley   Collective on dm
8691783e2ec8SMatthew G. Knepley 
8692a6ba4734SToby Isaac   Input Parameters:
86934c258f51SMatthew G. Knepley + dm       - The DM, with a PetscDS that matches the problem being constrained
8694f971fd6bSMatthew G. Knepley . type     - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8695a6ba4734SToby Isaac . name     - The BC name
869645480ffeSMatthew G. Knepley . label    - The label defining constrained points
869745480ffeSMatthew G. Knepley . Nv       - The number of DMLabel values for constrained points
869845480ffeSMatthew G. Knepley . values   - An array of values for constrained points
8699a6ba4734SToby Isaac . field    - The field to constrain
870045480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
8701a6ba4734SToby Isaac . comps    - An array of constrained component numbers
8702a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
870356cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
8704a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
8705a6ba4734SToby Isaac 
870645480ffeSMatthew G. Knepley   Output Parameter:
870745480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
870845480ffeSMatthew G. Knepley 
8709a6ba4734SToby Isaac   Options Database Keys:
8710a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8711a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8712a6ba4734SToby Isaac 
871356cf3b9cSMatthew G. Knepley   Note:
871456cf3b9cSMatthew 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:
871556cf3b9cSMatthew G. Knepley 
871656cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
871756cf3b9cSMatthew G. Knepley 
871856cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
871956cf3b9cSMatthew G. Knepley 
872056cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
872156cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
872256cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
872356cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
872456cf3b9cSMatthew G. Knepley 
872556cf3b9cSMatthew G. Knepley + dim - the spatial dimension
872656cf3b9cSMatthew G. Knepley . Nf - the number of fields
872756cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
872856cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
872956cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
873056cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
873156cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
873256cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
873356cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
873456cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
873556cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
873656cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
873756cf3b9cSMatthew G. Knepley . t - current time
873856cf3b9cSMatthew G. Knepley . x - coordinates of the current point
873956cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
874056cf3b9cSMatthew G. Knepley . constants - constant parameters
874156cf3b9cSMatthew G. Knepley - bcval - output values at the current point
874256cf3b9cSMatthew G. Knepley 
8743a6ba4734SToby Isaac   Level: developer
8744a6ba4734SToby Isaac 
874545480ffeSMatthew G. Knepley .seealso: DSGetBoundary(), PetscDSAddBoundary()
8746a6ba4734SToby Isaac @*/
874745480ffeSMatthew 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)
8748a6ba4734SToby Isaac {
8749e5e52638SMatthew G. Knepley   PetscDS        ds;
8750a6ba4734SToby Isaac   PetscErrorCode ierr;
8751a6ba4734SToby Isaac 
8752a6ba4734SToby Isaac   PetscFunctionBegin;
8753a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8754783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
875545480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
875645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
875745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
875845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
8759e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
876045480ffeSMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, label);CHKERRQ(ierr);
876145480ffeSMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd);CHKERRQ(ierr);
8762a6ba4734SToby Isaac   PetscFunctionReturn(0);
8763a6ba4734SToby Isaac }
8764a6ba4734SToby Isaac 
876545480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8766e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8767e6f8dbb6SToby Isaac {
8768e5e52638SMatthew G. Knepley   PetscDS        ds;
8769dff059c6SToby Isaac   DMBoundary    *lastnext;
8770e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8771e6f8dbb6SToby Isaac   PetscErrorCode ierr;
8772e6f8dbb6SToby Isaac 
8773e6f8dbb6SToby Isaac   PetscFunctionBegin;
8774e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8775e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
877647a1f5adSToby Isaac   if (dm->boundary) {
877747a1f5adSToby Isaac     DMBoundary next = dm->boundary;
877847a1f5adSToby Isaac 
877947a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
878047a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
878147a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
878247a1f5adSToby Isaac     while (next) {
878347a1f5adSToby Isaac       DMBoundary b = next;
878447a1f5adSToby Isaac 
878547a1f5adSToby Isaac       next = b->next;
878647a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
8787a6ba4734SToby Isaac     }
878847a1f5adSToby Isaac     dm->boundary = NULL;
8789a6ba4734SToby Isaac   }
879047a1f5adSToby Isaac 
8791dff059c6SToby Isaac   lastnext = &(dm->boundary);
8792e6f8dbb6SToby Isaac   while (dsbound) {
8793e6f8dbb6SToby Isaac     DMBoundary dmbound;
8794e6f8dbb6SToby Isaac 
8795e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
8796e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
879745480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
879847a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8799dff059c6SToby Isaac     *lastnext = dmbound;
8800dff059c6SToby Isaac     lastnext = &(dmbound->next);
8801dff059c6SToby Isaac     dsbound = dsbound->next;
8802a6ba4734SToby Isaac   }
8803a6ba4734SToby Isaac   PetscFunctionReturn(0);
8804a6ba4734SToby Isaac }
8805a6ba4734SToby Isaac 
8806a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8807a6ba4734SToby Isaac {
8808b95f2879SToby Isaac   DMBoundary     b;
8809a6ba4734SToby Isaac   PetscErrorCode ierr;
8810a6ba4734SToby Isaac 
8811a6ba4734SToby Isaac   PetscFunctionBegin;
8812a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8813534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8814a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8815e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
8816b95f2879SToby Isaac   b = dm->boundary;
8817a6ba4734SToby Isaac   while (b && !(*isBd)) {
8818e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8819e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8820a6ba4734SToby Isaac     PetscInt   i;
8821a6ba4734SToby Isaac 
882245480ffeSMatthew G. Knepley     if (label) {
882345480ffeSMatthew G. Knepley       for (i = 0; i < dsb->Nv && !(*isBd); ++i) {ierr = DMLabelStratumHasPoint(label, dsb->values[i], point, isBd);CHKERRQ(ierr);}
8824a6ba4734SToby Isaac     }
8825a6ba4734SToby Isaac     b = b->next;
8826a6ba4734SToby Isaac   }
8827a6ba4734SToby Isaac   PetscFunctionReturn(0);
8828a6ba4734SToby Isaac }
88294d6f44ffSToby Isaac 
88304d6f44ffSToby Isaac /*@C
8831a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8832a6e0b375SMatthew G. Knepley 
8833a6e0b375SMatthew G. Knepley   Collective on DM
88344d6f44ffSToby Isaac 
88354d6f44ffSToby Isaac   Input Parameters:
88364d6f44ffSToby Isaac + dm      - The DM
88370709b2feSToby Isaac . time    - The time
88384d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
88394d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
88404d6f44ffSToby Isaac - mode    - The insertion mode for values
88414d6f44ffSToby Isaac 
88424d6f44ffSToby Isaac   Output Parameter:
88434d6f44ffSToby Isaac . X - vector
88444d6f44ffSToby Isaac 
88454d6f44ffSToby Isaac    Calling sequence of func:
884677b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
88474d6f44ffSToby Isaac 
88484d6f44ffSToby Isaac +  dim - The spatial dimension
88498ec8862eSJed Brown .  time - The time at which to sample
88504d6f44ffSToby Isaac .  x   - The coordinates
885177b739a6SMatthew Knepley .  Nc  - The number of components
88524d6f44ffSToby Isaac .  u   - The output field values
88534d6f44ffSToby Isaac -  ctx - optional user-defined function context
88544d6f44ffSToby Isaac 
88554d6f44ffSToby Isaac   Level: developer
88564d6f44ffSToby Isaac 
8857a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
88584d6f44ffSToby Isaac @*/
88590709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
88604d6f44ffSToby Isaac {
88614d6f44ffSToby Isaac   Vec            localX;
88624d6f44ffSToby Isaac   PetscErrorCode ierr;
88634d6f44ffSToby Isaac 
88644d6f44ffSToby Isaac   PetscFunctionBegin;
88654d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88664d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
88670709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
88684d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
88694d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
88704d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
88714d6f44ffSToby Isaac   PetscFunctionReturn(0);
88724d6f44ffSToby Isaac }
88734d6f44ffSToby Isaac 
8874a6e0b375SMatthew G. Knepley /*@C
8875a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8876a6e0b375SMatthew G. Knepley 
8877a6e0b375SMatthew G. Knepley   Not collective
8878a6e0b375SMatthew G. Knepley 
8879a6e0b375SMatthew G. Knepley   Input Parameters:
8880a6e0b375SMatthew G. Knepley + dm      - The DM
8881a6e0b375SMatthew G. Knepley . time    - The time
8882a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8883a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8884a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8885a6e0b375SMatthew G. Knepley 
8886a6e0b375SMatthew G. Knepley   Output Parameter:
8887a6e0b375SMatthew G. Knepley . localX - vector
8888a6e0b375SMatthew G. Knepley 
8889a6e0b375SMatthew G. Knepley    Calling sequence of func:
889077b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8891a6e0b375SMatthew G. Knepley 
8892a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8893a6e0b375SMatthew G. Knepley .  x   - The coordinates
889477b739a6SMatthew Knepley .  Nc  - The number of components
8895a6e0b375SMatthew G. Knepley .  u   - The output field values
8896a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8897a6e0b375SMatthew G. Knepley 
8898a6e0b375SMatthew G. Knepley   Level: developer
8899a6e0b375SMatthew G. Knepley 
8900a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8901a6e0b375SMatthew G. Knepley @*/
89020709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
89034d6f44ffSToby Isaac {
89044d6f44ffSToby Isaac   PetscErrorCode ierr;
89054d6f44ffSToby Isaac 
89064d6f44ffSToby Isaac   PetscFunctionBegin;
89074d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8908064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
89090918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
89100709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
89114d6f44ffSToby Isaac   PetscFunctionReturn(0);
89124d6f44ffSToby Isaac }
89134d6f44ffSToby Isaac 
8914a6e0b375SMatthew G. Knepley /*@C
8915a6e0b375SMatthew 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.
8916a6e0b375SMatthew G. Knepley 
8917a6e0b375SMatthew G. Knepley   Collective on DM
8918a6e0b375SMatthew G. Knepley 
8919a6e0b375SMatthew G. Knepley   Input Parameters:
8920a6e0b375SMatthew G. Knepley + dm      - The DM
8921a6e0b375SMatthew G. Knepley . time    - The time
8922a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8923a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8924a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8925a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8926a6e0b375SMatthew G. Knepley 
8927a6e0b375SMatthew G. Knepley   Output Parameter:
8928a6e0b375SMatthew G. Knepley . X - vector
8929a6e0b375SMatthew G. Knepley 
8930a6e0b375SMatthew G. Knepley    Calling sequence of func:
893177b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8932a6e0b375SMatthew G. Knepley 
8933a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8934a6e0b375SMatthew G. Knepley .  x   - The coordinates
893577b739a6SMatthew Knepley .  Nc  - The number of components
8936a6e0b375SMatthew G. Knepley .  u   - The output field values
8937a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8938a6e0b375SMatthew G. Knepley 
8939a6e0b375SMatthew G. Knepley   Level: developer
8940a6e0b375SMatthew G. Knepley 
8941a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8942a6e0b375SMatthew G. Knepley @*/
89432c53366bSMatthew 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)
89442c53366bSMatthew G. Knepley {
89452c53366bSMatthew G. Knepley   Vec            localX;
89462c53366bSMatthew G. Knepley   PetscErrorCode ierr;
89472c53366bSMatthew G. Knepley 
89482c53366bSMatthew G. Knepley   PetscFunctionBegin;
89492c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89502c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
89512c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
89522c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
89532c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
89542c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
89552c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
89562c53366bSMatthew G. Knepley }
89572c53366bSMatthew G. Knepley 
8958a6e0b375SMatthew G. Knepley /*@C
8959a6e0b375SMatthew 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.
8960a6e0b375SMatthew G. Knepley 
8961a6e0b375SMatthew G. Knepley   Not collective
8962a6e0b375SMatthew G. Knepley 
8963a6e0b375SMatthew G. Knepley   Input Parameters:
8964a6e0b375SMatthew G. Knepley + dm      - The DM
8965a6e0b375SMatthew G. Knepley . time    - The time
8966a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8967a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8968a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8969a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8970a6e0b375SMatthew G. Knepley 
8971a6e0b375SMatthew G. Knepley   Output Parameter:
8972a6e0b375SMatthew G. Knepley . localX - vector
8973a6e0b375SMatthew G. Knepley 
8974a6e0b375SMatthew G. Knepley    Calling sequence of func:
897577b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8976a6e0b375SMatthew G. Knepley 
8977a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8978a6e0b375SMatthew G. Knepley .  x   - The coordinates
897977b739a6SMatthew Knepley .  Nc  - The number of components
8980a6e0b375SMatthew G. Knepley .  u   - The output field values
8981a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8982a6e0b375SMatthew G. Knepley 
8983a6e0b375SMatthew G. Knepley   Level: developer
8984a6e0b375SMatthew G. Knepley 
8985a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8986a6e0b375SMatthew G. Knepley @*/
89871c531cf8SMatthew 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)
89884d6f44ffSToby Isaac {
89894d6f44ffSToby Isaac   PetscErrorCode ierr;
89904d6f44ffSToby Isaac 
89914d6f44ffSToby Isaac   PetscFunctionBegin;
89924d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8993064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
89940918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
89951c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
89964d6f44ffSToby Isaac   PetscFunctionReturn(0);
89974d6f44ffSToby Isaac }
89982716604bSToby Isaac 
8999a6e0b375SMatthew G. Knepley /*@C
9000a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
9001a6e0b375SMatthew G. Knepley 
9002a6e0b375SMatthew G. Knepley   Not collective
9003a6e0b375SMatthew G. Knepley 
9004a6e0b375SMatthew G. Knepley   Input Parameters:
9005a6e0b375SMatthew G. Knepley + dm      - The DM
9006a6e0b375SMatthew G. Knepley . time    - The time
9007a6e0b375SMatthew G. Knepley . localU  - The input field vector
9008a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9009a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9010a6e0b375SMatthew G. Knepley 
9011a6e0b375SMatthew G. Knepley   Output Parameter:
9012a6e0b375SMatthew G. Knepley . localX  - The output vector
9013a6e0b375SMatthew G. Knepley 
9014a6e0b375SMatthew G. Knepley    Calling sequence of func:
9015a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9016a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9017a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9018a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9019a6e0b375SMatthew G. Knepley 
9020a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
9021a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
9022a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9023a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
9024a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9025a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
9026a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9027a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9028a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9029a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9030a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9031a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9032a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9033a6e0b375SMatthew G. Knepley .  t            - The current time
9034a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9035a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9036a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9037a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9038a6e0b375SMatthew G. Knepley 
9039a6e0b375SMatthew 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.
9040a6e0b375SMatthew 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
9041a6e0b375SMatthew 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
9042a6e0b375SMatthew 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.
9043a6e0b375SMatthew G. Knepley 
9044a6e0b375SMatthew G. Knepley   Level: intermediate
9045a6e0b375SMatthew G. Knepley 
9046a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9047a6e0b375SMatthew G. Knepley @*/
90488c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
90498c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
90508c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
90518c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9052191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
90538c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
90548c6c5593SMatthew G. Knepley {
90558c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
90568c6c5593SMatthew G. Knepley 
90578c6c5593SMatthew G. Knepley   PetscFunctionBegin;
90588c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
90598c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
90608c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
90610918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
90628c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
90638c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
90648c6c5593SMatthew G. Knepley }
90658c6c5593SMatthew G. Knepley 
9066a6e0b375SMatthew G. Knepley /*@C
9067a6e0b375SMatthew 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.
9068a6e0b375SMatthew G. Knepley 
9069a6e0b375SMatthew G. Knepley   Not collective
9070a6e0b375SMatthew G. Knepley 
9071a6e0b375SMatthew G. Knepley   Input Parameters:
9072a6e0b375SMatthew G. Knepley + dm      - The DM
9073a6e0b375SMatthew G. Knepley . time    - The time
9074a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
9075a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
9076a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
9077a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9078a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9079a6e0b375SMatthew G. Knepley . localU  - The input field vector
9080a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9081a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9082a6e0b375SMatthew G. Knepley 
9083a6e0b375SMatthew G. Knepley   Output Parameter:
9084a6e0b375SMatthew G. Knepley . localX  - The output vector
9085a6e0b375SMatthew G. Knepley 
9086a6e0b375SMatthew G. Knepley    Calling sequence of func:
9087a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9088a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9089a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9090a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9091a6e0b375SMatthew G. Knepley 
9092a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
9093a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
9094a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9095a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
9096a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9097a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
9098a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9099a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9100a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9101a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9102a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9103a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9104a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9105a6e0b375SMatthew G. Knepley .  t            - The current time
9106a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9107a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9108a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9109a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9110a6e0b375SMatthew G. Knepley 
9111a6e0b375SMatthew 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.
9112a6e0b375SMatthew 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
9113a6e0b375SMatthew 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
9114a6e0b375SMatthew 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.
9115a6e0b375SMatthew G. Knepley 
9116a6e0b375SMatthew G. Knepley   Level: intermediate
9117a6e0b375SMatthew G. Knepley 
9118a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9119a6e0b375SMatthew G. Knepley @*/
91201c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
91218c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
91228c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
91238c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9124191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
91258c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
91268c6c5593SMatthew G. Knepley {
91278c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
91288c6c5593SMatthew G. Knepley 
91298c6c5593SMatthew G. Knepley   PetscFunctionBegin;
91308c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9131064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9132064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
9133ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
91341c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
91358c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
91368c6c5593SMatthew G. Knepley }
91378c6c5593SMatthew G. Knepley 
91382716604bSToby Isaac /*@C
9139ece3a9fcSMatthew 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.
9140ece3a9fcSMatthew G. Knepley 
9141ece3a9fcSMatthew G. Knepley   Not collective
9142ece3a9fcSMatthew G. Knepley 
9143ece3a9fcSMatthew G. Knepley   Input Parameters:
9144ece3a9fcSMatthew G. Knepley + dm      - The DM
9145ece3a9fcSMatthew G. Knepley . time    - The time
9146ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
9147ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
9148ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
9149ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9150ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9151ece3a9fcSMatthew G. Knepley . localU  - The input field vector
9152ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9153ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
9154ece3a9fcSMatthew G. Knepley 
9155ece3a9fcSMatthew G. Knepley   Output Parameter:
9156ece3a9fcSMatthew G. Knepley . localX  - The output vector
9157ece3a9fcSMatthew G. Knepley 
9158ece3a9fcSMatthew G. Knepley    Calling sequence of func:
9159ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9160ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9161ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9162ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9163ece3a9fcSMatthew G. Knepley 
9164ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
9165ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
9166ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9167ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
9168ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9169ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
9170ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9171ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9172ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9173ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9174ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9175ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9176ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9177ece3a9fcSMatthew G. Knepley .  t            - The current time
9178ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
9179ece3a9fcSMatthew G. Knepley .  n            - The face normal
9180ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
9181ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
9182ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
9183ece3a9fcSMatthew G. Knepley 
9184ece3a9fcSMatthew G. Knepley   Note:
9185ece3a9fcSMatthew 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.
9186ece3a9fcSMatthew 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
9187ece3a9fcSMatthew 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
9188ece3a9fcSMatthew 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.
9189ece3a9fcSMatthew G. Knepley 
9190ece3a9fcSMatthew G. Knepley   Level: intermediate
9191ece3a9fcSMatthew G. Knepley 
9192ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9193ece3a9fcSMatthew G. Knepley @*/
9194ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
9195ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
9196ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9197ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9198ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
9199ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
9200ece3a9fcSMatthew G. Knepley {
9201ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
9202ece3a9fcSMatthew G. Knepley 
9203ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
9204ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9205064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9206064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
9207ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
9208ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
9209ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
9210ece3a9fcSMatthew G. Knepley }
9211ece3a9fcSMatthew G. Knepley 
9212ece3a9fcSMatthew G. Knepley /*@C
92132716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
92142716604bSToby Isaac 
92152716604bSToby Isaac   Input Parameters:
92162716604bSToby Isaac + dm    - The DM
92170709b2feSToby Isaac . time  - The time
92182716604bSToby Isaac . funcs - The functions to evaluate for each field component
92192716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9220574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
92212716604bSToby Isaac 
92222716604bSToby Isaac   Output Parameter:
92232716604bSToby Isaac . diff - The diff ||u - u_h||_2
92242716604bSToby Isaac 
92252716604bSToby Isaac   Level: developer
92262716604bSToby Isaac 
92271189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
92282716604bSToby Isaac @*/
92290709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
92302716604bSToby Isaac {
92312716604bSToby Isaac   PetscErrorCode ierr;
92322716604bSToby Isaac 
92332716604bSToby Isaac   PetscFunctionBegin;
92342716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9235b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
92360918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
92370709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
92382716604bSToby Isaac   PetscFunctionReturn(0);
92392716604bSToby Isaac }
9240b698f381SToby Isaac 
9241b698f381SToby Isaac /*@C
9242b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
9243b698f381SToby Isaac 
9244d083f849SBarry Smith   Collective on dm
9245d083f849SBarry Smith 
9246b698f381SToby Isaac   Input Parameters:
9247b698f381SToby Isaac + dm    - The DM
9248b698f381SToby Isaac , time  - The time
9249b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
9250b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9251574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
9252b698f381SToby Isaac - n     - The vector to project along
9253b698f381SToby Isaac 
9254b698f381SToby Isaac   Output Parameter:
9255b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
9256b698f381SToby Isaac 
9257b698f381SToby Isaac   Level: developer
9258b698f381SToby Isaac 
9259b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
9260b698f381SToby Isaac @*/
9261b698f381SToby 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)
9262b698f381SToby Isaac {
9263b698f381SToby Isaac   PetscErrorCode ierr;
9264b698f381SToby Isaac 
9265b698f381SToby Isaac   PetscFunctionBegin;
9266b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9267b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
9268b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
9269b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
9270b698f381SToby Isaac   PetscFunctionReturn(0);
9271b698f381SToby Isaac }
9272b698f381SToby Isaac 
92732a16baeaSToby Isaac /*@C
92742a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
92752a16baeaSToby Isaac 
9276d083f849SBarry Smith   Collective on dm
9277d083f849SBarry Smith 
92782a16baeaSToby Isaac   Input Parameters:
92792a16baeaSToby Isaac + dm    - The DM
92802a16baeaSToby Isaac . time  - The time
92812a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
92822a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9283574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
92842a16baeaSToby Isaac 
92852a16baeaSToby Isaac   Output Parameter:
92862a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
92872a16baeaSToby Isaac 
92882a16baeaSToby Isaac   Level: developer
92892a16baeaSToby Isaac 
92901189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
92912a16baeaSToby Isaac @*/
92921189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
92932a16baeaSToby Isaac {
92942a16baeaSToby Isaac   PetscErrorCode ierr;
92952a16baeaSToby Isaac 
92962a16baeaSToby Isaac   PetscFunctionBegin;
92972a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
92982a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
92990918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
93002a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
93012a16baeaSToby Isaac   PetscFunctionReturn(0);
93022a16baeaSToby Isaac }
93032a16baeaSToby Isaac 
9304df0b854cSToby Isaac /*@C
9305df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
9306cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
9307df0b854cSToby Isaac 
9308df0b854cSToby Isaac   Collective on dm
9309df0b854cSToby Isaac 
9310df0b854cSToby Isaac   Input parameters:
9311df0b854cSToby Isaac + dm - the pre-adaptation DM object
9312a1b0c543SToby Isaac - label - label with the flags
9313df0b854cSToby Isaac 
9314df0b854cSToby Isaac   Output parameters:
93150d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
9316df0b854cSToby Isaac 
9317df0b854cSToby Isaac   Level: intermediate
93180d1cd5e0SMatthew G. Knepley 
93190d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
9320df0b854cSToby Isaac @*/
93210d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
9322df0b854cSToby Isaac {
9323df0b854cSToby Isaac   PetscErrorCode ierr;
9324df0b854cSToby Isaac 
9325df0b854cSToby Isaac   PetscFunctionBegin;
9326df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9327a1b0c543SToby Isaac   PetscValidPointer(label,2);
93280d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
93290d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
93306f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
93310d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
9332a587d139SMark   if (*dmAdapt) {
9333a587d139SMark     (*dmAdapt)->prealloc_only = dm->prealloc_only;  /* maybe this should go .... */
9334a587d139SMark     ierr = PetscFree((*dmAdapt)->vectype);CHKERRQ(ierr);
9335a587d139SMark     ierr = PetscStrallocpy(dm->vectype,(char**)&(*dmAdapt)->vectype);CHKERRQ(ierr);
9336a587d139SMark     ierr = PetscFree((*dmAdapt)->mattype);CHKERRQ(ierr);
9337a587d139SMark     ierr = PetscStrallocpy(dm->mattype,(char**)&(*dmAdapt)->mattype);CHKERRQ(ierr);
9338a587d139SMark   }
93390d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
93400d1cd5e0SMatthew G. Knepley }
93410d1cd5e0SMatthew G. Knepley 
93420d1cd5e0SMatthew G. Knepley /*@C
93430d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
93440d1cd5e0SMatthew G. Knepley 
93450d1cd5e0SMatthew G. Knepley   Input Parameters:
93460d1cd5e0SMatthew G. Knepley + dm - The DM object
93470d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
93486f25b0d8SLisandro Dalcin - bdLabel - Label for boundary tags, which will be preserved in the output mesh. bdLabel should be NULL if there is no such label, and should be different from "_boundary_".
93490d1cd5e0SMatthew G. Knepley 
93500d1cd5e0SMatthew G. Knepley   Output Parameter:
93510d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
93520d1cd5e0SMatthew G. Knepley 
93530d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
93540d1cd5e0SMatthew G. Knepley 
93550d1cd5e0SMatthew G. Knepley   Level: advanced
93560d1cd5e0SMatthew G. Knepley 
93570d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
93580d1cd5e0SMatthew G. Knepley @*/
93590d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
93600d1cd5e0SMatthew G. Knepley {
93610d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
93620d1cd5e0SMatthew G. Knepley 
93630d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
93640d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
93650d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
93660d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
93670d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
93686f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
93696f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
93700d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
9371df0b854cSToby Isaac   PetscFunctionReturn(0);
9372df0b854cSToby Isaac }
9373c4088d22SMatthew G. Knepley 
9374502a2867SDave May /*@C
9375502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
9376502a2867SDave May 
9377502a2867SDave May  Not Collective
9378502a2867SDave May 
9379502a2867SDave May  Input Parameter:
9380502a2867SDave May .  dm    - The DM
9381502a2867SDave May 
93820a19bb7dSprj-  Output Parameters:
93830a19bb7dSprj- +  nranks - the number of neighbours
93840a19bb7dSprj- -  ranks - the neighbors ranks
9385502a2867SDave May 
9386502a2867SDave May  Notes:
9387502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
9388502a2867SDave May 
9389502a2867SDave May  Level: beginner
9390502a2867SDave May 
9391dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
9392502a2867SDave May @*/
9393502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
9394502a2867SDave May {
9395502a2867SDave May   PetscErrorCode ierr;
9396502a2867SDave May 
9397502a2867SDave May   PetscFunctionBegin;
9398502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
93990918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
9400502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
9401502a2867SDave May   PetscFunctionReturn(0);
9402502a2867SDave May }
9403502a2867SDave May 
9404531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
9405531c7667SBarry Smith 
9406531c7667SBarry Smith /*
9407531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
9408531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
9409531c7667SBarry Smith */
9410531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
9411531c7667SBarry Smith {
9412531c7667SBarry Smith   PetscErrorCode ierr;
9413531c7667SBarry Smith 
9414531c7667SBarry Smith   PetscFunctionBegin;
9415531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9416531c7667SBarry Smith     Vec x1local;
9417531c7667SBarry Smith     DM  dm;
9418531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9419531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
9420531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
9421531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9422531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9423531c7667SBarry Smith     x1   = x1local;
9424531c7667SBarry Smith   }
9425531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
9426531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9427531c7667SBarry Smith     DM  dm;
9428531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9429531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
9430531c7667SBarry Smith   }
9431531c7667SBarry Smith   PetscFunctionReturn(0);
9432531c7667SBarry Smith }
9433531c7667SBarry Smith 
9434531c7667SBarry Smith /*@
9435531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
9436531c7667SBarry Smith 
9437531c7667SBarry Smith     Input Parameter:
9438531c7667SBarry Smith .    coloring - the MatFDColoring object
9439531c7667SBarry Smith 
944095452b02SPatrick Sanan     Developer Notes:
944195452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
9442531c7667SBarry Smith 
94431b266c99SBarry Smith     Level: advanced
94441b266c99SBarry Smith 
9445531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
9446531c7667SBarry Smith @*/
9447531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
9448531c7667SBarry Smith {
9449531c7667SBarry Smith   PetscFunctionBegin;
9450531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
9451531c7667SBarry Smith   PetscFunctionReturn(0);
9452531c7667SBarry Smith }
94538320bc6fSPatrick Sanan 
94548320bc6fSPatrick Sanan /*@
94558320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
94568320bc6fSPatrick Sanan 
94578320bc6fSPatrick Sanan     Collective
94588320bc6fSPatrick Sanan 
94598320bc6fSPatrick Sanan     Input Parameters:
9460a5bc1bf3SBarry Smith +    dm1 - the first DM
94618320bc6fSPatrick Sanan -    dm2 - the second DM
94628320bc6fSPatrick Sanan 
94638320bc6fSPatrick Sanan     Output Parameters:
94648320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
94658320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
94668320bc6fSPatrick Sanan 
94678320bc6fSPatrick Sanan     Notes:
94688320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
94693d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
94708320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
94713d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
94723d862458SPatrick Sanan     decomposition, but hold different data.
94738320bc6fSPatrick Sanan 
94748320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
94758320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
94768320bc6fSPatrick Sanan 
94778320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
94788320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
94798320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
94808320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
94818320bc6fSPatrick Sanan 
94828320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
94838320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
94848320bc6fSPatrick Sanan .vb
94858320bc6fSPatrick Sanan   ...
94868320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
94878320bc6fSPatrick Sanan   if (set && compatible)  {
94888320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
94898320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
94903d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
94918320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
94928320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
94938320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
94948320bc6fSPatrick Sanan       }
94958320bc6fSPatrick Sanan     }
94968320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
94978320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
94988320bc6fSPatrick Sanan   } else {
94998320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
95008320bc6fSPatrick Sanan   }
95018320bc6fSPatrick Sanan   ...
95028320bc6fSPatrick Sanan .ve
95038320bc6fSPatrick Sanan 
95048320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
95058320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
95068320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
95078320bc6fSPatrick Sanan     always check the "set" output parameter.
95088320bc6fSPatrick Sanan 
95098320bc6fSPatrick Sanan     A DM is always compatible with itself.
95108320bc6fSPatrick Sanan 
95118320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
95128320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
95138320bc6fSPatrick Sanan     incompatible.
95148320bc6fSPatrick Sanan 
95158320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
95168320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
95178320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
95188320bc6fSPatrick Sanan 
95198320bc6fSPatrick Sanan     Developer Notes:
95203d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
95213d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
9522a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
95238320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
95248320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
95253d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
95263d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
95278320bc6fSPatrick Sanan 
95288320bc6fSPatrick Sanan     Level: advanced
95298320bc6fSPatrick Sanan 
95303d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
95318320bc6fSPatrick Sanan @*/
95328320bc6fSPatrick Sanan 
9533a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
95348320bc6fSPatrick Sanan {
95358320bc6fSPatrick Sanan   PetscErrorCode ierr;
95368320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
95378320bc6fSPatrick Sanan   DMType         type,type2;
95388320bc6fSPatrick Sanan   PetscBool      sameType;
95398320bc6fSPatrick Sanan 
95408320bc6fSPatrick Sanan   PetscFunctionBegin;
9541a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
95428320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
95438320bc6fSPatrick Sanan 
95448320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
9545a5bc1bf3SBarry Smith   if (dm1 == dm2) {
95468320bc6fSPatrick Sanan     *set = PETSC_TRUE;
95478320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
95488320bc6fSPatrick Sanan     PetscFunctionReturn(0);
95498320bc6fSPatrick Sanan   }
95508320bc6fSPatrick Sanan 
95518320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
95528320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
95538320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
95548320bc6fSPatrick Sanan      determined by the implementation-specific logic */
9555ffc4695bSBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRMPI(ierr);
95568320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
95578320bc6fSPatrick Sanan     *set = PETSC_TRUE;
95588320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
95598320bc6fSPatrick Sanan     PetscFunctionReturn(0);
95608320bc6fSPatrick Sanan   }
95618320bc6fSPatrick Sanan 
95628320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
9563a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
9564a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
9565b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
95668320bc6fSPatrick Sanan   }
95678320bc6fSPatrick Sanan 
9568a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
95698320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
9570a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
95718320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
95728320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
95738320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
9574a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
95758320bc6fSPatrick Sanan   } else {
95768320bc6fSPatrick Sanan     *set = PETSC_FALSE;
95778320bc6fSPatrick Sanan   }
95788320bc6fSPatrick Sanan   PetscFunctionReturn(0);
95798320bc6fSPatrick Sanan }
9580c0f0dcc3SMatthew G. Knepley 
9581c0f0dcc3SMatthew G. Knepley /*@C
9582c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
9583c0f0dcc3SMatthew G. Knepley 
9584c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9585c0f0dcc3SMatthew G. Knepley 
9586c0f0dcc3SMatthew G. Knepley   Input Parameters:
9587c0f0dcc3SMatthew G. Knepley + DM - the DM
9588c0f0dcc3SMatthew G. Knepley . f - the monitor function
9589c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
9590c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
9591c0f0dcc3SMatthew G. Knepley 
9592c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9593c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
9594c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
9595c0f0dcc3SMatthew G. Knepley 
9596c0f0dcc3SMatthew G. Knepley   Notes:
9597c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
9598c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
9599c0f0dcc3SMatthew G. Knepley   order in which they were set.
9600c0f0dcc3SMatthew G. Knepley 
9601c0f0dcc3SMatthew G. Knepley   Fortran Notes:
9602c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
9603c0f0dcc3SMatthew G. Knepley 
9604c0f0dcc3SMatthew G. Knepley   Level: intermediate
9605c0f0dcc3SMatthew G. Knepley 
9606c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
9607c0f0dcc3SMatthew G. Knepley @*/
9608c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
9609c0f0dcc3SMatthew G. Knepley {
9610c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9611c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9612c0f0dcc3SMatthew G. Knepley 
9613c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9614c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9615c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9616c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9617c0f0dcc3SMatthew G. Knepley 
9618c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
9619c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9620c0f0dcc3SMatthew G. Knepley   }
9621c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9622c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9623c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9624c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9625c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9626c0f0dcc3SMatthew G. Knepley }
9627c0f0dcc3SMatthew G. Knepley 
9628c0f0dcc3SMatthew G. Knepley /*@
9629c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9630c0f0dcc3SMatthew G. Knepley 
9631c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9632c0f0dcc3SMatthew G. Knepley 
9633c0f0dcc3SMatthew G. Knepley   Input Parameter:
9634c0f0dcc3SMatthew G. Knepley . dm - the DM
9635c0f0dcc3SMatthew G. Knepley 
9636c0f0dcc3SMatthew G. Knepley   Options Database Key:
9637c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9638c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9639c0f0dcc3SMatthew G. Knepley   set via the options database
9640c0f0dcc3SMatthew G. Knepley 
9641c0f0dcc3SMatthew G. Knepley   Notes:
9642c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9643c0f0dcc3SMatthew G. Knepley 
9644c0f0dcc3SMatthew G. Knepley   Level: intermediate
9645c0f0dcc3SMatthew G. Knepley 
9646c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9647c0f0dcc3SMatthew G. Knepley @*/
9648c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9649c0f0dcc3SMatthew G. Knepley {
9650c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9651c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9652c0f0dcc3SMatthew G. Knepley 
9653c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9654c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9655c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9656c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
9657c0f0dcc3SMatthew G. Knepley   }
9658c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9659c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9660c0f0dcc3SMatthew G. Knepley }
9661c0f0dcc3SMatthew G. Knepley 
9662c0f0dcc3SMatthew G. Knepley /*@C
9663c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9664c0f0dcc3SMatthew G. Knepley 
9665c0f0dcc3SMatthew G. Knepley   Collective on DM
9666c0f0dcc3SMatthew G. Knepley 
9667c0f0dcc3SMatthew G. Knepley   Input Parameters:
9668c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9669c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9670c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9671c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9672c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9673c0f0dcc3SMatthew 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
9674c0f0dcc3SMatthew G. Knepley 
9675c0f0dcc3SMatthew G. Knepley   Output Parameter:
9676c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9677c0f0dcc3SMatthew G. Knepley 
9678c0f0dcc3SMatthew G. Knepley   Level: developer
9679c0f0dcc3SMatthew G. Knepley 
9680c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9681c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9682c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9683c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9684c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9685c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9686c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9687c0f0dcc3SMatthew G. Knepley @*/
9688c0f0dcc3SMatthew 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)
9689c0f0dcc3SMatthew G. Knepley {
9690c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9691c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9692c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
9693c0f0dcc3SMatthew G. Knepley 
9694c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9695c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9696c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
9697c0f0dcc3SMatthew G. Knepley   if (*flg) {
9698c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9699c0f0dcc3SMatthew G. Knepley 
9700c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
9701c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
9702c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
9703c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
9704c0f0dcc3SMatthew G. Knepley   }
9705c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9706c0f0dcc3SMatthew G. Knepley }
9707c0f0dcc3SMatthew G. Knepley 
9708c0f0dcc3SMatthew G. Knepley /*@
9709c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9710c0f0dcc3SMatthew G. Knepley 
9711c0f0dcc3SMatthew G. Knepley    Collective on DM
9712c0f0dcc3SMatthew G. Knepley 
9713c0f0dcc3SMatthew G. Knepley    Input Parameters:
9714c0f0dcc3SMatthew G. Knepley .  dm - The DM
9715c0f0dcc3SMatthew G. Knepley 
9716c0f0dcc3SMatthew G. Knepley    Level: developer
9717c0f0dcc3SMatthew G. Knepley 
9718c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9719c0f0dcc3SMatthew G. Knepley @*/
9720c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9721c0f0dcc3SMatthew G. Knepley {
9722c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9723c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9724c0f0dcc3SMatthew G. Knepley 
9725c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9726c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9727c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9728c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9729c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
9730c0f0dcc3SMatthew G. Knepley   }
9731c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9732c0f0dcc3SMatthew G. Knepley }
97332e4af2aeSMatthew G. Knepley 
97342e4af2aeSMatthew G. Knepley /*@
97352e4af2aeSMatthew G. Knepley   DMComputeError - Computes the error assuming the user has given exact solution functions
97362e4af2aeSMatthew G. Knepley 
97372e4af2aeSMatthew G. Knepley   Collective on DM
97382e4af2aeSMatthew G. Knepley 
97392e4af2aeSMatthew G. Knepley   Input Parameters:
97402e4af2aeSMatthew G. Knepley + dm     - The DM
97416b867d5aSJose E. Roman - sol    - The solution vector
97422e4af2aeSMatthew G. Knepley 
97436b867d5aSJose E. Roman   Input/Output Parameter:
97446b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
97456b867d5aSJose E. Roman            contains the error in each field
97466b867d5aSJose E. Roman 
97476b867d5aSJose E. Roman   Output Parameter:
97486b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
97492e4af2aeSMatthew G. Knepley 
97502e4af2aeSMatthew G. Knepley   Note: The exact solutions come from the PetscDS object, and the time comes from DMGetOutputSequenceNumber().
97512e4af2aeSMatthew G. Knepley 
97522e4af2aeSMatthew G. Knepley   Level: developer
97532e4af2aeSMatthew G. Knepley 
97542e4af2aeSMatthew G. Knepley .seealso: DMMonitorSet(), DMGetRegionNumDS(), PetscDSGetExactSolution(), DMGetOutputSequenceNumber()
97552e4af2aeSMatthew G. Knepley @*/
97562e4af2aeSMatthew G. Knepley PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
97572e4af2aeSMatthew G. Knepley {
97582e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
97592e4af2aeSMatthew G. Knepley   void            **ctxs;
97602e4af2aeSMatthew G. Knepley   PetscReal         time;
97612e4af2aeSMatthew G. Knepley   PetscInt          Nf, f, Nds, s;
97622e4af2aeSMatthew G. Knepley   PetscErrorCode    ierr;
97632e4af2aeSMatthew G. Knepley 
97642e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
97652e4af2aeSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
97662e4af2aeSMatthew G. Knepley   ierr = PetscCalloc2(Nf, &exactSol, Nf, &ctxs);CHKERRQ(ierr);
97672e4af2aeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
97682e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
97692e4af2aeSMatthew G. Knepley     PetscDS         ds;
97702e4af2aeSMatthew G. Knepley     DMLabel         label;
97712e4af2aeSMatthew G. Knepley     IS              fieldIS;
97722e4af2aeSMatthew G. Knepley     const PetscInt *fields;
97732e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
97742e4af2aeSMatthew G. Knepley 
97752e4af2aeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
97762e4af2aeSMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
97772e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);}
97782e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
97792e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
97802e4af2aeSMatthew G. Knepley       ierr = PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]);CHKERRQ(ierr);
97812e4af2aeSMatthew G. Knepley     }
97822e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);}
97832e4af2aeSMatthew G. Knepley   }
97842e4af2aeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
97852e4af2aeSMatthew G. Knepley     if (!exactSol[f]) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "DS must contain exact solution functions in order to calculate error, missing for field %D", f);
97862e4af2aeSMatthew G. Knepley   }
97872e4af2aeSMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
97882e4af2aeSMatthew G. Knepley   if (errors) {ierr = DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors);CHKERRQ(ierr);}
97892e4af2aeSMatthew G. Knepley   if (errorVec) {
97902e4af2aeSMatthew G. Knepley     DM             edm;
97912e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
97922e4af2aeSMatthew G. Knepley     PetscBool      simplex;
97932e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
97942e4af2aeSMatthew G. Knepley 
97952e4af2aeSMatthew G. Knepley     ierr = DMClone(dm, &edm);CHKERRQ(ierr);
97962e4af2aeSMatthew G. Knepley     ierr = DMGetDimension(edm, &dim);CHKERRQ(ierr);
97972e4af2aeSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr);
97982e4af2aeSMatthew G. Knepley     ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
97992e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
98002e4af2aeSMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
98012e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
98022e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
98032e4af2aeSMatthew G. Knepley       PetscQuadrature q;
98042e4af2aeSMatthew G. Knepley       const char     *name;
98052e4af2aeSMatthew G. Knepley 
98062e4af2aeSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
98072e4af2aeSMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe);CHKERRQ(ierr);
98082e4af2aeSMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
98092e4af2aeSMatthew G. Knepley       ierr = PetscObjectSetName((PetscObject) efe, name);CHKERRQ(ierr);
98102e4af2aeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
98112e4af2aeSMatthew G. Knepley       ierr = PetscFESetQuadrature(efe, q);CHKERRQ(ierr);
98122e4af2aeSMatthew G. Knepley       ierr = DMSetField(edm, f, NULL, (PetscObject) efe);CHKERRQ(ierr);
98132e4af2aeSMatthew G. Knepley       ierr = PetscFEDestroy(&efe);CHKERRQ(ierr);
98142e4af2aeSMatthew G. Knepley     }
98152e4af2aeSMatthew G. Knepley     ierr = DMCreateDS(edm);CHKERRQ(ierr);
98162e4af2aeSMatthew G. Knepley 
98171e1ea65dSPierre Jolivet     ierr = DMCreateGlobalVector(edm, errorVec);CHKERRQ(ierr);
98182e4af2aeSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) *errorVec, "Error");CHKERRQ(ierr);
98192e4af2aeSMatthew G. Knepley     ierr = DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec);CHKERRQ(ierr);
98202e4af2aeSMatthew G. Knepley     ierr = DMDestroy(&edm);CHKERRQ(ierr);
98212e4af2aeSMatthew G. Knepley   }
98222e4af2aeSMatthew G. Knepley   ierr = PetscFree2(exactSol, ctxs);CHKERRQ(ierr);
98232e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
98242e4af2aeSMatthew G. Knepley }
98259a2a23afSMatthew G. Knepley 
98269a2a23afSMatthew G. Knepley /*@
98279a2a23afSMatthew G. Knepley   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this DM
98289a2a23afSMatthew G. Knepley 
98299a2a23afSMatthew G. Knepley   Not collective
98309a2a23afSMatthew G. Knepley 
98319a2a23afSMatthew G. Knepley   Input Parameter:
98329a2a23afSMatthew G. Knepley . dm     - The DM
98339a2a23afSMatthew G. Knepley 
98349a2a23afSMatthew G. Knepley   Output Parameter:
9835a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
98369a2a23afSMatthew G. Knepley 
98379a2a23afSMatthew G. Knepley   Level: advanced
98389a2a23afSMatthew G. Knepley 
98399a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryLabels(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
98409a2a23afSMatthew G. Knepley @*/
98419a2a23afSMatthew G. Knepley PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
98429a2a23afSMatthew G. Knepley {
98439a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
98449a2a23afSMatthew G. Knepley 
98459a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98469a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98479a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetSize(dm->auxData, numAux);CHKERRQ(ierr);
98489a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98499a2a23afSMatthew G. Knepley }
98509a2a23afSMatthew G. Knepley 
98519a2a23afSMatthew G. Knepley /*@
98529a2a23afSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value
98539a2a23afSMatthew G. Knepley 
98549a2a23afSMatthew G. Knepley   Not collective
98559a2a23afSMatthew G. Knepley 
98569a2a23afSMatthew G. Knepley   Input Parameters:
98579a2a23afSMatthew G. Knepley + dm     - The DM
98589a2a23afSMatthew G. Knepley . label  - The DMLabel
98599a2a23afSMatthew G. Knepley - value  - The label value indicating the region
98609a2a23afSMatthew G. Knepley 
98619a2a23afSMatthew G. Knepley   Output Parameter:
98629a2a23afSMatthew G. Knepley . aux    - The Vec holding auxiliary field data
98639a2a23afSMatthew G. Knepley 
986404c51a94SMatthew G. Knepley   Note: If no auxiliary vector is found for this (label, value), (NULL, 0) is checked as well.
986504c51a94SMatthew G. Knepley 
98669a2a23afSMatthew G. Knepley   Level: advanced
98679a2a23afSMatthew G. Knepley 
98689a2a23afSMatthew G. Knepley .seealso: DMSetAuxiliaryVec(), DMGetNumAuxiliaryVec()
98699a2a23afSMatthew G. Knepley @*/
98709a2a23afSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, Vec *aux)
98719a2a23afSMatthew G. Knepley {
987204c51a94SMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0};
987304c51a94SMatthew G. Knepley   PetscBool       has;
98749a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
98759a2a23afSMatthew G. Knepley 
98769a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98779a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98789a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
98799a2a23afSMatthew G. Knepley   key.label = label;
98809a2a23afSMatthew G. Knepley   key.value = value;
988104c51a94SMatthew G. Knepley   ierr = PetscHMapAuxHas(dm->auxData, key, &has);CHKERRQ(ierr);
988204c51a94SMatthew G. Knepley   if (has) {ierr = PetscHMapAuxGet(dm->auxData, key,  aux);CHKERRQ(ierr);}
988304c51a94SMatthew G. Knepley   else     {ierr = PetscHMapAuxGet(dm->auxData, wild, aux);CHKERRQ(ierr);}
98849a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98859a2a23afSMatthew G. Knepley }
98869a2a23afSMatthew G. Knepley 
98879a2a23afSMatthew G. Knepley /*@
98889a2a23afSMatthew G. Knepley   DMSetAuxiliaryVec - Set the auxiliary vector for region specified by the given label and value
98899a2a23afSMatthew G. Knepley 
98909a2a23afSMatthew G. Knepley   Not collective
98919a2a23afSMatthew G. Knepley 
98929a2a23afSMatthew G. Knepley   Input Parameters:
98939a2a23afSMatthew G. Knepley + dm     - The DM
98949a2a23afSMatthew G. Knepley . label  - The DMLabel
98959a2a23afSMatthew G. Knepley . value  - The label value indicating the region
98969a2a23afSMatthew G. Knepley - aux    - The Vec holding auxiliary field data
98979a2a23afSMatthew G. Knepley 
98989a2a23afSMatthew G. Knepley   Level: advanced
98999a2a23afSMatthew G. Knepley 
99009a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryVec()
99019a2a23afSMatthew G. Knepley @*/
99029a2a23afSMatthew G. Knepley PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, Vec aux)
99039a2a23afSMatthew G. Knepley {
99049a2a23afSMatthew G. Knepley   Vec             old;
99059a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
99069a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
99079a2a23afSMatthew G. Knepley 
99089a2a23afSMatthew G. Knepley   PetscFunctionBegin;
99099a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
99109a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
99119a2a23afSMatthew G. Knepley   key.label = label;
99129a2a23afSMatthew G. Knepley   key.value = value;
99139a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGet(dm->auxData, key, &old);CHKERRQ(ierr);
99149a2a23afSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) aux);CHKERRQ(ierr);
99159a2a23afSMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) old);CHKERRQ(ierr);
99169a2a23afSMatthew G. Knepley   if (!aux) {ierr = PetscHMapAuxDel(dm->auxData, key);CHKERRQ(ierr);}
99179a2a23afSMatthew G. Knepley   else      {ierr = PetscHMapAuxSet(dm->auxData, key, aux);CHKERRQ(ierr);}
99189a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
99199a2a23afSMatthew G. Knepley }
99209a2a23afSMatthew G. Knepley 
99219a2a23afSMatthew G. Knepley /*@C
99229a2a23afSMatthew G. Knepley   DMGetAuxiliaryLabels - Get the labels and values for all auxiliary vectors in this DM
99239a2a23afSMatthew G. Knepley 
99249a2a23afSMatthew G. Knepley   Not collective
99259a2a23afSMatthew G. Knepley 
99269a2a23afSMatthew G. Knepley   Input Parameter:
99279a2a23afSMatthew G. Knepley . dm      - The DM
99289a2a23afSMatthew G. Knepley 
99299a2a23afSMatthew G. Knepley   Output Parameters:
99309a2a23afSMatthew G. Knepley + labels  - The DMLabels for each Vec
99319a2a23afSMatthew G. Knepley - values  - The label values for each Vec
99329a2a23afSMatthew G. Knepley 
99339a2a23afSMatthew G. Knepley   Note: The arrays passed in must be at least as large as DMGetNumAuxiliaryVec().
99349a2a23afSMatthew G. Knepley 
99359a2a23afSMatthew G. Knepley   Level: advanced
99369a2a23afSMatthew G. Knepley 
99379a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
99389a2a23afSMatthew G. Knepley @*/
99399a2a23afSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[])
99409a2a23afSMatthew G. Knepley {
99419a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
99429a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
99439a2a23afSMatthew G. Knepley   PetscErrorCode   ierr;
99449a2a23afSMatthew G. Knepley 
99459a2a23afSMatthew G. Knepley   PetscFunctionBegin;
99469a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
99479a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
99489a2a23afSMatthew G. Knepley   PetscValidPointer(values, 3);
99499a2a23afSMatthew G. Knepley   ierr = DMGetNumAuxiliaryVec(dm, &n);CHKERRQ(ierr);
99509a2a23afSMatthew G. Knepley   ierr = PetscMalloc1(n, &keys);CHKERRQ(ierr);
99519a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetKeys(dm->auxData, &off, keys);CHKERRQ(ierr);
99529a2a23afSMatthew G. Knepley   for (i = 0; i < n; ++i) {labels[i] = keys[i].label; values[i] = keys[i].value;}
99539a2a23afSMatthew G. Knepley   ierr = PetscFree(keys);CHKERRQ(ierr);
99549a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
99559a2a23afSMatthew G. Knepley }
99569a2a23afSMatthew G. Knepley 
99579a2a23afSMatthew G. Knepley /*@
99589a2a23afSMatthew G. Knepley   DMCopyAuxiliaryVec - Copy the auxiliary data to a new DM
99599a2a23afSMatthew G. Knepley 
99609a2a23afSMatthew G. Knepley   Not collective
99619a2a23afSMatthew G. Knepley 
99629a2a23afSMatthew G. Knepley   Input Parameter:
99639a2a23afSMatthew G. Knepley . dm    - The DM
99649a2a23afSMatthew G. Knepley 
99659a2a23afSMatthew G. Knepley   Output Parameter:
99669a2a23afSMatthew G. Knepley . dmNew - The new DM, now with the same auxiliary data
99679a2a23afSMatthew G. Knepley 
99689a2a23afSMatthew G. Knepley   Level: advanced
99699a2a23afSMatthew G. Knepley 
99709a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
99719a2a23afSMatthew G. Knepley @*/
99729a2a23afSMatthew G. Knepley PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
99739a2a23afSMatthew G. Knepley {
99749a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
99759a2a23afSMatthew G. Knepley 
99769a2a23afSMatthew G. Knepley   PetscFunctionBegin;
99779a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
99789a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDestroy(&dmNew->auxData);CHKERRQ(ierr);
99799a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData);CHKERRQ(ierr);
99809a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
99819a2a23afSMatthew G. Knepley }
9982b5a892a1SMatthew G. Knepley 
9983b5a892a1SMatthew G. Knepley /*@C
9984b5a892a1SMatthew G. Knepley   DMPolytopeMatchOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
9985b5a892a1SMatthew G. Knepley 
9986b5a892a1SMatthew G. Knepley   Not collective
9987b5a892a1SMatthew G. Knepley 
9988b5a892a1SMatthew G. Knepley   Input Parameters:
9989b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9990b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9991b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9992b5a892a1SMatthew G. Knepley 
9993b5a892a1SMatthew G. Knepley   Output Parameters:
9994b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
9995b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9996b5a892a1SMatthew G. Knepley 
9997b5a892a1SMatthew G. Knepley   Level: advanced
9998b5a892a1SMatthew G. Knepley 
9999b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchVertexOrientation()
10000b5a892a1SMatthew G. Knepley @*/
10001b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
10002b5a892a1SMatthew G. Knepley {
10003b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
10004b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
10005b5a892a1SMatthew G. Knepley   PetscInt       o, c;
10006b5a892a1SMatthew G. Knepley 
10007b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10008b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
10009b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
10010b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
10011b5a892a1SMatthew G. Knepley 
10012b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceCone[arr[c*2]] != targetCone[c]) break;
10013b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
10014b5a892a1SMatthew G. Knepley   }
10015b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
10016b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10017b5a892a1SMatthew G. Knepley }
10018b5a892a1SMatthew G. Knepley 
10019b5a892a1SMatthew G. Knepley /*@C
10020b5a892a1SMatthew G. Knepley   DMPolytopeGetOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
10021b5a892a1SMatthew G. Knepley 
10022b5a892a1SMatthew G. Knepley   Not collective
10023b5a892a1SMatthew G. Knepley 
10024b5a892a1SMatthew G. Knepley   Input Parameters:
10025b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10026b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
10027b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
10028b5a892a1SMatthew G. Knepley 
10029b5a892a1SMatthew G. Knepley   Output Parameters:
10030b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
10031b5a892a1SMatthew G. Knepley 
10032b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
10033b5a892a1SMatthew G. Knepley 
10034b5a892a1SMatthew G. Knepley   Level: advanced
10035b5a892a1SMatthew G. Knepley 
10036b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchOrientation(), DMPolytopeGetVertexOrientation()
10037b5a892a1SMatthew G. Knepley @*/
10038b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
10039b5a892a1SMatthew G. Knepley {
10040b5a892a1SMatthew G. Knepley   PetscBool      found;
10041b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
10042b5a892a1SMatthew G. Knepley 
10043b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10044b5a892a1SMatthew G. Knepley   ierr = DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found);CHKERRQ(ierr);
10045b5a892a1SMatthew G. Knepley   if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
10046b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10047b5a892a1SMatthew G. Knepley }
10048b5a892a1SMatthew G. Knepley 
10049b5a892a1SMatthew G. Knepley /*@C
10050b5a892a1SMatthew G. Knepley   DMPolytopeMatchVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
10051b5a892a1SMatthew G. Knepley 
10052b5a892a1SMatthew G. Knepley   Not collective
10053b5a892a1SMatthew G. Knepley 
10054b5a892a1SMatthew G. Knepley   Input Parameters:
10055b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10056b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
10057b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
10058b5a892a1SMatthew G. Knepley 
10059b5a892a1SMatthew G. Knepley   Output Parameters:
10060b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
10061b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
10062b5a892a1SMatthew G. Knepley 
10063b5a892a1SMatthew G. Knepley   Level: advanced
10064b5a892a1SMatthew G. Knepley 
10065b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchOrientation()
10066b5a892a1SMatthew G. Knepley @*/
10067b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
10068b5a892a1SMatthew G. Knepley {
10069b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
10070b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
10071b5a892a1SMatthew G. Knepley   PetscInt       o, c;
10072b5a892a1SMatthew G. Knepley 
10073b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10074b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
10075b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
10076b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
10077b5a892a1SMatthew G. Knepley 
10078b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceVert[arr[c]] != targetVert[c]) break;
10079b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
10080b5a892a1SMatthew G. Knepley   }
10081b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
10082b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10083b5a892a1SMatthew G. Knepley }
10084b5a892a1SMatthew G. Knepley 
10085b5a892a1SMatthew G. Knepley /*@C
10086b5a892a1SMatthew G. Knepley   DMPolytopeGetVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
10087b5a892a1SMatthew G. Knepley 
10088b5a892a1SMatthew G. Knepley   Not collective
10089b5a892a1SMatthew G. Knepley 
10090b5a892a1SMatthew G. Knepley   Input Parameters:
10091b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10092b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
10093b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
10094b5a892a1SMatthew G. Knepley 
10095b5a892a1SMatthew G. Knepley   Output Parameters:
10096b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
10097b5a892a1SMatthew G. Knepley 
10098b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
10099b5a892a1SMatthew G. Knepley 
10100b5a892a1SMatthew G. Knepley   Level: advanced
10101b5a892a1SMatthew G. Knepley 
10102b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchVertexOrientation(), DMPolytopeGetOrientation()
10103b5a892a1SMatthew G. Knepley @*/
10104b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
10105b5a892a1SMatthew G. Knepley {
10106b5a892a1SMatthew G. Knepley   PetscBool      found;
10107b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
10108b5a892a1SMatthew G. Knepley 
10109b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10110b5a892a1SMatthew G. Knepley   ierr = DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found);CHKERRQ(ierr);
10111b5a892a1SMatthew G. Knepley   if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
10112b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10113b5a892a1SMatthew G. Knepley }
10114012bc364SMatthew G. Knepley 
10115012bc364SMatthew G. Knepley /*@C
10116012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
10117012bc364SMatthew G. Knepley 
10118012bc364SMatthew G. Knepley   Not collective
10119012bc364SMatthew G. Knepley 
10120012bc364SMatthew G. Knepley   Input Parameters:
10121012bc364SMatthew G. Knepley + ct    - The DMPolytopeType
10122012bc364SMatthew G. Knepley - point - Coordinates of the point
10123012bc364SMatthew G. Knepley 
10124012bc364SMatthew G. Knepley   Output Parameters:
10125012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
10126012bc364SMatthew G. Knepley 
10127012bc364SMatthew G. Knepley   Level: advanced
10128012bc364SMatthew G. Knepley 
10129012bc364SMatthew G. Knepley .seealso: DMLocatePoints()
10130012bc364SMatthew G. Knepley @*/
10131012bc364SMatthew G. Knepley PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
10132012bc364SMatthew G. Knepley {
10133012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
10134012bc364SMatthew G. Knepley   PetscInt  d;
10135012bc364SMatthew G. Knepley 
10136012bc364SMatthew G. Knepley   PetscFunctionBegin;
10137012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
10138012bc364SMatthew G. Knepley   switch (ct) {
10139012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
10140012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
10141012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
10142012bc364SMatthew G. Knepley       if (point[d] < -1.0) {*inside = PETSC_FALSE; break;}
10143012bc364SMatthew G. Knepley       sum += point[d];
10144012bc364SMatthew G. Knepley     }
10145012bc364SMatthew G. Knepley     if (sum > PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10146012bc364SMatthew G. Knepley     break;
10147012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
10148012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
10149012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
10150012bc364SMatthew G. Knepley       if (PetscAbsReal(point[d]) > 1.+PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10151012bc364SMatthew G. Knepley     break;
10152012bc364SMatthew G. Knepley   default:
10153012bc364SMatthew G. Knepley     SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
10154012bc364SMatthew G. Knepley   }
10155012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
10156012bc364SMatthew G. Knepley }
10157