xref: /petsc/src/dm/interface/dm.c (revision 7a8be3513cf479fb6a672bd91de7eb6883fcfd02)
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;
733b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
743b8ba7d1SJed Brown   v->defaultConstraint.mat    = NULL;
7579769bd5SJed Brown   v->defaultConstraint.bias   = NULL;
76c6b900c6SMatthew G. Knepley   v->L                        = NULL;
77c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
785dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
799a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
8096173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
81435a35e8SMatthew G Knepley   {
82435a35e8SMatthew G Knepley     PetscInt i;
83435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
840298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
85f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
86435a35e8SMatthew G Knepley     }
87435a35e8SMatthew G Knepley   }
8845480ffeSMatthew G. Knepley   ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
89b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
90e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
919a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxCreate(&v->auxData);CHKERRQ(ierr);
9214f150ffSMatthew G. Knepley   v->dmBC = NULL;
93a8fb8f29SToby Isaac   v->coarseMesh = NULL;
94f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
95cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
96c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
97b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
984a7a4c06SLawrence Mitchell 
991411c6eeSJed Brown   *dm = v;
100a4121054SBarry Smith   PetscFunctionReturn(0);
101a4121054SBarry Smith }
102a4121054SBarry Smith 
10338221697SMatthew G. Knepley /*@
10438221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
10538221697SMatthew G. Knepley 
106d083f849SBarry Smith   Collective
10738221697SMatthew G. Knepley 
10838221697SMatthew G. Knepley   Input Parameter:
10938221697SMatthew G. Knepley . dm - The original DM object
11038221697SMatthew G. Knepley 
11138221697SMatthew G. Knepley   Output Parameter:
11238221697SMatthew G. Knepley . newdm  - The new DM object
11338221697SMatthew G. Knepley 
11438221697SMatthew G. Knepley   Level: beginner
11538221697SMatthew G. Knepley 
1161cb8cacdSPatrick Sanan   Notes:
1171cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1181cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1191cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1201bb6d2a8SBarry Smith 
12189706ed2SPatrick Sanan   The clone is considered set up iff the original is.
12289706ed2SPatrick Sanan 
12392cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1241bb6d2a8SBarry Smith 
12538221697SMatthew G. Knepley @*/
12638221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
12738221697SMatthew G. Knepley {
12838221697SMatthew G. Knepley   PetscSF        sf;
12938221697SMatthew G. Knepley   Vec            coords;
13038221697SMatthew G. Knepley   void          *ctx;
131a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
13238221697SMatthew G. Knepley   PetscErrorCode ierr;
13338221697SMatthew G. Knepley 
13438221697SMatthew G. Knepley   PetscFunctionBegin;
13538221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13638221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
13738221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1382cbb9b06SVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL);CHKERRQ(ierr);
139ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
140ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
141c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
142a587d139SMark   ierr = PetscFree((*newdm)->vectype);CHKERRQ(ierr);
143a587d139SMark   ierr = PetscStrallocpy(dm->vectype,(char**)&(*newdm)->vectype);CHKERRQ(ierr);
144a587d139SMark   ierr = PetscFree((*newdm)->mattype);CHKERRQ(ierr);
145a587d139SMark   ierr = PetscStrallocpy(dm->mattype,(char**)&(*newdm)->mattype);CHKERRQ(ierr);
1461de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1471de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
14838221697SMatthew G. Knepley   if (dm->ops->clone) {
14938221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
15038221697SMatthew G. Knepley   }
1513f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
15238221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
15338221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
15438221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
15538221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
156be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
157be4c1c3eSMatthew G. Knepley     DM           ncdm;
158be4c1c3eSMatthew G. Knepley     PetscSection cs;
1595a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
160be4c1c3eSMatthew G. Knepley 
16192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
162be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
163ffc4695bSBarry Smith     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
1645a0206caSToby Isaac     if (pEndMax >= 0) {
165be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
16683bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
16792fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
168a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
169be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
170be4c1c3eSMatthew G. Knepley     }
171be4c1c3eSMatthew G. Knepley   }
172a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
173a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
17438221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
17538221697SMatthew G. Knepley   if (coords) {
17638221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
17738221697SMatthew G. Knepley   } else {
17838221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
17938221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
18038221697SMatthew G. Knepley   }
18190b157c4SStefano Zampini   {
18290b157c4SStefano Zampini     PetscBool             isper;
183c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1845dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
18590b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
18690b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
187c6b900c6SMatthew G. Knepley   }
18834aa8a36SMatthew G. Knepley   {
18934aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
19034aa8a36SMatthew G. Knepley 
19134aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
19234aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
19334aa8a36SMatthew G. Knepley   }
19438221697SMatthew G. Knepley   PetscFunctionReturn(0);
19538221697SMatthew G. Knepley }
19638221697SMatthew G. Knepley 
1979a42bb27SBarry Smith /*@C
198564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1999a42bb27SBarry Smith 
200d083f849SBarry Smith    Logically Collective on da
2019a42bb27SBarry Smith 
202147403d9SBarry Smith    Input Parameters:
2039a42bb27SBarry Smith +  da - initial distributed array
204147403d9SBarry Smith -  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
2059a42bb27SBarry Smith 
2069a42bb27SBarry Smith    Options Database:
207147403d9SBarry Smith .   -dm_vec_type ctype - the type of vector to create
2089a42bb27SBarry Smith 
2099a42bb27SBarry Smith    Level: intermediate
2109a42bb27SBarry Smith 
211a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
2129a42bb27SBarry Smith @*/
21319fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2149a42bb27SBarry Smith {
2159a42bb27SBarry Smith   PetscErrorCode ierr;
2169a42bb27SBarry Smith 
2179a42bb27SBarry Smith   PetscFunctionBegin;
2189a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2199a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
22019fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2219a42bb27SBarry Smith   PetscFunctionReturn(0);
2229a42bb27SBarry Smith }
2239a42bb27SBarry Smith 
224c0dedaeaSBarry Smith /*@C
225c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
226c0dedaeaSBarry Smith 
227d083f849SBarry Smith    Logically Collective on da
228c0dedaeaSBarry Smith 
229c0dedaeaSBarry Smith    Input Parameter:
230c0dedaeaSBarry Smith .  da - initial distributed array
231c0dedaeaSBarry Smith 
232c0dedaeaSBarry Smith    Output Parameter:
233c0dedaeaSBarry Smith .  ctype - the vector type
234c0dedaeaSBarry Smith 
235c0dedaeaSBarry Smith    Level: intermediate
236c0dedaeaSBarry Smith 
237a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
238c0dedaeaSBarry Smith @*/
239c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
240c0dedaeaSBarry Smith {
241c0dedaeaSBarry Smith   PetscFunctionBegin;
242c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
243c0dedaeaSBarry Smith   *ctype = da->vectype;
244c0dedaeaSBarry Smith   PetscFunctionReturn(0);
245c0dedaeaSBarry Smith }
246c0dedaeaSBarry Smith 
2475f1ad066SMatthew G Knepley /*@
24834f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2495f1ad066SMatthew G Knepley 
2505f1ad066SMatthew G Knepley   Not collective
2515f1ad066SMatthew G Knepley 
2525f1ad066SMatthew G Knepley   Input Parameter:
2535f1ad066SMatthew G Knepley . v - The Vec
2545f1ad066SMatthew G Knepley 
2555f1ad066SMatthew G Knepley   Output Parameter:
2565f1ad066SMatthew G Knepley . dm - The DM
2575f1ad066SMatthew G Knepley 
2585f1ad066SMatthew G Knepley   Level: intermediate
2595f1ad066SMatthew G Knepley 
2605f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2615f1ad066SMatthew G Knepley @*/
2625f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2635f1ad066SMatthew G Knepley {
2645f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2655f1ad066SMatthew G Knepley 
2665f1ad066SMatthew G Knepley   PetscFunctionBegin;
2675f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2685f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2695f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2705f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2715f1ad066SMatthew G Knepley }
2725f1ad066SMatthew G Knepley 
2735f1ad066SMatthew G Knepley /*@
274d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2755f1ad066SMatthew G Knepley 
2765f1ad066SMatthew G Knepley   Not collective
2775f1ad066SMatthew G Knepley 
2785f1ad066SMatthew G Knepley   Input Parameters:
2795f1ad066SMatthew G Knepley + v - The Vec
2805f1ad066SMatthew G Knepley - dm - The DM
2815f1ad066SMatthew G Knepley 
282d9805387SMatthew 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.
283d9805387SMatthew G. Knepley 
2845f1ad066SMatthew G Knepley   Level: intermediate
2855f1ad066SMatthew G Knepley 
2865f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2875f1ad066SMatthew G Knepley @*/
2885f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2895f1ad066SMatthew G Knepley {
2905f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2915f1ad066SMatthew G Knepley 
2925f1ad066SMatthew G Knepley   PetscFunctionBegin;
2935f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
294d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2955f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2965f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2975f1ad066SMatthew G Knepley }
2985f1ad066SMatthew G Knepley 
299521d9a4cSLisandro Dalcin /*@C
3008f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
3018f1509bcSBarry Smith 
302d083f849SBarry Smith    Logically Collective on dm
3038f1509bcSBarry Smith 
3048f1509bcSBarry Smith    Input Parameters:
3058f1509bcSBarry Smith +  dm - the DM context
3068f1509bcSBarry Smith -  ctype - the matrix type
3078f1509bcSBarry Smith 
3088f1509bcSBarry Smith    Options Database:
3098f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3108f1509bcSBarry Smith 
3118f1509bcSBarry Smith    Level: intermediate
3128f1509bcSBarry Smith 
3138f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3148f1509bcSBarry Smith           DMGetISColoringType()
3158f1509bcSBarry Smith @*/
3168f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3178f1509bcSBarry Smith {
3188f1509bcSBarry Smith   PetscFunctionBegin;
3198f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3208f1509bcSBarry Smith   dm->coloringtype = ctype;
3218f1509bcSBarry Smith   PetscFunctionReturn(0);
3228f1509bcSBarry Smith }
3238f1509bcSBarry Smith 
3248f1509bcSBarry Smith /*@C
3258f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
326521d9a4cSLisandro Dalcin 
327d083f849SBarry Smith    Logically Collective on dm
328521d9a4cSLisandro Dalcin 
329521d9a4cSLisandro Dalcin    Input Parameter:
3308f1509bcSBarry Smith .  dm - the DM context
3318f1509bcSBarry Smith 
3328f1509bcSBarry Smith    Output Parameter:
3338f1509bcSBarry Smith .  ctype - the matrix type
3348f1509bcSBarry Smith 
3358f1509bcSBarry Smith    Options Database:
3368f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3378f1509bcSBarry Smith 
3388f1509bcSBarry Smith    Level: intermediate
3398f1509bcSBarry Smith 
3408f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3418f1509bcSBarry Smith           DMGetISColoringType()
3428f1509bcSBarry Smith @*/
3438f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3448f1509bcSBarry Smith {
3458f1509bcSBarry Smith   PetscFunctionBegin;
3468f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3478f1509bcSBarry Smith   *ctype = dm->coloringtype;
3488f1509bcSBarry Smith   PetscFunctionReturn(0);
3498f1509bcSBarry Smith }
3508f1509bcSBarry Smith 
3518f1509bcSBarry Smith /*@C
3528f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3538f1509bcSBarry Smith 
354d083f849SBarry Smith    Logically Collective on dm
3558f1509bcSBarry Smith 
3568f1509bcSBarry Smith    Input Parameters:
357521d9a4cSLisandro Dalcin +  dm - the DM context
358a2b5a043SBarry Smith -  ctype - the matrix type
359521d9a4cSLisandro Dalcin 
360521d9a4cSLisandro Dalcin    Options Database:
361147403d9SBarry Smith .   -dm_mat_type ctype - the type of the matrix to create
362521d9a4cSLisandro Dalcin 
363521d9a4cSLisandro Dalcin    Level: intermediate
364521d9a4cSLisandro Dalcin 
365a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
366521d9a4cSLisandro Dalcin @*/
36719fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
368521d9a4cSLisandro Dalcin {
369521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
37088f0584fSBarry Smith 
371521d9a4cSLisandro Dalcin   PetscFunctionBegin;
372521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
373521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
37419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
375521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
376521d9a4cSLisandro Dalcin }
377521d9a4cSLisandro Dalcin 
378c0dedaeaSBarry Smith /*@C
379c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
380c0dedaeaSBarry Smith 
381d083f849SBarry Smith    Logically Collective on dm
382c0dedaeaSBarry Smith 
383c0dedaeaSBarry Smith    Input Parameter:
384c0dedaeaSBarry Smith .  dm - the DM context
385c0dedaeaSBarry Smith 
386c0dedaeaSBarry Smith    Output Parameter:
387c0dedaeaSBarry Smith .  ctype - the matrix type
388c0dedaeaSBarry Smith 
389c0dedaeaSBarry Smith    Options Database:
390147403d9SBarry Smith .   -dm_mat_type ctype - the matrix type
391c0dedaeaSBarry Smith 
392c0dedaeaSBarry Smith    Level: intermediate
393c0dedaeaSBarry Smith 
394a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
395c0dedaeaSBarry Smith @*/
396c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
397c0dedaeaSBarry Smith {
398c0dedaeaSBarry Smith   PetscFunctionBegin;
399c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
400c0dedaeaSBarry Smith   *ctype = dm->mattype;
401c0dedaeaSBarry Smith   PetscFunctionReturn(0);
402c0dedaeaSBarry Smith }
403c0dedaeaSBarry Smith 
404c688c046SMatthew G Knepley /*@
40534f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
406c688c046SMatthew G Knepley 
407c688c046SMatthew G Knepley   Not collective
408c688c046SMatthew G Knepley 
409c688c046SMatthew G Knepley   Input Parameter:
410c688c046SMatthew G Knepley . A - The Mat
411c688c046SMatthew G Knepley 
412c688c046SMatthew G Knepley   Output Parameter:
413c688c046SMatthew G Knepley . dm - The DM
414c688c046SMatthew G Knepley 
415c688c046SMatthew G Knepley   Level: intermediate
416c688c046SMatthew G Knepley 
4178f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4188f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4198f1509bcSBarry Smith 
420c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
421c688c046SMatthew G Knepley @*/
422c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
423c688c046SMatthew G Knepley {
424c688c046SMatthew G Knepley   PetscErrorCode ierr;
425c688c046SMatthew G Knepley 
426c688c046SMatthew G Knepley   PetscFunctionBegin;
427c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
428c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
429c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
430c688c046SMatthew G Knepley   PetscFunctionReturn(0);
431c688c046SMatthew G Knepley }
432c688c046SMatthew G Knepley 
433c688c046SMatthew G Knepley /*@
434c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
435c688c046SMatthew G Knepley 
436c688c046SMatthew G Knepley   Not collective
437c688c046SMatthew G Knepley 
438c688c046SMatthew G Knepley   Input Parameters:
439c688c046SMatthew G Knepley + A - The Mat
440c688c046SMatthew G Knepley - dm - The DM
441c688c046SMatthew G Knepley 
442c688c046SMatthew G Knepley   Level: intermediate
443c688c046SMatthew G Knepley 
4448f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4458f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4468f1509bcSBarry Smith 
447c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
448c688c046SMatthew G Knepley @*/
449c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
450c688c046SMatthew G Knepley {
451c688c046SMatthew G Knepley   PetscErrorCode ierr;
452c688c046SMatthew G Knepley 
453c688c046SMatthew G Knepley   PetscFunctionBegin;
454c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4558865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
456c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
457c688c046SMatthew G Knepley   PetscFunctionReturn(0);
458c688c046SMatthew G Knepley }
459c688c046SMatthew G Knepley 
4609a42bb27SBarry Smith /*@C
4619a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4626757b960SDave May    DM options in the database.
4639a42bb27SBarry Smith 
464d083f849SBarry Smith    Logically Collective on dm
4659a42bb27SBarry Smith 
466d8d19677SJose E. Roman    Input Parameters:
4678353ddbbSDave May +  da - the DM context
4689a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4699a42bb27SBarry Smith 
4709a42bb27SBarry Smith    Notes:
4719a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4729a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4739a42bb27SBarry Smith 
4749a42bb27SBarry Smith    Level: advanced
4759a42bb27SBarry Smith 
4769a42bb27SBarry Smith .seealso: DMSetFromOptions()
4779a42bb27SBarry Smith @*/
4787087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4799a42bb27SBarry Smith {
4809a42bb27SBarry Smith   PetscErrorCode ierr;
4819a42bb27SBarry Smith 
4829a42bb27SBarry Smith   PetscFunctionBegin;
4839a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4849a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
485691be533SLawrence Mitchell   if (dm->sf) {
486691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
487691be533SLawrence Mitchell   }
4881bb6d2a8SBarry Smith   if (dm->sectionSF) {
4891bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
490691be533SLawrence Mitchell   }
4919a42bb27SBarry Smith   PetscFunctionReturn(0);
4929a42bb27SBarry Smith }
4939a42bb27SBarry Smith 
49431697293SDave May /*@C
49531697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
49631697293SDave May    DM options in the database.
49731697293SDave May 
498d083f849SBarry Smith    Logically Collective on dm
49931697293SDave May 
50031697293SDave May    Input Parameters:
50131697293SDave May +  dm - the DM context
50231697293SDave May -  prefix - the prefix string to prepend to all DM option requests
50331697293SDave May 
50431697293SDave May    Notes:
50531697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
50631697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
50731697293SDave May 
50831697293SDave May    Level: advanced
50931697293SDave May 
51031697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
51131697293SDave May @*/
51231697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
51331697293SDave May {
51431697293SDave May   PetscErrorCode ierr;
51531697293SDave May 
51631697293SDave May   PetscFunctionBegin;
51731697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
51831697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
51931697293SDave May   PetscFunctionReturn(0);
52031697293SDave May }
52131697293SDave May 
52231697293SDave May /*@C
52331697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
52431697293SDave May    DM options in the database.
52531697293SDave May 
52631697293SDave May    Not Collective
52731697293SDave May 
52831697293SDave May    Input Parameters:
52931697293SDave May .  dm - the DM context
53031697293SDave May 
53131697293SDave May    Output Parameters:
53231697293SDave May .  prefix - pointer to the prefix string used is returned
53331697293SDave May 
53495452b02SPatrick Sanan    Notes:
53595452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
53631697293SDave May    sufficient length to hold the prefix.
53731697293SDave May 
53831697293SDave May    Level: advanced
53931697293SDave May 
54031697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
54131697293SDave May @*/
54231697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
54331697293SDave May {
54431697293SDave May   PetscErrorCode ierr;
54531697293SDave May 
54631697293SDave May   PetscFunctionBegin;
54731697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54831697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
54931697293SDave May   PetscFunctionReturn(0);
55031697293SDave May }
55131697293SDave May 
55288bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
55388bdff64SToby Isaac {
5546eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
55588bdff64SToby Isaac   PetscErrorCode ierr;
55688bdff64SToby Isaac 
55788bdff64SToby Isaac   PetscFunctionBegin;
558aab5bcd8SJed Brown   *ncrefct = 0;
55988bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
56088bdff64SToby Isaac     refct--;
56188bdff64SToby Isaac     if (recurseCoarse) {
56288bdff64SToby Isaac       PetscInt coarseCount;
56388bdff64SToby Isaac 
56488bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
56588bdff64SToby Isaac       refct += coarseCount;
56688bdff64SToby Isaac     }
56788bdff64SToby Isaac   }
56888bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56988bdff64SToby Isaac     refct--;
57088bdff64SToby Isaac     if (recurseFine) {
57188bdff64SToby Isaac       PetscInt fineCount;
57288bdff64SToby Isaac 
57388bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
57488bdff64SToby Isaac       refct += fineCount;
57588bdff64SToby Isaac     }
57688bdff64SToby Isaac   }
57788bdff64SToby Isaac   *ncrefct = refct;
57888bdff64SToby Isaac   PetscFunctionReturn(0);
57988bdff64SToby Isaac }
58088bdff64SToby Isaac 
581f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
582354557abSToby Isaac {
5835d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
584354557abSToby Isaac   PetscErrorCode ierr;
585354557abSToby Isaac 
586354557abSToby Isaac   PetscFunctionBegin;
587354557abSToby Isaac   /* destroy the labels */
588354557abSToby Isaac   while (next) {
589354557abSToby Isaac     DMLabelLink tmp = next->next;
590354557abSToby Isaac 
5915d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
592ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
593354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
594354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
595354557abSToby Isaac     next = tmp;
596354557abSToby Isaac   }
5975d80c0bfSVaclav Hapla   dm->labels = NULL;
598354557abSToby Isaac   PetscFunctionReturn(0);
599354557abSToby Isaac }
600354557abSToby Isaac 
6011fb7b255SJunchao Zhang /*@C
6028472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
60347c6ae99SBarry Smith 
604d083f849SBarry Smith     Collective on dm
60547c6ae99SBarry Smith 
60647c6ae99SBarry Smith     Input Parameter:
60747c6ae99SBarry Smith .   dm - the DM object to destroy
60847c6ae99SBarry Smith 
60947c6ae99SBarry Smith     Level: developer
61047c6ae99SBarry Smith 
611e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
61247c6ae99SBarry Smith 
61347c6ae99SBarry Smith @*/
614fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
61547c6ae99SBarry Smith {
6166eb26441SStefano Zampini   PetscInt       cnt;
617dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61847c6ae99SBarry Smith   PetscErrorCode ierr;
61947c6ae99SBarry Smith 
62047c6ae99SBarry Smith   PetscFunctionBegin;
6216bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6226bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
62387e657c6SBarry Smith 
62488bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
62588bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
62688bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
627ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6286bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6296bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6306eb26441SStefano Zampini 
6316eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6326eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6336eb26441SStefano Zampini 
634f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6350298fd71SBarry Smith   (*dm)->namedglobal = NULL;
636f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6372348bcf4SPeter Brune     nnext = nlink->next;
638*7a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6392348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6402348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6412348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6422348bcf4SPeter Brune   }
643f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6440298fd71SBarry Smith   (*dm)->namedlocal = NULL;
645f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
646f490541aSPeter Brune     nnext = nlink->next;
647*7a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
648f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
649f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
650f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
651f490541aSPeter Brune   }
6522348bcf4SPeter Brune 
653b17ce1afSJed Brown   /* Destroy the list of hooks */
654c833c3b5SJed Brown   {
655c833c3b5SJed Brown     DMCoarsenHookLink link,next;
656b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
657b17ce1afSJed Brown       next = link->next;
658b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
659b17ce1afSJed Brown     }
6600298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
661c833c3b5SJed Brown   }
662c833c3b5SJed Brown   {
663c833c3b5SJed Brown     DMRefineHookLink link,next;
664c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
665c833c3b5SJed Brown       next = link->next;
666c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
667c833c3b5SJed Brown     }
6680298fd71SBarry Smith     (*dm)->refinehook = NULL;
669c833c3b5SJed Brown   }
670be081cd6SPeter Brune   {
671be081cd6SPeter Brune     DMSubDomainHookLink link,next;
672be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
673be081cd6SPeter Brune       next = link->next;
674be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
675be081cd6SPeter Brune     }
6760298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
677be081cd6SPeter Brune   }
678baf369e7SPeter Brune   {
679baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
680baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
681baf369e7SPeter Brune       next = link->next;
682baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
683baf369e7SPeter Brune     }
6840298fd71SBarry Smith     (*dm)->gtolhook = NULL;
685baf369e7SPeter Brune   }
686d4d07f1eSToby Isaac   {
687d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
688d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
689d4d07f1eSToby Isaac       next = link->next;
690d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
691d4d07f1eSToby Isaac     }
692d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
693d4d07f1eSToby Isaac   }
694aa1993deSMatthew G Knepley   /* Destroy the work arrays */
695aa1993deSMatthew G Knepley   {
696aa1993deSMatthew G Knepley     DMWorkLink link,next;
697*7a8be351SBarry Smith     PetscCheck(!(*dm)->workout,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
698aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
699aa1993deSMatthew G Knepley       next = link->next;
700aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
701aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
702aa1993deSMatthew G Knepley     }
7030298fd71SBarry Smith     (*dm)->workin = NULL;
704aa1993deSMatthew G Knepley   }
705c58f1c22SToby Isaac   /* destroy the labels */
706f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
707f4cdcedcSVaclav Hapla   /* destroy the fields */
708e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
709f4cdcedcSVaclav Hapla   /* destroy the boundaries */
710e6f8dbb6SToby Isaac   {
711e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
712e6f8dbb6SToby Isaac     while (next) {
713e6f8dbb6SToby Isaac       DMBoundary b = next;
714e6f8dbb6SToby Isaac 
715e6f8dbb6SToby Isaac       next = b->next;
716e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
717e6f8dbb6SToby Isaac     }
718e6f8dbb6SToby Isaac   }
719b17ce1afSJed Brown 
72052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
72152536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
72252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
72352536dc3SBarry Smith 
7241a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7251a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7261a266240SBarry Smith   }
72771cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7286bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7296bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
730073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
73188ed4aceSMatthew G Knepley 
7321bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7331bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7348b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
7353b8ba7d1SJed Brown   ierr = PetscSectionDestroy(&(*dm)->defaultConstraint.section);CHKERRQ(ierr);
7363b8ba7d1SJed Brown   ierr = MatDestroy(&(*dm)->defaultConstraint.mat);CHKERRQ(ierr);
73788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7381bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
739736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
740736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7418e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
742736995cdSBlaise Bourdin     }
743736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
744736995cdSBlaise Bourdin   }
7459a2a23afSMatthew G. Knepley   {
7469a2a23afSMatthew G. Knepley     Vec     *auxData;
7479a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7489a2a23afSMatthew G. Knepley 
7499a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxGetSize((*dm)->auxData, &n);CHKERRQ(ierr);
7509a2a23afSMatthew G. Knepley     ierr = PetscMalloc1(n, &auxData);CHKERRQ(ierr);
7519a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxGetVals((*dm)->auxData, &off, auxData);CHKERRQ(ierr);
7529a2a23afSMatthew G. Knepley     for (i = 0; i < n; ++i) {ierr = VecDestroy(&auxData[i]);CHKERRQ(ierr);}
7539a2a23afSMatthew G. Knepley     ierr = PetscFree(auxData);CHKERRQ(ierr);
7549a2a23afSMatthew G. Knepley     ierr = PetscHMapAuxDestroy(&(*dm)->auxData);CHKERRQ(ierr);
7559a2a23afSMatthew G. Knepley   }
75688bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
75788bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
75888bdff64SToby Isaac   }
7596eb26441SStefano Zampini 
760a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
76188bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
76288bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
76388bdff64SToby Isaac   }
76488bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
765f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7666636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7676636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7686636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
769412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
770412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
771412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
772ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
773ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
774ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7756636e97aSMatthew G Knepley 
776e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
77714f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
778e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
779e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
780732e2eb9SMatthew G Knepley 
781ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7826bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
783ed3c66a1SDave May   }
784c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
785f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
786a2c9b50fSJeremy L Thompson   ierr = CeedElemRestrictionDestroy(&(*dm)->ceedERestrict);CHKERRQ_CEED(ierr);
787a2c9b50fSJeremy L Thompson   ierr = CeedDestroy(&(*dm)->ceed);CHKERRQ_CEED(ierr);
788f918ec44SMatthew G. Knepley #endif
789435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
790732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
79147c6ae99SBarry Smith   PetscFunctionReturn(0);
79247c6ae99SBarry Smith }
79347c6ae99SBarry Smith 
794d7bf68aeSBarry Smith /*@
795d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
796d7bf68aeSBarry Smith 
797d083f849SBarry Smith     Collective on dm
798d7bf68aeSBarry Smith 
799d7bf68aeSBarry Smith     Input Parameter:
800d7bf68aeSBarry Smith .   dm - the DM object to setup
801d7bf68aeSBarry Smith 
802d7bf68aeSBarry Smith     Level: developer
803d7bf68aeSBarry Smith 
804e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
805d7bf68aeSBarry Smith 
806d7bf68aeSBarry Smith @*/
8077087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
808d7bf68aeSBarry Smith {
809d7bf68aeSBarry Smith   PetscErrorCode ierr;
810d7bf68aeSBarry Smith 
811d7bf68aeSBarry Smith   PetscFunctionBegin;
812171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8138387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
814d7bf68aeSBarry Smith   if (dm->ops->setup) {
815d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
816d7bf68aeSBarry Smith   }
8178387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
818d7bf68aeSBarry Smith   PetscFunctionReturn(0);
819d7bf68aeSBarry Smith }
820d7bf68aeSBarry Smith 
821d7bf68aeSBarry Smith /*@
822d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
823d7bf68aeSBarry Smith 
824d083f849SBarry Smith     Collective on dm
825d7bf68aeSBarry Smith 
826d7bf68aeSBarry Smith     Input Parameter:
827d7bf68aeSBarry Smith .   dm - the DM object to set options for
828d7bf68aeSBarry Smith 
829732e2eb9SMatthew G Knepley     Options Database:
8304164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8314164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8324164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
833a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
834a4ea9b21SRichard 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
835732e2eb9SMatthew G Knepley 
8369318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8379318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8389318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
839cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
8409318fe57SMatthew G. Knepley . -dm_plex_shape <shape>            - The domain shape, such as DM_SHAPE_BOX, DM_SHAPE_SPHERE, etc.
8419318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8429318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8439318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
8449318fe57SMatthew G. Knepley . -dm_plex_simplex <bool>           - PETSC_TRUE for simplex elements, PETSC_FALSE for tensor elements
8459318fe57SMatthew G. Knepley . -dm_plex_interpolate <bool>       - PETSC_TRUE turns on topological interpolation (creating edges and faces)
8469318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8479318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8489318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8499318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
8509318fe57SMatthew G. Knepley . -dm_plex_box_bd <bx,by,bz>        - Specify the DMBoundaryType for each direction
8519318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8529318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8539318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8549318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
855bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8569318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8579318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8589318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8599318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
860bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
861d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
862d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
863d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
864d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
865d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
866909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
867909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8689318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8699318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8709318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8719318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8729318fe57SMatthew G. Knepley 
873384a6580SVaclav Hapla     DMPLEX Specific Checks
874384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
875384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
876384a6580SVaclav 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()
877384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
878384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
879384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
880384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
881d7bf68aeSBarry Smith 
88295eb5ee5SVaclav Hapla     Level: intermediate
88395eb5ee5SVaclav Hapla 
88495eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
88595eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
886d7bf68aeSBarry Smith 
887d7bf68aeSBarry Smith @*/
8887087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
889d7bf68aeSBarry Smith {
8907781c08eSBarry Smith   char           typeName[256];
891ca266f36SBarry Smith   PetscBool      flg;
892d7bf68aeSBarry Smith   PetscErrorCode ierr;
893d7bf68aeSBarry Smith 
894d7bf68aeSBarry Smith   PetscFunctionBegin;
895171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
89649be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
89749be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8981bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8993194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
9000298fd71SBarry 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);
901a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
902f9ba7244SBarry Smith   if (flg) {
903f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
904f9ba7244SBarry Smith   }
905a264d7a6SBarry 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);
906073dac72SJed Brown   if (flg) {
907521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
908073dac72SJed Brown   }
9098f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
910a4ea9b21SRichard 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);
911f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
912e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
913f9ba7244SBarry Smith   }
914f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
9150633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
91682fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
917d7bf68aeSBarry Smith   PetscFunctionReturn(0);
918d7bf68aeSBarry Smith }
919d7bf68aeSBarry Smith 
920fc9bc008SSatish Balay /*@C
921fe2efc57SMark    DMViewFromOptions - View from Options
922fe2efc57SMark 
923fe2efc57SMark    Collective on DM
924fe2efc57SMark 
925fe2efc57SMark    Input Parameters:
926fe2efc57SMark +  dm - the DM object
927736c3998SJose E. Roman .  obj - Optional object
928736c3998SJose E. Roman -  name - command line option
929fe2efc57SMark 
930fe2efc57SMark    Level: intermediate
931fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
932fe2efc57SMark @*/
933fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
934fe2efc57SMark {
935fe2efc57SMark   PetscErrorCode ierr;
936fe2efc57SMark 
937fe2efc57SMark   PetscFunctionBegin;
938fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
939fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
940fe2efc57SMark   PetscFunctionReturn(0);
941fe2efc57SMark }
942fe2efc57SMark 
943fe2efc57SMark /*@C
944224748a4SBarry Smith     DMView - Views a DM
94547c6ae99SBarry Smith 
946d083f849SBarry Smith     Collective on dm
94747c6ae99SBarry Smith 
948d8d19677SJose E. Roman     Input Parameters:
94947c6ae99SBarry Smith +   dm - the DM object to view
95047c6ae99SBarry Smith -   v - the viewer
95147c6ae99SBarry Smith 
952cd7e8a5eSksagiyam     Notes:
953cd7e8a5eSksagiyam     Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
954cd7e8a5eSksagiyam     meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
955cd7e8a5eSksagiyam     before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
956cd7e8a5eSksagiyam 
957224748a4SBarry Smith     Level: beginner
95847c6ae99SBarry Smith 
959cd7e8a5eSksagiyam .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMLoad(), PetscObjectSetName()
96047c6ae99SBarry Smith 
96147c6ae99SBarry Smith @*/
9627087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
96347c6ae99SBarry Smith {
96447c6ae99SBarry Smith   PetscErrorCode    ierr;
96532c0f0efSBarry Smith   PetscBool         isbinary;
96676a8abe0SBarry Smith   PetscMPIInt       size;
96776a8abe0SBarry Smith   PetscViewerFormat format;
96847c6ae99SBarry Smith 
96947c6ae99SBarry Smith   PetscFunctionBegin;
970171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9713014e516SBarry Smith   if (!v) {
972ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9733014e516SBarry Smith   }
974b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
97574903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
97674903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
97774903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97874903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97974903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
98074903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
98174903a4fSStefano Zampini      in an error here */
98274903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9838bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
984b1b135c8SBarry Smith 
98576a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
986ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRMPI(ierr);
98776a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
98898c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
98932c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
99032c0f0efSBarry Smith   if (isbinary) {
99155849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
99232c0f0efSBarry Smith     char     type[256];
99332c0f0efSBarry Smith 
994f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
99532c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
996f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
99732c0f0efSBarry Smith   }
9980c010503SBarry Smith   if (dm->ops->view) {
9990c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
100047c6ae99SBarry Smith   }
100147c6ae99SBarry Smith   PetscFunctionReturn(0);
100247c6ae99SBarry Smith }
100347c6ae99SBarry Smith 
100447c6ae99SBarry Smith /*@
10058472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
100647c6ae99SBarry Smith 
1007d083f849SBarry Smith     Collective on dm
100847c6ae99SBarry Smith 
100947c6ae99SBarry Smith     Input Parameter:
101047c6ae99SBarry Smith .   dm - the DM object
101147c6ae99SBarry Smith 
101247c6ae99SBarry Smith     Output Parameter:
101347c6ae99SBarry Smith .   vec - the global vector
101447c6ae99SBarry Smith 
1015073dac72SJed Brown     Level: beginner
101647c6ae99SBarry Smith 
1017ec075b9fSPatrick Sanan .seealso DMCreateLocalVector(), DMGetGlobalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
101847c6ae99SBarry Smith 
101947c6ae99SBarry Smith @*/
10207087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
102147c6ae99SBarry Smith {
102247c6ae99SBarry Smith   PetscErrorCode ierr;
102347c6ae99SBarry Smith 
102447c6ae99SBarry Smith   PetscFunctionBegin;
1025171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1026b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1027*7a8be351SBarry Smith   PetscCheck(dm->ops->createglobalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
102847c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
102976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1030c6b011d8SStefano Zampini     DM vdm;
1031c6b011d8SStefano Zampini 
1032c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1033*7a8be351SBarry Smith     PetscCheck(vdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
1034c6b011d8SStefano Zampini   }
103547c6ae99SBarry Smith   PetscFunctionReturn(0);
103647c6ae99SBarry Smith }
103747c6ae99SBarry Smith 
103847c6ae99SBarry Smith /*@
10398472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
104047c6ae99SBarry Smith 
104147c6ae99SBarry Smith     Not Collective
104247c6ae99SBarry Smith 
104347c6ae99SBarry Smith     Input Parameter:
104447c6ae99SBarry Smith .   dm - the DM object
104547c6ae99SBarry Smith 
104647c6ae99SBarry Smith     Output Parameter:
104747c6ae99SBarry Smith .   vec - the local vector
104847c6ae99SBarry Smith 
1049073dac72SJed Brown     Level: beginner
105047c6ae99SBarry Smith 
1051ec075b9fSPatrick Sanan .seealso DMCreateGlobalVector(), DMGetLocalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
105247c6ae99SBarry Smith 
105347c6ae99SBarry Smith @*/
10547087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
105547c6ae99SBarry Smith {
105647c6ae99SBarry Smith   PetscErrorCode ierr;
105747c6ae99SBarry Smith 
105847c6ae99SBarry Smith   PetscFunctionBegin;
1059171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1060b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
1061*7a8be351SBarry Smith   PetscCheck(dm->ops->createlocalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
106247c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
106376bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1064c6b011d8SStefano Zampini     DM vdm;
1065c6b011d8SStefano Zampini 
1066c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
1067*7a8be351SBarry Smith     PetscCheck(vdm,PETSC_COMM_SELF,PETSC_ERR_LIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
1068c6b011d8SStefano Zampini   }
106947c6ae99SBarry Smith   PetscFunctionReturn(0);
107047c6ae99SBarry Smith }
107147c6ae99SBarry Smith 
10721411c6eeSJed Brown /*@
10731411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10741411c6eeSJed Brown 
1075d083f849SBarry Smith    Collective on dm
10761411c6eeSJed Brown 
10771411c6eeSJed Brown    Input Parameter:
10781411c6eeSJed Brown .  dm - the DM that provides the mapping
10791411c6eeSJed Brown 
10801411c6eeSJed Brown    Output Parameter:
10811411c6eeSJed Brown .  ltog - the mapping
10821411c6eeSJed Brown 
10831411c6eeSJed Brown    Level: intermediate
10841411c6eeSJed Brown 
10851411c6eeSJed Brown    Notes:
10861411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10871411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10881411c6eeSJed Brown 
1089fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10901411c6eeSJed Brown @*/
10917087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10921411c6eeSJed Brown {
10930be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10941411c6eeSJed Brown   PetscErrorCode ierr;
10951411c6eeSJed Brown 
10961411c6eeSJed Brown   PetscFunctionBegin;
10971411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10981411c6eeSJed Brown   PetscValidPointer(ltog,2);
10991411c6eeSJed Brown   if (!dm->ltogmap) {
110037d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
110137d0c07bSMatthew G Knepley 
110292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
110337d0c07bSMatthew G Knepley     if (section) {
1104a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
110537d0c07bSMatthew G Knepley       PetscInt       *ltog;
1106ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
110737d0c07bSMatthew G Knepley 
1108e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
110937d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1110ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1111ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
111237d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1113e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
111437d0c07bSMatthew G Knepley 
111537d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
111637d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1117a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1118a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
111937d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
11201a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11211a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
11221a7dc684SMatthew G. Knepley         if (dof) {
11235b8243e1SJed Brown           bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11241a7dc684SMatthew G. Knepley         }
11255227eafbSStefano Zampini 
1126e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11275227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1128e6befd46SJed Brown             ltog[l] = off < 0 ? off-c : -(off+c+1);
1129e6befd46SJed Brown             cind++;
1130e6befd46SJed Brown           } else {
11315227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off+1) : off) + c - cind;
1132e6befd46SJed Brown           }
113337d0c07bSMatthew G Knepley         }
113437d0c07bSMatthew G Knepley       }
1135bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11360be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
11370be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
11380be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11390be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
11407591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11417591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1142ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1143ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1144ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1145ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l]/bs : -(-(ltog[l]+1)/bs + 1);
1146ca469d19SJed Brown         }
1147ccf3bd66SMatthew G. Knepley         n /= bs;
1148ccf3bd66SMatthew G. Knepley       }
1149ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
11503bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
115137d0c07bSMatthew G Knepley     } else {
1152*7a8be351SBarry Smith       PetscCheck(dm->ops->getlocaltoglobalmapping,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1153184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
11541411c6eeSJed Brown     }
115537d0c07bSMatthew G Knepley   }
11561411c6eeSJed Brown   *ltog = dm->ltogmap;
11571411c6eeSJed Brown   PetscFunctionReturn(0);
11581411c6eeSJed Brown }
11591411c6eeSJed Brown 
11601411c6eeSJed Brown /*@
11611411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
11621411c6eeSJed Brown 
11631411c6eeSJed Brown    Not Collective
11641411c6eeSJed Brown 
11651411c6eeSJed Brown    Input Parameter:
11661411c6eeSJed Brown .  dm - the DM with block structure
11671411c6eeSJed Brown 
11681411c6eeSJed Brown    Output Parameter:
11691411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11701411c6eeSJed Brown 
11711411c6eeSJed Brown    Level: intermediate
11721411c6eeSJed Brown 
1173fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
11741411c6eeSJed Brown @*/
11757087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
11761411c6eeSJed Brown {
11771411c6eeSJed Brown   PetscFunctionBegin;
11781411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1179534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
1180*7a8be351SBarry Smith   PetscCheck(dm->bs >= 1,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
11811411c6eeSJed Brown   *bs = dm->bs;
11821411c6eeSJed Brown   PetscFunctionReturn(0);
11831411c6eeSJed Brown }
11841411c6eeSJed Brown 
118548eeb7c8SBarry Smith /*@C
11868472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
118747c6ae99SBarry Smith 
1188a5bc1bf3SBarry Smith     Collective on dmc
118947c6ae99SBarry Smith 
1190d8d19677SJose E. Roman     Input Parameters:
1191a5bc1bf3SBarry Smith +   dmc - the DM object
1192a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
119347c6ae99SBarry Smith 
1194d8d19677SJose E. Roman     Output Parameters:
119547c6ae99SBarry Smith +  mat - the interpolation
119647c6ae99SBarry Smith -  vec - the scaling (optional)
119747c6ae99SBarry Smith 
119847c6ae99SBarry Smith     Level: developer
119947c6ae99SBarry Smith 
120095452b02SPatrick Sanan     Notes:
120195452b02SPatrick 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
120285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1203d52bd9f3SBarry Smith 
12041f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1205d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
120685afcc9aSBarry Smith 
12072ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
120847c6ae99SBarry Smith 
120947c6ae99SBarry Smith @*/
1210a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
121147c6ae99SBarry Smith {
121247c6ae99SBarry Smith   PetscErrorCode ierr;
121347c6ae99SBarry Smith 
121447c6ae99SBarry Smith   PetscFunctionBegin;
1215a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1216a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1217c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1218*7a8be351SBarry Smith   PetscCheck(dmc->ops->createinterpolation,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1219a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1220a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1221a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
122247c6ae99SBarry Smith   PetscFunctionReturn(0);
122347c6ae99SBarry Smith }
122447c6ae99SBarry Smith 
12253ad4599aSBarry Smith /*@
1226d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
12272ed6491fSPatrick Sanan 
12282ed6491fSPatrick Sanan   Input Parameters:
12292ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
12302ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
12312ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12322ed6491fSPatrick Sanan 
12332ed6491fSPatrick Sanan   Output Parameter:
12342ed6491fSPatrick Sanan .    scale - the scaled vector
12352ed6491fSPatrick Sanan 
12362ed6491fSPatrick Sanan   Level: developer
12372ed6491fSPatrick Sanan 
1238e9c74fd6SRichard Tran Mills   Developer Notes:
1239e9c74fd6SRichard Tran Mills   If the fine-scale DMDA has the -dm_bind_below option set to true, then DMCreateInterpolationScale() calls MatSetBindingPropagates()
1240e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1241e9c74fd6SRichard Tran Mills 
12422ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
12432ed6491fSPatrick Sanan 
12442ed6491fSPatrick Sanan @*/
12452ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
12462ed6491fSPatrick Sanan {
12472ed6491fSPatrick Sanan   PetscErrorCode ierr;
12482ed6491fSPatrick Sanan   Vec            fine;
12492ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
12509704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1251e9c74fd6SRichard Tran Mills   PetscBool      bindingpropagates,isbound;
12529704db99SRichard Tran Mills #endif
12532ed6491fSPatrick Sanan 
12542ed6491fSPatrick Sanan   PetscFunctionBegin;
12552ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
12562ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
12572ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
12589704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12599704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12609704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12619704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
1262e9c74fd6SRichard Tran Mills   ierr = VecGetBindingPropagates(fine,&bindingpropagates);CHKERRQ(ierr);
1263e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
1264b1538989SRichard Tran Mills     ierr = MatSetBindingPropagates(mat,PETSC_TRUE);CHKERRQ(ierr);
1265e9c74fd6SRichard Tran Mills     ierr = VecBoundToCPU(fine,&isbound);CHKERRQ(ierr);
1266e9c74fd6SRichard Tran Mills     ierr = MatBindToCPU(mat,isbound);CHKERRQ(ierr);
126783aa49f4SRichard Tran Mills   }
12689704db99SRichard Tran Mills #endif
12692ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
12702ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
12712ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
12722ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12732ed6491fSPatrick Sanan }
12742ed6491fSPatrick Sanan 
12752ed6491fSPatrick Sanan /*@
12763ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
12773ad4599aSBarry Smith 
1278a5bc1bf3SBarry Smith     Collective on dmc
12793ad4599aSBarry Smith 
1280d8d19677SJose E. Roman     Input Parameters:
1281a5bc1bf3SBarry Smith +   dmc - the DM object
1282a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
12833ad4599aSBarry Smith 
12843ad4599aSBarry Smith     Output Parameter:
12853ad4599aSBarry Smith .  mat - the restriction
12863ad4599aSBarry Smith 
12873ad4599aSBarry Smith     Level: developer
12883ad4599aSBarry Smith 
128995452b02SPatrick Sanan     Notes:
129095452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
12913ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
12923ad4599aSBarry Smith 
12933ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
12943ad4599aSBarry Smith 
12953ad4599aSBarry Smith @*/
1296a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12973ad4599aSBarry Smith {
12983ad4599aSBarry Smith   PetscErrorCode ierr;
12993ad4599aSBarry Smith 
13003ad4599aSBarry Smith   PetscFunctionBegin;
1301a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1302a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
13035a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1304*7a8be351SBarry Smith   PetscCheck(dmc->ops->createrestriction,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1305a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1306a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1307a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
13083ad4599aSBarry Smith   PetscFunctionReturn(0);
13093ad4599aSBarry Smith }
13103ad4599aSBarry Smith 
131147c6ae99SBarry Smith /*@
13128472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
131347c6ae99SBarry Smith 
1314a5bc1bf3SBarry Smith     Collective on dac
131547c6ae99SBarry Smith 
1316d8d19677SJose E. Roman     Input Parameters:
1317a5bc1bf3SBarry Smith +   dac - the DM object
1318a5bc1bf3SBarry Smith -   daf - the second, finer DM object
131947c6ae99SBarry Smith 
132047c6ae99SBarry Smith     Output Parameter:
13216dbf9973SLawrence Mitchell .   mat - the injection
132247c6ae99SBarry Smith 
132347c6ae99SBarry Smith     Level: developer
132447c6ae99SBarry Smith 
132595452b02SPatrick Sanan    Notes:
132695452b02SPatrick 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
132785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
132885afcc9aSBarry Smith 
1329e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
133047c6ae99SBarry Smith 
133147c6ae99SBarry Smith @*/
1332a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
133347c6ae99SBarry Smith {
133447c6ae99SBarry Smith   PetscErrorCode ierr;
133547c6ae99SBarry Smith 
133647c6ae99SBarry Smith   PetscFunctionBegin;
1337a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1338a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
13395a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1340*7a8be351SBarry Smith   PetscCheck(dac->ops->createinjection,PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1341a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1342a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1343a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
134447c6ae99SBarry Smith   PetscFunctionReturn(0);
134547c6ae99SBarry Smith }
134647c6ae99SBarry Smith 
1347b412c318SBarry Smith /*@
1348bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1349bd041c0cSMatthew G. Knepley 
1350a5bc1bf3SBarry Smith   Collective on dac
1351bd041c0cSMatthew G. Knepley 
1352d8d19677SJose E. Roman   Input Parameters:
1353b4937a87SMatthew G. Knepley + dmc - the target DM object
1354b4937a87SMatthew G. Knepley - dmf - the source DM object
1355bd041c0cSMatthew G. Knepley 
1356bd041c0cSMatthew G. Knepley   Output Parameter:
1357b4937a87SMatthew G. Knepley . mat - the mass matrix
1358bd041c0cSMatthew G. Knepley 
1359bd041c0cSMatthew G. Knepley   Level: developer
1360bd041c0cSMatthew G. Knepley 
1361b4937a87SMatthew G. Knepley .seealso DMCreateMassMatrixLumped(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1362bd041c0cSMatthew G. Knepley @*/
1363b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1364bd041c0cSMatthew G. Knepley {
1365bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1366bd041c0cSMatthew G. Knepley 
1367bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1368b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1369b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13705a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
13712c71b3e2SJacob Faibussowitsch   PetscCheck(dmc->ops->createmassmatrix,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dmc)->type_name);
1372b4937a87SMatthew G. Knepley   ierr = (*dmc->ops->createmassmatrix)(dmc, dmf, mat);CHKERRQ(ierr);
1373b4937a87SMatthew G. Knepley   PetscFunctionReturn(0);
1374b4937a87SMatthew G. Knepley }
1375b4937a87SMatthew G. Knepley 
1376b4937a87SMatthew G. Knepley /*@
1377b4937a87SMatthew G. Knepley   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given DM
1378b4937a87SMatthew G. Knepley 
1379b4937a87SMatthew G. Knepley   Collective on dm
1380b4937a87SMatthew G. Knepley 
1381b4937a87SMatthew G. Knepley   Input Parameter:
1382b4937a87SMatthew G. Knepley . dm - the DM object
1383b4937a87SMatthew G. Knepley 
1384b4937a87SMatthew G. Knepley   Output Parameter:
1385b4937a87SMatthew G. Knepley . lm - the lumped mass matrix
1386b4937a87SMatthew G. Knepley 
1387b4937a87SMatthew G. Knepley   Level: developer
1388b4937a87SMatthew G. Knepley 
1389b4937a87SMatthew G. Knepley .seealso DMCreateMassMatrix(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1390b4937a87SMatthew G. Knepley @*/
1391b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1392b4937a87SMatthew G. Knepley {
1393b4937a87SMatthew G. Knepley   PetscErrorCode ierr;
1394b4937a87SMatthew G. Knepley 
1395b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1396b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1397b4937a87SMatthew G. Knepley   PetscValidPointer(lm,2);
13982c71b3e2SJacob Faibussowitsch   PetscCheck(dm->ops->createmassmatrixlumped,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrixLumped",((PetscObject)dm)->type_name);
1399b4937a87SMatthew G. Knepley   ierr = (*dm->ops->createmassmatrixlumped)(dm, lm);CHKERRQ(ierr);
1400bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1401bd041c0cSMatthew G. Knepley }
1402bd041c0cSMatthew G. Knepley 
1403bd041c0cSMatthew G. Knepley /*@
1404b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
140547c6ae99SBarry Smith 
1406d083f849SBarry Smith     Collective on dm
140747c6ae99SBarry Smith 
1408d8d19677SJose E. Roman     Input Parameters:
140947c6ae99SBarry Smith +   dm - the DM object
14105bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
141147c6ae99SBarry Smith 
141247c6ae99SBarry Smith     Output Parameter:
141347c6ae99SBarry Smith .   coloring - the coloring
141447c6ae99SBarry Smith 
1415ec5066bdSBarry Smith     Notes:
1416ec5066bdSBarry 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
1417ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1418ec5066bdSBarry Smith 
1419ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1420ec5066bdSBarry Smith 
142147c6ae99SBarry Smith     Level: developer
142247c6ae99SBarry Smith 
1423ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
142447c6ae99SBarry Smith 
1425aab9d709SJed Brown @*/
1426b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
142747c6ae99SBarry Smith {
142847c6ae99SBarry Smith   PetscErrorCode ierr;
142947c6ae99SBarry Smith 
143047c6ae99SBarry Smith   PetscFunctionBegin;
1431171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14325a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1433*7a8be351SBarry Smith   PetscCheck(dm->ops->getcoloring,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1434b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
143547c6ae99SBarry Smith   PetscFunctionReturn(0);
143647c6ae99SBarry Smith }
143747c6ae99SBarry Smith 
1438b412c318SBarry Smith /*@
14398472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
144047c6ae99SBarry Smith 
1441d083f849SBarry Smith     Collective on dm
144247c6ae99SBarry Smith 
144347c6ae99SBarry Smith     Input Parameter:
1444b412c318SBarry Smith .   dm - the DM object
144547c6ae99SBarry Smith 
144647c6ae99SBarry Smith     Output Parameter:
144747c6ae99SBarry Smith .   mat - the empty Jacobian
144847c6ae99SBarry Smith 
1449073dac72SJed Brown     Level: beginner
145047c6ae99SBarry Smith 
1451f27dd7c6SMatthew G. Knepley     Options Database Keys:
1452f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1453f27dd7c6SMatthew G. Knepley 
145495452b02SPatrick Sanan     Notes:
145595452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
145694013140SBarry Smith        do not need to do it yourself.
145794013140SBarry Smith 
145894013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
14597889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
146094013140SBarry Smith 
146194013140SBarry 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
146294013140SBarry Smith        internally by PETSc.
146394013140SBarry Smith 
146494013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1465aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
146694013140SBarry Smith 
1467b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
146847c6ae99SBarry Smith 
1469aab9d709SJed Brown @*/
1470b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
147147c6ae99SBarry Smith {
147247c6ae99SBarry Smith   PetscErrorCode ierr;
147347c6ae99SBarry Smith 
147447c6ae99SBarry Smith   PetscFunctionBegin;
1475171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1476064a246eSJacob Faibussowitsch   PetscValidPointer(mat,2);
1477*7a8be351SBarry Smith   PetscCheck(dm->ops->creatematrix,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
14785a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1479fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1480b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
148176bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1482c6b011d8SStefano Zampini     DM mdm;
1483c6b011d8SStefano Zampini 
1484c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1485*7a8be351SBarry Smith     PetscCheck(mdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the matrix",((PetscObject)dm)->type_name);
1486c6b011d8SStefano Zampini   }
1487e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1488e5e52638SMatthew G. Knepley   if (dm->Nf) {
1489e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1490649ef022SMatthew Knepley     PetscInt     Nf, f;
1491e571a35bSMatthew G. Knepley 
1492e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1493649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1494649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
1495649ef022SMatthew Knepley         ierr = (*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1496e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1497e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1498649ef022SMatthew Knepley         break;
1499e571a35bSMatthew G. Knepley       }
1500649ef022SMatthew Knepley     }
1501649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1502649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
1503649ef022SMatthew Knepley         ierr = (*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace);CHKERRQ(ierr);
1504e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1505e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1506e571a35bSMatthew G. Knepley       }
1507e571a35bSMatthew G. Knepley     }
1508e571a35bSMatthew G. Knepley   }
1509fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
151047c6ae99SBarry Smith   PetscFunctionReturn(0);
151147c6ae99SBarry Smith }
151247c6ae99SBarry Smith 
1513732e2eb9SMatthew G Knepley /*@
1514aa0f6e3cSJed Brown   DMSetMatrixPreallocateSkip - When DMCreateMatrix() is called the matrix sizes and ISLocalToGlobalMapping will be
1515aa0f6e3cSJed Brown   properly set, but the entries will not be preallocated. This is most useful to reduce initialization costs when
1516aa0f6e3cSJed Brown   MatSetPreallocationCOO() and MatSetValuesCOO() will be used.
1517aa0f6e3cSJed Brown 
1518aa0f6e3cSJed Brown   Logically Collective on dm
1519aa0f6e3cSJed Brown 
1520aa0f6e3cSJed Brown   Input Parameters:
1521aa0f6e3cSJed Brown + dm - the DM
1522aa0f6e3cSJed Brown - skip - PETSC_TRUE to skip preallocation
1523aa0f6e3cSJed Brown 
1524aa0f6e3cSJed Brown   Level: developer
1525aa0f6e3cSJed Brown 
1526aa0f6e3cSJed Brown .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1527aa0f6e3cSJed Brown @*/
1528aa0f6e3cSJed Brown PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1529aa0f6e3cSJed Brown {
1530aa0f6e3cSJed Brown   PetscFunctionBegin;
1531aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1532aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
1533aa0f6e3cSJed Brown   PetscFunctionReturn(0);
1534aa0f6e3cSJed Brown }
1535aa0f6e3cSJed Brown 
1536aa0f6e3cSJed Brown /*@
1537950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1538732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1539732e2eb9SMatthew G Knepley 
1540d083f849SBarry Smith   Logically Collective on dm
1541732e2eb9SMatthew G Knepley 
1542d8d19677SJose E. Roman   Input Parameters:
1543732e2eb9SMatthew G Knepley + dm - the DM
1544732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1545732e2eb9SMatthew G Knepley 
1546732e2eb9SMatthew G Knepley   Level: developer
1547f27dd7c6SMatthew G. Knepley 
1548f27dd7c6SMatthew G. Knepley   Options Database Keys:
1549f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1550f27dd7c6SMatthew G. Knepley 
1551b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1552732e2eb9SMatthew G Knepley @*/
1553732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1554732e2eb9SMatthew G Knepley {
1555732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1556732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1557732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1558732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1559732e2eb9SMatthew G Knepley }
1560732e2eb9SMatthew G Knepley 
1561b06ff27eSHong Zhang /*@
1562b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1563b06ff27eSHong Zhang     but the array for values will not be allocated.
1564b06ff27eSHong Zhang 
1565d083f849SBarry Smith   Logically Collective on dm
1566b06ff27eSHong Zhang 
1567d8d19677SJose E. Roman   Input Parameters:
1568b06ff27eSHong Zhang + dm - the DM
1569b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1570b06ff27eSHong Zhang 
1571b06ff27eSHong Zhang   Level: developer
1572b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1573b06ff27eSHong Zhang @*/
1574b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1575b06ff27eSHong Zhang {
1576b06ff27eSHong Zhang   PetscFunctionBegin;
1577b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1578b06ff27eSHong Zhang   dm->structure_only = only;
1579b06ff27eSHong Zhang   PetscFunctionReturn(0);
1580b06ff27eSHong Zhang }
1581b06ff27eSHong Zhang 
1582a89ea682SMatthew G Knepley /*@C
1583aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1584a89ea682SMatthew G Knepley 
1585a89ea682SMatthew G Knepley   Not Collective
1586a89ea682SMatthew G Knepley 
1587a89ea682SMatthew G Knepley   Input Parameters:
1588a89ea682SMatthew G Knepley + dm - the DM object
1589a5b23f4aSJose E. Roman . count - The minimum size
159069291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1591a89ea682SMatthew G Knepley 
1592a89ea682SMatthew G Knepley   Output Parameter:
1593a89ea682SMatthew G Knepley . array - the work array
1594a89ea682SMatthew G Knepley 
1595a89ea682SMatthew G Knepley   Level: developer
1596a89ea682SMatthew G Knepley 
1597a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1598a89ea682SMatthew G Knepley @*/
159969291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1600a89ea682SMatthew G Knepley {
1601a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1602aa1993deSMatthew G Knepley   DMWorkLink     link;
160369291d52SBarry Smith   PetscMPIInt    dsize;
1604a89ea682SMatthew G Knepley 
1605a89ea682SMatthew G Knepley   PetscFunctionBegin;
1606a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1607aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1608aa1993deSMatthew G Knepley   if (dm->workin) {
1609aa1993deSMatthew G Knepley     link       = dm->workin;
1610aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1611aa1993deSMatthew G Knepley   } else {
1612b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1613a89ea682SMatthew G Knepley   }
1614ffc4695bSBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRMPI(ierr);
16155056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1616aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1617854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1618854ce69bSBarry Smith     link->bytes = dsize*count;
1619aa1993deSMatthew G Knepley   }
1620aa1993deSMatthew G Knepley   link->next   = dm->workout;
1621aa1993deSMatthew G Knepley   dm->workout  = link;
162200d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
162300d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
162400d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
162500d952a4SJed Brown #endif
1626aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1627a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1628a89ea682SMatthew G Knepley }
1629a89ea682SMatthew G Knepley 
1630aa1993deSMatthew G Knepley /*@C
1631aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1632aa1993deSMatthew G Knepley 
1633aa1993deSMatthew G Knepley   Not Collective
1634aa1993deSMatthew G Knepley 
1635aa1993deSMatthew G Knepley   Input Parameters:
1636aa1993deSMatthew G Knepley + dm - the DM object
1637a5b23f4aSJose E. Roman . count - The minimum size
163869291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1639aa1993deSMatthew G Knepley 
1640aa1993deSMatthew G Knepley   Output Parameter:
1641aa1993deSMatthew G Knepley . array - the work array
1642aa1993deSMatthew G Knepley 
1643aa1993deSMatthew G Knepley   Level: developer
1644aa1993deSMatthew G Knepley 
164595452b02SPatrick Sanan   Developer Notes:
164695452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1647147403d9SBarry Smith 
1648aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1649aa1993deSMatthew G Knepley @*/
165069291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1651aa1993deSMatthew G Knepley {
1652aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1653aa1993deSMatthew G Knepley 
1654aa1993deSMatthew G Knepley   PetscFunctionBegin;
1655aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1656aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1657aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1658aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1659aa1993deSMatthew G Knepley       *p           = link->next;
1660aa1993deSMatthew G Knepley       link->next   = dm->workin;
1661aa1993deSMatthew G Knepley       dm->workin   = link;
16620298fd71SBarry Smith       *(void**)mem = NULL;
1663aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1664aa1993deSMatthew G Knepley     }
1665aa1993deSMatthew G Knepley   }
1666aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1667aa1993deSMatthew G Knepley }
1668e7c4fc90SDmitry Karpeev 
16698cda7954SMatthew G. Knepley /*@C
1670147403d9SBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field when function spaces are joined or split, such as in DMCreateSubDM()
16718cda7954SMatthew G. Knepley 
16728cda7954SMatthew G. Knepley   Logically collective on DM
16738cda7954SMatthew G. Knepley 
16748cda7954SMatthew G. Knepley   Input Parameters:
16758cda7954SMatthew G. Knepley + dm     - The DM
16768cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16778cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
16788cda7954SMatthew G. Knepley 
1679147403d9SBarry Smith   Calling sequence of nullsp:
1680147403d9SBarry Smith .vb
1681147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1682147403d9SBarry Smith .ve
1683147403d9SBarry Smith +  dm        - The present DM
1684147403d9SBarry Smith .  origField - The field number given above, in the original DM
1685147403d9SBarry Smith .  field     - The field number in dm
1686147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16878cda7954SMatthew G. Knepley 
16888cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16898cda7954SMatthew G. Knepley 
16908cda7954SMatthew G. Knepley .seealso: DMGetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
1691147403d9SBarry Smith @*/
1692147403d9SBarry Smith PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace*))
1693435a35e8SMatthew G Knepley {
1694435a35e8SMatthew G Knepley   PetscFunctionBegin;
1695435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1696*7a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1697435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1698435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1699435a35e8SMatthew G Knepley }
1700435a35e8SMatthew G Knepley 
17018cda7954SMatthew G. Knepley /*@C
17028cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
17038cda7954SMatthew G. Knepley 
17048cda7954SMatthew G. Knepley   Not collective
17058cda7954SMatthew G. Knepley 
17068cda7954SMatthew G. Knepley   Input Parameters:
17078cda7954SMatthew G. Knepley + dm     - The DM
17088cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17098cda7954SMatthew G. Knepley 
17108cda7954SMatthew G. Knepley   Output Parameter:
17118cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17128cda7954SMatthew G. Knepley 
1713147403d9SBarry Smith   Calling sequence of nullsp:
1714147403d9SBarry Smith .vb
1715147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1716147403d9SBarry Smith .ve
1717147403d9SBarry Smith +  dm        - The present DM
1718147403d9SBarry Smith .  origField - The field number given above, in the original DM
1719147403d9SBarry Smith .  field     - The field number in dm
1720147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17218cda7954SMatthew G. Knepley 
17228cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
17238cda7954SMatthew G. Knepley 
17248cda7954SMatthew G. Knepley .seealso: DMSetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
1725147403d9SBarry Smith @*/
1726147403d9SBarry Smith PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
17270a50eb56SMatthew G. Knepley {
17280a50eb56SMatthew G. Knepley   PetscFunctionBegin;
17290a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1730f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1731*7a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
17320a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
17330a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
17340a50eb56SMatthew G. Knepley }
17350a50eb56SMatthew G. Knepley 
17368cda7954SMatthew G. Knepley /*@C
17378cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
17388cda7954SMatthew G. Knepley 
17398cda7954SMatthew G. Knepley   Logically collective on DM
17408cda7954SMatthew G. Knepley 
17418cda7954SMatthew G. Knepley   Input Parameters:
17428cda7954SMatthew G. Knepley + dm     - The DM
17438cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17448cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
17458cda7954SMatthew G. Knepley 
1746147403d9SBarry Smith   Calling sequence of nullsp:
1747147403d9SBarry Smith .vb
1748147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1749147403d9SBarry Smith .ve
1750147403d9SBarry Smith +  dm        - The present DM
1751147403d9SBarry Smith .  origField - The field number given above, in the original DM
1752147403d9SBarry Smith .  field     - The field number in dm
1753147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17548cda7954SMatthew G. Knepley 
17558cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
17568cda7954SMatthew G. Knepley 
17578cda7954SMatthew G. Knepley .seealso: DMGetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
1758147403d9SBarry Smith @*/
1759147403d9SBarry Smith PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1760f9d4088aSMatthew G. Knepley {
1761f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1762f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1763*7a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1764f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1765f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1766f9d4088aSMatthew G. Knepley }
1767f9d4088aSMatthew G. Knepley 
17688cda7954SMatthew G. Knepley /*@C
17698cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
17708cda7954SMatthew G. Knepley 
17718cda7954SMatthew G. Knepley   Not collective
17728cda7954SMatthew G. Knepley 
17738cda7954SMatthew G. Knepley   Input Parameters:
17748cda7954SMatthew G. Knepley + dm     - The DM
17758cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17768cda7954SMatthew G. Knepley 
17778cda7954SMatthew G. Knepley   Output Parameter:
17788cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
17798cda7954SMatthew G. Knepley 
1780147403d9SBarry Smith   Calling sequence of nullsp:
1781147403d9SBarry Smith .vb
1782147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1783147403d9SBarry Smith .ve
1784147403d9SBarry Smith +  dm        - The present DM
1785147403d9SBarry Smith .  origField - The field number given above, in the original DM
1786147403d9SBarry Smith .  field     - The field number in dm
1787147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17888cda7954SMatthew G. Knepley 
17898cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
17908cda7954SMatthew G. Knepley 
17918cda7954SMatthew G. Knepley .seealso: DMSetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
1792147403d9SBarry Smith @*/
1793147403d9SBarry Smith PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1794f9d4088aSMatthew G. Knepley {
1795f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1796f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1797f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1798*7a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1799f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1800f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1801f9d4088aSMatthew G. Knepley }
1802f9d4088aSMatthew G. Knepley 
18034f3b5142SJed Brown /*@C
18044d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
18054d343eeaSMatthew G Knepley 
18064d343eeaSMatthew G Knepley   Not collective
18074d343eeaSMatthew G Knepley 
18084d343eeaSMatthew G Knepley   Input Parameter:
18094d343eeaSMatthew G Knepley . dm - the DM object
18104d343eeaSMatthew G Knepley 
18114d343eeaSMatthew G Knepley   Output Parameters:
18120298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
18130298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
18140298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
18154d343eeaSMatthew G Knepley 
18164d343eeaSMatthew G Knepley   Level: intermediate
18174d343eeaSMatthew G Knepley 
181821c9b008SJed Brown   Notes:
181921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
182021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
182121c9b008SJed Brown   PetscFree().
182221c9b008SJed Brown 
18234d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
18244d343eeaSMatthew G Knepley @*/
182537d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
18264d343eeaSMatthew G Knepley {
182737d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
18284d343eeaSMatthew G Knepley   PetscErrorCode ierr;
18294d343eeaSMatthew G Knepley 
18304d343eeaSMatthew G Knepley   PetscFunctionBegin;
18314d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
183269ca1f37SDmitry Karpeev   if (numFields) {
1833534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
183469ca1f37SDmitry Karpeev     *numFields = 0;
183569ca1f37SDmitry Karpeev   }
183637d0c07bSMatthew G Knepley   if (fieldNames) {
183737d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
18380298fd71SBarry Smith     *fieldNames = NULL;
183969ca1f37SDmitry Karpeev   }
184069ca1f37SDmitry Karpeev   if (fields) {
184169ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
18420298fd71SBarry Smith     *fields = NULL;
184369ca1f37SDmitry Karpeev   }
184492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
184537d0c07bSMatthew G Knepley   if (section) {
18463a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
184737d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
184837d0c07bSMatthew G Knepley 
1849e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
185037d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
18513a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
185237d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
185337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
185437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
18553a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
185637d0c07bSMatthew G Knepley     }
185737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
185837d0c07bSMatthew G Knepley       PetscInt gdof;
185937d0c07bSMatthew G Knepley 
186037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
186137d0c07bSMatthew G Knepley       if (gdof > 0) {
186237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
18633a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
186437d0c07bSMatthew G Knepley 
186537d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
186637d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
18673a544194SStefano Zampini           fpdof = fdof-fcdof;
18683a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
18693a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
18703a544194SStefano Zampini             fieldNc[f] = 1;
18713a544194SStefano Zampini           }
18723a544194SStefano Zampini           fieldSizes[f] += fpdof;
187337d0c07bSMatthew G Knepley         }
187437d0c07bSMatthew G Knepley       }
187537d0c07bSMatthew G Knepley     }
187637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1877785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
187837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
187937d0c07bSMatthew G Knepley     }
188037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
188137d0c07bSMatthew G Knepley       PetscInt gdof, goff;
188237d0c07bSMatthew G Knepley 
188337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
188437d0c07bSMatthew G Knepley       if (gdof > 0) {
188537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
188637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
188737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
188837d0c07bSMatthew G Knepley 
188937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
189037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
189137d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
189237d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
189337d0c07bSMatthew G Knepley           }
189437d0c07bSMatthew G Knepley         }
189537d0c07bSMatthew G Knepley       }
189637d0c07bSMatthew G Knepley     }
18978865f1eaSKarl Rupp     if (numFields) *numFields = nF;
189837d0c07bSMatthew G Knepley     if (fieldNames) {
1899785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
190037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
190137d0c07bSMatthew G Knepley         const char *fieldName;
190237d0c07bSMatthew G Knepley 
190337d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
190437d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
190537d0c07bSMatthew G Knepley       }
190637d0c07bSMatthew G Knepley     }
190737d0c07bSMatthew G Knepley     if (fields) {
1908785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
190937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19103a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19113a544194SStefano Zampini 
191282f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
19133a544194SStefano Zampini         in[0] = -fieldNc[f];
19143a544194SStefano Zampini         in[1] = fieldNc[f];
1915820f2d46SBarry Smith         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
19163a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
19173a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
191837d0c07bSMatthew G Knepley       }
191937d0c07bSMatthew G Knepley     }
19203a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
19218865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
19228865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
192369ca1f37SDmitry Karpeev   }
19244d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
19254d343eeaSMatthew G Knepley }
19264d343eeaSMatthew G Knepley 
192716621825SDmitry Karpeev /*@C
192816621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
192916621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
193016621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1931e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1932e7c4fc90SDmitry Karpeev 
1933e7c4fc90SDmitry Karpeev   Not collective
1934e7c4fc90SDmitry Karpeev 
1935e7c4fc90SDmitry Karpeev   Input Parameter:
1936e7c4fc90SDmitry Karpeev . dm - the DM object
1937e7c4fc90SDmitry Karpeev 
1938e7c4fc90SDmitry Karpeev   Output Parameters:
19390298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
19400298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
19410298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
19420298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1943e7c4fc90SDmitry Karpeev 
1944e7c4fc90SDmitry Karpeev   Level: intermediate
1945e7c4fc90SDmitry Karpeev 
1946e7c4fc90SDmitry Karpeev   Notes:
1947e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1948e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1949e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1950e7c4fc90SDmitry Karpeev 
1951e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1952e7c4fc90SDmitry Karpeev @*/
195316621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1954e7c4fc90SDmitry Karpeev {
1955e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1956e7c4fc90SDmitry Karpeev 
1957e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1958e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19598865f1eaSKarl Rupp   if (len) {
1960534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
19618865f1eaSKarl Rupp     *len = 0;
19628865f1eaSKarl Rupp   }
19638865f1eaSKarl Rupp   if (namelist) {
19648865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1965ea78f98cSLisandro Dalcin     *namelist = NULL;
19668865f1eaSKarl Rupp   }
19678865f1eaSKarl Rupp   if (islist) {
19688865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1969ea78f98cSLisandro Dalcin     *islist = NULL;
19708865f1eaSKarl Rupp   }
19718865f1eaSKarl Rupp   if (dmlist) {
19728865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1973ea78f98cSLisandro Dalcin     *dmlist = NULL;
19748865f1eaSKarl Rupp   }
1975f3f0edfdSDmitry Karpeev   /*
1976f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1977f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1978f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1979f3f0edfdSDmitry Karpeev    */
1980*7a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
198116621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1982435a35e8SMatthew G Knepley     PetscSection section;
1983435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1984435a35e8SMatthew G Knepley 
198592fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1986435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1987435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1988f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
198903dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
199003dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
199103dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1992435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1993435a35e8SMatthew G Knepley         const char *fieldName;
1994435a35e8SMatthew G Knepley 
199503dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
199603dc3394SMatthew G. Knepley         if (namelist) {
1997435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1998435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1999435a35e8SMatthew G Knepley         }
200003dc3394SMatthew G. Knepley       }
2001435a35e8SMatthew G Knepley     } else {
200269ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
2003e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20040298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2005e7c4fc90SDmitry Karpeev     }
20068865f1eaSKarl Rupp   } else {
200716621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
200816621825SDmitry Karpeev   }
200916621825SDmitry Karpeev   PetscFunctionReturn(0);
201016621825SDmitry Karpeev }
201116621825SDmitry Karpeev 
2012564cec59SMatthew G. Knepley /*@
2013435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
2014435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
2015435a35e8SMatthew G Knepley 
2016435a35e8SMatthew G Knepley   Not collective
2017435a35e8SMatthew G Knepley 
2018435a35e8SMatthew G Knepley   Input Parameters:
20192adcc780SMatthew G. Knepley + dm        - The DM object
20202adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
20212adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2022435a35e8SMatthew G Knepley 
2023435a35e8SMatthew G Knepley   Output Parameters:
20242adcc780SMatthew G. Knepley + is - The global indices for the subproblem
20252adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
2026435a35e8SMatthew G Knepley 
20275d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
20285d3b26e6SMatthew G. Knepley 
2029435a35e8SMatthew G Knepley   Level: intermediate
2030435a35e8SMatthew G Knepley 
20315d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2032435a35e8SMatthew G Knepley @*/
203337bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2034435a35e8SMatthew G Knepley {
2035435a35e8SMatthew G Knepley   PetscErrorCode ierr;
2036435a35e8SMatthew G Knepley 
2037435a35e8SMatthew G Knepley   PetscFunctionBegin;
2038435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2039435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
20408865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
20418865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
2042*7a8be351SBarry Smith   PetscCheck(dm->ops->createsubdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
2043435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2044435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
2045435a35e8SMatthew G Knepley }
2046435a35e8SMatthew G Knepley 
20472adcc780SMatthew G. Knepley /*@C
20482adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
20492adcc780SMatthew G. Knepley 
20502adcc780SMatthew G. Knepley   Not collective
20512adcc780SMatthew G. Knepley 
2052d8d19677SJose E. Roman   Input Parameters:
20532adcc780SMatthew G. Knepley + dms - The DM objects
20542adcc780SMatthew G. Knepley - len - The number of DMs
20552adcc780SMatthew G. Knepley 
20562adcc780SMatthew G. Knepley   Output Parameters:
2057a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
20582adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
20592adcc780SMatthew G. Knepley 
20605d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
20615d3b26e6SMatthew G. Knepley 
20622adcc780SMatthew G. Knepley   Level: intermediate
20632adcc780SMatthew G. Knepley 
20645d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
20652adcc780SMatthew G. Knepley @*/
20662adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
20672adcc780SMatthew G. Knepley {
20682adcc780SMatthew G. Knepley   PetscInt       i;
20692adcc780SMatthew G. Knepley   PetscErrorCode ierr;
20702adcc780SMatthew G. Knepley 
20712adcc780SMatthew G. Knepley   PetscFunctionBegin;
20722adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
20732adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
20742adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
2075a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
2076*7a8be351SBarry Smith   PetscCheck(len >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
20772adcc780SMatthew G. Knepley   if (len) {
2078b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2079*7a8be351SBarry Smith     PetscCheck(dm->ops->createsuperdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
2080b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
20812adcc780SMatthew G. Knepley   }
20822adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
20832adcc780SMatthew G. Knepley }
20842adcc780SMatthew G. Knepley 
208516621825SDmitry Karpeev /*@C
20868d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
20878d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
20888d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
20898d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
20908d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
209116621825SDmitry Karpeev 
209216621825SDmitry Karpeev   Not collective
209316621825SDmitry Karpeev 
209416621825SDmitry Karpeev   Input Parameter:
209516621825SDmitry Karpeev . dm - the DM object
209616621825SDmitry Karpeev 
209716621825SDmitry Karpeev   Output Parameters:
20980298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
20990298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
21000298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21010298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
21020298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
210316621825SDmitry Karpeev 
210416621825SDmitry Karpeev   Level: intermediate
210516621825SDmitry Karpeev 
210616621825SDmitry Karpeev   Notes:
210716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
210816621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
210916621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
211016621825SDmitry Karpeev 
2111245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
211216621825SDmitry Karpeev @*/
21138d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
211416621825SDmitry Karpeev {
211516621825SDmitry Karpeev   PetscErrorCode      ierr;
2116be081cd6SPeter Brune   DMSubDomainHookLink link;
2117be081cd6SPeter Brune   PetscInt            i,l;
211816621825SDmitry Karpeev 
211916621825SDmitry Karpeev   PetscFunctionBegin;
212016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
212114a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
21220298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
21230298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
21240298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
21250298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
2126f3f0edfdSDmitry Karpeev   /*
2127f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2128f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2129f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2130f3f0edfdSDmitry Karpeev    */
2131*7a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
213216621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2133be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
213414a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2135f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2136be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2137be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
2138be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
2139be081cd6SPeter Brune         }
2140648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2141e7c4fc90SDmitry Karpeev       }
214214a18fd3SPeter Brune     }
214314a18fd3SPeter Brune     if (len) *len = l;
214414a18fd3SPeter Brune   }
2145e30e807fSPeter Brune   PetscFunctionReturn(0);
2146e30e807fSPeter Brune }
2147e30e807fSPeter Brune 
2148e30e807fSPeter Brune /*@C
2149e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2150e30e807fSPeter Brune 
2151e30e807fSPeter Brune   Not collective
2152e30e807fSPeter Brune 
2153e30e807fSPeter Brune   Input Parameters:
2154e30e807fSPeter Brune + dm - the DM object
2155e30e807fSPeter Brune . n  - the number of subdomain scatters
2156e30e807fSPeter Brune - subdms - the local subdomains
2157e30e807fSPeter Brune 
2158e30e807fSPeter Brune   Output Parameters:
21596b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2160e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2161e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2162e30e807fSPeter Brune 
216395452b02SPatrick Sanan   Notes:
216495452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2165e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2166e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2167e30e807fSPeter Brune   solution and residual data.
2168e30e807fSPeter Brune 
2169e30e807fSPeter Brune   Level: developer
2170e30e807fSPeter Brune 
2171e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2172e30e807fSPeter Brune @*/
2173e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2174e30e807fSPeter Brune {
2175e30e807fSPeter Brune   PetscErrorCode ierr;
2176e30e807fSPeter Brune 
2177e30e807fSPeter Brune   PetscFunctionBegin;
2178e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2179e30e807fSPeter Brune   PetscValidPointer(subdms,3);
2180*7a8be351SBarry Smith   PetscCheck(dm->ops->createddscatters,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
2181e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
2182e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2183e7c4fc90SDmitry Karpeev }
2184e7c4fc90SDmitry Karpeev 
218547c6ae99SBarry Smith /*@
218647c6ae99SBarry Smith   DMRefine - Refines a DM object
218747c6ae99SBarry Smith 
2188d083f849SBarry Smith   Collective on dm
218947c6ae99SBarry Smith 
2190d8d19677SJose E. Roman   Input Parameters:
219147c6ae99SBarry Smith + dm   - the DM object
219291d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
219347c6ae99SBarry Smith 
219447c6ae99SBarry Smith   Output Parameter:
21950298fd71SBarry Smith . dmf - the refined DM, or NULL
2196ae0a1c52SMatthew G Knepley 
2197f27dd7c6SMatthew G. Knepley   Options Database Keys:
2198412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2199412e9a14SMatthew G. Knepley 
22000298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
220147c6ae99SBarry Smith 
220247c6ae99SBarry Smith   Level: developer
220347c6ae99SBarry Smith 
2204e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
220547c6ae99SBarry Smith @*/
22067087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
220747c6ae99SBarry Smith {
220847c6ae99SBarry Smith   PetscErrorCode   ierr;
2209c833c3b5SJed Brown   DMRefineHookLink link;
221047c6ae99SBarry Smith 
221147c6ae99SBarry Smith   PetscFunctionBegin;
2212732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2213*7a8be351SBarry Smith   PetscCheck(dm->ops->refine,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
22141ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
221547c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
22164057135bSMatthew G Knepley   if (*dmf) {
221743842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
22188865f1eaSKarl Rupp 
22198cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
22208865f1eaSKarl Rupp 
2221644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
22220598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2223656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
22248865f1eaSKarl Rupp 
2225e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
2226c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
22278865f1eaSKarl Rupp       if (link->refinehook) {
22288865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
22298865f1eaSKarl Rupp       }
2230c833c3b5SJed Brown     }
2231c833c3b5SJed Brown   }
22321ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
2233c833c3b5SJed Brown   PetscFunctionReturn(0);
2234c833c3b5SJed Brown }
2235c833c3b5SJed Brown 
2236bb9467b5SJed Brown /*@C
2237c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2238c833c3b5SJed Brown 
2239c833c3b5SJed Brown    Logically Collective
2240c833c3b5SJed Brown 
22414165533cSJose E. Roman    Input Parameters:
2242c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2243c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2244c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
22450298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2246c833c3b5SJed Brown 
2247c833c3b5SJed Brown    Calling sequence of refinehook:
2248c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2249c833c3b5SJed Brown 
2250c833c3b5SJed Brown +  coarse - coarse level DM
2251c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2252c833c3b5SJed Brown -  ctx - optional user-defined function context
2253c833c3b5SJed Brown 
2254c833c3b5SJed Brown    Calling sequence for interphook:
2255c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2256c833c3b5SJed Brown 
2257c833c3b5SJed Brown +  coarse - coarse level DM
2258c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2259c833c3b5SJed Brown .  fine - fine level DM to update
2260c833c3b5SJed Brown -  ctx - optional user-defined function context
2261c833c3b5SJed Brown 
2262c833c3b5SJed Brown    Level: advanced
2263c833c3b5SJed Brown 
2264c833c3b5SJed Brown    Notes:
2265c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2266c833c3b5SJed Brown 
2267c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2268c833c3b5SJed Brown 
2269bb9467b5SJed Brown    This function is currently not available from Fortran.
2270bb9467b5SJed Brown 
2271c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2272c833c3b5SJed Brown @*/
2273c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2274c833c3b5SJed Brown {
2275c833c3b5SJed Brown   PetscErrorCode   ierr;
2276c833c3b5SJed Brown   DMRefineHookLink link,*p;
2277c833c3b5SJed Brown 
2278c833c3b5SJed Brown   PetscFunctionBegin;
2279c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22803d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
22813d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
22823d8e3701SJed Brown   }
228395dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2284c833c3b5SJed Brown   link->refinehook = refinehook;
2285c833c3b5SJed Brown   link->interphook = interphook;
2286c833c3b5SJed Brown   link->ctx        = ctx;
22870298fd71SBarry Smith   link->next       = NULL;
2288c833c3b5SJed Brown   *p               = link;
2289c833c3b5SJed Brown   PetscFunctionReturn(0);
2290c833c3b5SJed Brown }
2291c833c3b5SJed Brown 
22923d8e3701SJed Brown /*@C
22933d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
22943d8e3701SJed Brown 
22953d8e3701SJed Brown    Logically Collective
22963d8e3701SJed Brown 
22974165533cSJose E. Roman    Input Parameters:
22983d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
22993d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
23003d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
23013d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23023d8e3701SJed Brown 
23033d8e3701SJed Brown    Level: advanced
23043d8e3701SJed Brown 
23053d8e3701SJed Brown    Notes:
23063d8e3701SJed Brown    This function does nothing if the hook is not in the list.
23073d8e3701SJed Brown 
23083d8e3701SJed Brown    This function is currently not available from Fortran.
23093d8e3701SJed Brown 
23103d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
23113d8e3701SJed Brown @*/
23123d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
23133d8e3701SJed Brown {
23143d8e3701SJed Brown   PetscErrorCode   ierr;
23153d8e3701SJed Brown   DMRefineHookLink link,*p;
23163d8e3701SJed Brown 
23173d8e3701SJed Brown   PetscFunctionBegin;
23183d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
23193d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
23203d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
23213d8e3701SJed Brown       link = *p;
23223d8e3701SJed Brown       *p = link->next;
23233d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
23243d8e3701SJed Brown       break;
23253d8e3701SJed Brown     }
23263d8e3701SJed Brown   }
23273d8e3701SJed Brown   PetscFunctionReturn(0);
23283d8e3701SJed Brown }
23293d8e3701SJed Brown 
2330c833c3b5SJed Brown /*@
2331c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2332c833c3b5SJed Brown 
2333c833c3b5SJed Brown    Collective if any hooks are
2334c833c3b5SJed Brown 
23354165533cSJose E. Roman    Input Parameters:
2336c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2337e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2338c833c3b5SJed Brown -  fine - finer DM to update
2339c833c3b5SJed Brown 
2340c833c3b5SJed Brown    Level: developer
2341c833c3b5SJed Brown 
2342c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2343c833c3b5SJed Brown @*/
2344c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2345c833c3b5SJed Brown {
2346c833c3b5SJed Brown   PetscErrorCode   ierr;
2347c833c3b5SJed Brown   DMRefineHookLink link;
2348c833c3b5SJed Brown 
2349c833c3b5SJed Brown   PetscFunctionBegin;
2350c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
23518865f1eaSKarl Rupp     if (link->interphook) {
23528865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
23538865f1eaSKarl Rupp     }
23544057135bSMatthew G Knepley   }
235547c6ae99SBarry Smith   PetscFunctionReturn(0);
235647c6ae99SBarry Smith }
235747c6ae99SBarry Smith 
2358eb3f98d2SBarry Smith /*@
23591f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
23601f3379b2SToby Isaac 
23611f3379b2SToby Isaac    Collective on DM
23621f3379b2SToby Isaac 
23634165533cSJose E. Roman    Input Parameters:
23641f3379b2SToby Isaac +  coarse - coarse DM
23651f3379b2SToby Isaac .  fine   - fine DM
23661f3379b2SToby Isaac .  interp - (optional) the matrix computed by DMCreateInterpolation().  Implementations may not need this, but if it
23671f3379b2SToby Isaac             is available it can avoid some recomputation.  If it is provided, MatInterpolate() will be used if
23681f3379b2SToby Isaac             the coarse DM does not have a specialized implementation.
23691f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
23701f3379b2SToby Isaac 
23714165533cSJose E. Roman    Output Parameter:
23721f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
23731f3379b2SToby Isaac 
23741f3379b2SToby Isaac    Level: developer
23751f3379b2SToby Isaac 
23761f3379b2SToby Isaac    Note: This function exists because the interpolation of a solution vector between meshes is not always a linear
23771f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
23781f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
23791f3379b2SToby Isaac    slope-limiting reconstruction.
23801f3379b2SToby Isaac 
23811f3379b2SToby Isaac .seealso DMInterpolate(), DMCreateInterpolation()
23821f3379b2SToby Isaac @*/
23831f3379b2SToby Isaac PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
23841f3379b2SToby Isaac {
23851f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM,DM,Mat,Vec,Vec) = NULL;
23861f3379b2SToby Isaac   PetscErrorCode ierr;
23871f3379b2SToby Isaac 
23881f3379b2SToby Isaac   PetscFunctionBegin;
23891f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
23901f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp,MAT_CLASSID,3);
23911f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol,VEC_CLASSID,4);
23921f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol,VEC_CLASSID,5);
23931f3379b2SToby Isaac 
23941f3379b2SToby Isaac   ierr = PetscObjectQueryFunction((PetscObject)coarse,"DMInterpolateSolution_C", &interpsol);CHKERRQ(ierr);
23951f3379b2SToby Isaac   if (interpsol) {
23961f3379b2SToby Isaac     ierr = (*interpsol)(coarse, fine, interp, coarseSol, fineSol);CHKERRQ(ierr);
23971f3379b2SToby Isaac   } else if (interp) {
23981f3379b2SToby Isaac     ierr = MatInterpolate(interp, coarseSol, fineSol);CHKERRQ(ierr);
239998921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24001f3379b2SToby Isaac   PetscFunctionReturn(0);
24011f3379b2SToby Isaac }
24021f3379b2SToby Isaac 
24031f3379b2SToby Isaac /*@
2404aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2405eb3f98d2SBarry Smith 
2406eb3f98d2SBarry Smith     Not Collective
2407eb3f98d2SBarry Smith 
2408eb3f98d2SBarry Smith     Input Parameter:
2409eb3f98d2SBarry Smith .   dm - the DM object
2410eb3f98d2SBarry Smith 
2411eb3f98d2SBarry Smith     Output Parameter:
2412eb3f98d2SBarry Smith .   level - number of refinements
2413eb3f98d2SBarry Smith 
2414eb3f98d2SBarry Smith     Level: developer
2415eb3f98d2SBarry Smith 
24166a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2417eb3f98d2SBarry Smith 
2418eb3f98d2SBarry Smith @*/
2419eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2420eb3f98d2SBarry Smith {
2421eb3f98d2SBarry Smith   PetscFunctionBegin;
2422eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2423eb3f98d2SBarry Smith   *level = dm->levelup;
2424eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2425eb3f98d2SBarry Smith }
2426eb3f98d2SBarry Smith 
2427fef3a512SBarry Smith /*@
2428aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2429fef3a512SBarry Smith 
2430fef3a512SBarry Smith     Not Collective
2431fef3a512SBarry Smith 
2432d8d19677SJose E. Roman     Input Parameters:
2433fef3a512SBarry Smith +   dm - the DM object
2434fef3a512SBarry Smith -   level - number of refinements
2435fef3a512SBarry Smith 
2436fef3a512SBarry Smith     Level: advanced
2437fef3a512SBarry Smith 
243895452b02SPatrick Sanan     Notes:
243995452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2440fef3a512SBarry Smith 
2441fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2442fef3a512SBarry Smith 
2443fef3a512SBarry Smith @*/
2444fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2445fef3a512SBarry Smith {
2446fef3a512SBarry Smith   PetscFunctionBegin;
2447fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2448fef3a512SBarry Smith   dm->levelup = level;
2449fef3a512SBarry Smith   PetscFunctionReturn(0);
2450fef3a512SBarry Smith }
2451fef3a512SBarry Smith 
2452d410b0cfSMatthew G. Knepley /*@
2453d410b0cfSMatthew G. Knepley   DMExtrude - Extrude a DM object from a surface
2454d410b0cfSMatthew G. Knepley 
2455d410b0cfSMatthew G. Knepley   Collective on dm
2456d410b0cfSMatthew G. Knepley 
2457f1a722f8SMatthew G. Knepley   Input Parameters:
2458d410b0cfSMatthew G. Knepley + dm     - the DM object
2459d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2460d410b0cfSMatthew G. Knepley 
2461d410b0cfSMatthew G. Knepley   Output Parameter:
2462d410b0cfSMatthew G. Knepley . dme - the extruded DM, or NULL
2463d410b0cfSMatthew G. Knepley 
2464d410b0cfSMatthew G. Knepley   Note: If no extrusion was done, the return value is NULL
2465d410b0cfSMatthew G. Knepley 
2466d410b0cfSMatthew G. Knepley   Level: developer
2467d410b0cfSMatthew G. Knepley 
2468d410b0cfSMatthew G. Knepley .seealso DMRefine(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector()
2469d410b0cfSMatthew G. Knepley @*/
2470d410b0cfSMatthew G. Knepley PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2471d410b0cfSMatthew G. Knepley {
2472d410b0cfSMatthew G. Knepley   PetscErrorCode ierr;
2473d410b0cfSMatthew G. Knepley 
2474d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2475d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2476*7a8be351SBarry Smith   PetscCheck(dm->ops->extrude,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "DM type %s does not implement DMExtrude", ((PetscObject) dm)->type_name);
2477d410b0cfSMatthew G. Knepley   ierr = (*dm->ops->extrude)(dm, layers, dme);CHKERRQ(ierr);
2478d410b0cfSMatthew G. Knepley   if (*dme) {
2479d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
2480d410b0cfSMatthew G. Knepley     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject) dm, (PetscObject) *dme);CHKERRQ(ierr);
2481d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
2482d410b0cfSMatthew G. Knepley     ierr = DMSetMatType(*dme, dm->mattype);CHKERRQ(ierr);
2483d410b0cfSMatthew G. Knepley   }
2484d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2485d410b0cfSMatthew G. Knepley }
2486d410b0cfSMatthew G. Knepley 
2487ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2488ca3d3a14SMatthew G. Knepley {
2489ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2490ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2491ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2492ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2493ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2494ca3d3a14SMatthew G. Knepley }
2495ca3d3a14SMatthew G. Knepley 
2496ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2497ca3d3a14SMatthew G. Knepley {
2498ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2499ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2500ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2501ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2502ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2503ca3d3a14SMatthew G. Knepley }
2504ca3d3a14SMatthew G. Knepley 
2505ca3d3a14SMatthew G. Knepley /*@
2506c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2507ca3d3a14SMatthew G. Knepley 
2508ca3d3a14SMatthew G. Knepley   Input Parameter:
2509ca3d3a14SMatthew G. Knepley . dm - The DM
2510ca3d3a14SMatthew G. Knepley 
2511ca3d3a14SMatthew G. Knepley   Output Parameter:
2512ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2513ca3d3a14SMatthew G. Knepley 
2514ca3d3a14SMatthew G. Knepley   Level: developer
2515ca3d3a14SMatthew G. Knepley 
2516436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2517ca3d3a14SMatthew G. Knepley @*/
2518ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2519ca3d3a14SMatthew G. Knepley {
2520ca3d3a14SMatthew G. Knepley   Vec            tv;
2521ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2522ca3d3a14SMatthew G. Knepley 
2523ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2524ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2525534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2526ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2527ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2528ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2529ca3d3a14SMatthew G. Knepley }
2530ca3d3a14SMatthew G. Knepley 
2531ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2532ca3d3a14SMatthew G. Knepley {
2533ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2534ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2535ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2536ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2537ca3d3a14SMatthew G. Knepley 
2538ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2539ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
254092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2541ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2542ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2543ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
254492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2545ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2546ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2547ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2548ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2549ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2550ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2551ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2552ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2553ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2554ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2555ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2556ca3d3a14SMatthew G. Knepley     }
2557ca3d3a14SMatthew G. Knepley   }
2558ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2559ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2560ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2561ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2562ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2563ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2564ca3d3a14SMatthew G. Knepley       if (dof) {
2565ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2566ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2567ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2568ca3d3a14SMatthew G. Knepley 
2569ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2570ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2571ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2572580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2573ca3d3a14SMatthew G. Knepley       }
2574ca3d3a14SMatthew G. Knepley     }
2575ca3d3a14SMatthew G. Knepley   }
2576ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2577ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2578ca3d3a14SMatthew G. Knepley }
2579ca3d3a14SMatthew G. Knepley 
2580ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2581ca3d3a14SMatthew G. Knepley {
2582ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2583ca3d3a14SMatthew G. Knepley 
2584ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2585ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2586ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2587ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2588ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2589ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2590ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2591ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2592ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2593ca3d3a14SMatthew G. Knepley }
2594ca3d3a14SMatthew G. Knepley 
2595bb9467b5SJed Brown /*@C
2596baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2597baf369e7SPeter Brune 
2598baf369e7SPeter Brune    Logically Collective
2599baf369e7SPeter Brune 
26004165533cSJose E. Roman    Input Parameters:
2601baf369e7SPeter Brune +  dm - the DM
2602baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2603baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
26040298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2605baf369e7SPeter Brune 
2606baf369e7SPeter Brune    Calling sequence for beginhook:
2607baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2608baf369e7SPeter Brune 
2609baf369e7SPeter Brune +  dm - global DM
2610baf369e7SPeter Brune .  g - global vector
2611baf369e7SPeter Brune .  mode - mode
2612baf369e7SPeter Brune .  l - local vector
2613baf369e7SPeter Brune -  ctx - optional user-defined function context
2614baf369e7SPeter Brune 
2615baf369e7SPeter Brune    Calling sequence for endhook:
2616ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2617baf369e7SPeter Brune 
2618baf369e7SPeter Brune +  global - global DM
2619baf369e7SPeter Brune -  ctx - optional user-defined function context
2620baf369e7SPeter Brune 
2621baf369e7SPeter Brune    Level: advanced
2622baf369e7SPeter Brune 
2623baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2624baf369e7SPeter Brune @*/
2625baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2626baf369e7SPeter Brune {
2627baf369e7SPeter Brune   PetscErrorCode          ierr;
2628baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2629baf369e7SPeter Brune 
2630baf369e7SPeter Brune   PetscFunctionBegin;
2631baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2632baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
263395dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2634baf369e7SPeter Brune   link->beginhook = beginhook;
2635baf369e7SPeter Brune   link->endhook   = endhook;
2636baf369e7SPeter Brune   link->ctx       = ctx;
26370298fd71SBarry Smith   link->next      = NULL;
2638baf369e7SPeter Brune   *p              = link;
2639baf369e7SPeter Brune   PetscFunctionReturn(0);
2640baf369e7SPeter Brune }
2641baf369e7SPeter Brune 
26424c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
26434c274da1SToby Isaac {
26444c274da1SToby Isaac   Mat cMat;
264579769bd5SJed Brown   Vec cVec, cBias;
26464c274da1SToby Isaac   PetscSection section, cSec;
26474c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
26484c274da1SToby Isaac   PetscErrorCode ierr;
26494c274da1SToby Isaac 
26504c274da1SToby Isaac   PetscFunctionBegin;
26514c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
265279769bd5SJed Brown   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat,&cBias);CHKERRQ(ierr);
26534c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
26545db9a05bSToby Isaac     PetscInt nRows;
26555db9a05bSToby Isaac 
26565db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
26575db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
265892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
26597711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
26604c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
266179769bd5SJed Brown     if (cBias) {ierr = VecAXPY(cVec,1.,cBias);CHKERRQ(ierr);}
26624c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
26634c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
26644c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
26654c274da1SToby Isaac       if (dof) {
26664c274da1SToby Isaac         PetscScalar *vals;
26674c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
26684c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
26694c274da1SToby Isaac       }
26704c274da1SToby Isaac     }
26714c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
26724c274da1SToby Isaac   }
26734c274da1SToby Isaac   PetscFunctionReturn(0);
26744c274da1SToby Isaac }
26754c274da1SToby Isaac 
267647c6ae99SBarry Smith /*@
267701729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
267801729b5cSPatrick Sanan 
2679d083f849SBarry Smith     Neighbor-wise Collective on dm
268001729b5cSPatrick Sanan 
268101729b5cSPatrick Sanan     Input Parameters:
268201729b5cSPatrick Sanan +   dm - the DM object
268301729b5cSPatrick Sanan .   g - the global vector
268401729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
268501729b5cSPatrick Sanan -   l - the local vector
268601729b5cSPatrick Sanan 
268701729b5cSPatrick Sanan     Notes:
268801729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
268901729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
269001729b5cSPatrick Sanan 
269101729b5cSPatrick Sanan     Level: beginner
269201729b5cSPatrick Sanan 
269301729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
269401729b5cSPatrick Sanan 
269501729b5cSPatrick Sanan @*/
269601729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
269701729b5cSPatrick Sanan {
269801729b5cSPatrick Sanan   PetscErrorCode ierr;
269901729b5cSPatrick Sanan 
270001729b5cSPatrick Sanan   PetscFunctionBegin;
270101729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
270201729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
270301729b5cSPatrick Sanan   PetscFunctionReturn(0);
270401729b5cSPatrick Sanan }
270501729b5cSPatrick Sanan 
270601729b5cSPatrick Sanan /*@
270747c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
270847c6ae99SBarry Smith 
2709d083f849SBarry Smith     Neighbor-wise Collective on dm
271047c6ae99SBarry Smith 
271147c6ae99SBarry Smith     Input Parameters:
271247c6ae99SBarry Smith +   dm - the DM object
271347c6ae99SBarry Smith .   g - the global vector
271447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
271547c6ae99SBarry Smith -   l - the local vector
271647c6ae99SBarry Smith 
271701729b5cSPatrick Sanan     Level: intermediate
271847c6ae99SBarry Smith 
271901729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
272047c6ae99SBarry Smith 
272147c6ae99SBarry Smith @*/
27227087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
272347c6ae99SBarry Smith {
27247128ae9fSMatthew G Knepley   PetscSF                 sf;
272547c6ae99SBarry Smith   PetscErrorCode          ierr;
2726baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
272747c6ae99SBarry Smith 
272847c6ae99SBarry Smith   PetscFunctionBegin;
2729171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2730baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
27318865f1eaSKarl Rupp     if (link->beginhook) {
27328865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
27338865f1eaSKarl Rupp     }
2734baf369e7SPeter Brune   }
27351bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
27367128ae9fSMatthew G Knepley   if (sf) {
2737ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2738ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2739d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
27407128ae9fSMatthew G Knepley 
2741*7a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
2742a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2743a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2744ad227feaSJunchao Zhang     ierr = PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE);CHKERRQ(ierr);
2745a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2746a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
27477128ae9fSMatthew G Knepley   } else {
2748*7a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2749843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
27507128ae9fSMatthew G Knepley   }
275147c6ae99SBarry Smith   PetscFunctionReturn(0);
275247c6ae99SBarry Smith }
275347c6ae99SBarry Smith 
275447c6ae99SBarry Smith /*@
275547c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
275647c6ae99SBarry Smith 
2757d083f849SBarry Smith     Neighbor-wise Collective on dm
275847c6ae99SBarry Smith 
275947c6ae99SBarry Smith     Input Parameters:
276047c6ae99SBarry Smith +   dm - the DM object
276147c6ae99SBarry Smith .   g - the global vector
276247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
276347c6ae99SBarry Smith -   l - the local vector
276447c6ae99SBarry Smith 
276501729b5cSPatrick Sanan     Level: intermediate
276647c6ae99SBarry Smith 
276701729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
276847c6ae99SBarry Smith 
276947c6ae99SBarry Smith @*/
27707087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
277147c6ae99SBarry Smith {
27727128ae9fSMatthew G Knepley   PetscSF                 sf;
277347c6ae99SBarry Smith   PetscErrorCode          ierr;
2774ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2775ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2776ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2777baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2778d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
277947c6ae99SBarry Smith 
278047c6ae99SBarry Smith   PetscFunctionBegin;
2781171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27821bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2783ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
27847128ae9fSMatthew G Knepley   if (sf) {
2785*7a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
27867128ae9fSMatthew G Knepley 
2787a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2788a256111fSJunchao Zhang     ierr = VecGetArrayReadAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2789ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray,MPI_REPLACE);CHKERRQ(ierr);
2790a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(l, &lArray);CHKERRQ(ierr);
2791a256111fSJunchao Zhang     ierr = VecRestoreArrayReadAndMemType(g, &gArray);CHKERRQ(ierr);
2792ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
27937128ae9fSMatthew G Knepley   } else {
2794*7a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2795843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
27967128ae9fSMatthew G Knepley   }
27974c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2798baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2799baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2800baf369e7SPeter Brune   }
280147c6ae99SBarry Smith   PetscFunctionReturn(0);
280247c6ae99SBarry Smith }
280347c6ae99SBarry Smith 
2804d4d07f1eSToby Isaac /*@C
2805d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2806d4d07f1eSToby Isaac 
2807d4d07f1eSToby Isaac    Logically Collective
2808d4d07f1eSToby Isaac 
28094165533cSJose E. Roman    Input Parameters:
2810d4d07f1eSToby Isaac +  dm - the DM
2811d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2812d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2813d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2814d4d07f1eSToby Isaac 
2815d4d07f1eSToby Isaac    Calling sequence for beginhook:
2816d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2817d4d07f1eSToby Isaac 
2818d4d07f1eSToby Isaac +  dm - global DM
2819d4d07f1eSToby Isaac .  l - local vector
2820d4d07f1eSToby Isaac .  mode - mode
2821d4d07f1eSToby Isaac .  g - global vector
2822d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2823d4d07f1eSToby Isaac 
2824d4d07f1eSToby Isaac    Calling sequence for endhook:
2825d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2826d4d07f1eSToby Isaac 
2827d4d07f1eSToby Isaac +  global - global DM
2828d4d07f1eSToby Isaac .  l - local vector
2829d4d07f1eSToby Isaac .  mode - mode
2830d4d07f1eSToby Isaac .  g - global vector
2831d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2832d4d07f1eSToby Isaac 
2833d4d07f1eSToby Isaac    Level: advanced
2834d4d07f1eSToby Isaac 
2835d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2836d4d07f1eSToby Isaac @*/
2837d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2838d4d07f1eSToby Isaac {
2839d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2840d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2841d4d07f1eSToby Isaac 
2842d4d07f1eSToby Isaac   PetscFunctionBegin;
2843d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2844d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
284595dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2846d4d07f1eSToby Isaac   link->beginhook = beginhook;
2847d4d07f1eSToby Isaac   link->endhook   = endhook;
2848d4d07f1eSToby Isaac   link->ctx       = ctx;
2849d4d07f1eSToby Isaac   link->next      = NULL;
2850d4d07f1eSToby Isaac   *p              = link;
2851d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2852d4d07f1eSToby Isaac }
2853d4d07f1eSToby Isaac 
28544c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
28554c274da1SToby Isaac {
28564c274da1SToby Isaac   Mat cMat;
28574c274da1SToby Isaac   Vec cVec;
28584c274da1SToby Isaac   PetscSection section, cSec;
28594c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
28604c274da1SToby Isaac   PetscErrorCode ierr;
28614c274da1SToby Isaac 
28624c274da1SToby Isaac   PetscFunctionBegin;
28634c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
286479769bd5SJed Brown   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat,NULL);CHKERRQ(ierr);
28654c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
28665db9a05bSToby Isaac     PetscInt nRows;
28675db9a05bSToby Isaac 
28685db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
28695db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
287092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
28717711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
28724c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
28734c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
28744c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
28754c274da1SToby Isaac       if (dof) {
28764c274da1SToby Isaac         PetscInt d;
28774c274da1SToby Isaac         PetscScalar *vals;
28784c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
28794c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
28804c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
28814c274da1SToby Isaac          * we just extracted */
28824c274da1SToby Isaac         for (d = 0; d < dof; d++) {
28834c274da1SToby Isaac           vals[d] = 0.;
28844c274da1SToby Isaac         }
28854c274da1SToby Isaac       }
28864c274da1SToby Isaac     }
28874c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
28884c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
28894c274da1SToby Isaac   }
28904c274da1SToby Isaac   PetscFunctionReturn(0);
28914c274da1SToby Isaac }
289201729b5cSPatrick Sanan /*@
289301729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
289401729b5cSPatrick Sanan 
2895d083f849SBarry Smith     Neighbor-wise Collective on dm
289601729b5cSPatrick Sanan 
289701729b5cSPatrick Sanan     Input Parameters:
289801729b5cSPatrick Sanan +   dm - the DM object
289901729b5cSPatrick Sanan .   l - the local vector
290001729b5cSPatrick 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.
290101729b5cSPatrick Sanan -   g - the global vector
290201729b5cSPatrick Sanan 
290301729b5cSPatrick Sanan     Notes:
290401729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
290501729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
290601729b5cSPatrick Sanan 
290701729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
290801729b5cSPatrick 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.
290901729b5cSPatrick Sanan 
291001729b5cSPatrick Sanan     Level: beginner
291101729b5cSPatrick Sanan 
291201729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
291301729b5cSPatrick Sanan 
291401729b5cSPatrick Sanan @*/
291501729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
291601729b5cSPatrick Sanan {
291701729b5cSPatrick Sanan   PetscErrorCode ierr;
291801729b5cSPatrick Sanan 
291901729b5cSPatrick Sanan   PetscFunctionBegin;
292001729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
292101729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
292201729b5cSPatrick Sanan   PetscFunctionReturn(0);
292301729b5cSPatrick Sanan }
29244c274da1SToby Isaac 
292547c6ae99SBarry Smith /*@
292601729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
29279a42bb27SBarry Smith 
2928d083f849SBarry Smith     Neighbor-wise Collective on dm
29299a42bb27SBarry Smith 
29309a42bb27SBarry Smith     Input Parameters:
29319a42bb27SBarry Smith +   dm - the DM object
2932f6813fd5SJed Brown .   l - the local vector
29331eb28f2eSBarry 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.
29341eb28f2eSBarry Smith -   g - the global vector
29359a42bb27SBarry Smith 
293695452b02SPatrick Sanan     Notes:
293795452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
293884330215SMatthew 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.
29399a42bb27SBarry Smith 
294001729b5cSPatrick Sanan     Level: intermediate
29419a42bb27SBarry Smith 
294201729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
29439a42bb27SBarry Smith 
29449a42bb27SBarry Smith @*/
29457087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
29469a42bb27SBarry Smith {
29477128ae9fSMatthew G Knepley   PetscSF                 sf;
294884330215SMatthew G. Knepley   PetscSection            s, gs;
2949d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2950ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2951ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2952ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2953fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
295484330215SMatthew G. Knepley   PetscErrorCode          ierr;
2955d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
29569a42bb27SBarry Smith 
29579a42bb27SBarry Smith   PetscFunctionBegin;
2958171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2959d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2960d4d07f1eSToby Isaac     if (link->beginhook) {
2961d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2962d4d07f1eSToby Isaac     }
2963d4d07f1eSToby Isaac   }
29644c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
29651bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
296692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
29677128ae9fSMatthew G Knepley   switch (mode) {
29687128ae9fSMatthew G Knepley   case INSERT_VALUES:
29697128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2970304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
297184330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
29727128ae9fSMatthew G Knepley   case ADD_VALUES:
29737128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2974304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
297584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
29767128ae9fSMatthew G Knepley   default:
297798921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
29787128ae9fSMatthew G Knepley   }
2979ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2980ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2981ca3d3a14SMatthew G. Knepley     if (transform) {
2982ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2983ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2984ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2985ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2986fa88e482SJed Brown     } else if (isInsert) {
2987ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2988fa88e482SJed Brown     } else {
2989a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, &lmtype);CHKERRQ(ierr);
2990fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2991ca3d3a14SMatthew G. Knepley     }
2992fa88e482SJed Brown     if (s && isInsert) {
29937128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2994fa88e482SJed Brown     } else {
2995a256111fSJunchao Zhang       ierr = VecGetArrayAndMemType(g, &gArray, &gmtype);CHKERRQ(ierr);
2996fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2997fa88e482SJed Brown     }
2998ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2999d0295fc0SJunchao Zhang       ierr = PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM);CHKERRQ(ierr);
300084330215SMatthew G. Knepley     } else if (s && isInsert) {
300184330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
300284330215SMatthew G. Knepley 
3003e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
300484330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
300584330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
300684330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3007b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
300884330215SMatthew G. Knepley 
300984330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
301003442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
301184330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
3012b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
301384330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
301484330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
3015b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
301603442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
3017*7a8be351SBarry Smith         PetscCheck(dof == gdof,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %" PetscInt_FMT " dof: %" PetscInt_FMT " gdof: %" PetscInt_FMT " cdof: %" PetscInt_FMT " gcdof: %" PetscInt_FMT, p, dof, gdof, cdof, gcdof);
3018b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3019b3b16f48SMatthew G. Knepley         if (!gcdof) {
302084330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
3021b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3022b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
302384330215SMatthew G. Knepley           const PetscInt *cdofs;
302484330215SMatthew G. Knepley           PetscInt        cind = 0;
302584330215SMatthew G. Knepley 
302684330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
3027b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
302884330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
3029b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
303084330215SMatthew G. Knepley           }
3031*7a8be351SBarry Smith         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %" PetscInt_FMT " dof: %" PetscInt_FMT " gdof: %" PetscInt_FMT " cdof: %" PetscInt_FMT " gcdof: %" PetscInt_FMT, p, dof, gdof, cdof, gcdof);
303284330215SMatthew G. Knepley       }
3033ca3d3a14SMatthew G. Knepley     }
3034fa88e482SJed Brown     if (g_inplace) {
3035a256111fSJunchao Zhang       ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
3036fa88e482SJed Brown     } else {
30377128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
3038fa88e482SJed Brown     }
3039ca3d3a14SMatthew G. Knepley     if (transform) {
3040ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
3041ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
3042fa88e482SJed Brown     } else if (l_inplace) {
3043a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
3044ca3d3a14SMatthew G. Knepley     } else {
3045ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
3046ca3d3a14SMatthew G. Knepley     }
30477128ae9fSMatthew G Knepley   } else {
3048*7a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
3049843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
30507128ae9fSMatthew G Knepley   }
30519a42bb27SBarry Smith   PetscFunctionReturn(0);
30529a42bb27SBarry Smith }
30539a42bb27SBarry Smith 
30549a42bb27SBarry Smith /*@
30559a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
305647c6ae99SBarry Smith 
3057d083f849SBarry Smith     Neighbor-wise Collective on dm
305847c6ae99SBarry Smith 
305947c6ae99SBarry Smith     Input Parameters:
306047c6ae99SBarry Smith +   dm - the DM object
3061f6813fd5SJed Brown .   l - the local vector
306247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
3063f6813fd5SJed Brown -   g - the global vector
306447c6ae99SBarry Smith 
306501729b5cSPatrick Sanan     Level: intermediate
306647c6ae99SBarry Smith 
3067e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
306847c6ae99SBarry Smith 
306947c6ae99SBarry Smith @*/
30707087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
307147c6ae99SBarry Smith {
30727128ae9fSMatthew G Knepley   PetscSF                 sf;
307384330215SMatthew G. Knepley   PetscSection            s;
3074d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3075ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
307684330215SMatthew G. Knepley   PetscErrorCode          ierr;
307747c6ae99SBarry Smith 
307847c6ae99SBarry Smith   PetscFunctionBegin;
3079171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30801bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
308192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
30827128ae9fSMatthew G Knepley   switch (mode) {
30837128ae9fSMatthew G Knepley   case INSERT_VALUES:
30847128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
308584330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
30867128ae9fSMatthew G Knepley   case ADD_VALUES:
30877128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
308884330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
30897128ae9fSMatthew G Knepley   default:
309098921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
30917128ae9fSMatthew G Knepley   }
309284330215SMatthew G. Knepley   if (sf && !isInsert) {
3093ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3094ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3095ca3d3a14SMatthew G. Knepley     Vec                tmpl;
309684330215SMatthew G. Knepley 
3097ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
3098ca3d3a14SMatthew G. Knepley     if (transform) {
3099ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
3100ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
3101ca3d3a14SMatthew G. Knepley     } else {
3102a256111fSJunchao Zhang       ierr = VecGetArrayReadAndMemType(l, &lArray, NULL);CHKERRQ(ierr);
3103ca3d3a14SMatthew G. Knepley     }
3104a256111fSJunchao Zhang     ierr = VecGetArrayAndMemType(g, &gArray, NULL);CHKERRQ(ierr);
3105a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
3106ca3d3a14SMatthew G. Knepley     if (transform) {
3107ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
3108ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
3109ca3d3a14SMatthew G. Knepley     } else {
3110a256111fSJunchao Zhang       ierr = VecRestoreArrayReadAndMemType(l, &lArray);CHKERRQ(ierr);
3111ca3d3a14SMatthew G. Knepley     }
3112a256111fSJunchao Zhang     ierr = VecRestoreArrayAndMemType(g, &gArray);CHKERRQ(ierr);
311384330215SMatthew G. Knepley   } else if (s && isInsert) {
31147128ae9fSMatthew G Knepley   } else {
3115*7a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
3116843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
31177128ae9fSMatthew G Knepley   }
3118d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
3119d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
3120d4d07f1eSToby Isaac   }
312147c6ae99SBarry Smith   PetscFunctionReturn(0);
312247c6ae99SBarry Smith }
312347c6ae99SBarry Smith 
3124f089877aSRichard Tran Mills /*@
3125bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
3126bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3127d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
3128f089877aSRichard Tran Mills 
3129d083f849SBarry Smith    Neighbor-wise Collective on dm
3130f089877aSRichard Tran Mills 
3131f089877aSRichard Tran Mills    Input Parameters:
3132f089877aSRichard Tran Mills +  dm - the DM object
3133bc0a1609SRichard Tran Mills .  g - the original local vector
3134bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3135f089877aSRichard Tran Mills 
3136bc0a1609SRichard Tran Mills    Output Parameter:
3137bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3138f089877aSRichard Tran Mills 
3139f089877aSRichard Tran Mills    Level: intermediate
3140f089877aSRichard Tran Mills 
3141bc0a1609SRichard Tran Mills    Notes:
3142bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3143bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3144bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3145bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3146bc0a1609SRichard Tran Mills 
3147bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3148f089877aSRichard Tran Mills 
3149f089877aSRichard Tran Mills @*/
3150f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
3151f089877aSRichard Tran Mills {
3152f089877aSRichard Tran Mills   PetscErrorCode          ierr;
3153f089877aSRichard Tran Mills 
3154f089877aSRichard Tran Mills   PetscFunctionBegin;
3155f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3156*7a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalbegin,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3157f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
3158f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3159f089877aSRichard Tran Mills }
3160f089877aSRichard Tran Mills 
3161f089877aSRichard Tran Mills /*@
3162bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
3163bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3164d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
3165f089877aSRichard Tran Mills 
3166d083f849SBarry Smith    Neighbor-wise Collective on dm
3167f089877aSRichard Tran Mills 
3168f089877aSRichard Tran Mills    Input Parameters:
3169bc0a1609SRichard Tran Mills +  da - the DM object
3170bc0a1609SRichard Tran Mills .  g - the original local vector
3171bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3172f089877aSRichard Tran Mills 
3173bc0a1609SRichard Tran Mills    Output Parameter:
3174bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3175f089877aSRichard Tran Mills 
3176f089877aSRichard Tran Mills    Level: intermediate
3177f089877aSRichard Tran Mills 
3178bc0a1609SRichard Tran Mills    Notes:
3179bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3180bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3181bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3182bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3183bc0a1609SRichard Tran Mills 
3184bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3185f089877aSRichard Tran Mills 
3186f089877aSRichard Tran Mills @*/
3187f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
3188f089877aSRichard Tran Mills {
3189f089877aSRichard Tran Mills   PetscErrorCode          ierr;
3190f089877aSRichard Tran Mills 
3191f089877aSRichard Tran Mills   PetscFunctionBegin;
3192f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3193*7a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalend,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3194f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
3195f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3196f089877aSRichard Tran Mills }
3197f089877aSRichard Tran Mills 
319847c6ae99SBarry Smith /*@
319947c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
320047c6ae99SBarry Smith 
3201d083f849SBarry Smith     Collective on dm
320247c6ae99SBarry Smith 
3203d8d19677SJose E. Roman     Input Parameters:
320447c6ae99SBarry Smith +   dm - the DM object
320591d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
320647c6ae99SBarry Smith 
320747c6ae99SBarry Smith     Output Parameter:
320847c6ae99SBarry Smith .   dmc - the coarsened DM
320947c6ae99SBarry Smith 
321047c6ae99SBarry Smith     Level: developer
321147c6ae99SBarry Smith 
3212e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
321347c6ae99SBarry Smith 
321447c6ae99SBarry Smith @*/
32157087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
321647c6ae99SBarry Smith {
321747c6ae99SBarry Smith   PetscErrorCode    ierr;
3218b17ce1afSJed Brown   DMCoarsenHookLink link;
321947c6ae99SBarry Smith 
322047c6ae99SBarry Smith   PetscFunctionBegin;
3221171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3222*7a8be351SBarry Smith   PetscCheck(dm->ops->coarsen,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
322347a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
322447c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
3225b9d85ea2SLisandro Dalcin   if (*dmc) {
3226a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
3227a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
322843842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
32298cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
3230644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
32310598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3232656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
3233e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
3234b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
3235b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
3236b17ce1afSJed Brown     }
3237b9d85ea2SLisandro Dalcin   }
323847a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
3239*7a8be351SBarry Smith   PetscCheck(*dmc,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3240b17ce1afSJed Brown   PetscFunctionReturn(0);
3241b17ce1afSJed Brown }
3242b17ce1afSJed Brown 
3243bb9467b5SJed Brown /*@C
3244b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3245b17ce1afSJed Brown 
3246b17ce1afSJed Brown    Logically Collective
3247b17ce1afSJed Brown 
32484165533cSJose E. Roman    Input Parameters:
3249b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3250b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3251b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
32520298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3253b17ce1afSJed Brown 
3254b17ce1afSJed Brown    Calling sequence of coarsenhook:
3255b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3256b17ce1afSJed Brown 
3257b17ce1afSJed Brown +  fine - fine level DM
3258b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3259b17ce1afSJed Brown -  ctx - optional user-defined function context
3260b17ce1afSJed Brown 
3261b17ce1afSJed Brown    Calling sequence for restricthook:
3262c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3263b17ce1afSJed Brown 
3264b17ce1afSJed Brown +  fine - fine level DM
3265b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3266c833c3b5SJed Brown .  rscale - scaling vector for restriction
3267c833c3b5SJed Brown .  inject - matrix restricting by injection
3268b17ce1afSJed Brown .  coarse - coarse level DM to update
3269b17ce1afSJed Brown -  ctx - optional user-defined function context
3270b17ce1afSJed Brown 
3271b17ce1afSJed Brown    Level: advanced
3272b17ce1afSJed Brown 
3273b17ce1afSJed Brown    Notes:
3274b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3275b17ce1afSJed Brown 
3276b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3277b17ce1afSJed Brown 
3278b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3279b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3280b17ce1afSJed Brown 
3281bb9467b5SJed Brown    This function is currently not available from Fortran.
3282bb9467b5SJed Brown 
3283dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3284b17ce1afSJed Brown @*/
3285b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3286b17ce1afSJed Brown {
3287b17ce1afSJed Brown   PetscErrorCode    ierr;
3288b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3289b17ce1afSJed Brown 
3290b17ce1afSJed Brown   PetscFunctionBegin;
3291b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
32921e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
32931e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
32941e3d8eccSJed Brown   }
329595dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
3296b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3297b17ce1afSJed Brown   link->restricthook = restricthook;
3298b17ce1afSJed Brown   link->ctx          = ctx;
32990298fd71SBarry Smith   link->next         = NULL;
3300b17ce1afSJed Brown   *p                 = link;
3301b17ce1afSJed Brown   PetscFunctionReturn(0);
3302b17ce1afSJed Brown }
3303b17ce1afSJed Brown 
3304dc822a44SJed Brown /*@C
3305dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3306dc822a44SJed Brown 
3307dc822a44SJed Brown    Logically Collective
3308dc822a44SJed Brown 
33094165533cSJose E. Roman    Input Parameters:
3310dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3311dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3312dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3313dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3314dc822a44SJed Brown 
3315dc822a44SJed Brown    Level: advanced
3316dc822a44SJed Brown 
3317dc822a44SJed Brown    Notes:
3318dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3319dc822a44SJed Brown 
3320dc822a44SJed Brown    This function is currently not available from Fortran.
3321dc822a44SJed Brown 
3322dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3323dc822a44SJed Brown @*/
3324dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3325dc822a44SJed Brown {
3326dc822a44SJed Brown   PetscErrorCode    ierr;
3327dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3328dc822a44SJed Brown 
3329dc822a44SJed Brown   PetscFunctionBegin;
3330dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3331dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3332dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3333dc822a44SJed Brown       link = *p;
3334dc822a44SJed Brown       *p = link->next;
3335dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3336dc822a44SJed Brown       break;
3337dc822a44SJed Brown     }
3338dc822a44SJed Brown   }
3339dc822a44SJed Brown   PetscFunctionReturn(0);
3340dc822a44SJed Brown }
3341dc822a44SJed Brown 
3342b17ce1afSJed Brown /*@
3343b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3344b17ce1afSJed Brown 
3345b17ce1afSJed Brown    Collective if any hooks are
3346b17ce1afSJed Brown 
33474165533cSJose E. Roman    Input Parameters:
3348b17ce1afSJed Brown +  fine - finer DM to use as a base
3349b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3350e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3351b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3352e91eccc2SStefano Zampini -  coarse - coarser DM to update
3353b17ce1afSJed Brown 
3354b17ce1afSJed Brown    Level: developer
3355b17ce1afSJed Brown 
3356b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3357b17ce1afSJed Brown @*/
3358b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3359b17ce1afSJed Brown {
3360b17ce1afSJed Brown   PetscErrorCode    ierr;
3361b17ce1afSJed Brown   DMCoarsenHookLink link;
3362b17ce1afSJed Brown 
3363b17ce1afSJed Brown   PetscFunctionBegin;
3364b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
33658865f1eaSKarl Rupp     if (link->restricthook) {
33668865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
33678865f1eaSKarl Rupp     }
3368b17ce1afSJed Brown   }
336947c6ae99SBarry Smith   PetscFunctionReturn(0);
337047c6ae99SBarry Smith }
337147c6ae99SBarry Smith 
3372bb9467b5SJed Brown /*@C
3373be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
33745dbd56e3SPeter Brune 
3375d083f849SBarry Smith    Logically Collective on global
33765dbd56e3SPeter Brune 
33774165533cSJose E. Roman    Input Parameters:
33785dbd56e3SPeter Brune +  global - global DM
3379ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
33805dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
33810298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
33825dbd56e3SPeter Brune 
3383ec4806b8SPeter Brune    Calling sequence for ddhook:
3384ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3385ec4806b8SPeter Brune 
3386ec4806b8SPeter Brune +  global - global DM
3387ec4806b8SPeter Brune .  block  - block DM
3388ec4806b8SPeter Brune -  ctx - optional user-defined function context
3389ec4806b8SPeter Brune 
33905dbd56e3SPeter Brune    Calling sequence for restricthook:
3391ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
33925dbd56e3SPeter Brune 
33935dbd56e3SPeter Brune +  global - global DM
33945dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
33955dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3396ec4806b8SPeter Brune .  block  - block DM
33975dbd56e3SPeter Brune -  ctx - optional user-defined function context
33985dbd56e3SPeter Brune 
33995dbd56e3SPeter Brune    Level: advanced
34005dbd56e3SPeter Brune 
34015dbd56e3SPeter Brune    Notes:
3402ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
34035dbd56e3SPeter Brune 
34045dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
34055dbd56e3SPeter Brune 
34065dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3407ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
34085dbd56e3SPeter Brune 
3409bb9467b5SJed Brown    This function is currently not available from Fortran.
3410bb9467b5SJed Brown 
34115dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
34125dbd56e3SPeter Brune @*/
3413be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
34145dbd56e3SPeter Brune {
34155dbd56e3SPeter Brune   PetscErrorCode      ierr;
3416be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
34175dbd56e3SPeter Brune 
34185dbd56e3SPeter Brune   PetscFunctionBegin;
34195dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3420b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3421b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3422b3a6b972SJed Brown   }
342395dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
34245dbd56e3SPeter Brune   link->restricthook = restricthook;
3425be081cd6SPeter Brune   link->ddhook       = ddhook;
34265dbd56e3SPeter Brune   link->ctx          = ctx;
34270298fd71SBarry Smith   link->next         = NULL;
34285dbd56e3SPeter Brune   *p                 = link;
34295dbd56e3SPeter Brune   PetscFunctionReturn(0);
34305dbd56e3SPeter Brune }
34315dbd56e3SPeter Brune 
3432b3a6b972SJed Brown /*@C
3433b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3434b3a6b972SJed Brown 
3435b3a6b972SJed Brown    Logically Collective
3436b3a6b972SJed Brown 
34374165533cSJose E. Roman    Input Parameters:
3438b3a6b972SJed Brown +  global - global DM
3439b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3440b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3441b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3442b3a6b972SJed Brown 
3443b3a6b972SJed Brown    Level: advanced
3444b3a6b972SJed Brown 
3445b3a6b972SJed Brown    Notes:
3446b3a6b972SJed Brown 
3447b3a6b972SJed Brown    This function is currently not available from Fortran.
3448b3a6b972SJed Brown 
3449b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3450b3a6b972SJed Brown @*/
3451b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3452b3a6b972SJed Brown {
3453b3a6b972SJed Brown   PetscErrorCode      ierr;
3454b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3455b3a6b972SJed Brown 
3456b3a6b972SJed Brown   PetscFunctionBegin;
3457b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3458b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3459b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3460b3a6b972SJed Brown       link = *p;
3461b3a6b972SJed Brown       *p = link->next;
3462b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3463b3a6b972SJed Brown       break;
3464b3a6b972SJed Brown     }
3465b3a6b972SJed Brown   }
3466b3a6b972SJed Brown   PetscFunctionReturn(0);
3467b3a6b972SJed Brown }
3468b3a6b972SJed Brown 
34695dbd56e3SPeter Brune /*@
3470be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
34715dbd56e3SPeter Brune 
34725dbd56e3SPeter Brune    Collective if any hooks are
34735dbd56e3SPeter Brune 
34744165533cSJose E. Roman    Input Parameters:
34755dbd56e3SPeter Brune +  fine - finer DM to use as a base
3476be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3477be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
34785dbd56e3SPeter Brune -  coarse - coarer DM to update
34795dbd56e3SPeter Brune 
34805dbd56e3SPeter Brune    Level: developer
34815dbd56e3SPeter Brune 
34825dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
34835dbd56e3SPeter Brune @*/
3484be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
34855dbd56e3SPeter Brune {
34865dbd56e3SPeter Brune   PetscErrorCode      ierr;
3487be081cd6SPeter Brune   DMSubDomainHookLink link;
34885dbd56e3SPeter Brune 
34895dbd56e3SPeter Brune   PetscFunctionBegin;
3490be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
34918865f1eaSKarl Rupp     if (link->restricthook) {
34928865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
34938865f1eaSKarl Rupp     }
34945dbd56e3SPeter Brune   }
34955dbd56e3SPeter Brune   PetscFunctionReturn(0);
34965dbd56e3SPeter Brune }
34975dbd56e3SPeter Brune 
34985fe1f584SPeter Brune /*@
34996a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
35005fe1f584SPeter Brune 
35015fe1f584SPeter Brune     Not Collective
35025fe1f584SPeter Brune 
35035fe1f584SPeter Brune     Input Parameter:
35045fe1f584SPeter Brune .   dm - the DM object
35055fe1f584SPeter Brune 
35065fe1f584SPeter Brune     Output Parameter:
35076a7d9d85SPeter Brune .   level - number of coarsenings
35085fe1f584SPeter Brune 
35095fe1f584SPeter Brune     Level: developer
35105fe1f584SPeter Brune 
35116a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
35125fe1f584SPeter Brune 
35135fe1f584SPeter Brune @*/
35145fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
35155fe1f584SPeter Brune {
35165fe1f584SPeter Brune   PetscFunctionBegin;
35175fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3518b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
35195fe1f584SPeter Brune   *level = dm->leveldown;
35205fe1f584SPeter Brune   PetscFunctionReturn(0);
35215fe1f584SPeter Brune }
35225fe1f584SPeter Brune 
35239a64c4a8SMatthew G. Knepley /*@
35249a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
35259a64c4a8SMatthew G. Knepley 
35269a64c4a8SMatthew G. Knepley     Not Collective
35279a64c4a8SMatthew G. Knepley 
35289a64c4a8SMatthew G. Knepley     Input Parameters:
35299a64c4a8SMatthew G. Knepley +   dm - the DM object
35309a64c4a8SMatthew G. Knepley -   level - number of coarsenings
35319a64c4a8SMatthew G. Knepley 
35329a64c4a8SMatthew G. Knepley     Level: developer
35339a64c4a8SMatthew G. Knepley 
35349a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
35359a64c4a8SMatthew G. Knepley @*/
35369a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
35379a64c4a8SMatthew G. Knepley {
35389a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
35399a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35409a64c4a8SMatthew G. Knepley   dm->leveldown = level;
35419a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
35429a64c4a8SMatthew G. Knepley }
35439a64c4a8SMatthew G. Knepley 
354447c6ae99SBarry Smith /*@C
354547c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
354647c6ae99SBarry Smith 
3547d083f849SBarry Smith     Collective on dm
354847c6ae99SBarry Smith 
3549d8d19677SJose E. Roman     Input Parameters:
355047c6ae99SBarry Smith +   dm - the DM object
355147c6ae99SBarry Smith -   nlevels - the number of levels of refinement
355247c6ae99SBarry Smith 
355347c6ae99SBarry Smith     Output Parameter:
355447c6ae99SBarry Smith .   dmf - the refined DM hierarchy
355547c6ae99SBarry Smith 
355647c6ae99SBarry Smith     Level: developer
355747c6ae99SBarry Smith 
3558e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
355947c6ae99SBarry Smith 
356047c6ae99SBarry Smith @*/
35617087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
356247c6ae99SBarry Smith {
356347c6ae99SBarry Smith   PetscErrorCode ierr;
356447c6ae99SBarry Smith 
356547c6ae99SBarry Smith   PetscFunctionBegin;
3566171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3567*7a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
356847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3569b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
357047c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
357147c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
357247c6ae99SBarry Smith   } else if (dm->ops->refine) {
357347c6ae99SBarry Smith     PetscInt i;
357447c6ae99SBarry Smith 
3575ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
357647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3577ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
357847c6ae99SBarry Smith     }
3579ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
358047c6ae99SBarry Smith   PetscFunctionReturn(0);
358147c6ae99SBarry Smith }
358247c6ae99SBarry Smith 
358347c6ae99SBarry Smith /*@C
358447c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
358547c6ae99SBarry Smith 
3586d083f849SBarry Smith     Collective on dm
358747c6ae99SBarry Smith 
3588d8d19677SJose E. Roman     Input Parameters:
358947c6ae99SBarry Smith +   dm - the DM object
359047c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
359147c6ae99SBarry Smith 
359247c6ae99SBarry Smith     Output Parameter:
359347c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
359447c6ae99SBarry Smith 
359547c6ae99SBarry Smith     Level: developer
359647c6ae99SBarry Smith 
3597e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
359847c6ae99SBarry Smith 
359947c6ae99SBarry Smith @*/
36007087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
360147c6ae99SBarry Smith {
360247c6ae99SBarry Smith   PetscErrorCode ierr;
360347c6ae99SBarry Smith 
360447c6ae99SBarry Smith   PetscFunctionBegin;
3605171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3606*7a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
360747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
360847c6ae99SBarry Smith   PetscValidPointer(dmc,3);
360947c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
361047c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
361147c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
361247c6ae99SBarry Smith     PetscInt i;
361347c6ae99SBarry Smith 
3614ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
361547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3616ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
361747c6ae99SBarry Smith     }
3618ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
361947c6ae99SBarry Smith   PetscFunctionReturn(0);
362047c6ae99SBarry Smith }
362147c6ae99SBarry Smith 
36221a266240SBarry Smith /*@C
36231a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
36241a266240SBarry Smith 
36251a266240SBarry Smith     Not Collective
36261a266240SBarry Smith 
36271a266240SBarry Smith     Input Parameters:
36281a266240SBarry Smith +   dm - the DM object
36291a266240SBarry Smith -   destroy - the destroy function
36301a266240SBarry Smith 
36311a266240SBarry Smith     Level: intermediate
36321a266240SBarry Smith 
3633e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
36341a266240SBarry Smith 
3635f07f9ceaSJed Brown @*/
36361a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
36371a266240SBarry Smith {
36381a266240SBarry Smith   PetscFunctionBegin;
3639171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36401a266240SBarry Smith   dm->ctxdestroy = destroy;
36411a266240SBarry Smith   PetscFunctionReturn(0);
36421a266240SBarry Smith }
36431a266240SBarry Smith 
3644b07ff414SBarry Smith /*@
36451b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
364647c6ae99SBarry Smith 
364747c6ae99SBarry Smith     Not Collective
364847c6ae99SBarry Smith 
364947c6ae99SBarry Smith     Input Parameters:
365047c6ae99SBarry Smith +   dm - the DM object
365147c6ae99SBarry Smith -   ctx - the user context
365247c6ae99SBarry Smith 
365347c6ae99SBarry Smith     Level: intermediate
365447c6ae99SBarry Smith 
3655e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
365647c6ae99SBarry Smith 
365747c6ae99SBarry Smith @*/
36581b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
365947c6ae99SBarry Smith {
366047c6ae99SBarry Smith   PetscFunctionBegin;
3661171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
366247c6ae99SBarry Smith   dm->ctx = ctx;
366347c6ae99SBarry Smith   PetscFunctionReturn(0);
366447c6ae99SBarry Smith }
366547c6ae99SBarry Smith 
366647c6ae99SBarry Smith /*@
36671b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
366847c6ae99SBarry Smith 
366947c6ae99SBarry Smith     Not Collective
367047c6ae99SBarry Smith 
367147c6ae99SBarry Smith     Input Parameter:
367247c6ae99SBarry Smith .   dm - the DM object
367347c6ae99SBarry Smith 
367447c6ae99SBarry Smith     Output Parameter:
367547c6ae99SBarry Smith .   ctx - the user context
367647c6ae99SBarry Smith 
367747c6ae99SBarry Smith     Level: intermediate
367847c6ae99SBarry Smith 
3679e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
368047c6ae99SBarry Smith 
368147c6ae99SBarry Smith @*/
36821b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
368347c6ae99SBarry Smith {
368447c6ae99SBarry Smith   PetscFunctionBegin;
3685171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36861b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
368747c6ae99SBarry Smith   PetscFunctionReturn(0);
368847c6ae99SBarry Smith }
368947c6ae99SBarry Smith 
369008da532bSDmitry Karpeev /*@C
3691df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
369208da532bSDmitry Karpeev 
3693d083f849SBarry Smith     Logically Collective on dm
369408da532bSDmitry Karpeev 
3695d8d19677SJose E. Roman     Input Parameters:
369608da532bSDmitry Karpeev +   dm - the DM object
36970298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
369808da532bSDmitry Karpeev 
369908da532bSDmitry Karpeev     Level: intermediate
370008da532bSDmitry Karpeev 
3701835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
370208da532bSDmitry Karpeev          DMSetJacobian()
370308da532bSDmitry Karpeev 
370408da532bSDmitry Karpeev @*/
370508da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
370608da532bSDmitry Karpeev {
370708da532bSDmitry Karpeev   PetscFunctionBegin;
37085a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
370908da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
371008da532bSDmitry Karpeev   PetscFunctionReturn(0);
371108da532bSDmitry Karpeev }
371208da532bSDmitry Karpeev 
371308da532bSDmitry Karpeev /*@
371408da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
371508da532bSDmitry Karpeev 
371608da532bSDmitry Karpeev     Not Collective
371708da532bSDmitry Karpeev 
371808da532bSDmitry Karpeev     Input Parameter:
371908da532bSDmitry Karpeev .   dm - the DM object to destroy
372008da532bSDmitry Karpeev 
372108da532bSDmitry Karpeev     Output Parameter:
372208da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
372308da532bSDmitry Karpeev 
372408da532bSDmitry Karpeev     Level: developer
372508da532bSDmitry Karpeev 
372674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
372708da532bSDmitry Karpeev 
372808da532bSDmitry Karpeev @*/
372908da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
373008da532bSDmitry Karpeev {
373108da532bSDmitry Karpeev   PetscFunctionBegin;
37325a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3733534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
373408da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
373508da532bSDmitry Karpeev   PetscFunctionReturn(0);
373608da532bSDmitry Karpeev }
373708da532bSDmitry Karpeev 
373808da532bSDmitry Karpeev /*@C
373908da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
374008da532bSDmitry Karpeev 
3741d083f849SBarry Smith     Logically Collective on dm
374208da532bSDmitry Karpeev 
3743f899ff85SJose E. Roman     Input Parameter:
3744907376e6SBarry Smith .   dm - the DM object
374508da532bSDmitry Karpeev 
374608da532bSDmitry Karpeev     Output parameters:
374708da532bSDmitry Karpeev +   xl - lower bound
374808da532bSDmitry Karpeev -   xu - upper bound
374908da532bSDmitry Karpeev 
3750907376e6SBarry Smith     Level: advanced
3751907376e6SBarry Smith 
375295452b02SPatrick Sanan     Notes:
375395452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
375408da532bSDmitry Karpeev 
375574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
375608da532bSDmitry Karpeev 
375708da532bSDmitry Karpeev @*/
375808da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
375908da532bSDmitry Karpeev {
376008da532bSDmitry Karpeev   PetscErrorCode ierr;
37615fd66863SKarl Rupp 
376208da532bSDmitry Karpeev   PetscFunctionBegin;
37635a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
376408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
37655a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3766*7a8be351SBarry Smith   PetscCheck(dm->ops->computevariablebounds,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
376708da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
376808da532bSDmitry Karpeev   PetscFunctionReturn(0);
376908da532bSDmitry Karpeev }
377008da532bSDmitry Karpeev 
3771b0ae01b7SPeter Brune /*@
3772b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3773b0ae01b7SPeter Brune 
3774b0ae01b7SPeter Brune     Not Collective
3775b0ae01b7SPeter Brune 
3776b0ae01b7SPeter Brune     Input Parameter:
3777b0ae01b7SPeter Brune .   dm - the DM object
3778b0ae01b7SPeter Brune 
3779b0ae01b7SPeter Brune     Output Parameter:
3780b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3781b0ae01b7SPeter Brune 
3782b0ae01b7SPeter Brune     Level: developer
3783b0ae01b7SPeter Brune 
37841565f0a7SPatrick Sanan .seealso DMCreateColoring()
3785b0ae01b7SPeter Brune 
3786b0ae01b7SPeter Brune @*/
3787b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3788b0ae01b7SPeter Brune {
3789b0ae01b7SPeter Brune   PetscFunctionBegin;
37905a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3791534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3792b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3793b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3794b0ae01b7SPeter Brune }
3795b0ae01b7SPeter Brune 
37963ad4599aSBarry Smith /*@
37973ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
37983ad4599aSBarry Smith 
37993ad4599aSBarry Smith     Not Collective
38003ad4599aSBarry Smith 
38013ad4599aSBarry Smith     Input Parameter:
38023ad4599aSBarry Smith .   dm - the DM object
38033ad4599aSBarry Smith 
38043ad4599aSBarry Smith     Output Parameter:
38053ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
38063ad4599aSBarry Smith 
38073ad4599aSBarry Smith     Level: developer
38083ad4599aSBarry Smith 
38091565f0a7SPatrick Sanan .seealso DMCreateRestriction()
38103ad4599aSBarry Smith 
38113ad4599aSBarry Smith @*/
38123ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
38133ad4599aSBarry Smith {
38143ad4599aSBarry Smith   PetscFunctionBegin;
38155a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3816534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
38173ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38183ad4599aSBarry Smith   PetscFunctionReturn(0);
38193ad4599aSBarry Smith }
38203ad4599aSBarry Smith 
3821a7058e45SLawrence Mitchell /*@
3822a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3823a7058e45SLawrence Mitchell 
3824a7058e45SLawrence Mitchell     Not Collective
3825a7058e45SLawrence Mitchell 
3826a7058e45SLawrence Mitchell     Input Parameter:
3827a7058e45SLawrence Mitchell .   dm - the DM object
3828a7058e45SLawrence Mitchell 
3829a7058e45SLawrence Mitchell     Output Parameter:
3830a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3831a7058e45SLawrence Mitchell 
3832a7058e45SLawrence Mitchell     Level: developer
3833a7058e45SLawrence Mitchell 
38341565f0a7SPatrick Sanan .seealso DMCreateInjection()
3835a7058e45SLawrence Mitchell 
3836a7058e45SLawrence Mitchell @*/
3837a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3838a7058e45SLawrence Mitchell {
38394a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
38405a84ad33SLisandro Dalcin 
3841a7058e45SLawrence Mitchell   PetscFunctionBegin;
38425a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3843534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
38445a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
38454a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
38465a84ad33SLisandro Dalcin   } else {
38475a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
38485a84ad33SLisandro Dalcin   }
3849a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3850a7058e45SLawrence Mitchell }
3851a7058e45SLawrence Mitchell 
38520298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3853264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3854264ace61SBarry Smith 
3855264ace61SBarry Smith /*@C
3856264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3857264ace61SBarry Smith 
3858d083f849SBarry Smith   Collective on dm
3859264ace61SBarry Smith 
3860264ace61SBarry Smith   Input Parameters:
3861264ace61SBarry Smith + dm     - The DM object
3862264ace61SBarry Smith - method - The name of the DM type
3863264ace61SBarry Smith 
3864264ace61SBarry Smith   Options Database Key:
3865264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3866264ace61SBarry Smith 
3867264ace61SBarry Smith   Notes:
3868e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3869264ace61SBarry Smith 
3870264ace61SBarry Smith   Level: intermediate
3871264ace61SBarry Smith 
3872264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3873264ace61SBarry Smith @*/
387419fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3875264ace61SBarry Smith {
3876264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3877264ace61SBarry Smith   PetscBool      match;
3878264ace61SBarry Smith   PetscErrorCode ierr;
3879264ace61SBarry Smith 
3880264ace61SBarry Smith   PetscFunctionBegin;
3881264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3882251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3883264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3884264ace61SBarry Smith 
38850f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
38861c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3887*7a8be351SBarry Smith   PetscCheck(r,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3888264ace61SBarry Smith 
3889264ace61SBarry Smith   if (dm->ops->destroy) {
3890264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3891264ace61SBarry Smith   }
3892d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3893264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3894d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3895264ace61SBarry Smith   PetscFunctionReturn(0);
3896264ace61SBarry Smith }
3897264ace61SBarry Smith 
3898264ace61SBarry Smith /*@C
3899264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3900264ace61SBarry Smith 
3901264ace61SBarry Smith   Not Collective
3902264ace61SBarry Smith 
3903264ace61SBarry Smith   Input Parameter:
3904264ace61SBarry Smith . dm  - The DM
3905264ace61SBarry Smith 
3906264ace61SBarry Smith   Output Parameter:
3907264ace61SBarry Smith . type - The DM type name
3908264ace61SBarry Smith 
3909264ace61SBarry Smith   Level: intermediate
3910264ace61SBarry Smith 
3911264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3912264ace61SBarry Smith @*/
391319fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3914264ace61SBarry Smith {
3915264ace61SBarry Smith   PetscErrorCode ierr;
3916264ace61SBarry Smith 
3917264ace61SBarry Smith   PetscFunctionBegin;
3918264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3919c959eef4SJed Brown   PetscValidPointer(type,2);
3920607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3921264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3922264ace61SBarry Smith   PetscFunctionReturn(0);
3923264ace61SBarry Smith }
3924264ace61SBarry Smith 
392567a56275SMatthew G Knepley /*@C
392667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
392767a56275SMatthew G Knepley 
3928d083f849SBarry Smith   Collective on dm
392967a56275SMatthew G Knepley 
393067a56275SMatthew G Knepley   Input Parameters:
393167a56275SMatthew G Knepley + dm - the DM
393267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
393367a56275SMatthew G Knepley 
393467a56275SMatthew G Knepley   Output Parameter:
393567a56275SMatthew G Knepley . M - pointer to new DM
393667a56275SMatthew G Knepley 
393767a56275SMatthew G Knepley   Notes:
393867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
393967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
394067a56275SMatthew G Knepley   of the input DM.
394167a56275SMatthew G Knepley 
394267a56275SMatthew G Knepley   Level: intermediate
394367a56275SMatthew G Knepley 
394467a56275SMatthew G Knepley .seealso: DMCreate()
394567a56275SMatthew G Knepley @*/
394619fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
394767a56275SMatthew G Knepley {
394867a56275SMatthew G Knepley   DM             B;
394967a56275SMatthew G Knepley   char           convname[256];
3950c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
395167a56275SMatthew G Knepley   PetscErrorCode ierr;
395267a56275SMatthew G Knepley 
395367a56275SMatthew G Knepley   PetscFunctionBegin;
395467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
395567a56275SMatthew G Knepley   PetscValidType(dm,1);
395667a56275SMatthew G Knepley   PetscValidPointer(M,3);
3957251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3958c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3959c067b6caSMatthew G. Knepley   if (sametype) {
3960c067b6caSMatthew G. Knepley     *M   = dm;
3961c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3962c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3963c067b6caSMatthew G. Knepley   } else {
39640298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
396567a56275SMatthew G Knepley 
396667a56275SMatthew G Knepley     /*
396767a56275SMatthew G Knepley        Order of precedence:
396867a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
396967a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
397067a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
397167a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
397267a56275SMatthew G Knepley        5) Use a really basic converter.
397367a56275SMatthew G Knepley     */
397467a56275SMatthew G Knepley 
397567a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3976a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3977a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3978a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3979a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3980a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
39810005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
398267a56275SMatthew G Knepley     if (conv) goto foundconv;
398367a56275SMatthew G Knepley 
398467a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
398582f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
398667a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3987a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3988a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3989a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3990a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3991a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
39920005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
399367a56275SMatthew G Knepley     if (conv) {
3994fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
399567a56275SMatthew G Knepley       goto foundconv;
399667a56275SMatthew G Knepley     }
399767a56275SMatthew G Knepley 
399867a56275SMatthew G Knepley #if 0
399967a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
400067a56275SMatthew G Knepley     conv = B->ops->convertfrom;
4001fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
400267a56275SMatthew G Knepley     if (conv) goto foundconv;
400367a56275SMatthew G Knepley 
400467a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
400567a56275SMatthew G Knepley     if (dm->ops->convert) {
400667a56275SMatthew G Knepley       conv = dm->ops->convert;
400767a56275SMatthew G Knepley     }
400867a56275SMatthew G Knepley     if (conv) goto foundconv;
400967a56275SMatthew G Knepley #endif
401067a56275SMatthew G Knepley 
401167a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
401298921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
401367a56275SMatthew G Knepley 
401467a56275SMatthew G Knepley foundconv:
401567a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
401667a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
401712fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
401890b157c4SStefano Zampini     {
401990b157c4SStefano Zampini       PetscBool             isper;
402012fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
402112fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
402290b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
402390b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
4024c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
4025a587d139SMark       ierr = PetscFree((*M)->vectype);CHKERRQ(ierr);
4026a587d139SMark       ierr = PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype);CHKERRQ(ierr);
4027a587d139SMark       ierr = PetscFree((*M)->mattype);CHKERRQ(ierr);
4028a587d139SMark       ierr = PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype);CHKERRQ(ierr);
402912fa691eSMatthew G. Knepley     }
403067a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
403167a56275SMatthew G Knepley   }
403267a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
403367a56275SMatthew G Knepley   PetscFunctionReturn(0);
403467a56275SMatthew G Knepley }
4035264ace61SBarry Smith 
4036264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4037264ace61SBarry Smith 
4038264ace61SBarry Smith /*@C
40391c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
40401c84c290SBarry Smith 
40411c84c290SBarry Smith   Not Collective
40421c84c290SBarry Smith 
40431c84c290SBarry Smith   Input Parameters:
40441c84c290SBarry Smith + name        - The name of a new user-defined creation routine
40451c84c290SBarry Smith - create_func - The creation routine itself
40461c84c290SBarry Smith 
40471c84c290SBarry Smith   Notes:
40481c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
40491c84c290SBarry Smith 
40501c84c290SBarry Smith   Sample usage:
40511c84c290SBarry Smith .vb
4052bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40531c84c290SBarry Smith .ve
40541c84c290SBarry Smith 
40551c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
40561c84c290SBarry Smith .vb
40571c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40581c84c290SBarry Smith     DMSetType(DM,"my_da");
40591c84c290SBarry Smith .ve
40601c84c290SBarry Smith    or at runtime via the option
40611c84c290SBarry Smith .vb
40621c84c290SBarry Smith     -da_type my_da
40631c84c290SBarry Smith .ve
4064264ace61SBarry Smith 
4065264ace61SBarry Smith   Level: advanced
40661c84c290SBarry Smith 
4067bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
40681c84c290SBarry Smith 
4069264ace61SBarry Smith @*/
4070bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
4071264ace61SBarry Smith {
4072264ace61SBarry Smith   PetscErrorCode ierr;
4073264ace61SBarry Smith 
4074264ace61SBarry Smith   PetscFunctionBegin;
40751d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
4076a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
4077264ace61SBarry Smith   PetscFunctionReturn(0);
4078264ace61SBarry Smith }
4079264ace61SBarry Smith 
4080b859378eSBarry Smith /*@C
408155849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
4082b859378eSBarry Smith 
4083d083f849SBarry Smith   Collective on viewer
4084b859378eSBarry Smith 
4085b859378eSBarry Smith   Input Parameters:
4086b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
4087b859378eSBarry Smith            some related function before a call to DMLoad().
4088b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
4089b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
4090b859378eSBarry Smith 
4091b859378eSBarry Smith    Level: intermediate
4092b859378eSBarry Smith 
4093b859378eSBarry Smith   Notes:
409455849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4095b859378eSBarry Smith 
4096cd7e8a5eSksagiyam   Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
4097cd7e8a5eSksagiyam   meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
4098cd7e8a5eSksagiyam   before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
4099cd7e8a5eSksagiyam 
4100b859378eSBarry Smith   Notes for advanced users:
4101b859378eSBarry Smith   Most users should not need to know the details of the binary storage
4102b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
4103b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
4104b859378eSBarry Smith   format is
4105b859378eSBarry Smith .vb
4106b859378eSBarry Smith      has not yet been determined
4107b859378eSBarry Smith .ve
4108b859378eSBarry Smith 
4109b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
4110b859378eSBarry Smith @*/
4111b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
4112b859378eSBarry Smith {
41139331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
4114b859378eSBarry Smith   PetscErrorCode ierr;
4115b859378eSBarry Smith 
4116b859378eSBarry Smith   PetscFunctionBegin;
4117b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
4118b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4119fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
412032c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
41219331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
412258cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
41239331c7a4SMatthew G. Knepley   if (isbinary) {
41249331c7a4SMatthew G. Knepley     PetscInt classid;
41259331c7a4SMatthew G. Knepley     char     type[256];
4126b859378eSBarry Smith 
4127060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
4128*7a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID,PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
4129060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
413032c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
41319331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
41329331c7a4SMatthew G. Knepley   } else if (ishdf5) {
41339331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
41349331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
413558cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
4136b859378eSBarry Smith   PetscFunctionReturn(0);
4137b859378eSBarry Smith }
4138b859378eSBarry Smith 
4139b2e4378dSMatthew G. Knepley /*@
4140b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
4141b2e4378dSMatthew G. Knepley 
4142b2e4378dSMatthew G. Knepley   Not collective
4143b2e4378dSMatthew G. Knepley 
4144b2e4378dSMatthew G. Knepley   Input Parameter:
4145b2e4378dSMatthew G. Knepley . dm - the DM
4146b2e4378dSMatthew G. Knepley 
4147b2e4378dSMatthew G. Knepley   Output Parameters:
4148b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
4149b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
4150b2e4378dSMatthew G. Knepley 
4151b2e4378dSMatthew G. Knepley   Level: beginner
4152b2e4378dSMatthew G. Knepley 
4153b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
4154b2e4378dSMatthew G. Knepley 
4155b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
4156b2e4378dSMatthew G. Knepley @*/
4157b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
4158b2e4378dSMatthew G. Knepley {
4159b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
4160b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
4161b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
4162b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
4163b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
4164b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
4165b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
4166b2e4378dSMatthew G. Knepley 
4167b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4168b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4169b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4170b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
4171b2e4378dSMatthew G. Knepley   if (coords) {
4172b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
4173b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
4174b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
4175b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
4176b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
4177b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4178b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4179b2e4378dSMatthew G. Knepley       }
4180b2e4378dSMatthew G. Knepley     }
4181b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
4182b2e4378dSMatthew G. Knepley   } else {
4183b2e4378dSMatthew G. Knepley     PetscBool isda;
4184b2e4378dSMatthew G. Knepley 
4185b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
4186b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
4187b2e4378dSMatthew G. Knepley   }
4188b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
4189b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
4190b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4191b2e4378dSMatthew G. Knepley }
4192b2e4378dSMatthew G. Knepley 
4193b2e4378dSMatthew G. Knepley /*@
4194b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
4195b2e4378dSMatthew G. Knepley 
4196b2e4378dSMatthew G. Knepley   Collective
4197b2e4378dSMatthew G. Knepley 
4198b2e4378dSMatthew G. Knepley   Input Parameter:
4199b2e4378dSMatthew G. Knepley . dm - the DM
4200b2e4378dSMatthew G. Knepley 
4201b2e4378dSMatthew G. Knepley   Output Parameters:
4202b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
4203b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
4204b2e4378dSMatthew G. Knepley 
4205b2e4378dSMatthew G. Knepley   Level: beginner
4206b2e4378dSMatthew G. Knepley 
4207b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
4208b2e4378dSMatthew G. Knepley @*/
4209b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
4210b2e4378dSMatthew G. Knepley {
4211b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
42126ce308c4SMatthew G. Knepley   PetscInt       cdim;
42136ce308c4SMatthew G. Knepley   PetscMPIInt    count;
4214b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
4215b2e4378dSMatthew G. Knepley 
4216b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4217b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4218b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
4219b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
4220b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
4221820f2d46SBarry Smith   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4222820f2d46SBarry Smith   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);}
4223b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4224b2e4378dSMatthew G. Knepley }
4225b2e4378dSMatthew G. Knepley 
42267da65231SMatthew G Knepley /******************************** FEM Support **********************************/
42277da65231SMatthew G Knepley 
4228a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4229a6dfd86eSKarl Rupp {
42301d47ebbbSSatish Balay   PetscInt       f;
42311b30c384SMatthew G Knepley   PetscErrorCode ierr;
42321b30c384SMatthew G Knepley 
42337da65231SMatthew G Knepley   PetscFunctionBegin;
423474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
42351d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
423657622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
42377da65231SMatthew G Knepley   }
42387da65231SMatthew G Knepley   PetscFunctionReturn(0);
42397da65231SMatthew G Knepley }
42407da65231SMatthew G Knepley 
4241a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4242a6dfd86eSKarl Rupp {
42431b30c384SMatthew G Knepley   PetscInt       f, g;
42447da65231SMatthew G Knepley   PetscErrorCode ierr;
42457da65231SMatthew G Knepley 
42467da65231SMatthew G Knepley   PetscFunctionBegin;
424774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
42481d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
424974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
42501d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
4251e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
42527da65231SMatthew G Knepley     }
425374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
42547da65231SMatthew G Knepley   }
42557da65231SMatthew G Knepley   PetscFunctionReturn(0);
42567da65231SMatthew G Knepley }
4257e7c4fc90SDmitry Karpeev 
42586113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4259e759306cSMatthew G. Knepley {
42600c5b8624SToby Isaac   PetscInt          localSize, bs;
42610c5b8624SToby Isaac   PetscMPIInt       size;
42620c5b8624SToby Isaac   Vec               x, xglob;
42630c5b8624SToby Isaac   const PetscScalar *xarray;
4264e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
4265e759306cSMatthew G. Knepley 
4266e759306cSMatthew G. Knepley   PetscFunctionBegin;
4267ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRMPI(ierr);
4268e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
4269e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
42706113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
42710c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
42720c5b8624SToby Isaac   if (size > 1) {
42730c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
42740c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
42750c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
42760c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
42770c5b8624SToby Isaac   } else {
42780c5b8624SToby Isaac     xglob = x;
42790c5b8624SToby Isaac   }
42800c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
42810c5b8624SToby Isaac   if (size > 1) {
42820c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
42830c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
42840c5b8624SToby Isaac   }
4285e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
4286e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4287e759306cSMatthew G. Knepley }
4288e759306cSMatthew G. Knepley 
428988ed4aceSMatthew G Knepley /*@
42901bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4291061576a5SJed Brown 
4292061576a5SJed Brown   Input Parameter:
4293061576a5SJed Brown . dm - The DM
4294061576a5SJed Brown 
4295061576a5SJed Brown   Output Parameter:
4296061576a5SJed Brown . section - The PetscSection
4297061576a5SJed Brown 
4298061576a5SJed Brown   Options Database Keys:
4299061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4300061576a5SJed Brown 
4301061576a5SJed Brown   Level: advanced
4302061576a5SJed Brown 
4303061576a5SJed Brown   Notes:
4304061576a5SJed Brown   Use DMGetLocalSection() in new code.
4305061576a5SJed Brown 
4306061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4307061576a5SJed Brown 
4308061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
4309061576a5SJed Brown @*/
4310061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4311061576a5SJed Brown {
4312061576a5SJed Brown   PetscErrorCode ierr;
4313061576a5SJed Brown 
4314061576a5SJed Brown   PetscFunctionBegin;
4315061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
4316061576a5SJed Brown   PetscFunctionReturn(0);
4317061576a5SJed Brown }
4318061576a5SJed Brown 
4319061576a5SJed Brown /*@
4320061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
432188ed4aceSMatthew G Knepley 
432288ed4aceSMatthew G Knepley   Input Parameter:
432388ed4aceSMatthew G Knepley . dm - The DM
432488ed4aceSMatthew G Knepley 
432588ed4aceSMatthew G Knepley   Output Parameter:
432688ed4aceSMatthew G Knepley . section - The PetscSection
432788ed4aceSMatthew G Knepley 
4328e5893cccSMatthew G. Knepley   Options Database Keys:
4329e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4330e5893cccSMatthew G. Knepley 
433188ed4aceSMatthew G Knepley   Level: intermediate
433288ed4aceSMatthew G Knepley 
433388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
433488ed4aceSMatthew G Knepley 
4335061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
433688ed4aceSMatthew G Knepley @*/
4337061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
43380adebc6cSBarry Smith {
4339fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4340fd59a867SMatthew G. Knepley 
434188ed4aceSMatthew G Knepley   PetscFunctionBegin;
434288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
434388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
43441bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4345e5e52638SMatthew G. Knepley     PetscInt d;
4346e5e52638SMatthew G. Knepley 
434745480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
434845480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject) dm;
434945480ffeSMatthew G. Knepley       PetscViewer       viewer;
435045480ffeSMatthew G. Knepley       PetscViewerFormat format;
435145480ffeSMatthew G. Knepley       PetscBool         flg;
435245480ffeSMatthew G. Knepley 
435345480ffeSMatthew G. Knepley       ierr = PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg);CHKERRQ(ierr);
435445480ffeSMatthew G. Knepley       if (flg) {ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr);}
435545480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
435645480ffeSMatthew G. Knepley         ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);
435745480ffeSMatthew G. Knepley         if (flg) {ierr = PetscDSView(dm->probs[d].ds, viewer);CHKERRQ(ierr);}
435845480ffeSMatthew G. Knepley       }
435945480ffeSMatthew G. Knepley       if (flg) {
436045480ffeSMatthew G. Knepley         ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
436145480ffeSMatthew G. Knepley         ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
436245480ffeSMatthew G. Knepley         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
436345480ffeSMatthew G. Knepley       }
436445480ffeSMatthew G. Knepley     }
43651bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
43661bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
43672f0f8703SMatthew G. Knepley   }
43681bb6d2a8SBarry Smith   *section = dm->localSection;
436988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
437088ed4aceSMatthew G Knepley }
437188ed4aceSMatthew G Knepley 
437288ed4aceSMatthew G Knepley /*@
43731bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4374061576a5SJed Brown 
4375061576a5SJed Brown   Input Parameters:
4376061576a5SJed Brown + dm - The DM
4377061576a5SJed Brown - section - The PetscSection
4378061576a5SJed Brown 
4379061576a5SJed Brown   Level: advanced
4380061576a5SJed Brown 
4381061576a5SJed Brown   Notes:
4382061576a5SJed Brown   Use DMSetLocalSection() in new code.
4383061576a5SJed Brown 
4384061576a5SJed Brown   Any existing Section will be destroyed
4385061576a5SJed Brown 
4386061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4387061576a5SJed Brown @*/
4388061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4389061576a5SJed Brown {
4390061576a5SJed Brown   PetscErrorCode ierr;
4391061576a5SJed Brown 
4392061576a5SJed Brown   PetscFunctionBegin;
4393061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4394061576a5SJed Brown   PetscFunctionReturn(0);
4395061576a5SJed Brown }
4396061576a5SJed Brown 
4397061576a5SJed Brown /*@
4398061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
439988ed4aceSMatthew G Knepley 
440088ed4aceSMatthew G Knepley   Input Parameters:
440188ed4aceSMatthew G Knepley + dm - The DM
440288ed4aceSMatthew G Knepley - section - The PetscSection
440388ed4aceSMatthew G Knepley 
440488ed4aceSMatthew G Knepley   Level: intermediate
440588ed4aceSMatthew G Knepley 
440688ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
440788ed4aceSMatthew G Knepley 
4408061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
440988ed4aceSMatthew G Knepley @*/
4410061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
44110adebc6cSBarry Smith {
4412c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4413af122d2aSMatthew G Knepley   PetscInt       f;
441488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
441588ed4aceSMatthew G Knepley 
441688ed4aceSMatthew G Knepley   PetscFunctionBegin;
441788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4418b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
44191d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
44201bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
44211bb6d2a8SBarry Smith   dm->localSection = section;
44221bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4423af122d2aSMatthew G Knepley   if (numFields) {
4424af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4425af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
44260f21e855SMatthew G. Knepley       PetscObject disc;
4427af122d2aSMatthew G Knepley       const char *name;
4428af122d2aSMatthew G Knepley 
44291bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
443044a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
44310f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4432af122d2aSMatthew G Knepley     }
4433af122d2aSMatthew G Knepley   }
4434e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
44351bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
443688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
443788ed4aceSMatthew G Knepley }
443888ed4aceSMatthew G Knepley 
44399435951eSToby Isaac /*@
4440b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
44419435951eSToby Isaac 
4442e228b242SToby Isaac   not collective
4443e228b242SToby Isaac 
44449435951eSToby Isaac   Input Parameter:
44459435951eSToby Isaac . dm - The DM
44469435951eSToby Isaac 
4447d8d19677SJose E. Roman   Output Parameters:
44489435951eSToby 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.
444979769bd5SJed Brown . 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.
445079769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
44519435951eSToby Isaac 
44529435951eSToby Isaac   Level: advanced
44539435951eSToby Isaac 
445479769bd5SJed Brown   Note: This gets borrowed references, so the user should not destroy the PetscSection, Mat, or Vec.
44559435951eSToby Isaac 
44569435951eSToby Isaac .seealso: DMSetDefaultConstraints()
44579435951eSToby Isaac @*/
445879769bd5SJed Brown PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
44599435951eSToby Isaac {
44609435951eSToby Isaac   PetscErrorCode ierr;
44619435951eSToby Isaac 
44629435951eSToby Isaac   PetscFunctionBegin;
44639435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44643b8ba7d1SJed Brown   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
44653b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44663b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
446779769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44689435951eSToby Isaac   PetscFunctionReturn(0);
44699435951eSToby Isaac }
44709435951eSToby Isaac 
44719435951eSToby Isaac /*@
4472b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
44739435951eSToby Isaac 
447479769bd5SJed Brown   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 + bias, l[s[i]] = c[i], where the scatter s is defined by the PetscSection returned by DMGetDefaultConstraints().
44759435951eSToby Isaac 
447679769bd5SJed Brown   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.  Any bias, if specified, is ignored when accumulating.
44779435951eSToby Isaac 
4478e228b242SToby Isaac   collective on dm
4479e228b242SToby Isaac 
44809435951eSToby Isaac   Input Parameters:
44819435951eSToby Isaac + dm - The DM
448279769bd5SJed Brown . 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).
448379769bd5SJed Brown . 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).
448479769bd5SJed Brown - bias - A bias vector to be added to constrained values in the local vector.  NULL indicates no bias.  Must have a local communicator (PETSC_COMM_SELF or derivative).
44859435951eSToby Isaac 
44869435951eSToby Isaac   Level: advanced
44879435951eSToby Isaac 
448879769bd5SJed Brown   Note: This increments the references of the PetscSection, Mat, and Vec, so they user can destroy them.
44899435951eSToby Isaac 
44909435951eSToby Isaac .seealso: DMGetDefaultConstraints()
44919435951eSToby Isaac @*/
449279769bd5SJed Brown PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
44939435951eSToby Isaac {
4494e228b242SToby Isaac   PetscMPIInt result;
44959435951eSToby Isaac   PetscErrorCode ierr;
44969435951eSToby Isaac 
44979435951eSToby Isaac   PetscFunctionBegin;
44989435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4499e228b242SToby Isaac   if (section) {
4500e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4501ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRMPI(ierr);
4502*7a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4503e228b242SToby Isaac   }
4504e228b242SToby Isaac   if (mat) {
4505e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4506ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRMPI(ierr);
4507*7a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4508e228b242SToby Isaac   }
450979769bd5SJed Brown   if (bias) {
451079769bd5SJed Brown     PetscValidHeaderSpecific(bias,VEC_CLASSID,4);
451179769bd5SJed Brown     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)bias),&result);CHKERRMPI(ierr);
451279769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint bias must have local communicator");
451379769bd5SJed Brown   }
45149435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
45153b8ba7d1SJed Brown   ierr = PetscSectionDestroy(&dm->defaultConstraint.section);CHKERRQ(ierr);
45163b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
45179435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
45183b8ba7d1SJed Brown   ierr = MatDestroy(&dm->defaultConstraint.mat);CHKERRQ(ierr);
45193b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
452079769bd5SJed Brown   ierr = PetscObjectReference((PetscObject)bias);CHKERRQ(ierr);
452179769bd5SJed Brown   ierr = VecDestroy(&dm->defaultConstraint.bias);CHKERRQ(ierr);
452279769bd5SJed Brown   dm->defaultConstraint.bias = bias;
45239435951eSToby Isaac   PetscFunctionReturn(0);
45249435951eSToby Isaac }
45259435951eSToby Isaac 
4526497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4527507e4973SMatthew G. Knepley /*
4528507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4529507e4973SMatthew G. Knepley 
4530507e4973SMatthew G. Knepley   Input Parameters:
4531507e4973SMatthew G. Knepley + dm - The DM
4532507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4533507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4534507e4973SMatthew G. Knepley 
4535507e4973SMatthew G. Knepley   Level: intermediate
4536507e4973SMatthew G. Knepley 
45371bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4538507e4973SMatthew G. Knepley */
4539f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4540507e4973SMatthew G. Knepley {
4541507e4973SMatthew G. Knepley   MPI_Comm        comm;
4542507e4973SMatthew G. Knepley   PetscLayout     layout;
4543507e4973SMatthew G. Knepley   const PetscInt *ranges;
4544507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4545507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4546507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4547507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4548507e4973SMatthew G. Knepley 
4549507e4973SMatthew G. Knepley   PetscFunctionBegin;
4550507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4551507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4552ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm, &size);CHKERRMPI(ierr);
4553ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
4554507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4555507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4556507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4557507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4558507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4559507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4560507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4561507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4562f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4563507e4973SMatthew G. Knepley 
4564507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4565507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4566507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4567507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4568507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4569507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4570507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4571507e4973SMatthew 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;}
4572507e4973SMatthew 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;}
4573507e4973SMatthew G. Knepley     if (gdof < 0) {
4574507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4575507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4576507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4577507e4973SMatthew G. Knepley 
4578507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4579507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4580507e4973SMatthew 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;}
4581507e4973SMatthew G. Knepley       }
4582507e4973SMatthew G. Knepley     }
4583507e4973SMatthew G. Knepley   }
4584507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4585507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4586820f2d46SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRMPI(ierr);
4587507e4973SMatthew G. Knepley   if (!gvalid) {
4588507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4589507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4590507e4973SMatthew G. Knepley   }
4591507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4592507e4973SMatthew G. Knepley }
4593f741bcd2SMatthew G. Knepley #endif
4594507e4973SMatthew G. Knepley 
459588ed4aceSMatthew G Knepley /*@
4596e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
459788ed4aceSMatthew G Knepley 
4598d083f849SBarry Smith   Collective on dm
45998b1ab98fSJed Brown 
460088ed4aceSMatthew G Knepley   Input Parameter:
460188ed4aceSMatthew G Knepley . dm - The DM
460288ed4aceSMatthew G Knepley 
460388ed4aceSMatthew G Knepley   Output Parameter:
460488ed4aceSMatthew G Knepley . section - The PetscSection
460588ed4aceSMatthew G Knepley 
460688ed4aceSMatthew G Knepley   Level: intermediate
460788ed4aceSMatthew G Knepley 
460888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
460988ed4aceSMatthew G Knepley 
461092fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
461188ed4aceSMatthew G Knepley @*/
4612e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
46130adebc6cSBarry Smith {
461488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
461588ed4aceSMatthew G Knepley 
461688ed4aceSMatthew G Knepley   PetscFunctionBegin;
461788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
461888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
46191bb6d2a8SBarry Smith   if (!dm->globalSection) {
4620fd59a867SMatthew G. Knepley     PetscSection s;
4621fd59a867SMatthew G. Knepley 
462292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4623*7a8be351SBarry Smith     PetscCheck(s,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
4624*7a8be351SBarry Smith     PetscCheck(dm->sf,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
46251bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4626cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
46271bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
46281bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
462988ed4aceSMatthew G Knepley   }
46301bb6d2a8SBarry Smith   *section = dm->globalSection;
463188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
463288ed4aceSMatthew G Knepley }
463388ed4aceSMatthew G Knepley 
4634b21d0597SMatthew G Knepley /*@
4635e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4636b21d0597SMatthew G Knepley 
4637b21d0597SMatthew G Knepley   Input Parameters:
4638b21d0597SMatthew G Knepley + dm - The DM
46395080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4640b21d0597SMatthew G Knepley 
4641b21d0597SMatthew G Knepley   Level: intermediate
4642b21d0597SMatthew G Knepley 
4643b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4644b21d0597SMatthew G Knepley 
464592fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4646b21d0597SMatthew G Knepley @*/
4647e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
46480adebc6cSBarry Smith {
4649b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4650b21d0597SMatthew G Knepley 
4651b21d0597SMatthew G Knepley   PetscFunctionBegin;
4652b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46535080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
46541d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
46551bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
46561bb6d2a8SBarry Smith   dm->globalSection = section;
4657497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46581bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4659507e4973SMatthew G. Knepley #endif
4660b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4661b21d0597SMatthew G Knepley }
4662b21d0597SMatthew G Knepley 
466388ed4aceSMatthew G Knepley /*@
46641bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
466588ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
466688ed4aceSMatthew G Knepley 
466788ed4aceSMatthew G Knepley   Input Parameter:
466888ed4aceSMatthew G Knepley . dm - The DM
466988ed4aceSMatthew G Knepley 
467088ed4aceSMatthew G Knepley   Output Parameter:
467188ed4aceSMatthew G Knepley . sf - The PetscSF
467288ed4aceSMatthew G Knepley 
467388ed4aceSMatthew G Knepley   Level: intermediate
467488ed4aceSMatthew G Knepley 
467588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
467688ed4aceSMatthew G Knepley 
46771bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
467888ed4aceSMatthew G Knepley @*/
46791bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
46800adebc6cSBarry Smith {
468188ed4aceSMatthew G Knepley   PetscInt       nroots;
468288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
468388ed4aceSMatthew G Knepley 
468488ed4aceSMatthew G Knepley   PetscFunctionBegin;
468588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
468688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
46871bb6d2a8SBarry Smith   if (!dm->sectionSF) {
46881bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
468933907cc2SStefano Zampini   }
46901bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
469188ed4aceSMatthew G Knepley   if (nroots < 0) {
469288ed4aceSMatthew G Knepley     PetscSection section, gSection;
469388ed4aceSMatthew G Knepley 
469492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
469531ea6d37SMatthew G Knepley     if (section) {
4696e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
46971bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
469831ea6d37SMatthew G Knepley     } else {
46990298fd71SBarry Smith       *sf = NULL;
470031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
470131ea6d37SMatthew G Knepley     }
470288ed4aceSMatthew G Knepley   }
47031bb6d2a8SBarry Smith   *sf = dm->sectionSF;
470488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
470588ed4aceSMatthew G Knepley }
470688ed4aceSMatthew G Knepley 
470788ed4aceSMatthew G Knepley /*@
47081bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
470988ed4aceSMatthew G Knepley 
471088ed4aceSMatthew G Knepley   Input Parameters:
471188ed4aceSMatthew G Knepley + dm - The DM
471288ed4aceSMatthew G Knepley - sf - The PetscSF
471388ed4aceSMatthew G Knepley 
471488ed4aceSMatthew G Knepley   Level: intermediate
471588ed4aceSMatthew G Knepley 
471688ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
471788ed4aceSMatthew G Knepley 
47181bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
471988ed4aceSMatthew G Knepley @*/
47201bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
47210adebc6cSBarry Smith {
472288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
472388ed4aceSMatthew G Knepley 
472488ed4aceSMatthew G Knepley   PetscFunctionBegin;
472588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4726b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
472733907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
47281bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
47291bb6d2a8SBarry Smith   dm->sectionSF = sf;
473088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
473188ed4aceSMatthew G Knepley }
473288ed4aceSMatthew G Knepley 
473388ed4aceSMatthew G Knepley /*@C
47341bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
473588ed4aceSMatthew G Knepley   describing the data layout.
473688ed4aceSMatthew G Knepley 
473788ed4aceSMatthew G Knepley   Input Parameters:
473888ed4aceSMatthew G Knepley + dm - The DM
473988ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
474088ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
474188ed4aceSMatthew G Knepley 
47421bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
474388ed4aceSMatthew G Knepley 
47441bb6d2a8SBarry Smith   Level: developer
47451bb6d2a8SBarry Smith 
47461bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
47471bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
47481bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
47491bb6d2a8SBarry Smith 
47501bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
475188ed4aceSMatthew G Knepley @*/
47521bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
475388ed4aceSMatthew G Knepley {
475488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
475588ed4aceSMatthew G Knepley 
475688ed4aceSMatthew G Knepley   PetscFunctionBegin;
475788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4758b0c7db22SLisandro Dalcin   ierr = PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection);CHKERRQ(ierr);
475988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
476088ed4aceSMatthew G Knepley }
4761af122d2aSMatthew G Knepley 
4762b21d0597SMatthew G Knepley /*@
4763b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4764b21d0597SMatthew G Knepley 
4765b21d0597SMatthew G Knepley   Input Parameter:
4766b21d0597SMatthew G Knepley . dm - The DM
4767b21d0597SMatthew G Knepley 
4768b21d0597SMatthew G Knepley   Output Parameter:
4769b21d0597SMatthew G Knepley . sf - The PetscSF
4770b21d0597SMatthew G Knepley 
4771b21d0597SMatthew G Knepley   Level: intermediate
4772b21d0597SMatthew G Knepley 
4773b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4774b21d0597SMatthew G Knepley 
47751bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4776b21d0597SMatthew G Knepley @*/
47770adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
47780adebc6cSBarry Smith {
4779b21d0597SMatthew G Knepley   PetscFunctionBegin;
4780b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4781b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4782b21d0597SMatthew G Knepley   *sf = dm->sf;
4783b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4784b21d0597SMatthew G Knepley }
4785b21d0597SMatthew G Knepley 
4786057b4bcdSMatthew G Knepley /*@
4787057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4788057b4bcdSMatthew G Knepley 
4789057b4bcdSMatthew G Knepley   Input Parameters:
4790057b4bcdSMatthew G Knepley + dm - The DM
4791057b4bcdSMatthew G Knepley - sf - The PetscSF
4792057b4bcdSMatthew G Knepley 
4793057b4bcdSMatthew G Knepley   Level: intermediate
4794057b4bcdSMatthew G Knepley 
47951bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4796057b4bcdSMatthew G Knepley @*/
47970adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
47980adebc6cSBarry Smith {
4799057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4800057b4bcdSMatthew G Knepley 
4801057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4802057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4803b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4804057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
480533907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4806057b4bcdSMatthew G Knepley   dm->sf = sf;
4807057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4808057b4bcdSMatthew G Knepley }
4809057b4bcdSMatthew G Knepley 
48104f37162bSMatthew G. Knepley /*@
48114f37162bSMatthew G. Knepley   DMGetNaturalSF - Get the PetscSF encoding the map back to the original mesh ordering
48124f37162bSMatthew G. Knepley 
48134f37162bSMatthew G. Knepley   Input Parameter:
48144f37162bSMatthew G. Knepley . dm - The DM
48154f37162bSMatthew G. Knepley 
48164f37162bSMatthew G. Knepley   Output Parameter:
48174f37162bSMatthew G. Knepley . sf - The PetscSF
48184f37162bSMatthew G. Knepley 
48194f37162bSMatthew G. Knepley   Level: intermediate
48204f37162bSMatthew G. Knepley 
48214f37162bSMatthew G. Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
48224f37162bSMatthew G. Knepley 
48234f37162bSMatthew G. Knepley .seealso: DMSetNaturalSF(), DMSetUseNatural(), DMGetUseNatural(), DMPlexCreateGlobalToNaturalSF(), DMPlexDistribute()
48244f37162bSMatthew G. Knepley @*/
48254f37162bSMatthew G. Knepley PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
48264f37162bSMatthew G. Knepley {
48274f37162bSMatthew G. Knepley   PetscFunctionBegin;
48284f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48294f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
48304f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48314f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
48324f37162bSMatthew G. Knepley }
48334f37162bSMatthew G. Knepley 
48344f37162bSMatthew G. Knepley /*@
48354f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48364f37162bSMatthew G. Knepley 
48374f37162bSMatthew G. Knepley   Input Parameters:
48384f37162bSMatthew G. Knepley + dm - The DM
48394f37162bSMatthew G. Knepley - sf - The PetscSF
48404f37162bSMatthew G. Knepley 
48414f37162bSMatthew G. Knepley   Level: intermediate
48424f37162bSMatthew G. Knepley 
48434f37162bSMatthew G. Knepley .seealso: DMGetNaturalSF(), DMSetUseNatural(), DMGetUseNatural(), DMPlexCreateGlobalToNaturalSF(), DMPlexDistribute()
48444f37162bSMatthew G. Knepley @*/
48454f37162bSMatthew G. Knepley PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
48464f37162bSMatthew G. Knepley {
48474f37162bSMatthew G. Knepley   PetscErrorCode ierr;
48484f37162bSMatthew G. Knepley 
48494f37162bSMatthew G. Knepley   PetscFunctionBegin;
48504f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48514f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48524f37162bSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
48534f37162bSMatthew G. Knepley   ierr = PetscSFDestroy(&dm->sfNatural);CHKERRQ(ierr);
48544f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48554f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
48564f37162bSMatthew G. Knepley }
48574f37162bSMatthew G. Knepley 
485834aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
485934aa8a36SMatthew G. Knepley {
486034aa8a36SMatthew G. Knepley   PetscClassId   id;
486134aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
486234aa8a36SMatthew G. Knepley 
486334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
486434aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
486534aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
486634aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
486734aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
486834aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
486917c1d62eSMatthew G. Knepley   } else {
487017c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
487134aa8a36SMatthew G. Knepley   }
487234aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
487334aa8a36SMatthew G. Knepley }
487434aa8a36SMatthew G. Knepley 
487544a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
487644a7f3ddSMatthew G. Knepley {
487744a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
487844a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
487944a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
488044a7f3ddSMatthew G. Knepley 
488144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
488244a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
488344a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
488444a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4885e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
488644a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
488744a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
488844a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
488944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
489044a7f3ddSMatthew G. Knepley }
489144a7f3ddSMatthew G. Knepley 
489244a7f3ddSMatthew G. Knepley /*@
489344a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
489444a7f3ddSMatthew G. Knepley 
4895d083f849SBarry Smith   Logically collective on dm
489644a7f3ddSMatthew G. Knepley 
489744a7f3ddSMatthew G. Knepley   Input Parameter:
489844a7f3ddSMatthew G. Knepley . dm - The DM
489944a7f3ddSMatthew G. Knepley 
490044a7f3ddSMatthew G. Knepley   Level: intermediate
490144a7f3ddSMatthew G. Knepley 
490244a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
490344a7f3ddSMatthew G. Knepley @*/
490444a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
490544a7f3ddSMatthew G. Knepley {
490644a7f3ddSMatthew G. Knepley   PetscInt       f;
490744a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
490844a7f3ddSMatthew G. Knepley 
490944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
491044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
491144a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
491244a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
491344a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
491444a7f3ddSMatthew G. Knepley   }
491544a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
491644a7f3ddSMatthew G. Knepley   dm->fields = NULL;
491744a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
491844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
491944a7f3ddSMatthew G. Knepley }
492044a7f3ddSMatthew G. Knepley 
4921689b5837SMatthew G. Knepley /*@
4922689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4923689b5837SMatthew G. Knepley 
4924689b5837SMatthew G. Knepley   Not collective
4925689b5837SMatthew G. Knepley 
4926689b5837SMatthew G. Knepley   Input Parameter:
4927689b5837SMatthew G. Knepley . dm - The DM
4928689b5837SMatthew G. Knepley 
4929689b5837SMatthew G. Knepley   Output Parameter:
4930689b5837SMatthew G. Knepley . Nf - The number of fields
4931689b5837SMatthew G. Knepley 
4932689b5837SMatthew G. Knepley   Level: intermediate
4933689b5837SMatthew G. Knepley 
4934689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4935689b5837SMatthew G. Knepley @*/
49360f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
49370f21e855SMatthew G. Knepley {
49380f21e855SMatthew G. Knepley   PetscFunctionBegin;
49390f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4940534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
494144a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4942af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4943af122d2aSMatthew G Knepley }
4944af122d2aSMatthew G Knepley 
4945689b5837SMatthew G. Knepley /*@
4946689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4947689b5837SMatthew G. Knepley 
4948d083f849SBarry Smith   Logically collective on dm
4949689b5837SMatthew G. Knepley 
4950689b5837SMatthew G. Knepley   Input Parameters:
4951689b5837SMatthew G. Knepley + dm - The DM
4952689b5837SMatthew G. Knepley - Nf - The number of fields
4953689b5837SMatthew G. Knepley 
4954689b5837SMatthew G. Knepley   Level: intermediate
4955689b5837SMatthew G. Knepley 
4956689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4957689b5837SMatthew G. Knepley @*/
4958af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4959af122d2aSMatthew G Knepley {
49600f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4961af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4962af122d2aSMatthew G Knepley 
4963af122d2aSMatthew G Knepley   PetscFunctionBegin;
4964af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
496544a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
49660f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49670f21e855SMatthew G. Knepley     PetscContainer obj;
49680f21e855SMatthew G. Knepley 
49690f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
497044a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
49710f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4972af122d2aSMatthew G Knepley   }
4973af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4974af122d2aSMatthew G Knepley }
4975af122d2aSMatthew G Knepley 
4976c1929be8SMatthew G. Knepley /*@
4977c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4978c1929be8SMatthew G. Knepley 
4979c1929be8SMatthew G. Knepley   Not collective
4980c1929be8SMatthew G. Knepley 
4981c1929be8SMatthew G. Knepley   Input Parameters:
4982c1929be8SMatthew G. Knepley + dm - The DM
4983c1929be8SMatthew G. Knepley - f  - The field number
4984c1929be8SMatthew G. Knepley 
498544a7f3ddSMatthew G. Knepley   Output Parameters:
498644a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
498744a7f3ddSMatthew G. Knepley - field - The discretization object
4988c1929be8SMatthew G. Knepley 
498944a7f3ddSMatthew G. Knepley   Level: intermediate
4990c1929be8SMatthew G. Knepley 
499144a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4992c1929be8SMatthew G. Knepley @*/
499344a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4994af122d2aSMatthew G Knepley {
4995af122d2aSMatthew G Knepley   PetscFunctionBegin;
4996af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4997064a246eSJacob Faibussowitsch   PetscValidPointer(field, 4);
4998*7a8be351SBarry Smith   PetscCheck((f >= 0) && (f < dm->Nf),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, dm->Nf);
499944a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
500044a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
5001decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
5002decb47aaSMatthew G. Knepley }
5003decb47aaSMatthew G. Knepley 
5004083401c6SMatthew G. Knepley /* Does not clear the DS */
5005083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
5006083401c6SMatthew G. Knepley {
5007083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5008083401c6SMatthew G. Knepley 
5009083401c6SMatthew G. Knepley   PetscFunctionBegin;
5010083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
5011083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
5012083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
5013083401c6SMatthew G. Knepley   dm->fields[f].label = label;
5014083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
5015083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5016083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
5017083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5018083401c6SMatthew G. Knepley }
5019083401c6SMatthew G. Knepley 
5020c1929be8SMatthew G. Knepley /*@
5021c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
5022c1929be8SMatthew G. Knepley 
5023d083f849SBarry Smith   Logically collective on dm
5024c1929be8SMatthew G. Knepley 
5025c1929be8SMatthew G. Knepley   Input Parameters:
5026c1929be8SMatthew G. Knepley + dm    - The DM
5027c1929be8SMatthew G. Knepley . f     - The field number
502844a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
5029c1929be8SMatthew G. Knepley - field - The discretization object
5030c1929be8SMatthew G. Knepley 
503144a7f3ddSMatthew G. Knepley   Level: intermediate
5032c1929be8SMatthew G. Knepley 
503344a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
5034c1929be8SMatthew G. Knepley @*/
503544a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
5036decb47aaSMatthew G. Knepley {
5037decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
5038decb47aaSMatthew G. Knepley 
5039decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5040decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5041e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
504244a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
5043*7a8be351SBarry Smith   PetscCheck(f >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5044083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
504534aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
50462df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
504744a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
504844a7f3ddSMatthew G. Knepley }
504944a7f3ddSMatthew G. Knepley 
505044a7f3ddSMatthew G. Knepley /*@
505144a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
505244a7f3ddSMatthew G. Knepley 
5053d083f849SBarry Smith   Logically collective on dm
505444a7f3ddSMatthew G. Knepley 
505544a7f3ddSMatthew G. Knepley   Input Parameters:
505644a7f3ddSMatthew G. Knepley + dm    - The DM
505744a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
505844a7f3ddSMatthew G. Knepley - field - The discretization object
505944a7f3ddSMatthew G. Knepley 
506044a7f3ddSMatthew G. Knepley   Level: intermediate
506144a7f3ddSMatthew G. Knepley 
506244a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
506344a7f3ddSMatthew G. Knepley @*/
506444a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
506544a7f3ddSMatthew G. Knepley {
506644a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
506744a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
506844a7f3ddSMatthew G. Knepley 
506944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
507044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5071064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
507244a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
507344a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
507444a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
507544a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
507644a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
507744a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
507834aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
50792df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
5080af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
5081af122d2aSMatthew G Knepley }
50826636e97aSMatthew G Knepley 
5083e5e52638SMatthew G. Knepley /*@
5084e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5085e0b68406SMatthew Knepley 
5086e0b68406SMatthew Knepley   Logically collective on dm
5087e0b68406SMatthew Knepley 
5088e0b68406SMatthew Knepley   Input Parameters:
5089e0b68406SMatthew Knepley + dm          - The DM
5090e0b68406SMatthew Knepley . f           - The field index
5091e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
5092e0b68406SMatthew Knepley 
5093e0b68406SMatthew Knepley   Level: intermediate
5094e0b68406SMatthew Knepley 
5095e0b68406SMatthew Knepley .seealso: DMGetFieldAvoidTensor(), DMSetField(), DMGetField()
5096e0b68406SMatthew Knepley @*/
5097e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5098e0b68406SMatthew Knepley {
5099e0b68406SMatthew Knepley   PetscFunctionBegin;
5100*7a8be351SBarry Smith   PetscCheck((f > 0) && (f < dm->Nf),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
5101e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
5102e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5103e0b68406SMatthew Knepley }
5104e0b68406SMatthew Knepley 
5105e0b68406SMatthew Knepley /*@
5106e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5107e0b68406SMatthew Knepley 
5108e0b68406SMatthew Knepley   Logically collective on dm
5109e0b68406SMatthew Knepley 
5110e0b68406SMatthew Knepley   Input Parameters:
5111e0b68406SMatthew Knepley + dm          - The DM
5112e0b68406SMatthew Knepley - f           - The field index
5113e0b68406SMatthew Knepley 
5114e0b68406SMatthew Knepley   Output Parameter:
5115e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5116e0b68406SMatthew Knepley 
5117e0b68406SMatthew Knepley   Level: intermediate
5118e0b68406SMatthew Knepley 
5119e0b68406SMatthew Knepley .seealso: DMSetFieldAvoidTensor(), DMSetField(), DMGetField()
5120e0b68406SMatthew Knepley @*/
5121e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5122e0b68406SMatthew Knepley {
5123e0b68406SMatthew Knepley   PetscFunctionBegin;
5124*7a8be351SBarry Smith   PetscCheck((f >= 0) && (f < dm->Nf),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
5125e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
5126e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5127e0b68406SMatthew Knepley }
5128e0b68406SMatthew Knepley 
5129e0b68406SMatthew Knepley /*@
5130e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
5131e5e52638SMatthew G. Knepley 
5132d083f849SBarry Smith   Collective on dm
5133e5e52638SMatthew G. Knepley 
5134e5e52638SMatthew G. Knepley   Input Parameter:
5135e5e52638SMatthew G. Knepley . dm - The DM
5136e5e52638SMatthew G. Knepley 
5137e5e52638SMatthew G. Knepley   Output Parameter:
5138e5e52638SMatthew G. Knepley . newdm - The DM
5139e5e52638SMatthew G. Knepley 
5140e5e52638SMatthew G. Knepley   Level: advanced
5141e5e52638SMatthew G. Knepley 
5142e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
5143e5e52638SMatthew G. Knepley @*/
5144e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
5145e5e52638SMatthew G. Knepley {
5146e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
5147e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5148e5e52638SMatthew G. Knepley 
5149e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5150e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5151e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5152e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
5153e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5154e5e52638SMatthew G. Knepley     DMLabel     label;
5155e5e52638SMatthew G. Knepley     PetscObject field;
515634aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5157e5e52638SMatthew G. Knepley 
5158e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
5159e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
516034aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
516134aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
516234aa8a36SMatthew G. Knepley   }
516334aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
516434aa8a36SMatthew G. Knepley }
516534aa8a36SMatthew G. Knepley 
516634aa8a36SMatthew G. Knepley /*@
516734aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
516834aa8a36SMatthew G. Knepley 
516934aa8a36SMatthew G. Knepley   Not collective
517034aa8a36SMatthew G. Knepley 
517134aa8a36SMatthew G. Knepley   Input Parameters:
517234aa8a36SMatthew G. Knepley + dm - The DM object
517334aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
517434aa8a36SMatthew G. Knepley 
5175d8d19677SJose E. Roman   Output Parameters:
517634aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
517734aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
517834aa8a36SMatthew G. Knepley 
517934aa8a36SMatthew G. Knepley   Notes:
518034aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
518134aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
518234aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5183979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
518434aa8a36SMatthew G. Knepley 
518534aa8a36SMatthew G. Knepley   Level: developer
518634aa8a36SMatthew G. Knepley 
518734aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
518834aa8a36SMatthew G. Knepley @*/
518934aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
519034aa8a36SMatthew G. Knepley {
519134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
519234aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5193534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
5194534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
519534aa8a36SMatthew G. Knepley   if (f < 0) {
519634aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
519734aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
519834aa8a36SMatthew G. Knepley   } else {
519934aa8a36SMatthew G. Knepley     PetscInt       Nf;
520034aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
520134aa8a36SMatthew G. Knepley 
520234aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5203*7a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
520434aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
520534aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
520634aa8a36SMatthew G. Knepley   }
520734aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
520834aa8a36SMatthew G. Knepley }
520934aa8a36SMatthew G. Knepley 
521034aa8a36SMatthew G. Knepley /*@
521134aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
521234aa8a36SMatthew G. Knepley 
521334aa8a36SMatthew G. Knepley   Not collective
521434aa8a36SMatthew G. Knepley 
521534aa8a36SMatthew G. Knepley   Input Parameters:
521634aa8a36SMatthew G. Knepley + dm         - The DM object
521734aa8a36SMatthew G. Knepley . f          - The field number
521834aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
521934aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
522034aa8a36SMatthew G. Knepley 
522134aa8a36SMatthew G. Knepley   Notes:
522234aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
522334aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
522434aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5225979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
522634aa8a36SMatthew G. Knepley 
522734aa8a36SMatthew G. Knepley   Level: developer
522834aa8a36SMatthew G. Knepley 
522934aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
523034aa8a36SMatthew G. Knepley @*/
523134aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
523234aa8a36SMatthew G. Knepley {
523334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
523434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
523534aa8a36SMatthew G. Knepley   if (f < 0) {
523634aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
523734aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
523834aa8a36SMatthew G. Knepley   } else {
523934aa8a36SMatthew G. Knepley     PetscInt       Nf;
524034aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
524134aa8a36SMatthew G. Knepley 
524234aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5243*7a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
524434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
524534aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5246e5e52638SMatthew G. Knepley   }
5247e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5248e5e52638SMatthew G. Knepley }
5249e5e52638SMatthew G. Knepley 
5250b0441da4SMatthew G. Knepley /*@
5251b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5252b0441da4SMatthew G. Knepley 
5253b0441da4SMatthew G. Knepley   Not collective
5254b0441da4SMatthew G. Knepley 
5255f899ff85SJose E. Roman   Input Parameter:
5256b0441da4SMatthew G. Knepley . dm - The DM object
5257b0441da4SMatthew G. Knepley 
5258d8d19677SJose E. Roman   Output Parameters:
5259b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5260b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5261b0441da4SMatthew G. Knepley 
5262b0441da4SMatthew G. Knepley   Notes:
5263b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5264b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5265b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5266b0441da4SMatthew G. Knepley 
5267b0441da4SMatthew G. Knepley   Level: developer
5268b0441da4SMatthew G. Knepley 
5269b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
5270b0441da4SMatthew G. Knepley @*/
5271b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5272b0441da4SMatthew G. Knepley {
5273b0441da4SMatthew G. Knepley   PetscInt       Nf;
5274b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5275b0441da4SMatthew G. Knepley 
5276b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5277b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5278064a246eSJacob Faibussowitsch   if (useCone)    PetscValidBoolPointer(useCone, 2);
5279064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
5280b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5281b0441da4SMatthew G. Knepley   if (!Nf) {
5282b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5283b0441da4SMatthew G. Knepley   } else {
5284b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5285b0441da4SMatthew G. Knepley   }
5286b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5287b0441da4SMatthew G. Knepley }
5288b0441da4SMatthew G. Knepley 
5289b0441da4SMatthew G. Knepley /*@
5290b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5291b0441da4SMatthew G. Knepley 
5292b0441da4SMatthew G. Knepley   Not collective
5293b0441da4SMatthew G. Knepley 
5294b0441da4SMatthew G. Knepley   Input Parameters:
5295b0441da4SMatthew G. Knepley + dm         - The DM object
5296b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5297b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5298b0441da4SMatthew G. Knepley 
5299b0441da4SMatthew G. Knepley   Notes:
5300b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5301b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5302b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5303b0441da4SMatthew G. Knepley 
5304b0441da4SMatthew G. Knepley   Level: developer
5305b0441da4SMatthew G. Knepley 
5306b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
5307b0441da4SMatthew G. Knepley @*/
5308b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5309b0441da4SMatthew G. Knepley {
5310b0441da4SMatthew G. Knepley   PetscInt       Nf;
5311b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5312b0441da4SMatthew G. Knepley 
5313b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5314b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5315b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5316b0441da4SMatthew G. Knepley   if (!Nf) {
5317b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5318b0441da4SMatthew G. Knepley   } else {
5319b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5320e5e52638SMatthew G. Knepley   }
5321e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5322e5e52638SMatthew G. Knepley }
5323e5e52638SMatthew G. Knepley 
5324783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
532545480ffeSMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, DMLabel label)
5326783e2ec8SMatthew G. Knepley {
5327783e2ec8SMatthew G. Knepley   PetscObject    obj;
5328783e2ec8SMatthew G. Knepley   PetscClassId   id;
5329783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
5330783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
5331783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
5332783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
5333783e2ec8SMatthew G. Knepley 
5334783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5335783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
5336783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5337783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
5338783e2ec8SMatthew G. Knepley   if (isFE && label) {
5339783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
5340783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5341783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
534245480ffeSMatthew G. Knepley       DMLabel l;
5343783e2ec8SMatthew G. Knepley 
534445480ffeSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, NULL, &l, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
534545480ffeSMatthew G. Knepley       duplicate = l == label ? PETSC_TRUE : PETSC_FALSE;
5346783e2ec8SMatthew G. Knepley       if (duplicate) break;
5347783e2ec8SMatthew G. Knepley     }
5348783e2ec8SMatthew G. Knepley     if (!duplicate) {
5349783e2ec8SMatthew G. Knepley       DM plex;
5350783e2ec8SMatthew G. Knepley 
5351783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
5352783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
5353783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
5354783e2ec8SMatthew G. Knepley     }
5355783e2ec8SMatthew G. Knepley   }
5356783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5357783e2ec8SMatthew G. Knepley }
5358783e2ec8SMatthew G. Knepley 
5359e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5360e5e52638SMatthew G. Knepley {
5361e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5362e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5363e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5364e5e52638SMatthew G. Knepley 
5365e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5366e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5367e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
5368e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5369b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5370e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5371e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5372e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5373e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5374e5e52638SMatthew G. Knepley }
5375e5e52638SMatthew G. Knepley 
5376e5e52638SMatthew G. Knepley /*@
5377e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5378e5e52638SMatthew G. Knepley 
5379e5e52638SMatthew G. Knepley   Not collective
5380e5e52638SMatthew G. Knepley 
5381e5e52638SMatthew G. Knepley   Input Parameter:
5382e5e52638SMatthew G. Knepley . dm - The DM
5383e5e52638SMatthew G. Knepley 
5384e5e52638SMatthew G. Knepley   Output Parameter:
5385e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5386e5e52638SMatthew G. Knepley 
5387e5e52638SMatthew G. Knepley   Level: intermediate
5388e5e52638SMatthew G. Knepley 
5389e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5390e5e52638SMatthew G. Knepley @*/
5391e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5392e5e52638SMatthew G. Knepley {
5393e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5394e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5395534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5396e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5397e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5398e5e52638SMatthew G. Knepley }
5399e5e52638SMatthew G. Knepley 
5400e5e52638SMatthew G. Knepley /*@
5401e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5402e5e52638SMatthew G. Knepley 
5403d083f849SBarry Smith   Logically collective on dm
5404e5e52638SMatthew G. Knepley 
5405e5e52638SMatthew G. Knepley   Input Parameter:
5406e5e52638SMatthew G. Knepley . dm - The DM
5407e5e52638SMatthew G. Knepley 
5408e5e52638SMatthew G. Knepley   Level: intermediate
5409e5e52638SMatthew G. Knepley 
5410e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5411e5e52638SMatthew G. Knepley @*/
5412e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5413e5e52638SMatthew G. Knepley {
5414e5e52638SMatthew G. Knepley   PetscInt       s;
5415e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5416e5e52638SMatthew G. Knepley 
5417e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5418e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5419e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5420e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5421e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5422b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5423e5e52638SMatthew G. Knepley   }
5424e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5425e5e52638SMatthew G. Knepley   dm->probs = NULL;
5426e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5427e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5428e5e52638SMatthew G. Knepley }
5429e5e52638SMatthew G. Knepley 
5430e5e52638SMatthew G. Knepley /*@
5431e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5432e5e52638SMatthew G. Knepley 
5433e5e52638SMatthew G. Knepley   Not collective
5434e5e52638SMatthew G. Knepley 
5435e5e52638SMatthew G. Knepley   Input Parameter:
5436e5e52638SMatthew G. Knepley . dm    - The DM
5437e5e52638SMatthew G. Knepley 
5438e5e52638SMatthew G. Knepley   Output Parameter:
5439e5e52638SMatthew G. Knepley . prob - The default PetscDS
5440e5e52638SMatthew G. Knepley 
5441e5e52638SMatthew G. Knepley   Level: intermediate
5442e5e52638SMatthew G. Knepley 
5443e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5444e5e52638SMatthew G. Knepley @*/
5445e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5446e5e52638SMatthew G. Knepley {
5447b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5448b0143b4dSMatthew G. Knepley 
5449e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5450e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5451e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5452b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5453b0143b4dSMatthew G. Knepley     PetscDS ds;
5454b0143b4dSMatthew G. Knepley 
545545480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5456b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5457b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5458b0143b4dSMatthew G. Knepley   }
5459b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5460e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5461e5e52638SMatthew G. Knepley }
5462e5e52638SMatthew G. Knepley 
5463e5e52638SMatthew G. Knepley /*@
5464e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5465e5e52638SMatthew G. Knepley 
5466e5e52638SMatthew G. Knepley   Not collective
5467e5e52638SMatthew G. Knepley 
5468e5e52638SMatthew G. Knepley   Input Parameters:
5469e5e52638SMatthew G. Knepley + dm    - The DM
5470e5e52638SMatthew G. Knepley - point - Cell for the DS
5471e5e52638SMatthew G. Knepley 
5472e5e52638SMatthew G. Knepley   Output Parameter:
5473e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5474e5e52638SMatthew G. Knepley 
5475e5e52638SMatthew G. Knepley   Level: developer
5476e5e52638SMatthew G. Knepley 
5477b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5478e5e52638SMatthew G. Knepley @*/
5479e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5480e5e52638SMatthew G. Knepley {
5481e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5482e5e52638SMatthew G. Knepley   PetscInt       s;
5483e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5484e5e52638SMatthew G. Knepley 
5485e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5486e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5487e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5488*7a8be351SBarry Smith   PetscCheck(point >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5489e5e52638SMatthew G. Knepley   *prob = NULL;
5490e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5491e5e52638SMatthew G. Knepley     PetscInt val;
5492e5e52638SMatthew G. Knepley 
5493e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5494e5e52638SMatthew G. Knepley     else {
5495e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5496e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5497e5e52638SMatthew G. Knepley     }
5498e5e52638SMatthew G. Knepley   }
5499e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5500e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5501e5e52638SMatthew G. Knepley }
5502e5e52638SMatthew G. Knepley 
5503e5e52638SMatthew G. Knepley /*@
5504e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5505e5e52638SMatthew G. Knepley 
5506e5e52638SMatthew G. Knepley   Not collective
5507e5e52638SMatthew G. Knepley 
5508e5e52638SMatthew G. Knepley   Input Parameters:
5509e5e52638SMatthew G. Knepley + dm    - The DM
5510e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5511e5e52638SMatthew G. Knepley 
5512b3cf3223SMatthew G. Knepley   Output Parameters:
5513b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5514b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5515e5e52638SMatthew G. Knepley 
5516154ca461SJed Brown   Note:
5517154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5518154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5519154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5520e5e52638SMatthew G. Knepley 
5521e5e52638SMatthew G. Knepley   Level: advanced
5522e5e52638SMatthew G. Knepley 
5523e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5524e5e52638SMatthew G. Knepley @*/
5525b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5526e5e52638SMatthew G. Knepley {
5527e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5528e5e52638SMatthew G. Knepley 
5529e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5530e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5531e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5532b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5533b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5534e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5535154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5536b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5537b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5538154ca461SJed Brown       if (dm->probs[s].label) PetscFunctionReturn(0);
5539b3cf3223SMatthew G. Knepley     }
5540e5e52638SMatthew G. Knepley   }
55412df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5542e5e52638SMatthew G. Knepley }
5543e5e52638SMatthew G. Knepley 
5544e5e52638SMatthew G. Knepley /*@
5545083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5546083401c6SMatthew G. Knepley 
5547083401c6SMatthew G. Knepley   Collective on dm
5548083401c6SMatthew G. Knepley 
5549083401c6SMatthew G. Knepley   Input Parameters:
5550083401c6SMatthew G. Knepley + dm     - The DM
5551083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5552083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5553083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5554083401c6SMatthew G. Knepley 
5555083401c6SMatthew 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,
5556083401c6SMatthew G. Knepley   the fields argument is ignored.
5557083401c6SMatthew G. Knepley 
5558083401c6SMatthew G. Knepley   Level: advanced
5559083401c6SMatthew G. Knepley 
5560083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5561083401c6SMatthew G. Knepley @*/
5562083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5563083401c6SMatthew G. Knepley {
5564083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5565083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5566083401c6SMatthew G. Knepley 
5567083401c6SMatthew G. Knepley   PetscFunctionBegin;
5568083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5569083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5570064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5571083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5572083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5573083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5574083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5575083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5576083401c6SMatthew G. Knepley     }
5577083401c6SMatthew G. Knepley   }
5578083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5579083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5580083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5581083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5582083401c6SMatthew G. Knepley   if (!label) {
5583083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5584083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5585083401c6SMatthew G. Knepley     Nds = 0;
5586083401c6SMatthew G. Knepley   }
5587083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5588083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5589083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5590083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5591083401c6SMatthew G. Knepley }
5592083401c6SMatthew G. Knepley 
5593083401c6SMatthew G. Knepley /*@
5594e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5595e5e52638SMatthew G. Knepley 
5596e5e52638SMatthew G. Knepley   Not collective
5597e5e52638SMatthew G. Knepley 
5598e5e52638SMatthew G. Knepley   Input Parameters:
5599e5e52638SMatthew G. Knepley + dm  - The DM
5600e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5601e5e52638SMatthew G. Knepley 
5602e5e52638SMatthew G. Knepley   Output Parameters:
5603b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5604b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5605083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5606e5e52638SMatthew G. Knepley 
5607e5e52638SMatthew G. Knepley   Level: advanced
5608e5e52638SMatthew G. Knepley 
5609e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5610e5e52638SMatthew G. Knepley @*/
5611b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5612e5e52638SMatthew G. Knepley {
5613e5e52638SMatthew G. Knepley   PetscInt       Nds;
5614e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5615e5e52638SMatthew G. Knepley 
5616e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5617e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5618e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5619*7a8be351SBarry Smith   PetscCheck((num >= 0) && (num < Nds),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
5620e5e52638SMatthew G. Knepley   if (label) {
5621e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5622e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5623e5e52638SMatthew G. Knepley   }
5624b3cf3223SMatthew G. Knepley   if (fields) {
5625b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5626b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5627b3cf3223SMatthew G. Knepley   }
5628e5e52638SMatthew G. Knepley   if (ds) {
5629b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5630e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5631e5e52638SMatthew G. Knepley   }
5632e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5633e5e52638SMatthew G. Knepley }
5634e5e52638SMatthew G. Knepley 
5635e5e52638SMatthew G. Knepley /*@
5636083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5637e5e52638SMatthew G. Knepley 
5638083401c6SMatthew G. Knepley   Not collective
5639e5e52638SMatthew G. Knepley 
5640e5e52638SMatthew G. Knepley   Input Parameters:
5641e5e52638SMatthew G. Knepley + dm     - The DM
5642083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5643083401c6SMatthew G. Knepley . label  - The region label, or NULL
5644083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5645083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5646e5e52638SMatthew G. Knepley 
5647e5e52638SMatthew G. Knepley   Level: advanced
5648e5e52638SMatthew G. Knepley 
5649083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5650e5e52638SMatthew G. Knepley @*/
5651083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5652e5e52638SMatthew G. Knepley {
5653083401c6SMatthew G. Knepley   PetscInt       Nds;
5654e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5655e5e52638SMatthew G. Knepley 
5656e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5657e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5658083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5659083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5660*7a8be351SBarry Smith   PetscCheck((num >= 0) && (num < Nds),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
5661e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5662083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5663083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5664083401c6SMatthew G. Knepley   if (fields) {
5665083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5666b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5667083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5668083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5669e5e52638SMatthew G. Knepley   }
5670083401c6SMatthew G. Knepley   if (ds) {
5671083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5672083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5673083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5674083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5675083401c6SMatthew G. Knepley   }
5676e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5677e5e52638SMatthew G. Knepley }
5678e5e52638SMatthew G. Knepley 
5679e5e52638SMatthew G. Knepley /*@
56801d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
56811d3af9e0SMatthew G. Knepley 
56821d3af9e0SMatthew G. Knepley   Not collective
56831d3af9e0SMatthew G. Knepley 
56841d3af9e0SMatthew G. Knepley   Input Parameters:
56851d3af9e0SMatthew G. Knepley + dm  - The DM
56861d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
56871d3af9e0SMatthew G. Knepley 
56881d3af9e0SMatthew G. Knepley   Output Parameter:
56891d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
56901d3af9e0SMatthew G. Knepley 
56911d3af9e0SMatthew G. Knepley   Level: advanced
56921d3af9e0SMatthew G. Knepley 
56931d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
56941d3af9e0SMatthew G. Knepley @*/
56951d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
56961d3af9e0SMatthew G. Knepley {
56971d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
56981d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
56991d3af9e0SMatthew G. Knepley 
57001d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
57011d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57021d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
57031d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
57041d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
57051d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
57061d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
57071d3af9e0SMatthew G. Knepley   else          *num = n;
57081d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
57091d3af9e0SMatthew G. Knepley }
57101d3af9e0SMatthew G. Knepley 
57112df84da0SMatthew G. Knepley /*@C
57122df84da0SMatthew G. Knepley   DMCreateFEDefault - Create a PetscFE based on the celltype for the mesh
57132df84da0SMatthew G. Knepley 
57142df84da0SMatthew G. Knepley   Not collective
57152df84da0SMatthew G. Knepley 
5716f1a722f8SMatthew G. Knepley   Input Parameters:
57172df84da0SMatthew G. Knepley + dm     - The DM
57182df84da0SMatthew G. Knepley . Nc     - The number of components for the field
57192df84da0SMatthew G. Knepley . prefix - The options prefix for the output PetscFE, or NULL
57202df84da0SMatthew G. Knepley - qorder - The quadrature order or PETSC_DETERMINE to use PetscSpace polynomial degree
57212df84da0SMatthew G. Knepley 
57222df84da0SMatthew G. Knepley   Output Parameter:
57232df84da0SMatthew G. Knepley . fem - The PetscFE
57242df84da0SMatthew G. Knepley 
57252df84da0SMatthew G. Knepley   Note: This is a convenience method that just calls PetscFECreateByCell() underneath.
57262df84da0SMatthew G. Knepley 
57272df84da0SMatthew G. Knepley   Level: intermediate
57282df84da0SMatthew G. Knepley 
57292df84da0SMatthew G. Knepley .seealso: PetscFECreateByCell(), DMAddField(), DMCreateDS(), DMGetCellDS(), DMGetRegionDS()
57302df84da0SMatthew G. Knepley @*/
57312df84da0SMatthew G. Knepley PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
57322df84da0SMatthew G. Knepley {
57332df84da0SMatthew G. Knepley   DMPolytopeType ct;
57342df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
57352df84da0SMatthew G. Knepley   PetscErrorCode ierr;
57362df84da0SMatthew G. Knepley 
57372df84da0SMatthew G. Knepley   PetscFunctionBegin;
57382df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57392df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
57402df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
57412df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
57422df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
57432df84da0SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
57442df84da0SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr);
57452df84da0SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
57462df84da0SMatthew G. Knepley   ierr = PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem);CHKERRQ(ierr);
57472df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
57482df84da0SMatthew G. Knepley }
57492df84da0SMatthew G. Knepley 
57501d3af9e0SMatthew G. Knepley /*@
5751e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5752e5e52638SMatthew G. Knepley 
5753d083f849SBarry Smith   Collective on dm
5754e5e52638SMatthew G. Knepley 
5755e5e52638SMatthew G. Knepley   Input Parameter:
5756e5e52638SMatthew G. Knepley . dm - The DM
5757e5e52638SMatthew G. Knepley 
575845480ffeSMatthew G. Knepley   Options Database Keys:
575945480ffeSMatthew G. Knepley . -dm_petscds_view - View all the PetscDS objects in this DM
576045480ffeSMatthew G. Knepley 
5761e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5762e5e52638SMatthew G. Knepley 
5763e5e52638SMatthew G. Knepley   Level: intermediate
5764e5e52638SMatthew G. Knepley 
5765e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5766e5e52638SMatthew G. Knepley @*/
5767e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5768e5e52638SMatthew G. Knepley {
5769e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5770083401c6SMatthew G. Knepley   PetscDS        dsDef;
5771083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5772f9244615SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5773f9244615SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE, flg;
5774e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5775e5e52638SMatthew G. Knepley 
5776e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5777e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5778e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5779e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5780083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5781083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5782083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5783083401c6SMatthew G. Knepley   Nl   = 0;
5784083401c6SMatthew G. Knepley   Ndef = 0;
5785083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5786083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5787083401c6SMatthew G. Knepley     PetscInt l;
5788083401c6SMatthew G. Knepley 
5789f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5790f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5791f918ec44SMatthew G. Knepley     {
5792f918ec44SMatthew G. Knepley       PetscClassId id;
5793f918ec44SMatthew G. Knepley 
5794f918ec44SMatthew G. Knepley       ierr = PetscObjectGetClassId(dm->fields[f].disc, &id);CHKERRQ(ierr);
5795f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5796f918ec44SMatthew G. Knepley         Ceed ceed;
5797f918ec44SMatthew G. Knepley 
5798f918ec44SMatthew G. Knepley         ierr = DMGetCeed(dm, &ceed);CHKERRQ(ierr);
5799f918ec44SMatthew G. Knepley         ierr = PetscFESetCeed((PetscFE) dm->fields[f].disc, ceed);CHKERRQ(ierr);
5800f918ec44SMatthew G. Knepley       }
5801f918ec44SMatthew G. Knepley     }
5802f918ec44SMatthew G. Knepley #endif
5803083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5804083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5805083401c6SMatthew G. Knepley     if (l < Nl) continue;
5806083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5807083401c6SMatthew G. Knepley   }
5808083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5809083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5810083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5811b3cf3223SMatthew G. Knepley     IS        fields;
5812b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5813b3cf3223SMatthew G. Knepley 
5814b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5815*7a8be351SBarry Smith     PetscCheck(nf,comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
5816b3cf3223SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5817b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
581888f0c812SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
581988f0c812SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
582088f0c812SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
582188f0c812SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
582288f0c812SMatthew G. Knepley 
582345480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5824083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5825083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5826b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
58272df9ee95SMatthew G. Knepley   }
5828083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5829083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5830083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5831083401c6SMatthew G. Knepley   if (Ndef && Nl) {
58320122748bSMatthew G. Knepley     DM              plex;
5833083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5834083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5835083401c6SMatthew G. Knepley     PetscInt       *fields;
5836083401c6SMatthew G. Knepley     const PetscInt *cells;
5837083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
58380122748bSMatthew G. Knepley 
58390122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
58400122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5841083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5842083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
58435fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5844083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5845083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5846083401c6SMatthew G. Knepley       IS      pointIS;
5847083401c6SMatthew G. Knepley 
5848083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5849083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5850083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5851083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5852083401c6SMatthew G. Knepley     }
5853083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5854083401c6SMatthew G. Knepley 
5855083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5856083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5857083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5858083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5859083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5860083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5861083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5862083401c6SMatthew G. Knepley 
5863083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5864083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5865083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5866083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5867083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5868083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5869083401c6SMatthew G. Knepley 
587045480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &dsDef);CHKERRQ(ierr);
5871083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5872083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
58735fedec97SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5874083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5875083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
58760122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5877083401c6SMatthew G. Knepley   }
5878083401c6SMatthew G. Knepley   /* Create label DSes
5879083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5880083401c6SMatthew G. Knepley   */
5881083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5882083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5883083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5884083401c6SMatthew G. Knepley     PetscDS   ds;
5885083401c6SMatthew G. Knepley     IS        fields;
5886083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5887083401c6SMatthew G. Knepley 
588845480ffeSMatthew G. Knepley     ierr = PetscDSCreate(PETSC_COMM_SELF, &ds);CHKERRQ(ierr);
5889083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5890083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5891083401c6SMatthew G. Knepley     for (f = 0, nf  = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5892083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5893083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5894083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5895083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5896083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5897083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5898083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5899083401c6SMatthew G. Knepley     {
5900083401c6SMatthew G. Knepley       DMPolytopeType ct;
5901083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
59025fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
59030122748bSMatthew G. Knepley 
5904e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5905665f567fSMatthew G. Knepley       if (lStart >= 0) {
5906412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5907412e9a14SMatthew G. Knepley         switch (ct) {
5908412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5909412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5910412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5911412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
59125fedec97SMatthew G. Knepley             isCohesiveLocal = PETSC_TRUE;break;
5913083401c6SMatthew G. Knepley           default: break;
5914412e9a14SMatthew G. Knepley         }
5915665f567fSMatthew G. Knepley       }
59165fedec97SMatthew G. Knepley       ierr = MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRMPI(ierr);
59175fedec97SMatthew G. Knepley       for (f = 0, nf  = 0; f < Nf; ++f) {
59185fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
59195fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
59205fedec97SMatthew G. Knepley             ierr = PetscDSSetDiscretization(ds, nf, NULL);CHKERRQ(ierr);
59215fedec97SMatthew G. Knepley             ierr = PetscDSSetCohesive(ds, nf, isCohesive);CHKERRQ(ierr);
59225fedec97SMatthew G. Knepley           }
59235fedec97SMatthew G. Knepley           ++nf;
59245fedec97SMatthew G. Knepley         }
59255fedec97SMatthew G. Knepley       }
5926e5e52638SMatthew G. Knepley     }
5927083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5928e5e52638SMatthew G. Knepley   }
5929083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5930e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5931083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5932083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5933083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5934083401c6SMatthew G. Knepley     const PetscInt *fld;
59355fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
59365fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5937e5e52638SMatthew G. Knepley 
59385fedec97SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsnf);CHKERRQ(ierr);
59395fedec97SMatthew G. Knepley     ierr = PetscDSIsCohesive(ds, &isCohesive);CHKERRQ(ierr);
5940083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5941083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5942083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5943083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
59445fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5945e5e52638SMatthew G. Knepley       PetscClassId id;
5946e5e52638SMatthew G. Knepley 
59475fedec97SMatthew G. Knepley       /* Handle DS with no fields */
59485fedec97SMatthew G. Knepley       if (dsnf) {ierr = PetscDSGetCohesive(ds, f, &isCohesiveField);CHKERRQ(ierr);}
59495fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
59505fedec97SMatthew G. Knepley       if (isCohesive && !isCohesiveField) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5951083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5952083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5953e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5954e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5955e5e52638SMatthew G. Knepley     }
5956083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5957e5e52638SMatthew G. Knepley   }
5958f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
5959f9244615SMatthew G. Knepley   ierr = PetscOptionsGetInt(NULL, ((PetscObject) dm)->prefix, "-dm_ds_jet_degree", &k, &flg);CHKERRQ(ierr);
5960f9244615SMatthew G. Knepley   if (flg) {
59613b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
59623b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
59633b4aee56SMatthew G. Knepley       PetscInt Nf, f;
59643b4aee56SMatthew G. Knepley 
59653b4aee56SMatthew G. Knepley       ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
59663b4aee56SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {ierr = PetscDSSetJetDegree(ds, f, k);CHKERRQ(ierr);}
59673b4aee56SMatthew G. Knepley     }
5968f9244615SMatthew G. Knepley   }
5969e5e52638SMatthew G. Knepley   /* Setup DSes */
5970e5e52638SMatthew G. Knepley   if (doSetup) {
5971e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5972e5e52638SMatthew G. Knepley   }
5973e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5974e5e52638SMatthew G. Knepley }
5975e5e52638SMatthew G. Knepley 
5976e5e52638SMatthew G. Knepley /*@
59777f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
59787f96f943SMatthew G. Knepley 
5979f2cacb80SMatthew G. Knepley   Collective on DM
5980f2cacb80SMatthew G. Knepley 
59817f96f943SMatthew G. Knepley   Input Parameters:
59827f96f943SMatthew G. Knepley + dm   - The DM
59837f96f943SMatthew G. Knepley - time - The time
59847f96f943SMatthew G. Knepley 
59857f96f943SMatthew G. Knepley   Output Parameters:
5986f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5987f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
59887f96f943SMatthew G. Knepley 
59897f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
59907f96f943SMatthew G. Knepley 
59917f96f943SMatthew G. Knepley   Level: developer
59927f96f943SMatthew G. Knepley 
59937f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
59947f96f943SMatthew G. Knepley @*/
5995f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
59967f96f943SMatthew G. Knepley {
59977f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
59987f96f943SMatthew G. Knepley   void            **ectxs;
59997f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
60007f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
60017f96f943SMatthew G. Knepley 
60027f96f943SMatthew G. Knepley   PetscFunctionBegin;
6003f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6004f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6005f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
60067f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
60077f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
60087f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
60097f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
60107f96f943SMatthew G. Knepley     PetscDS         ds;
60117f96f943SMatthew G. Knepley     DMLabel         label;
60127f96f943SMatthew G. Knepley     IS              fieldIS;
60137f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
60147f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
60157f96f943SMatthew G. Knepley 
60167f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
60177f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
60187f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
60197f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
60207f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
6021f2cacb80SMatthew G. Knepley     if (u) {
60227f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
60237f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
60247f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
60257f96f943SMatthew G. Knepley       }
60267f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
60277f96f943SMatthew G. Knepley       if (label) {
60287f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
60297f96f943SMatthew G. Knepley       } else {
60307f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
60317f96f943SMatthew G. Knepley       }
60327f96f943SMatthew G. Knepley     }
6033f2cacb80SMatthew G. Knepley     if (u_t) {
6034f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
6035f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
6036f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
6037f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
6038f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
6039f2cacb80SMatthew G. Knepley       }
6040f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
6041f2cacb80SMatthew G. Knepley       if (label) {
6042f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
6043f2cacb80SMatthew G. Knepley       } else {
6044f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
6045f2cacb80SMatthew G. Knepley       }
6046f2cacb80SMatthew G. Knepley     }
6047f2cacb80SMatthew G. Knepley   }
6048f2cacb80SMatthew G. Knepley   if (u) {
60497f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
60507f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
6051f2cacb80SMatthew G. Knepley   }
6052f2cacb80SMatthew G. Knepley   if (u_t) {
6053f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
6054f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
6055f2cacb80SMatthew G. Knepley   }
60567f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
60577f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
60587f96f943SMatthew G. Knepley }
60597f96f943SMatthew G. Knepley 
606045480ffeSMatthew G. Knepley PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
606145480ffeSMatthew G. Knepley {
606245480ffeSMatthew G. Knepley   PetscDS        dsNew;
606345480ffeSMatthew G. Knepley   DSBoundary     b;
606445480ffeSMatthew G. Knepley   PetscInt       cdim, Nf, f;
60655fedec97SMatthew G. Knepley   PetscBool      isCohesive;
606645480ffeSMatthew G. Knepley   void          *ctx;
606745480ffeSMatthew G. Knepley   PetscErrorCode ierr;
606845480ffeSMatthew G. Knepley 
606945480ffeSMatthew G. Knepley   PetscFunctionBegin;
607045480ffeSMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) ds), &dsNew);CHKERRQ(ierr);
607145480ffeSMatthew G. Knepley   ierr = PetscDSCopyConstants(ds, dsNew);CHKERRQ(ierr);
607245480ffeSMatthew G. Knepley   ierr = PetscDSCopyExactSolutions(ds, dsNew);CHKERRQ(ierr);
607345480ffeSMatthew G. Knepley   ierr = PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
607445480ffeSMatthew G. Knepley   ierr = PetscDSCopyEquations(ds, dsNew);CHKERRQ(ierr);
607545480ffeSMatthew G. Knepley   ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
607645480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
607745480ffeSMatthew G. Knepley     ierr = PetscDSGetContext(ds, f, &ctx);CHKERRQ(ierr);
607845480ffeSMatthew G. Knepley     ierr = PetscDSSetContext(dsNew, f, ctx);CHKERRQ(ierr);
60795fedec97SMatthew G. Knepley     ierr = PetscDSGetCohesive(ds, f, &isCohesive);CHKERRQ(ierr);
60805fedec97SMatthew G. Knepley     ierr = PetscDSSetCohesive(dsNew, f, isCohesive);CHKERRQ(ierr);
608145480ffeSMatthew G. Knepley   }
608245480ffeSMatthew G. Knepley   if (Nf) {
608345480ffeSMatthew G. Knepley     ierr = PetscDSGetCoordinateDimension(ds, &cdim);CHKERRQ(ierr);
608445480ffeSMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsNew, cdim);CHKERRQ(ierr);
608545480ffeSMatthew G. Knepley   }
608645480ffeSMatthew G. Knepley   ierr = PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew);CHKERRQ(ierr);
608745480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
608845480ffeSMatthew G. Knepley     ierr = DMGetLabel(dm, b->lname, &b->label);CHKERRQ(ierr);
608945480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
6090*7a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
609145480ffeSMatthew G. Knepley   }
609245480ffeSMatthew G. Knepley 
609345480ffeSMatthew G. Knepley   ierr = DMSetRegionDS(dm, label, fields, dsNew);CHKERRQ(ierr);
609445480ffeSMatthew G. Knepley   ierr = PetscDSDestroy(&dsNew);CHKERRQ(ierr);
609545480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
609645480ffeSMatthew G. Knepley }
609745480ffeSMatthew G. Knepley 
60987f96f943SMatthew G. Knepley /*@
6099e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
6100e5e52638SMatthew G. Knepley 
6101d083f849SBarry Smith   Collective on dm
6102e5e52638SMatthew G. Knepley 
6103e5e52638SMatthew G. Knepley   Input Parameter:
6104e5e52638SMatthew G. Knepley . dm - The DM
6105e5e52638SMatthew G. Knepley 
6106e5e52638SMatthew G. Knepley   Output Parameter:
6107e5e52638SMatthew G. Knepley . newdm - The DM
6108e5e52638SMatthew G. Knepley 
6109e5e52638SMatthew G. Knepley   Level: advanced
6110e5e52638SMatthew G. Knepley 
6111e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
6112e5e52638SMatthew G. Knepley @*/
6113e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
6114e5e52638SMatthew G. Knepley {
6115e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
6116e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
6117e5e52638SMatthew G. Knepley 
6118e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6119e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
6120e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
6121e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
6122e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6123e5e52638SMatthew G. Knepley     DMLabel  label;
6124b3cf3223SMatthew G. Knepley     IS       fields;
612545480ffeSMatthew G. Knepley     PetscDS  ds, newds;
6126783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6127e5e52638SMatthew G. Knepley 
6128b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
6129b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
613045480ffeSMatthew G. Knepley     ierr = DMTransferDS_Internal(newdm, label, fields, ds);CHKERRQ(ierr);
613145480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
613245480ffeSMatthew G. Knepley     ierr = DMGetRegionDS(newdm, label, NULL, &newds);CHKERRQ(ierr);
613345480ffeSMatthew G. Knepley     ierr = PetscDSGetNumBoundary(newds, &Nbd);CHKERRQ(ierr);
6134783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6135b8025e53SMatthew G. Knepley       PetscWeakForm wf;
613645480ffeSMatthew G. Knepley       DMLabel       label;
6137783e2ec8SMatthew G. Knepley       PetscInt      field;
6138783e2ec8SMatthew G. Knepley 
6139b8025e53SMatthew G. Knepley       ierr = PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
614045480ffeSMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, newds, field, bd, label);CHKERRQ(ierr);
6141b8025e53SMatthew G. Knepley       ierr = PetscWeakFormReplaceLabel(wf, label);CHKERRQ(ierr);
6142783e2ec8SMatthew G. Knepley     }
6143e5e52638SMatthew G. Knepley   }
6144e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6145e5e52638SMatthew G. Knepley }
6146e5e52638SMatthew G. Knepley 
6147e5e52638SMatthew G. Knepley /*@
6148e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
6149e5e52638SMatthew G. Knepley 
6150d083f849SBarry Smith   Collective on dm
6151e5e52638SMatthew G. Knepley 
6152e5e52638SMatthew G. Knepley   Input Parameter:
6153e5e52638SMatthew G. Knepley . dm - The DM
6154e5e52638SMatthew G. Knepley 
6155e5e52638SMatthew G. Knepley   Output Parameter:
6156e5e52638SMatthew G. Knepley . newdm - The DM
6157e5e52638SMatthew G. Knepley 
6158e5e52638SMatthew G. Knepley   Level: advanced
6159e5e52638SMatthew G. Knepley 
6160e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
6161e5e52638SMatthew G. Knepley @*/
6162e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6163e5e52638SMatthew G. Knepley {
6164e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
6165e5e52638SMatthew G. Knepley 
6166e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6167e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
6168e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
6169e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6170e5e52638SMatthew G. Knepley }
6171e5e52638SMatthew G. Knepley 
6172b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
6173b64e0483SPeter Brune {
6174b64e0483SPeter Brune   DM dm_coord,dmc_coord;
6175b64e0483SPeter Brune   PetscErrorCode ierr;
6176b64e0483SPeter Brune   Vec coords,ccoords;
61776dbf9973SLawrence Mitchell   Mat inject;
6178b64e0483SPeter Brune   PetscFunctionBegin;
6179b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
6180b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
6181b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
6182b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
6183b64e0483SPeter Brune   if (coords && !ccoords) {
6184b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
61856668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
61866dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
61872adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
61886dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
6189b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
6190b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
6191b64e0483SPeter Brune   }
6192b64e0483SPeter Brune   PetscFunctionReturn(0);
6193b64e0483SPeter Brune }
6194b64e0483SPeter Brune 
619503dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
619603dadc2fSPeter Brune {
619703dadc2fSPeter Brune   DM dm_coord,subdm_coord;
619803dadc2fSPeter Brune   PetscErrorCode ierr;
619903dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
620003dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
620103dadc2fSPeter Brune   PetscFunctionBegin;
620203dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
620303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
620403dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
620503dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
620603dadc2fSPeter Brune   if (coords && !ccoords) {
620703dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
62086668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
620903dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
621024640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
621103dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
621203dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
621303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
62141ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
621503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
621603dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
621703dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
621803dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
621903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
622003dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
622103dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
622203dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
622303dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
622403dadc2fSPeter Brune   }
622503dadc2fSPeter Brune   PetscFunctionReturn(0);
622603dadc2fSPeter Brune }
622703dadc2fSPeter Brune 
6228c73cfb54SMatthew G. Knepley /*@
6229c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
6230c73cfb54SMatthew G. Knepley 
6231c73cfb54SMatthew G. Knepley   Not collective
6232c73cfb54SMatthew G. Knepley 
6233c73cfb54SMatthew G. Knepley   Input Parameter:
6234c73cfb54SMatthew G. Knepley . dm - The DM
6235c73cfb54SMatthew G. Knepley 
6236c73cfb54SMatthew G. Knepley   Output Parameter:
6237c73cfb54SMatthew G. Knepley . dim - The topological dimension
6238c73cfb54SMatthew G. Knepley 
6239c73cfb54SMatthew G. Knepley   Level: beginner
6240c73cfb54SMatthew G. Knepley 
6241c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
6242c73cfb54SMatthew G. Knepley @*/
6243c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6244c73cfb54SMatthew G. Knepley {
6245c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6246c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6247534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6248c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6249c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6250c73cfb54SMatthew G. Knepley }
6251c73cfb54SMatthew G. Knepley 
6252c73cfb54SMatthew G. Knepley /*@
6253c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
6254c73cfb54SMatthew G. Knepley 
6255c73cfb54SMatthew G. Knepley   Collective on dm
6256c73cfb54SMatthew G. Knepley 
6257c73cfb54SMatthew G. Knepley   Input Parameters:
6258c73cfb54SMatthew G. Knepley + dm - The DM
6259c73cfb54SMatthew G. Knepley - dim - The topological dimension
6260c73cfb54SMatthew G. Knepley 
6261c73cfb54SMatthew G. Knepley   Level: beginner
6262c73cfb54SMatthew G. Knepley 
6263c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
6264c73cfb54SMatthew G. Knepley @*/
6265c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6266c73cfb54SMatthew G. Knepley {
6267e5e52638SMatthew G. Knepley   PetscDS        ds;
626845480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6269f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6270f17e8794SMatthew G. Knepley 
6271c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6272c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6273c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6274c73cfb54SMatthew G. Knepley   dm->dim = dim;
6275d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
627645480ffeSMatthew G. Knepley     ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
627745480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
627845480ffeSMatthew G. Knepley       ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
627945480ffeSMatthew G. Knepley       if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);}
628045480ffeSMatthew G. Knepley     }
6281d17bd122SMatthew G. Knepley   }
6282c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6283c73cfb54SMatthew G. Knepley }
6284c73cfb54SMatthew G. Knepley 
6285793f3fe5SMatthew G. Knepley /*@
6286793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6287793f3fe5SMatthew G. Knepley 
6288d083f849SBarry Smith   Collective on dm
6289793f3fe5SMatthew G. Knepley 
6290793f3fe5SMatthew G. Knepley   Input Parameters:
6291793f3fe5SMatthew G. Knepley + dm - the DM
6292793f3fe5SMatthew G. Knepley - dim - the dimension
6293793f3fe5SMatthew G. Knepley 
6294793f3fe5SMatthew G. Knepley   Output Parameters:
6295793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6296aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6297793f3fe5SMatthew G. Knepley 
6298793f3fe5SMatthew G. Knepley   Note:
6299793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6300a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6301793f3fe5SMatthew G. Knepley   then the interval is empty.
6302793f3fe5SMatthew G. Knepley 
6303793f3fe5SMatthew G. Knepley   Level: intermediate
6304793f3fe5SMatthew G. Knepley 
6305793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
6306793f3fe5SMatthew G. Knepley @*/
6307793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6308793f3fe5SMatthew G. Knepley {
6309793f3fe5SMatthew G. Knepley   PetscInt       d;
6310793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
6311793f3fe5SMatthew G. Knepley 
6312793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6313793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6314793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
6315*7a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6316*7a8be351SBarry Smith   PetscCheck(dm->ops->getdimpoints,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
6317793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
6318793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6319793f3fe5SMatthew G. Knepley }
6320793f3fe5SMatthew G. Knepley 
63216636e97aSMatthew G Knepley /*@
63226636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
63236636e97aSMatthew G Knepley 
6324d083f849SBarry Smith   Collective on dm
63256636e97aSMatthew G Knepley 
63266636e97aSMatthew G Knepley   Input Parameters:
63276636e97aSMatthew G Knepley + dm - the DM
63286636e97aSMatthew G Knepley - c - coordinate vector
63296636e97aSMatthew G Knepley 
63302dd40e9bSPatrick Sanan   Notes:
63312dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
63322dd40e9bSPatrick Sanan 
63332dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
63346636e97aSMatthew G Knepley 
63356636e97aSMatthew G Knepley   Level: intermediate
63366636e97aSMatthew G Knepley 
633760c22052SBarry Smith .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
63386636e97aSMatthew G Knepley @*/
63396636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
63406636e97aSMatthew G Knepley {
63416636e97aSMatthew G Knepley   PetscErrorCode ierr;
63426636e97aSMatthew G Knepley 
63436636e97aSMatthew G Knepley   PetscFunctionBegin;
63446636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63456636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
63466636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
63476636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
63486636e97aSMatthew G Knepley   dm->coordinates = c;
63496636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
6350b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
635103dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
63526636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63536636e97aSMatthew G Knepley }
63546636e97aSMatthew G Knepley 
63556636e97aSMatthew G Knepley /*@
63566636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
63576636e97aSMatthew G Knepley 
63587058e716SVaclav Hapla   Not collective
63596636e97aSMatthew G Knepley 
63606636e97aSMatthew G Knepley    Input Parameters:
63616636e97aSMatthew G Knepley +  dm - the DM
63626636e97aSMatthew G Knepley -  c - coordinate vector
63636636e97aSMatthew G Knepley 
63642dd40e9bSPatrick Sanan   Notes:
63656636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
63666636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
63676636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
63686636e97aSMatthew G Knepley 
63692dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
63702dd40e9bSPatrick Sanan 
63716636e97aSMatthew G Knepley   Level: intermediate
63726636e97aSMatthew G Knepley 
63736636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
63746636e97aSMatthew G Knepley @*/
63756636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
63766636e97aSMatthew G Knepley {
63776636e97aSMatthew G Knepley   PetscErrorCode ierr;
63786636e97aSMatthew G Knepley 
63796636e97aSMatthew G Knepley   PetscFunctionBegin;
63806636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6381b7f5c055SJed Brown   if (c) PetscValidHeaderSpecific(c,VEC_CLASSID,2);
63826636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
63836636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
63848865f1eaSKarl Rupp 
63856636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
63868865f1eaSKarl Rupp 
63876636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
63886636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63896636e97aSMatthew G Knepley }
63906636e97aSMatthew G Knepley 
63916636e97aSMatthew G Knepley /*@
63926636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
63936636e97aSMatthew G Knepley 
6394d083f849SBarry Smith   Collective on dm
63956636e97aSMatthew G Knepley 
63966636e97aSMatthew G Knepley   Input Parameter:
63976636e97aSMatthew G Knepley . dm - the DM
63986636e97aSMatthew G Knepley 
63996636e97aSMatthew G Knepley   Output Parameter:
64006636e97aSMatthew G Knepley . c - global coordinate vector
64016636e97aSMatthew G Knepley 
64026636e97aSMatthew G Knepley   Note:
640360c22052SBarry Smith   This is a borrowed reference, so the user should NOT destroy this vector. When the DM is
640460c22052SBarry Smith   destroyed the array will no longer be valid.
64056636e97aSMatthew G Knepley 
6406959c7569SPatrick Sanan   Each process has only the locally-owned portion of the global coordinates (does NOT have the ghost coordinates).
64076636e97aSMatthew G Knepley 
64086636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
64096636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
64106636e97aSMatthew G Knepley 
64116636e97aSMatthew G Knepley   Level: intermediate
64126636e97aSMatthew G Knepley 
641360c22052SBarry Smith .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
64146636e97aSMatthew G Knepley @*/
64156636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
64166636e97aSMatthew G Knepley {
64176636e97aSMatthew G Knepley   PetscErrorCode ierr;
64186636e97aSMatthew G Knepley 
64196636e97aSMatthew G Knepley   PetscFunctionBegin;
64206636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64216636e97aSMatthew G Knepley   PetscValidPointer(c,2);
64221f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
64230298fd71SBarry Smith     DM        cdm = NULL;
64241970a576SMatthew G. Knepley     PetscBool localized;
64256636e97aSMatthew G Knepley 
64266636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
64276636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
64281970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
64291970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
64301970a576SMatthew G. Knepley     if (localized) {
64311970a576SMatthew G. Knepley       PetscInt cdim;
64321970a576SMatthew G. Knepley 
64331970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
64341970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
64351970a576SMatthew G. Knepley     }
64366636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
64376636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
64386636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
64396636e97aSMatthew G Knepley   }
64406636e97aSMatthew G Knepley   *c = dm->coordinates;
64416636e97aSMatthew G Knepley   PetscFunctionReturn(0);
64426636e97aSMatthew G Knepley }
64436636e97aSMatthew G Knepley 
64446636e97aSMatthew G Knepley /*@
644581e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
644681e9a530SVaclav Hapla 
6447d083f849SBarry Smith   Collective on dm
644881e9a530SVaclav Hapla 
644981e9a530SVaclav Hapla   Input Parameter:
645081e9a530SVaclav Hapla . dm - the DM
645181e9a530SVaclav Hapla 
645281e9a530SVaclav Hapla   Level: advanced
645381e9a530SVaclav Hapla 
645481e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
645581e9a530SVaclav Hapla @*/
645681e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
645781e9a530SVaclav Hapla {
645881e9a530SVaclav Hapla   PetscErrorCode ierr;
645981e9a530SVaclav Hapla 
646081e9a530SVaclav Hapla   PetscFunctionBegin;
646181e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
646281e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
64631970a576SMatthew G. Knepley     DM        cdm = NULL;
64641970a576SMatthew G. Knepley     PetscBool localized;
64651970a576SMatthew G. Knepley 
646681e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
646781e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
64681970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
64691970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
64701970a576SMatthew G. Knepley     if (localized) {
64711970a576SMatthew G. Knepley       PetscInt cdim;
64721970a576SMatthew G. Knepley 
64731970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
64741970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
64751970a576SMatthew G. Knepley     }
647681e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
647781e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
647881e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
647981e9a530SVaclav Hapla   }
648081e9a530SVaclav Hapla   PetscFunctionReturn(0);
648181e9a530SVaclav Hapla }
648281e9a530SVaclav Hapla 
648381e9a530SVaclav Hapla /*@
64846636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
64856636e97aSMatthew G Knepley 
6486d083f849SBarry Smith   Collective on dm
64876636e97aSMatthew G Knepley 
64886636e97aSMatthew G Knepley   Input Parameter:
64896636e97aSMatthew G Knepley . dm - the DM
64906636e97aSMatthew G Knepley 
64916636e97aSMatthew G Knepley   Output Parameter:
64926636e97aSMatthew G Knepley . c - coordinate vector
64936636e97aSMatthew G Knepley 
64946636e97aSMatthew G Knepley   Note:
64956636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
64966636e97aSMatthew G Knepley 
64976636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
64986636e97aSMatthew G Knepley 
64996636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
65006636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
65016636e97aSMatthew G Knepley 
65026636e97aSMatthew G Knepley   Level: intermediate
65036636e97aSMatthew G Knepley 
650481e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
65056636e97aSMatthew G Knepley @*/
65066636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
65076636e97aSMatthew G Knepley {
65086636e97aSMatthew G Knepley   PetscErrorCode ierr;
65096636e97aSMatthew G Knepley 
65106636e97aSMatthew G Knepley   PetscFunctionBegin;
65116636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65126636e97aSMatthew G Knepley   PetscValidPointer(c,2);
651381e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
65146636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
65156636e97aSMatthew G Knepley   PetscFunctionReturn(0);
65166636e97aSMatthew G Knepley }
65176636e97aSMatthew G Knepley 
651881e9a530SVaclav Hapla /*@
651981e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
652081e9a530SVaclav Hapla 
652181e9a530SVaclav Hapla   Not collective
652281e9a530SVaclav Hapla 
652381e9a530SVaclav Hapla   Input Parameter:
652481e9a530SVaclav Hapla . dm - the DM
652581e9a530SVaclav Hapla 
652681e9a530SVaclav Hapla   Output Parameter:
652781e9a530SVaclav Hapla . c - coordinate vector
652881e9a530SVaclav Hapla 
652981e9a530SVaclav Hapla   Level: advanced
653081e9a530SVaclav Hapla 
653181e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
653281e9a530SVaclav Hapla @*/
653381e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
653481e9a530SVaclav Hapla {
653581e9a530SVaclav Hapla   PetscFunctionBegin;
653681e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
653781e9a530SVaclav Hapla   PetscValidPointer(c,2);
6538*7a8be351SBarry Smith   PetscCheck(dm->coordinatesLocal || !dm->coordinates,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
65396636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
65406636e97aSMatthew G Knepley   PetscFunctionReturn(0);
65416636e97aSMatthew G Knepley }
65426636e97aSMatthew G Knepley 
65432db98f8dSVaclav Hapla /*@
65442db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
65452db98f8dSVaclav Hapla 
65462db98f8dSVaclav Hapla   Not collective
65472db98f8dSVaclav Hapla 
6548d8d19677SJose E. Roman   Input Parameters:
65492db98f8dSVaclav Hapla + dm - the DM
65502db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
65512db98f8dSVaclav Hapla 
6552d8d19677SJose E. Roman   Output Parameters:
65532db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
65542db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
65552db98f8dSVaclav Hapla 
65562db98f8dSVaclav Hapla   Note:
65572db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
65582db98f8dSVaclav Hapla 
65592db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
65602db98f8dSVaclav Hapla 
65612db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
65622db98f8dSVaclav Hapla 
65632db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
65642db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
65652db98f8dSVaclav Hapla 
65662db98f8dSVaclav Hapla   Level: advanced
65672db98f8dSVaclav Hapla 
65682db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
65692db98f8dSVaclav Hapla @*/
65702db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
65712db98f8dSVaclav Hapla {
65722db98f8dSVaclav Hapla   PetscSection        cs, newcs;
65732db98f8dSVaclav Hapla   Vec                 coords;
65742db98f8dSVaclav Hapla   const PetscScalar   *arr;
65752db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
65762db98f8dSVaclav Hapla   PetscInt            n;
65772db98f8dSVaclav Hapla   PetscErrorCode      ierr;
65782db98f8dSVaclav Hapla 
65792db98f8dSVaclav Hapla   PetscFunctionBegin;
65802db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65812db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
65822db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
65832db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
6584*7a8be351SBarry Smith   PetscCheck(dm->coordinatesLocal,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
6585*7a8be351SBarry Smith   PetscCheck(dm->coordinateDM && dm->coordinateDM->localSection,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
65861bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
65872db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
65882db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
65892db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
65902db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
65912db98f8dSVaclav Hapla   if (pCoord) {
65922db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
65932db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
65942db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
65952db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6596ad9ac99dSVaclav Hapla   } else {
6597ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
65982db98f8dSVaclav Hapla   }
6599ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6600ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
66012db98f8dSVaclav Hapla   PetscFunctionReturn(0);
66022db98f8dSVaclav Hapla }
66032db98f8dSVaclav Hapla 
6604f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6605f19dbd58SToby Isaac {
6606f19dbd58SToby Isaac   PetscErrorCode ierr;
6607f19dbd58SToby Isaac 
6608f19dbd58SToby Isaac   PetscFunctionBegin;
6609f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6610f19dbd58SToby Isaac   PetscValidPointer(field,2);
6611f19dbd58SToby Isaac   if (!dm->coordinateField) {
6612f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6613f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6614f19dbd58SToby Isaac     }
6615f19dbd58SToby Isaac   }
6616f19dbd58SToby Isaac   *field = dm->coordinateField;
6617f19dbd58SToby Isaac   PetscFunctionReturn(0);
6618f19dbd58SToby Isaac }
6619f19dbd58SToby Isaac 
6620f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6621f19dbd58SToby Isaac {
6622f19dbd58SToby Isaac   PetscErrorCode ierr;
6623f19dbd58SToby Isaac 
6624f19dbd58SToby Isaac   PetscFunctionBegin;
6625f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6626f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6627c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6628f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6629f19dbd58SToby Isaac   dm->coordinateField = field;
6630f19dbd58SToby Isaac   PetscFunctionReturn(0);
6631f19dbd58SToby Isaac }
6632f19dbd58SToby Isaac 
66336636e97aSMatthew G Knepley /*@
66341cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
66356636e97aSMatthew G Knepley 
6636d083f849SBarry Smith   Collective on dm
66376636e97aSMatthew G Knepley 
66386636e97aSMatthew G Knepley   Input Parameter:
66396636e97aSMatthew G Knepley . dm - the DM
66406636e97aSMatthew G Knepley 
66416636e97aSMatthew G Knepley   Output Parameter:
66426636e97aSMatthew G Knepley . cdm - coordinate DM
66436636e97aSMatthew G Knepley 
66446636e97aSMatthew G Knepley   Level: intermediate
66456636e97aSMatthew G Knepley 
66461cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
66476636e97aSMatthew G Knepley @*/
66486636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
66496636e97aSMatthew G Knepley {
66506636e97aSMatthew G Knepley   PetscErrorCode ierr;
66516636e97aSMatthew G Knepley 
66526636e97aSMatthew G Knepley   PetscFunctionBegin;
66536636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
66546636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
66556636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6656308f8a94SToby Isaac     DM cdm;
6657308f8a94SToby Isaac 
6658*7a8be351SBarry Smith     PetscCheck(dm->ops->createcoordinatedm,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6659308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6660c9ad657eSksagiyam     ierr = PetscObjectSetName((PetscObject)cdm, "coordinateDM");CHKERRQ(ierr);
6661308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6662308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6663308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6664308f8a94SToby Isaac     dm->coordinateDM = cdm;
66656636e97aSMatthew G Knepley   }
66666636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
66676636e97aSMatthew G Knepley   PetscFunctionReturn(0);
66686636e97aSMatthew G Knepley }
6669e87bb0d3SMatthew G Knepley 
66701cfe2091SMatthew G. Knepley /*@
66711cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
66721cfe2091SMatthew G. Knepley 
6673d083f849SBarry Smith   Logically Collective on dm
66741cfe2091SMatthew G. Knepley 
66751cfe2091SMatthew G. Knepley   Input Parameters:
66761cfe2091SMatthew G. Knepley + dm - the DM
66771cfe2091SMatthew G. Knepley - cdm - coordinate DM
66781cfe2091SMatthew G. Knepley 
66791cfe2091SMatthew G. Knepley   Level: intermediate
66801cfe2091SMatthew G. Knepley 
66811cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
66821cfe2091SMatthew G. Knepley @*/
66831cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
66841cfe2091SMatthew G. Knepley {
66851cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
66861cfe2091SMatthew G. Knepley 
66871cfe2091SMatthew G. Knepley   PetscFunctionBegin;
66881cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
66891cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6690f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
66911cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
66921cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
66931cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
66941cfe2091SMatthew G. Knepley }
66951cfe2091SMatthew G. Knepley 
669646e270d4SMatthew G. Knepley /*@
669746e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
669846e270d4SMatthew G. Knepley 
669946e270d4SMatthew G. Knepley   Not Collective
670046e270d4SMatthew G. Knepley 
670146e270d4SMatthew G. Knepley   Input Parameter:
670246e270d4SMatthew G. Knepley . dm - The DM object
670346e270d4SMatthew G. Knepley 
670446e270d4SMatthew G. Knepley   Output Parameter:
670546e270d4SMatthew G. Knepley . dim - The embedding dimension
670646e270d4SMatthew G. Knepley 
670746e270d4SMatthew G. Knepley   Level: intermediate
670846e270d4SMatthew G. Knepley 
670992fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
671046e270d4SMatthew G. Knepley @*/
671146e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
671246e270d4SMatthew G. Knepley {
671346e270d4SMatthew G. Knepley   PetscFunctionBegin;
671446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6715534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
67169a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
67179a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
67189a9a41abSToby Isaac   }
671946e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
672046e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
672146e270d4SMatthew G. Knepley }
672246e270d4SMatthew G. Knepley 
672346e270d4SMatthew G. Knepley /*@
672446e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
672546e270d4SMatthew G. Knepley 
672646e270d4SMatthew G. Knepley   Not Collective
672746e270d4SMatthew G. Knepley 
672846e270d4SMatthew G. Knepley   Input Parameters:
672946e270d4SMatthew G. Knepley + dm  - The DM object
673046e270d4SMatthew G. Knepley - dim - The embedding dimension
673146e270d4SMatthew G. Knepley 
673246e270d4SMatthew G. Knepley   Level: intermediate
673346e270d4SMatthew G. Knepley 
673492fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
673546e270d4SMatthew G. Knepley @*/
673646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
673746e270d4SMatthew G. Knepley {
6738e5e52638SMatthew G. Knepley   PetscDS        ds;
673945480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6740f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6741f17e8794SMatthew G. Knepley 
674246e270d4SMatthew G. Knepley   PetscFunctionBegin;
674346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
674446e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6745d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
674645480ffeSMatthew G. Knepley     ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
674745480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
674845480ffeSMatthew G. Knepley       ierr = DMGetRegionNumDS(dm, n, NULL, NULL, &ds);CHKERRQ(ierr);
6749e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
675045480ffeSMatthew G. Knepley     }
6751d17bd122SMatthew G. Knepley   }
675246e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
675346e270d4SMatthew G. Knepley }
675446e270d4SMatthew G. Knepley 
6755e8abe2deSMatthew G. Knepley /*@
6756e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6757e8abe2deSMatthew G. Knepley 
6758d083f849SBarry Smith   Collective on dm
6759e8abe2deSMatthew G. Knepley 
6760e8abe2deSMatthew G. Knepley   Input Parameter:
6761e8abe2deSMatthew G. Knepley . dm - The DM object
6762e8abe2deSMatthew G. Knepley 
6763e8abe2deSMatthew G. Knepley   Output Parameter:
6764e8abe2deSMatthew G. Knepley . section - The PetscSection object
6765e8abe2deSMatthew G. Knepley 
6766e8abe2deSMatthew G. Knepley   Level: intermediate
6767e8abe2deSMatthew G. Knepley 
676892fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6769e8abe2deSMatthew G. Knepley @*/
6770e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6771e8abe2deSMatthew G. Knepley {
6772e8abe2deSMatthew G. Knepley   DM             cdm;
6773e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6774e8abe2deSMatthew G. Knepley 
6775e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6776e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6777e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6778e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
677992fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6780e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6781e8abe2deSMatthew G. Knepley }
6782e8abe2deSMatthew G. Knepley 
6783e8abe2deSMatthew G. Knepley /*@
6784e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6785e8abe2deSMatthew G. Knepley 
6786e8abe2deSMatthew G. Knepley   Not Collective
6787e8abe2deSMatthew G. Knepley 
6788e8abe2deSMatthew G. Knepley   Input Parameters:
6789e8abe2deSMatthew G. Knepley + dm      - The DM object
679046e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6791e8abe2deSMatthew G. Knepley - section - The PetscSection object
6792e8abe2deSMatthew G. Knepley 
6793e8abe2deSMatthew G. Knepley   Level: intermediate
6794e8abe2deSMatthew G. Knepley 
679592fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6796e8abe2deSMatthew G. Knepley @*/
679746e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6798e8abe2deSMatthew G. Knepley {
6799e8abe2deSMatthew G. Knepley   DM             cdm;
6800e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6801e8abe2deSMatthew G. Knepley 
6802e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6803e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
680446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6805e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
680692fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
680746e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
68084c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
680946e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
681046e270d4SMatthew G. Knepley 
681146e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
681246e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
681346e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
681446e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
681546e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
681646e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
681746e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
681846e270d4SMatthew G. Knepley     }
6819ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
682046e270d4SMatthew G. Knepley   }
6821e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6822e8abe2deSMatthew G. Knepley }
6823e8abe2deSMatthew G. Knepley 
6824d864a3eaSLisandro Dalcin /*@
6825d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6826d864a3eaSLisandro Dalcin 
6827d864a3eaSLisandro Dalcin   Input Parameters:
6828d864a3eaSLisandro Dalcin + dm      - The DM object
68294f9ab2b4SJed Brown - disc    - The new coordinate discretization or NULL to ensure a coordinate discretization exists
6830d864a3eaSLisandro Dalcin 
6831d864a3eaSLisandro Dalcin   Level: intermediate
6832d864a3eaSLisandro Dalcin 
6833d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6834d864a3eaSLisandro Dalcin @*/
6835d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6836d864a3eaSLisandro Dalcin {
68374f9ab2b4SJed Brown   PetscFE        discOld;
6838d864a3eaSLisandro Dalcin   PetscClassId   classid;
6839d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6840d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6841d864a3eaSLisandro Dalcin   Mat            matInterp;
68424f9ab2b4SJed Brown   PetscBool      same_space = PETSC_TRUE;
6843d864a3eaSLisandro Dalcin   PetscErrorCode ierr;
6844d864a3eaSLisandro Dalcin 
6845d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6846d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
68474f9ab2b4SJed Brown   if (disc) PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6848d864a3eaSLisandro Dalcin 
6849d864a3eaSLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdmOld);CHKERRQ(ierr);
6850d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
68514f9ab2b4SJed Brown   ierr = DMGetField(cdmOld, 0, NULL, (PetscObject*)&discOld);CHKERRQ(ierr);
68524f9ab2b4SJed Brown   ierr = PetscObjectGetClassId((PetscObject)discOld, &classid);CHKERRQ(ierr);
685329ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
685429ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
685529ad44c5SMatthew G. Knepley       PetscFE        feLinear;
685629ad44c5SMatthew G. Knepley       DMPolytopeType ct;
685729ad44c5SMatthew G. Knepley       PetscInt       dim, dE, cStart;
685829ad44c5SMatthew G. Knepley       PetscBool      simplex;
685929ad44c5SMatthew G. Knepley 
686029ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
686129ad44c5SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
686229ad44c5SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
686329ad44c5SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdmOld, 0, &cStart, NULL);CHKERRQ(ierr);
686429ad44c5SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
686529ad44c5SMatthew G. Knepley       switch (ct) {
686629ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM:
686729ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
686829ad44c5SMatthew G. Knepley           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
686929ad44c5SMatthew G. Knepley         default: break;
687029ad44c5SMatthew G. Knepley       }
687129ad44c5SMatthew G. Knepley       simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
687229ad44c5SMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear);CHKERRQ(ierr);
687329ad44c5SMatthew G. Knepley       ierr = DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear);CHKERRQ(ierr);
687429ad44c5SMatthew G. Knepley       ierr = PetscFEDestroy(&feLinear);CHKERRQ(ierr);
687529ad44c5SMatthew G. Knepley       ierr = DMCreateDS(cdmOld);CHKERRQ(ierr);
68764f9ab2b4SJed Brown       ierr = DMGetField(cdmOld, 0, NULL, (PetscObject*)&discOld);CHKERRQ(ierr);
687729ad44c5SMatthew G. Knepley     } else {
687829ad44c5SMatthew G. Knepley       const char *discname;
687929ad44c5SMatthew G. Knepley 
68804f9ab2b4SJed Brown       ierr = PetscObjectGetType((PetscObject)discOld, &discname);CHKERRQ(ierr);
68814f9ab2b4SJed Brown       SETERRQ(PetscObjectComm((PetscObject)discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
688229ad44c5SMatthew G. Knepley     }
688329ad44c5SMatthew G. Knepley   }
68844f9ab2b4SJed Brown   if (!disc) PetscFunctionReturn(0);
68854f9ab2b4SJed Brown   { // Check if the new space is the same as the old modulo quadrature
68864f9ab2b4SJed Brown     PetscDualSpace dsOld, ds;
68874f9ab2b4SJed Brown     ierr = PetscFEGetDualSpace(discOld, &dsOld);CHKERRQ(ierr);
68884f9ab2b4SJed Brown     ierr = PetscFEGetDualSpace(disc, &ds);CHKERRQ(ierr);
68894f9ab2b4SJed Brown     ierr = PetscDualSpaceEqual(dsOld, ds, &same_space);CHKERRQ(ierr);
68904f9ab2b4SJed Brown   }
6891d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6892d864a3eaSLisandro Dalcin   ierr = DMClone(cdmOld, &cdmNew);CHKERRQ(ierr);
6893d864a3eaSLisandro Dalcin   ierr = DMSetField(cdmNew, 0, NULL, (PetscObject) disc);CHKERRQ(ierr);
6894d864a3eaSLisandro Dalcin   ierr = DMCreateDS(cdmNew);CHKERRQ(ierr);
6895d864a3eaSLisandro Dalcin   ierr = DMGetCoordinates(dm, &coordsOld);CHKERRQ(ierr);
68964f9ab2b4SJed Brown   if (same_space) {
68974f9ab2b4SJed Brown     ierr = PetscObjectReference((PetscObject)coordsOld);CHKERRQ(ierr);
68984f9ab2b4SJed Brown     coordsNew = coordsOld;
68994f9ab2b4SJed Brown   } else { // Project the coordinate vector from old to new space
6900d864a3eaSLisandro Dalcin     ierr = DMCreateGlobalVector(cdmNew, &coordsNew);CHKERRQ(ierr);
6901d864a3eaSLisandro Dalcin     ierr = DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL);CHKERRQ(ierr);
6902d864a3eaSLisandro Dalcin     ierr = MatInterpolate(matInterp, coordsOld, coordsNew);CHKERRQ(ierr);
6903d864a3eaSLisandro Dalcin     ierr = MatDestroy(&matInterp);CHKERRQ(ierr);
69044f9ab2b4SJed Brown   }
6905d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6906d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateField(dm, NULL);CHKERRQ(ierr);
6907d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateDM(dm, cdmNew);CHKERRQ(ierr);
6908d864a3eaSLisandro Dalcin   ierr = DMSetCoordinates(dm, coordsNew);CHKERRQ(ierr);
6909d864a3eaSLisandro Dalcin   ierr = VecDestroy(&coordsNew);CHKERRQ(ierr);
6910d864a3eaSLisandro Dalcin   ierr = DMDestroy(&cdmNew);CHKERRQ(ierr);
6911d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6912d864a3eaSLisandro Dalcin }
6913d864a3eaSLisandro Dalcin 
69145dc8c3f7SMatthew G. Knepley /*@C
691590b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
69165dc8c3f7SMatthew G. Knepley 
6917f899ff85SJose E. Roman   Input Parameter:
691890b157c4SStefano Zampini . dm      - The DM object
691990b157c4SStefano Zampini 
692090b157c4SStefano Zampini   Output Parameters:
692190b157c4SStefano Zampini + per     - Whether the DM is periodic or not
69225dc8c3f7SMatthew 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
69235dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
69245dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
69255dc8c3f7SMatthew G. Knepley 
69265dc8c3f7SMatthew G. Knepley   Level: developer
69275dc8c3f7SMatthew G. Knepley 
69285dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
69295dc8c3f7SMatthew G. Knepley @*/
693090b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6931c6b900c6SMatthew G. Knepley {
6932c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6933c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
693490b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6935c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6936c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
69375dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6938c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6939c6b900c6SMatthew G. Knepley }
6940c6b900c6SMatthew G. Knepley 
69415dc8c3f7SMatthew G. Knepley /*@C
69425dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
69435dc8c3f7SMatthew G. Knepley 
69445dc8c3f7SMatthew G. Knepley   Input Parameters:
69455dc8c3f7SMatthew G. Knepley + dm      - The DM object
6946db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6947db2bf62eSStefano 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.
69485dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
69495dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
69505dc8c3f7SMatthew G. Knepley 
6951db2bf62eSStefano 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.
6952db2bf62eSStefano Zampini 
69535dc8c3f7SMatthew G. Knepley   Level: developer
69545dc8c3f7SMatthew G. Knepley 
69555dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
69565dc8c3f7SMatthew G. Knepley @*/
695790b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6958c6b900c6SMatthew G. Knepley {
6959c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6960c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6961c6b900c6SMatthew G. Knepley 
6962c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6963c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
696490b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6965412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6966412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6967412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
69685dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
696990b157c4SStefano Zampini   if (maxCell) {
6970412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6971412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6972db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6973db2bf62eSStefano Zampini     ierr = PetscFree(dm->maxCell);CHKERRQ(ierr);
6974412e9a14SMatthew G. Knepley   }
6975db2bf62eSStefano Zampini 
6976412e9a14SMatthew G. Knepley   if (L) {
6977412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6978412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6979412e9a14SMatthew G. Knepley   }
6980412e9a14SMatthew G. Knepley   if (bd) {
6981412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6982412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
698390b157c4SStefano Zampini   }
6984072d7d67SStefano Zampini   dm->periodic = per;
6985c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6986c6b900c6SMatthew G. Knepley }
6987c6b900c6SMatthew G. Knepley 
69882e17dfb7SMatthew G. Knepley /*@
69892e17dfb7SMatthew 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.
69902e17dfb7SMatthew G. Knepley 
69912e17dfb7SMatthew G. Knepley   Input Parameters:
69922e17dfb7SMatthew G. Knepley + dm     - The DM
699365da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
699465da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
69952e17dfb7SMatthew G. Knepley 
69962e17dfb7SMatthew G. Knepley   Output Parameter:
69972e17dfb7SMatthew G. Knepley . out - The localized coordinate point
69982e17dfb7SMatthew G. Knepley 
69992e17dfb7SMatthew G. Knepley   Level: developer
70002e17dfb7SMatthew G. Knepley 
70012e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
70022e17dfb7SMatthew G. Knepley @*/
700365da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
70042e17dfb7SMatthew G. Knepley {
70052e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
70062e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
70072e17dfb7SMatthew G. Knepley 
70082e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
70092e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
70102e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
70112e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
70122e17dfb7SMatthew G. Knepley   } else {
701365da65dcSMatthew G. Knepley     if (endpoint) {
701465da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
7015da3333bfSMatthew 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)) {
7016da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
701765da65dcSMatthew G. Knepley         } else {
7018da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
701965da65dcSMatthew G. Knepley         }
702065da65dcSMatthew G. Knepley       }
702165da65dcSMatthew G. Knepley     } else {
70222e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
70231118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
70242e17dfb7SMatthew G. Knepley       }
70252e17dfb7SMatthew G. Knepley     }
702665da65dcSMatthew G. Knepley   }
70272e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
70282e17dfb7SMatthew G. Knepley }
70292e17dfb7SMatthew G. Knepley 
70302e17dfb7SMatthew G. Knepley /*
70312e17dfb7SMatthew 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.
70322e17dfb7SMatthew G. Knepley 
70332e17dfb7SMatthew G. Knepley   Input Parameters:
70342e17dfb7SMatthew G. Knepley + dm     - The DM
70352e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
70362e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
70372e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
70382e17dfb7SMatthew G. Knepley 
70392e17dfb7SMatthew G. Knepley   Output Parameter:
70402e17dfb7SMatthew G. Knepley . out - The localized coordinate point
70412e17dfb7SMatthew G. Knepley 
70422e17dfb7SMatthew G. Knepley   Level: developer
70432e17dfb7SMatthew G. Knepley 
70442e17dfb7SMatthew 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
70452e17dfb7SMatthew G. Knepley 
70462e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
70472e17dfb7SMatthew G. Knepley */
70482e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
70492e17dfb7SMatthew G. Knepley {
70502e17dfb7SMatthew G. Knepley   PetscInt d;
70512e17dfb7SMatthew G. Knepley 
70522e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
70532e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
70542e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
70552e17dfb7SMatthew G. Knepley   } else {
70562e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
7057908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
70582e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
70592e17dfb7SMatthew G. Knepley       } else {
70602e17dfb7SMatthew G. Knepley         out[d] = in[d];
70612e17dfb7SMatthew G. Knepley       }
70622e17dfb7SMatthew G. Knepley     }
70632e17dfb7SMatthew G. Knepley   }
70642e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
70652e17dfb7SMatthew G. Knepley }
7066a5801f52SStefano Zampini 
70672e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
70682e17dfb7SMatthew G. Knepley {
70692e17dfb7SMatthew G. Knepley   PetscInt d;
70702e17dfb7SMatthew G. Knepley 
70712e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
70722e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
70732e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
70742e17dfb7SMatthew G. Knepley   } else {
70752e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
7076908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
70772e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
70782e17dfb7SMatthew G. Knepley       } else {
70792e17dfb7SMatthew G. Knepley         out[d] = in[d];
70802e17dfb7SMatthew G. Knepley       }
70812e17dfb7SMatthew G. Knepley     }
70822e17dfb7SMatthew G. Knepley   }
70832e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
70842e17dfb7SMatthew G. Knepley }
70852e17dfb7SMatthew G. Knepley 
70862e17dfb7SMatthew G. Knepley /*
70872e17dfb7SMatthew 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.
70882e17dfb7SMatthew G. Knepley 
70892e17dfb7SMatthew G. Knepley   Input Parameters:
70902e17dfb7SMatthew G. Knepley + dm     - The DM
70912e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
70922e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
70932e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
70942e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
70952e17dfb7SMatthew G. Knepley 
70962e17dfb7SMatthew G. Knepley   Output Parameter:
70972e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
70982e17dfb7SMatthew G. Knepley 
70992e17dfb7SMatthew G. Knepley   Level: developer
71002e17dfb7SMatthew G. Knepley 
71012e17dfb7SMatthew 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
71022e17dfb7SMatthew G. Knepley 
71032e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
71042e17dfb7SMatthew G. Knepley */
71052e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
71062e17dfb7SMatthew G. Knepley {
71072e17dfb7SMatthew G. Knepley   PetscInt d;
71082e17dfb7SMatthew G. Knepley 
71092e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
71102e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
71112e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
71122e17dfb7SMatthew G. Knepley   } else {
71132e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
7114412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
7115412e9a14SMatthew G. Knepley 
7116412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
7117412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
7118412e9a14SMatthew G. Knepley 
7119412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
712098921bdaSJacob Faibussowitsch           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%D-Coordinate %g more than %g away from anchor %g", d, (double) PetscRealPart(in[d]), (double) maxC, (double) PetscRealPart(anchor[d]));
7121412e9a14SMatthew G. Knepley         out[d] += newCoord;
71222e17dfb7SMatthew G. Knepley       } else {
71232e17dfb7SMatthew G. Knepley         out[d] += in[d];
71242e17dfb7SMatthew G. Knepley       }
71252e17dfb7SMatthew G. Knepley     }
71262e17dfb7SMatthew G. Knepley   }
71272e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
71282e17dfb7SMatthew G. Knepley }
71292e17dfb7SMatthew G. Knepley 
713036447a5eSToby Isaac /*@
71318f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
71328f700142SStefano Zampini 
71338f700142SStefano Zampini   Not collective
713436447a5eSToby Isaac 
713536447a5eSToby Isaac   Input Parameter:
713636447a5eSToby Isaac . dm - The DM
713736447a5eSToby Isaac 
713836447a5eSToby Isaac   Output Parameter:
713936447a5eSToby Isaac   areLocalized - True if localized
714036447a5eSToby Isaac 
714136447a5eSToby Isaac   Level: developer
714236447a5eSToby Isaac 
71438f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
714436447a5eSToby Isaac @*/
71458f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
714636447a5eSToby Isaac {
714736447a5eSToby Isaac   DM             cdm;
714836447a5eSToby Isaac   PetscSection   coordSection;
71492ccac677SMatthew G. Knepley   PetscInt       depth, cStart, cEnd, sStart, sEnd, c, dof;
715046a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
715136447a5eSToby Isaac   PetscErrorCode ierr;
715236447a5eSToby Isaac 
715336447a5eSToby Isaac   PetscFunctionBegin;
715436447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7155534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
71568b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
715746a3a80fSLisandro Dalcin 
715836447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
715936447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
716046a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
71619f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
71622ccac677SMatthew G. Knepley   ierr = DMPlexGetDepth(cdm, &depth);CHKERRQ(ierr);
71632ccac677SMatthew G. Knepley   if (!depth) PetscFunctionReturn(0);
716446a3a80fSLisandro Dalcin 
71659f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
716646a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
716736447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
716836447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
716946a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
717046a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
717136447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
717246a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
717336447a5eSToby Isaac   }
71748f700142SStefano Zampini   *areLocalized = alreadyLocalized;
717536447a5eSToby Isaac   PetscFunctionReturn(0);
717636447a5eSToby Isaac }
717736447a5eSToby Isaac 
71788f700142SStefano Zampini /*@
71798f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
71808f700142SStefano Zampini 
71818f700142SStefano Zampini   Collective on dm
71828f700142SStefano Zampini 
71838f700142SStefano Zampini   Input Parameter:
71848f700142SStefano Zampini . dm - The DM
71858f700142SStefano Zampini 
71868f700142SStefano Zampini   Output Parameter:
71878f700142SStefano Zampini   areLocalized - True if localized
71888f700142SStefano Zampini 
71898f700142SStefano Zampini   Level: developer
71908f700142SStefano Zampini 
71918f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
71928f700142SStefano Zampini @*/
71938f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
71948f700142SStefano Zampini {
71958f700142SStefano Zampini   PetscBool      localized;
71968f700142SStefano Zampini   PetscErrorCode ierr;
71978f700142SStefano Zampini 
71988f700142SStefano Zampini   PetscFunctionBegin;
71998f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7200534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
72018f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
7202820f2d46SBarry Smith   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
72038f700142SStefano Zampini   PetscFunctionReturn(0);
72048f700142SStefano Zampini }
720536447a5eSToby Isaac 
72062e17dfb7SMatthew G. Knepley /*@
7207492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
72082e17dfb7SMatthew G. Knepley 
72098f700142SStefano Zampini   Collective on dm
72108f700142SStefano Zampini 
72112e17dfb7SMatthew G. Knepley   Input Parameter:
72122e17dfb7SMatthew G. Knepley . dm - The DM
72132e17dfb7SMatthew G. Knepley 
72142e17dfb7SMatthew G. Knepley   Level: developer
72152e17dfb7SMatthew G. Knepley 
72168f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
72172e17dfb7SMatthew G. Knepley @*/
72182e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
72192e17dfb7SMatthew G. Knepley {
72202e17dfb7SMatthew G. Knepley   DM             cdm;
72212e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
72222e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
72233e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
72243e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
7225e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
72263e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
72273e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
72282e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
72292e17dfb7SMatthew G. Knepley 
72302e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
72312e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
723292c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
7233f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
7234f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
7235f7cbd40bSStefano Zampini 
72362e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
72372e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
72382e17dfb7SMatthew G. Knepley   {
72392e17dfb7SMatthew G. Knepley     PetscBool isplex;
72402e17dfb7SMatthew G. Knepley 
72412e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
72422e17dfb7SMatthew G. Knepley     if (isplex) {
72432e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
72443e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
724569291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
72463e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
72473e922f36SToby Isaac       newStart = vStart;
72483e922f36SToby Isaac       newEnd   = vEnd;
72493e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
72503e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
72513e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
72523e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
72533e922f36SToby Isaac       }
72542e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
72552e17dfb7SMatthew G. Knepley   }
72562e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
7257*7a8be351SBarry Smith   PetscCheck(coordinates,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
72582e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
72593e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
7260e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
72613e922f36SToby Isaac 
72622e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
72632e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
72642e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
72652e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
72663e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
72673e922f36SToby Isaac 
726869291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
72693e922f36SToby Isaac   localized = &anchor[bs];
72703e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
72713e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
72723e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
72733e922f36SToby Isaac 
72743e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
72753e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
72763e922f36SToby Isaac       PetscInt     b;
72773e922f36SToby Isaac 
72783e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
72793e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
72803e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
72813e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
72823e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
72833e922f36SToby Isaac         for (b = 0; b < bs; b++) {
72843e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
72853e922f36SToby Isaac         }
72863e922f36SToby Isaac         if (b < bs) break;
72873e922f36SToby Isaac       }
72883e922f36SToby Isaac       if (d < dof/bs) {
72893e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
72903e922f36SToby Isaac           PetscInt cdof;
72913e922f36SToby Isaac 
72923e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
72933e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
72943e922f36SToby Isaac         }
72953e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
72963e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
72973e922f36SToby Isaac       }
72983e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
72993e922f36SToby Isaac     }
73003e922f36SToby Isaac   }
7301ffc4695bSBarry Smith   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
73023e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
730369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
73043e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
730569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
73063e922f36SToby Isaac     PetscFunctionReturn(0);
73073e922f36SToby Isaac   }
73082e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
73092e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
73102e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
73112e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
73122e17dfb7SMatthew G. Knepley   }
73132e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
73142e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
7315c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
73162e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
73172e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
73182e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
73192e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
7320c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
73212e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
73222e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
73232e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
73242e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
73252e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
73262e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
73272e17dfb7SMatthew G. Knepley   }
73283e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
73293e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
73303e922f36SToby Isaac 
73312e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
73322e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
73333e922f36SToby Isaac       PetscInt     b, cdof;
73342e17dfb7SMatthew G. Knepley 
73353e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
73363e922f36SToby Isaac       if (!cdof) continue;
73372e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
73382e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
73392e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
73402e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
73412e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
73422e17dfb7SMatthew G. Knepley     }
73433e922f36SToby Isaac   }
734469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
734569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
7346c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
73472e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
73482e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
73492e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
73502e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
73512e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
73522e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
73532e17dfb7SMatthew G. Knepley }
73542e17dfb7SMatthew G. Knepley 
7355e87bb0d3SMatthew G Knepley /*@
73563a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
7357e87bb0d3SMatthew G Knepley 
7358d083f849SBarry Smith   Collective on v (see explanation below)
7359e87bb0d3SMatthew G Knepley 
7360e87bb0d3SMatthew G Knepley   Input Parameters:
7361e87bb0d3SMatthew G Knepley + dm - The DM
73626b867d5aSJose E. Roman - ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
7363e87bb0d3SMatthew G Knepley 
73646b867d5aSJose E. Roman   Input/Output Parameters:
73656b867d5aSJose E. Roman + v - The Vec of points, on output contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
73666b867d5aSJose E. Roman - cellSF - Points to either NULL, or a PetscSF with guesses for which cells contain each point;
73676b867d5aSJose E. Roman            on output, the PetscSF containing the ranks and local indices of the containing points
73683a93e3b7SToby Isaac 
7369e87bb0d3SMatthew G Knepley   Level: developer
737061e3bb9bSMatthew G Knepley 
737162a38674SMatthew G. Knepley   Notes:
73723a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
737362a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
73743a93e3b7SToby Isaac 
73753a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
737662a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
73773a93e3b7SToby Isaac 
73783a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
73793a93e3b7SToby Isaac 
738062a38674SMatthew G. Knepley $    const PetscSFNode *cells;
738162a38674SMatthew G. Knepley $    PetscInt           nFound;
7382a6216909SToby Isaac $    const PetscInt    *found;
738362a38674SMatthew G. Knepley $
7384a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
73853a93e3b7SToby Isaac 
73863a93e3b7SToby 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
73873a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
73883a93e3b7SToby Isaac 
738962a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
739061e3bb9bSMatthew G Knepley @*/
739162a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
7392e87bb0d3SMatthew G Knepley {
7393735aa83eSMatthew G Knepley   PetscErrorCode ierr;
7394735aa83eSMatthew G Knepley 
7395e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
7396e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7397e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
7398e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
73993a93e3b7SToby Isaac   if (*cellSF) {
74003a93e3b7SToby Isaac     PetscMPIInt result;
74013a93e3b7SToby Isaac 
7402e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
7403ffc4695bSBarry Smith     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRMPI(ierr);
7404*7a8be351SBarry Smith     PetscCheck(result == MPI_IDENT || result == MPI_CONGRUENT,PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"cellSF must have a communicator congruent to v's");
7405e0fc9d1bSMatthew G. Knepley   } else {
74063a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
74073a93e3b7SToby Isaac   }
7408*7a8be351SBarry Smith   PetscCheck(dm->ops->locatepoints,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
740947a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
741062a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
741147a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
7412e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
7413e87bb0d3SMatthew G Knepley }
741414f150ffSMatthew G. Knepley 
7415f4d763aaSMatthew G. Knepley /*@
7416f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
7417f4d763aaSMatthew G. Knepley 
74188f700142SStefano Zampini   Collective on dm
74198f700142SStefano Zampini 
7420f4d763aaSMatthew G. Knepley   Input Parameter:
7421f4d763aaSMatthew G. Knepley . dm - The original DM
7422f4d763aaSMatthew G. Knepley 
7423f4d763aaSMatthew G. Knepley   Output Parameter:
7424f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
7425f4d763aaSMatthew G. Knepley 
7426f4d763aaSMatthew G. Knepley   Level: intermediate
7427f4d763aaSMatthew G. Knepley 
7428e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
7429f4d763aaSMatthew G. Knepley @*/
743014f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
743114f150ffSMatthew G. Knepley {
7432c26acbdeSMatthew G. Knepley   PetscSection   section;
74332d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
743414f150ffSMatthew G. Knepley   PetscErrorCode ierr;
743514f150ffSMatthew G. Knepley 
743614f150ffSMatthew G. Knepley   PetscFunctionBegin;
743714f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
743814f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
743992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
7440c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
7441ffc4695bSBarry Smith   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
74422d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
7443c26acbdeSMatthew G. Knepley     *odm = dm;
7444c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
7445c26acbdeSMatthew G. Knepley   }
744614f150ffSMatthew G. Knepley   if (!dm->dmBC) {
7447c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
744814f150ffSMatthew G. Knepley     PetscSF      sf;
744914f150ffSMatthew G. Knepley 
745014f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
7451e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
745214f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
745392fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
745414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
745514f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
745615b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
7457e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
745814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
745914f150ffSMatthew G. Knepley   }
746014f150ffSMatthew G. Knepley   *odm = dm->dmBC;
746114f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
746214f150ffSMatthew G. Knepley }
7463f4d763aaSMatthew G. Knepley 
7464f4d763aaSMatthew G. Knepley /*@
7465cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7466f4d763aaSMatthew G. Knepley 
7467f4d763aaSMatthew G. Knepley   Input Parameter:
7468f4d763aaSMatthew G. Knepley . dm - The original DM
7469f4d763aaSMatthew G. Knepley 
7470cdb7a50dSMatthew G. Knepley   Output Parameters:
7471cdb7a50dSMatthew G. Knepley + num - The output sequence number
7472cdb7a50dSMatthew G. Knepley - val - The output sequence value
7473f4d763aaSMatthew G. Knepley 
7474f4d763aaSMatthew G. Knepley   Level: intermediate
7475f4d763aaSMatthew G. Knepley 
7476f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7477f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7478f4d763aaSMatthew G. Knepley 
7479f4d763aaSMatthew G. Knepley .seealso: VecView()
7480f4d763aaSMatthew G. Knepley @*/
7481cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7482f4d763aaSMatthew G. Knepley {
7483f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7484f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7485534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7486534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7487f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7488f4d763aaSMatthew G. Knepley }
7489f4d763aaSMatthew G. Knepley 
7490f4d763aaSMatthew G. Knepley /*@
7491cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7492f4d763aaSMatthew G. Knepley 
7493f4d763aaSMatthew G. Knepley   Input Parameters:
7494f4d763aaSMatthew G. Knepley + dm - The original DM
7495cdb7a50dSMatthew G. Knepley . num - The output sequence number
7496cdb7a50dSMatthew G. Knepley - val - The output sequence value
7497f4d763aaSMatthew G. Knepley 
7498f4d763aaSMatthew G. Knepley   Level: intermediate
7499f4d763aaSMatthew G. Knepley 
7500f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7501f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7502f4d763aaSMatthew G. Knepley 
7503f4d763aaSMatthew G. Knepley .seealso: VecView()
7504f4d763aaSMatthew G. Knepley @*/
7505cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7506f4d763aaSMatthew G. Knepley {
7507f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7508f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7509f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7510cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7511cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7512cdb7a50dSMatthew G. Knepley }
7513cdb7a50dSMatthew G. Knepley 
7514cdb7a50dSMatthew G. Knepley /*@C
7515cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7516cdb7a50dSMatthew G. Knepley 
7517cdb7a50dSMatthew G. Knepley   Input Parameters:
7518cdb7a50dSMatthew G. Knepley + dm   - The original DM
7519cdb7a50dSMatthew G. Knepley . name - The sequence name
7520cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7521cdb7a50dSMatthew G. Knepley 
7522cdb7a50dSMatthew G. Knepley   Output Parameter:
7523cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7524cdb7a50dSMatthew G. Knepley 
7525cdb7a50dSMatthew G. Knepley   Level: intermediate
7526cdb7a50dSMatthew G. Knepley 
7527cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7528cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7529cdb7a50dSMatthew G. Knepley 
7530cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7531cdb7a50dSMatthew G. Knepley @*/
7532cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7533cdb7a50dSMatthew G. Knepley {
7534cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7535cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
7536cdb7a50dSMatthew G. Knepley 
7537cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7538cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7539cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7540064a246eSJacob Faibussowitsch   PetscValidRealPointer(val,5);
7541cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
7542cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7543cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7544cdb7a50dSMatthew G. Knepley     PetscScalar value;
7545cdb7a50dSMatthew G. Knepley 
754639d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
75474aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7548cdb7a50dSMatthew G. Knepley #endif
7549cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7550f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7551f4d763aaSMatthew G. Knepley }
75528e4ac7eaSMatthew G. Knepley 
75538e4ac7eaSMatthew G. Knepley /*@
75548e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
75558e4ac7eaSMatthew G. Knepley 
75568e4ac7eaSMatthew G. Knepley   Not collective
75578e4ac7eaSMatthew G. Knepley 
75588e4ac7eaSMatthew G. Knepley   Input Parameter:
75598e4ac7eaSMatthew G. Knepley . dm - The DM
75608e4ac7eaSMatthew G. Knepley 
75618e4ac7eaSMatthew G. Knepley   Output Parameter:
75628e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
75638e4ac7eaSMatthew G. Knepley 
75648e4ac7eaSMatthew G. Knepley   Level: beginner
75658e4ac7eaSMatthew G. Knepley 
75668e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
75678e4ac7eaSMatthew G. Knepley @*/
75688e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
75698e4ac7eaSMatthew G. Knepley {
75708e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
75718e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7572534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
75738e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
75748e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
75758e4ac7eaSMatthew G. Knepley }
75768e4ac7eaSMatthew G. Knepley 
75778e4ac7eaSMatthew G. Knepley /*@
75785d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
75798e4ac7eaSMatthew G. Knepley 
75808e4ac7eaSMatthew G. Knepley   Collective on dm
75818e4ac7eaSMatthew G. Knepley 
75828e4ac7eaSMatthew G. Knepley   Input Parameters:
75838e4ac7eaSMatthew G. Knepley + dm - The DM
75848e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
75858e4ac7eaSMatthew G. Knepley 
75865d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
75875d3b26e6SMatthew G. Knepley 
75888e4ac7eaSMatthew G. Knepley   Level: beginner
75898e4ac7eaSMatthew G. Knepley 
75905d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
75918e4ac7eaSMatthew G. Knepley @*/
75928e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
75938e4ac7eaSMatthew G. Knepley {
75948e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
75958e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75968833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
75978e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
75988e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
75998e4ac7eaSMatthew G. Knepley }
7600c58f1c22SToby Isaac 
7601c58f1c22SToby Isaac /*@C
7602c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7603c58f1c22SToby Isaac 
7604c58f1c22SToby Isaac   Not Collective
7605c58f1c22SToby Isaac 
7606c58f1c22SToby Isaac   Input Parameters:
7607c58f1c22SToby Isaac + dm   - The DM object
7608c58f1c22SToby Isaac - name - The label name
7609c58f1c22SToby Isaac 
7610c58f1c22SToby Isaac   Level: intermediate
7611c58f1c22SToby Isaac 
7612c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7613c58f1c22SToby Isaac @*/
7614c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7615c58f1c22SToby Isaac {
76165d80c0bfSVaclav Hapla   PetscBool      flg;
76175d80c0bfSVaclav Hapla   DMLabel        label;
7618c58f1c22SToby Isaac   PetscErrorCode ierr;
7619c58f1c22SToby Isaac 
7620c58f1c22SToby Isaac   PetscFunctionBegin;
7621c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7622c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
76235d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7624c58f1c22SToby Isaac   if (!flg) {
76255d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
76265d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
76275d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7628c58f1c22SToby Isaac   }
7629c58f1c22SToby Isaac   PetscFunctionReturn(0);
7630c58f1c22SToby Isaac }
7631c58f1c22SToby Isaac 
7632c58f1c22SToby Isaac /*@C
76330fdc7489SMatthew Knepley   DMCreateLabelAtIndex - Create a label of the given name at the iven index. If it already exists, move it to this index.
76340fdc7489SMatthew Knepley 
76350fdc7489SMatthew Knepley   Not Collective
76360fdc7489SMatthew Knepley 
76370fdc7489SMatthew Knepley   Input Parameters:
76380fdc7489SMatthew Knepley + dm   - The DM object
76390fdc7489SMatthew Knepley . l    - The index for the label
76400fdc7489SMatthew Knepley - name - The label name
76410fdc7489SMatthew Knepley 
76420fdc7489SMatthew Knepley   Level: intermediate
76430fdc7489SMatthew Knepley 
76440fdc7489SMatthew Knepley .seealso: DMCreateLabel(), DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
76450fdc7489SMatthew Knepley @*/
76460fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
76470fdc7489SMatthew Knepley {
76480fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
76490fdc7489SMatthew Knepley   DMLabel        label;
76500fdc7489SMatthew Knepley   PetscInt       Nl, m;
76510fdc7489SMatthew Knepley   PetscBool      flg, match;
76520fdc7489SMatthew Knepley   const char    *lname;
76530fdc7489SMatthew Knepley   PetscErrorCode ierr;
76540fdc7489SMatthew Knepley 
76550fdc7489SMatthew Knepley   PetscFunctionBegin;
76560fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7657064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
76580fdc7489SMatthew Knepley   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
76590fdc7489SMatthew Knepley   if (!flg) {
76600fdc7489SMatthew Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
76610fdc7489SMatthew Knepley     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
76620fdc7489SMatthew Knepley     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
76630fdc7489SMatthew Knepley   }
76640fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
7665*7a8be351SBarry Smith   PetscCheck(l < Nl,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %D must be in [0, %D)", l, Nl);
76660fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
76670fdc7489SMatthew Knepley     ierr = PetscObjectGetName((PetscObject) orig->label, &lname);CHKERRQ(ierr);
76680fdc7489SMatthew Knepley     ierr = PetscStrcmp(name, lname, &match);CHKERRQ(ierr);
76690fdc7489SMatthew Knepley     if (match) break;
76700fdc7489SMatthew Knepley   }
76710fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
76720fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
76730fdc7489SMatthew Knepley   else    prev->next = orig->next;
76740fdc7489SMatthew Knepley   if (!l) {
76750fdc7489SMatthew Knepley     orig->next = dm->labels;
76760fdc7489SMatthew Knepley     dm->labels = orig;
76770fdc7489SMatthew Knepley   } else {
76780fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
76790fdc7489SMatthew Knepley     orig->next = prev->next;
76800fdc7489SMatthew Knepley     prev->next = orig;
76810fdc7489SMatthew Knepley   }
76820fdc7489SMatthew Knepley   PetscFunctionReturn(0);
76830fdc7489SMatthew Knepley }
76840fdc7489SMatthew Knepley 
76850fdc7489SMatthew Knepley /*@C
7686c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7687c58f1c22SToby Isaac 
7688c58f1c22SToby Isaac   Not Collective
7689c58f1c22SToby Isaac 
7690c58f1c22SToby Isaac   Input Parameters:
7691c58f1c22SToby Isaac + dm   - The DM object
7692c58f1c22SToby Isaac . name - The label name
7693c58f1c22SToby Isaac - point - The mesh point
7694c58f1c22SToby Isaac 
7695c58f1c22SToby Isaac   Output Parameter:
7696c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7697c58f1c22SToby Isaac 
7698c58f1c22SToby Isaac   Level: beginner
7699c58f1c22SToby Isaac 
7700c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7701c58f1c22SToby Isaac @*/
7702c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7703c58f1c22SToby Isaac {
7704c58f1c22SToby Isaac   DMLabel        label;
7705c58f1c22SToby Isaac   PetscErrorCode ierr;
7706c58f1c22SToby Isaac 
7707c58f1c22SToby Isaac   PetscFunctionBegin;
7708c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7709c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7710c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7711*7a8be351SBarry Smith   PetscCheck(label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7712c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7713c58f1c22SToby Isaac   PetscFunctionReturn(0);
7714c58f1c22SToby Isaac }
7715c58f1c22SToby Isaac 
7716c58f1c22SToby Isaac /*@C
7717c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7718c58f1c22SToby Isaac 
7719c58f1c22SToby Isaac   Not Collective
7720c58f1c22SToby Isaac 
7721c58f1c22SToby Isaac   Input Parameters:
7722c58f1c22SToby Isaac + dm   - The DM object
7723c58f1c22SToby Isaac . name - The label name
7724c58f1c22SToby Isaac . point - The mesh point
7725c58f1c22SToby Isaac - value - The label value for this point
7726c58f1c22SToby Isaac 
7727c58f1c22SToby Isaac   Output Parameter:
7728c58f1c22SToby Isaac 
7729c58f1c22SToby Isaac   Level: beginner
7730c58f1c22SToby Isaac 
7731c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7732c58f1c22SToby Isaac @*/
7733c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7734c58f1c22SToby Isaac {
7735c58f1c22SToby Isaac   DMLabel        label;
7736c58f1c22SToby Isaac   PetscErrorCode ierr;
7737c58f1c22SToby Isaac 
7738c58f1c22SToby Isaac   PetscFunctionBegin;
7739c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7740c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7741c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7742c58f1c22SToby Isaac   if (!label) {
7743c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7744c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7745c58f1c22SToby Isaac   }
7746c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7747c58f1c22SToby Isaac   PetscFunctionReturn(0);
7748c58f1c22SToby Isaac }
7749c58f1c22SToby Isaac 
7750c58f1c22SToby Isaac /*@C
7751c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7752c58f1c22SToby Isaac 
7753c58f1c22SToby Isaac   Not Collective
7754c58f1c22SToby Isaac 
7755c58f1c22SToby Isaac   Input Parameters:
7756c58f1c22SToby Isaac + dm   - The DM object
7757c58f1c22SToby Isaac . name - The label name
7758c58f1c22SToby Isaac . point - The mesh point
7759c58f1c22SToby Isaac - value - The label value for this point
7760c58f1c22SToby Isaac 
7761c58f1c22SToby Isaac   Output Parameter:
7762c58f1c22SToby Isaac 
7763c58f1c22SToby Isaac   Level: beginner
7764c58f1c22SToby Isaac 
7765c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7766c58f1c22SToby Isaac @*/
7767c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7768c58f1c22SToby Isaac {
7769c58f1c22SToby Isaac   DMLabel        label;
7770c58f1c22SToby Isaac   PetscErrorCode ierr;
7771c58f1c22SToby Isaac 
7772c58f1c22SToby Isaac   PetscFunctionBegin;
7773c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7774c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7775c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7776c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7777c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7778c58f1c22SToby Isaac   PetscFunctionReturn(0);
7779c58f1c22SToby Isaac }
7780c58f1c22SToby Isaac 
7781c58f1c22SToby Isaac /*@C
7782c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7783c58f1c22SToby Isaac 
7784c58f1c22SToby Isaac   Not Collective
7785c58f1c22SToby Isaac 
7786c58f1c22SToby Isaac   Input Parameters:
7787c58f1c22SToby Isaac + dm   - The DM object
7788c58f1c22SToby Isaac - name - The label name
7789c58f1c22SToby Isaac 
7790c58f1c22SToby Isaac   Output Parameter:
7791c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7792c58f1c22SToby Isaac 
7793c58f1c22SToby Isaac   Level: beginner
7794c58f1c22SToby Isaac 
7795df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7796c58f1c22SToby Isaac @*/
7797c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7798c58f1c22SToby Isaac {
7799c58f1c22SToby Isaac   DMLabel        label;
7800c58f1c22SToby Isaac   PetscErrorCode ierr;
7801c58f1c22SToby Isaac 
7802c58f1c22SToby Isaac   PetscFunctionBegin;
7803c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7804c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7805534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7806c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7807c58f1c22SToby Isaac   *size = 0;
7808c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7809c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7810c58f1c22SToby Isaac   PetscFunctionReturn(0);
7811c58f1c22SToby Isaac }
7812c58f1c22SToby Isaac 
7813c58f1c22SToby Isaac /*@C
7814c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7815c58f1c22SToby Isaac 
7816c58f1c22SToby Isaac   Not Collective
7817c58f1c22SToby Isaac 
7818c58f1c22SToby Isaac   Input Parameters:
7819c58f1c22SToby Isaac + mesh - The DM object
7820c58f1c22SToby Isaac - name - The label name
7821c58f1c22SToby Isaac 
7822c58f1c22SToby Isaac   Output Parameter:
7823c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7824c58f1c22SToby Isaac 
7825c58f1c22SToby Isaac   Level: beginner
7826c58f1c22SToby Isaac 
7827c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7828c58f1c22SToby Isaac @*/
7829c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7830c58f1c22SToby Isaac {
7831c58f1c22SToby Isaac   DMLabel        label;
7832c58f1c22SToby Isaac   PetscErrorCode ierr;
7833c58f1c22SToby Isaac 
7834c58f1c22SToby Isaac   PetscFunctionBegin;
7835c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7836c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7837c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7838c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7839c58f1c22SToby Isaac   *ids = NULL;
7840dab2e251SBlaise Bourdin  if (label) {
7841c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7842dab2e251SBlaise Bourdin   } else {
7843dab2e251SBlaise Bourdin     /* returning an empty IS */
7844dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7845dab2e251SBlaise Bourdin   }
7846c58f1c22SToby Isaac   PetscFunctionReturn(0);
7847c58f1c22SToby Isaac }
7848c58f1c22SToby Isaac 
7849c58f1c22SToby Isaac /*@C
7850c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7851c58f1c22SToby Isaac 
7852c58f1c22SToby Isaac   Not Collective
7853c58f1c22SToby Isaac 
7854c58f1c22SToby Isaac   Input Parameters:
7855c58f1c22SToby Isaac + dm - The DM object
7856c58f1c22SToby Isaac . name - The label name
7857c58f1c22SToby Isaac - value - The stratum value
7858c58f1c22SToby Isaac 
7859c58f1c22SToby Isaac   Output Parameter:
7860c58f1c22SToby Isaac . size - The stratum size
7861c58f1c22SToby Isaac 
7862c58f1c22SToby Isaac   Level: beginner
7863c58f1c22SToby Isaac 
7864c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7865c58f1c22SToby Isaac @*/
7866c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7867c58f1c22SToby Isaac {
7868c58f1c22SToby Isaac   DMLabel        label;
7869c58f1c22SToby Isaac   PetscErrorCode ierr;
7870c58f1c22SToby Isaac 
7871c58f1c22SToby Isaac   PetscFunctionBegin;
7872c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7873c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7874534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7875c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7876c58f1c22SToby Isaac   *size = 0;
7877c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7878c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7879c58f1c22SToby Isaac   PetscFunctionReturn(0);
7880c58f1c22SToby Isaac }
7881c58f1c22SToby Isaac 
7882c58f1c22SToby Isaac /*@C
7883c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7884c58f1c22SToby Isaac 
7885c58f1c22SToby Isaac   Not Collective
7886c58f1c22SToby Isaac 
7887c58f1c22SToby Isaac   Input Parameters:
7888c58f1c22SToby Isaac + dm - The DM object
7889c58f1c22SToby Isaac . name - The label name
7890c58f1c22SToby Isaac - value - The stratum value
7891c58f1c22SToby Isaac 
7892c58f1c22SToby Isaac   Output Parameter:
7893c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7894c58f1c22SToby Isaac 
7895c58f1c22SToby Isaac   Level: beginner
7896c58f1c22SToby Isaac 
7897c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7898c58f1c22SToby Isaac @*/
7899c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7900c58f1c22SToby Isaac {
7901c58f1c22SToby Isaac   DMLabel        label;
7902c58f1c22SToby Isaac   PetscErrorCode ierr;
7903c58f1c22SToby Isaac 
7904c58f1c22SToby Isaac   PetscFunctionBegin;
7905c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7906c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7907c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7908c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7909c58f1c22SToby Isaac   *points = NULL;
7910c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7911c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7912c58f1c22SToby Isaac   PetscFunctionReturn(0);
7913c58f1c22SToby Isaac }
7914c58f1c22SToby Isaac 
79154de306b1SToby Isaac /*@C
79169044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
79174de306b1SToby Isaac 
79184de306b1SToby Isaac   Not Collective
79194de306b1SToby Isaac 
79204de306b1SToby Isaac   Input Parameters:
79214de306b1SToby Isaac + dm - The DM object
79224de306b1SToby Isaac . name - The label name
79234de306b1SToby Isaac . value - The stratum value
79244de306b1SToby Isaac - points - The stratum points
79254de306b1SToby Isaac 
79264de306b1SToby Isaac   Level: beginner
79274de306b1SToby Isaac 
79284de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
79294de306b1SToby Isaac @*/
79304de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
79314de306b1SToby Isaac {
79324de306b1SToby Isaac   DMLabel        label;
79334de306b1SToby Isaac   PetscErrorCode ierr;
79344de306b1SToby Isaac 
79354de306b1SToby Isaac   PetscFunctionBegin;
79364de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79374de306b1SToby Isaac   PetscValidCharPointer(name, 2);
79384de306b1SToby Isaac   PetscValidPointer(points, 4);
79394de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
79404de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
79414de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
79424de306b1SToby Isaac   PetscFunctionReturn(0);
79434de306b1SToby Isaac }
79444de306b1SToby Isaac 
7945c58f1c22SToby Isaac /*@C
7946c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7947c58f1c22SToby Isaac 
7948c58f1c22SToby Isaac   Not Collective
7949c58f1c22SToby Isaac 
7950c58f1c22SToby Isaac   Input Parameters:
7951c58f1c22SToby Isaac + dm   - The DM object
7952c58f1c22SToby Isaac . name - The label name
7953c58f1c22SToby Isaac - value - The label value for this point
7954c58f1c22SToby Isaac 
7955c58f1c22SToby Isaac   Output Parameter:
7956c58f1c22SToby Isaac 
7957c58f1c22SToby Isaac   Level: beginner
7958c58f1c22SToby Isaac 
7959c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7960c58f1c22SToby Isaac @*/
7961c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7962c58f1c22SToby Isaac {
7963c58f1c22SToby Isaac   DMLabel        label;
7964c58f1c22SToby Isaac   PetscErrorCode ierr;
7965c58f1c22SToby Isaac 
7966c58f1c22SToby Isaac   PetscFunctionBegin;
7967c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7968c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7969c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7970c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7971c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7972c58f1c22SToby Isaac   PetscFunctionReturn(0);
7973c58f1c22SToby Isaac }
7974c58f1c22SToby Isaac 
7975c58f1c22SToby Isaac /*@
7976c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7977c58f1c22SToby Isaac 
7978c58f1c22SToby Isaac   Not Collective
7979c58f1c22SToby Isaac 
7980c58f1c22SToby Isaac   Input Parameter:
7981c58f1c22SToby Isaac . dm   - The DM object
7982c58f1c22SToby Isaac 
7983c58f1c22SToby Isaac   Output Parameter:
7984c58f1c22SToby Isaac . numLabels - the number of Labels
7985c58f1c22SToby Isaac 
7986c58f1c22SToby Isaac   Level: intermediate
7987c58f1c22SToby Isaac 
7988c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7989c58f1c22SToby Isaac @*/
7990c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7991c58f1c22SToby Isaac {
79925d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7993c58f1c22SToby Isaac   PetscInt  n    = 0;
7994c58f1c22SToby Isaac 
7995c58f1c22SToby Isaac   PetscFunctionBegin;
7996c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7997534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7998c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7999c58f1c22SToby Isaac   *numLabels = n;
8000c58f1c22SToby Isaac   PetscFunctionReturn(0);
8001c58f1c22SToby Isaac }
8002c58f1c22SToby Isaac 
8003c58f1c22SToby Isaac /*@C
8004c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
8005c58f1c22SToby Isaac 
8006c58f1c22SToby Isaac   Not Collective
8007c58f1c22SToby Isaac 
8008c58f1c22SToby Isaac   Input Parameters:
8009c58f1c22SToby Isaac + dm - The DM object
8010c58f1c22SToby Isaac - n  - the label number
8011c58f1c22SToby Isaac 
8012c58f1c22SToby Isaac   Output Parameter:
8013c58f1c22SToby Isaac . name - the label name
8014c58f1c22SToby Isaac 
8015c58f1c22SToby Isaac   Level: intermediate
8016c58f1c22SToby Isaac 
8017c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8018c58f1c22SToby Isaac @*/
8019c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
8020c58f1c22SToby Isaac {
80215d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8022c58f1c22SToby Isaac   PetscInt       l    = 0;
8023d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
8024c58f1c22SToby Isaac 
8025c58f1c22SToby Isaac   PetscFunctionBegin;
8026c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8027c58f1c22SToby Isaac   PetscValidPointer(name, 3);
8028c58f1c22SToby Isaac   while (next) {
8029c58f1c22SToby Isaac     if (l == n) {
8030d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
8031c58f1c22SToby Isaac       PetscFunctionReturn(0);
8032c58f1c22SToby Isaac     }
8033c58f1c22SToby Isaac     ++l;
8034c58f1c22SToby Isaac     next = next->next;
8035c58f1c22SToby Isaac   }
803698921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
8037c58f1c22SToby Isaac }
8038c58f1c22SToby Isaac 
8039c58f1c22SToby Isaac /*@C
8040c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
8041c58f1c22SToby Isaac 
8042c58f1c22SToby Isaac   Not Collective
8043c58f1c22SToby Isaac 
8044c58f1c22SToby Isaac   Input Parameters:
8045c58f1c22SToby Isaac + dm   - The DM object
8046c58f1c22SToby Isaac - name - The label name
8047c58f1c22SToby Isaac 
8048c58f1c22SToby Isaac   Output Parameter:
8049c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
8050c58f1c22SToby Isaac 
8051c58f1c22SToby Isaac   Level: intermediate
8052c58f1c22SToby Isaac 
8053c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8054c58f1c22SToby Isaac @*/
8055c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
8056c58f1c22SToby Isaac {
80575d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8058d67d17b1SMatthew G. Knepley   const char    *lname;
8059c58f1c22SToby Isaac   PetscErrorCode ierr;
8060c58f1c22SToby Isaac 
8061c58f1c22SToby Isaac   PetscFunctionBegin;
8062c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8063c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
8064534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
8065c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
8066c58f1c22SToby Isaac   while (next) {
8067d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8068d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
8069c58f1c22SToby Isaac     if (*hasLabel) break;
8070c58f1c22SToby Isaac     next = next->next;
8071c58f1c22SToby Isaac   }
8072c58f1c22SToby Isaac   PetscFunctionReturn(0);
8073c58f1c22SToby Isaac }
8074c58f1c22SToby Isaac 
8075c58f1c22SToby Isaac /*@C
8076c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
8077c58f1c22SToby Isaac 
8078c58f1c22SToby Isaac   Not Collective
8079c58f1c22SToby Isaac 
8080c58f1c22SToby Isaac   Input Parameters:
8081c58f1c22SToby Isaac + dm   - The DM object
8082c58f1c22SToby Isaac - name - The label name
8083c58f1c22SToby Isaac 
8084c58f1c22SToby Isaac   Output Parameter:
8085c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
8086c58f1c22SToby Isaac 
80876d7c9049SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
80886d7c9049SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
80896d7c9049SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
80906d7c9049SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
80916d7c9049SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
80926d7c9049SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
80936d7c9049SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
80946d7c9049SMatthew G. Knepley 
8095c58f1c22SToby Isaac   Level: intermediate
8096c58f1c22SToby Isaac 
80976d7c9049SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
8098c58f1c22SToby Isaac @*/
8099c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
8100c58f1c22SToby Isaac {
81015d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8102c58f1c22SToby Isaac   PetscBool      hasLabel;
8103d67d17b1SMatthew G. Knepley   const char    *lname;
8104c58f1c22SToby Isaac   PetscErrorCode ierr;
8105c58f1c22SToby Isaac 
8106c58f1c22SToby Isaac   PetscFunctionBegin;
8107c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8108c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
8109c58f1c22SToby Isaac   PetscValidPointer(label, 3);
8110c58f1c22SToby Isaac   *label = NULL;
8111c58f1c22SToby Isaac   while (next) {
8112d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8113d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
8114c58f1c22SToby Isaac     if (hasLabel) {
8115c58f1c22SToby Isaac       *label = next->label;
8116c58f1c22SToby Isaac       break;
8117c58f1c22SToby Isaac     }
8118c58f1c22SToby Isaac     next = next->next;
8119c58f1c22SToby Isaac   }
8120c58f1c22SToby Isaac   PetscFunctionReturn(0);
8121c58f1c22SToby Isaac }
8122c58f1c22SToby Isaac 
8123c58f1c22SToby Isaac /*@C
8124c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
8125c58f1c22SToby Isaac 
8126c58f1c22SToby Isaac   Not Collective
8127c58f1c22SToby Isaac 
8128c58f1c22SToby Isaac   Input Parameters:
8129c58f1c22SToby Isaac + dm - The DM object
8130c58f1c22SToby Isaac - n  - the label number
8131c58f1c22SToby Isaac 
8132c58f1c22SToby Isaac   Output Parameter:
8133c58f1c22SToby Isaac . label - the label
8134c58f1c22SToby Isaac 
8135c58f1c22SToby Isaac   Level: intermediate
8136c58f1c22SToby Isaac 
8137c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8138c58f1c22SToby Isaac @*/
8139c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
8140c58f1c22SToby Isaac {
81415d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
8142c58f1c22SToby Isaac   PetscInt    l    = 0;
8143c58f1c22SToby Isaac 
8144c58f1c22SToby Isaac   PetscFunctionBegin;
8145c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8146c58f1c22SToby Isaac   PetscValidPointer(label, 3);
8147c58f1c22SToby Isaac   while (next) {
8148c58f1c22SToby Isaac     if (l == n) {
8149c58f1c22SToby Isaac       *label = next->label;
8150c58f1c22SToby Isaac       PetscFunctionReturn(0);
8151c58f1c22SToby Isaac     }
8152c58f1c22SToby Isaac     ++l;
8153c58f1c22SToby Isaac     next = next->next;
8154c58f1c22SToby Isaac   }
815598921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
8156c58f1c22SToby Isaac }
8157c58f1c22SToby Isaac 
8158c58f1c22SToby Isaac /*@C
8159c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
8160c58f1c22SToby Isaac 
8161c58f1c22SToby Isaac   Not Collective
8162c58f1c22SToby Isaac 
8163c58f1c22SToby Isaac   Input Parameters:
8164c58f1c22SToby Isaac + dm   - The DM object
8165c58f1c22SToby Isaac - label - The DMLabel
8166c58f1c22SToby Isaac 
8167c58f1c22SToby Isaac   Level: developer
8168c58f1c22SToby Isaac 
8169c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8170c58f1c22SToby Isaac @*/
8171c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
8172c58f1c22SToby Isaac {
81735d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
8174c58f1c22SToby Isaac   PetscBool      hasLabel;
8175d67d17b1SMatthew G. Knepley   const char    *lname;
81765d80c0bfSVaclav Hapla   PetscBool      flg;
8177c58f1c22SToby Isaac   PetscErrorCode ierr;
8178c58f1c22SToby Isaac 
8179c58f1c22SToby Isaac   PetscFunctionBegin;
8180c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8181d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
8182d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
8183*7a8be351SBarry Smith   PetscCheck(!hasLabel,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
8184c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
8185c58f1c22SToby Isaac   tmpLabel->label  = label;
8186c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
81875d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
81885d80c0bfSVaclav Hapla   *p = tmpLabel;
818908f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
81905d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
81915d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
8192ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
8193ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
8194c58f1c22SToby Isaac   PetscFunctionReturn(0);
8195c58f1c22SToby Isaac }
8196c58f1c22SToby Isaac 
8197c58f1c22SToby Isaac /*@C
81984a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
81994a7ee7d0SMatthew G. Knepley 
82004a7ee7d0SMatthew G. Knepley   Not Collective
82014a7ee7d0SMatthew G. Knepley 
82024a7ee7d0SMatthew G. Knepley   Input Parameters:
82034a7ee7d0SMatthew G. Knepley + dm    - The DM object
82044a7ee7d0SMatthew G. Knepley - label - The DMLabel, having the same name, to substitute
82054a7ee7d0SMatthew G. Knepley 
82064a7ee7d0SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
82074a7ee7d0SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
82084a7ee7d0SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
82094a7ee7d0SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
82104a7ee7d0SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
82114a7ee7d0SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
82124a7ee7d0SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
82134a7ee7d0SMatthew G. Knepley 
82144a7ee7d0SMatthew G. Knepley   Level: intermediate
82154a7ee7d0SMatthew G. Knepley 
82164a7ee7d0SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
82174a7ee7d0SMatthew G. Knepley @*/
82184a7ee7d0SMatthew G. Knepley PetscErrorCode DMSetLabel(DM dm, DMLabel label)
82194a7ee7d0SMatthew G. Knepley {
82204a7ee7d0SMatthew G. Knepley   DMLabelLink    next = dm->labels;
82214a7ee7d0SMatthew G. Knepley   PetscBool      hasLabel, flg;
82224a7ee7d0SMatthew G. Knepley   const char    *name, *lname;
82234a7ee7d0SMatthew G. Knepley   PetscErrorCode ierr;
82244a7ee7d0SMatthew G. Knepley 
82254a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
82264a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82274a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
82284a7ee7d0SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &name);CHKERRQ(ierr);
82294a7ee7d0SMatthew G. Knepley   while (next) {
82304a7ee7d0SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
82314a7ee7d0SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
82324a7ee7d0SMatthew G. Knepley     if (hasLabel) {
82334a7ee7d0SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
82344a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
82354a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
82364a7ee7d0SMatthew G. Knepley       ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
82374a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
82384a7ee7d0SMatthew G. Knepley       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
82394a7ee7d0SMatthew G. Knepley       next->label = label;
82404a7ee7d0SMatthew G. Knepley       break;
82414a7ee7d0SMatthew G. Knepley     }
82424a7ee7d0SMatthew G. Knepley     next = next->next;
82434a7ee7d0SMatthew G. Knepley   }
82444a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
82454a7ee7d0SMatthew G. Knepley }
82464a7ee7d0SMatthew G. Knepley 
82474a7ee7d0SMatthew G. Knepley /*@C
8248e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
8249c58f1c22SToby Isaac 
8250c58f1c22SToby Isaac   Not Collective
8251c58f1c22SToby Isaac 
8252c58f1c22SToby Isaac   Input Parameters:
8253c58f1c22SToby Isaac + dm   - The DM object
8254c58f1c22SToby Isaac - name - The label name
8255c58f1c22SToby Isaac 
8256c58f1c22SToby Isaac   Output Parameter:
8257c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
8258c58f1c22SToby Isaac 
8259c58f1c22SToby Isaac   Level: developer
8260c58f1c22SToby Isaac 
8261e5472504SVaclav Hapla   Notes:
8262e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
8263e5472504SVaclav Hapla   DMLabelDestroy() on the label.
8264e5472504SVaclav Hapla 
8265e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
8266e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
8267e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
8268e5472504SVaclav Hapla 
8269e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
8270c58f1c22SToby Isaac @*/
8271c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
8272c58f1c22SToby Isaac {
827395d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
8274c58f1c22SToby Isaac   PetscBool      hasLabel;
8275d67d17b1SMatthew G. Knepley   const char    *lname;
8276c58f1c22SToby Isaac   PetscErrorCode ierr;
8277c58f1c22SToby Isaac 
8278c58f1c22SToby Isaac   PetscFunctionBegin;
8279c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8280e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
8281e5472504SVaclav Hapla   if (label) {
8282e5472504SVaclav Hapla     PetscValidPointer(label, 3);
8283c58f1c22SToby Isaac     *label = NULL;
8284e5472504SVaclav Hapla   }
82855d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
828695d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
8287d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
8288c58f1c22SToby Isaac     if (hasLabel) {
828995d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
8290c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
829195d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
8292ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
8293ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
829495d578d6SVaclav Hapla       if (label) *label = link->label;
829595d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
829695d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8297c58f1c22SToby Isaac       break;
8298c58f1c22SToby Isaac     }
8299c58f1c22SToby Isaac   }
8300c58f1c22SToby Isaac   PetscFunctionReturn(0);
8301c58f1c22SToby Isaac }
8302c58f1c22SToby Isaac 
8303306894acSVaclav Hapla /*@
8304306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
8305306894acSVaclav Hapla 
8306306894acSVaclav Hapla   Not Collective
8307306894acSVaclav Hapla 
8308306894acSVaclav Hapla   Input Parameters:
8309306894acSVaclav Hapla + dm   - The DM object
8310876aa926SVaclav Hapla . label - The DMLabel to be removed from the DM
8311306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
8312306894acSVaclav Hapla 
8313306894acSVaclav Hapla   Level: developer
8314306894acSVaclav Hapla 
8315306894acSVaclav Hapla   Notes:
8316306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
8317306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
8318306894acSVaclav Hapla   *label nullified.
8319306894acSVaclav Hapla 
8320306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
8321306894acSVaclav Hapla @*/
8322306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
8323306894acSVaclav Hapla {
832443e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
8325306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
8326306894acSVaclav Hapla   PetscErrorCode ierr;
8327306894acSVaclav Hapla 
8328306894acSVaclav Hapla   PetscFunctionBegin;
8329306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8330306894acSVaclav Hapla   PetscValidPointer(label, 2);
8331f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
8332306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
8333306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
83345d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
833543e45a93SVaclav Hapla     if (*label == link->label) {
8336306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
833743e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
8338306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
8339ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
834043e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
834143e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
834243e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
8343306894acSVaclav Hapla       break;
8344306894acSVaclav Hapla     }
8345306894acSVaclav Hapla   }
8346*7a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
8347306894acSVaclav Hapla   PetscFunctionReturn(0);
8348306894acSVaclav Hapla }
8349306894acSVaclav Hapla 
8350c58f1c22SToby Isaac /*@C
8351c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
8352c58f1c22SToby Isaac 
8353c58f1c22SToby Isaac   Not Collective
8354c58f1c22SToby Isaac 
8355c58f1c22SToby Isaac   Input Parameters:
8356c58f1c22SToby Isaac + dm   - The DM object
8357c58f1c22SToby Isaac - name - The label name
8358c58f1c22SToby Isaac 
8359c58f1c22SToby Isaac   Output Parameter:
8360c58f1c22SToby Isaac . output - The flag for output
8361c58f1c22SToby Isaac 
8362c58f1c22SToby Isaac   Level: developer
8363c58f1c22SToby Isaac 
8364c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8365c58f1c22SToby Isaac @*/
8366c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
8367c58f1c22SToby Isaac {
83685d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8369d67d17b1SMatthew G. Knepley   const char    *lname;
8370c58f1c22SToby Isaac   PetscErrorCode ierr;
8371c58f1c22SToby Isaac 
8372c58f1c22SToby Isaac   PetscFunctionBegin;
8373c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8374c58f1c22SToby Isaac   PetscValidPointer(name, 2);
8375c58f1c22SToby Isaac   PetscValidPointer(output, 3);
8376c58f1c22SToby Isaac   while (next) {
8377c58f1c22SToby Isaac     PetscBool flg;
8378c58f1c22SToby Isaac 
8379d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8380d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8381c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
8382c58f1c22SToby Isaac     next = next->next;
8383c58f1c22SToby Isaac   }
838498921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8385c58f1c22SToby Isaac }
8386c58f1c22SToby Isaac 
8387c58f1c22SToby Isaac /*@C
8388c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
8389c58f1c22SToby Isaac 
8390c58f1c22SToby Isaac   Not Collective
8391c58f1c22SToby Isaac 
8392c58f1c22SToby Isaac   Input Parameters:
8393c58f1c22SToby Isaac + dm     - The DM object
8394c58f1c22SToby Isaac . name   - The label name
8395c58f1c22SToby Isaac - output - The flag for output
8396c58f1c22SToby Isaac 
8397c58f1c22SToby Isaac   Level: developer
8398c58f1c22SToby Isaac 
8399c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8400c58f1c22SToby Isaac @*/
8401c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
8402c58f1c22SToby Isaac {
84035d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8404d67d17b1SMatthew G. Knepley   const char    *lname;
8405c58f1c22SToby Isaac   PetscErrorCode ierr;
8406c58f1c22SToby Isaac 
8407c58f1c22SToby Isaac   PetscFunctionBegin;
8408c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8409534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
8410c58f1c22SToby Isaac   while (next) {
8411c58f1c22SToby Isaac     PetscBool flg;
8412c58f1c22SToby Isaac 
8413d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
8414d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
8415c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
8416c58f1c22SToby Isaac     next = next->next;
8417c58f1c22SToby Isaac   }
841898921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8419c58f1c22SToby Isaac }
8420c58f1c22SToby Isaac 
8421c58f1c22SToby Isaac /*@
8422c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
8423c58f1c22SToby Isaac 
8424d083f849SBarry Smith   Collective on dmA
8425c58f1c22SToby Isaac 
8426d8d19677SJose E. Roman   Input Parameters:
84275d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
84282cbb9b06SVaclav Hapla . dmB - The DM object to which labels are copied
84295d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
84302cbb9b06SVaclav Hapla . all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
84312cbb9b06SVaclav Hapla - emode - How to behave when a DMLabel in the source and destination DMs with the same name is encountered (see DMCopyLabelsMode)
8432c58f1c22SToby Isaac 
8433c58f1c22SToby Isaac   Level: intermediate
8434c58f1c22SToby Isaac 
84352cbb9b06SVaclav Hapla   Notes:
84362cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
8437c58f1c22SToby Isaac 
84382cbb9b06SVaclav Hapla .seealso: DMAddLabel(), DMCopyLabelsMode
8439c58f1c22SToby Isaac @*/
84402cbb9b06SVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
8441c58f1c22SToby Isaac {
84422cbb9b06SVaclav Hapla   DMLabel        label, labelNew, labelOld;
8443c58f1c22SToby Isaac   const char    *name;
8444c58f1c22SToby Isaac   PetscBool      flg;
84455d80c0bfSVaclav Hapla   DMLabelLink    link;
84465d80c0bfSVaclav Hapla   PetscErrorCode ierr;
8447c58f1c22SToby Isaac 
84485d80c0bfSVaclav Hapla   PetscFunctionBegin;
84495d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
84505d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
84515d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
84525d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
8453*7a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER,PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
84545d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
84555d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
84565d80c0bfSVaclav Hapla     label=link->label;
84575d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
84585d80c0bfSVaclav Hapla     if (!all) {
8459c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
8460c58f1c22SToby Isaac       if (flg) continue;
84617d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
84627d5acc75SStefano Zampini       if (flg) continue;
8463ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
8464ba2698f1SMatthew G. Knepley       if (flg) continue;
84655d80c0bfSVaclav Hapla     }
84662cbb9b06SVaclav Hapla     ierr = DMGetLabel(dmB, name, &labelOld);CHKERRQ(ierr);
84672cbb9b06SVaclav Hapla     if (labelOld) {
84682cbb9b06SVaclav Hapla       switch (emode) {
84692cbb9b06SVaclav Hapla         case DM_COPY_LABELS_KEEP:
84702cbb9b06SVaclav Hapla           continue;
84712cbb9b06SVaclav Hapla         case DM_COPY_LABELS_REPLACE:
84722cbb9b06SVaclav Hapla           ierr = DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE);CHKERRQ(ierr);
84732cbb9b06SVaclav Hapla           break;
84742cbb9b06SVaclav Hapla         case DM_COPY_LABELS_FAIL:
847598921bdaSJacob Faibussowitsch           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
84762cbb9b06SVaclav Hapla         default:
8477*7a8be351SBarry Smith           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
84782cbb9b06SVaclav Hapla       }
84792cbb9b06SVaclav Hapla     }
84805d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
8481c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
84825d80c0bfSVaclav Hapla     } else {
84835d80c0bfSVaclav Hapla       labelNew = label;
84845d80c0bfSVaclav Hapla     }
8485c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
84865d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
8487c58f1c22SToby Isaac   }
8488c58f1c22SToby Isaac   PetscFunctionReturn(0);
8489c58f1c22SToby Isaac }
8490461a15a0SLisandro Dalcin 
8491609dae6eSVaclav Hapla /*@C
8492609dae6eSVaclav Hapla   DMCompareLabels - Compare labels of two DMPlex meshes
8493609dae6eSVaclav Hapla 
84945efe38ccSVaclav Hapla   Collective
8495609dae6eSVaclav Hapla 
8496609dae6eSVaclav Hapla   Input Parameters:
8497609dae6eSVaclav Hapla + dm0 - First DM object
8498609dae6eSVaclav Hapla - dm1 - Second DM object
8499609dae6eSVaclav Hapla 
8500609dae6eSVaclav Hapla   Output Parameters
85015efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
8502609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
8503609dae6eSVaclav Hapla 
8504609dae6eSVaclav Hapla   Level: intermediate
8505609dae6eSVaclav Hapla 
8506609dae6eSVaclav Hapla   Notes:
85075efe38ccSVaclav Hapla   The output flag equal is the same on all processes.
85085efe38ccSVaclav Hapla   If it is passed as NULL and difference is found, an error is thrown on all processes.
85095efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
8510609dae6eSVaclav Hapla 
85115efe38ccSVaclav Hapla   The output message is set independently on each rank.
85125efe38ccSVaclav Hapla   It is set to NULL if no difference was found on the current rank. It must be freed by user.
85135efe38ccSVaclav Hapla   If message is passed as NULL and difference is found, the difference description is printed to stderr in synchronized manner.
85145efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
8515609dae6eSVaclav Hapla 
8516609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
8517609dae6eSVaclav Hapla   DMLabelCompare() is used to compare each pair of labels with the same name.
8518609dae6eSVaclav Hapla 
8519609dae6eSVaclav Hapla   Fortran Notes:
8520609dae6eSVaclav Hapla   This function is currently not available from Fortran.
8521609dae6eSVaclav Hapla 
8522609dae6eSVaclav Hapla .seealso: DMAddLabel(), DMCopyLabelsMode, DMLabelCompare()
8523609dae6eSVaclav Hapla @*/
85245efe38ccSVaclav Hapla PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
8525609dae6eSVaclav Hapla {
85265efe38ccSVaclav Hapla   PetscInt        n, i;
8527609dae6eSVaclav Hapla   char            msg[PETSC_MAX_PATH_LEN] = "";
85285efe38ccSVaclav Hapla   PetscBool       eq;
8529609dae6eSVaclav Hapla   MPI_Comm        comm;
85305efe38ccSVaclav Hapla   PetscMPIInt     rank;
8531609dae6eSVaclav Hapla   PetscErrorCode  ierr;
8532609dae6eSVaclav Hapla 
8533609dae6eSVaclav Hapla   PetscFunctionBegin;
8534609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0,DM_CLASSID,1);
8535609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1,DM_CLASSID,2);
8536609dae6eSVaclav Hapla   PetscCheckSameComm(dm0,1,dm1,2);
85375efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal,3);
8538609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
8539609dae6eSVaclav Hapla   ierr = PetscObjectGetComm((PetscObject)dm0, &comm);CHKERRQ(ierr);
85405efe38ccSVaclav Hapla   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
85415efe38ccSVaclav Hapla   {
85425efe38ccSVaclav Hapla     PetscInt n1;
85435efe38ccSVaclav Hapla 
85445efe38ccSVaclav Hapla     ierr = DMGetNumLabels(dm0, &n);CHKERRQ(ierr);
8545609dae6eSVaclav Hapla     ierr = DMGetNumLabels(dm1, &n1);CHKERRQ(ierr);
85465efe38ccSVaclav Hapla     eq = (PetscBool) (n == n1);
85475efe38ccSVaclav Hapla     if (!eq) {
85485efe38ccSVaclav Hapla       ierr = PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %D != %D = Number of labels in dm1", n, n1);CHKERRQ(ierr);
8549609dae6eSVaclav Hapla     }
85505efe38ccSVaclav Hapla     ierr = MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRMPI(ierr);
85515efe38ccSVaclav Hapla     if (!eq) goto finish;
85525efe38ccSVaclav Hapla   }
85535efe38ccSVaclav Hapla   for (i=0; i<n; i++) {
8554609dae6eSVaclav Hapla     DMLabel     l0, l1;
8555609dae6eSVaclav Hapla     const char *name;
8556609dae6eSVaclav Hapla     char       *msgInner;
8557609dae6eSVaclav Hapla 
8558609dae6eSVaclav Hapla     /* Ignore label order */
8559609dae6eSVaclav Hapla     ierr = DMGetLabelByNum(dm0, i, &l0);CHKERRQ(ierr);
8560609dae6eSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)l0, &name);CHKERRQ(ierr);
8561609dae6eSVaclav Hapla     ierr = DMGetLabel(dm1, name, &l1);CHKERRQ(ierr);
8562609dae6eSVaclav Hapla     if (!l1) {
8563609dae6eSVaclav Hapla       ierr = PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%D in dm0) not found in dm1", name, i);CHKERRQ(ierr);
85645efe38ccSVaclav Hapla       eq = PETSC_FALSE;
85655efe38ccSVaclav Hapla       break;
8566609dae6eSVaclav Hapla     }
85675efe38ccSVaclav Hapla     ierr = DMLabelCompare(comm, l0, l1, &eq, &msgInner);CHKERRQ(ierr);
8568609dae6eSVaclav Hapla     ierr = PetscStrncpy(msg, msgInner, sizeof(msg));CHKERRQ(ierr);
8569609dae6eSVaclav Hapla     ierr = PetscFree(msgInner);CHKERRQ(ierr);
85705efe38ccSVaclav Hapla     if (!eq) break;
8571609dae6eSVaclav Hapla   }
85725efe38ccSVaclav Hapla   ierr = MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRMPI(ierr);
8573609dae6eSVaclav Hapla finish:
85745efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
8575609dae6eSVaclav Hapla   if (message) {
8576609dae6eSVaclav Hapla     *message = NULL;
8577609dae6eSVaclav Hapla     if (msg[0]) {
8578609dae6eSVaclav Hapla       ierr = PetscStrallocpy(msg, message);CHKERRQ(ierr);
8579609dae6eSVaclav Hapla     }
85805efe38ccSVaclav Hapla   } else {
85815efe38ccSVaclav Hapla     if (msg[0]) {
85825efe38ccSVaclav Hapla       ierr = PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg);CHKERRQ(ierr);
8583609dae6eSVaclav Hapla     }
85845efe38ccSVaclav Hapla     ierr = PetscSynchronizedFlush(comm, PETSC_STDERR);CHKERRQ(ierr);
85855efe38ccSVaclav Hapla   }
85865efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
85875efe38ccSVaclav Hapla   if (equal) *equal = eq;
8588*7a8be351SBarry Smith   else PetscCheck(eq,comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
8589609dae6eSVaclav Hapla   PetscFunctionReturn(0);
8590609dae6eSVaclav Hapla }
8591609dae6eSVaclav Hapla 
8592461a15a0SLisandro Dalcin PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
8593461a15a0SLisandro Dalcin {
8594461a15a0SLisandro Dalcin   PetscErrorCode ierr;
8595609dae6eSVaclav Hapla 
8596461a15a0SLisandro Dalcin   PetscFunctionBegin;
8597461a15a0SLisandro Dalcin   PetscValidPointer(label,2);
8598461a15a0SLisandro Dalcin   if (!*label) {
8599461a15a0SLisandro Dalcin     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
8600461a15a0SLisandro Dalcin     ierr = DMGetLabel(dm, name, label);CHKERRQ(ierr);
8601461a15a0SLisandro Dalcin   }
8602461a15a0SLisandro Dalcin   ierr = DMLabelSetValue(*label, point, value);CHKERRQ(ierr);
8603461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
8604461a15a0SLisandro Dalcin }
8605461a15a0SLisandro Dalcin 
86060fdc7489SMatthew Knepley /*
86070fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
86080fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
86090fdc7489SMatthew Knepley   (label, id) pair in the DM.
86100fdc7489SMatthew Knepley 
86110fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
86120fdc7489SMatthew Knepley   each label.
86130fdc7489SMatthew Knepley */
86140fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
86150fdc7489SMatthew Knepley {
86160fdc7489SMatthew Knepley   DMUniversalLabel ul;
86170fdc7489SMatthew Knepley   PetscBool       *active;
86180fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
86190fdc7489SMatthew Knepley   PetscErrorCode   ierr;
86200fdc7489SMatthew Knepley 
86210fdc7489SMatthew Knepley   PetscFunctionBegin;
86220fdc7489SMatthew Knepley   ierr = PetscMalloc1(1, &ul);CHKERRQ(ierr);
86230fdc7489SMatthew Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label);CHKERRQ(ierr);
86240fdc7489SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
86250fdc7489SMatthew Knepley   ierr = PetscCalloc1(Nl, &active);CHKERRQ(ierr);
86260fdc7489SMatthew Knepley   ul->Nl = 0;
86270fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
86280fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
86290fdc7489SMatthew Knepley     const char *name;
86300fdc7489SMatthew Knepley 
86310fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
86320fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "depth", 6, &isdepth);CHKERRQ(ierr);
86330fdc7489SMatthew Knepley     ierr = PetscStrncmp(name, "celltype", 9, &iscelltype);CHKERRQ(ierr);
86340fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
86350fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
86360fdc7489SMatthew Knepley   }
86370fdc7489SMatthew 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);
86380fdc7489SMatthew Knepley   ul->Nv = 0;
86390fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
86400fdc7489SMatthew Knepley     DMLabel     label;
86410fdc7489SMatthew Knepley     PetscInt    nv;
86420fdc7489SMatthew Knepley     const char *name;
86430fdc7489SMatthew Knepley 
86440fdc7489SMatthew Knepley     if (!active[l]) continue;
86450fdc7489SMatthew Knepley     ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
86460fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
86470fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
86480fdc7489SMatthew Knepley     ierr = PetscStrallocpy(name, &ul->names[m]);CHKERRQ(ierr);
86490fdc7489SMatthew Knepley     ul->indices[m]   = l;
86500fdc7489SMatthew Knepley     ul->Nv          += nv;
86510fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
86520fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
86530fdc7489SMatthew Knepley     ++m;
86540fdc7489SMatthew Knepley   }
86550fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
86560fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
86570fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
86580fdc7489SMatthew Knepley   }
86590fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
86600fdc7489SMatthew Knepley     PetscInt b;
86610fdc7489SMatthew Knepley 
86620fdc7489SMatthew Knepley     ul->masks[l] = 0;
86630fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
86640fdc7489SMatthew Knepley   }
86650fdc7489SMatthew Knepley   ierr = PetscMalloc1(ul->Nv, &ul->values);CHKERRQ(ierr);
86660fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
86670fdc7489SMatthew Knepley     DMLabel         label;
86680fdc7489SMatthew Knepley     IS              valueIS;
86690fdc7489SMatthew Knepley     const PetscInt *varr;
86700fdc7489SMatthew Knepley     PetscInt        nv, v;
86710fdc7489SMatthew Knepley 
86720fdc7489SMatthew Knepley     if (!active[l]) continue;
86730fdc7489SMatthew Knepley     ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
86740fdc7489SMatthew Knepley     ierr = DMLabelGetNumValues(label, &nv);CHKERRQ(ierr);
86750fdc7489SMatthew Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
86760fdc7489SMatthew Knepley     ierr = ISGetIndices(valueIS, &varr);CHKERRQ(ierr);
86770fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
86780fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
86790fdc7489SMatthew Knepley     }
86800fdc7489SMatthew Knepley     ierr = ISRestoreIndices(valueIS, &varr);CHKERRQ(ierr);
86810fdc7489SMatthew Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
86820fdc7489SMatthew Knepley     ierr = PetscSortInt(nv, &ul->values[ul->offsets[m]]);CHKERRQ(ierr);
86830fdc7489SMatthew Knepley     ++m;
86840fdc7489SMatthew Knepley   }
86850fdc7489SMatthew Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
86860fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
86870fdc7489SMatthew Knepley     PetscInt  uval = 0;
86880fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
86890fdc7489SMatthew Knepley 
86900fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
86910fdc7489SMatthew Knepley       DMLabel  label;
86920649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
86930fdc7489SMatthew Knepley 
86940fdc7489SMatthew Knepley       if (!active[l]) continue;
86950fdc7489SMatthew Knepley       ierr = DMGetLabelByNum(dm, l, &label);CHKERRQ(ierr);
86960fdc7489SMatthew Knepley       ierr = DMLabelGetValue(label, p, &val);CHKERRQ(ierr);
86970fdc7489SMatthew Knepley       ierr = DMLabelGetDefaultValue(label, &defval);CHKERRQ(ierr);
86980fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
86990649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
87000fdc7489SMatthew Knepley       marked = PETSC_TRUE;
87010fdc7489SMatthew Knepley       ierr = PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc);CHKERRQ(ierr);
8702*7a8be351SBarry Smith       PetscCheck(loc >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %D not found in compression array", val);
87030fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
87040fdc7489SMatthew Knepley       ++m;
87050fdc7489SMatthew Knepley     }
87060fdc7489SMatthew Knepley     if (marked) {ierr = DMLabelSetValue(ul->label, p, uval);CHKERRQ(ierr);}
87070fdc7489SMatthew Knepley   }
87080fdc7489SMatthew Knepley   ierr = PetscFree(active);CHKERRQ(ierr);
87090fdc7489SMatthew Knepley   *universal = ul;
87100fdc7489SMatthew Knepley   PetscFunctionReturn(0);
87110fdc7489SMatthew Knepley }
87120fdc7489SMatthew Knepley 
87130fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
87140fdc7489SMatthew Knepley {
87150fdc7489SMatthew Knepley   PetscInt       l;
87160fdc7489SMatthew Knepley   PetscErrorCode ierr;
87170fdc7489SMatthew Knepley 
87180fdc7489SMatthew Knepley   PetscFunctionBegin;
87190fdc7489SMatthew Knepley   for (l = 0; l < (*universal)->Nl; ++l) {ierr = PetscFree((*universal)->names[l]);CHKERRQ(ierr);}
87200fdc7489SMatthew Knepley   ierr = DMLabelDestroy(&(*universal)->label);CHKERRQ(ierr);
87210fdc7489SMatthew Knepley   ierr = PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks);CHKERRQ(ierr);
87220fdc7489SMatthew Knepley   ierr = PetscFree((*universal)->values);CHKERRQ(ierr);
87230fdc7489SMatthew Knepley   ierr = PetscFree(*universal);CHKERRQ(ierr);
87240fdc7489SMatthew Knepley   *universal = NULL;
87250fdc7489SMatthew Knepley   PetscFunctionReturn(0);
87260fdc7489SMatthew Knepley }
87270fdc7489SMatthew Knepley 
87280fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
87290fdc7489SMatthew Knepley {
87300fdc7489SMatthew Knepley   PetscFunctionBegin;
87310fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
87320fdc7489SMatthew Knepley   *ulabel = ul->label;
87330fdc7489SMatthew Knepley   PetscFunctionReturn(0);
87340fdc7489SMatthew Knepley }
87350fdc7489SMatthew Knepley 
87360fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
87370fdc7489SMatthew Knepley {
87380fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
87390fdc7489SMatthew Knepley   PetscErrorCode ierr;
87400fdc7489SMatthew Knepley 
87410fdc7489SMatthew Knepley   PetscFunctionBegin;
8742064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
87430fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
87440fdc7489SMatthew Knepley     if (preserveOrder) {ierr = DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]);CHKERRQ(ierr);}
87450fdc7489SMatthew Knepley     else               {ierr = DMCreateLabel(dm, ul->names[l]);CHKERRQ(ierr);}
87460fdc7489SMatthew Knepley   }
87470fdc7489SMatthew Knepley   if (preserveOrder) {
87480fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
87490fdc7489SMatthew Knepley       const char *name;
87500fdc7489SMatthew Knepley       PetscBool   match;
87510fdc7489SMatthew Knepley 
87520fdc7489SMatthew Knepley       ierr = DMGetLabelName(dm, ul->indices[l], &name);CHKERRQ(ierr);
87530fdc7489SMatthew Knepley       ierr = PetscStrcmp(name, ul->names[l], &match);CHKERRQ(ierr);
8754*7a8be351SBarry Smith       PetscCheck(match,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Label %D name %s does not match new name %s", l, name, ul->names[l]);
87550fdc7489SMatthew Knepley     }
87560fdc7489SMatthew Knepley   }
87570fdc7489SMatthew Knepley   PetscFunctionReturn(0);
87580fdc7489SMatthew Knepley }
87590fdc7489SMatthew Knepley 
87600fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
87610fdc7489SMatthew Knepley {
87620fdc7489SMatthew Knepley   PetscInt       l;
87630fdc7489SMatthew Knepley   PetscErrorCode ierr;
87640fdc7489SMatthew Knepley 
87650fdc7489SMatthew Knepley   PetscFunctionBegin;
87660fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
87670fdc7489SMatthew Knepley     DMLabel  label;
87680fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
87690fdc7489SMatthew Knepley 
87700fdc7489SMatthew Knepley     if (lval) {
87710fdc7489SMatthew Knepley       if (useIndex) {ierr = DMGetLabelByNum(dm, ul->indices[l], &label);CHKERRQ(ierr);}
87720fdc7489SMatthew Knepley       else          {ierr = DMGetLabel(dm, ul->names[l], &label);CHKERRQ(ierr);}
87730fdc7489SMatthew Knepley       ierr = DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]);CHKERRQ(ierr);
87740fdc7489SMatthew Knepley     }
87750fdc7489SMatthew Knepley   }
87760fdc7489SMatthew Knepley   PetscFunctionReturn(0);
87770fdc7489SMatthew Knepley }
8778a8fb8f29SToby Isaac 
8779a8fb8f29SToby Isaac /*@
8780a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
8781a8fb8f29SToby Isaac 
8782a8fb8f29SToby Isaac   Input Parameter:
8783a8fb8f29SToby Isaac . dm - The DM object
8784a8fb8f29SToby Isaac 
8785a8fb8f29SToby Isaac   Output Parameter:
8786a8fb8f29SToby Isaac . cdm - The coarse DM
8787a8fb8f29SToby Isaac 
8788a8fb8f29SToby Isaac   Level: intermediate
8789a8fb8f29SToby Isaac 
8790a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
8791a8fb8f29SToby Isaac @*/
8792a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
8793a8fb8f29SToby Isaac {
8794a8fb8f29SToby Isaac   PetscFunctionBegin;
8795a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8796a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
8797a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
8798a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8799a8fb8f29SToby Isaac }
8800a8fb8f29SToby Isaac 
8801a8fb8f29SToby Isaac /*@
8802a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
8803a8fb8f29SToby Isaac 
8804a8fb8f29SToby Isaac   Input Parameters:
8805a8fb8f29SToby Isaac + dm - The DM object
8806a8fb8f29SToby Isaac - cdm - The coarse DM
8807a8fb8f29SToby Isaac 
8808a8fb8f29SToby Isaac   Level: intermediate
8809a8fb8f29SToby Isaac 
8810a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
8811a8fb8f29SToby Isaac @*/
8812a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
8813a8fb8f29SToby Isaac {
8814a8fb8f29SToby Isaac   PetscErrorCode ierr;
8815a8fb8f29SToby Isaac 
8816a8fb8f29SToby Isaac   PetscFunctionBegin;
8817a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8818a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
8819a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
8820a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
8821a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
8822a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8823a8fb8f29SToby Isaac }
8824a8fb8f29SToby Isaac 
882588bdff64SToby Isaac /*@
882688bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
882788bdff64SToby Isaac 
882888bdff64SToby Isaac   Input Parameter:
882988bdff64SToby Isaac . dm - The DM object
883088bdff64SToby Isaac 
883188bdff64SToby Isaac   Output Parameter:
883288bdff64SToby Isaac . fdm - The fine DM
883388bdff64SToby Isaac 
883488bdff64SToby Isaac   Level: intermediate
883588bdff64SToby Isaac 
883688bdff64SToby Isaac .seealso: DMSetFineDM()
883788bdff64SToby Isaac @*/
883888bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
883988bdff64SToby Isaac {
884088bdff64SToby Isaac   PetscFunctionBegin;
884188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
884288bdff64SToby Isaac   PetscValidPointer(fdm, 2);
884388bdff64SToby Isaac   *fdm = dm->fineMesh;
884488bdff64SToby Isaac   PetscFunctionReturn(0);
884588bdff64SToby Isaac }
884688bdff64SToby Isaac 
884788bdff64SToby Isaac /*@
884888bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
884988bdff64SToby Isaac 
885088bdff64SToby Isaac   Input Parameters:
885188bdff64SToby Isaac + dm - The DM object
885288bdff64SToby Isaac - fdm - The fine DM
885388bdff64SToby Isaac 
885488bdff64SToby Isaac   Level: intermediate
885588bdff64SToby Isaac 
885688bdff64SToby Isaac .seealso: DMGetFineDM()
885788bdff64SToby Isaac @*/
885888bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
885988bdff64SToby Isaac {
886088bdff64SToby Isaac   PetscErrorCode ierr;
886188bdff64SToby Isaac 
886288bdff64SToby Isaac   PetscFunctionBegin;
886388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
886488bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
886588bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
886688bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
886788bdff64SToby Isaac   dm->fineMesh = fdm;
886888bdff64SToby Isaac   PetscFunctionReturn(0);
886988bdff64SToby Isaac }
887088bdff64SToby Isaac 
8871a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8872a6ba4734SToby Isaac 
8873a6ba4734SToby Isaac /*@C
8874a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8875a6ba4734SToby Isaac 
8876783e2ec8SMatthew G. Knepley   Collective on dm
8877783e2ec8SMatthew G. Knepley 
8878a6ba4734SToby Isaac   Input Parameters:
88794c258f51SMatthew G. Knepley + dm       - The DM, with a PetscDS that matches the problem being constrained
8880f971fd6bSMatthew G. Knepley . type     - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8881a6ba4734SToby Isaac . name     - The BC name
888245480ffeSMatthew G. Knepley . label    - The label defining constrained points
888345480ffeSMatthew G. Knepley . Nv       - The number of DMLabel values for constrained points
888445480ffeSMatthew G. Knepley . values   - An array of values for constrained points
8885a6ba4734SToby Isaac . field    - The field to constrain
888645480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
8887a6ba4734SToby Isaac . comps    - An array of constrained component numbers
8888a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
888956cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
8890a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
8891a6ba4734SToby Isaac 
889245480ffeSMatthew G. Knepley   Output Parameter:
889345480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
889445480ffeSMatthew G. Knepley 
8895a6ba4734SToby Isaac   Options Database Keys:
8896a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8897a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8898a6ba4734SToby Isaac 
889956cf3b9cSMatthew G. Knepley   Note:
890056cf3b9cSMatthew 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:
890156cf3b9cSMatthew G. Knepley 
890256cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
890356cf3b9cSMatthew G. Knepley 
890456cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
890556cf3b9cSMatthew G. Knepley 
890656cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
890756cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
890856cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
890956cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
891056cf3b9cSMatthew G. Knepley 
891156cf3b9cSMatthew G. Knepley + dim - the spatial dimension
891256cf3b9cSMatthew G. Knepley . Nf - the number of fields
891356cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
891456cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
891556cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
891656cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
891756cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
891856cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
891956cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
892056cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
892156cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
892256cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
892356cf3b9cSMatthew G. Knepley . t - current time
892456cf3b9cSMatthew G. Knepley . x - coordinates of the current point
892556cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
892656cf3b9cSMatthew G. Knepley . constants - constant parameters
892756cf3b9cSMatthew G. Knepley - bcval - output values at the current point
892856cf3b9cSMatthew G. Knepley 
8929ed808b8fSJed Brown   Level: intermediate
8930a6ba4734SToby Isaac 
893145480ffeSMatthew G. Knepley .seealso: DSGetBoundary(), PetscDSAddBoundary()
8932a6ba4734SToby Isaac @*/
893345480ffeSMatthew 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)
8934a6ba4734SToby Isaac {
8935e5e52638SMatthew G. Knepley   PetscDS        ds;
8936a6ba4734SToby Isaac   PetscErrorCode ierr;
8937a6ba4734SToby Isaac 
8938a6ba4734SToby Isaac   PetscFunctionBegin;
8939a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8940783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
894145480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
894245480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
894345480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
894445480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
8945e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
894645480ffeSMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, label);CHKERRQ(ierr);
894745480ffeSMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd);CHKERRQ(ierr);
8948a6ba4734SToby Isaac   PetscFunctionReturn(0);
8949a6ba4734SToby Isaac }
8950a6ba4734SToby Isaac 
895145480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8952e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8953e6f8dbb6SToby Isaac {
8954e5e52638SMatthew G. Knepley   PetscDS        ds;
8955dff059c6SToby Isaac   DMBoundary    *lastnext;
8956e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8957e6f8dbb6SToby Isaac   PetscErrorCode ierr;
8958e6f8dbb6SToby Isaac 
8959e6f8dbb6SToby Isaac   PetscFunctionBegin;
8960e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8961e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
896247a1f5adSToby Isaac   if (dm->boundary) {
896347a1f5adSToby Isaac     DMBoundary next = dm->boundary;
896447a1f5adSToby Isaac 
896547a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
896647a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
896747a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
896847a1f5adSToby Isaac     while (next) {
896947a1f5adSToby Isaac       DMBoundary b = next;
897047a1f5adSToby Isaac 
897147a1f5adSToby Isaac       next = b->next;
897247a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
8973a6ba4734SToby Isaac     }
897447a1f5adSToby Isaac     dm->boundary = NULL;
8975a6ba4734SToby Isaac   }
897647a1f5adSToby Isaac 
8977dff059c6SToby Isaac   lastnext = &(dm->boundary);
8978e6f8dbb6SToby Isaac   while (dsbound) {
8979e6f8dbb6SToby Isaac     DMBoundary dmbound;
8980e6f8dbb6SToby Isaac 
8981e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
8982e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
898345480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
898447a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8985dff059c6SToby Isaac     *lastnext = dmbound;
8986dff059c6SToby Isaac     lastnext = &(dmbound->next);
8987dff059c6SToby Isaac     dsbound = dsbound->next;
8988a6ba4734SToby Isaac   }
8989a6ba4734SToby Isaac   PetscFunctionReturn(0);
8990a6ba4734SToby Isaac }
8991a6ba4734SToby Isaac 
8992a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8993a6ba4734SToby Isaac {
8994b95f2879SToby Isaac   DMBoundary     b;
8995a6ba4734SToby Isaac   PetscErrorCode ierr;
8996a6ba4734SToby Isaac 
8997a6ba4734SToby Isaac   PetscFunctionBegin;
8998a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8999534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
9000a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
9001e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
9002b95f2879SToby Isaac   b = dm->boundary;
9003a6ba4734SToby Isaac   while (b && !(*isBd)) {
9004e6f8dbb6SToby Isaac     DMLabel    label = b->label;
9005e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
9006a6ba4734SToby Isaac     PetscInt   i;
9007a6ba4734SToby Isaac 
900845480ffeSMatthew G. Knepley     if (label) {
900945480ffeSMatthew G. Knepley       for (i = 0; i < dsb->Nv && !(*isBd); ++i) {ierr = DMLabelStratumHasPoint(label, dsb->values[i], point, isBd);CHKERRQ(ierr);}
9010a6ba4734SToby Isaac     }
9011a6ba4734SToby Isaac     b = b->next;
9012a6ba4734SToby Isaac   }
9013a6ba4734SToby Isaac   PetscFunctionReturn(0);
9014a6ba4734SToby Isaac }
90154d6f44ffSToby Isaac 
90164d6f44ffSToby Isaac /*@C
9017a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
9018a6e0b375SMatthew G. Knepley 
9019a6e0b375SMatthew G. Knepley   Collective on DM
90204d6f44ffSToby Isaac 
90214d6f44ffSToby Isaac   Input Parameters:
90224d6f44ffSToby Isaac + dm      - The DM
90230709b2feSToby Isaac . time    - The time
90244d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
90254d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
90264d6f44ffSToby Isaac - mode    - The insertion mode for values
90274d6f44ffSToby Isaac 
90284d6f44ffSToby Isaac   Output Parameter:
90294d6f44ffSToby Isaac . X - vector
90304d6f44ffSToby Isaac 
90314d6f44ffSToby Isaac    Calling sequence of func:
903277b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
90334d6f44ffSToby Isaac 
90344d6f44ffSToby Isaac +  dim - The spatial dimension
90358ec8862eSJed Brown .  time - The time at which to sample
90364d6f44ffSToby Isaac .  x   - The coordinates
903777b739a6SMatthew Knepley .  Nc  - The number of components
90384d6f44ffSToby Isaac .  u   - The output field values
90394d6f44ffSToby Isaac -  ctx - optional user-defined function context
90404d6f44ffSToby Isaac 
90414d6f44ffSToby Isaac   Level: developer
90424d6f44ffSToby Isaac 
9043a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
90444d6f44ffSToby Isaac @*/
90450709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
90464d6f44ffSToby Isaac {
90474d6f44ffSToby Isaac   Vec            localX;
90484d6f44ffSToby Isaac   PetscErrorCode ierr;
90494d6f44ffSToby Isaac 
90504d6f44ffSToby Isaac   PetscFunctionBegin;
90514d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90524d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
90530709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
90544d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
90554d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
90564d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
90574d6f44ffSToby Isaac   PetscFunctionReturn(0);
90584d6f44ffSToby Isaac }
90594d6f44ffSToby Isaac 
9060a6e0b375SMatthew G. Knepley /*@C
9061a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
9062a6e0b375SMatthew G. Knepley 
9063a6e0b375SMatthew G. Knepley   Not collective
9064a6e0b375SMatthew G. Knepley 
9065a6e0b375SMatthew G. Knepley   Input Parameters:
9066a6e0b375SMatthew G. Knepley + dm      - The DM
9067a6e0b375SMatthew G. Knepley . time    - The time
9068a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
9069a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
9070a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9071a6e0b375SMatthew G. Knepley 
9072a6e0b375SMatthew G. Knepley   Output Parameter:
9073a6e0b375SMatthew G. Knepley . localX - vector
9074a6e0b375SMatthew G. Knepley 
9075a6e0b375SMatthew G. Knepley    Calling sequence of func:
907677b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
9077a6e0b375SMatthew G. Knepley 
9078a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
9079a6e0b375SMatthew G. Knepley .  x   - The coordinates
908077b739a6SMatthew Knepley .  Nc  - The number of components
9081a6e0b375SMatthew G. Knepley .  u   - The output field values
9082a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
9083a6e0b375SMatthew G. Knepley 
9084a6e0b375SMatthew G. Knepley   Level: developer
9085a6e0b375SMatthew G. Knepley 
9086a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
9087a6e0b375SMatthew G. Knepley @*/
90880709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
90894d6f44ffSToby Isaac {
90904d6f44ffSToby Isaac   PetscErrorCode ierr;
90914d6f44ffSToby Isaac 
90924d6f44ffSToby Isaac   PetscFunctionBegin;
90934d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9094064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
9095*7a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
90960709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
90974d6f44ffSToby Isaac   PetscFunctionReturn(0);
90984d6f44ffSToby Isaac }
90994d6f44ffSToby Isaac 
9100a6e0b375SMatthew G. Knepley /*@C
9101a6e0b375SMatthew 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.
9102a6e0b375SMatthew G. Knepley 
9103a6e0b375SMatthew G. Knepley   Collective on DM
9104a6e0b375SMatthew G. Knepley 
9105a6e0b375SMatthew G. Knepley   Input Parameters:
9106a6e0b375SMatthew G. Knepley + dm      - The DM
9107a6e0b375SMatthew G. Knepley . time    - The time
9108a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
9109a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
9110a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
9111a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9112a6e0b375SMatthew G. Knepley 
9113a6e0b375SMatthew G. Knepley   Output Parameter:
9114a6e0b375SMatthew G. Knepley . X - vector
9115a6e0b375SMatthew G. Knepley 
9116a6e0b375SMatthew G. Knepley    Calling sequence of func:
911777b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
9118a6e0b375SMatthew G. Knepley 
9119a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
9120a6e0b375SMatthew G. Knepley .  x   - The coordinates
912177b739a6SMatthew Knepley .  Nc  - The number of components
9122a6e0b375SMatthew G. Knepley .  u   - The output field values
9123a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
9124a6e0b375SMatthew G. Knepley 
9125a6e0b375SMatthew G. Knepley   Level: developer
9126a6e0b375SMatthew G. Knepley 
9127a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
9128a6e0b375SMatthew G. Knepley @*/
91292c53366bSMatthew 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)
91302c53366bSMatthew G. Knepley {
91312c53366bSMatthew G. Knepley   Vec            localX;
91322c53366bSMatthew G. Knepley   PetscErrorCode ierr;
91332c53366bSMatthew G. Knepley 
91342c53366bSMatthew G. Knepley   PetscFunctionBegin;
91352c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91362c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
91372c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
91382c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
91392c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
91402c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
91412c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
91422c53366bSMatthew G. Knepley }
91432c53366bSMatthew G. Knepley 
9144a6e0b375SMatthew G. Knepley /*@C
9145a6e0b375SMatthew 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.
9146a6e0b375SMatthew G. Knepley 
9147a6e0b375SMatthew G. Knepley   Not collective
9148a6e0b375SMatthew G. Knepley 
9149a6e0b375SMatthew G. Knepley   Input Parameters:
9150a6e0b375SMatthew G. Knepley + dm      - The DM
9151a6e0b375SMatthew G. Knepley . time    - The time
9152a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
9153a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
9154a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
9155a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9156a6e0b375SMatthew G. Knepley 
9157a6e0b375SMatthew G. Knepley   Output Parameter:
9158a6e0b375SMatthew G. Knepley . localX - vector
9159a6e0b375SMatthew G. Knepley 
9160a6e0b375SMatthew G. Knepley    Calling sequence of func:
916177b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
9162a6e0b375SMatthew G. Knepley 
9163a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
9164a6e0b375SMatthew G. Knepley .  x   - The coordinates
916577b739a6SMatthew Knepley .  Nc  - The number of components
9166a6e0b375SMatthew G. Knepley .  u   - The output field values
9167a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
9168a6e0b375SMatthew G. Knepley 
9169a6e0b375SMatthew G. Knepley   Level: developer
9170a6e0b375SMatthew G. Knepley 
9171a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
9172a6e0b375SMatthew G. Knepley @*/
91731c531cf8SMatthew 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)
91744d6f44ffSToby Isaac {
91754d6f44ffSToby Isaac   PetscErrorCode ierr;
91764d6f44ffSToby Isaac 
91774d6f44ffSToby Isaac   PetscFunctionBegin;
91784d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9179064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
9180*7a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
91811c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
91824d6f44ffSToby Isaac   PetscFunctionReturn(0);
91834d6f44ffSToby Isaac }
91842716604bSToby Isaac 
9185a6e0b375SMatthew G. Knepley /*@C
9186a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
9187a6e0b375SMatthew G. Knepley 
9188a6e0b375SMatthew G. Knepley   Not collective
9189a6e0b375SMatthew G. Knepley 
9190a6e0b375SMatthew G. Knepley   Input Parameters:
9191a6e0b375SMatthew G. Knepley + dm      - The DM
9192a6e0b375SMatthew G. Knepley . time    - The time
9193a6e0b375SMatthew G. Knepley . localU  - The input field vector
9194a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9195a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9196a6e0b375SMatthew G. Knepley 
9197a6e0b375SMatthew G. Knepley   Output Parameter:
9198a6e0b375SMatthew G. Knepley . localX  - The output vector
9199a6e0b375SMatthew G. Knepley 
9200a6e0b375SMatthew G. Knepley    Calling sequence of func:
9201a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9202a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9203a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9204a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9205a6e0b375SMatthew G. Knepley 
9206a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
9207a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
9208a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9209a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
9210a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9211a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
9212a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9213a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9214a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9215a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9216a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9217a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9218a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9219a6e0b375SMatthew G. Knepley .  t            - The current time
9220a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9221a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9222a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9223a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9224a6e0b375SMatthew G. Knepley 
9225a6e0b375SMatthew 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.
9226a6e0b375SMatthew 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
9227a6e0b375SMatthew 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
9228a6e0b375SMatthew 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.
9229a6e0b375SMatthew G. Knepley 
9230a6e0b375SMatthew G. Knepley   Level: intermediate
9231a6e0b375SMatthew G. Knepley 
9232a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9233a6e0b375SMatthew G. Knepley @*/
92348c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
92358c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
92368c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
92378c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9238191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
92398c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
92408c6c5593SMatthew G. Knepley {
92418c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
92428c6c5593SMatthew G. Knepley 
92438c6c5593SMatthew G. Knepley   PetscFunctionBegin;
92448c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
92458c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
92468c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
9247*7a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
92488c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
92498c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
92508c6c5593SMatthew G. Knepley }
92518c6c5593SMatthew G. Knepley 
9252a6e0b375SMatthew G. Knepley /*@C
9253a6e0b375SMatthew 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.
9254a6e0b375SMatthew G. Knepley 
9255a6e0b375SMatthew G. Knepley   Not collective
9256a6e0b375SMatthew G. Knepley 
9257a6e0b375SMatthew G. Knepley   Input Parameters:
9258a6e0b375SMatthew G. Knepley + dm      - The DM
9259a6e0b375SMatthew G. Knepley . time    - The time
9260a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
9261a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
9262a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
9263a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9264a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9265a6e0b375SMatthew G. Knepley . localU  - The input field vector
9266a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9267a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9268a6e0b375SMatthew G. Knepley 
9269a6e0b375SMatthew G. Knepley   Output Parameter:
9270a6e0b375SMatthew G. Knepley . localX  - The output vector
9271a6e0b375SMatthew G. Knepley 
9272a6e0b375SMatthew G. Knepley    Calling sequence of func:
9273a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9274a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9275a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9276a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9277a6e0b375SMatthew G. Knepley 
9278a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
9279a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
9280a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9281a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
9282a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9283a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
9284a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9285a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9286a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9287a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9288a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9289a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9290a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9291a6e0b375SMatthew G. Knepley .  t            - The current time
9292a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9293a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9294a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9295a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9296a6e0b375SMatthew G. Knepley 
9297a6e0b375SMatthew 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.
9298a6e0b375SMatthew 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
9299a6e0b375SMatthew 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
9300a6e0b375SMatthew 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.
9301a6e0b375SMatthew G. Knepley 
9302a6e0b375SMatthew G. Knepley   Level: intermediate
9303a6e0b375SMatthew G. Knepley 
9304a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9305a6e0b375SMatthew G. Knepley @*/
93061c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
93078c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
93088c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
93098c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9310191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
93118c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
93128c6c5593SMatthew G. Knepley {
93138c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
93148c6c5593SMatthew G. Knepley 
93158c6c5593SMatthew G. Knepley   PetscFunctionBegin;
93168c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9317064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9318064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
9319*7a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
93201c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
93218c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
93228c6c5593SMatthew G. Knepley }
93238c6c5593SMatthew G. Knepley 
93242716604bSToby Isaac /*@C
9325ece3a9fcSMatthew 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.
9326ece3a9fcSMatthew G. Knepley 
9327ece3a9fcSMatthew G. Knepley   Not collective
9328ece3a9fcSMatthew G. Knepley 
9329ece3a9fcSMatthew G. Knepley   Input Parameters:
9330ece3a9fcSMatthew G. Knepley + dm      - The DM
9331ece3a9fcSMatthew G. Knepley . time    - The time
9332ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
9333ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
9334ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
9335ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9336ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9337ece3a9fcSMatthew G. Knepley . localU  - The input field vector
9338ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9339ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
9340ece3a9fcSMatthew G. Knepley 
9341ece3a9fcSMatthew G. Knepley   Output Parameter:
9342ece3a9fcSMatthew G. Knepley . localX  - The output vector
9343ece3a9fcSMatthew G. Knepley 
9344ece3a9fcSMatthew G. Knepley    Calling sequence of func:
9345ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9346ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9347ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9348ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9349ece3a9fcSMatthew G. Knepley 
9350ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
9351ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
9352ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9353ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
9354ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9355ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
9356ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9357ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9358ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9359ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9360ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9361ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9362ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9363ece3a9fcSMatthew G. Knepley .  t            - The current time
9364ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
9365ece3a9fcSMatthew G. Knepley .  n            - The face normal
9366ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
9367ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
9368ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
9369ece3a9fcSMatthew G. Knepley 
9370ece3a9fcSMatthew G. Knepley   Note:
9371ece3a9fcSMatthew 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.
9372ece3a9fcSMatthew 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
9373ece3a9fcSMatthew 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
9374ece3a9fcSMatthew 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.
9375ece3a9fcSMatthew G. Knepley 
9376ece3a9fcSMatthew G. Knepley   Level: intermediate
9377ece3a9fcSMatthew G. Knepley 
9378ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9379ece3a9fcSMatthew G. Knepley @*/
9380ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
9381ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
9382ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9383ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9384ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
9385ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
9386ece3a9fcSMatthew G. Knepley {
9387ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
9388ece3a9fcSMatthew G. Knepley 
9389ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
9390ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9391064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9392064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
9393*7a8be351SBarry Smith   PetscCheck(dm->ops->projectbdfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
9394ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
9395ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
9396ece3a9fcSMatthew G. Knepley }
9397ece3a9fcSMatthew G. Knepley 
9398ece3a9fcSMatthew G. Knepley /*@C
93992716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
94002716604bSToby Isaac 
94012716604bSToby Isaac   Input Parameters:
94022716604bSToby Isaac + dm    - The DM
94030709b2feSToby Isaac . time  - The time
94042716604bSToby Isaac . funcs - The functions to evaluate for each field component
94052716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9406574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
94072716604bSToby Isaac 
94082716604bSToby Isaac   Output Parameter:
94092716604bSToby Isaac . diff - The diff ||u - u_h||_2
94102716604bSToby Isaac 
94112716604bSToby Isaac   Level: developer
94122716604bSToby Isaac 
94131189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
94142716604bSToby Isaac @*/
94150709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
94162716604bSToby Isaac {
94172716604bSToby Isaac   PetscErrorCode ierr;
94182716604bSToby Isaac 
94192716604bSToby Isaac   PetscFunctionBegin;
94202716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9421b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
9422*7a8be351SBarry Smith   PetscCheck(dm->ops->computel2diff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
94230709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
94242716604bSToby Isaac   PetscFunctionReturn(0);
94252716604bSToby Isaac }
9426b698f381SToby Isaac 
9427b698f381SToby Isaac /*@C
9428b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
9429b698f381SToby Isaac 
9430d083f849SBarry Smith   Collective on dm
9431d083f849SBarry Smith 
9432b698f381SToby Isaac   Input Parameters:
9433b698f381SToby Isaac + dm    - The DM
9434b698f381SToby Isaac , time  - The time
9435b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
9436b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9437574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
9438b698f381SToby Isaac - n     - The vector to project along
9439b698f381SToby Isaac 
9440b698f381SToby Isaac   Output Parameter:
9441b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
9442b698f381SToby Isaac 
9443b698f381SToby Isaac   Level: developer
9444b698f381SToby Isaac 
9445b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
9446b698f381SToby Isaac @*/
9447b698f381SToby 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)
9448b698f381SToby Isaac {
9449b698f381SToby Isaac   PetscErrorCode ierr;
9450b698f381SToby Isaac 
9451b698f381SToby Isaac   PetscFunctionBegin;
9452b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9453b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
9454*7a8be351SBarry Smith   PetscCheck(dm->ops->computel2gradientdiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
9455b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
9456b698f381SToby Isaac   PetscFunctionReturn(0);
9457b698f381SToby Isaac }
9458b698f381SToby Isaac 
94592a16baeaSToby Isaac /*@C
94602a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
94612a16baeaSToby Isaac 
9462d083f849SBarry Smith   Collective on dm
9463d083f849SBarry Smith 
94642a16baeaSToby Isaac   Input Parameters:
94652a16baeaSToby Isaac + dm    - The DM
94662a16baeaSToby Isaac . time  - The time
94672a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
94682a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9469574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
94702a16baeaSToby Isaac 
94712a16baeaSToby Isaac   Output Parameter:
94722a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
94732a16baeaSToby Isaac 
94742a16baeaSToby Isaac   Level: developer
94752a16baeaSToby Isaac 
94761189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
94772a16baeaSToby Isaac @*/
94781189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
94792a16baeaSToby Isaac {
94802a16baeaSToby Isaac   PetscErrorCode ierr;
94812a16baeaSToby Isaac 
94822a16baeaSToby Isaac   PetscFunctionBegin;
94832a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
94842a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
9485*7a8be351SBarry Smith   PetscCheck(dm->ops->computel2fielddiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
94862a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
94872a16baeaSToby Isaac   PetscFunctionReturn(0);
94882a16baeaSToby Isaac }
94892a16baeaSToby Isaac 
9490df0b854cSToby Isaac /*@C
9491502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
9492502a2867SDave May 
9493502a2867SDave May  Not Collective
9494502a2867SDave May 
9495502a2867SDave May  Input Parameter:
9496502a2867SDave May .  dm    - The DM
9497502a2867SDave May 
94980a19bb7dSprj-  Output Parameters:
94990a19bb7dSprj- +  nranks - the number of neighbours
95000a19bb7dSprj- -  ranks - the neighbors ranks
9501502a2867SDave May 
9502502a2867SDave May  Notes:
9503502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
9504502a2867SDave May 
9505502a2867SDave May  Level: beginner
9506502a2867SDave May 
9507dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
9508502a2867SDave May @*/
9509502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
9510502a2867SDave May {
9511502a2867SDave May   PetscErrorCode ierr;
9512502a2867SDave May 
9513502a2867SDave May   PetscFunctionBegin;
9514502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9515*7a8be351SBarry Smith   PetscCheck(dm->ops->getneighbors,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
9516502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
9517502a2867SDave May   PetscFunctionReturn(0);
9518502a2867SDave May }
9519502a2867SDave May 
9520531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
9521531c7667SBarry Smith 
9522531c7667SBarry Smith /*
9523531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
9524531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
9525531c7667SBarry Smith */
9526531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
9527531c7667SBarry Smith {
9528531c7667SBarry Smith   PetscErrorCode ierr;
9529531c7667SBarry Smith 
9530531c7667SBarry Smith   PetscFunctionBegin;
9531531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9532531c7667SBarry Smith     Vec x1local;
9533531c7667SBarry Smith     DM  dm;
9534531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9535*7a8be351SBarry Smith     PetscCheck(dm,PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
9536531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
9537531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9538531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
9539531c7667SBarry Smith     x1   = x1local;
9540531c7667SBarry Smith   }
9541531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
9542531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9543531c7667SBarry Smith     DM  dm;
9544531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
9545531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
9546531c7667SBarry Smith   }
9547531c7667SBarry Smith   PetscFunctionReturn(0);
9548531c7667SBarry Smith }
9549531c7667SBarry Smith 
9550531c7667SBarry Smith /*@
9551531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
9552531c7667SBarry Smith 
9553531c7667SBarry Smith     Input Parameter:
9554531c7667SBarry Smith .    coloring - the MatFDColoring object
9555531c7667SBarry Smith 
955695452b02SPatrick Sanan     Developer Notes:
955795452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
9558531c7667SBarry Smith 
95591b266c99SBarry Smith     Level: advanced
95601b266c99SBarry Smith 
9561531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
9562531c7667SBarry Smith @*/
9563531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
9564531c7667SBarry Smith {
9565531c7667SBarry Smith   PetscFunctionBegin;
9566531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
9567531c7667SBarry Smith   PetscFunctionReturn(0);
9568531c7667SBarry Smith }
95698320bc6fSPatrick Sanan 
95708320bc6fSPatrick Sanan /*@
95718320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
95728320bc6fSPatrick Sanan 
95738320bc6fSPatrick Sanan     Collective
95748320bc6fSPatrick Sanan 
95758320bc6fSPatrick Sanan     Input Parameters:
9576a5bc1bf3SBarry Smith +    dm1 - the first DM
95778320bc6fSPatrick Sanan -    dm2 - the second DM
95788320bc6fSPatrick Sanan 
95798320bc6fSPatrick Sanan     Output Parameters:
95808320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
95818320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
95828320bc6fSPatrick Sanan 
95838320bc6fSPatrick Sanan     Notes:
95848320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
95853d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
95868320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
95873d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
95883d862458SPatrick Sanan     decomposition, but hold different data.
95898320bc6fSPatrick Sanan 
95908320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
95918320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
95928320bc6fSPatrick Sanan 
95938320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
95948320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
95958320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
95968320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
95978320bc6fSPatrick Sanan 
95988320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
95998320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
96008320bc6fSPatrick Sanan .vb
96018320bc6fSPatrick Sanan   ...
96028320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
96038320bc6fSPatrick Sanan   if (set && compatible)  {
96048320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
96058320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
96063d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
96078320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
96088320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
96098320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
96108320bc6fSPatrick Sanan       }
96118320bc6fSPatrick Sanan     }
96128320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
96138320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
96148320bc6fSPatrick Sanan   } else {
96158320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
96168320bc6fSPatrick Sanan   }
96178320bc6fSPatrick Sanan   ...
96188320bc6fSPatrick Sanan .ve
96198320bc6fSPatrick Sanan 
96208320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
96218320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
96228320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
96238320bc6fSPatrick Sanan     always check the "set" output parameter.
96248320bc6fSPatrick Sanan 
96258320bc6fSPatrick Sanan     A DM is always compatible with itself.
96268320bc6fSPatrick Sanan 
96278320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
96288320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
96298320bc6fSPatrick Sanan     incompatible.
96308320bc6fSPatrick Sanan 
96318320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
96328320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
96338320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
96348320bc6fSPatrick Sanan 
96358320bc6fSPatrick Sanan     Developer Notes:
96363d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
96373d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
9638a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
96398320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
96408320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
96413d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
96423d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
96438320bc6fSPatrick Sanan 
96448320bc6fSPatrick Sanan     Level: advanced
96458320bc6fSPatrick Sanan 
96463d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
96478320bc6fSPatrick Sanan @*/
96488320bc6fSPatrick Sanan 
9649a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
96508320bc6fSPatrick Sanan {
96518320bc6fSPatrick Sanan   PetscErrorCode ierr;
96528320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
96538320bc6fSPatrick Sanan   DMType         type,type2;
96548320bc6fSPatrick Sanan   PetscBool      sameType;
96558320bc6fSPatrick Sanan 
96568320bc6fSPatrick Sanan   PetscFunctionBegin;
9657a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
96588320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
96598320bc6fSPatrick Sanan 
96608320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
9661a5bc1bf3SBarry Smith   if (dm1 == dm2) {
96628320bc6fSPatrick Sanan     *set = PETSC_TRUE;
96638320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
96648320bc6fSPatrick Sanan     PetscFunctionReturn(0);
96658320bc6fSPatrick Sanan   }
96668320bc6fSPatrick Sanan 
96678320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
96688320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
96698320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
96708320bc6fSPatrick Sanan      determined by the implementation-specific logic */
9671ffc4695bSBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRMPI(ierr);
96728320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
96738320bc6fSPatrick Sanan     *set = PETSC_TRUE;
96748320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
96758320bc6fSPatrick Sanan     PetscFunctionReturn(0);
96768320bc6fSPatrick Sanan   }
96778320bc6fSPatrick Sanan 
96788320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
9679a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
9680a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
9681b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
96828320bc6fSPatrick Sanan   }
96838320bc6fSPatrick Sanan 
9684a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
96858320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
9686a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
96878320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
96888320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
96898320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
9690a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
96918320bc6fSPatrick Sanan   } else {
96928320bc6fSPatrick Sanan     *set = PETSC_FALSE;
96938320bc6fSPatrick Sanan   }
96948320bc6fSPatrick Sanan   PetscFunctionReturn(0);
96958320bc6fSPatrick Sanan }
9696c0f0dcc3SMatthew G. Knepley 
9697c0f0dcc3SMatthew G. Knepley /*@C
9698c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
9699c0f0dcc3SMatthew G. Knepley 
9700c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9701c0f0dcc3SMatthew G. Knepley 
9702c0f0dcc3SMatthew G. Knepley   Input Parameters:
9703c0f0dcc3SMatthew G. Knepley + DM - the DM
9704c0f0dcc3SMatthew G. Knepley . f - the monitor function
9705c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
9706c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
9707c0f0dcc3SMatthew G. Knepley 
9708c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9709c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
9710c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
9711c0f0dcc3SMatthew G. Knepley 
9712c0f0dcc3SMatthew G. Knepley   Notes:
9713c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
9714c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
9715c0f0dcc3SMatthew G. Knepley   order in which they were set.
9716c0f0dcc3SMatthew G. Knepley 
9717c0f0dcc3SMatthew G. Knepley   Fortran Notes:
9718c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
9719c0f0dcc3SMatthew G. Knepley 
9720c0f0dcc3SMatthew G. Knepley   Level: intermediate
9721c0f0dcc3SMatthew G. Knepley 
9722c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
9723c0f0dcc3SMatthew G. Knepley @*/
9724c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
9725c0f0dcc3SMatthew G. Knepley {
9726c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9727c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9728c0f0dcc3SMatthew G. Knepley 
9729c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9730c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9731c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9732c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9733c0f0dcc3SMatthew G. Knepley 
9734c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
9735c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9736c0f0dcc3SMatthew G. Knepley   }
9737*7a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9738c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9739c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9740c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9741c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9742c0f0dcc3SMatthew G. Knepley }
9743c0f0dcc3SMatthew G. Knepley 
9744c0f0dcc3SMatthew G. Knepley /*@
9745c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9746c0f0dcc3SMatthew G. Knepley 
9747c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9748c0f0dcc3SMatthew G. Knepley 
9749c0f0dcc3SMatthew G. Knepley   Input Parameter:
9750c0f0dcc3SMatthew G. Knepley . dm - the DM
9751c0f0dcc3SMatthew G. Knepley 
9752c0f0dcc3SMatthew G. Knepley   Options Database Key:
9753c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9754c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9755c0f0dcc3SMatthew G. Knepley   set via the options database
9756c0f0dcc3SMatthew G. Knepley 
9757c0f0dcc3SMatthew G. Knepley   Notes:
9758c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9759c0f0dcc3SMatthew G. Knepley 
9760c0f0dcc3SMatthew G. Knepley   Level: intermediate
9761c0f0dcc3SMatthew G. Knepley 
9762c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9763c0f0dcc3SMatthew G. Knepley @*/
9764c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9765c0f0dcc3SMatthew G. Knepley {
9766c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9767c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9768c0f0dcc3SMatthew G. Knepley 
9769c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9770c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9771c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9772c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
9773c0f0dcc3SMatthew G. Knepley   }
9774c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9775c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9776c0f0dcc3SMatthew G. Knepley }
9777c0f0dcc3SMatthew G. Knepley 
9778c0f0dcc3SMatthew G. Knepley /*@C
9779c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9780c0f0dcc3SMatthew G. Knepley 
9781c0f0dcc3SMatthew G. Knepley   Collective on DM
9782c0f0dcc3SMatthew G. Knepley 
9783c0f0dcc3SMatthew G. Knepley   Input Parameters:
9784c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9785c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9786c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9787c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9788c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9789c0f0dcc3SMatthew 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
9790c0f0dcc3SMatthew G. Knepley 
9791c0f0dcc3SMatthew G. Knepley   Output Parameter:
9792c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9793c0f0dcc3SMatthew G. Knepley 
9794c0f0dcc3SMatthew G. Knepley   Level: developer
9795c0f0dcc3SMatthew G. Knepley 
9796c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9797c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9798c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9799c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9800c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9801c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9802c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9803c0f0dcc3SMatthew G. Knepley @*/
9804c0f0dcc3SMatthew 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)
9805c0f0dcc3SMatthew G. Knepley {
9806c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9807c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9808c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
9809c0f0dcc3SMatthew G. Knepley 
9810c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9811c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9812c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
9813c0f0dcc3SMatthew G. Knepley   if (*flg) {
9814c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9815c0f0dcc3SMatthew G. Knepley 
9816c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
9817c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
9818c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
9819c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
9820c0f0dcc3SMatthew G. Knepley   }
9821c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9822c0f0dcc3SMatthew G. Knepley }
9823c0f0dcc3SMatthew G. Knepley 
9824c0f0dcc3SMatthew G. Knepley /*@
9825c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9826c0f0dcc3SMatthew G. Knepley 
9827c0f0dcc3SMatthew G. Knepley    Collective on DM
9828c0f0dcc3SMatthew G. Knepley 
9829c0f0dcc3SMatthew G. Knepley    Input Parameters:
9830c0f0dcc3SMatthew G. Knepley .  dm - The DM
9831c0f0dcc3SMatthew G. Knepley 
9832c0f0dcc3SMatthew G. Knepley    Level: developer
9833c0f0dcc3SMatthew G. Knepley 
9834c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9835c0f0dcc3SMatthew G. Knepley @*/
9836c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9837c0f0dcc3SMatthew G. Knepley {
9838c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9839c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9840c0f0dcc3SMatthew G. Knepley 
9841c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9842c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9843c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9844c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9845c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
9846c0f0dcc3SMatthew G. Knepley   }
9847c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9848c0f0dcc3SMatthew G. Knepley }
98492e4af2aeSMatthew G. Knepley 
98502e4af2aeSMatthew G. Knepley /*@
98512e4af2aeSMatthew G. Knepley   DMComputeError - Computes the error assuming the user has given exact solution functions
98522e4af2aeSMatthew G. Knepley 
98532e4af2aeSMatthew G. Knepley   Collective on DM
98542e4af2aeSMatthew G. Knepley 
98552e4af2aeSMatthew G. Knepley   Input Parameters:
98562e4af2aeSMatthew G. Knepley + dm     - The DM
98576b867d5aSJose E. Roman - sol    - The solution vector
98582e4af2aeSMatthew G. Knepley 
98596b867d5aSJose E. Roman   Input/Output Parameter:
98606b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
98616b867d5aSJose E. Roman            contains the error in each field
98626b867d5aSJose E. Roman 
98636b867d5aSJose E. Roman   Output Parameter:
98646b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
98652e4af2aeSMatthew G. Knepley 
98662e4af2aeSMatthew G. Knepley   Note: The exact solutions come from the PetscDS object, and the time comes from DMGetOutputSequenceNumber().
98672e4af2aeSMatthew G. Knepley 
98682e4af2aeSMatthew G. Knepley   Level: developer
98692e4af2aeSMatthew G. Knepley 
98702e4af2aeSMatthew G. Knepley .seealso: DMMonitorSet(), DMGetRegionNumDS(), PetscDSGetExactSolution(), DMGetOutputSequenceNumber()
98712e4af2aeSMatthew G. Knepley @*/
98722e4af2aeSMatthew G. Knepley PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
98732e4af2aeSMatthew G. Knepley {
98742e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
98752e4af2aeSMatthew G. Knepley   void            **ctxs;
98762e4af2aeSMatthew G. Knepley   PetscReal         time;
98772e4af2aeSMatthew G. Knepley   PetscInt          Nf, f, Nds, s;
98782e4af2aeSMatthew G. Knepley   PetscErrorCode    ierr;
98792e4af2aeSMatthew G. Knepley 
98802e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
98812e4af2aeSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
98822e4af2aeSMatthew G. Knepley   ierr = PetscCalloc2(Nf, &exactSol, Nf, &ctxs);CHKERRQ(ierr);
98832e4af2aeSMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
98842e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
98852e4af2aeSMatthew G. Knepley     PetscDS         ds;
98862e4af2aeSMatthew G. Knepley     DMLabel         label;
98872e4af2aeSMatthew G. Knepley     IS              fieldIS;
98882e4af2aeSMatthew G. Knepley     const PetscInt *fields;
98892e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
98902e4af2aeSMatthew G. Knepley 
98912e4af2aeSMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
98922e4af2aeSMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
98932e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);}
98942e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
98952e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
98962e4af2aeSMatthew G. Knepley       ierr = PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]);CHKERRQ(ierr);
98972e4af2aeSMatthew G. Knepley     }
98982e4af2aeSMatthew G. Knepley     if (fieldIS) {ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);}
98992e4af2aeSMatthew G. Knepley   }
99002e4af2aeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
9901*7a8be351SBarry Smith     PetscCheck(exactSol[f],PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "DS must contain exact solution functions in order to calculate error, missing for field %D", f);
99022e4af2aeSMatthew G. Knepley   }
99032e4af2aeSMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
99042e4af2aeSMatthew G. Knepley   if (errors) {ierr = DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors);CHKERRQ(ierr);}
99052e4af2aeSMatthew G. Knepley   if (errorVec) {
99062e4af2aeSMatthew G. Knepley     DM             edm;
99072e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
99082e4af2aeSMatthew G. Knepley     PetscBool      simplex;
99092e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
99102e4af2aeSMatthew G. Knepley 
99112e4af2aeSMatthew G. Knepley     ierr = DMClone(dm, &edm);CHKERRQ(ierr);
99122e4af2aeSMatthew G. Knepley     ierr = DMGetDimension(edm, &dim);CHKERRQ(ierr);
99132e4af2aeSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr);
99142e4af2aeSMatthew G. Knepley     ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
99152e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
99162e4af2aeSMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
99172e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
99182e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
99192e4af2aeSMatthew G. Knepley       PetscQuadrature q;
99202e4af2aeSMatthew G. Knepley       const char     *name;
99212e4af2aeSMatthew G. Knepley 
99222e4af2aeSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
99232e4af2aeSMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe);CHKERRQ(ierr);
99242e4af2aeSMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
99252e4af2aeSMatthew G. Knepley       ierr = PetscObjectSetName((PetscObject) efe, name);CHKERRQ(ierr);
99262e4af2aeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
99272e4af2aeSMatthew G. Knepley       ierr = PetscFESetQuadrature(efe, q);CHKERRQ(ierr);
99282e4af2aeSMatthew G. Knepley       ierr = DMSetField(edm, f, NULL, (PetscObject) efe);CHKERRQ(ierr);
99292e4af2aeSMatthew G. Knepley       ierr = PetscFEDestroy(&efe);CHKERRQ(ierr);
99302e4af2aeSMatthew G. Knepley     }
99312e4af2aeSMatthew G. Knepley     ierr = DMCreateDS(edm);CHKERRQ(ierr);
99322e4af2aeSMatthew G. Knepley 
99331e1ea65dSPierre Jolivet     ierr = DMCreateGlobalVector(edm, errorVec);CHKERRQ(ierr);
99342e4af2aeSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) *errorVec, "Error");CHKERRQ(ierr);
99352e4af2aeSMatthew G. Knepley     ierr = DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec);CHKERRQ(ierr);
99362e4af2aeSMatthew G. Knepley     ierr = DMDestroy(&edm);CHKERRQ(ierr);
99372e4af2aeSMatthew G. Knepley   }
99382e4af2aeSMatthew G. Knepley   ierr = PetscFree2(exactSol, ctxs);CHKERRQ(ierr);
99392e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
99402e4af2aeSMatthew G. Knepley }
99419a2a23afSMatthew G. Knepley 
99429a2a23afSMatthew G. Knepley /*@
99439a2a23afSMatthew G. Knepley   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this DM
99449a2a23afSMatthew G. Knepley 
99459a2a23afSMatthew G. Knepley   Not collective
99469a2a23afSMatthew G. Knepley 
99479a2a23afSMatthew G. Knepley   Input Parameter:
99489a2a23afSMatthew G. Knepley . dm     - The DM
99499a2a23afSMatthew G. Knepley 
99509a2a23afSMatthew G. Knepley   Output Parameter:
9951a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
99529a2a23afSMatthew G. Knepley 
99539a2a23afSMatthew G. Knepley   Level: advanced
99549a2a23afSMatthew G. Knepley 
99559a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryLabels(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
99569a2a23afSMatthew G. Knepley @*/
99579a2a23afSMatthew G. Knepley PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
99589a2a23afSMatthew G. Knepley {
99599a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
99609a2a23afSMatthew G. Knepley 
99619a2a23afSMatthew G. Knepley   PetscFunctionBegin;
99629a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
99639a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetSize(dm->auxData, numAux);CHKERRQ(ierr);
99649a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
99659a2a23afSMatthew G. Knepley }
99669a2a23afSMatthew G. Knepley 
99679a2a23afSMatthew G. Knepley /*@
9968ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
99699a2a23afSMatthew G. Knepley 
99709a2a23afSMatthew G. Knepley   Not collective
99719a2a23afSMatthew G. Knepley 
99729a2a23afSMatthew G. Knepley   Input Parameters:
99739a2a23afSMatthew G. Knepley + dm     - The DM
99749a2a23afSMatthew G. Knepley . label  - The DMLabel
9975ac17215fSMatthew G. Knepley . value  - The label value indicating the region
9976ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
99779a2a23afSMatthew G. Knepley 
99789a2a23afSMatthew G. Knepley   Output Parameter:
99799a2a23afSMatthew G. Knepley . aux    - The Vec holding auxiliary field data
99809a2a23afSMatthew G. Knepley 
9981ac17215fSMatthew G. Knepley   Note: If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
998204c51a94SMatthew G. Knepley 
99839a2a23afSMatthew G. Knepley   Level: advanced
99849a2a23afSMatthew G. Knepley 
99859a2a23afSMatthew G. Knepley .seealso: DMSetAuxiliaryVec(), DMGetNumAuxiliaryVec()
99869a2a23afSMatthew G. Knepley @*/
9987ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
99889a2a23afSMatthew G. Knepley {
9989ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
999004c51a94SMatthew G. Knepley   PetscBool       has;
99919a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
99929a2a23afSMatthew G. Knepley 
99939a2a23afSMatthew G. Knepley   PetscFunctionBegin;
99949a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
99959a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
99969a2a23afSMatthew G. Knepley   key.label = label;
99979a2a23afSMatthew G. Knepley   key.value = value;
9998ac17215fSMatthew G. Knepley   key.part  = part;
999904c51a94SMatthew G. Knepley   ierr = PetscHMapAuxHas(dm->auxData, key, &has);CHKERRQ(ierr);
1000004c51a94SMatthew G. Knepley   if (has) {ierr = PetscHMapAuxGet(dm->auxData, key,  aux);CHKERRQ(ierr);}
1000104c51a94SMatthew G. Knepley   else     {ierr = PetscHMapAuxGet(dm->auxData, wild, aux);CHKERRQ(ierr);}
100029a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
100039a2a23afSMatthew G. Knepley }
100049a2a23afSMatthew G. Knepley 
100059a2a23afSMatthew G. Knepley /*@
10006ac17215fSMatthew G. Knepley   DMSetAuxiliaryVec - Set the auxiliary vector for region specified by the given label and value, and equation part
100079a2a23afSMatthew G. Knepley 
100089a2a23afSMatthew G. Knepley   Not collective
100099a2a23afSMatthew G. Knepley 
100109a2a23afSMatthew G. Knepley   Input Parameters:
100119a2a23afSMatthew G. Knepley + dm     - The DM
100129a2a23afSMatthew G. Knepley . label  - The DMLabel
100139a2a23afSMatthew G. Knepley . value  - The label value indicating the region
10014ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
100159a2a23afSMatthew G. Knepley - aux    - The Vec holding auxiliary field data
100169a2a23afSMatthew G. Knepley 
100179a2a23afSMatthew G. Knepley   Level: advanced
100189a2a23afSMatthew G. Knepley 
100199a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryVec()
100209a2a23afSMatthew G. Knepley @*/
10021ac17215fSMatthew G. Knepley PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
100229a2a23afSMatthew G. Knepley {
100239a2a23afSMatthew G. Knepley   Vec             old;
100249a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
100259a2a23afSMatthew G. Knepley   PetscErrorCode  ierr;
100269a2a23afSMatthew G. Knepley 
100279a2a23afSMatthew G. Knepley   PetscFunctionBegin;
100289a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
100299a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
100309a2a23afSMatthew G. Knepley   key.label = label;
100319a2a23afSMatthew G. Knepley   key.value = value;
10032ac17215fSMatthew G. Knepley   key.part  = part;
100339a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGet(dm->auxData, key, &old);CHKERRQ(ierr);
100349a2a23afSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) aux);CHKERRQ(ierr);
100359a2a23afSMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) old);CHKERRQ(ierr);
100369a2a23afSMatthew G. Knepley   if (!aux) {ierr = PetscHMapAuxDel(dm->auxData, key);CHKERRQ(ierr);}
100379a2a23afSMatthew G. Knepley   else      {ierr = PetscHMapAuxSet(dm->auxData, key, aux);CHKERRQ(ierr);}
100389a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
100399a2a23afSMatthew G. Knepley }
100409a2a23afSMatthew G. Knepley 
100419a2a23afSMatthew G. Knepley /*@C
10042ac17215fSMatthew G. Knepley   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this DM
100439a2a23afSMatthew G. Knepley 
100449a2a23afSMatthew G. Knepley   Not collective
100459a2a23afSMatthew G. Knepley 
100469a2a23afSMatthew G. Knepley   Input Parameter:
100479a2a23afSMatthew G. Knepley . dm      - The DM
100489a2a23afSMatthew G. Knepley 
100499a2a23afSMatthew G. Knepley   Output Parameters:
100509a2a23afSMatthew G. Knepley + labels  - The DMLabels for each Vec
10051ac17215fSMatthew G. Knepley . values  - The label values for each Vec
10052ac17215fSMatthew G. Knepley - parts   - The equation parts for each Vec
100539a2a23afSMatthew G. Knepley 
100549a2a23afSMatthew G. Knepley   Note: The arrays passed in must be at least as large as DMGetNumAuxiliaryVec().
100559a2a23afSMatthew G. Knepley 
100569a2a23afSMatthew G. Knepley   Level: advanced
100579a2a23afSMatthew G. Knepley 
100589a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
100599a2a23afSMatthew G. Knepley @*/
10060ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
100619a2a23afSMatthew G. Knepley {
100629a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
100639a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
100649a2a23afSMatthew G. Knepley   PetscErrorCode   ierr;
100659a2a23afSMatthew G. Knepley 
100669a2a23afSMatthew G. Knepley   PetscFunctionBegin;
100679a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
100689a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
100699a2a23afSMatthew G. Knepley   PetscValidPointer(values, 3);
10070ac17215fSMatthew G. Knepley   PetscValidPointer(parts,  4);
100719a2a23afSMatthew G. Knepley   ierr = DMGetNumAuxiliaryVec(dm, &n);CHKERRQ(ierr);
100729a2a23afSMatthew G. Knepley   ierr = PetscMalloc1(n, &keys);CHKERRQ(ierr);
100739a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxGetKeys(dm->auxData, &off, keys);CHKERRQ(ierr);
10074ac17215fSMatthew G. Knepley   for (i = 0; i < n; ++i) {labels[i] = keys[i].label; values[i] = keys[i].value; parts[i] = keys[i].part;}
100759a2a23afSMatthew G. Knepley   ierr = PetscFree(keys);CHKERRQ(ierr);
100769a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
100779a2a23afSMatthew G. Knepley }
100789a2a23afSMatthew G. Knepley 
100799a2a23afSMatthew G. Knepley /*@
100809a2a23afSMatthew G. Knepley   DMCopyAuxiliaryVec - Copy the auxiliary data to a new DM
100819a2a23afSMatthew G. Knepley 
100829a2a23afSMatthew G. Knepley   Not collective
100839a2a23afSMatthew G. Knepley 
100849a2a23afSMatthew G. Knepley   Input Parameter:
100859a2a23afSMatthew G. Knepley . dm    - The DM
100869a2a23afSMatthew G. Knepley 
100879a2a23afSMatthew G. Knepley   Output Parameter:
100889a2a23afSMatthew G. Knepley . dmNew - The new DM, now with the same auxiliary data
100899a2a23afSMatthew G. Knepley 
100909a2a23afSMatthew G. Knepley   Level: advanced
100919a2a23afSMatthew G. Knepley 
100929a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
100939a2a23afSMatthew G. Knepley @*/
100949a2a23afSMatthew G. Knepley PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
100959a2a23afSMatthew G. Knepley {
100969a2a23afSMatthew G. Knepley   PetscErrorCode ierr;
100979a2a23afSMatthew G. Knepley 
100989a2a23afSMatthew G. Knepley   PetscFunctionBegin;
100999a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
101009a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDestroy(&dmNew->auxData);CHKERRQ(ierr);
101019a2a23afSMatthew G. Knepley   ierr = PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData);CHKERRQ(ierr);
101029a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
101039a2a23afSMatthew G. Knepley }
10104b5a892a1SMatthew G. Knepley 
10105b5a892a1SMatthew G. Knepley /*@C
10106b5a892a1SMatthew G. Knepley   DMPolytopeMatchOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
10107b5a892a1SMatthew G. Knepley 
10108b5a892a1SMatthew G. Knepley   Not collective
10109b5a892a1SMatthew G. Knepley 
10110b5a892a1SMatthew G. Knepley   Input Parameters:
10111b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10112b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
10113b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
10114b5a892a1SMatthew G. Knepley 
10115b5a892a1SMatthew G. Knepley   Output Parameters:
10116b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
10117b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
10118b5a892a1SMatthew G. Knepley 
10119b5a892a1SMatthew G. Knepley   Level: advanced
10120b5a892a1SMatthew G. Knepley 
10121b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchVertexOrientation()
10122b5a892a1SMatthew G. Knepley @*/
10123b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
10124b5a892a1SMatthew G. Knepley {
10125b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
10126b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
10127b5a892a1SMatthew G. Knepley   PetscInt       o, c;
10128b5a892a1SMatthew G. Knepley 
10129b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10130b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
10131b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
10132b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
10133b5a892a1SMatthew G. Knepley 
10134b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceCone[arr[c*2]] != targetCone[c]) break;
10135b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
10136b5a892a1SMatthew G. Knepley   }
10137b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
10138b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10139b5a892a1SMatthew G. Knepley }
10140b5a892a1SMatthew G. Knepley 
10141b5a892a1SMatthew G. Knepley /*@C
10142b5a892a1SMatthew G. Knepley   DMPolytopeGetOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
10143b5a892a1SMatthew G. Knepley 
10144b5a892a1SMatthew G. Knepley   Not collective
10145b5a892a1SMatthew G. Knepley 
10146b5a892a1SMatthew G. Knepley   Input Parameters:
10147b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10148b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
10149b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
10150b5a892a1SMatthew G. Knepley 
10151b5a892a1SMatthew G. Knepley   Output Parameters:
10152b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
10153b5a892a1SMatthew G. Knepley 
10154b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
10155b5a892a1SMatthew G. Knepley 
10156b5a892a1SMatthew G. Knepley   Level: advanced
10157b5a892a1SMatthew G. Knepley 
10158b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchOrientation(), DMPolytopeGetVertexOrientation()
10159b5a892a1SMatthew G. Knepley @*/
10160b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
10161b5a892a1SMatthew G. Knepley {
10162b5a892a1SMatthew G. Knepley   PetscBool      found;
10163b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
10164b5a892a1SMatthew G. Knepley 
10165b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10166b5a892a1SMatthew G. Knepley   ierr = DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found);CHKERRQ(ierr);
10167*7a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
10168b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10169b5a892a1SMatthew G. Knepley }
10170b5a892a1SMatthew G. Knepley 
10171b5a892a1SMatthew G. Knepley /*@C
10172b5a892a1SMatthew G. Knepley   DMPolytopeMatchVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
10173b5a892a1SMatthew G. Knepley 
10174b5a892a1SMatthew G. Knepley   Not collective
10175b5a892a1SMatthew G. Knepley 
10176b5a892a1SMatthew G. Knepley   Input Parameters:
10177b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10178b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
10179b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
10180b5a892a1SMatthew G. Knepley 
10181b5a892a1SMatthew G. Knepley   Output Parameters:
10182b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
10183b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
10184b5a892a1SMatthew G. Knepley 
10185b5a892a1SMatthew G. Knepley   Level: advanced
10186b5a892a1SMatthew G. Knepley 
10187b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchOrientation()
10188b5a892a1SMatthew G. Knepley @*/
10189b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
10190b5a892a1SMatthew G. Knepley {
10191b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
10192b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
10193b5a892a1SMatthew G. Knepley   PetscInt       o, c;
10194b5a892a1SMatthew G. Knepley 
10195b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10196b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
10197b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
10198b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
10199b5a892a1SMatthew G. Knepley 
10200b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceVert[arr[c]] != targetVert[c]) break;
10201b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
10202b5a892a1SMatthew G. Knepley   }
10203b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
10204b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10205b5a892a1SMatthew G. Knepley }
10206b5a892a1SMatthew G. Knepley 
10207b5a892a1SMatthew G. Knepley /*@C
10208b5a892a1SMatthew G. Knepley   DMPolytopeGetVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
10209b5a892a1SMatthew G. Knepley 
10210b5a892a1SMatthew G. Knepley   Not collective
10211b5a892a1SMatthew G. Knepley 
10212b5a892a1SMatthew G. Knepley   Input Parameters:
10213b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
10214b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
10215b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
10216b5a892a1SMatthew G. Knepley 
10217b5a892a1SMatthew G. Knepley   Output Parameters:
10218b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
10219b5a892a1SMatthew G. Knepley 
10220b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
10221b5a892a1SMatthew G. Knepley 
10222b5a892a1SMatthew G. Knepley   Level: advanced
10223b5a892a1SMatthew G. Knepley 
10224b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchVertexOrientation(), DMPolytopeGetOrientation()
10225b5a892a1SMatthew G. Knepley @*/
10226b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
10227b5a892a1SMatthew G. Knepley {
10228b5a892a1SMatthew G. Knepley   PetscBool      found;
10229b5a892a1SMatthew G. Knepley   PetscErrorCode ierr;
10230b5a892a1SMatthew G. Knepley 
10231b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
10232b5a892a1SMatthew G. Knepley   ierr = DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found);CHKERRQ(ierr);
10233*7a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
10234b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
10235b5a892a1SMatthew G. Knepley }
10236012bc364SMatthew G. Knepley 
10237012bc364SMatthew G. Knepley /*@C
10238012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
10239012bc364SMatthew G. Knepley 
10240012bc364SMatthew G. Knepley   Not collective
10241012bc364SMatthew G. Knepley 
10242012bc364SMatthew G. Knepley   Input Parameters:
10243012bc364SMatthew G. Knepley + ct    - The DMPolytopeType
10244012bc364SMatthew G. Knepley - point - Coordinates of the point
10245012bc364SMatthew G. Knepley 
10246012bc364SMatthew G. Knepley   Output Parameters:
10247012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
10248012bc364SMatthew G. Knepley 
10249012bc364SMatthew G. Knepley   Level: advanced
10250012bc364SMatthew G. Knepley 
10251012bc364SMatthew G. Knepley .seealso: DMLocatePoints()
10252012bc364SMatthew G. Knepley @*/
10253012bc364SMatthew G. Knepley PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
10254012bc364SMatthew G. Knepley {
10255012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
10256012bc364SMatthew G. Knepley   PetscInt  d;
10257012bc364SMatthew G. Knepley 
10258012bc364SMatthew G. Knepley   PetscFunctionBegin;
10259012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
10260012bc364SMatthew G. Knepley   switch (ct) {
10261012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
10262012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
10263012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
10264012bc364SMatthew G. Knepley       if (point[d] < -1.0) {*inside = PETSC_FALSE; break;}
10265012bc364SMatthew G. Knepley       sum += point[d];
10266012bc364SMatthew G. Knepley     }
10267012bc364SMatthew G. Knepley     if (sum > PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10268012bc364SMatthew G. Knepley     break;
10269012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
10270012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
10271012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
10272012bc364SMatthew G. Knepley       if (PetscAbsReal(point[d]) > 1.+PETSC_SMALL) {*inside = PETSC_FALSE; break;}
10273012bc364SMatthew G. Knepley     break;
10274012bc364SMatthew G. Knepley   default:
1027598921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
10276012bc364SMatthew G. Knepley   }
10277012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
10278012bc364SMatthew G. Knepley }
10279