xref: /petsc/src/dm/interface/dm.c (revision d78e899e0acfb33a880f460f9d151d1996065aab)
1b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
20c312b8eSJed Brown #include <petscsf.h>
347c6ae99SBarry Smith 
4732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
5f089877aSRichard Tran Mills PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal;
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);
184d7f50e27SLisandro Dalcin   if (dm) 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 
32987e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
330732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
3318865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
3328865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
333732e2eb9SMatthew G Knepley   }
334dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
33571cd77b2SBarry Smith   if ((*dm)->x) {
336c688c046SMatthew G Knepley     DM obj;
337c688c046SMatthew G Knepley     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
338c688c046SMatthew G Knepley     if (obj == *dm) cnt++;
33971cd77b2SBarry Smith   }
3402348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
3412348bcf4SPeter Brune   if ((*dm)->x) {
3422348bcf4SPeter Brune     DM obj;
3432348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
3442348bcf4SPeter Brune     if (obj == *dm) cnt++;
3452348bcf4SPeter Brune   }
346732e2eb9SMatthew G Knepley 
3476bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
348732e2eb9SMatthew G Knepley   /*
349732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
350732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
351732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
352732e2eb9SMatthew G Knepley   */
3536bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
3546bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
355732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
3566bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
3576bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
358732e2eb9SMatthew G Knepley   }
359dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
360dfe15315SJed Brown     nnext = nlink->next;
361dfe15315SJed 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);
362dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
363dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
364dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
365dfe15315SJed Brown   }
3660298fd71SBarry Smith   (*dm)->namedglobal = NULL;
3671a266240SBarry Smith 
3682348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nnext) { /* Destroy the named local vectors */
3692348bcf4SPeter Brune     nnext = nlink->next;
3702348bcf4SPeter 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);
3712348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
3722348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
3732348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
3742348bcf4SPeter Brune   }
3750298fd71SBarry Smith   (*dm)->namedlocal = NULL;
3762348bcf4SPeter Brune 
377b17ce1afSJed Brown   /* Destroy the list of hooks */
378c833c3b5SJed Brown   {
379c833c3b5SJed Brown     DMCoarsenHookLink link,next;
380b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
381b17ce1afSJed Brown       next = link->next;
382b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
383b17ce1afSJed Brown     }
3840298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
385c833c3b5SJed Brown   }
386c833c3b5SJed Brown   {
387c833c3b5SJed Brown     DMRefineHookLink link,next;
388c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
389c833c3b5SJed Brown       next = link->next;
390c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
391c833c3b5SJed Brown     }
3920298fd71SBarry Smith     (*dm)->refinehook = NULL;
393c833c3b5SJed Brown   }
394be081cd6SPeter Brune   {
395be081cd6SPeter Brune     DMSubDomainHookLink link,next;
396be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
397be081cd6SPeter Brune       next = link->next;
398be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
399be081cd6SPeter Brune     }
4000298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
401be081cd6SPeter Brune   }
402baf369e7SPeter Brune   {
403baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
404baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
405baf369e7SPeter Brune       next = link->next;
406baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
407baf369e7SPeter Brune     }
4080298fd71SBarry Smith     (*dm)->gtolhook = NULL;
409baf369e7SPeter Brune   }
410aa1993deSMatthew G Knepley   /* Destroy the work arrays */
411aa1993deSMatthew G Knepley   {
412aa1993deSMatthew G Knepley     DMWorkLink link,next;
413aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
414aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
415aa1993deSMatthew G Knepley       next = link->next;
416aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
417aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
418aa1993deSMatthew G Knepley     }
4190298fd71SBarry Smith     (*dm)->workin = NULL;
420aa1993deSMatthew G Knepley   }
421b17ce1afSJed Brown 
42252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
42352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
42452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
42552536dc3SBarry Smith 
4261a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
4271a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
4281a266240SBarry Smith   }
42987e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
43071cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
4314dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
4326bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
4336bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
4346bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
435073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
43688ed4aceSMatthew G Knepley 
43788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
43888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
4398b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
44088ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
44188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
442af122d2aSMatthew G Knepley 
4436636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
4446636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
4456636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
4466636e97aSMatthew G Knepley 
447af122d2aSMatthew G Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
448af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr);
449af122d2aSMatthew G Knepley   }
450af122d2aSMatthew G Knepley   ierr = PetscFree((*dm)->fields);CHKERRQ(ierr);
451732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
452f05ece33SBarry Smith   ierr = PetscObjectAMSViewOff((PetscObject)*dm);CHKERRQ(ierr);
453732e2eb9SMatthew G Knepley 
4546bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
455435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
456732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
45747c6ae99SBarry Smith   PetscFunctionReturn(0);
45847c6ae99SBarry Smith }
45947c6ae99SBarry Smith 
46047c6ae99SBarry Smith #undef __FUNCT__
461d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
462d7bf68aeSBarry Smith /*@
463d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
464d7bf68aeSBarry Smith 
465d7bf68aeSBarry Smith     Collective on DM
466d7bf68aeSBarry Smith 
467d7bf68aeSBarry Smith     Input Parameter:
468d7bf68aeSBarry Smith .   dm - the DM object to setup
469d7bf68aeSBarry Smith 
470d7bf68aeSBarry Smith     Level: developer
471d7bf68aeSBarry Smith 
472e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
473d7bf68aeSBarry Smith 
474d7bf68aeSBarry Smith @*/
4757087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
476d7bf68aeSBarry Smith {
477d7bf68aeSBarry Smith   PetscErrorCode ierr;
478d7bf68aeSBarry Smith 
479d7bf68aeSBarry Smith   PetscFunctionBegin;
480171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4818387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
482d7bf68aeSBarry Smith   if (dm->ops->setup) {
483d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
484d7bf68aeSBarry Smith   }
4858387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
486d7bf68aeSBarry Smith   PetscFunctionReturn(0);
487d7bf68aeSBarry Smith }
488d7bf68aeSBarry Smith 
489d7bf68aeSBarry Smith #undef __FUNCT__
490d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
491d7bf68aeSBarry Smith /*@
492d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
493d7bf68aeSBarry Smith 
494d7bf68aeSBarry Smith     Collective on DM
495d7bf68aeSBarry Smith 
496d7bf68aeSBarry Smith     Input Parameter:
497d7bf68aeSBarry Smith .   dm - the DM object to set options for
498d7bf68aeSBarry Smith 
499732e2eb9SMatthew G Knepley     Options Database:
500dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
501dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
502171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
503171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
504732e2eb9SMatthew G Knepley 
505d7bf68aeSBarry Smith     Level: developer
506d7bf68aeSBarry Smith 
507e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
508d7bf68aeSBarry Smith 
509d7bf68aeSBarry Smith @*/
5107087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
511d7bf68aeSBarry Smith {
512f55f929bSMatthew G Knepley   char           typeName[256] = MATAIJ;
513ca266f36SBarry Smith   PetscBool      flg;
514d7bf68aeSBarry Smith   PetscErrorCode ierr;
515d7bf68aeSBarry Smith 
516d7bf68aeSBarry Smith   PetscFunctionBegin;
517171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5183194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
5190298fd71SBarry 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);
520f9ba7244SBarry Smith   ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
521f9ba7244SBarry Smith   if (flg) {
522f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
523f9ba7244SBarry Smith   }
5248caf3d72SBarry 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);
525073dac72SJed Brown   if (flg) {
526521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
527073dac72SJed Brown   }
5280298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
529f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
530f9ba7244SBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
531f9ba7244SBarry Smith   }
532f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
533f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
53482fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
535146574abSBarry Smith   ierr = DMViewFromOptions(dm,NULL,"-dm_view");CHKERRQ(ierr);
536d7bf68aeSBarry Smith   PetscFunctionReturn(0);
537d7bf68aeSBarry Smith }
538d7bf68aeSBarry Smith 
539d7bf68aeSBarry Smith #undef __FUNCT__
54047c6ae99SBarry Smith #define __FUNCT__ "DMView"
541fc9bc008SSatish Balay /*@C
542aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
54347c6ae99SBarry Smith 
54447c6ae99SBarry Smith     Collective on DM
54547c6ae99SBarry Smith 
54647c6ae99SBarry Smith     Input Parameter:
54747c6ae99SBarry Smith +   dm - the DM object to view
54847c6ae99SBarry Smith -   v - the viewer
54947c6ae99SBarry Smith 
55047c6ae99SBarry Smith     Level: developer
55147c6ae99SBarry Smith 
552e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
55347c6ae99SBarry Smith 
55447c6ae99SBarry Smith @*/
5557087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
55647c6ae99SBarry Smith {
55747c6ae99SBarry Smith   PetscErrorCode ierr;
55832c0f0efSBarry Smith   PetscBool      isbinary;
55947c6ae99SBarry Smith 
56047c6ae99SBarry Smith   PetscFunctionBegin;
561171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5623014e516SBarry Smith   if (!v) {
563ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
5643014e516SBarry Smith   }
56532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
56632c0f0efSBarry Smith   if (isbinary) {
56755849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
56832c0f0efSBarry Smith     char     type[256];
56932c0f0efSBarry Smith 
57032c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
57132c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
57232c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
57332c0f0efSBarry Smith   }
5740c010503SBarry Smith   if (dm->ops->view) {
5750c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
57647c6ae99SBarry Smith   }
57747c6ae99SBarry Smith   PetscFunctionReturn(0);
57847c6ae99SBarry Smith }
57947c6ae99SBarry Smith 
58047c6ae99SBarry Smith #undef __FUNCT__
58147c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
58247c6ae99SBarry Smith /*@
583aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
58447c6ae99SBarry Smith 
58547c6ae99SBarry Smith     Collective on DM
58647c6ae99SBarry Smith 
58747c6ae99SBarry Smith     Input Parameter:
58847c6ae99SBarry Smith .   dm - the DM object
58947c6ae99SBarry Smith 
59047c6ae99SBarry Smith     Output Parameter:
59147c6ae99SBarry Smith .   vec - the global vector
59247c6ae99SBarry Smith 
593073dac72SJed Brown     Level: beginner
59447c6ae99SBarry Smith 
595e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
59647c6ae99SBarry Smith 
59747c6ae99SBarry Smith @*/
5987087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
59947c6ae99SBarry Smith {
60047c6ae99SBarry Smith   PetscErrorCode ierr;
60147c6ae99SBarry Smith 
60247c6ae99SBarry Smith   PetscFunctionBegin;
603171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60447c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
60547c6ae99SBarry Smith   PetscFunctionReturn(0);
60647c6ae99SBarry Smith }
60747c6ae99SBarry Smith 
60847c6ae99SBarry Smith #undef __FUNCT__
60947c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
61047c6ae99SBarry Smith /*@
611aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
61247c6ae99SBarry Smith 
61347c6ae99SBarry Smith     Not Collective
61447c6ae99SBarry Smith 
61547c6ae99SBarry Smith     Input Parameter:
61647c6ae99SBarry Smith .   dm - the DM object
61747c6ae99SBarry Smith 
61847c6ae99SBarry Smith     Output Parameter:
61947c6ae99SBarry Smith .   vec - the local vector
62047c6ae99SBarry Smith 
621073dac72SJed Brown     Level: beginner
62247c6ae99SBarry Smith 
623e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith @*/
6267087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
62747c6ae99SBarry Smith {
62847c6ae99SBarry Smith   PetscErrorCode ierr;
62947c6ae99SBarry Smith 
63047c6ae99SBarry Smith   PetscFunctionBegin;
631171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63247c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
63347c6ae99SBarry Smith   PetscFunctionReturn(0);
63447c6ae99SBarry Smith }
63547c6ae99SBarry Smith 
63647c6ae99SBarry Smith #undef __FUNCT__
6371411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
6381411c6eeSJed Brown /*@
6391411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
6401411c6eeSJed Brown 
6411411c6eeSJed Brown    Collective on DM
6421411c6eeSJed Brown 
6431411c6eeSJed Brown    Input Parameter:
6441411c6eeSJed Brown .  dm - the DM that provides the mapping
6451411c6eeSJed Brown 
6461411c6eeSJed Brown    Output Parameter:
6471411c6eeSJed Brown .  ltog - the mapping
6481411c6eeSJed Brown 
6491411c6eeSJed Brown    Level: intermediate
6501411c6eeSJed Brown 
6511411c6eeSJed Brown    Notes:
6521411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
6531411c6eeSJed Brown    MatSetLocalToGlobalMapping().
6541411c6eeSJed Brown 
6551411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
6561411c6eeSJed Brown @*/
6577087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
6581411c6eeSJed Brown {
6591411c6eeSJed Brown   PetscErrorCode ierr;
6601411c6eeSJed Brown 
6611411c6eeSJed Brown   PetscFunctionBegin;
6621411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6631411c6eeSJed Brown   PetscValidPointer(ltog,2);
6641411c6eeSJed Brown   if (!dm->ltogmap) {
66537d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
66637d0c07bSMatthew G Knepley 
66737d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
66837d0c07bSMatthew G Knepley     if (section) {
66937d0c07bSMatthew G Knepley       PetscInt *ltog;
67037d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
67137d0c07bSMatthew G Knepley 
67237d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
67337d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
67437d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
67537d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
67637d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
67737d0c07bSMatthew G Knepley         PetscInt dof, off, c;
67837d0c07bSMatthew G Knepley 
67937d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
68037d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
68137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
68237d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
68337d0c07bSMatthew G Knepley           ltog[l] = off+c;
68437d0c07bSMatthew G Knepley         }
68537d0c07bSMatthew G Knepley       }
68637d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
68737d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
68837d0c07bSMatthew G Knepley     } else {
689184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
690184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
6911411c6eeSJed Brown     }
69237d0c07bSMatthew G Knepley   }
6931411c6eeSJed Brown   *ltog = dm->ltogmap;
6941411c6eeSJed Brown   PetscFunctionReturn(0);
6951411c6eeSJed Brown }
6961411c6eeSJed Brown 
6971411c6eeSJed Brown #undef __FUNCT__
6981411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
6991411c6eeSJed Brown /*@
7001411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
7011411c6eeSJed Brown 
7021411c6eeSJed Brown    Collective on DM
7031411c6eeSJed Brown 
7041411c6eeSJed Brown    Input Parameter:
7051411c6eeSJed Brown .  da - the distributed array that provides the mapping
7061411c6eeSJed Brown 
7071411c6eeSJed Brown    Output Parameter:
7081411c6eeSJed Brown .  ltog - the block mapping
7091411c6eeSJed Brown 
7101411c6eeSJed Brown    Level: intermediate
7111411c6eeSJed Brown 
7121411c6eeSJed Brown    Notes:
7131411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
7141411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
7151411c6eeSJed Brown 
7161411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
7171411c6eeSJed Brown @*/
7187087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
7191411c6eeSJed Brown {
7201411c6eeSJed Brown   PetscErrorCode ierr;
7211411c6eeSJed Brown 
7221411c6eeSJed Brown   PetscFunctionBegin;
7231411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7241411c6eeSJed Brown   PetscValidPointer(ltog,2);
7251411c6eeSJed Brown   if (!dm->ltogmapb) {
7261411c6eeSJed Brown     PetscInt bs;
7271411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
7281411c6eeSJed Brown     if (bs > 1) {
729184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmappingblock) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
730184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
7311411c6eeSJed Brown     } else {
7321411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
7331411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
7341411c6eeSJed Brown     }
7351411c6eeSJed Brown   }
7361411c6eeSJed Brown   *ltog = dm->ltogmapb;
7371411c6eeSJed Brown   PetscFunctionReturn(0);
7381411c6eeSJed Brown }
7391411c6eeSJed Brown 
7401411c6eeSJed Brown #undef __FUNCT__
7411411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7421411c6eeSJed Brown /*@
7431411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7441411c6eeSJed Brown 
7451411c6eeSJed Brown    Not Collective
7461411c6eeSJed Brown 
7471411c6eeSJed Brown    Input Parameter:
7481411c6eeSJed Brown .  dm - the DM with block structure
7491411c6eeSJed Brown 
7501411c6eeSJed Brown    Output Parameter:
7511411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7521411c6eeSJed Brown 
7531411c6eeSJed Brown    Level: intermediate
7541411c6eeSJed Brown 
7551411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
7561411c6eeSJed Brown @*/
7577087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7581411c6eeSJed Brown {
7591411c6eeSJed Brown   PetscFunctionBegin;
7601411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7611411c6eeSJed Brown   PetscValidPointer(bs,2);
7621411c6eeSJed 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");
7631411c6eeSJed Brown   *bs = dm->bs;
7641411c6eeSJed Brown   PetscFunctionReturn(0);
7651411c6eeSJed Brown }
7661411c6eeSJed Brown 
7671411c6eeSJed Brown #undef __FUNCT__
768e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
76947c6ae99SBarry Smith /*@
770e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
77147c6ae99SBarry Smith 
77247c6ae99SBarry Smith     Collective on DM
77347c6ae99SBarry Smith 
77447c6ae99SBarry Smith     Input Parameter:
77547c6ae99SBarry Smith +   dm1 - the DM object
77647c6ae99SBarry Smith -   dm2 - the second, finer DM object
77747c6ae99SBarry Smith 
77847c6ae99SBarry Smith     Output Parameter:
77947c6ae99SBarry Smith +  mat - the interpolation
78047c6ae99SBarry Smith -  vec - the scaling (optional)
78147c6ae99SBarry Smith 
78247c6ae99SBarry Smith     Level: developer
78347c6ae99SBarry Smith 
78485afcc9aSBarry 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
78585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
786d52bd9f3SBarry Smith 
7871f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
788d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
78985afcc9aSBarry Smith 
79085afcc9aSBarry Smith 
791e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
79247c6ae99SBarry Smith 
79347c6ae99SBarry Smith @*/
794e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
79547c6ae99SBarry Smith {
79647c6ae99SBarry Smith   PetscErrorCode ierr;
79747c6ae99SBarry Smith 
79847c6ae99SBarry Smith   PetscFunctionBegin;
799171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
800171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
80125296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
80247c6ae99SBarry Smith   PetscFunctionReturn(0);
80347c6ae99SBarry Smith }
80447c6ae99SBarry Smith 
80547c6ae99SBarry Smith #undef __FUNCT__
806e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
80747c6ae99SBarry Smith /*@
808e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
80947c6ae99SBarry Smith 
81047c6ae99SBarry Smith     Collective on DM
81147c6ae99SBarry Smith 
81247c6ae99SBarry Smith     Input Parameter:
81347c6ae99SBarry Smith +   dm1 - the DM object
81447c6ae99SBarry Smith -   dm2 - the second, finer DM object
81547c6ae99SBarry Smith 
81647c6ae99SBarry Smith     Output Parameter:
81747c6ae99SBarry Smith .   ctx - the injection
81847c6ae99SBarry Smith 
81947c6ae99SBarry Smith     Level: developer
82047c6ae99SBarry Smith 
82185afcc9aSBarry 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
82285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
82385afcc9aSBarry Smith 
824e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
82547c6ae99SBarry Smith 
82647c6ae99SBarry Smith @*/
827e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
82847c6ae99SBarry Smith {
82947c6ae99SBarry Smith   PetscErrorCode ierr;
83047c6ae99SBarry Smith 
83147c6ae99SBarry Smith   PetscFunctionBegin;
832171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
833171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
83447c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
83547c6ae99SBarry Smith   PetscFunctionReturn(0);
83647c6ae99SBarry Smith }
83747c6ae99SBarry Smith 
83847c6ae99SBarry Smith #undef __FUNCT__
839e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
840d1e2c406SBarry Smith /*@C
841e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
84247c6ae99SBarry Smith 
84347c6ae99SBarry Smith     Collective on DM
84447c6ae99SBarry Smith 
84547c6ae99SBarry Smith     Input Parameter:
84647c6ae99SBarry Smith +   dm - the DM object
84747c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
84847c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
84947c6ae99SBarry Smith 
85047c6ae99SBarry Smith     Output Parameter:
85147c6ae99SBarry Smith .   coloring - the coloring
85247c6ae99SBarry Smith 
85347c6ae99SBarry Smith     Level: developer
85447c6ae99SBarry Smith 
855e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
85647c6ae99SBarry Smith 
857aab9d709SJed Brown @*/
85819fd82e9SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,MatType mtype,ISColoring *coloring)
85947c6ae99SBarry Smith {
86047c6ae99SBarry Smith   PetscErrorCode ierr;
86147c6ae99SBarry Smith 
86247c6ae99SBarry Smith   PetscFunctionBegin;
863171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
864ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
86547c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
86647c6ae99SBarry Smith   PetscFunctionReturn(0);
86747c6ae99SBarry Smith }
86847c6ae99SBarry Smith 
86947c6ae99SBarry Smith #undef __FUNCT__
870950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
87147c6ae99SBarry Smith /*@C
872950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
87347c6ae99SBarry Smith 
87447c6ae99SBarry Smith     Collective on DM
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith     Input Parameter:
87747c6ae99SBarry Smith +   dm - the DM object
87847c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
87994013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
88047c6ae99SBarry Smith 
88147c6ae99SBarry Smith     Output Parameter:
88247c6ae99SBarry Smith .   mat - the empty Jacobian
88347c6ae99SBarry Smith 
884073dac72SJed Brown     Level: beginner
88547c6ae99SBarry Smith 
88694013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
88794013140SBarry Smith        do not need to do it yourself.
88894013140SBarry Smith 
88994013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
890aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
89194013140SBarry Smith 
89294013140SBarry 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
89394013140SBarry Smith        internally by PETSc.
89494013140SBarry Smith 
89594013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
896aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
89794013140SBarry Smith 
898e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
89947c6ae99SBarry Smith 
900aab9d709SJed Brown @*/
90119fd82e9SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,MatType mtype,Mat *mat)
90247c6ae99SBarry Smith {
90347c6ae99SBarry Smith   PetscErrorCode ierr;
90447c6ae99SBarry Smith 
90547c6ae99SBarry Smith   PetscFunctionBegin;
906171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
907519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
908607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
909235683edSBarry Smith #endif
910c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
911c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
912073dac72SJed Brown   if (dm->mattype) {
91325296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
914073dac72SJed Brown   } else {
91525296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
916c7b7c8a4SJed Brown   }
91747c6ae99SBarry Smith   PetscFunctionReturn(0);
91847c6ae99SBarry Smith }
91947c6ae99SBarry Smith 
92047c6ae99SBarry Smith #undef __FUNCT__
921732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
922732e2eb9SMatthew G Knepley /*@
923950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
924732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
925732e2eb9SMatthew G Knepley 
926732e2eb9SMatthew G Knepley   Logically Collective on DMDA
927732e2eb9SMatthew G Knepley 
928732e2eb9SMatthew G Knepley   Input Parameter:
929732e2eb9SMatthew G Knepley + dm - the DM
930732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
931732e2eb9SMatthew G Knepley 
932732e2eb9SMatthew G Knepley   Level: developer
933950540a4SJed Brown .seealso DMCreateMatrix()
934732e2eb9SMatthew G Knepley @*/
935732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
936732e2eb9SMatthew G Knepley {
937732e2eb9SMatthew G Knepley   PetscFunctionBegin;
938732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
939732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
940732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
941732e2eb9SMatthew G Knepley }
942732e2eb9SMatthew G Knepley 
943732e2eb9SMatthew G Knepley #undef __FUNCT__
944a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
945a89ea682SMatthew G Knepley /*@C
946aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
947a89ea682SMatthew G Knepley 
948a89ea682SMatthew G Knepley   Not Collective
949a89ea682SMatthew G Knepley 
950a89ea682SMatthew G Knepley   Input Parameters:
951a89ea682SMatthew G Knepley + dm - the DM object
952aa1993deSMatthew G Knepley . count - The minium size
953aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
954a89ea682SMatthew G Knepley 
955a89ea682SMatthew G Knepley   Output Parameter:
956a89ea682SMatthew G Knepley . array - the work array
957a89ea682SMatthew G Knepley 
958a89ea682SMatthew G Knepley   Level: developer
959a89ea682SMatthew G Knepley 
960a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
961a89ea682SMatthew G Knepley @*/
962aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
963a89ea682SMatthew G Knepley {
964a89ea682SMatthew G Knepley   PetscErrorCode ierr;
965aa1993deSMatthew G Knepley   DMWorkLink     link;
966aa1993deSMatthew G Knepley   size_t         size;
967a89ea682SMatthew G Knepley 
968a89ea682SMatthew G Knepley   PetscFunctionBegin;
969a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
970aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
971aa1993deSMatthew G Knepley   if (dm->workin) {
972aa1993deSMatthew G Knepley     link       = dm->workin;
973aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
974aa1993deSMatthew G Knepley   } else {
975aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
976a89ea682SMatthew G Knepley   }
977aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
978aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
979aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
980aa1993deSMatthew G Knepley     ierr        = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
981aa1993deSMatthew G Knepley     link->bytes = size*count;
982aa1993deSMatthew G Knepley   }
983aa1993deSMatthew G Knepley   link->next   = dm->workout;
984aa1993deSMatthew G Knepley   dm->workout  = link;
985aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
986a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
987a89ea682SMatthew G Knepley }
988a89ea682SMatthew G Knepley 
989aa1993deSMatthew G Knepley #undef __FUNCT__
990aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
991aa1993deSMatthew G Knepley /*@C
992aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
993aa1993deSMatthew G Knepley 
994aa1993deSMatthew G Knepley   Not Collective
995aa1993deSMatthew G Knepley 
996aa1993deSMatthew G Knepley   Input Parameters:
997aa1993deSMatthew G Knepley + dm - the DM object
998aa1993deSMatthew G Knepley . count - The minium size
999aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1000aa1993deSMatthew G Knepley 
1001aa1993deSMatthew G Knepley   Output Parameter:
1002aa1993deSMatthew G Knepley . array - the work array
1003aa1993deSMatthew G Knepley 
1004aa1993deSMatthew G Knepley   Level: developer
1005aa1993deSMatthew G Knepley 
1006aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1007aa1993deSMatthew G Knepley @*/
1008aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1009aa1993deSMatthew G Knepley {
1010aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1011aa1993deSMatthew G Knepley 
1012aa1993deSMatthew G Knepley   PetscFunctionBegin;
1013aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1014aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1015aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1016aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1017aa1993deSMatthew G Knepley       *p           = link->next;
1018aa1993deSMatthew G Knepley       link->next   = dm->workin;
1019aa1993deSMatthew G Knepley       dm->workin   = link;
10200298fd71SBarry Smith       *(void**)mem = NULL;
1021aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1022aa1993deSMatthew G Knepley     }
1023aa1993deSMatthew G Knepley   }
1024aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1025aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1026aa1993deSMatthew G Knepley }
1027e7c4fc90SDmitry Karpeev 
1028e7c4fc90SDmitry Karpeev #undef __FUNCT__
1029435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1030435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1031435a35e8SMatthew G Knepley {
1032435a35e8SMatthew G Knepley   PetscFunctionBegin;
1033435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
103482f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1035435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1036435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1037435a35e8SMatthew G Knepley }
1038435a35e8SMatthew G Knepley 
1039435a35e8SMatthew G Knepley #undef __FUNCT__
10404d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10414f3b5142SJed Brown /*@C
10424d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10434d343eeaSMatthew G Knepley 
10444d343eeaSMatthew G Knepley   Not collective
10454d343eeaSMatthew G Knepley 
10464d343eeaSMatthew G Knepley   Input Parameter:
10474d343eeaSMatthew G Knepley . dm - the DM object
10484d343eeaSMatthew G Knepley 
10494d343eeaSMatthew G Knepley   Output Parameters:
10500298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
10510298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
10520298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
10534d343eeaSMatthew G Knepley 
10544d343eeaSMatthew G Knepley   Level: intermediate
10554d343eeaSMatthew G Knepley 
105621c9b008SJed Brown   Notes:
105721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
105821c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
105921c9b008SJed Brown   PetscFree().
106021c9b008SJed Brown 
10614d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10624d343eeaSMatthew G Knepley @*/
106337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10644d343eeaSMatthew G Knepley {
106537d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10664d343eeaSMatthew G Knepley   PetscErrorCode ierr;
10674d343eeaSMatthew G Knepley 
10684d343eeaSMatthew G Knepley   PetscFunctionBegin;
10694d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
107069ca1f37SDmitry Karpeev   if (numFields) {
107169ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
107269ca1f37SDmitry Karpeev     *numFields = 0;
107369ca1f37SDmitry Karpeev   }
107437d0c07bSMatthew G Knepley   if (fieldNames) {
107537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
10760298fd71SBarry Smith     *fieldNames = NULL;
107769ca1f37SDmitry Karpeev   }
107869ca1f37SDmitry Karpeev   if (fields) {
107969ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
10800298fd71SBarry Smith     *fields = NULL;
108169ca1f37SDmitry Karpeev   }
108237d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
108337d0c07bSMatthew G Knepley   if (section) {
108437d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
108537d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
108637d0c07bSMatthew G Knepley 
108737d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
108837d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
108937d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt*,&fieldIndices);CHKERRQ(ierr);
109037d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
109137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
109237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
109337d0c07bSMatthew G Knepley     }
109437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
109537d0c07bSMatthew G Knepley       PetscInt gdof;
109637d0c07bSMatthew G Knepley 
109737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
109837d0c07bSMatthew G Knepley       if (gdof > 0) {
109937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
110037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
110137d0c07bSMatthew G Knepley 
110237d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
110337d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
110437d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
110537d0c07bSMatthew G Knepley         }
110637d0c07bSMatthew G Knepley       }
110737d0c07bSMatthew G Knepley     }
110837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
110937d0c07bSMatthew G Knepley       ierr          = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
111037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
111137d0c07bSMatthew G Knepley     }
111237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
111337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
111437d0c07bSMatthew G Knepley 
111537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
111637d0c07bSMatthew G Knepley       if (gdof > 0) {
111737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
111837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
111937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
112037d0c07bSMatthew G Knepley 
112137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
112237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
112337d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
112437d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
112537d0c07bSMatthew G Knepley           }
112637d0c07bSMatthew G Knepley         }
112737d0c07bSMatthew G Knepley       }
112837d0c07bSMatthew G Knepley     }
11298865f1eaSKarl Rupp     if (numFields) *numFields = nF;
113037d0c07bSMatthew G Knepley     if (fieldNames) {
113137d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char*), fieldNames);CHKERRQ(ierr);
113237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
113337d0c07bSMatthew G Knepley         const char *fieldName;
113437d0c07bSMatthew G Knepley 
113537d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
113637d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
113737d0c07bSMatthew G Knepley       }
113837d0c07bSMatthew G Knepley     }
113937d0c07bSMatthew G Knepley     if (fields) {
114037d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
114137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
114282f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
114337d0c07bSMatthew G Knepley       }
114437d0c07bSMatthew G Knepley     }
114537d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
11468865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
11478865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
114869ca1f37SDmitry Karpeev   }
11494d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11504d343eeaSMatthew G Knepley }
11514d343eeaSMatthew G Knepley 
115216621825SDmitry Karpeev 
115316621825SDmitry Karpeev #undef __FUNCT__
115416621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
115516621825SDmitry Karpeev /*@C
115616621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
115716621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
115816621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1159e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1160e7c4fc90SDmitry Karpeev 
1161e7c4fc90SDmitry Karpeev   Not collective
1162e7c4fc90SDmitry Karpeev 
1163e7c4fc90SDmitry Karpeev   Input Parameter:
1164e7c4fc90SDmitry Karpeev . dm - the DM object
1165e7c4fc90SDmitry Karpeev 
1166e7c4fc90SDmitry Karpeev   Output Parameters:
11670298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
11680298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
11690298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
11700298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1171e7c4fc90SDmitry Karpeev 
1172e7c4fc90SDmitry Karpeev   Level: intermediate
1173e7c4fc90SDmitry Karpeev 
1174e7c4fc90SDmitry Karpeev   Notes:
1175e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1176e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1177e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1178e7c4fc90SDmitry Karpeev 
1179e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1180e7c4fc90SDmitry Karpeev @*/
118116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1182e7c4fc90SDmitry Karpeev {
1183e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1184e7c4fc90SDmitry Karpeev 
1185e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1186e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11878865f1eaSKarl Rupp   if (len) {
11888865f1eaSKarl Rupp     PetscValidPointer(len,2);
11898865f1eaSKarl Rupp     *len = 0;
11908865f1eaSKarl Rupp   }
11918865f1eaSKarl Rupp   if (namelist) {
11928865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
11938865f1eaSKarl Rupp     *namelist = 0;
11948865f1eaSKarl Rupp   }
11958865f1eaSKarl Rupp   if (islist) {
11968865f1eaSKarl Rupp     PetscValidPointer(islist,4);
11978865f1eaSKarl Rupp     *islist = 0;
11988865f1eaSKarl Rupp   }
11998865f1eaSKarl Rupp   if (dmlist) {
12008865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
12018865f1eaSKarl Rupp     *dmlist = 0;
12028865f1eaSKarl Rupp   }
1203f3f0edfdSDmitry Karpeev   /*
1204f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1205f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1206f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1207f3f0edfdSDmitry Karpeev    */
1208ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
120916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1210435a35e8SMatthew G Knepley     PetscSection section;
1211435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1212435a35e8SMatthew G Knepley 
1213435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1214435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1215435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1216435a35e8SMatthew G Knepley       *len = numFields;
1217435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1218435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1219435a35e8SMatthew G Knepley         const char *fieldName;
1220435a35e8SMatthew G Knepley 
1221435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1222435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1223435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1224435a35e8SMatthew G Knepley       }
1225435a35e8SMatthew G Knepley     } else {
122669ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1227e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
12280298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1229e7c4fc90SDmitry Karpeev     }
12308865f1eaSKarl Rupp   } else {
123116621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
123216621825SDmitry Karpeev   }
123316621825SDmitry Karpeev   PetscFunctionReturn(0);
123416621825SDmitry Karpeev }
123516621825SDmitry Karpeev 
123616621825SDmitry Karpeev #undef __FUNCT__
1237435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1238435a35e8SMatthew G Knepley /*@C
1239435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1240435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1241435a35e8SMatthew G Knepley 
1242435a35e8SMatthew G Knepley   Not collective
1243435a35e8SMatthew G Knepley 
1244435a35e8SMatthew G Knepley   Input Parameters:
1245435a35e8SMatthew G Knepley + dm - the DM object
1246435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
12470298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1248435a35e8SMatthew G Knepley 
1249435a35e8SMatthew G Knepley   Output Parameters:
1250435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1251435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1252435a35e8SMatthew G Knepley 
1253435a35e8SMatthew G Knepley   Level: intermediate
1254435a35e8SMatthew G Knepley 
1255435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1256435a35e8SMatthew G Knepley @*/
1257435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1258435a35e8SMatthew G Knepley {
1259435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1260435a35e8SMatthew G Knepley 
1261435a35e8SMatthew G Knepley   PetscFunctionBegin;
1262435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1263435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
12648865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
12658865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1266435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1267435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
126882f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1269435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1270435a35e8SMatthew G Knepley }
1271435a35e8SMatthew G Knepley 
127216621825SDmitry Karpeev 
127316621825SDmitry Karpeev #undef __FUNCT__
127416621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
127516621825SDmitry Karpeev /*@C
12768d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
12778d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
12788d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
12798d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
12808d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
128116621825SDmitry Karpeev 
128216621825SDmitry Karpeev   Not collective
128316621825SDmitry Karpeev 
128416621825SDmitry Karpeev   Input Parameter:
128516621825SDmitry Karpeev . dm - the DM object
128616621825SDmitry Karpeev 
128716621825SDmitry Karpeev   Output Parameters:
12880298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
12890298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
12900298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
12910298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
12920298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
129316621825SDmitry Karpeev 
129416621825SDmitry Karpeev   Level: intermediate
129516621825SDmitry Karpeev 
129616621825SDmitry Karpeev   Notes:
129716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
129816621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
129916621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
130016621825SDmitry Karpeev 
13018d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
130216621825SDmitry Karpeev @*/
13038d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
130416621825SDmitry Karpeev {
130516621825SDmitry Karpeev   PetscErrorCode      ierr;
1306be081cd6SPeter Brune   DMSubDomainHookLink link;
1307be081cd6SPeter Brune   PetscInt            i,l;
130816621825SDmitry Karpeev 
130916621825SDmitry Karpeev   PetscFunctionBegin;
131016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
131114a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
13120298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
13130298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
13140298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
13150298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1316f3f0edfdSDmitry Karpeev   /*
1317f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1318f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1319f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1320f3f0edfdSDmitry Karpeev    */
1321ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
132216621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1323be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
132414a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
132514a18fd3SPeter Brune     if (dmlist) {
1326be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1327be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1328be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1329be081cd6SPeter Brune         }
1330d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1331e7c4fc90SDmitry Karpeev       }
133214a18fd3SPeter Brune     }
133314a18fd3SPeter Brune     if (len) *len = l;
133414a18fd3SPeter Brune   }
1335e30e807fSPeter Brune   PetscFunctionReturn(0);
1336e30e807fSPeter Brune }
1337e30e807fSPeter Brune 
1338e30e807fSPeter Brune 
1339e30e807fSPeter Brune #undef __FUNCT__
1340e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1341e30e807fSPeter Brune /*@C
1342e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1343e30e807fSPeter Brune 
1344e30e807fSPeter Brune   Not collective
1345e30e807fSPeter Brune 
1346e30e807fSPeter Brune   Input Parameters:
1347e30e807fSPeter Brune + dm - the DM object
1348e30e807fSPeter Brune . n  - the number of subdomain scatters
1349e30e807fSPeter Brune - subdms - the local subdomains
1350e30e807fSPeter Brune 
1351e30e807fSPeter Brune   Output Parameters:
1352e30e807fSPeter Brune + n     - the number of scatters returned
1353e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1354e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1355e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1356e30e807fSPeter Brune 
1357e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1358e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1359e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1360e30e807fSPeter Brune   solution and residual data.
1361e30e807fSPeter Brune 
1362e30e807fSPeter Brune   Level: developer
1363e30e807fSPeter Brune 
1364e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1365e30e807fSPeter Brune @*/
1366e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1367e30e807fSPeter Brune {
1368e30e807fSPeter Brune   PetscErrorCode ierr;
1369e30e807fSPeter Brune 
1370e30e807fSPeter Brune   PetscFunctionBegin;
1371e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1372e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1373e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1374e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
137582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1376e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1377e7c4fc90SDmitry Karpeev }
1378e7c4fc90SDmitry Karpeev 
1379731c8d9eSDmitry Karpeev #undef __FUNCT__
138047c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
138147c6ae99SBarry Smith /*@
138247c6ae99SBarry Smith   DMRefine - Refines a DM object
138347c6ae99SBarry Smith 
138447c6ae99SBarry Smith   Collective on DM
138547c6ae99SBarry Smith 
138647c6ae99SBarry Smith   Input Parameter:
138747c6ae99SBarry Smith + dm   - the DM object
138891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
138947c6ae99SBarry Smith 
139047c6ae99SBarry Smith   Output Parameter:
13910298fd71SBarry Smith . dmf - the refined DM, or NULL
1392ae0a1c52SMatthew G Knepley 
13930298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
139447c6ae99SBarry Smith 
139547c6ae99SBarry Smith   Level: developer
139647c6ae99SBarry Smith 
1397e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
139847c6ae99SBarry Smith @*/
13997087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
140047c6ae99SBarry Smith {
140147c6ae99SBarry Smith   PetscErrorCode   ierr;
1402c833c3b5SJed Brown   DMRefineHookLink link;
140347c6ae99SBarry Smith 
140447c6ae99SBarry Smith   PetscFunctionBegin;
1405732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
140647c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14074057135bSMatthew G Knepley   if (*dmf) {
140843842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
14098865f1eaSKarl Rupp 
14108cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
14118865f1eaSKarl Rupp 
1412644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14130598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1414656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
14158865f1eaSKarl Rupp 
1416e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1417c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
14188865f1eaSKarl Rupp       if (link->refinehook) {
14198865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
14208865f1eaSKarl Rupp       }
1421c833c3b5SJed Brown     }
1422c833c3b5SJed Brown   }
1423c833c3b5SJed Brown   PetscFunctionReturn(0);
1424c833c3b5SJed Brown }
1425c833c3b5SJed Brown 
1426c833c3b5SJed Brown #undef __FUNCT__
1427c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1428bb9467b5SJed Brown /*@C
1429c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1430c833c3b5SJed Brown 
1431c833c3b5SJed Brown    Logically Collective
1432c833c3b5SJed Brown 
1433c833c3b5SJed Brown    Input Arguments:
1434c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1435c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1436c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
14370298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1438c833c3b5SJed Brown 
1439c833c3b5SJed Brown    Calling sequence of refinehook:
1440c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1441c833c3b5SJed Brown 
1442c833c3b5SJed Brown +  coarse - coarse level DM
1443c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1444c833c3b5SJed Brown -  ctx - optional user-defined function context
1445c833c3b5SJed Brown 
1446c833c3b5SJed Brown    Calling sequence for interphook:
1447c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1448c833c3b5SJed Brown 
1449c833c3b5SJed Brown +  coarse - coarse level DM
1450c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1451c833c3b5SJed Brown .  fine - fine level DM to update
1452c833c3b5SJed Brown -  ctx - optional user-defined function context
1453c833c3b5SJed Brown 
1454c833c3b5SJed Brown    Level: advanced
1455c833c3b5SJed Brown 
1456c833c3b5SJed Brown    Notes:
1457c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1458c833c3b5SJed Brown 
1459c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1460c833c3b5SJed Brown 
1461bb9467b5SJed Brown    This function is currently not available from Fortran.
1462bb9467b5SJed Brown 
1463c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1464c833c3b5SJed Brown @*/
1465c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1466c833c3b5SJed Brown {
1467c833c3b5SJed Brown   PetscErrorCode   ierr;
1468c833c3b5SJed Brown   DMRefineHookLink link,*p;
1469c833c3b5SJed Brown 
1470c833c3b5SJed Brown   PetscFunctionBegin;
1471c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1472c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1473c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1474c833c3b5SJed Brown   link->refinehook = refinehook;
1475c833c3b5SJed Brown   link->interphook = interphook;
1476c833c3b5SJed Brown   link->ctx        = ctx;
14770298fd71SBarry Smith   link->next       = NULL;
1478c833c3b5SJed Brown   *p               = link;
1479c833c3b5SJed Brown   PetscFunctionReturn(0);
1480c833c3b5SJed Brown }
1481c833c3b5SJed Brown 
1482c833c3b5SJed Brown #undef __FUNCT__
1483c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1484c833c3b5SJed Brown /*@
1485c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1486c833c3b5SJed Brown 
1487c833c3b5SJed Brown    Collective if any hooks are
1488c833c3b5SJed Brown 
1489c833c3b5SJed Brown    Input Arguments:
1490c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1491c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1492c833c3b5SJed Brown -  fine - finer DM to update
1493c833c3b5SJed Brown 
1494c833c3b5SJed Brown    Level: developer
1495c833c3b5SJed Brown 
1496c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1497c833c3b5SJed Brown @*/
1498c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1499c833c3b5SJed Brown {
1500c833c3b5SJed Brown   PetscErrorCode   ierr;
1501c833c3b5SJed Brown   DMRefineHookLink link;
1502c833c3b5SJed Brown 
1503c833c3b5SJed Brown   PetscFunctionBegin;
1504c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
15058865f1eaSKarl Rupp     if (link->interphook) {
15068865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
15078865f1eaSKarl Rupp     }
15084057135bSMatthew G Knepley   }
150947c6ae99SBarry Smith   PetscFunctionReturn(0);
151047c6ae99SBarry Smith }
151147c6ae99SBarry Smith 
151247c6ae99SBarry Smith #undef __FUNCT__
1513eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1514eb3f98d2SBarry Smith /*@
1515eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1516eb3f98d2SBarry Smith 
1517eb3f98d2SBarry Smith     Not Collective
1518eb3f98d2SBarry Smith 
1519eb3f98d2SBarry Smith     Input Parameter:
1520eb3f98d2SBarry Smith .   dm - the DM object
1521eb3f98d2SBarry Smith 
1522eb3f98d2SBarry Smith     Output Parameter:
1523eb3f98d2SBarry Smith .   level - number of refinements
1524eb3f98d2SBarry Smith 
1525eb3f98d2SBarry Smith     Level: developer
1526eb3f98d2SBarry Smith 
15276a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1528eb3f98d2SBarry Smith 
1529eb3f98d2SBarry Smith @*/
1530eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1531eb3f98d2SBarry Smith {
1532eb3f98d2SBarry Smith   PetscFunctionBegin;
1533eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1534eb3f98d2SBarry Smith   *level = dm->levelup;
1535eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1536eb3f98d2SBarry Smith }
1537eb3f98d2SBarry Smith 
1538eb3f98d2SBarry Smith #undef __FUNCT__
1539baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1540bb9467b5SJed Brown /*@C
1541baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1542baf369e7SPeter Brune 
1543baf369e7SPeter Brune    Logically Collective
1544baf369e7SPeter Brune 
1545baf369e7SPeter Brune    Input Arguments:
1546baf369e7SPeter Brune +  dm - the DM
1547baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1548baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
15490298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1550baf369e7SPeter Brune 
1551baf369e7SPeter Brune    Calling sequence for beginhook:
1552baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1553baf369e7SPeter Brune 
1554baf369e7SPeter Brune +  dm - global DM
1555baf369e7SPeter Brune .  g - global vector
1556baf369e7SPeter Brune .  mode - mode
1557baf369e7SPeter Brune .  l - local vector
1558baf369e7SPeter Brune -  ctx - optional user-defined function context
1559baf369e7SPeter Brune 
1560baf369e7SPeter Brune 
1561baf369e7SPeter Brune    Calling sequence for endhook:
1562ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1563baf369e7SPeter Brune 
1564baf369e7SPeter Brune +  global - global DM
1565baf369e7SPeter Brune -  ctx - optional user-defined function context
1566baf369e7SPeter Brune 
1567baf369e7SPeter Brune    Level: advanced
1568baf369e7SPeter Brune 
1569baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1570baf369e7SPeter Brune @*/
1571baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1572baf369e7SPeter Brune {
1573baf369e7SPeter Brune   PetscErrorCode          ierr;
1574baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1575baf369e7SPeter Brune 
1576baf369e7SPeter Brune   PetscFunctionBegin;
1577baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1578baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1579baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1580baf369e7SPeter Brune   link->beginhook = beginhook;
1581baf369e7SPeter Brune   link->endhook   = endhook;
1582baf369e7SPeter Brune   link->ctx       = ctx;
15830298fd71SBarry Smith   link->next      = NULL;
1584baf369e7SPeter Brune   *p              = link;
1585baf369e7SPeter Brune   PetscFunctionReturn(0);
1586baf369e7SPeter Brune }
1587baf369e7SPeter Brune 
1588baf369e7SPeter Brune #undef __FUNCT__
158947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
159047c6ae99SBarry Smith /*@
159147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
159247c6ae99SBarry Smith 
159347c6ae99SBarry Smith     Neighbor-wise Collective on DM
159447c6ae99SBarry Smith 
159547c6ae99SBarry Smith     Input Parameters:
159647c6ae99SBarry Smith +   dm - the DM object
159747c6ae99SBarry Smith .   g - the global vector
159847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
159947c6ae99SBarry Smith -   l - the local vector
160047c6ae99SBarry Smith 
160147c6ae99SBarry Smith 
160247c6ae99SBarry Smith     Level: beginner
160347c6ae99SBarry Smith 
1604e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
160547c6ae99SBarry Smith 
160647c6ae99SBarry Smith @*/
16077087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
160847c6ae99SBarry Smith {
16097128ae9fSMatthew G Knepley   PetscSF                 sf;
161047c6ae99SBarry Smith   PetscErrorCode          ierr;
1611baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
161247c6ae99SBarry Smith 
161347c6ae99SBarry Smith   PetscFunctionBegin;
1614171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1615baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
16168865f1eaSKarl Rupp     if (link->beginhook) {
16178865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
16188865f1eaSKarl Rupp     }
1619baf369e7SPeter Brune   }
16207128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16217128ae9fSMatthew G Knepley   if (sf) {
16227128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16237128ae9fSMatthew G Knepley 
162482f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16257128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16267128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16277128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16287128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16297128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16307128ae9fSMatthew G Knepley   } else {
1631843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16327128ae9fSMatthew G Knepley   }
163347c6ae99SBarry Smith   PetscFunctionReturn(0);
163447c6ae99SBarry Smith }
163547c6ae99SBarry Smith 
163647c6ae99SBarry Smith #undef __FUNCT__
163747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
163847c6ae99SBarry Smith /*@
163947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
164047c6ae99SBarry Smith 
164147c6ae99SBarry Smith     Neighbor-wise Collective on DM
164247c6ae99SBarry Smith 
164347c6ae99SBarry Smith     Input Parameters:
164447c6ae99SBarry Smith +   dm - the DM object
164547c6ae99SBarry Smith .   g - the global vector
164647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
164747c6ae99SBarry Smith -   l - the local vector
164847c6ae99SBarry Smith 
164947c6ae99SBarry Smith 
165047c6ae99SBarry Smith     Level: beginner
165147c6ae99SBarry Smith 
1652e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
165347c6ae99SBarry Smith 
165447c6ae99SBarry Smith @*/
16557087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
165647c6ae99SBarry Smith {
16577128ae9fSMatthew G Knepley   PetscSF                 sf;
165847c6ae99SBarry Smith   PetscErrorCode          ierr;
165961a3c1faSSatish Balay   PetscScalar             *lArray, *gArray;
1660baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
166147c6ae99SBarry Smith 
166247c6ae99SBarry Smith   PetscFunctionBegin;
1663171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16647128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16657128ae9fSMatthew G Knepley   if (sf) {
166682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16677128ae9fSMatthew G Knepley 
16687128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16697128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16707128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16717128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16727128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16737128ae9fSMatthew G Knepley   } else {
1674843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16757128ae9fSMatthew G Knepley   }
1676baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1677baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1678baf369e7SPeter Brune   }
167947c6ae99SBarry Smith   PetscFunctionReturn(0);
168047c6ae99SBarry Smith }
168147c6ae99SBarry Smith 
168247c6ae99SBarry Smith #undef __FUNCT__
16839a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
168447c6ae99SBarry Smith /*@
16859a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
16869a42bb27SBarry Smith 
16879a42bb27SBarry Smith     Neighbor-wise Collective on DM
16889a42bb27SBarry Smith 
16899a42bb27SBarry Smith     Input Parameters:
16909a42bb27SBarry Smith +   dm - the DM object
1691f6813fd5SJed Brown .   l - the local vector
16929a42bb27SBarry 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
16939a42bb27SBarry Smith            base point.
1694f6813fd5SJed Brown - - the global vector
16959a42bb27SBarry Smith 
16969a42bb27SBarry 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
16979a42bb27SBarry 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
16989a42bb27SBarry Smith            global array to the final global array with VecAXPY().
16999a42bb27SBarry Smith 
17009a42bb27SBarry Smith     Level: beginner
17019a42bb27SBarry Smith 
1702e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
17039a42bb27SBarry Smith 
17049a42bb27SBarry Smith @*/
17057087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
17069a42bb27SBarry Smith {
17077128ae9fSMatthew G Knepley   PetscSF        sf;
17089a42bb27SBarry Smith   PetscErrorCode ierr;
17099a42bb27SBarry Smith 
17109a42bb27SBarry Smith   PetscFunctionBegin;
1711171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17127128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17137128ae9fSMatthew G Knepley   if (sf) {
17147128ae9fSMatthew G Knepley     MPI_Op      op;
17157128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17167128ae9fSMatthew G Knepley 
17177128ae9fSMatthew G Knepley     switch (mode) {
17187128ae9fSMatthew G Knepley     case INSERT_VALUES:
17197128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17208bfbc91cSJed Brown       op = MPIU_REPLACE; break;
17217128ae9fSMatthew G Knepley     case ADD_VALUES:
17227128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17237128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17247128ae9fSMatthew G Knepley     default:
172582f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17267128ae9fSMatthew G Knepley     }
17277128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17287128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17297128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17307128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17317128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17327128ae9fSMatthew G Knepley   } else {
1733843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17347128ae9fSMatthew G Knepley   }
17359a42bb27SBarry Smith   PetscFunctionReturn(0);
17369a42bb27SBarry Smith }
17379a42bb27SBarry Smith 
17389a42bb27SBarry Smith #undef __FUNCT__
17399a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
17409a42bb27SBarry Smith /*@
17419a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
174247c6ae99SBarry Smith 
174347c6ae99SBarry Smith     Neighbor-wise Collective on DM
174447c6ae99SBarry Smith 
174547c6ae99SBarry Smith     Input Parameters:
174647c6ae99SBarry Smith +   dm - the DM object
1747f6813fd5SJed Brown .   l - the local vector
174847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1749f6813fd5SJed Brown -   g - the global vector
175047c6ae99SBarry Smith 
175147c6ae99SBarry Smith 
175247c6ae99SBarry Smith     Level: beginner
175347c6ae99SBarry Smith 
1754e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
175547c6ae99SBarry Smith 
175647c6ae99SBarry Smith @*/
17577087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
175847c6ae99SBarry Smith {
17597128ae9fSMatthew G Knepley   PetscSF        sf;
176047c6ae99SBarry Smith   PetscErrorCode ierr;
176147c6ae99SBarry Smith 
176247c6ae99SBarry Smith   PetscFunctionBegin;
1763171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17647128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17657128ae9fSMatthew G Knepley   if (sf) {
17667128ae9fSMatthew G Knepley     MPI_Op      op;
17677128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17687128ae9fSMatthew G Knepley 
17697128ae9fSMatthew G Knepley     switch (mode) {
17707128ae9fSMatthew G Knepley     case INSERT_VALUES:
17717128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17728bfbc91cSJed Brown       op = MPIU_REPLACE; break;
17737128ae9fSMatthew G Knepley     case ADD_VALUES:
17747128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17757128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17767128ae9fSMatthew G Knepley     default:
177782f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17787128ae9fSMatthew G Knepley     }
17797128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17807128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17817128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17827128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17837128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17847128ae9fSMatthew G Knepley   } else {
1785843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17867128ae9fSMatthew G Knepley   }
178747c6ae99SBarry Smith   PetscFunctionReturn(0);
178847c6ae99SBarry Smith }
178947c6ae99SBarry Smith 
179047c6ae99SBarry Smith #undef __FUNCT__
1791f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
1792f089877aSRichard Tran Mills /*@
1793bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
1794bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
1795*d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
1796f089877aSRichard Tran Mills 
1797bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
1798f089877aSRichard Tran Mills 
1799f089877aSRichard Tran Mills    Input Parameters:
1800f089877aSRichard Tran Mills +  dm - the DM object
1801bc0a1609SRichard Tran Mills .  g - the original local vector
1802bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
1803f089877aSRichard Tran Mills 
1804bc0a1609SRichard Tran Mills    Output Parameter:
1805bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
1806f089877aSRichard Tran Mills 
1807f089877aSRichard Tran Mills    Level: intermediate
1808f089877aSRichard Tran Mills 
1809bc0a1609SRichard Tran Mills    Notes:
1810bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
1811bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
1812bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
1813bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
1814bc0a1609SRichard Tran Mills 
1815bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
1816bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
1817f089877aSRichard Tran Mills 
1818f089877aSRichard Tran Mills @*/
1819f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
1820f089877aSRichard Tran Mills {
1821f089877aSRichard Tran Mills   PetscSF                 sf;
1822f089877aSRichard Tran Mills   PetscErrorCode          ierr;
1823f089877aSRichard Tran Mills 
1824f089877aSRichard Tran Mills   PetscFunctionBegin;
1825f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1826f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
1827f089877aSRichard Tran Mills   PetscFunctionReturn(0);
1828f089877aSRichard Tran Mills }
1829f089877aSRichard Tran Mills 
1830f089877aSRichard Tran Mills #undef __FUNCT__
1831f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
1832f089877aSRichard Tran Mills /*@
1833bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
1834bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
1835*d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
1836f089877aSRichard Tran Mills 
1837bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
1838f089877aSRichard Tran Mills 
1839f089877aSRichard Tran Mills    Input Parameters:
1840bc0a1609SRichard Tran Mills +  da - the DM object
1841bc0a1609SRichard Tran Mills .  g - the original local vector
1842bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
1843f089877aSRichard Tran Mills 
1844bc0a1609SRichard Tran Mills    Output Parameter:
1845bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
1846f089877aSRichard Tran Mills 
1847f089877aSRichard Tran Mills    Level: intermediate
1848f089877aSRichard Tran Mills 
1849bc0a1609SRichard Tran Mills    Notes:
1850bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
1851bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
1852bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
1853bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
1854bc0a1609SRichard Tran Mills 
1855bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
1856bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
1857f089877aSRichard Tran Mills 
1858f089877aSRichard Tran Mills @*/
1859f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
1860f089877aSRichard Tran Mills {
1861f089877aSRichard Tran Mills   PetscSF                 sf;
1862f089877aSRichard Tran Mills   PetscErrorCode          ierr;
1863f089877aSRichard Tran Mills   PetscScalar             *lArray, *gArray;
1864f089877aSRichard Tran Mills   DMGlobalToLocalHookLink link;
1865f089877aSRichard Tran Mills 
1866f089877aSRichard Tran Mills   PetscFunctionBegin;
1867f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1868f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
1869f089877aSRichard Tran Mills   PetscFunctionReturn(0);
1870f089877aSRichard Tran Mills }
1871f089877aSRichard Tran Mills 
1872f089877aSRichard Tran Mills 
1873f089877aSRichard Tran Mills #undef __FUNCT__
187447c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
187547c6ae99SBarry Smith /*@
187647c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
187747c6ae99SBarry Smith 
187847c6ae99SBarry Smith     Collective on DM
187947c6ae99SBarry Smith 
188047c6ae99SBarry Smith     Input Parameter:
188147c6ae99SBarry Smith +   dm - the DM object
188291d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
188347c6ae99SBarry Smith 
188447c6ae99SBarry Smith     Output Parameter:
188547c6ae99SBarry Smith .   dmc - the coarsened DM
188647c6ae99SBarry Smith 
188747c6ae99SBarry Smith     Level: developer
188847c6ae99SBarry Smith 
1889e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
189047c6ae99SBarry Smith 
189147c6ae99SBarry Smith @*/
18927087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
189347c6ae99SBarry Smith {
189447c6ae99SBarry Smith   PetscErrorCode    ierr;
1895b17ce1afSJed Brown   DMCoarsenHookLink link;
189647c6ae99SBarry Smith 
189747c6ae99SBarry Smith   PetscFunctionBegin;
1898171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
189947c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
190043842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
19018cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1902644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
19030598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
1904656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
1905e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1906b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1907b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1908b17ce1afSJed Brown   }
1909b17ce1afSJed Brown   PetscFunctionReturn(0);
1910b17ce1afSJed Brown }
1911b17ce1afSJed Brown 
1912b17ce1afSJed Brown #undef __FUNCT__
1913b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1914bb9467b5SJed Brown /*@C
1915b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1916b17ce1afSJed Brown 
1917b17ce1afSJed Brown    Logically Collective
1918b17ce1afSJed Brown 
1919b17ce1afSJed Brown    Input Arguments:
1920b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1921b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1922b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
19230298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1924b17ce1afSJed Brown 
1925b17ce1afSJed Brown    Calling sequence of coarsenhook:
1926b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1927b17ce1afSJed Brown 
1928b17ce1afSJed Brown +  fine - fine level DM
1929b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1930b17ce1afSJed Brown -  ctx - optional user-defined function context
1931b17ce1afSJed Brown 
1932b17ce1afSJed Brown    Calling sequence for restricthook:
1933c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1934b17ce1afSJed Brown 
1935b17ce1afSJed Brown +  fine - fine level DM
1936b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1937c833c3b5SJed Brown .  rscale - scaling vector for restriction
1938c833c3b5SJed Brown .  inject - matrix restricting by injection
1939b17ce1afSJed Brown .  coarse - coarse level DM to update
1940b17ce1afSJed Brown -  ctx - optional user-defined function context
1941b17ce1afSJed Brown 
1942b17ce1afSJed Brown    Level: advanced
1943b17ce1afSJed Brown 
1944b17ce1afSJed Brown    Notes:
1945b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1946b17ce1afSJed Brown 
1947b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1948b17ce1afSJed Brown 
1949b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1950b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1951b17ce1afSJed Brown 
1952bb9467b5SJed Brown    This function is currently not available from Fortran.
1953bb9467b5SJed Brown 
1954c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1955b17ce1afSJed Brown @*/
1956b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1957b17ce1afSJed Brown {
1958b17ce1afSJed Brown   PetscErrorCode    ierr;
1959b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1960b17ce1afSJed Brown 
1961b17ce1afSJed Brown   PetscFunctionBegin;
1962b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
19636bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1964b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1965b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
1966b17ce1afSJed Brown   link->restricthook = restricthook;
1967b17ce1afSJed Brown   link->ctx          = ctx;
19680298fd71SBarry Smith   link->next         = NULL;
1969b17ce1afSJed Brown   *p                 = link;
1970b17ce1afSJed Brown   PetscFunctionReturn(0);
1971b17ce1afSJed Brown }
1972b17ce1afSJed Brown 
1973b17ce1afSJed Brown #undef __FUNCT__
1974b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1975b17ce1afSJed Brown /*@
1976b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1977b17ce1afSJed Brown 
1978b17ce1afSJed Brown    Collective if any hooks are
1979b17ce1afSJed Brown 
1980b17ce1afSJed Brown    Input Arguments:
1981b17ce1afSJed Brown +  fine - finer DM to use as a base
1982b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1983b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1984b17ce1afSJed Brown -  coarse - coarer DM to update
1985b17ce1afSJed Brown 
1986b17ce1afSJed Brown    Level: developer
1987b17ce1afSJed Brown 
1988b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1989b17ce1afSJed Brown @*/
1990b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1991b17ce1afSJed Brown {
1992b17ce1afSJed Brown   PetscErrorCode    ierr;
1993b17ce1afSJed Brown   DMCoarsenHookLink link;
1994b17ce1afSJed Brown 
1995b17ce1afSJed Brown   PetscFunctionBegin;
1996b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
19978865f1eaSKarl Rupp     if (link->restricthook) {
19988865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
19998865f1eaSKarl Rupp     }
2000b17ce1afSJed Brown   }
200147c6ae99SBarry Smith   PetscFunctionReturn(0);
200247c6ae99SBarry Smith }
200347c6ae99SBarry Smith 
200447c6ae99SBarry Smith #undef __FUNCT__
2005be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2006bb9467b5SJed Brown /*@C
2007be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
20085dbd56e3SPeter Brune 
20095dbd56e3SPeter Brune    Logically Collective
20105dbd56e3SPeter Brune 
20115dbd56e3SPeter Brune    Input Arguments:
20125dbd56e3SPeter Brune +  global - global DM
2013ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
20145dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
20150298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
20165dbd56e3SPeter Brune 
2017ec4806b8SPeter Brune 
2018ec4806b8SPeter Brune    Calling sequence for ddhook:
2019ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2020ec4806b8SPeter Brune 
2021ec4806b8SPeter Brune +  global - global DM
2022ec4806b8SPeter Brune .  block  - block DM
2023ec4806b8SPeter Brune -  ctx - optional user-defined function context
2024ec4806b8SPeter Brune 
20255dbd56e3SPeter Brune    Calling sequence for restricthook:
2026ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
20275dbd56e3SPeter Brune 
20285dbd56e3SPeter Brune +  global - global DM
20295dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
20305dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2031ec4806b8SPeter Brune .  block  - block DM
20325dbd56e3SPeter Brune -  ctx - optional user-defined function context
20335dbd56e3SPeter Brune 
20345dbd56e3SPeter Brune    Level: advanced
20355dbd56e3SPeter Brune 
20365dbd56e3SPeter Brune    Notes:
2037ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
20385dbd56e3SPeter Brune 
20395dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
20405dbd56e3SPeter Brune 
20415dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2042ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
20435dbd56e3SPeter Brune 
2044bb9467b5SJed Brown    This function is currently not available from Fortran.
2045bb9467b5SJed Brown 
20465dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20475dbd56e3SPeter Brune @*/
2048be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
20495dbd56e3SPeter Brune {
20505dbd56e3SPeter Brune   PetscErrorCode      ierr;
2051be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
20525dbd56e3SPeter Brune 
20535dbd56e3SPeter Brune   PetscFunctionBegin;
20545dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2055be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2056be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
20575dbd56e3SPeter Brune   link->restricthook = restricthook;
2058be081cd6SPeter Brune   link->ddhook       = ddhook;
20595dbd56e3SPeter Brune   link->ctx          = ctx;
20600298fd71SBarry Smith   link->next         = NULL;
20615dbd56e3SPeter Brune   *p                 = link;
20625dbd56e3SPeter Brune   PetscFunctionReturn(0);
20635dbd56e3SPeter Brune }
20645dbd56e3SPeter Brune 
20655dbd56e3SPeter Brune #undef __FUNCT__
2066be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
20675dbd56e3SPeter Brune /*@
2068be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
20695dbd56e3SPeter Brune 
20705dbd56e3SPeter Brune    Collective if any hooks are
20715dbd56e3SPeter Brune 
20725dbd56e3SPeter Brune    Input Arguments:
20735dbd56e3SPeter Brune +  fine - finer DM to use as a base
2074be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2075be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
20765dbd56e3SPeter Brune -  coarse - coarer DM to update
20775dbd56e3SPeter Brune 
20785dbd56e3SPeter Brune    Level: developer
20795dbd56e3SPeter Brune 
20805dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
20815dbd56e3SPeter Brune @*/
2082be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
20835dbd56e3SPeter Brune {
20845dbd56e3SPeter Brune   PetscErrorCode      ierr;
2085be081cd6SPeter Brune   DMSubDomainHookLink link;
20865dbd56e3SPeter Brune 
20875dbd56e3SPeter Brune   PetscFunctionBegin;
2088be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
20898865f1eaSKarl Rupp     if (link->restricthook) {
20908865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
20918865f1eaSKarl Rupp     }
20925dbd56e3SPeter Brune   }
20935dbd56e3SPeter Brune   PetscFunctionReturn(0);
20945dbd56e3SPeter Brune }
20955dbd56e3SPeter Brune 
20965dbd56e3SPeter Brune #undef __FUNCT__
20975fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
20985fe1f584SPeter Brune /*@
20996a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
21005fe1f584SPeter Brune 
21015fe1f584SPeter Brune     Not Collective
21025fe1f584SPeter Brune 
21035fe1f584SPeter Brune     Input Parameter:
21045fe1f584SPeter Brune .   dm - the DM object
21055fe1f584SPeter Brune 
21065fe1f584SPeter Brune     Output Parameter:
21076a7d9d85SPeter Brune .   level - number of coarsenings
21085fe1f584SPeter Brune 
21095fe1f584SPeter Brune     Level: developer
21105fe1f584SPeter Brune 
21116a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
21125fe1f584SPeter Brune 
21135fe1f584SPeter Brune @*/
21145fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
21155fe1f584SPeter Brune {
21165fe1f584SPeter Brune   PetscFunctionBegin;
21175fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21185fe1f584SPeter Brune   *level = dm->leveldown;
21195fe1f584SPeter Brune   PetscFunctionReturn(0);
21205fe1f584SPeter Brune }
21215fe1f584SPeter Brune 
21225fe1f584SPeter Brune 
21235fe1f584SPeter Brune 
21245fe1f584SPeter Brune #undef __FUNCT__
212547c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
212647c6ae99SBarry Smith /*@C
212747c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
212847c6ae99SBarry Smith 
212947c6ae99SBarry Smith     Collective on DM
213047c6ae99SBarry Smith 
213147c6ae99SBarry Smith     Input Parameter:
213247c6ae99SBarry Smith +   dm - the DM object
213347c6ae99SBarry Smith -   nlevels - the number of levels of refinement
213447c6ae99SBarry Smith 
213547c6ae99SBarry Smith     Output Parameter:
213647c6ae99SBarry Smith .   dmf - the refined DM hierarchy
213747c6ae99SBarry Smith 
213847c6ae99SBarry Smith     Level: developer
213947c6ae99SBarry Smith 
2140e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
214147c6ae99SBarry Smith 
214247c6ae99SBarry Smith @*/
21437087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
214447c6ae99SBarry Smith {
214547c6ae99SBarry Smith   PetscErrorCode ierr;
214647c6ae99SBarry Smith 
214747c6ae99SBarry Smith   PetscFunctionBegin;
2148171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2149ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
215047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
215147c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
215247c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
215347c6ae99SBarry Smith   } else if (dm->ops->refine) {
215447c6ae99SBarry Smith     PetscInt i;
215547c6ae99SBarry Smith 
2156ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
215747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2158ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
215947c6ae99SBarry Smith     }
2160ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
216147c6ae99SBarry Smith   PetscFunctionReturn(0);
216247c6ae99SBarry Smith }
216347c6ae99SBarry Smith 
216447c6ae99SBarry Smith #undef __FUNCT__
216547c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
216647c6ae99SBarry Smith /*@C
216747c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
216847c6ae99SBarry Smith 
216947c6ae99SBarry Smith     Collective on DM
217047c6ae99SBarry Smith 
217147c6ae99SBarry Smith     Input Parameter:
217247c6ae99SBarry Smith +   dm - the DM object
217347c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
217447c6ae99SBarry Smith 
217547c6ae99SBarry Smith     Output Parameter:
217647c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
217747c6ae99SBarry Smith 
217847c6ae99SBarry Smith     Level: developer
217947c6ae99SBarry Smith 
2180e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
218147c6ae99SBarry Smith 
218247c6ae99SBarry Smith @*/
21837087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
218447c6ae99SBarry Smith {
218547c6ae99SBarry Smith   PetscErrorCode ierr;
218647c6ae99SBarry Smith 
218747c6ae99SBarry Smith   PetscFunctionBegin;
2188171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2189ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
219047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
219147c6ae99SBarry Smith   PetscValidPointer(dmc,3);
219247c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
219347c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
219447c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
219547c6ae99SBarry Smith     PetscInt i;
219647c6ae99SBarry Smith 
2197ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
219847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2199ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
220047c6ae99SBarry Smith     }
2201ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
220247c6ae99SBarry Smith   PetscFunctionReturn(0);
220347c6ae99SBarry Smith }
220447c6ae99SBarry Smith 
220547c6ae99SBarry Smith #undef __FUNCT__
2206e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
220747c6ae99SBarry Smith /*@
2208e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
220947c6ae99SBarry Smith    grids associated with two DMs.
221047c6ae99SBarry Smith 
221147c6ae99SBarry Smith    Collective on DM
221247c6ae99SBarry Smith 
221347c6ae99SBarry Smith    Input Parameters:
221447c6ae99SBarry Smith +  dmc - the coarse grid DM
221547c6ae99SBarry Smith -  dmf - the fine grid DM
221647c6ae99SBarry Smith 
221747c6ae99SBarry Smith    Output Parameters:
221847c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
221947c6ae99SBarry Smith 
222047c6ae99SBarry Smith    Level: intermediate
222147c6ae99SBarry Smith 
222247c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
222347c6ae99SBarry Smith 
2224e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
222547c6ae99SBarry Smith @*/
2226e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
222747c6ae99SBarry Smith {
222847c6ae99SBarry Smith   PetscErrorCode ierr;
222947c6ae99SBarry Smith 
223047c6ae99SBarry Smith   PetscFunctionBegin;
2231171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2232171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
223347c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
223447c6ae99SBarry Smith   PetscFunctionReturn(0);
223547c6ae99SBarry Smith }
223647c6ae99SBarry Smith 
223747c6ae99SBarry Smith #undef __FUNCT__
22381a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
22391a266240SBarry Smith /*@C
22401a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
22411a266240SBarry Smith 
22421a266240SBarry Smith     Not Collective
22431a266240SBarry Smith 
22441a266240SBarry Smith     Input Parameters:
22451a266240SBarry Smith +   dm - the DM object
22461a266240SBarry Smith -   destroy - the destroy function
22471a266240SBarry Smith 
22481a266240SBarry Smith     Level: intermediate
22491a266240SBarry Smith 
2250e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
22511a266240SBarry Smith 
2252f07f9ceaSJed Brown @*/
22531a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
22541a266240SBarry Smith {
22551a266240SBarry Smith   PetscFunctionBegin;
2256171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22571a266240SBarry Smith   dm->ctxdestroy = destroy;
22581a266240SBarry Smith   PetscFunctionReturn(0);
22591a266240SBarry Smith }
22601a266240SBarry Smith 
22611a266240SBarry Smith #undef __FUNCT__
22621b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2263b07ff414SBarry Smith /*@
22641b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
226547c6ae99SBarry Smith 
226647c6ae99SBarry Smith     Not Collective
226747c6ae99SBarry Smith 
226847c6ae99SBarry Smith     Input Parameters:
226947c6ae99SBarry Smith +   dm - the DM object
227047c6ae99SBarry Smith -   ctx - the user context
227147c6ae99SBarry Smith 
227247c6ae99SBarry Smith     Level: intermediate
227347c6ae99SBarry Smith 
2274e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
227547c6ae99SBarry Smith 
227647c6ae99SBarry Smith @*/
22771b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
227847c6ae99SBarry Smith {
227947c6ae99SBarry Smith   PetscFunctionBegin;
2280171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
228147c6ae99SBarry Smith   dm->ctx = ctx;
228247c6ae99SBarry Smith   PetscFunctionReturn(0);
228347c6ae99SBarry Smith }
228447c6ae99SBarry Smith 
228547c6ae99SBarry Smith #undef __FUNCT__
22861b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
228747c6ae99SBarry Smith /*@
22881b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
228947c6ae99SBarry Smith 
229047c6ae99SBarry Smith     Not Collective
229147c6ae99SBarry Smith 
229247c6ae99SBarry Smith     Input Parameter:
229347c6ae99SBarry Smith .   dm - the DM object
229447c6ae99SBarry Smith 
229547c6ae99SBarry Smith     Output Parameter:
229647c6ae99SBarry Smith .   ctx - the user context
229747c6ae99SBarry Smith 
229847c6ae99SBarry Smith     Level: intermediate
229947c6ae99SBarry Smith 
2300e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
230147c6ae99SBarry Smith 
230247c6ae99SBarry Smith @*/
23031b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
230447c6ae99SBarry Smith {
230547c6ae99SBarry Smith   PetscFunctionBegin;
2306171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23071b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
230847c6ae99SBarry Smith   PetscFunctionReturn(0);
230947c6ae99SBarry Smith }
231047c6ae99SBarry Smith 
231147c6ae99SBarry Smith #undef __FUNCT__
231208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
231308da532bSDmitry Karpeev /*@C
231408da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
231508da532bSDmitry Karpeev 
231608da532bSDmitry Karpeev     Logically Collective on DM
231708da532bSDmitry Karpeev 
231808da532bSDmitry Karpeev     Input Parameter:
231908da532bSDmitry Karpeev +   dm - the DM object
23200298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
232108da532bSDmitry Karpeev 
232208da532bSDmitry Karpeev     Level: intermediate
232308da532bSDmitry Karpeev 
2324835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
232508da532bSDmitry Karpeev          DMSetJacobian()
232608da532bSDmitry Karpeev 
232708da532bSDmitry Karpeev @*/
232808da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
232908da532bSDmitry Karpeev {
233008da532bSDmitry Karpeev   PetscFunctionBegin;
233108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
233208da532bSDmitry Karpeev   PetscFunctionReturn(0);
233308da532bSDmitry Karpeev }
233408da532bSDmitry Karpeev 
233508da532bSDmitry Karpeev #undef __FUNCT__
233608da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
233708da532bSDmitry Karpeev /*@
233808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
233908da532bSDmitry Karpeev 
234008da532bSDmitry Karpeev     Not Collective
234108da532bSDmitry Karpeev 
234208da532bSDmitry Karpeev     Input Parameter:
234308da532bSDmitry Karpeev .   dm - the DM object to destroy
234408da532bSDmitry Karpeev 
234508da532bSDmitry Karpeev     Output Parameter:
234608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
234708da532bSDmitry Karpeev 
234808da532bSDmitry Karpeev     Level: developer
234908da532bSDmitry Karpeev 
235074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
235108da532bSDmitry Karpeev 
235208da532bSDmitry Karpeev @*/
235308da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
235408da532bSDmitry Karpeev {
235508da532bSDmitry Karpeev   PetscFunctionBegin;
235608da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
235708da532bSDmitry Karpeev   PetscFunctionReturn(0);
235808da532bSDmitry Karpeev }
235908da532bSDmitry Karpeev 
236008da532bSDmitry Karpeev #undef __FUNCT__
236108da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
236208da532bSDmitry Karpeev /*@C
236308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
236408da532bSDmitry Karpeev 
236508da532bSDmitry Karpeev     Logically Collective on DM
236608da532bSDmitry Karpeev 
236708da532bSDmitry Karpeev     Input Parameters:
236808da532bSDmitry Karpeev +   dm - the DM object to destroy
236908da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
237008da532bSDmitry Karpeev 
237108da532bSDmitry Karpeev     Output parameters:
237208da532bSDmitry Karpeev +   xl - lower bound
237308da532bSDmitry Karpeev -   xu - upper bound
237408da532bSDmitry Karpeev 
237508da532bSDmitry Karpeev     Level: intermediate
237608da532bSDmitry Karpeev 
237774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
237808da532bSDmitry Karpeev 
237908da532bSDmitry Karpeev @*/
238008da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
238108da532bSDmitry Karpeev {
238208da532bSDmitry Karpeev   PetscErrorCode ierr;
23835fd66863SKarl Rupp 
238408da532bSDmitry Karpeev   PetscFunctionBegin;
238508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
238608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
238708da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
238808da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
23898865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
239008da532bSDmitry Karpeev   PetscFunctionReturn(0);
239108da532bSDmitry Karpeev }
239208da532bSDmitry Karpeev 
239308da532bSDmitry Karpeev #undef __FUNCT__
2394b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2395b0ae01b7SPeter Brune /*@
2396b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2397b0ae01b7SPeter Brune 
2398b0ae01b7SPeter Brune     Not Collective
2399b0ae01b7SPeter Brune 
2400b0ae01b7SPeter Brune     Input Parameter:
2401b0ae01b7SPeter Brune .   dm - the DM object
2402b0ae01b7SPeter Brune 
2403b0ae01b7SPeter Brune     Output Parameter:
2404b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2405b0ae01b7SPeter Brune 
2406b0ae01b7SPeter Brune     Level: developer
2407b0ae01b7SPeter Brune 
2408b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2409b0ae01b7SPeter Brune 
2410b0ae01b7SPeter Brune @*/
2411b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2412b0ae01b7SPeter Brune {
2413b0ae01b7SPeter Brune   PetscFunctionBegin;
2414b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2415b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2416b0ae01b7SPeter Brune }
2417b0ae01b7SPeter Brune 
2418b0ae01b7SPeter Brune #undef  __FUNCT__
241908da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2420748fac09SDmitry Karpeev /*@C
242108da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
242208da532bSDmitry Karpeev 
242308da532bSDmitry Karpeev     Collective on DM
242408da532bSDmitry Karpeev 
242508da532bSDmitry Karpeev     Input Parameter:
242608da532bSDmitry Karpeev +   dm - the DM object
24270298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
242808da532bSDmitry Karpeev 
242908da532bSDmitry Karpeev     Level: developer
243008da532bSDmitry Karpeev 
243174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
243208da532bSDmitry Karpeev 
243308da532bSDmitry Karpeev @*/
243408da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
243508da532bSDmitry Karpeev {
243608da532bSDmitry Karpeev   PetscErrorCode ierr;
24375fd66863SKarl Rupp 
243808da532bSDmitry Karpeev   PetscFunctionBegin;
243908da532bSDmitry Karpeev   if (x) {
244008da532bSDmitry Karpeev     if (!dm->x) {
244108da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
244208da532bSDmitry Karpeev     }
244308da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
24448865f1eaSKarl Rupp   } else if (dm->x) {
244508da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
244608da532bSDmitry Karpeev   }
244708da532bSDmitry Karpeev   PetscFunctionReturn(0);
244808da532bSDmitry Karpeev }
244908da532bSDmitry Karpeev 
24500298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2451264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2452264ace61SBarry Smith 
2453264ace61SBarry Smith #undef __FUNCT__
2454264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2455264ace61SBarry Smith /*@C
2456264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2457264ace61SBarry Smith 
2458264ace61SBarry Smith   Collective on DM
2459264ace61SBarry Smith 
2460264ace61SBarry Smith   Input Parameters:
2461264ace61SBarry Smith + dm     - The DM object
2462264ace61SBarry Smith - method - The name of the DM type
2463264ace61SBarry Smith 
2464264ace61SBarry Smith   Options Database Key:
2465264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2466264ace61SBarry Smith 
2467264ace61SBarry Smith   Notes:
2468e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2469264ace61SBarry Smith 
2470264ace61SBarry Smith   Level: intermediate
2471264ace61SBarry Smith 
2472264ace61SBarry Smith .keywords: DM, set, type
2473264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2474264ace61SBarry Smith @*/
247519fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2476264ace61SBarry Smith {
2477264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2478264ace61SBarry Smith   PetscBool      match;
2479264ace61SBarry Smith   PetscErrorCode ierr;
2480264ace61SBarry Smith 
2481264ace61SBarry Smith   PetscFunctionBegin;
2482264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2483251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2484264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2485264ace61SBarry Smith 
2486607a6623SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll();CHKERRQ(ierr);}
24871c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2488ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2489264ace61SBarry Smith 
2490264ace61SBarry Smith   if (dm->ops->destroy) {
2491264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
24920298fd71SBarry Smith     dm->ops->destroy = NULL;
2493264ace61SBarry Smith   }
2494264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2495264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2496264ace61SBarry Smith   PetscFunctionReturn(0);
2497264ace61SBarry Smith }
2498264ace61SBarry Smith 
2499264ace61SBarry Smith #undef __FUNCT__
2500264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2501264ace61SBarry Smith /*@C
2502264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2503264ace61SBarry Smith 
2504264ace61SBarry Smith   Not Collective
2505264ace61SBarry Smith 
2506264ace61SBarry Smith   Input Parameter:
2507264ace61SBarry Smith . dm  - The DM
2508264ace61SBarry Smith 
2509264ace61SBarry Smith   Output Parameter:
2510264ace61SBarry Smith . type - The DM type name
2511264ace61SBarry Smith 
2512264ace61SBarry Smith   Level: intermediate
2513264ace61SBarry Smith 
2514264ace61SBarry Smith .keywords: DM, get, type, name
2515264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2516264ace61SBarry Smith @*/
251719fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2518264ace61SBarry Smith {
2519264ace61SBarry Smith   PetscErrorCode ierr;
2520264ace61SBarry Smith 
2521264ace61SBarry Smith   PetscFunctionBegin;
2522264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2523264ace61SBarry Smith   PetscValidCharPointer(type,2);
2524264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2525607a6623SBarry Smith     ierr = DMRegisterAll();CHKERRQ(ierr);
2526264ace61SBarry Smith   }
2527264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2528264ace61SBarry Smith   PetscFunctionReturn(0);
2529264ace61SBarry Smith }
2530264ace61SBarry Smith 
253167a56275SMatthew G Knepley #undef __FUNCT__
253267a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
253367a56275SMatthew G Knepley /*@C
253467a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
253567a56275SMatthew G Knepley 
253667a56275SMatthew G Knepley   Collective on DM
253767a56275SMatthew G Knepley 
253867a56275SMatthew G Knepley   Input Parameters:
253967a56275SMatthew G Knepley + dm - the DM
254067a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
254167a56275SMatthew G Knepley 
254267a56275SMatthew G Knepley   Output Parameter:
254367a56275SMatthew G Knepley . M - pointer to new DM
254467a56275SMatthew G Knepley 
254567a56275SMatthew G Knepley   Notes:
254667a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
254767a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
254867a56275SMatthew G Knepley   of the input DM.
254967a56275SMatthew G Knepley 
255067a56275SMatthew G Knepley   Level: intermediate
255167a56275SMatthew G Knepley 
255267a56275SMatthew G Knepley .seealso: DMCreate()
255367a56275SMatthew G Knepley @*/
255419fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
255567a56275SMatthew G Knepley {
255667a56275SMatthew G Knepley   DM             B;
255767a56275SMatthew G Knepley   char           convname[256];
255867a56275SMatthew G Knepley   PetscBool      sametype, issame;
255967a56275SMatthew G Knepley   PetscErrorCode ierr;
256067a56275SMatthew G Knepley 
256167a56275SMatthew G Knepley   PetscFunctionBegin;
256267a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
256367a56275SMatthew G Knepley   PetscValidType(dm,1);
256467a56275SMatthew G Knepley   PetscValidPointer(M,3);
2565251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
256667a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
256767a56275SMatthew G Knepley   {
25680298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
256967a56275SMatthew G Knepley 
257067a56275SMatthew G Knepley     /*
257167a56275SMatthew G Knepley        Order of precedence:
257267a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
257367a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
257467a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
257567a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
257667a56275SMatthew G Knepley        5) Use a really basic converter.
257767a56275SMatthew G Knepley     */
257867a56275SMatthew G Knepley 
257967a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
258067a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
258167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
258267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
258367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
258467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
25850005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
258667a56275SMatthew G Knepley     if (conv) goto foundconv;
258767a56275SMatthew G Knepley 
258867a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
258982f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
259067a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
259167a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
259267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
259367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
259467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
259567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
25960005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
259767a56275SMatthew G Knepley     if (conv) {
2598fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
259967a56275SMatthew G Knepley       goto foundconv;
260067a56275SMatthew G Knepley     }
260167a56275SMatthew G Knepley 
260267a56275SMatthew G Knepley #if 0
260367a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
260467a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2605fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
260667a56275SMatthew G Knepley     if (conv) goto foundconv;
260767a56275SMatthew G Knepley 
260867a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
260967a56275SMatthew G Knepley     if (dm->ops->convert) {
261067a56275SMatthew G Knepley       conv = dm->ops->convert;
261167a56275SMatthew G Knepley     }
261267a56275SMatthew G Knepley     if (conv) goto foundconv;
261367a56275SMatthew G Knepley #endif
261467a56275SMatthew G Knepley 
261567a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
261682f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
261767a56275SMatthew G Knepley 
261867a56275SMatthew G Knepley foundconv:
261967a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
262067a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
262167a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
262267a56275SMatthew G Knepley   }
262367a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
262467a56275SMatthew G Knepley   PetscFunctionReturn(0);
262567a56275SMatthew G Knepley }
2626264ace61SBarry Smith 
2627264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2628264ace61SBarry Smith 
2629264ace61SBarry Smith #undef __FUNCT__
2630264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2631264ace61SBarry Smith /*@C
26321c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
26331c84c290SBarry Smith 
26341c84c290SBarry Smith   Not Collective
26351c84c290SBarry Smith 
26361c84c290SBarry Smith   Input Parameters:
26371c84c290SBarry Smith + name        - The name of a new user-defined creation routine
26381c84c290SBarry Smith - create_func - The creation routine itself
26391c84c290SBarry Smith 
26401c84c290SBarry Smith   Notes:
26411c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
26421c84c290SBarry Smith 
26431c84c290SBarry Smith 
26441c84c290SBarry Smith   Sample usage:
26451c84c290SBarry Smith .vb
2646bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
26471c84c290SBarry Smith .ve
26481c84c290SBarry Smith 
26491c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
26501c84c290SBarry Smith .vb
26511c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
26521c84c290SBarry Smith     DMSetType(DM,"my_da");
26531c84c290SBarry Smith .ve
26541c84c290SBarry Smith    or at runtime via the option
26551c84c290SBarry Smith .vb
26561c84c290SBarry Smith     -da_type my_da
26571c84c290SBarry Smith .ve
2658264ace61SBarry Smith 
2659264ace61SBarry Smith   Level: advanced
26601c84c290SBarry Smith 
26611c84c290SBarry Smith .keywords: DM, register
2662bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
26631c84c290SBarry Smith 
2664264ace61SBarry Smith @*/
2665bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2666264ace61SBarry Smith {
2667264ace61SBarry Smith   PetscErrorCode ierr;
2668264ace61SBarry Smith 
2669264ace61SBarry Smith   PetscFunctionBegin;
2670a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2671264ace61SBarry Smith   PetscFunctionReturn(0);
2672264ace61SBarry Smith }
2673264ace61SBarry Smith 
2674b859378eSBarry Smith #undef __FUNCT__
2675b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2676b859378eSBarry Smith /*@C
267755849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2678b859378eSBarry Smith 
2679b859378eSBarry Smith   Collective on PetscViewer
2680b859378eSBarry Smith 
2681b859378eSBarry Smith   Input Parameters:
2682b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2683b859378eSBarry Smith            some related function before a call to DMLoad().
2684b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2685b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2686b859378eSBarry Smith 
2687b859378eSBarry Smith    Level: intermediate
2688b859378eSBarry Smith 
2689b859378eSBarry Smith   Notes:
269055849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2691b859378eSBarry Smith 
2692b859378eSBarry Smith   Notes for advanced users:
2693b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2694b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2695b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2696b859378eSBarry Smith   format is
2697b859378eSBarry Smith .vb
2698b859378eSBarry Smith      has not yet been determined
2699b859378eSBarry Smith .ve
2700b859378eSBarry Smith 
2701b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2702b859378eSBarry Smith @*/
2703b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2704b859378eSBarry Smith {
2705b859378eSBarry Smith   PetscErrorCode ierr;
270632c0f0efSBarry Smith   PetscBool      isbinary;
270755849f57SBarry Smith   PetscInt       classid;
270832c0f0efSBarry Smith   char           type[256];
2709b859378eSBarry Smith 
2710b859378eSBarry Smith   PetscFunctionBegin;
2711b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2712b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
271332c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
271432c0f0efSBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
2715b859378eSBarry Smith 
271632c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
2717ce94432eSBarry Smith   if (classid != DM_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file");
271832c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
271932c0f0efSBarry Smith   ierr = DMSetType(newdm, type);CHKERRQ(ierr);
27202d53ad75SBarry Smith   if (newdm->ops->load) {
2721b859378eSBarry Smith     ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
27222d53ad75SBarry Smith   }
2723b859378eSBarry Smith   PetscFunctionReturn(0);
2724b859378eSBarry Smith }
2725b859378eSBarry Smith 
27267da65231SMatthew G Knepley /******************************** FEM Support **********************************/
27277da65231SMatthew G Knepley 
27287da65231SMatthew G Knepley #undef __FUNCT__
27297da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
2730a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
2731a6dfd86eSKarl Rupp {
27321d47ebbbSSatish Balay   PetscInt       f;
27331b30c384SMatthew G Knepley   PetscErrorCode ierr;
27341b30c384SMatthew G Knepley 
27357da65231SMatthew G Knepley   PetscFunctionBegin;
273674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27371d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
273874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
27397da65231SMatthew G Knepley   }
27407da65231SMatthew G Knepley   PetscFunctionReturn(0);
27417da65231SMatthew G Knepley }
27427da65231SMatthew G Knepley 
27437da65231SMatthew G Knepley #undef __FUNCT__
27447da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
2745a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
2746a6dfd86eSKarl Rupp {
27471b30c384SMatthew G Knepley   PetscInt       f, g;
27487da65231SMatthew G Knepley   PetscErrorCode ierr;
27497da65231SMatthew G Knepley 
27507da65231SMatthew G Knepley   PetscFunctionBegin;
275174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27521d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
275374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
27541d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
275574778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
27567da65231SMatthew G Knepley     }
275774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
27587da65231SMatthew G Knepley   }
27597da65231SMatthew G Knepley   PetscFunctionReturn(0);
27607da65231SMatthew G Knepley }
2761e7c4fc90SDmitry Karpeev 
2762970e74d5SMatthew G Knepley #undef __FUNCT__
276388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
276488ed4aceSMatthew G Knepley /*@
276588ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
276688ed4aceSMatthew G Knepley 
276788ed4aceSMatthew G Knepley   Input Parameter:
276888ed4aceSMatthew G Knepley . dm - The DM
276988ed4aceSMatthew G Knepley 
277088ed4aceSMatthew G Knepley   Output Parameter:
277188ed4aceSMatthew G Knepley . section - The PetscSection
277288ed4aceSMatthew G Knepley 
277388ed4aceSMatthew G Knepley   Level: intermediate
277488ed4aceSMatthew G Knepley 
277588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
277688ed4aceSMatthew G Knepley 
277788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
277888ed4aceSMatthew G Knepley @*/
27790adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
27800adebc6cSBarry Smith {
278188ed4aceSMatthew G Knepley   PetscFunctionBegin;
278288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
278388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
278488ed4aceSMatthew G Knepley   *section = dm->defaultSection;
278588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
278688ed4aceSMatthew G Knepley }
278788ed4aceSMatthew G Knepley 
278888ed4aceSMatthew G Knepley #undef __FUNCT__
278988ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
279088ed4aceSMatthew G Knepley /*@
279188ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
279288ed4aceSMatthew G Knepley 
279388ed4aceSMatthew G Knepley   Input Parameters:
279488ed4aceSMatthew G Knepley + dm - The DM
279588ed4aceSMatthew G Knepley - section - The PetscSection
279688ed4aceSMatthew G Knepley 
279788ed4aceSMatthew G Knepley   Level: intermediate
279888ed4aceSMatthew G Knepley 
279988ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
280088ed4aceSMatthew G Knepley 
280188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
280288ed4aceSMatthew G Knepley @*/
28030adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
28040adebc6cSBarry Smith {
2805af122d2aSMatthew G Knepley   PetscInt       numFields;
2806af122d2aSMatthew G Knepley   PetscInt       f;
280788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
280888ed4aceSMatthew G Knepley 
280988ed4aceSMatthew G Knepley   PetscFunctionBegin;
281088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28111d799100SJed Brown   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
28121d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
281388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
281488ed4aceSMatthew G Knepley   dm->defaultSection = section;
2815af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
2816af122d2aSMatthew G Knepley   if (numFields) {
2817af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
2818af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
2819af122d2aSMatthew G Knepley       const char *name;
2820af122d2aSMatthew G Knepley 
2821af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
2822af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
2823af122d2aSMatthew G Knepley     }
2824af122d2aSMatthew G Knepley   }
28251d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
28261d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
282788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
282888ed4aceSMatthew G Knepley }
282988ed4aceSMatthew G Knepley 
283088ed4aceSMatthew G Knepley #undef __FUNCT__
283188ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
283288ed4aceSMatthew G Knepley /*@
283388ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
283488ed4aceSMatthew G Knepley 
28358b1ab98fSJed Brown   Collective on DM
28368b1ab98fSJed Brown 
283788ed4aceSMatthew G Knepley   Input Parameter:
283888ed4aceSMatthew G Knepley . dm - The DM
283988ed4aceSMatthew G Knepley 
284088ed4aceSMatthew G Knepley   Output Parameter:
284188ed4aceSMatthew G Knepley . section - The PetscSection
284288ed4aceSMatthew G Knepley 
284388ed4aceSMatthew G Knepley   Level: intermediate
284488ed4aceSMatthew G Knepley 
284588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
284688ed4aceSMatthew G Knepley 
284788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
284888ed4aceSMatthew G Knepley @*/
28490adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
28500adebc6cSBarry Smith {
285188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
285288ed4aceSMatthew G Knepley 
285388ed4aceSMatthew G Knepley   PetscFunctionBegin;
285488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
285588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
285688ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
285782f516ccSBarry 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");
2858b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
2859cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
2860ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm),dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr);
286188ed4aceSMatthew G Knepley   }
286288ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
286388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
286488ed4aceSMatthew G Knepley }
286588ed4aceSMatthew G Knepley 
286688ed4aceSMatthew G Knepley #undef __FUNCT__
2867b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
2868b21d0597SMatthew G Knepley /*@
2869b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
2870b21d0597SMatthew G Knepley 
2871b21d0597SMatthew G Knepley   Input Parameters:
2872b21d0597SMatthew G Knepley + dm - The DM
28735080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
2874b21d0597SMatthew G Knepley 
2875b21d0597SMatthew G Knepley   Level: intermediate
2876b21d0597SMatthew G Knepley 
2877b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
2878b21d0597SMatthew G Knepley 
2879b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
2880b21d0597SMatthew G Knepley @*/
28810adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
28820adebc6cSBarry Smith {
2883b21d0597SMatthew G Knepley   PetscErrorCode ierr;
2884b21d0597SMatthew G Knepley 
2885b21d0597SMatthew G Knepley   PetscFunctionBegin;
2886b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28875080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
28881d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
2889b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
2890b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
2891b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2892b21d0597SMatthew G Knepley }
2893b21d0597SMatthew G Knepley 
2894b21d0597SMatthew G Knepley #undef __FUNCT__
289588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
289688ed4aceSMatthew G Knepley /*@
289788ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
289888ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
289988ed4aceSMatthew G Knepley 
290088ed4aceSMatthew G Knepley   Input Parameter:
290188ed4aceSMatthew G Knepley . dm - The DM
290288ed4aceSMatthew G Knepley 
290388ed4aceSMatthew G Knepley   Output Parameter:
290488ed4aceSMatthew G Knepley . sf - The PetscSF
290588ed4aceSMatthew G Knepley 
290688ed4aceSMatthew G Knepley   Level: intermediate
290788ed4aceSMatthew G Knepley 
290888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
290988ed4aceSMatthew G Knepley 
291088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
291188ed4aceSMatthew G Knepley @*/
29120adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
29130adebc6cSBarry Smith {
291488ed4aceSMatthew G Knepley   PetscInt       nroots;
291588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
291688ed4aceSMatthew G Knepley 
291788ed4aceSMatthew G Knepley   PetscFunctionBegin;
291888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
291988ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
29200298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
292188ed4aceSMatthew G Knepley   if (nroots < 0) {
292288ed4aceSMatthew G Knepley     PetscSection section, gSection;
292388ed4aceSMatthew G Knepley 
292488ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
292531ea6d37SMatthew G Knepley     if (section) {
292688ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
292788ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
292831ea6d37SMatthew G Knepley     } else {
29290298fd71SBarry Smith       *sf = NULL;
293031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
293131ea6d37SMatthew G Knepley     }
293288ed4aceSMatthew G Knepley   }
293388ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
293488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
293588ed4aceSMatthew G Knepley }
293688ed4aceSMatthew G Knepley 
293788ed4aceSMatthew G Knepley #undef __FUNCT__
293888ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
293988ed4aceSMatthew G Knepley /*@
294088ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
294188ed4aceSMatthew G Knepley 
294288ed4aceSMatthew G Knepley   Input Parameters:
294388ed4aceSMatthew G Knepley + dm - The DM
294488ed4aceSMatthew G Knepley - sf - The PetscSF
294588ed4aceSMatthew G Knepley 
294688ed4aceSMatthew G Knepley   Level: intermediate
294788ed4aceSMatthew G Knepley 
294888ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
294988ed4aceSMatthew G Knepley 
295088ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
295188ed4aceSMatthew G Knepley @*/
29520adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
29530adebc6cSBarry Smith {
295488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
295588ed4aceSMatthew G Knepley 
295688ed4aceSMatthew G Knepley   PetscFunctionBegin;
295788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
295888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
295988ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
296088ed4aceSMatthew G Knepley   dm->defaultSF = sf;
296188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
296288ed4aceSMatthew G Knepley }
296388ed4aceSMatthew G Knepley 
296488ed4aceSMatthew G Knepley #undef __FUNCT__
296588ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
296688ed4aceSMatthew G Knepley /*@C
296788ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
296888ed4aceSMatthew G Knepley   describing the data layout.
296988ed4aceSMatthew G Knepley 
297088ed4aceSMatthew G Knepley   Input Parameters:
297188ed4aceSMatthew G Knepley + dm - The DM
297288ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
297388ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
297488ed4aceSMatthew G Knepley 
297588ed4aceSMatthew G Knepley   Level: intermediate
297688ed4aceSMatthew G Knepley 
297788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
297888ed4aceSMatthew G Knepley @*/
297988ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
298088ed4aceSMatthew G Knepley {
298182f516ccSBarry Smith   MPI_Comm       comm;
298288ed4aceSMatthew G Knepley   PetscLayout    layout;
298388ed4aceSMatthew G Knepley   const PetscInt *ranges;
298488ed4aceSMatthew G Knepley   PetscInt       *local;
298588ed4aceSMatthew G Knepley   PetscSFNode    *remote;
298688ed4aceSMatthew G Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves, l;
298788ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
298888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
298988ed4aceSMatthew G Knepley 
299088ed4aceSMatthew G Knepley   PetscFunctionBegin;
299182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
299288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
299388ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
299488ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
299588ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
299688ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
299788ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
299888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
299988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
300088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
300188ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
300288ed4aceSMatthew G Knepley   for (p = pStart, nleaves = 0; p < pEnd; ++p) {
30036636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
300488ed4aceSMatthew G Knepley 
30056636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
30066636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
30076636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
300888ed4aceSMatthew G Knepley   }
300988ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
301088ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
301188ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
30121f588964SMatthew G Knepley     const PetscInt *cind;
30136636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
301488ed4aceSMatthew G Knepley 
301588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
301688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
301788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
301888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
301988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
30206636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
302188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
30226636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
30236636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
30246636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3025057b4bcdSMatthew 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);
30266636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
30276636e97aSMatthew G Knepley     }
302888ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
302988ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
303088ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
303188ed4aceSMatthew G Knepley     }
303288ed4aceSMatthew G Knepley     if (gdof < 0) {
30336636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
303488ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
303588ed4aceSMatthew G Knepley 
303631d3f06eSJed Brown         ierr = PetscFindInt(offset,size,ranges,&r);CHKERRQ(ierr);
303731d3f06eSJed Brown         if (r < 0) r = -(r+2);
303888ed4aceSMatthew G Knepley         remote[l].rank  = r;
303988ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
304088ed4aceSMatthew G Knepley       }
304188ed4aceSMatthew G Knepley     } else {
30426636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
304388ed4aceSMatthew G Knepley         remote[l].rank  = rank;
304488ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
304588ed4aceSMatthew G Knepley       }
304688ed4aceSMatthew G Knepley     }
304788ed4aceSMatthew G Knepley   }
30486636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
304988ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
305088ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
305188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
305288ed4aceSMatthew G Knepley }
3053af122d2aSMatthew G Knepley 
3054af122d2aSMatthew G Knepley #undef __FUNCT__
3055b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3056b21d0597SMatthew G Knepley /*@
3057b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3058b21d0597SMatthew G Knepley 
3059b21d0597SMatthew G Knepley   Input Parameter:
3060b21d0597SMatthew G Knepley . dm - The DM
3061b21d0597SMatthew G Knepley 
3062b21d0597SMatthew G Knepley   Output Parameter:
3063b21d0597SMatthew G Knepley . sf - The PetscSF
3064b21d0597SMatthew G Knepley 
3065b21d0597SMatthew G Knepley   Level: intermediate
3066b21d0597SMatthew G Knepley 
3067b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3068b21d0597SMatthew G Knepley 
3069057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3070b21d0597SMatthew G Knepley @*/
30710adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
30720adebc6cSBarry Smith {
3073b21d0597SMatthew G Knepley   PetscFunctionBegin;
3074b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3075b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3076b21d0597SMatthew G Knepley   *sf = dm->sf;
3077b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3078b21d0597SMatthew G Knepley }
3079b21d0597SMatthew G Knepley 
3080b21d0597SMatthew G Knepley #undef __FUNCT__
3081057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3082057b4bcdSMatthew G Knepley /*@
3083057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3084057b4bcdSMatthew G Knepley 
3085057b4bcdSMatthew G Knepley   Input Parameters:
3086057b4bcdSMatthew G Knepley + dm - The DM
3087057b4bcdSMatthew G Knepley - sf - The PetscSF
3088057b4bcdSMatthew G Knepley 
3089057b4bcdSMatthew G Knepley   Level: intermediate
3090057b4bcdSMatthew G Knepley 
3091057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3092057b4bcdSMatthew G Knepley @*/
30930adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
30940adebc6cSBarry Smith {
3095057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3096057b4bcdSMatthew G Knepley 
3097057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3098057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3099057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3100057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3101057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3102057b4bcdSMatthew G Knepley   dm->sf = sf;
3103057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3104057b4bcdSMatthew G Knepley }
3105057b4bcdSMatthew G Knepley 
3106057b4bcdSMatthew G Knepley #undef __FUNCT__
3107af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3108af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3109af122d2aSMatthew G Knepley {
3110af122d2aSMatthew G Knepley   PetscFunctionBegin;
3111af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3112af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3113af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3114af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3115af122d2aSMatthew G Knepley }
3116af122d2aSMatthew G Knepley 
3117af122d2aSMatthew G Knepley #undef __FUNCT__
3118af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3119af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3120af122d2aSMatthew G Knepley {
3121af122d2aSMatthew G Knepley   PetscInt       f;
3122af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3123af122d2aSMatthew G Knepley 
3124af122d2aSMatthew G Knepley   PetscFunctionBegin;
3125af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3126af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3127af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3128af122d2aSMatthew G Knepley   }
3129af122d2aSMatthew G Knepley   ierr          = PetscFree(dm->fields);CHKERRQ(ierr);
3130af122d2aSMatthew G Knepley   dm->numFields = numFields;
3131af122d2aSMatthew G Knepley   ierr          = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3132af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
313382f516ccSBarry Smith     ierr = PetscContainerCreate(PetscObjectComm((PetscObject)dm), (PetscContainer*) &dm->fields[f]);CHKERRQ(ierr);
3134af122d2aSMatthew G Knepley   }
3135af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3136af122d2aSMatthew G Knepley }
3137af122d2aSMatthew G Knepley 
3138af122d2aSMatthew G Knepley #undef __FUNCT__
3139af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3140af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3141af122d2aSMatthew G Knepley {
3142af122d2aSMatthew G Knepley   PetscFunctionBegin;
3143af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3144af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
314582f516ccSBarry Smith   if (!dm->fields) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
314682f516ccSBarry 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);
3147af122d2aSMatthew G Knepley   *field = dm->fields[f];
3148af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3149af122d2aSMatthew G Knepley }
31506636e97aSMatthew G Knepley 
31516636e97aSMatthew G Knepley #undef __FUNCT__
31526636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
31536636e97aSMatthew G Knepley /*@
31546636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
31556636e97aSMatthew G Knepley 
31566636e97aSMatthew G Knepley   Collective on DM
31576636e97aSMatthew G Knepley 
31586636e97aSMatthew G Knepley   Input Parameters:
31596636e97aSMatthew G Knepley + dm - the DM
31606636e97aSMatthew G Knepley - c - coordinate vector
31616636e97aSMatthew G Knepley 
31626636e97aSMatthew G Knepley   Note:
31636636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
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: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
31696636e97aSMatthew G Knepley @*/
31706636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(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   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
31776636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
31786636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
31796636e97aSMatthew G Knepley   dm->coordinates = c;
31806636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
31816636e97aSMatthew G Knepley   PetscFunctionReturn(0);
31826636e97aSMatthew G Knepley }
31836636e97aSMatthew G Knepley 
31846636e97aSMatthew G Knepley #undef __FUNCT__
31856636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
31866636e97aSMatthew G Knepley /*@
31876636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
31886636e97aSMatthew G Knepley 
31896636e97aSMatthew G Knepley   Collective on DM
31906636e97aSMatthew G Knepley 
31916636e97aSMatthew G Knepley    Input Parameters:
31926636e97aSMatthew G Knepley +  dm - the DM
31936636e97aSMatthew G Knepley -  c - coordinate vector
31946636e97aSMatthew G Knepley 
31956636e97aSMatthew G Knepley   Note:
31966636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
31976636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
31986636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
31996636e97aSMatthew G Knepley 
32006636e97aSMatthew G Knepley   Level: intermediate
32016636e97aSMatthew G Knepley 
32026636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
32036636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
32046636e97aSMatthew G Knepley @*/
32056636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
32066636e97aSMatthew G Knepley {
32076636e97aSMatthew G Knepley   PetscErrorCode ierr;
32086636e97aSMatthew G Knepley 
32096636e97aSMatthew G Knepley   PetscFunctionBegin;
32106636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32116636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
32126636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
32136636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
32148865f1eaSKarl Rupp 
32156636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
32168865f1eaSKarl Rupp 
32176636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
32186636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32196636e97aSMatthew G Knepley }
32206636e97aSMatthew G Knepley 
32216636e97aSMatthew G Knepley #undef __FUNCT__
32226636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
32236636e97aSMatthew G Knepley /*@
32246636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
32256636e97aSMatthew G Knepley 
32266636e97aSMatthew G Knepley   Not Collective
32276636e97aSMatthew G Knepley 
32286636e97aSMatthew G Knepley   Input Parameter:
32296636e97aSMatthew G Knepley . dm - the DM
32306636e97aSMatthew G Knepley 
32316636e97aSMatthew G Knepley   Output Parameter:
32326636e97aSMatthew G Knepley . c - global coordinate vector
32336636e97aSMatthew G Knepley 
32346636e97aSMatthew G Knepley   Note:
32356636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
32366636e97aSMatthew G Knepley 
32376636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
32386636e97aSMatthew G Knepley 
32396636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
32406636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
32416636e97aSMatthew G Knepley 
32426636e97aSMatthew G Knepley   Level: intermediate
32436636e97aSMatthew G Knepley 
32446636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
32456636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
32466636e97aSMatthew G Knepley @*/
32476636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
32486636e97aSMatthew G Knepley {
32496636e97aSMatthew G Knepley   PetscErrorCode ierr;
32506636e97aSMatthew G Knepley 
32516636e97aSMatthew G Knepley   PetscFunctionBegin;
32526636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32536636e97aSMatthew G Knepley   PetscValidPointer(c,2);
32541f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
32550298fd71SBarry Smith     DM cdm = NULL;
32566636e97aSMatthew G Knepley 
32576636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
32586636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
32596636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
32606636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
32616636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
32626636e97aSMatthew G Knepley   }
32636636e97aSMatthew G Knepley   *c = dm->coordinates;
32646636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32656636e97aSMatthew G Knepley }
32666636e97aSMatthew G Knepley 
32676636e97aSMatthew G Knepley #undef __FUNCT__
32686636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
32696636e97aSMatthew G Knepley /*@
32706636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
32716636e97aSMatthew G Knepley 
32726636e97aSMatthew G Knepley   Collective on DM
32736636e97aSMatthew G Knepley 
32746636e97aSMatthew G Knepley   Input Parameter:
32756636e97aSMatthew G Knepley . dm - the DM
32766636e97aSMatthew G Knepley 
32776636e97aSMatthew G Knepley   Output Parameter:
32786636e97aSMatthew G Knepley . c - coordinate vector
32796636e97aSMatthew G Knepley 
32806636e97aSMatthew G Knepley   Note:
32816636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
32826636e97aSMatthew G Knepley 
32836636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
32846636e97aSMatthew G Knepley 
32856636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
32866636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
32876636e97aSMatthew G Knepley 
32886636e97aSMatthew G Knepley   Level: intermediate
32896636e97aSMatthew G Knepley 
32906636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
32916636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
32926636e97aSMatthew G Knepley @*/
32936636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
32946636e97aSMatthew G Knepley {
32956636e97aSMatthew G Knepley   PetscErrorCode ierr;
32966636e97aSMatthew G Knepley 
32976636e97aSMatthew G Knepley   PetscFunctionBegin;
32986636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32996636e97aSMatthew G Knepley   PetscValidPointer(c,2);
33001f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
33010298fd71SBarry Smith     DM cdm = NULL;
33026636e97aSMatthew G Knepley 
33036636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
33046636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
33056636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
33066636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
33076636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
33086636e97aSMatthew G Knepley   }
33096636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
33106636e97aSMatthew G Knepley   PetscFunctionReturn(0);
33116636e97aSMatthew G Knepley }
33126636e97aSMatthew G Knepley 
33136636e97aSMatthew G Knepley #undef __FUNCT__
33146636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
33156636e97aSMatthew G Knepley /*@
33166636e97aSMatthew G Knepley   DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates
33176636e97aSMatthew G Knepley 
33186636e97aSMatthew G Knepley   Collective on DM
33196636e97aSMatthew G Knepley 
33206636e97aSMatthew G Knepley   Input Parameter:
33216636e97aSMatthew G Knepley . dm - the DM
33226636e97aSMatthew G Knepley 
33236636e97aSMatthew G Knepley   Output Parameter:
33246636e97aSMatthew G Knepley . cdm - coordinate DM
33256636e97aSMatthew G Knepley 
33266636e97aSMatthew G Knepley   Level: intermediate
33276636e97aSMatthew G Knepley 
33286636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
33296636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
33306636e97aSMatthew G Knepley @*/
33316636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
33326636e97aSMatthew G Knepley {
33336636e97aSMatthew G Knepley   PetscErrorCode ierr;
33346636e97aSMatthew G Knepley 
33356636e97aSMatthew G Knepley   PetscFunctionBegin;
33366636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33376636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
33386636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
333982f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
33406636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
33416636e97aSMatthew G Knepley   }
33426636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
33436636e97aSMatthew G Knepley   PetscFunctionReturn(0);
33446636e97aSMatthew G Knepley }
3345e87bb0d3SMatthew G Knepley 
3346e87bb0d3SMatthew G Knepley #undef __FUNCT__
3347e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
3348e87bb0d3SMatthew G Knepley /*@
3349e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
3350e87bb0d3SMatthew G Knepley 
3351e87bb0d3SMatthew G Knepley   Not collective
3352e87bb0d3SMatthew G Knepley 
3353e87bb0d3SMatthew G Knepley   Input Parameters:
3354e87bb0d3SMatthew G Knepley + dm - The DM
3355e87bb0d3SMatthew G Knepley - v - The Vec of points
3356e87bb0d3SMatthew G Knepley 
335761e3bb9bSMatthew G Knepley   Output Parameter:
3358e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
3359e87bb0d3SMatthew G Knepley 
3360e87bb0d3SMatthew G Knepley   Level: developer
336161e3bb9bSMatthew G Knepley 
336261e3bb9bSMatthew G Knepley .keywords: point location, mesh
336361e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
336461e3bb9bSMatthew G Knepley @*/
3365e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
3366e87bb0d3SMatthew G Knepley {
3367735aa83eSMatthew G Knepley   PetscErrorCode ierr;
3368735aa83eSMatthew G Knepley 
3369e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
3370e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3371e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
3372e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
3373735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
3374735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
337582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
3376e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
3377e87bb0d3SMatthew G Knepley }
3378