xref: /petsc/src/dm/interface/dm.c (revision a546ed68133816a2effa1984479f60f911ec91aa)
1b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
20c312b8eSJed Brown #include <petscsf.h>
347c6ae99SBarry Smith 
4732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
567a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal;
667a56275SMatthew G Knepley 
747c6ae99SBarry Smith #undef __FUNCT__
8ca266f36SBarry Smith #define __FUNCT__ "DMViewFromOptions"
9ca266f36SBarry Smith /*
10c55c6a9aSJed Brown   DMViewFromOptions - Processes command line options to determine if/how a DM is to be viewed.
11ca266f36SBarry Smith 
12c55c6a9aSJed Brown   Collective on Vec
13c55c6a9aSJed Brown 
14c55c6a9aSJed Brown   Input Parameters:
15c55c6a9aSJed Brown + dm   - the DM
16c55c6a9aSJed Brown . prefix - prefix to use for viewing, or NULL to use prefix of 'rnd'
17c55c6a9aSJed Brown - optionname - option to activate viewing
18c55c6a9aSJed Brown 
19c55c6a9aSJed Brown   Level: intermediate
20c55c6a9aSJed Brown 
21c55c6a9aSJed Brown .keywords: DM, view, options, database
22c55c6a9aSJed Brown .seealso: VecViewFromOptions(), MatViewFromOptions()
23ca266f36SBarry Smith */
24146574abSBarry Smith PetscErrorCode  DMViewFromOptions(DM dm,const char prefix[],const char optionname[])
25ca266f36SBarry Smith {
26ca266f36SBarry Smith   PetscErrorCode    ierr;
27ca266f36SBarry Smith   PetscBool         flg;
28ca266f36SBarry Smith   PetscViewer       viewer;
29cffb1e40SBarry Smith   PetscViewerFormat format;
30ca266f36SBarry Smith 
31ca266f36SBarry Smith   PetscFunctionBegin;
32146574abSBarry Smith   if (prefix) {
33146574abSBarry Smith     ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm),prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr);
34146574abSBarry Smith   } else {
35ce94432eSBarry Smith     ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm),((PetscObject)dm)->prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr);
36146574abSBarry Smith   }
37ca266f36SBarry Smith   if (flg) {
38cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
39ca266f36SBarry Smith     ierr = DMView(dm,viewer);CHKERRQ(ierr);
40cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
41cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
42ca266f36SBarry Smith   }
43ca266f36SBarry Smith   PetscFunctionReturn(0);
44ca266f36SBarry Smith }
45ca266f36SBarry Smith 
46ca266f36SBarry Smith 
47ca266f36SBarry Smith #undef __FUNCT__
48a4121054SBarry Smith #define __FUNCT__ "DMCreate"
49a4121054SBarry Smith /*@
50de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
51a4121054SBarry Smith 
52a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
53a4121054SBarry Smith    error when you try to use the vector.
54a4121054SBarry Smith 
55a4121054SBarry Smith   Collective on MPI_Comm
56a4121054SBarry Smith 
57a4121054SBarry Smith   Input Parameter:
58a4121054SBarry Smith . comm - The communicator for the DM object
59a4121054SBarry Smith 
60a4121054SBarry Smith   Output Parameter:
61a4121054SBarry Smith . dm - The DM object
62a4121054SBarry Smith 
63a4121054SBarry Smith   Level: beginner
64a4121054SBarry Smith 
65a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
66a4121054SBarry Smith @*/
677087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
68a4121054SBarry Smith {
69a4121054SBarry Smith   DM             v;
70a4121054SBarry Smith   PetscErrorCode ierr;
71a4121054SBarry Smith 
72a4121054SBarry Smith   PetscFunctionBegin;
731411c6eeSJed Brown   PetscValidPointer(dm,2);
740298fd71SBarry Smith   *dm = NULL;
75519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
76607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
77607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
78607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
79a4121054SBarry Smith #endif
80a4121054SBarry Smith 
8167c2884eSBarry Smith   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
82a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
831411c6eeSJed Brown 
84e7c4fc90SDmitry Karpeev 
850298fd71SBarry Smith   v->ltogmap              = NULL;
860298fd71SBarry Smith   v->ltogmapb             = NULL;
871411c6eeSJed Brown   v->bs                   = 1;
88171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
8988ed4aceSMatthew G Knepley   ierr                    = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
9088ed4aceSMatthew G Knepley   ierr                    = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
910298fd71SBarry Smith   v->defaultSection       = NULL;
920298fd71SBarry Smith   v->defaultGlobalSection = NULL;
93435a35e8SMatthew G Knepley   {
94435a35e8SMatthew G Knepley     PetscInt i;
95435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
960298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
97435a35e8SMatthew G Knepley     }
98435a35e8SMatthew G Knepley   }
99af122d2aSMatthew G Knepley   v->numFields = 0;
1000298fd71SBarry Smith   v->fields    = NULL;
1011411c6eeSJed Brown 
1021411c6eeSJed Brown   *dm = v;
103a4121054SBarry Smith   PetscFunctionReturn(0);
104a4121054SBarry Smith }
105a4121054SBarry Smith 
106a4121054SBarry Smith #undef __FUNCT__
1079a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1089a42bb27SBarry Smith /*@C
109564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1109a42bb27SBarry Smith 
111aa219208SBarry Smith    Logically Collective on DMDA
1129a42bb27SBarry Smith 
1139a42bb27SBarry Smith    Input Parameter:
1149a42bb27SBarry Smith +  da - initial distributed array
1158154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1169a42bb27SBarry Smith 
1179a42bb27SBarry Smith    Options Database:
118dd85299cSBarry Smith .   -dm_vec_type ctype
1199a42bb27SBarry Smith 
1209a42bb27SBarry Smith    Level: intermediate
1219a42bb27SBarry Smith 
122aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
1239a42bb27SBarry Smith @*/
12419fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1259a42bb27SBarry Smith {
1269a42bb27SBarry Smith   PetscErrorCode ierr;
1279a42bb27SBarry Smith 
1289a42bb27SBarry Smith   PetscFunctionBegin;
1299a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1309a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
13119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1329a42bb27SBarry Smith   PetscFunctionReturn(0);
1339a42bb27SBarry Smith }
1349a42bb27SBarry Smith 
1359a42bb27SBarry Smith #undef __FUNCT__
1365f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1375f1ad066SMatthew G Knepley /*@
13834f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1395f1ad066SMatthew G Knepley 
1405f1ad066SMatthew G Knepley   Not collective
1415f1ad066SMatthew G Knepley 
1425f1ad066SMatthew G Knepley   Input Parameter:
1435f1ad066SMatthew G Knepley . v - The Vec
1445f1ad066SMatthew G Knepley 
1455f1ad066SMatthew G Knepley   Output Parameter:
1465f1ad066SMatthew G Knepley . dm - The DM
1475f1ad066SMatthew G Knepley 
1485f1ad066SMatthew G Knepley   Level: intermediate
1495f1ad066SMatthew G Knepley 
1505f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1515f1ad066SMatthew G Knepley @*/
1525f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
1535f1ad066SMatthew G Knepley {
1545f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1555f1ad066SMatthew G Knepley 
1565f1ad066SMatthew G Knepley   PetscFunctionBegin;
1575f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1585f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
1595f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
1605f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
1615f1ad066SMatthew G Knepley }
1625f1ad066SMatthew G Knepley 
1635f1ad066SMatthew G Knepley #undef __FUNCT__
1645f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
1655f1ad066SMatthew G Knepley /*@
1665f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
1675f1ad066SMatthew G Knepley 
1685f1ad066SMatthew G Knepley   Not collective
1695f1ad066SMatthew G Knepley 
1705f1ad066SMatthew G Knepley   Input Parameters:
1715f1ad066SMatthew G Knepley + v - The Vec
1725f1ad066SMatthew G Knepley - dm - The DM
1735f1ad066SMatthew G Knepley 
1745f1ad066SMatthew G Knepley   Level: intermediate
1755f1ad066SMatthew G Knepley 
1765f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1775f1ad066SMatthew G Knepley @*/
1785f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
1795f1ad066SMatthew G Knepley {
1805f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1815f1ad066SMatthew G Knepley 
1825f1ad066SMatthew G Knepley   PetscFunctionBegin;
1835f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1845f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
1855f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
1865f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
1875f1ad066SMatthew G Knepley }
1885f1ad066SMatthew G Knepley 
1895f1ad066SMatthew G Knepley #undef __FUNCT__
190521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
191521d9a4cSLisandro Dalcin /*@C
192521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
193521d9a4cSLisandro Dalcin 
194521d9a4cSLisandro Dalcin    Logically Collective on DM
195521d9a4cSLisandro Dalcin 
196521d9a4cSLisandro Dalcin    Input Parameter:
197521d9a4cSLisandro Dalcin +  dm - the DM context
198521d9a4cSLisandro Dalcin .  ctype - the matrix type
199521d9a4cSLisandro Dalcin 
200521d9a4cSLisandro Dalcin    Options Database:
201521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
202521d9a4cSLisandro Dalcin 
203521d9a4cSLisandro Dalcin    Level: intermediate
204521d9a4cSLisandro Dalcin 
205521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType
206521d9a4cSLisandro Dalcin @*/
20719fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
208521d9a4cSLisandro Dalcin {
209521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
21088f0584fSBarry Smith 
211521d9a4cSLisandro Dalcin   PetscFunctionBegin;
212521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
213521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
21419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
215521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
216521d9a4cSLisandro Dalcin }
217521d9a4cSLisandro Dalcin 
218521d9a4cSLisandro Dalcin #undef __FUNCT__
219c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
220c688c046SMatthew G Knepley /*@
22134f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
222c688c046SMatthew G Knepley 
223c688c046SMatthew G Knepley   Not collective
224c688c046SMatthew G Knepley 
225c688c046SMatthew G Knepley   Input Parameter:
226c688c046SMatthew G Knepley . A - The Mat
227c688c046SMatthew G Knepley 
228c688c046SMatthew G Knepley   Output Parameter:
229c688c046SMatthew G Knepley . dm - The DM
230c688c046SMatthew G Knepley 
231c688c046SMatthew G Knepley   Level: intermediate
232c688c046SMatthew G Knepley 
233c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
234c688c046SMatthew G Knepley @*/
235c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
236c688c046SMatthew G Knepley {
237c688c046SMatthew G Knepley   PetscErrorCode ierr;
238c688c046SMatthew G Knepley 
239c688c046SMatthew G Knepley   PetscFunctionBegin;
240c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
241c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
242c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
243c688c046SMatthew G Knepley   PetscFunctionReturn(0);
244c688c046SMatthew G Knepley }
245c688c046SMatthew G Knepley 
246c688c046SMatthew G Knepley #undef __FUNCT__
247c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
248c688c046SMatthew G Knepley /*@
249c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
250c688c046SMatthew G Knepley 
251c688c046SMatthew G Knepley   Not collective
252c688c046SMatthew G Knepley 
253c688c046SMatthew G Knepley   Input Parameters:
254c688c046SMatthew G Knepley + A - The Mat
255c688c046SMatthew G Knepley - dm - The DM
256c688c046SMatthew G Knepley 
257c688c046SMatthew G Knepley   Level: intermediate
258c688c046SMatthew G Knepley 
259c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
260c688c046SMatthew G Knepley @*/
261c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
262c688c046SMatthew G Knepley {
263c688c046SMatthew G Knepley   PetscErrorCode ierr;
264c688c046SMatthew G Knepley 
265c688c046SMatthew G Knepley   PetscFunctionBegin;
266c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
2678865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
268c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
269c688c046SMatthew G Knepley   PetscFunctionReturn(0);
270c688c046SMatthew G Knepley }
271c688c046SMatthew G Knepley 
272c688c046SMatthew G Knepley #undef __FUNCT__
2739a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
2749a42bb27SBarry Smith /*@C
2759a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
276aa219208SBarry Smith    DMDA options in the database.
2779a42bb27SBarry Smith 
278aa219208SBarry Smith    Logically Collective on DMDA
2799a42bb27SBarry Smith 
2809a42bb27SBarry Smith    Input Parameter:
281aa219208SBarry Smith +  da - the DMDA context
2829a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
2839a42bb27SBarry Smith 
2849a42bb27SBarry Smith    Notes:
2859a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2869a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2879a42bb27SBarry Smith 
2889a42bb27SBarry Smith    Level: advanced
2899a42bb27SBarry Smith 
290aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
2919a42bb27SBarry Smith 
2929a42bb27SBarry Smith .seealso: DMSetFromOptions()
2939a42bb27SBarry Smith @*/
2947087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
2959a42bb27SBarry Smith {
2969a42bb27SBarry Smith   PetscErrorCode ierr;
2979a42bb27SBarry Smith 
2989a42bb27SBarry Smith   PetscFunctionBegin;
2999a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3009a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
3019a42bb27SBarry Smith   PetscFunctionReturn(0);
3029a42bb27SBarry Smith }
3039a42bb27SBarry Smith 
3049a42bb27SBarry Smith #undef __FUNCT__
30547c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
30647c6ae99SBarry Smith /*@
307aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
30847c6ae99SBarry Smith 
30947c6ae99SBarry Smith     Collective on DM
31047c6ae99SBarry Smith 
31147c6ae99SBarry Smith     Input Parameter:
31247c6ae99SBarry Smith .   dm - the DM object to destroy
31347c6ae99SBarry Smith 
31447c6ae99SBarry Smith     Level: developer
31547c6ae99SBarry Smith 
316e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
31747c6ae99SBarry Smith 
31847c6ae99SBarry Smith @*/
319fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
32047c6ae99SBarry Smith {
321af122d2aSMatthew G Knepley   PetscInt       i, cnt = 0, f;
322dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
32347c6ae99SBarry Smith   PetscErrorCode ierr;
32447c6ae99SBarry Smith 
32547c6ae99SBarry Smith   PetscFunctionBegin;
3266bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
3276bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
32887e657c6SBarry Smith 
329*a546ed68SMatthew G. Knepley   /* I think it makes sense to dump all attached things when you are destroyed, which also eliminates circular references */
330*a546ed68SMatthew G. Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
331*a546ed68SMatthew G. Knepley     ierr = PetscObjectCompose((*dm)->fields[f], "pmat", NULL);CHKERRQ(ierr);
332*a546ed68SMatthew G. Knepley     ierr = PetscObjectCompose((*dm)->fields[f], "nullspace", NULL);CHKERRQ(ierr);
333*a546ed68SMatthew G. Knepley     ierr = PetscObjectCompose((*dm)->fields[f], "nearnullspace", NULL);CHKERRQ(ierr);
334*a546ed68SMatthew G. Knepley   }
33587e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
336732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
3378865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
3388865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
339732e2eb9SMatthew G Knepley   }
340dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
34171cd77b2SBarry Smith   if ((*dm)->x) {
342c688c046SMatthew G Knepley     DM obj;
343c688c046SMatthew G Knepley     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
344c688c046SMatthew G Knepley     if (obj == *dm) cnt++;
34571cd77b2SBarry Smith   }
3462348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
3472348bcf4SPeter Brune   if ((*dm)->x) {
3482348bcf4SPeter Brune     DM obj;
3492348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
3502348bcf4SPeter Brune     if (obj == *dm) cnt++;
3512348bcf4SPeter Brune   }
352732e2eb9SMatthew G Knepley 
3536bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
354732e2eb9SMatthew G Knepley   /*
355732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
356732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
357732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
358732e2eb9SMatthew G Knepley   */
3596bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
3606bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
361732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
3626bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
3636bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
364732e2eb9SMatthew G Knepley   }
365dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
366dfe15315SJed Brown     nnext = nlink->next;
367dfe15315SJed Brown     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
368dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
369dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
370dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
371dfe15315SJed Brown   }
3720298fd71SBarry Smith   (*dm)->namedglobal = NULL;
3731a266240SBarry Smith 
3742348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nnext) { /* Destroy the named local vectors */
3752348bcf4SPeter Brune     nnext = nlink->next;
3762348bcf4SPeter 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);
3772348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
3782348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
3792348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
3802348bcf4SPeter Brune   }
3810298fd71SBarry Smith   (*dm)->namedlocal = NULL;
3822348bcf4SPeter Brune 
383b17ce1afSJed Brown   /* Destroy the list of hooks */
384c833c3b5SJed Brown   {
385c833c3b5SJed Brown     DMCoarsenHookLink link,next;
386b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
387b17ce1afSJed Brown       next = link->next;
388b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
389b17ce1afSJed Brown     }
3900298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
391c833c3b5SJed Brown   }
392c833c3b5SJed Brown   {
393c833c3b5SJed Brown     DMRefineHookLink link,next;
394c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
395c833c3b5SJed Brown       next = link->next;
396c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
397c833c3b5SJed Brown     }
3980298fd71SBarry Smith     (*dm)->refinehook = NULL;
399c833c3b5SJed Brown   }
400be081cd6SPeter Brune   {
401be081cd6SPeter Brune     DMSubDomainHookLink link,next;
402be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
403be081cd6SPeter Brune       next = link->next;
404be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
405be081cd6SPeter Brune     }
4060298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
407be081cd6SPeter Brune   }
408baf369e7SPeter Brune   {
409baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
410baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
411baf369e7SPeter Brune       next = link->next;
412baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
413baf369e7SPeter Brune     }
4140298fd71SBarry Smith     (*dm)->gtolhook = NULL;
415baf369e7SPeter Brune   }
416aa1993deSMatthew G Knepley   /* Destroy the work arrays */
417aa1993deSMatthew G Knepley   {
418aa1993deSMatthew G Knepley     DMWorkLink link,next;
419aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
420aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
421aa1993deSMatthew G Knepley       next = link->next;
422aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
423aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
424aa1993deSMatthew G Knepley     }
4250298fd71SBarry Smith     (*dm)->workin = NULL;
426aa1993deSMatthew G Knepley   }
427b17ce1afSJed Brown 
42852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
42952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
43052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
43152536dc3SBarry Smith 
4321a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
4331a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
4341a266240SBarry Smith   }
43587e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
43671cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
4374dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
4386bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
4396bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
4406bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
441073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
44288ed4aceSMatthew G Knepley 
44388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
44488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
4458b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
44688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
44788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
448af122d2aSMatthew G Knepley 
4496636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
4506636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
4516636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
4526636e97aSMatthew G Knepley 
453af122d2aSMatthew G Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
454af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr);
455af122d2aSMatthew G Knepley   }
456af122d2aSMatthew G Knepley   ierr = PetscFree((*dm)->fields);CHKERRQ(ierr);
457732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
458f05ece33SBarry Smith   ierr = PetscObjectAMSViewOff((PetscObject)*dm);CHKERRQ(ierr);
459732e2eb9SMatthew G Knepley 
4606bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
461435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
462732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
46347c6ae99SBarry Smith   PetscFunctionReturn(0);
46447c6ae99SBarry Smith }
46547c6ae99SBarry Smith 
46647c6ae99SBarry Smith #undef __FUNCT__
467d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
468d7bf68aeSBarry Smith /*@
469d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
470d7bf68aeSBarry Smith 
471d7bf68aeSBarry Smith     Collective on DM
472d7bf68aeSBarry Smith 
473d7bf68aeSBarry Smith     Input Parameter:
474d7bf68aeSBarry Smith .   dm - the DM object to setup
475d7bf68aeSBarry Smith 
476d7bf68aeSBarry Smith     Level: developer
477d7bf68aeSBarry Smith 
478e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
479d7bf68aeSBarry Smith 
480d7bf68aeSBarry Smith @*/
4817087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
482d7bf68aeSBarry Smith {
483d7bf68aeSBarry Smith   PetscErrorCode ierr;
484d7bf68aeSBarry Smith 
485d7bf68aeSBarry Smith   PetscFunctionBegin;
486171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4878387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
488d7bf68aeSBarry Smith   if (dm->ops->setup) {
489d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
490d7bf68aeSBarry Smith   }
4918387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
492d7bf68aeSBarry Smith   PetscFunctionReturn(0);
493d7bf68aeSBarry Smith }
494d7bf68aeSBarry Smith 
495d7bf68aeSBarry Smith #undef __FUNCT__
496d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
497d7bf68aeSBarry Smith /*@
498d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
499d7bf68aeSBarry Smith 
500d7bf68aeSBarry Smith     Collective on DM
501d7bf68aeSBarry Smith 
502d7bf68aeSBarry Smith     Input Parameter:
503d7bf68aeSBarry Smith .   dm - the DM object to set options for
504d7bf68aeSBarry Smith 
505732e2eb9SMatthew G Knepley     Options Database:
506dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
507dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
508171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
509171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
510732e2eb9SMatthew G Knepley 
511d7bf68aeSBarry Smith     Level: developer
512d7bf68aeSBarry Smith 
513e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
514d7bf68aeSBarry Smith 
515d7bf68aeSBarry Smith @*/
5167087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
517d7bf68aeSBarry Smith {
518f55f929bSMatthew G Knepley   char           typeName[256] = MATAIJ;
519ca266f36SBarry Smith   PetscBool      flg;
520d7bf68aeSBarry Smith   PetscErrorCode ierr;
521d7bf68aeSBarry Smith 
522d7bf68aeSBarry Smith   PetscFunctionBegin;
523171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5243194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
5250298fd71SBarry 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);
526f9ba7244SBarry Smith   ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
527f9ba7244SBarry Smith   if (flg) {
528f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
529f9ba7244SBarry Smith   }
5308caf3d72SBarry Smith   ierr = PetscOptionsList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg);CHKERRQ(ierr);
531073dac72SJed Brown   if (flg) {
532521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
533073dac72SJed Brown   }
5340298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
535f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
536f9ba7244SBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
537f9ba7244SBarry Smith   }
538f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
539f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
54082fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
541146574abSBarry Smith   ierr = DMViewFromOptions(dm,NULL,"-dm_view");CHKERRQ(ierr);
542d7bf68aeSBarry Smith   PetscFunctionReturn(0);
543d7bf68aeSBarry Smith }
544d7bf68aeSBarry Smith 
545d7bf68aeSBarry Smith #undef __FUNCT__
54647c6ae99SBarry Smith #define __FUNCT__ "DMView"
547fc9bc008SSatish Balay /*@C
548aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
54947c6ae99SBarry Smith 
55047c6ae99SBarry Smith     Collective on DM
55147c6ae99SBarry Smith 
55247c6ae99SBarry Smith     Input Parameter:
55347c6ae99SBarry Smith +   dm - the DM object to view
55447c6ae99SBarry Smith -   v - the viewer
55547c6ae99SBarry Smith 
55647c6ae99SBarry Smith     Level: developer
55747c6ae99SBarry Smith 
558e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
55947c6ae99SBarry Smith 
56047c6ae99SBarry Smith @*/
5617087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
56247c6ae99SBarry Smith {
56347c6ae99SBarry Smith   PetscErrorCode ierr;
56432c0f0efSBarry Smith   PetscBool      isbinary;
56547c6ae99SBarry Smith 
56647c6ae99SBarry Smith   PetscFunctionBegin;
567171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5683014e516SBarry Smith   if (!v) {
569ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
5703014e516SBarry Smith   }
57132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
57232c0f0efSBarry Smith   if (isbinary) {
57355849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
57432c0f0efSBarry Smith     char     type[256];
57532c0f0efSBarry Smith 
57632c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
57732c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
57832c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
57932c0f0efSBarry Smith   }
5800c010503SBarry Smith   if (dm->ops->view) {
5810c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
58247c6ae99SBarry Smith   }
58347c6ae99SBarry Smith   PetscFunctionReturn(0);
58447c6ae99SBarry Smith }
58547c6ae99SBarry Smith 
58647c6ae99SBarry Smith #undef __FUNCT__
58747c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
58847c6ae99SBarry Smith /*@
589aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
59047c6ae99SBarry Smith 
59147c6ae99SBarry Smith     Collective on DM
59247c6ae99SBarry Smith 
59347c6ae99SBarry Smith     Input Parameter:
59447c6ae99SBarry Smith .   dm - the DM object
59547c6ae99SBarry Smith 
59647c6ae99SBarry Smith     Output Parameter:
59747c6ae99SBarry Smith .   vec - the global vector
59847c6ae99SBarry Smith 
599073dac72SJed Brown     Level: beginner
60047c6ae99SBarry Smith 
601e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60247c6ae99SBarry Smith 
60347c6ae99SBarry Smith @*/
6047087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
60547c6ae99SBarry Smith {
60647c6ae99SBarry Smith   PetscErrorCode ierr;
60747c6ae99SBarry Smith 
60847c6ae99SBarry Smith   PetscFunctionBegin;
609171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61047c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
61147c6ae99SBarry Smith   PetscFunctionReturn(0);
61247c6ae99SBarry Smith }
61347c6ae99SBarry Smith 
61447c6ae99SBarry Smith #undef __FUNCT__
61547c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
61647c6ae99SBarry Smith /*@
617aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
61847c6ae99SBarry Smith 
61947c6ae99SBarry Smith     Not Collective
62047c6ae99SBarry Smith 
62147c6ae99SBarry Smith     Input Parameter:
62247c6ae99SBarry Smith .   dm - the DM object
62347c6ae99SBarry Smith 
62447c6ae99SBarry Smith     Output Parameter:
62547c6ae99SBarry Smith .   vec - the local vector
62647c6ae99SBarry Smith 
627073dac72SJed Brown     Level: beginner
62847c6ae99SBarry Smith 
629e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
63047c6ae99SBarry Smith 
63147c6ae99SBarry Smith @*/
6327087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
63347c6ae99SBarry Smith {
63447c6ae99SBarry Smith   PetscErrorCode ierr;
63547c6ae99SBarry Smith 
63647c6ae99SBarry Smith   PetscFunctionBegin;
637171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63847c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
63947c6ae99SBarry Smith   PetscFunctionReturn(0);
64047c6ae99SBarry Smith }
64147c6ae99SBarry Smith 
64247c6ae99SBarry Smith #undef __FUNCT__
6431411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
6441411c6eeSJed Brown /*@
6451411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
6461411c6eeSJed Brown 
6471411c6eeSJed Brown    Collective on DM
6481411c6eeSJed Brown 
6491411c6eeSJed Brown    Input Parameter:
6501411c6eeSJed Brown .  dm - the DM that provides the mapping
6511411c6eeSJed Brown 
6521411c6eeSJed Brown    Output Parameter:
6531411c6eeSJed Brown .  ltog - the mapping
6541411c6eeSJed Brown 
6551411c6eeSJed Brown    Level: intermediate
6561411c6eeSJed Brown 
6571411c6eeSJed Brown    Notes:
6581411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
6591411c6eeSJed Brown    MatSetLocalToGlobalMapping().
6601411c6eeSJed Brown 
6611411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
6621411c6eeSJed Brown @*/
6637087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
6641411c6eeSJed Brown {
6651411c6eeSJed Brown   PetscErrorCode ierr;
6661411c6eeSJed Brown 
6671411c6eeSJed Brown   PetscFunctionBegin;
6681411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6691411c6eeSJed Brown   PetscValidPointer(ltog,2);
6701411c6eeSJed Brown   if (!dm->ltogmap) {
67137d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
67237d0c07bSMatthew G Knepley 
67337d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
67437d0c07bSMatthew G Knepley     if (section) {
67537d0c07bSMatthew G Knepley       PetscInt *ltog;
67637d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
67737d0c07bSMatthew G Knepley 
67837d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
67937d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
68037d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
68137d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
68237d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
68337d0c07bSMatthew G Knepley         PetscInt dof, off, c;
68437d0c07bSMatthew G Knepley 
68537d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
68637d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
68737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
68837d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
68937d0c07bSMatthew G Knepley           ltog[l] = off+c;
69037d0c07bSMatthew G Knepley         }
69137d0c07bSMatthew G Knepley       }
69237d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
69337d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
69437d0c07bSMatthew G Knepley     } else {
695184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
696184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
6971411c6eeSJed Brown     }
69837d0c07bSMatthew G Knepley   }
6991411c6eeSJed Brown   *ltog = dm->ltogmap;
7001411c6eeSJed Brown   PetscFunctionReturn(0);
7011411c6eeSJed Brown }
7021411c6eeSJed Brown 
7031411c6eeSJed Brown #undef __FUNCT__
7041411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
7051411c6eeSJed Brown /*@
7061411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
7071411c6eeSJed Brown 
7081411c6eeSJed Brown    Collective on DM
7091411c6eeSJed Brown 
7101411c6eeSJed Brown    Input Parameter:
7111411c6eeSJed Brown .  da - the distributed array that provides the mapping
7121411c6eeSJed Brown 
7131411c6eeSJed Brown    Output Parameter:
7141411c6eeSJed Brown .  ltog - the block mapping
7151411c6eeSJed Brown 
7161411c6eeSJed Brown    Level: intermediate
7171411c6eeSJed Brown 
7181411c6eeSJed Brown    Notes:
7191411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
7201411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
7211411c6eeSJed Brown 
7221411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
7231411c6eeSJed Brown @*/
7247087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
7251411c6eeSJed Brown {
7261411c6eeSJed Brown   PetscErrorCode ierr;
7271411c6eeSJed Brown 
7281411c6eeSJed Brown   PetscFunctionBegin;
7291411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7301411c6eeSJed Brown   PetscValidPointer(ltog,2);
7311411c6eeSJed Brown   if (!dm->ltogmapb) {
7321411c6eeSJed Brown     PetscInt bs;
7331411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
7341411c6eeSJed Brown     if (bs > 1) {
735184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmappingblock) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
736184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
7371411c6eeSJed Brown     } else {
7381411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
7391411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
7401411c6eeSJed Brown     }
7411411c6eeSJed Brown   }
7421411c6eeSJed Brown   *ltog = dm->ltogmapb;
7431411c6eeSJed Brown   PetscFunctionReturn(0);
7441411c6eeSJed Brown }
7451411c6eeSJed Brown 
7461411c6eeSJed Brown #undef __FUNCT__
7471411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7481411c6eeSJed Brown /*@
7491411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7501411c6eeSJed Brown 
7511411c6eeSJed Brown    Not Collective
7521411c6eeSJed Brown 
7531411c6eeSJed Brown    Input Parameter:
7541411c6eeSJed Brown .  dm - the DM with block structure
7551411c6eeSJed Brown 
7561411c6eeSJed Brown    Output Parameter:
7571411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7581411c6eeSJed Brown 
7591411c6eeSJed Brown    Level: intermediate
7601411c6eeSJed Brown 
7611411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
7621411c6eeSJed Brown @*/
7637087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7641411c6eeSJed Brown {
7651411c6eeSJed Brown   PetscFunctionBegin;
7661411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7671411c6eeSJed Brown   PetscValidPointer(bs,2);
7681411c6eeSJed 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");
7691411c6eeSJed Brown   *bs = dm->bs;
7701411c6eeSJed Brown   PetscFunctionReturn(0);
7711411c6eeSJed Brown }
7721411c6eeSJed Brown 
7731411c6eeSJed Brown #undef __FUNCT__
774e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
77547c6ae99SBarry Smith /*@
776e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
77747c6ae99SBarry Smith 
77847c6ae99SBarry Smith     Collective on DM
77947c6ae99SBarry Smith 
78047c6ae99SBarry Smith     Input Parameter:
78147c6ae99SBarry Smith +   dm1 - the DM object
78247c6ae99SBarry Smith -   dm2 - the second, finer DM object
78347c6ae99SBarry Smith 
78447c6ae99SBarry Smith     Output Parameter:
78547c6ae99SBarry Smith +  mat - the interpolation
78647c6ae99SBarry Smith -  vec - the scaling (optional)
78747c6ae99SBarry Smith 
78847c6ae99SBarry Smith     Level: developer
78947c6ae99SBarry Smith 
79085afcc9aSBarry 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
79185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
792d52bd9f3SBarry Smith 
7931f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
794d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
79585afcc9aSBarry Smith 
79685afcc9aSBarry Smith 
797e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
79847c6ae99SBarry Smith 
79947c6ae99SBarry Smith @*/
800e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
80147c6ae99SBarry Smith {
80247c6ae99SBarry Smith   PetscErrorCode ierr;
80347c6ae99SBarry Smith 
80447c6ae99SBarry Smith   PetscFunctionBegin;
805171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
806171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
80725296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
80847c6ae99SBarry Smith   PetscFunctionReturn(0);
80947c6ae99SBarry Smith }
81047c6ae99SBarry Smith 
81147c6ae99SBarry Smith #undef __FUNCT__
812e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
81347c6ae99SBarry Smith /*@
814e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
81547c6ae99SBarry Smith 
81647c6ae99SBarry Smith     Collective on DM
81747c6ae99SBarry Smith 
81847c6ae99SBarry Smith     Input Parameter:
81947c6ae99SBarry Smith +   dm1 - the DM object
82047c6ae99SBarry Smith -   dm2 - the second, finer DM object
82147c6ae99SBarry Smith 
82247c6ae99SBarry Smith     Output Parameter:
82347c6ae99SBarry Smith .   ctx - the injection
82447c6ae99SBarry Smith 
82547c6ae99SBarry Smith     Level: developer
82647c6ae99SBarry Smith 
82785afcc9aSBarry 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
82885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
82985afcc9aSBarry Smith 
830e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
83147c6ae99SBarry Smith 
83247c6ae99SBarry Smith @*/
833e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
83447c6ae99SBarry Smith {
83547c6ae99SBarry Smith   PetscErrorCode ierr;
83647c6ae99SBarry Smith 
83747c6ae99SBarry Smith   PetscFunctionBegin;
838171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
839171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
84047c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
84147c6ae99SBarry Smith   PetscFunctionReturn(0);
84247c6ae99SBarry Smith }
84347c6ae99SBarry Smith 
84447c6ae99SBarry Smith #undef __FUNCT__
845e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
846d1e2c406SBarry Smith /*@C
847e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
84847c6ae99SBarry Smith 
84947c6ae99SBarry Smith     Collective on DM
85047c6ae99SBarry Smith 
85147c6ae99SBarry Smith     Input Parameter:
85247c6ae99SBarry Smith +   dm - the DM object
85347c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
85447c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
85547c6ae99SBarry Smith 
85647c6ae99SBarry Smith     Output Parameter:
85747c6ae99SBarry Smith .   coloring - the coloring
85847c6ae99SBarry Smith 
85947c6ae99SBarry Smith     Level: developer
86047c6ae99SBarry Smith 
861e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
86247c6ae99SBarry Smith 
863aab9d709SJed Brown @*/
86419fd82e9SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,MatType mtype,ISColoring *coloring)
86547c6ae99SBarry Smith {
86647c6ae99SBarry Smith   PetscErrorCode ierr;
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith   PetscFunctionBegin;
869171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
870ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
87147c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
87247c6ae99SBarry Smith   PetscFunctionReturn(0);
87347c6ae99SBarry Smith }
87447c6ae99SBarry Smith 
87547c6ae99SBarry Smith #undef __FUNCT__
876950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
87747c6ae99SBarry Smith /*@C
878950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
87947c6ae99SBarry Smith 
88047c6ae99SBarry Smith     Collective on DM
88147c6ae99SBarry Smith 
88247c6ae99SBarry Smith     Input Parameter:
88347c6ae99SBarry Smith +   dm - the DM object
88447c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
88594013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
88647c6ae99SBarry Smith 
88747c6ae99SBarry Smith     Output Parameter:
88847c6ae99SBarry Smith .   mat - the empty Jacobian
88947c6ae99SBarry Smith 
890073dac72SJed Brown     Level: beginner
89147c6ae99SBarry Smith 
89294013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
89394013140SBarry Smith        do not need to do it yourself.
89494013140SBarry Smith 
89594013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
896aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
89794013140SBarry Smith 
89894013140SBarry 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
89994013140SBarry Smith        internally by PETSc.
90094013140SBarry Smith 
90194013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
902aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
90394013140SBarry Smith 
904e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
90547c6ae99SBarry Smith 
906aab9d709SJed Brown @*/
90719fd82e9SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,MatType mtype,Mat *mat)
90847c6ae99SBarry Smith {
90947c6ae99SBarry Smith   PetscErrorCode ierr;
91047c6ae99SBarry Smith 
91147c6ae99SBarry Smith   PetscFunctionBegin;
912171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
913519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
914607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
915235683edSBarry Smith #endif
916c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
917c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
918073dac72SJed Brown   if (dm->mattype) {
91925296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
920073dac72SJed Brown   } else {
92125296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
922c7b7c8a4SJed Brown   }
92347c6ae99SBarry Smith   PetscFunctionReturn(0);
92447c6ae99SBarry Smith }
92547c6ae99SBarry Smith 
92647c6ae99SBarry Smith #undef __FUNCT__
927732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
928732e2eb9SMatthew G Knepley /*@
929950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
930732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
931732e2eb9SMatthew G Knepley 
932732e2eb9SMatthew G Knepley   Logically Collective on DMDA
933732e2eb9SMatthew G Knepley 
934732e2eb9SMatthew G Knepley   Input Parameter:
935732e2eb9SMatthew G Knepley + dm - the DM
936732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
937732e2eb9SMatthew G Knepley 
938732e2eb9SMatthew G Knepley   Level: developer
939950540a4SJed Brown .seealso DMCreateMatrix()
940732e2eb9SMatthew G Knepley @*/
941732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
942732e2eb9SMatthew G Knepley {
943732e2eb9SMatthew G Knepley   PetscFunctionBegin;
944732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
945732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
946732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
947732e2eb9SMatthew G Knepley }
948732e2eb9SMatthew G Knepley 
949732e2eb9SMatthew G Knepley #undef __FUNCT__
950a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
951a89ea682SMatthew G Knepley /*@C
952aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
953a89ea682SMatthew G Knepley 
954a89ea682SMatthew G Knepley   Not Collective
955a89ea682SMatthew G Knepley 
956a89ea682SMatthew G Knepley   Input Parameters:
957a89ea682SMatthew G Knepley + dm - the DM object
958aa1993deSMatthew G Knepley . count - The minium size
959aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
960a89ea682SMatthew G Knepley 
961a89ea682SMatthew G Knepley   Output Parameter:
962a89ea682SMatthew G Knepley . array - the work array
963a89ea682SMatthew G Knepley 
964a89ea682SMatthew G Knepley   Level: developer
965a89ea682SMatthew G Knepley 
966a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
967a89ea682SMatthew G Knepley @*/
968aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
969a89ea682SMatthew G Knepley {
970a89ea682SMatthew G Knepley   PetscErrorCode ierr;
971aa1993deSMatthew G Knepley   DMWorkLink     link;
972aa1993deSMatthew G Knepley   size_t         size;
973a89ea682SMatthew G Knepley 
974a89ea682SMatthew G Knepley   PetscFunctionBegin;
975a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
976aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
977aa1993deSMatthew G Knepley   if (dm->workin) {
978aa1993deSMatthew G Knepley     link       = dm->workin;
979aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
980aa1993deSMatthew G Knepley   } else {
981aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
982a89ea682SMatthew G Knepley   }
983aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
984aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
985aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
986aa1993deSMatthew G Knepley     ierr        = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
987aa1993deSMatthew G Knepley     link->bytes = size*count;
988aa1993deSMatthew G Knepley   }
989aa1993deSMatthew G Knepley   link->next   = dm->workout;
990aa1993deSMatthew G Knepley   dm->workout  = link;
991aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
992a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
993a89ea682SMatthew G Knepley }
994a89ea682SMatthew G Knepley 
995aa1993deSMatthew G Knepley #undef __FUNCT__
996aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
997aa1993deSMatthew G Knepley /*@C
998aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
999aa1993deSMatthew G Knepley 
1000aa1993deSMatthew G Knepley   Not Collective
1001aa1993deSMatthew G Knepley 
1002aa1993deSMatthew G Knepley   Input Parameters:
1003aa1993deSMatthew G Knepley + dm - the DM object
1004aa1993deSMatthew G Knepley . count - The minium size
1005aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1006aa1993deSMatthew G Knepley 
1007aa1993deSMatthew G Knepley   Output Parameter:
1008aa1993deSMatthew G Knepley . array - the work array
1009aa1993deSMatthew G Knepley 
1010aa1993deSMatthew G Knepley   Level: developer
1011aa1993deSMatthew G Knepley 
1012aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1013aa1993deSMatthew G Knepley @*/
1014aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1015aa1993deSMatthew G Knepley {
1016aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1017aa1993deSMatthew G Knepley 
1018aa1993deSMatthew G Knepley   PetscFunctionBegin;
1019aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1020aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1021aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1022aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1023aa1993deSMatthew G Knepley       *p           = link->next;
1024aa1993deSMatthew G Knepley       link->next   = dm->workin;
1025aa1993deSMatthew G Knepley       dm->workin   = link;
10260298fd71SBarry Smith       *(void**)mem = NULL;
1027aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1028aa1993deSMatthew G Knepley     }
1029aa1993deSMatthew G Knepley   }
1030aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1031aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1032aa1993deSMatthew G Knepley }
1033e7c4fc90SDmitry Karpeev 
1034e7c4fc90SDmitry Karpeev #undef __FUNCT__
1035435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1036435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1037435a35e8SMatthew G Knepley {
1038435a35e8SMatthew G Knepley   PetscFunctionBegin;
1039435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
104082f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1041435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1042435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1043435a35e8SMatthew G Knepley }
1044435a35e8SMatthew G Knepley 
1045435a35e8SMatthew G Knepley #undef __FUNCT__
10464d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10474f3b5142SJed Brown /*@C
10484d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10494d343eeaSMatthew G Knepley 
10504d343eeaSMatthew G Knepley   Not collective
10514d343eeaSMatthew G Knepley 
10524d343eeaSMatthew G Knepley   Input Parameter:
10534d343eeaSMatthew G Knepley . dm - the DM object
10544d343eeaSMatthew G Knepley 
10554d343eeaSMatthew G Knepley   Output Parameters:
10560298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
10570298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
10580298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
10594d343eeaSMatthew G Knepley 
10604d343eeaSMatthew G Knepley   Level: intermediate
10614d343eeaSMatthew G Knepley 
106221c9b008SJed Brown   Notes:
106321c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
106421c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
106521c9b008SJed Brown   PetscFree().
106621c9b008SJed Brown 
10674d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10684d343eeaSMatthew G Knepley @*/
106937d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10704d343eeaSMatthew G Knepley {
107137d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10724d343eeaSMatthew G Knepley   PetscErrorCode ierr;
10734d343eeaSMatthew G Knepley 
10744d343eeaSMatthew G Knepley   PetscFunctionBegin;
10754d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
107669ca1f37SDmitry Karpeev   if (numFields) {
107769ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
107869ca1f37SDmitry Karpeev     *numFields = 0;
107969ca1f37SDmitry Karpeev   }
108037d0c07bSMatthew G Knepley   if (fieldNames) {
108137d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
10820298fd71SBarry Smith     *fieldNames = NULL;
108369ca1f37SDmitry Karpeev   }
108469ca1f37SDmitry Karpeev   if (fields) {
108569ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
10860298fd71SBarry Smith     *fields = NULL;
108769ca1f37SDmitry Karpeev   }
108837d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
108937d0c07bSMatthew G Knepley   if (section) {
109037d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
109137d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
109237d0c07bSMatthew G Knepley 
109337d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
109437d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
109537d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt*,&fieldIndices);CHKERRQ(ierr);
109637d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
109737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
109837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
109937d0c07bSMatthew G Knepley     }
110037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
110137d0c07bSMatthew G Knepley       PetscInt gdof;
110237d0c07bSMatthew G Knepley 
110337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
110437d0c07bSMatthew G Knepley       if (gdof > 0) {
110537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
110637d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
110737d0c07bSMatthew G Knepley 
110837d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
110937d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
111037d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
111137d0c07bSMatthew G Knepley         }
111237d0c07bSMatthew G Knepley       }
111337d0c07bSMatthew G Knepley     }
111437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
111537d0c07bSMatthew G Knepley       ierr          = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
111637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
111737d0c07bSMatthew G Knepley     }
111837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
111937d0c07bSMatthew G Knepley       PetscInt gdof, goff;
112037d0c07bSMatthew G Knepley 
112137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
112237d0c07bSMatthew G Knepley       if (gdof > 0) {
112337d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
112437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
112537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
112637d0c07bSMatthew G Knepley 
112737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
112837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
112937d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
113037d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
113137d0c07bSMatthew G Knepley           }
113237d0c07bSMatthew G Knepley         }
113337d0c07bSMatthew G Knepley       }
113437d0c07bSMatthew G Knepley     }
11358865f1eaSKarl Rupp     if (numFields) *numFields = nF;
113637d0c07bSMatthew G Knepley     if (fieldNames) {
113737d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char*), fieldNames);CHKERRQ(ierr);
113837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
113937d0c07bSMatthew G Knepley         const char *fieldName;
114037d0c07bSMatthew G Knepley 
114137d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
114237d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
114337d0c07bSMatthew G Knepley       }
114437d0c07bSMatthew G Knepley     }
114537d0c07bSMatthew G Knepley     if (fields) {
114637d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
114737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
114882f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
114937d0c07bSMatthew G Knepley       }
115037d0c07bSMatthew G Knepley     }
115137d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
11528865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
11538865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
115469ca1f37SDmitry Karpeev   }
11554d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11564d343eeaSMatthew G Knepley }
11574d343eeaSMatthew G Knepley 
115816621825SDmitry Karpeev 
115916621825SDmitry Karpeev #undef __FUNCT__
116016621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
116116621825SDmitry Karpeev /*@C
116216621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
116316621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
116416621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1165e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1166e7c4fc90SDmitry Karpeev 
1167e7c4fc90SDmitry Karpeev   Not collective
1168e7c4fc90SDmitry Karpeev 
1169e7c4fc90SDmitry Karpeev   Input Parameter:
1170e7c4fc90SDmitry Karpeev . dm - the DM object
1171e7c4fc90SDmitry Karpeev 
1172e7c4fc90SDmitry Karpeev   Output Parameters:
11730298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
11740298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
11750298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
11760298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1177e7c4fc90SDmitry Karpeev 
1178e7c4fc90SDmitry Karpeev   Level: intermediate
1179e7c4fc90SDmitry Karpeev 
1180e7c4fc90SDmitry Karpeev   Notes:
1181e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1182e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1183e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1184e7c4fc90SDmitry Karpeev 
1185e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1186e7c4fc90SDmitry Karpeev @*/
118716621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1188e7c4fc90SDmitry Karpeev {
1189e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1190e7c4fc90SDmitry Karpeev 
1191e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1192e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11938865f1eaSKarl Rupp   if (len) {
11948865f1eaSKarl Rupp     PetscValidPointer(len,2);
11958865f1eaSKarl Rupp     *len = 0;
11968865f1eaSKarl Rupp   }
11978865f1eaSKarl Rupp   if (namelist) {
11988865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
11998865f1eaSKarl Rupp     *namelist = 0;
12008865f1eaSKarl Rupp   }
12018865f1eaSKarl Rupp   if (islist) {
12028865f1eaSKarl Rupp     PetscValidPointer(islist,4);
12038865f1eaSKarl Rupp     *islist = 0;
12048865f1eaSKarl Rupp   }
12058865f1eaSKarl Rupp   if (dmlist) {
12068865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
12078865f1eaSKarl Rupp     *dmlist = 0;
12088865f1eaSKarl Rupp   }
1209f3f0edfdSDmitry Karpeev   /*
1210f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1211f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1212f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1213f3f0edfdSDmitry Karpeev    */
1214ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
121516621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1216435a35e8SMatthew G Knepley     PetscSection section;
1217435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1218435a35e8SMatthew G Knepley 
1219435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1220435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1221435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1222435a35e8SMatthew G Knepley       *len = numFields;
1223435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1224435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1225435a35e8SMatthew G Knepley         const char *fieldName;
1226435a35e8SMatthew G Knepley 
1227435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1228435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1229435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1230435a35e8SMatthew G Knepley       }
1231435a35e8SMatthew G Knepley     } else {
123269ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1233e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
12340298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1235e7c4fc90SDmitry Karpeev     }
12368865f1eaSKarl Rupp   } else {
123716621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
123816621825SDmitry Karpeev   }
123916621825SDmitry Karpeev   PetscFunctionReturn(0);
124016621825SDmitry Karpeev }
124116621825SDmitry Karpeev 
124216621825SDmitry Karpeev #undef __FUNCT__
1243435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1244435a35e8SMatthew G Knepley /*@C
1245435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1246435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1247435a35e8SMatthew G Knepley 
1248435a35e8SMatthew G Knepley   Not collective
1249435a35e8SMatthew G Knepley 
1250435a35e8SMatthew G Knepley   Input Parameters:
1251435a35e8SMatthew G Knepley + dm - the DM object
1252435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
12530298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1254435a35e8SMatthew G Knepley 
1255435a35e8SMatthew G Knepley   Output Parameters:
1256435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1257435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1258435a35e8SMatthew G Knepley 
1259435a35e8SMatthew G Knepley   Level: intermediate
1260435a35e8SMatthew G Knepley 
1261435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1262435a35e8SMatthew G Knepley @*/
1263435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1264435a35e8SMatthew G Knepley {
1265435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1266435a35e8SMatthew G Knepley 
1267435a35e8SMatthew G Knepley   PetscFunctionBegin;
1268435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1269435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
12708865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
12718865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1272435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1273435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
127482f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1275435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1276435a35e8SMatthew G Knepley }
1277435a35e8SMatthew G Knepley 
127816621825SDmitry Karpeev 
127916621825SDmitry Karpeev #undef __FUNCT__
128016621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
128116621825SDmitry Karpeev /*@C
12828d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
12838d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
12848d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
12858d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
12868d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
128716621825SDmitry Karpeev 
128816621825SDmitry Karpeev   Not collective
128916621825SDmitry Karpeev 
129016621825SDmitry Karpeev   Input Parameter:
129116621825SDmitry Karpeev . dm - the DM object
129216621825SDmitry Karpeev 
129316621825SDmitry Karpeev   Output Parameters:
12940298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
12950298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
12960298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
12970298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
12980298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
129916621825SDmitry Karpeev 
130016621825SDmitry Karpeev   Level: intermediate
130116621825SDmitry Karpeev 
130216621825SDmitry Karpeev   Notes:
130316621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
130416621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
130516621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
130616621825SDmitry Karpeev 
13078d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
130816621825SDmitry Karpeev @*/
13098d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
131016621825SDmitry Karpeev {
131116621825SDmitry Karpeev   PetscErrorCode      ierr;
1312be081cd6SPeter Brune   DMSubDomainHookLink link;
1313be081cd6SPeter Brune   PetscInt            i,l;
131416621825SDmitry Karpeev 
131516621825SDmitry Karpeev   PetscFunctionBegin;
131616621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
131714a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
13180298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
13190298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
13200298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
13210298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1322f3f0edfdSDmitry Karpeev   /*
1323f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1324f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1325f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1326f3f0edfdSDmitry Karpeev    */
1327ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
132816621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1329be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
133014a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
133114a18fd3SPeter Brune     if (dmlist) {
1332be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1333be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1334be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1335be081cd6SPeter Brune         }
1336d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1337e7c4fc90SDmitry Karpeev       }
133814a18fd3SPeter Brune     }
133914a18fd3SPeter Brune     if (len) *len = l;
134014a18fd3SPeter Brune   }
1341e30e807fSPeter Brune   PetscFunctionReturn(0);
1342e30e807fSPeter Brune }
1343e30e807fSPeter Brune 
1344e30e807fSPeter Brune 
1345e30e807fSPeter Brune #undef __FUNCT__
1346e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1347e30e807fSPeter Brune /*@C
1348e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1349e30e807fSPeter Brune 
1350e30e807fSPeter Brune   Not collective
1351e30e807fSPeter Brune 
1352e30e807fSPeter Brune   Input Parameters:
1353e30e807fSPeter Brune + dm - the DM object
1354e30e807fSPeter Brune . n  - the number of subdomain scatters
1355e30e807fSPeter Brune - subdms - the local subdomains
1356e30e807fSPeter Brune 
1357e30e807fSPeter Brune   Output Parameters:
1358e30e807fSPeter Brune + n     - the number of scatters returned
1359e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1360e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1361e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1362e30e807fSPeter Brune 
1363e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1364e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1365e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1366e30e807fSPeter Brune   solution and residual data.
1367e30e807fSPeter Brune 
1368e30e807fSPeter Brune   Level: developer
1369e30e807fSPeter Brune 
1370e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1371e30e807fSPeter Brune @*/
1372e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1373e30e807fSPeter Brune {
1374e30e807fSPeter Brune   PetscErrorCode ierr;
1375e30e807fSPeter Brune 
1376e30e807fSPeter Brune   PetscFunctionBegin;
1377e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1378e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1379e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1380e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
138182f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1382e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1383e7c4fc90SDmitry Karpeev }
1384e7c4fc90SDmitry Karpeev 
1385731c8d9eSDmitry Karpeev #undef __FUNCT__
138647c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
138747c6ae99SBarry Smith /*@
138847c6ae99SBarry Smith   DMRefine - Refines a DM object
138947c6ae99SBarry Smith 
139047c6ae99SBarry Smith   Collective on DM
139147c6ae99SBarry Smith 
139247c6ae99SBarry Smith   Input Parameter:
139347c6ae99SBarry Smith + dm   - the DM object
139491d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
139547c6ae99SBarry Smith 
139647c6ae99SBarry Smith   Output Parameter:
13970298fd71SBarry Smith . dmf - the refined DM, or NULL
1398ae0a1c52SMatthew G Knepley 
13990298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
140047c6ae99SBarry Smith 
140147c6ae99SBarry Smith   Level: developer
140247c6ae99SBarry Smith 
1403e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
140447c6ae99SBarry Smith @*/
14057087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
140647c6ae99SBarry Smith {
140747c6ae99SBarry Smith   PetscErrorCode   ierr;
1408c833c3b5SJed Brown   DMRefineHookLink link;
140947c6ae99SBarry Smith 
141047c6ae99SBarry Smith   PetscFunctionBegin;
1411732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
141247c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14134057135bSMatthew G Knepley   if (*dmf) {
141443842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
14158865f1eaSKarl Rupp 
14168cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
14178865f1eaSKarl Rupp 
1418644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14190598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1420656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
14218865f1eaSKarl Rupp 
1422e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1423c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
14248865f1eaSKarl Rupp       if (link->refinehook) {
14258865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
14268865f1eaSKarl Rupp       }
1427c833c3b5SJed Brown     }
1428c833c3b5SJed Brown   }
1429c833c3b5SJed Brown   PetscFunctionReturn(0);
1430c833c3b5SJed Brown }
1431c833c3b5SJed Brown 
1432c833c3b5SJed Brown #undef __FUNCT__
1433c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1434bb9467b5SJed Brown /*@C
1435c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1436c833c3b5SJed Brown 
1437c833c3b5SJed Brown    Logically Collective
1438c833c3b5SJed Brown 
1439c833c3b5SJed Brown    Input Arguments:
1440c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1441c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1442c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
14430298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1444c833c3b5SJed Brown 
1445c833c3b5SJed Brown    Calling sequence of refinehook:
1446c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1447c833c3b5SJed Brown 
1448c833c3b5SJed Brown +  coarse - coarse level DM
1449c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1450c833c3b5SJed Brown -  ctx - optional user-defined function context
1451c833c3b5SJed Brown 
1452c833c3b5SJed Brown    Calling sequence for interphook:
1453c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1454c833c3b5SJed Brown 
1455c833c3b5SJed Brown +  coarse - coarse level DM
1456c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1457c833c3b5SJed Brown .  fine - fine level DM to update
1458c833c3b5SJed Brown -  ctx - optional user-defined function context
1459c833c3b5SJed Brown 
1460c833c3b5SJed Brown    Level: advanced
1461c833c3b5SJed Brown 
1462c833c3b5SJed Brown    Notes:
1463c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1464c833c3b5SJed Brown 
1465c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1466c833c3b5SJed Brown 
1467bb9467b5SJed Brown    This function is currently not available from Fortran.
1468bb9467b5SJed Brown 
1469c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1470c833c3b5SJed Brown @*/
1471c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1472c833c3b5SJed Brown {
1473c833c3b5SJed Brown   PetscErrorCode   ierr;
1474c833c3b5SJed Brown   DMRefineHookLink link,*p;
1475c833c3b5SJed Brown 
1476c833c3b5SJed Brown   PetscFunctionBegin;
1477c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1478c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1479c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1480c833c3b5SJed Brown   link->refinehook = refinehook;
1481c833c3b5SJed Brown   link->interphook = interphook;
1482c833c3b5SJed Brown   link->ctx        = ctx;
14830298fd71SBarry Smith   link->next       = NULL;
1484c833c3b5SJed Brown   *p               = link;
1485c833c3b5SJed Brown   PetscFunctionReturn(0);
1486c833c3b5SJed Brown }
1487c833c3b5SJed Brown 
1488c833c3b5SJed Brown #undef __FUNCT__
1489c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1490c833c3b5SJed Brown /*@
1491c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1492c833c3b5SJed Brown 
1493c833c3b5SJed Brown    Collective if any hooks are
1494c833c3b5SJed Brown 
1495c833c3b5SJed Brown    Input Arguments:
1496c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1497c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1498c833c3b5SJed Brown -  fine - finer DM to update
1499c833c3b5SJed Brown 
1500c833c3b5SJed Brown    Level: developer
1501c833c3b5SJed Brown 
1502c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1503c833c3b5SJed Brown @*/
1504c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1505c833c3b5SJed Brown {
1506c833c3b5SJed Brown   PetscErrorCode   ierr;
1507c833c3b5SJed Brown   DMRefineHookLink link;
1508c833c3b5SJed Brown 
1509c833c3b5SJed Brown   PetscFunctionBegin;
1510c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
15118865f1eaSKarl Rupp     if (link->interphook) {
15128865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
15138865f1eaSKarl Rupp     }
15144057135bSMatthew G Knepley   }
151547c6ae99SBarry Smith   PetscFunctionReturn(0);
151647c6ae99SBarry Smith }
151747c6ae99SBarry Smith 
151847c6ae99SBarry Smith #undef __FUNCT__
1519eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1520eb3f98d2SBarry Smith /*@
1521eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1522eb3f98d2SBarry Smith 
1523eb3f98d2SBarry Smith     Not Collective
1524eb3f98d2SBarry Smith 
1525eb3f98d2SBarry Smith     Input Parameter:
1526eb3f98d2SBarry Smith .   dm - the DM object
1527eb3f98d2SBarry Smith 
1528eb3f98d2SBarry Smith     Output Parameter:
1529eb3f98d2SBarry Smith .   level - number of refinements
1530eb3f98d2SBarry Smith 
1531eb3f98d2SBarry Smith     Level: developer
1532eb3f98d2SBarry Smith 
15336a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1534eb3f98d2SBarry Smith 
1535eb3f98d2SBarry Smith @*/
1536eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1537eb3f98d2SBarry Smith {
1538eb3f98d2SBarry Smith   PetscFunctionBegin;
1539eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1540eb3f98d2SBarry Smith   *level = dm->levelup;
1541eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1542eb3f98d2SBarry Smith }
1543eb3f98d2SBarry Smith 
1544eb3f98d2SBarry Smith #undef __FUNCT__
1545baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1546bb9467b5SJed Brown /*@C
1547baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1548baf369e7SPeter Brune 
1549baf369e7SPeter Brune    Logically Collective
1550baf369e7SPeter Brune 
1551baf369e7SPeter Brune    Input Arguments:
1552baf369e7SPeter Brune +  dm - the DM
1553baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1554baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
15550298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1556baf369e7SPeter Brune 
1557baf369e7SPeter Brune    Calling sequence for beginhook:
1558baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1559baf369e7SPeter Brune 
1560baf369e7SPeter Brune +  dm - global DM
1561baf369e7SPeter Brune .  g - global vector
1562baf369e7SPeter Brune .  mode - mode
1563baf369e7SPeter Brune .  l - local vector
1564baf369e7SPeter Brune -  ctx - optional user-defined function context
1565baf369e7SPeter Brune 
1566baf369e7SPeter Brune 
1567baf369e7SPeter Brune    Calling sequence for endhook:
1568ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1569baf369e7SPeter Brune 
1570baf369e7SPeter Brune +  global - global DM
1571baf369e7SPeter Brune -  ctx - optional user-defined function context
1572baf369e7SPeter Brune 
1573baf369e7SPeter Brune    Level: advanced
1574baf369e7SPeter Brune 
1575baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1576baf369e7SPeter Brune @*/
1577baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1578baf369e7SPeter Brune {
1579baf369e7SPeter Brune   PetscErrorCode          ierr;
1580baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1581baf369e7SPeter Brune 
1582baf369e7SPeter Brune   PetscFunctionBegin;
1583baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1584baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1585baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1586baf369e7SPeter Brune   link->beginhook = beginhook;
1587baf369e7SPeter Brune   link->endhook   = endhook;
1588baf369e7SPeter Brune   link->ctx       = ctx;
15890298fd71SBarry Smith   link->next      = NULL;
1590baf369e7SPeter Brune   *p              = link;
1591baf369e7SPeter Brune   PetscFunctionReturn(0);
1592baf369e7SPeter Brune }
1593baf369e7SPeter Brune 
1594baf369e7SPeter Brune #undef __FUNCT__
159547c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
159647c6ae99SBarry Smith /*@
159747c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
159847c6ae99SBarry Smith 
159947c6ae99SBarry Smith     Neighbor-wise Collective on DM
160047c6ae99SBarry Smith 
160147c6ae99SBarry Smith     Input Parameters:
160247c6ae99SBarry Smith +   dm - the DM object
160347c6ae99SBarry Smith .   g - the global vector
160447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
160547c6ae99SBarry Smith -   l - the local vector
160647c6ae99SBarry Smith 
160747c6ae99SBarry Smith 
160847c6ae99SBarry Smith     Level: beginner
160947c6ae99SBarry Smith 
1610e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
161147c6ae99SBarry Smith 
161247c6ae99SBarry Smith @*/
16137087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
161447c6ae99SBarry Smith {
16157128ae9fSMatthew G Knepley   PetscSF                 sf;
161647c6ae99SBarry Smith   PetscErrorCode          ierr;
1617baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
161847c6ae99SBarry Smith 
161947c6ae99SBarry Smith   PetscFunctionBegin;
1620171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1621baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
16228865f1eaSKarl Rupp     if (link->beginhook) {
16238865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
16248865f1eaSKarl Rupp     }
1625baf369e7SPeter Brune   }
16267128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16277128ae9fSMatthew G Knepley   if (sf) {
16287128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16297128ae9fSMatthew G Knepley 
163082f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16317128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16327128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16337128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16347128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16357128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16367128ae9fSMatthew G Knepley   } else {
1637843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16387128ae9fSMatthew G Knepley   }
163947c6ae99SBarry Smith   PetscFunctionReturn(0);
164047c6ae99SBarry Smith }
164147c6ae99SBarry Smith 
164247c6ae99SBarry Smith #undef __FUNCT__
164347c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
164447c6ae99SBarry Smith /*@
164547c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
164647c6ae99SBarry Smith 
164747c6ae99SBarry Smith     Neighbor-wise Collective on DM
164847c6ae99SBarry Smith 
164947c6ae99SBarry Smith     Input Parameters:
165047c6ae99SBarry Smith +   dm - the DM object
165147c6ae99SBarry Smith .   g - the global vector
165247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
165347c6ae99SBarry Smith -   l - the local vector
165447c6ae99SBarry Smith 
165547c6ae99SBarry Smith 
165647c6ae99SBarry Smith     Level: beginner
165747c6ae99SBarry Smith 
1658e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
165947c6ae99SBarry Smith 
166047c6ae99SBarry Smith @*/
16617087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
166247c6ae99SBarry Smith {
16637128ae9fSMatthew G Knepley   PetscSF                 sf;
166447c6ae99SBarry Smith   PetscErrorCode          ierr;
166561a3c1faSSatish Balay   PetscScalar             *lArray, *gArray;
1666baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
166747c6ae99SBarry Smith 
166847c6ae99SBarry Smith   PetscFunctionBegin;
1669171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16707128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16717128ae9fSMatthew G Knepley   if (sf) {
167282f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16737128ae9fSMatthew G Knepley 
16747128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16757128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16767128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16777128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16787128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16797128ae9fSMatthew G Knepley   } else {
1680843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16817128ae9fSMatthew G Knepley   }
1682baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1683baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1684baf369e7SPeter Brune   }
168547c6ae99SBarry Smith   PetscFunctionReturn(0);
168647c6ae99SBarry Smith }
168747c6ae99SBarry Smith 
168847c6ae99SBarry Smith #undef __FUNCT__
16899a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
169047c6ae99SBarry Smith /*@
16919a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
16929a42bb27SBarry Smith 
16939a42bb27SBarry Smith     Neighbor-wise Collective on DM
16949a42bb27SBarry Smith 
16959a42bb27SBarry Smith     Input Parameters:
16969a42bb27SBarry Smith +   dm - the DM object
1697f6813fd5SJed Brown .   l - the local vector
16989a42bb27SBarry 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
16999a42bb27SBarry Smith            base point.
1700f6813fd5SJed Brown - - the global vector
17019a42bb27SBarry Smith 
17029a42bb27SBarry 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
17039a42bb27SBarry 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
17049a42bb27SBarry Smith            global array to the final global array with VecAXPY().
17059a42bb27SBarry Smith 
17069a42bb27SBarry Smith     Level: beginner
17079a42bb27SBarry Smith 
1708e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
17099a42bb27SBarry Smith 
17109a42bb27SBarry Smith @*/
17117087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
17129a42bb27SBarry Smith {
17137128ae9fSMatthew G Knepley   PetscSF        sf;
17149a42bb27SBarry Smith   PetscErrorCode ierr;
17159a42bb27SBarry Smith 
17169a42bb27SBarry Smith   PetscFunctionBegin;
1717171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17187128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17197128ae9fSMatthew G Knepley   if (sf) {
17207128ae9fSMatthew G Knepley     MPI_Op      op;
17217128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17227128ae9fSMatthew G Knepley 
17237128ae9fSMatthew G Knepley     switch (mode) {
17247128ae9fSMatthew G Knepley     case INSERT_VALUES:
17257128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17268bfbc91cSJed Brown       op = MPIU_REPLACE; break;
17277128ae9fSMatthew G Knepley     case ADD_VALUES:
17287128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17297128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17307128ae9fSMatthew G Knepley     default:
173182f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17327128ae9fSMatthew G Knepley     }
17337128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17347128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17357128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17367128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17377128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17387128ae9fSMatthew G Knepley   } else {
1739843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17407128ae9fSMatthew G Knepley   }
17419a42bb27SBarry Smith   PetscFunctionReturn(0);
17429a42bb27SBarry Smith }
17439a42bb27SBarry Smith 
17449a42bb27SBarry Smith #undef __FUNCT__
17459a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
17469a42bb27SBarry Smith /*@
17479a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
174847c6ae99SBarry Smith 
174947c6ae99SBarry Smith     Neighbor-wise Collective on DM
175047c6ae99SBarry Smith 
175147c6ae99SBarry Smith     Input Parameters:
175247c6ae99SBarry Smith +   dm - the DM object
1753f6813fd5SJed Brown .   l - the local vector
175447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1755f6813fd5SJed Brown -   g - the global vector
175647c6ae99SBarry Smith 
175747c6ae99SBarry Smith 
175847c6ae99SBarry Smith     Level: beginner
175947c6ae99SBarry Smith 
1760e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
176147c6ae99SBarry Smith 
176247c6ae99SBarry Smith @*/
17637087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
176447c6ae99SBarry Smith {
17657128ae9fSMatthew G Knepley   PetscSF        sf;
176647c6ae99SBarry Smith   PetscErrorCode ierr;
176747c6ae99SBarry Smith 
176847c6ae99SBarry Smith   PetscFunctionBegin;
1769171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17707128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17717128ae9fSMatthew G Knepley   if (sf) {
17727128ae9fSMatthew G Knepley     MPI_Op      op;
17737128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17747128ae9fSMatthew G Knepley 
17757128ae9fSMatthew G Knepley     switch (mode) {
17767128ae9fSMatthew G Knepley     case INSERT_VALUES:
17777128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17788bfbc91cSJed Brown       op = MPIU_REPLACE; break;
17797128ae9fSMatthew G Knepley     case ADD_VALUES:
17807128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17817128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17827128ae9fSMatthew G Knepley     default:
178382f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17847128ae9fSMatthew G Knepley     }
17857128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17867128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17877128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17887128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17897128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17907128ae9fSMatthew G Knepley   } else {
1791843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17927128ae9fSMatthew G Knepley   }
179347c6ae99SBarry Smith   PetscFunctionReturn(0);
179447c6ae99SBarry Smith }
179547c6ae99SBarry Smith 
179647c6ae99SBarry Smith #undef __FUNCT__
179747c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
179847c6ae99SBarry Smith /*@
179947c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
180047c6ae99SBarry Smith 
180147c6ae99SBarry Smith     Collective on DM
180247c6ae99SBarry Smith 
180347c6ae99SBarry Smith     Input Parameter:
180447c6ae99SBarry Smith +   dm - the DM object
180591d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
180647c6ae99SBarry Smith 
180747c6ae99SBarry Smith     Output Parameter:
180847c6ae99SBarry Smith .   dmc - the coarsened DM
180947c6ae99SBarry Smith 
181047c6ae99SBarry Smith     Level: developer
181147c6ae99SBarry Smith 
1812e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
181347c6ae99SBarry Smith 
181447c6ae99SBarry Smith @*/
18157087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
181647c6ae99SBarry Smith {
181747c6ae99SBarry Smith   PetscErrorCode    ierr;
1818b17ce1afSJed Brown   DMCoarsenHookLink link;
181947c6ae99SBarry Smith 
182047c6ae99SBarry Smith   PetscFunctionBegin;
1821171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
182247c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
182343842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
18248cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1825644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
18260598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
1827656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
1828e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1829b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1830b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1831b17ce1afSJed Brown   }
1832b17ce1afSJed Brown   PetscFunctionReturn(0);
1833b17ce1afSJed Brown }
1834b17ce1afSJed Brown 
1835b17ce1afSJed Brown #undef __FUNCT__
1836b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1837bb9467b5SJed Brown /*@C
1838b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1839b17ce1afSJed Brown 
1840b17ce1afSJed Brown    Logically Collective
1841b17ce1afSJed Brown 
1842b17ce1afSJed Brown    Input Arguments:
1843b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1844b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1845b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
18460298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1847b17ce1afSJed Brown 
1848b17ce1afSJed Brown    Calling sequence of coarsenhook:
1849b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1850b17ce1afSJed Brown 
1851b17ce1afSJed Brown +  fine - fine level DM
1852b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1853b17ce1afSJed Brown -  ctx - optional user-defined function context
1854b17ce1afSJed Brown 
1855b17ce1afSJed Brown    Calling sequence for restricthook:
1856c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1857b17ce1afSJed Brown 
1858b17ce1afSJed Brown +  fine - fine level DM
1859b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1860c833c3b5SJed Brown .  rscale - scaling vector for restriction
1861c833c3b5SJed Brown .  inject - matrix restricting by injection
1862b17ce1afSJed Brown .  coarse - coarse level DM to update
1863b17ce1afSJed Brown -  ctx - optional user-defined function context
1864b17ce1afSJed Brown 
1865b17ce1afSJed Brown    Level: advanced
1866b17ce1afSJed Brown 
1867b17ce1afSJed Brown    Notes:
1868b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1869b17ce1afSJed Brown 
1870b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1871b17ce1afSJed Brown 
1872b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1873b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1874b17ce1afSJed Brown 
1875bb9467b5SJed Brown    This function is currently not available from Fortran.
1876bb9467b5SJed Brown 
1877c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1878b17ce1afSJed Brown @*/
1879b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1880b17ce1afSJed Brown {
1881b17ce1afSJed Brown   PetscErrorCode    ierr;
1882b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1883b17ce1afSJed Brown 
1884b17ce1afSJed Brown   PetscFunctionBegin;
1885b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
18866bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1887b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1888b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
1889b17ce1afSJed Brown   link->restricthook = restricthook;
1890b17ce1afSJed Brown   link->ctx          = ctx;
18910298fd71SBarry Smith   link->next         = NULL;
1892b17ce1afSJed Brown   *p                 = link;
1893b17ce1afSJed Brown   PetscFunctionReturn(0);
1894b17ce1afSJed Brown }
1895b17ce1afSJed Brown 
1896b17ce1afSJed Brown #undef __FUNCT__
1897b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1898b17ce1afSJed Brown /*@
1899b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1900b17ce1afSJed Brown 
1901b17ce1afSJed Brown    Collective if any hooks are
1902b17ce1afSJed Brown 
1903b17ce1afSJed Brown    Input Arguments:
1904b17ce1afSJed Brown +  fine - finer DM to use as a base
1905b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1906b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1907b17ce1afSJed Brown -  coarse - coarer DM to update
1908b17ce1afSJed Brown 
1909b17ce1afSJed Brown    Level: developer
1910b17ce1afSJed Brown 
1911b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1912b17ce1afSJed Brown @*/
1913b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1914b17ce1afSJed Brown {
1915b17ce1afSJed Brown   PetscErrorCode    ierr;
1916b17ce1afSJed Brown   DMCoarsenHookLink link;
1917b17ce1afSJed Brown 
1918b17ce1afSJed Brown   PetscFunctionBegin;
1919b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
19208865f1eaSKarl Rupp     if (link->restricthook) {
19218865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
19228865f1eaSKarl Rupp     }
1923b17ce1afSJed Brown   }
192447c6ae99SBarry Smith   PetscFunctionReturn(0);
192547c6ae99SBarry Smith }
192647c6ae99SBarry Smith 
192747c6ae99SBarry Smith #undef __FUNCT__
1928be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
1929bb9467b5SJed Brown /*@C
1930be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
19315dbd56e3SPeter Brune 
19325dbd56e3SPeter Brune    Logically Collective
19335dbd56e3SPeter Brune 
19345dbd56e3SPeter Brune    Input Arguments:
19355dbd56e3SPeter Brune +  global - global DM
1936ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
19375dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
19380298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19395dbd56e3SPeter Brune 
1940ec4806b8SPeter Brune 
1941ec4806b8SPeter Brune    Calling sequence for ddhook:
1942ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
1943ec4806b8SPeter Brune 
1944ec4806b8SPeter Brune +  global - global DM
1945ec4806b8SPeter Brune .  block  - block DM
1946ec4806b8SPeter Brune -  ctx - optional user-defined function context
1947ec4806b8SPeter Brune 
19485dbd56e3SPeter Brune    Calling sequence for restricthook:
1949ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
19505dbd56e3SPeter Brune 
19515dbd56e3SPeter Brune +  global - global DM
19525dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
19535dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
1954ec4806b8SPeter Brune .  block  - block DM
19555dbd56e3SPeter Brune -  ctx - optional user-defined function context
19565dbd56e3SPeter Brune 
19575dbd56e3SPeter Brune    Level: advanced
19585dbd56e3SPeter Brune 
19595dbd56e3SPeter Brune    Notes:
1960ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
19615dbd56e3SPeter Brune 
19625dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
19635dbd56e3SPeter Brune 
19645dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1965ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
19665dbd56e3SPeter Brune 
1967bb9467b5SJed Brown    This function is currently not available from Fortran.
1968bb9467b5SJed Brown 
19695dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19705dbd56e3SPeter Brune @*/
1971be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
19725dbd56e3SPeter Brune {
19735dbd56e3SPeter Brune   PetscErrorCode      ierr;
1974be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
19755dbd56e3SPeter Brune 
19765dbd56e3SPeter Brune   PetscFunctionBegin;
19775dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
1978be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1979be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
19805dbd56e3SPeter Brune   link->restricthook = restricthook;
1981be081cd6SPeter Brune   link->ddhook       = ddhook;
19825dbd56e3SPeter Brune   link->ctx          = ctx;
19830298fd71SBarry Smith   link->next         = NULL;
19845dbd56e3SPeter Brune   *p                 = link;
19855dbd56e3SPeter Brune   PetscFunctionReturn(0);
19865dbd56e3SPeter Brune }
19875dbd56e3SPeter Brune 
19885dbd56e3SPeter Brune #undef __FUNCT__
1989be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
19905dbd56e3SPeter Brune /*@
1991be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
19925dbd56e3SPeter Brune 
19935dbd56e3SPeter Brune    Collective if any hooks are
19945dbd56e3SPeter Brune 
19955dbd56e3SPeter Brune    Input Arguments:
19965dbd56e3SPeter Brune +  fine - finer DM to use as a base
1997be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
1998be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
19995dbd56e3SPeter Brune -  coarse - coarer DM to update
20005dbd56e3SPeter Brune 
20015dbd56e3SPeter Brune    Level: developer
20025dbd56e3SPeter Brune 
20035dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
20045dbd56e3SPeter Brune @*/
2005be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
20065dbd56e3SPeter Brune {
20075dbd56e3SPeter Brune   PetscErrorCode      ierr;
2008be081cd6SPeter Brune   DMSubDomainHookLink link;
20095dbd56e3SPeter Brune 
20105dbd56e3SPeter Brune   PetscFunctionBegin;
2011be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
20128865f1eaSKarl Rupp     if (link->restricthook) {
20138865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
20148865f1eaSKarl Rupp     }
20155dbd56e3SPeter Brune   }
20165dbd56e3SPeter Brune   PetscFunctionReturn(0);
20175dbd56e3SPeter Brune }
20185dbd56e3SPeter Brune 
20195dbd56e3SPeter Brune #undef __FUNCT__
20205fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
20215fe1f584SPeter Brune /*@
20226a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
20235fe1f584SPeter Brune 
20245fe1f584SPeter Brune     Not Collective
20255fe1f584SPeter Brune 
20265fe1f584SPeter Brune     Input Parameter:
20275fe1f584SPeter Brune .   dm - the DM object
20285fe1f584SPeter Brune 
20295fe1f584SPeter Brune     Output Parameter:
20306a7d9d85SPeter Brune .   level - number of coarsenings
20315fe1f584SPeter Brune 
20325fe1f584SPeter Brune     Level: developer
20335fe1f584SPeter Brune 
20346a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
20355fe1f584SPeter Brune 
20365fe1f584SPeter Brune @*/
20375fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
20385fe1f584SPeter Brune {
20395fe1f584SPeter Brune   PetscFunctionBegin;
20405fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20415fe1f584SPeter Brune   *level = dm->leveldown;
20425fe1f584SPeter Brune   PetscFunctionReturn(0);
20435fe1f584SPeter Brune }
20445fe1f584SPeter Brune 
20455fe1f584SPeter Brune 
20465fe1f584SPeter Brune 
20475fe1f584SPeter Brune #undef __FUNCT__
204847c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
204947c6ae99SBarry Smith /*@C
205047c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
205147c6ae99SBarry Smith 
205247c6ae99SBarry Smith     Collective on DM
205347c6ae99SBarry Smith 
205447c6ae99SBarry Smith     Input Parameter:
205547c6ae99SBarry Smith +   dm - the DM object
205647c6ae99SBarry Smith -   nlevels - the number of levels of refinement
205747c6ae99SBarry Smith 
205847c6ae99SBarry Smith     Output Parameter:
205947c6ae99SBarry Smith .   dmf - the refined DM hierarchy
206047c6ae99SBarry Smith 
206147c6ae99SBarry Smith     Level: developer
206247c6ae99SBarry Smith 
2063e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
206447c6ae99SBarry Smith 
206547c6ae99SBarry Smith @*/
20667087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
206747c6ae99SBarry Smith {
206847c6ae99SBarry Smith   PetscErrorCode ierr;
206947c6ae99SBarry Smith 
207047c6ae99SBarry Smith   PetscFunctionBegin;
2071171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2072ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
207347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
207447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
207547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
207647c6ae99SBarry Smith   } else if (dm->ops->refine) {
207747c6ae99SBarry Smith     PetscInt i;
207847c6ae99SBarry Smith 
2079ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
208047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2081ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
208247c6ae99SBarry Smith     }
2083ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
208447c6ae99SBarry Smith   PetscFunctionReturn(0);
208547c6ae99SBarry Smith }
208647c6ae99SBarry Smith 
208747c6ae99SBarry Smith #undef __FUNCT__
208847c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
208947c6ae99SBarry Smith /*@C
209047c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
209147c6ae99SBarry Smith 
209247c6ae99SBarry Smith     Collective on DM
209347c6ae99SBarry Smith 
209447c6ae99SBarry Smith     Input Parameter:
209547c6ae99SBarry Smith +   dm - the DM object
209647c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
209747c6ae99SBarry Smith 
209847c6ae99SBarry Smith     Output Parameter:
209947c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
210047c6ae99SBarry Smith 
210147c6ae99SBarry Smith     Level: developer
210247c6ae99SBarry Smith 
2103e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
210447c6ae99SBarry Smith 
210547c6ae99SBarry Smith @*/
21067087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
210747c6ae99SBarry Smith {
210847c6ae99SBarry Smith   PetscErrorCode ierr;
210947c6ae99SBarry Smith 
211047c6ae99SBarry Smith   PetscFunctionBegin;
2111171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2112ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
211347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
211447c6ae99SBarry Smith   PetscValidPointer(dmc,3);
211547c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
211647c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
211747c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
211847c6ae99SBarry Smith     PetscInt i;
211947c6ae99SBarry Smith 
2120ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
212147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2122ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
212347c6ae99SBarry Smith     }
2124ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
212547c6ae99SBarry Smith   PetscFunctionReturn(0);
212647c6ae99SBarry Smith }
212747c6ae99SBarry Smith 
212847c6ae99SBarry Smith #undef __FUNCT__
2129e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
213047c6ae99SBarry Smith /*@
2131e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
213247c6ae99SBarry Smith    grids associated with two DMs.
213347c6ae99SBarry Smith 
213447c6ae99SBarry Smith    Collective on DM
213547c6ae99SBarry Smith 
213647c6ae99SBarry Smith    Input Parameters:
213747c6ae99SBarry Smith +  dmc - the coarse grid DM
213847c6ae99SBarry Smith -  dmf - the fine grid DM
213947c6ae99SBarry Smith 
214047c6ae99SBarry Smith    Output Parameters:
214147c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
214247c6ae99SBarry Smith 
214347c6ae99SBarry Smith    Level: intermediate
214447c6ae99SBarry Smith 
214547c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
214647c6ae99SBarry Smith 
2147e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
214847c6ae99SBarry Smith @*/
2149e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
215047c6ae99SBarry Smith {
215147c6ae99SBarry Smith   PetscErrorCode ierr;
215247c6ae99SBarry Smith 
215347c6ae99SBarry Smith   PetscFunctionBegin;
2154171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2155171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
215647c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
215747c6ae99SBarry Smith   PetscFunctionReturn(0);
215847c6ae99SBarry Smith }
215947c6ae99SBarry Smith 
216047c6ae99SBarry Smith #undef __FUNCT__
21611a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
21621a266240SBarry Smith /*@C
21631a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
21641a266240SBarry Smith 
21651a266240SBarry Smith     Not Collective
21661a266240SBarry Smith 
21671a266240SBarry Smith     Input Parameters:
21681a266240SBarry Smith +   dm - the DM object
21691a266240SBarry Smith -   destroy - the destroy function
21701a266240SBarry Smith 
21711a266240SBarry Smith     Level: intermediate
21721a266240SBarry Smith 
2173e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
21741a266240SBarry Smith 
2175f07f9ceaSJed Brown @*/
21761a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
21771a266240SBarry Smith {
21781a266240SBarry Smith   PetscFunctionBegin;
2179171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21801a266240SBarry Smith   dm->ctxdestroy = destroy;
21811a266240SBarry Smith   PetscFunctionReturn(0);
21821a266240SBarry Smith }
21831a266240SBarry Smith 
21841a266240SBarry Smith #undef __FUNCT__
21851b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2186b07ff414SBarry Smith /*@
21871b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
218847c6ae99SBarry Smith 
218947c6ae99SBarry Smith     Not Collective
219047c6ae99SBarry Smith 
219147c6ae99SBarry Smith     Input Parameters:
219247c6ae99SBarry Smith +   dm - the DM object
219347c6ae99SBarry Smith -   ctx - the user context
219447c6ae99SBarry Smith 
219547c6ae99SBarry Smith     Level: intermediate
219647c6ae99SBarry Smith 
2197e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
219847c6ae99SBarry Smith 
219947c6ae99SBarry Smith @*/
22001b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
220147c6ae99SBarry Smith {
220247c6ae99SBarry Smith   PetscFunctionBegin;
2203171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
220447c6ae99SBarry Smith   dm->ctx = ctx;
220547c6ae99SBarry Smith   PetscFunctionReturn(0);
220647c6ae99SBarry Smith }
220747c6ae99SBarry Smith 
220847c6ae99SBarry Smith #undef __FUNCT__
22091b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
221047c6ae99SBarry Smith /*@
22111b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
221247c6ae99SBarry Smith 
221347c6ae99SBarry Smith     Not Collective
221447c6ae99SBarry Smith 
221547c6ae99SBarry Smith     Input Parameter:
221647c6ae99SBarry Smith .   dm - the DM object
221747c6ae99SBarry Smith 
221847c6ae99SBarry Smith     Output Parameter:
221947c6ae99SBarry Smith .   ctx - the user context
222047c6ae99SBarry Smith 
222147c6ae99SBarry Smith     Level: intermediate
222247c6ae99SBarry Smith 
2223e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
222447c6ae99SBarry Smith 
222547c6ae99SBarry Smith @*/
22261b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
222747c6ae99SBarry Smith {
222847c6ae99SBarry Smith   PetscFunctionBegin;
2229171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22301b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
223147c6ae99SBarry Smith   PetscFunctionReturn(0);
223247c6ae99SBarry Smith }
223347c6ae99SBarry Smith 
223447c6ae99SBarry Smith #undef __FUNCT__
223508da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
223608da532bSDmitry Karpeev /*@C
223708da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
223808da532bSDmitry Karpeev 
223908da532bSDmitry Karpeev     Logically Collective on DM
224008da532bSDmitry Karpeev 
224108da532bSDmitry Karpeev     Input Parameter:
224208da532bSDmitry Karpeev +   dm - the DM object
22430298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
224408da532bSDmitry Karpeev 
224508da532bSDmitry Karpeev     Level: intermediate
224608da532bSDmitry Karpeev 
2247835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
224808da532bSDmitry Karpeev          DMSetJacobian()
224908da532bSDmitry Karpeev 
225008da532bSDmitry Karpeev @*/
225108da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
225208da532bSDmitry Karpeev {
225308da532bSDmitry Karpeev   PetscFunctionBegin;
225408da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
225508da532bSDmitry Karpeev   PetscFunctionReturn(0);
225608da532bSDmitry Karpeev }
225708da532bSDmitry Karpeev 
225808da532bSDmitry Karpeev #undef __FUNCT__
225908da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
226008da532bSDmitry Karpeev /*@
226108da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
226208da532bSDmitry Karpeev 
226308da532bSDmitry Karpeev     Not Collective
226408da532bSDmitry Karpeev 
226508da532bSDmitry Karpeev     Input Parameter:
226608da532bSDmitry Karpeev .   dm - the DM object to destroy
226708da532bSDmitry Karpeev 
226808da532bSDmitry Karpeev     Output Parameter:
226908da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
227008da532bSDmitry Karpeev 
227108da532bSDmitry Karpeev     Level: developer
227208da532bSDmitry Karpeev 
227374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
227408da532bSDmitry Karpeev 
227508da532bSDmitry Karpeev @*/
227608da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
227708da532bSDmitry Karpeev {
227808da532bSDmitry Karpeev   PetscFunctionBegin;
227908da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
228008da532bSDmitry Karpeev   PetscFunctionReturn(0);
228108da532bSDmitry Karpeev }
228208da532bSDmitry Karpeev 
228308da532bSDmitry Karpeev #undef __FUNCT__
228408da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
228508da532bSDmitry Karpeev /*@C
228608da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
228708da532bSDmitry Karpeev 
228808da532bSDmitry Karpeev     Logically Collective on DM
228908da532bSDmitry Karpeev 
229008da532bSDmitry Karpeev     Input Parameters:
229108da532bSDmitry Karpeev +   dm - the DM object to destroy
229208da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
229308da532bSDmitry Karpeev 
229408da532bSDmitry Karpeev     Output parameters:
229508da532bSDmitry Karpeev +   xl - lower bound
229608da532bSDmitry Karpeev -   xu - upper bound
229708da532bSDmitry Karpeev 
229808da532bSDmitry Karpeev     Level: intermediate
229908da532bSDmitry Karpeev 
230074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
230108da532bSDmitry Karpeev 
230208da532bSDmitry Karpeev @*/
230308da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
230408da532bSDmitry Karpeev {
230508da532bSDmitry Karpeev   PetscErrorCode ierr;
23065fd66863SKarl Rupp 
230708da532bSDmitry Karpeev   PetscFunctionBegin;
230808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
230908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
231008da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
231108da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
23128865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
231308da532bSDmitry Karpeev   PetscFunctionReturn(0);
231408da532bSDmitry Karpeev }
231508da532bSDmitry Karpeev 
231608da532bSDmitry Karpeev #undef __FUNCT__
2317b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2318b0ae01b7SPeter Brune /*@
2319b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2320b0ae01b7SPeter Brune 
2321b0ae01b7SPeter Brune     Not Collective
2322b0ae01b7SPeter Brune 
2323b0ae01b7SPeter Brune     Input Parameter:
2324b0ae01b7SPeter Brune .   dm - the DM object
2325b0ae01b7SPeter Brune 
2326b0ae01b7SPeter Brune     Output Parameter:
2327b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2328b0ae01b7SPeter Brune 
2329b0ae01b7SPeter Brune     Level: developer
2330b0ae01b7SPeter Brune 
2331b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2332b0ae01b7SPeter Brune 
2333b0ae01b7SPeter Brune @*/
2334b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2335b0ae01b7SPeter Brune {
2336b0ae01b7SPeter Brune   PetscFunctionBegin;
2337b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2338b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2339b0ae01b7SPeter Brune }
2340b0ae01b7SPeter Brune 
2341b0ae01b7SPeter Brune #undef  __FUNCT__
234208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2343748fac09SDmitry Karpeev /*@C
234408da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
234508da532bSDmitry Karpeev 
234608da532bSDmitry Karpeev     Collective on DM
234708da532bSDmitry Karpeev 
234808da532bSDmitry Karpeev     Input Parameter:
234908da532bSDmitry Karpeev +   dm - the DM object
23500298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
235108da532bSDmitry Karpeev 
235208da532bSDmitry Karpeev     Level: developer
235308da532bSDmitry Karpeev 
235474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
235508da532bSDmitry Karpeev 
235608da532bSDmitry Karpeev @*/
235708da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
235808da532bSDmitry Karpeev {
235908da532bSDmitry Karpeev   PetscErrorCode ierr;
23605fd66863SKarl Rupp 
236108da532bSDmitry Karpeev   PetscFunctionBegin;
236208da532bSDmitry Karpeev   if (x) {
236308da532bSDmitry Karpeev     if (!dm->x) {
236408da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
236508da532bSDmitry Karpeev     }
236608da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
23678865f1eaSKarl Rupp   } else if (dm->x) {
236808da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
236908da532bSDmitry Karpeev   }
237008da532bSDmitry Karpeev   PetscFunctionReturn(0);
237108da532bSDmitry Karpeev }
237208da532bSDmitry Karpeev 
23730298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2374264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2375264ace61SBarry Smith 
2376264ace61SBarry Smith #undef __FUNCT__
2377264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2378264ace61SBarry Smith /*@C
2379264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2380264ace61SBarry Smith 
2381264ace61SBarry Smith   Collective on DM
2382264ace61SBarry Smith 
2383264ace61SBarry Smith   Input Parameters:
2384264ace61SBarry Smith + dm     - The DM object
2385264ace61SBarry Smith - method - The name of the DM type
2386264ace61SBarry Smith 
2387264ace61SBarry Smith   Options Database Key:
2388264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2389264ace61SBarry Smith 
2390264ace61SBarry Smith   Notes:
2391e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2392264ace61SBarry Smith 
2393264ace61SBarry Smith   Level: intermediate
2394264ace61SBarry Smith 
2395264ace61SBarry Smith .keywords: DM, set, type
2396264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2397264ace61SBarry Smith @*/
239819fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2399264ace61SBarry Smith {
2400264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2401264ace61SBarry Smith   PetscBool      match;
2402264ace61SBarry Smith   PetscErrorCode ierr;
2403264ace61SBarry Smith 
2404264ace61SBarry Smith   PetscFunctionBegin;
2405264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2406251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2407264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2408264ace61SBarry Smith 
2409607a6623SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll();CHKERRQ(ierr);}
24101c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2411ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2412264ace61SBarry Smith 
2413264ace61SBarry Smith   if (dm->ops->destroy) {
2414264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
24150298fd71SBarry Smith     dm->ops->destroy = NULL;
2416264ace61SBarry Smith   }
2417264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2418264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2419264ace61SBarry Smith   PetscFunctionReturn(0);
2420264ace61SBarry Smith }
2421264ace61SBarry Smith 
2422264ace61SBarry Smith #undef __FUNCT__
2423264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2424264ace61SBarry Smith /*@C
2425264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2426264ace61SBarry Smith 
2427264ace61SBarry Smith   Not Collective
2428264ace61SBarry Smith 
2429264ace61SBarry Smith   Input Parameter:
2430264ace61SBarry Smith . dm  - The DM
2431264ace61SBarry Smith 
2432264ace61SBarry Smith   Output Parameter:
2433264ace61SBarry Smith . type - The DM type name
2434264ace61SBarry Smith 
2435264ace61SBarry Smith   Level: intermediate
2436264ace61SBarry Smith 
2437264ace61SBarry Smith .keywords: DM, get, type, name
2438264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2439264ace61SBarry Smith @*/
244019fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2441264ace61SBarry Smith {
2442264ace61SBarry Smith   PetscErrorCode ierr;
2443264ace61SBarry Smith 
2444264ace61SBarry Smith   PetscFunctionBegin;
2445264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2446264ace61SBarry Smith   PetscValidCharPointer(type,2);
2447264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2448607a6623SBarry Smith     ierr = DMRegisterAll();CHKERRQ(ierr);
2449264ace61SBarry Smith   }
2450264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2451264ace61SBarry Smith   PetscFunctionReturn(0);
2452264ace61SBarry Smith }
2453264ace61SBarry Smith 
245467a56275SMatthew G Knepley #undef __FUNCT__
245567a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
245667a56275SMatthew G Knepley /*@C
245767a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
245867a56275SMatthew G Knepley 
245967a56275SMatthew G Knepley   Collective on DM
246067a56275SMatthew G Knepley 
246167a56275SMatthew G Knepley   Input Parameters:
246267a56275SMatthew G Knepley + dm - the DM
246367a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
246467a56275SMatthew G Knepley 
246567a56275SMatthew G Knepley   Output Parameter:
246667a56275SMatthew G Knepley . M - pointer to new DM
246767a56275SMatthew G Knepley 
246867a56275SMatthew G Knepley   Notes:
246967a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
247067a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
247167a56275SMatthew G Knepley   of the input DM.
247267a56275SMatthew G Knepley 
247367a56275SMatthew G Knepley   Level: intermediate
247467a56275SMatthew G Knepley 
247567a56275SMatthew G Knepley .seealso: DMCreate()
247667a56275SMatthew G Knepley @*/
247719fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
247867a56275SMatthew G Knepley {
247967a56275SMatthew G Knepley   DM             B;
248067a56275SMatthew G Knepley   char           convname[256];
248167a56275SMatthew G Knepley   PetscBool      sametype, issame;
248267a56275SMatthew G Knepley   PetscErrorCode ierr;
248367a56275SMatthew G Knepley 
248467a56275SMatthew G Knepley   PetscFunctionBegin;
248567a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
248667a56275SMatthew G Knepley   PetscValidType(dm,1);
248767a56275SMatthew G Knepley   PetscValidPointer(M,3);
2488251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
248967a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
249067a56275SMatthew G Knepley   {
24910298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
249267a56275SMatthew G Knepley 
249367a56275SMatthew G Knepley     /*
249467a56275SMatthew G Knepley        Order of precedence:
249567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
249667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
249767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
249867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
249967a56275SMatthew G Knepley        5) Use a really basic converter.
250067a56275SMatthew G Knepley     */
250167a56275SMatthew G Knepley 
250267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
250367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
250467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
250567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
250667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
250767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
25080005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
250967a56275SMatthew G Knepley     if (conv) goto foundconv;
251067a56275SMatthew G Knepley 
251167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
251282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
251367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
251467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
251567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
251667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
251767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
251867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
25190005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
252067a56275SMatthew G Knepley     if (conv) {
2521fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
252267a56275SMatthew G Knepley       goto foundconv;
252367a56275SMatthew G Knepley     }
252467a56275SMatthew G Knepley 
252567a56275SMatthew G Knepley #if 0
252667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
252767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2528fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
252967a56275SMatthew G Knepley     if (conv) goto foundconv;
253067a56275SMatthew G Knepley 
253167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
253267a56275SMatthew G Knepley     if (dm->ops->convert) {
253367a56275SMatthew G Knepley       conv = dm->ops->convert;
253467a56275SMatthew G Knepley     }
253567a56275SMatthew G Knepley     if (conv) goto foundconv;
253667a56275SMatthew G Knepley #endif
253767a56275SMatthew G Knepley 
253867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
253982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
254067a56275SMatthew G Knepley 
254167a56275SMatthew G Knepley foundconv:
254267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
254367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
254467a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
254567a56275SMatthew G Knepley   }
254667a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
254767a56275SMatthew G Knepley   PetscFunctionReturn(0);
254867a56275SMatthew G Knepley }
2549264ace61SBarry Smith 
2550264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2551264ace61SBarry Smith 
2552264ace61SBarry Smith #undef __FUNCT__
2553264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2554264ace61SBarry Smith /*@C
25551c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
25561c84c290SBarry Smith 
25571c84c290SBarry Smith   Not Collective
25581c84c290SBarry Smith 
25591c84c290SBarry Smith   Input Parameters:
25601c84c290SBarry Smith + name        - The name of a new user-defined creation routine
25611c84c290SBarry Smith - create_func - The creation routine itself
25621c84c290SBarry Smith 
25631c84c290SBarry Smith   Notes:
25641c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
25651c84c290SBarry Smith 
25661c84c290SBarry Smith 
25671c84c290SBarry Smith   Sample usage:
25681c84c290SBarry Smith .vb
2569bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
25701c84c290SBarry Smith .ve
25711c84c290SBarry Smith 
25721c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
25731c84c290SBarry Smith .vb
25741c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
25751c84c290SBarry Smith     DMSetType(DM,"my_da");
25761c84c290SBarry Smith .ve
25771c84c290SBarry Smith    or at runtime via the option
25781c84c290SBarry Smith .vb
25791c84c290SBarry Smith     -da_type my_da
25801c84c290SBarry Smith .ve
2581264ace61SBarry Smith 
2582264ace61SBarry Smith   Level: advanced
25831c84c290SBarry Smith 
25841c84c290SBarry Smith .keywords: DM, register
2585bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
25861c84c290SBarry Smith 
2587264ace61SBarry Smith @*/
2588bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2589264ace61SBarry Smith {
2590264ace61SBarry Smith   PetscErrorCode ierr;
2591264ace61SBarry Smith 
2592264ace61SBarry Smith   PetscFunctionBegin;
2593a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2594264ace61SBarry Smith   PetscFunctionReturn(0);
2595264ace61SBarry Smith }
2596264ace61SBarry Smith 
2597b859378eSBarry Smith #undef __FUNCT__
2598b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2599b859378eSBarry Smith /*@C
260055849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2601b859378eSBarry Smith 
2602b859378eSBarry Smith   Collective on PetscViewer
2603b859378eSBarry Smith 
2604b859378eSBarry Smith   Input Parameters:
2605b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2606b859378eSBarry Smith            some related function before a call to DMLoad().
2607b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2608b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2609b859378eSBarry Smith 
2610b859378eSBarry Smith    Level: intermediate
2611b859378eSBarry Smith 
2612b859378eSBarry Smith   Notes:
261355849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2614b859378eSBarry Smith 
2615b859378eSBarry Smith   Notes for advanced users:
2616b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2617b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2618b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2619b859378eSBarry Smith   format is
2620b859378eSBarry Smith .vb
2621b859378eSBarry Smith      has not yet been determined
2622b859378eSBarry Smith .ve
2623b859378eSBarry Smith 
2624b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2625b859378eSBarry Smith @*/
2626b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2627b859378eSBarry Smith {
2628b859378eSBarry Smith   PetscErrorCode ierr;
262932c0f0efSBarry Smith   PetscBool      isbinary;
263055849f57SBarry Smith   PetscInt       classid;
263132c0f0efSBarry Smith   char           type[256];
2632b859378eSBarry Smith 
2633b859378eSBarry Smith   PetscFunctionBegin;
2634b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2635b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
263632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
263732c0f0efSBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
2638b859378eSBarry Smith 
263932c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
2640ce94432eSBarry Smith   if (classid != DM_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file");
264132c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
264232c0f0efSBarry Smith   ierr = DMSetType(newdm, type);CHKERRQ(ierr);
26432d53ad75SBarry Smith   if (newdm->ops->load) {
2644b859378eSBarry Smith     ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
26452d53ad75SBarry Smith   }
2646b859378eSBarry Smith   PetscFunctionReturn(0);
2647b859378eSBarry Smith }
2648b859378eSBarry Smith 
26497da65231SMatthew G Knepley /******************************** FEM Support **********************************/
26507da65231SMatthew G Knepley 
26517da65231SMatthew G Knepley #undef __FUNCT__
26527da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
2653a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
2654a6dfd86eSKarl Rupp {
26551d47ebbbSSatish Balay   PetscInt       f;
26561b30c384SMatthew G Knepley   PetscErrorCode ierr;
26571b30c384SMatthew G Knepley 
26587da65231SMatthew G Knepley   PetscFunctionBegin;
265974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26601d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
266174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
26627da65231SMatthew G Knepley   }
26637da65231SMatthew G Knepley   PetscFunctionReturn(0);
26647da65231SMatthew G Knepley }
26657da65231SMatthew G Knepley 
26667da65231SMatthew G Knepley #undef __FUNCT__
26677da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
2668a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
2669a6dfd86eSKarl Rupp {
26701b30c384SMatthew G Knepley   PetscInt       f, g;
26717da65231SMatthew G Knepley   PetscErrorCode ierr;
26727da65231SMatthew G Knepley 
26737da65231SMatthew G Knepley   PetscFunctionBegin;
267474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26751d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
267674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
26771d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
267874778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
26797da65231SMatthew G Knepley     }
268074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
26817da65231SMatthew G Knepley   }
26827da65231SMatthew G Knepley   PetscFunctionReturn(0);
26837da65231SMatthew G Knepley }
2684e7c4fc90SDmitry Karpeev 
2685970e74d5SMatthew G Knepley #undef __FUNCT__
268688ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
268788ed4aceSMatthew G Knepley /*@
268888ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
268988ed4aceSMatthew G Knepley 
269088ed4aceSMatthew G Knepley   Input Parameter:
269188ed4aceSMatthew G Knepley . dm - The DM
269288ed4aceSMatthew G Knepley 
269388ed4aceSMatthew G Knepley   Output Parameter:
269488ed4aceSMatthew G Knepley . section - The PetscSection
269588ed4aceSMatthew G Knepley 
269688ed4aceSMatthew G Knepley   Level: intermediate
269788ed4aceSMatthew G Knepley 
269888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
269988ed4aceSMatthew G Knepley 
270088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
270188ed4aceSMatthew G Knepley @*/
27020adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
27030adebc6cSBarry Smith {
270488ed4aceSMatthew G Knepley   PetscFunctionBegin;
270588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
270688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
270788ed4aceSMatthew G Knepley   *section = dm->defaultSection;
270888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
270988ed4aceSMatthew G Knepley }
271088ed4aceSMatthew G Knepley 
271188ed4aceSMatthew G Knepley #undef __FUNCT__
271288ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
271388ed4aceSMatthew G Knepley /*@
271488ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
271588ed4aceSMatthew G Knepley 
271688ed4aceSMatthew G Knepley   Input Parameters:
271788ed4aceSMatthew G Knepley + dm - The DM
271888ed4aceSMatthew G Knepley - section - The PetscSection
271988ed4aceSMatthew G Knepley 
272088ed4aceSMatthew G Knepley   Level: intermediate
272188ed4aceSMatthew G Knepley 
272288ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
272388ed4aceSMatthew G Knepley 
272488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
272588ed4aceSMatthew G Knepley @*/
27260adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
27270adebc6cSBarry Smith {
2728af122d2aSMatthew G Knepley   PetscInt       numFields;
2729af122d2aSMatthew G Knepley   PetscInt       f;
273088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
273188ed4aceSMatthew G Knepley 
273288ed4aceSMatthew G Knepley   PetscFunctionBegin;
273388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27341d799100SJed Brown   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
27351d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
273688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
273788ed4aceSMatthew G Knepley   dm->defaultSection = section;
2738af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
2739af122d2aSMatthew G Knepley   if (numFields) {
2740af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
2741af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
2742af122d2aSMatthew G Knepley       const char *name;
2743af122d2aSMatthew G Knepley 
2744af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
2745af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
2746af122d2aSMatthew G Knepley     }
2747af122d2aSMatthew G Knepley   }
27481d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
27491d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
275088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
275188ed4aceSMatthew G Knepley }
275288ed4aceSMatthew G Knepley 
275388ed4aceSMatthew G Knepley #undef __FUNCT__
275488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
275588ed4aceSMatthew G Knepley /*@
275688ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
275788ed4aceSMatthew G Knepley 
27588b1ab98fSJed Brown   Collective on DM
27598b1ab98fSJed Brown 
276088ed4aceSMatthew G Knepley   Input Parameter:
276188ed4aceSMatthew G Knepley . dm - The DM
276288ed4aceSMatthew G Knepley 
276388ed4aceSMatthew G Knepley   Output Parameter:
276488ed4aceSMatthew G Knepley . section - The PetscSection
276588ed4aceSMatthew G Knepley 
276688ed4aceSMatthew G Knepley   Level: intermediate
276788ed4aceSMatthew G Knepley 
276888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
276988ed4aceSMatthew G Knepley 
277088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
277188ed4aceSMatthew G Knepley @*/
27720adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
27730adebc6cSBarry Smith {
277488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
277588ed4aceSMatthew G Knepley 
277688ed4aceSMatthew G Knepley   PetscFunctionBegin;
277788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
277888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
277988ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
278082f516ccSBarry Smith     if (!dm->defaultSection || !dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection and PetscSF in order to create a global PetscSection");
2781b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
2782cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
2783ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm),dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr);
278488ed4aceSMatthew G Knepley   }
278588ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
278688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
278788ed4aceSMatthew G Knepley }
278888ed4aceSMatthew G Knepley 
278988ed4aceSMatthew G Knepley #undef __FUNCT__
2790b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
2791b21d0597SMatthew G Knepley /*@
2792b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
2793b21d0597SMatthew G Knepley 
2794b21d0597SMatthew G Knepley   Input Parameters:
2795b21d0597SMatthew G Knepley + dm - The DM
27965080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
2797b21d0597SMatthew G Knepley 
2798b21d0597SMatthew G Knepley   Level: intermediate
2799b21d0597SMatthew G Knepley 
2800b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
2801b21d0597SMatthew G Knepley 
2802b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
2803b21d0597SMatthew G Knepley @*/
28040adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
28050adebc6cSBarry Smith {
2806b21d0597SMatthew G Knepley   PetscErrorCode ierr;
2807b21d0597SMatthew G Knepley 
2808b21d0597SMatthew G Knepley   PetscFunctionBegin;
2809b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28105080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
28111d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
2812b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
2813b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
2814b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2815b21d0597SMatthew G Knepley }
2816b21d0597SMatthew G Knepley 
2817b21d0597SMatthew G Knepley #undef __FUNCT__
281888ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
281988ed4aceSMatthew G Knepley /*@
282088ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
282188ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
282288ed4aceSMatthew G Knepley 
282388ed4aceSMatthew G Knepley   Input Parameter:
282488ed4aceSMatthew G Knepley . dm - The DM
282588ed4aceSMatthew G Knepley 
282688ed4aceSMatthew G Knepley   Output Parameter:
282788ed4aceSMatthew G Knepley . sf - The PetscSF
282888ed4aceSMatthew G Knepley 
282988ed4aceSMatthew G Knepley   Level: intermediate
283088ed4aceSMatthew G Knepley 
283188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
283288ed4aceSMatthew G Knepley 
283388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
283488ed4aceSMatthew G Knepley @*/
28350adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
28360adebc6cSBarry Smith {
283788ed4aceSMatthew G Knepley   PetscInt       nroots;
283888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
283988ed4aceSMatthew G Knepley 
284088ed4aceSMatthew G Knepley   PetscFunctionBegin;
284188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
284288ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
28430298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
284488ed4aceSMatthew G Knepley   if (nroots < 0) {
284588ed4aceSMatthew G Knepley     PetscSection section, gSection;
284688ed4aceSMatthew G Knepley 
284788ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
284831ea6d37SMatthew G Knepley     if (section) {
284988ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
285088ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
285131ea6d37SMatthew G Knepley     } else {
28520298fd71SBarry Smith       *sf = NULL;
285331ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
285431ea6d37SMatthew G Knepley     }
285588ed4aceSMatthew G Knepley   }
285688ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
285788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
285888ed4aceSMatthew G Knepley }
285988ed4aceSMatthew G Knepley 
286088ed4aceSMatthew G Knepley #undef __FUNCT__
286188ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
286288ed4aceSMatthew G Knepley /*@
286388ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
286488ed4aceSMatthew G Knepley 
286588ed4aceSMatthew G Knepley   Input Parameters:
286688ed4aceSMatthew G Knepley + dm - The DM
286788ed4aceSMatthew G Knepley - sf - The PetscSF
286888ed4aceSMatthew G Knepley 
286988ed4aceSMatthew G Knepley   Level: intermediate
287088ed4aceSMatthew G Knepley 
287188ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
287288ed4aceSMatthew G Knepley 
287388ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
287488ed4aceSMatthew G Knepley @*/
28750adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
28760adebc6cSBarry Smith {
287788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
287888ed4aceSMatthew G Knepley 
287988ed4aceSMatthew G Knepley   PetscFunctionBegin;
288088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
288188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
288288ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
288388ed4aceSMatthew G Knepley   dm->defaultSF = sf;
288488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
288588ed4aceSMatthew G Knepley }
288688ed4aceSMatthew G Knepley 
288788ed4aceSMatthew G Knepley #undef __FUNCT__
288888ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
288988ed4aceSMatthew G Knepley /*@C
289088ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
289188ed4aceSMatthew G Knepley   describing the data layout.
289288ed4aceSMatthew G Knepley 
289388ed4aceSMatthew G Knepley   Input Parameters:
289488ed4aceSMatthew G Knepley + dm - The DM
289588ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
289688ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
289788ed4aceSMatthew G Knepley 
289888ed4aceSMatthew G Knepley   Level: intermediate
289988ed4aceSMatthew G Knepley 
290088ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
290188ed4aceSMatthew G Knepley @*/
290288ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
290388ed4aceSMatthew G Knepley {
290482f516ccSBarry Smith   MPI_Comm       comm;
290588ed4aceSMatthew G Knepley   PetscLayout    layout;
290688ed4aceSMatthew G Knepley   const PetscInt *ranges;
290788ed4aceSMatthew G Knepley   PetscInt       *local;
290888ed4aceSMatthew G Knepley   PetscSFNode    *remote;
290988ed4aceSMatthew G Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves, l;
291088ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
291188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
291288ed4aceSMatthew G Knepley 
291388ed4aceSMatthew G Knepley   PetscFunctionBegin;
291482f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
291588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
291688ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
291788ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
291888ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
291988ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
292088ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
292188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
292288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
292388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
292488ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
292588ed4aceSMatthew G Knepley   for (p = pStart, nleaves = 0; p < pEnd; ++p) {
29266636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
292788ed4aceSMatthew G Knepley 
29286636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
29296636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
29306636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
293188ed4aceSMatthew G Knepley   }
293288ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
293388ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
293488ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
29351f588964SMatthew G Knepley     const PetscInt *cind;
29366636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
293788ed4aceSMatthew G Knepley 
293888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
293988ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
294088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
294188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
294288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
29436636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
294488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
29456636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
29466636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
29476636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
2948057b4bcdSMatthew 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);
29496636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
29506636e97aSMatthew G Knepley     }
295188ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
295288ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
295388ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
295488ed4aceSMatthew G Knepley     }
295588ed4aceSMatthew G Knepley     if (gdof < 0) {
29566636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
295788ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
295888ed4aceSMatthew G Knepley 
295931d3f06eSJed Brown         ierr = PetscFindInt(offset,size,ranges,&r);CHKERRQ(ierr);
296031d3f06eSJed Brown         if (r < 0) r = -(r+2);
296188ed4aceSMatthew G Knepley         remote[l].rank  = r;
296288ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
296388ed4aceSMatthew G Knepley       }
296488ed4aceSMatthew G Knepley     } else {
29656636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
296688ed4aceSMatthew G Knepley         remote[l].rank  = rank;
296788ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
296888ed4aceSMatthew G Knepley       }
296988ed4aceSMatthew G Knepley     }
297088ed4aceSMatthew G Knepley   }
29716636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
297288ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
297388ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
297488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
297588ed4aceSMatthew G Knepley }
2976af122d2aSMatthew G Knepley 
2977af122d2aSMatthew G Knepley #undef __FUNCT__
2978b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
2979b21d0597SMatthew G Knepley /*@
2980b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
2981b21d0597SMatthew G Knepley 
2982b21d0597SMatthew G Knepley   Input Parameter:
2983b21d0597SMatthew G Knepley . dm - The DM
2984b21d0597SMatthew G Knepley 
2985b21d0597SMatthew G Knepley   Output Parameter:
2986b21d0597SMatthew G Knepley . sf - The PetscSF
2987b21d0597SMatthew G Knepley 
2988b21d0597SMatthew G Knepley   Level: intermediate
2989b21d0597SMatthew G Knepley 
2990b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
2991b21d0597SMatthew G Knepley 
2992057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
2993b21d0597SMatthew G Knepley @*/
29940adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
29950adebc6cSBarry Smith {
2996b21d0597SMatthew G Knepley   PetscFunctionBegin;
2997b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2998b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
2999b21d0597SMatthew G Knepley   *sf = dm->sf;
3000b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3001b21d0597SMatthew G Knepley }
3002b21d0597SMatthew G Knepley 
3003b21d0597SMatthew G Knepley #undef __FUNCT__
3004057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3005057b4bcdSMatthew G Knepley /*@
3006057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3007057b4bcdSMatthew G Knepley 
3008057b4bcdSMatthew G Knepley   Input Parameters:
3009057b4bcdSMatthew G Knepley + dm - The DM
3010057b4bcdSMatthew G Knepley - sf - The PetscSF
3011057b4bcdSMatthew G Knepley 
3012057b4bcdSMatthew G Knepley   Level: intermediate
3013057b4bcdSMatthew G Knepley 
3014057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3015057b4bcdSMatthew G Knepley @*/
30160adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
30170adebc6cSBarry Smith {
3018057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3019057b4bcdSMatthew G Knepley 
3020057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3021057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3022057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3023057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3024057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3025057b4bcdSMatthew G Knepley   dm->sf = sf;
3026057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3027057b4bcdSMatthew G Knepley }
3028057b4bcdSMatthew G Knepley 
3029057b4bcdSMatthew G Knepley #undef __FUNCT__
3030af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3031af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3032af122d2aSMatthew G Knepley {
3033af122d2aSMatthew G Knepley   PetscFunctionBegin;
3034af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3035af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3036af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3037af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3038af122d2aSMatthew G Knepley }
3039af122d2aSMatthew G Knepley 
3040af122d2aSMatthew G Knepley #undef __FUNCT__
3041af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3042af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3043af122d2aSMatthew G Knepley {
3044af122d2aSMatthew G Knepley   PetscInt       f;
3045af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3046af122d2aSMatthew G Knepley 
3047af122d2aSMatthew G Knepley   PetscFunctionBegin;
3048af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3049af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3050af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3051af122d2aSMatthew G Knepley   }
3052af122d2aSMatthew G Knepley   ierr          = PetscFree(dm->fields);CHKERRQ(ierr);
3053af122d2aSMatthew G Knepley   dm->numFields = numFields;
3054af122d2aSMatthew G Knepley   ierr          = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3055af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
305682f516ccSBarry Smith     ierr = PetscContainerCreate(PetscObjectComm((PetscObject)dm), (PetscContainer*) &dm->fields[f]);CHKERRQ(ierr);
3057af122d2aSMatthew G Knepley   }
3058af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3059af122d2aSMatthew G Knepley }
3060af122d2aSMatthew G Knepley 
3061af122d2aSMatthew G Knepley #undef __FUNCT__
3062af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3063af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3064af122d2aSMatthew G Knepley {
3065af122d2aSMatthew G Knepley   PetscFunctionBegin;
3066af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3067af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
306882f516ccSBarry Smith   if (!dm->fields) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
306982f516ccSBarry Smith   if ((f < 0) || (f >= dm->numFields)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %d should be in [%d,%d)", f, 0, dm->numFields);
3070af122d2aSMatthew G Knepley   *field = dm->fields[f];
3071af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3072af122d2aSMatthew G Knepley }
30736636e97aSMatthew G Knepley 
30746636e97aSMatthew G Knepley #undef __FUNCT__
30756636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
30766636e97aSMatthew G Knepley /*@
30776636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
30786636e97aSMatthew G Knepley 
30796636e97aSMatthew G Knepley   Collective on DM
30806636e97aSMatthew G Knepley 
30816636e97aSMatthew G Knepley   Input Parameters:
30826636e97aSMatthew G Knepley + dm - the DM
30836636e97aSMatthew G Knepley - c - coordinate vector
30846636e97aSMatthew G Knepley 
30856636e97aSMatthew G Knepley   Note:
30866636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
30876636e97aSMatthew G Knepley 
30886636e97aSMatthew G Knepley   Level: intermediate
30896636e97aSMatthew G Knepley 
30906636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
30916636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
30926636e97aSMatthew G Knepley @*/
30936636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
30946636e97aSMatthew G Knepley {
30956636e97aSMatthew G Knepley   PetscErrorCode ierr;
30966636e97aSMatthew G Knepley 
30976636e97aSMatthew G Knepley   PetscFunctionBegin;
30986636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30996636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
31006636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
31016636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
31026636e97aSMatthew G Knepley   dm->coordinates = c;
31036636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
31046636e97aSMatthew G Knepley   PetscFunctionReturn(0);
31056636e97aSMatthew G Knepley }
31066636e97aSMatthew G Knepley 
31076636e97aSMatthew G Knepley #undef __FUNCT__
31086636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
31096636e97aSMatthew G Knepley /*@
31106636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
31116636e97aSMatthew G Knepley 
31126636e97aSMatthew G Knepley   Collective on DM
31136636e97aSMatthew G Knepley 
31146636e97aSMatthew G Knepley    Input Parameters:
31156636e97aSMatthew G Knepley +  dm - the DM
31166636e97aSMatthew G Knepley -  c - coordinate vector
31176636e97aSMatthew G Knepley 
31186636e97aSMatthew G Knepley   Note:
31196636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
31206636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
31216636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
31226636e97aSMatthew G Knepley 
31236636e97aSMatthew G Knepley   Level: intermediate
31246636e97aSMatthew G Knepley 
31256636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31266636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
31276636e97aSMatthew G Knepley @*/
31286636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
31296636e97aSMatthew G Knepley {
31306636e97aSMatthew G Knepley   PetscErrorCode ierr;
31316636e97aSMatthew G Knepley 
31326636e97aSMatthew G Knepley   PetscFunctionBegin;
31336636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31346636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
31356636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
31366636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
31378865f1eaSKarl Rupp 
31386636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
31398865f1eaSKarl Rupp 
31406636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
31416636e97aSMatthew G Knepley   PetscFunctionReturn(0);
31426636e97aSMatthew G Knepley }
31436636e97aSMatthew G Knepley 
31446636e97aSMatthew G Knepley #undef __FUNCT__
31456636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
31466636e97aSMatthew G Knepley /*@
31476636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
31486636e97aSMatthew G Knepley 
31496636e97aSMatthew G Knepley   Not Collective
31506636e97aSMatthew G Knepley 
31516636e97aSMatthew G Knepley   Input Parameter:
31526636e97aSMatthew G Knepley . dm - the DM
31536636e97aSMatthew G Knepley 
31546636e97aSMatthew G Knepley   Output Parameter:
31556636e97aSMatthew G Knepley . c - global coordinate vector
31566636e97aSMatthew G Knepley 
31576636e97aSMatthew G Knepley   Note:
31586636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
31596636e97aSMatthew G Knepley 
31606636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
31616636e97aSMatthew G Knepley 
31626636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
31636636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
31646636e97aSMatthew G Knepley 
31656636e97aSMatthew G Knepley   Level: intermediate
31666636e97aSMatthew G Knepley 
31676636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31686636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
31696636e97aSMatthew G Knepley @*/
31706636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
31716636e97aSMatthew G Knepley {
31726636e97aSMatthew G Knepley   PetscErrorCode ierr;
31736636e97aSMatthew G Knepley 
31746636e97aSMatthew G Knepley   PetscFunctionBegin;
31756636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31766636e97aSMatthew G Knepley   PetscValidPointer(c,2);
31771f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
31780298fd71SBarry Smith     DM cdm = NULL;
31796636e97aSMatthew G Knepley 
31806636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
31816636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
31826636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
31836636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
31846636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
31856636e97aSMatthew G Knepley   }
31866636e97aSMatthew G Knepley   *c = dm->coordinates;
31876636e97aSMatthew G Knepley   PetscFunctionReturn(0);
31886636e97aSMatthew G Knepley }
31896636e97aSMatthew G Knepley 
31906636e97aSMatthew G Knepley #undef __FUNCT__
31916636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
31926636e97aSMatthew G Knepley /*@
31936636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
31946636e97aSMatthew G Knepley 
31956636e97aSMatthew G Knepley   Collective on DM
31966636e97aSMatthew G Knepley 
31976636e97aSMatthew G Knepley   Input Parameter:
31986636e97aSMatthew G Knepley . dm - the DM
31996636e97aSMatthew G Knepley 
32006636e97aSMatthew G Knepley   Output Parameter:
32016636e97aSMatthew G Knepley . c - coordinate vector
32026636e97aSMatthew G Knepley 
32036636e97aSMatthew G Knepley   Note:
32046636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
32056636e97aSMatthew G Knepley 
32066636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
32076636e97aSMatthew G Knepley 
32086636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
32096636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
32106636e97aSMatthew G Knepley 
32116636e97aSMatthew G Knepley   Level: intermediate
32126636e97aSMatthew G Knepley 
32136636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
32146636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
32156636e97aSMatthew G Knepley @*/
32166636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
32176636e97aSMatthew G Knepley {
32186636e97aSMatthew G Knepley   PetscErrorCode ierr;
32196636e97aSMatthew G Knepley 
32206636e97aSMatthew G Knepley   PetscFunctionBegin;
32216636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32226636e97aSMatthew G Knepley   PetscValidPointer(c,2);
32231f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
32240298fd71SBarry Smith     DM cdm = NULL;
32256636e97aSMatthew G Knepley 
32266636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
32276636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
32286636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
32296636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
32306636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
32316636e97aSMatthew G Knepley   }
32326636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
32336636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32346636e97aSMatthew G Knepley }
32356636e97aSMatthew G Knepley 
32366636e97aSMatthew G Knepley #undef __FUNCT__
32376636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
32386636e97aSMatthew G Knepley /*@
32396636e97aSMatthew G Knepley   DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates
32406636e97aSMatthew G Knepley 
32416636e97aSMatthew G Knepley   Collective on DM
32426636e97aSMatthew G Knepley 
32436636e97aSMatthew G Knepley   Input Parameter:
32446636e97aSMatthew G Knepley . dm - the DM
32456636e97aSMatthew G Knepley 
32466636e97aSMatthew G Knepley   Output Parameter:
32476636e97aSMatthew G Knepley . cdm - coordinate DM
32486636e97aSMatthew G Knepley 
32496636e97aSMatthew G Knepley   Level: intermediate
32506636e97aSMatthew G Knepley 
32516636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
32526636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
32536636e97aSMatthew G Knepley @*/
32546636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
32556636e97aSMatthew G Knepley {
32566636e97aSMatthew G Knepley   PetscErrorCode ierr;
32576636e97aSMatthew G Knepley 
32586636e97aSMatthew G Knepley   PetscFunctionBegin;
32596636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32606636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
32616636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
326282f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
32636636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
32646636e97aSMatthew G Knepley   }
32656636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
32666636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32676636e97aSMatthew G Knepley }
3268e87bb0d3SMatthew G Knepley 
3269e87bb0d3SMatthew G Knepley #undef __FUNCT__
3270e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
3271e87bb0d3SMatthew G Knepley /*@
3272e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
3273e87bb0d3SMatthew G Knepley 
3274e87bb0d3SMatthew G Knepley   Not collective
3275e87bb0d3SMatthew G Knepley 
3276e87bb0d3SMatthew G Knepley   Input Parameters:
3277e87bb0d3SMatthew G Knepley + dm - The DM
3278e87bb0d3SMatthew G Knepley - v - The Vec of points
3279e87bb0d3SMatthew G Knepley 
328061e3bb9bSMatthew G Knepley   Output Parameter:
3281e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
3282e87bb0d3SMatthew G Knepley 
3283e87bb0d3SMatthew G Knepley   Level: developer
328461e3bb9bSMatthew G Knepley 
328561e3bb9bSMatthew G Knepley .keywords: point location, mesh
328661e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
328761e3bb9bSMatthew G Knepley @*/
3288e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
3289e87bb0d3SMatthew G Knepley {
3290735aa83eSMatthew G Knepley   PetscErrorCode ierr;
3291735aa83eSMatthew G Knepley 
3292e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
3293e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3294e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
3295e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
3296735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
3297735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
329882f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
3299e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
3300e87bb0d3SMatthew G Knepley }
3301