xref: /petsc/src/dm/interface/dm.c (revision 5f80ce2ab25dff0f4601e710601cbbcecf323266)
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 
50a4121054SBarry Smith   PetscFunctionBegin;
511411c6eeSJed Brown   PetscValidPointer(dm,2);
520298fd71SBarry Smith   *dm = NULL;
53*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMInitializePackage());
54a4121054SBarry Smith 
55*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
56e7c4fc90SDmitry Karpeev 
5749be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
5849be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
590298fd71SBarry Smith   v->ltogmap                  = NULL;
60a4ea9b21SRichard Tran Mills   v->bind_below               = 0;
611411c6eeSJed Brown   v->bs                       = 1;
62171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
63*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFCreate(comm, &v->sf));
64*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFCreate(comm, &v->sectionSF));
65c58f1c22SToby Isaac   v->labels                   = NULL;
6634aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
6734aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
68c58f1c22SToby Isaac   v->depthLabel               = NULL;
69ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
701bb6d2a8SBarry Smith   v->localSection             = NULL;
711bb6d2a8SBarry Smith   v->globalSection            = NULL;
723b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
733b8ba7d1SJed Brown   v->defaultConstraint.mat    = NULL;
7479769bd5SJed Brown   v->defaultConstraint.bias   = NULL;
75c6b900c6SMatthew G. Knepley   v->L                        = NULL;
76c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
775dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
789a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7996173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
80435a35e8SMatthew G Knepley   {
81435a35e8SMatthew G Knepley     PetscInt i;
82435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
830298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
84f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
85435a35e8SMatthew G Knepley     }
86435a35e8SMatthew G Knepley   }
87*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSCreate(PETSC_COMM_SELF, &ds));
88*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetRegionDS(v, NULL, NULL, ds));
89*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSDestroy(&ds));
90*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHMapAuxCreate(&v->auxData));
9114f150ffSMatthew G. Knepley   v->dmBC = NULL;
92a8fb8f29SToby Isaac   v->coarseMesh = NULL;
93f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
94cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
95*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetVecType(v,VECSTANDARD));
96*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetMatType(v,MATAIJ));
974a7a4c06SLawrence Mitchell 
981411c6eeSJed Brown   *dm = v;
99a4121054SBarry Smith   PetscFunctionReturn(0);
100a4121054SBarry Smith }
101a4121054SBarry Smith 
10238221697SMatthew G. Knepley /*@
10338221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
10438221697SMatthew G. Knepley 
105d083f849SBarry Smith   Collective
10638221697SMatthew G. Knepley 
10738221697SMatthew G. Knepley   Input Parameter:
10838221697SMatthew G. Knepley . dm - The original DM object
10938221697SMatthew G. Knepley 
11038221697SMatthew G. Knepley   Output Parameter:
11138221697SMatthew G. Knepley . newdm  - The new DM object
11238221697SMatthew G. Knepley 
11338221697SMatthew G. Knepley   Level: beginner
11438221697SMatthew G. Knepley 
1151cb8cacdSPatrick Sanan   Notes:
1161cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1171cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1181cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1191bb6d2a8SBarry Smith 
12089706ed2SPatrick Sanan   The clone is considered set up iff the original is.
12189706ed2SPatrick Sanan 
12292cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1231bb6d2a8SBarry Smith 
12438221697SMatthew G. Knepley @*/
12538221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
12638221697SMatthew G. Knepley {
12738221697SMatthew G. Knepley   PetscSF        sf;
12838221697SMatthew G. Knepley   Vec            coords;
12938221697SMatthew G. Knepley   void          *ctx;
130a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
13138221697SMatthew G. Knepley 
13238221697SMatthew G. Knepley   PetscFunctionBegin;
13338221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13438221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
135*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreate(PetscObjectComm((PetscObject) dm), newdm));
136*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
137ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
138ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
139c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
140*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree((*newdm)->vectype));
141*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrallocpy(dm->vectype,(char**)&(*newdm)->vectype));
142*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree((*newdm)->mattype));
143*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrallocpy(dm->mattype,(char**)&(*newdm)->mattype));
144*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
145*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetDimension(*newdm, dim));
14638221697SMatthew G. Knepley   if (dm->ops->clone) {
147*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->clone)(dm, newdm));
14838221697SMatthew G. Knepley   }
1493f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
150*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetPointSF(dm, &sf));
151*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetPointSF(*newdm, sf));
152*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetApplicationContext(dm, &ctx));
153*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetApplicationContext(*newdm, ctx));
154be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
155be4c1c3eSMatthew G. Knepley     DM           ncdm;
156be4c1c3eSMatthew G. Knepley     PetscSection cs;
1575a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
158be4c1c3eSMatthew G. Knepley 
159*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLocalSection(dm->coordinateDM, &cs));
160*5f80ce2aSJacob Faibussowitsch     if (cs) CHKERRQ(PetscSectionGetChart(cs, NULL, &pEnd));
161*5f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm)));
1625a0206caSToby Isaac     if (pEndMax >= 0) {
163*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMClone(dm->coordinateDM, &ncdm));
164*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMCopyDisc(dm->coordinateDM, ncdm));
165*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMSetLocalSection(ncdm, cs));
166*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMSetCoordinateDM(*newdm, ncdm));
167*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMDestroy(&ncdm));
168be4c1c3eSMatthew G. Knepley     }
169be4c1c3eSMatthew G. Knepley   }
170*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDim(dm, &cdim));
171*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetCoordinateDim(*newdm, cdim));
172*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinatesLocal(dm, &coords));
17338221697SMatthew G. Knepley   if (coords) {
174*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetCoordinatesLocal(*newdm, coords));
17538221697SMatthew G. Knepley   } else {
176*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinates(dm, &coords));
177*5f80ce2aSJacob Faibussowitsch     if (coords) CHKERRQ(DMSetCoordinates(*newdm, coords));
17838221697SMatthew G. Knepley   }
17990b157c4SStefano Zampini   {
18090b157c4SStefano Zampini     PetscBool             isper;
181c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1825dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
183*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd));
184*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd));
185c6b900c6SMatthew G. Knepley   }
18634aa8a36SMatthew G. Knepley   {
18734aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
18834aa8a36SMatthew G. Knepley 
189*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
190*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
19134aa8a36SMatthew G. Knepley   }
19238221697SMatthew G. Knepley   PetscFunctionReturn(0);
19338221697SMatthew G. Knepley }
19438221697SMatthew G. Knepley 
1959a42bb27SBarry Smith /*@C
196564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1979a42bb27SBarry Smith 
198d083f849SBarry Smith    Logically Collective on da
1999a42bb27SBarry Smith 
200147403d9SBarry Smith    Input Parameters:
2019a42bb27SBarry Smith +  da - initial distributed array
202147403d9SBarry Smith -  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
2039a42bb27SBarry Smith 
2049a42bb27SBarry Smith    Options Database:
205147403d9SBarry Smith .   -dm_vec_type ctype - the type of vector to create
2069a42bb27SBarry Smith 
2079a42bb27SBarry Smith    Level: intermediate
2089a42bb27SBarry Smith 
209a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
2109a42bb27SBarry Smith @*/
21119fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2129a42bb27SBarry Smith {
2139a42bb27SBarry Smith   PetscFunctionBegin;
2149a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
215*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(da->vectype));
216*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrallocpy(ctype,(char**)&da->vectype));
2179a42bb27SBarry Smith   PetscFunctionReturn(0);
2189a42bb27SBarry Smith }
2199a42bb27SBarry Smith 
220c0dedaeaSBarry Smith /*@C
221c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
222c0dedaeaSBarry Smith 
223d083f849SBarry Smith    Logically Collective on da
224c0dedaeaSBarry Smith 
225c0dedaeaSBarry Smith    Input Parameter:
226c0dedaeaSBarry Smith .  da - initial distributed array
227c0dedaeaSBarry Smith 
228c0dedaeaSBarry Smith    Output Parameter:
229c0dedaeaSBarry Smith .  ctype - the vector type
230c0dedaeaSBarry Smith 
231c0dedaeaSBarry Smith    Level: intermediate
232c0dedaeaSBarry Smith 
233a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
234c0dedaeaSBarry Smith @*/
235c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
236c0dedaeaSBarry Smith {
237c0dedaeaSBarry Smith   PetscFunctionBegin;
238c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
239c0dedaeaSBarry Smith   *ctype = da->vectype;
240c0dedaeaSBarry Smith   PetscFunctionReturn(0);
241c0dedaeaSBarry Smith }
242c0dedaeaSBarry Smith 
2435f1ad066SMatthew G Knepley /*@
24434f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2455f1ad066SMatthew G Knepley 
2465f1ad066SMatthew G Knepley   Not collective
2475f1ad066SMatthew G Knepley 
2485f1ad066SMatthew G Knepley   Input Parameter:
2495f1ad066SMatthew G Knepley . v - The Vec
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley   Output Parameter:
2525f1ad066SMatthew G Knepley . dm - The DM
2535f1ad066SMatthew G Knepley 
2545f1ad066SMatthew G Knepley   Level: intermediate
2555f1ad066SMatthew G Knepley 
2565f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2575f1ad066SMatthew G Knepley @*/
2585f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2595f1ad066SMatthew G Knepley {
2605f1ad066SMatthew G Knepley   PetscFunctionBegin;
2615f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2625f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
263*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm));
2645f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2655f1ad066SMatthew G Knepley }
2665f1ad066SMatthew G Knepley 
2675f1ad066SMatthew G Knepley /*@
268d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2695f1ad066SMatthew G Knepley 
2705f1ad066SMatthew G Knepley   Not collective
2715f1ad066SMatthew G Knepley 
2725f1ad066SMatthew G Knepley   Input Parameters:
2735f1ad066SMatthew G Knepley + v - The Vec
2745f1ad066SMatthew G Knepley - dm - The DM
2755f1ad066SMatthew G Knepley 
276d9805387SMatthew 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.
277d9805387SMatthew G. Knepley 
2785f1ad066SMatthew G Knepley   Level: intermediate
2795f1ad066SMatthew G Knepley 
2805f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2815f1ad066SMatthew G Knepley @*/
2825f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2835f1ad066SMatthew G Knepley {
2845f1ad066SMatthew G Knepley   PetscFunctionBegin;
2855f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
286d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
287*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm));
2885f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2895f1ad066SMatthew G Knepley }
2905f1ad066SMatthew G Knepley 
291521d9a4cSLisandro Dalcin /*@C
2928f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2938f1509bcSBarry Smith 
294d083f849SBarry Smith    Logically Collective on dm
2958f1509bcSBarry Smith 
2968f1509bcSBarry Smith    Input Parameters:
2978f1509bcSBarry Smith +  dm - the DM context
2988f1509bcSBarry Smith -  ctype - the matrix type
2998f1509bcSBarry Smith 
3008f1509bcSBarry Smith    Options Database:
3018f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3028f1509bcSBarry Smith 
3038f1509bcSBarry Smith    Level: intermediate
3048f1509bcSBarry Smith 
3058f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3068f1509bcSBarry Smith           DMGetISColoringType()
3078f1509bcSBarry Smith @*/
3088f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3098f1509bcSBarry Smith {
3108f1509bcSBarry Smith   PetscFunctionBegin;
3118f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3128f1509bcSBarry Smith   dm->coloringtype = ctype;
3138f1509bcSBarry Smith   PetscFunctionReturn(0);
3148f1509bcSBarry Smith }
3158f1509bcSBarry Smith 
3168f1509bcSBarry Smith /*@C
3178f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
318521d9a4cSLisandro Dalcin 
319d083f849SBarry Smith    Logically Collective on dm
320521d9a4cSLisandro Dalcin 
321521d9a4cSLisandro Dalcin    Input Parameter:
3228f1509bcSBarry Smith .  dm - the DM context
3238f1509bcSBarry Smith 
3248f1509bcSBarry Smith    Output Parameter:
3258f1509bcSBarry Smith .  ctype - the matrix type
3268f1509bcSBarry Smith 
3278f1509bcSBarry Smith    Options Database:
3288f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3298f1509bcSBarry Smith 
3308f1509bcSBarry Smith    Level: intermediate
3318f1509bcSBarry Smith 
3328f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3338f1509bcSBarry Smith           DMGetISColoringType()
3348f1509bcSBarry Smith @*/
3358f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3368f1509bcSBarry Smith {
3378f1509bcSBarry Smith   PetscFunctionBegin;
3388f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3398f1509bcSBarry Smith   *ctype = dm->coloringtype;
3408f1509bcSBarry Smith   PetscFunctionReturn(0);
3418f1509bcSBarry Smith }
3428f1509bcSBarry Smith 
3438f1509bcSBarry Smith /*@C
3448f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3458f1509bcSBarry Smith 
346d083f849SBarry Smith    Logically Collective on dm
3478f1509bcSBarry Smith 
3488f1509bcSBarry Smith    Input Parameters:
349521d9a4cSLisandro Dalcin +  dm - the DM context
350a2b5a043SBarry Smith -  ctype - the matrix type
351521d9a4cSLisandro Dalcin 
352521d9a4cSLisandro Dalcin    Options Database:
353147403d9SBarry Smith .   -dm_mat_type ctype - the type of the matrix to create
354521d9a4cSLisandro Dalcin 
355521d9a4cSLisandro Dalcin    Level: intermediate
356521d9a4cSLisandro Dalcin 
357a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
358521d9a4cSLisandro Dalcin @*/
35919fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
360521d9a4cSLisandro Dalcin {
361521d9a4cSLisandro Dalcin   PetscFunctionBegin;
362521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
363*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(dm->mattype));
364*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrallocpy(ctype,(char**)&dm->mattype));
365521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
366521d9a4cSLisandro Dalcin }
367521d9a4cSLisandro Dalcin 
368c0dedaeaSBarry Smith /*@C
369c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
370c0dedaeaSBarry Smith 
371d083f849SBarry Smith    Logically Collective on dm
372c0dedaeaSBarry Smith 
373c0dedaeaSBarry Smith    Input Parameter:
374c0dedaeaSBarry Smith .  dm - the DM context
375c0dedaeaSBarry Smith 
376c0dedaeaSBarry Smith    Output Parameter:
377c0dedaeaSBarry Smith .  ctype - the matrix type
378c0dedaeaSBarry Smith 
379c0dedaeaSBarry Smith    Options Database:
380147403d9SBarry Smith .   -dm_mat_type ctype - the matrix type
381c0dedaeaSBarry Smith 
382c0dedaeaSBarry Smith    Level: intermediate
383c0dedaeaSBarry Smith 
384a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
385c0dedaeaSBarry Smith @*/
386c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
387c0dedaeaSBarry Smith {
388c0dedaeaSBarry Smith   PetscFunctionBegin;
389c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
390c0dedaeaSBarry Smith   *ctype = dm->mattype;
391c0dedaeaSBarry Smith   PetscFunctionReturn(0);
392c0dedaeaSBarry Smith }
393c0dedaeaSBarry Smith 
394c688c046SMatthew G Knepley /*@
39534f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
396c688c046SMatthew G Knepley 
397c688c046SMatthew G Knepley   Not collective
398c688c046SMatthew G Knepley 
399c688c046SMatthew G Knepley   Input Parameter:
400c688c046SMatthew G Knepley . A - The Mat
401c688c046SMatthew G Knepley 
402c688c046SMatthew G Knepley   Output Parameter:
403c688c046SMatthew G Knepley . dm - The DM
404c688c046SMatthew G Knepley 
405c688c046SMatthew G Knepley   Level: intermediate
406c688c046SMatthew G Knepley 
4078f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4088f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4098f1509bcSBarry Smith 
410c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
411c688c046SMatthew G Knepley @*/
412c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
413c688c046SMatthew G Knepley {
414c688c046SMatthew G Knepley   PetscFunctionBegin;
415c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
416c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
417*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm));
418c688c046SMatthew G Knepley   PetscFunctionReturn(0);
419c688c046SMatthew G Knepley }
420c688c046SMatthew G Knepley 
421c688c046SMatthew G Knepley /*@
422c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
423c688c046SMatthew G Knepley 
424c688c046SMatthew G Knepley   Not collective
425c688c046SMatthew G Knepley 
426c688c046SMatthew G Knepley   Input Parameters:
427c688c046SMatthew G Knepley + A - The Mat
428c688c046SMatthew G Knepley - dm - The DM
429c688c046SMatthew G Knepley 
430c688c046SMatthew G Knepley   Level: intermediate
431c688c046SMatthew G Knepley 
4328f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4338f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4348f1509bcSBarry Smith 
435c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
436c688c046SMatthew G Knepley @*/
437c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
438c688c046SMatthew G Knepley {
439c688c046SMatthew G Knepley   PetscFunctionBegin;
440c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4418865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
442*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm));
443c688c046SMatthew G Knepley   PetscFunctionReturn(0);
444c688c046SMatthew G Knepley }
445c688c046SMatthew G Knepley 
4469a42bb27SBarry Smith /*@C
4479a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4486757b960SDave May    DM options in the database.
4499a42bb27SBarry Smith 
450d083f849SBarry Smith    Logically Collective on dm
4519a42bb27SBarry Smith 
452d8d19677SJose E. Roman    Input Parameters:
4538353ddbbSDave May +  da - the DM context
4549a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4559a42bb27SBarry Smith 
4569a42bb27SBarry Smith    Notes:
4579a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4589a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4599a42bb27SBarry Smith 
4609a42bb27SBarry Smith    Level: advanced
4619a42bb27SBarry Smith 
4629a42bb27SBarry Smith .seealso: DMSetFromOptions()
4639a42bb27SBarry Smith @*/
4647087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4659a42bb27SBarry Smith {
4669a42bb27SBarry Smith   PetscFunctionBegin;
4679a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
468*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetOptionsPrefix((PetscObject)dm,prefix));
469691be533SLawrence Mitchell   if (dm->sf) {
470*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix));
471691be533SLawrence Mitchell   }
4721bb6d2a8SBarry Smith   if (dm->sectionSF) {
473*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix));
474691be533SLawrence Mitchell   }
4759a42bb27SBarry Smith   PetscFunctionReturn(0);
4769a42bb27SBarry Smith }
4779a42bb27SBarry Smith 
47831697293SDave May /*@C
47931697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
48031697293SDave May    DM options in the database.
48131697293SDave May 
482d083f849SBarry Smith    Logically Collective on dm
48331697293SDave May 
48431697293SDave May    Input Parameters:
48531697293SDave May +  dm - the DM context
48631697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48731697293SDave May 
48831697293SDave May    Notes:
48931697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
49031697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49131697293SDave May 
49231697293SDave May    Level: advanced
49331697293SDave May 
49431697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49531697293SDave May @*/
49631697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49731697293SDave May {
49831697293SDave May   PetscFunctionBegin;
49931697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
500*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix));
50131697293SDave May   PetscFunctionReturn(0);
50231697293SDave May }
50331697293SDave May 
50431697293SDave May /*@C
50531697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50631697293SDave May    DM options in the database.
50731697293SDave May 
50831697293SDave May    Not Collective
50931697293SDave May 
51031697293SDave May    Input Parameters:
51131697293SDave May .  dm - the DM context
51231697293SDave May 
51331697293SDave May    Output Parameters:
51431697293SDave May .  prefix - pointer to the prefix string used is returned
51531697293SDave May 
51695452b02SPatrick Sanan    Notes:
51795452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51831697293SDave May    sufficient length to hold the prefix.
51931697293SDave May 
52031697293SDave May    Level: advanced
52131697293SDave May 
52231697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52331697293SDave May @*/
52431697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52531697293SDave May {
52631697293SDave May   PetscFunctionBegin;
52731697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
528*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetOptionsPrefix((PetscObject)dm,prefix));
52931697293SDave May   PetscFunctionReturn(0);
53031697293SDave May }
53131697293SDave May 
53288bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53388bdff64SToby Isaac {
5346eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
53588bdff64SToby Isaac 
53688bdff64SToby Isaac   PetscFunctionBegin;
537aab5bcd8SJed Brown   *ncrefct = 0;
53888bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
53988bdff64SToby Isaac     refct--;
54088bdff64SToby Isaac     if (recurseCoarse) {
54188bdff64SToby Isaac       PetscInt coarseCount;
54288bdff64SToby Isaac 
543*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount));
54488bdff64SToby Isaac       refct += coarseCount;
54588bdff64SToby Isaac     }
54688bdff64SToby Isaac   }
54788bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
54888bdff64SToby Isaac     refct--;
54988bdff64SToby Isaac     if (recurseFine) {
55088bdff64SToby Isaac       PetscInt fineCount;
55188bdff64SToby Isaac 
552*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount));
55388bdff64SToby Isaac       refct += fineCount;
55488bdff64SToby Isaac     }
55588bdff64SToby Isaac   }
55688bdff64SToby Isaac   *ncrefct = refct;
55788bdff64SToby Isaac   PetscFunctionReturn(0);
55888bdff64SToby Isaac }
55988bdff64SToby Isaac 
560f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
561354557abSToby Isaac {
5625d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
563354557abSToby Isaac 
564354557abSToby Isaac   PetscFunctionBegin;
565354557abSToby Isaac   /* destroy the labels */
566354557abSToby Isaac   while (next) {
567354557abSToby Isaac     DMLabelLink tmp = next->next;
568354557abSToby Isaac 
5695d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
570ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
571*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelDestroy(&next->label));
572*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(next));
573354557abSToby Isaac     next = tmp;
574354557abSToby Isaac   }
5755d80c0bfSVaclav Hapla   dm->labels = NULL;
576354557abSToby Isaac   PetscFunctionReturn(0);
577354557abSToby Isaac }
578354557abSToby Isaac 
5791fb7b255SJunchao Zhang /*@C
5808472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
58147c6ae99SBarry Smith 
582d083f849SBarry Smith     Collective on dm
58347c6ae99SBarry Smith 
58447c6ae99SBarry Smith     Input Parameter:
58547c6ae99SBarry Smith .   dm - the DM object to destroy
58647c6ae99SBarry Smith 
58747c6ae99SBarry Smith     Level: developer
58847c6ae99SBarry Smith 
589e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
59047c6ae99SBarry Smith 
59147c6ae99SBarry Smith @*/
592fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
59347c6ae99SBarry Smith {
5946eb26441SStefano Zampini   PetscInt       cnt;
595dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
59647c6ae99SBarry Smith 
59747c6ae99SBarry Smith   PetscFunctionBegin;
5986bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5996bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
60087e657c6SBarry Smith 
60188bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
602*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt));
60388bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
604ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6056bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6066bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6076eb26441SStefano Zampini 
608*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMClearGlobalVectors(*dm));
609*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMClearLocalVectors(*dm));
6106eb26441SStefano Zampini 
611f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6120298fd71SBarry Smith   (*dm)->namedglobal = NULL;
613f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6142348bcf4SPeter Brune     nnext = nlink->next;
6157a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
616*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(nlink->name));
617*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecDestroy(&nlink->X));
618*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(nlink));
6192348bcf4SPeter Brune   }
620f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6210298fd71SBarry Smith   (*dm)->namedlocal = NULL;
622f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
623f490541aSPeter Brune     nnext = nlink->next;
6247a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN,((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
625*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(nlink->name));
626*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecDestroy(&nlink->X));
627*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(nlink));
628f490541aSPeter Brune   }
6292348bcf4SPeter Brune 
630b17ce1afSJed Brown   /* Destroy the list of hooks */
631c833c3b5SJed Brown   {
632c833c3b5SJed Brown     DMCoarsenHookLink link,next;
633b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
634b17ce1afSJed Brown       next = link->next;
635*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(link));
636b17ce1afSJed Brown     }
6370298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
638c833c3b5SJed Brown   }
639c833c3b5SJed Brown   {
640c833c3b5SJed Brown     DMRefineHookLink link,next;
641c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
642c833c3b5SJed Brown       next = link->next;
643*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(link));
644c833c3b5SJed Brown     }
6450298fd71SBarry Smith     (*dm)->refinehook = NULL;
646c833c3b5SJed Brown   }
647be081cd6SPeter Brune   {
648be081cd6SPeter Brune     DMSubDomainHookLink link,next;
649be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
650be081cd6SPeter Brune       next = link->next;
651*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(link));
652be081cd6SPeter Brune     }
6530298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
654be081cd6SPeter Brune   }
655baf369e7SPeter Brune   {
656baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
657baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
658baf369e7SPeter Brune       next = link->next;
659*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(link));
660baf369e7SPeter Brune     }
6610298fd71SBarry Smith     (*dm)->gtolhook = NULL;
662baf369e7SPeter Brune   }
663d4d07f1eSToby Isaac   {
664d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
665d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
666d4d07f1eSToby Isaac       next = link->next;
667*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(link));
668d4d07f1eSToby Isaac     }
669d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
670d4d07f1eSToby Isaac   }
671aa1993deSMatthew G Knepley   /* Destroy the work arrays */
672aa1993deSMatthew G Knepley   {
673aa1993deSMatthew G Knepley     DMWorkLink link,next;
6747a8be351SBarry Smith     PetscCheck(!(*dm)->workout,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
675aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
676aa1993deSMatthew G Knepley       next = link->next;
677*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(link->mem));
678*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(link));
679aa1993deSMatthew G Knepley     }
6800298fd71SBarry Smith     (*dm)->workin = NULL;
681aa1993deSMatthew G Knepley   }
682c58f1c22SToby Isaac   /* destroy the labels */
683*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroyLabelLinkList_Internal(*dm));
684f4cdcedcSVaclav Hapla   /* destroy the fields */
685*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMClearFields(*dm));
686f4cdcedcSVaclav Hapla   /* destroy the boundaries */
687e6f8dbb6SToby Isaac   {
688e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
689e6f8dbb6SToby Isaac     while (next) {
690e6f8dbb6SToby Isaac       DMBoundary b = next;
691e6f8dbb6SToby Isaac 
692e6f8dbb6SToby Isaac       next = b->next;
693*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(b));
694e6f8dbb6SToby Isaac     }
695e6f8dbb6SToby Isaac   }
696b17ce1afSJed Brown 
697*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectDestroy(&(*dm)->dmksp));
698*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectDestroy(&(*dm)->dmsnes));
699*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectDestroy(&(*dm)->dmts));
70052536dc3SBarry Smith 
7011a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
702*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7031a266240SBarry Smith   }
704*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatFDColoringDestroy(&(*dm)->fd));
705*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
706*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree((*dm)->vectype));
707*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree((*dm)->mattype));
70888ed4aceSMatthew G Knepley 
709*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&(*dm)->localSection));
710*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&(*dm)->globalSection));
711*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutDestroy(&(*dm)->map));
712*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
713*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatDestroy(&(*dm)->defaultConstraint.mat));
714*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFDestroy(&(*dm)->sf));
715*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFDestroy(&(*dm)->sectionSF));
716736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
717736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
718*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFDestroy(&(*dm)->sfNatural));
719736995cdSBlaise Bourdin     }
720*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectDereference((PetscObject) (*dm)->sfMigration));
721736995cdSBlaise Bourdin   }
7229a2a23afSMatthew G. Knepley   {
7239a2a23afSMatthew G. Knepley     Vec     *auxData;
7249a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7259a2a23afSMatthew G. Knepley 
726*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHMapAuxGetSize((*dm)->auxData, &n));
727*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(n, &auxData));
728*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData));
729*5f80ce2aSJacob Faibussowitsch     for (i = 0; i < n; ++i) CHKERRQ(VecDestroy(&auxData[i]));
730*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(auxData));
731*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHMapAuxDestroy(&(*dm)->auxData));
7329a2a23afSMatthew G. Knepley   }
73388bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
734*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetFineDM((*dm)->coarseMesh,NULL));
73588bdff64SToby Isaac   }
7366eb26441SStefano Zampini 
737*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&(*dm)->coarseMesh));
73888bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
739*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetCoarseDM((*dm)->fineMesh,NULL));
74088bdff64SToby Isaac   }
741*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&(*dm)->fineMesh));
742*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMFieldDestroy(&(*dm)->coordinateField));
743*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&(*dm)->coordinateDM));
744*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&(*dm)->coordinates));
745*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&(*dm)->coordinatesLocal));
746*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree((*dm)->L));
747*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree((*dm)->maxCell));
748*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree((*dm)->bdtype));
749*5f80ce2aSJacob Faibussowitsch   if ((*dm)->transformDestroy) CHKERRQ((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
750*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&(*dm)->transformDM));
751*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&(*dm)->transform));
7526636e97aSMatthew G Knepley 
753*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMClearDS(*dm));
754*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&(*dm)->dmBC));
755e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
756*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSAWsViewOff((PetscObject)*dm));
757732e2eb9SMatthew G Knepley 
758ed3c66a1SDave May   if ((*dm)->ops->destroy) {
759*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*(*dm)->ops->destroy)(*dm));
760ed3c66a1SDave May   }
761*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMMonitorCancel(*dm));
762f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
763*5f80ce2aSJacob Faibussowitsch   CHKERRQ_CEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
764*5f80ce2aSJacob Faibussowitsch   CHKERRQ_CEED(CeedDestroy(&(*dm)->ceed));
765f918ec44SMatthew G. Knepley #endif
766435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
767*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHeaderDestroy(dm));
76847c6ae99SBarry Smith   PetscFunctionReturn(0);
76947c6ae99SBarry Smith }
77047c6ae99SBarry Smith 
771d7bf68aeSBarry Smith /*@
772d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
773d7bf68aeSBarry Smith 
774d083f849SBarry Smith     Collective on dm
775d7bf68aeSBarry Smith 
776d7bf68aeSBarry Smith     Input Parameter:
777d7bf68aeSBarry Smith .   dm - the DM object to setup
778d7bf68aeSBarry Smith 
779d7bf68aeSBarry Smith     Level: developer
780d7bf68aeSBarry Smith 
781e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
782d7bf68aeSBarry Smith 
783d7bf68aeSBarry Smith @*/
7847087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
785d7bf68aeSBarry Smith {
786d7bf68aeSBarry Smith   PetscFunctionBegin;
787171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7888387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
789d7bf68aeSBarry Smith   if (dm->ops->setup) {
790*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->setup)(dm));
791d7bf68aeSBarry Smith   }
7928387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
793d7bf68aeSBarry Smith   PetscFunctionReturn(0);
794d7bf68aeSBarry Smith }
795d7bf68aeSBarry Smith 
796d7bf68aeSBarry Smith /*@
797d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
798d7bf68aeSBarry Smith 
799d083f849SBarry Smith     Collective on dm
800d7bf68aeSBarry Smith 
801d7bf68aeSBarry Smith     Input Parameter:
802d7bf68aeSBarry Smith .   dm - the DM object to set options for
803d7bf68aeSBarry Smith 
804732e2eb9SMatthew G Knepley     Options Database:
8054164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8064164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8074164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
808a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
809a4ea9b21SRichard 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
810732e2eb9SMatthew G Knepley 
8119318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8129318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8139318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
814cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
8159318fe57SMatthew G. Knepley . -dm_plex_shape <shape>            - The domain shape, such as DM_SHAPE_BOX, DM_SHAPE_SPHERE, etc.
8169318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8179318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8189318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
8199318fe57SMatthew G. Knepley . -dm_plex_simplex <bool>           - PETSC_TRUE for simplex elements, PETSC_FALSE for tensor elements
8209318fe57SMatthew G. Knepley . -dm_plex_interpolate <bool>       - PETSC_TRUE turns on topological interpolation (creating edges and faces)
8219318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8229318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8239318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8249318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
8259318fe57SMatthew G. Knepley . -dm_plex_box_bd <bx,by,bz>        - Specify the DMBoundaryType for each direction
8269318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8279318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8289318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8299318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
830bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8319318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8329318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8339318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8349318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
835bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
836d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
837d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
838d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
839d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
840d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
841909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
842909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8439318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8449318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8459318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8469318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8479318fe57SMatthew G. Knepley 
848384a6580SVaclav Hapla     DMPLEX Specific Checks
849384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
850384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
851384a6580SVaclav 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()
852384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
853384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
854384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
855384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
856d7bf68aeSBarry Smith 
85795eb5ee5SVaclav Hapla     Level: intermediate
85895eb5ee5SVaclav Hapla 
85995eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
86095eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
861d7bf68aeSBarry Smith 
862d7bf68aeSBarry Smith @*/
8637087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
864d7bf68aeSBarry Smith {
8657781c08eSBarry Smith   char           typeName[256];
866ca266f36SBarry Smith   PetscBool      flg;
867d7bf68aeSBarry Smith   PetscErrorCode ierr;
868d7bf68aeSBarry Smith 
869d7bf68aeSBarry Smith   PetscFunctionBegin;
870171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87149be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
872*5f80ce2aSJacob Faibussowitsch   if (dm->sf) CHKERRQ(PetscSFSetFromOptions(dm->sf));
873*5f80ce2aSJacob Faibussowitsch   if (dm->sectionSF) CHKERRQ(PetscSFSetFromOptions(dm->sectionSF));
8743194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
875*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,NULL));
876*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg));
877f9ba7244SBarry Smith   if (flg) {
878*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetVecType(dm,typeName));
879f9ba7244SBarry Smith   }
880*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsFList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg));
881073dac72SJed Brown   if (flg) {
882*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetMatType(dm,typeName));
883073dac72SJed Brown   }
884*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL));
885*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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));
886f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
887*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->setfromoptions)(PetscOptionsObject,dm));
888f9ba7244SBarry Smith   }
889f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
890*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm));
89182fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
892d7bf68aeSBarry Smith   PetscFunctionReturn(0);
893d7bf68aeSBarry Smith }
894d7bf68aeSBarry Smith 
895fc9bc008SSatish Balay /*@C
896fe2efc57SMark    DMViewFromOptions - View from Options
897fe2efc57SMark 
898fe2efc57SMark    Collective on DM
899fe2efc57SMark 
900fe2efc57SMark    Input Parameters:
901fe2efc57SMark +  dm - the DM object
902736c3998SJose E. Roman .  obj - Optional object
903736c3998SJose E. Roman -  name - command line option
904fe2efc57SMark 
905fe2efc57SMark    Level: intermediate
906fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
907fe2efc57SMark @*/
908fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
909fe2efc57SMark {
910fe2efc57SMark   PetscFunctionBegin;
911fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
912*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectViewFromOptions((PetscObject)dm,obj,name));
913fe2efc57SMark   PetscFunctionReturn(0);
914fe2efc57SMark }
915fe2efc57SMark 
916fe2efc57SMark /*@C
917224748a4SBarry Smith     DMView - Views a DM
91847c6ae99SBarry Smith 
919d083f849SBarry Smith     Collective on dm
92047c6ae99SBarry Smith 
921d8d19677SJose E. Roman     Input Parameters:
92247c6ae99SBarry Smith +   dm - the DM object to view
92347c6ae99SBarry Smith -   v - the viewer
92447c6ae99SBarry Smith 
925cd7e8a5eSksagiyam     Notes:
926cd7e8a5eSksagiyam     Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
927cd7e8a5eSksagiyam     meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
928cd7e8a5eSksagiyam     before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
929cd7e8a5eSksagiyam 
930224748a4SBarry Smith     Level: beginner
93147c6ae99SBarry Smith 
932cd7e8a5eSksagiyam .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMLoad(), PetscObjectSetName()
93347c6ae99SBarry Smith 
93447c6ae99SBarry Smith @*/
9357087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
93647c6ae99SBarry Smith {
93732c0f0efSBarry Smith   PetscBool         isbinary;
93876a8abe0SBarry Smith   PetscMPIInt       size;
93976a8abe0SBarry Smith   PetscViewerFormat format;
94047c6ae99SBarry Smith 
94147c6ae99SBarry Smith   PetscFunctionBegin;
942171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9433014e516SBarry Smith   if (!v) {
944*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v));
9453014e516SBarry Smith   }
946b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
94774903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
94874903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
94974903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
95074903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
95174903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
95274903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
95374903a4fSStefano Zampini      in an error here */
95474903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
955*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerCheckWritable(v));
956b1b135c8SBarry Smith 
957*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerGetFormat(v,&format));
958*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size));
95976a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
960*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectPrintClassNamePrefixType((PetscObject)dm,v));
961*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary));
96232c0f0efSBarry Smith   if (isbinary) {
96355849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
96432c0f0efSBarry Smith     char     type[256];
96532c0f0efSBarry Smith 
966*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerBinaryWrite(v,&classid,1,PETSC_INT));
967*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrncpy(type,((PetscObject)dm)->type_name,256));
968*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerBinaryWrite(v,type,256,PETSC_CHAR));
96932c0f0efSBarry Smith   }
9700c010503SBarry Smith   if (dm->ops->view) {
971*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->view)(dm,v));
97247c6ae99SBarry Smith   }
97347c6ae99SBarry Smith   PetscFunctionReturn(0);
97447c6ae99SBarry Smith }
97547c6ae99SBarry Smith 
97647c6ae99SBarry Smith /*@
9778472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
97847c6ae99SBarry Smith 
979d083f849SBarry Smith     Collective on dm
98047c6ae99SBarry Smith 
98147c6ae99SBarry Smith     Input Parameter:
98247c6ae99SBarry Smith .   dm - the DM object
98347c6ae99SBarry Smith 
98447c6ae99SBarry Smith     Output Parameter:
98547c6ae99SBarry Smith .   vec - the global vector
98647c6ae99SBarry Smith 
987073dac72SJed Brown     Level: beginner
98847c6ae99SBarry Smith 
989ec075b9fSPatrick Sanan .seealso DMCreateLocalVector(), DMGetGlobalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
99047c6ae99SBarry Smith 
99147c6ae99SBarry Smith @*/
9927087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
99347c6ae99SBarry Smith {
99447c6ae99SBarry Smith   PetscFunctionBegin;
995171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
996b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
9977a8be351SBarry Smith   PetscCheck(dm->ops->createglobalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
998*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->createglobalvector)(dm,vec));
99976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1000c6b011d8SStefano Zampini     DM vdm;
1001c6b011d8SStefano Zampini 
1002*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetDM(*vec,&vdm));
10037a8be351SBarry Smith     PetscCheck(vdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
1004c6b011d8SStefano Zampini   }
100547c6ae99SBarry Smith   PetscFunctionReturn(0);
100647c6ae99SBarry Smith }
100747c6ae99SBarry Smith 
100847c6ae99SBarry Smith /*@
10098472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith     Not Collective
101247c6ae99SBarry Smith 
101347c6ae99SBarry Smith     Input Parameter:
101447c6ae99SBarry Smith .   dm - the DM object
101547c6ae99SBarry Smith 
101647c6ae99SBarry Smith     Output Parameter:
101747c6ae99SBarry Smith .   vec - the local vector
101847c6ae99SBarry Smith 
1019073dac72SJed Brown     Level: beginner
102047c6ae99SBarry Smith 
1021ec075b9fSPatrick Sanan .seealso DMCreateGlobalVector(), DMGetLocalVector(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
102247c6ae99SBarry Smith 
102347c6ae99SBarry Smith @*/
10247087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
102547c6ae99SBarry Smith {
102647c6ae99SBarry Smith   PetscFunctionBegin;
1027171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1028b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
10297a8be351SBarry Smith   PetscCheck(dm->ops->createlocalvector,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
1030*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->createlocalvector)(dm,vec));
103176bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1032c6b011d8SStefano Zampini     DM vdm;
1033c6b011d8SStefano Zampini 
1034*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetDM(*vec,&vdm));
10357a8be351SBarry Smith     PetscCheck(vdm,PETSC_COMM_SELF,PETSC_ERR_LIB,"DM type '%s' did not attach the DM to the vector",((PetscObject)dm)->type_name);
1036c6b011d8SStefano Zampini   }
103747c6ae99SBarry Smith   PetscFunctionReturn(0);
103847c6ae99SBarry Smith }
103947c6ae99SBarry Smith 
10401411c6eeSJed Brown /*@
10411411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10421411c6eeSJed Brown 
1043d083f849SBarry Smith    Collective on dm
10441411c6eeSJed Brown 
10451411c6eeSJed Brown    Input Parameter:
10461411c6eeSJed Brown .  dm - the DM that provides the mapping
10471411c6eeSJed Brown 
10481411c6eeSJed Brown    Output Parameter:
10491411c6eeSJed Brown .  ltog - the mapping
10501411c6eeSJed Brown 
10511411c6eeSJed Brown    Level: intermediate
10521411c6eeSJed Brown 
10531411c6eeSJed Brown    Notes:
10541411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10551411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10561411c6eeSJed Brown 
1057fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10581411c6eeSJed Brown @*/
10597087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10601411c6eeSJed Brown {
10610be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10621411c6eeSJed Brown 
10631411c6eeSJed Brown   PetscFunctionBegin;
10641411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10651411c6eeSJed Brown   PetscValidPointer(ltog,2);
10661411c6eeSJed Brown   if (!dm->ltogmap) {
106737d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
106837d0c07bSMatthew G Knepley 
1069*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLocalSection(dm, &section));
107037d0c07bSMatthew G Knepley     if (section) {
1071a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
107237d0c07bSMatthew G Knepley       PetscInt       *ltog;
1073ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
107437d0c07bSMatthew G Knepley 
1075*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetGlobalSection(dm, &sectionGlobal));
1076*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetChart(section, &pStart, &pEnd));
1077*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetStorageSize(section, &n));
1078*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
107937d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1080e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
108137d0c07bSMatthew G Knepley 
108237d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
1083*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(section, p, &dof));
1084*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetConstraintDof(section, p, &cdof));
1085*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetConstraintIndices(section, p, &cdofs));
1086*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(sectionGlobal, p, &off));
10871a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10881a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10891a7dc684SMatthew G. Knepley         if (dof) {
10905b8243e1SJed Brown           bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
10911a7dc684SMatthew G. Knepley         }
10925227eafbSStefano Zampini 
1093e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
10945227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1095e6befd46SJed Brown             ltog[l] = off < 0 ? off-c : -(off+c+1);
1096e6befd46SJed Brown             cind++;
1097e6befd46SJed Brown           } else {
10985227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off+1) : off) + c - cind;
1099e6befd46SJed Brown           }
110037d0c07bSMatthew G Knepley         }
110137d0c07bSMatthew G Knepley       }
1102bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11030be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
1104*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax));
11050be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
11060be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
11077591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11087591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1109ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1110ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1111ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1112ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l]/bs : -(-(ltog[l]+1)/bs + 1);
1113ca469d19SJed Brown         }
1114ccf3bd66SMatthew G. Knepley         n /= bs;
1115ccf3bd66SMatthew G. Knepley       }
1116*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1117*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap));
111837d0c07bSMatthew G Knepley     } else {
11197a8be351SBarry Smith       PetscCheck(dm->ops->getlocaltoglobalmapping,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1120*5f80ce2aSJacob Faibussowitsch       CHKERRQ((*dm->ops->getlocaltoglobalmapping)(dm));
11211411c6eeSJed Brown     }
112237d0c07bSMatthew G Knepley   }
11231411c6eeSJed Brown   *ltog = dm->ltogmap;
11241411c6eeSJed Brown   PetscFunctionReturn(0);
11251411c6eeSJed Brown }
11261411c6eeSJed Brown 
11271411c6eeSJed Brown /*@
11281411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
11291411c6eeSJed Brown 
11301411c6eeSJed Brown    Not Collective
11311411c6eeSJed Brown 
11321411c6eeSJed Brown    Input Parameter:
11331411c6eeSJed Brown .  dm - the DM with block structure
11341411c6eeSJed Brown 
11351411c6eeSJed Brown    Output Parameter:
11361411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11371411c6eeSJed Brown 
11381411c6eeSJed Brown    Level: intermediate
11391411c6eeSJed Brown 
1140fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
11411411c6eeSJed Brown @*/
11427087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
11431411c6eeSJed Brown {
11441411c6eeSJed Brown   PetscFunctionBegin;
11451411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1146534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11477a8be351SBarry Smith   PetscCheck(dm->bs >= 1,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
11481411c6eeSJed Brown   *bs = dm->bs;
11491411c6eeSJed Brown   PetscFunctionReturn(0);
11501411c6eeSJed Brown }
11511411c6eeSJed Brown 
115248eeb7c8SBarry Smith /*@C
11538472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
115447c6ae99SBarry Smith 
1155a5bc1bf3SBarry Smith     Collective on dmc
115647c6ae99SBarry Smith 
1157d8d19677SJose E. Roman     Input Parameters:
1158a5bc1bf3SBarry Smith +   dmc - the DM object
1159a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
116047c6ae99SBarry Smith 
1161d8d19677SJose E. Roman     Output Parameters:
116247c6ae99SBarry Smith +  mat - the interpolation
116347c6ae99SBarry Smith -  vec - the scaling (optional)
116447c6ae99SBarry Smith 
116547c6ae99SBarry Smith     Level: developer
116647c6ae99SBarry Smith 
116795452b02SPatrick Sanan     Notes:
116895452b02SPatrick 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
116985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1170d52bd9f3SBarry Smith 
11711f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1172d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
117385afcc9aSBarry Smith 
11742ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
117547c6ae99SBarry Smith 
117647c6ae99SBarry Smith @*/
1177a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
117847c6ae99SBarry Smith {
117947c6ae99SBarry Smith   PetscFunctionBegin;
1180a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1181a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1182c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
11837a8be351SBarry Smith   PetscCheck(dmc->ops->createinterpolation,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1184*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0));
1185*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dmc->ops->createinterpolation)(dmc,dmf,mat,vec));
1186*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0));
118747c6ae99SBarry Smith   PetscFunctionReturn(0);
118847c6ae99SBarry Smith }
118947c6ae99SBarry Smith 
11903ad4599aSBarry Smith /*@
1191d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11922ed6491fSPatrick Sanan 
11932ed6491fSPatrick Sanan   Input Parameters:
11942ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
11952ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
11962ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
11972ed6491fSPatrick Sanan 
11982ed6491fSPatrick Sanan   Output Parameter:
11992ed6491fSPatrick Sanan .    scale - the scaled vector
12002ed6491fSPatrick Sanan 
12012ed6491fSPatrick Sanan   Level: developer
12022ed6491fSPatrick Sanan 
1203e9c74fd6SRichard Tran Mills   Developer Notes:
1204e9c74fd6SRichard Tran Mills   If the fine-scale DMDA has the -dm_bind_below option set to true, then DMCreateInterpolationScale() calls MatSetBindingPropagates()
1205e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1206e9c74fd6SRichard Tran Mills 
12072ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
12082ed6491fSPatrick Sanan 
12092ed6491fSPatrick Sanan @*/
12102ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
12112ed6491fSPatrick Sanan {
12122ed6491fSPatrick Sanan   Vec            fine;
12132ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
12149704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1215e9c74fd6SRichard Tran Mills   PetscBool      bindingpropagates,isbound;
12169704db99SRichard Tran Mills #endif
12172ed6491fSPatrick Sanan 
12182ed6491fSPatrick Sanan   PetscFunctionBegin;
1219*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateGlobalVector(daf,&fine));
1220*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateGlobalVector(dac,scale));
1221*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSet(fine,one));
12229704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12239704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12249704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12259704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
1226*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetBindingPropagates(fine,&bindingpropagates));
1227e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
1228*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatSetBindingPropagates(mat,PETSC_TRUE));
1229*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecBoundToCPU(fine,&isbound));
1230*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatBindToCPU(mat,isbound));
123183aa49f4SRichard Tran Mills   }
12329704db99SRichard Tran Mills #endif
1233*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatRestrict(mat,fine,*scale));
1234*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&fine));
1235*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecReciprocal(*scale));
12362ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12372ed6491fSPatrick Sanan }
12382ed6491fSPatrick Sanan 
12392ed6491fSPatrick Sanan /*@
12403ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
12413ad4599aSBarry Smith 
1242a5bc1bf3SBarry Smith     Collective on dmc
12433ad4599aSBarry Smith 
1244d8d19677SJose E. Roman     Input Parameters:
1245a5bc1bf3SBarry Smith +   dmc - the DM object
1246a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
12473ad4599aSBarry Smith 
12483ad4599aSBarry Smith     Output Parameter:
12493ad4599aSBarry Smith .  mat - the restriction
12503ad4599aSBarry Smith 
12513ad4599aSBarry Smith     Level: developer
12523ad4599aSBarry Smith 
125395452b02SPatrick Sanan     Notes:
125495452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
12553ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
12563ad4599aSBarry Smith 
12573ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
12583ad4599aSBarry Smith 
12593ad4599aSBarry Smith @*/
1260a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12613ad4599aSBarry Smith {
12623ad4599aSBarry Smith   PetscFunctionBegin;
1263a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1264a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12655a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
12667a8be351SBarry Smith   PetscCheck(dmc->ops->createrestriction,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1267*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0));
1268*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dmc->ops->createrestriction)(dmc,dmf,mat));
1269*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0));
12703ad4599aSBarry Smith   PetscFunctionReturn(0);
12713ad4599aSBarry Smith }
12723ad4599aSBarry Smith 
127347c6ae99SBarry Smith /*@
12748472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
127547c6ae99SBarry Smith 
1276a5bc1bf3SBarry Smith     Collective on dac
127747c6ae99SBarry Smith 
1278d8d19677SJose E. Roman     Input Parameters:
1279a5bc1bf3SBarry Smith +   dac - the DM object
1280a5bc1bf3SBarry Smith -   daf - the second, finer DM object
128147c6ae99SBarry Smith 
128247c6ae99SBarry Smith     Output Parameter:
12836dbf9973SLawrence Mitchell .   mat - the injection
128447c6ae99SBarry Smith 
128547c6ae99SBarry Smith     Level: developer
128647c6ae99SBarry Smith 
128795452b02SPatrick Sanan    Notes:
128895452b02SPatrick 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
128985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
129085afcc9aSBarry Smith 
1291e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
129247c6ae99SBarry Smith 
129347c6ae99SBarry Smith @*/
1294a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
129547c6ae99SBarry Smith {
129647c6ae99SBarry Smith   PetscFunctionBegin;
1297a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1298a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
12995a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
13007a8be351SBarry Smith   PetscCheck(dac->ops->createinjection,PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1301*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0));
1302*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dac->ops->createinjection)(dac,daf,mat));
1303*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0));
130447c6ae99SBarry Smith   PetscFunctionReturn(0);
130547c6ae99SBarry Smith }
130647c6ae99SBarry Smith 
1307b412c318SBarry Smith /*@
1308bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1309bd041c0cSMatthew G. Knepley 
1310a5bc1bf3SBarry Smith   Collective on dac
1311bd041c0cSMatthew G. Knepley 
1312d8d19677SJose E. Roman   Input Parameters:
1313b4937a87SMatthew G. Knepley + dmc - the target DM object
1314b4937a87SMatthew G. Knepley - dmf - the source DM object
1315bd041c0cSMatthew G. Knepley 
1316bd041c0cSMatthew G. Knepley   Output Parameter:
1317b4937a87SMatthew G. Knepley . mat - the mass matrix
1318bd041c0cSMatthew G. Knepley 
1319bd041c0cSMatthew G. Knepley   Level: developer
1320bd041c0cSMatthew G. Knepley 
1321b4937a87SMatthew G. Knepley .seealso DMCreateMassMatrixLumped(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1322bd041c0cSMatthew G. Knepley @*/
1323b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1324bd041c0cSMatthew G. Knepley {
1325bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1326b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1327b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13285a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
13292c71b3e2SJacob Faibussowitsch   PetscCheck(dmc->ops->createmassmatrix,PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dmc)->type_name);
1330*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dmc->ops->createmassmatrix)(dmc, dmf, mat));
1331b4937a87SMatthew G. Knepley   PetscFunctionReturn(0);
1332b4937a87SMatthew G. Knepley }
1333b4937a87SMatthew G. Knepley 
1334b4937a87SMatthew G. Knepley /*@
1335b4937a87SMatthew G. Knepley   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given DM
1336b4937a87SMatthew G. Knepley 
1337b4937a87SMatthew G. Knepley   Collective on dm
1338b4937a87SMatthew G. Knepley 
1339b4937a87SMatthew G. Knepley   Input Parameter:
1340b4937a87SMatthew G. Knepley . dm - the DM object
1341b4937a87SMatthew G. Knepley 
1342b4937a87SMatthew G. Knepley   Output Parameter:
1343b4937a87SMatthew G. Knepley . lm - the lumped mass matrix
1344b4937a87SMatthew G. Knepley 
1345b4937a87SMatthew G. Knepley   Level: developer
1346b4937a87SMatthew G. Knepley 
1347b4937a87SMatthew G. Knepley .seealso DMCreateMassMatrix(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1348b4937a87SMatthew G. Knepley @*/
1349b4937a87SMatthew G. Knepley PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1350b4937a87SMatthew G. Knepley {
1351b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1352b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1353b4937a87SMatthew G. Knepley   PetscValidPointer(lm,2);
13542c71b3e2SJacob Faibussowitsch   PetscCheck(dm->ops->createmassmatrixlumped,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrixLumped",((PetscObject)dm)->type_name);
1355*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->createmassmatrixlumped)(dm, lm));
1356bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1357bd041c0cSMatthew G. Knepley }
1358bd041c0cSMatthew G. Knepley 
1359bd041c0cSMatthew G. Knepley /*@
1360b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
136147c6ae99SBarry Smith 
1362d083f849SBarry Smith     Collective on dm
136347c6ae99SBarry Smith 
1364d8d19677SJose E. Roman     Input Parameters:
136547c6ae99SBarry Smith +   dm - the DM object
13665bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
136747c6ae99SBarry Smith 
136847c6ae99SBarry Smith     Output Parameter:
136947c6ae99SBarry Smith .   coloring - the coloring
137047c6ae99SBarry Smith 
1371ec5066bdSBarry Smith     Notes:
1372ec5066bdSBarry 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
1373ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1374ec5066bdSBarry Smith 
1375ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1376ec5066bdSBarry Smith 
137747c6ae99SBarry Smith     Level: developer
137847c6ae99SBarry Smith 
1379ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
138047c6ae99SBarry Smith 
1381aab9d709SJed Brown @*/
1382b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
138347c6ae99SBarry Smith {
138447c6ae99SBarry Smith   PetscFunctionBegin;
1385171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13865a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
13877a8be351SBarry Smith   PetscCheck(dm->ops->getcoloring,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1388*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->getcoloring)(dm,ctype,coloring));
138947c6ae99SBarry Smith   PetscFunctionReturn(0);
139047c6ae99SBarry Smith }
139147c6ae99SBarry Smith 
1392b412c318SBarry Smith /*@
13938472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
139447c6ae99SBarry Smith 
1395d083f849SBarry Smith     Collective on dm
139647c6ae99SBarry Smith 
139747c6ae99SBarry Smith     Input Parameter:
1398b412c318SBarry Smith .   dm - the DM object
139947c6ae99SBarry Smith 
140047c6ae99SBarry Smith     Output Parameter:
140147c6ae99SBarry Smith .   mat - the empty Jacobian
140247c6ae99SBarry Smith 
1403073dac72SJed Brown     Level: beginner
140447c6ae99SBarry Smith 
1405f27dd7c6SMatthew G. Knepley     Options Database Keys:
1406f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1407f27dd7c6SMatthew G. Knepley 
140895452b02SPatrick Sanan     Notes:
140995452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
141094013140SBarry Smith        do not need to do it yourself.
141194013140SBarry Smith 
141294013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
14137889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
141494013140SBarry Smith 
141594013140SBarry 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
141694013140SBarry Smith        internally by PETSc.
141794013140SBarry Smith 
141894013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1419aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
142094013140SBarry Smith 
1421b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
142247c6ae99SBarry Smith 
1423aab9d709SJed Brown @*/
1424b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
142547c6ae99SBarry Smith {
142647c6ae99SBarry Smith   PetscFunctionBegin;
1427171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1428064a246eSJacob Faibussowitsch   PetscValidPointer(mat,2);
14297a8be351SBarry Smith   PetscCheck(dm->ops->creatematrix,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
1430*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatInitializePackage());
1431*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(DM_CreateMatrix,0,0,0,0));
1432*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->creatematrix)(dm,mat));
143376bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1434c6b011d8SStefano Zampini     DM mdm;
1435c6b011d8SStefano Zampini 
1436*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatGetDM(*mat,&mdm));
14377a8be351SBarry Smith     PetscCheck(mdm,PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the matrix",((PetscObject)dm)->type_name);
1438c6b011d8SStefano Zampini   }
1439e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1440e5e52638SMatthew G. Knepley   if (dm->Nf) {
1441e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1442649ef022SMatthew Knepley     PetscInt     Nf, f;
1443e571a35bSMatthew G. Knepley 
1444*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetNumFields(dm, &Nf));
1445649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1446649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
1447*5f80ce2aSJacob Faibussowitsch         CHKERRQ((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
1448*5f80ce2aSJacob Faibussowitsch         CHKERRQ(MatSetNullSpace(*mat, nullSpace));
1449*5f80ce2aSJacob Faibussowitsch         CHKERRQ(MatNullSpaceDestroy(&nullSpace));
1450649ef022SMatthew Knepley         break;
1451e571a35bSMatthew G. Knepley       }
1452649ef022SMatthew Knepley     }
1453649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1454649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
1455*5f80ce2aSJacob Faibussowitsch         CHKERRQ((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
1456*5f80ce2aSJacob Faibussowitsch         CHKERRQ(MatSetNearNullSpace(*mat, nullSpace));
1457*5f80ce2aSJacob Faibussowitsch         CHKERRQ(MatNullSpaceDestroy(&nullSpace));
1458e571a35bSMatthew G. Knepley       }
1459e571a35bSMatthew G. Knepley     }
1460e571a35bSMatthew G. Knepley   }
1461*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(DM_CreateMatrix,0,0,0,0));
146247c6ae99SBarry Smith   PetscFunctionReturn(0);
146347c6ae99SBarry Smith }
146447c6ae99SBarry Smith 
1465732e2eb9SMatthew G Knepley /*@
1466aa0f6e3cSJed Brown   DMSetMatrixPreallocateSkip - When DMCreateMatrix() is called the matrix sizes and ISLocalToGlobalMapping will be
1467aa0f6e3cSJed Brown   properly set, but the entries will not be preallocated. This is most useful to reduce initialization costs when
1468aa0f6e3cSJed Brown   MatSetPreallocationCOO() and MatSetValuesCOO() will be used.
1469aa0f6e3cSJed Brown 
1470aa0f6e3cSJed Brown   Logically Collective on dm
1471aa0f6e3cSJed Brown 
1472aa0f6e3cSJed Brown   Input Parameters:
1473aa0f6e3cSJed Brown + dm - the DM
1474aa0f6e3cSJed Brown - skip - PETSC_TRUE to skip preallocation
1475aa0f6e3cSJed Brown 
1476aa0f6e3cSJed Brown   Level: developer
1477aa0f6e3cSJed Brown 
1478aa0f6e3cSJed Brown .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1479aa0f6e3cSJed Brown @*/
1480aa0f6e3cSJed Brown PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1481aa0f6e3cSJed Brown {
1482aa0f6e3cSJed Brown   PetscFunctionBegin;
1483aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1484aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
1485aa0f6e3cSJed Brown   PetscFunctionReturn(0);
1486aa0f6e3cSJed Brown }
1487aa0f6e3cSJed Brown 
1488aa0f6e3cSJed Brown /*@
1489950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1490732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1491732e2eb9SMatthew G Knepley 
1492d083f849SBarry Smith   Logically Collective on dm
1493732e2eb9SMatthew G Knepley 
1494d8d19677SJose E. Roman   Input Parameters:
1495732e2eb9SMatthew G Knepley + dm - the DM
1496732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1497732e2eb9SMatthew G Knepley 
1498732e2eb9SMatthew G Knepley   Level: developer
1499f27dd7c6SMatthew G. Knepley 
1500f27dd7c6SMatthew G. Knepley   Options Database Keys:
1501f27dd7c6SMatthew G. Knepley . -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
1502f27dd7c6SMatthew G. Knepley 
1503b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1504732e2eb9SMatthew G Knepley @*/
1505732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1506732e2eb9SMatthew G Knepley {
1507732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1508732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1509732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1510732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1511732e2eb9SMatthew G Knepley }
1512732e2eb9SMatthew G Knepley 
1513b06ff27eSHong Zhang /*@
1514b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1515b06ff27eSHong Zhang     but the array for values will not be allocated.
1516b06ff27eSHong Zhang 
1517d083f849SBarry Smith   Logically Collective on dm
1518b06ff27eSHong Zhang 
1519d8d19677SJose E. Roman   Input Parameters:
1520b06ff27eSHong Zhang + dm - the DM
1521b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1522b06ff27eSHong Zhang 
1523b06ff27eSHong Zhang   Level: developer
1524b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1525b06ff27eSHong Zhang @*/
1526b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1527b06ff27eSHong Zhang {
1528b06ff27eSHong Zhang   PetscFunctionBegin;
1529b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1530b06ff27eSHong Zhang   dm->structure_only = only;
1531b06ff27eSHong Zhang   PetscFunctionReturn(0);
1532b06ff27eSHong Zhang }
1533b06ff27eSHong Zhang 
1534a89ea682SMatthew G Knepley /*@C
1535aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1536a89ea682SMatthew G Knepley 
1537a89ea682SMatthew G Knepley   Not Collective
1538a89ea682SMatthew G Knepley 
1539a89ea682SMatthew G Knepley   Input Parameters:
1540a89ea682SMatthew G Knepley + dm - the DM object
1541a5b23f4aSJose E. Roman . count - The minimum size
154269291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1543a89ea682SMatthew G Knepley 
1544a89ea682SMatthew G Knepley   Output Parameter:
1545a89ea682SMatthew G Knepley . array - the work array
1546a89ea682SMatthew G Knepley 
1547a89ea682SMatthew G Knepley   Level: developer
1548a89ea682SMatthew G Knepley 
1549a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1550a89ea682SMatthew G Knepley @*/
155169291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1552a89ea682SMatthew G Knepley {
1553aa1993deSMatthew G Knepley   DMWorkLink     link;
155469291d52SBarry Smith   PetscMPIInt    dsize;
1555a89ea682SMatthew G Knepley 
1556a89ea682SMatthew G Knepley   PetscFunctionBegin;
1557a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1558aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1559aa1993deSMatthew G Knepley   if (dm->workin) {
1560aa1993deSMatthew G Knepley     link       = dm->workin;
1561aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1562aa1993deSMatthew G Knepley   } else {
1563*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscNewLog(dm,&link));
1564a89ea682SMatthew G Knepley   }
1565*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Type_size(dtype,&dsize));
15665056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1567*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(link->mem));
1568*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc(dsize*count,&link->mem));
1569854ce69bSBarry Smith     link->bytes = dsize*count;
1570aa1993deSMatthew G Knepley   }
1571aa1993deSMatthew G Knepley   link->next   = dm->workout;
1572aa1993deSMatthew G Knepley   dm->workout  = link;
157300d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
157400d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
157500d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
157600d952a4SJed Brown #endif
1577aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1578a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1579a89ea682SMatthew G Knepley }
1580a89ea682SMatthew G Knepley 
1581aa1993deSMatthew G Knepley /*@C
1582aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1583aa1993deSMatthew G Knepley 
1584aa1993deSMatthew G Knepley   Not Collective
1585aa1993deSMatthew G Knepley 
1586aa1993deSMatthew G Knepley   Input Parameters:
1587aa1993deSMatthew G Knepley + dm - the DM object
1588a5b23f4aSJose E. Roman . count - The minimum size
158969291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1590aa1993deSMatthew G Knepley 
1591aa1993deSMatthew G Knepley   Output Parameter:
1592aa1993deSMatthew G Knepley . array - the work array
1593aa1993deSMatthew G Knepley 
1594aa1993deSMatthew G Knepley   Level: developer
1595aa1993deSMatthew G Knepley 
159695452b02SPatrick Sanan   Developer Notes:
159795452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1598147403d9SBarry Smith 
1599aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1600aa1993deSMatthew G Knepley @*/
160169291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1602aa1993deSMatthew G Knepley {
1603aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1604aa1993deSMatthew G Knepley 
1605aa1993deSMatthew G Knepley   PetscFunctionBegin;
1606aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1607aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1608aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1609aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1610aa1993deSMatthew G Knepley       *p           = link->next;
1611aa1993deSMatthew G Knepley       link->next   = dm->workin;
1612aa1993deSMatthew G Knepley       dm->workin   = link;
16130298fd71SBarry Smith       *(void**)mem = NULL;
1614aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1615aa1993deSMatthew G Knepley     }
1616aa1993deSMatthew G Knepley   }
1617aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1618aa1993deSMatthew G Knepley }
1619e7c4fc90SDmitry Karpeev 
16208cda7954SMatthew G. Knepley /*@C
1621147403d9SBarry 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()
16228cda7954SMatthew G. Knepley 
16238cda7954SMatthew G. Knepley   Logically collective on DM
16248cda7954SMatthew G. Knepley 
16258cda7954SMatthew G. Knepley   Input Parameters:
16268cda7954SMatthew G. Knepley + dm     - The DM
16278cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16288cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
16298cda7954SMatthew G. Knepley 
1630147403d9SBarry Smith   Calling sequence of nullsp:
1631147403d9SBarry Smith .vb
1632147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1633147403d9SBarry Smith .ve
1634147403d9SBarry Smith +  dm        - The present DM
1635147403d9SBarry Smith .  origField - The field number given above, in the original DM
1636147403d9SBarry Smith .  field     - The field number in dm
1637147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16388cda7954SMatthew G. Knepley 
16398cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16408cda7954SMatthew G. Knepley 
16418cda7954SMatthew G. Knepley .seealso: DMGetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
1642147403d9SBarry Smith @*/
1643147403d9SBarry Smith PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace*))
1644435a35e8SMatthew G Knepley {
1645435a35e8SMatthew G Knepley   PetscFunctionBegin;
1646435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16477a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1648435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1649435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1650435a35e8SMatthew G Knepley }
1651435a35e8SMatthew G Knepley 
16528cda7954SMatthew G. Knepley /*@C
16538cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
16548cda7954SMatthew G. Knepley 
16558cda7954SMatthew G. Knepley   Not collective
16568cda7954SMatthew G. Knepley 
16578cda7954SMatthew G. Knepley   Input Parameters:
16588cda7954SMatthew G. Knepley + dm     - The DM
16598cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16608cda7954SMatthew G. Knepley 
16618cda7954SMatthew G. Knepley   Output Parameter:
16628cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
16638cda7954SMatthew G. Knepley 
1664147403d9SBarry Smith   Calling sequence of nullsp:
1665147403d9SBarry Smith .vb
1666147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1667147403d9SBarry Smith .ve
1668147403d9SBarry Smith +  dm        - The present DM
1669147403d9SBarry Smith .  origField - The field number given above, in the original DM
1670147403d9SBarry Smith .  field     - The field number in dm
1671147403d9SBarry Smith -  nullSpace - The nullspace for the given field
16728cda7954SMatthew G. Knepley 
16738cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16748cda7954SMatthew G. Knepley 
16758cda7954SMatthew G. Knepley .seealso: DMSetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
1676147403d9SBarry Smith @*/
1677147403d9SBarry Smith PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
16780a50eb56SMatthew G. Knepley {
16790a50eb56SMatthew G. Knepley   PetscFunctionBegin;
16800a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1681f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
16827a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
16830a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
16840a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
16850a50eb56SMatthew G. Knepley }
16860a50eb56SMatthew G. Knepley 
16878cda7954SMatthew G. Knepley /*@C
16888cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
16898cda7954SMatthew G. Knepley 
16908cda7954SMatthew G. Knepley   Logically collective on DM
16918cda7954SMatthew G. Knepley 
16928cda7954SMatthew G. Knepley   Input Parameters:
16938cda7954SMatthew G. Knepley + dm     - The DM
16948cda7954SMatthew G. Knepley . field  - The field number for the nullspace
16958cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
16968cda7954SMatthew G. Knepley 
1697147403d9SBarry Smith   Calling sequence of nullsp:
1698147403d9SBarry Smith .vb
1699147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1700147403d9SBarry Smith .ve
1701147403d9SBarry Smith +  dm        - The present DM
1702147403d9SBarry Smith .  origField - The field number given above, in the original DM
1703147403d9SBarry Smith .  field     - The field number in dm
1704147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17058cda7954SMatthew G. Knepley 
17068cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
17078cda7954SMatthew G. Knepley 
17088cda7954SMatthew G. Knepley .seealso: DMGetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
1709147403d9SBarry Smith @*/
1710147403d9SBarry Smith PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1711f9d4088aSMatthew G. Knepley {
1712f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1713f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17147a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1715f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1716f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1717f9d4088aSMatthew G. Knepley }
1718f9d4088aSMatthew G. Knepley 
17198cda7954SMatthew G. Knepley /*@C
17208cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
17218cda7954SMatthew G. Knepley 
17228cda7954SMatthew G. Knepley   Not collective
17238cda7954SMatthew G. Knepley 
17248cda7954SMatthew G. Knepley   Input Parameters:
17258cda7954SMatthew G. Knepley + dm     - The DM
17268cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17278cda7954SMatthew G. Knepley 
17288cda7954SMatthew G. Knepley   Output Parameter:
17298cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
17308cda7954SMatthew G. Knepley 
1731147403d9SBarry Smith   Calling sequence of nullsp:
1732147403d9SBarry Smith .vb
1733147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1734147403d9SBarry Smith .ve
1735147403d9SBarry Smith +  dm        - The present DM
1736147403d9SBarry Smith .  origField - The field number given above, in the original DM
1737147403d9SBarry Smith .  field     - The field number in dm
1738147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17398cda7954SMatthew G. Knepley 
17408cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
17418cda7954SMatthew G. Knepley 
17428cda7954SMatthew G. Knepley .seealso: DMSetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
1743147403d9SBarry Smith @*/
1744147403d9SBarry Smith PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1745f9d4088aSMatthew G. Knepley {
1746f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1747f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1748f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17497a8be351SBarry Smith   PetscCheck(field < 10,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1750f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1751f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1752f9d4088aSMatthew G. Knepley }
1753f9d4088aSMatthew G. Knepley 
17544f3b5142SJed Brown /*@C
17554d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
17564d343eeaSMatthew G Knepley 
17574d343eeaSMatthew G Knepley   Not collective
17584d343eeaSMatthew G Knepley 
17594d343eeaSMatthew G Knepley   Input Parameter:
17604d343eeaSMatthew G Knepley . dm - the DM object
17614d343eeaSMatthew G Knepley 
17624d343eeaSMatthew G Knepley   Output Parameters:
17630298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
17640298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
17650298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
17664d343eeaSMatthew G Knepley 
17674d343eeaSMatthew G Knepley   Level: intermediate
17684d343eeaSMatthew G Knepley 
176921c9b008SJed Brown   Notes:
177021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
177121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
177221c9b008SJed Brown   PetscFree().
177321c9b008SJed Brown 
17744d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
17754d343eeaSMatthew G Knepley @*/
177637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
17774d343eeaSMatthew G Knepley {
177837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
17794d343eeaSMatthew G Knepley 
17804d343eeaSMatthew G Knepley   PetscFunctionBegin;
17814d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
178269ca1f37SDmitry Karpeev   if (numFields) {
1783534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
178469ca1f37SDmitry Karpeev     *numFields = 0;
178569ca1f37SDmitry Karpeev   }
178637d0c07bSMatthew G Knepley   if (fieldNames) {
178737d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
17880298fd71SBarry Smith     *fieldNames = NULL;
178969ca1f37SDmitry Karpeev   }
179069ca1f37SDmitry Karpeev   if (fields) {
179169ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
17920298fd71SBarry Smith     *fields = NULL;
179369ca1f37SDmitry Karpeev   }
1794*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalSection(dm, &section));
179537d0c07bSMatthew G Knepley   if (section) {
17963a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
179737d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
179837d0c07bSMatthew G Knepley 
1799*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetGlobalSection(dm, &sectionGlobal));
1800*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetNumFields(section, &nF));
1801*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices));
1802*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
180337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
180437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
1805*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
180637d0c07bSMatthew G Knepley     }
180737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
180837d0c07bSMatthew G Knepley       PetscInt gdof;
180937d0c07bSMatthew G Knepley 
1810*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(sectionGlobal, p, &gdof));
181137d0c07bSMatthew G Knepley       if (gdof > 0) {
181237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
18133a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
181437d0c07bSMatthew G Knepley 
1815*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetFieldDof(section, p, f, &fdof));
1816*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
18173a544194SStefano Zampini           fpdof = fdof-fcdof;
18183a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
18193a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
18203a544194SStefano Zampini             fieldNc[f] = 1;
18213a544194SStefano Zampini           }
18223a544194SStefano Zampini           fieldSizes[f] += fpdof;
182337d0c07bSMatthew G Knepley         }
182437d0c07bSMatthew G Knepley       }
182537d0c07bSMatthew G Knepley     }
182637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1827*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
182837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
182937d0c07bSMatthew G Knepley     }
183037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
183137d0c07bSMatthew G Knepley       PetscInt gdof, goff;
183237d0c07bSMatthew G Knepley 
1833*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(sectionGlobal, p, &gdof));
183437d0c07bSMatthew G Knepley       if (gdof > 0) {
1835*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(sectionGlobal, p, &goff));
183637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
183737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
183837d0c07bSMatthew G Knepley 
1839*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetFieldDof(section, p, f, &fdof));
1840*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
184137d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
184237d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
184337d0c07bSMatthew G Knepley           }
184437d0c07bSMatthew G Knepley         }
184537d0c07bSMatthew G Knepley       }
184637d0c07bSMatthew G Knepley     }
18478865f1eaSKarl Rupp     if (numFields) *numFields = nF;
184837d0c07bSMatthew G Knepley     if (fieldNames) {
1849*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(nF, fieldNames));
185037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
185137d0c07bSMatthew G Knepley         const char *fieldName;
185237d0c07bSMatthew G Knepley 
1853*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetFieldName(section, f, &fieldName));
1854*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]));
185537d0c07bSMatthew G Knepley       }
185637d0c07bSMatthew G Knepley     }
185737d0c07bSMatthew G Knepley     if (fields) {
1858*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(nF, fields));
185937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
18603a544194SStefano Zampini         PetscInt bs, in[2], out[2];
18613a544194SStefano Zampini 
1862*5f80ce2aSJacob Faibussowitsch         CHKERRQ(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
18633a544194SStefano Zampini         in[0] = -fieldNc[f];
18643a544194SStefano Zampini         in[1] = fieldNc[f];
1865*5f80ce2aSJacob Faibussowitsch         CHKERRMPI(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
18663a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
1867*5f80ce2aSJacob Faibussowitsch         CHKERRQ(ISSetBlockSize((*fields)[f], bs));
186837d0c07bSMatthew G Knepley       }
186937d0c07bSMatthew G Knepley     }
1870*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree3(fieldSizes,fieldNc,fieldIndices));
18718865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
1872*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->createfieldis)(dm, numFields, fieldNames, fields));
187369ca1f37SDmitry Karpeev   }
18744d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
18754d343eeaSMatthew G Knepley }
18764d343eeaSMatthew G Knepley 
187716621825SDmitry Karpeev /*@C
187816621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
187916621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
188016621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1881e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1882e7c4fc90SDmitry Karpeev 
1883e7c4fc90SDmitry Karpeev   Not collective
1884e7c4fc90SDmitry Karpeev 
1885e7c4fc90SDmitry Karpeev   Input Parameter:
1886e7c4fc90SDmitry Karpeev . dm - the DM object
1887e7c4fc90SDmitry Karpeev 
1888e7c4fc90SDmitry Karpeev   Output Parameters:
18890298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
18900298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
18910298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
18920298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1893e7c4fc90SDmitry Karpeev 
1894e7c4fc90SDmitry Karpeev   Level: intermediate
1895e7c4fc90SDmitry Karpeev 
1896e7c4fc90SDmitry Karpeev   Notes:
1897e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1898e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1899e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1900e7c4fc90SDmitry Karpeev 
1901e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1902e7c4fc90SDmitry Karpeev @*/
190316621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1904e7c4fc90SDmitry Karpeev {
1905e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1906e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19078865f1eaSKarl Rupp   if (len) {
1908534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
19098865f1eaSKarl Rupp     *len = 0;
19108865f1eaSKarl Rupp   }
19118865f1eaSKarl Rupp   if (namelist) {
19128865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1913ea78f98cSLisandro Dalcin     *namelist = NULL;
19148865f1eaSKarl Rupp   }
19158865f1eaSKarl Rupp   if (islist) {
19168865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1917ea78f98cSLisandro Dalcin     *islist = NULL;
19188865f1eaSKarl Rupp   }
19198865f1eaSKarl Rupp   if (dmlist) {
19208865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1921ea78f98cSLisandro Dalcin     *dmlist = NULL;
19228865f1eaSKarl Rupp   }
1923f3f0edfdSDmitry Karpeev   /*
1924f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1925f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1926f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1927f3f0edfdSDmitry Karpeev    */
19287a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
192916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1930435a35e8SMatthew G Knepley     PetscSection section;
1931435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1932435a35e8SMatthew G Knepley 
1933*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLocalSection(dm, &section));
1934*5f80ce2aSJacob Faibussowitsch     if (section) CHKERRQ(PetscSectionGetNumFields(section, &numFields));
1935435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1936f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
1937*5f80ce2aSJacob Faibussowitsch       if (namelist) CHKERRQ(PetscMalloc1(numFields,namelist));
1938*5f80ce2aSJacob Faibussowitsch       if (islist)   CHKERRQ(PetscMalloc1(numFields,islist));
1939*5f80ce2aSJacob Faibussowitsch       if (dmlist)   CHKERRQ(PetscMalloc1(numFields,dmlist));
1940435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1941435a35e8SMatthew G Knepley         const char *fieldName;
1942435a35e8SMatthew G Knepley 
1943*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
194403dc3394SMatthew G. Knepley         if (namelist) {
1945*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetFieldName(section, f, &fieldName));
1946*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscStrallocpy(fieldName, (char**) &(*namelist)[f]));
1947435a35e8SMatthew G Knepley         }
194803dc3394SMatthew G. Knepley       }
1949435a35e8SMatthew G Knepley     } else {
1950*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMCreateFieldIS(dm, len, namelist, islist));
1951e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
19520298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1953e7c4fc90SDmitry Karpeev     }
19548865f1eaSKarl Rupp   } else {
1955*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist));
195616621825SDmitry Karpeev   }
195716621825SDmitry Karpeev   PetscFunctionReturn(0);
195816621825SDmitry Karpeev }
195916621825SDmitry Karpeev 
1960564cec59SMatthew G. Knepley /*@
1961435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1962435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1963435a35e8SMatthew G Knepley 
1964435a35e8SMatthew G Knepley   Not collective
1965435a35e8SMatthew G Knepley 
1966435a35e8SMatthew G Knepley   Input Parameters:
19672adcc780SMatthew G. Knepley + dm        - The DM object
19682adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
19692adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1970435a35e8SMatthew G Knepley 
1971435a35e8SMatthew G Knepley   Output Parameters:
19722adcc780SMatthew G. Knepley + is - The global indices for the subproblem
19732adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1974435a35e8SMatthew G Knepley 
19755d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19765d3b26e6SMatthew G. Knepley 
1977435a35e8SMatthew G Knepley   Level: intermediate
1978435a35e8SMatthew G Knepley 
19795d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1980435a35e8SMatthew G Knepley @*/
198137bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1982435a35e8SMatthew G Knepley {
1983435a35e8SMatthew G Knepley   PetscFunctionBegin;
1984435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1985435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
19868865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
19878865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
19887a8be351SBarry Smith   PetscCheck(dm->ops->createsubdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1989*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->createsubdm)(dm, numFields, fields, is, subdm));
1990435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1991435a35e8SMatthew G Knepley }
1992435a35e8SMatthew G Knepley 
19932adcc780SMatthew G. Knepley /*@C
19942adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
19952adcc780SMatthew G. Knepley 
19962adcc780SMatthew G. Knepley   Not collective
19972adcc780SMatthew G. Knepley 
1998d8d19677SJose E. Roman   Input Parameters:
19992adcc780SMatthew G. Knepley + dms - The DM objects
20002adcc780SMatthew G. Knepley - len - The number of DMs
20012adcc780SMatthew G. Knepley 
20022adcc780SMatthew G. Knepley   Output Parameters:
2003a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
20042adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
20052adcc780SMatthew G. Knepley 
20065d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
20075d3b26e6SMatthew G. Knepley 
20082adcc780SMatthew G. Knepley   Level: intermediate
20092adcc780SMatthew G. Knepley 
20105d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
20112adcc780SMatthew G. Knepley @*/
20122adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
20132adcc780SMatthew G. Knepley {
20142adcc780SMatthew G. Knepley   PetscInt       i;
20152adcc780SMatthew G. Knepley 
20162adcc780SMatthew G. Knepley   PetscFunctionBegin;
20172adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
20182adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
20192adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
2020a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
20217a8be351SBarry Smith   PetscCheck(len >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
20222adcc780SMatthew G. Knepley   if (len) {
2023b9d85ea2SLisandro Dalcin     DM dm = dms[0];
20247a8be351SBarry Smith     PetscCheck(dm->ops->createsuperdm,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
2025*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->createsuperdm)(dms, len, is, superdm));
20262adcc780SMatthew G. Knepley   }
20272adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
20282adcc780SMatthew G. Knepley }
20292adcc780SMatthew G. Knepley 
203016621825SDmitry Karpeev /*@C
20318d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
20328d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
20338d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
20348d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
20358d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
203616621825SDmitry Karpeev 
203716621825SDmitry Karpeev   Not collective
203816621825SDmitry Karpeev 
203916621825SDmitry Karpeev   Input Parameter:
204016621825SDmitry Karpeev . dm - the DM object
204116621825SDmitry Karpeev 
204216621825SDmitry Karpeev   Output Parameters:
20430298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
20440298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
20450298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
20460298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
20470298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
204816621825SDmitry Karpeev 
204916621825SDmitry Karpeev   Level: intermediate
205016621825SDmitry Karpeev 
205116621825SDmitry Karpeev   Notes:
205216621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
205316621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
205416621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
205516621825SDmitry Karpeev 
2056245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
205716621825SDmitry Karpeev @*/
20588d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
205916621825SDmitry Karpeev {
2060be081cd6SPeter Brune   DMSubDomainHookLink link;
2061be081cd6SPeter Brune   PetscInt            i,l;
206216621825SDmitry Karpeev 
206316621825SDmitry Karpeev   PetscFunctionBegin;
206416621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
206514a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
20660298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
20670298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
20680298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
20690298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
2070f3f0edfdSDmitry Karpeev   /*
2071f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2072f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2073f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2074f3f0edfdSDmitry Karpeev    */
20757a8be351SBarry Smith   PetscCheck(dm->setupcalled,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
207616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2077*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist));
207814a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2079f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2080be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2081be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
2082*5f80ce2aSJacob Faibussowitsch           if (link->ddhook) CHKERRQ((*link->ddhook)(dm,(*dmlist)[i],link->ctx));
2083be081cd6SPeter Brune         }
2084648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2085e7c4fc90SDmitry Karpeev       }
208614a18fd3SPeter Brune     }
208714a18fd3SPeter Brune     if (len) *len = l;
208814a18fd3SPeter Brune   }
2089e30e807fSPeter Brune   PetscFunctionReturn(0);
2090e30e807fSPeter Brune }
2091e30e807fSPeter Brune 
2092e30e807fSPeter Brune /*@C
2093e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2094e30e807fSPeter Brune 
2095e30e807fSPeter Brune   Not collective
2096e30e807fSPeter Brune 
2097e30e807fSPeter Brune   Input Parameters:
2098e30e807fSPeter Brune + dm - the DM object
2099e30e807fSPeter Brune . n  - the number of subdomain scatters
2100e30e807fSPeter Brune - subdms - the local subdomains
2101e30e807fSPeter Brune 
2102e30e807fSPeter Brune   Output Parameters:
21036b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2104e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2105e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2106e30e807fSPeter Brune 
210795452b02SPatrick Sanan   Notes:
210895452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2109e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2110e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2111e30e807fSPeter Brune   solution and residual data.
2112e30e807fSPeter Brune 
2113e30e807fSPeter Brune   Level: developer
2114e30e807fSPeter Brune 
2115e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2116e30e807fSPeter Brune @*/
2117e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2118e30e807fSPeter Brune {
2119e30e807fSPeter Brune   PetscFunctionBegin;
2120e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2121e30e807fSPeter Brune   PetscValidPointer(subdms,3);
21227a8be351SBarry Smith   PetscCheck(dm->ops->createddscatters,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
2123*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat));
2124e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2125e7c4fc90SDmitry Karpeev }
2126e7c4fc90SDmitry Karpeev 
212747c6ae99SBarry Smith /*@
212847c6ae99SBarry Smith   DMRefine - Refines a DM object
212947c6ae99SBarry Smith 
2130d083f849SBarry Smith   Collective on dm
213147c6ae99SBarry Smith 
2132d8d19677SJose E. Roman   Input Parameters:
213347c6ae99SBarry Smith + dm   - the DM object
213491d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
213547c6ae99SBarry Smith 
213647c6ae99SBarry Smith   Output Parameter:
21370298fd71SBarry Smith . dmf - the refined DM, or NULL
2138ae0a1c52SMatthew G Knepley 
2139f27dd7c6SMatthew G. Knepley   Options Database Keys:
2140412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2141412e9a14SMatthew G. Knepley 
21420298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
214347c6ae99SBarry Smith 
214447c6ae99SBarry Smith   Level: developer
214547c6ae99SBarry Smith 
2146e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
214747c6ae99SBarry Smith @*/
21487087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
214947c6ae99SBarry Smith {
2150c833c3b5SJed Brown   DMRefineHookLink link;
215147c6ae99SBarry Smith 
215247c6ae99SBarry Smith   PetscFunctionBegin;
2153732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21547a8be351SBarry Smith   PetscCheck(dm->ops->refine,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
2155*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(DM_Refine,dm,0,0,0));
2156*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->refine)(dm,comm,dmf));
21574057135bSMatthew G Knepley   if (*dmf) {
215843842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
21598865f1eaSKarl Rupp 
2160*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf));
21618865f1eaSKarl Rupp 
2162644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
21630598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2164656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
21658865f1eaSKarl Rupp 
2166*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetMatType(*dmf,dm->mattype));
2167c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
21688865f1eaSKarl Rupp       if (link->refinehook) {
2169*5f80ce2aSJacob Faibussowitsch         CHKERRQ((*link->refinehook)(dm,*dmf,link->ctx));
21708865f1eaSKarl Rupp       }
2171c833c3b5SJed Brown     }
2172c833c3b5SJed Brown   }
2173*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(DM_Refine,dm,0,0,0));
2174c833c3b5SJed Brown   PetscFunctionReturn(0);
2175c833c3b5SJed Brown }
2176c833c3b5SJed Brown 
2177bb9467b5SJed Brown /*@C
2178c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2179c833c3b5SJed Brown 
2180c833c3b5SJed Brown    Logically Collective
2181c833c3b5SJed Brown 
21824165533cSJose E. Roman    Input Parameters:
2183c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2184c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2185c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21860298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2187c833c3b5SJed Brown 
2188c833c3b5SJed Brown    Calling sequence of refinehook:
2189c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2190c833c3b5SJed Brown 
2191c833c3b5SJed Brown +  coarse - coarse level DM
2192c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2193c833c3b5SJed Brown -  ctx - optional user-defined function context
2194c833c3b5SJed Brown 
2195c833c3b5SJed Brown    Calling sequence for interphook:
2196c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2197c833c3b5SJed Brown 
2198c833c3b5SJed Brown +  coarse - coarse level DM
2199c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2200c833c3b5SJed Brown .  fine - fine level DM to update
2201c833c3b5SJed Brown -  ctx - optional user-defined function context
2202c833c3b5SJed Brown 
2203c833c3b5SJed Brown    Level: advanced
2204c833c3b5SJed Brown 
2205c833c3b5SJed Brown    Notes:
2206c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2207c833c3b5SJed Brown 
2208c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2209c833c3b5SJed Brown 
2210bb9467b5SJed Brown    This function is currently not available from Fortran.
2211bb9467b5SJed Brown 
2212c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2213c833c3b5SJed Brown @*/
2214c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2215c833c3b5SJed Brown {
2216c833c3b5SJed Brown   DMRefineHookLink link,*p;
2217c833c3b5SJed Brown 
2218c833c3b5SJed Brown   PetscFunctionBegin;
2219c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22203d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
22213d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
22223d8e3701SJed Brown   }
2223*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscNew(&link));
2224c833c3b5SJed Brown   link->refinehook = refinehook;
2225c833c3b5SJed Brown   link->interphook = interphook;
2226c833c3b5SJed Brown   link->ctx        = ctx;
22270298fd71SBarry Smith   link->next       = NULL;
2228c833c3b5SJed Brown   *p               = link;
2229c833c3b5SJed Brown   PetscFunctionReturn(0);
2230c833c3b5SJed Brown }
2231c833c3b5SJed Brown 
22323d8e3701SJed Brown /*@C
22333d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
22343d8e3701SJed Brown 
22353d8e3701SJed Brown    Logically Collective
22363d8e3701SJed Brown 
22374165533cSJose E. Roman    Input Parameters:
22383d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
22393d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
22403d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
22413d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22423d8e3701SJed Brown 
22433d8e3701SJed Brown    Level: advanced
22443d8e3701SJed Brown 
22453d8e3701SJed Brown    Notes:
22463d8e3701SJed Brown    This function does nothing if the hook is not in the list.
22473d8e3701SJed Brown 
22483d8e3701SJed Brown    This function is currently not available from Fortran.
22493d8e3701SJed Brown 
22503d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
22513d8e3701SJed Brown @*/
22523d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
22533d8e3701SJed Brown {
22543d8e3701SJed Brown   DMRefineHookLink link,*p;
22553d8e3701SJed Brown 
22563d8e3701SJed Brown   PetscFunctionBegin;
22573d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
22583d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
22593d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
22603d8e3701SJed Brown       link = *p;
22613d8e3701SJed Brown       *p = link->next;
2262*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(link));
22633d8e3701SJed Brown       break;
22643d8e3701SJed Brown     }
22653d8e3701SJed Brown   }
22663d8e3701SJed Brown   PetscFunctionReturn(0);
22673d8e3701SJed Brown }
22683d8e3701SJed Brown 
2269c833c3b5SJed Brown /*@
2270c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2271c833c3b5SJed Brown 
2272c833c3b5SJed Brown    Collective if any hooks are
2273c833c3b5SJed Brown 
22744165533cSJose E. Roman    Input Parameters:
2275c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2276e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2277c833c3b5SJed Brown -  fine - finer DM to update
2278c833c3b5SJed Brown 
2279c833c3b5SJed Brown    Level: developer
2280c833c3b5SJed Brown 
2281c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2282c833c3b5SJed Brown @*/
2283c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2284c833c3b5SJed Brown {
2285c833c3b5SJed Brown   DMRefineHookLink link;
2286c833c3b5SJed Brown 
2287c833c3b5SJed Brown   PetscFunctionBegin;
2288c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
22898865f1eaSKarl Rupp     if (link->interphook) {
2290*5f80ce2aSJacob Faibussowitsch       CHKERRQ((*link->interphook)(coarse,interp,fine,link->ctx));
22918865f1eaSKarl Rupp     }
22924057135bSMatthew G Knepley   }
229347c6ae99SBarry Smith   PetscFunctionReturn(0);
229447c6ae99SBarry Smith }
229547c6ae99SBarry Smith 
2296eb3f98d2SBarry Smith /*@
22971f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
22981f3379b2SToby Isaac 
22991f3379b2SToby Isaac    Collective on DM
23001f3379b2SToby Isaac 
23014165533cSJose E. Roman    Input Parameters:
23021f3379b2SToby Isaac +  coarse - coarse DM
23031f3379b2SToby Isaac .  fine   - fine DM
23041f3379b2SToby Isaac .  interp - (optional) the matrix computed by DMCreateInterpolation().  Implementations may not need this, but if it
23051f3379b2SToby Isaac             is available it can avoid some recomputation.  If it is provided, MatInterpolate() will be used if
23061f3379b2SToby Isaac             the coarse DM does not have a specialized implementation.
23071f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
23081f3379b2SToby Isaac 
23094165533cSJose E. Roman    Output Parameter:
23101f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
23111f3379b2SToby Isaac 
23121f3379b2SToby Isaac    Level: developer
23131f3379b2SToby Isaac 
23141f3379b2SToby Isaac    Note: This function exists because the interpolation of a solution vector between meshes is not always a linear
23151f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
23161f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
23171f3379b2SToby Isaac    slope-limiting reconstruction.
23181f3379b2SToby Isaac 
23191f3379b2SToby Isaac .seealso DMInterpolate(), DMCreateInterpolation()
23201f3379b2SToby Isaac @*/
23211f3379b2SToby Isaac PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
23221f3379b2SToby Isaac {
23231f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM,DM,Mat,Vec,Vec) = NULL;
23241f3379b2SToby Isaac 
23251f3379b2SToby Isaac   PetscFunctionBegin;
23261f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
23271f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp,MAT_CLASSID,3);
23281f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol,VEC_CLASSID,4);
23291f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol,VEC_CLASSID,5);
23301f3379b2SToby Isaac 
2331*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectQueryFunction((PetscObject)coarse,"DMInterpolateSolution_C", &interpsol));
23321f3379b2SToby Isaac   if (interpsol) {
2333*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
23341f3379b2SToby Isaac   } else if (interp) {
2335*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatInterpolate(interp, coarseSol, fineSol));
233698921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
23371f3379b2SToby Isaac   PetscFunctionReturn(0);
23381f3379b2SToby Isaac }
23391f3379b2SToby Isaac 
23401f3379b2SToby Isaac /*@
2341aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2342eb3f98d2SBarry Smith 
2343eb3f98d2SBarry Smith     Not Collective
2344eb3f98d2SBarry Smith 
2345eb3f98d2SBarry Smith     Input Parameter:
2346eb3f98d2SBarry Smith .   dm - the DM object
2347eb3f98d2SBarry Smith 
2348eb3f98d2SBarry Smith     Output Parameter:
2349eb3f98d2SBarry Smith .   level - number of refinements
2350eb3f98d2SBarry Smith 
2351eb3f98d2SBarry Smith     Level: developer
2352eb3f98d2SBarry Smith 
23536a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2354eb3f98d2SBarry Smith 
2355eb3f98d2SBarry Smith @*/
2356eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2357eb3f98d2SBarry Smith {
2358eb3f98d2SBarry Smith   PetscFunctionBegin;
2359eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2360eb3f98d2SBarry Smith   *level = dm->levelup;
2361eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2362eb3f98d2SBarry Smith }
2363eb3f98d2SBarry Smith 
2364fef3a512SBarry Smith /*@
2365aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2366fef3a512SBarry Smith 
2367fef3a512SBarry Smith     Not Collective
2368fef3a512SBarry Smith 
2369d8d19677SJose E. Roman     Input Parameters:
2370fef3a512SBarry Smith +   dm - the DM object
2371fef3a512SBarry Smith -   level - number of refinements
2372fef3a512SBarry Smith 
2373fef3a512SBarry Smith     Level: advanced
2374fef3a512SBarry Smith 
237595452b02SPatrick Sanan     Notes:
237695452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2377fef3a512SBarry Smith 
2378fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2379fef3a512SBarry Smith 
2380fef3a512SBarry Smith @*/
2381fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2382fef3a512SBarry Smith {
2383fef3a512SBarry Smith   PetscFunctionBegin;
2384fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2385fef3a512SBarry Smith   dm->levelup = level;
2386fef3a512SBarry Smith   PetscFunctionReturn(0);
2387fef3a512SBarry Smith }
2388fef3a512SBarry Smith 
2389d410b0cfSMatthew G. Knepley /*@
2390d410b0cfSMatthew G. Knepley   DMExtrude - Extrude a DM object from a surface
2391d410b0cfSMatthew G. Knepley 
2392d410b0cfSMatthew G. Knepley   Collective on dm
2393d410b0cfSMatthew G. Knepley 
2394f1a722f8SMatthew G. Knepley   Input Parameters:
2395d410b0cfSMatthew G. Knepley + dm     - the DM object
2396d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2397d410b0cfSMatthew G. Knepley 
2398d410b0cfSMatthew G. Knepley   Output Parameter:
2399d410b0cfSMatthew G. Knepley . dme - the extruded DM, or NULL
2400d410b0cfSMatthew G. Knepley 
2401d410b0cfSMatthew G. Knepley   Note: If no extrusion was done, the return value is NULL
2402d410b0cfSMatthew G. Knepley 
2403d410b0cfSMatthew G. Knepley   Level: developer
2404d410b0cfSMatthew G. Knepley 
2405d410b0cfSMatthew G. Knepley .seealso DMRefine(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector()
2406d410b0cfSMatthew G. Knepley @*/
2407d410b0cfSMatthew G. Knepley PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2408d410b0cfSMatthew G. Knepley {
2409d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2410d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24117a8be351SBarry Smith   PetscCheck(dm->ops->extrude,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "DM type %s does not implement DMExtrude", ((PetscObject) dm)->type_name);
2412*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->extrude)(dm, layers, dme));
2413d410b0cfSMatthew G. Knepley   if (*dme) {
2414d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
2415*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectCopyFortranFunctionPointers((PetscObject) dm, (PetscObject) *dme));
2416d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
2417*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetMatType(*dme, dm->mattype));
2418d410b0cfSMatthew G. Knepley   }
2419d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2420d410b0cfSMatthew G. Knepley }
2421d410b0cfSMatthew G. Knepley 
2422ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2423ca3d3a14SMatthew G. Knepley {
2424ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2425ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2426ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2427ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2428ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2429ca3d3a14SMatthew G. Knepley }
2430ca3d3a14SMatthew G. Knepley 
2431ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2432ca3d3a14SMatthew G. Knepley {
2433ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2434ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2435ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2436ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2437ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2438ca3d3a14SMatthew G. Knepley }
2439ca3d3a14SMatthew G. Knepley 
2440ca3d3a14SMatthew G. Knepley /*@
2441c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2442ca3d3a14SMatthew G. Knepley 
2443ca3d3a14SMatthew G. Knepley   Input Parameter:
2444ca3d3a14SMatthew G. Knepley . dm - The DM
2445ca3d3a14SMatthew G. Knepley 
2446ca3d3a14SMatthew G. Knepley   Output Parameter:
2447ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2448ca3d3a14SMatthew G. Knepley 
2449ca3d3a14SMatthew G. Knepley   Level: developer
2450ca3d3a14SMatthew G. Knepley 
2451436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2452ca3d3a14SMatthew G. Knepley @*/
2453ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2454ca3d3a14SMatthew G. Knepley {
2455ca3d3a14SMatthew G. Knepley   Vec            tv;
2456ca3d3a14SMatthew G. Knepley 
2457ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2458ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2459534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2460*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetBasisTransformVec_Internal(dm, &tv));
2461ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2462ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2463ca3d3a14SMatthew G. Knepley }
2464ca3d3a14SMatthew G. Knepley 
2465ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2466ca3d3a14SMatthew G. Knepley {
2467ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2468ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2469ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2470ca3d3a14SMatthew G. Knepley 
2471ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2472*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDim(dm, &cdim));
2473*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalSection(dm, &s));
2474*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(s, &pStart, &pEnd));
2475*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetNumFields(s, &Nf));
2476*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMClone(dm, &dm->transformDM));
2477*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalSection(dm->transformDM, &ts));
2478*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetNumFields(ts, Nf));
2479*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(ts, pStart, pEnd));
2480ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2481*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldComponents(s, f, &Nc));
2482ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2483ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2484ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2485*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldDof(s, p, f, &dof));
2486ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2487*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
2488*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2489ca3d3a14SMatthew G. Knepley     }
2490ca3d3a14SMatthew G. Knepley   }
2491*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(ts));
2492*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateLocalVector(dm->transformDM, &dm->transform));
2493*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArray(dm->transform, &ta));
2494ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2495ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2496*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldDof(ts, p, f, &dof));
2497ca3d3a14SMatthew G. Knepley       if (dof) {
2498ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2499ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2500ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2501ca3d3a14SMatthew G. Knepley 
2502ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2503*5f80ce2aSJacob Faibussowitsch         CHKERRQ((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
2504*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva));
2505*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscArraycpy(tva, A, PetscSqr(cdim)));
2506ca3d3a14SMatthew G. Knepley       }
2507ca3d3a14SMatthew G. Knepley     }
2508ca3d3a14SMatthew G. Knepley   }
2509*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArray(dm->transform, &ta));
2510ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2511ca3d3a14SMatthew G. Knepley }
2512ca3d3a14SMatthew G. Knepley 
2513ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2514ca3d3a14SMatthew G. Knepley {
2515ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2516ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2517ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2518ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2519ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2520ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2521ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2522*5f80ce2aSJacob Faibussowitsch   if (newdm->transformSetUp) CHKERRQ(DMConstructBasisTransform_Internal(newdm));
2523ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2524ca3d3a14SMatthew G. Knepley }
2525ca3d3a14SMatthew G. Knepley 
2526bb9467b5SJed Brown /*@C
2527baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2528baf369e7SPeter Brune 
2529baf369e7SPeter Brune    Logically Collective
2530baf369e7SPeter Brune 
25314165533cSJose E. Roman    Input Parameters:
2532baf369e7SPeter Brune +  dm - the DM
2533baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2534baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
25350298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2536baf369e7SPeter Brune 
2537baf369e7SPeter Brune    Calling sequence for beginhook:
2538baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2539baf369e7SPeter Brune 
2540baf369e7SPeter Brune +  dm - global DM
2541baf369e7SPeter Brune .  g - global vector
2542baf369e7SPeter Brune .  mode - mode
2543baf369e7SPeter Brune .  l - local vector
2544baf369e7SPeter Brune -  ctx - optional user-defined function context
2545baf369e7SPeter Brune 
2546baf369e7SPeter Brune    Calling sequence for endhook:
2547ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2548baf369e7SPeter Brune 
2549baf369e7SPeter Brune +  global - global DM
2550baf369e7SPeter Brune -  ctx - optional user-defined function context
2551baf369e7SPeter Brune 
2552baf369e7SPeter Brune    Level: advanced
2553baf369e7SPeter Brune 
2554baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2555baf369e7SPeter Brune @*/
2556baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2557baf369e7SPeter Brune {
2558baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2559baf369e7SPeter Brune 
2560baf369e7SPeter Brune   PetscFunctionBegin;
2561baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2562baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2563*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscNew(&link));
2564baf369e7SPeter Brune   link->beginhook = beginhook;
2565baf369e7SPeter Brune   link->endhook   = endhook;
2566baf369e7SPeter Brune   link->ctx       = ctx;
25670298fd71SBarry Smith   link->next      = NULL;
2568baf369e7SPeter Brune   *p              = link;
2569baf369e7SPeter Brune   PetscFunctionReturn(0);
2570baf369e7SPeter Brune }
2571baf369e7SPeter Brune 
25724c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
25734c274da1SToby Isaac {
25744c274da1SToby Isaac   Mat cMat;
257579769bd5SJed Brown   Vec cVec, cBias;
25764c274da1SToby Isaac   PetscSection section, cSec;
25774c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
25784c274da1SToby Isaac 
25794c274da1SToby Isaac   PetscFunctionBegin;
25804c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2581*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDefaultConstraints(dm,&cSec,&cMat,&cBias));
25824c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
25835db9a05bSToby Isaac     PetscInt nRows;
25845db9a05bSToby Isaac 
2585*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatGetSize(cMat,&nRows,NULL));
25865db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2587*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLocalSection(dm,&section));
2588*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatCreateVecs(cMat,NULL,&cVec));
2589*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatMult(cMat,l,cVec));
2590*5f80ce2aSJacob Faibussowitsch     if (cBias) CHKERRQ(VecAXPY(cVec,1.,cBias));
2591*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetChart(cSec,&pStart,&pEnd));
25924c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
2593*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(cSec,p,&dof));
25944c274da1SToby Isaac       if (dof) {
25954c274da1SToby Isaac         PetscScalar *vals;
2596*5f80ce2aSJacob Faibussowitsch         CHKERRQ(VecGetValuesSection(cVec,cSec,p,&vals));
2597*5f80ce2aSJacob Faibussowitsch         CHKERRQ(VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES));
25984c274da1SToby Isaac       }
25994c274da1SToby Isaac     }
2600*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecDestroy(&cVec));
26014c274da1SToby Isaac   }
26024c274da1SToby Isaac   PetscFunctionReturn(0);
26034c274da1SToby Isaac }
26044c274da1SToby Isaac 
260547c6ae99SBarry Smith /*@
260601729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
260701729b5cSPatrick Sanan 
2608d083f849SBarry Smith     Neighbor-wise Collective on dm
260901729b5cSPatrick Sanan 
261001729b5cSPatrick Sanan     Input Parameters:
261101729b5cSPatrick Sanan +   dm - the DM object
261201729b5cSPatrick Sanan .   g - the global vector
261301729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
261401729b5cSPatrick Sanan -   l - the local vector
261501729b5cSPatrick Sanan 
261601729b5cSPatrick Sanan     Notes:
261701729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
261801729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
261901729b5cSPatrick Sanan 
262001729b5cSPatrick Sanan     Level: beginner
262101729b5cSPatrick Sanan 
262201729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
262301729b5cSPatrick Sanan 
262401729b5cSPatrick Sanan @*/
262501729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
262601729b5cSPatrick Sanan {
262701729b5cSPatrick Sanan   PetscFunctionBegin;
2628*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGlobalToLocalBegin(dm,g,mode,l));
2629*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGlobalToLocalEnd(dm,g,mode,l));
263001729b5cSPatrick Sanan   PetscFunctionReturn(0);
263101729b5cSPatrick Sanan }
263201729b5cSPatrick Sanan 
263301729b5cSPatrick Sanan /*@
263447c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
263547c6ae99SBarry Smith 
2636d083f849SBarry Smith     Neighbor-wise Collective on dm
263747c6ae99SBarry Smith 
263847c6ae99SBarry Smith     Input Parameters:
263947c6ae99SBarry Smith +   dm - the DM object
264047c6ae99SBarry Smith .   g - the global vector
264147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
264247c6ae99SBarry Smith -   l - the local vector
264347c6ae99SBarry Smith 
264401729b5cSPatrick Sanan     Level: intermediate
264547c6ae99SBarry Smith 
264601729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
264747c6ae99SBarry Smith 
264847c6ae99SBarry Smith @*/
26497087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
265047c6ae99SBarry Smith {
26517128ae9fSMatthew G Knepley   PetscSF                 sf;
2652baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
265347c6ae99SBarry Smith 
265447c6ae99SBarry Smith   PetscFunctionBegin;
2655171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2656baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
26578865f1eaSKarl Rupp     if (link->beginhook) {
2658*5f80ce2aSJacob Faibussowitsch       CHKERRQ((*link->beginhook)(dm,g,mode,l,link->ctx));
26598865f1eaSKarl Rupp     }
2660baf369e7SPeter Brune   }
2661*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetSectionSF(dm, &sf));
26627128ae9fSMatthew G Knepley   if (sf) {
2663ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2664ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2665d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
26667128ae9fSMatthew G Knepley 
26677a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
2668*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetArrayAndMemType(l, &lArray, &lmtype));
2669*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
2670*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
2671*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecRestoreArrayAndMemType(l, &lArray));
2672*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecRestoreArrayReadAndMemType(g, &gArray));
26737128ae9fSMatthew G Knepley   } else {
26747a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2675*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
26767128ae9fSMatthew G Knepley   }
267747c6ae99SBarry Smith   PetscFunctionReturn(0);
267847c6ae99SBarry Smith }
267947c6ae99SBarry Smith 
268047c6ae99SBarry Smith /*@
268147c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
268247c6ae99SBarry Smith 
2683d083f849SBarry Smith     Neighbor-wise Collective on dm
268447c6ae99SBarry Smith 
268547c6ae99SBarry Smith     Input Parameters:
268647c6ae99SBarry Smith +   dm - the DM object
268747c6ae99SBarry Smith .   g - the global vector
268847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
268947c6ae99SBarry Smith -   l - the local vector
269047c6ae99SBarry Smith 
269101729b5cSPatrick Sanan     Level: intermediate
269247c6ae99SBarry Smith 
269301729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
269447c6ae99SBarry Smith 
269547c6ae99SBarry Smith @*/
26967087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
269747c6ae99SBarry Smith {
26987128ae9fSMatthew G Knepley   PetscSF                 sf;
2699ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2700ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2701ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2702baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2703d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
270447c6ae99SBarry Smith 
270547c6ae99SBarry Smith   PetscFunctionBegin;
2706171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2707*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetSectionSF(dm, &sf));
2708*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMHasBasisTransform(dm, &transform));
27097128ae9fSMatthew G Knepley   if (sf) {
27107a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
27117128ae9fSMatthew G Knepley 
2712*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetArrayAndMemType(l, &lArray, &lmtype));
2713*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
2714*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray,MPI_REPLACE));
2715*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecRestoreArrayAndMemType(l, &lArray));
2716*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecRestoreArrayReadAndMemType(g, &gArray));
2717*5f80ce2aSJacob Faibussowitsch     if (transform) CHKERRQ(DMPlexGlobalToLocalBasis(dm, l));
27187128ae9fSMatthew G Knepley   } else {
27197a8be351SBarry Smith     PetscCheck(dm->ops->globaltolocalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2720*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
27217128ae9fSMatthew G Knepley   }
2722*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL));
2723baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2724*5f80ce2aSJacob Faibussowitsch     if (link->endhook) CHKERRQ((*link->endhook)(dm,g,mode,l,link->ctx));
2725baf369e7SPeter Brune   }
272647c6ae99SBarry Smith   PetscFunctionReturn(0);
272747c6ae99SBarry Smith }
272847c6ae99SBarry Smith 
2729d4d07f1eSToby Isaac /*@C
2730d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2731d4d07f1eSToby Isaac 
2732d4d07f1eSToby Isaac    Logically Collective
2733d4d07f1eSToby Isaac 
27344165533cSJose E. Roman    Input Parameters:
2735d4d07f1eSToby Isaac +  dm - the DM
2736d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2737d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2738d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2739d4d07f1eSToby Isaac 
2740d4d07f1eSToby Isaac    Calling sequence for beginhook:
2741d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2742d4d07f1eSToby Isaac 
2743d4d07f1eSToby Isaac +  dm - global DM
2744d4d07f1eSToby Isaac .  l - local vector
2745d4d07f1eSToby Isaac .  mode - mode
2746d4d07f1eSToby Isaac .  g - global vector
2747d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2748d4d07f1eSToby Isaac 
2749d4d07f1eSToby Isaac    Calling sequence for endhook:
2750d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2751d4d07f1eSToby Isaac 
2752d4d07f1eSToby Isaac +  global - global DM
2753d4d07f1eSToby Isaac .  l - local vector
2754d4d07f1eSToby Isaac .  mode - mode
2755d4d07f1eSToby Isaac .  g - global vector
2756d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2757d4d07f1eSToby Isaac 
2758d4d07f1eSToby Isaac    Level: advanced
2759d4d07f1eSToby Isaac 
2760d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2761d4d07f1eSToby Isaac @*/
2762d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2763d4d07f1eSToby Isaac {
2764d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2765d4d07f1eSToby Isaac 
2766d4d07f1eSToby Isaac   PetscFunctionBegin;
2767d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2768d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2769*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscNew(&link));
2770d4d07f1eSToby Isaac   link->beginhook = beginhook;
2771d4d07f1eSToby Isaac   link->endhook   = endhook;
2772d4d07f1eSToby Isaac   link->ctx       = ctx;
2773d4d07f1eSToby Isaac   link->next      = NULL;
2774d4d07f1eSToby Isaac   *p              = link;
2775d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2776d4d07f1eSToby Isaac }
2777d4d07f1eSToby Isaac 
27784c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
27794c274da1SToby Isaac {
27804c274da1SToby Isaac   Mat cMat;
27814c274da1SToby Isaac   Vec cVec;
27824c274da1SToby Isaac   PetscSection section, cSec;
27834c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
27844c274da1SToby Isaac 
27854c274da1SToby Isaac   PetscFunctionBegin;
27864c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2787*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDefaultConstraints(dm,&cSec,&cMat,NULL));
27884c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
27895db9a05bSToby Isaac     PetscInt nRows;
27905db9a05bSToby Isaac 
2791*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatGetSize(cMat,&nRows,NULL));
27925db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2793*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLocalSection(dm,&section));
2794*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatCreateVecs(cMat,NULL,&cVec));
2795*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetChart(cSec,&pStart,&pEnd));
27964c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
2797*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(cSec,p,&dof));
27984c274da1SToby Isaac       if (dof) {
27994c274da1SToby Isaac         PetscInt d;
28004c274da1SToby Isaac         PetscScalar *vals;
2801*5f80ce2aSJacob Faibussowitsch         CHKERRQ(VecGetValuesSection(l,section,p,&vals));
2802*5f80ce2aSJacob Faibussowitsch         CHKERRQ(VecSetValuesSection(cVec,cSec,p,vals,mode));
28034c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
28044c274da1SToby Isaac          * we just extracted */
28054c274da1SToby Isaac         for (d = 0; d < dof; d++) {
28064c274da1SToby Isaac           vals[d] = 0.;
28074c274da1SToby Isaac         }
28084c274da1SToby Isaac       }
28094c274da1SToby Isaac     }
2810*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatMultTransposeAdd(cMat,cVec,l,l));
2811*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecDestroy(&cVec));
28124c274da1SToby Isaac   }
28134c274da1SToby Isaac   PetscFunctionReturn(0);
28144c274da1SToby Isaac }
281501729b5cSPatrick Sanan /*@
281601729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
281701729b5cSPatrick Sanan 
2818d083f849SBarry Smith     Neighbor-wise Collective on dm
281901729b5cSPatrick Sanan 
282001729b5cSPatrick Sanan     Input Parameters:
282101729b5cSPatrick Sanan +   dm - the DM object
282201729b5cSPatrick Sanan .   l - the local vector
282301729b5cSPatrick 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.
282401729b5cSPatrick Sanan -   g - the global vector
282501729b5cSPatrick Sanan 
282601729b5cSPatrick Sanan     Notes:
282701729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
282801729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
282901729b5cSPatrick Sanan 
283001729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
283101729b5cSPatrick 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.
283201729b5cSPatrick Sanan 
283301729b5cSPatrick Sanan     Level: beginner
283401729b5cSPatrick Sanan 
283501729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
283601729b5cSPatrick Sanan 
283701729b5cSPatrick Sanan @*/
283801729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
283901729b5cSPatrick Sanan {
284001729b5cSPatrick Sanan   PetscFunctionBegin;
2841*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLocalToGlobalBegin(dm,l,mode,g));
2842*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLocalToGlobalEnd(dm,l,mode,g));
284301729b5cSPatrick Sanan   PetscFunctionReturn(0);
284401729b5cSPatrick Sanan }
28454c274da1SToby Isaac 
284647c6ae99SBarry Smith /*@
284701729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
28489a42bb27SBarry Smith 
2849d083f849SBarry Smith     Neighbor-wise Collective on dm
28509a42bb27SBarry Smith 
28519a42bb27SBarry Smith     Input Parameters:
28529a42bb27SBarry Smith +   dm - the DM object
2853f6813fd5SJed Brown .   l - the local vector
28541eb28f2eSBarry 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.
28551eb28f2eSBarry Smith -   g - the global vector
28569a42bb27SBarry Smith 
285795452b02SPatrick Sanan     Notes:
285895452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
285984330215SMatthew 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.
28609a42bb27SBarry Smith 
286101729b5cSPatrick Sanan     Level: intermediate
28629a42bb27SBarry Smith 
286301729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
28649a42bb27SBarry Smith 
28659a42bb27SBarry Smith @*/
28667087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
28679a42bb27SBarry Smith {
28687128ae9fSMatthew G Knepley   PetscSF                 sf;
286984330215SMatthew G. Knepley   PetscSection            s, gs;
2870d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2871ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2872ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2873ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2874fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
2875d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
28769a42bb27SBarry Smith 
28779a42bb27SBarry Smith   PetscFunctionBegin;
2878171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2879d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2880d4d07f1eSToby Isaac     if (link->beginhook) {
2881*5f80ce2aSJacob Faibussowitsch       CHKERRQ((*link->beginhook)(dm,l,mode,g,link->ctx));
2882d4d07f1eSToby Isaac     }
2883d4d07f1eSToby Isaac   }
2884*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL));
2885*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetSectionSF(dm, &sf));
2886*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalSection(dm, &s));
28877128ae9fSMatthew G Knepley   switch (mode) {
28887128ae9fSMatthew G Knepley   case INSERT_VALUES:
28897128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2890304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
289184330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
28927128ae9fSMatthew G Knepley   case ADD_VALUES:
28937128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2894304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
289584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
28967128ae9fSMatthew G Knepley   default:
289798921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
28987128ae9fSMatthew G Knepley   }
2899ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2900*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMHasBasisTransform(dm, &transform));
2901ca3d3a14SMatthew G. Knepley     if (transform) {
2902*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
2903*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecCopy(l, tmpl));
2904*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexLocalToGlobalBasis(dm, tmpl));
2905*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetArrayRead(tmpl, &lArray));
2906fa88e482SJed Brown     } else if (isInsert) {
2907*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetArrayRead(l, &lArray));
2908fa88e482SJed Brown     } else {
2909*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
2910fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2911ca3d3a14SMatthew G. Knepley     }
2912fa88e482SJed Brown     if (s && isInsert) {
2913*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetArray(g, &gArray));
2914fa88e482SJed Brown     } else {
2915*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetArrayAndMemType(g, &gArray, &gmtype));
2916fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2917fa88e482SJed Brown     }
2918ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2919*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
292084330215SMatthew G. Knepley     } else if (s && isInsert) {
292184330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
292284330215SMatthew G. Knepley 
2923*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetGlobalSection(dm, &gs));
2924*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetChart(s, &pStart, &pEnd));
2925*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetOwnershipRange(g, &gStart, NULL));
292684330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2927b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
292884330215SMatthew G. Knepley 
2929*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(s, p, &dof));
2930*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(gs, p, &gdof));
2931*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetConstraintDof(s, p, &cdof));
2932*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetConstraintDof(gs, p, &gcdof));
2933*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(s, p, &off));
2934*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(gs, p, &goff));
2935b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
293603442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
29377a8be351SBarry 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);
2938b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2939b3b16f48SMatthew G. Knepley         if (!gcdof) {
294084330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2941b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2942b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
294384330215SMatthew G. Knepley           const PetscInt *cdofs;
294484330215SMatthew G. Knepley           PetscInt        cind = 0;
294584330215SMatthew G. Knepley 
2946*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetConstraintIndices(s, p, &cdofs));
2947b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
294884330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2949b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
295084330215SMatthew G. Knepley           }
29517a8be351SBarry 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);
295284330215SMatthew G. Knepley       }
2953ca3d3a14SMatthew G. Knepley     }
2954fa88e482SJed Brown     if (g_inplace) {
2955*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecRestoreArrayAndMemType(g, &gArray));
2956fa88e482SJed Brown     } else {
2957*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecRestoreArray(g, &gArray));
2958fa88e482SJed Brown     }
2959ca3d3a14SMatthew G. Knepley     if (transform) {
2960*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecRestoreArrayRead(tmpl, &lArray));
2961*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
2962fa88e482SJed Brown     } else if (l_inplace) {
2963*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecRestoreArrayReadAndMemType(l, &lArray));
2964ca3d3a14SMatthew G. Knepley     } else {
2965*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecRestoreArrayRead(l, &lArray));
2966ca3d3a14SMatthew G. Knepley     }
29677128ae9fSMatthew G Knepley   } else {
29687a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalbegin,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2969*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g));
29707128ae9fSMatthew G Knepley   }
29719a42bb27SBarry Smith   PetscFunctionReturn(0);
29729a42bb27SBarry Smith }
29739a42bb27SBarry Smith 
29749a42bb27SBarry Smith /*@
29759a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
297647c6ae99SBarry Smith 
2977d083f849SBarry Smith     Neighbor-wise Collective on dm
297847c6ae99SBarry Smith 
297947c6ae99SBarry Smith     Input Parameters:
298047c6ae99SBarry Smith +   dm - the DM object
2981f6813fd5SJed Brown .   l - the local vector
298247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2983f6813fd5SJed Brown -   g - the global vector
298447c6ae99SBarry Smith 
298501729b5cSPatrick Sanan     Level: intermediate
298647c6ae99SBarry Smith 
2987e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
298847c6ae99SBarry Smith 
298947c6ae99SBarry Smith @*/
29907087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
299147c6ae99SBarry Smith {
29927128ae9fSMatthew G Knepley   PetscSF                 sf;
299384330215SMatthew G. Knepley   PetscSection            s;
2994d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2995ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
299647c6ae99SBarry Smith 
299747c6ae99SBarry Smith   PetscFunctionBegin;
2998171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2999*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetSectionSF(dm, &sf));
3000*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalSection(dm, &s));
30017128ae9fSMatthew G Knepley   switch (mode) {
30027128ae9fSMatthew G Knepley   case INSERT_VALUES:
30037128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
300484330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
30057128ae9fSMatthew G Knepley   case ADD_VALUES:
30067128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
300784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
30087128ae9fSMatthew G Knepley   default:
300998921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
30107128ae9fSMatthew G Knepley   }
301184330215SMatthew G. Knepley   if (sf && !isInsert) {
3012ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3013ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3014ca3d3a14SMatthew G. Knepley     Vec                tmpl;
301584330215SMatthew G. Knepley 
3016*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMHasBasisTransform(dm, &transform));
3017ca3d3a14SMatthew G. Knepley     if (transform) {
3018*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3019*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetArrayRead(tmpl, &lArray));
3020ca3d3a14SMatthew G. Knepley     } else {
3021*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetArrayReadAndMemType(l, &lArray, NULL));
3022ca3d3a14SMatthew G. Knepley     }
3023*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetArrayAndMemType(g, &gArray, NULL));
3024*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3025ca3d3a14SMatthew G. Knepley     if (transform) {
3026*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecRestoreArrayRead(tmpl, &lArray));
3027*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3028ca3d3a14SMatthew G. Knepley     } else {
3029*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecRestoreArrayReadAndMemType(l, &lArray));
3030ca3d3a14SMatthew G. Knepley     }
3031*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecRestoreArrayAndMemType(g, &gArray));
303284330215SMatthew G. Knepley   } else if (s && isInsert) {
30337128ae9fSMatthew G Knepley   } else {
30347a8be351SBarry Smith     PetscCheck(dm->ops->localtoglobalend,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
3035*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g));
30367128ae9fSMatthew G Knepley   }
3037d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
3038*5f80ce2aSJacob Faibussowitsch     if (link->endhook) CHKERRQ((*link->endhook)(dm,g,mode,l,link->ctx));
3039d4d07f1eSToby Isaac   }
304047c6ae99SBarry Smith   PetscFunctionReturn(0);
304147c6ae99SBarry Smith }
304247c6ae99SBarry Smith 
3043f089877aSRichard Tran Mills /*@
3044bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
3045bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3046d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
3047f089877aSRichard Tran Mills 
3048d083f849SBarry Smith    Neighbor-wise Collective on dm
3049f089877aSRichard Tran Mills 
3050f089877aSRichard Tran Mills    Input Parameters:
3051f089877aSRichard Tran Mills +  dm - the DM object
3052bc0a1609SRichard Tran Mills .  g - the original local vector
3053bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3054f089877aSRichard Tran Mills 
3055bc0a1609SRichard Tran Mills    Output Parameter:
3056bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3057f089877aSRichard Tran Mills 
3058f089877aSRichard Tran Mills    Level: intermediate
3059f089877aSRichard Tran Mills 
3060bc0a1609SRichard Tran Mills    Notes:
3061bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3062bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3063bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3064bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3065bc0a1609SRichard Tran Mills 
3066bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3067f089877aSRichard Tran Mills 
3068f089877aSRichard Tran Mills @*/
3069f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
3070f089877aSRichard Tran Mills {
3071f089877aSRichard Tran Mills   PetscFunctionBegin;
3072f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30737a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalbegin,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3074*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
3075f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3076f089877aSRichard Tran Mills }
3077f089877aSRichard Tran Mills 
3078f089877aSRichard Tran Mills /*@
3079bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
3080bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3081d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
3082f089877aSRichard Tran Mills 
3083d083f849SBarry Smith    Neighbor-wise Collective on dm
3084f089877aSRichard Tran Mills 
3085f089877aSRichard Tran Mills    Input Parameters:
3086bc0a1609SRichard Tran Mills +  da - the DM object
3087bc0a1609SRichard Tran Mills .  g - the original local vector
3088bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
3089f089877aSRichard Tran Mills 
3090bc0a1609SRichard Tran Mills    Output Parameter:
3091bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3092f089877aSRichard Tran Mills 
3093f089877aSRichard Tran Mills    Level: intermediate
3094f089877aSRichard Tran Mills 
3095bc0a1609SRichard Tran Mills    Notes:
3096bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
3097bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
3098bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
3099bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
3100bc0a1609SRichard Tran Mills 
3101bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
3102f089877aSRichard Tran Mills 
3103f089877aSRichard Tran Mills @*/
3104f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
3105f089877aSRichard Tran Mills {
3106f089877aSRichard Tran Mills   PetscFunctionBegin;
3107f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31087a8be351SBarry Smith   PetscCheck(dm->ops->localtolocalend,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
3109*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l));
3110f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3111f089877aSRichard Tran Mills }
3112f089877aSRichard Tran Mills 
311347c6ae99SBarry Smith /*@
311447c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
311547c6ae99SBarry Smith 
3116d083f849SBarry Smith     Collective on dm
311747c6ae99SBarry Smith 
3118d8d19677SJose E. Roman     Input Parameters:
311947c6ae99SBarry Smith +   dm - the DM object
312091d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
312147c6ae99SBarry Smith 
312247c6ae99SBarry Smith     Output Parameter:
312347c6ae99SBarry Smith .   dmc - the coarsened DM
312447c6ae99SBarry Smith 
312547c6ae99SBarry Smith     Level: developer
312647c6ae99SBarry Smith 
3127e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
312847c6ae99SBarry Smith 
312947c6ae99SBarry Smith @*/
31307087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
313147c6ae99SBarry Smith {
3132b17ce1afSJed Brown   DMCoarsenHookLink link;
313347c6ae99SBarry Smith 
313447c6ae99SBarry Smith   PetscFunctionBegin;
3135171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31367a8be351SBarry Smith   PetscCheck(dm->ops->coarsen,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
3137*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(DM_Coarsen,dm,0,0,0));
3138*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->coarsen)(dm, comm, dmc));
3139b9d85ea2SLisandro Dalcin   if (*dmc) {
3140a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
3141*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetCoarseDM(dm,*dmc));
314243842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
3143*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc));
3144644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
31450598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
3146656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
3147*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetMatType(*dmc,dm->mattype));
3148b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
3149*5f80ce2aSJacob Faibussowitsch       if (link->coarsenhook) CHKERRQ((*link->coarsenhook)(dm,*dmc,link->ctx));
3150b17ce1afSJed Brown     }
3151b9d85ea2SLisandro Dalcin   }
3152*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(DM_Coarsen,dm,0,0,0));
31537a8be351SBarry Smith   PetscCheck(*dmc,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3154b17ce1afSJed Brown   PetscFunctionReturn(0);
3155b17ce1afSJed Brown }
3156b17ce1afSJed Brown 
3157bb9467b5SJed Brown /*@C
3158b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3159b17ce1afSJed Brown 
3160b17ce1afSJed Brown    Logically Collective
3161b17ce1afSJed Brown 
31624165533cSJose E. Roman    Input Parameters:
3163b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3164b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3165b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
31660298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3167b17ce1afSJed Brown 
3168b17ce1afSJed Brown    Calling sequence of coarsenhook:
3169b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3170b17ce1afSJed Brown 
3171b17ce1afSJed Brown +  fine - fine level DM
3172b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3173b17ce1afSJed Brown -  ctx - optional user-defined function context
3174b17ce1afSJed Brown 
3175b17ce1afSJed Brown    Calling sequence for restricthook:
3176c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3177b17ce1afSJed Brown 
3178b17ce1afSJed Brown +  fine - fine level DM
3179b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3180c833c3b5SJed Brown .  rscale - scaling vector for restriction
3181c833c3b5SJed Brown .  inject - matrix restricting by injection
3182b17ce1afSJed Brown .  coarse - coarse level DM to update
3183b17ce1afSJed Brown -  ctx - optional user-defined function context
3184b17ce1afSJed Brown 
3185b17ce1afSJed Brown    Level: advanced
3186b17ce1afSJed Brown 
3187b17ce1afSJed Brown    Notes:
3188b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3189b17ce1afSJed Brown 
3190b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3191b17ce1afSJed Brown 
3192b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3193b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3194b17ce1afSJed Brown 
3195bb9467b5SJed Brown    This function is currently not available from Fortran.
3196bb9467b5SJed Brown 
3197dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3198b17ce1afSJed Brown @*/
3199b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3200b17ce1afSJed Brown {
3201b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3202b17ce1afSJed Brown 
3203b17ce1afSJed Brown   PetscFunctionBegin;
3204b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
32051e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
32061e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
32071e3d8eccSJed Brown   }
3208*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscNew(&link));
3209b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3210b17ce1afSJed Brown   link->restricthook = restricthook;
3211b17ce1afSJed Brown   link->ctx          = ctx;
32120298fd71SBarry Smith   link->next         = NULL;
3213b17ce1afSJed Brown   *p                 = link;
3214b17ce1afSJed Brown   PetscFunctionReturn(0);
3215b17ce1afSJed Brown }
3216b17ce1afSJed Brown 
3217dc822a44SJed Brown /*@C
3218dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3219dc822a44SJed Brown 
3220dc822a44SJed Brown    Logically Collective
3221dc822a44SJed Brown 
32224165533cSJose E. Roman    Input Parameters:
3223dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3224dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3225dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3226dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3227dc822a44SJed Brown 
3228dc822a44SJed Brown    Level: advanced
3229dc822a44SJed Brown 
3230dc822a44SJed Brown    Notes:
3231dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3232dc822a44SJed Brown 
3233dc822a44SJed Brown    This function is currently not available from Fortran.
3234dc822a44SJed Brown 
3235dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3236dc822a44SJed Brown @*/
3237dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3238dc822a44SJed Brown {
3239dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3240dc822a44SJed Brown 
3241dc822a44SJed Brown   PetscFunctionBegin;
3242dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3243dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3244dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3245dc822a44SJed Brown       link = *p;
3246dc822a44SJed Brown       *p = link->next;
3247*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(link));
3248dc822a44SJed Brown       break;
3249dc822a44SJed Brown     }
3250dc822a44SJed Brown   }
3251dc822a44SJed Brown   PetscFunctionReturn(0);
3252dc822a44SJed Brown }
3253dc822a44SJed Brown 
3254b17ce1afSJed Brown /*@
3255b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3256b17ce1afSJed Brown 
3257b17ce1afSJed Brown    Collective if any hooks are
3258b17ce1afSJed Brown 
32594165533cSJose E. Roman    Input Parameters:
3260b17ce1afSJed Brown +  fine - finer DM to use as a base
3261b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3262e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3263b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3264e91eccc2SStefano Zampini -  coarse - coarser DM to update
3265b17ce1afSJed Brown 
3266b17ce1afSJed Brown    Level: developer
3267b17ce1afSJed Brown 
3268b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3269b17ce1afSJed Brown @*/
3270b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3271b17ce1afSJed Brown {
3272b17ce1afSJed Brown   DMCoarsenHookLink link;
3273b17ce1afSJed Brown 
3274b17ce1afSJed Brown   PetscFunctionBegin;
3275b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
32768865f1eaSKarl Rupp     if (link->restricthook) {
3277*5f80ce2aSJacob Faibussowitsch       CHKERRQ((*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx));
32788865f1eaSKarl Rupp     }
3279b17ce1afSJed Brown   }
328047c6ae99SBarry Smith   PetscFunctionReturn(0);
328147c6ae99SBarry Smith }
328247c6ae99SBarry Smith 
3283bb9467b5SJed Brown /*@C
3284be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
32855dbd56e3SPeter Brune 
3286d083f849SBarry Smith    Logically Collective on global
32875dbd56e3SPeter Brune 
32884165533cSJose E. Roman    Input Parameters:
32895dbd56e3SPeter Brune +  global - global DM
3290ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
32915dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
32920298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
32935dbd56e3SPeter Brune 
3294ec4806b8SPeter Brune    Calling sequence for ddhook:
3295ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3296ec4806b8SPeter Brune 
3297ec4806b8SPeter Brune +  global - global DM
3298ec4806b8SPeter Brune .  block  - block DM
3299ec4806b8SPeter Brune -  ctx - optional user-defined function context
3300ec4806b8SPeter Brune 
33015dbd56e3SPeter Brune    Calling sequence for restricthook:
3302ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
33035dbd56e3SPeter Brune 
33045dbd56e3SPeter Brune +  global - global DM
33055dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
33065dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3307ec4806b8SPeter Brune .  block  - block DM
33085dbd56e3SPeter Brune -  ctx - optional user-defined function context
33095dbd56e3SPeter Brune 
33105dbd56e3SPeter Brune    Level: advanced
33115dbd56e3SPeter Brune 
33125dbd56e3SPeter Brune    Notes:
3313ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
33145dbd56e3SPeter Brune 
33155dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
33165dbd56e3SPeter Brune 
33175dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3318ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
33195dbd56e3SPeter Brune 
3320bb9467b5SJed Brown    This function is currently not available from Fortran.
3321bb9467b5SJed Brown 
33225dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
33235dbd56e3SPeter Brune @*/
3324be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
33255dbd56e3SPeter Brune {
3326be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
33275dbd56e3SPeter Brune 
33285dbd56e3SPeter Brune   PetscFunctionBegin;
33295dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3330b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3331b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3332b3a6b972SJed Brown   }
3333*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscNew(&link));
33345dbd56e3SPeter Brune   link->restricthook = restricthook;
3335be081cd6SPeter Brune   link->ddhook       = ddhook;
33365dbd56e3SPeter Brune   link->ctx          = ctx;
33370298fd71SBarry Smith   link->next         = NULL;
33385dbd56e3SPeter Brune   *p                 = link;
33395dbd56e3SPeter Brune   PetscFunctionReturn(0);
33405dbd56e3SPeter Brune }
33415dbd56e3SPeter Brune 
3342b3a6b972SJed Brown /*@C
3343b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3344b3a6b972SJed Brown 
3345b3a6b972SJed Brown    Logically Collective
3346b3a6b972SJed Brown 
33474165533cSJose E. Roman    Input Parameters:
3348b3a6b972SJed Brown +  global - global DM
3349b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3350b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3351b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3352b3a6b972SJed Brown 
3353b3a6b972SJed Brown    Level: advanced
3354b3a6b972SJed Brown 
3355b3a6b972SJed Brown    Notes:
3356b3a6b972SJed Brown 
3357b3a6b972SJed Brown    This function is currently not available from Fortran.
3358b3a6b972SJed Brown 
3359b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3360b3a6b972SJed Brown @*/
3361b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3362b3a6b972SJed Brown {
3363b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3364b3a6b972SJed Brown 
3365b3a6b972SJed Brown   PetscFunctionBegin;
3366b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3367b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3368b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3369b3a6b972SJed Brown       link = *p;
3370b3a6b972SJed Brown       *p = link->next;
3371*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(link));
3372b3a6b972SJed Brown       break;
3373b3a6b972SJed Brown     }
3374b3a6b972SJed Brown   }
3375b3a6b972SJed Brown   PetscFunctionReturn(0);
3376b3a6b972SJed Brown }
3377b3a6b972SJed Brown 
33785dbd56e3SPeter Brune /*@
3379be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
33805dbd56e3SPeter Brune 
33815dbd56e3SPeter Brune    Collective if any hooks are
33825dbd56e3SPeter Brune 
33834165533cSJose E. Roman    Input Parameters:
33845dbd56e3SPeter Brune +  fine - finer DM to use as a base
3385be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3386be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
33875dbd56e3SPeter Brune -  coarse - coarer DM to update
33885dbd56e3SPeter Brune 
33895dbd56e3SPeter Brune    Level: developer
33905dbd56e3SPeter Brune 
33915dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
33925dbd56e3SPeter Brune @*/
3393be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
33945dbd56e3SPeter Brune {
3395be081cd6SPeter Brune   DMSubDomainHookLink link;
33965dbd56e3SPeter Brune 
33975dbd56e3SPeter Brune   PetscFunctionBegin;
3398be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
33998865f1eaSKarl Rupp     if (link->restricthook) {
3400*5f80ce2aSJacob Faibussowitsch       CHKERRQ((*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx));
34018865f1eaSKarl Rupp     }
34025dbd56e3SPeter Brune   }
34035dbd56e3SPeter Brune   PetscFunctionReturn(0);
34045dbd56e3SPeter Brune }
34055dbd56e3SPeter Brune 
34065fe1f584SPeter Brune /*@
34076a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
34085fe1f584SPeter Brune 
34095fe1f584SPeter Brune     Not Collective
34105fe1f584SPeter Brune 
34115fe1f584SPeter Brune     Input Parameter:
34125fe1f584SPeter Brune .   dm - the DM object
34135fe1f584SPeter Brune 
34145fe1f584SPeter Brune     Output Parameter:
34156a7d9d85SPeter Brune .   level - number of coarsenings
34165fe1f584SPeter Brune 
34175fe1f584SPeter Brune     Level: developer
34185fe1f584SPeter Brune 
34196a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
34205fe1f584SPeter Brune 
34215fe1f584SPeter Brune @*/
34225fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
34235fe1f584SPeter Brune {
34245fe1f584SPeter Brune   PetscFunctionBegin;
34255fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3426b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
34275fe1f584SPeter Brune   *level = dm->leveldown;
34285fe1f584SPeter Brune   PetscFunctionReturn(0);
34295fe1f584SPeter Brune }
34305fe1f584SPeter Brune 
34319a64c4a8SMatthew G. Knepley /*@
34329a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
34339a64c4a8SMatthew G. Knepley 
34349a64c4a8SMatthew G. Knepley     Not Collective
34359a64c4a8SMatthew G. Knepley 
34369a64c4a8SMatthew G. Knepley     Input Parameters:
34379a64c4a8SMatthew G. Knepley +   dm - the DM object
34389a64c4a8SMatthew G. Knepley -   level - number of coarsenings
34399a64c4a8SMatthew G. Knepley 
34409a64c4a8SMatthew G. Knepley     Level: developer
34419a64c4a8SMatthew G. Knepley 
34429a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
34439a64c4a8SMatthew G. Knepley @*/
34449a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
34459a64c4a8SMatthew G. Knepley {
34469a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
34479a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34489a64c4a8SMatthew G. Knepley   dm->leveldown = level;
34499a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
34509a64c4a8SMatthew G. Knepley }
34519a64c4a8SMatthew G. Knepley 
345247c6ae99SBarry Smith /*@C
345347c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
345447c6ae99SBarry Smith 
3455d083f849SBarry Smith     Collective on dm
345647c6ae99SBarry Smith 
3457d8d19677SJose E. Roman     Input Parameters:
345847c6ae99SBarry Smith +   dm - the DM object
345947c6ae99SBarry Smith -   nlevels - the number of levels of refinement
346047c6ae99SBarry Smith 
346147c6ae99SBarry Smith     Output Parameter:
346247c6ae99SBarry Smith .   dmf - the refined DM hierarchy
346347c6ae99SBarry Smith 
346447c6ae99SBarry Smith     Level: developer
346547c6ae99SBarry Smith 
3466e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
346747c6ae99SBarry Smith 
346847c6ae99SBarry Smith @*/
34697087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
347047c6ae99SBarry Smith {
347147c6ae99SBarry Smith   PetscFunctionBegin;
3472171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34737a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
347447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3475b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
347647c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
3477*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->refinehierarchy)(dm,nlevels,dmf));
347847c6ae99SBarry Smith   } else if (dm->ops->refine) {
347947c6ae99SBarry Smith     PetscInt i;
348047c6ae99SBarry Smith 
3481*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]));
348247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3483*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]));
348447c6ae99SBarry Smith     }
3485ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
348647c6ae99SBarry Smith   PetscFunctionReturn(0);
348747c6ae99SBarry Smith }
348847c6ae99SBarry Smith 
348947c6ae99SBarry Smith /*@C
349047c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
349147c6ae99SBarry Smith 
3492d083f849SBarry Smith     Collective on dm
349347c6ae99SBarry Smith 
3494d8d19677SJose E. Roman     Input Parameters:
349547c6ae99SBarry Smith +   dm - the DM object
349647c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
349747c6ae99SBarry Smith 
349847c6ae99SBarry Smith     Output Parameter:
349947c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
350047c6ae99SBarry Smith 
350147c6ae99SBarry Smith     Level: developer
350247c6ae99SBarry Smith 
3503e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
350447c6ae99SBarry Smith 
350547c6ae99SBarry Smith @*/
35067087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
350747c6ae99SBarry Smith {
350847c6ae99SBarry Smith   PetscFunctionBegin;
3509171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35107a8be351SBarry Smith   PetscCheck(nlevels >= 0,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
351147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
351247c6ae99SBarry Smith   PetscValidPointer(dmc,3);
351347c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
3514*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->coarsenhierarchy)(dm, nlevels, dmc));
351547c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
351647c6ae99SBarry Smith     PetscInt i;
351747c6ae99SBarry Smith 
3518*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]));
351947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3520*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]));
352147c6ae99SBarry Smith     }
3522ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
352347c6ae99SBarry Smith   PetscFunctionReturn(0);
352447c6ae99SBarry Smith }
352547c6ae99SBarry Smith 
35261a266240SBarry Smith /*@C
35271a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
35281a266240SBarry Smith 
35291a266240SBarry Smith     Not Collective
35301a266240SBarry Smith 
35311a266240SBarry Smith     Input Parameters:
35321a266240SBarry Smith +   dm - the DM object
35331a266240SBarry Smith -   destroy - the destroy function
35341a266240SBarry Smith 
35351a266240SBarry Smith     Level: intermediate
35361a266240SBarry Smith 
3537e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
35381a266240SBarry Smith 
3539f07f9ceaSJed Brown @*/
35401a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
35411a266240SBarry Smith {
35421a266240SBarry Smith   PetscFunctionBegin;
3543171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35441a266240SBarry Smith   dm->ctxdestroy = destroy;
35451a266240SBarry Smith   PetscFunctionReturn(0);
35461a266240SBarry Smith }
35471a266240SBarry Smith 
3548b07ff414SBarry Smith /*@
35491b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
355047c6ae99SBarry Smith 
355147c6ae99SBarry Smith     Not Collective
355247c6ae99SBarry Smith 
355347c6ae99SBarry Smith     Input Parameters:
355447c6ae99SBarry Smith +   dm - the DM object
355547c6ae99SBarry Smith -   ctx - the user context
355647c6ae99SBarry Smith 
355747c6ae99SBarry Smith     Level: intermediate
355847c6ae99SBarry Smith 
3559e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
356047c6ae99SBarry Smith 
356147c6ae99SBarry Smith @*/
35621b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
356347c6ae99SBarry Smith {
356447c6ae99SBarry Smith   PetscFunctionBegin;
3565171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
356647c6ae99SBarry Smith   dm->ctx = ctx;
356747c6ae99SBarry Smith   PetscFunctionReturn(0);
356847c6ae99SBarry Smith }
356947c6ae99SBarry Smith 
357047c6ae99SBarry Smith /*@
35711b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
357247c6ae99SBarry Smith 
357347c6ae99SBarry Smith     Not Collective
357447c6ae99SBarry Smith 
357547c6ae99SBarry Smith     Input Parameter:
357647c6ae99SBarry Smith .   dm - the DM object
357747c6ae99SBarry Smith 
357847c6ae99SBarry Smith     Output Parameter:
357947c6ae99SBarry Smith .   ctx - the user context
358047c6ae99SBarry Smith 
358147c6ae99SBarry Smith     Level: intermediate
358247c6ae99SBarry Smith 
3583e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
358447c6ae99SBarry Smith 
358547c6ae99SBarry Smith @*/
35861b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
358747c6ae99SBarry Smith {
358847c6ae99SBarry Smith   PetscFunctionBegin;
3589171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35901b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
359147c6ae99SBarry Smith   PetscFunctionReturn(0);
359247c6ae99SBarry Smith }
359347c6ae99SBarry Smith 
359408da532bSDmitry Karpeev /*@C
3595df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
359608da532bSDmitry Karpeev 
3597d083f849SBarry Smith     Logically Collective on dm
359808da532bSDmitry Karpeev 
3599d8d19677SJose E. Roman     Input Parameters:
360008da532bSDmitry Karpeev +   dm - the DM object
36010298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
360208da532bSDmitry Karpeev 
360308da532bSDmitry Karpeev     Level: intermediate
360408da532bSDmitry Karpeev 
3605835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
360608da532bSDmitry Karpeev          DMSetJacobian()
360708da532bSDmitry Karpeev 
360808da532bSDmitry Karpeev @*/
360908da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
361008da532bSDmitry Karpeev {
361108da532bSDmitry Karpeev   PetscFunctionBegin;
36125a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
361308da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
361408da532bSDmitry Karpeev   PetscFunctionReturn(0);
361508da532bSDmitry Karpeev }
361608da532bSDmitry Karpeev 
361708da532bSDmitry Karpeev /*@
361808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
361908da532bSDmitry Karpeev 
362008da532bSDmitry Karpeev     Not Collective
362108da532bSDmitry Karpeev 
362208da532bSDmitry Karpeev     Input Parameter:
362308da532bSDmitry Karpeev .   dm - the DM object to destroy
362408da532bSDmitry Karpeev 
362508da532bSDmitry Karpeev     Output Parameter:
362608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
362708da532bSDmitry Karpeev 
362808da532bSDmitry Karpeev     Level: developer
362908da532bSDmitry Karpeev 
363074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
363108da532bSDmitry Karpeev 
363208da532bSDmitry Karpeev @*/
363308da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
363408da532bSDmitry Karpeev {
363508da532bSDmitry Karpeev   PetscFunctionBegin;
36365a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3637534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
363808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
363908da532bSDmitry Karpeev   PetscFunctionReturn(0);
364008da532bSDmitry Karpeev }
364108da532bSDmitry Karpeev 
364208da532bSDmitry Karpeev /*@C
364308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
364408da532bSDmitry Karpeev 
3645d083f849SBarry Smith     Logically Collective on dm
364608da532bSDmitry Karpeev 
3647f899ff85SJose E. Roman     Input Parameter:
3648907376e6SBarry Smith .   dm - the DM object
364908da532bSDmitry Karpeev 
365008da532bSDmitry Karpeev     Output parameters:
365108da532bSDmitry Karpeev +   xl - lower bound
365208da532bSDmitry Karpeev -   xu - upper bound
365308da532bSDmitry Karpeev 
3654907376e6SBarry Smith     Level: advanced
3655907376e6SBarry Smith 
365695452b02SPatrick Sanan     Notes:
365795452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
365808da532bSDmitry Karpeev 
365974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
366008da532bSDmitry Karpeev 
366108da532bSDmitry Karpeev @*/
366208da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
366308da532bSDmitry Karpeev {
366408da532bSDmitry Karpeev   PetscFunctionBegin;
36655a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
366608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
36675a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
36687a8be351SBarry Smith   PetscCheck(dm->ops->computevariablebounds,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
3669*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->computevariablebounds)(dm, xl,xu));
367008da532bSDmitry Karpeev   PetscFunctionReturn(0);
367108da532bSDmitry Karpeev }
367208da532bSDmitry Karpeev 
3673b0ae01b7SPeter Brune /*@
3674b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3675b0ae01b7SPeter Brune 
3676b0ae01b7SPeter Brune     Not Collective
3677b0ae01b7SPeter Brune 
3678b0ae01b7SPeter Brune     Input Parameter:
3679b0ae01b7SPeter Brune .   dm - the DM object
3680b0ae01b7SPeter Brune 
3681b0ae01b7SPeter Brune     Output Parameter:
3682b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3683b0ae01b7SPeter Brune 
3684b0ae01b7SPeter Brune     Level: developer
3685b0ae01b7SPeter Brune 
36861565f0a7SPatrick Sanan .seealso DMCreateColoring()
3687b0ae01b7SPeter Brune 
3688b0ae01b7SPeter Brune @*/
3689b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3690b0ae01b7SPeter Brune {
3691b0ae01b7SPeter Brune   PetscFunctionBegin;
36925a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3693534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3694b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3695b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3696b0ae01b7SPeter Brune }
3697b0ae01b7SPeter Brune 
36983ad4599aSBarry Smith /*@
36993ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
37003ad4599aSBarry Smith 
37013ad4599aSBarry Smith     Not Collective
37023ad4599aSBarry Smith 
37033ad4599aSBarry Smith     Input Parameter:
37043ad4599aSBarry Smith .   dm - the DM object
37053ad4599aSBarry Smith 
37063ad4599aSBarry Smith     Output Parameter:
37073ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
37083ad4599aSBarry Smith 
37093ad4599aSBarry Smith     Level: developer
37103ad4599aSBarry Smith 
37111565f0a7SPatrick Sanan .seealso DMCreateRestriction()
37123ad4599aSBarry Smith 
37133ad4599aSBarry Smith @*/
37143ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
37153ad4599aSBarry Smith {
37163ad4599aSBarry Smith   PetscFunctionBegin;
37175a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3718534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37193ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
37203ad4599aSBarry Smith   PetscFunctionReturn(0);
37213ad4599aSBarry Smith }
37223ad4599aSBarry Smith 
3723a7058e45SLawrence Mitchell /*@
3724a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3725a7058e45SLawrence Mitchell 
3726a7058e45SLawrence Mitchell     Not Collective
3727a7058e45SLawrence Mitchell 
3728a7058e45SLawrence Mitchell     Input Parameter:
3729a7058e45SLawrence Mitchell .   dm - the DM object
3730a7058e45SLawrence Mitchell 
3731a7058e45SLawrence Mitchell     Output Parameter:
3732a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3733a7058e45SLawrence Mitchell 
3734a7058e45SLawrence Mitchell     Level: developer
3735a7058e45SLawrence Mitchell 
37361565f0a7SPatrick Sanan .seealso DMCreateInjection()
3737a7058e45SLawrence Mitchell 
3738a7058e45SLawrence Mitchell @*/
3739a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3740a7058e45SLawrence Mitchell {
3741a7058e45SLawrence Mitchell   PetscFunctionBegin;
37425a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3743534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
37445a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
3745*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->hascreateinjection)(dm,flg));
37465a84ad33SLisandro Dalcin   } else {
37475a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
37485a84ad33SLisandro Dalcin   }
3749a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3750a7058e45SLawrence Mitchell }
3751a7058e45SLawrence Mitchell 
37520298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3753264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3754264ace61SBarry Smith 
3755264ace61SBarry Smith /*@C
3756264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3757264ace61SBarry Smith 
3758d083f849SBarry Smith   Collective on dm
3759264ace61SBarry Smith 
3760264ace61SBarry Smith   Input Parameters:
3761264ace61SBarry Smith + dm     - The DM object
3762264ace61SBarry Smith - method - The name of the DM type
3763264ace61SBarry Smith 
3764264ace61SBarry Smith   Options Database Key:
3765264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3766264ace61SBarry Smith 
3767264ace61SBarry Smith   Notes:
3768e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3769264ace61SBarry Smith 
3770264ace61SBarry Smith   Level: intermediate
3771264ace61SBarry Smith 
3772264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3773264ace61SBarry Smith @*/
377419fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3775264ace61SBarry Smith {
3776264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3777264ace61SBarry Smith   PetscBool      match;
3778264ace61SBarry Smith 
3779264ace61SBarry Smith   PetscFunctionBegin;
3780264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3781*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject) dm, method, &match));
3782264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3783264ace61SBarry Smith 
3784*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMRegisterAll());
3785*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFunctionListFind(DMList,method,&r));
37867a8be351SBarry Smith   PetscCheck(r,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3787264ace61SBarry Smith 
3788264ace61SBarry Smith   if (dm->ops->destroy) {
3789*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->destroy)(dm));
3790264ace61SBarry Smith   }
3791*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMemzero(dm->ops,sizeof(*dm->ops)));
3792*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectChangeTypeName((PetscObject)dm,method));
3793*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*r)(dm));
3794264ace61SBarry Smith   PetscFunctionReturn(0);
3795264ace61SBarry Smith }
3796264ace61SBarry Smith 
3797264ace61SBarry Smith /*@C
3798264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3799264ace61SBarry Smith 
3800264ace61SBarry Smith   Not Collective
3801264ace61SBarry Smith 
3802264ace61SBarry Smith   Input Parameter:
3803264ace61SBarry Smith . dm  - The DM
3804264ace61SBarry Smith 
3805264ace61SBarry Smith   Output Parameter:
3806264ace61SBarry Smith . type - The DM type name
3807264ace61SBarry Smith 
3808264ace61SBarry Smith   Level: intermediate
3809264ace61SBarry Smith 
3810264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3811264ace61SBarry Smith @*/
381219fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3813264ace61SBarry Smith {
3814264ace61SBarry Smith   PetscFunctionBegin;
3815264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3816c959eef4SJed Brown   PetscValidPointer(type,2);
3817*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMRegisterAll());
3818264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3819264ace61SBarry Smith   PetscFunctionReturn(0);
3820264ace61SBarry Smith }
3821264ace61SBarry Smith 
382267a56275SMatthew G Knepley /*@C
382367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
382467a56275SMatthew G Knepley 
3825d083f849SBarry Smith   Collective on dm
382667a56275SMatthew G Knepley 
382767a56275SMatthew G Knepley   Input Parameters:
382867a56275SMatthew G Knepley + dm - the DM
382967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
383067a56275SMatthew G Knepley 
383167a56275SMatthew G Knepley   Output Parameter:
383267a56275SMatthew G Knepley . M - pointer to new DM
383367a56275SMatthew G Knepley 
383467a56275SMatthew G Knepley   Notes:
383567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
383667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
383767a56275SMatthew G Knepley   of the input DM.
383867a56275SMatthew G Knepley 
383967a56275SMatthew G Knepley   Level: intermediate
384067a56275SMatthew G Knepley 
384167a56275SMatthew G Knepley .seealso: DMCreate()
384267a56275SMatthew G Knepley @*/
384319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
384467a56275SMatthew G Knepley {
384567a56275SMatthew G Knepley   DM             B;
384667a56275SMatthew G Knepley   char           convname[256];
3847c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
384867a56275SMatthew G Knepley 
384967a56275SMatthew G Knepley   PetscFunctionBegin;
385067a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
385167a56275SMatthew G Knepley   PetscValidType(dm,1);
385267a56275SMatthew G Knepley   PetscValidPointer(M,3);
3853*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype));
3854*5f80ce2aSJacob Faibussowitsch   /* CHKERRQ(PetscStrcmp(newtype, "same", &issame)); */
3855c067b6caSMatthew G. Knepley   if (sametype) {
3856c067b6caSMatthew G. Knepley     *M   = dm;
3857*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectReference((PetscObject) dm));
3858c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3859c067b6caSMatthew G. Knepley   } else {
38600298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
386167a56275SMatthew G Knepley 
386267a56275SMatthew G Knepley     /*
386367a56275SMatthew G Knepley        Order of precedence:
386467a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
386567a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
386667a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
386767a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
386867a56275SMatthew G Knepley        5) Use a really basic converter.
386967a56275SMatthew G Knepley     */
387067a56275SMatthew G Knepley 
387167a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3872*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrncpy(convname,"DMConvert_",sizeof(convname)));
3873*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname)));
3874*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrlcat(convname,"_",sizeof(convname)));
3875*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrlcat(convname,newtype,sizeof(convname)));
3876*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrlcat(convname,"_C",sizeof(convname)));
3877*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectQueryFunction((PetscObject)dm,convname,&conv));
387867a56275SMatthew G Knepley     if (conv) goto foundconv;
387967a56275SMatthew G Knepley 
388067a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
3881*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreate(PetscObjectComm((PetscObject)dm), &B));
3882*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetType(B, newtype));
3883*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrncpy(convname,"DMConvert_",sizeof(convname)));
3884*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname)));
3885*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrlcat(convname,"_",sizeof(convname)));
3886*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrlcat(convname,newtype,sizeof(convname)));
3887*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrlcat(convname,"_C",sizeof(convname)));
3888*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectQueryFunction((PetscObject)B,convname,&conv));
388967a56275SMatthew G Knepley     if (conv) {
3890*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMDestroy(&B));
389167a56275SMatthew G Knepley       goto foundconv;
389267a56275SMatthew G Knepley     }
389367a56275SMatthew G Knepley 
389467a56275SMatthew G Knepley #if 0
389567a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
389667a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3897*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMDestroy(&B));
389867a56275SMatthew G Knepley     if (conv) goto foundconv;
389967a56275SMatthew G Knepley 
390067a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
390167a56275SMatthew G Knepley     if (dm->ops->convert) {
390267a56275SMatthew G Knepley       conv = dm->ops->convert;
390367a56275SMatthew G Knepley     }
390467a56275SMatthew G Knepley     if (conv) goto foundconv;
390567a56275SMatthew G Knepley #endif
390667a56275SMatthew G Knepley 
390767a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
390898921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
390967a56275SMatthew G Knepley 
391067a56275SMatthew G Knepley foundconv:
3911*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscLogEventBegin(DM_Convert,dm,0,0,0));
3912*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*conv)(dm,newtype,M));
391312fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
391490b157c4SStefano Zampini     {
391590b157c4SStefano Zampini       PetscBool             isper;
391612fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
391712fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
3918*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd));
3919*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMSetPeriodicity(*M, isper, maxCell,  L,  bd));
3920c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
3921*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree((*M)->vectype));
3922*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscStrallocpy(dm->vectype,(char**)&(*M)->vectype));
3923*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree((*M)->mattype));
3924*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscStrallocpy(dm->mattype,(char**)&(*M)->mattype));
392512fa691eSMatthew G. Knepley     }
3926*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscLogEventEnd(DM_Convert,dm,0,0,0));
392767a56275SMatthew G Knepley   }
3928*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectStateIncrease((PetscObject) *M));
392967a56275SMatthew G Knepley   PetscFunctionReturn(0);
393067a56275SMatthew G Knepley }
3931264ace61SBarry Smith 
3932264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3933264ace61SBarry Smith 
3934264ace61SBarry Smith /*@C
39351c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
39361c84c290SBarry Smith 
39371c84c290SBarry Smith   Not Collective
39381c84c290SBarry Smith 
39391c84c290SBarry Smith   Input Parameters:
39401c84c290SBarry Smith + name        - The name of a new user-defined creation routine
39411c84c290SBarry Smith - create_func - The creation routine itself
39421c84c290SBarry Smith 
39431c84c290SBarry Smith   Notes:
39441c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
39451c84c290SBarry Smith 
39461c84c290SBarry Smith   Sample usage:
39471c84c290SBarry Smith .vb
3948bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
39491c84c290SBarry Smith .ve
39501c84c290SBarry Smith 
39511c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
39521c84c290SBarry Smith .vb
39531c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
39541c84c290SBarry Smith     DMSetType(DM,"my_da");
39551c84c290SBarry Smith .ve
39561c84c290SBarry Smith    or at runtime via the option
39571c84c290SBarry Smith .vb
39581c84c290SBarry Smith     -da_type my_da
39591c84c290SBarry Smith .ve
3960264ace61SBarry Smith 
3961264ace61SBarry Smith   Level: advanced
39621c84c290SBarry Smith 
3963bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
39641c84c290SBarry Smith 
3965264ace61SBarry Smith @*/
3966bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3967264ace61SBarry Smith {
3968264ace61SBarry Smith   PetscFunctionBegin;
3969*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMInitializePackage());
3970*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFunctionListAdd(&DMList,sname,function));
3971264ace61SBarry Smith   PetscFunctionReturn(0);
3972264ace61SBarry Smith }
3973264ace61SBarry Smith 
3974b859378eSBarry Smith /*@C
397555849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3976b859378eSBarry Smith 
3977d083f849SBarry Smith   Collective on viewer
3978b859378eSBarry Smith 
3979b859378eSBarry Smith   Input Parameters:
3980b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3981b859378eSBarry Smith            some related function before a call to DMLoad().
3982b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3983b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3984b859378eSBarry Smith 
3985b859378eSBarry Smith    Level: intermediate
3986b859378eSBarry Smith 
3987b859378eSBarry Smith   Notes:
398855849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
3989b859378eSBarry Smith 
3990cd7e8a5eSksagiyam   Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
3991cd7e8a5eSksagiyam   meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
3992cd7e8a5eSksagiyam   before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
3993cd7e8a5eSksagiyam 
3994b859378eSBarry Smith   Notes for advanced users:
3995b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3996b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3997b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3998b859378eSBarry Smith   format is
3999b859378eSBarry Smith .vb
4000b859378eSBarry Smith      has not yet been determined
4001b859378eSBarry Smith .ve
4002b859378eSBarry Smith 
4003b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
4004b859378eSBarry Smith @*/
4005b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
4006b859378eSBarry Smith {
40079331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
4008b859378eSBarry Smith 
4009b859378eSBarry Smith   PetscFunctionBegin;
4010b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
4011b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4012*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerCheckReadable(viewer));
4013*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary));
4014*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5));
4015*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(DM_Load,viewer,0,0,0));
40169331c7a4SMatthew G. Knepley   if (isbinary) {
40179331c7a4SMatthew G. Knepley     PetscInt classid;
40189331c7a4SMatthew G. Knepley     char     type[256];
4019b859378eSBarry Smith 
4020*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT));
40217a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID,PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
4022*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR));
4023*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetType(newdm, type));
4024*5f80ce2aSJacob Faibussowitsch     if (newdm->ops->load) CHKERRQ((*newdm->ops->load)(newdm,viewer));
40259331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4026*5f80ce2aSJacob Faibussowitsch     if (newdm->ops->load) CHKERRQ((*newdm->ops->load)(newdm,viewer));
40279331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
4028*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(DM_Load,viewer,0,0,0));
4029b859378eSBarry Smith   PetscFunctionReturn(0);
4030b859378eSBarry Smith }
4031b859378eSBarry Smith 
4032b2e4378dSMatthew G. Knepley /*@
4033b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
4034b2e4378dSMatthew G. Knepley 
4035b2e4378dSMatthew G. Knepley   Not collective
4036b2e4378dSMatthew G. Knepley 
4037b2e4378dSMatthew G. Knepley   Input Parameter:
4038b2e4378dSMatthew G. Knepley . dm - the DM
4039b2e4378dSMatthew G. Knepley 
4040b2e4378dSMatthew G. Knepley   Output Parameters:
4041b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
4042b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
4043b2e4378dSMatthew G. Knepley 
4044b2e4378dSMatthew G. Knepley   Level: beginner
4045b2e4378dSMatthew G. Knepley 
4046b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
4047b2e4378dSMatthew G. Knepley 
4048b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
4049b2e4378dSMatthew G. Knepley @*/
4050b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
4051b2e4378dSMatthew G. Knepley {
4052b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
4053b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
4054b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
4055b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
4056b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
4057b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
4058b2e4378dSMatthew G. Knepley 
4059b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4060b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4061*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDim(dm, &cdim));
4062*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinates(dm, &coords));
4063b2e4378dSMatthew G. Knepley   if (coords) {
4064*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetArrayRead(coords, &local_coords));
4065*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetLocalSize(coords, &N));
4066b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
4067b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
4068b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
4069b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4070b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
4071b2e4378dSMatthew G. Knepley       }
4072b2e4378dSMatthew G. Knepley     }
4073*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecRestoreArrayRead(coords, &local_coords));
4074b2e4378dSMatthew G. Knepley   } else {
4075b2e4378dSMatthew G. Knepley     PetscBool isda;
4076b2e4378dSMatthew G. Knepley 
4077*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda));
4078*5f80ce2aSJacob Faibussowitsch     if (isda) CHKERRQ(DMGetLocalBoundingIndices_DMDA(dm, min, max));
4079b2e4378dSMatthew G. Knepley   }
4080*5f80ce2aSJacob Faibussowitsch   if (lmin) CHKERRQ(PetscArraycpy(lmin, min, cdim));
4081*5f80ce2aSJacob Faibussowitsch   if (lmax) CHKERRQ(PetscArraycpy(lmax, max, cdim));
4082b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4083b2e4378dSMatthew G. Knepley }
4084b2e4378dSMatthew G. Knepley 
4085b2e4378dSMatthew G. Knepley /*@
4086b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
4087b2e4378dSMatthew G. Knepley 
4088b2e4378dSMatthew G. Knepley   Collective
4089b2e4378dSMatthew G. Knepley 
4090b2e4378dSMatthew G. Knepley   Input Parameter:
4091b2e4378dSMatthew G. Knepley . dm - the DM
4092b2e4378dSMatthew G. Knepley 
4093b2e4378dSMatthew G. Knepley   Output Parameters:
4094b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
4095b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
4096b2e4378dSMatthew G. Knepley 
4097b2e4378dSMatthew G. Knepley   Level: beginner
4098b2e4378dSMatthew G. Knepley 
4099b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
4100b2e4378dSMatthew G. Knepley @*/
4101b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
4102b2e4378dSMatthew G. Knepley {
4103b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
41046ce308c4SMatthew G. Knepley   PetscInt       cdim;
41056ce308c4SMatthew G. Knepley   PetscMPIInt    count;
4106b2e4378dSMatthew G. Knepley 
4107b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
4108b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4109*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDim(dm, &cdim));
4110*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMPIIntCast(cdim, &count));
4111*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalBoundingBox(dm, lmin, lmax));
4112*5f80ce2aSJacob Faibussowitsch   if (gmin) CHKERRMPI(MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm)));
4113*5f80ce2aSJacob Faibussowitsch   if (gmax) CHKERRMPI(MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm)));
4114b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
4115b2e4378dSMatthew G. Knepley }
4116b2e4378dSMatthew G. Knepley 
41177da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41187da65231SMatthew G Knepley 
4119a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4120a6dfd86eSKarl Rupp {
41211d47ebbbSSatish Balay   PetscInt       f;
41221b30c384SMatthew G Knepley 
41237da65231SMatthew G Knepley   PetscFunctionBegin;
4124*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name));
41251d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
4126*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41277da65231SMatthew G Knepley   }
41287da65231SMatthew G Knepley   PetscFunctionReturn(0);
41297da65231SMatthew G Knepley }
41307da65231SMatthew G Knepley 
4131a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4132a6dfd86eSKarl Rupp {
41331b30c384SMatthew G Knepley   PetscInt       f, g;
41347da65231SMatthew G Knepley 
41357da65231SMatthew G Knepley   PetscFunctionBegin;
4136*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name));
41371d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
4138*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPrintf(PETSC_COMM_SELF, "  |"));
41391d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
4140*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g])));
41417da65231SMatthew G Knepley     }
4142*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPrintf(PETSC_COMM_SELF, " |\n"));
41437da65231SMatthew G Knepley   }
41447da65231SMatthew G Knepley   PetscFunctionReturn(0);
41457da65231SMatthew G Knepley }
4146e7c4fc90SDmitry Karpeev 
41476113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4148e759306cSMatthew G. Knepley {
41490c5b8624SToby Isaac   PetscInt          localSize, bs;
41500c5b8624SToby Isaac   PetscMPIInt       size;
41510c5b8624SToby Isaac   Vec               x, xglob;
41520c5b8624SToby Isaac   const PetscScalar *xarray;
4153e759306cSMatthew G. Knepley 
4154e759306cSMatthew G. Knepley   PetscFunctionBegin;
4155*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size));
4156*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDuplicate(X, &x));
4157*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecCopy(X, x));
4158*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecChop(x, tol));
4159*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name));
41600c5b8624SToby Isaac   if (size > 1) {
4161*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetLocalSize(x,&localSize));
4162*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetArrayRead(x,&xarray));
4163*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetBlockSize(x,&bs));
4164*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob));
41650c5b8624SToby Isaac   } else {
41660c5b8624SToby Isaac     xglob = x;
41670c5b8624SToby Isaac   }
4168*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm))));
41690c5b8624SToby Isaac   if (size > 1) {
4170*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecDestroy(&xglob));
4171*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecRestoreArrayRead(x,&xarray));
41720c5b8624SToby Isaac   }
4173*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&x));
4174e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4175e759306cSMatthew G. Knepley }
4176e759306cSMatthew G. Knepley 
417788ed4aceSMatthew G Knepley /*@
41781bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4179061576a5SJed Brown 
4180061576a5SJed Brown   Input Parameter:
4181061576a5SJed Brown . dm - The DM
4182061576a5SJed Brown 
4183061576a5SJed Brown   Output Parameter:
4184061576a5SJed Brown . section - The PetscSection
4185061576a5SJed Brown 
4186061576a5SJed Brown   Options Database Keys:
4187061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4188061576a5SJed Brown 
4189061576a5SJed Brown   Level: advanced
4190061576a5SJed Brown 
4191061576a5SJed Brown   Notes:
4192061576a5SJed Brown   Use DMGetLocalSection() in new code.
4193061576a5SJed Brown 
4194061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4195061576a5SJed Brown 
4196061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
4197061576a5SJed Brown @*/
4198061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4199061576a5SJed Brown {
4200061576a5SJed Brown   PetscFunctionBegin;
4201*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalSection(dm,section));
4202061576a5SJed Brown   PetscFunctionReturn(0);
4203061576a5SJed Brown }
4204061576a5SJed Brown 
4205061576a5SJed Brown /*@
4206061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
420788ed4aceSMatthew G Knepley 
420888ed4aceSMatthew G Knepley   Input Parameter:
420988ed4aceSMatthew G Knepley . dm - The DM
421088ed4aceSMatthew G Knepley 
421188ed4aceSMatthew G Knepley   Output Parameter:
421288ed4aceSMatthew G Knepley . section - The PetscSection
421388ed4aceSMatthew G Knepley 
4214e5893cccSMatthew G. Knepley   Options Database Keys:
4215e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4216e5893cccSMatthew G. Knepley 
421788ed4aceSMatthew G Knepley   Level: intermediate
421888ed4aceSMatthew G Knepley 
421988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
422088ed4aceSMatthew G Knepley 
4221061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
422288ed4aceSMatthew G Knepley @*/
4223061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
42240adebc6cSBarry Smith {
422588ed4aceSMatthew G Knepley   PetscFunctionBegin;
422688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
422788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42281bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4229e5e52638SMatthew G. Knepley     PetscInt d;
4230e5e52638SMatthew G. Knepley 
423145480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
423245480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject) dm;
423345480ffeSMatthew G. Knepley       PetscViewer       viewer;
423445480ffeSMatthew G. Knepley       PetscViewerFormat format;
423545480ffeSMatthew G. Knepley       PetscBool         flg;
423645480ffeSMatthew G. Knepley 
4237*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
4238*5f80ce2aSJacob Faibussowitsch       if (flg) CHKERRQ(PetscViewerPushFormat(viewer, format));
423945480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
4240*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSSetFromOptions(dm->probs[d].ds));
4241*5f80ce2aSJacob Faibussowitsch         if (flg) CHKERRQ(PetscDSView(dm->probs[d].ds, viewer));
424245480ffeSMatthew G. Knepley       }
424345480ffeSMatthew G. Knepley       if (flg) {
4244*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscViewerFlush(viewer));
4245*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscViewerPopFormat(viewer));
4246*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscViewerDestroy(&viewer));
424745480ffeSMatthew G. Knepley       }
424845480ffeSMatthew G. Knepley     }
4249*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->createlocalsection)(dm));
4250*5f80ce2aSJacob Faibussowitsch     if (dm->localSection) CHKERRQ(PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view"));
42512f0f8703SMatthew G. Knepley   }
42521bb6d2a8SBarry Smith   *section = dm->localSection;
425388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
425488ed4aceSMatthew G Knepley }
425588ed4aceSMatthew G Knepley 
425688ed4aceSMatthew G Knepley /*@
42571bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4258061576a5SJed Brown 
4259061576a5SJed Brown   Input Parameters:
4260061576a5SJed Brown + dm - The DM
4261061576a5SJed Brown - section - The PetscSection
4262061576a5SJed Brown 
4263061576a5SJed Brown   Level: advanced
4264061576a5SJed Brown 
4265061576a5SJed Brown   Notes:
4266061576a5SJed Brown   Use DMSetLocalSection() in new code.
4267061576a5SJed Brown 
4268061576a5SJed Brown   Any existing Section will be destroyed
4269061576a5SJed Brown 
4270061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4271061576a5SJed Brown @*/
4272061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4273061576a5SJed Brown {
4274061576a5SJed Brown   PetscFunctionBegin;
4275*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetLocalSection(dm,section));
4276061576a5SJed Brown   PetscFunctionReturn(0);
4277061576a5SJed Brown }
4278061576a5SJed Brown 
4279061576a5SJed Brown /*@
4280061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
428188ed4aceSMatthew G Knepley 
428288ed4aceSMatthew G Knepley   Input Parameters:
428388ed4aceSMatthew G Knepley + dm - The DM
428488ed4aceSMatthew G Knepley - section - The PetscSection
428588ed4aceSMatthew G Knepley 
428688ed4aceSMatthew G Knepley   Level: intermediate
428788ed4aceSMatthew G Knepley 
428888ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
428988ed4aceSMatthew G Knepley 
4290061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
429188ed4aceSMatthew G Knepley @*/
4292061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
42930adebc6cSBarry Smith {
4294c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4295af122d2aSMatthew G Knepley   PetscInt       f;
429688ed4aceSMatthew G Knepley 
429788ed4aceSMatthew G Knepley   PetscFunctionBegin;
429888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4299b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4300*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject)section));
4301*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&dm->localSection));
43021bb6d2a8SBarry Smith   dm->localSection = section;
4303*5f80ce2aSJacob Faibussowitsch   if (section) CHKERRQ(PetscSectionGetNumFields(dm->localSection, &numFields));
4304af122d2aSMatthew G Knepley   if (numFields) {
4305*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetNumFields(dm, numFields));
4306af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43070f21e855SMatthew G. Knepley       PetscObject disc;
4308af122d2aSMatthew G Knepley       const char *name;
4309af122d2aSMatthew G Knepley 
4310*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetFieldName(dm->localSection, f, &name));
4311*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetField(dm, f, NULL, &disc));
4312*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscObjectSetName(disc, name));
4313af122d2aSMatthew G Knepley     }
4314af122d2aSMatthew G Knepley   }
4315e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
4316*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&dm->globalSection));
431788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
431888ed4aceSMatthew G Knepley }
431988ed4aceSMatthew G Knepley 
43209435951eSToby Isaac /*@
4321b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
43229435951eSToby Isaac 
4323e228b242SToby Isaac   not collective
4324e228b242SToby Isaac 
43259435951eSToby Isaac   Input Parameter:
43269435951eSToby Isaac . dm - The DM
43279435951eSToby Isaac 
4328d8d19677SJose E. Roman   Output Parameters:
43299435951eSToby 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.
433079769bd5SJed 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.
433179769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
43329435951eSToby Isaac 
43339435951eSToby Isaac   Level: advanced
43349435951eSToby Isaac 
433579769bd5SJed Brown   Note: This gets borrowed references, so the user should not destroy the PetscSection, Mat, or Vec.
43369435951eSToby Isaac 
43379435951eSToby Isaac .seealso: DMSetDefaultConstraints()
43389435951eSToby Isaac @*/
433979769bd5SJed Brown PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
43409435951eSToby Isaac {
43419435951eSToby Isaac   PetscFunctionBegin;
43429435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4343*5f80ce2aSJacob Faibussowitsch   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) CHKERRQ((*dm->ops->createdefaultconstraints)(dm));
43443b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
43453b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
434679769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
43479435951eSToby Isaac   PetscFunctionReturn(0);
43489435951eSToby Isaac }
43499435951eSToby Isaac 
43509435951eSToby Isaac /*@
4351b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
43529435951eSToby Isaac 
435379769bd5SJed 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().
43549435951eSToby Isaac 
435579769bd5SJed 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.
43569435951eSToby Isaac 
4357e228b242SToby Isaac   collective on dm
4358e228b242SToby Isaac 
43599435951eSToby Isaac   Input Parameters:
43609435951eSToby Isaac + dm - The DM
436179769bd5SJed 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).
436279769bd5SJed 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).
436379769bd5SJed 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).
43649435951eSToby Isaac 
43659435951eSToby Isaac   Level: advanced
43669435951eSToby Isaac 
436779769bd5SJed Brown   Note: This increments the references of the PetscSection, Mat, and Vec, so they user can destroy them.
43689435951eSToby Isaac 
43699435951eSToby Isaac .seealso: DMGetDefaultConstraints()
43709435951eSToby Isaac @*/
437179769bd5SJed Brown PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
43729435951eSToby Isaac {
4373e228b242SToby Isaac   PetscMPIInt result;
43749435951eSToby Isaac 
43759435951eSToby Isaac   PetscFunctionBegin;
43769435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4377e228b242SToby Isaac   if (section) {
4378e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4379*5f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result));
43807a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4381e228b242SToby Isaac   }
4382e228b242SToby Isaac   if (mat) {
4383e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4384*5f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result));
43857a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4386e228b242SToby Isaac   }
438779769bd5SJed Brown   if (bias) {
438879769bd5SJed Brown     PetscValidHeaderSpecific(bias,VEC_CLASSID,4);
4389*5f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)bias),&result));
439079769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT,PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint bias must have local communicator");
439179769bd5SJed Brown   }
4392*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject)section));
4393*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&dm->defaultConstraint.section));
43943b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
4395*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject)mat));
4396*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatDestroy(&dm->defaultConstraint.mat));
43973b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
4398*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject)bias));
4399*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&dm->defaultConstraint.bias));
440079769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44019435951eSToby Isaac   PetscFunctionReturn(0);
44029435951eSToby Isaac }
44039435951eSToby Isaac 
4404497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4405507e4973SMatthew G. Knepley /*
4406507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4407507e4973SMatthew G. Knepley 
4408507e4973SMatthew G. Knepley   Input Parameters:
4409507e4973SMatthew G. Knepley + dm - The DM
4410507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4411507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4412507e4973SMatthew G. Knepley 
4413507e4973SMatthew G. Knepley   Level: intermediate
4414507e4973SMatthew G. Knepley 
44151bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4416507e4973SMatthew G. Knepley */
4417f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4418507e4973SMatthew G. Knepley {
4419507e4973SMatthew G. Knepley   MPI_Comm        comm;
4420507e4973SMatthew G. Knepley   PetscLayout     layout;
4421507e4973SMatthew G. Knepley   const PetscInt *ranges;
4422507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4423507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4424507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4425507e4973SMatthew G. Knepley 
4426507e4973SMatthew G. Knepley   PetscFunctionBegin;
4427*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetComm((PetscObject)dm,&comm));
4428507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4429*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_size(comm, &size));
4430*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_rank(comm, &rank));
4431*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(globalSection, &pStart, &pEnd));
4432*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
4433*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutCreate(comm, &layout));
4434*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutSetBlockSize(layout, 1));
4435*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutSetLocalSize(layout, nroots));
4436*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutSetUp(layout));
4437*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutGetRanges(layout, &ranges));
4438507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4439f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4440507e4973SMatthew G. Knepley 
4441*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(localSection, p, &dof));
4442*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(localSection, p, &off));
4443*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintDof(localSection, p, &cdof));
4444*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(globalSection, p, &gdof));
4445*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
4446*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(globalSection, p, &goff));
4447507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4448*5f80ce2aSJacob Faibussowitsch     if ((gdof < 0 ? -(gdof+1) : gdof) != dof) {CHKERRQ(PetscSynchronizedPrintf(comm, "[%d]Global dof %d for point %d not equal to local dof %d\n", rank, gdof, p, dof)); valid = PETSC_FALSE;}
4449*5f80ce2aSJacob Faibussowitsch     if (gcdof && (gcdof != cdof)) {CHKERRQ(PetscSynchronizedPrintf(comm, "[%d]Global constraints %d for point %d not equal to local constraints %d\n", rank, gcdof, p, cdof)); valid = PETSC_FALSE;}
4450507e4973SMatthew G. Knepley     if (gdof < 0) {
4451507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4452507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4453507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4454507e4973SMatthew G. Knepley 
4455*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscFindInt(offset,size+1,ranges,&r));
4456507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4457*5f80ce2aSJacob Faibussowitsch         if ((r < 0) || (r >= size)) {CHKERRQ(PetscSynchronizedPrintf(comm, "[%d]Point %d mapped to invalid process %d (%d, %d)\n", rank, p, r, gdof, goff)); valid = PETSC_FALSE;break;}
4458507e4973SMatthew G. Knepley       }
4459507e4973SMatthew G. Knepley     }
4460507e4973SMatthew G. Knepley   }
4461*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLayoutDestroy(&layout));
4462*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSynchronizedFlush(comm, NULL));
4463*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4464507e4973SMatthew G. Knepley   if (!gvalid) {
4465*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMView(dm, NULL));
4466507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4467507e4973SMatthew G. Knepley   }
4468507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4469507e4973SMatthew G. Knepley }
4470f741bcd2SMatthew G. Knepley #endif
4471507e4973SMatthew G. Knepley 
447288ed4aceSMatthew G Knepley /*@
4473e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
447488ed4aceSMatthew G Knepley 
4475d083f849SBarry Smith   Collective on dm
44768b1ab98fSJed Brown 
447788ed4aceSMatthew G Knepley   Input Parameter:
447888ed4aceSMatthew G Knepley . dm - The DM
447988ed4aceSMatthew G Knepley 
448088ed4aceSMatthew G Knepley   Output Parameter:
448188ed4aceSMatthew G Knepley . section - The PetscSection
448288ed4aceSMatthew G Knepley 
448388ed4aceSMatthew G Knepley   Level: intermediate
448488ed4aceSMatthew G Knepley 
448588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
448688ed4aceSMatthew G Knepley 
448792fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
448888ed4aceSMatthew G Knepley @*/
4489e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
44900adebc6cSBarry Smith {
449188ed4aceSMatthew G Knepley   PetscFunctionBegin;
449288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
449388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
44941bb6d2a8SBarry Smith   if (!dm->globalSection) {
4495fd59a867SMatthew G. Knepley     PetscSection s;
4496fd59a867SMatthew G. Knepley 
4497*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLocalSection(dm, &s));
44987a8be351SBarry Smith     PetscCheck(s,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
44997a8be351SBarry Smith     PetscCheck(dm->sf,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
4500*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
4501*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscLayoutDestroy(&dm->map));
4502*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
4503*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
450488ed4aceSMatthew G Knepley   }
45051bb6d2a8SBarry Smith   *section = dm->globalSection;
450688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
450788ed4aceSMatthew G Knepley }
450888ed4aceSMatthew G Knepley 
4509b21d0597SMatthew G Knepley /*@
4510e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4511b21d0597SMatthew G Knepley 
4512b21d0597SMatthew G Knepley   Input Parameters:
4513b21d0597SMatthew G Knepley + dm - The DM
45145080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4515b21d0597SMatthew G Knepley 
4516b21d0597SMatthew G Knepley   Level: intermediate
4517b21d0597SMatthew G Knepley 
4518b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4519b21d0597SMatthew G Knepley 
452092fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4521b21d0597SMatthew G Knepley @*/
4522e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
45230adebc6cSBarry Smith {
4524b21d0597SMatthew G Knepley   PetscFunctionBegin;
4525b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45265080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4527*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject)section));
4528*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&dm->globalSection));
45291bb6d2a8SBarry Smith   dm->globalSection = section;
4530497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4531*5f80ce2aSJacob Faibussowitsch   if (section) CHKERRQ(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4532507e4973SMatthew G. Knepley #endif
4533b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4534b21d0597SMatthew G Knepley }
4535b21d0597SMatthew G Knepley 
453688ed4aceSMatthew G Knepley /*@
45371bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
453888ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
453988ed4aceSMatthew G Knepley 
454088ed4aceSMatthew G Knepley   Input Parameter:
454188ed4aceSMatthew G Knepley . dm - The DM
454288ed4aceSMatthew G Knepley 
454388ed4aceSMatthew G Knepley   Output Parameter:
454488ed4aceSMatthew G Knepley . sf - The PetscSF
454588ed4aceSMatthew G Knepley 
454688ed4aceSMatthew G Knepley   Level: intermediate
454788ed4aceSMatthew G Knepley 
454888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
454988ed4aceSMatthew G Knepley 
45501bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
455188ed4aceSMatthew G Knepley @*/
45521bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
45530adebc6cSBarry Smith {
455488ed4aceSMatthew G Knepley   PetscInt       nroots;
455588ed4aceSMatthew G Knepley 
455688ed4aceSMatthew G Knepley   PetscFunctionBegin;
455788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
455888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
45591bb6d2a8SBarry Smith   if (!dm->sectionSF) {
4560*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF));
456133907cc2SStefano Zampini   }
4562*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
456388ed4aceSMatthew G Knepley   if (nroots < 0) {
456488ed4aceSMatthew G Knepley     PetscSection section, gSection;
456588ed4aceSMatthew G Knepley 
4566*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLocalSection(dm, &section));
456731ea6d37SMatthew G Knepley     if (section) {
4568*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetGlobalSection(dm, &gSection));
4569*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMCreateSectionSF(dm, section, gSection));
457031ea6d37SMatthew G Knepley     } else {
45710298fd71SBarry Smith       *sf = NULL;
457231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
457331ea6d37SMatthew G Knepley     }
457488ed4aceSMatthew G Knepley   }
45751bb6d2a8SBarry Smith   *sf = dm->sectionSF;
457688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
457788ed4aceSMatthew G Knepley }
457888ed4aceSMatthew G Knepley 
457988ed4aceSMatthew G Knepley /*@
45801bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
458188ed4aceSMatthew G Knepley 
458288ed4aceSMatthew G Knepley   Input Parameters:
458388ed4aceSMatthew G Knepley + dm - The DM
458488ed4aceSMatthew G Knepley - sf - The PetscSF
458588ed4aceSMatthew G Knepley 
458688ed4aceSMatthew G Knepley   Level: intermediate
458788ed4aceSMatthew G Knepley 
458888ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
458988ed4aceSMatthew G Knepley 
45901bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
459188ed4aceSMatthew G Knepley @*/
45921bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
45930adebc6cSBarry Smith {
459488ed4aceSMatthew G Knepley   PetscFunctionBegin;
459588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4596b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4597*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) sf));
4598*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFDestroy(&dm->sectionSF));
45991bb6d2a8SBarry Smith   dm->sectionSF = sf;
460088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
460188ed4aceSMatthew G Knepley }
460288ed4aceSMatthew G Knepley 
460388ed4aceSMatthew G Knepley /*@C
46041bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
460588ed4aceSMatthew G Knepley   describing the data layout.
460688ed4aceSMatthew G Knepley 
460788ed4aceSMatthew G Knepley   Input Parameters:
460888ed4aceSMatthew G Knepley + dm - The DM
460988ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
461088ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
461188ed4aceSMatthew G Knepley 
46121bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
461388ed4aceSMatthew G Knepley 
46141bb6d2a8SBarry Smith   Level: developer
46151bb6d2a8SBarry Smith 
46161bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
46171bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
46181bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
46191bb6d2a8SBarry Smith 
46201bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
462188ed4aceSMatthew G Knepley @*/
46221bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
462388ed4aceSMatthew G Knepley {
462488ed4aceSMatthew G Knepley   PetscFunctionBegin;
462588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4626*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
462788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
462888ed4aceSMatthew G Knepley }
4629af122d2aSMatthew G Knepley 
4630b21d0597SMatthew G Knepley /*@
4631b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4632b21d0597SMatthew G Knepley 
4633b21d0597SMatthew G Knepley   Input Parameter:
4634b21d0597SMatthew G Knepley . dm - The DM
4635b21d0597SMatthew G Knepley 
4636b21d0597SMatthew G Knepley   Output Parameter:
4637b21d0597SMatthew G Knepley . sf - The PetscSF
4638b21d0597SMatthew G Knepley 
4639b21d0597SMatthew G Knepley   Level: intermediate
4640b21d0597SMatthew G Knepley 
4641b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4642b21d0597SMatthew G Knepley 
46431bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4644b21d0597SMatthew G Knepley @*/
46450adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
46460adebc6cSBarry Smith {
4647b21d0597SMatthew G Knepley   PetscFunctionBegin;
4648b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4649b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4650b21d0597SMatthew G Knepley   *sf = dm->sf;
4651b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4652b21d0597SMatthew G Knepley }
4653b21d0597SMatthew G Knepley 
4654057b4bcdSMatthew G Knepley /*@
4655057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4656057b4bcdSMatthew G Knepley 
4657057b4bcdSMatthew G Knepley   Input Parameters:
4658057b4bcdSMatthew G Knepley + dm - The DM
4659057b4bcdSMatthew G Knepley - sf - The PetscSF
4660057b4bcdSMatthew G Knepley 
4661057b4bcdSMatthew G Knepley   Level: intermediate
4662057b4bcdSMatthew G Knepley 
46631bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4664057b4bcdSMatthew G Knepley @*/
46650adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
46660adebc6cSBarry Smith {
4667057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4668057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4669b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4670*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) sf));
4671*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFDestroy(&dm->sf));
4672057b4bcdSMatthew G Knepley   dm->sf = sf;
4673057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4674057b4bcdSMatthew G Knepley }
4675057b4bcdSMatthew G Knepley 
46764f37162bSMatthew G. Knepley /*@
46774f37162bSMatthew G. Knepley   DMGetNaturalSF - Get the PetscSF encoding the map back to the original mesh ordering
46784f37162bSMatthew G. Knepley 
46794f37162bSMatthew G. Knepley   Input Parameter:
46804f37162bSMatthew G. Knepley . dm - The DM
46814f37162bSMatthew G. Knepley 
46824f37162bSMatthew G. Knepley   Output Parameter:
46834f37162bSMatthew G. Knepley . sf - The PetscSF
46844f37162bSMatthew G. Knepley 
46854f37162bSMatthew G. Knepley   Level: intermediate
46864f37162bSMatthew G. Knepley 
46874f37162bSMatthew G. Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
46884f37162bSMatthew G. Knepley 
46894f37162bSMatthew G. Knepley .seealso: DMSetNaturalSF(), DMSetUseNatural(), DMGetUseNatural(), DMPlexCreateGlobalToNaturalSF(), DMPlexDistribute()
46904f37162bSMatthew G. Knepley @*/
46914f37162bSMatthew G. Knepley PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
46924f37162bSMatthew G. Knepley {
46934f37162bSMatthew G. Knepley   PetscFunctionBegin;
46944f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46954f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
46964f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
46974f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
46984f37162bSMatthew G. Knepley }
46994f37162bSMatthew G. Knepley 
47004f37162bSMatthew G. Knepley /*@
47014f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
47024f37162bSMatthew G. Knepley 
47034f37162bSMatthew G. Knepley   Input Parameters:
47044f37162bSMatthew G. Knepley + dm - The DM
47054f37162bSMatthew G. Knepley - sf - The PetscSF
47064f37162bSMatthew G. Knepley 
47074f37162bSMatthew G. Knepley   Level: intermediate
47084f37162bSMatthew G. Knepley 
47094f37162bSMatthew G. Knepley .seealso: DMGetNaturalSF(), DMSetUseNatural(), DMGetUseNatural(), DMPlexCreateGlobalToNaturalSF(), DMPlexDistribute()
47104f37162bSMatthew G. Knepley @*/
47114f37162bSMatthew G. Knepley PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
47124f37162bSMatthew G. Knepley {
47134f37162bSMatthew G. Knepley   PetscFunctionBegin;
47144f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47154f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4716*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) sf));
4717*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFDestroy(&dm->sfNatural));
47184f37162bSMatthew G. Knepley   dm->sfNatural = sf;
47194f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
47204f37162bSMatthew G. Knepley }
47214f37162bSMatthew G. Knepley 
472234aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
472334aa8a36SMatthew G. Knepley {
472434aa8a36SMatthew G. Knepley   PetscClassId   id;
472534aa8a36SMatthew G. Knepley 
472634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
4727*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetClassId(disc, &id));
472834aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
4729*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
473034aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
4731*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
473217c1d62eSMatthew G. Knepley   } else {
4733*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
473434aa8a36SMatthew G. Knepley   }
473534aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
473634aa8a36SMatthew G. Knepley }
473734aa8a36SMatthew G. Knepley 
473844a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
473944a7f3ddSMatthew G. Knepley {
474044a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
474144a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
474244a7f3ddSMatthew G. Knepley 
474344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
474444a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
4745*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(NfNew, &tmpr));
474644a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4747e0b68406SMatthew Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL; tmpr[f].avoidTensor = PETSC_FALSE;}
4748*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(dm->fields));
474944a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
475044a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
475144a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
475244a7f3ddSMatthew G. Knepley }
475344a7f3ddSMatthew G. Knepley 
475444a7f3ddSMatthew G. Knepley /*@
475544a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
475644a7f3ddSMatthew G. Knepley 
4757d083f849SBarry Smith   Logically collective on dm
475844a7f3ddSMatthew G. Knepley 
475944a7f3ddSMatthew G. Knepley   Input Parameter:
476044a7f3ddSMatthew G. Knepley . dm - The DM
476144a7f3ddSMatthew G. Knepley 
476244a7f3ddSMatthew G. Knepley   Level: intermediate
476344a7f3ddSMatthew G. Knepley 
476444a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
476544a7f3ddSMatthew G. Knepley @*/
476644a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
476744a7f3ddSMatthew G. Knepley {
476844a7f3ddSMatthew G. Knepley   PetscInt       f;
476944a7f3ddSMatthew G. Knepley 
477044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
477144a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
477244a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
4773*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectDestroy(&dm->fields[f].disc));
4774*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelDestroy(&dm->fields[f].label));
477544a7f3ddSMatthew G. Knepley   }
4776*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(dm->fields));
477744a7f3ddSMatthew G. Knepley   dm->fields = NULL;
477844a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
477944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
478044a7f3ddSMatthew G. Knepley }
478144a7f3ddSMatthew G. Knepley 
4782689b5837SMatthew G. Knepley /*@
4783689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4784689b5837SMatthew G. Knepley 
4785689b5837SMatthew G. Knepley   Not collective
4786689b5837SMatthew G. Knepley 
4787689b5837SMatthew G. Knepley   Input Parameter:
4788689b5837SMatthew G. Knepley . dm - The DM
4789689b5837SMatthew G. Knepley 
4790689b5837SMatthew G. Knepley   Output Parameter:
4791689b5837SMatthew G. Knepley . Nf - The number of fields
4792689b5837SMatthew G. Knepley 
4793689b5837SMatthew G. Knepley   Level: intermediate
4794689b5837SMatthew G. Knepley 
4795689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4796689b5837SMatthew G. Knepley @*/
47970f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
47980f21e855SMatthew G. Knepley {
47990f21e855SMatthew G. Knepley   PetscFunctionBegin;
48000f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4801534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
480244a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4803af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4804af122d2aSMatthew G Knepley }
4805af122d2aSMatthew G Knepley 
4806689b5837SMatthew G. Knepley /*@
4807689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4808689b5837SMatthew G. Knepley 
4809d083f849SBarry Smith   Logically collective on dm
4810689b5837SMatthew G. Knepley 
4811689b5837SMatthew G. Knepley   Input Parameters:
4812689b5837SMatthew G. Knepley + dm - The DM
4813689b5837SMatthew G. Knepley - Nf - The number of fields
4814689b5837SMatthew G. Knepley 
4815689b5837SMatthew G. Knepley   Level: intermediate
4816689b5837SMatthew G. Knepley 
4817689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4818689b5837SMatthew G. Knepley @*/
4819af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4820af122d2aSMatthew G Knepley {
48210f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4822af122d2aSMatthew G Knepley 
4823af122d2aSMatthew G Knepley   PetscFunctionBegin;
4824af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4825*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumFields(dm, &Nf));
48260f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
48270f21e855SMatthew G. Knepley     PetscContainer obj;
48280f21e855SMatthew G. Knepley 
4829*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj));
4830*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMAddField(dm, NULL, (PetscObject) obj));
4831*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscContainerDestroy(&obj));
4832af122d2aSMatthew G Knepley   }
4833af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4834af122d2aSMatthew G Knepley }
4835af122d2aSMatthew G Knepley 
4836c1929be8SMatthew G. Knepley /*@
4837c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4838c1929be8SMatthew G. Knepley 
4839c1929be8SMatthew G. Knepley   Not collective
4840c1929be8SMatthew G. Knepley 
4841c1929be8SMatthew G. Knepley   Input Parameters:
4842c1929be8SMatthew G. Knepley + dm - The DM
4843c1929be8SMatthew G. Knepley - f  - The field number
4844c1929be8SMatthew G. Knepley 
484544a7f3ddSMatthew G. Knepley   Output Parameters:
484644a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
484744a7f3ddSMatthew G. Knepley - field - The discretization object
4848c1929be8SMatthew G. Knepley 
484944a7f3ddSMatthew G. Knepley   Level: intermediate
4850c1929be8SMatthew G. Knepley 
485144a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4852c1929be8SMatthew G. Knepley @*/
485344a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4854af122d2aSMatthew G Knepley {
4855af122d2aSMatthew G Knepley   PetscFunctionBegin;
4856af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4857064a246eSJacob Faibussowitsch   PetscValidPointer(field, 4);
48587a8be351SBarry 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);
485944a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
486044a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4861decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4862decb47aaSMatthew G. Knepley }
4863decb47aaSMatthew G. Knepley 
4864083401c6SMatthew G. Knepley /* Does not clear the DS */
4865083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4866083401c6SMatthew G. Knepley {
4867083401c6SMatthew G. Knepley   PetscFunctionBegin;
4868*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMFieldEnlarge_Static(dm, f+1));
4869*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelDestroy(&dm->fields[f].label));
4870*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectDestroy(&dm->fields[f].disc));
4871083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4872083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4873*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) label));
4874*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) field));
4875083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4876083401c6SMatthew G. Knepley }
4877083401c6SMatthew G. Knepley 
4878c1929be8SMatthew G. Knepley /*@
4879c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4880c1929be8SMatthew G. Knepley 
4881d083f849SBarry Smith   Logically collective on dm
4882c1929be8SMatthew G. Knepley 
4883c1929be8SMatthew G. Knepley   Input Parameters:
4884c1929be8SMatthew G. Knepley + dm    - The DM
4885c1929be8SMatthew G. Knepley . f     - The field number
488644a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4887c1929be8SMatthew G. Knepley - field - The discretization object
4888c1929be8SMatthew G. Knepley 
488944a7f3ddSMatthew G. Knepley   Level: intermediate
4890c1929be8SMatthew G. Knepley 
489144a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4892c1929be8SMatthew G. Knepley @*/
489344a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4894decb47aaSMatthew G. Knepley {
4895decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4896decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4897e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
489844a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
48997a8be351SBarry Smith   PetscCheck(f >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
4900*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetField_Internal(dm, f, label, field));
4901*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetDefaultAdjacency_Private(dm, f, field));
4902*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMClearDS(dm));
490344a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
490444a7f3ddSMatthew G. Knepley }
490544a7f3ddSMatthew G. Knepley 
490644a7f3ddSMatthew G. Knepley /*@
490744a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
490844a7f3ddSMatthew G. Knepley 
4909d083f849SBarry Smith   Logically collective on dm
491044a7f3ddSMatthew G. Knepley 
491144a7f3ddSMatthew G. Knepley   Input Parameters:
491244a7f3ddSMatthew G. Knepley + dm    - The DM
491344a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
491444a7f3ddSMatthew G. Knepley - field - The discretization object
491544a7f3ddSMatthew G. Knepley 
491644a7f3ddSMatthew G. Knepley   Level: intermediate
491744a7f3ddSMatthew G. Knepley 
491844a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
491944a7f3ddSMatthew G. Knepley @*/
492044a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
492144a7f3ddSMatthew G. Knepley {
492244a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
492344a7f3ddSMatthew G. Knepley 
492444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
492544a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4926064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
492744a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
4928*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMFieldEnlarge_Static(dm, Nf+1));
492944a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
493044a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
4931*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) label));
4932*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) field));
4933*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetDefaultAdjacency_Private(dm, Nf, field));
4934*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMClearDS(dm));
4935af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4936af122d2aSMatthew G Knepley }
49376636e97aSMatthew G Knepley 
4938e5e52638SMatthew G. Knepley /*@
4939e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4940e0b68406SMatthew Knepley 
4941e0b68406SMatthew Knepley   Logically collective on dm
4942e0b68406SMatthew Knepley 
4943e0b68406SMatthew Knepley   Input Parameters:
4944e0b68406SMatthew Knepley + dm          - The DM
4945e0b68406SMatthew Knepley . f           - The field index
4946e0b68406SMatthew Knepley - avoidTensor - The flag to avoid defining the field on tensor cells
4947e0b68406SMatthew Knepley 
4948e0b68406SMatthew Knepley   Level: intermediate
4949e0b68406SMatthew Knepley 
4950e0b68406SMatthew Knepley .seealso: DMGetFieldAvoidTensor(), DMSetField(), DMGetField()
4951e0b68406SMatthew Knepley @*/
4952e0b68406SMatthew Knepley PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
4953e0b68406SMatthew Knepley {
4954e0b68406SMatthew Knepley   PetscFunctionBegin;
495512e84ecbSMatthew Knepley   PetscCheck((f >= 0) && (f < dm->Nf),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
4956e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
4957e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4958e0b68406SMatthew Knepley }
4959e0b68406SMatthew Knepley 
4960e0b68406SMatthew Knepley /*@
4961e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
4962e0b68406SMatthew Knepley 
4963e0b68406SMatthew Knepley   Logically collective on dm
4964e0b68406SMatthew Knepley 
4965e0b68406SMatthew Knepley   Input Parameters:
4966e0b68406SMatthew Knepley + dm          - The DM
4967e0b68406SMatthew Knepley - f           - The field index
4968e0b68406SMatthew Knepley 
4969e0b68406SMatthew Knepley   Output Parameter:
4970e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
4971e0b68406SMatthew Knepley 
4972e0b68406SMatthew Knepley   Level: intermediate
4973e0b68406SMatthew Knepley 
4974e0b68406SMatthew Knepley .seealso: DMSetFieldAvoidTensor(), DMSetField(), DMGetField()
4975e0b68406SMatthew Knepley @*/
4976e0b68406SMatthew Knepley PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
4977e0b68406SMatthew Knepley {
4978e0b68406SMatthew Knepley   PetscFunctionBegin;
49797a8be351SBarry Smith   PetscCheck((f >= 0) && (f < dm->Nf),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, %D)", f, dm->Nf);
4980e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
4981e0b68406SMatthew Knepley   PetscFunctionReturn(0);
4982e0b68406SMatthew Knepley }
4983e0b68406SMatthew Knepley 
4984e0b68406SMatthew Knepley /*@
4985e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4986e5e52638SMatthew G. Knepley 
4987d083f849SBarry Smith   Collective on dm
4988e5e52638SMatthew G. Knepley 
4989e5e52638SMatthew G. Knepley   Input Parameter:
4990e5e52638SMatthew G. Knepley . dm - The DM
4991e5e52638SMatthew G. Knepley 
4992e5e52638SMatthew G. Knepley   Output Parameter:
4993e5e52638SMatthew G. Knepley . newdm - The DM
4994e5e52638SMatthew G. Knepley 
4995e5e52638SMatthew G. Knepley   Level: advanced
4996e5e52638SMatthew G. Knepley 
4997e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4998e5e52638SMatthew G. Knepley @*/
4999e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
5000e5e52638SMatthew G. Knepley {
5001e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
5002e5e52638SMatthew G. Knepley 
5003e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5004e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5005*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumFields(dm, &Nf));
5006*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMClearFields(newdm));
5007e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5008e5e52638SMatthew G. Knepley     DMLabel     label;
5009e5e52638SMatthew G. Knepley     PetscObject field;
501034aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5011e5e52638SMatthew G. Knepley 
5012*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetField(dm, f, &label, &field));
5013*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetField(newdm, f, label, field));
5014*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetAdjacency(dm, f, &useCone, &useClosure));
5015*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetAdjacency(newdm, f, useCone, useClosure));
501634aa8a36SMatthew G. Knepley   }
501734aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
501834aa8a36SMatthew G. Knepley }
501934aa8a36SMatthew G. Knepley 
502034aa8a36SMatthew G. Knepley /*@
502134aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
502234aa8a36SMatthew G. Knepley 
502334aa8a36SMatthew G. Knepley   Not collective
502434aa8a36SMatthew G. Knepley 
502534aa8a36SMatthew G. Knepley   Input Parameters:
502634aa8a36SMatthew G. Knepley + dm - The DM object
502734aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
502834aa8a36SMatthew G. Knepley 
5029d8d19677SJose E. Roman   Output Parameters:
503034aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
503134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
503234aa8a36SMatthew G. Knepley 
503334aa8a36SMatthew G. Knepley   Notes:
503434aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
503534aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
503634aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5037979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
503834aa8a36SMatthew G. Knepley 
503934aa8a36SMatthew G. Knepley   Level: developer
504034aa8a36SMatthew G. Knepley 
504134aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
504234aa8a36SMatthew G. Knepley @*/
504334aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
504434aa8a36SMatthew G. Knepley {
504534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
504634aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5047534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
5048534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
504934aa8a36SMatthew G. Knepley   if (f < 0) {
505034aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
505134aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
505234aa8a36SMatthew G. Knepley   } else {
505334aa8a36SMatthew G. Knepley     PetscInt       Nf;
505434aa8a36SMatthew G. Knepley 
5055*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetNumFields(dm, &Nf));
50567a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
505734aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
505834aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
505934aa8a36SMatthew G. Knepley   }
506034aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
506134aa8a36SMatthew G. Knepley }
506234aa8a36SMatthew G. Knepley 
506334aa8a36SMatthew G. Knepley /*@
506434aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
506534aa8a36SMatthew G. Knepley 
506634aa8a36SMatthew G. Knepley   Not collective
506734aa8a36SMatthew G. Knepley 
506834aa8a36SMatthew G. Knepley   Input Parameters:
506934aa8a36SMatthew G. Knepley + dm         - The DM object
507034aa8a36SMatthew G. Knepley . f          - The field number
507134aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
507234aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
507334aa8a36SMatthew G. Knepley 
507434aa8a36SMatthew G. Knepley   Notes:
507534aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
507634aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
507734aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5078979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
507934aa8a36SMatthew G. Knepley 
508034aa8a36SMatthew G. Knepley   Level: developer
508134aa8a36SMatthew G. Knepley 
508234aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
508334aa8a36SMatthew G. Knepley @*/
508434aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
508534aa8a36SMatthew G. Knepley {
508634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
508734aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
508834aa8a36SMatthew G. Knepley   if (f < 0) {
508934aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
509034aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
509134aa8a36SMatthew G. Knepley   } else {
509234aa8a36SMatthew G. Knepley     PetscInt       Nf;
509334aa8a36SMatthew G. Knepley 
5094*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetNumFields(dm, &Nf));
50957a8be351SBarry Smith     PetscCheck(f < Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
509634aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
509734aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5098e5e52638SMatthew G. Knepley   }
5099e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5100e5e52638SMatthew G. Knepley }
5101e5e52638SMatthew G. Knepley 
5102b0441da4SMatthew G. Knepley /*@
5103b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5104b0441da4SMatthew G. Knepley 
5105b0441da4SMatthew G. Knepley   Not collective
5106b0441da4SMatthew G. Knepley 
5107f899ff85SJose E. Roman   Input Parameter:
5108b0441da4SMatthew G. Knepley . dm - The DM object
5109b0441da4SMatthew G. Knepley 
5110d8d19677SJose E. Roman   Output Parameters:
5111b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5112b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5113b0441da4SMatthew G. Knepley 
5114b0441da4SMatthew G. Knepley   Notes:
5115b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5116b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5117b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5118b0441da4SMatthew G. Knepley 
5119b0441da4SMatthew G. Knepley   Level: developer
5120b0441da4SMatthew G. Knepley 
5121b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
5122b0441da4SMatthew G. Knepley @*/
5123b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5124b0441da4SMatthew G. Knepley {
5125b0441da4SMatthew G. Knepley   PetscInt       Nf;
5126b0441da4SMatthew G. Knepley 
5127b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5128b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5129064a246eSJacob Faibussowitsch   if (useCone)    PetscValidBoolPointer(useCone, 2);
5130064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
5131*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumFields(dm, &Nf));
5132b0441da4SMatthew G. Knepley   if (!Nf) {
5133*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5134b0441da4SMatthew G. Knepley   } else {
5135*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetAdjacency(dm, 0, useCone, useClosure));
5136b0441da4SMatthew G. Knepley   }
5137b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5138b0441da4SMatthew G. Knepley }
5139b0441da4SMatthew G. Knepley 
5140b0441da4SMatthew G. Knepley /*@
5141b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5142b0441da4SMatthew G. Knepley 
5143b0441da4SMatthew G. Knepley   Not collective
5144b0441da4SMatthew G. Knepley 
5145b0441da4SMatthew G. Knepley   Input Parameters:
5146b0441da4SMatthew G. Knepley + dm         - The DM object
5147b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5148b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5149b0441da4SMatthew G. Knepley 
5150b0441da4SMatthew G. Knepley   Notes:
5151b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5152b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5153b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5154b0441da4SMatthew G. Knepley 
5155b0441da4SMatthew G. Knepley   Level: developer
5156b0441da4SMatthew G. Knepley 
5157b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
5158b0441da4SMatthew G. Knepley @*/
5159b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5160b0441da4SMatthew G. Knepley {
5161b0441da4SMatthew G. Knepley   PetscInt       Nf;
5162b0441da4SMatthew G. Knepley 
5163b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5164b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5165*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumFields(dm, &Nf));
5166b0441da4SMatthew G. Knepley   if (!Nf) {
5167*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5168b0441da4SMatthew G. Knepley   } else {
5169*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetAdjacency(dm, 0, useCone, useClosure));
5170e5e52638SMatthew G. Knepley   }
5171e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5172e5e52638SMatthew G. Knepley }
5173e5e52638SMatthew G. Knepley 
5174783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
517545480ffeSMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, DMLabel label)
5176783e2ec8SMatthew G. Knepley {
5177783e2ec8SMatthew G. Knepley   PetscObject    obj;
5178783e2ec8SMatthew G. Knepley   PetscClassId   id;
5179783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
5180783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
5181783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
5182783e2ec8SMatthew G. Knepley 
5183783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5184*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetField(dm, field, NULL, &obj));
5185*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetClassId(obj, &id));
5186783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
5187783e2ec8SMatthew G. Knepley   if (isFE && label) {
5188783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
5189*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSGetNumBoundary(ds, &Nbd));
5190783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
519145480ffeSMatthew G. Knepley       DMLabel l;
5192783e2ec8SMatthew G. Knepley 
5193*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSGetBoundary(ds, bd, NULL, NULL, NULL, &l, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
519445480ffeSMatthew G. Knepley       duplicate = l == label ? PETSC_TRUE : PETSC_FALSE;
5195783e2ec8SMatthew G. Knepley       if (duplicate) break;
5196783e2ec8SMatthew G. Knepley     }
5197783e2ec8SMatthew G. Knepley     if (!duplicate) {
5198783e2ec8SMatthew G. Knepley       DM plex;
5199783e2ec8SMatthew G. Knepley 
5200*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMConvert(dm, DMPLEX, &plex));
5201*5f80ce2aSJacob Faibussowitsch       if (plex) CHKERRQ(DMPlexLabelComplete(plex, label));
5202*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMDestroy(&plex));
5203783e2ec8SMatthew G. Knepley     }
5204783e2ec8SMatthew G. Knepley   }
5205783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5206783e2ec8SMatthew G. Knepley }
5207783e2ec8SMatthew G. Knepley 
5208e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5209e5e52638SMatthew G. Knepley {
5210e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5211e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5212e5e52638SMatthew G. Knepley 
5213e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5214e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5215*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(NdsNew, &tmpd));
5216e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5217b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5218*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(dm->probs));
5219e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5220e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5221e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5222e5e52638SMatthew G. Knepley }
5223e5e52638SMatthew G. Knepley 
5224e5e52638SMatthew G. Knepley /*@
5225e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5226e5e52638SMatthew G. Knepley 
5227e5e52638SMatthew G. Knepley   Not collective
5228e5e52638SMatthew G. Knepley 
5229e5e52638SMatthew G. Knepley   Input Parameter:
5230e5e52638SMatthew G. Knepley . dm - The DM
5231e5e52638SMatthew G. Knepley 
5232e5e52638SMatthew G. Knepley   Output Parameter:
5233e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5234e5e52638SMatthew G. Knepley 
5235e5e52638SMatthew G. Knepley   Level: intermediate
5236e5e52638SMatthew G. Knepley 
5237e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5238e5e52638SMatthew G. Knepley @*/
5239e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5240e5e52638SMatthew G. Knepley {
5241e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5242e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5243534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5244e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5245e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5246e5e52638SMatthew G. Knepley }
5247e5e52638SMatthew G. Knepley 
5248e5e52638SMatthew G. Knepley /*@
5249e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5250e5e52638SMatthew G. Knepley 
5251d083f849SBarry Smith   Logically collective on dm
5252e5e52638SMatthew G. Knepley 
5253e5e52638SMatthew G. Knepley   Input Parameter:
5254e5e52638SMatthew G. Knepley . dm - The DM
5255e5e52638SMatthew G. Knepley 
5256e5e52638SMatthew G. Knepley   Level: intermediate
5257e5e52638SMatthew G. Knepley 
5258e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5259e5e52638SMatthew G. Knepley @*/
5260e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5261e5e52638SMatthew G. Knepley {
5262e5e52638SMatthew G. Knepley   PetscInt       s;
5263e5e52638SMatthew G. Knepley 
5264e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5265e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5266e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5267*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSDestroy(&dm->probs[s].ds));
5268*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelDestroy(&dm->probs[s].label));
5269*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&dm->probs[s].fields));
5270e5e52638SMatthew G. Knepley   }
5271*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(dm->probs));
5272e5e52638SMatthew G. Knepley   dm->probs = NULL;
5273e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5274e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5275e5e52638SMatthew G. Knepley }
5276e5e52638SMatthew G. Knepley 
5277e5e52638SMatthew G. Knepley /*@
5278e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5279e5e52638SMatthew G. Knepley 
5280e5e52638SMatthew G. Knepley   Not collective
5281e5e52638SMatthew G. Knepley 
5282e5e52638SMatthew G. Knepley   Input Parameter:
5283e5e52638SMatthew G. Knepley . dm    - The DM
5284e5e52638SMatthew G. Knepley 
5285e5e52638SMatthew G. Knepley   Output Parameter:
5286e5e52638SMatthew G. Knepley . prob - The default PetscDS
5287e5e52638SMatthew G. Knepley 
5288e5e52638SMatthew G. Knepley   Level: intermediate
5289e5e52638SMatthew G. Knepley 
5290e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5291e5e52638SMatthew G. Knepley @*/
5292e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5293e5e52638SMatthew G. Knepley {
5294e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5295e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5296e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5297b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5298b0143b4dSMatthew G. Knepley     PetscDS ds;
5299b0143b4dSMatthew G. Knepley 
5300*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSCreate(PETSC_COMM_SELF, &ds));
5301*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetRegionDS(dm, NULL, NULL, ds));
5302*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSDestroy(&ds));
5303b0143b4dSMatthew G. Knepley   }
5304b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5305e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5306e5e52638SMatthew G. Knepley }
5307e5e52638SMatthew G. Knepley 
5308e5e52638SMatthew G. Knepley /*@
5309e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5310e5e52638SMatthew G. Knepley 
5311e5e52638SMatthew G. Knepley   Not collective
5312e5e52638SMatthew G. Knepley 
5313e5e52638SMatthew G. Knepley   Input Parameters:
5314e5e52638SMatthew G. Knepley + dm    - The DM
5315e5e52638SMatthew G. Knepley - point - Cell for the DS
5316e5e52638SMatthew G. Knepley 
5317e5e52638SMatthew G. Knepley   Output Parameter:
5318e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5319e5e52638SMatthew G. Knepley 
5320e5e52638SMatthew G. Knepley   Level: developer
5321e5e52638SMatthew G. Knepley 
5322b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5323e5e52638SMatthew G. Knepley @*/
5324e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5325e5e52638SMatthew G. Knepley {
5326e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5327e5e52638SMatthew G. Knepley   PetscInt       s;
5328e5e52638SMatthew G. Knepley 
5329e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5330e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5331e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
53327a8be351SBarry Smith   PetscCheck(point >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5333e5e52638SMatthew G. Knepley   *prob = NULL;
5334e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5335e5e52638SMatthew G. Knepley     PetscInt val;
5336e5e52638SMatthew G. Knepley 
5337e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5338e5e52638SMatthew G. Knepley     else {
5339*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetValue(dm->probs[s].label, point, &val));
5340e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5341e5e52638SMatthew G. Knepley     }
5342e5e52638SMatthew G. Knepley   }
5343e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5344e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5345e5e52638SMatthew G. Knepley }
5346e5e52638SMatthew G. Knepley 
5347e5e52638SMatthew G. Knepley /*@
5348e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5349e5e52638SMatthew G. Knepley 
5350e5e52638SMatthew G. Knepley   Not collective
5351e5e52638SMatthew G. Knepley 
5352e5e52638SMatthew G. Knepley   Input Parameters:
5353e5e52638SMatthew G. Knepley + dm    - The DM
5354e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5355e5e52638SMatthew G. Knepley 
5356b3cf3223SMatthew G. Knepley   Output Parameters:
5357b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5358b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5359e5e52638SMatthew G. Knepley 
5360154ca461SJed Brown   Note:
5361154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5362154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5363154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5364e5e52638SMatthew G. Knepley 
5365e5e52638SMatthew G. Knepley   Level: advanced
5366e5e52638SMatthew G. Knepley 
5367e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5368e5e52638SMatthew G. Knepley @*/
5369b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5370e5e52638SMatthew G. Knepley {
5371e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5372e5e52638SMatthew G. Knepley 
5373e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5374e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5375e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5376b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5377b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5378e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5379154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5380b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5381b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5382154ca461SJed Brown       if (dm->probs[s].label) PetscFunctionReturn(0);
5383b3cf3223SMatthew G. Knepley     }
5384e5e52638SMatthew G. Knepley   }
53852df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5386e5e52638SMatthew G. Knepley }
5387e5e52638SMatthew G. Knepley 
5388e5e52638SMatthew G. Knepley /*@
5389083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5390083401c6SMatthew G. Knepley 
5391083401c6SMatthew G. Knepley   Collective on dm
5392083401c6SMatthew G. Knepley 
5393083401c6SMatthew G. Knepley   Input Parameters:
5394083401c6SMatthew G. Knepley + dm     - The DM
5395083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5396083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5397083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5398083401c6SMatthew G. Knepley 
5399083401c6SMatthew 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,
5400083401c6SMatthew G. Knepley   the fields argument is ignored.
5401083401c6SMatthew G. Knepley 
5402083401c6SMatthew G. Knepley   Level: advanced
5403083401c6SMatthew G. Knepley 
5404083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5405083401c6SMatthew G. Knepley @*/
5406083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5407083401c6SMatthew G. Knepley {
5408083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5409083401c6SMatthew G. Knepley 
5410083401c6SMatthew G. Knepley   PetscFunctionBegin;
5411083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5412083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5413064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5414083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5415083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5416*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSDestroy(&dm->probs[s].ds));
5417083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5418083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5419083401c6SMatthew G. Knepley     }
5420083401c6SMatthew G. Knepley   }
5421*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDSEnlarge_Static(dm, Nds+1));
5422*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) label));
5423*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) fields));
5424*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) ds));
5425083401c6SMatthew G. Knepley   if (!label) {
5426083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5427083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5428083401c6SMatthew G. Knepley     Nds = 0;
5429083401c6SMatthew G. Knepley   }
5430083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5431083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5432083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5433083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5434083401c6SMatthew G. Knepley }
5435083401c6SMatthew G. Knepley 
5436083401c6SMatthew G. Knepley /*@
5437e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5438e5e52638SMatthew G. Knepley 
5439e5e52638SMatthew G. Knepley   Not collective
5440e5e52638SMatthew G. Knepley 
5441e5e52638SMatthew G. Knepley   Input Parameters:
5442e5e52638SMatthew G. Knepley + dm  - The DM
5443e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5444e5e52638SMatthew G. Knepley 
5445e5e52638SMatthew G. Knepley   Output Parameters:
5446b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5447b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5448083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5449e5e52638SMatthew G. Knepley 
5450e5e52638SMatthew G. Knepley   Level: advanced
5451e5e52638SMatthew G. Knepley 
5452e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5453e5e52638SMatthew G. Knepley @*/
5454b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5455e5e52638SMatthew G. Knepley {
5456e5e52638SMatthew G. Knepley   PetscInt       Nds;
5457e5e52638SMatthew G. Knepley 
5458e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5459e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5460*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumDS(dm, &Nds));
54617a8be351SBarry Smith   PetscCheck((num >= 0) && (num < Nds),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
5462e5e52638SMatthew G. Knepley   if (label) {
5463e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5464e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5465e5e52638SMatthew G. Knepley   }
5466b3cf3223SMatthew G. Knepley   if (fields) {
5467b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5468b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5469b3cf3223SMatthew G. Knepley   }
5470e5e52638SMatthew G. Knepley   if (ds) {
5471b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5472e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5473e5e52638SMatthew G. Knepley   }
5474e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5475e5e52638SMatthew G. Knepley }
5476e5e52638SMatthew G. Knepley 
5477e5e52638SMatthew G. Knepley /*@
5478083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5479e5e52638SMatthew G. Knepley 
5480083401c6SMatthew G. Knepley   Not collective
5481e5e52638SMatthew G. Knepley 
5482e5e52638SMatthew G. Knepley   Input Parameters:
5483e5e52638SMatthew G. Knepley + dm     - The DM
5484083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5485083401c6SMatthew G. Knepley . label  - The region label, or NULL
5486083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5487083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5488e5e52638SMatthew G. Knepley 
5489e5e52638SMatthew G. Knepley   Level: advanced
5490e5e52638SMatthew G. Knepley 
5491083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5492e5e52638SMatthew G. Knepley @*/
5493083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5494e5e52638SMatthew G. Knepley {
5495083401c6SMatthew G. Knepley   PetscInt       Nds;
5496e5e52638SMatthew G. Knepley 
5497e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5498e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5499083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5500*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumDS(dm, &Nds));
55017a8be351SBarry Smith   PetscCheck((num >= 0) && (num < Nds),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
5502*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) label));
5503*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelDestroy(&dm->probs[num].label));
5504083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5505083401c6SMatthew G. Knepley   if (fields) {
5506083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5507*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectReference((PetscObject) fields));
5508*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&dm->probs[num].fields));
5509083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5510e5e52638SMatthew G. Knepley   }
5511083401c6SMatthew G. Knepley   if (ds) {
5512083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5513*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectReference((PetscObject) ds));
5514*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSDestroy(&dm->probs[num].ds));
5515083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5516083401c6SMatthew G. Knepley   }
5517e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5518e5e52638SMatthew G. Knepley }
5519e5e52638SMatthew G. Knepley 
5520e5e52638SMatthew G. Knepley /*@
55211d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
55221d3af9e0SMatthew G. Knepley 
55231d3af9e0SMatthew G. Knepley   Not collective
55241d3af9e0SMatthew G. Knepley 
55251d3af9e0SMatthew G. Knepley   Input Parameters:
55261d3af9e0SMatthew G. Knepley + dm  - The DM
55271d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
55281d3af9e0SMatthew G. Knepley 
55291d3af9e0SMatthew G. Knepley   Output Parameter:
55301d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
55311d3af9e0SMatthew G. Knepley 
55321d3af9e0SMatthew G. Knepley   Level: advanced
55331d3af9e0SMatthew G. Knepley 
55341d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
55351d3af9e0SMatthew G. Knepley @*/
55361d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
55371d3af9e0SMatthew G. Knepley {
55381d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
55391d3af9e0SMatthew G. Knepley 
55401d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
55411d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55421d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
55431d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
5544*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumDS(dm, &Nds));
55451d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
55461d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
55471d3af9e0SMatthew G. Knepley   else          *num = n;
55481d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
55491d3af9e0SMatthew G. Knepley }
55501d3af9e0SMatthew G. Knepley 
55512df84da0SMatthew G. Knepley /*@C
55522df84da0SMatthew G. Knepley   DMCreateFEDefault - Create a PetscFE based on the celltype for the mesh
55532df84da0SMatthew G. Knepley 
55542df84da0SMatthew G. Knepley   Not collective
55552df84da0SMatthew G. Knepley 
5556f1a722f8SMatthew G. Knepley   Input Parameters:
55572df84da0SMatthew G. Knepley + dm     - The DM
55582df84da0SMatthew G. Knepley . Nc     - The number of components for the field
55592df84da0SMatthew G. Knepley . prefix - The options prefix for the output PetscFE, or NULL
55602df84da0SMatthew G. Knepley - qorder - The quadrature order or PETSC_DETERMINE to use PetscSpace polynomial degree
55612df84da0SMatthew G. Knepley 
55622df84da0SMatthew G. Knepley   Output Parameter:
55632df84da0SMatthew G. Knepley . fem - The PetscFE
55642df84da0SMatthew G. Knepley 
55652df84da0SMatthew G. Knepley   Note: This is a convenience method that just calls PetscFECreateByCell() underneath.
55662df84da0SMatthew G. Knepley 
55672df84da0SMatthew G. Knepley   Level: intermediate
55682df84da0SMatthew G. Knepley 
55692df84da0SMatthew G. Knepley .seealso: PetscFECreateByCell(), DMAddField(), DMCreateDS(), DMGetCellDS(), DMGetRegionDS()
55702df84da0SMatthew G. Knepley @*/
55712df84da0SMatthew G. Knepley PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
55722df84da0SMatthew G. Knepley {
55732df84da0SMatthew G. Knepley   DMPolytopeType ct;
55742df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
55752df84da0SMatthew G. Knepley 
55762df84da0SMatthew G. Knepley   PetscFunctionBegin;
55772df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55782df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
55792df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
55802df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
55812df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
5582*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
5583*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
5584*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetCellType(dm, cStart, &ct));
5585*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
55862df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
55872df84da0SMatthew G. Knepley }
55882df84da0SMatthew G. Knepley 
55891d3af9e0SMatthew G. Knepley /*@
5590e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5591e5e52638SMatthew G. Knepley 
5592d083f849SBarry Smith   Collective on dm
5593e5e52638SMatthew G. Knepley 
5594e5e52638SMatthew G. Knepley   Input Parameter:
5595e5e52638SMatthew G. Knepley . dm - The DM
5596e5e52638SMatthew G. Knepley 
559745480ffeSMatthew G. Knepley   Options Database Keys:
559845480ffeSMatthew G. Knepley . -dm_petscds_view - View all the PetscDS objects in this DM
559945480ffeSMatthew G. Knepley 
5600e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5601e5e52638SMatthew G. Knepley 
5602e5e52638SMatthew G. Knepley   Level: intermediate
5603e5e52638SMatthew G. Knepley 
5604e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5605e5e52638SMatthew G. Knepley @*/
5606e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5607e5e52638SMatthew G. Knepley {
5608e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5609083401c6SMatthew G. Knepley   PetscDS        dsDef;
5610083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5611f9244615SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5612f9244615SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE, flg;
5613e5e52638SMatthew G. Knepley 
5614e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5615e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5616e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5617*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetComm((PetscObject) dm, &comm));
5618*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDim(dm, &dE));
5619083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5620*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(Nf, &labelSet));
5621083401c6SMatthew G. Knepley   Nl   = 0;
5622083401c6SMatthew G. Knepley   Ndef = 0;
5623083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5624083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5625083401c6SMatthew G. Knepley     PetscInt l;
5626083401c6SMatthew G. Knepley 
5627f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5628f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5629f918ec44SMatthew G. Knepley     {
5630f918ec44SMatthew G. Knepley       PetscClassId id;
5631f918ec44SMatthew G. Knepley 
5632*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscObjectGetClassId(dm->fields[f].disc, &id));
5633f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5634f918ec44SMatthew G. Knepley         Ceed ceed;
5635f918ec44SMatthew G. Knepley 
5636*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMGetCeed(dm, &ceed));
5637*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscFESetCeed((PetscFE) dm->fields[f].disc, ceed));
5638f918ec44SMatthew G. Knepley       }
5639f918ec44SMatthew G. Knepley     }
5640f918ec44SMatthew G. Knepley #endif
5641083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5642083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5643083401c6SMatthew G. Knepley     if (l < Nl) continue;
5644083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5645083401c6SMatthew G. Knepley   }
5646083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5647*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5648083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5649b3cf3223SMatthew G. Knepley     IS        fields;
5650b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5651b3cf3223SMatthew G. Knepley 
5652b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
56537a8be351SBarry Smith     PetscCheck(nf,comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
5654*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(nf, &fld));
5655b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
5656*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreate(PETSC_COMM_SELF, &fields));
5657*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_"));
5658*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISSetType(fields, ISGENERAL));
5659*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
566088f0c812SMatthew G. Knepley 
5661*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
5662*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetRegionDS(dm, NULL, fields, dsDef));
5663*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSDestroy(&dsDef));
5664*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&fields));
56652df9ee95SMatthew G. Knepley   }
5666*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5667*5f80ce2aSJacob Faibussowitsch   if (dsDef) CHKERRQ(PetscDSSetCoordinateDimension(dsDef, dE));
5668083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5669083401c6SMatthew G. Knepley   if (Ndef && Nl) {
56700122748bSMatthew G. Knepley     DM              plex;
5671083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5672083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5673083401c6SMatthew G. Knepley     PetscInt       *fields;
5674083401c6SMatthew G. Knepley     const PetscInt *cells;
5675083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
56760122748bSMatthew G. Knepley 
5677*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMConvert(dm, DMPLEX, &plex));
5678*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetDepth(plex, &depth));
5679*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetStratumIS(plex, "dim", depth, &allcellIS));
5680*5f80ce2aSJacob Faibussowitsch     if (!allcellIS) CHKERRQ(DMGetStratumIS(plex, "depth", depth, &allcellIS));
56815fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5682083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5683083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5684083401c6SMatthew G. Knepley       IS      pointIS;
5685083401c6SMatthew G. Knepley 
5686*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISDestroy(&defcellIS));
5687*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetStratumIS(label, 1, &pointIS));
5688*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISDifference(allcellIS, pointIS, &defcellIS));
5689*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISDestroy(&pointIS));
5690083401c6SMatthew G. Knepley     }
5691*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&allcellIS));
5692083401c6SMatthew G. Knepley 
5693*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
5694*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(defcellIS, &n));
5695*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(defcellIS, &cells));
5696*5f80ce2aSJacob Faibussowitsch     for (c = 0; c < n; ++c) CHKERRQ(DMLabelSetValue(cellLabel, cells[c], 1));
5697*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(defcellIS, &cells));
5698*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&defcellIS));
5699*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexLabelComplete(plex, cellLabel));
5700083401c6SMatthew G. Knepley 
5701*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(Ndef, &fields));
5702083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5703*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreate(PETSC_COMM_SELF, &fieldIS));
5704*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_"));
5705*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISSetType(fieldIS, ISGENERAL));
5706*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5707083401c6SMatthew G. Knepley 
5708*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
5709*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef));
5710*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSSetCoordinateDimension(dsDef, dE));
5711*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelDestroy(&cellLabel));
5712*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSDestroy(&dsDef));
5713*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&fieldIS));
5714*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMDestroy(&plex));
5715083401c6SMatthew G. Knepley   }
5716083401c6SMatthew G. Knepley   /* Create label DSes
5717083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5718083401c6SMatthew G. Knepley   */
5719083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5720083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5721083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5722083401c6SMatthew G. Knepley     PetscDS   ds;
5723083401c6SMatthew G. Knepley     IS        fields;
5724083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5725083401c6SMatthew G. Knepley 
5726*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSCreate(PETSC_COMM_SELF, &ds));
5727083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5728*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(nf, &fld));
5729083401c6SMatthew G. Knepley     for (f = 0, nf  = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5730*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreate(PETSC_COMM_SELF, &fields));
5731*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_"));
5732*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISSetType(fields, ISGENERAL));
5733*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
5734*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetRegionDS(dm, label, fields, ds));
5735*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&fields));
5736*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSSetCoordinateDimension(ds, dE));
5737083401c6SMatthew G. Knepley     {
5738083401c6SMatthew G. Knepley       DMPolytopeType ct;
5739083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
57405fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
57410122748bSMatthew G. Knepley 
5742*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetBounds(label, &lStart, &lEnd));
5743665f567fSMatthew G. Knepley       if (lStart >= 0) {
5744*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetCellType(dm, lStart, &ct));
5745412e9a14SMatthew G. Knepley         switch (ct) {
5746412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5747412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5748412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5749412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
57505fedec97SMatthew G. Knepley             isCohesiveLocal = PETSC_TRUE;break;
5751083401c6SMatthew G. Knepley           default: break;
5752412e9a14SMatthew G. Knepley         }
5753665f567fSMatthew G. Knepley       }
5754*5f80ce2aSJacob Faibussowitsch       CHKERRMPI(MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
57555fedec97SMatthew G. Knepley       for (f = 0, nf  = 0; f < Nf; ++f) {
57565fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
57575fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
5758*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscDSSetDiscretization(ds, nf, NULL));
5759*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscDSSetCohesive(ds, nf, isCohesive));
57605fedec97SMatthew G. Knepley           }
57615fedec97SMatthew G. Knepley           ++nf;
57625fedec97SMatthew G. Knepley         }
57635fedec97SMatthew G. Knepley       }
5764e5e52638SMatthew G. Knepley     }
5765*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSDestroy(&ds));
5766e5e52638SMatthew G. Knepley   }
5767*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(labelSet));
5768e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5769083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5770083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5771083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5772083401c6SMatthew G. Knepley     const PetscInt *fld;
57735fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
57745fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5775e5e52638SMatthew G. Knepley 
5776*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSGetNumFields(ds, &dsnf));
5777*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSIsCohesive(ds, &isCohesive));
5778*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(fields, &nf));
5779*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(fields, &fld));
5780083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5781083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
57825fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5783e5e52638SMatthew G. Knepley       PetscClassId id;
5784e5e52638SMatthew G. Knepley 
57855fedec97SMatthew G. Knepley       /* Handle DS with no fields */
5786*5f80ce2aSJacob Faibussowitsch       if (dsnf) CHKERRQ(PetscDSGetCohesive(ds, f, &isCohesiveField));
57875fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
5788*5f80ce2aSJacob Faibussowitsch       if (isCohesive && !isCohesiveField) CHKERRQ(PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc));
5789*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetDiscretization(ds, f, disc));
5790083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5791*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscObjectGetClassId(disc, &id));
5792e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5793e5e52638SMatthew G. Knepley     }
5794*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(fields, &fld));
5795e5e52638SMatthew G. Knepley   }
5796f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
5797*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetInt(NULL, ((PetscObject) dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5798f9244615SMatthew G. Knepley   if (flg) {
57993b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
58003b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
58013b4aee56SMatthew G. Knepley       PetscInt Nf, f;
58023b4aee56SMatthew G. Knepley 
5803*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSGetNumFields(ds, &Nf));
5804*5f80ce2aSJacob Faibussowitsch       for (f = 0; f < Nf; ++f) CHKERRQ(PetscDSSetJetDegree(ds, f, k));
58053b4aee56SMatthew G. Knepley     }
5806f9244615SMatthew G. Knepley   }
5807e5e52638SMatthew G. Knepley   /* Setup DSes */
5808e5e52638SMatthew G. Knepley   if (doSetup) {
5809*5f80ce2aSJacob Faibussowitsch     for (s = 0; s < dm->Nds; ++s) CHKERRQ(PetscDSSetUp(dm->probs[s].ds));
5810e5e52638SMatthew G. Knepley   }
5811e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5812e5e52638SMatthew G. Knepley }
5813e5e52638SMatthew G. Knepley 
5814e5e52638SMatthew G. Knepley /*@
58157f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
58167f96f943SMatthew G. Knepley 
5817f2cacb80SMatthew G. Knepley   Collective on DM
5818f2cacb80SMatthew G. Knepley 
58197f96f943SMatthew G. Knepley   Input Parameters:
58207f96f943SMatthew G. Knepley + dm   - The DM
58217f96f943SMatthew G. Knepley - time - The time
58227f96f943SMatthew G. Knepley 
58237f96f943SMatthew G. Knepley   Output Parameters:
5824f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5825f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
58267f96f943SMatthew G. Knepley 
58277f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
58287f96f943SMatthew G. Knepley 
58297f96f943SMatthew G. Knepley   Level: developer
58307f96f943SMatthew G. Knepley 
58317f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
58327f96f943SMatthew G. Knepley @*/
5833f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
58347f96f943SMatthew G. Knepley {
58357f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
58367f96f943SMatthew G. Knepley   void            **ectxs;
58377f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
58387f96f943SMatthew G. Knepley 
58397f96f943SMatthew G. Knepley   PetscFunctionBegin;
5840f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5841f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5842f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
5843*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumFields(dm, &Nf));
5844*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
5845*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumDS(dm, &Nds));
58467f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
58477f96f943SMatthew G. Knepley     PetscDS         ds;
58487f96f943SMatthew G. Knepley     DMLabel         label;
58497f96f943SMatthew G. Knepley     IS              fieldIS;
58507f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
58517f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
58527f96f943SMatthew G. Knepley 
5853*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
5854*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSGetNumFields(ds, &dsNf));
5855*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(fieldIS, &fields));
5856*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscArrayzero(exacts, Nf));
5857*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscArrayzero(ectxs, Nf));
5858f2cacb80SMatthew G. Knepley     if (u) {
58597f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
58607f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
5861*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]));
58627f96f943SMatthew G. Knepley       }
5863*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISRestoreIndices(fieldIS, &fields));
58647f96f943SMatthew G. Knepley       if (label) {
5865*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u));
58667f96f943SMatthew G. Knepley       } else {
5867*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u));
58687f96f943SMatthew G. Knepley       }
58697f96f943SMatthew G. Knepley     }
5870f2cacb80SMatthew G. Knepley     if (u_t) {
5871*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscArrayzero(exacts, Nf));
5872*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscArrayzero(ectxs, Nf));
5873f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5874f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5875*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]));
5876f2cacb80SMatthew G. Knepley       }
5877*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISRestoreIndices(fieldIS, &fields));
5878f2cacb80SMatthew G. Knepley       if (label) {
5879*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5880f2cacb80SMatthew G. Knepley       } else {
5881*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t));
5882f2cacb80SMatthew G. Knepley       }
5883f2cacb80SMatthew G. Knepley     }
5884f2cacb80SMatthew G. Knepley   }
5885f2cacb80SMatthew G. Knepley   if (u) {
5886*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject) u, "Exact Solution"));
5887*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetOptionsPrefix((PetscObject) u, "exact_"));
5888f2cacb80SMatthew G. Knepley   }
5889f2cacb80SMatthew G. Knepley   if (u_t) {
5890*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative"));
5891*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_"));
5892f2cacb80SMatthew G. Knepley   }
5893*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree2(exacts, ectxs));
58947f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
58957f96f943SMatthew G. Knepley }
58967f96f943SMatthew G. Knepley 
589745480ffeSMatthew G. Knepley PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
589845480ffeSMatthew G. Knepley {
589945480ffeSMatthew G. Knepley   PetscDS        dsNew;
590045480ffeSMatthew G. Knepley   DSBoundary     b;
590145480ffeSMatthew G. Knepley   PetscInt       cdim, Nf, f;
59025fedec97SMatthew G. Knepley   PetscBool      isCohesive;
590345480ffeSMatthew G. Knepley   void          *ctx;
590445480ffeSMatthew G. Knepley 
590545480ffeSMatthew G. Knepley   PetscFunctionBegin;
5906*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSCreate(PetscObjectComm((PetscObject) ds), &dsNew));
5907*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSCopyConstants(ds, dsNew));
5908*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSCopyExactSolutions(ds, dsNew));
5909*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew));
5910*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSCopyEquations(ds, dsNew));
5911*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSGetNumFields(ds, &Nf));
591245480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5913*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSGetContext(ds, f, &ctx));
5914*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSSetContext(dsNew, f, ctx));
5915*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSGetCohesive(ds, f, &isCohesive));
5916*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSSetCohesive(dsNew, f, isCohesive));
591745480ffeSMatthew G. Knepley   }
591845480ffeSMatthew G. Knepley   if (Nf) {
5919*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSGetCoordinateDimension(ds, &cdim));
5920*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSSetCoordinateDimension(dsNew, cdim));
592145480ffeSMatthew G. Knepley   }
5922*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew));
592345480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
5924*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabel(dm, b->lname, &b->label));
592545480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
59267a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
592745480ffeSMatthew G. Knepley   }
592845480ffeSMatthew G. Knepley 
5929*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetRegionDS(dm, label, fields, dsNew));
5930*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSDestroy(&dsNew));
593145480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
593245480ffeSMatthew G. Knepley }
593345480ffeSMatthew G. Knepley 
59347f96f943SMatthew G. Knepley /*@
5935e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5936e5e52638SMatthew G. Knepley 
5937d083f849SBarry Smith   Collective on dm
5938e5e52638SMatthew G. Knepley 
5939e5e52638SMatthew G. Knepley   Input Parameter:
5940e5e52638SMatthew G. Knepley . dm - The DM
5941e5e52638SMatthew G. Knepley 
5942e5e52638SMatthew G. Knepley   Output Parameter:
5943e5e52638SMatthew G. Knepley . newdm - The DM
5944e5e52638SMatthew G. Knepley 
5945e5e52638SMatthew G. Knepley   Level: advanced
5946e5e52638SMatthew G. Knepley 
5947e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5948e5e52638SMatthew G. Knepley @*/
5949e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5950e5e52638SMatthew G. Knepley {
5951e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5952e5e52638SMatthew G. Knepley 
5953e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5954e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5955*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumDS(dm, &Nds));
5956*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMClearDS(newdm));
5957e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5958e5e52638SMatthew G. Knepley     DMLabel  label;
5959b3cf3223SMatthew G. Knepley     IS       fields;
596045480ffeSMatthew G. Knepley     PetscDS  ds, newds;
5961783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5962e5e52638SMatthew G. Knepley 
5963*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetRegionNumDS(dm, s, &label, &fields, &ds));
5964b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
5965*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMTransferDS_Internal(newdm, label, fields, ds));
596645480ffeSMatthew G. Knepley     /* Commplete new labels in the new DS */
5967*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetRegionDS(newdm, label, NULL, &newds));
5968*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSGetNumBoundary(newds, &Nbd));
5969783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5970b8025e53SMatthew G. Knepley       PetscWeakForm wf;
597145480ffeSMatthew G. Knepley       DMLabel       label;
5972783e2ec8SMatthew G. Knepley       PetscInt      field;
5973783e2ec8SMatthew G. Knepley 
5974*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
5975*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMCompleteBoundaryLabel_Internal(newdm, newds, field, bd, label));
5976*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscWeakFormReplaceLabel(wf, label));
5977783e2ec8SMatthew G. Knepley     }
5978e5e52638SMatthew G. Knepley   }
5979e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5980e5e52638SMatthew G. Knepley }
5981e5e52638SMatthew G. Knepley 
5982e5e52638SMatthew G. Knepley /*@
5983e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5984e5e52638SMatthew G. Knepley 
5985d083f849SBarry Smith   Collective on dm
5986e5e52638SMatthew G. Knepley 
5987e5e52638SMatthew G. Knepley   Input Parameter:
5988e5e52638SMatthew G. Knepley . dm - The DM
5989e5e52638SMatthew G. Knepley 
5990e5e52638SMatthew G. Knepley   Output Parameter:
5991e5e52638SMatthew G. Knepley . newdm - The DM
5992e5e52638SMatthew G. Knepley 
5993e5e52638SMatthew G. Knepley   Level: advanced
5994e5e52638SMatthew G. Knepley 
5995e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5996e5e52638SMatthew G. Knepley @*/
5997e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5998e5e52638SMatthew G. Knepley {
5999e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6000*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCopyFields(dm, newdm));
6001*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCopyDS(dm, newdm));
6002e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6003e5e52638SMatthew G. Knepley }
6004e5e52638SMatthew G. Knepley 
6005b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
6006b64e0483SPeter Brune {
6007b64e0483SPeter Brune   DM dm_coord,dmc_coord;
6008b64e0483SPeter Brune   Vec coords,ccoords;
60096dbf9973SLawrence Mitchell   Mat inject;
6010b64e0483SPeter Brune   PetscFunctionBegin;
6011*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDM(dm,&dm_coord));
6012*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDM(dmc,&dmc_coord));
6013*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinates(dm,&coords));
6014*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinates(dmc,&ccoords));
6015b64e0483SPeter Brune   if (coords && !ccoords) {
6016*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateGlobalVector(dmc_coord,&ccoords));
6017*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject)ccoords,"coordinates"));
6018*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateInjection(dmc_coord,dm_coord,&inject));
6019*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatRestrict(inject,coords,ccoords));
6020*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatDestroy(&inject));
6021*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetCoordinates(dmc,ccoords));
6022*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecDestroy(&ccoords));
6023b64e0483SPeter Brune   }
6024b64e0483SPeter Brune   PetscFunctionReturn(0);
6025b64e0483SPeter Brune }
6026b64e0483SPeter Brune 
602703dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
602803dadc2fSPeter Brune {
602903dadc2fSPeter Brune   DM dm_coord,subdm_coord;
603003dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
603103dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
603203dadc2fSPeter Brune   PetscFunctionBegin;
6033*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDM(dm,&dm_coord));
6034*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDM(subdm,&subdm_coord));
6035*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinates(dm,&coords));
6036*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinates(subdm,&ccoords));
603703dadc2fSPeter Brune   if (coords && !ccoords) {
6038*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateGlobalVector(subdm_coord,&ccoords));
6039*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject)ccoords,"coordinates"));
6040*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateLocalVector(subdm_coord,&clcoords));
6041*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject)clcoords,"coordinates"));
6042*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g));
6043*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD));
6044*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD));
6045*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD));
6046*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD));
6047*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetCoordinates(subdm,ccoords));
6048*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetCoordinatesLocal(subdm,clcoords));
6049*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecScatterDestroy(&scat_i[0]));
6050*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecScatterDestroy(&scat_g[0]));
6051*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecDestroy(&ccoords));
6052*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecDestroy(&clcoords));
6053*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(scat_i));
6054*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(scat_g));
605503dadc2fSPeter Brune   }
605603dadc2fSPeter Brune   PetscFunctionReturn(0);
605703dadc2fSPeter Brune }
605803dadc2fSPeter Brune 
6059c73cfb54SMatthew G. Knepley /*@
6060c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
6061c73cfb54SMatthew G. Knepley 
6062c73cfb54SMatthew G. Knepley   Not collective
6063c73cfb54SMatthew G. Knepley 
6064c73cfb54SMatthew G. Knepley   Input Parameter:
6065c73cfb54SMatthew G. Knepley . dm - The DM
6066c73cfb54SMatthew G. Knepley 
6067c73cfb54SMatthew G. Knepley   Output Parameter:
6068c73cfb54SMatthew G. Knepley . dim - The topological dimension
6069c73cfb54SMatthew G. Knepley 
6070c73cfb54SMatthew G. Knepley   Level: beginner
6071c73cfb54SMatthew G. Knepley 
6072c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
6073c73cfb54SMatthew G. Knepley @*/
6074c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6075c73cfb54SMatthew G. Knepley {
6076c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6077c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6078534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6079c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6080c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6081c73cfb54SMatthew G. Knepley }
6082c73cfb54SMatthew G. Knepley 
6083c73cfb54SMatthew G. Knepley /*@
6084c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
6085c73cfb54SMatthew G. Knepley 
6086c73cfb54SMatthew G. Knepley   Collective on dm
6087c73cfb54SMatthew G. Knepley 
6088c73cfb54SMatthew G. Knepley   Input Parameters:
6089c73cfb54SMatthew G. Knepley + dm - The DM
6090c73cfb54SMatthew G. Knepley - dim - The topological dimension
6091c73cfb54SMatthew G. Knepley 
6092c73cfb54SMatthew G. Knepley   Level: beginner
6093c73cfb54SMatthew G. Knepley 
6094c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
6095c73cfb54SMatthew G. Knepley @*/
6096c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6097c73cfb54SMatthew G. Knepley {
6098e5e52638SMatthew G. Knepley   PetscDS        ds;
609945480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6100f17e8794SMatthew G. Knepley 
6101c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6102c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6103c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6104c73cfb54SMatthew G. Knepley   dm->dim = dim;
6105d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
6106*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetNumDS(dm, &Nds));
610745480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
6108*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
6109*5f80ce2aSJacob Faibussowitsch       if (ds->dimEmbed < 0) CHKERRQ(PetscDSSetCoordinateDimension(ds, dim));
611045480ffeSMatthew G. Knepley     }
6111d17bd122SMatthew G. Knepley   }
6112c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6113c73cfb54SMatthew G. Knepley }
6114c73cfb54SMatthew G. Knepley 
6115793f3fe5SMatthew G. Knepley /*@
6116793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6117793f3fe5SMatthew G. Knepley 
6118d083f849SBarry Smith   Collective on dm
6119793f3fe5SMatthew G. Knepley 
6120793f3fe5SMatthew G. Knepley   Input Parameters:
6121793f3fe5SMatthew G. Knepley + dm - the DM
6122793f3fe5SMatthew G. Knepley - dim - the dimension
6123793f3fe5SMatthew G. Knepley 
6124793f3fe5SMatthew G. Knepley   Output Parameters:
6125793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6126aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6127793f3fe5SMatthew G. Knepley 
6128793f3fe5SMatthew G. Knepley   Note:
6129793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6130a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6131793f3fe5SMatthew G. Knepley   then the interval is empty.
6132793f3fe5SMatthew G. Knepley 
6133793f3fe5SMatthew G. Knepley   Level: intermediate
6134793f3fe5SMatthew G. Knepley 
6135793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
6136793f3fe5SMatthew G. Knepley @*/
6137793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6138793f3fe5SMatthew G. Knepley {
6139793f3fe5SMatthew G. Knepley   PetscInt       d;
6140793f3fe5SMatthew G. Knepley 
6141793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6142793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6143*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &d));
61447a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
61457a8be351SBarry Smith   PetscCheck(dm->ops->getdimpoints,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
6146*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->getdimpoints)(dm, dim, pStart, pEnd));
6147793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6148793f3fe5SMatthew G. Knepley }
6149793f3fe5SMatthew G. Knepley 
61506636e97aSMatthew G Knepley /*@
61516636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
61526636e97aSMatthew G Knepley 
6153d083f849SBarry Smith   Collective on dm
61546636e97aSMatthew G Knepley 
61556636e97aSMatthew G Knepley   Input Parameters:
61566636e97aSMatthew G Knepley + dm - the DM
61576636e97aSMatthew G Knepley - c - coordinate vector
61586636e97aSMatthew G Knepley 
61592dd40e9bSPatrick Sanan   Notes:
61602dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
61612dd40e9bSPatrick Sanan 
61622dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
61636636e97aSMatthew G Knepley 
61646636e97aSMatthew G Knepley   Level: intermediate
61656636e97aSMatthew G Knepley 
616660c22052SBarry Smith .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
61676636e97aSMatthew G Knepley @*/
61686636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
61696636e97aSMatthew G Knepley {
61706636e97aSMatthew G Knepley   PetscFunctionBegin;
61716636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61726636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
6173*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) c));
6174*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&dm->coordinates));
61756636e97aSMatthew G Knepley   dm->coordinates = c;
6176*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&dm->coordinatesLocal));
6177*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL));
6178*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL));
61796636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61806636e97aSMatthew G Knepley }
61816636e97aSMatthew G Knepley 
61826636e97aSMatthew G Knepley /*@
61836636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
61846636e97aSMatthew G Knepley 
61857058e716SVaclav Hapla   Not collective
61866636e97aSMatthew G Knepley 
61876636e97aSMatthew G Knepley    Input Parameters:
61886636e97aSMatthew G Knepley +  dm - the DM
61896636e97aSMatthew G Knepley -  c - coordinate vector
61906636e97aSMatthew G Knepley 
61912dd40e9bSPatrick Sanan   Notes:
61926636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
61936636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
61946636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
61956636e97aSMatthew G Knepley 
61962dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
61972dd40e9bSPatrick Sanan 
61986636e97aSMatthew G Knepley   Level: intermediate
61996636e97aSMatthew G Knepley 
62006636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
62016636e97aSMatthew G Knepley @*/
62026636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
62036636e97aSMatthew G Knepley {
62046636e97aSMatthew G Knepley   PetscFunctionBegin;
62056636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6206b7f5c055SJed Brown   if (c) PetscValidHeaderSpecific(c,VEC_CLASSID,2);
6207*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) c));
6208*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&dm->coordinatesLocal));
62098865f1eaSKarl Rupp 
62106636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
62118865f1eaSKarl Rupp 
6212*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&dm->coordinates));
62136636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62146636e97aSMatthew G Knepley }
62156636e97aSMatthew G Knepley 
62166636e97aSMatthew G Knepley /*@
62176636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
62186636e97aSMatthew G Knepley 
6219d083f849SBarry Smith   Collective on dm
62206636e97aSMatthew G Knepley 
62216636e97aSMatthew G Knepley   Input Parameter:
62226636e97aSMatthew G Knepley . dm - the DM
62236636e97aSMatthew G Knepley 
62246636e97aSMatthew G Knepley   Output Parameter:
62256636e97aSMatthew G Knepley . c - global coordinate vector
62266636e97aSMatthew G Knepley 
62276636e97aSMatthew G Knepley   Note:
622860c22052SBarry Smith   This is a borrowed reference, so the user should NOT destroy this vector. When the DM is
622960c22052SBarry Smith   destroyed the array will no longer be valid.
62306636e97aSMatthew G Knepley 
6231959c7569SPatrick Sanan   Each process has only the locally-owned portion of the global coordinates (does NOT have the ghost coordinates).
62326636e97aSMatthew G Knepley 
62336636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
62346636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
62356636e97aSMatthew G Knepley 
62366636e97aSMatthew G Knepley   Level: intermediate
62376636e97aSMatthew G Knepley 
623860c22052SBarry Smith .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMDASetUniformCoordinates()
62396636e97aSMatthew G Knepley @*/
62406636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
62416636e97aSMatthew G Knepley {
62426636e97aSMatthew G Knepley   PetscFunctionBegin;
62436636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62446636e97aSMatthew G Knepley   PetscValidPointer(c,2);
62451f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
62460298fd71SBarry Smith     DM        cdm = NULL;
62471970a576SMatthew G. Knepley     PetscBool localized;
62486636e97aSMatthew G Knepley 
6249*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinateDM(dm, &cdm));
6250*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateGlobalVector(cdm, &dm->coordinates));
6251*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinatesLocalized(dm, &localized));
62521970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
62531970a576SMatthew G. Knepley     if (localized) {
62541970a576SMatthew G. Knepley       PetscInt cdim;
62551970a576SMatthew G. Knepley 
6256*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetCoordinateDim(dm, &cdim));
6257*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecSetBlockSize(dm->coordinates, cdim));
62581970a576SMatthew G. Knepley     }
6259*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject) dm->coordinates, "coordinates"));
6260*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates));
6261*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates));
62626636e97aSMatthew G Knepley   }
62636636e97aSMatthew G Knepley   *c = dm->coordinates;
62646636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62656636e97aSMatthew G Knepley }
62666636e97aSMatthew G Knepley 
62676636e97aSMatthew G Knepley /*@
626881e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
626981e9a530SVaclav Hapla 
6270d083f849SBarry Smith   Collective on dm
627181e9a530SVaclav Hapla 
627281e9a530SVaclav Hapla   Input Parameter:
627381e9a530SVaclav Hapla . dm - the DM
627481e9a530SVaclav Hapla 
627581e9a530SVaclav Hapla   Level: advanced
627681e9a530SVaclav Hapla 
627781e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
627881e9a530SVaclav Hapla @*/
627981e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
628081e9a530SVaclav Hapla {
628181e9a530SVaclav Hapla   PetscFunctionBegin;
628281e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
628381e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
62841970a576SMatthew G. Knepley     DM        cdm = NULL;
62851970a576SMatthew G. Knepley     PetscBool localized;
62861970a576SMatthew G. Knepley 
6287*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinateDM(dm, &cdm));
6288*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateLocalVector(cdm, &dm->coordinatesLocal));
6289*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinatesLocalized(dm, &localized));
62901970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
62911970a576SMatthew G. Knepley     if (localized) {
62921970a576SMatthew G. Knepley       PetscInt cdim;
62931970a576SMatthew G. Knepley 
6294*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetCoordinateDim(dm, &cdim));
6295*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecSetBlockSize(dm->coordinates, cdim));
62961970a576SMatthew G. Knepley     }
6297*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates"));
6298*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal));
6299*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal));
630081e9a530SVaclav Hapla   }
630181e9a530SVaclav Hapla   PetscFunctionReturn(0);
630281e9a530SVaclav Hapla }
630381e9a530SVaclav Hapla 
630481e9a530SVaclav Hapla /*@
63056636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
63066636e97aSMatthew G Knepley 
6307d083f849SBarry Smith   Collective on dm
63086636e97aSMatthew G Knepley 
63096636e97aSMatthew G Knepley   Input Parameter:
63106636e97aSMatthew G Knepley . dm - the DM
63116636e97aSMatthew G Knepley 
63126636e97aSMatthew G Knepley   Output Parameter:
63136636e97aSMatthew G Knepley . c - coordinate vector
63146636e97aSMatthew G Knepley 
63156636e97aSMatthew G Knepley   Note:
63166636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
63176636e97aSMatthew G Knepley 
63186636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
63196636e97aSMatthew G Knepley 
63206636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
63216636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
63226636e97aSMatthew G Knepley 
63236636e97aSMatthew G Knepley   Level: intermediate
63246636e97aSMatthew G Knepley 
632581e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
63266636e97aSMatthew G Knepley @*/
63276636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
63286636e97aSMatthew G Knepley {
63296636e97aSMatthew G Knepley   PetscFunctionBegin;
63306636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63316636e97aSMatthew G Knepley   PetscValidPointer(c,2);
6332*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinatesLocalSetUp(dm));
63336636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
63346636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63356636e97aSMatthew G Knepley }
63366636e97aSMatthew G Knepley 
633781e9a530SVaclav Hapla /*@
633881e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
633981e9a530SVaclav Hapla 
634081e9a530SVaclav Hapla   Not collective
634181e9a530SVaclav Hapla 
634281e9a530SVaclav Hapla   Input Parameter:
634381e9a530SVaclav Hapla . dm - the DM
634481e9a530SVaclav Hapla 
634581e9a530SVaclav Hapla   Output Parameter:
634681e9a530SVaclav Hapla . c - coordinate vector
634781e9a530SVaclav Hapla 
634881e9a530SVaclav Hapla   Level: advanced
634981e9a530SVaclav Hapla 
635081e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
635181e9a530SVaclav Hapla @*/
635281e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
635381e9a530SVaclav Hapla {
635481e9a530SVaclav Hapla   PetscFunctionBegin;
635581e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
635681e9a530SVaclav Hapla   PetscValidPointer(c,2);
63577a8be351SBarry Smith   PetscCheck(dm->coordinatesLocal || !dm->coordinates,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
63586636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
63596636e97aSMatthew G Knepley   PetscFunctionReturn(0);
63606636e97aSMatthew G Knepley }
63616636e97aSMatthew G Knepley 
63622db98f8dSVaclav Hapla /*@
63632db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
63642db98f8dSVaclav Hapla 
63652db98f8dSVaclav Hapla   Not collective
63662db98f8dSVaclav Hapla 
6367d8d19677SJose E. Roman   Input Parameters:
63682db98f8dSVaclav Hapla + dm - the DM
63692db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
63702db98f8dSVaclav Hapla 
6371d8d19677SJose E. Roman   Output Parameters:
63722db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
63732db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
63742db98f8dSVaclav Hapla 
63752db98f8dSVaclav Hapla   Note:
63762db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
63772db98f8dSVaclav Hapla 
63782db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
63792db98f8dSVaclav Hapla 
63802db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
63812db98f8dSVaclav Hapla 
63822db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
63832db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
63842db98f8dSVaclav Hapla 
63852db98f8dSVaclav Hapla   Level: advanced
63862db98f8dSVaclav Hapla 
63872db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
63882db98f8dSVaclav Hapla @*/
63892db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
63902db98f8dSVaclav Hapla {
63912db98f8dSVaclav Hapla   PetscSection        cs, newcs;
63922db98f8dSVaclav Hapla   Vec                 coords;
63932db98f8dSVaclav Hapla   const PetscScalar   *arr;
63942db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
63952db98f8dSVaclav Hapla   PetscInt            n;
63962db98f8dSVaclav Hapla 
63972db98f8dSVaclav Hapla   PetscFunctionBegin;
63982db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63992db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
64002db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
64012db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
64027a8be351SBarry Smith   PetscCheck(dm->coordinatesLocal,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
64037a8be351SBarry Smith   PetscCheck(dm->coordinateDM && dm->coordinateDM->localSection,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
64041bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
64052db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
6406*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArrayRead(coords, &arr));
6407*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL));
6408*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArrayRead(coords, &arr));
64092db98f8dSVaclav Hapla   if (pCoord) {
6410*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetStorageSize(newcs, &n));
64112db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
6412*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord));
6413*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecReplaceArray(*pCoord, newarr));
6414ad9ac99dSVaclav Hapla   } else {
6415*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(newarr));
64162db98f8dSVaclav Hapla   }
6417ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6418*5f80ce2aSJacob Faibussowitsch   else               CHKERRQ(PetscSectionDestroy(&newcs));
64192db98f8dSVaclav Hapla   PetscFunctionReturn(0);
64202db98f8dSVaclav Hapla }
64212db98f8dSVaclav Hapla 
6422f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6423f19dbd58SToby Isaac {
6424f19dbd58SToby Isaac   PetscFunctionBegin;
6425f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6426f19dbd58SToby Isaac   PetscValidPointer(field,2);
6427f19dbd58SToby Isaac   if (!dm->coordinateField) {
6428f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6429*5f80ce2aSJacob Faibussowitsch       CHKERRQ((*dm->ops->createcoordinatefield)(dm,&dm->coordinateField));
6430f19dbd58SToby Isaac     }
6431f19dbd58SToby Isaac   }
6432f19dbd58SToby Isaac   *field = dm->coordinateField;
6433f19dbd58SToby Isaac   PetscFunctionReturn(0);
6434f19dbd58SToby Isaac }
6435f19dbd58SToby Isaac 
6436f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6437f19dbd58SToby Isaac {
6438f19dbd58SToby Isaac   PetscFunctionBegin;
6439f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6440f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6441*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject)field));
6442*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMFieldDestroy(&dm->coordinateField));
6443f19dbd58SToby Isaac   dm->coordinateField = field;
6444f19dbd58SToby Isaac   PetscFunctionReturn(0);
6445f19dbd58SToby Isaac }
6446f19dbd58SToby Isaac 
64476636e97aSMatthew G Knepley /*@
64481cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
64496636e97aSMatthew G Knepley 
6450d083f849SBarry Smith   Collective on dm
64516636e97aSMatthew G Knepley 
64526636e97aSMatthew G Knepley   Input Parameter:
64536636e97aSMatthew G Knepley . dm - the DM
64546636e97aSMatthew G Knepley 
64556636e97aSMatthew G Knepley   Output Parameter:
64566636e97aSMatthew G Knepley . cdm - coordinate DM
64576636e97aSMatthew G Knepley 
64586636e97aSMatthew G Knepley   Level: intermediate
64596636e97aSMatthew G Knepley 
64601cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
64616636e97aSMatthew G Knepley @*/
64626636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
64636636e97aSMatthew G Knepley {
64646636e97aSMatthew G Knepley   PetscFunctionBegin;
64656636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64666636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
64676636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6468308f8a94SToby Isaac     DM cdm;
6469308f8a94SToby Isaac 
64707a8be351SBarry Smith     PetscCheck(dm->ops->createcoordinatedm,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6471*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->ops->createcoordinatedm)(dm, &cdm));
6472*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject)cdm, "coordinateDM"));
6473308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6474308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6475*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMDestroy(&dm->coordinateDM));
6476308f8a94SToby Isaac     dm->coordinateDM = cdm;
64776636e97aSMatthew G Knepley   }
64786636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
64796636e97aSMatthew G Knepley   PetscFunctionReturn(0);
64806636e97aSMatthew G Knepley }
6481e87bb0d3SMatthew G Knepley 
64821cfe2091SMatthew G. Knepley /*@
64831cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
64841cfe2091SMatthew G. Knepley 
6485d083f849SBarry Smith   Logically Collective on dm
64861cfe2091SMatthew G. Knepley 
64871cfe2091SMatthew G. Knepley   Input Parameters:
64881cfe2091SMatthew G. Knepley + dm - the DM
64891cfe2091SMatthew G. Knepley - cdm - coordinate DM
64901cfe2091SMatthew G. Knepley 
64911cfe2091SMatthew G. Knepley   Level: intermediate
64921cfe2091SMatthew G. Knepley 
64931cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
64941cfe2091SMatthew G. Knepley @*/
64951cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
64961cfe2091SMatthew G. Knepley {
64971cfe2091SMatthew G. Knepley   PetscFunctionBegin;
64981cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64991cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6500*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject)cdm));
6501*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&dm->coordinateDM));
65021cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
65031cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
65041cfe2091SMatthew G. Knepley }
65051cfe2091SMatthew G. Knepley 
650646e270d4SMatthew G. Knepley /*@
650746e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
650846e270d4SMatthew G. Knepley 
650946e270d4SMatthew G. Knepley   Not Collective
651046e270d4SMatthew G. Knepley 
651146e270d4SMatthew G. Knepley   Input Parameter:
651246e270d4SMatthew G. Knepley . dm - The DM object
651346e270d4SMatthew G. Knepley 
651446e270d4SMatthew G. Knepley   Output Parameter:
651546e270d4SMatthew G. Knepley . dim - The embedding dimension
651646e270d4SMatthew G. Knepley 
651746e270d4SMatthew G. Knepley   Level: intermediate
651846e270d4SMatthew G. Knepley 
651992fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
652046e270d4SMatthew G. Knepley @*/
652146e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
652246e270d4SMatthew G. Knepley {
652346e270d4SMatthew G. Knepley   PetscFunctionBegin;
652446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6525534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
65269a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
65279a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
65289a9a41abSToby Isaac   }
652946e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
653046e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
653146e270d4SMatthew G. Knepley }
653246e270d4SMatthew G. Knepley 
653346e270d4SMatthew G. Knepley /*@
653446e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
653546e270d4SMatthew G. Knepley 
653646e270d4SMatthew G. Knepley   Not Collective
653746e270d4SMatthew G. Knepley 
653846e270d4SMatthew G. Knepley   Input Parameters:
653946e270d4SMatthew G. Knepley + dm  - The DM object
654046e270d4SMatthew G. Knepley - dim - The embedding dimension
654146e270d4SMatthew G. Knepley 
654246e270d4SMatthew G. Knepley   Level: intermediate
654346e270d4SMatthew G. Knepley 
654492fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
654546e270d4SMatthew G. Knepley @*/
654646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
654746e270d4SMatthew G. Knepley {
6548e5e52638SMatthew G. Knepley   PetscDS        ds;
654945480ffeSMatthew G. Knepley   PetscInt       Nds, n;
6550f17e8794SMatthew G. Knepley 
655146e270d4SMatthew G. Knepley   PetscFunctionBegin;
655246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
655346e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6554d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
6555*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetNumDS(dm, &Nds));
655645480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
6557*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
6558*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSSetCoordinateDimension(ds, dim));
655945480ffeSMatthew G. Knepley     }
6560d17bd122SMatthew G. Knepley   }
656146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
656246e270d4SMatthew G. Knepley }
656346e270d4SMatthew G. Knepley 
6564e8abe2deSMatthew G. Knepley /*@
6565e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6566e8abe2deSMatthew G. Knepley 
6567d083f849SBarry Smith   Collective on dm
6568e8abe2deSMatthew G. Knepley 
6569e8abe2deSMatthew G. Knepley   Input Parameter:
6570e8abe2deSMatthew G. Knepley . dm - The DM object
6571e8abe2deSMatthew G. Knepley 
6572e8abe2deSMatthew G. Knepley   Output Parameter:
6573e8abe2deSMatthew G. Knepley . section - The PetscSection object
6574e8abe2deSMatthew G. Knepley 
6575e8abe2deSMatthew G. Knepley   Level: intermediate
6576e8abe2deSMatthew G. Knepley 
657792fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6578e8abe2deSMatthew G. Knepley @*/
6579e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6580e8abe2deSMatthew G. Knepley {
6581e8abe2deSMatthew G. Knepley   DM             cdm;
6582e8abe2deSMatthew G. Knepley 
6583e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6584e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6585e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6586*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDM(dm, &cdm));
6587*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalSection(cdm, section));
6588e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6589e8abe2deSMatthew G. Knepley }
6590e8abe2deSMatthew G. Knepley 
6591e8abe2deSMatthew G. Knepley /*@
6592e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6593e8abe2deSMatthew G. Knepley 
6594e8abe2deSMatthew G. Knepley   Not Collective
6595e8abe2deSMatthew G. Knepley 
6596e8abe2deSMatthew G. Knepley   Input Parameters:
6597e8abe2deSMatthew G. Knepley + dm      - The DM object
659846e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6599e8abe2deSMatthew G. Knepley - section - The PetscSection object
6600e8abe2deSMatthew G. Knepley 
6601e8abe2deSMatthew G. Knepley   Level: intermediate
6602e8abe2deSMatthew G. Knepley 
660392fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6604e8abe2deSMatthew G. Knepley @*/
660546e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6606e8abe2deSMatthew G. Knepley {
6607e8abe2deSMatthew G. Knepley   DM             cdm;
6608e8abe2deSMatthew G. Knepley 
6609e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6610e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
661146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6612*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDM(dm, &cdm));
6613*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetLocalSection(cdm, section));
661446e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
66154c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
661646e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
661746e270d4SMatthew G. Knepley 
6618*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetChart(section, &pStart, &pEnd));
6619*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetDimPoints(dm, 0, &vStart, &vEnd));
662046e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
662146e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
662246e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
6623*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(section, v, &dd));
662446e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
662546e270d4SMatthew G. Knepley     }
6626*5f80ce2aSJacob Faibussowitsch     if (d >= 0) CHKERRQ(DMSetCoordinateDim(dm, d));
662746e270d4SMatthew G. Knepley   }
6628e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6629e8abe2deSMatthew G. Knepley }
6630e8abe2deSMatthew G. Knepley 
6631d864a3eaSLisandro Dalcin /*@
6632d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6633d864a3eaSLisandro Dalcin 
6634d864a3eaSLisandro Dalcin   Input Parameters:
6635d864a3eaSLisandro Dalcin + dm      - The DM object
66364f9ab2b4SJed Brown - disc    - The new coordinate discretization or NULL to ensure a coordinate discretization exists
6637d864a3eaSLisandro Dalcin 
6638d864a3eaSLisandro Dalcin   Level: intermediate
6639d864a3eaSLisandro Dalcin 
6640d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6641d864a3eaSLisandro Dalcin @*/
6642d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6643d864a3eaSLisandro Dalcin {
66444f9ab2b4SJed Brown   PetscFE        discOld;
6645d864a3eaSLisandro Dalcin   PetscClassId   classid;
6646d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6647d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6648d864a3eaSLisandro Dalcin   Mat            matInterp;
66494f9ab2b4SJed Brown   PetscBool      same_space = PETSC_TRUE;
6650d864a3eaSLisandro Dalcin 
6651d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6652d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
66534f9ab2b4SJed Brown   if (disc) PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6654d864a3eaSLisandro Dalcin 
6655*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDM(dm, &cdmOld));
6656d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
6657*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetField(cdmOld, 0, NULL, (PetscObject*)&discOld));
6658*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetClassId((PetscObject)discOld, &classid));
665929ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
666029ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
666129ad44c5SMatthew G. Knepley       PetscFE        feLinear;
666229ad44c5SMatthew G. Knepley       DMPolytopeType ct;
666329ad44c5SMatthew G. Knepley       PetscInt       dim, dE, cStart;
666429ad44c5SMatthew G. Knepley       PetscBool      simplex;
666529ad44c5SMatthew G. Knepley 
666629ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
6667*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetDimension(dm, &dim));
6668*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetCoordinateDim(dm, &dE));
6669*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetHeightStratum(cdmOld, 0, &cStart, NULL));
6670*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetCellType(dm, cStart, &ct));
667129ad44c5SMatthew G. Knepley       switch (ct) {
667229ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM:
667329ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
667429ad44c5SMatthew G. Knepley           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
667529ad44c5SMatthew G. Knepley         default: break;
667629ad44c5SMatthew G. Knepley       }
667729ad44c5SMatthew G. Knepley       simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
6678*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear));
6679*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear));
6680*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFEDestroy(&feLinear));
6681*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMCreateDS(cdmOld));
6682*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetField(cdmOld, 0, NULL, (PetscObject*)&discOld));
668329ad44c5SMatthew G. Knepley     } else {
668429ad44c5SMatthew G. Knepley       const char *discname;
668529ad44c5SMatthew G. Knepley 
6686*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscObjectGetType((PetscObject)discOld, &discname));
66874f9ab2b4SJed Brown       SETERRQ(PetscObjectComm((PetscObject)discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
668829ad44c5SMatthew G. Knepley     }
668929ad44c5SMatthew G. Knepley   }
66904f9ab2b4SJed Brown   if (!disc) PetscFunctionReturn(0);
66914f9ab2b4SJed Brown   { // Check if the new space is the same as the old modulo quadrature
66924f9ab2b4SJed Brown     PetscDualSpace dsOld, ds;
6693*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFEGetDualSpace(discOld, &dsOld));
6694*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFEGetDualSpace(disc, &ds));
6695*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDualSpaceEqual(dsOld, ds, &same_space));
66964f9ab2b4SJed Brown   }
6697d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6698*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMClone(cdmOld, &cdmNew));
6699*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetField(cdmNew, 0, NULL, (PetscObject) disc));
6700*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateDS(cdmNew));
6701*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinates(dm, &coordsOld));
67024f9ab2b4SJed Brown   if (same_space) {
6703*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectReference((PetscObject)coordsOld));
67044f9ab2b4SJed Brown     coordsNew = coordsOld;
67054f9ab2b4SJed Brown   } else { // Project the coordinate vector from old to new space
6706*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateGlobalVector(cdmNew, &coordsNew));
6707*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL));
6708*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatInterpolate(matInterp, coordsOld, coordsNew));
6709*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatDestroy(&matInterp));
67104f9ab2b4SJed Brown   }
6711d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6712*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetCoordinateField(dm, NULL));
6713*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetCoordinateDM(dm, cdmNew));
6714*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetCoordinates(dm, coordsNew));
6715*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&coordsNew));
6716*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&cdmNew));
6717d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6718d864a3eaSLisandro Dalcin }
6719d864a3eaSLisandro Dalcin 
67205dc8c3f7SMatthew G. Knepley /*@C
672190b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
67225dc8c3f7SMatthew G. Knepley 
6723f899ff85SJose E. Roman   Input Parameter:
672490b157c4SStefano Zampini . dm      - The DM object
672590b157c4SStefano Zampini 
672690b157c4SStefano Zampini   Output Parameters:
672790b157c4SStefano Zampini + per     - Whether the DM is periodic or not
67285dc8c3f7SMatthew 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
67295dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
67305dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
67315dc8c3f7SMatthew G. Knepley 
67325dc8c3f7SMatthew G. Knepley   Level: developer
67335dc8c3f7SMatthew G. Knepley 
67345dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
67355dc8c3f7SMatthew G. Knepley @*/
673690b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6737c6b900c6SMatthew G. Knepley {
6738c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6739c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
674090b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6741c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6742c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
67435dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6744c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6745c6b900c6SMatthew G. Knepley }
6746c6b900c6SMatthew G. Knepley 
67475dc8c3f7SMatthew G. Knepley /*@C
67485dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
67495dc8c3f7SMatthew G. Knepley 
67505dc8c3f7SMatthew G. Knepley   Input Parameters:
67515dc8c3f7SMatthew G. Knepley + dm      - The DM object
6752db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6753db2bf62eSStefano 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.
67545dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
67555dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
67565dc8c3f7SMatthew G. Knepley 
6757db2bf62eSStefano 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.
6758db2bf62eSStefano Zampini 
67595dc8c3f7SMatthew G. Knepley   Level: developer
67605dc8c3f7SMatthew G. Knepley 
67615dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
67625dc8c3f7SMatthew G. Knepley @*/
676390b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6764c6b900c6SMatthew G. Knepley {
6765c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6766c6b900c6SMatthew G. Knepley 
6767c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6768c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
676990b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6770412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6771412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6772412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
6773*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
677490b157c4SStefano Zampini   if (maxCell) {
6775*5f80ce2aSJacob Faibussowitsch     if (!dm->maxCell) CHKERRQ(PetscMalloc1(dim, &dm->maxCell));
6776412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6777db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6778*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(dm->maxCell));
6779412e9a14SMatthew G. Knepley   }
6780db2bf62eSStefano Zampini 
6781412e9a14SMatthew G. Knepley   if (L) {
6782*5f80ce2aSJacob Faibussowitsch     if (!dm->L) CHKERRQ(PetscMalloc1(dim, &dm->L));
6783412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6784412e9a14SMatthew G. Knepley   }
6785412e9a14SMatthew G. Knepley   if (bd) {
6786*5f80ce2aSJacob Faibussowitsch     if (!dm->bdtype) CHKERRQ(PetscMalloc1(dim, &dm->bdtype));
6787412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
678890b157c4SStefano Zampini   }
6789072d7d67SStefano Zampini   dm->periodic = per;
6790c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6791c6b900c6SMatthew G. Knepley }
6792c6b900c6SMatthew G. Knepley 
67932e17dfb7SMatthew G. Knepley /*@
67942e17dfb7SMatthew 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.
67952e17dfb7SMatthew G. Knepley 
67962e17dfb7SMatthew G. Knepley   Input Parameters:
67972e17dfb7SMatthew G. Knepley + dm     - The DM
679865da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
679965da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
68002e17dfb7SMatthew G. Knepley 
68012e17dfb7SMatthew G. Knepley   Output Parameter:
68022e17dfb7SMatthew G. Knepley . out - The localized coordinate point
68032e17dfb7SMatthew G. Knepley 
68042e17dfb7SMatthew G. Knepley   Level: developer
68052e17dfb7SMatthew G. Knepley 
68062e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
68072e17dfb7SMatthew G. Knepley @*/
680865da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
68092e17dfb7SMatthew G. Knepley {
68102e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
68112e17dfb7SMatthew G. Knepley 
68122e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
6813*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDim(dm, &dim));
68142e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68152e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68162e17dfb7SMatthew G. Knepley   } else {
681765da65dcSMatthew G. Knepley     if (endpoint) {
681865da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6819da3333bfSMatthew 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)) {
6820da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
682165da65dcSMatthew G. Knepley         } else {
6822da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
682365da65dcSMatthew G. Knepley         }
682465da65dcSMatthew G. Knepley       }
682565da65dcSMatthew G. Knepley     } else {
68262e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
68271118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
68282e17dfb7SMatthew G. Knepley       }
68292e17dfb7SMatthew G. Knepley     }
683065da65dcSMatthew G. Knepley   }
68312e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68322e17dfb7SMatthew G. Knepley }
68332e17dfb7SMatthew G. Knepley 
68342e17dfb7SMatthew G. Knepley /*
68352e17dfb7SMatthew 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.
68362e17dfb7SMatthew G. Knepley 
68372e17dfb7SMatthew G. Knepley   Input Parameters:
68382e17dfb7SMatthew G. Knepley + dm     - The DM
68392e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
68402e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
68412e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
68422e17dfb7SMatthew G. Knepley 
68432e17dfb7SMatthew G. Knepley   Output Parameter:
68442e17dfb7SMatthew G. Knepley . out - The localized coordinate point
68452e17dfb7SMatthew G. Knepley 
68462e17dfb7SMatthew G. Knepley   Level: developer
68472e17dfb7SMatthew G. Knepley 
68482e17dfb7SMatthew 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
68492e17dfb7SMatthew G. Knepley 
68502e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
68512e17dfb7SMatthew G. Knepley */
68522e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
68532e17dfb7SMatthew G. Knepley {
68542e17dfb7SMatthew G. Knepley   PetscInt d;
68552e17dfb7SMatthew G. Knepley 
68562e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68572e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68582e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68592e17dfb7SMatthew G. Knepley   } else {
68602e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6861908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
68622e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
68632e17dfb7SMatthew G. Knepley       } else {
68642e17dfb7SMatthew G. Knepley         out[d] = in[d];
68652e17dfb7SMatthew G. Knepley       }
68662e17dfb7SMatthew G. Knepley     }
68672e17dfb7SMatthew G. Knepley   }
68682e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68692e17dfb7SMatthew G. Knepley }
6870a5801f52SStefano Zampini 
68712e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
68722e17dfb7SMatthew G. Knepley {
68732e17dfb7SMatthew G. Knepley   PetscInt d;
68742e17dfb7SMatthew G. Knepley 
68752e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
68762e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
68772e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
68782e17dfb7SMatthew G. Knepley   } else {
68792e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6880908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
68812e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
68822e17dfb7SMatthew G. Knepley       } else {
68832e17dfb7SMatthew G. Knepley         out[d] = in[d];
68842e17dfb7SMatthew G. Knepley       }
68852e17dfb7SMatthew G. Knepley     }
68862e17dfb7SMatthew G. Knepley   }
68872e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68882e17dfb7SMatthew G. Knepley }
68892e17dfb7SMatthew G. Knepley 
68902e17dfb7SMatthew G. Knepley /*
68912e17dfb7SMatthew 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.
68922e17dfb7SMatthew G. Knepley 
68932e17dfb7SMatthew G. Knepley   Input Parameters:
68942e17dfb7SMatthew G. Knepley + dm     - The DM
68952e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
68962e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
68972e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
68982e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
68992e17dfb7SMatthew G. Knepley 
69002e17dfb7SMatthew G. Knepley   Output Parameter:
69012e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
69022e17dfb7SMatthew G. Knepley 
69032e17dfb7SMatthew G. Knepley   Level: developer
69042e17dfb7SMatthew G. Knepley 
69052e17dfb7SMatthew 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
69062e17dfb7SMatthew G. Knepley 
69072e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
69082e17dfb7SMatthew G. Knepley */
69092e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
69102e17dfb7SMatthew G. Knepley {
69112e17dfb7SMatthew G. Knepley   PetscInt d;
69122e17dfb7SMatthew G. Knepley 
69132e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
69142e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
69152e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
69162e17dfb7SMatthew G. Knepley   } else {
69172e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6918412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6919412e9a14SMatthew G. Knepley 
6920412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6921412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6922412e9a14SMatthew G. Knepley 
6923412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
692498921bdaSJacob 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]));
6925412e9a14SMatthew G. Knepley         out[d] += newCoord;
69262e17dfb7SMatthew G. Knepley       } else {
69272e17dfb7SMatthew G. Knepley         out[d] += in[d];
69282e17dfb7SMatthew G. Knepley       }
69292e17dfb7SMatthew G. Knepley     }
69302e17dfb7SMatthew G. Knepley   }
69312e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69322e17dfb7SMatthew G. Knepley }
69332e17dfb7SMatthew G. Knepley 
693436447a5eSToby Isaac /*@
69358f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
69368f700142SStefano Zampini 
69378f700142SStefano Zampini   Not collective
693836447a5eSToby Isaac 
693936447a5eSToby Isaac   Input Parameter:
694036447a5eSToby Isaac . dm - The DM
694136447a5eSToby Isaac 
694236447a5eSToby Isaac   Output Parameter:
694336447a5eSToby Isaac   areLocalized - True if localized
694436447a5eSToby Isaac 
694536447a5eSToby Isaac   Level: developer
694636447a5eSToby Isaac 
69478f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
694836447a5eSToby Isaac @*/
69498f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
695036447a5eSToby Isaac {
695136447a5eSToby Isaac   DM             cdm;
695236447a5eSToby Isaac   PetscSection   coordSection;
69532ccac677SMatthew G. Knepley   PetscInt       depth, cStart, cEnd, sStart, sEnd, c, dof;
695446a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
695536447a5eSToby Isaac 
695636447a5eSToby Isaac   PetscFunctionBegin;
695736447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6958534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
69598b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
696046a3a80fSLisandro Dalcin 
696136447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
6962*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDM(dm, &cdm));
6963*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex));
69649f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
6965*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepth(cdm, &depth));
69662ccac677SMatthew G. Knepley   if (!depth) PetscFunctionReturn(0);
696746a3a80fSLisandro Dalcin 
6968*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateSection(dm, &coordSection));
6969*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd));
6970*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(coordSection, &sStart, &sEnd));
697136447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
697246a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
697346a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
6974*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(coordSection, c, &dof));
697546a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
697636447a5eSToby Isaac   }
69778f700142SStefano Zampini   *areLocalized = alreadyLocalized;
697836447a5eSToby Isaac   PetscFunctionReturn(0);
697936447a5eSToby Isaac }
698036447a5eSToby Isaac 
69818f700142SStefano Zampini /*@
69828f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
69838f700142SStefano Zampini 
69848f700142SStefano Zampini   Collective on dm
69858f700142SStefano Zampini 
69868f700142SStefano Zampini   Input Parameter:
69878f700142SStefano Zampini . dm - The DM
69888f700142SStefano Zampini 
69898f700142SStefano Zampini   Output Parameter:
69908f700142SStefano Zampini   areLocalized - True if localized
69918f700142SStefano Zampini 
69928f700142SStefano Zampini   Level: developer
69938f700142SStefano Zampini 
69948f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
69958f700142SStefano Zampini @*/
69968f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
69978f700142SStefano Zampini {
69988f700142SStefano Zampini   PetscBool      localized;
69998f700142SStefano Zampini 
70008f700142SStefano Zampini   PetscFunctionBegin;
70018f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7002534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
7003*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinatesLocalizedLocal(dm,&localized));
7004*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm)));
70058f700142SStefano Zampini   PetscFunctionReturn(0);
70068f700142SStefano Zampini }
700736447a5eSToby Isaac 
70082e17dfb7SMatthew G. Knepley /*@
7009492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
70102e17dfb7SMatthew G. Knepley 
70118f700142SStefano Zampini   Collective on dm
70128f700142SStefano Zampini 
70132e17dfb7SMatthew G. Knepley   Input Parameter:
70142e17dfb7SMatthew G. Knepley . dm - The DM
70152e17dfb7SMatthew G. Knepley 
70162e17dfb7SMatthew G. Knepley   Level: developer
70172e17dfb7SMatthew G. Knepley 
70188f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
70192e17dfb7SMatthew G. Knepley @*/
70202e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
70212e17dfb7SMatthew G. Knepley {
70222e17dfb7SMatthew G. Knepley   DM             cdm;
70232e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
70242e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
70253e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
70263e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
7027e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
70283e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
70293e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
70302e17dfb7SMatthew G. Knepley 
70312e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
70322e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
703392c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
7034*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinatesLocalized(dm, &alreadyLocalized));
7035f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
7036f7cbd40bSStefano Zampini 
70372e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
7038*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDM(dm, &cdm));
70392e17dfb7SMatthew G. Knepley   {
70402e17dfb7SMatthew G. Knepley     PetscBool isplex;
70412e17dfb7SMatthew G. Knepley 
7042*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex));
70432e17dfb7SMatthew G. Knepley     if (isplex) {
7044*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd));
7045*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetMaxProjectionHeight(cdm,&maxHeight));
7046*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart));
70473e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
70483e922f36SToby Isaac       newStart = vStart;
70493e922f36SToby Isaac       newEnd   = vEnd;
70503e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
7051*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]));
70523e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
70533e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
70543e922f36SToby Isaac       }
70552e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
70562e17dfb7SMatthew G. Knepley   }
7057*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinatesLocal(dm, &coordinates));
70587a8be351SBarry Smith   PetscCheck(coordinates,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
7059*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateSection(dm, &coordSection));
7060*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetBlockSize(coordinates, &bs));
7061*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(coordSection,&sStart,&sEnd));
70623e922f36SToby Isaac 
7063*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection));
7064*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetNumFields(cSection, 1));
7065*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetFieldComponents(coordSection, 0, &Nc));
7066*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetFieldComponents(cSection, 0, Nc));
7067*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetChart(cSection, newStart, newEnd));
70683e922f36SToby Isaac 
7069*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor));
70703e922f36SToby Isaac   localized = &anchor[bs];
70713e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
70723e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
70733e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
70743e922f36SToby Isaac 
70753e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
70763e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
70773e922f36SToby Isaac       PetscInt     b;
70783e922f36SToby Isaac 
70793e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
7080*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords));
70813e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
70823e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
7083*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized));
70843e922f36SToby Isaac         for (b = 0; b < bs; b++) {
70853e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
70863e922f36SToby Isaac         }
70873e922f36SToby Isaac         if (b < bs) break;
70883e922f36SToby Isaac       }
70893e922f36SToby Isaac       if (d < dof/bs) {
70903e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
70913e922f36SToby Isaac           PetscInt cdof;
70923e922f36SToby Isaac 
7093*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscSectionGetDof(coordSection, c, &cdof));
70943e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
70953e922f36SToby Isaac         }
7096*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionSetDof(cSection, c, dof));
7097*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionSetFieldDof(cSection, c, 0, dof));
70983e922f36SToby Isaac       }
7099*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords));
71003e922f36SToby Isaac     }
71013e922f36SToby Isaac   }
7102*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm)));
71033e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
7104*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor));
7105*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionDestroy(&cSection));
7106*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart));
71073e922f36SToby Isaac     PetscFunctionReturn(0);
71083e922f36SToby Isaac   }
71092e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
7110*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(coordSection, v, &dof));
7111*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetDof(cSection, v, dof));
7112*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldDof(cSection, v, 0, dof));
71132e17dfb7SMatthew G. Knepley   }
7114*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(cSection));
7115*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetStorageSize(cSection, &coordSize));
7116*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecCreate(PETSC_COMM_SELF, &cVec));
7117*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject)cVec,"coordinates"));
7118*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSetBlockSize(cVec, bs));
7119*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSetSizes(cVec, coordSize, PETSC_DETERMINE));
7120*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSetType(cVec, VECSTANDARD));
7121*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArrayRead(coordinates, (const PetscScalar**)&coords));
7122*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArray(cVec, &coords2));
71232e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
7124*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(coordSection, v, &dof));
7125*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(coordSection, v, &off));
7126*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(cSection,     v, &off2));
71272e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
71282e17dfb7SMatthew G. Knepley   }
71293e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
71303e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
71313e922f36SToby Isaac 
71322e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
71332e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
71343e922f36SToby Isaac       PetscInt     b, cdof;
71352e17dfb7SMatthew G. Knepley 
7136*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(cSection,c,&cdof));
71373e922f36SToby Isaac       if (!cdof) continue;
7138*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords));
7139*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetOffset(cSection, c, &off2));
71402e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
7141*5f80ce2aSJacob Faibussowitsch       for (d = 0; d < dof/bs; ++d) CHKERRQ(DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]));
7142*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords));
71432e17dfb7SMatthew G. Knepley     }
71443e922f36SToby Isaac   }
7145*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor));
7146*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart));
7147*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords));
7148*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArray(cVec, &coords2));
7149*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection));
7150*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetCoordinatesLocal(dm, cVec));
7151*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&cVec));
7152*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionDestroy(&cSection));
71532e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
71542e17dfb7SMatthew G. Knepley }
71552e17dfb7SMatthew G. Knepley 
7156e87bb0d3SMatthew G Knepley /*@
71573a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
7158e87bb0d3SMatthew G Knepley 
7159d083f849SBarry Smith   Collective on v (see explanation below)
7160e87bb0d3SMatthew G Knepley 
7161e87bb0d3SMatthew G Knepley   Input Parameters:
7162e87bb0d3SMatthew G Knepley + dm - The DM
71636b867d5aSJose E. Roman - ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
7164e87bb0d3SMatthew G Knepley 
71656b867d5aSJose E. Roman   Input/Output Parameters:
71666b867d5aSJose E. Roman + v - The Vec of points, on output contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
71676b867d5aSJose E. Roman - cellSF - Points to either NULL, or a PetscSF with guesses for which cells contain each point;
71686b867d5aSJose E. Roman            on output, the PetscSF containing the ranks and local indices of the containing points
71693a93e3b7SToby Isaac 
7170e87bb0d3SMatthew G Knepley   Level: developer
717161e3bb9bSMatthew G Knepley 
717262a38674SMatthew G. Knepley   Notes:
71733a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
717462a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
71753a93e3b7SToby Isaac 
71763a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
717762a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
71783a93e3b7SToby Isaac 
71793a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
71803a93e3b7SToby Isaac 
718162a38674SMatthew G. Knepley $    const PetscSFNode *cells;
718262a38674SMatthew G. Knepley $    PetscInt           nFound;
7183a6216909SToby Isaac $    const PetscInt    *found;
718462a38674SMatthew G. Knepley $
7185a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
71863a93e3b7SToby Isaac 
71873a93e3b7SToby 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
71883a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
71893a93e3b7SToby Isaac 
719062a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
719161e3bb9bSMatthew G Knepley @*/
719262a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
7193e87bb0d3SMatthew G Knepley {
7194e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
7195e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7196e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
7197e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
71983a93e3b7SToby Isaac   if (*cellSF) {
71993a93e3b7SToby Isaac     PetscMPIInt result;
72003a93e3b7SToby Isaac 
7201e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
7202*5f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result));
72037a8be351SBarry Smith     PetscCheck(result == MPI_IDENT || result == MPI_CONGRUENT,PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"cellSF must have a communicator congruent to v's");
7204e0fc9d1bSMatthew G. Knepley   } else {
7205*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF));
72063a93e3b7SToby Isaac   }
72077a8be351SBarry Smith   PetscCheck(dm->ops->locatepoints,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
7208*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventBegin(DM_LocatePoints,dm,0,0,0));
7209*5f80ce2aSJacob Faibussowitsch   CHKERRQ((*dm->ops->locatepoints)(dm,v,ltype,*cellSF));
7210*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscLogEventEnd(DM_LocatePoints,dm,0,0,0));
7211e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
7212e87bb0d3SMatthew G Knepley }
721314f150ffSMatthew G. Knepley 
7214f4d763aaSMatthew G. Knepley /*@
7215f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
7216f4d763aaSMatthew G. Knepley 
72178f700142SStefano Zampini   Collective on dm
72188f700142SStefano Zampini 
7219f4d763aaSMatthew G. Knepley   Input Parameter:
7220f4d763aaSMatthew G. Knepley . dm - The original DM
7221f4d763aaSMatthew G. Knepley 
7222f4d763aaSMatthew G. Knepley   Output Parameter:
7223f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
7224f4d763aaSMatthew G. Knepley 
7225f4d763aaSMatthew G. Knepley   Level: intermediate
7226f4d763aaSMatthew G. Knepley 
7227e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
7228f4d763aaSMatthew G. Knepley @*/
722914f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
723014f150ffSMatthew G. Knepley {
7231c26acbdeSMatthew G. Knepley   PetscSection   section;
72322d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
723314f150ffSMatthew G. Knepley 
723414f150ffSMatthew G. Knepley   PetscFunctionBegin;
723514f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
723614f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
7237*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalSection(dm, &section));
7238*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionHasConstraints(section, &hasConstraints));
7239*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm)));
72402d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
7241c26acbdeSMatthew G. Knepley     *odm = dm;
7242c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
7243c26acbdeSMatthew G. Knepley   }
724414f150ffSMatthew G. Knepley   if (!dm->dmBC) {
7245c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
724614f150ffSMatthew G. Knepley     PetscSF      sf;
724714f150ffSMatthew G. Knepley 
7248*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMClone(dm, &dm->dmBC));
7249*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCopyDisc(dm, dm->dmBC));
7250*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionClone(section, &newSection));
7251*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetLocalSection(dm->dmBC, newSection));
7252*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionDestroy(&newSection));
7253*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetPointSF(dm->dmBC, &sf));
7254*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
7255*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetGlobalSection(dm->dmBC, gsection));
7256*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionDestroy(&gsection));
725714f150ffSMatthew G. Knepley   }
725814f150ffSMatthew G. Knepley   *odm = dm->dmBC;
725914f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
726014f150ffSMatthew G. Knepley }
7261f4d763aaSMatthew G. Knepley 
7262f4d763aaSMatthew G. Knepley /*@
7263cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7264f4d763aaSMatthew G. Knepley 
7265f4d763aaSMatthew G. Knepley   Input Parameter:
7266f4d763aaSMatthew G. Knepley . dm - The original DM
7267f4d763aaSMatthew G. Knepley 
7268cdb7a50dSMatthew G. Knepley   Output Parameters:
7269cdb7a50dSMatthew G. Knepley + num - The output sequence number
7270cdb7a50dSMatthew G. Knepley - val - The output sequence value
7271f4d763aaSMatthew G. Knepley 
7272f4d763aaSMatthew G. Knepley   Level: intermediate
7273f4d763aaSMatthew G. Knepley 
7274f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7275f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7276f4d763aaSMatthew G. Knepley 
7277f4d763aaSMatthew G. Knepley .seealso: VecView()
7278f4d763aaSMatthew G. Knepley @*/
7279cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7280f4d763aaSMatthew G. Knepley {
7281f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7282f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7283534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7284534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7285f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7286f4d763aaSMatthew G. Knepley }
7287f4d763aaSMatthew G. Knepley 
7288f4d763aaSMatthew G. Knepley /*@
7289cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7290f4d763aaSMatthew G. Knepley 
7291f4d763aaSMatthew G. Knepley   Input Parameters:
7292f4d763aaSMatthew G. Knepley + dm - The original DM
7293cdb7a50dSMatthew G. Knepley . num - The output sequence number
7294cdb7a50dSMatthew G. Knepley - val - The output sequence value
7295f4d763aaSMatthew G. Knepley 
7296f4d763aaSMatthew G. Knepley   Level: intermediate
7297f4d763aaSMatthew G. Knepley 
7298f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7299f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7300f4d763aaSMatthew G. Knepley 
7301f4d763aaSMatthew G. Knepley .seealso: VecView()
7302f4d763aaSMatthew G. Knepley @*/
7303cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7304f4d763aaSMatthew G. Knepley {
7305f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7306f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7307f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7308cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7309cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7310cdb7a50dSMatthew G. Knepley }
7311cdb7a50dSMatthew G. Knepley 
7312cdb7a50dSMatthew G. Knepley /*@C
7313cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7314cdb7a50dSMatthew G. Knepley 
7315cdb7a50dSMatthew G. Knepley   Input Parameters:
7316cdb7a50dSMatthew G. Knepley + dm   - The original DM
7317cdb7a50dSMatthew G. Knepley . name - The sequence name
7318cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7319cdb7a50dSMatthew G. Knepley 
7320cdb7a50dSMatthew G. Knepley   Output Parameter:
7321cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7322cdb7a50dSMatthew G. Knepley 
7323cdb7a50dSMatthew G. Knepley   Level: intermediate
7324cdb7a50dSMatthew G. Knepley 
7325cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7326cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7327cdb7a50dSMatthew G. Knepley 
7328cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7329cdb7a50dSMatthew G. Knepley @*/
7330cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7331cdb7a50dSMatthew G. Knepley {
7332cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7333cdb7a50dSMatthew G. Knepley 
7334cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7335cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7336cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7337064a246eSJacob Faibussowitsch   PetscValidRealPointer(val,5);
7338*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5));
7339cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7340cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7341cdb7a50dSMatthew G. Knepley     PetscScalar value;
7342cdb7a50dSMatthew G. Knepley 
7343*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
73444aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7345cdb7a50dSMatthew G. Knepley #endif
7346cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7347f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7348f4d763aaSMatthew G. Knepley }
73498e4ac7eaSMatthew G. Knepley 
73508e4ac7eaSMatthew G. Knepley /*@
73518e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
73528e4ac7eaSMatthew G. Knepley 
73538e4ac7eaSMatthew G. Knepley   Not collective
73548e4ac7eaSMatthew G. Knepley 
73558e4ac7eaSMatthew G. Knepley   Input Parameter:
73568e4ac7eaSMatthew G. Knepley . dm - The DM
73578e4ac7eaSMatthew G. Knepley 
73588e4ac7eaSMatthew G. Knepley   Output Parameter:
73598e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
73608e4ac7eaSMatthew G. Knepley 
73618e4ac7eaSMatthew G. Knepley   Level: beginner
73628e4ac7eaSMatthew G. Knepley 
73638e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
73648e4ac7eaSMatthew G. Knepley @*/
73658e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
73668e4ac7eaSMatthew G. Knepley {
73678e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
73688e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7369534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
73708e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
73718e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
73728e4ac7eaSMatthew G. Knepley }
73738e4ac7eaSMatthew G. Knepley 
73748e4ac7eaSMatthew G. Knepley /*@
73755d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
73768e4ac7eaSMatthew G. Knepley 
73778e4ac7eaSMatthew G. Knepley   Collective on dm
73788e4ac7eaSMatthew G. Knepley 
73798e4ac7eaSMatthew G. Knepley   Input Parameters:
73808e4ac7eaSMatthew G. Knepley + dm - The DM
73818e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
73828e4ac7eaSMatthew G. Knepley 
73835d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
73845d3b26e6SMatthew G. Knepley 
73858e4ac7eaSMatthew G. Knepley   Level: beginner
73868e4ac7eaSMatthew G. Knepley 
73875d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
73888e4ac7eaSMatthew G. Knepley @*/
73898e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
73908e4ac7eaSMatthew G. Knepley {
73918e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
73928e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73938833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
73948e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
73958e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
73968e4ac7eaSMatthew G. Knepley }
7397c58f1c22SToby Isaac 
7398c58f1c22SToby Isaac /*@C
7399c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7400c58f1c22SToby Isaac 
7401c58f1c22SToby Isaac   Not Collective
7402c58f1c22SToby Isaac 
7403c58f1c22SToby Isaac   Input Parameters:
7404c58f1c22SToby Isaac + dm   - The DM object
7405c58f1c22SToby Isaac - name - The label name
7406c58f1c22SToby Isaac 
7407c58f1c22SToby Isaac   Level: intermediate
7408c58f1c22SToby Isaac 
7409c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7410c58f1c22SToby Isaac @*/
7411c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7412c58f1c22SToby Isaac {
74135d80c0bfSVaclav Hapla   PetscBool      flg;
74145d80c0bfSVaclav Hapla   DMLabel        label;
7415c58f1c22SToby Isaac 
7416c58f1c22SToby Isaac   PetscFunctionBegin;
7417c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7418c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7419*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMHasLabel(dm, name, &flg));
7420c58f1c22SToby Isaac   if (!flg) {
7421*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelCreate(PETSC_COMM_SELF, name, &label));
7422*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMAddLabel(dm, label));
7423*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelDestroy(&label));
7424c58f1c22SToby Isaac   }
7425c58f1c22SToby Isaac   PetscFunctionReturn(0);
7426c58f1c22SToby Isaac }
7427c58f1c22SToby Isaac 
7428c58f1c22SToby Isaac /*@C
74290fdc7489SMatthew Knepley   DMCreateLabelAtIndex - Create a label of the given name at the iven index. If it already exists, move it to this index.
74300fdc7489SMatthew Knepley 
74310fdc7489SMatthew Knepley   Not Collective
74320fdc7489SMatthew Knepley 
74330fdc7489SMatthew Knepley   Input Parameters:
74340fdc7489SMatthew Knepley + dm   - The DM object
74350fdc7489SMatthew Knepley . l    - The index for the label
74360fdc7489SMatthew Knepley - name - The label name
74370fdc7489SMatthew Knepley 
74380fdc7489SMatthew Knepley   Level: intermediate
74390fdc7489SMatthew Knepley 
74400fdc7489SMatthew Knepley .seealso: DMCreateLabel(), DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
74410fdc7489SMatthew Knepley @*/
74420fdc7489SMatthew Knepley PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
74430fdc7489SMatthew Knepley {
74440fdc7489SMatthew Knepley   DMLabelLink    orig, prev = NULL;
74450fdc7489SMatthew Knepley   DMLabel        label;
74460fdc7489SMatthew Knepley   PetscInt       Nl, m;
74470fdc7489SMatthew Knepley   PetscBool      flg, match;
74480fdc7489SMatthew Knepley   const char    *lname;
74490fdc7489SMatthew Knepley 
74500fdc7489SMatthew Knepley   PetscFunctionBegin;
74510fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7452064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
7453*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMHasLabel(dm, name, &flg));
74540fdc7489SMatthew Knepley   if (!flg) {
7455*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelCreate(PETSC_COMM_SELF, name, &label));
7456*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMAddLabel(dm, label));
7457*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelDestroy(&label));
74580fdc7489SMatthew Knepley   }
7459*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumLabels(dm, &Nl));
74607a8be351SBarry Smith   PetscCheck(l < Nl,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %D must be in [0, %D)", l, Nl);
74610fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
7462*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectGetName((PetscObject) orig->label, &lname));
7463*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcmp(name, lname, &match));
74640fdc7489SMatthew Knepley     if (match) break;
74650fdc7489SMatthew Knepley   }
74660fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
74670fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
74680fdc7489SMatthew Knepley   else    prev->next = orig->next;
74690fdc7489SMatthew Knepley   if (!l) {
74700fdc7489SMatthew Knepley     orig->next = dm->labels;
74710fdc7489SMatthew Knepley     dm->labels = orig;
74720fdc7489SMatthew Knepley   } else {
74730fdc7489SMatthew Knepley     for (m = 0, prev = dm->labels; m < l-1; ++m, prev = prev->next);
74740fdc7489SMatthew Knepley     orig->next = prev->next;
74750fdc7489SMatthew Knepley     prev->next = orig;
74760fdc7489SMatthew Knepley   }
74770fdc7489SMatthew Knepley   PetscFunctionReturn(0);
74780fdc7489SMatthew Knepley }
74790fdc7489SMatthew Knepley 
74800fdc7489SMatthew Knepley /*@C
7481c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7482c58f1c22SToby Isaac 
7483c58f1c22SToby Isaac   Not Collective
7484c58f1c22SToby Isaac 
7485c58f1c22SToby Isaac   Input Parameters:
7486c58f1c22SToby Isaac + dm   - The DM object
7487c58f1c22SToby Isaac . name - The label name
7488c58f1c22SToby Isaac - point - The mesh point
7489c58f1c22SToby Isaac 
7490c58f1c22SToby Isaac   Output Parameter:
7491c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7492c58f1c22SToby Isaac 
7493c58f1c22SToby Isaac   Level: beginner
7494c58f1c22SToby Isaac 
7495c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7496c58f1c22SToby Isaac @*/
7497c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7498c58f1c22SToby Isaac {
7499c58f1c22SToby Isaac   DMLabel        label;
7500c58f1c22SToby Isaac 
7501c58f1c22SToby Isaac   PetscFunctionBegin;
7502c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7503c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7504*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLabel(dm, name, &label));
75057a8be351SBarry Smith   PetscCheck(label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7506*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetValue(label, point, value));
7507c58f1c22SToby Isaac   PetscFunctionReturn(0);
7508c58f1c22SToby Isaac }
7509c58f1c22SToby Isaac 
7510c58f1c22SToby Isaac /*@C
7511c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7512c58f1c22SToby Isaac 
7513c58f1c22SToby Isaac   Not Collective
7514c58f1c22SToby Isaac 
7515c58f1c22SToby Isaac   Input Parameters:
7516c58f1c22SToby Isaac + dm   - The DM object
7517c58f1c22SToby Isaac . name - The label name
7518c58f1c22SToby Isaac . point - The mesh point
7519c58f1c22SToby Isaac - value - The label value for this point
7520c58f1c22SToby Isaac 
7521c58f1c22SToby Isaac   Output Parameter:
7522c58f1c22SToby Isaac 
7523c58f1c22SToby Isaac   Level: beginner
7524c58f1c22SToby Isaac 
7525c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7526c58f1c22SToby Isaac @*/
7527c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7528c58f1c22SToby Isaac {
7529c58f1c22SToby Isaac   DMLabel        label;
7530c58f1c22SToby Isaac 
7531c58f1c22SToby Isaac   PetscFunctionBegin;
7532c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7533c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7534*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLabel(dm, name, &label));
7535c58f1c22SToby Isaac   if (!label) {
7536*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateLabel(dm, name));
7537*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabel(dm, name, &label));
7538c58f1c22SToby Isaac   }
7539*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelSetValue(label, point, value));
7540c58f1c22SToby Isaac   PetscFunctionReturn(0);
7541c58f1c22SToby Isaac }
7542c58f1c22SToby Isaac 
7543c58f1c22SToby Isaac /*@C
7544c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7545c58f1c22SToby Isaac 
7546c58f1c22SToby Isaac   Not Collective
7547c58f1c22SToby Isaac 
7548c58f1c22SToby Isaac   Input Parameters:
7549c58f1c22SToby Isaac + dm   - The DM object
7550c58f1c22SToby Isaac . name - The label name
7551c58f1c22SToby Isaac . point - The mesh point
7552c58f1c22SToby Isaac - value - The label value for this point
7553c58f1c22SToby Isaac 
7554c58f1c22SToby Isaac   Output Parameter:
7555c58f1c22SToby Isaac 
7556c58f1c22SToby Isaac   Level: beginner
7557c58f1c22SToby Isaac 
7558c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7559c58f1c22SToby Isaac @*/
7560c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7561c58f1c22SToby Isaac {
7562c58f1c22SToby Isaac   DMLabel        label;
7563c58f1c22SToby Isaac 
7564c58f1c22SToby Isaac   PetscFunctionBegin;
7565c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7566c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7567*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLabel(dm, name, &label));
7568c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7569*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelClearValue(label, point, value));
7570c58f1c22SToby Isaac   PetscFunctionReturn(0);
7571c58f1c22SToby Isaac }
7572c58f1c22SToby Isaac 
7573c58f1c22SToby Isaac /*@C
7574c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7575c58f1c22SToby Isaac 
7576c58f1c22SToby Isaac   Not Collective
7577c58f1c22SToby Isaac 
7578c58f1c22SToby Isaac   Input Parameters:
7579c58f1c22SToby Isaac + dm   - The DM object
7580c58f1c22SToby Isaac - name - The label name
7581c58f1c22SToby Isaac 
7582c58f1c22SToby Isaac   Output Parameter:
7583c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7584c58f1c22SToby Isaac 
7585c58f1c22SToby Isaac   Level: beginner
7586c58f1c22SToby Isaac 
7587df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7588c58f1c22SToby Isaac @*/
7589c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7590c58f1c22SToby Isaac {
7591c58f1c22SToby Isaac   DMLabel        label;
7592c58f1c22SToby Isaac 
7593c58f1c22SToby Isaac   PetscFunctionBegin;
7594c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7595c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7596534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7597*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLabel(dm, name, &label));
7598c58f1c22SToby Isaac   *size = 0;
7599c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7600*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetNumValues(label, size));
7601c58f1c22SToby Isaac   PetscFunctionReturn(0);
7602c58f1c22SToby Isaac }
7603c58f1c22SToby Isaac 
7604c58f1c22SToby Isaac /*@C
7605c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7606c58f1c22SToby Isaac 
7607c58f1c22SToby Isaac   Not Collective
7608c58f1c22SToby Isaac 
7609c58f1c22SToby Isaac   Input Parameters:
7610c58f1c22SToby Isaac + mesh - The DM object
7611c58f1c22SToby Isaac - name - The label name
7612c58f1c22SToby Isaac 
7613c58f1c22SToby Isaac   Output Parameter:
7614c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7615c58f1c22SToby Isaac 
7616c58f1c22SToby Isaac   Level: beginner
7617c58f1c22SToby Isaac 
7618c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7619c58f1c22SToby Isaac @*/
7620c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7621c58f1c22SToby Isaac {
7622c58f1c22SToby Isaac   DMLabel        label;
7623c58f1c22SToby Isaac 
7624c58f1c22SToby Isaac   PetscFunctionBegin;
7625c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7626c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7627c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7628*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLabel(dm, name, &label));
7629c58f1c22SToby Isaac   *ids = NULL;
7630dab2e251SBlaise Bourdin  if (label) {
7631*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetValueIS(label, ids));
7632dab2e251SBlaise Bourdin   } else {
7633dab2e251SBlaise Bourdin     /* returning an empty IS */
7634*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids));
7635dab2e251SBlaise Bourdin   }
7636c58f1c22SToby Isaac   PetscFunctionReturn(0);
7637c58f1c22SToby Isaac }
7638c58f1c22SToby Isaac 
7639c58f1c22SToby Isaac /*@C
7640c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7641c58f1c22SToby Isaac 
7642c58f1c22SToby Isaac   Not Collective
7643c58f1c22SToby Isaac 
7644c58f1c22SToby Isaac   Input Parameters:
7645c58f1c22SToby Isaac + dm - The DM object
7646c58f1c22SToby Isaac . name - The label name
7647c58f1c22SToby Isaac - value - The stratum value
7648c58f1c22SToby Isaac 
7649c58f1c22SToby Isaac   Output Parameter:
7650c58f1c22SToby Isaac . size - The stratum size
7651c58f1c22SToby Isaac 
7652c58f1c22SToby Isaac   Level: beginner
7653c58f1c22SToby Isaac 
7654c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7655c58f1c22SToby Isaac @*/
7656c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7657c58f1c22SToby Isaac {
7658c58f1c22SToby Isaac   DMLabel        label;
7659c58f1c22SToby Isaac 
7660c58f1c22SToby Isaac   PetscFunctionBegin;
7661c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7662c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7663534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7664*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLabel(dm, name, &label));
7665c58f1c22SToby Isaac   *size = 0;
7666c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7667*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetStratumSize(label, value, size));
7668c58f1c22SToby Isaac   PetscFunctionReturn(0);
7669c58f1c22SToby Isaac }
7670c58f1c22SToby Isaac 
7671c58f1c22SToby Isaac /*@C
7672c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7673c58f1c22SToby Isaac 
7674c58f1c22SToby Isaac   Not Collective
7675c58f1c22SToby Isaac 
7676c58f1c22SToby Isaac   Input Parameters:
7677c58f1c22SToby Isaac + dm - The DM object
7678c58f1c22SToby Isaac . name - The label name
7679c58f1c22SToby Isaac - value - The stratum value
7680c58f1c22SToby Isaac 
7681c58f1c22SToby Isaac   Output Parameter:
7682c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7683c58f1c22SToby Isaac 
7684c58f1c22SToby Isaac   Level: beginner
7685c58f1c22SToby Isaac 
7686c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7687c58f1c22SToby Isaac @*/
7688c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7689c58f1c22SToby Isaac {
7690c58f1c22SToby Isaac   DMLabel        label;
7691c58f1c22SToby Isaac 
7692c58f1c22SToby Isaac   PetscFunctionBegin;
7693c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7694c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7695c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7696*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLabel(dm, name, &label));
7697c58f1c22SToby Isaac   *points = NULL;
7698c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7699*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetStratumIS(label, value, points));
7700c58f1c22SToby Isaac   PetscFunctionReturn(0);
7701c58f1c22SToby Isaac }
7702c58f1c22SToby Isaac 
77034de306b1SToby Isaac /*@C
77049044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
77054de306b1SToby Isaac 
77064de306b1SToby Isaac   Not Collective
77074de306b1SToby Isaac 
77084de306b1SToby Isaac   Input Parameters:
77094de306b1SToby Isaac + dm - The DM object
77104de306b1SToby Isaac . name - The label name
77114de306b1SToby Isaac . value - The stratum value
77124de306b1SToby Isaac - points - The stratum points
77134de306b1SToby Isaac 
77144de306b1SToby Isaac   Level: beginner
77154de306b1SToby Isaac 
77164de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
77174de306b1SToby Isaac @*/
77184de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
77194de306b1SToby Isaac {
77204de306b1SToby Isaac   DMLabel        label;
77214de306b1SToby Isaac 
77224de306b1SToby Isaac   PetscFunctionBegin;
77234de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77244de306b1SToby Isaac   PetscValidCharPointer(name, 2);
77254de306b1SToby Isaac   PetscValidPointer(points, 4);
7726*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLabel(dm, name, &label));
77274de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
7728*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelSetStratumIS(label, value, points));
77294de306b1SToby Isaac   PetscFunctionReturn(0);
77304de306b1SToby Isaac }
77314de306b1SToby Isaac 
7732c58f1c22SToby Isaac /*@C
7733c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7734c58f1c22SToby Isaac 
7735c58f1c22SToby Isaac   Not Collective
7736c58f1c22SToby Isaac 
7737c58f1c22SToby Isaac   Input Parameters:
7738c58f1c22SToby Isaac + dm   - The DM object
7739c58f1c22SToby Isaac . name - The label name
7740c58f1c22SToby Isaac - value - The label value for this point
7741c58f1c22SToby Isaac 
7742c58f1c22SToby Isaac   Output Parameter:
7743c58f1c22SToby Isaac 
7744c58f1c22SToby Isaac   Level: beginner
7745c58f1c22SToby Isaac 
7746c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7747c58f1c22SToby Isaac @*/
7748c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7749c58f1c22SToby Isaac {
7750c58f1c22SToby Isaac   DMLabel        label;
7751c58f1c22SToby Isaac 
7752c58f1c22SToby Isaac   PetscFunctionBegin;
7753c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7754c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7755*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLabel(dm, name, &label));
7756c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7757*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelClearStratum(label, value));
7758c58f1c22SToby Isaac   PetscFunctionReturn(0);
7759c58f1c22SToby Isaac }
7760c58f1c22SToby Isaac 
7761c58f1c22SToby Isaac /*@
7762c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7763c58f1c22SToby Isaac 
7764c58f1c22SToby Isaac   Not Collective
7765c58f1c22SToby Isaac 
7766c58f1c22SToby Isaac   Input Parameter:
7767c58f1c22SToby Isaac . dm   - The DM object
7768c58f1c22SToby Isaac 
7769c58f1c22SToby Isaac   Output Parameter:
7770c58f1c22SToby Isaac . numLabels - the number of Labels
7771c58f1c22SToby Isaac 
7772c58f1c22SToby Isaac   Level: intermediate
7773c58f1c22SToby Isaac 
7774c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7775c58f1c22SToby Isaac @*/
7776c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7777c58f1c22SToby Isaac {
77785d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7779c58f1c22SToby Isaac   PetscInt  n    = 0;
7780c58f1c22SToby Isaac 
7781c58f1c22SToby Isaac   PetscFunctionBegin;
7782c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7783534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7784c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7785c58f1c22SToby Isaac   *numLabels = n;
7786c58f1c22SToby Isaac   PetscFunctionReturn(0);
7787c58f1c22SToby Isaac }
7788c58f1c22SToby Isaac 
7789c58f1c22SToby Isaac /*@C
7790c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7791c58f1c22SToby Isaac 
7792c58f1c22SToby Isaac   Not Collective
7793c58f1c22SToby Isaac 
7794c58f1c22SToby Isaac   Input Parameters:
7795c58f1c22SToby Isaac + dm - The DM object
7796c58f1c22SToby Isaac - n  - the label number
7797c58f1c22SToby Isaac 
7798c58f1c22SToby Isaac   Output Parameter:
7799c58f1c22SToby Isaac . name - the label name
7800c58f1c22SToby Isaac 
7801c58f1c22SToby Isaac   Level: intermediate
7802c58f1c22SToby Isaac 
7803c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7804c58f1c22SToby Isaac @*/
7805c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7806c58f1c22SToby Isaac {
78075d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7808c58f1c22SToby Isaac   PetscInt       l    = 0;
7809c58f1c22SToby Isaac 
7810c58f1c22SToby Isaac   PetscFunctionBegin;
7811c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7812c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7813c58f1c22SToby Isaac   while (next) {
7814c58f1c22SToby Isaac     if (l == n) {
7815*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscObjectGetName((PetscObject) next->label, name));
7816c58f1c22SToby Isaac       PetscFunctionReturn(0);
7817c58f1c22SToby Isaac     }
7818c58f1c22SToby Isaac     ++l;
7819c58f1c22SToby Isaac     next = next->next;
7820c58f1c22SToby Isaac   }
782198921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7822c58f1c22SToby Isaac }
7823c58f1c22SToby Isaac 
7824c58f1c22SToby Isaac /*@C
7825c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7826c58f1c22SToby Isaac 
7827c58f1c22SToby Isaac   Not Collective
7828c58f1c22SToby Isaac 
7829c58f1c22SToby Isaac   Input Parameters:
7830c58f1c22SToby Isaac + dm   - The DM object
7831c58f1c22SToby Isaac - name - The label name
7832c58f1c22SToby Isaac 
7833c58f1c22SToby Isaac   Output Parameter:
7834c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7835c58f1c22SToby Isaac 
7836c58f1c22SToby Isaac   Level: intermediate
7837c58f1c22SToby Isaac 
7838c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7839c58f1c22SToby Isaac @*/
7840c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7841c58f1c22SToby Isaac {
78425d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7843d67d17b1SMatthew G. Knepley   const char    *lname;
7844c58f1c22SToby Isaac 
7845c58f1c22SToby Isaac   PetscFunctionBegin;
7846c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7847c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7848534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7849c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7850c58f1c22SToby Isaac   while (next) {
7851*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectGetName((PetscObject) next->label, &lname));
7852*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcmp(name, lname, hasLabel));
7853c58f1c22SToby Isaac     if (*hasLabel) break;
7854c58f1c22SToby Isaac     next = next->next;
7855c58f1c22SToby Isaac   }
7856c58f1c22SToby Isaac   PetscFunctionReturn(0);
7857c58f1c22SToby Isaac }
7858c58f1c22SToby Isaac 
7859c58f1c22SToby Isaac /*@C
7860c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7861c58f1c22SToby Isaac 
7862c58f1c22SToby Isaac   Not Collective
7863c58f1c22SToby Isaac 
7864c58f1c22SToby Isaac   Input Parameters:
7865c58f1c22SToby Isaac + dm   - The DM object
7866c58f1c22SToby Isaac - name - The label name
7867c58f1c22SToby Isaac 
7868c58f1c22SToby Isaac   Output Parameter:
7869c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7870c58f1c22SToby Isaac 
78716d7c9049SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
78726d7c9049SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
78736d7c9049SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
78746d7c9049SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
78756d7c9049SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
78766d7c9049SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
78776d7c9049SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
78786d7c9049SMatthew G. Knepley 
7879c58f1c22SToby Isaac   Level: intermediate
7880c58f1c22SToby Isaac 
78816d7c9049SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
7882c58f1c22SToby Isaac @*/
7883c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7884c58f1c22SToby Isaac {
78855d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7886c58f1c22SToby Isaac   PetscBool      hasLabel;
7887d67d17b1SMatthew G. Knepley   const char    *lname;
7888c58f1c22SToby Isaac 
7889c58f1c22SToby Isaac   PetscFunctionBegin;
7890c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7891c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7892c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7893c58f1c22SToby Isaac   *label = NULL;
7894c58f1c22SToby Isaac   while (next) {
7895*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectGetName((PetscObject) next->label, &lname));
7896*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcmp(name, lname, &hasLabel));
7897c58f1c22SToby Isaac     if (hasLabel) {
7898c58f1c22SToby Isaac       *label = next->label;
7899c58f1c22SToby Isaac       break;
7900c58f1c22SToby Isaac     }
7901c58f1c22SToby Isaac     next = next->next;
7902c58f1c22SToby Isaac   }
7903c58f1c22SToby Isaac   PetscFunctionReturn(0);
7904c58f1c22SToby Isaac }
7905c58f1c22SToby Isaac 
7906c58f1c22SToby Isaac /*@C
7907c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7908c58f1c22SToby Isaac 
7909c58f1c22SToby Isaac   Not Collective
7910c58f1c22SToby Isaac 
7911c58f1c22SToby Isaac   Input Parameters:
7912c58f1c22SToby Isaac + dm - The DM object
7913c58f1c22SToby Isaac - n  - the label number
7914c58f1c22SToby Isaac 
7915c58f1c22SToby Isaac   Output Parameter:
7916c58f1c22SToby Isaac . label - the label
7917c58f1c22SToby Isaac 
7918c58f1c22SToby Isaac   Level: intermediate
7919c58f1c22SToby Isaac 
7920c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7921c58f1c22SToby Isaac @*/
7922c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7923c58f1c22SToby Isaac {
79245d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7925c58f1c22SToby Isaac   PetscInt    l    = 0;
7926c58f1c22SToby Isaac 
7927c58f1c22SToby Isaac   PetscFunctionBegin;
7928c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7929c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7930c58f1c22SToby Isaac   while (next) {
7931c58f1c22SToby Isaac     if (l == n) {
7932c58f1c22SToby Isaac       *label = next->label;
7933c58f1c22SToby Isaac       PetscFunctionReturn(0);
7934c58f1c22SToby Isaac     }
7935c58f1c22SToby Isaac     ++l;
7936c58f1c22SToby Isaac     next = next->next;
7937c58f1c22SToby Isaac   }
793898921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7939c58f1c22SToby Isaac }
7940c58f1c22SToby Isaac 
7941c58f1c22SToby Isaac /*@C
7942c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7943c58f1c22SToby Isaac 
7944c58f1c22SToby Isaac   Not Collective
7945c58f1c22SToby Isaac 
7946c58f1c22SToby Isaac   Input Parameters:
7947c58f1c22SToby Isaac + dm   - The DM object
7948c58f1c22SToby Isaac - label - The DMLabel
7949c58f1c22SToby Isaac 
7950c58f1c22SToby Isaac   Level: developer
7951c58f1c22SToby Isaac 
7952c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7953c58f1c22SToby Isaac @*/
7954c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7955c58f1c22SToby Isaac {
79565d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7957c58f1c22SToby Isaac   PetscBool      hasLabel;
7958d67d17b1SMatthew G. Knepley   const char    *lname;
79595d80c0bfSVaclav Hapla   PetscBool      flg;
7960c58f1c22SToby Isaac 
7961c58f1c22SToby Isaac   PetscFunctionBegin;
7962c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7963*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetName((PetscObject) label, &lname));
7964*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMHasLabel(dm, lname, &hasLabel));
79657a8be351SBarry Smith   PetscCheck(!hasLabel,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7966*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscCalloc1(1, &tmpLabel));
7967c58f1c22SToby Isaac   tmpLabel->label  = label;
7968c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
79695d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
79705d80c0bfSVaclav Hapla   *p = tmpLabel;
7971*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject)label));
7972*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrcmp(lname, "depth", &flg));
79735d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7974*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrcmp(lname, "celltype", &flg));
7975ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7976c58f1c22SToby Isaac   PetscFunctionReturn(0);
7977c58f1c22SToby Isaac }
7978c58f1c22SToby Isaac 
7979c58f1c22SToby Isaac /*@C
79804a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
79814a7ee7d0SMatthew G. Knepley 
79824a7ee7d0SMatthew G. Knepley   Not Collective
79834a7ee7d0SMatthew G. Knepley 
79844a7ee7d0SMatthew G. Knepley   Input Parameters:
79854a7ee7d0SMatthew G. Knepley + dm    - The DM object
79864a7ee7d0SMatthew G. Knepley - label - The DMLabel, having the same name, to substitute
79874a7ee7d0SMatthew G. Knepley 
79884a7ee7d0SMatthew G. Knepley   Note: Some of the default labels in a DMPlex will be
79894a7ee7d0SMatthew G. Knepley $ "depth"       - Holds the depth (co-dimension) of each mesh point
79904a7ee7d0SMatthew G. Knepley $ "celltype"    - Holds the topological type of each cell
79914a7ee7d0SMatthew G. Knepley $ "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
79924a7ee7d0SMatthew G. Knepley $ "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
79934a7ee7d0SMatthew G. Knepley $ "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
79944a7ee7d0SMatthew G. Knepley $ "Vertex Sets" - Mirrors the vertex sets defined by GMsh
79954a7ee7d0SMatthew G. Knepley 
79964a7ee7d0SMatthew G. Knepley   Level: intermediate
79974a7ee7d0SMatthew G. Knepley 
79984a7ee7d0SMatthew G. Knepley .seealso: DMCreateLabel(), DMHasLabel(), DMPlexGetDepthLabel(), DMPlexGetCellType()
79994a7ee7d0SMatthew G. Knepley @*/
80004a7ee7d0SMatthew G. Knepley PetscErrorCode DMSetLabel(DM dm, DMLabel label)
80014a7ee7d0SMatthew G. Knepley {
80024a7ee7d0SMatthew G. Knepley   DMLabelLink    next = dm->labels;
80034a7ee7d0SMatthew G. Knepley   PetscBool      hasLabel, flg;
80044a7ee7d0SMatthew G. Knepley   const char    *name, *lname;
80054a7ee7d0SMatthew G. Knepley 
80064a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
80074a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80084a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
8009*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetName((PetscObject) label, &name));
80104a7ee7d0SMatthew G. Knepley   while (next) {
8011*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectGetName((PetscObject) next->label, &lname));
8012*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcmp(name, lname, &hasLabel));
80134a7ee7d0SMatthew G. Knepley     if (hasLabel) {
8014*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscObjectReference((PetscObject) label));
8015*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscStrcmp(lname, "depth", &flg));
80164a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
8017*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscStrcmp(lname, "celltype", &flg));
80184a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
8019*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelDestroy(&next->label));
80204a7ee7d0SMatthew G. Knepley       next->label = label;
80214a7ee7d0SMatthew G. Knepley       break;
80224a7ee7d0SMatthew G. Knepley     }
80234a7ee7d0SMatthew G. Knepley     next = next->next;
80244a7ee7d0SMatthew G. Knepley   }
80254a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
80264a7ee7d0SMatthew G. Knepley }
80274a7ee7d0SMatthew G. Knepley 
80284a7ee7d0SMatthew G. Knepley /*@C
8029e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
8030c58f1c22SToby Isaac 
8031c58f1c22SToby Isaac   Not Collective
8032c58f1c22SToby Isaac 
8033c58f1c22SToby Isaac   Input Parameters:
8034c58f1c22SToby Isaac + dm   - The DM object
8035c58f1c22SToby Isaac - name - The label name
8036c58f1c22SToby Isaac 
8037c58f1c22SToby Isaac   Output Parameter:
8038c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
8039c58f1c22SToby Isaac 
8040c58f1c22SToby Isaac   Level: developer
8041c58f1c22SToby Isaac 
8042e5472504SVaclav Hapla   Notes:
8043e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
8044e5472504SVaclav Hapla   DMLabelDestroy() on the label.
8045e5472504SVaclav Hapla 
8046e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
8047e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
8048e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
8049e5472504SVaclav Hapla 
8050e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
8051c58f1c22SToby Isaac @*/
8052c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
8053c58f1c22SToby Isaac {
805495d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
8055c58f1c22SToby Isaac   PetscBool      hasLabel;
8056d67d17b1SMatthew G. Knepley   const char    *lname;
8057c58f1c22SToby Isaac 
8058c58f1c22SToby Isaac   PetscFunctionBegin;
8059c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8060e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
8061e5472504SVaclav Hapla   if (label) {
8062e5472504SVaclav Hapla     PetscValidPointer(label, 3);
8063c58f1c22SToby Isaac     *label = NULL;
8064e5472504SVaclav Hapla   }
80655d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
8066*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectGetName((PetscObject) link->label, &lname));
8067*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcmp(name, lname, &hasLabel));
8068c58f1c22SToby Isaac     if (hasLabel) {
806995d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
8070*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscStrcmp(name, "depth", &hasLabel));
807195d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
8072*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscStrcmp(name, "celltype", &hasLabel));
8073ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
807495d578d6SVaclav Hapla       if (label) *label = link->label;
8075*5f80ce2aSJacob Faibussowitsch       else       CHKERRQ(DMLabelDestroy(&link->label));
8076*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(link));
8077c58f1c22SToby Isaac       break;
8078c58f1c22SToby Isaac     }
8079c58f1c22SToby Isaac   }
8080c58f1c22SToby Isaac   PetscFunctionReturn(0);
8081c58f1c22SToby Isaac }
8082c58f1c22SToby Isaac 
8083306894acSVaclav Hapla /*@
8084306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
8085306894acSVaclav Hapla 
8086306894acSVaclav Hapla   Not Collective
8087306894acSVaclav Hapla 
8088306894acSVaclav Hapla   Input Parameters:
8089306894acSVaclav Hapla + dm   - The DM object
8090876aa926SVaclav Hapla . label - The DMLabel to be removed from the DM
8091306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
8092306894acSVaclav Hapla 
8093306894acSVaclav Hapla   Level: developer
8094306894acSVaclav Hapla 
8095306894acSVaclav Hapla   Notes:
8096306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
8097306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
8098306894acSVaclav Hapla   *label nullified.
8099306894acSVaclav Hapla 
8100306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
8101306894acSVaclav Hapla @*/
8102306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
8103306894acSVaclav Hapla {
810443e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
8105306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
8106306894acSVaclav Hapla 
8107306894acSVaclav Hapla   PetscFunctionBegin;
8108306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8109306894acSVaclav Hapla   PetscValidPointer(label, 2);
8110f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
8111306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
8112306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
81135d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
811443e45a93SVaclav Hapla     if (*label == link->label) {
8115306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
811643e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
8117306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
8118ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
811943e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
8120*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelDestroy(&link->label));
8121*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(link));
8122306894acSVaclav Hapla       break;
8123306894acSVaclav Hapla     }
8124306894acSVaclav Hapla   }
81257a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
8126306894acSVaclav Hapla   PetscFunctionReturn(0);
8127306894acSVaclav Hapla }
8128306894acSVaclav Hapla 
8129c58f1c22SToby Isaac /*@C
8130c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
8131c58f1c22SToby Isaac 
8132c58f1c22SToby Isaac   Not Collective
8133c58f1c22SToby Isaac 
8134c58f1c22SToby Isaac   Input Parameters:
8135c58f1c22SToby Isaac + dm   - The DM object
8136c58f1c22SToby Isaac - name - The label name
8137c58f1c22SToby Isaac 
8138c58f1c22SToby Isaac   Output Parameter:
8139c58f1c22SToby Isaac . output - The flag for output
8140c58f1c22SToby Isaac 
8141c58f1c22SToby Isaac   Level: developer
8142c58f1c22SToby Isaac 
8143c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8144c58f1c22SToby Isaac @*/
8145c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
8146c58f1c22SToby Isaac {
81475d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8148d67d17b1SMatthew G. Knepley   const char    *lname;
8149c58f1c22SToby Isaac 
8150c58f1c22SToby Isaac   PetscFunctionBegin;
8151c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8152c58f1c22SToby Isaac   PetscValidPointer(name, 2);
8153c58f1c22SToby Isaac   PetscValidPointer(output, 3);
8154c58f1c22SToby Isaac   while (next) {
8155c58f1c22SToby Isaac     PetscBool flg;
8156c58f1c22SToby Isaac 
8157*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectGetName((PetscObject) next->label, &lname));
8158*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcmp(name, lname, &flg));
8159c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
8160c58f1c22SToby Isaac     next = next->next;
8161c58f1c22SToby Isaac   }
816298921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8163c58f1c22SToby Isaac }
8164c58f1c22SToby Isaac 
8165c58f1c22SToby Isaac /*@C
8166c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
8167c58f1c22SToby Isaac 
8168c58f1c22SToby Isaac   Not Collective
8169c58f1c22SToby Isaac 
8170c58f1c22SToby Isaac   Input Parameters:
8171c58f1c22SToby Isaac + dm     - The DM object
8172c58f1c22SToby Isaac . name   - The label name
8173c58f1c22SToby Isaac - output - The flag for output
8174c58f1c22SToby Isaac 
8175c58f1c22SToby Isaac   Level: developer
8176c58f1c22SToby Isaac 
8177c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
8178c58f1c22SToby Isaac @*/
8179c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
8180c58f1c22SToby Isaac {
81815d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
8182d67d17b1SMatthew G. Knepley   const char    *lname;
8183c58f1c22SToby Isaac 
8184c58f1c22SToby Isaac   PetscFunctionBegin;
8185c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8186534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
8187c58f1c22SToby Isaac   while (next) {
8188c58f1c22SToby Isaac     PetscBool flg;
8189c58f1c22SToby Isaac 
8190*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectGetName((PetscObject) next->label, &lname));
8191*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcmp(name, lname, &flg));
8192c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
8193c58f1c22SToby Isaac     next = next->next;
8194c58f1c22SToby Isaac   }
819598921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
8196c58f1c22SToby Isaac }
8197c58f1c22SToby Isaac 
8198c58f1c22SToby Isaac /*@
8199c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
8200c58f1c22SToby Isaac 
8201d083f849SBarry Smith   Collective on dmA
8202c58f1c22SToby Isaac 
8203d8d19677SJose E. Roman   Input Parameters:
82045d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
82052cbb9b06SVaclav Hapla . dmB - The DM object to which labels are copied
82065d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
82072cbb9b06SVaclav Hapla . all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
82082cbb9b06SVaclav Hapla - emode - How to behave when a DMLabel in the source and destination DMs with the same name is encountered (see DMCopyLabelsMode)
8209c58f1c22SToby Isaac 
8210c58f1c22SToby Isaac   Level: intermediate
8211c58f1c22SToby Isaac 
82122cbb9b06SVaclav Hapla   Notes:
82132cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
8214c58f1c22SToby Isaac 
82152cbb9b06SVaclav Hapla .seealso: DMAddLabel(), DMCopyLabelsMode
8216c58f1c22SToby Isaac @*/
82172cbb9b06SVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
8218c58f1c22SToby Isaac {
82192cbb9b06SVaclav Hapla   DMLabel        label, labelNew, labelOld;
8220c58f1c22SToby Isaac   const char    *name;
8221c58f1c22SToby Isaac   PetscBool      flg;
82225d80c0bfSVaclav Hapla   DMLabelLink    link;
8223c58f1c22SToby Isaac 
82245d80c0bfSVaclav Hapla   PetscFunctionBegin;
82255d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
82265d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
82275d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
82285d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
82297a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER,PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
82305d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
82315d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
82325d80c0bfSVaclav Hapla     label=link->label;
8233*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectGetName((PetscObject)label, &name));
82345d80c0bfSVaclav Hapla     if (!all) {
8235*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscStrcmp(name, "depth", &flg));
8236c58f1c22SToby Isaac       if (flg) continue;
8237*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscStrcmp(name, "dim", &flg));
82387d5acc75SStefano Zampini       if (flg) continue;
8239*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscStrcmp(name, "celltype", &flg));
8240ba2698f1SMatthew G. Knepley       if (flg) continue;
82415d80c0bfSVaclav Hapla     }
8242*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabel(dmB, name, &labelOld));
82432cbb9b06SVaclav Hapla     if (labelOld) {
82442cbb9b06SVaclav Hapla       switch (emode) {
82452cbb9b06SVaclav Hapla         case DM_COPY_LABELS_KEEP:
82462cbb9b06SVaclav Hapla           continue;
82472cbb9b06SVaclav Hapla         case DM_COPY_LABELS_REPLACE:
8248*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
82492cbb9b06SVaclav Hapla           break;
82502cbb9b06SVaclav Hapla         case DM_COPY_LABELS_FAIL:
825198921bdaSJacob Faibussowitsch           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
82522cbb9b06SVaclav Hapla         default:
82537a8be351SBarry Smith           SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
82542cbb9b06SVaclav Hapla       }
82552cbb9b06SVaclav Hapla     }
82565d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
8257*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelDuplicate(label, &labelNew));
82585d80c0bfSVaclav Hapla     } else {
82595d80c0bfSVaclav Hapla       labelNew = label;
82605d80c0bfSVaclav Hapla     }
8261*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMAddLabel(dmB, labelNew));
8262*5f80ce2aSJacob Faibussowitsch     if (mode==PETSC_COPY_VALUES) CHKERRQ(DMLabelDestroy(&labelNew));
8263c58f1c22SToby Isaac   }
8264c58f1c22SToby Isaac   PetscFunctionReturn(0);
8265c58f1c22SToby Isaac }
8266461a15a0SLisandro Dalcin 
8267609dae6eSVaclav Hapla /*@C
8268609dae6eSVaclav Hapla   DMCompareLabels - Compare labels of two DMPlex meshes
8269609dae6eSVaclav Hapla 
82705efe38ccSVaclav Hapla   Collective
8271609dae6eSVaclav Hapla 
8272609dae6eSVaclav Hapla   Input Parameters:
8273609dae6eSVaclav Hapla + dm0 - First DM object
8274609dae6eSVaclav Hapla - dm1 - Second DM object
8275609dae6eSVaclav Hapla 
8276609dae6eSVaclav Hapla   Output Parameters
82775efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
8278609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
8279609dae6eSVaclav Hapla 
8280609dae6eSVaclav Hapla   Level: intermediate
8281609dae6eSVaclav Hapla 
8282609dae6eSVaclav Hapla   Notes:
82835efe38ccSVaclav Hapla   The output flag equal is the same on all processes.
82845efe38ccSVaclav Hapla   If it is passed as NULL and difference is found, an error is thrown on all processes.
82855efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
8286609dae6eSVaclav Hapla 
82875efe38ccSVaclav Hapla   The output message is set independently on each rank.
82885efe38ccSVaclav Hapla   It is set to NULL if no difference was found on the current rank. It must be freed by user.
82895efe38ccSVaclav Hapla   If message is passed as NULL and difference is found, the difference description is printed to stderr in synchronized manner.
82905efe38ccSVaclav Hapla   Make sure to pass NULL on all processes.
8291609dae6eSVaclav Hapla 
8292609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
8293609dae6eSVaclav Hapla   DMLabelCompare() is used to compare each pair of labels with the same name.
8294609dae6eSVaclav Hapla 
8295609dae6eSVaclav Hapla   Fortran Notes:
8296609dae6eSVaclav Hapla   This function is currently not available from Fortran.
8297609dae6eSVaclav Hapla 
8298609dae6eSVaclav Hapla .seealso: DMAddLabel(), DMCopyLabelsMode, DMLabelCompare()
8299609dae6eSVaclav Hapla @*/
83005efe38ccSVaclav Hapla PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
8301609dae6eSVaclav Hapla {
83025efe38ccSVaclav Hapla   PetscInt        n, i;
8303609dae6eSVaclav Hapla   char            msg[PETSC_MAX_PATH_LEN] = "";
83045efe38ccSVaclav Hapla   PetscBool       eq;
8305609dae6eSVaclav Hapla   MPI_Comm        comm;
83065efe38ccSVaclav Hapla   PetscMPIInt     rank;
8307609dae6eSVaclav Hapla 
8308609dae6eSVaclav Hapla   PetscFunctionBegin;
8309609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0,DM_CLASSID,1);
8310609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1,DM_CLASSID,2);
8311609dae6eSVaclav Hapla   PetscCheckSameComm(dm0,1,dm1,2);
83125efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal,3);
8313609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
8314*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetComm((PetscObject)dm0, &comm));
8315*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_rank(comm, &rank));
83165efe38ccSVaclav Hapla   {
83175efe38ccSVaclav Hapla     PetscInt n1;
83185efe38ccSVaclav Hapla 
8319*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetNumLabels(dm0, &n));
8320*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetNumLabels(dm1, &n1));
83215efe38ccSVaclav Hapla     eq = (PetscBool) (n == n1);
83225efe38ccSVaclav Hapla     if (!eq) {
8323*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %D != %D = Number of labels in dm1", n, n1));
8324609dae6eSVaclav Hapla     }
8325*5f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
83265efe38ccSVaclav Hapla     if (!eq) goto finish;
83275efe38ccSVaclav Hapla   }
83285efe38ccSVaclav Hapla   for (i=0; i<n; i++) {
8329609dae6eSVaclav Hapla     DMLabel     l0, l1;
8330609dae6eSVaclav Hapla     const char *name;
8331609dae6eSVaclav Hapla     char       *msgInner;
8332609dae6eSVaclav Hapla 
8333609dae6eSVaclav Hapla     /* Ignore label order */
8334*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabelByNum(dm0, i, &l0));
8335*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectGetName((PetscObject)l0, &name));
8336*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabel(dm1, name, &l1));
8337609dae6eSVaclav Hapla     if (!l1) {
8338*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%D in dm0) not found in dm1", name, i));
83395efe38ccSVaclav Hapla       eq = PETSC_FALSE;
83405efe38ccSVaclav Hapla       break;
8341609dae6eSVaclav Hapla     }
8342*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
8343*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrncpy(msg, msgInner, sizeof(msg)));
8344*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(msgInner));
83455efe38ccSVaclav Hapla     if (!eq) break;
8346609dae6eSVaclav Hapla   }
8347*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
8348609dae6eSVaclav Hapla finish:
83495efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
8350609dae6eSVaclav Hapla   if (message) {
8351609dae6eSVaclav Hapla     *message = NULL;
8352609dae6eSVaclav Hapla     if (msg[0]) {
8353*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscStrallocpy(msg, message));
8354609dae6eSVaclav Hapla     }
83555efe38ccSVaclav Hapla   } else {
83565efe38ccSVaclav Hapla     if (msg[0]) {
8357*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
8358609dae6eSVaclav Hapla     }
8359*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSynchronizedFlush(comm, PETSC_STDERR));
83605efe38ccSVaclav Hapla   }
83615efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
83625efe38ccSVaclav Hapla   if (equal) *equal = eq;
83637a8be351SBarry Smith   else PetscCheck(eq,comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
8364609dae6eSVaclav Hapla   PetscFunctionReturn(0);
8365609dae6eSVaclav Hapla }
8366609dae6eSVaclav Hapla 
8367461a15a0SLisandro Dalcin PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
8368461a15a0SLisandro Dalcin {
8369461a15a0SLisandro Dalcin   PetscFunctionBegin;
8370461a15a0SLisandro Dalcin   PetscValidPointer(label,2);
8371461a15a0SLisandro Dalcin   if (!*label) {
8372*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateLabel(dm, name));
8373*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabel(dm, name, label));
8374461a15a0SLisandro Dalcin   }
8375*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelSetValue(*label, point, value));
8376461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
8377461a15a0SLisandro Dalcin }
8378461a15a0SLisandro Dalcin 
83790fdc7489SMatthew Knepley /*
83800fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
83810fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
83820fdc7489SMatthew Knepley   (label, id) pair in the DM.
83830fdc7489SMatthew Knepley 
83840fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
83850fdc7489SMatthew Knepley   each label.
83860fdc7489SMatthew Knepley */
83870fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
83880fdc7489SMatthew Knepley {
83890fdc7489SMatthew Knepley   DMUniversalLabel ul;
83900fdc7489SMatthew Knepley   PetscBool       *active;
83910fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
83920fdc7489SMatthew Knepley 
83930fdc7489SMatthew Knepley   PetscFunctionBegin;
8394*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(1, &ul));
8395*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
8396*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumLabels(dm, &Nl));
8397*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscCalloc1(Nl, &active));
83980fdc7489SMatthew Knepley   ul->Nl = 0;
83990fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
84000fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
84010fdc7489SMatthew Knepley     const char *name;
84020fdc7489SMatthew Knepley 
8403*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabelName(dm, l, &name));
8404*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrncmp(name, "depth", 6, &isdepth));
8405*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrncmp(name, "celltype", 9, &iscelltype));
84060fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
84070fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
84080fdc7489SMatthew Knepley   }
8409*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscCalloc5(ul->Nl, &ul->names, ul->Nl, &ul->indices, ul->Nl+1, &ul->offsets, ul->Nl+1, &ul->bits, ul->Nl, &ul->masks));
84100fdc7489SMatthew Knepley   ul->Nv = 0;
84110fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
84120fdc7489SMatthew Knepley     DMLabel     label;
84130fdc7489SMatthew Knepley     PetscInt    nv;
84140fdc7489SMatthew Knepley     const char *name;
84150fdc7489SMatthew Knepley 
84160fdc7489SMatthew Knepley     if (!active[l]) continue;
8417*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabelName(dm, l, &name));
8418*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabelByNum(dm, l, &label));
8419*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetNumValues(label, &nv));
8420*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrallocpy(name, &ul->names[m]));
84210fdc7489SMatthew Knepley     ul->indices[m]   = l;
84220fdc7489SMatthew Knepley     ul->Nv          += nv;
84230fdc7489SMatthew Knepley     ul->offsets[m+1] = nv;
84240fdc7489SMatthew Knepley     ul->bits[m+1]    = PetscCeilReal(PetscLog2Real(nv+1));
84250fdc7489SMatthew Knepley     ++m;
84260fdc7489SMatthew Knepley   }
84270fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
84280fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l-1] + ul->offsets[l];
84290fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l-1]    + ul->bits[l];
84300fdc7489SMatthew Knepley   }
84310fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
84320fdc7489SMatthew Knepley     PetscInt b;
84330fdc7489SMatthew Knepley 
84340fdc7489SMatthew Knepley     ul->masks[l] = 0;
84350fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l+1]; ++b) ul->masks[l] |= 1 << b;
84360fdc7489SMatthew Knepley   }
8437*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(ul->Nv, &ul->values));
84380fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
84390fdc7489SMatthew Knepley     DMLabel         label;
84400fdc7489SMatthew Knepley     IS              valueIS;
84410fdc7489SMatthew Knepley     const PetscInt *varr;
84420fdc7489SMatthew Knepley     PetscInt        nv, v;
84430fdc7489SMatthew Knepley 
84440fdc7489SMatthew Knepley     if (!active[l]) continue;
8445*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabelByNum(dm, l, &label));
8446*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetNumValues(label, &nv));
8447*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetValueIS(label, &valueIS));
8448*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(valueIS, &varr));
84490fdc7489SMatthew Knepley     for (v = 0; v < nv; ++v) {
84500fdc7489SMatthew Knepley       ul->values[ul->offsets[m]+v] = varr[v];
84510fdc7489SMatthew Knepley     }
8452*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(valueIS, &varr));
8453*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&valueIS));
8454*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
84550fdc7489SMatthew Knepley     ++m;
84560fdc7489SMatthew Knepley   }
8457*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetChart(dm, &pStart, &pEnd));
84580fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
84590fdc7489SMatthew Knepley     PetscInt  uval = 0;
84600fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
84610fdc7489SMatthew Knepley 
84620fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
84630fdc7489SMatthew Knepley       DMLabel  label;
84640649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
84650fdc7489SMatthew Knepley 
84660fdc7489SMatthew Knepley       if (!active[l]) continue;
8467*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetLabelByNum(dm, l, &label));
8468*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetValue(label, p, &val));
8469*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetDefaultValue(label, &defval));
84700fdc7489SMatthew Knepley       if (val == defval) {++m; continue;}
84710649b39aSStefano Zampini       nv = ul->offsets[m+1]-ul->offsets[m];
84720fdc7489SMatthew Knepley       marked = PETSC_TRUE;
8473*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
84747a8be351SBarry Smith       PetscCheck(loc >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %D not found in compression array", val);
84750fdc7489SMatthew Knepley       uval += (loc+1) << ul->bits[m];
84760fdc7489SMatthew Knepley       ++m;
84770fdc7489SMatthew Knepley     }
8478*5f80ce2aSJacob Faibussowitsch     if (marked) CHKERRQ(DMLabelSetValue(ul->label, p, uval));
84790fdc7489SMatthew Knepley   }
8480*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(active));
84810fdc7489SMatthew Knepley   *universal = ul;
84820fdc7489SMatthew Knepley   PetscFunctionReturn(0);
84830fdc7489SMatthew Knepley }
84840fdc7489SMatthew Knepley 
84850fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
84860fdc7489SMatthew Knepley {
84870fdc7489SMatthew Knepley   PetscInt       l;
84880fdc7489SMatthew Knepley 
84890fdc7489SMatthew Knepley   PetscFunctionBegin;
8490*5f80ce2aSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) CHKERRQ(PetscFree((*universal)->names[l]));
8491*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelDestroy(&(*universal)->label));
8492*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
8493*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree((*universal)->values));
8494*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(*universal));
84950fdc7489SMatthew Knepley   *universal = NULL;
84960fdc7489SMatthew Knepley   PetscFunctionReturn(0);
84970fdc7489SMatthew Knepley }
84980fdc7489SMatthew Knepley 
84990fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
85000fdc7489SMatthew Knepley {
85010fdc7489SMatthew Knepley   PetscFunctionBegin;
85020fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
85030fdc7489SMatthew Knepley   *ulabel = ul->label;
85040fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85050fdc7489SMatthew Knepley }
85060fdc7489SMatthew Knepley 
85070fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
85080fdc7489SMatthew Knepley {
85090fdc7489SMatthew Knepley   PetscInt       Nl = ul->Nl, l;
85100fdc7489SMatthew Knepley 
85110fdc7489SMatthew Knepley   PetscFunctionBegin;
8512064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
85130fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
8514*5f80ce2aSJacob Faibussowitsch     if (preserveOrder) CHKERRQ(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
8515*5f80ce2aSJacob Faibussowitsch     else               CHKERRQ(DMCreateLabel(dm, ul->names[l]));
85160fdc7489SMatthew Knepley   }
85170fdc7489SMatthew Knepley   if (preserveOrder) {
85180fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
85190fdc7489SMatthew Knepley       const char *name;
85200fdc7489SMatthew Knepley       PetscBool   match;
85210fdc7489SMatthew Knepley 
8522*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetLabelName(dm, ul->indices[l], &name));
8523*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscStrcmp(name, ul->names[l], &match));
85247a8be351SBarry 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]);
85250fdc7489SMatthew Knepley     }
85260fdc7489SMatthew Knepley   }
85270fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85280fdc7489SMatthew Knepley }
85290fdc7489SMatthew Knepley 
85300fdc7489SMatthew Knepley PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
85310fdc7489SMatthew Knepley {
85320fdc7489SMatthew Knepley   PetscInt       l;
85330fdc7489SMatthew Knepley 
85340fdc7489SMatthew Knepley   PetscFunctionBegin;
85350fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
85360fdc7489SMatthew Knepley     DMLabel  label;
85370fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
85380fdc7489SMatthew Knepley 
85390fdc7489SMatthew Knepley     if (lval) {
8540*5f80ce2aSJacob Faibussowitsch       if (useIndex) CHKERRQ(DMGetLabelByNum(dm, ul->indices[l], &label));
8541*5f80ce2aSJacob Faibussowitsch       else          CHKERRQ(DMGetLabel(dm, ul->names[l], &label));
8542*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelSetValue(label, p, ul->values[ul->offsets[l]+lval-1]));
85430fdc7489SMatthew Knepley     }
85440fdc7489SMatthew Knepley   }
85450fdc7489SMatthew Knepley   PetscFunctionReturn(0);
85460fdc7489SMatthew Knepley }
8547a8fb8f29SToby Isaac 
8548a8fb8f29SToby Isaac /*@
8549a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
8550a8fb8f29SToby Isaac 
8551a8fb8f29SToby Isaac   Input Parameter:
8552a8fb8f29SToby Isaac . dm - The DM object
8553a8fb8f29SToby Isaac 
8554a8fb8f29SToby Isaac   Output Parameter:
8555a8fb8f29SToby Isaac . cdm - The coarse DM
8556a8fb8f29SToby Isaac 
8557a8fb8f29SToby Isaac   Level: intermediate
8558a8fb8f29SToby Isaac 
8559a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
8560a8fb8f29SToby Isaac @*/
8561a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
8562a8fb8f29SToby Isaac {
8563a8fb8f29SToby Isaac   PetscFunctionBegin;
8564a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8565a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
8566a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
8567a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8568a8fb8f29SToby Isaac }
8569a8fb8f29SToby Isaac 
8570a8fb8f29SToby Isaac /*@
8571a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
8572a8fb8f29SToby Isaac 
8573a8fb8f29SToby Isaac   Input Parameters:
8574a8fb8f29SToby Isaac + dm - The DM object
8575a8fb8f29SToby Isaac - cdm - The coarse DM
8576a8fb8f29SToby Isaac 
8577a8fb8f29SToby Isaac   Level: intermediate
8578a8fb8f29SToby Isaac 
8579a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
8580a8fb8f29SToby Isaac @*/
8581a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
8582a8fb8f29SToby Isaac {
8583a8fb8f29SToby Isaac   PetscFunctionBegin;
8584a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8585a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
8586*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject)cdm));
8587*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&dm->coarseMesh));
8588a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
8589a8fb8f29SToby Isaac   PetscFunctionReturn(0);
8590a8fb8f29SToby Isaac }
8591a8fb8f29SToby Isaac 
859288bdff64SToby Isaac /*@
859388bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
859488bdff64SToby Isaac 
859588bdff64SToby Isaac   Input Parameter:
859688bdff64SToby Isaac . dm - The DM object
859788bdff64SToby Isaac 
859888bdff64SToby Isaac   Output Parameter:
859988bdff64SToby Isaac . fdm - The fine DM
860088bdff64SToby Isaac 
860188bdff64SToby Isaac   Level: intermediate
860288bdff64SToby Isaac 
860388bdff64SToby Isaac .seealso: DMSetFineDM()
860488bdff64SToby Isaac @*/
860588bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
860688bdff64SToby Isaac {
860788bdff64SToby Isaac   PetscFunctionBegin;
860888bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
860988bdff64SToby Isaac   PetscValidPointer(fdm, 2);
861088bdff64SToby Isaac   *fdm = dm->fineMesh;
861188bdff64SToby Isaac   PetscFunctionReturn(0);
861288bdff64SToby Isaac }
861388bdff64SToby Isaac 
861488bdff64SToby Isaac /*@
861588bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
861688bdff64SToby Isaac 
861788bdff64SToby Isaac   Input Parameters:
861888bdff64SToby Isaac + dm - The DM object
861988bdff64SToby Isaac - fdm - The fine DM
862088bdff64SToby Isaac 
862188bdff64SToby Isaac   Level: intermediate
862288bdff64SToby Isaac 
862388bdff64SToby Isaac .seealso: DMGetFineDM()
862488bdff64SToby Isaac @*/
862588bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
862688bdff64SToby Isaac {
862788bdff64SToby Isaac   PetscFunctionBegin;
862888bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
862988bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
8630*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject)fdm));
8631*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&dm->fineMesh));
863288bdff64SToby Isaac   dm->fineMesh = fdm;
863388bdff64SToby Isaac   PetscFunctionReturn(0);
863488bdff64SToby Isaac }
863588bdff64SToby Isaac 
8636a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8637a6ba4734SToby Isaac 
8638a6ba4734SToby Isaac /*@C
8639a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8640a6ba4734SToby Isaac 
8641783e2ec8SMatthew G. Knepley   Collective on dm
8642783e2ec8SMatthew G. Knepley 
8643a6ba4734SToby Isaac   Input Parameters:
86444c258f51SMatthew G. Knepley + dm       - The DM, with a PetscDS that matches the problem being constrained
8645f971fd6bSMatthew G. Knepley . type     - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8646a6ba4734SToby Isaac . name     - The BC name
864745480ffeSMatthew G. Knepley . label    - The label defining constrained points
864845480ffeSMatthew G. Knepley . Nv       - The number of DMLabel values for constrained points
864945480ffeSMatthew G. Knepley . values   - An array of values for constrained points
8650a6ba4734SToby Isaac . field    - The field to constrain
865145480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
8652a6ba4734SToby Isaac . comps    - An array of constrained component numbers
8653a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
865456cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
8655a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
8656a6ba4734SToby Isaac 
865745480ffeSMatthew G. Knepley   Output Parameter:
865845480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
865945480ffeSMatthew G. Knepley 
8660a6ba4734SToby Isaac   Options Database Keys:
8661a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8662a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8663a6ba4734SToby Isaac 
866456cf3b9cSMatthew G. Knepley   Note:
866556cf3b9cSMatthew 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:
866656cf3b9cSMatthew G. Knepley 
866756cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
866856cf3b9cSMatthew G. Knepley 
866956cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
867056cf3b9cSMatthew G. Knepley 
867156cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
867256cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
867356cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
867456cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
867556cf3b9cSMatthew G. Knepley 
867656cf3b9cSMatthew G. Knepley + dim - the spatial dimension
867756cf3b9cSMatthew G. Knepley . Nf - the number of fields
867856cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
867956cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
868056cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
868156cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
868256cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
868356cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
868456cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
868556cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
868656cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
868756cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
868856cf3b9cSMatthew G. Knepley . t - current time
868956cf3b9cSMatthew G. Knepley . x - coordinates of the current point
869056cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
869156cf3b9cSMatthew G. Knepley . constants - constant parameters
869256cf3b9cSMatthew G. Knepley - bcval - output values at the current point
869356cf3b9cSMatthew G. Knepley 
8694ed808b8fSJed Brown   Level: intermediate
8695a6ba4734SToby Isaac 
869645480ffeSMatthew G. Knepley .seealso: DSGetBoundary(), PetscDSAddBoundary()
8697a6ba4734SToby Isaac @*/
869845480ffeSMatthew 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)
8699a6ba4734SToby Isaac {
8700e5e52638SMatthew G. Knepley   PetscDS        ds;
8701a6ba4734SToby Isaac 
8702a6ba4734SToby Isaac   PetscFunctionBegin;
8703a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8704783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
870545480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
870645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
870745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
870845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
8709*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDS(dm, &ds));
8710*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, label));
8711*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
8712a6ba4734SToby Isaac   PetscFunctionReturn(0);
8713a6ba4734SToby Isaac }
8714a6ba4734SToby Isaac 
871545480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8716e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8717e6f8dbb6SToby Isaac {
8718e5e52638SMatthew G. Knepley   PetscDS        ds;
8719dff059c6SToby Isaac   DMBoundary    *lastnext;
8720e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8721e6f8dbb6SToby Isaac 
8722e6f8dbb6SToby Isaac   PetscFunctionBegin;
8723*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDS(dm, &ds));
8724e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
872547a1f5adSToby Isaac   if (dm->boundary) {
872647a1f5adSToby Isaac     DMBoundary next = dm->boundary;
872747a1f5adSToby Isaac 
872847a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
872947a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
873047a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
873147a1f5adSToby Isaac     while (next) {
873247a1f5adSToby Isaac       DMBoundary b = next;
873347a1f5adSToby Isaac 
873447a1f5adSToby Isaac       next = b->next;
8735*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree(b));
8736a6ba4734SToby Isaac     }
873747a1f5adSToby Isaac     dm->boundary = NULL;
8738a6ba4734SToby Isaac   }
873947a1f5adSToby Isaac 
8740dff059c6SToby Isaac   lastnext = &(dm->boundary);
8741e6f8dbb6SToby Isaac   while (dsbound) {
8742e6f8dbb6SToby Isaac     DMBoundary dmbound;
8743e6f8dbb6SToby Isaac 
8744*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscNew(&dmbound));
8745e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
874645480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
874747a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8748dff059c6SToby Isaac     *lastnext = dmbound;
8749dff059c6SToby Isaac     lastnext = &(dmbound->next);
8750dff059c6SToby Isaac     dsbound = dsbound->next;
8751a6ba4734SToby Isaac   }
8752a6ba4734SToby Isaac   PetscFunctionReturn(0);
8753a6ba4734SToby Isaac }
8754a6ba4734SToby Isaac 
8755a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8756a6ba4734SToby Isaac {
8757b95f2879SToby Isaac   DMBoundary     b;
8758a6ba4734SToby Isaac 
8759a6ba4734SToby Isaac   PetscFunctionBegin;
8760a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8761534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8762a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8763*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPopulateBoundary(dm));
8764b95f2879SToby Isaac   b = dm->boundary;
8765a6ba4734SToby Isaac   while (b && !(*isBd)) {
8766e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8767e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8768a6ba4734SToby Isaac     PetscInt   i;
8769a6ba4734SToby Isaac 
877045480ffeSMatthew G. Knepley     if (label) {
8771*5f80ce2aSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) CHKERRQ(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8772a6ba4734SToby Isaac     }
8773a6ba4734SToby Isaac     b = b->next;
8774a6ba4734SToby Isaac   }
8775a6ba4734SToby Isaac   PetscFunctionReturn(0);
8776a6ba4734SToby Isaac }
87774d6f44ffSToby Isaac 
87784d6f44ffSToby Isaac /*@C
8779a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8780a6e0b375SMatthew G. Knepley 
8781a6e0b375SMatthew G. Knepley   Collective on DM
87824d6f44ffSToby Isaac 
87834d6f44ffSToby Isaac   Input Parameters:
87844d6f44ffSToby Isaac + dm      - The DM
87850709b2feSToby Isaac . time    - The time
87864d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
87874d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
87884d6f44ffSToby Isaac - mode    - The insertion mode for values
87894d6f44ffSToby Isaac 
87904d6f44ffSToby Isaac   Output Parameter:
87914d6f44ffSToby Isaac . X - vector
87924d6f44ffSToby Isaac 
87934d6f44ffSToby Isaac    Calling sequence of func:
879477b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
87954d6f44ffSToby Isaac 
87964d6f44ffSToby Isaac +  dim - The spatial dimension
87978ec8862eSJed Brown .  time - The time at which to sample
87984d6f44ffSToby Isaac .  x   - The coordinates
879977b739a6SMatthew Knepley .  Nc  - The number of components
88004d6f44ffSToby Isaac .  u   - The output field values
88014d6f44ffSToby Isaac -  ctx - optional user-defined function context
88024d6f44ffSToby Isaac 
88034d6f44ffSToby Isaac   Level: developer
88044d6f44ffSToby Isaac 
8805a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
88064d6f44ffSToby Isaac @*/
88070709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
88084d6f44ffSToby Isaac {
88094d6f44ffSToby Isaac   Vec            localX;
88104d6f44ffSToby Isaac 
88114d6f44ffSToby Isaac   PetscFunctionBegin;
88124d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8813*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalVector(dm, &localX));
8814*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
8815*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLocalToGlobalBegin(dm, localX, mode, X));
8816*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLocalToGlobalEnd(dm, localX, mode, X));
8817*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMRestoreLocalVector(dm, &localX));
88184d6f44ffSToby Isaac   PetscFunctionReturn(0);
88194d6f44ffSToby Isaac }
88204d6f44ffSToby Isaac 
8821a6e0b375SMatthew G. Knepley /*@C
8822a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8823a6e0b375SMatthew G. Knepley 
8824a6e0b375SMatthew G. Knepley   Not collective
8825a6e0b375SMatthew G. Knepley 
8826a6e0b375SMatthew G. Knepley   Input Parameters:
8827a6e0b375SMatthew G. Knepley + dm      - The DM
8828a6e0b375SMatthew G. Knepley . time    - The time
8829a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8830a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8831a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8832a6e0b375SMatthew G. Knepley 
8833a6e0b375SMatthew G. Knepley   Output Parameter:
8834a6e0b375SMatthew G. Knepley . localX - vector
8835a6e0b375SMatthew G. Knepley 
8836a6e0b375SMatthew G. Knepley    Calling sequence of func:
883777b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8838a6e0b375SMatthew G. Knepley 
8839a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8840a6e0b375SMatthew G. Knepley .  x   - The coordinates
884177b739a6SMatthew Knepley .  Nc  - The number of components
8842a6e0b375SMatthew G. Knepley .  u   - The output field values
8843a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8844a6e0b375SMatthew G. Knepley 
8845a6e0b375SMatthew G. Knepley   Level: developer
8846a6e0b375SMatthew G. Knepley 
8847a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8848a6e0b375SMatthew G. Knepley @*/
88490709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
88504d6f44ffSToby Isaac {
88514d6f44ffSToby Isaac   PetscFunctionBegin;
88524d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8853064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
88547a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
8855*5f80ce2aSJacob Faibussowitsch   CHKERRQ((dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX));
88564d6f44ffSToby Isaac   PetscFunctionReturn(0);
88574d6f44ffSToby Isaac }
88584d6f44ffSToby Isaac 
8859a6e0b375SMatthew G. Knepley /*@C
8860a6e0b375SMatthew 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.
8861a6e0b375SMatthew G. Knepley 
8862a6e0b375SMatthew G. Knepley   Collective on DM
8863a6e0b375SMatthew G. Knepley 
8864a6e0b375SMatthew G. Knepley   Input Parameters:
8865a6e0b375SMatthew G. Knepley + dm      - The DM
8866a6e0b375SMatthew G. Knepley . time    - The time
8867a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8868a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8869a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8870a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8871a6e0b375SMatthew G. Knepley 
8872a6e0b375SMatthew G. Knepley   Output Parameter:
8873a6e0b375SMatthew G. Knepley . X - vector
8874a6e0b375SMatthew G. Knepley 
8875a6e0b375SMatthew G. Knepley    Calling sequence of func:
887677b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8877a6e0b375SMatthew G. Knepley 
8878a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8879a6e0b375SMatthew G. Knepley .  x   - The coordinates
888077b739a6SMatthew Knepley .  Nc  - The number of components
8881a6e0b375SMatthew G. Knepley .  u   - The output field values
8882a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8883a6e0b375SMatthew G. Knepley 
8884a6e0b375SMatthew G. Knepley   Level: developer
8885a6e0b375SMatthew G. Knepley 
8886a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8887a6e0b375SMatthew G. Knepley @*/
88882c53366bSMatthew 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)
88892c53366bSMatthew G. Knepley {
88902c53366bSMatthew G. Knepley   Vec            localX;
88912c53366bSMatthew G. Knepley 
88922c53366bSMatthew G. Knepley   PetscFunctionBegin;
88932c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8894*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLocalVector(dm, &localX));
8895*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
8896*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLocalToGlobalBegin(dm, localX, mode, X));
8897*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLocalToGlobalEnd(dm, localX, mode, X));
8898*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMRestoreLocalVector(dm, &localX));
88992c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
89002c53366bSMatthew G. Knepley }
89012c53366bSMatthew G. Knepley 
8902a6e0b375SMatthew G. Knepley /*@C
8903a6e0b375SMatthew 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.
8904a6e0b375SMatthew G. Knepley 
8905a6e0b375SMatthew G. Knepley   Not collective
8906a6e0b375SMatthew G. Knepley 
8907a6e0b375SMatthew G. Knepley   Input Parameters:
8908a6e0b375SMatthew G. Knepley + dm      - The DM
8909a6e0b375SMatthew G. Knepley . time    - The time
8910a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8911a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8912a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8913a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8914a6e0b375SMatthew G. Knepley 
8915a6e0b375SMatthew G. Knepley   Output Parameter:
8916a6e0b375SMatthew G. Knepley . localX - vector
8917a6e0b375SMatthew G. Knepley 
8918a6e0b375SMatthew G. Knepley    Calling sequence of func:
891977b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8920a6e0b375SMatthew G. Knepley 
8921a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8922a6e0b375SMatthew G. Knepley .  x   - The coordinates
892377b739a6SMatthew Knepley .  Nc  - The number of components
8924a6e0b375SMatthew G. Knepley .  u   - The output field values
8925a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8926a6e0b375SMatthew G. Knepley 
8927a6e0b375SMatthew G. Knepley   Level: developer
8928a6e0b375SMatthew G. Knepley 
8929a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8930a6e0b375SMatthew G. Knepley @*/
89311c531cf8SMatthew 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)
89324d6f44ffSToby Isaac {
89334d6f44ffSToby Isaac   PetscFunctionBegin;
89344d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8935064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
89367a8be351SBarry Smith   PetscCheck(dm->ops->projectfunctionlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
8937*5f80ce2aSJacob Faibussowitsch   CHKERRQ((dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
89384d6f44ffSToby Isaac   PetscFunctionReturn(0);
89394d6f44ffSToby Isaac }
89402716604bSToby Isaac 
8941a6e0b375SMatthew G. Knepley /*@C
8942a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8943a6e0b375SMatthew G. Knepley 
8944a6e0b375SMatthew G. Knepley   Not collective
8945a6e0b375SMatthew G. Knepley 
8946a6e0b375SMatthew G. Knepley   Input Parameters:
8947a6e0b375SMatthew G. Knepley + dm      - The DM
8948a6e0b375SMatthew G. Knepley . time    - The time
8949a6e0b375SMatthew G. Knepley . localU  - The input field vector
8950a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8951a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8952a6e0b375SMatthew G. Knepley 
8953a6e0b375SMatthew G. Knepley   Output Parameter:
8954a6e0b375SMatthew G. Knepley . localX  - The output vector
8955a6e0b375SMatthew G. Knepley 
8956a6e0b375SMatthew G. Knepley    Calling sequence of func:
8957a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8958a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8959a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8960a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8961a6e0b375SMatthew G. Knepley 
8962a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8963a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8964a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8965a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8966a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8967a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8968a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8969a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8970a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8971a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8972a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8973a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8974a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8975a6e0b375SMatthew G. Knepley .  t            - The current time
8976a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8977a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8978a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8979a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8980a6e0b375SMatthew G. Knepley 
8981a6e0b375SMatthew 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.
8982a6e0b375SMatthew 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
8983a6e0b375SMatthew 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
8984a6e0b375SMatthew 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.
8985a6e0b375SMatthew G. Knepley 
8986a6e0b375SMatthew G. Knepley   Level: intermediate
8987a6e0b375SMatthew G. Knepley 
8988a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8989a6e0b375SMatthew G. Knepley @*/
89908c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
89918c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
89928c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
89938c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8994191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
89958c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
89968c6c5593SMatthew G. Knepley {
89978c6c5593SMatthew G. Knepley   PetscFunctionBegin;
89988c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
89998c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
90008c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
90017a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
9002*5f80ce2aSJacob Faibussowitsch   CHKERRQ((dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX));
90038c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
90048c6c5593SMatthew G. Knepley }
90058c6c5593SMatthew G. Knepley 
9006a6e0b375SMatthew G. Knepley /*@C
9007a6e0b375SMatthew 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.
9008a6e0b375SMatthew G. Knepley 
9009a6e0b375SMatthew G. Knepley   Not collective
9010a6e0b375SMatthew G. Knepley 
9011a6e0b375SMatthew G. Knepley   Input Parameters:
9012a6e0b375SMatthew G. Knepley + dm      - The DM
9013a6e0b375SMatthew G. Knepley . time    - The time
9014a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
9015a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
9016a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
9017a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9018a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9019a6e0b375SMatthew G. Knepley . localU  - The input field vector
9020a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9021a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
9022a6e0b375SMatthew G. Knepley 
9023a6e0b375SMatthew G. Knepley   Output Parameter:
9024a6e0b375SMatthew G. Knepley . localX  - The output vector
9025a6e0b375SMatthew G. Knepley 
9026a6e0b375SMatthew G. Knepley    Calling sequence of func:
9027a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9028a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9029a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9030a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9031a6e0b375SMatthew G. Knepley 
9032a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
9033a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
9034a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9035a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
9036a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9037a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
9038a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9039a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9040a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9041a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9042a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9043a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9044a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9045a6e0b375SMatthew G. Knepley .  t            - The current time
9046a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
9047a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
9048a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
9049a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
9050a6e0b375SMatthew G. Knepley 
9051a6e0b375SMatthew 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.
9052a6e0b375SMatthew 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
9053a6e0b375SMatthew 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
9054a6e0b375SMatthew 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.
9055a6e0b375SMatthew G. Knepley 
9056a6e0b375SMatthew G. Knepley   Level: intermediate
9057a6e0b375SMatthew G. Knepley 
9058a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9059a6e0b375SMatthew G. Knepley @*/
90601c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
90618c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
90628c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
90638c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9064191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
90658c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
90668c6c5593SMatthew G. Knepley {
90678c6c5593SMatthew G. Knepley   PetscFunctionBegin;
90688c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9069064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9070064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
90717a8be351SBarry Smith   PetscCheck(dm->ops->projectfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
9072*5f80ce2aSJacob Faibussowitsch   CHKERRQ((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
90738c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
90748c6c5593SMatthew G. Knepley }
90758c6c5593SMatthew G. Knepley 
90762716604bSToby Isaac /*@C
9077ece3a9fcSMatthew 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.
9078ece3a9fcSMatthew G. Knepley 
9079ece3a9fcSMatthew G. Knepley   Not collective
9080ece3a9fcSMatthew G. Knepley 
9081ece3a9fcSMatthew G. Knepley   Input Parameters:
9082ece3a9fcSMatthew G. Knepley + dm      - The DM
9083ece3a9fcSMatthew G. Knepley . time    - The time
9084ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
9085ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
9086ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
9087ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
9088ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
9089ece3a9fcSMatthew G. Knepley . localU  - The input field vector
9090ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
9091ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
9092ece3a9fcSMatthew G. Knepley 
9093ece3a9fcSMatthew G. Knepley   Output Parameter:
9094ece3a9fcSMatthew G. Knepley . localX  - The output vector
9095ece3a9fcSMatthew G. Knepley 
9096ece3a9fcSMatthew G. Knepley    Calling sequence of func:
9097ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
9098ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
9099ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
9100ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
9101ece3a9fcSMatthew G. Knepley 
9102ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
9103ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
9104ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
9105ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
9106ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
9107ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
9108ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
9109ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
9110ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
9111ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
9112ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
9113ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
9114ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
9115ece3a9fcSMatthew G. Knepley .  t            - The current time
9116ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
9117ece3a9fcSMatthew G. Knepley .  n            - The face normal
9118ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
9119ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
9120ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
9121ece3a9fcSMatthew G. Knepley 
9122ece3a9fcSMatthew G. Knepley   Note:
9123ece3a9fcSMatthew 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.
9124ece3a9fcSMatthew 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
9125ece3a9fcSMatthew 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
9126ece3a9fcSMatthew 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.
9127ece3a9fcSMatthew G. Knepley 
9128ece3a9fcSMatthew G. Knepley   Level: intermediate
9129ece3a9fcSMatthew G. Knepley 
9130ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
9131ece3a9fcSMatthew G. Knepley @*/
9132ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
9133ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
9134ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9135ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
9136ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
9137ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
9138ece3a9fcSMatthew G. Knepley {
9139ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
9140ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9141064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU,VEC_CLASSID,8);
9142064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX,VEC_CLASSID,11);
91437a8be351SBarry Smith   PetscCheck(dm->ops->projectbdfieldlabellocal,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
9144*5f80ce2aSJacob Faibussowitsch   CHKERRQ((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
9145ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
9146ece3a9fcSMatthew G. Knepley }
9147ece3a9fcSMatthew G. Knepley 
9148ece3a9fcSMatthew G. Knepley /*@C
91492716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
91502716604bSToby Isaac 
91512716604bSToby Isaac   Input Parameters:
91522716604bSToby Isaac + dm    - The DM
91530709b2feSToby Isaac . time  - The time
91542716604bSToby Isaac . funcs - The functions to evaluate for each field component
91552716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9156574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
91572716604bSToby Isaac 
91582716604bSToby Isaac   Output Parameter:
91592716604bSToby Isaac . diff - The diff ||u - u_h||_2
91602716604bSToby Isaac 
91612716604bSToby Isaac   Level: developer
91622716604bSToby Isaac 
91631189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
91642716604bSToby Isaac @*/
91650709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
91662716604bSToby Isaac {
91672716604bSToby Isaac   PetscFunctionBegin;
91682716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9169b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
91707a8be351SBarry Smith   PetscCheck(dm->ops->computel2diff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
9171*5f80ce2aSJacob Faibussowitsch   CHKERRQ((dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff));
91722716604bSToby Isaac   PetscFunctionReturn(0);
91732716604bSToby Isaac }
9174b698f381SToby Isaac 
9175b698f381SToby Isaac /*@C
9176b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
9177b698f381SToby Isaac 
9178d083f849SBarry Smith   Collective on dm
9179d083f849SBarry Smith 
9180b698f381SToby Isaac   Input Parameters:
9181b698f381SToby Isaac + dm    - The DM
9182b698f381SToby Isaac , time  - The time
9183b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
9184b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9185574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
9186b698f381SToby Isaac - n     - The vector to project along
9187b698f381SToby Isaac 
9188b698f381SToby Isaac   Output Parameter:
9189b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
9190b698f381SToby Isaac 
9191b698f381SToby Isaac   Level: developer
9192b698f381SToby Isaac 
9193b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
9194b698f381SToby Isaac @*/
9195b698f381SToby 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)
9196b698f381SToby Isaac {
9197b698f381SToby Isaac   PetscFunctionBegin;
9198b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9199b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
92007a8be351SBarry Smith   PetscCheck(dm->ops->computel2gradientdiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
9201*5f80ce2aSJacob Faibussowitsch   CHKERRQ((dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff));
9202b698f381SToby Isaac   PetscFunctionReturn(0);
9203b698f381SToby Isaac }
9204b698f381SToby Isaac 
92052a16baeaSToby Isaac /*@C
92062a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
92072a16baeaSToby Isaac 
9208d083f849SBarry Smith   Collective on dm
9209d083f849SBarry Smith 
92102a16baeaSToby Isaac   Input Parameters:
92112a16baeaSToby Isaac + dm    - The DM
92122a16baeaSToby Isaac . time  - The time
92132a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
92142a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
9215574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
92162a16baeaSToby Isaac 
92172a16baeaSToby Isaac   Output Parameter:
92182a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
92192a16baeaSToby Isaac 
92202a16baeaSToby Isaac   Level: developer
92212a16baeaSToby Isaac 
92221189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
92232a16baeaSToby Isaac @*/
92241189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
92252a16baeaSToby Isaac {
92262a16baeaSToby Isaac   PetscFunctionBegin;
92272a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
92282a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
92297a8be351SBarry Smith   PetscCheck(dm->ops->computel2fielddiff,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
9230*5f80ce2aSJacob Faibussowitsch   CHKERRQ((dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff));
92312a16baeaSToby Isaac   PetscFunctionReturn(0);
92322a16baeaSToby Isaac }
92332a16baeaSToby Isaac 
9234df0b854cSToby Isaac /*@C
9235502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
9236502a2867SDave May 
9237502a2867SDave May  Not Collective
9238502a2867SDave May 
9239502a2867SDave May  Input Parameter:
9240502a2867SDave May .  dm    - The DM
9241502a2867SDave May 
92420a19bb7dSprj-  Output Parameters:
92430a19bb7dSprj- +  nranks - the number of neighbours
92440a19bb7dSprj- -  ranks - the neighbors ranks
9245502a2867SDave May 
9246502a2867SDave May  Notes:
9247502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
9248502a2867SDave May 
9249502a2867SDave May  Level: beginner
9250502a2867SDave May 
9251dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
9252502a2867SDave May @*/
9253502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
9254502a2867SDave May {
9255502a2867SDave May   PetscFunctionBegin;
9256502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
92577a8be351SBarry Smith   PetscCheck(dm->ops->getneighbors,PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
9258*5f80ce2aSJacob Faibussowitsch   CHKERRQ((dm->ops->getneighbors)(dm,nranks,ranks));
9259502a2867SDave May   PetscFunctionReturn(0);
9260502a2867SDave May }
9261502a2867SDave May 
9262531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
9263531c7667SBarry Smith 
9264531c7667SBarry Smith /*
9265531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
9266531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
9267531c7667SBarry Smith */
9268531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
9269531c7667SBarry Smith {
9270531c7667SBarry Smith   PetscFunctionBegin;
9271531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9272531c7667SBarry Smith     Vec x1local;
9273531c7667SBarry Smith     DM  dm;
9274*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatGetDM(J,&dm));
92757a8be351SBarry Smith     PetscCheck(dm,PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
9276*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLocalVector(dm,&x1local));
9277*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local));
9278*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local));
9279531c7667SBarry Smith     x1   = x1local;
9280531c7667SBarry Smith   }
9281*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatFDColoringApply_AIJ(J,coloring,x1,sctx));
9282531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
9283531c7667SBarry Smith     DM  dm;
9284*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatGetDM(J,&dm));
9285*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMRestoreLocalVector(dm,&x1));
9286531c7667SBarry Smith   }
9287531c7667SBarry Smith   PetscFunctionReturn(0);
9288531c7667SBarry Smith }
9289531c7667SBarry Smith 
9290531c7667SBarry Smith /*@
9291531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
9292531c7667SBarry Smith 
9293531c7667SBarry Smith     Input Parameter:
9294531c7667SBarry Smith .    coloring - the MatFDColoring object
9295531c7667SBarry Smith 
929695452b02SPatrick Sanan     Developer Notes:
929795452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
9298531c7667SBarry Smith 
92991b266c99SBarry Smith     Level: advanced
93001b266c99SBarry Smith 
9301531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
9302531c7667SBarry Smith @*/
9303531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
9304531c7667SBarry Smith {
9305531c7667SBarry Smith   PetscFunctionBegin;
9306531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
9307531c7667SBarry Smith   PetscFunctionReturn(0);
9308531c7667SBarry Smith }
93098320bc6fSPatrick Sanan 
93108320bc6fSPatrick Sanan /*@
93118320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
93128320bc6fSPatrick Sanan 
93138320bc6fSPatrick Sanan     Collective
93148320bc6fSPatrick Sanan 
93158320bc6fSPatrick Sanan     Input Parameters:
9316a5bc1bf3SBarry Smith +    dm1 - the first DM
93178320bc6fSPatrick Sanan -    dm2 - the second DM
93188320bc6fSPatrick Sanan 
93198320bc6fSPatrick Sanan     Output Parameters:
93208320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
93218320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
93228320bc6fSPatrick Sanan 
93238320bc6fSPatrick Sanan     Notes:
93248320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
93253d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
93268320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
93273d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
93283d862458SPatrick Sanan     decomposition, but hold different data.
93298320bc6fSPatrick Sanan 
93308320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
93318320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
93328320bc6fSPatrick Sanan 
93338320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
93348320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
93358320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
93368320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
93378320bc6fSPatrick Sanan 
93388320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
93398320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
93408320bc6fSPatrick Sanan .vb
93418320bc6fSPatrick Sanan   ...
9342*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCompatibility(da1,da2,&compatible,&set));
93438320bc6fSPatrick Sanan   if (set && compatible)  {
9344*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMDAVecGetArrayDOF(da1,vec1,&arr1));
9345*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMDAVecGetArrayDOF(da2,vec2,&arr2));
9346*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
93478320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
93488320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
93498320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
93508320bc6fSPatrick Sanan       }
93518320bc6fSPatrick Sanan     }
9352*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
9353*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
93548320bc6fSPatrick Sanan   } else {
93558320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
93568320bc6fSPatrick Sanan   }
93578320bc6fSPatrick Sanan   ...
93588320bc6fSPatrick Sanan .ve
93598320bc6fSPatrick Sanan 
93608320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
93618320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
93628320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
93638320bc6fSPatrick Sanan     always check the "set" output parameter.
93648320bc6fSPatrick Sanan 
93658320bc6fSPatrick Sanan     A DM is always compatible with itself.
93668320bc6fSPatrick Sanan 
93678320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
93688320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
93698320bc6fSPatrick Sanan     incompatible.
93708320bc6fSPatrick Sanan 
93718320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
93728320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
93738320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
93748320bc6fSPatrick Sanan 
93758320bc6fSPatrick Sanan     Developer Notes:
93763d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
93773d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
9378a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
93798320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
93808320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
93813d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
93823d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
93838320bc6fSPatrick Sanan 
93848320bc6fSPatrick Sanan     Level: advanced
93858320bc6fSPatrick Sanan 
93863d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
93878320bc6fSPatrick Sanan @*/
93888320bc6fSPatrick Sanan 
9389a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
93908320bc6fSPatrick Sanan {
93918320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
93928320bc6fSPatrick Sanan   DMType         type,type2;
93938320bc6fSPatrick Sanan   PetscBool      sameType;
93948320bc6fSPatrick Sanan 
93958320bc6fSPatrick Sanan   PetscFunctionBegin;
9396a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
93978320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
93988320bc6fSPatrick Sanan 
93998320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
9400a5bc1bf3SBarry Smith   if (dm1 == dm2) {
94018320bc6fSPatrick Sanan     *set = PETSC_TRUE;
94028320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
94038320bc6fSPatrick Sanan     PetscFunctionReturn(0);
94048320bc6fSPatrick Sanan   }
94058320bc6fSPatrick Sanan 
94068320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
94078320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
94088320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
94098320bc6fSPatrick Sanan      determined by the implementation-specific logic */
9410*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult));
94118320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
94128320bc6fSPatrick Sanan     *set = PETSC_TRUE;
94138320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
94148320bc6fSPatrick Sanan     PetscFunctionReturn(0);
94158320bc6fSPatrick Sanan   }
94168320bc6fSPatrick Sanan 
94178320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
9418a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
9419*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm1->ops->getcompatibility)(dm1,dm2,compatible,set));
9420b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
94218320bc6fSPatrick Sanan   }
94228320bc6fSPatrick Sanan 
9423a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
94248320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
9425*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetType(dm1,&type));
9426*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetType(dm2,&type2));
9427*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrcmp(type,type2,&sameType));
94288320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
9429*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm2->ops->getcompatibility)(dm2,dm1,compatible,set)); /* Note argument order */
94308320bc6fSPatrick Sanan   } else {
94318320bc6fSPatrick Sanan     *set = PETSC_FALSE;
94328320bc6fSPatrick Sanan   }
94338320bc6fSPatrick Sanan   PetscFunctionReturn(0);
94348320bc6fSPatrick Sanan }
9435c0f0dcc3SMatthew G. Knepley 
9436c0f0dcc3SMatthew G. Knepley /*@C
9437c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
9438c0f0dcc3SMatthew G. Knepley 
9439c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9440c0f0dcc3SMatthew G. Knepley 
9441c0f0dcc3SMatthew G. Knepley   Input Parameters:
9442c0f0dcc3SMatthew G. Knepley + DM - the DM
9443c0f0dcc3SMatthew G. Knepley . f - the monitor function
9444c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
9445c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
9446c0f0dcc3SMatthew G. Knepley 
9447c0f0dcc3SMatthew G. Knepley   Options Database Keys:
9448c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
9449c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
9450c0f0dcc3SMatthew G. Knepley 
9451c0f0dcc3SMatthew G. Knepley   Notes:
9452c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
9453c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
9454c0f0dcc3SMatthew G. Knepley   order in which they were set.
9455c0f0dcc3SMatthew G. Knepley 
9456c0f0dcc3SMatthew G. Knepley   Fortran Notes:
9457c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
9458c0f0dcc3SMatthew G. Knepley 
9459c0f0dcc3SMatthew G. Knepley   Level: intermediate
9460c0f0dcc3SMatthew G. Knepley 
9461c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
9462c0f0dcc3SMatthew G. Knepley @*/
9463c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
9464c0f0dcc3SMatthew G. Knepley {
9465c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9466c0f0dcc3SMatthew G. Knepley 
9467c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9468c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9469c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9470c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9471c0f0dcc3SMatthew G. Knepley 
9472*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
9473c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9474c0f0dcc3SMatthew G. Knepley   }
94757a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9476c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9477c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9478c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9479c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9480c0f0dcc3SMatthew G. Knepley }
9481c0f0dcc3SMatthew G. Knepley 
9482c0f0dcc3SMatthew G. Knepley /*@
9483c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9484c0f0dcc3SMatthew G. Knepley 
9485c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9486c0f0dcc3SMatthew G. Knepley 
9487c0f0dcc3SMatthew G. Knepley   Input Parameter:
9488c0f0dcc3SMatthew G. Knepley . dm - the DM
9489c0f0dcc3SMatthew G. Knepley 
9490c0f0dcc3SMatthew G. Knepley   Options Database Key:
9491c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9492c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9493c0f0dcc3SMatthew G. Knepley   set via the options database
9494c0f0dcc3SMatthew G. Knepley 
9495c0f0dcc3SMatthew G. Knepley   Notes:
9496c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9497c0f0dcc3SMatthew G. Knepley 
9498c0f0dcc3SMatthew G. Knepley   Level: intermediate
9499c0f0dcc3SMatthew G. Knepley 
9500c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9501c0f0dcc3SMatthew G. Knepley @*/
9502c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9503c0f0dcc3SMatthew G. Knepley {
9504c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9505c0f0dcc3SMatthew G. Knepley 
9506c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9507c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9508c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9509*5f80ce2aSJacob Faibussowitsch     if (dm->monitordestroy[m]) CHKERRQ((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
9510c0f0dcc3SMatthew G. Knepley   }
9511c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9512c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9513c0f0dcc3SMatthew G. Knepley }
9514c0f0dcc3SMatthew G. Knepley 
9515c0f0dcc3SMatthew G. Knepley /*@C
9516c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9517c0f0dcc3SMatthew G. Knepley 
9518c0f0dcc3SMatthew G. Knepley   Collective on DM
9519c0f0dcc3SMatthew G. Knepley 
9520c0f0dcc3SMatthew G. Knepley   Input Parameters:
9521c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9522c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9523c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9524c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9525c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9526c0f0dcc3SMatthew 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
9527c0f0dcc3SMatthew G. Knepley 
9528c0f0dcc3SMatthew G. Knepley   Output Parameter:
9529c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9530c0f0dcc3SMatthew G. Knepley 
9531c0f0dcc3SMatthew G. Knepley   Level: developer
9532c0f0dcc3SMatthew G. Knepley 
9533c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9534c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9535c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9536c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9537c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9538c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9539c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9540c0f0dcc3SMatthew G. Knepley @*/
9541c0f0dcc3SMatthew 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)
9542c0f0dcc3SMatthew G. Knepley {
9543c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9544c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9545c0f0dcc3SMatthew G. Knepley 
9546c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9547c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9548*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg));
9549c0f0dcc3SMatthew G. Knepley   if (*flg) {
9550c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9551c0f0dcc3SMatthew G. Knepley 
9552*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerAndFormatCreate(viewer, format, &vf));
9553*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectDereference((PetscObject) viewer));
9554*5f80ce2aSJacob Faibussowitsch     if (monitorsetup) CHKERRQ((*monitorsetup)(dm, vf));
9555*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy));
9556c0f0dcc3SMatthew G. Knepley   }
9557c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9558c0f0dcc3SMatthew G. Knepley }
9559c0f0dcc3SMatthew G. Knepley 
9560c0f0dcc3SMatthew G. Knepley /*@
9561c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9562c0f0dcc3SMatthew G. Knepley 
9563c0f0dcc3SMatthew G. Knepley    Collective on DM
9564c0f0dcc3SMatthew G. Knepley 
9565c0f0dcc3SMatthew G. Knepley    Input Parameters:
9566c0f0dcc3SMatthew G. Knepley .  dm - The DM
9567c0f0dcc3SMatthew G. Knepley 
9568c0f0dcc3SMatthew G. Knepley    Level: developer
9569c0f0dcc3SMatthew G. Knepley 
9570c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9571c0f0dcc3SMatthew G. Knepley @*/
9572c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9573c0f0dcc3SMatthew G. Knepley {
9574c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9575c0f0dcc3SMatthew G. Knepley 
9576c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9577c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9578c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9579c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9580*5f80ce2aSJacob Faibussowitsch     CHKERRQ((*dm->monitor[m])(dm, dm->monitorcontext[m]));
9581c0f0dcc3SMatthew G. Knepley   }
9582c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9583c0f0dcc3SMatthew G. Knepley }
95842e4af2aeSMatthew G. Knepley 
95852e4af2aeSMatthew G. Knepley /*@
95862e4af2aeSMatthew G. Knepley   DMComputeError - Computes the error assuming the user has given exact solution functions
95872e4af2aeSMatthew G. Knepley 
95882e4af2aeSMatthew G. Knepley   Collective on DM
95892e4af2aeSMatthew G. Knepley 
95902e4af2aeSMatthew G. Knepley   Input Parameters:
95912e4af2aeSMatthew G. Knepley + dm     - The DM
95926b867d5aSJose E. Roman - sol    - The solution vector
95932e4af2aeSMatthew G. Knepley 
95946b867d5aSJose E. Roman   Input/Output Parameter:
95956b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
95966b867d5aSJose E. Roman            contains the error in each field
95976b867d5aSJose E. Roman 
95986b867d5aSJose E. Roman   Output Parameter:
95996b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
96002e4af2aeSMatthew G. Knepley 
96012e4af2aeSMatthew G. Knepley   Note: The exact solutions come from the PetscDS object, and the time comes from DMGetOutputSequenceNumber().
96022e4af2aeSMatthew G. Knepley 
96032e4af2aeSMatthew G. Knepley   Level: developer
96042e4af2aeSMatthew G. Knepley 
96052e4af2aeSMatthew G. Knepley .seealso: DMMonitorSet(), DMGetRegionNumDS(), PetscDSGetExactSolution(), DMGetOutputSequenceNumber()
96062e4af2aeSMatthew G. Knepley @*/
96072e4af2aeSMatthew G. Knepley PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
96082e4af2aeSMatthew G. Knepley {
96092e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
96102e4af2aeSMatthew G. Knepley   void            **ctxs;
96112e4af2aeSMatthew G. Knepley   PetscReal         time;
96122e4af2aeSMatthew G. Knepley   PetscInt          Nf, f, Nds, s;
96132e4af2aeSMatthew G. Knepley 
96142e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
9615*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumFields(dm, &Nf));
9616*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
9617*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumDS(dm, &Nds));
96182e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
96192e4af2aeSMatthew G. Knepley     PetscDS         ds;
96202e4af2aeSMatthew G. Knepley     DMLabel         label;
96212e4af2aeSMatthew G. Knepley     IS              fieldIS;
96222e4af2aeSMatthew G. Knepley     const PetscInt *fields;
96232e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
96242e4af2aeSMatthew G. Knepley 
9625*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
9626*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscDSGetNumFields(ds, &dsNf));
9627*5f80ce2aSJacob Faibussowitsch     if (fieldIS) CHKERRQ(ISGetIndices(fieldIS, &fields));
96282e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
96292e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
9630*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
96312e4af2aeSMatthew G. Knepley     }
9632*5f80ce2aSJacob Faibussowitsch     if (fieldIS) CHKERRQ(ISRestoreIndices(fieldIS, &fields));
96332e4af2aeSMatthew G. Knepley   }
96342e4af2aeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
96357a8be351SBarry 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);
96362e4af2aeSMatthew G. Knepley   }
9637*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetOutputSequenceNumber(dm, NULL, &time));
9638*5f80ce2aSJacob Faibussowitsch   if (errors) CHKERRQ(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
96392e4af2aeSMatthew G. Knepley   if (errorVec) {
96402e4af2aeSMatthew G. Knepley     DM             edm;
96412e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
96422e4af2aeSMatthew G. Knepley     PetscBool      simplex;
96432e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
96442e4af2aeSMatthew G. Knepley 
9645*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMClone(dm, &edm));
9646*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetDimension(edm, &dim));
9647*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
9648*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetCellType(dm, cStart, &ct));
96492e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
9650*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetNumFields(dm, &Nf));
96512e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
96522e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
96532e4af2aeSMatthew G. Knepley       PetscQuadrature q;
96542e4af2aeSMatthew G. Knepley       const char     *name;
96552e4af2aeSMatthew G. Knepley 
9656*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetField(dm, f, NULL, (PetscObject *) &fe));
9657*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
9658*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscObjectGetName((PetscObject) fe, &name));
9659*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscObjectSetName((PetscObject) efe, name));
9660*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFEGetQuadrature(fe, &q));
9661*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFESetQuadrature(efe, q));
9662*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMSetField(edm, f, NULL, (PetscObject) efe));
9663*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFEDestroy(&efe));
96642e4af2aeSMatthew G. Knepley     }
9665*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateDS(edm));
96662e4af2aeSMatthew G. Knepley 
9667*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateGlobalVector(edm, errorVec));
9668*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject) *errorVec, "Error"));
9669*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
9670*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMDestroy(&edm));
96712e4af2aeSMatthew G. Knepley   }
9672*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree2(exactSol, ctxs));
96732e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
96742e4af2aeSMatthew G. Knepley }
96759a2a23afSMatthew G. Knepley 
96769a2a23afSMatthew G. Knepley /*@
96779a2a23afSMatthew G. Knepley   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this DM
96789a2a23afSMatthew G. Knepley 
96799a2a23afSMatthew G. Knepley   Not collective
96809a2a23afSMatthew G. Knepley 
96819a2a23afSMatthew G. Knepley   Input Parameter:
96829a2a23afSMatthew G. Knepley . dm     - The DM
96839a2a23afSMatthew G. Knepley 
96849a2a23afSMatthew G. Knepley   Output Parameter:
9685a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
96869a2a23afSMatthew G. Knepley 
96879a2a23afSMatthew G. Knepley   Level: advanced
96889a2a23afSMatthew G. Knepley 
96899a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryLabels(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
96909a2a23afSMatthew G. Knepley @*/
96919a2a23afSMatthew G. Knepley PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
96929a2a23afSMatthew G. Knepley {
96939a2a23afSMatthew G. Knepley   PetscFunctionBegin;
96949a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9695*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHMapAuxGetSize(dm->auxData, numAux));
96969a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
96979a2a23afSMatthew G. Knepley }
96989a2a23afSMatthew G. Knepley 
96999a2a23afSMatthew G. Knepley /*@
9700ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
97019a2a23afSMatthew G. Knepley 
97029a2a23afSMatthew G. Knepley   Not collective
97039a2a23afSMatthew G. Knepley 
97049a2a23afSMatthew G. Knepley   Input Parameters:
97059a2a23afSMatthew G. Knepley + dm     - The DM
97069a2a23afSMatthew G. Knepley . label  - The DMLabel
9707ac17215fSMatthew G. Knepley . value  - The label value indicating the region
9708ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
97099a2a23afSMatthew G. Knepley 
97109a2a23afSMatthew G. Knepley   Output Parameter:
97119a2a23afSMatthew G. Knepley . aux    - The Vec holding auxiliary field data
97129a2a23afSMatthew G. Knepley 
9713ac17215fSMatthew G. Knepley   Note: If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
971404c51a94SMatthew G. Knepley 
97159a2a23afSMatthew G. Knepley   Level: advanced
97169a2a23afSMatthew G. Knepley 
97179a2a23afSMatthew G. Knepley .seealso: DMSetAuxiliaryVec(), DMGetNumAuxiliaryVec()
97189a2a23afSMatthew G. Knepley @*/
9719ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
97209a2a23afSMatthew G. Knepley {
9721ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
972204c51a94SMatthew G. Knepley   PetscBool       has;
97239a2a23afSMatthew G. Knepley 
97249a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97259a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97269a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
97279a2a23afSMatthew G. Knepley   key.label = label;
97289a2a23afSMatthew G. Knepley   key.value = value;
9729ac17215fSMatthew G. Knepley   key.part  = part;
9730*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHMapAuxHas(dm->auxData, key, &has));
9731*5f80ce2aSJacob Faibussowitsch   if (has) CHKERRQ(PetscHMapAuxGet(dm->auxData, key,  aux));
9732*5f80ce2aSJacob Faibussowitsch   else     CHKERRQ(PetscHMapAuxGet(dm->auxData, wild, aux));
97339a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
97349a2a23afSMatthew G. Knepley }
97359a2a23afSMatthew G. Knepley 
97369a2a23afSMatthew G. Knepley /*@
9737ac17215fSMatthew G. Knepley   DMSetAuxiliaryVec - Set the auxiliary vector for region specified by the given label and value, and equation part
97389a2a23afSMatthew G. Knepley 
97399a2a23afSMatthew G. Knepley   Not collective
97409a2a23afSMatthew G. Knepley 
97419a2a23afSMatthew G. Knepley   Input Parameters:
97429a2a23afSMatthew G. Knepley + dm     - The DM
97439a2a23afSMatthew G. Knepley . label  - The DMLabel
97449a2a23afSMatthew G. Knepley . value  - The label value indicating the region
9745ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
97469a2a23afSMatthew G. Knepley - aux    - The Vec holding auxiliary field data
97479a2a23afSMatthew G. Knepley 
97489a2a23afSMatthew G. Knepley   Level: advanced
97499a2a23afSMatthew G. Knepley 
97509a2a23afSMatthew G. Knepley .seealso: DMGetAuxiliaryVec()
97519a2a23afSMatthew G. Knepley @*/
9752ac17215fSMatthew G. Knepley PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
97539a2a23afSMatthew G. Knepley {
97549a2a23afSMatthew G. Knepley   Vec             old;
97559a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
97569a2a23afSMatthew G. Knepley 
97579a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97589a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97599a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
97609a2a23afSMatthew G. Knepley   key.label = label;
97619a2a23afSMatthew G. Knepley   key.value = value;
9762ac17215fSMatthew G. Knepley   key.part  = part;
9763*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHMapAuxGet(dm->auxData, key, &old));
9764*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) aux));
9765*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectDereference((PetscObject) old));
9766*5f80ce2aSJacob Faibussowitsch   if (!aux) CHKERRQ(PetscHMapAuxDel(dm->auxData, key));
9767*5f80ce2aSJacob Faibussowitsch   else      CHKERRQ(PetscHMapAuxSet(dm->auxData, key, aux));
97689a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
97699a2a23afSMatthew G. Knepley }
97709a2a23afSMatthew G. Knepley 
97719a2a23afSMatthew G. Knepley /*@C
9772ac17215fSMatthew G. Knepley   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this DM
97739a2a23afSMatthew G. Knepley 
97749a2a23afSMatthew G. Knepley   Not collective
97759a2a23afSMatthew G. Knepley 
97769a2a23afSMatthew G. Knepley   Input Parameter:
97779a2a23afSMatthew G. Knepley . dm      - The DM
97789a2a23afSMatthew G. Knepley 
97799a2a23afSMatthew G. Knepley   Output Parameters:
97809a2a23afSMatthew G. Knepley + labels  - The DMLabels for each Vec
9781ac17215fSMatthew G. Knepley . values  - The label values for each Vec
9782ac17215fSMatthew G. Knepley - parts   - The equation parts for each Vec
97839a2a23afSMatthew G. Knepley 
97849a2a23afSMatthew G. Knepley   Note: The arrays passed in must be at least as large as DMGetNumAuxiliaryVec().
97859a2a23afSMatthew G. Knepley 
97869a2a23afSMatthew G. Knepley   Level: advanced
97879a2a23afSMatthew G. Knepley 
97889a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
97899a2a23afSMatthew G. Knepley @*/
9790ac17215fSMatthew G. Knepley PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
97919a2a23afSMatthew G. Knepley {
97929a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
97939a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
97949a2a23afSMatthew G. Knepley 
97959a2a23afSMatthew G. Knepley   PetscFunctionBegin;
97969a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97979a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
97989a2a23afSMatthew G. Knepley   PetscValidPointer(values, 3);
9799ac17215fSMatthew G. Knepley   PetscValidPointer(parts,  4);
9800*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumAuxiliaryVec(dm, &n));
9801*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(n, &keys));
9802*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
9803ac17215fSMatthew G. Knepley   for (i = 0; i < n; ++i) {labels[i] = keys[i].label; values[i] = keys[i].value; parts[i] = keys[i].part;}
9804*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(keys));
98059a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98069a2a23afSMatthew G. Knepley }
98079a2a23afSMatthew G. Knepley 
98089a2a23afSMatthew G. Knepley /*@
98099a2a23afSMatthew G. Knepley   DMCopyAuxiliaryVec - Copy the auxiliary data to a new DM
98109a2a23afSMatthew G. Knepley 
98119a2a23afSMatthew G. Knepley   Not collective
98129a2a23afSMatthew G. Knepley 
98139a2a23afSMatthew G. Knepley   Input Parameter:
98149a2a23afSMatthew G. Knepley . dm    - The DM
98159a2a23afSMatthew G. Knepley 
98169a2a23afSMatthew G. Knepley   Output Parameter:
98179a2a23afSMatthew G. Knepley . dmNew - The new DM, now with the same auxiliary data
98189a2a23afSMatthew G. Knepley 
98199a2a23afSMatthew G. Knepley   Level: advanced
98209a2a23afSMatthew G. Knepley 
98219a2a23afSMatthew G. Knepley .seealso: DMGetNumAuxiliaryVec(), DMGetAuxiliaryVec(), DMSetAuxiliaryVec()
98229a2a23afSMatthew G. Knepley @*/
98239a2a23afSMatthew G. Knepley PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
98249a2a23afSMatthew G. Knepley {
98259a2a23afSMatthew G. Knepley   PetscFunctionBegin;
98269a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9827*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHMapAuxDestroy(&dmNew->auxData));
9828*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
98299a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
98309a2a23afSMatthew G. Knepley }
9831b5a892a1SMatthew G. Knepley 
9832b5a892a1SMatthew G. Knepley /*@C
9833b5a892a1SMatthew G. Knepley   DMPolytopeMatchOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
9834b5a892a1SMatthew G. Knepley 
9835b5a892a1SMatthew G. Knepley   Not collective
9836b5a892a1SMatthew G. Knepley 
9837b5a892a1SMatthew G. Knepley   Input Parameters:
9838b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9839b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9840b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9841b5a892a1SMatthew G. Knepley 
9842b5a892a1SMatthew G. Knepley   Output Parameters:
9843b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
9844b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9845b5a892a1SMatthew G. Knepley 
9846b5a892a1SMatthew G. Knepley   Level: advanced
9847b5a892a1SMatthew G. Knepley 
9848b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchVertexOrientation()
9849b5a892a1SMatthew G. Knepley @*/
9850b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9851b5a892a1SMatthew G. Knepley {
9852b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9853b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
9854b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9855b5a892a1SMatthew G. Knepley 
9856b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9857b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
9858b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9859b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9860b5a892a1SMatthew G. Knepley 
9861b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceCone[arr[c*2]] != targetCone[c]) break;
9862b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
9863b5a892a1SMatthew G. Knepley   }
9864b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9865b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9866b5a892a1SMatthew G. Knepley }
9867b5a892a1SMatthew G. Knepley 
9868b5a892a1SMatthew G. Knepley /*@C
9869b5a892a1SMatthew G. Knepley   DMPolytopeGetOrientation - Determine an orientation that takes the source face arrangement to the target face arrangement
9870b5a892a1SMatthew G. Knepley 
9871b5a892a1SMatthew G. Knepley   Not collective
9872b5a892a1SMatthew G. Knepley 
9873b5a892a1SMatthew G. Knepley   Input Parameters:
9874b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9875b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9876b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9877b5a892a1SMatthew G. Knepley 
9878b5a892a1SMatthew G. Knepley   Output Parameters:
9879b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
9880b5a892a1SMatthew G. Knepley 
9881b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
9882b5a892a1SMatthew G. Knepley 
9883b5a892a1SMatthew G. Knepley   Level: advanced
9884b5a892a1SMatthew G. Knepley 
9885b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchOrientation(), DMPolytopeGetVertexOrientation()
9886b5a892a1SMatthew G. Knepley @*/
9887b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9888b5a892a1SMatthew G. Knepley {
9889b5a892a1SMatthew G. Knepley   PetscBool      found;
9890b5a892a1SMatthew G. Knepley 
9891b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9892*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
98937a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9894b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9895b5a892a1SMatthew G. Knepley }
9896b5a892a1SMatthew G. Knepley 
9897b5a892a1SMatthew G. Knepley /*@C
9898b5a892a1SMatthew G. Knepley   DMPolytopeMatchVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
9899b5a892a1SMatthew G. Knepley 
9900b5a892a1SMatthew G. Knepley   Not collective
9901b5a892a1SMatthew G. Knepley 
9902b5a892a1SMatthew G. Knepley   Input Parameters:
9903b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9904b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9905b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9906b5a892a1SMatthew G. Knepley 
9907b5a892a1SMatthew G. Knepley   Output Parameters:
9908b5a892a1SMatthew G. Knepley + ornt  - The orientation which will take the source arrangement to the target arrangement
9909b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9910b5a892a1SMatthew G. Knepley 
9911b5a892a1SMatthew G. Knepley   Level: advanced
9912b5a892a1SMatthew G. Knepley 
9913b5a892a1SMatthew G. Knepley .seealso: DMPolytopeGetOrientation(), DMPolytopeMatchOrientation()
9914b5a892a1SMatthew G. Knepley @*/
9915b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9916b5a892a1SMatthew G. Knepley {
9917b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9918b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct)/2;
9919b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9920b5a892a1SMatthew G. Knepley 
9921b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9922b5a892a1SMatthew G. Knepley   if (!nO) {*ornt = 0; *found = PETSC_TRUE; PetscFunctionReturn(0);}
9923b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9924b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9925b5a892a1SMatthew G. Knepley 
9926b5a892a1SMatthew G. Knepley     for (c = 0; c < cS; ++c) if (sourceVert[arr[c]] != targetVert[c]) break;
9927b5a892a1SMatthew G. Knepley     if (c == cS) {*ornt = o; break;}
9928b5a892a1SMatthew G. Knepley   }
9929b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9930b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9931b5a892a1SMatthew G. Knepley }
9932b5a892a1SMatthew G. Knepley 
9933b5a892a1SMatthew G. Knepley /*@C
9934b5a892a1SMatthew G. Knepley   DMPolytopeGetVertexOrientation - Determine an orientation that takes the source vertex arrangement to the target vertex arrangement
9935b5a892a1SMatthew G. Knepley 
9936b5a892a1SMatthew G. Knepley   Not collective
9937b5a892a1SMatthew G. Knepley 
9938b5a892a1SMatthew G. Knepley   Input Parameters:
9939b5a892a1SMatthew G. Knepley + ct         - The DMPolytopeType
9940b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9941b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9942b5a892a1SMatthew G. Knepley 
9943b5a892a1SMatthew G. Knepley   Output Parameters:
9944b5a892a1SMatthew G. Knepley . ornt  - The orientation which will take the source arrangement to the target arrangement
9945b5a892a1SMatthew G. Knepley 
9946b5a892a1SMatthew G. Knepley   Note: This function will fail if no suitable orientation can be found.
9947b5a892a1SMatthew G. Knepley 
9948b5a892a1SMatthew G. Knepley   Level: advanced
9949b5a892a1SMatthew G. Knepley 
9950b5a892a1SMatthew G. Knepley .seealso: DMPolytopeMatchVertexOrientation(), DMPolytopeGetOrientation()
9951b5a892a1SMatthew G. Knepley @*/
9952b5a892a1SMatthew G. Knepley PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9953b5a892a1SMatthew G. Knepley {
9954b5a892a1SMatthew G. Knepley   PetscBool      found;
9955b5a892a1SMatthew G. Knepley 
9956b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
9957*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
99587a8be351SBarry Smith   PetscCheck(found,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9959b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9960b5a892a1SMatthew G. Knepley }
9961012bc364SMatthew G. Knepley 
9962012bc364SMatthew G. Knepley /*@C
9963012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9964012bc364SMatthew G. Knepley 
9965012bc364SMatthew G. Knepley   Not collective
9966012bc364SMatthew G. Knepley 
9967012bc364SMatthew G. Knepley   Input Parameters:
9968012bc364SMatthew G. Knepley + ct    - The DMPolytopeType
9969012bc364SMatthew G. Knepley - point - Coordinates of the point
9970012bc364SMatthew G. Knepley 
9971012bc364SMatthew G. Knepley   Output Parameters:
9972012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
9973012bc364SMatthew G. Knepley 
9974012bc364SMatthew G. Knepley   Level: advanced
9975012bc364SMatthew G. Knepley 
9976012bc364SMatthew G. Knepley .seealso: DMLocatePoints()
9977012bc364SMatthew G. Knepley @*/
9978012bc364SMatthew G. Knepley PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9979012bc364SMatthew G. Knepley {
9980012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9981012bc364SMatthew G. Knepley   PetscInt  d;
9982012bc364SMatthew G. Knepley 
9983012bc364SMatthew G. Knepley   PetscFunctionBegin;
9984012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9985012bc364SMatthew G. Knepley   switch (ct) {
9986012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9987012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9988012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
9989012bc364SMatthew G. Knepley       if (point[d] < -1.0) {*inside = PETSC_FALSE; break;}
9990012bc364SMatthew G. Knepley       sum += point[d];
9991012bc364SMatthew G. Knepley     }
9992012bc364SMatthew G. Knepley     if (sum > PETSC_SMALL) {*inside = PETSC_FALSE; break;}
9993012bc364SMatthew G. Knepley     break;
9994012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9995012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9996012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
9997012bc364SMatthew G. Knepley       if (PetscAbsReal(point[d]) > 1.+PETSC_SMALL) {*inside = PETSC_FALSE; break;}
9998012bc364SMatthew G. Knepley     break;
9999012bc364SMatthew G. Knepley   default:
1000098921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
10001012bc364SMatthew G. Knepley   }
10002012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
10003012bc364SMatthew G. Knepley }
10004