xref: /petsc/src/dm/interface/dm.c (revision c1929be8775bcfcc0cf085dd096e85f66be27d3f)
1b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
20c312b8eSJed Brown #include <petscsf.h>
30f21e855SMatthew G. Knepley #include <petscproblem.h>
447c6ae99SBarry Smith 
5732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
6f089877aSRichard Tran Mills PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal;
767a56275SMatthew G Knepley 
8bff4a2f0SMatthew G. Knepley const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DM_BOUNDARY_",0};
9bff4a2f0SMatthew G. Knepley 
1047c6ae99SBarry Smith #undef __FUNCT__
11a4121054SBarry Smith #define __FUNCT__ "DMCreate"
12a4121054SBarry Smith /*@
13de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
14a4121054SBarry Smith 
15a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
16a4121054SBarry Smith    error when you try to use the vector.
17a4121054SBarry Smith 
18a4121054SBarry Smith   Collective on MPI_Comm
19a4121054SBarry Smith 
20a4121054SBarry Smith   Input Parameter:
21a4121054SBarry Smith . comm - The communicator for the DM object
22a4121054SBarry Smith 
23a4121054SBarry Smith   Output Parameter:
24a4121054SBarry Smith . dm - The DM object
25a4121054SBarry Smith 
26a4121054SBarry Smith   Level: beginner
27a4121054SBarry Smith 
28a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
29a4121054SBarry Smith @*/
307087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
31a4121054SBarry Smith {
32a4121054SBarry Smith   DM             v;
33a4121054SBarry Smith   PetscErrorCode ierr;
34a4121054SBarry Smith 
35a4121054SBarry Smith   PetscFunctionBegin;
361411c6eeSJed Brown   PetscValidPointer(dm,2);
370298fd71SBarry Smith   *dm = NULL;
388b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
39607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
40607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
41607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
42a4121054SBarry Smith 
4367c2884eSBarry Smith   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
44a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
451411c6eeSJed Brown 
46e7c4fc90SDmitry Karpeev 
470298fd71SBarry Smith   v->ltogmap              = NULL;
481411c6eeSJed Brown   v->bs                   = 1;
49171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
5088ed4aceSMatthew G Knepley   ierr                    = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5188ed4aceSMatthew G Knepley   ierr                    = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
520298fd71SBarry Smith   v->defaultSection       = NULL;
530298fd71SBarry Smith   v->defaultGlobalSection = NULL;
54c6b900c6SMatthew G. Knepley   v->L                    = NULL;
55c6b900c6SMatthew G. Knepley   v->maxCell              = NULL;
56435a35e8SMatthew G Knepley   {
57435a35e8SMatthew G Knepley     PetscInt i;
58435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
590298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
60435a35e8SMatthew G Knepley     }
61435a35e8SMatthew G Knepley   }
620f21e855SMatthew G. Knepley   ierr = PetscProblemCreate(comm, &v->prob);CHKERRQ(ierr);
6314f150ffSMatthew G. Knepley   v->dmBC = NULL;
64f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
65c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
66b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
671411c6eeSJed Brown   *dm = v;
68a4121054SBarry Smith   PetscFunctionReturn(0);
69a4121054SBarry Smith }
70a4121054SBarry Smith 
71a4121054SBarry Smith #undef __FUNCT__
7238221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
7338221697SMatthew G. Knepley /*@
7438221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
7538221697SMatthew G. Knepley 
7638221697SMatthew G. Knepley   Collective on MPI_Comm
7738221697SMatthew G. Knepley 
7838221697SMatthew G. Knepley   Input Parameter:
7938221697SMatthew G. Knepley . dm - The original DM object
8038221697SMatthew G. Knepley 
8138221697SMatthew G. Knepley   Output Parameter:
8238221697SMatthew G. Knepley . newdm  - The new DM object
8338221697SMatthew G. Knepley 
8438221697SMatthew G. Knepley   Level: beginner
8538221697SMatthew G. Knepley 
8638221697SMatthew G. Knepley .keywords: DM, topology, create
8738221697SMatthew G. Knepley @*/
8838221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
8938221697SMatthew G. Knepley {
9038221697SMatthew G. Knepley   PetscSF        sf;
9138221697SMatthew G. Knepley   Vec            coords;
9238221697SMatthew G. Knepley   void          *ctx;
9338221697SMatthew G. Knepley   PetscErrorCode ierr;
9438221697SMatthew G. Knepley 
9538221697SMatthew G. Knepley   PetscFunctionBegin;
9638221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9738221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
9838221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
9938221697SMatthew G. Knepley   if (dm->ops->clone) {
10038221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
10138221697SMatthew G. Knepley   }
102cd27c7c9SMatthew G. Knepley   (*newdm)->setupcalled = PETSC_TRUE;
10338221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
10438221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
10538221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
10638221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
10738221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
10838221697SMatthew G. Knepley   if (coords) {
10938221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
11038221697SMatthew G. Knepley   } else {
11138221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
11238221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
11338221697SMatthew G. Knepley   }
114c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
115c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
116c6b900c6SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L);CHKERRQ(ierr);
117c6b900c6SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L);CHKERRQ(ierr);
118c6b900c6SMatthew G. Knepley   }
11938221697SMatthew G. Knepley   PetscFunctionReturn(0);
12038221697SMatthew G. Knepley }
12138221697SMatthew G. Knepley 
12238221697SMatthew G. Knepley #undef __FUNCT__
1239a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1249a42bb27SBarry Smith /*@C
125564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1269a42bb27SBarry Smith 
127aa219208SBarry Smith    Logically Collective on DMDA
1289a42bb27SBarry Smith 
1299a42bb27SBarry Smith    Input Parameter:
1309a42bb27SBarry Smith +  da - initial distributed array
1318154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1329a42bb27SBarry Smith 
1339a42bb27SBarry Smith    Options Database:
134dd85299cSBarry Smith .   -dm_vec_type ctype
1359a42bb27SBarry Smith 
1369a42bb27SBarry Smith    Level: intermediate
1379a42bb27SBarry Smith 
138c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType, DMGetVecType()
1399a42bb27SBarry Smith @*/
14019fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1419a42bb27SBarry Smith {
1429a42bb27SBarry Smith   PetscErrorCode ierr;
1439a42bb27SBarry Smith 
1449a42bb27SBarry Smith   PetscFunctionBegin;
1459a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1469a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
14719fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1489a42bb27SBarry Smith   PetscFunctionReturn(0);
1499a42bb27SBarry Smith }
1509a42bb27SBarry Smith 
1519a42bb27SBarry Smith #undef __FUNCT__
152c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
153c0dedaeaSBarry Smith /*@C
154c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
155c0dedaeaSBarry Smith 
156c0dedaeaSBarry Smith    Logically Collective on DMDA
157c0dedaeaSBarry Smith 
158c0dedaeaSBarry Smith    Input Parameter:
159c0dedaeaSBarry Smith .  da - initial distributed array
160c0dedaeaSBarry Smith 
161c0dedaeaSBarry Smith    Output Parameter:
162c0dedaeaSBarry Smith .  ctype - the vector type
163c0dedaeaSBarry Smith 
164c0dedaeaSBarry Smith    Level: intermediate
165c0dedaeaSBarry Smith 
166c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
167c0dedaeaSBarry Smith @*/
168c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
169c0dedaeaSBarry Smith {
170c0dedaeaSBarry Smith   PetscFunctionBegin;
171c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
172c0dedaeaSBarry Smith   *ctype = da->vectype;
173c0dedaeaSBarry Smith   PetscFunctionReturn(0);
174c0dedaeaSBarry Smith }
175c0dedaeaSBarry Smith 
176c0dedaeaSBarry Smith #undef __FUNCT__
1775f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1785f1ad066SMatthew G Knepley /*@
17934f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1805f1ad066SMatthew G Knepley 
1815f1ad066SMatthew G Knepley   Not collective
1825f1ad066SMatthew G Knepley 
1835f1ad066SMatthew G Knepley   Input Parameter:
1845f1ad066SMatthew G Knepley . v - The Vec
1855f1ad066SMatthew G Knepley 
1865f1ad066SMatthew G Knepley   Output Parameter:
1875f1ad066SMatthew G Knepley . dm - The DM
1885f1ad066SMatthew G Knepley 
1895f1ad066SMatthew G Knepley   Level: intermediate
1905f1ad066SMatthew G Knepley 
1915f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1925f1ad066SMatthew G Knepley @*/
1935f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
1945f1ad066SMatthew G Knepley {
1955f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1965f1ad066SMatthew G Knepley 
1975f1ad066SMatthew G Knepley   PetscFunctionBegin;
1985f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1995f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2005f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2015f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2025f1ad066SMatthew G Knepley }
2035f1ad066SMatthew G Knepley 
2045f1ad066SMatthew G Knepley #undef __FUNCT__
2055f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2065f1ad066SMatthew G Knepley /*@
2075f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
2085f1ad066SMatthew G Knepley 
2095f1ad066SMatthew G Knepley   Not collective
2105f1ad066SMatthew G Knepley 
2115f1ad066SMatthew G Knepley   Input Parameters:
2125f1ad066SMatthew G Knepley + v - The Vec
2135f1ad066SMatthew G Knepley - dm - The DM
2145f1ad066SMatthew G Knepley 
2155f1ad066SMatthew G Knepley   Level: intermediate
2165f1ad066SMatthew G Knepley 
2175f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2185f1ad066SMatthew G Knepley @*/
2195f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2205f1ad066SMatthew G Knepley {
2215f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2225f1ad066SMatthew G Knepley 
2235f1ad066SMatthew G Knepley   PetscFunctionBegin;
2245f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
225d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2265f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2275f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2285f1ad066SMatthew G Knepley }
2295f1ad066SMatthew G Knepley 
2305f1ad066SMatthew G Knepley #undef __FUNCT__
231521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
232521d9a4cSLisandro Dalcin /*@C
233521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
234521d9a4cSLisandro Dalcin 
235521d9a4cSLisandro Dalcin    Logically Collective on DM
236521d9a4cSLisandro Dalcin 
237521d9a4cSLisandro Dalcin    Input Parameter:
238521d9a4cSLisandro Dalcin +  dm - the DM context
239521d9a4cSLisandro Dalcin .  ctype - the matrix type
240521d9a4cSLisandro Dalcin 
241521d9a4cSLisandro Dalcin    Options Database:
242521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
243521d9a4cSLisandro Dalcin 
244521d9a4cSLisandro Dalcin    Level: intermediate
245521d9a4cSLisandro Dalcin 
246c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
247521d9a4cSLisandro Dalcin @*/
24819fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
249521d9a4cSLisandro Dalcin {
250521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
25188f0584fSBarry Smith 
252521d9a4cSLisandro Dalcin   PetscFunctionBegin;
253521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
254521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
25519fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
256521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
257521d9a4cSLisandro Dalcin }
258521d9a4cSLisandro Dalcin 
259521d9a4cSLisandro Dalcin #undef __FUNCT__
260c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
261c0dedaeaSBarry Smith /*@C
262c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
263c0dedaeaSBarry Smith 
264c0dedaeaSBarry Smith    Logically Collective on DM
265c0dedaeaSBarry Smith 
266c0dedaeaSBarry Smith    Input Parameter:
267c0dedaeaSBarry Smith .  dm - the DM context
268c0dedaeaSBarry Smith 
269c0dedaeaSBarry Smith    Output Parameter:
270c0dedaeaSBarry Smith .  ctype - the matrix type
271c0dedaeaSBarry Smith 
272c0dedaeaSBarry Smith    Options Database:
273c0dedaeaSBarry Smith .   -dm_mat_type ctype
274c0dedaeaSBarry Smith 
275c0dedaeaSBarry Smith    Level: intermediate
276c0dedaeaSBarry Smith 
277c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
278c0dedaeaSBarry Smith @*/
279c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
280c0dedaeaSBarry Smith {
281c0dedaeaSBarry Smith   PetscFunctionBegin;
282c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
283c0dedaeaSBarry Smith   *ctype = dm->mattype;
284c0dedaeaSBarry Smith   PetscFunctionReturn(0);
285c0dedaeaSBarry Smith }
286c0dedaeaSBarry Smith 
287c0dedaeaSBarry Smith #undef __FUNCT__
288c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
289c688c046SMatthew G Knepley /*@
29034f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
291c688c046SMatthew G Knepley 
292c688c046SMatthew G Knepley   Not collective
293c688c046SMatthew G Knepley 
294c688c046SMatthew G Knepley   Input Parameter:
295c688c046SMatthew G Knepley . A - The Mat
296c688c046SMatthew G Knepley 
297c688c046SMatthew G Knepley   Output Parameter:
298c688c046SMatthew G Knepley . dm - The DM
299c688c046SMatthew G Knepley 
300c688c046SMatthew G Knepley   Level: intermediate
301c688c046SMatthew G Knepley 
302c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
303c688c046SMatthew G Knepley @*/
304c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
305c688c046SMatthew G Knepley {
306c688c046SMatthew G Knepley   PetscErrorCode ierr;
307c688c046SMatthew G Knepley 
308c688c046SMatthew G Knepley   PetscFunctionBegin;
309c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
310c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
311c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
312c688c046SMatthew G Knepley   PetscFunctionReturn(0);
313c688c046SMatthew G Knepley }
314c688c046SMatthew G Knepley 
315c688c046SMatthew G Knepley #undef __FUNCT__
316c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
317c688c046SMatthew G Knepley /*@
318c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
319c688c046SMatthew G Knepley 
320c688c046SMatthew G Knepley   Not collective
321c688c046SMatthew G Knepley 
322c688c046SMatthew G Knepley   Input Parameters:
323c688c046SMatthew G Knepley + A - The Mat
324c688c046SMatthew G Knepley - dm - The DM
325c688c046SMatthew G Knepley 
326c688c046SMatthew G Knepley   Level: intermediate
327c688c046SMatthew G Knepley 
328c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
329c688c046SMatthew G Knepley @*/
330c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
331c688c046SMatthew G Knepley {
332c688c046SMatthew G Knepley   PetscErrorCode ierr;
333c688c046SMatthew G Knepley 
334c688c046SMatthew G Knepley   PetscFunctionBegin;
335c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3368865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
337c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
338c688c046SMatthew G Knepley   PetscFunctionReturn(0);
339c688c046SMatthew G Knepley }
340c688c046SMatthew G Knepley 
341c688c046SMatthew G Knepley #undef __FUNCT__
3429a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3439a42bb27SBarry Smith /*@C
3449a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
345aa219208SBarry Smith    DMDA options in the database.
3469a42bb27SBarry Smith 
347aa219208SBarry Smith    Logically Collective on DMDA
3489a42bb27SBarry Smith 
3499a42bb27SBarry Smith    Input Parameter:
350aa219208SBarry Smith +  da - the DMDA context
3519a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3529a42bb27SBarry Smith 
3539a42bb27SBarry Smith    Notes:
3549a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3559a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3569a42bb27SBarry Smith 
3579a42bb27SBarry Smith    Level: advanced
3589a42bb27SBarry Smith 
359aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
3609a42bb27SBarry Smith 
3619a42bb27SBarry Smith .seealso: DMSetFromOptions()
3629a42bb27SBarry Smith @*/
3637087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3649a42bb27SBarry Smith {
3659a42bb27SBarry Smith   PetscErrorCode ierr;
3669a42bb27SBarry Smith 
3679a42bb27SBarry Smith   PetscFunctionBegin;
3689a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3699a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
3709a42bb27SBarry Smith   PetscFunctionReturn(0);
3719a42bb27SBarry Smith }
3729a42bb27SBarry Smith 
3739a42bb27SBarry Smith #undef __FUNCT__
37447c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
37547c6ae99SBarry Smith /*@
376aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
37747c6ae99SBarry Smith 
37847c6ae99SBarry Smith     Collective on DM
37947c6ae99SBarry Smith 
38047c6ae99SBarry Smith     Input Parameter:
38147c6ae99SBarry Smith .   dm - the DM object to destroy
38247c6ae99SBarry Smith 
38347c6ae99SBarry Smith     Level: developer
38447c6ae99SBarry Smith 
385e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
38647c6ae99SBarry Smith 
38747c6ae99SBarry Smith @*/
388fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
38947c6ae99SBarry Smith {
3900f21e855SMatthew G. Knepley   PetscInt       i, cnt = 0, Nf = 0, f;
391dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
39247c6ae99SBarry Smith   PetscErrorCode ierr;
39347c6ae99SBarry Smith 
39447c6ae99SBarry Smith   PetscFunctionBegin;
3956bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
3966bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
39787e657c6SBarry Smith 
3980f21e855SMatthew G. Knepley   if ((*dm)->prob) {
3990f21e855SMatthew G. Knepley     PetscObject disc;
4000f21e855SMatthew G. Knepley 
401a546ed68SMatthew G. Knepley     /* I think it makes sense to dump all attached things when you are destroyed, which also eliminates circular references */
4020f21e855SMatthew G. Knepley     ierr = PetscProblemGetNumFields((*dm)->prob, &Nf);CHKERRQ(ierr);
4030f21e855SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
4040f21e855SMatthew G. Knepley       ierr = PetscProblemGetDiscretization((*dm)->prob, f, &disc);CHKERRQ(ierr);
4050f21e855SMatthew G. Knepley       ierr = PetscObjectCompose(disc, "pmat", NULL);CHKERRQ(ierr);
4060f21e855SMatthew G. Knepley       ierr = PetscObjectCompose(disc, "nullspace", NULL);CHKERRQ(ierr);
4070f21e855SMatthew G. Knepley       ierr = PetscObjectCompose(disc, "nearnullspace", NULL);CHKERRQ(ierr);
4080f21e855SMatthew G. Knepley     }
409a546ed68SMatthew G. Knepley   }
41087e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
411732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4128865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
4138865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
414732e2eb9SMatthew G Knepley   }
415dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
4162348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
4172348bcf4SPeter Brune   if ((*dm)->x) {
4182348bcf4SPeter Brune     DM obj;
4192348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
4202348bcf4SPeter Brune     if (obj == *dm) cnt++;
4212348bcf4SPeter Brune   }
422732e2eb9SMatthew G Knepley 
4236bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
424732e2eb9SMatthew G Knepley   /*
425732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
426732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
427732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
428732e2eb9SMatthew G Knepley   */
4296bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
4306bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
431732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4326bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
4336bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
434732e2eb9SMatthew G Knepley   }
435f490541aSPeter Brune   nnext=(*dm)->namedglobal;
4360298fd71SBarry Smith   (*dm)->namedglobal = NULL;
437f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
4382348bcf4SPeter Brune     nnext = nlink->next;
4392348bcf4SPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
4402348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
4412348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
4422348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
4432348bcf4SPeter Brune   }
444f490541aSPeter Brune   nnext=(*dm)->namedlocal;
4450298fd71SBarry Smith   (*dm)->namedlocal = NULL;
446f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
447f490541aSPeter Brune     nnext = nlink->next;
448f490541aSPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
449f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
450f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
451f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
452f490541aSPeter Brune   }
4532348bcf4SPeter Brune 
454b17ce1afSJed Brown   /* Destroy the list of hooks */
455c833c3b5SJed Brown   {
456c833c3b5SJed Brown     DMCoarsenHookLink link,next;
457b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
458b17ce1afSJed Brown       next = link->next;
459b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
460b17ce1afSJed Brown     }
4610298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
462c833c3b5SJed Brown   }
463c833c3b5SJed Brown   {
464c833c3b5SJed Brown     DMRefineHookLink link,next;
465c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
466c833c3b5SJed Brown       next = link->next;
467c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
468c833c3b5SJed Brown     }
4690298fd71SBarry Smith     (*dm)->refinehook = NULL;
470c833c3b5SJed Brown   }
471be081cd6SPeter Brune   {
472be081cd6SPeter Brune     DMSubDomainHookLink link,next;
473be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
474be081cd6SPeter Brune       next = link->next;
475be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
476be081cd6SPeter Brune     }
4770298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
478be081cd6SPeter Brune   }
479baf369e7SPeter Brune   {
480baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
481baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
482baf369e7SPeter Brune       next = link->next;
483baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
484baf369e7SPeter Brune     }
4850298fd71SBarry Smith     (*dm)->gtolhook = NULL;
486baf369e7SPeter Brune   }
487aa1993deSMatthew G Knepley   /* Destroy the work arrays */
488aa1993deSMatthew G Knepley   {
489aa1993deSMatthew G Knepley     DMWorkLink link,next;
490aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
491aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
492aa1993deSMatthew G Knepley       next = link->next;
493aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
494aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
495aa1993deSMatthew G Knepley     }
4960298fd71SBarry Smith     (*dm)->workin = NULL;
497aa1993deSMatthew G Knepley   }
498b17ce1afSJed Brown 
49952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
50052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
50152536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
50252536dc3SBarry Smith 
5031a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
5041a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
5051a266240SBarry Smith   }
50687e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
50771cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
5084dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
5096bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
5106bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
511073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
51288ed4aceSMatthew G Knepley 
51388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
51488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
5158b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
51688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
51788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
518af122d2aSMatthew G Knepley 
5196636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
5206636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
5216636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
522c6b900c6SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
523c6b900c6SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
5246636e97aSMatthew G Knepley 
5250f21e855SMatthew G. Knepley   ierr = PetscProblemDestroy(&(*dm)->prob);CHKERRQ(ierr);
52614f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
527e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
528e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
529732e2eb9SMatthew G Knepley 
5306bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
531435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
532732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
53347c6ae99SBarry Smith   PetscFunctionReturn(0);
53447c6ae99SBarry Smith }
53547c6ae99SBarry Smith 
53647c6ae99SBarry Smith #undef __FUNCT__
537d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
538d7bf68aeSBarry Smith /*@
539d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
540d7bf68aeSBarry Smith 
541d7bf68aeSBarry Smith     Collective on DM
542d7bf68aeSBarry Smith 
543d7bf68aeSBarry Smith     Input Parameter:
544d7bf68aeSBarry Smith .   dm - the DM object to setup
545d7bf68aeSBarry Smith 
546d7bf68aeSBarry Smith     Level: developer
547d7bf68aeSBarry Smith 
548e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
549d7bf68aeSBarry Smith 
550d7bf68aeSBarry Smith @*/
5517087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
552d7bf68aeSBarry Smith {
553d7bf68aeSBarry Smith   PetscErrorCode ierr;
554d7bf68aeSBarry Smith 
555d7bf68aeSBarry Smith   PetscFunctionBegin;
556171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5578387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
558d7bf68aeSBarry Smith   if (dm->ops->setup) {
559d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
560d7bf68aeSBarry Smith   }
5618387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
562d7bf68aeSBarry Smith   PetscFunctionReturn(0);
563d7bf68aeSBarry Smith }
564d7bf68aeSBarry Smith 
565d7bf68aeSBarry Smith #undef __FUNCT__
566d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
567d7bf68aeSBarry Smith /*@
568d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
569d7bf68aeSBarry Smith 
570d7bf68aeSBarry Smith     Collective on DM
571d7bf68aeSBarry Smith 
572d7bf68aeSBarry Smith     Input Parameter:
573d7bf68aeSBarry Smith .   dm - the DM object to set options for
574d7bf68aeSBarry Smith 
575732e2eb9SMatthew G Knepley     Options Database:
576dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
577dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
578171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
579171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
580732e2eb9SMatthew G Knepley 
581d7bf68aeSBarry Smith     Level: developer
582d7bf68aeSBarry Smith 
583e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
584d7bf68aeSBarry Smith 
585d7bf68aeSBarry Smith @*/
5867087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
587d7bf68aeSBarry Smith {
5887781c08eSBarry Smith   char           typeName[256];
589ca266f36SBarry Smith   PetscBool      flg;
590d7bf68aeSBarry Smith   PetscErrorCode ierr;
591d7bf68aeSBarry Smith 
592d7bf68aeSBarry Smith   PetscFunctionBegin;
593171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5943194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
5950298fd71SBarry Smith   ierr = PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,NULL);CHKERRQ(ierr);
596a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
597f9ba7244SBarry Smith   if (flg) {
598f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
599f9ba7244SBarry Smith   }
600a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg);CHKERRQ(ierr);
601073dac72SJed Brown   if (flg) {
602521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
603073dac72SJed Brown   }
6040298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
605f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
606f9ba7244SBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
607f9ba7244SBarry Smith   }
608f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
609f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
61082fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
611d7bf68aeSBarry Smith   PetscFunctionReturn(0);
612d7bf68aeSBarry Smith }
613d7bf68aeSBarry Smith 
614d7bf68aeSBarry Smith #undef __FUNCT__
61547c6ae99SBarry Smith #define __FUNCT__ "DMView"
616fc9bc008SSatish Balay /*@C
617aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
61847c6ae99SBarry Smith 
61947c6ae99SBarry Smith     Collective on DM
62047c6ae99SBarry Smith 
62147c6ae99SBarry Smith     Input Parameter:
62247c6ae99SBarry Smith +   dm - the DM object to view
62347c6ae99SBarry Smith -   v - the viewer
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith     Level: developer
62647c6ae99SBarry Smith 
627e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
62847c6ae99SBarry Smith 
62947c6ae99SBarry Smith @*/
6307087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
63147c6ae99SBarry Smith {
63247c6ae99SBarry Smith   PetscErrorCode ierr;
63332c0f0efSBarry Smith   PetscBool      isbinary;
63447c6ae99SBarry Smith 
63547c6ae99SBarry Smith   PetscFunctionBegin;
636171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6373014e516SBarry Smith   if (!v) {
638ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
6393014e516SBarry Smith   }
64098c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
64132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
64232c0f0efSBarry Smith   if (isbinary) {
64355849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
64432c0f0efSBarry Smith     char     type[256];
64532c0f0efSBarry Smith 
64632c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
64732c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
64832c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
64932c0f0efSBarry Smith   }
6500c010503SBarry Smith   if (dm->ops->view) {
6510c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
65247c6ae99SBarry Smith   }
65347c6ae99SBarry Smith   PetscFunctionReturn(0);
65447c6ae99SBarry Smith }
65547c6ae99SBarry Smith 
65647c6ae99SBarry Smith #undef __FUNCT__
65747c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
65847c6ae99SBarry Smith /*@
659aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
66047c6ae99SBarry Smith 
66147c6ae99SBarry Smith     Collective on DM
66247c6ae99SBarry Smith 
66347c6ae99SBarry Smith     Input Parameter:
66447c6ae99SBarry Smith .   dm - the DM object
66547c6ae99SBarry Smith 
66647c6ae99SBarry Smith     Output Parameter:
66747c6ae99SBarry Smith .   vec - the global vector
66847c6ae99SBarry Smith 
669073dac72SJed Brown     Level: beginner
67047c6ae99SBarry Smith 
671e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
67247c6ae99SBarry Smith 
67347c6ae99SBarry Smith @*/
6747087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
67547c6ae99SBarry Smith {
67647c6ae99SBarry Smith   PetscErrorCode ierr;
67747c6ae99SBarry Smith 
67847c6ae99SBarry Smith   PetscFunctionBegin;
679171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
68047c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
68147c6ae99SBarry Smith   PetscFunctionReturn(0);
68247c6ae99SBarry Smith }
68347c6ae99SBarry Smith 
68447c6ae99SBarry Smith #undef __FUNCT__
68547c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
68647c6ae99SBarry Smith /*@
687aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
68847c6ae99SBarry Smith 
68947c6ae99SBarry Smith     Not Collective
69047c6ae99SBarry Smith 
69147c6ae99SBarry Smith     Input Parameter:
69247c6ae99SBarry Smith .   dm - the DM object
69347c6ae99SBarry Smith 
69447c6ae99SBarry Smith     Output Parameter:
69547c6ae99SBarry Smith .   vec - the local vector
69647c6ae99SBarry Smith 
697073dac72SJed Brown     Level: beginner
69847c6ae99SBarry Smith 
699e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
70047c6ae99SBarry Smith 
70147c6ae99SBarry Smith @*/
7027087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
70347c6ae99SBarry Smith {
70447c6ae99SBarry Smith   PetscErrorCode ierr;
70547c6ae99SBarry Smith 
70647c6ae99SBarry Smith   PetscFunctionBegin;
707171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
70847c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
70947c6ae99SBarry Smith   PetscFunctionReturn(0);
71047c6ae99SBarry Smith }
71147c6ae99SBarry Smith 
71247c6ae99SBarry Smith #undef __FUNCT__
7131411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
7141411c6eeSJed Brown /*@
7151411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
7161411c6eeSJed Brown 
7171411c6eeSJed Brown    Collective on DM
7181411c6eeSJed Brown 
7191411c6eeSJed Brown    Input Parameter:
7201411c6eeSJed Brown .  dm - the DM that provides the mapping
7211411c6eeSJed Brown 
7221411c6eeSJed Brown    Output Parameter:
7231411c6eeSJed Brown .  ltog - the mapping
7241411c6eeSJed Brown 
7251411c6eeSJed Brown    Level: intermediate
7261411c6eeSJed Brown 
7271411c6eeSJed Brown    Notes:
7281411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
7291411c6eeSJed Brown    MatSetLocalToGlobalMapping().
7301411c6eeSJed Brown 
731fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
7321411c6eeSJed Brown @*/
7337087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
7341411c6eeSJed Brown {
7351411c6eeSJed Brown   PetscErrorCode ierr;
7361411c6eeSJed Brown 
7371411c6eeSJed Brown   PetscFunctionBegin;
7381411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7391411c6eeSJed Brown   PetscValidPointer(ltog,2);
7401411c6eeSJed Brown   if (!dm->ltogmap) {
74137d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
74237d0c07bSMatthew G Knepley 
74337d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
74437d0c07bSMatthew G Knepley     if (section) {
74537d0c07bSMatthew G Knepley       PetscInt *ltog;
74637d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
74737d0c07bSMatthew G Knepley 
74837d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
74937d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
75037d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
751785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
75237d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
75337d0c07bSMatthew G Knepley         PetscInt dof, off, c;
75437d0c07bSMatthew G Knepley 
75537d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
75637d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
75737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
75837d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
75937d0c07bSMatthew G Knepley           ltog[l] = off+c;
76037d0c07bSMatthew G Knepley         }
76137d0c07bSMatthew G Knepley       }
762f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
7633bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
76437d0c07bSMatthew G Knepley     } else {
765184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
766184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
7671411c6eeSJed Brown     }
76837d0c07bSMatthew G Knepley   }
7691411c6eeSJed Brown   *ltog = dm->ltogmap;
7701411c6eeSJed Brown   PetscFunctionReturn(0);
7711411c6eeSJed Brown }
7721411c6eeSJed Brown 
7731411c6eeSJed Brown #undef __FUNCT__
7741411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7751411c6eeSJed Brown /*@
7761411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7771411c6eeSJed Brown 
7781411c6eeSJed Brown    Not Collective
7791411c6eeSJed Brown 
7801411c6eeSJed Brown    Input Parameter:
7811411c6eeSJed Brown .  dm - the DM with block structure
7821411c6eeSJed Brown 
7831411c6eeSJed Brown    Output Parameter:
7841411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7851411c6eeSJed Brown 
7861411c6eeSJed Brown    Level: intermediate
7871411c6eeSJed Brown 
788fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
7891411c6eeSJed Brown @*/
7907087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7911411c6eeSJed Brown {
7921411c6eeSJed Brown   PetscFunctionBegin;
7931411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7941411c6eeSJed Brown   PetscValidPointer(bs,2);
7951411c6eeSJed Brown   if (dm->bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
7961411c6eeSJed Brown   *bs = dm->bs;
7971411c6eeSJed Brown   PetscFunctionReturn(0);
7981411c6eeSJed Brown }
7991411c6eeSJed Brown 
8001411c6eeSJed Brown #undef __FUNCT__
801e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
80247c6ae99SBarry Smith /*@
803e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
80447c6ae99SBarry Smith 
80547c6ae99SBarry Smith     Collective on DM
80647c6ae99SBarry Smith 
80747c6ae99SBarry Smith     Input Parameter:
80847c6ae99SBarry Smith +   dm1 - the DM object
80947c6ae99SBarry Smith -   dm2 - the second, finer DM object
81047c6ae99SBarry Smith 
81147c6ae99SBarry Smith     Output Parameter:
81247c6ae99SBarry Smith +  mat - the interpolation
81347c6ae99SBarry Smith -  vec - the scaling (optional)
81447c6ae99SBarry Smith 
81547c6ae99SBarry Smith     Level: developer
81647c6ae99SBarry Smith 
81785afcc9aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
81885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
819d52bd9f3SBarry Smith 
8201f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
821d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
82285afcc9aSBarry Smith 
82385afcc9aSBarry Smith 
824e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
82547c6ae99SBarry Smith 
82647c6ae99SBarry Smith @*/
827e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
82847c6ae99SBarry Smith {
82947c6ae99SBarry Smith   PetscErrorCode ierr;
83047c6ae99SBarry Smith 
83147c6ae99SBarry Smith   PetscFunctionBegin;
832171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
833171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
83425296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
83547c6ae99SBarry Smith   PetscFunctionReturn(0);
83647c6ae99SBarry Smith }
83747c6ae99SBarry Smith 
83847c6ae99SBarry Smith #undef __FUNCT__
839e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
84047c6ae99SBarry Smith /*@
841e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
84247c6ae99SBarry Smith 
84347c6ae99SBarry Smith     Collective on DM
84447c6ae99SBarry Smith 
84547c6ae99SBarry Smith     Input Parameter:
84647c6ae99SBarry Smith +   dm1 - the DM object
84747c6ae99SBarry Smith -   dm2 - the second, finer DM object
84847c6ae99SBarry Smith 
84947c6ae99SBarry Smith     Output Parameter:
85047c6ae99SBarry Smith .   ctx - the injection
85147c6ae99SBarry Smith 
85247c6ae99SBarry Smith     Level: developer
85347c6ae99SBarry Smith 
85485afcc9aSBarry Smith    Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
85585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
85685afcc9aSBarry Smith 
857e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
85847c6ae99SBarry Smith 
85947c6ae99SBarry Smith @*/
860e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
86147c6ae99SBarry Smith {
86247c6ae99SBarry Smith   PetscErrorCode ierr;
86347c6ae99SBarry Smith 
86447c6ae99SBarry Smith   PetscFunctionBegin;
865171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
866171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
86747c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
86847c6ae99SBarry Smith   PetscFunctionReturn(0);
86947c6ae99SBarry Smith }
87047c6ae99SBarry Smith 
87147c6ae99SBarry Smith #undef __FUNCT__
872e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
873b412c318SBarry Smith /*@
874b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith     Collective on DM
87747c6ae99SBarry Smith 
87847c6ae99SBarry Smith     Input Parameter:
87947c6ae99SBarry Smith +   dm - the DM object
880b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
88147c6ae99SBarry Smith 
88247c6ae99SBarry Smith     Output Parameter:
88347c6ae99SBarry Smith .   coloring - the coloring
88447c6ae99SBarry Smith 
88547c6ae99SBarry Smith     Level: developer
88647c6ae99SBarry Smith 
887b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
88847c6ae99SBarry Smith 
889aab9d709SJed Brown @*/
890b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
89147c6ae99SBarry Smith {
89247c6ae99SBarry Smith   PetscErrorCode ierr;
89347c6ae99SBarry Smith 
89447c6ae99SBarry Smith   PetscFunctionBegin;
895171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
896ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
897b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
89847c6ae99SBarry Smith   PetscFunctionReturn(0);
89947c6ae99SBarry Smith }
90047c6ae99SBarry Smith 
90147c6ae99SBarry Smith #undef __FUNCT__
902950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
903b412c318SBarry Smith /*@
904950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
90547c6ae99SBarry Smith 
90647c6ae99SBarry Smith     Collective on DM
90747c6ae99SBarry Smith 
90847c6ae99SBarry Smith     Input Parameter:
909b412c318SBarry Smith .   dm - the DM object
91047c6ae99SBarry Smith 
91147c6ae99SBarry Smith     Output Parameter:
91247c6ae99SBarry Smith .   mat - the empty Jacobian
91347c6ae99SBarry Smith 
914073dac72SJed Brown     Level: beginner
91547c6ae99SBarry Smith 
91694013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
91794013140SBarry Smith        do not need to do it yourself.
91894013140SBarry Smith 
91994013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
920aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
92194013140SBarry Smith 
92294013140SBarry 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
92394013140SBarry Smith        internally by PETSc.
92494013140SBarry Smith 
92594013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
926aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
92794013140SBarry Smith 
928b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
92947c6ae99SBarry Smith 
930aab9d709SJed Brown @*/
931b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
93247c6ae99SBarry Smith {
93347c6ae99SBarry Smith   PetscErrorCode ierr;
93447c6ae99SBarry Smith 
93547c6ae99SBarry Smith   PetscFunctionBegin;
936171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
937607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
938c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
939c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
940b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
94147c6ae99SBarry Smith   PetscFunctionReturn(0);
94247c6ae99SBarry Smith }
94347c6ae99SBarry Smith 
94447c6ae99SBarry Smith #undef __FUNCT__
945732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
946732e2eb9SMatthew G Knepley /*@
947950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
948732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
949732e2eb9SMatthew G Knepley 
950732e2eb9SMatthew G Knepley   Logically Collective on DMDA
951732e2eb9SMatthew G Knepley 
952732e2eb9SMatthew G Knepley   Input Parameter:
953732e2eb9SMatthew G Knepley + dm - the DM
954732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
955732e2eb9SMatthew G Knepley 
956732e2eb9SMatthew G Knepley   Level: developer
957950540a4SJed Brown .seealso DMCreateMatrix()
958732e2eb9SMatthew G Knepley @*/
959732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
960732e2eb9SMatthew G Knepley {
961732e2eb9SMatthew G Knepley   PetscFunctionBegin;
962732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
963732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
964732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
965732e2eb9SMatthew G Knepley }
966732e2eb9SMatthew G Knepley 
967732e2eb9SMatthew G Knepley #undef __FUNCT__
968a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
969a89ea682SMatthew G Knepley /*@C
970aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
971a89ea682SMatthew G Knepley 
972a89ea682SMatthew G Knepley   Not Collective
973a89ea682SMatthew G Knepley 
974a89ea682SMatthew G Knepley   Input Parameters:
975a89ea682SMatthew G Knepley + dm - the DM object
976aa1993deSMatthew G Knepley . count - The minium size
977aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
978a89ea682SMatthew G Knepley 
979a89ea682SMatthew G Knepley   Output Parameter:
980a89ea682SMatthew G Knepley . array - the work array
981a89ea682SMatthew G Knepley 
982a89ea682SMatthew G Knepley   Level: developer
983a89ea682SMatthew G Knepley 
984a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
985a89ea682SMatthew G Knepley @*/
986aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
987a89ea682SMatthew G Knepley {
988a89ea682SMatthew G Knepley   PetscErrorCode ierr;
989aa1993deSMatthew G Knepley   DMWorkLink     link;
990aa1993deSMatthew G Knepley   size_t         size;
991a89ea682SMatthew G Knepley 
992a89ea682SMatthew G Knepley   PetscFunctionBegin;
993a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
994aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
995aa1993deSMatthew G Knepley   if (dm->workin) {
996aa1993deSMatthew G Knepley     link       = dm->workin;
997aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
998aa1993deSMatthew G Knepley   } else {
999b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1000a89ea682SMatthew G Knepley   }
1001aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
1002aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
1003aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1004aa1993deSMatthew G Knepley     ierr        = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
1005aa1993deSMatthew G Knepley     link->bytes = size*count;
1006aa1993deSMatthew G Knepley   }
1007aa1993deSMatthew G Knepley   link->next   = dm->workout;
1008aa1993deSMatthew G Knepley   dm->workout  = link;
1009aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1010a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1011a89ea682SMatthew G Knepley }
1012a89ea682SMatthew G Knepley 
1013aa1993deSMatthew G Knepley #undef __FUNCT__
1014aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1015aa1993deSMatthew G Knepley /*@C
1016aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1017aa1993deSMatthew G Knepley 
1018aa1993deSMatthew G Knepley   Not Collective
1019aa1993deSMatthew G Knepley 
1020aa1993deSMatthew G Knepley   Input Parameters:
1021aa1993deSMatthew G Knepley + dm - the DM object
1022aa1993deSMatthew G Knepley . count - The minium size
1023aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1024aa1993deSMatthew G Knepley 
1025aa1993deSMatthew G Knepley   Output Parameter:
1026aa1993deSMatthew G Knepley . array - the work array
1027aa1993deSMatthew G Knepley 
1028aa1993deSMatthew G Knepley   Level: developer
1029aa1993deSMatthew G Knepley 
1030aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1031aa1993deSMatthew G Knepley @*/
1032aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1033aa1993deSMatthew G Knepley {
1034aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1035aa1993deSMatthew G Knepley 
1036aa1993deSMatthew G Knepley   PetscFunctionBegin;
1037aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1038aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1039aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1040aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1041aa1993deSMatthew G Knepley       *p           = link->next;
1042aa1993deSMatthew G Knepley       link->next   = dm->workin;
1043aa1993deSMatthew G Knepley       dm->workin   = link;
10440298fd71SBarry Smith       *(void**)mem = NULL;
1045aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1046aa1993deSMatthew G Knepley     }
1047aa1993deSMatthew G Knepley   }
1048aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1049aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1050aa1993deSMatthew G Knepley }
1051e7c4fc90SDmitry Karpeev 
1052e7c4fc90SDmitry Karpeev #undef __FUNCT__
1053435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1054435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1055435a35e8SMatthew G Knepley {
1056435a35e8SMatthew G Knepley   PetscFunctionBegin;
1057435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
105882f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1059435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1060435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1061435a35e8SMatthew G Knepley }
1062435a35e8SMatthew G Knepley 
1063435a35e8SMatthew G Knepley #undef __FUNCT__
10644d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10654f3b5142SJed Brown /*@C
10664d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10674d343eeaSMatthew G Knepley 
10684d343eeaSMatthew G Knepley   Not collective
10694d343eeaSMatthew G Knepley 
10704d343eeaSMatthew G Knepley   Input Parameter:
10714d343eeaSMatthew G Knepley . dm - the DM object
10724d343eeaSMatthew G Knepley 
10734d343eeaSMatthew G Knepley   Output Parameters:
10740298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
10750298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
10760298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
10774d343eeaSMatthew G Knepley 
10784d343eeaSMatthew G Knepley   Level: intermediate
10794d343eeaSMatthew G Knepley 
108021c9b008SJed Brown   Notes:
108121c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
108221c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
108321c9b008SJed Brown   PetscFree().
108421c9b008SJed Brown 
10854d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10864d343eeaSMatthew G Knepley @*/
108737d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10884d343eeaSMatthew G Knepley {
108937d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10904d343eeaSMatthew G Knepley   PetscErrorCode ierr;
10914d343eeaSMatthew G Knepley 
10924d343eeaSMatthew G Knepley   PetscFunctionBegin;
10934d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
109469ca1f37SDmitry Karpeev   if (numFields) {
109569ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
109669ca1f37SDmitry Karpeev     *numFields = 0;
109769ca1f37SDmitry Karpeev   }
109837d0c07bSMatthew G Knepley   if (fieldNames) {
109937d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
11000298fd71SBarry Smith     *fieldNames = NULL;
110169ca1f37SDmitry Karpeev   }
110269ca1f37SDmitry Karpeev   if (fields) {
110369ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
11040298fd71SBarry Smith     *fields = NULL;
110569ca1f37SDmitry Karpeev   }
110637d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
110737d0c07bSMatthew G Knepley   if (section) {
110837d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
110937d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
111037d0c07bSMatthew G Knepley 
111137d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
111237d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1113dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
111437d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
111537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
111637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
111737d0c07bSMatthew G Knepley     }
111837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
111937d0c07bSMatthew G Knepley       PetscInt gdof;
112037d0c07bSMatthew G Knepley 
112137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
112237d0c07bSMatthew G Knepley       if (gdof > 0) {
112337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
112437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
112537d0c07bSMatthew G Knepley 
112637d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
112737d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
112837d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
112937d0c07bSMatthew G Knepley         }
113037d0c07bSMatthew G Knepley       }
113137d0c07bSMatthew G Knepley     }
113237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1133785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
113437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
113537d0c07bSMatthew G Knepley     }
113637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
113737d0c07bSMatthew G Knepley       PetscInt gdof, goff;
113837d0c07bSMatthew G Knepley 
113937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
114037d0c07bSMatthew G Knepley       if (gdof > 0) {
114137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
114237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
114337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
114437d0c07bSMatthew G Knepley 
114537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
114637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
114737d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
114837d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
114937d0c07bSMatthew G Knepley           }
115037d0c07bSMatthew G Knepley         }
115137d0c07bSMatthew G Knepley       }
115237d0c07bSMatthew G Knepley     }
11538865f1eaSKarl Rupp     if (numFields) *numFields = nF;
115437d0c07bSMatthew G Knepley     if (fieldNames) {
1155785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
115637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
115737d0c07bSMatthew G Knepley         const char *fieldName;
115837d0c07bSMatthew G Knepley 
115937d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
116037d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
116137d0c07bSMatthew G Knepley       }
116237d0c07bSMatthew G Knepley     }
116337d0c07bSMatthew G Knepley     if (fields) {
1164785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
116537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
116682f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
116737d0c07bSMatthew G Knepley       }
116837d0c07bSMatthew G Knepley     }
116937d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
11708865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
11718865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
117269ca1f37SDmitry Karpeev   }
11734d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11744d343eeaSMatthew G Knepley }
11754d343eeaSMatthew G Knepley 
117616621825SDmitry Karpeev 
117716621825SDmitry Karpeev #undef __FUNCT__
117816621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
117916621825SDmitry Karpeev /*@C
118016621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
118116621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
118216621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1183e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1184e7c4fc90SDmitry Karpeev 
1185e7c4fc90SDmitry Karpeev   Not collective
1186e7c4fc90SDmitry Karpeev 
1187e7c4fc90SDmitry Karpeev   Input Parameter:
1188e7c4fc90SDmitry Karpeev . dm - the DM object
1189e7c4fc90SDmitry Karpeev 
1190e7c4fc90SDmitry Karpeev   Output Parameters:
11910298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
11920298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
11930298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
11940298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1195e7c4fc90SDmitry Karpeev 
1196e7c4fc90SDmitry Karpeev   Level: intermediate
1197e7c4fc90SDmitry Karpeev 
1198e7c4fc90SDmitry Karpeev   Notes:
1199e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1200e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1201e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1202e7c4fc90SDmitry Karpeev 
1203e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1204e7c4fc90SDmitry Karpeev @*/
120516621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1206e7c4fc90SDmitry Karpeev {
1207e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1208e7c4fc90SDmitry Karpeev 
1209e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1210e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12118865f1eaSKarl Rupp   if (len) {
12128865f1eaSKarl Rupp     PetscValidPointer(len,2);
12138865f1eaSKarl Rupp     *len = 0;
12148865f1eaSKarl Rupp   }
12158865f1eaSKarl Rupp   if (namelist) {
12168865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
12178865f1eaSKarl Rupp     *namelist = 0;
12188865f1eaSKarl Rupp   }
12198865f1eaSKarl Rupp   if (islist) {
12208865f1eaSKarl Rupp     PetscValidPointer(islist,4);
12218865f1eaSKarl Rupp     *islist = 0;
12228865f1eaSKarl Rupp   }
12238865f1eaSKarl Rupp   if (dmlist) {
12248865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
12258865f1eaSKarl Rupp     *dmlist = 0;
12268865f1eaSKarl Rupp   }
1227f3f0edfdSDmitry Karpeev   /*
1228f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1229f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1230f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1231f3f0edfdSDmitry Karpeev    */
1232ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
123316621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1234435a35e8SMatthew G Knepley     PetscSection section;
1235435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1236435a35e8SMatthew G Knepley 
1237435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1238435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1239435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1240435a35e8SMatthew G Knepley       *len = numFields;
124103dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
124203dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
124303dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1244435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1245435a35e8SMatthew G Knepley         const char *fieldName;
1246435a35e8SMatthew G Knepley 
124703dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
124803dc3394SMatthew G. Knepley         if (namelist) {
1249435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1250435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1251435a35e8SMatthew G Knepley         }
125203dc3394SMatthew G. Knepley       }
1253435a35e8SMatthew G Knepley     } else {
125469ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1255e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
12560298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1257e7c4fc90SDmitry Karpeev     }
12588865f1eaSKarl Rupp   } else {
125916621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
126016621825SDmitry Karpeev   }
126116621825SDmitry Karpeev   PetscFunctionReturn(0);
126216621825SDmitry Karpeev }
126316621825SDmitry Karpeev 
126416621825SDmitry Karpeev #undef __FUNCT__
1265435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1266435a35e8SMatthew G Knepley /*@C
1267435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1268435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1269435a35e8SMatthew G Knepley 
1270435a35e8SMatthew G Knepley   Not collective
1271435a35e8SMatthew G Knepley 
1272435a35e8SMatthew G Knepley   Input Parameters:
1273435a35e8SMatthew G Knepley + dm - the DM object
1274435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
12750298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1276435a35e8SMatthew G Knepley 
1277435a35e8SMatthew G Knepley   Output Parameters:
1278435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1279435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1280435a35e8SMatthew G Knepley 
1281435a35e8SMatthew G Knepley   Level: intermediate
1282435a35e8SMatthew G Knepley 
1283435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1284435a35e8SMatthew G Knepley @*/
1285435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1286435a35e8SMatthew G Knepley {
1287435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1288435a35e8SMatthew G Knepley 
1289435a35e8SMatthew G Knepley   PetscFunctionBegin;
1290435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1291435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
12928865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
12938865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1294435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1295435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
129682f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1297435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1298435a35e8SMatthew G Knepley }
1299435a35e8SMatthew G Knepley 
130016621825SDmitry Karpeev 
130116621825SDmitry Karpeev #undef __FUNCT__
130216621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
130316621825SDmitry Karpeev /*@C
13048d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13058d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13068d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13078d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13088d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
130916621825SDmitry Karpeev 
131016621825SDmitry Karpeev   Not collective
131116621825SDmitry Karpeev 
131216621825SDmitry Karpeev   Input Parameter:
131316621825SDmitry Karpeev . dm - the DM object
131416621825SDmitry Karpeev 
131516621825SDmitry Karpeev   Output Parameters:
13160298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
13170298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
13180298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
13190298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
13200298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
132116621825SDmitry Karpeev 
132216621825SDmitry Karpeev   Level: intermediate
132316621825SDmitry Karpeev 
132416621825SDmitry Karpeev   Notes:
132516621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
132616621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
132716621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
132816621825SDmitry Karpeev 
13298d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
133016621825SDmitry Karpeev @*/
13318d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
133216621825SDmitry Karpeev {
133316621825SDmitry Karpeev   PetscErrorCode      ierr;
1334be081cd6SPeter Brune   DMSubDomainHookLink link;
1335be081cd6SPeter Brune   PetscInt            i,l;
133616621825SDmitry Karpeev 
133716621825SDmitry Karpeev   PetscFunctionBegin;
133816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
133914a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
13400298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
13410298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
13420298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
13430298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1344f3f0edfdSDmitry Karpeev   /*
1345f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1346f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1347f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1348f3f0edfdSDmitry Karpeev    */
1349ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
135016621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1351be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
135214a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
135314a18fd3SPeter Brune     if (dmlist) {
1354be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1355be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1356be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1357be081cd6SPeter Brune         }
1358d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1359e7c4fc90SDmitry Karpeev       }
136014a18fd3SPeter Brune     }
136114a18fd3SPeter Brune     if (len) *len = l;
136214a18fd3SPeter Brune   }
1363e30e807fSPeter Brune   PetscFunctionReturn(0);
1364e30e807fSPeter Brune }
1365e30e807fSPeter Brune 
1366e30e807fSPeter Brune 
1367e30e807fSPeter Brune #undef __FUNCT__
1368e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1369e30e807fSPeter Brune /*@C
1370e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1371e30e807fSPeter Brune 
1372e30e807fSPeter Brune   Not collective
1373e30e807fSPeter Brune 
1374e30e807fSPeter Brune   Input Parameters:
1375e30e807fSPeter Brune + dm - the DM object
1376e30e807fSPeter Brune . n  - the number of subdomain scatters
1377e30e807fSPeter Brune - subdms - the local subdomains
1378e30e807fSPeter Brune 
1379e30e807fSPeter Brune   Output Parameters:
1380e30e807fSPeter Brune + n     - the number of scatters returned
1381e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1382e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1383e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1384e30e807fSPeter Brune 
1385e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1386e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1387e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1388e30e807fSPeter Brune   solution and residual data.
1389e30e807fSPeter Brune 
1390e30e807fSPeter Brune   Level: developer
1391e30e807fSPeter Brune 
1392e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1393e30e807fSPeter Brune @*/
1394e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1395e30e807fSPeter Brune {
1396e30e807fSPeter Brune   PetscErrorCode ierr;
1397e30e807fSPeter Brune 
1398e30e807fSPeter Brune   PetscFunctionBegin;
1399e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1400e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1401e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1402e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
140382f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1404e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1405e7c4fc90SDmitry Karpeev }
1406e7c4fc90SDmitry Karpeev 
1407731c8d9eSDmitry Karpeev #undef __FUNCT__
140847c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
140947c6ae99SBarry Smith /*@
141047c6ae99SBarry Smith   DMRefine - Refines a DM object
141147c6ae99SBarry Smith 
141247c6ae99SBarry Smith   Collective on DM
141347c6ae99SBarry Smith 
141447c6ae99SBarry Smith   Input Parameter:
141547c6ae99SBarry Smith + dm   - the DM object
141691d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
141747c6ae99SBarry Smith 
141847c6ae99SBarry Smith   Output Parameter:
14190298fd71SBarry Smith . dmf - the refined DM, or NULL
1420ae0a1c52SMatthew G Knepley 
14210298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
142247c6ae99SBarry Smith 
142347c6ae99SBarry Smith   Level: developer
142447c6ae99SBarry Smith 
1425e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
142647c6ae99SBarry Smith @*/
14277087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
142847c6ae99SBarry Smith {
142947c6ae99SBarry Smith   PetscErrorCode   ierr;
1430c833c3b5SJed Brown   DMRefineHookLink link;
143147c6ae99SBarry Smith 
143247c6ae99SBarry Smith   PetscFunctionBegin;
1433732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
143447c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14354057135bSMatthew G Knepley   if (*dmf) {
143643842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
14378865f1eaSKarl Rupp 
14388cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
14398865f1eaSKarl Rupp 
1440644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14410598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1442656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
14438865f1eaSKarl Rupp 
1444e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1445c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
14468865f1eaSKarl Rupp       if (link->refinehook) {
14478865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
14488865f1eaSKarl Rupp       }
1449c833c3b5SJed Brown     }
1450c833c3b5SJed Brown   }
1451c833c3b5SJed Brown   PetscFunctionReturn(0);
1452c833c3b5SJed Brown }
1453c833c3b5SJed Brown 
1454c833c3b5SJed Brown #undef __FUNCT__
1455c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1456bb9467b5SJed Brown /*@C
1457c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1458c833c3b5SJed Brown 
1459c833c3b5SJed Brown    Logically Collective
1460c833c3b5SJed Brown 
1461c833c3b5SJed Brown    Input Arguments:
1462c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1463c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1464c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
14650298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1466c833c3b5SJed Brown 
1467c833c3b5SJed Brown    Calling sequence of refinehook:
1468c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1469c833c3b5SJed Brown 
1470c833c3b5SJed Brown +  coarse - coarse level DM
1471c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1472c833c3b5SJed Brown -  ctx - optional user-defined function context
1473c833c3b5SJed Brown 
1474c833c3b5SJed Brown    Calling sequence for interphook:
1475c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1476c833c3b5SJed Brown 
1477c833c3b5SJed Brown +  coarse - coarse level DM
1478c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1479c833c3b5SJed Brown .  fine - fine level DM to update
1480c833c3b5SJed Brown -  ctx - optional user-defined function context
1481c833c3b5SJed Brown 
1482c833c3b5SJed Brown    Level: advanced
1483c833c3b5SJed Brown 
1484c833c3b5SJed Brown    Notes:
1485c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1486c833c3b5SJed Brown 
1487c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1488c833c3b5SJed Brown 
1489bb9467b5SJed Brown    This function is currently not available from Fortran.
1490bb9467b5SJed Brown 
1491c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1492c833c3b5SJed Brown @*/
1493c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1494c833c3b5SJed Brown {
1495c833c3b5SJed Brown   PetscErrorCode   ierr;
1496c833c3b5SJed Brown   DMRefineHookLink link,*p;
1497c833c3b5SJed Brown 
1498c833c3b5SJed Brown   PetscFunctionBegin;
1499c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1500c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1501c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1502c833c3b5SJed Brown   link->refinehook = refinehook;
1503c833c3b5SJed Brown   link->interphook = interphook;
1504c833c3b5SJed Brown   link->ctx        = ctx;
15050298fd71SBarry Smith   link->next       = NULL;
1506c833c3b5SJed Brown   *p               = link;
1507c833c3b5SJed Brown   PetscFunctionReturn(0);
1508c833c3b5SJed Brown }
1509c833c3b5SJed Brown 
1510c833c3b5SJed Brown #undef __FUNCT__
1511c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1512c833c3b5SJed Brown /*@
1513c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1514c833c3b5SJed Brown 
1515c833c3b5SJed Brown    Collective if any hooks are
1516c833c3b5SJed Brown 
1517c833c3b5SJed Brown    Input Arguments:
1518c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1519c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1520c833c3b5SJed Brown -  fine - finer DM to update
1521c833c3b5SJed Brown 
1522c833c3b5SJed Brown    Level: developer
1523c833c3b5SJed Brown 
1524c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1525c833c3b5SJed Brown @*/
1526c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1527c833c3b5SJed Brown {
1528c833c3b5SJed Brown   PetscErrorCode   ierr;
1529c833c3b5SJed Brown   DMRefineHookLink link;
1530c833c3b5SJed Brown 
1531c833c3b5SJed Brown   PetscFunctionBegin;
1532c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
15338865f1eaSKarl Rupp     if (link->interphook) {
15348865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
15358865f1eaSKarl Rupp     }
15364057135bSMatthew G Knepley   }
153747c6ae99SBarry Smith   PetscFunctionReturn(0);
153847c6ae99SBarry Smith }
153947c6ae99SBarry Smith 
154047c6ae99SBarry Smith #undef __FUNCT__
1541eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1542eb3f98d2SBarry Smith /*@
1543eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1544eb3f98d2SBarry Smith 
1545eb3f98d2SBarry Smith     Not Collective
1546eb3f98d2SBarry Smith 
1547eb3f98d2SBarry Smith     Input Parameter:
1548eb3f98d2SBarry Smith .   dm - the DM object
1549eb3f98d2SBarry Smith 
1550eb3f98d2SBarry Smith     Output Parameter:
1551eb3f98d2SBarry Smith .   level - number of refinements
1552eb3f98d2SBarry Smith 
1553eb3f98d2SBarry Smith     Level: developer
1554eb3f98d2SBarry Smith 
15556a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1556eb3f98d2SBarry Smith 
1557eb3f98d2SBarry Smith @*/
1558eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1559eb3f98d2SBarry Smith {
1560eb3f98d2SBarry Smith   PetscFunctionBegin;
1561eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1562eb3f98d2SBarry Smith   *level = dm->levelup;
1563eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1564eb3f98d2SBarry Smith }
1565eb3f98d2SBarry Smith 
1566eb3f98d2SBarry Smith #undef __FUNCT__
1567baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1568bb9467b5SJed Brown /*@C
1569baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1570baf369e7SPeter Brune 
1571baf369e7SPeter Brune    Logically Collective
1572baf369e7SPeter Brune 
1573baf369e7SPeter Brune    Input Arguments:
1574baf369e7SPeter Brune +  dm - the DM
1575baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1576baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
15770298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1578baf369e7SPeter Brune 
1579baf369e7SPeter Brune    Calling sequence for beginhook:
1580baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1581baf369e7SPeter Brune 
1582baf369e7SPeter Brune +  dm - global DM
1583baf369e7SPeter Brune .  g - global vector
1584baf369e7SPeter Brune .  mode - mode
1585baf369e7SPeter Brune .  l - local vector
1586baf369e7SPeter Brune -  ctx - optional user-defined function context
1587baf369e7SPeter Brune 
1588baf369e7SPeter Brune 
1589baf369e7SPeter Brune    Calling sequence for endhook:
1590ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1591baf369e7SPeter Brune 
1592baf369e7SPeter Brune +  global - global DM
1593baf369e7SPeter Brune -  ctx - optional user-defined function context
1594baf369e7SPeter Brune 
1595baf369e7SPeter Brune    Level: advanced
1596baf369e7SPeter Brune 
1597baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1598baf369e7SPeter Brune @*/
1599baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1600baf369e7SPeter Brune {
1601baf369e7SPeter Brune   PetscErrorCode          ierr;
1602baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1603baf369e7SPeter Brune 
1604baf369e7SPeter Brune   PetscFunctionBegin;
1605baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1606baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1607baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1608baf369e7SPeter Brune   link->beginhook = beginhook;
1609baf369e7SPeter Brune   link->endhook   = endhook;
1610baf369e7SPeter Brune   link->ctx       = ctx;
16110298fd71SBarry Smith   link->next      = NULL;
1612baf369e7SPeter Brune   *p              = link;
1613baf369e7SPeter Brune   PetscFunctionReturn(0);
1614baf369e7SPeter Brune }
1615baf369e7SPeter Brune 
1616baf369e7SPeter Brune #undef __FUNCT__
161747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
161847c6ae99SBarry Smith /*@
161947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
162047c6ae99SBarry Smith 
162147c6ae99SBarry Smith     Neighbor-wise Collective on DM
162247c6ae99SBarry Smith 
162347c6ae99SBarry Smith     Input Parameters:
162447c6ae99SBarry Smith +   dm - the DM object
162547c6ae99SBarry Smith .   g - the global vector
162647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
162747c6ae99SBarry Smith -   l - the local vector
162847c6ae99SBarry Smith 
162947c6ae99SBarry Smith 
163047c6ae99SBarry Smith     Level: beginner
163147c6ae99SBarry Smith 
1632e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
163347c6ae99SBarry Smith 
163447c6ae99SBarry Smith @*/
16357087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
163647c6ae99SBarry Smith {
16377128ae9fSMatthew G Knepley   PetscSF                 sf;
163847c6ae99SBarry Smith   PetscErrorCode          ierr;
1639baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
164047c6ae99SBarry Smith 
164147c6ae99SBarry Smith   PetscFunctionBegin;
1642171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1643baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
16448865f1eaSKarl Rupp     if (link->beginhook) {
16458865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
16468865f1eaSKarl Rupp     }
1647baf369e7SPeter Brune   }
16487128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16497128ae9fSMatthew G Knepley   if (sf) {
16507128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16517128ae9fSMatthew G Knepley 
165282f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16537128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16547128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16557128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16567128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16577128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16587128ae9fSMatthew G Knepley   } else {
1659843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16607128ae9fSMatthew G Knepley   }
166147c6ae99SBarry Smith   PetscFunctionReturn(0);
166247c6ae99SBarry Smith }
166347c6ae99SBarry Smith 
166447c6ae99SBarry Smith #undef __FUNCT__
166547c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
166647c6ae99SBarry Smith /*@
166747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
166847c6ae99SBarry Smith 
166947c6ae99SBarry Smith     Neighbor-wise Collective on DM
167047c6ae99SBarry Smith 
167147c6ae99SBarry Smith     Input Parameters:
167247c6ae99SBarry Smith +   dm - the DM object
167347c6ae99SBarry Smith .   g - the global vector
167447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
167547c6ae99SBarry Smith -   l - the local vector
167647c6ae99SBarry Smith 
167747c6ae99SBarry Smith 
167847c6ae99SBarry Smith     Level: beginner
167947c6ae99SBarry Smith 
1680e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
168147c6ae99SBarry Smith 
168247c6ae99SBarry Smith @*/
16837087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
168447c6ae99SBarry Smith {
16857128ae9fSMatthew G Knepley   PetscSF                 sf;
168647c6ae99SBarry Smith   PetscErrorCode          ierr;
168761a3c1faSSatish Balay   PetscScalar             *lArray, *gArray;
1688baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
168947c6ae99SBarry Smith 
169047c6ae99SBarry Smith   PetscFunctionBegin;
1691171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16927128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16937128ae9fSMatthew G Knepley   if (sf) {
169482f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16957128ae9fSMatthew G Knepley 
16967128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16977128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16987128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16997128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17007128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17017128ae9fSMatthew G Knepley   } else {
1702843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17037128ae9fSMatthew G Knepley   }
1704baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1705baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1706baf369e7SPeter Brune   }
170747c6ae99SBarry Smith   PetscFunctionReturn(0);
170847c6ae99SBarry Smith }
170947c6ae99SBarry Smith 
171047c6ae99SBarry Smith #undef __FUNCT__
17119a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
171247c6ae99SBarry Smith /*@
17139a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
17149a42bb27SBarry Smith 
17159a42bb27SBarry Smith     Neighbor-wise Collective on DM
17169a42bb27SBarry Smith 
17179a42bb27SBarry Smith     Input Parameters:
17189a42bb27SBarry Smith +   dm - the DM object
1719f6813fd5SJed Brown .   l - the local vector
17209a42bb27SBarry 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
17219a42bb27SBarry Smith            base point.
1722f6813fd5SJed Brown - - the global vector
17239a42bb27SBarry Smith 
17249a42bb27SBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation. If you would like to simply add the non-ghosted values in the local
17259a42bb27SBarry Smith            array into the global array you need to either (1) zero the ghosted locations and use ADD_VALUES or (2) use INSERT_VALUES into a work global array and then add the work
17269a42bb27SBarry Smith            global array to the final global array with VecAXPY().
17279a42bb27SBarry Smith 
17289a42bb27SBarry Smith     Level: beginner
17299a42bb27SBarry Smith 
1730e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
17319a42bb27SBarry Smith 
17329a42bb27SBarry Smith @*/
17337087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
17349a42bb27SBarry Smith {
17357128ae9fSMatthew G Knepley   PetscSF        sf;
17369a42bb27SBarry Smith   PetscErrorCode ierr;
17379a42bb27SBarry Smith 
17389a42bb27SBarry Smith   PetscFunctionBegin;
1739171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17407128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17417128ae9fSMatthew G Knepley   if (sf) {
17427128ae9fSMatthew G Knepley     MPI_Op      op;
17437128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17447128ae9fSMatthew G Knepley 
17457128ae9fSMatthew G Knepley     switch (mode) {
17467128ae9fSMatthew G Knepley     case INSERT_VALUES:
17477128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17488bfbc91cSJed Brown       op = MPIU_REPLACE; break;
17497128ae9fSMatthew G Knepley     case ADD_VALUES:
17507128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17517128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17527128ae9fSMatthew G Knepley     default:
175382f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17547128ae9fSMatthew G Knepley     }
17557128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17567128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17577128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17587128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17597128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17607128ae9fSMatthew G Knepley   } else {
1761843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17627128ae9fSMatthew G Knepley   }
17639a42bb27SBarry Smith   PetscFunctionReturn(0);
17649a42bb27SBarry Smith }
17659a42bb27SBarry Smith 
17669a42bb27SBarry Smith #undef __FUNCT__
17679a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
17689a42bb27SBarry Smith /*@
17699a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
177047c6ae99SBarry Smith 
177147c6ae99SBarry Smith     Neighbor-wise Collective on DM
177247c6ae99SBarry Smith 
177347c6ae99SBarry Smith     Input Parameters:
177447c6ae99SBarry Smith +   dm - the DM object
1775f6813fd5SJed Brown .   l - the local vector
177647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1777f6813fd5SJed Brown -   g - the global vector
177847c6ae99SBarry Smith 
177947c6ae99SBarry Smith 
178047c6ae99SBarry Smith     Level: beginner
178147c6ae99SBarry Smith 
1782e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
178347c6ae99SBarry Smith 
178447c6ae99SBarry Smith @*/
17857087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
178647c6ae99SBarry Smith {
17877128ae9fSMatthew G Knepley   PetscSF        sf;
178847c6ae99SBarry Smith   PetscErrorCode ierr;
178947c6ae99SBarry Smith 
179047c6ae99SBarry Smith   PetscFunctionBegin;
1791171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17927128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17937128ae9fSMatthew G Knepley   if (sf) {
17947128ae9fSMatthew G Knepley     MPI_Op      op;
17957128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17967128ae9fSMatthew G Knepley 
17977128ae9fSMatthew G Knepley     switch (mode) {
17987128ae9fSMatthew G Knepley     case INSERT_VALUES:
17997128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
18008bfbc91cSJed Brown       op = MPIU_REPLACE; break;
18017128ae9fSMatthew G Knepley     case ADD_VALUES:
18027128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
18037128ae9fSMatthew G Knepley       op = MPI_SUM; break;
18047128ae9fSMatthew G Knepley     default:
180582f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18067128ae9fSMatthew G Knepley     }
18077128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
18087128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
18097128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
18107128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
18117128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
18127128ae9fSMatthew G Knepley   } else {
1813843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
18147128ae9fSMatthew G Knepley   }
181547c6ae99SBarry Smith   PetscFunctionReturn(0);
181647c6ae99SBarry Smith }
181747c6ae99SBarry Smith 
181847c6ae99SBarry Smith #undef __FUNCT__
1819f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
1820f089877aSRichard Tran Mills /*@
1821bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
1822bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
1823d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
1824f089877aSRichard Tran Mills 
1825bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
1826f089877aSRichard Tran Mills 
1827f089877aSRichard Tran Mills    Input Parameters:
1828f089877aSRichard Tran Mills +  dm - the DM object
1829bc0a1609SRichard Tran Mills .  g - the original local vector
1830bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
1831f089877aSRichard Tran Mills 
1832bc0a1609SRichard Tran Mills    Output Parameter:
1833bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
1834f089877aSRichard Tran Mills 
1835f089877aSRichard Tran Mills    Level: intermediate
1836f089877aSRichard Tran Mills 
1837bc0a1609SRichard Tran Mills    Notes:
1838bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
1839bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
1840bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
1841bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
1842bc0a1609SRichard Tran Mills 
1843bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
1844bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
1845f089877aSRichard Tran Mills 
1846f089877aSRichard Tran Mills @*/
1847f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
1848f089877aSRichard Tran Mills {
1849f089877aSRichard Tran Mills   PetscErrorCode          ierr;
1850f089877aSRichard Tran Mills 
1851f089877aSRichard Tran Mills   PetscFunctionBegin;
1852f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1853f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
1854f089877aSRichard Tran Mills   PetscFunctionReturn(0);
1855f089877aSRichard Tran Mills }
1856f089877aSRichard Tran Mills 
1857f089877aSRichard Tran Mills #undef __FUNCT__
1858f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
1859f089877aSRichard Tran Mills /*@
1860bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
1861bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
1862d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
1863f089877aSRichard Tran Mills 
1864bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
1865f089877aSRichard Tran Mills 
1866f089877aSRichard Tran Mills    Input Parameters:
1867bc0a1609SRichard Tran Mills +  da - the DM object
1868bc0a1609SRichard Tran Mills .  g - the original local vector
1869bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
1870f089877aSRichard Tran Mills 
1871bc0a1609SRichard Tran Mills    Output Parameter:
1872bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
1873f089877aSRichard Tran Mills 
1874f089877aSRichard Tran Mills    Level: intermediate
1875f089877aSRichard Tran Mills 
1876bc0a1609SRichard Tran Mills    Notes:
1877bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
1878bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
1879bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
1880bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
1881bc0a1609SRichard Tran Mills 
1882bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
1883bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
1884f089877aSRichard Tran Mills 
1885f089877aSRichard Tran Mills @*/
1886f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
1887f089877aSRichard Tran Mills {
1888f089877aSRichard Tran Mills   PetscErrorCode          ierr;
1889f089877aSRichard Tran Mills 
1890f089877aSRichard Tran Mills   PetscFunctionBegin;
1891f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1892f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
1893f089877aSRichard Tran Mills   PetscFunctionReturn(0);
1894f089877aSRichard Tran Mills }
1895f089877aSRichard Tran Mills 
1896f089877aSRichard Tran Mills 
1897f089877aSRichard Tran Mills #undef __FUNCT__
189847c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
189947c6ae99SBarry Smith /*@
190047c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
190147c6ae99SBarry Smith 
190247c6ae99SBarry Smith     Collective on DM
190347c6ae99SBarry Smith 
190447c6ae99SBarry Smith     Input Parameter:
190547c6ae99SBarry Smith +   dm - the DM object
190691d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
190747c6ae99SBarry Smith 
190847c6ae99SBarry Smith     Output Parameter:
190947c6ae99SBarry Smith .   dmc - the coarsened DM
191047c6ae99SBarry Smith 
191147c6ae99SBarry Smith     Level: developer
191247c6ae99SBarry Smith 
1913e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
191447c6ae99SBarry Smith 
191547c6ae99SBarry Smith @*/
19167087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
191747c6ae99SBarry Smith {
191847c6ae99SBarry Smith   PetscErrorCode    ierr;
1919b17ce1afSJed Brown   DMCoarsenHookLink link;
192047c6ae99SBarry Smith 
192147c6ae99SBarry Smith   PetscFunctionBegin;
1922171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
192347c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
192443842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
19258cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1926644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
19270598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
1928656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
1929e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1930b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1931b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1932b17ce1afSJed Brown   }
1933b17ce1afSJed Brown   PetscFunctionReturn(0);
1934b17ce1afSJed Brown }
1935b17ce1afSJed Brown 
1936b17ce1afSJed Brown #undef __FUNCT__
1937b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1938bb9467b5SJed Brown /*@C
1939b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1940b17ce1afSJed Brown 
1941b17ce1afSJed Brown    Logically Collective
1942b17ce1afSJed Brown 
1943b17ce1afSJed Brown    Input Arguments:
1944b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1945b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1946b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
19470298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1948b17ce1afSJed Brown 
1949b17ce1afSJed Brown    Calling sequence of coarsenhook:
1950b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1951b17ce1afSJed Brown 
1952b17ce1afSJed Brown +  fine - fine level DM
1953b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1954b17ce1afSJed Brown -  ctx - optional user-defined function context
1955b17ce1afSJed Brown 
1956b17ce1afSJed Brown    Calling sequence for restricthook:
1957c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1958b17ce1afSJed Brown 
1959b17ce1afSJed Brown +  fine - fine level DM
1960b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1961c833c3b5SJed Brown .  rscale - scaling vector for restriction
1962c833c3b5SJed Brown .  inject - matrix restricting by injection
1963b17ce1afSJed Brown .  coarse - coarse level DM to update
1964b17ce1afSJed Brown -  ctx - optional user-defined function context
1965b17ce1afSJed Brown 
1966b17ce1afSJed Brown    Level: advanced
1967b17ce1afSJed Brown 
1968b17ce1afSJed Brown    Notes:
1969b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1970b17ce1afSJed Brown 
1971b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1972b17ce1afSJed Brown 
1973b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1974b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1975b17ce1afSJed Brown 
1976bb9467b5SJed Brown    This function is currently not available from Fortran.
1977bb9467b5SJed Brown 
1978c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1979b17ce1afSJed Brown @*/
1980b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1981b17ce1afSJed Brown {
1982b17ce1afSJed Brown   PetscErrorCode    ierr;
1983b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1984b17ce1afSJed Brown 
1985b17ce1afSJed Brown   PetscFunctionBegin;
1986b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
19876bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1988b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1989b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
1990b17ce1afSJed Brown   link->restricthook = restricthook;
1991b17ce1afSJed Brown   link->ctx          = ctx;
19920298fd71SBarry Smith   link->next         = NULL;
1993b17ce1afSJed Brown   *p                 = link;
1994b17ce1afSJed Brown   PetscFunctionReturn(0);
1995b17ce1afSJed Brown }
1996b17ce1afSJed Brown 
1997b17ce1afSJed Brown #undef __FUNCT__
1998b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1999b17ce1afSJed Brown /*@
2000b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2001b17ce1afSJed Brown 
2002b17ce1afSJed Brown    Collective if any hooks are
2003b17ce1afSJed Brown 
2004b17ce1afSJed Brown    Input Arguments:
2005b17ce1afSJed Brown +  fine - finer DM to use as a base
2006b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2007b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2008b17ce1afSJed Brown -  coarse - coarer DM to update
2009b17ce1afSJed Brown 
2010b17ce1afSJed Brown    Level: developer
2011b17ce1afSJed Brown 
2012b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2013b17ce1afSJed Brown @*/
2014b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2015b17ce1afSJed Brown {
2016b17ce1afSJed Brown   PetscErrorCode    ierr;
2017b17ce1afSJed Brown   DMCoarsenHookLink link;
2018b17ce1afSJed Brown 
2019b17ce1afSJed Brown   PetscFunctionBegin;
2020b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
20218865f1eaSKarl Rupp     if (link->restricthook) {
20228865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
20238865f1eaSKarl Rupp     }
2024b17ce1afSJed Brown   }
202547c6ae99SBarry Smith   PetscFunctionReturn(0);
202647c6ae99SBarry Smith }
202747c6ae99SBarry Smith 
202847c6ae99SBarry Smith #undef __FUNCT__
2029be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2030bb9467b5SJed Brown /*@C
2031be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
20325dbd56e3SPeter Brune 
20335dbd56e3SPeter Brune    Logically Collective
20345dbd56e3SPeter Brune 
20355dbd56e3SPeter Brune    Input Arguments:
20365dbd56e3SPeter Brune +  global - global DM
2037ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
20385dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
20390298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
20405dbd56e3SPeter Brune 
2041ec4806b8SPeter Brune 
2042ec4806b8SPeter Brune    Calling sequence for ddhook:
2043ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2044ec4806b8SPeter Brune 
2045ec4806b8SPeter Brune +  global - global DM
2046ec4806b8SPeter Brune .  block  - block DM
2047ec4806b8SPeter Brune -  ctx - optional user-defined function context
2048ec4806b8SPeter Brune 
20495dbd56e3SPeter Brune    Calling sequence for restricthook:
2050ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
20515dbd56e3SPeter Brune 
20525dbd56e3SPeter Brune +  global - global DM
20535dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
20545dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2055ec4806b8SPeter Brune .  block  - block DM
20565dbd56e3SPeter Brune -  ctx - optional user-defined function context
20575dbd56e3SPeter Brune 
20585dbd56e3SPeter Brune    Level: advanced
20595dbd56e3SPeter Brune 
20605dbd56e3SPeter Brune    Notes:
2061ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
20625dbd56e3SPeter Brune 
20635dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
20645dbd56e3SPeter Brune 
20655dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2066ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
20675dbd56e3SPeter Brune 
2068bb9467b5SJed Brown    This function is currently not available from Fortran.
2069bb9467b5SJed Brown 
20705dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20715dbd56e3SPeter Brune @*/
2072be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
20735dbd56e3SPeter Brune {
20745dbd56e3SPeter Brune   PetscErrorCode      ierr;
2075be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
20765dbd56e3SPeter Brune 
20775dbd56e3SPeter Brune   PetscFunctionBegin;
20785dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2079be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2080be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
20815dbd56e3SPeter Brune   link->restricthook = restricthook;
2082be081cd6SPeter Brune   link->ddhook       = ddhook;
20835dbd56e3SPeter Brune   link->ctx          = ctx;
20840298fd71SBarry Smith   link->next         = NULL;
20855dbd56e3SPeter Brune   *p                 = link;
20865dbd56e3SPeter Brune   PetscFunctionReturn(0);
20875dbd56e3SPeter Brune }
20885dbd56e3SPeter Brune 
20895dbd56e3SPeter Brune #undef __FUNCT__
2090be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
20915dbd56e3SPeter Brune /*@
2092be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
20935dbd56e3SPeter Brune 
20945dbd56e3SPeter Brune    Collective if any hooks are
20955dbd56e3SPeter Brune 
20965dbd56e3SPeter Brune    Input Arguments:
20975dbd56e3SPeter Brune +  fine - finer DM to use as a base
2098be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2099be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
21005dbd56e3SPeter Brune -  coarse - coarer DM to update
21015dbd56e3SPeter Brune 
21025dbd56e3SPeter Brune    Level: developer
21035dbd56e3SPeter Brune 
21045dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
21055dbd56e3SPeter Brune @*/
2106be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
21075dbd56e3SPeter Brune {
21085dbd56e3SPeter Brune   PetscErrorCode      ierr;
2109be081cd6SPeter Brune   DMSubDomainHookLink link;
21105dbd56e3SPeter Brune 
21115dbd56e3SPeter Brune   PetscFunctionBegin;
2112be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
21138865f1eaSKarl Rupp     if (link->restricthook) {
21148865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
21158865f1eaSKarl Rupp     }
21165dbd56e3SPeter Brune   }
21175dbd56e3SPeter Brune   PetscFunctionReturn(0);
21185dbd56e3SPeter Brune }
21195dbd56e3SPeter Brune 
21205dbd56e3SPeter Brune #undef __FUNCT__
21215fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
21225fe1f584SPeter Brune /*@
21236a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
21245fe1f584SPeter Brune 
21255fe1f584SPeter Brune     Not Collective
21265fe1f584SPeter Brune 
21275fe1f584SPeter Brune     Input Parameter:
21285fe1f584SPeter Brune .   dm - the DM object
21295fe1f584SPeter Brune 
21305fe1f584SPeter Brune     Output Parameter:
21316a7d9d85SPeter Brune .   level - number of coarsenings
21325fe1f584SPeter Brune 
21335fe1f584SPeter Brune     Level: developer
21345fe1f584SPeter Brune 
21356a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
21365fe1f584SPeter Brune 
21375fe1f584SPeter Brune @*/
21385fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
21395fe1f584SPeter Brune {
21405fe1f584SPeter Brune   PetscFunctionBegin;
21415fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21425fe1f584SPeter Brune   *level = dm->leveldown;
21435fe1f584SPeter Brune   PetscFunctionReturn(0);
21445fe1f584SPeter Brune }
21455fe1f584SPeter Brune 
21465fe1f584SPeter Brune 
21475fe1f584SPeter Brune 
21485fe1f584SPeter Brune #undef __FUNCT__
214947c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
215047c6ae99SBarry Smith /*@C
215147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
215247c6ae99SBarry Smith 
215347c6ae99SBarry Smith     Collective on DM
215447c6ae99SBarry Smith 
215547c6ae99SBarry Smith     Input Parameter:
215647c6ae99SBarry Smith +   dm - the DM object
215747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
215847c6ae99SBarry Smith 
215947c6ae99SBarry Smith     Output Parameter:
216047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
216147c6ae99SBarry Smith 
216247c6ae99SBarry Smith     Level: developer
216347c6ae99SBarry Smith 
2164e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
216547c6ae99SBarry Smith 
216647c6ae99SBarry Smith @*/
21677087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
216847c6ae99SBarry Smith {
216947c6ae99SBarry Smith   PetscErrorCode ierr;
217047c6ae99SBarry Smith 
217147c6ae99SBarry Smith   PetscFunctionBegin;
2172171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2173ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
217447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
217547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
217647c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
217747c6ae99SBarry Smith   } else if (dm->ops->refine) {
217847c6ae99SBarry Smith     PetscInt i;
217947c6ae99SBarry Smith 
2180ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
218147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2182ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
218347c6ae99SBarry Smith     }
2184ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
218547c6ae99SBarry Smith   PetscFunctionReturn(0);
218647c6ae99SBarry Smith }
218747c6ae99SBarry Smith 
218847c6ae99SBarry Smith #undef __FUNCT__
218947c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
219047c6ae99SBarry Smith /*@C
219147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
219247c6ae99SBarry Smith 
219347c6ae99SBarry Smith     Collective on DM
219447c6ae99SBarry Smith 
219547c6ae99SBarry Smith     Input Parameter:
219647c6ae99SBarry Smith +   dm - the DM object
219747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
219847c6ae99SBarry Smith 
219947c6ae99SBarry Smith     Output Parameter:
220047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
220147c6ae99SBarry Smith 
220247c6ae99SBarry Smith     Level: developer
220347c6ae99SBarry Smith 
2204e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
220547c6ae99SBarry Smith 
220647c6ae99SBarry Smith @*/
22077087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
220847c6ae99SBarry Smith {
220947c6ae99SBarry Smith   PetscErrorCode ierr;
221047c6ae99SBarry Smith 
221147c6ae99SBarry Smith   PetscFunctionBegin;
2212171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2213ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
221447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
221547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
221647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
221747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
221847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
221947c6ae99SBarry Smith     PetscInt i;
222047c6ae99SBarry Smith 
2221ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
222247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2223ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
222447c6ae99SBarry Smith     }
2225ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
222647c6ae99SBarry Smith   PetscFunctionReturn(0);
222747c6ae99SBarry Smith }
222847c6ae99SBarry Smith 
222947c6ae99SBarry Smith #undef __FUNCT__
2230e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
223147c6ae99SBarry Smith /*@
2232e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
223347c6ae99SBarry Smith    grids associated with two DMs.
223447c6ae99SBarry Smith 
223547c6ae99SBarry Smith    Collective on DM
223647c6ae99SBarry Smith 
223747c6ae99SBarry Smith    Input Parameters:
223847c6ae99SBarry Smith +  dmc - the coarse grid DM
223947c6ae99SBarry Smith -  dmf - the fine grid DM
224047c6ae99SBarry Smith 
224147c6ae99SBarry Smith    Output Parameters:
224247c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
224347c6ae99SBarry Smith 
224447c6ae99SBarry Smith    Level: intermediate
224547c6ae99SBarry Smith 
224647c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
224747c6ae99SBarry Smith 
2248e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
224947c6ae99SBarry Smith @*/
2250e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
225147c6ae99SBarry Smith {
225247c6ae99SBarry Smith   PetscErrorCode ierr;
225347c6ae99SBarry Smith 
225447c6ae99SBarry Smith   PetscFunctionBegin;
2255171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2256171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
225747c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
225847c6ae99SBarry Smith   PetscFunctionReturn(0);
225947c6ae99SBarry Smith }
226047c6ae99SBarry Smith 
226147c6ae99SBarry Smith #undef __FUNCT__
22621a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
22631a266240SBarry Smith /*@C
22641a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
22651a266240SBarry Smith 
22661a266240SBarry Smith     Not Collective
22671a266240SBarry Smith 
22681a266240SBarry Smith     Input Parameters:
22691a266240SBarry Smith +   dm - the DM object
22701a266240SBarry Smith -   destroy - the destroy function
22711a266240SBarry Smith 
22721a266240SBarry Smith     Level: intermediate
22731a266240SBarry Smith 
2274e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
22751a266240SBarry Smith 
2276f07f9ceaSJed Brown @*/
22771a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
22781a266240SBarry Smith {
22791a266240SBarry Smith   PetscFunctionBegin;
2280171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22811a266240SBarry Smith   dm->ctxdestroy = destroy;
22821a266240SBarry Smith   PetscFunctionReturn(0);
22831a266240SBarry Smith }
22841a266240SBarry Smith 
22851a266240SBarry Smith #undef __FUNCT__
22861b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2287b07ff414SBarry Smith /*@
22881b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
228947c6ae99SBarry Smith 
229047c6ae99SBarry Smith     Not Collective
229147c6ae99SBarry Smith 
229247c6ae99SBarry Smith     Input Parameters:
229347c6ae99SBarry Smith +   dm - the DM object
229447c6ae99SBarry Smith -   ctx - the user context
229547c6ae99SBarry Smith 
229647c6ae99SBarry Smith     Level: intermediate
229747c6ae99SBarry Smith 
2298e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
229947c6ae99SBarry Smith 
230047c6ae99SBarry Smith @*/
23011b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
230247c6ae99SBarry Smith {
230347c6ae99SBarry Smith   PetscFunctionBegin;
2304171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
230547c6ae99SBarry Smith   dm->ctx = ctx;
230647c6ae99SBarry Smith   PetscFunctionReturn(0);
230747c6ae99SBarry Smith }
230847c6ae99SBarry Smith 
230947c6ae99SBarry Smith #undef __FUNCT__
23101b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
231147c6ae99SBarry Smith /*@
23121b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
231347c6ae99SBarry Smith 
231447c6ae99SBarry Smith     Not Collective
231547c6ae99SBarry Smith 
231647c6ae99SBarry Smith     Input Parameter:
231747c6ae99SBarry Smith .   dm - the DM object
231847c6ae99SBarry Smith 
231947c6ae99SBarry Smith     Output Parameter:
232047c6ae99SBarry Smith .   ctx - the user context
232147c6ae99SBarry Smith 
232247c6ae99SBarry Smith     Level: intermediate
232347c6ae99SBarry Smith 
2324e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
232547c6ae99SBarry Smith 
232647c6ae99SBarry Smith @*/
23271b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
232847c6ae99SBarry Smith {
232947c6ae99SBarry Smith   PetscFunctionBegin;
2330171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23311b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
233247c6ae99SBarry Smith   PetscFunctionReturn(0);
233347c6ae99SBarry Smith }
233447c6ae99SBarry Smith 
233547c6ae99SBarry Smith #undef __FUNCT__
233608da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
233708da532bSDmitry Karpeev /*@C
233808da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
233908da532bSDmitry Karpeev 
234008da532bSDmitry Karpeev     Logically Collective on DM
234108da532bSDmitry Karpeev 
234208da532bSDmitry Karpeev     Input Parameter:
234308da532bSDmitry Karpeev +   dm - the DM object
23440298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
234508da532bSDmitry Karpeev 
234608da532bSDmitry Karpeev     Level: intermediate
234708da532bSDmitry Karpeev 
2348835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
234908da532bSDmitry Karpeev          DMSetJacobian()
235008da532bSDmitry Karpeev 
235108da532bSDmitry Karpeev @*/
235208da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
235308da532bSDmitry Karpeev {
235408da532bSDmitry Karpeev   PetscFunctionBegin;
235508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
235608da532bSDmitry Karpeev   PetscFunctionReturn(0);
235708da532bSDmitry Karpeev }
235808da532bSDmitry Karpeev 
235908da532bSDmitry Karpeev #undef __FUNCT__
236008da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
236108da532bSDmitry Karpeev /*@
236208da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
236308da532bSDmitry Karpeev 
236408da532bSDmitry Karpeev     Not Collective
236508da532bSDmitry Karpeev 
236608da532bSDmitry Karpeev     Input Parameter:
236708da532bSDmitry Karpeev .   dm - the DM object to destroy
236808da532bSDmitry Karpeev 
236908da532bSDmitry Karpeev     Output Parameter:
237008da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
237108da532bSDmitry Karpeev 
237208da532bSDmitry Karpeev     Level: developer
237308da532bSDmitry Karpeev 
237474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
237508da532bSDmitry Karpeev 
237608da532bSDmitry Karpeev @*/
237708da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
237808da532bSDmitry Karpeev {
237908da532bSDmitry Karpeev   PetscFunctionBegin;
238008da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
238108da532bSDmitry Karpeev   PetscFunctionReturn(0);
238208da532bSDmitry Karpeev }
238308da532bSDmitry Karpeev 
238408da532bSDmitry Karpeev #undef __FUNCT__
238508da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
238608da532bSDmitry Karpeev /*@C
238708da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
238808da532bSDmitry Karpeev 
238908da532bSDmitry Karpeev     Logically Collective on DM
239008da532bSDmitry Karpeev 
239108da532bSDmitry Karpeev     Input Parameters:
239208da532bSDmitry Karpeev +   dm - the DM object to destroy
239308da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
239408da532bSDmitry Karpeev 
239508da532bSDmitry Karpeev     Output parameters:
239608da532bSDmitry Karpeev +   xl - lower bound
239708da532bSDmitry Karpeev -   xu - upper bound
239808da532bSDmitry Karpeev 
239908da532bSDmitry Karpeev     Level: intermediate
240008da532bSDmitry Karpeev 
240174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
240208da532bSDmitry Karpeev 
240308da532bSDmitry Karpeev @*/
240408da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
240508da532bSDmitry Karpeev {
240608da532bSDmitry Karpeev   PetscErrorCode ierr;
24075fd66863SKarl Rupp 
240808da532bSDmitry Karpeev   PetscFunctionBegin;
240908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
241008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
241108da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
241208da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
24138865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
241408da532bSDmitry Karpeev   PetscFunctionReturn(0);
241508da532bSDmitry Karpeev }
241608da532bSDmitry Karpeev 
241708da532bSDmitry Karpeev #undef __FUNCT__
2418b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2419b0ae01b7SPeter Brune /*@
2420b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2421b0ae01b7SPeter Brune 
2422b0ae01b7SPeter Brune     Not Collective
2423b0ae01b7SPeter Brune 
2424b0ae01b7SPeter Brune     Input Parameter:
2425b0ae01b7SPeter Brune .   dm - the DM object
2426b0ae01b7SPeter Brune 
2427b0ae01b7SPeter Brune     Output Parameter:
2428b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2429b0ae01b7SPeter Brune 
2430b0ae01b7SPeter Brune     Level: developer
2431b0ae01b7SPeter Brune 
2432b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2433b0ae01b7SPeter Brune 
2434b0ae01b7SPeter Brune @*/
2435b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2436b0ae01b7SPeter Brune {
2437b0ae01b7SPeter Brune   PetscFunctionBegin;
2438b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2439b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2440b0ae01b7SPeter Brune }
2441b0ae01b7SPeter Brune 
2442b0ae01b7SPeter Brune #undef  __FUNCT__
244308da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2444748fac09SDmitry Karpeev /*@C
244508da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
244608da532bSDmitry Karpeev 
244708da532bSDmitry Karpeev     Collective on DM
244808da532bSDmitry Karpeev 
244908da532bSDmitry Karpeev     Input Parameter:
245008da532bSDmitry Karpeev +   dm - the DM object
24510298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
245208da532bSDmitry Karpeev 
245308da532bSDmitry Karpeev     Level: developer
245408da532bSDmitry Karpeev 
245574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
245608da532bSDmitry Karpeev 
245708da532bSDmitry Karpeev @*/
245808da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
245908da532bSDmitry Karpeev {
246008da532bSDmitry Karpeev   PetscErrorCode ierr;
24615fd66863SKarl Rupp 
246208da532bSDmitry Karpeev   PetscFunctionBegin;
246308da532bSDmitry Karpeev   if (x) {
246408da532bSDmitry Karpeev     if (!dm->x) {
246508da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
246608da532bSDmitry Karpeev     }
246708da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
24688865f1eaSKarl Rupp   } else if (dm->x) {
246908da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
247008da532bSDmitry Karpeev   }
247108da532bSDmitry Karpeev   PetscFunctionReturn(0);
247208da532bSDmitry Karpeev }
247308da532bSDmitry Karpeev 
24740298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2475264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2476264ace61SBarry Smith 
2477264ace61SBarry Smith #undef __FUNCT__
2478264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2479264ace61SBarry Smith /*@C
2480264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2481264ace61SBarry Smith 
2482264ace61SBarry Smith   Collective on DM
2483264ace61SBarry Smith 
2484264ace61SBarry Smith   Input Parameters:
2485264ace61SBarry Smith + dm     - The DM object
2486264ace61SBarry Smith - method - The name of the DM type
2487264ace61SBarry Smith 
2488264ace61SBarry Smith   Options Database Key:
2489264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2490264ace61SBarry Smith 
2491264ace61SBarry Smith   Notes:
2492e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2493264ace61SBarry Smith 
2494264ace61SBarry Smith   Level: intermediate
2495264ace61SBarry Smith 
2496264ace61SBarry Smith .keywords: DM, set, type
2497264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2498264ace61SBarry Smith @*/
249919fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2500264ace61SBarry Smith {
2501264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2502264ace61SBarry Smith   PetscBool      match;
2503264ace61SBarry Smith   PetscErrorCode ierr;
2504264ace61SBarry Smith 
2505264ace61SBarry Smith   PetscFunctionBegin;
2506264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2507251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2508264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2509264ace61SBarry Smith 
2510607a6623SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll();CHKERRQ(ierr);}
25111c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2512ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2513264ace61SBarry Smith 
2514264ace61SBarry Smith   if (dm->ops->destroy) {
2515264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
25160298fd71SBarry Smith     dm->ops->destroy = NULL;
2517264ace61SBarry Smith   }
2518264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2519264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2520264ace61SBarry Smith   PetscFunctionReturn(0);
2521264ace61SBarry Smith }
2522264ace61SBarry Smith 
2523264ace61SBarry Smith #undef __FUNCT__
2524264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2525264ace61SBarry Smith /*@C
2526264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2527264ace61SBarry Smith 
2528264ace61SBarry Smith   Not Collective
2529264ace61SBarry Smith 
2530264ace61SBarry Smith   Input Parameter:
2531264ace61SBarry Smith . dm  - The DM
2532264ace61SBarry Smith 
2533264ace61SBarry Smith   Output Parameter:
2534264ace61SBarry Smith . type - The DM type name
2535264ace61SBarry Smith 
2536264ace61SBarry Smith   Level: intermediate
2537264ace61SBarry Smith 
2538264ace61SBarry Smith .keywords: DM, get, type, name
2539264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2540264ace61SBarry Smith @*/
254119fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2542264ace61SBarry Smith {
2543264ace61SBarry Smith   PetscErrorCode ierr;
2544264ace61SBarry Smith 
2545264ace61SBarry Smith   PetscFunctionBegin;
2546264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2547264ace61SBarry Smith   PetscValidCharPointer(type,2);
2548264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2549607a6623SBarry Smith     ierr = DMRegisterAll();CHKERRQ(ierr);
2550264ace61SBarry Smith   }
2551264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2552264ace61SBarry Smith   PetscFunctionReturn(0);
2553264ace61SBarry Smith }
2554264ace61SBarry Smith 
255567a56275SMatthew G Knepley #undef __FUNCT__
255667a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
255767a56275SMatthew G Knepley /*@C
255867a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
255967a56275SMatthew G Knepley 
256067a56275SMatthew G Knepley   Collective on DM
256167a56275SMatthew G Knepley 
256267a56275SMatthew G Knepley   Input Parameters:
256367a56275SMatthew G Knepley + dm - the DM
256467a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
256567a56275SMatthew G Knepley 
256667a56275SMatthew G Knepley   Output Parameter:
256767a56275SMatthew G Knepley . M - pointer to new DM
256867a56275SMatthew G Knepley 
256967a56275SMatthew G Knepley   Notes:
257067a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
257167a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
257267a56275SMatthew G Knepley   of the input DM.
257367a56275SMatthew G Knepley 
257467a56275SMatthew G Knepley   Level: intermediate
257567a56275SMatthew G Knepley 
257667a56275SMatthew G Knepley .seealso: DMCreate()
257767a56275SMatthew G Knepley @*/
257819fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
257967a56275SMatthew G Knepley {
258067a56275SMatthew G Knepley   DM             B;
258167a56275SMatthew G Knepley   char           convname[256];
258267a56275SMatthew G Knepley   PetscBool      sametype, issame;
258367a56275SMatthew G Knepley   PetscErrorCode ierr;
258467a56275SMatthew G Knepley 
258567a56275SMatthew G Knepley   PetscFunctionBegin;
258667a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
258767a56275SMatthew G Knepley   PetscValidType(dm,1);
258867a56275SMatthew G Knepley   PetscValidPointer(M,3);
2589251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
259067a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
259167a56275SMatthew G Knepley   {
25920298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
259367a56275SMatthew G Knepley 
259467a56275SMatthew G Knepley     /*
259567a56275SMatthew G Knepley        Order of precedence:
259667a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
259767a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
259867a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
259967a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
260067a56275SMatthew G Knepley        5) Use a really basic converter.
260167a56275SMatthew G Knepley     */
260267a56275SMatthew G Knepley 
260367a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
260467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
260567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
260667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
260767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
260867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
26090005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
261067a56275SMatthew G Knepley     if (conv) goto foundconv;
261167a56275SMatthew G Knepley 
261267a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
261382f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
261467a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
261567a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
261667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
261767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
261867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
261967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
26200005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
262167a56275SMatthew G Knepley     if (conv) {
2622fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
262367a56275SMatthew G Knepley       goto foundconv;
262467a56275SMatthew G Knepley     }
262567a56275SMatthew G Knepley 
262667a56275SMatthew G Knepley #if 0
262767a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
262867a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2629fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
263067a56275SMatthew G Knepley     if (conv) goto foundconv;
263167a56275SMatthew G Knepley 
263267a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
263367a56275SMatthew G Knepley     if (dm->ops->convert) {
263467a56275SMatthew G Knepley       conv = dm->ops->convert;
263567a56275SMatthew G Knepley     }
263667a56275SMatthew G Knepley     if (conv) goto foundconv;
263767a56275SMatthew G Knepley #endif
263867a56275SMatthew G Knepley 
263967a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
264082f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
264167a56275SMatthew G Knepley 
264267a56275SMatthew G Knepley foundconv:
264367a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
264467a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
264567a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
264667a56275SMatthew G Knepley   }
264767a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
264867a56275SMatthew G Knepley   PetscFunctionReturn(0);
264967a56275SMatthew G Knepley }
2650264ace61SBarry Smith 
2651264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2652264ace61SBarry Smith 
2653264ace61SBarry Smith #undef __FUNCT__
2654264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2655264ace61SBarry Smith /*@C
26561c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
26571c84c290SBarry Smith 
26581c84c290SBarry Smith   Not Collective
26591c84c290SBarry Smith 
26601c84c290SBarry Smith   Input Parameters:
26611c84c290SBarry Smith + name        - The name of a new user-defined creation routine
26621c84c290SBarry Smith - create_func - The creation routine itself
26631c84c290SBarry Smith 
26641c84c290SBarry Smith   Notes:
26651c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
26661c84c290SBarry Smith 
26671c84c290SBarry Smith 
26681c84c290SBarry Smith   Sample usage:
26691c84c290SBarry Smith .vb
2670bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
26711c84c290SBarry Smith .ve
26721c84c290SBarry Smith 
26731c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
26741c84c290SBarry Smith .vb
26751c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
26761c84c290SBarry Smith     DMSetType(DM,"my_da");
26771c84c290SBarry Smith .ve
26781c84c290SBarry Smith    or at runtime via the option
26791c84c290SBarry Smith .vb
26801c84c290SBarry Smith     -da_type my_da
26811c84c290SBarry Smith .ve
2682264ace61SBarry Smith 
2683264ace61SBarry Smith   Level: advanced
26841c84c290SBarry Smith 
26851c84c290SBarry Smith .keywords: DM, register
2686bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
26871c84c290SBarry Smith 
2688264ace61SBarry Smith @*/
2689bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2690264ace61SBarry Smith {
2691264ace61SBarry Smith   PetscErrorCode ierr;
2692264ace61SBarry Smith 
2693264ace61SBarry Smith   PetscFunctionBegin;
2694a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2695264ace61SBarry Smith   PetscFunctionReturn(0);
2696264ace61SBarry Smith }
2697264ace61SBarry Smith 
2698b859378eSBarry Smith #undef __FUNCT__
2699b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2700b859378eSBarry Smith /*@C
270155849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2702b859378eSBarry Smith 
2703b859378eSBarry Smith   Collective on PetscViewer
2704b859378eSBarry Smith 
2705b859378eSBarry Smith   Input Parameters:
2706b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2707b859378eSBarry Smith            some related function before a call to DMLoad().
2708b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2709b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2710b859378eSBarry Smith 
2711b859378eSBarry Smith    Level: intermediate
2712b859378eSBarry Smith 
2713b859378eSBarry Smith   Notes:
271455849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2715b859378eSBarry Smith 
2716b859378eSBarry Smith   Notes for advanced users:
2717b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2718b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2719b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2720b859378eSBarry Smith   format is
2721b859378eSBarry Smith .vb
2722b859378eSBarry Smith      has not yet been determined
2723b859378eSBarry Smith .ve
2724b859378eSBarry Smith 
2725b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2726b859378eSBarry Smith @*/
2727b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2728b859378eSBarry Smith {
27299331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
2730b859378eSBarry Smith   PetscErrorCode ierr;
2731b859378eSBarry Smith 
2732b859378eSBarry Smith   PetscFunctionBegin;
2733b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2734b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
273532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
27369331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
27379331c7a4SMatthew G. Knepley   if (isbinary) {
27389331c7a4SMatthew G. Knepley     PetscInt classid;
27399331c7a4SMatthew G. Knepley     char     type[256];
2740b859378eSBarry Smith 
274132c0f0efSBarry Smith     ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
27429200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
274332c0f0efSBarry Smith     ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
274432c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
27459331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
27469331c7a4SMatthew G. Knepley   } else if (ishdf5) {
27479331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
27489331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
2749b859378eSBarry Smith   PetscFunctionReturn(0);
2750b859378eSBarry Smith }
2751b859378eSBarry Smith 
27527da65231SMatthew G Knepley /******************************** FEM Support **********************************/
27537da65231SMatthew G Knepley 
27547da65231SMatthew G Knepley #undef __FUNCT__
27557da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
2756a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
2757a6dfd86eSKarl Rupp {
27581d47ebbbSSatish Balay   PetscInt       f;
27591b30c384SMatthew G Knepley   PetscErrorCode ierr;
27601b30c384SMatthew G Knepley 
27617da65231SMatthew G Knepley   PetscFunctionBegin;
276274778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27631d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
276457622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
27657da65231SMatthew G Knepley   }
27667da65231SMatthew G Knepley   PetscFunctionReturn(0);
27677da65231SMatthew G Knepley }
27687da65231SMatthew G Knepley 
27697da65231SMatthew G Knepley #undef __FUNCT__
27707da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
2771a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
2772a6dfd86eSKarl Rupp {
27731b30c384SMatthew G Knepley   PetscInt       f, g;
27747da65231SMatthew G Knepley   PetscErrorCode ierr;
27757da65231SMatthew G Knepley 
27767da65231SMatthew G Knepley   PetscFunctionBegin;
277774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27781d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
277974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
27801d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
2781e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
27827da65231SMatthew G Knepley     }
278374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
27847da65231SMatthew G Knepley   }
27857da65231SMatthew G Knepley   PetscFunctionReturn(0);
27867da65231SMatthew G Knepley }
2787e7c4fc90SDmitry Karpeev 
2788970e74d5SMatthew G Knepley #undef __FUNCT__
2789e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
27906113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
2791e759306cSMatthew G. Knepley {
2792e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
2793e759306cSMatthew G. Knepley   PetscInt       p;
2794e759306cSMatthew G. Knepley   PetscErrorCode ierr;
2795e759306cSMatthew G. Knepley 
2796e759306cSMatthew G. Knepley   PetscFunctionBegin;
2797e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
2798e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
2799e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
2800e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
2801e759306cSMatthew G. Knepley     if (p == rank) {
2802e759306cSMatthew G. Knepley       Vec x;
2803e759306cSMatthew G. Knepley 
2804e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
2805e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
28066113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
2807e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
2808e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
2809e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
2810e759306cSMatthew G. Knepley     }
2811e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
2812e759306cSMatthew G. Knepley   }
2813e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
2814e759306cSMatthew G. Knepley }
2815e759306cSMatthew G. Knepley 
2816e759306cSMatthew G. Knepley #undef __FUNCT__
281788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
281888ed4aceSMatthew G Knepley /*@
281988ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
282088ed4aceSMatthew G Knepley 
282188ed4aceSMatthew G Knepley   Input Parameter:
282288ed4aceSMatthew G Knepley . dm - The DM
282388ed4aceSMatthew G Knepley 
282488ed4aceSMatthew G Knepley   Output Parameter:
282588ed4aceSMatthew G Knepley . section - The PetscSection
282688ed4aceSMatthew G Knepley 
282788ed4aceSMatthew G Knepley   Level: intermediate
282888ed4aceSMatthew G Knepley 
282988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
283088ed4aceSMatthew G Knepley 
283188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
283288ed4aceSMatthew G Knepley @*/
28330adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
28340adebc6cSBarry Smith {
2835fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
2836fd59a867SMatthew G. Knepley 
283788ed4aceSMatthew G Knepley   PetscFunctionBegin;
283888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
283988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
2840fd59a867SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);}
284188ed4aceSMatthew G Knepley   *section = dm->defaultSection;
284288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
284388ed4aceSMatthew G Knepley }
284488ed4aceSMatthew G Knepley 
284588ed4aceSMatthew G Knepley #undef __FUNCT__
284688ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
284788ed4aceSMatthew G Knepley /*@
284888ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
284988ed4aceSMatthew G Knepley 
285088ed4aceSMatthew G Knepley   Input Parameters:
285188ed4aceSMatthew G Knepley + dm - The DM
285288ed4aceSMatthew G Knepley - section - The PetscSection
285388ed4aceSMatthew G Knepley 
285488ed4aceSMatthew G Knepley   Level: intermediate
285588ed4aceSMatthew G Knepley 
285688ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
285788ed4aceSMatthew G Knepley 
285888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
285988ed4aceSMatthew G Knepley @*/
28600adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
28610adebc6cSBarry Smith {
2862af122d2aSMatthew G Knepley   PetscInt       numFields;
2863af122d2aSMatthew G Knepley   PetscInt       f;
286488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
286588ed4aceSMatthew G Knepley 
286688ed4aceSMatthew G Knepley   PetscFunctionBegin;
286788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28681d799100SJed Brown   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
28691d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
287088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
287188ed4aceSMatthew G Knepley   dm->defaultSection = section;
2872af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
2873af122d2aSMatthew G Knepley   if (numFields) {
2874af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
2875af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
28760f21e855SMatthew G. Knepley       PetscObject disc;
2877af122d2aSMatthew G Knepley       const char *name;
2878af122d2aSMatthew G Knepley 
2879af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
28800f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
28810f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
2882af122d2aSMatthew G Knepley     }
2883af122d2aSMatthew G Knepley   }
28841d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
28851d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
288688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
288788ed4aceSMatthew G Knepley }
288888ed4aceSMatthew G Knepley 
288988ed4aceSMatthew G Knepley #undef __FUNCT__
289088ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
289188ed4aceSMatthew G Knepley /*@
289288ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
289388ed4aceSMatthew G Knepley 
28948b1ab98fSJed Brown   Collective on DM
28958b1ab98fSJed Brown 
289688ed4aceSMatthew G Knepley   Input Parameter:
289788ed4aceSMatthew G Knepley . dm - The DM
289888ed4aceSMatthew G Knepley 
289988ed4aceSMatthew G Knepley   Output Parameter:
290088ed4aceSMatthew G Knepley . section - The PetscSection
290188ed4aceSMatthew G Knepley 
290288ed4aceSMatthew G Knepley   Level: intermediate
290388ed4aceSMatthew G Knepley 
290488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
290588ed4aceSMatthew G Knepley 
290688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
290788ed4aceSMatthew G Knepley @*/
29080adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
29090adebc6cSBarry Smith {
291088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
291188ed4aceSMatthew G Knepley 
291288ed4aceSMatthew G Knepley   PetscFunctionBegin;
291388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
291488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
291588ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
2916fd59a867SMatthew G. Knepley     PetscSection s;
2917fd59a867SMatthew G. Knepley 
2918fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
2919fd59a867SMatthew G. Knepley     if (!s)  SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
2920fd59a867SMatthew G. Knepley     if (!dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSF in order to create a global PetscSection");
2921fd59a867SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
2922cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
2923ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
292488ed4aceSMatthew G Knepley   }
292588ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
292688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
292788ed4aceSMatthew G Knepley }
292888ed4aceSMatthew G Knepley 
292988ed4aceSMatthew G Knepley #undef __FUNCT__
2930b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
2931b21d0597SMatthew G Knepley /*@
2932b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
2933b21d0597SMatthew G Knepley 
2934b21d0597SMatthew G Knepley   Input Parameters:
2935b21d0597SMatthew G Knepley + dm - The DM
29365080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
2937b21d0597SMatthew G Knepley 
2938b21d0597SMatthew G Knepley   Level: intermediate
2939b21d0597SMatthew G Knepley 
2940b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
2941b21d0597SMatthew G Knepley 
2942b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
2943b21d0597SMatthew G Knepley @*/
29440adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
29450adebc6cSBarry Smith {
2946b21d0597SMatthew G Knepley   PetscErrorCode ierr;
2947b21d0597SMatthew G Knepley 
2948b21d0597SMatthew G Knepley   PetscFunctionBegin;
2949b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29505080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
29511d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
2952b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
2953b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
2954b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2955b21d0597SMatthew G Knepley }
2956b21d0597SMatthew G Knepley 
2957b21d0597SMatthew G Knepley #undef __FUNCT__
295888ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
295988ed4aceSMatthew G Knepley /*@
296088ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
296188ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
296288ed4aceSMatthew G Knepley 
296388ed4aceSMatthew G Knepley   Input Parameter:
296488ed4aceSMatthew G Knepley . dm - The DM
296588ed4aceSMatthew G Knepley 
296688ed4aceSMatthew G Knepley   Output Parameter:
296788ed4aceSMatthew G Knepley . sf - The PetscSF
296888ed4aceSMatthew G Knepley 
296988ed4aceSMatthew G Knepley   Level: intermediate
297088ed4aceSMatthew G Knepley 
297188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
297288ed4aceSMatthew G Knepley 
297388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
297488ed4aceSMatthew G Knepley @*/
29750adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
29760adebc6cSBarry Smith {
297788ed4aceSMatthew G Knepley   PetscInt       nroots;
297888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
297988ed4aceSMatthew G Knepley 
298088ed4aceSMatthew G Knepley   PetscFunctionBegin;
298188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
298288ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
29830298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
298488ed4aceSMatthew G Knepley   if (nroots < 0) {
298588ed4aceSMatthew G Knepley     PetscSection section, gSection;
298688ed4aceSMatthew G Knepley 
298788ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
298831ea6d37SMatthew G Knepley     if (section) {
298988ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
299088ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
299131ea6d37SMatthew G Knepley     } else {
29920298fd71SBarry Smith       *sf = NULL;
299331ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
299431ea6d37SMatthew G Knepley     }
299588ed4aceSMatthew G Knepley   }
299688ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
299788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
299888ed4aceSMatthew G Knepley }
299988ed4aceSMatthew G Knepley 
300088ed4aceSMatthew G Knepley #undef __FUNCT__
300188ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
300288ed4aceSMatthew G Knepley /*@
300388ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
300488ed4aceSMatthew G Knepley 
300588ed4aceSMatthew G Knepley   Input Parameters:
300688ed4aceSMatthew G Knepley + dm - The DM
300788ed4aceSMatthew G Knepley - sf - The PetscSF
300888ed4aceSMatthew G Knepley 
300988ed4aceSMatthew G Knepley   Level: intermediate
301088ed4aceSMatthew G Knepley 
301188ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
301288ed4aceSMatthew G Knepley 
301388ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
301488ed4aceSMatthew G Knepley @*/
30150adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
30160adebc6cSBarry Smith {
301788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
301888ed4aceSMatthew G Knepley 
301988ed4aceSMatthew G Knepley   PetscFunctionBegin;
302088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
302188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
302288ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
302388ed4aceSMatthew G Knepley   dm->defaultSF = sf;
302488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
302588ed4aceSMatthew G Knepley }
302688ed4aceSMatthew G Knepley 
302788ed4aceSMatthew G Knepley #undef __FUNCT__
302888ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
302988ed4aceSMatthew G Knepley /*@C
303088ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
303188ed4aceSMatthew G Knepley   describing the data layout.
303288ed4aceSMatthew G Knepley 
303388ed4aceSMatthew G Knepley   Input Parameters:
303488ed4aceSMatthew G Knepley + dm - The DM
303588ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
303688ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
303788ed4aceSMatthew G Knepley 
303888ed4aceSMatthew G Knepley   Level: intermediate
303988ed4aceSMatthew G Knepley 
304088ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
304188ed4aceSMatthew G Knepley @*/
304288ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
304388ed4aceSMatthew G Knepley {
304482f516ccSBarry Smith   MPI_Comm       comm;
304588ed4aceSMatthew G Knepley   PetscLayout    layout;
304688ed4aceSMatthew G Knepley   const PetscInt *ranges;
304788ed4aceSMatthew G Knepley   PetscInt       *local;
304888ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3049ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
305088ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
305188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
305288ed4aceSMatthew G Knepley 
305388ed4aceSMatthew G Knepley   PetscFunctionBegin;
305482f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
305588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
305688ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
305788ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
305888ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
305988ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
306088ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
306188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
306288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
306388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
306488ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3065ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
30666636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
306788ed4aceSMatthew G Knepley 
30686636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
30696636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3070235fbf56SMatthew G. Knepley     if (gcdof > (gdof < 0 ? -(gdof+1) : gdof)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d has %d constraints > %d dof", p, gcdof, (gdof < 0 ? -(gdof+1) : gdof));
30716636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
307288ed4aceSMatthew G Knepley   }
3073785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3074785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
307588ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
30761f588964SMatthew G Knepley     const PetscInt *cind;
30776636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
307888ed4aceSMatthew G Knepley 
307988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
308088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
308188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
308288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
308388ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
30846636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
308588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
30866636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
30876636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
30886636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3089057b4bcdSMatthew G Knepley       if (gsize != dof) SETERRQ4(comm, PETSC_ERR_ARG_WRONG, "Global dof %d for point %d is neither the constrained size %d, nor the unconstrained %d", gsize, p, dof-cdof, dof);
30906636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
30916636e97aSMatthew G Knepley     }
309288ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
309388ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
309488ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
309588ed4aceSMatthew G Knepley     }
309688ed4aceSMatthew G Knepley     if (gdof < 0) {
30976636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
309888ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
309988ed4aceSMatthew G Knepley 
310005376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
310131d3f06eSJed Brown         if (r < 0) r = -(r+2);
310205376888SMatthew G. Knepley         if ((r < 0) || (r >= size)) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d mapped to invalid process %d (%d, %d)", p, r, gdof, goff);
310388ed4aceSMatthew G Knepley         remote[l].rank  = r;
310488ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
310588ed4aceSMatthew G Knepley       }
310688ed4aceSMatthew G Knepley     } else {
31076636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
310888ed4aceSMatthew G Knepley         remote[l].rank  = rank;
310988ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
311088ed4aceSMatthew G Knepley       }
311188ed4aceSMatthew G Knepley     }
311288ed4aceSMatthew G Knepley   }
31136636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
311488ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
311588ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
311688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
311788ed4aceSMatthew G Knepley }
3118af122d2aSMatthew G Knepley 
3119af122d2aSMatthew G Knepley #undef __FUNCT__
3120b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3121b21d0597SMatthew G Knepley /*@
3122b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3123b21d0597SMatthew G Knepley 
3124b21d0597SMatthew G Knepley   Input Parameter:
3125b21d0597SMatthew G Knepley . dm - The DM
3126b21d0597SMatthew G Knepley 
3127b21d0597SMatthew G Knepley   Output Parameter:
3128b21d0597SMatthew G Knepley . sf - The PetscSF
3129b21d0597SMatthew G Knepley 
3130b21d0597SMatthew G Knepley   Level: intermediate
3131b21d0597SMatthew G Knepley 
3132b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3133b21d0597SMatthew G Knepley 
3134057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3135b21d0597SMatthew G Knepley @*/
31360adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
31370adebc6cSBarry Smith {
3138b21d0597SMatthew G Knepley   PetscFunctionBegin;
3139b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3140b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3141b21d0597SMatthew G Knepley   *sf = dm->sf;
3142b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3143b21d0597SMatthew G Knepley }
3144b21d0597SMatthew G Knepley 
3145b21d0597SMatthew G Knepley #undef __FUNCT__
3146057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3147057b4bcdSMatthew G Knepley /*@
3148057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3149057b4bcdSMatthew G Knepley 
3150057b4bcdSMatthew G Knepley   Input Parameters:
3151057b4bcdSMatthew G Knepley + dm - The DM
3152057b4bcdSMatthew G Knepley - sf - The PetscSF
3153057b4bcdSMatthew G Knepley 
3154057b4bcdSMatthew G Knepley   Level: intermediate
3155057b4bcdSMatthew G Knepley 
3156057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3157057b4bcdSMatthew G Knepley @*/
31580adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
31590adebc6cSBarry Smith {
3160057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3161057b4bcdSMatthew G Knepley 
3162057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3163057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3164057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3165057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3166057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3167057b4bcdSMatthew G Knepley   dm->sf = sf;
3168057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3169057b4bcdSMatthew G Knepley }
3170057b4bcdSMatthew G Knepley 
3171057b4bcdSMatthew G Knepley #undef __FUNCT__
31720f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetProblem"
31730f21e855SMatthew G. Knepley PetscErrorCode DMGetProblem(DM dm, PetscProblem *prob)
3174af122d2aSMatthew G Knepley {
3175af122d2aSMatthew G Knepley   PetscFunctionBegin;
3176af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31770f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
31780f21e855SMatthew G. Knepley   *prob = dm->prob;
31790f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
31800f21e855SMatthew G. Knepley }
31810f21e855SMatthew G. Knepley 
31820f21e855SMatthew G. Knepley #undef __FUNCT__
31830f21e855SMatthew G. Knepley #define __FUNCT__ "DMSetProblem"
31840f21e855SMatthew G. Knepley PetscErrorCode DMSetProblem(DM dm, PetscProblem prob)
31850f21e855SMatthew G. Knepley {
31860f21e855SMatthew G. Knepley   PetscErrorCode ierr;
31870f21e855SMatthew G. Knepley 
31880f21e855SMatthew G. Knepley   PetscFunctionBegin;
31890f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31900f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCPROBLEM_CLASSID, 2);
31910f21e855SMatthew G. Knepley   ierr = PetscProblemDestroy(&dm->prob);CHKERRQ(ierr);
31920f21e855SMatthew G. Knepley   dm->prob = prob;
31930f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
31940f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
31950f21e855SMatthew G. Knepley }
31960f21e855SMatthew G. Knepley 
31970f21e855SMatthew G. Knepley #undef __FUNCT__
31980f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
31990f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
32000f21e855SMatthew G. Knepley {
32010f21e855SMatthew G. Knepley   PetscErrorCode ierr;
32020f21e855SMatthew G. Knepley 
32030f21e855SMatthew G. Knepley   PetscFunctionBegin;
32040f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32050f21e855SMatthew G. Knepley   ierr = PetscProblemGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3206af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3207af122d2aSMatthew G Knepley }
3208af122d2aSMatthew G Knepley 
3209af122d2aSMatthew G Knepley #undef __FUNCT__
3210af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3211af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3212af122d2aSMatthew G Knepley {
32130f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3214af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3215af122d2aSMatthew G Knepley 
3216af122d2aSMatthew G Knepley   PetscFunctionBegin;
3217af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32180f21e855SMatthew G. Knepley   ierr = PetscProblemGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
32190f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
32200f21e855SMatthew G. Knepley     PetscContainer obj;
32210f21e855SMatthew G. Knepley 
32220f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
32230f21e855SMatthew G. Knepley     ierr = PetscProblemSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
32240f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3225af122d2aSMatthew G Knepley   }
3226af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3227af122d2aSMatthew G Knepley }
3228af122d2aSMatthew G Knepley 
3229af122d2aSMatthew G Knepley #undef __FUNCT__
3230af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3231*c1929be8SMatthew G. Knepley /*@
3232*c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3233*c1929be8SMatthew G. Knepley 
3234*c1929be8SMatthew G. Knepley   Not collective
3235*c1929be8SMatthew G. Knepley 
3236*c1929be8SMatthew G. Knepley   Input Parameters:
3237*c1929be8SMatthew G. Knepley + dm - The DM
3238*c1929be8SMatthew G. Knepley - f  - The field number
3239*c1929be8SMatthew G. Knepley 
3240*c1929be8SMatthew G. Knepley   Output Parameter:
3241*c1929be8SMatthew G. Knepley . field - The discretization object
3242*c1929be8SMatthew G. Knepley 
3243*c1929be8SMatthew G. Knepley   Level: developer
3244*c1929be8SMatthew G. Knepley 
3245*c1929be8SMatthew G. Knepley .seealso: DMSetField()
3246*c1929be8SMatthew G. Knepley @*/
3247af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3248af122d2aSMatthew G Knepley {
32490f21e855SMatthew G. Knepley   PetscErrorCode ierr;
32500f21e855SMatthew G. Knepley 
3251af122d2aSMatthew G Knepley   PetscFunctionBegin;
3252af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32530f21e855SMatthew G. Knepley   ierr = PetscProblemGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3254decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3255decb47aaSMatthew G. Knepley }
3256decb47aaSMatthew G. Knepley 
3257decb47aaSMatthew G. Knepley #undef __FUNCT__
3258decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3259*c1929be8SMatthew G. Knepley /*@
3260*c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3261*c1929be8SMatthew G. Knepley 
3262*c1929be8SMatthew G. Knepley   Logically collective on DM
3263*c1929be8SMatthew G. Knepley 
3264*c1929be8SMatthew G. Knepley   Input Parameters:
3265*c1929be8SMatthew G. Knepley + dm - The DM
3266*c1929be8SMatthew G. Knepley . f  - The field number
3267*c1929be8SMatthew G. Knepley - field - The discretization object
3268*c1929be8SMatthew G. Knepley 
3269*c1929be8SMatthew G. Knepley   Level: developer
3270*c1929be8SMatthew G. Knepley 
3271*c1929be8SMatthew G. Knepley .seealso: DMGetField()
3272*c1929be8SMatthew G. Knepley @*/
3273decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3274decb47aaSMatthew G. Knepley {
3275decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3276decb47aaSMatthew G. Knepley 
3277decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3278decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32790f21e855SMatthew G. Knepley   ierr = PetscProblemSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3280af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3281af122d2aSMatthew G Knepley }
32826636e97aSMatthew G Knepley 
32836636e97aSMatthew G Knepley #undef __FUNCT__
3284b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3285b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3286b64e0483SPeter Brune {
3287b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3288b64e0483SPeter Brune   PetscErrorCode ierr;
3289b64e0483SPeter Brune   Vec coords,ccoords;
3290b64e0483SPeter Brune   VecScatter scat;
3291b64e0483SPeter Brune   PetscFunctionBegin;
3292b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3293b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3294b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3295b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3296b64e0483SPeter Brune   if (coords && !ccoords) {
3297b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
3298b64e0483SPeter Brune     ierr = DMCreateInjection(dmc_coord,dm_coord,&scat);CHKERRQ(ierr);
3299b64e0483SPeter Brune     ierr = VecScatterBegin(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3300b64e0483SPeter Brune     ierr = VecScatterEnd(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
3301b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3302b64e0483SPeter Brune     ierr = VecScatterDestroy(&scat);CHKERRQ(ierr);
3303b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3304b64e0483SPeter Brune   }
3305b64e0483SPeter Brune   PetscFunctionReturn(0);
3306b64e0483SPeter Brune }
3307b64e0483SPeter Brune 
3308b64e0483SPeter Brune #undef __FUNCT__
330903dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
331003dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
331103dadc2fSPeter Brune {
331203dadc2fSPeter Brune   DM dm_coord,subdm_coord;
331303dadc2fSPeter Brune   PetscErrorCode ierr;
331403dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
331503dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
331603dadc2fSPeter Brune   PetscFunctionBegin;
331703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
331803dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
331903dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
332003dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
332103dadc2fSPeter Brune   if (coords && !ccoords) {
332203dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
332303dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
332403dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
332503dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
332603dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
332703dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
332803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
332903dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
333003dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
333103dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
333203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
333303dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
333403dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
333503dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
333603dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
333703dadc2fSPeter Brune   }
333803dadc2fSPeter Brune   PetscFunctionReturn(0);
333903dadc2fSPeter Brune }
334003dadc2fSPeter Brune 
334103dadc2fSPeter Brune #undef __FUNCT__
33426636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
33436636e97aSMatthew G Knepley /*@
33446636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
33456636e97aSMatthew G Knepley 
33466636e97aSMatthew G Knepley   Collective on DM
33476636e97aSMatthew G Knepley 
33486636e97aSMatthew G Knepley   Input Parameters:
33496636e97aSMatthew G Knepley + dm - the DM
33506636e97aSMatthew G Knepley - c - coordinate vector
33516636e97aSMatthew G Knepley 
33526636e97aSMatthew G Knepley   Note:
33536636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
33546636e97aSMatthew G Knepley 
33556636e97aSMatthew G Knepley   Level: intermediate
33566636e97aSMatthew G Knepley 
33576636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
33586636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
33596636e97aSMatthew G Knepley @*/
33606636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
33616636e97aSMatthew G Knepley {
33626636e97aSMatthew G Knepley   PetscErrorCode ierr;
33636636e97aSMatthew G Knepley 
33646636e97aSMatthew G Knepley   PetscFunctionBegin;
33656636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33666636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
33676636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
33686636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
33696636e97aSMatthew G Knepley   dm->coordinates = c;
33706636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3371b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
337203dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
33736636e97aSMatthew G Knepley   PetscFunctionReturn(0);
33746636e97aSMatthew G Knepley }
33756636e97aSMatthew G Knepley 
33766636e97aSMatthew G Knepley #undef __FUNCT__
33776636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
33786636e97aSMatthew G Knepley /*@
33796636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
33806636e97aSMatthew G Knepley 
33816636e97aSMatthew G Knepley   Collective on DM
33826636e97aSMatthew G Knepley 
33836636e97aSMatthew G Knepley    Input Parameters:
33846636e97aSMatthew G Knepley +  dm - the DM
33856636e97aSMatthew G Knepley -  c - coordinate vector
33866636e97aSMatthew G Knepley 
33876636e97aSMatthew G Knepley   Note:
33886636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
33896636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
33906636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
33916636e97aSMatthew G Knepley 
33926636e97aSMatthew G Knepley   Level: intermediate
33936636e97aSMatthew G Knepley 
33946636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
33956636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
33966636e97aSMatthew G Knepley @*/
33976636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
33986636e97aSMatthew G Knepley {
33996636e97aSMatthew G Knepley   PetscErrorCode ierr;
34006636e97aSMatthew G Knepley 
34016636e97aSMatthew G Knepley   PetscFunctionBegin;
34026636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34036636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
34046636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
34056636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
34068865f1eaSKarl Rupp 
34076636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
34088865f1eaSKarl Rupp 
34096636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
34106636e97aSMatthew G Knepley   PetscFunctionReturn(0);
34116636e97aSMatthew G Knepley }
34126636e97aSMatthew G Knepley 
34136636e97aSMatthew G Knepley #undef __FUNCT__
34146636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
34156636e97aSMatthew G Knepley /*@
34166636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
34176636e97aSMatthew G Knepley 
34186636e97aSMatthew G Knepley   Not Collective
34196636e97aSMatthew G Knepley 
34206636e97aSMatthew G Knepley   Input Parameter:
34216636e97aSMatthew G Knepley . dm - the DM
34226636e97aSMatthew G Knepley 
34236636e97aSMatthew G Knepley   Output Parameter:
34246636e97aSMatthew G Knepley . c - global coordinate vector
34256636e97aSMatthew G Knepley 
34266636e97aSMatthew G Knepley   Note:
34276636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
34286636e97aSMatthew G Knepley 
34296636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
34306636e97aSMatthew G Knepley 
34316636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
34326636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
34336636e97aSMatthew G Knepley 
34346636e97aSMatthew G Knepley   Level: intermediate
34356636e97aSMatthew G Knepley 
34366636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
34376636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
34386636e97aSMatthew G Knepley @*/
34396636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
34406636e97aSMatthew G Knepley {
34416636e97aSMatthew G Knepley   PetscErrorCode ierr;
34426636e97aSMatthew G Knepley 
34436636e97aSMatthew G Knepley   PetscFunctionBegin;
34446636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34456636e97aSMatthew G Knepley   PetscValidPointer(c,2);
34461f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
34470298fd71SBarry Smith     DM cdm = NULL;
34486636e97aSMatthew G Knepley 
34496636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
34506636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
34516636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
34526636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
34536636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
34546636e97aSMatthew G Knepley   }
34556636e97aSMatthew G Knepley   *c = dm->coordinates;
34566636e97aSMatthew G Knepley   PetscFunctionReturn(0);
34576636e97aSMatthew G Knepley }
34586636e97aSMatthew G Knepley 
34596636e97aSMatthew G Knepley #undef __FUNCT__
34606636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
34616636e97aSMatthew G Knepley /*@
34626636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
34636636e97aSMatthew G Knepley 
34646636e97aSMatthew G Knepley   Collective on DM
34656636e97aSMatthew G Knepley 
34666636e97aSMatthew G Knepley   Input Parameter:
34676636e97aSMatthew G Knepley . dm - the DM
34686636e97aSMatthew G Knepley 
34696636e97aSMatthew G Knepley   Output Parameter:
34706636e97aSMatthew G Knepley . c - coordinate vector
34716636e97aSMatthew G Knepley 
34726636e97aSMatthew G Knepley   Note:
34736636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
34746636e97aSMatthew G Knepley 
34756636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
34766636e97aSMatthew G Knepley 
34776636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
34786636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
34796636e97aSMatthew G Knepley 
34806636e97aSMatthew G Knepley   Level: intermediate
34816636e97aSMatthew G Knepley 
34826636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
34836636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
34846636e97aSMatthew G Knepley @*/
34856636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
34866636e97aSMatthew G Knepley {
34876636e97aSMatthew G Knepley   PetscErrorCode ierr;
34886636e97aSMatthew G Knepley 
34896636e97aSMatthew G Knepley   PetscFunctionBegin;
34906636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34916636e97aSMatthew G Knepley   PetscValidPointer(c,2);
34921f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
34930298fd71SBarry Smith     DM cdm = NULL;
34946636e97aSMatthew G Knepley 
34956636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
34966636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
34976636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
34986636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
34996636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
35006636e97aSMatthew G Knepley   }
35016636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
35026636e97aSMatthew G Knepley   PetscFunctionReturn(0);
35036636e97aSMatthew G Knepley }
35046636e97aSMatthew G Knepley 
35056636e97aSMatthew G Knepley #undef __FUNCT__
35066636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
35076636e97aSMatthew G Knepley /*@
35081cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
35096636e97aSMatthew G Knepley 
35106636e97aSMatthew G Knepley   Collective on DM
35116636e97aSMatthew G Knepley 
35126636e97aSMatthew G Knepley   Input Parameter:
35136636e97aSMatthew G Knepley . dm - the DM
35146636e97aSMatthew G Knepley 
35156636e97aSMatthew G Knepley   Output Parameter:
35166636e97aSMatthew G Knepley . cdm - coordinate DM
35176636e97aSMatthew G Knepley 
35186636e97aSMatthew G Knepley   Level: intermediate
35196636e97aSMatthew G Knepley 
35206636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35211cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
35226636e97aSMatthew G Knepley @*/
35236636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
35246636e97aSMatthew G Knepley {
35256636e97aSMatthew G Knepley   PetscErrorCode ierr;
35266636e97aSMatthew G Knepley 
35276636e97aSMatthew G Knepley   PetscFunctionBegin;
35286636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35296636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
35306636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
353182f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
35326636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
35336636e97aSMatthew G Knepley   }
35346636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
35356636e97aSMatthew G Knepley   PetscFunctionReturn(0);
35366636e97aSMatthew G Knepley }
3537e87bb0d3SMatthew G Knepley 
3538e87bb0d3SMatthew G Knepley #undef __FUNCT__
35391cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
35401cfe2091SMatthew G. Knepley /*@
35411cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
35421cfe2091SMatthew G. Knepley 
35431cfe2091SMatthew G. Knepley   Logically Collective on DM
35441cfe2091SMatthew G. Knepley 
35451cfe2091SMatthew G. Knepley   Input Parameters:
35461cfe2091SMatthew G. Knepley + dm - the DM
35471cfe2091SMatthew G. Knepley - cdm - coordinate DM
35481cfe2091SMatthew G. Knepley 
35491cfe2091SMatthew G. Knepley   Level: intermediate
35501cfe2091SMatthew G. Knepley 
35511cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
35521cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
35531cfe2091SMatthew G. Knepley @*/
35541cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
35551cfe2091SMatthew G. Knepley {
35561cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
35571cfe2091SMatthew G. Knepley 
35581cfe2091SMatthew G. Knepley   PetscFunctionBegin;
35591cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35601cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
35611cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
35621cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
35631cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
35641cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
35651cfe2091SMatthew G. Knepley }
35661cfe2091SMatthew G. Knepley 
35671cfe2091SMatthew G. Knepley #undef __FUNCT__
3568e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
3569e8abe2deSMatthew G. Knepley /*@
3570e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
3571e8abe2deSMatthew G. Knepley 
3572e8abe2deSMatthew G. Knepley   Not Collective
3573e8abe2deSMatthew G. Knepley 
3574e8abe2deSMatthew G. Knepley   Input Parameter:
3575e8abe2deSMatthew G. Knepley . dm - The DM object
3576e8abe2deSMatthew G. Knepley 
3577e8abe2deSMatthew G. Knepley   Output Parameter:
3578e8abe2deSMatthew G. Knepley . section - The PetscSection object
3579e8abe2deSMatthew G. Knepley 
3580e8abe2deSMatthew G. Knepley   Level: intermediate
3581e8abe2deSMatthew G. Knepley 
3582e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
3583e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
3584e8abe2deSMatthew G. Knepley @*/
3585e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
3586e8abe2deSMatthew G. Knepley {
3587e8abe2deSMatthew G. Knepley   DM             cdm;
3588e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
3589e8abe2deSMatthew G. Knepley 
3590e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
3591e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3592e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
3593e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3594e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
3595e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
3596e8abe2deSMatthew G. Knepley }
3597e8abe2deSMatthew G. Knepley 
3598e8abe2deSMatthew G. Knepley #undef __FUNCT__
3599e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
3600e8abe2deSMatthew G. Knepley /*@
3601e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
3602e8abe2deSMatthew G. Knepley 
3603e8abe2deSMatthew G. Knepley   Not Collective
3604e8abe2deSMatthew G. Knepley 
3605e8abe2deSMatthew G. Knepley   Input Parameters:
3606e8abe2deSMatthew G. Knepley + dm      - The DM object
3607e8abe2deSMatthew G. Knepley - section - The PetscSection object
3608e8abe2deSMatthew G. Knepley 
3609e8abe2deSMatthew G. Knepley   Level: intermediate
3610e8abe2deSMatthew G. Knepley 
3611e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
3612e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
3613e8abe2deSMatthew G. Knepley @*/
3614e8abe2deSMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscSection section)
3615e8abe2deSMatthew G. Knepley {
3616e8abe2deSMatthew G. Knepley   DM             cdm;
3617e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
3618e8abe2deSMatthew G. Knepley 
3619e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
3620e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3621e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3622e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3623e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
3624e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
3625e8abe2deSMatthew G. Knepley }
3626e8abe2deSMatthew G. Knepley 
3627e8abe2deSMatthew G. Knepley #undef __FUNCT__
3628c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
3629c6b900c6SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L)
3630c6b900c6SMatthew G. Knepley {
3631c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
3632c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3633c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
3634c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
3635c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
3636c6b900c6SMatthew G. Knepley }
3637c6b900c6SMatthew G. Knepley 
3638c6b900c6SMatthew G. Knepley #undef __FUNCT__
3639c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
3640c6b900c6SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[])
3641c6b900c6SMatthew G. Knepley {
3642c6b900c6SMatthew G. Knepley   Vec            coordinates;
3643c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
3644c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
3645c6b900c6SMatthew G. Knepley 
3646c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
3647c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3648c6b900c6SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
3649c6b900c6SMatthew G. Knepley   ierr = VecGetBlockSize(coordinates, &dim);CHKERRQ(ierr);
3650c6b900c6SMatthew G. Knepley   ierr = PetscMalloc1(dim,&dm->L);CHKERRQ(ierr);
3651c6b900c6SMatthew G. Knepley   ierr = PetscMalloc1(dim,&dm->maxCell);CHKERRQ(ierr);
3652c6b900c6SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d];}
3653c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
3654c6b900c6SMatthew G. Knepley }
3655c6b900c6SMatthew G. Knepley 
3656c6b900c6SMatthew G. Knepley #undef __FUNCT__
3657e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
3658e87bb0d3SMatthew G Knepley /*@
3659e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
3660e87bb0d3SMatthew G Knepley 
3661e87bb0d3SMatthew G Knepley   Not collective
3662e87bb0d3SMatthew G Knepley 
3663e87bb0d3SMatthew G Knepley   Input Parameters:
3664e87bb0d3SMatthew G Knepley + dm - The DM
3665e87bb0d3SMatthew G Knepley - v - The Vec of points
3666e87bb0d3SMatthew G Knepley 
366761e3bb9bSMatthew G Knepley   Output Parameter:
3668e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
3669e87bb0d3SMatthew G Knepley 
3670e87bb0d3SMatthew G Knepley   Level: developer
367161e3bb9bSMatthew G Knepley 
367261e3bb9bSMatthew G Knepley .keywords: point location, mesh
367361e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
367461e3bb9bSMatthew G Knepley @*/
3675e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
3676e87bb0d3SMatthew G Knepley {
3677735aa83eSMatthew G Knepley   PetscErrorCode ierr;
3678735aa83eSMatthew G Knepley 
3679e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
3680e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3681e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
3682e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
3683735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
3684735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
368582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
3686e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
3687e87bb0d3SMatthew G Knepley }
368814f150ffSMatthew G. Knepley 
368914f150ffSMatthew G. Knepley #undef __FUNCT__
369014f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
3691f4d763aaSMatthew G. Knepley /*@
3692f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
3693f4d763aaSMatthew G. Knepley 
3694f4d763aaSMatthew G. Knepley   Input Parameter:
3695f4d763aaSMatthew G. Knepley . dm - The original DM
3696f4d763aaSMatthew G. Knepley 
3697f4d763aaSMatthew G. Knepley   Output Parameter:
3698f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
3699f4d763aaSMatthew G. Knepley 
3700f4d763aaSMatthew G. Knepley   Level: intermediate
3701f4d763aaSMatthew G. Knepley 
3702f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
3703f4d763aaSMatthew G. Knepley @*/
370414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
370514f150ffSMatthew G. Knepley {
3706c26acbdeSMatthew G. Knepley   PetscSection   section;
3707c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
370814f150ffSMatthew G. Knepley   PetscErrorCode ierr;
370914f150ffSMatthew G. Knepley 
371014f150ffSMatthew G. Knepley   PetscFunctionBegin;
371114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
371214f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
3713c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
3714c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
3715c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
3716c26acbdeSMatthew G. Knepley     *odm = dm;
3717c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
3718c26acbdeSMatthew G. Knepley   }
371914f150ffSMatthew G. Knepley   if (!dm->dmBC) {
3720c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
372114f150ffSMatthew G. Knepley     PetscSF      sf;
372214f150ffSMatthew G. Knepley 
372314f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
372414f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
372514f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
372614f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
372714f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
372814f150ffSMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, &gsection);CHKERRQ(ierr);
372914f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
373014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
373114f150ffSMatthew G. Knepley   }
373214f150ffSMatthew G. Knepley   *odm = dm->dmBC;
373314f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
373414f150ffSMatthew G. Knepley }
3735f4d763aaSMatthew G. Knepley 
3736f4d763aaSMatthew G. Knepley #undef __FUNCT__
3737f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
3738f4d763aaSMatthew G. Knepley /*@
3739f4d763aaSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number for output
3740f4d763aaSMatthew G. Knepley 
3741f4d763aaSMatthew G. Knepley   Input Parameter:
3742f4d763aaSMatthew G. Knepley . dm - The original DM
3743f4d763aaSMatthew G. Knepley 
3744f4d763aaSMatthew G. Knepley   Output Parameter:
3745f4d763aaSMatthew G. Knepley . num - The output sequence number
3746f4d763aaSMatthew G. Knepley 
3747f4d763aaSMatthew G. Knepley   Level: intermediate
3748f4d763aaSMatthew G. Knepley 
3749f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
3750f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
3751f4d763aaSMatthew G. Knepley 
3752f4d763aaSMatthew G. Knepley .seealso: VecView()
3753f4d763aaSMatthew G. Knepley @*/
3754f4d763aaSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num)
3755f4d763aaSMatthew G. Knepley {
3756f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
3757f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3758f4d763aaSMatthew G. Knepley   PetscValidPointer(num,2);
3759f4d763aaSMatthew G. Knepley   *num = dm->outputSequenceNum;
3760f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
3761f4d763aaSMatthew G. Knepley }
3762f4d763aaSMatthew G. Knepley 
3763f4d763aaSMatthew G. Knepley #undef __FUNCT__
3764f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
3765f4d763aaSMatthew G. Knepley /*@
3766f4d763aaSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number for output
3767f4d763aaSMatthew G. Knepley 
3768f4d763aaSMatthew G. Knepley   Input Parameters:
3769f4d763aaSMatthew G. Knepley + dm - The original DM
3770f4d763aaSMatthew G. Knepley - num - The output sequence number
3771f4d763aaSMatthew G. Knepley 
3772f4d763aaSMatthew G. Knepley   Level: intermediate
3773f4d763aaSMatthew G. Knepley 
3774f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
3775f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
3776f4d763aaSMatthew G. Knepley 
3777f4d763aaSMatthew G. Knepley .seealso: VecView()
3778f4d763aaSMatthew G. Knepley @*/
3779f4d763aaSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num)
3780f4d763aaSMatthew G. Knepley {
3781f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
3782f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3783f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
3784f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
3785f4d763aaSMatthew G. Knepley }
3786