xref: /petsc/src/dm/interface/dm.c (revision 146574ab6339e369828611fa942736f2bdad83fd)
1b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
20c312b8eSJed Brown #include <petscsf.h>
347c6ae99SBarry Smith 
4732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
567a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal;
667a56275SMatthew G Knepley 
747c6ae99SBarry Smith #undef __FUNCT__
8ca266f36SBarry Smith #define __FUNCT__ "DMViewFromOptions"
9ca266f36SBarry Smith /*
10ca266f36SBarry Smith   Processes command line options to determine if/how a dm
11ca266f36SBarry Smith   is to be viewed.
12ca266f36SBarry Smith 
13ca266f36SBarry Smith */
14*146574abSBarry Smith PetscErrorCode  DMViewFromOptions(DM dm,const char prefix[],const char optionname[])
15ca266f36SBarry Smith {
16ca266f36SBarry Smith   PetscErrorCode    ierr;
17ca266f36SBarry Smith   PetscBool         flg;
18ca266f36SBarry Smith   PetscViewer       viewer;
19cffb1e40SBarry Smith   PetscViewerFormat format;
20ca266f36SBarry Smith 
21ca266f36SBarry Smith   PetscFunctionBegin;
22*146574abSBarry Smith   if (prefix) {
23*146574abSBarry Smith     ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm),prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr);
24*146574abSBarry Smith   } else {
25ce94432eSBarry Smith     ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm),((PetscObject)dm)->prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr);
26*146574abSBarry Smith   }
27ca266f36SBarry Smith   if (flg) {
28cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
29ca266f36SBarry Smith     ierr = DMView(dm,viewer);CHKERRQ(ierr);
30cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
31cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
32ca266f36SBarry Smith   }
33ca266f36SBarry Smith   PetscFunctionReturn(0);
34ca266f36SBarry Smith }
35ca266f36SBarry Smith 
36ca266f36SBarry Smith 
37ca266f36SBarry Smith #undef __FUNCT__
38a4121054SBarry Smith #define __FUNCT__ "DMCreate"
39a4121054SBarry Smith /*@
40de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
41a4121054SBarry Smith 
42a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
43a4121054SBarry Smith    error when you try to use the vector.
44a4121054SBarry Smith 
45a4121054SBarry Smith   Collective on MPI_Comm
46a4121054SBarry Smith 
47a4121054SBarry Smith   Input Parameter:
48a4121054SBarry Smith . comm - The communicator for the DM object
49a4121054SBarry Smith 
50a4121054SBarry Smith   Output Parameter:
51a4121054SBarry Smith . dm - The DM object
52a4121054SBarry Smith 
53a4121054SBarry Smith   Level: beginner
54a4121054SBarry Smith 
55a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
56a4121054SBarry Smith @*/
577087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
58a4121054SBarry Smith {
59a4121054SBarry Smith   DM             v;
60a4121054SBarry Smith   PetscErrorCode ierr;
61a4121054SBarry Smith 
62a4121054SBarry Smith   PetscFunctionBegin;
631411c6eeSJed Brown   PetscValidPointer(dm,2);
640298fd71SBarry Smith   *dm = NULL;
65519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
66607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
67607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
68607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
69a4121054SBarry Smith #endif
70a4121054SBarry Smith 
7167c2884eSBarry Smith   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
72a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
731411c6eeSJed Brown 
74e7c4fc90SDmitry Karpeev 
750298fd71SBarry Smith   v->ltogmap              = NULL;
760298fd71SBarry Smith   v->ltogmapb             = NULL;
771411c6eeSJed Brown   v->bs                   = 1;
78171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
7988ed4aceSMatthew G Knepley   ierr                    = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
8088ed4aceSMatthew G Knepley   ierr                    = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
810298fd71SBarry Smith   v->defaultSection       = NULL;
820298fd71SBarry Smith   v->defaultGlobalSection = NULL;
83435a35e8SMatthew G Knepley   {
84435a35e8SMatthew G Knepley     PetscInt i;
85435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
860298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
87435a35e8SMatthew G Knepley     }
88435a35e8SMatthew G Knepley   }
89af122d2aSMatthew G Knepley   v->numFields = 0;
900298fd71SBarry Smith   v->fields    = NULL;
911411c6eeSJed Brown 
921411c6eeSJed Brown   *dm = v;
93a4121054SBarry Smith   PetscFunctionReturn(0);
94a4121054SBarry Smith }
95a4121054SBarry Smith 
96a4121054SBarry Smith #undef __FUNCT__
979a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
989a42bb27SBarry Smith /*@C
99564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1009a42bb27SBarry Smith 
101aa219208SBarry Smith    Logically Collective on DMDA
1029a42bb27SBarry Smith 
1039a42bb27SBarry Smith    Input Parameter:
1049a42bb27SBarry Smith +  da - initial distributed array
1058154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1069a42bb27SBarry Smith 
1079a42bb27SBarry Smith    Options Database:
108dd85299cSBarry Smith .   -dm_vec_type ctype
1099a42bb27SBarry Smith 
1109a42bb27SBarry Smith    Level: intermediate
1119a42bb27SBarry Smith 
112aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
1139a42bb27SBarry Smith @*/
11419fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1159a42bb27SBarry Smith {
1169a42bb27SBarry Smith   PetscErrorCode ierr;
1179a42bb27SBarry Smith 
1189a42bb27SBarry Smith   PetscFunctionBegin;
1199a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1209a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
12119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1229a42bb27SBarry Smith   PetscFunctionReturn(0);
1239a42bb27SBarry Smith }
1249a42bb27SBarry Smith 
1259a42bb27SBarry Smith #undef __FUNCT__
1265f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1275f1ad066SMatthew G Knepley /*@
12834f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1295f1ad066SMatthew G Knepley 
1305f1ad066SMatthew G Knepley   Not collective
1315f1ad066SMatthew G Knepley 
1325f1ad066SMatthew G Knepley   Input Parameter:
1335f1ad066SMatthew G Knepley . v - The Vec
1345f1ad066SMatthew G Knepley 
1355f1ad066SMatthew G Knepley   Output Parameter:
1365f1ad066SMatthew G Knepley . dm - The DM
1375f1ad066SMatthew G Knepley 
1385f1ad066SMatthew G Knepley   Level: intermediate
1395f1ad066SMatthew G Knepley 
1405f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1415f1ad066SMatthew G Knepley @*/
1425f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
1435f1ad066SMatthew G Knepley {
1445f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1455f1ad066SMatthew G Knepley 
1465f1ad066SMatthew G Knepley   PetscFunctionBegin;
1475f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1485f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
1495f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
1505f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
1515f1ad066SMatthew G Knepley }
1525f1ad066SMatthew G Knepley 
1535f1ad066SMatthew G Knepley #undef __FUNCT__
1545f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
1555f1ad066SMatthew G Knepley /*@
1565f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
1575f1ad066SMatthew G Knepley 
1585f1ad066SMatthew G Knepley   Not collective
1595f1ad066SMatthew G Knepley 
1605f1ad066SMatthew G Knepley   Input Parameters:
1615f1ad066SMatthew G Knepley + v - The Vec
1625f1ad066SMatthew G Knepley - dm - The DM
1635f1ad066SMatthew G Knepley 
1645f1ad066SMatthew G Knepley   Level: intermediate
1655f1ad066SMatthew G Knepley 
1665f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1675f1ad066SMatthew G Knepley @*/
1685f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
1695f1ad066SMatthew G Knepley {
1705f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1715f1ad066SMatthew G Knepley 
1725f1ad066SMatthew G Knepley   PetscFunctionBegin;
1735f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1745f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
1755f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
1765f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
1775f1ad066SMatthew G Knepley }
1785f1ad066SMatthew G Knepley 
1795f1ad066SMatthew G Knepley #undef __FUNCT__
180521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
181521d9a4cSLisandro Dalcin /*@C
182521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
183521d9a4cSLisandro Dalcin 
184521d9a4cSLisandro Dalcin    Logically Collective on DM
185521d9a4cSLisandro Dalcin 
186521d9a4cSLisandro Dalcin    Input Parameter:
187521d9a4cSLisandro Dalcin +  dm - the DM context
188521d9a4cSLisandro Dalcin .  ctype - the matrix type
189521d9a4cSLisandro Dalcin 
190521d9a4cSLisandro Dalcin    Options Database:
191521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
192521d9a4cSLisandro Dalcin 
193521d9a4cSLisandro Dalcin    Level: intermediate
194521d9a4cSLisandro Dalcin 
195521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType
196521d9a4cSLisandro Dalcin @*/
19719fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
198521d9a4cSLisandro Dalcin {
199521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
20088f0584fSBarry Smith 
201521d9a4cSLisandro Dalcin   PetscFunctionBegin;
202521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
203521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
20419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
205521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
206521d9a4cSLisandro Dalcin }
207521d9a4cSLisandro Dalcin 
208521d9a4cSLisandro Dalcin #undef __FUNCT__
209c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
210c688c046SMatthew G Knepley /*@
21134f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
212c688c046SMatthew G Knepley 
213c688c046SMatthew G Knepley   Not collective
214c688c046SMatthew G Knepley 
215c688c046SMatthew G Knepley   Input Parameter:
216c688c046SMatthew G Knepley . A - The Mat
217c688c046SMatthew G Knepley 
218c688c046SMatthew G Knepley   Output Parameter:
219c688c046SMatthew G Knepley . dm - The DM
220c688c046SMatthew G Knepley 
221c688c046SMatthew G Knepley   Level: intermediate
222c688c046SMatthew G Knepley 
223c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
224c688c046SMatthew G Knepley @*/
225c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
226c688c046SMatthew G Knepley {
227c688c046SMatthew G Knepley   PetscErrorCode ierr;
228c688c046SMatthew G Knepley 
229c688c046SMatthew G Knepley   PetscFunctionBegin;
230c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
231c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
232c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
233c688c046SMatthew G Knepley   PetscFunctionReturn(0);
234c688c046SMatthew G Knepley }
235c688c046SMatthew G Knepley 
236c688c046SMatthew G Knepley #undef __FUNCT__
237c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
238c688c046SMatthew G Knepley /*@
239c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
240c688c046SMatthew G Knepley 
241c688c046SMatthew G Knepley   Not collective
242c688c046SMatthew G Knepley 
243c688c046SMatthew G Knepley   Input Parameters:
244c688c046SMatthew G Knepley + A - The Mat
245c688c046SMatthew G Knepley - dm - The DM
246c688c046SMatthew G Knepley 
247c688c046SMatthew G Knepley   Level: intermediate
248c688c046SMatthew G Knepley 
249c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
250c688c046SMatthew G Knepley @*/
251c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
252c688c046SMatthew G Knepley {
253c688c046SMatthew G Knepley   PetscErrorCode ierr;
254c688c046SMatthew G Knepley 
255c688c046SMatthew G Knepley   PetscFunctionBegin;
256c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
2578865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
258c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
259c688c046SMatthew G Knepley   PetscFunctionReturn(0);
260c688c046SMatthew G Knepley }
261c688c046SMatthew G Knepley 
262c688c046SMatthew G Knepley #undef __FUNCT__
2639a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
2649a42bb27SBarry Smith /*@C
2659a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
266aa219208SBarry Smith    DMDA options in the database.
2679a42bb27SBarry Smith 
268aa219208SBarry Smith    Logically Collective on DMDA
2699a42bb27SBarry Smith 
2709a42bb27SBarry Smith    Input Parameter:
271aa219208SBarry Smith +  da - the DMDA context
2729a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
2739a42bb27SBarry Smith 
2749a42bb27SBarry Smith    Notes:
2759a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2769a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2779a42bb27SBarry Smith 
2789a42bb27SBarry Smith    Level: advanced
2799a42bb27SBarry Smith 
280aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
2819a42bb27SBarry Smith 
2829a42bb27SBarry Smith .seealso: DMSetFromOptions()
2839a42bb27SBarry Smith @*/
2847087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
2859a42bb27SBarry Smith {
2869a42bb27SBarry Smith   PetscErrorCode ierr;
2879a42bb27SBarry Smith 
2889a42bb27SBarry Smith   PetscFunctionBegin;
2899a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2909a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
2919a42bb27SBarry Smith   PetscFunctionReturn(0);
2929a42bb27SBarry Smith }
2939a42bb27SBarry Smith 
2949a42bb27SBarry Smith #undef __FUNCT__
29547c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
29647c6ae99SBarry Smith /*@
297aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
29847c6ae99SBarry Smith 
29947c6ae99SBarry Smith     Collective on DM
30047c6ae99SBarry Smith 
30147c6ae99SBarry Smith     Input Parameter:
30247c6ae99SBarry Smith .   dm - the DM object to destroy
30347c6ae99SBarry Smith 
30447c6ae99SBarry Smith     Level: developer
30547c6ae99SBarry Smith 
306e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
30747c6ae99SBarry Smith 
30847c6ae99SBarry Smith @*/
309fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
31047c6ae99SBarry Smith {
311af122d2aSMatthew G Knepley   PetscInt       i, cnt = 0, f;
312dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
31347c6ae99SBarry Smith   PetscErrorCode ierr;
31447c6ae99SBarry Smith 
31547c6ae99SBarry Smith   PetscFunctionBegin;
3166bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
3176bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
31887e657c6SBarry Smith 
31987e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
320732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
3218865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
3228865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
323732e2eb9SMatthew G Knepley   }
324dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
32571cd77b2SBarry Smith   if ((*dm)->x) {
326c688c046SMatthew G Knepley     DM obj;
327c688c046SMatthew G Knepley     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
328c688c046SMatthew G Knepley     if (obj == *dm) cnt++;
32971cd77b2SBarry Smith   }
3302348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
3312348bcf4SPeter Brune   if ((*dm)->x) {
3322348bcf4SPeter Brune     DM obj;
3332348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
3342348bcf4SPeter Brune     if (obj == *dm) cnt++;
3352348bcf4SPeter Brune   }
336732e2eb9SMatthew G Knepley 
3376bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
338732e2eb9SMatthew G Knepley   /*
339732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
340732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
341732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
342732e2eb9SMatthew G Knepley   */
3436bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
3446bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
345732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
3466bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
3476bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
348732e2eb9SMatthew G Knepley   }
349dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
350dfe15315SJed Brown     nnext = nlink->next;
351dfe15315SJed 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);
352dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
353dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
354dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
355dfe15315SJed Brown   }
3560298fd71SBarry Smith   (*dm)->namedglobal = NULL;
3571a266240SBarry Smith 
3582348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nnext) { /* Destroy the named local vectors */
3592348bcf4SPeter Brune     nnext = nlink->next;
3602348bcf4SPeter 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);
3612348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
3622348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
3632348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
3642348bcf4SPeter Brune   }
3650298fd71SBarry Smith   (*dm)->namedlocal = NULL;
3662348bcf4SPeter Brune 
367b17ce1afSJed Brown   /* Destroy the list of hooks */
368c833c3b5SJed Brown   {
369c833c3b5SJed Brown     DMCoarsenHookLink link,next;
370b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
371b17ce1afSJed Brown       next = link->next;
372b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
373b17ce1afSJed Brown     }
3740298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
375c833c3b5SJed Brown   }
376c833c3b5SJed Brown   {
377c833c3b5SJed Brown     DMRefineHookLink link,next;
378c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
379c833c3b5SJed Brown       next = link->next;
380c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
381c833c3b5SJed Brown     }
3820298fd71SBarry Smith     (*dm)->refinehook = NULL;
383c833c3b5SJed Brown   }
384be081cd6SPeter Brune   {
385be081cd6SPeter Brune     DMSubDomainHookLink link,next;
386be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
387be081cd6SPeter Brune       next = link->next;
388be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
389be081cd6SPeter Brune     }
3900298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
391be081cd6SPeter Brune   }
392baf369e7SPeter Brune   {
393baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
394baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
395baf369e7SPeter Brune       next = link->next;
396baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
397baf369e7SPeter Brune     }
3980298fd71SBarry Smith     (*dm)->gtolhook = NULL;
399baf369e7SPeter Brune   }
400aa1993deSMatthew G Knepley   /* Destroy the work arrays */
401aa1993deSMatthew G Knepley   {
402aa1993deSMatthew G Knepley     DMWorkLink link,next;
403aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
404aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
405aa1993deSMatthew G Knepley       next = link->next;
406aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
407aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
408aa1993deSMatthew G Knepley     }
4090298fd71SBarry Smith     (*dm)->workin = NULL;
410aa1993deSMatthew G Knepley   }
411b17ce1afSJed Brown 
41252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
41352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
41452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
41552536dc3SBarry Smith 
4161a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
4171a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
4181a266240SBarry Smith   }
41987e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
42071cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
4214dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
4226bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
4236bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
4246bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
425073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
42688ed4aceSMatthew G Knepley 
42788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
42888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
4298b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
43088ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
43188ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
432af122d2aSMatthew G Knepley 
4336636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
4346636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
4356636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
4366636e97aSMatthew G Knepley 
437af122d2aSMatthew G Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
438af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr);
439af122d2aSMatthew G Knepley   }
440af122d2aSMatthew G Knepley   ierr = PetscFree((*dm)->fields);CHKERRQ(ierr);
441732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
442f05ece33SBarry Smith   ierr = PetscObjectAMSViewOff((PetscObject)*dm);CHKERRQ(ierr);
443732e2eb9SMatthew G Knepley 
4446bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
445435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
446732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
44747c6ae99SBarry Smith   PetscFunctionReturn(0);
44847c6ae99SBarry Smith }
44947c6ae99SBarry Smith 
45047c6ae99SBarry Smith #undef __FUNCT__
451d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
452d7bf68aeSBarry Smith /*@
453d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
454d7bf68aeSBarry Smith 
455d7bf68aeSBarry Smith     Collective on DM
456d7bf68aeSBarry Smith 
457d7bf68aeSBarry Smith     Input Parameter:
458d7bf68aeSBarry Smith .   dm - the DM object to setup
459d7bf68aeSBarry Smith 
460d7bf68aeSBarry Smith     Level: developer
461d7bf68aeSBarry Smith 
462e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
463d7bf68aeSBarry Smith 
464d7bf68aeSBarry Smith @*/
4657087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
466d7bf68aeSBarry Smith {
467d7bf68aeSBarry Smith   PetscErrorCode ierr;
468d7bf68aeSBarry Smith 
469d7bf68aeSBarry Smith   PetscFunctionBegin;
470171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4718387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
472d7bf68aeSBarry Smith   if (dm->ops->setup) {
473d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
474d7bf68aeSBarry Smith   }
4758387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
476d7bf68aeSBarry Smith   PetscFunctionReturn(0);
477d7bf68aeSBarry Smith }
478d7bf68aeSBarry Smith 
479d7bf68aeSBarry Smith #undef __FUNCT__
480d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
481d7bf68aeSBarry Smith /*@
482d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
483d7bf68aeSBarry Smith 
484d7bf68aeSBarry Smith     Collective on DM
485d7bf68aeSBarry Smith 
486d7bf68aeSBarry Smith     Input Parameter:
487d7bf68aeSBarry Smith .   dm - the DM object to set options for
488d7bf68aeSBarry Smith 
489732e2eb9SMatthew G Knepley     Options Database:
490dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
491dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
492171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
493171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
494732e2eb9SMatthew G Knepley 
495d7bf68aeSBarry Smith     Level: developer
496d7bf68aeSBarry Smith 
497e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
498d7bf68aeSBarry Smith 
499d7bf68aeSBarry Smith @*/
5007087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
501d7bf68aeSBarry Smith {
502f55f929bSMatthew G Knepley   char           typeName[256] = MATAIJ;
503ca266f36SBarry Smith   PetscBool      flg;
504d7bf68aeSBarry Smith   PetscErrorCode ierr;
505d7bf68aeSBarry Smith 
506d7bf68aeSBarry Smith   PetscFunctionBegin;
507171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5083194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
5090298fd71SBarry 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);
510f9ba7244SBarry Smith   ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
511f9ba7244SBarry Smith   if (flg) {
512f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
513f9ba7244SBarry Smith   }
5148caf3d72SBarry 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);
515073dac72SJed Brown   if (flg) {
516521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
517073dac72SJed Brown   }
5180298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
519f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
520f9ba7244SBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
521f9ba7244SBarry Smith   }
522f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
523f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
52482fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
525*146574abSBarry Smith   ierr = DMViewFromOptions(dm,NULL,"-dm_view");CHKERRQ(ierr);
526d7bf68aeSBarry Smith   PetscFunctionReturn(0);
527d7bf68aeSBarry Smith }
528d7bf68aeSBarry Smith 
529d7bf68aeSBarry Smith #undef __FUNCT__
53047c6ae99SBarry Smith #define __FUNCT__ "DMView"
531fc9bc008SSatish Balay /*@C
532aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
53347c6ae99SBarry Smith 
53447c6ae99SBarry Smith     Collective on DM
53547c6ae99SBarry Smith 
53647c6ae99SBarry Smith     Input Parameter:
53747c6ae99SBarry Smith +   dm - the DM object to view
53847c6ae99SBarry Smith -   v - the viewer
53947c6ae99SBarry Smith 
54047c6ae99SBarry Smith     Level: developer
54147c6ae99SBarry Smith 
542e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
54347c6ae99SBarry Smith 
54447c6ae99SBarry Smith @*/
5457087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
54647c6ae99SBarry Smith {
54747c6ae99SBarry Smith   PetscErrorCode ierr;
54832c0f0efSBarry Smith   PetscBool      isbinary;
54947c6ae99SBarry Smith 
55047c6ae99SBarry Smith   PetscFunctionBegin;
551171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5523014e516SBarry Smith   if (!v) {
553ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
5543014e516SBarry Smith   }
55532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
55632c0f0efSBarry Smith   if (isbinary) {
55755849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
55832c0f0efSBarry Smith     char     type[256];
55932c0f0efSBarry Smith 
56032c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
56132c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
56232c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
56332c0f0efSBarry Smith   }
5640c010503SBarry Smith   if (dm->ops->view) {
5650c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
56647c6ae99SBarry Smith   }
56747c6ae99SBarry Smith   PetscFunctionReturn(0);
56847c6ae99SBarry Smith }
56947c6ae99SBarry Smith 
57047c6ae99SBarry Smith #undef __FUNCT__
57147c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
57247c6ae99SBarry Smith /*@
573aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
57447c6ae99SBarry Smith 
57547c6ae99SBarry Smith     Collective on DM
57647c6ae99SBarry Smith 
57747c6ae99SBarry Smith     Input Parameter:
57847c6ae99SBarry Smith .   dm - the DM object
57947c6ae99SBarry Smith 
58047c6ae99SBarry Smith     Output Parameter:
58147c6ae99SBarry Smith .   vec - the global vector
58247c6ae99SBarry Smith 
583073dac72SJed Brown     Level: beginner
58447c6ae99SBarry Smith 
585e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
58647c6ae99SBarry Smith 
58747c6ae99SBarry Smith @*/
5887087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
58947c6ae99SBarry Smith {
59047c6ae99SBarry Smith   PetscErrorCode ierr;
59147c6ae99SBarry Smith 
59247c6ae99SBarry Smith   PetscFunctionBegin;
593171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59447c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
59547c6ae99SBarry Smith   PetscFunctionReturn(0);
59647c6ae99SBarry Smith }
59747c6ae99SBarry Smith 
59847c6ae99SBarry Smith #undef __FUNCT__
59947c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
60047c6ae99SBarry Smith /*@
601aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
60247c6ae99SBarry Smith 
60347c6ae99SBarry Smith     Not Collective
60447c6ae99SBarry Smith 
60547c6ae99SBarry Smith     Input Parameter:
60647c6ae99SBarry Smith .   dm - the DM object
60747c6ae99SBarry Smith 
60847c6ae99SBarry Smith     Output Parameter:
60947c6ae99SBarry Smith .   vec - the local vector
61047c6ae99SBarry Smith 
611073dac72SJed Brown     Level: beginner
61247c6ae99SBarry Smith 
613e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
61447c6ae99SBarry Smith 
61547c6ae99SBarry Smith @*/
6167087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
61747c6ae99SBarry Smith {
61847c6ae99SBarry Smith   PetscErrorCode ierr;
61947c6ae99SBarry Smith 
62047c6ae99SBarry Smith   PetscFunctionBegin;
621171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62247c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
62347c6ae99SBarry Smith   PetscFunctionReturn(0);
62447c6ae99SBarry Smith }
62547c6ae99SBarry Smith 
62647c6ae99SBarry Smith #undef __FUNCT__
6271411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
6281411c6eeSJed Brown /*@
6291411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
6301411c6eeSJed Brown 
6311411c6eeSJed Brown    Collective on DM
6321411c6eeSJed Brown 
6331411c6eeSJed Brown    Input Parameter:
6341411c6eeSJed Brown .  dm - the DM that provides the mapping
6351411c6eeSJed Brown 
6361411c6eeSJed Brown    Output Parameter:
6371411c6eeSJed Brown .  ltog - the mapping
6381411c6eeSJed Brown 
6391411c6eeSJed Brown    Level: intermediate
6401411c6eeSJed Brown 
6411411c6eeSJed Brown    Notes:
6421411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
6431411c6eeSJed Brown    MatSetLocalToGlobalMapping().
6441411c6eeSJed Brown 
6451411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
6461411c6eeSJed Brown @*/
6477087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
6481411c6eeSJed Brown {
6491411c6eeSJed Brown   PetscErrorCode ierr;
6501411c6eeSJed Brown 
6511411c6eeSJed Brown   PetscFunctionBegin;
6521411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6531411c6eeSJed Brown   PetscValidPointer(ltog,2);
6541411c6eeSJed Brown   if (!dm->ltogmap) {
65537d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
65637d0c07bSMatthew G Knepley 
65737d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
65837d0c07bSMatthew G Knepley     if (section) {
65937d0c07bSMatthew G Knepley       PetscInt *ltog;
66037d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
66137d0c07bSMatthew G Knepley 
66237d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
66337d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
66437d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
66537d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
66637d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
66737d0c07bSMatthew G Knepley         PetscInt dof, off, c;
66837d0c07bSMatthew G Knepley 
66937d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
67037d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
67137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
67237d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
67337d0c07bSMatthew G Knepley           ltog[l] = off+c;
67437d0c07bSMatthew G Knepley         }
67537d0c07bSMatthew G Knepley       }
67637d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
67737d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
67837d0c07bSMatthew G Knepley     } else {
679184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
680184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
6811411c6eeSJed Brown     }
68237d0c07bSMatthew G Knepley   }
6831411c6eeSJed Brown   *ltog = dm->ltogmap;
6841411c6eeSJed Brown   PetscFunctionReturn(0);
6851411c6eeSJed Brown }
6861411c6eeSJed Brown 
6871411c6eeSJed Brown #undef __FUNCT__
6881411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
6891411c6eeSJed Brown /*@
6901411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
6911411c6eeSJed Brown 
6921411c6eeSJed Brown    Collective on DM
6931411c6eeSJed Brown 
6941411c6eeSJed Brown    Input Parameter:
6951411c6eeSJed Brown .  da - the distributed array that provides the mapping
6961411c6eeSJed Brown 
6971411c6eeSJed Brown    Output Parameter:
6981411c6eeSJed Brown .  ltog - the block mapping
6991411c6eeSJed Brown 
7001411c6eeSJed Brown    Level: intermediate
7011411c6eeSJed Brown 
7021411c6eeSJed Brown    Notes:
7031411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
7041411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
7051411c6eeSJed Brown 
7061411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
7071411c6eeSJed Brown @*/
7087087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
7091411c6eeSJed Brown {
7101411c6eeSJed Brown   PetscErrorCode ierr;
7111411c6eeSJed Brown 
7121411c6eeSJed Brown   PetscFunctionBegin;
7131411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7141411c6eeSJed Brown   PetscValidPointer(ltog,2);
7151411c6eeSJed Brown   if (!dm->ltogmapb) {
7161411c6eeSJed Brown     PetscInt bs;
7171411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
7181411c6eeSJed Brown     if (bs > 1) {
719184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmappingblock) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
720184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
7211411c6eeSJed Brown     } else {
7221411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
7231411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
7241411c6eeSJed Brown     }
7251411c6eeSJed Brown   }
7261411c6eeSJed Brown   *ltog = dm->ltogmapb;
7271411c6eeSJed Brown   PetscFunctionReturn(0);
7281411c6eeSJed Brown }
7291411c6eeSJed Brown 
7301411c6eeSJed Brown #undef __FUNCT__
7311411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7321411c6eeSJed Brown /*@
7331411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7341411c6eeSJed Brown 
7351411c6eeSJed Brown    Not Collective
7361411c6eeSJed Brown 
7371411c6eeSJed Brown    Input Parameter:
7381411c6eeSJed Brown .  dm - the DM with block structure
7391411c6eeSJed Brown 
7401411c6eeSJed Brown    Output Parameter:
7411411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7421411c6eeSJed Brown 
7431411c6eeSJed Brown    Level: intermediate
7441411c6eeSJed Brown 
7451411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
7461411c6eeSJed Brown @*/
7477087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7481411c6eeSJed Brown {
7491411c6eeSJed Brown   PetscFunctionBegin;
7501411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7511411c6eeSJed Brown   PetscValidPointer(bs,2);
7521411c6eeSJed 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");
7531411c6eeSJed Brown   *bs = dm->bs;
7541411c6eeSJed Brown   PetscFunctionReturn(0);
7551411c6eeSJed Brown }
7561411c6eeSJed Brown 
7571411c6eeSJed Brown #undef __FUNCT__
758e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
75947c6ae99SBarry Smith /*@
760e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
76147c6ae99SBarry Smith 
76247c6ae99SBarry Smith     Collective on DM
76347c6ae99SBarry Smith 
76447c6ae99SBarry Smith     Input Parameter:
76547c6ae99SBarry Smith +   dm1 - the DM object
76647c6ae99SBarry Smith -   dm2 - the second, finer DM object
76747c6ae99SBarry Smith 
76847c6ae99SBarry Smith     Output Parameter:
76947c6ae99SBarry Smith +  mat - the interpolation
77047c6ae99SBarry Smith -  vec - the scaling (optional)
77147c6ae99SBarry Smith 
77247c6ae99SBarry Smith     Level: developer
77347c6ae99SBarry Smith 
77485afcc9aSBarry 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
77585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
776d52bd9f3SBarry Smith 
7771f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
778d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
77985afcc9aSBarry Smith 
78085afcc9aSBarry Smith 
781e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
78247c6ae99SBarry Smith 
78347c6ae99SBarry Smith @*/
784e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
78547c6ae99SBarry Smith {
78647c6ae99SBarry Smith   PetscErrorCode ierr;
78747c6ae99SBarry Smith 
78847c6ae99SBarry Smith   PetscFunctionBegin;
789171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
790171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
79125296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
79247c6ae99SBarry Smith   PetscFunctionReturn(0);
79347c6ae99SBarry Smith }
79447c6ae99SBarry Smith 
79547c6ae99SBarry Smith #undef __FUNCT__
796e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
79747c6ae99SBarry Smith /*@
798e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
79947c6ae99SBarry Smith 
80047c6ae99SBarry Smith     Collective on DM
80147c6ae99SBarry Smith 
80247c6ae99SBarry Smith     Input Parameter:
80347c6ae99SBarry Smith +   dm1 - the DM object
80447c6ae99SBarry Smith -   dm2 - the second, finer DM object
80547c6ae99SBarry Smith 
80647c6ae99SBarry Smith     Output Parameter:
80747c6ae99SBarry Smith .   ctx - the injection
80847c6ae99SBarry Smith 
80947c6ae99SBarry Smith     Level: developer
81047c6ae99SBarry Smith 
81185afcc9aSBarry 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
81285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
81385afcc9aSBarry Smith 
814e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
81547c6ae99SBarry Smith 
81647c6ae99SBarry Smith @*/
817e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
81847c6ae99SBarry Smith {
81947c6ae99SBarry Smith   PetscErrorCode ierr;
82047c6ae99SBarry Smith 
82147c6ae99SBarry Smith   PetscFunctionBegin;
822171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
823171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
82447c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
82547c6ae99SBarry Smith   PetscFunctionReturn(0);
82647c6ae99SBarry Smith }
82747c6ae99SBarry Smith 
82847c6ae99SBarry Smith #undef __FUNCT__
829e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
830d1e2c406SBarry Smith /*@C
831e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
83247c6ae99SBarry Smith 
83347c6ae99SBarry Smith     Collective on DM
83447c6ae99SBarry Smith 
83547c6ae99SBarry Smith     Input Parameter:
83647c6ae99SBarry Smith +   dm - the DM object
83747c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
83847c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
83947c6ae99SBarry Smith 
84047c6ae99SBarry Smith     Output Parameter:
84147c6ae99SBarry Smith .   coloring - the coloring
84247c6ae99SBarry Smith 
84347c6ae99SBarry Smith     Level: developer
84447c6ae99SBarry Smith 
845e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
84647c6ae99SBarry Smith 
847aab9d709SJed Brown @*/
84819fd82e9SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,MatType mtype,ISColoring *coloring)
84947c6ae99SBarry Smith {
85047c6ae99SBarry Smith   PetscErrorCode ierr;
85147c6ae99SBarry Smith 
85247c6ae99SBarry Smith   PetscFunctionBegin;
853171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
854ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
85547c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
85647c6ae99SBarry Smith   PetscFunctionReturn(0);
85747c6ae99SBarry Smith }
85847c6ae99SBarry Smith 
85947c6ae99SBarry Smith #undef __FUNCT__
860950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
86147c6ae99SBarry Smith /*@C
862950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
86347c6ae99SBarry Smith 
86447c6ae99SBarry Smith     Collective on DM
86547c6ae99SBarry Smith 
86647c6ae99SBarry Smith     Input Parameter:
86747c6ae99SBarry Smith +   dm - the DM object
86847c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
86994013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
87047c6ae99SBarry Smith 
87147c6ae99SBarry Smith     Output Parameter:
87247c6ae99SBarry Smith .   mat - the empty Jacobian
87347c6ae99SBarry Smith 
874073dac72SJed Brown     Level: beginner
87547c6ae99SBarry Smith 
87694013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
87794013140SBarry Smith        do not need to do it yourself.
87894013140SBarry Smith 
87994013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
880aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
88194013140SBarry Smith 
88294013140SBarry 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
88394013140SBarry Smith        internally by PETSc.
88494013140SBarry Smith 
88594013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
886aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
88794013140SBarry Smith 
888e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
88947c6ae99SBarry Smith 
890aab9d709SJed Brown @*/
89119fd82e9SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,MatType mtype,Mat *mat)
89247c6ae99SBarry Smith {
89347c6ae99SBarry Smith   PetscErrorCode ierr;
89447c6ae99SBarry Smith 
89547c6ae99SBarry Smith   PetscFunctionBegin;
896171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
897519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
898607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
899235683edSBarry Smith #endif
900c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
901c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
902073dac72SJed Brown   if (dm->mattype) {
90325296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
904073dac72SJed Brown   } else {
90525296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
906c7b7c8a4SJed Brown   }
90747c6ae99SBarry Smith   PetscFunctionReturn(0);
90847c6ae99SBarry Smith }
90947c6ae99SBarry Smith 
91047c6ae99SBarry Smith #undef __FUNCT__
911732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
912732e2eb9SMatthew G Knepley /*@
913950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
914732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
915732e2eb9SMatthew G Knepley 
916732e2eb9SMatthew G Knepley   Logically Collective on DMDA
917732e2eb9SMatthew G Knepley 
918732e2eb9SMatthew G Knepley   Input Parameter:
919732e2eb9SMatthew G Knepley + dm - the DM
920732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
921732e2eb9SMatthew G Knepley 
922732e2eb9SMatthew G Knepley   Level: developer
923950540a4SJed Brown .seealso DMCreateMatrix()
924732e2eb9SMatthew G Knepley @*/
925732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
926732e2eb9SMatthew G Knepley {
927732e2eb9SMatthew G Knepley   PetscFunctionBegin;
928732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
929732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
930732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
931732e2eb9SMatthew G Knepley }
932732e2eb9SMatthew G Knepley 
933732e2eb9SMatthew G Knepley #undef __FUNCT__
934a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
935a89ea682SMatthew G Knepley /*@C
936aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
937a89ea682SMatthew G Knepley 
938a89ea682SMatthew G Knepley   Not Collective
939a89ea682SMatthew G Knepley 
940a89ea682SMatthew G Knepley   Input Parameters:
941a89ea682SMatthew G Knepley + dm - the DM object
942aa1993deSMatthew G Knepley . count - The minium size
943aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
944a89ea682SMatthew G Knepley 
945a89ea682SMatthew G Knepley   Output Parameter:
946a89ea682SMatthew G Knepley . array - the work array
947a89ea682SMatthew G Knepley 
948a89ea682SMatthew G Knepley   Level: developer
949a89ea682SMatthew G Knepley 
950a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
951a89ea682SMatthew G Knepley @*/
952aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
953a89ea682SMatthew G Knepley {
954a89ea682SMatthew G Knepley   PetscErrorCode ierr;
955aa1993deSMatthew G Knepley   DMWorkLink     link;
956aa1993deSMatthew G Knepley   size_t         size;
957a89ea682SMatthew G Knepley 
958a89ea682SMatthew G Knepley   PetscFunctionBegin;
959a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
960aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
961aa1993deSMatthew G Knepley   if (dm->workin) {
962aa1993deSMatthew G Knepley     link       = dm->workin;
963aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
964aa1993deSMatthew G Knepley   } else {
965aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
966a89ea682SMatthew G Knepley   }
967aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
968aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
969aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
970aa1993deSMatthew G Knepley     ierr        = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
971aa1993deSMatthew G Knepley     link->bytes = size*count;
972aa1993deSMatthew G Knepley   }
973aa1993deSMatthew G Knepley   link->next   = dm->workout;
974aa1993deSMatthew G Knepley   dm->workout  = link;
975aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
976a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
977a89ea682SMatthew G Knepley }
978a89ea682SMatthew G Knepley 
979aa1993deSMatthew G Knepley #undef __FUNCT__
980aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
981aa1993deSMatthew G Knepley /*@C
982aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
983aa1993deSMatthew G Knepley 
984aa1993deSMatthew G Knepley   Not Collective
985aa1993deSMatthew G Knepley 
986aa1993deSMatthew G Knepley   Input Parameters:
987aa1993deSMatthew G Knepley + dm - the DM object
988aa1993deSMatthew G Knepley . count - The minium size
989aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
990aa1993deSMatthew G Knepley 
991aa1993deSMatthew G Knepley   Output Parameter:
992aa1993deSMatthew G Knepley . array - the work array
993aa1993deSMatthew G Knepley 
994aa1993deSMatthew G Knepley   Level: developer
995aa1993deSMatthew G Knepley 
996aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
997aa1993deSMatthew G Knepley @*/
998aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
999aa1993deSMatthew G Knepley {
1000aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1001aa1993deSMatthew G Knepley 
1002aa1993deSMatthew G Knepley   PetscFunctionBegin;
1003aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1004aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1005aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1006aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1007aa1993deSMatthew G Knepley       *p           = link->next;
1008aa1993deSMatthew G Knepley       link->next   = dm->workin;
1009aa1993deSMatthew G Knepley       dm->workin   = link;
10100298fd71SBarry Smith       *(void**)mem = NULL;
1011aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1012aa1993deSMatthew G Knepley     }
1013aa1993deSMatthew G Knepley   }
1014aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1015aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1016aa1993deSMatthew G Knepley }
1017e7c4fc90SDmitry Karpeev 
1018e7c4fc90SDmitry Karpeev #undef __FUNCT__
1019435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1020435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1021435a35e8SMatthew G Knepley {
1022435a35e8SMatthew G Knepley   PetscFunctionBegin;
1023435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
102482f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1025435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1026435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1027435a35e8SMatthew G Knepley }
1028435a35e8SMatthew G Knepley 
1029435a35e8SMatthew G Knepley #undef __FUNCT__
10304d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10314f3b5142SJed Brown /*@C
10324d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10334d343eeaSMatthew G Knepley 
10344d343eeaSMatthew G Knepley   Not collective
10354d343eeaSMatthew G Knepley 
10364d343eeaSMatthew G Knepley   Input Parameter:
10374d343eeaSMatthew G Knepley . dm - the DM object
10384d343eeaSMatthew G Knepley 
10394d343eeaSMatthew G Knepley   Output Parameters:
10400298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
10410298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
10420298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
10434d343eeaSMatthew G Knepley 
10444d343eeaSMatthew G Knepley   Level: intermediate
10454d343eeaSMatthew G Knepley 
104621c9b008SJed Brown   Notes:
104721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
104821c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
104921c9b008SJed Brown   PetscFree().
105021c9b008SJed Brown 
10514d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10524d343eeaSMatthew G Knepley @*/
105337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10544d343eeaSMatthew G Knepley {
105537d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10564d343eeaSMatthew G Knepley   PetscErrorCode ierr;
10574d343eeaSMatthew G Knepley 
10584d343eeaSMatthew G Knepley   PetscFunctionBegin;
10594d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
106069ca1f37SDmitry Karpeev   if (numFields) {
106169ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
106269ca1f37SDmitry Karpeev     *numFields = 0;
106369ca1f37SDmitry Karpeev   }
106437d0c07bSMatthew G Knepley   if (fieldNames) {
106537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
10660298fd71SBarry Smith     *fieldNames = NULL;
106769ca1f37SDmitry Karpeev   }
106869ca1f37SDmitry Karpeev   if (fields) {
106969ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
10700298fd71SBarry Smith     *fields = NULL;
107169ca1f37SDmitry Karpeev   }
107237d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
107337d0c07bSMatthew G Knepley   if (section) {
107437d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
107537d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
107637d0c07bSMatthew G Knepley 
107737d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
107837d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
107937d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt*,&fieldIndices);CHKERRQ(ierr);
108037d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
108137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
108237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
108337d0c07bSMatthew G Knepley     }
108437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
108537d0c07bSMatthew G Knepley       PetscInt gdof;
108637d0c07bSMatthew G Knepley 
108737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
108837d0c07bSMatthew G Knepley       if (gdof > 0) {
108937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
109037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
109137d0c07bSMatthew G Knepley 
109237d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
109337d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
109437d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
109537d0c07bSMatthew G Knepley         }
109637d0c07bSMatthew G Knepley       }
109737d0c07bSMatthew G Knepley     }
109837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
109937d0c07bSMatthew G Knepley       ierr          = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
110037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
110137d0c07bSMatthew G Knepley     }
110237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
110337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
110437d0c07bSMatthew G Knepley 
110537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
110637d0c07bSMatthew G Knepley       if (gdof > 0) {
110737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
110837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
110937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
111037d0c07bSMatthew G Knepley 
111137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
111237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
111337d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
111437d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
111537d0c07bSMatthew G Knepley           }
111637d0c07bSMatthew G Knepley         }
111737d0c07bSMatthew G Knepley       }
111837d0c07bSMatthew G Knepley     }
11198865f1eaSKarl Rupp     if (numFields) *numFields = nF;
112037d0c07bSMatthew G Knepley     if (fieldNames) {
112137d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char*), fieldNames);CHKERRQ(ierr);
112237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
112337d0c07bSMatthew G Knepley         const char *fieldName;
112437d0c07bSMatthew G Knepley 
112537d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
112637d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
112737d0c07bSMatthew G Knepley       }
112837d0c07bSMatthew G Knepley     }
112937d0c07bSMatthew G Knepley     if (fields) {
113037d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
113137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
113282f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
113337d0c07bSMatthew G Knepley       }
113437d0c07bSMatthew G Knepley     }
113537d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
11368865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
11378865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
113869ca1f37SDmitry Karpeev   }
11394d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11404d343eeaSMatthew G Knepley }
11414d343eeaSMatthew G Knepley 
114216621825SDmitry Karpeev 
114316621825SDmitry Karpeev #undef __FUNCT__
114416621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
114516621825SDmitry Karpeev /*@C
114616621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
114716621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
114816621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1149e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1150e7c4fc90SDmitry Karpeev 
1151e7c4fc90SDmitry Karpeev   Not collective
1152e7c4fc90SDmitry Karpeev 
1153e7c4fc90SDmitry Karpeev   Input Parameter:
1154e7c4fc90SDmitry Karpeev . dm - the DM object
1155e7c4fc90SDmitry Karpeev 
1156e7c4fc90SDmitry Karpeev   Output Parameters:
11570298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
11580298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
11590298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
11600298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1161e7c4fc90SDmitry Karpeev 
1162e7c4fc90SDmitry Karpeev   Level: intermediate
1163e7c4fc90SDmitry Karpeev 
1164e7c4fc90SDmitry Karpeev   Notes:
1165e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1166e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1167e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1168e7c4fc90SDmitry Karpeev 
1169e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1170e7c4fc90SDmitry Karpeev @*/
117116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1172e7c4fc90SDmitry Karpeev {
1173e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1174e7c4fc90SDmitry Karpeev 
1175e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1176e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11778865f1eaSKarl Rupp   if (len) {
11788865f1eaSKarl Rupp     PetscValidPointer(len,2);
11798865f1eaSKarl Rupp     *len = 0;
11808865f1eaSKarl Rupp   }
11818865f1eaSKarl Rupp   if (namelist) {
11828865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
11838865f1eaSKarl Rupp     *namelist = 0;
11848865f1eaSKarl Rupp   }
11858865f1eaSKarl Rupp   if (islist) {
11868865f1eaSKarl Rupp     PetscValidPointer(islist,4);
11878865f1eaSKarl Rupp     *islist = 0;
11888865f1eaSKarl Rupp   }
11898865f1eaSKarl Rupp   if (dmlist) {
11908865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
11918865f1eaSKarl Rupp     *dmlist = 0;
11928865f1eaSKarl Rupp   }
1193f3f0edfdSDmitry Karpeev   /*
1194f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1195f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1196f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1197f3f0edfdSDmitry Karpeev    */
1198ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
119916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1200435a35e8SMatthew G Knepley     PetscSection section;
1201435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1202435a35e8SMatthew G Knepley 
1203435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1204435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1205435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1206435a35e8SMatthew G Knepley       *len = numFields;
1207435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1208435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1209435a35e8SMatthew G Knepley         const char *fieldName;
1210435a35e8SMatthew G Knepley 
1211435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1212435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1213435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1214435a35e8SMatthew G Knepley       }
1215435a35e8SMatthew G Knepley     } else {
121669ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1217e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
12180298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1219e7c4fc90SDmitry Karpeev     }
12208865f1eaSKarl Rupp   } else {
122116621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
122216621825SDmitry Karpeev   }
122316621825SDmitry Karpeev   PetscFunctionReturn(0);
122416621825SDmitry Karpeev }
122516621825SDmitry Karpeev 
122616621825SDmitry Karpeev #undef __FUNCT__
1227435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1228435a35e8SMatthew G Knepley /*@C
1229435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1230435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1231435a35e8SMatthew G Knepley 
1232435a35e8SMatthew G Knepley   Not collective
1233435a35e8SMatthew G Knepley 
1234435a35e8SMatthew G Knepley   Input Parameters:
1235435a35e8SMatthew G Knepley + dm - the DM object
1236435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
12370298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1238435a35e8SMatthew G Knepley 
1239435a35e8SMatthew G Knepley   Output Parameters:
1240435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1241435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1242435a35e8SMatthew G Knepley 
1243435a35e8SMatthew G Knepley   Level: intermediate
1244435a35e8SMatthew G Knepley 
1245435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1246435a35e8SMatthew G Knepley @*/
1247435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1248435a35e8SMatthew G Knepley {
1249435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1250435a35e8SMatthew G Knepley 
1251435a35e8SMatthew G Knepley   PetscFunctionBegin;
1252435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1253435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
12548865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
12558865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1256435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1257435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
125882f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1259435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1260435a35e8SMatthew G Knepley }
1261435a35e8SMatthew G Knepley 
126216621825SDmitry Karpeev 
126316621825SDmitry Karpeev #undef __FUNCT__
126416621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
126516621825SDmitry Karpeev /*@C
12668d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
12678d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
12688d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
12698d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
12708d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
127116621825SDmitry Karpeev 
127216621825SDmitry Karpeev   Not collective
127316621825SDmitry Karpeev 
127416621825SDmitry Karpeev   Input Parameter:
127516621825SDmitry Karpeev . dm - the DM object
127616621825SDmitry Karpeev 
127716621825SDmitry Karpeev   Output Parameters:
12780298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
12790298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
12800298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
12810298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
12820298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
128316621825SDmitry Karpeev 
128416621825SDmitry Karpeev   Level: intermediate
128516621825SDmitry Karpeev 
128616621825SDmitry Karpeev   Notes:
128716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
128816621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
128916621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
129016621825SDmitry Karpeev 
12918d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
129216621825SDmitry Karpeev @*/
12938d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
129416621825SDmitry Karpeev {
129516621825SDmitry Karpeev   PetscErrorCode      ierr;
1296be081cd6SPeter Brune   DMSubDomainHookLink link;
1297be081cd6SPeter Brune   PetscInt            i,l;
129816621825SDmitry Karpeev 
129916621825SDmitry Karpeev   PetscFunctionBegin;
130016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
130114a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
13020298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
13030298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
13040298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
13050298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1306f3f0edfdSDmitry Karpeev   /*
1307f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1308f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1309f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1310f3f0edfdSDmitry Karpeev    */
1311ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
131216621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1313be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
131414a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
131514a18fd3SPeter Brune     if (dmlist) {
1316be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1317be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1318be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1319be081cd6SPeter Brune         }
1320d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1321e7c4fc90SDmitry Karpeev       }
132214a18fd3SPeter Brune     }
132314a18fd3SPeter Brune     if (len) *len = l;
132414a18fd3SPeter Brune   }
1325e30e807fSPeter Brune   PetscFunctionReturn(0);
1326e30e807fSPeter Brune }
1327e30e807fSPeter Brune 
1328e30e807fSPeter Brune 
1329e30e807fSPeter Brune #undef __FUNCT__
1330e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1331e30e807fSPeter Brune /*@C
1332e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1333e30e807fSPeter Brune 
1334e30e807fSPeter Brune   Not collective
1335e30e807fSPeter Brune 
1336e30e807fSPeter Brune   Input Parameters:
1337e30e807fSPeter Brune + dm - the DM object
1338e30e807fSPeter Brune . n  - the number of subdomain scatters
1339e30e807fSPeter Brune - subdms - the local subdomains
1340e30e807fSPeter Brune 
1341e30e807fSPeter Brune   Output Parameters:
1342e30e807fSPeter Brune + n     - the number of scatters returned
1343e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1344e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1345e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1346e30e807fSPeter Brune 
1347e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1348e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1349e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1350e30e807fSPeter Brune   solution and residual data.
1351e30e807fSPeter Brune 
1352e30e807fSPeter Brune   Level: developer
1353e30e807fSPeter Brune 
1354e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1355e30e807fSPeter Brune @*/
1356e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1357e30e807fSPeter Brune {
1358e30e807fSPeter Brune   PetscErrorCode ierr;
1359e30e807fSPeter Brune 
1360e30e807fSPeter Brune   PetscFunctionBegin;
1361e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1362e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1363e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1364e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
136582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1366e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1367e7c4fc90SDmitry Karpeev }
1368e7c4fc90SDmitry Karpeev 
1369731c8d9eSDmitry Karpeev #undef __FUNCT__
137047c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
137147c6ae99SBarry Smith /*@
137247c6ae99SBarry Smith   DMRefine - Refines a DM object
137347c6ae99SBarry Smith 
137447c6ae99SBarry Smith   Collective on DM
137547c6ae99SBarry Smith 
137647c6ae99SBarry Smith   Input Parameter:
137747c6ae99SBarry Smith + dm   - the DM object
137891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
137947c6ae99SBarry Smith 
138047c6ae99SBarry Smith   Output Parameter:
13810298fd71SBarry Smith . dmf - the refined DM, or NULL
1382ae0a1c52SMatthew G Knepley 
13830298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
138447c6ae99SBarry Smith 
138547c6ae99SBarry Smith   Level: developer
138647c6ae99SBarry Smith 
1387e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
138847c6ae99SBarry Smith @*/
13897087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
139047c6ae99SBarry Smith {
139147c6ae99SBarry Smith   PetscErrorCode   ierr;
1392c833c3b5SJed Brown   DMRefineHookLink link;
139347c6ae99SBarry Smith 
139447c6ae99SBarry Smith   PetscFunctionBegin;
1395732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
139647c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
13974057135bSMatthew G Knepley   if (*dmf) {
139843842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
13998865f1eaSKarl Rupp 
14008cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
14018865f1eaSKarl Rupp 
1402644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14030598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1404656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
14058865f1eaSKarl Rupp 
1406e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1407c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
14088865f1eaSKarl Rupp       if (link->refinehook) {
14098865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
14108865f1eaSKarl Rupp       }
1411c833c3b5SJed Brown     }
1412c833c3b5SJed Brown   }
1413c833c3b5SJed Brown   PetscFunctionReturn(0);
1414c833c3b5SJed Brown }
1415c833c3b5SJed Brown 
1416c833c3b5SJed Brown #undef __FUNCT__
1417c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1418bb9467b5SJed Brown /*@C
1419c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1420c833c3b5SJed Brown 
1421c833c3b5SJed Brown    Logically Collective
1422c833c3b5SJed Brown 
1423c833c3b5SJed Brown    Input Arguments:
1424c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1425c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1426c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
14270298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1428c833c3b5SJed Brown 
1429c833c3b5SJed Brown    Calling sequence of refinehook:
1430c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1431c833c3b5SJed Brown 
1432c833c3b5SJed Brown +  coarse - coarse level DM
1433c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1434c833c3b5SJed Brown -  ctx - optional user-defined function context
1435c833c3b5SJed Brown 
1436c833c3b5SJed Brown    Calling sequence for interphook:
1437c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1438c833c3b5SJed Brown 
1439c833c3b5SJed Brown +  coarse - coarse level DM
1440c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1441c833c3b5SJed Brown .  fine - fine level DM to update
1442c833c3b5SJed Brown -  ctx - optional user-defined function context
1443c833c3b5SJed Brown 
1444c833c3b5SJed Brown    Level: advanced
1445c833c3b5SJed Brown 
1446c833c3b5SJed Brown    Notes:
1447c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1448c833c3b5SJed Brown 
1449c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1450c833c3b5SJed Brown 
1451bb9467b5SJed Brown    This function is currently not available from Fortran.
1452bb9467b5SJed Brown 
1453c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1454c833c3b5SJed Brown @*/
1455c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1456c833c3b5SJed Brown {
1457c833c3b5SJed Brown   PetscErrorCode   ierr;
1458c833c3b5SJed Brown   DMRefineHookLink link,*p;
1459c833c3b5SJed Brown 
1460c833c3b5SJed Brown   PetscFunctionBegin;
1461c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1462c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1463c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1464c833c3b5SJed Brown   link->refinehook = refinehook;
1465c833c3b5SJed Brown   link->interphook = interphook;
1466c833c3b5SJed Brown   link->ctx        = ctx;
14670298fd71SBarry Smith   link->next       = NULL;
1468c833c3b5SJed Brown   *p               = link;
1469c833c3b5SJed Brown   PetscFunctionReturn(0);
1470c833c3b5SJed Brown }
1471c833c3b5SJed Brown 
1472c833c3b5SJed Brown #undef __FUNCT__
1473c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1474c833c3b5SJed Brown /*@
1475c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1476c833c3b5SJed Brown 
1477c833c3b5SJed Brown    Collective if any hooks are
1478c833c3b5SJed Brown 
1479c833c3b5SJed Brown    Input Arguments:
1480c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1481c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1482c833c3b5SJed Brown -  fine - finer DM to update
1483c833c3b5SJed Brown 
1484c833c3b5SJed Brown    Level: developer
1485c833c3b5SJed Brown 
1486c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1487c833c3b5SJed Brown @*/
1488c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1489c833c3b5SJed Brown {
1490c833c3b5SJed Brown   PetscErrorCode   ierr;
1491c833c3b5SJed Brown   DMRefineHookLink link;
1492c833c3b5SJed Brown 
1493c833c3b5SJed Brown   PetscFunctionBegin;
1494c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
14958865f1eaSKarl Rupp     if (link->interphook) {
14968865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
14978865f1eaSKarl Rupp     }
14984057135bSMatthew G Knepley   }
149947c6ae99SBarry Smith   PetscFunctionReturn(0);
150047c6ae99SBarry Smith }
150147c6ae99SBarry Smith 
150247c6ae99SBarry Smith #undef __FUNCT__
1503eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1504eb3f98d2SBarry Smith /*@
1505eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1506eb3f98d2SBarry Smith 
1507eb3f98d2SBarry Smith     Not Collective
1508eb3f98d2SBarry Smith 
1509eb3f98d2SBarry Smith     Input Parameter:
1510eb3f98d2SBarry Smith .   dm - the DM object
1511eb3f98d2SBarry Smith 
1512eb3f98d2SBarry Smith     Output Parameter:
1513eb3f98d2SBarry Smith .   level - number of refinements
1514eb3f98d2SBarry Smith 
1515eb3f98d2SBarry Smith     Level: developer
1516eb3f98d2SBarry Smith 
15176a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1518eb3f98d2SBarry Smith 
1519eb3f98d2SBarry Smith @*/
1520eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1521eb3f98d2SBarry Smith {
1522eb3f98d2SBarry Smith   PetscFunctionBegin;
1523eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1524eb3f98d2SBarry Smith   *level = dm->levelup;
1525eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1526eb3f98d2SBarry Smith }
1527eb3f98d2SBarry Smith 
1528eb3f98d2SBarry Smith #undef __FUNCT__
1529baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1530bb9467b5SJed Brown /*@C
1531baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1532baf369e7SPeter Brune 
1533baf369e7SPeter Brune    Logically Collective
1534baf369e7SPeter Brune 
1535baf369e7SPeter Brune    Input Arguments:
1536baf369e7SPeter Brune +  dm - the DM
1537baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1538baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
15390298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1540baf369e7SPeter Brune 
1541baf369e7SPeter Brune    Calling sequence for beginhook:
1542baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1543baf369e7SPeter Brune 
1544baf369e7SPeter Brune +  dm - global DM
1545baf369e7SPeter Brune .  g - global vector
1546baf369e7SPeter Brune .  mode - mode
1547baf369e7SPeter Brune .  l - local vector
1548baf369e7SPeter Brune -  ctx - optional user-defined function context
1549baf369e7SPeter Brune 
1550baf369e7SPeter Brune 
1551baf369e7SPeter Brune    Calling sequence for endhook:
1552ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1553baf369e7SPeter Brune 
1554baf369e7SPeter Brune +  global - global DM
1555baf369e7SPeter Brune -  ctx - optional user-defined function context
1556baf369e7SPeter Brune 
1557baf369e7SPeter Brune    Level: advanced
1558baf369e7SPeter Brune 
1559baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1560baf369e7SPeter Brune @*/
1561baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1562baf369e7SPeter Brune {
1563baf369e7SPeter Brune   PetscErrorCode          ierr;
1564baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1565baf369e7SPeter Brune 
1566baf369e7SPeter Brune   PetscFunctionBegin;
1567baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1568baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1569baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1570baf369e7SPeter Brune   link->beginhook = beginhook;
1571baf369e7SPeter Brune   link->endhook   = endhook;
1572baf369e7SPeter Brune   link->ctx       = ctx;
15730298fd71SBarry Smith   link->next      = NULL;
1574baf369e7SPeter Brune   *p              = link;
1575baf369e7SPeter Brune   PetscFunctionReturn(0);
1576baf369e7SPeter Brune }
1577baf369e7SPeter Brune 
1578baf369e7SPeter Brune #undef __FUNCT__
157947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
158047c6ae99SBarry Smith /*@
158147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
158247c6ae99SBarry Smith 
158347c6ae99SBarry Smith     Neighbor-wise Collective on DM
158447c6ae99SBarry Smith 
158547c6ae99SBarry Smith     Input Parameters:
158647c6ae99SBarry Smith +   dm - the DM object
158747c6ae99SBarry Smith .   g - the global vector
158847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
158947c6ae99SBarry Smith -   l - the local vector
159047c6ae99SBarry Smith 
159147c6ae99SBarry Smith 
159247c6ae99SBarry Smith     Level: beginner
159347c6ae99SBarry Smith 
1594e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
159547c6ae99SBarry Smith 
159647c6ae99SBarry Smith @*/
15977087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
159847c6ae99SBarry Smith {
15997128ae9fSMatthew G Knepley   PetscSF                 sf;
160047c6ae99SBarry Smith   PetscErrorCode          ierr;
1601baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
160247c6ae99SBarry Smith 
160347c6ae99SBarry Smith   PetscFunctionBegin;
1604171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1605baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
16068865f1eaSKarl Rupp     if (link->beginhook) {
16078865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
16088865f1eaSKarl Rupp     }
1609baf369e7SPeter Brune   }
16107128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16117128ae9fSMatthew G Knepley   if (sf) {
16127128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16137128ae9fSMatthew G Knepley 
161482f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16157128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16167128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16177128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16187128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16197128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16207128ae9fSMatthew G Knepley   } else {
1621843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16227128ae9fSMatthew G Knepley   }
162347c6ae99SBarry Smith   PetscFunctionReturn(0);
162447c6ae99SBarry Smith }
162547c6ae99SBarry Smith 
162647c6ae99SBarry Smith #undef __FUNCT__
162747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
162847c6ae99SBarry Smith /*@
162947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
163047c6ae99SBarry Smith 
163147c6ae99SBarry Smith     Neighbor-wise Collective on DM
163247c6ae99SBarry Smith 
163347c6ae99SBarry Smith     Input Parameters:
163447c6ae99SBarry Smith +   dm - the DM object
163547c6ae99SBarry Smith .   g - the global vector
163647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
163747c6ae99SBarry Smith -   l - the local vector
163847c6ae99SBarry Smith 
163947c6ae99SBarry Smith 
164047c6ae99SBarry Smith     Level: beginner
164147c6ae99SBarry Smith 
1642e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
164347c6ae99SBarry Smith 
164447c6ae99SBarry Smith @*/
16457087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
164647c6ae99SBarry Smith {
16477128ae9fSMatthew G Knepley   PetscSF                 sf;
164847c6ae99SBarry Smith   PetscErrorCode          ierr;
164961a3c1faSSatish Balay   PetscScalar             *lArray, *gArray;
1650baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
165147c6ae99SBarry Smith 
165247c6ae99SBarry Smith   PetscFunctionBegin;
1653171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16547128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16557128ae9fSMatthew G Knepley   if (sf) {
165682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16577128ae9fSMatthew G Knepley 
16587128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16597128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16607128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16617128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16627128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16637128ae9fSMatthew G Knepley   } else {
1664843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16657128ae9fSMatthew G Knepley   }
1666baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1667baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1668baf369e7SPeter Brune   }
166947c6ae99SBarry Smith   PetscFunctionReturn(0);
167047c6ae99SBarry Smith }
167147c6ae99SBarry Smith 
167247c6ae99SBarry Smith #undef __FUNCT__
16739a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
167447c6ae99SBarry Smith /*@
16759a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
16769a42bb27SBarry Smith 
16779a42bb27SBarry Smith     Neighbor-wise Collective on DM
16789a42bb27SBarry Smith 
16799a42bb27SBarry Smith     Input Parameters:
16809a42bb27SBarry Smith +   dm - the DM object
1681f6813fd5SJed Brown .   l - the local vector
16829a42bb27SBarry 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
16839a42bb27SBarry Smith            base point.
1684f6813fd5SJed Brown - - the global vector
16859a42bb27SBarry Smith 
16869a42bb27SBarry 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
16879a42bb27SBarry 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
16889a42bb27SBarry Smith            global array to the final global array with VecAXPY().
16899a42bb27SBarry Smith 
16909a42bb27SBarry Smith     Level: beginner
16919a42bb27SBarry Smith 
1692e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
16939a42bb27SBarry Smith 
16949a42bb27SBarry Smith @*/
16957087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
16969a42bb27SBarry Smith {
16977128ae9fSMatthew G Knepley   PetscSF        sf;
16989a42bb27SBarry Smith   PetscErrorCode ierr;
16999a42bb27SBarry Smith 
17009a42bb27SBarry Smith   PetscFunctionBegin;
1701171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17027128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17037128ae9fSMatthew G Knepley   if (sf) {
17047128ae9fSMatthew G Knepley     MPI_Op      op;
17057128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17067128ae9fSMatthew G Knepley 
17077128ae9fSMatthew G Knepley     switch (mode) {
17087128ae9fSMatthew G Knepley     case INSERT_VALUES:
17097128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17108bfbc91cSJed Brown       op = MPIU_REPLACE; break;
17117128ae9fSMatthew G Knepley     case ADD_VALUES:
17127128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17137128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17147128ae9fSMatthew G Knepley     default:
171582f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17167128ae9fSMatthew G Knepley     }
17177128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17187128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17197128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17207128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17217128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17227128ae9fSMatthew G Knepley   } else {
1723843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17247128ae9fSMatthew G Knepley   }
17259a42bb27SBarry Smith   PetscFunctionReturn(0);
17269a42bb27SBarry Smith }
17279a42bb27SBarry Smith 
17289a42bb27SBarry Smith #undef __FUNCT__
17299a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
17309a42bb27SBarry Smith /*@
17319a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
173247c6ae99SBarry Smith 
173347c6ae99SBarry Smith     Neighbor-wise Collective on DM
173447c6ae99SBarry Smith 
173547c6ae99SBarry Smith     Input Parameters:
173647c6ae99SBarry Smith +   dm - the DM object
1737f6813fd5SJed Brown .   l - the local vector
173847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1739f6813fd5SJed Brown -   g - the global vector
174047c6ae99SBarry Smith 
174147c6ae99SBarry Smith 
174247c6ae99SBarry Smith     Level: beginner
174347c6ae99SBarry Smith 
1744e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
174547c6ae99SBarry Smith 
174647c6ae99SBarry Smith @*/
17477087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
174847c6ae99SBarry Smith {
17497128ae9fSMatthew G Knepley   PetscSF        sf;
175047c6ae99SBarry Smith   PetscErrorCode ierr;
175147c6ae99SBarry Smith 
175247c6ae99SBarry Smith   PetscFunctionBegin;
1753171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17547128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17557128ae9fSMatthew G Knepley   if (sf) {
17567128ae9fSMatthew G Knepley     MPI_Op      op;
17577128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17587128ae9fSMatthew G Knepley 
17597128ae9fSMatthew G Knepley     switch (mode) {
17607128ae9fSMatthew G Knepley     case INSERT_VALUES:
17617128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17628bfbc91cSJed Brown       op = MPIU_REPLACE; break;
17637128ae9fSMatthew G Knepley     case ADD_VALUES:
17647128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17657128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17667128ae9fSMatthew G Knepley     default:
176782f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17687128ae9fSMatthew G Knepley     }
17697128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17707128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17717128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17727128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17737128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17747128ae9fSMatthew G Knepley   } else {
1775843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
17767128ae9fSMatthew G Knepley   }
177747c6ae99SBarry Smith   PetscFunctionReturn(0);
177847c6ae99SBarry Smith }
177947c6ae99SBarry Smith 
178047c6ae99SBarry Smith #undef __FUNCT__
178147c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
178247c6ae99SBarry Smith /*@
178347c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
178447c6ae99SBarry Smith 
178547c6ae99SBarry Smith     Collective on DM
178647c6ae99SBarry Smith 
178747c6ae99SBarry Smith     Input Parameter:
178847c6ae99SBarry Smith +   dm - the DM object
178991d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
179047c6ae99SBarry Smith 
179147c6ae99SBarry Smith     Output Parameter:
179247c6ae99SBarry Smith .   dmc - the coarsened DM
179347c6ae99SBarry Smith 
179447c6ae99SBarry Smith     Level: developer
179547c6ae99SBarry Smith 
1796e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
179747c6ae99SBarry Smith 
179847c6ae99SBarry Smith @*/
17997087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
180047c6ae99SBarry Smith {
180147c6ae99SBarry Smith   PetscErrorCode    ierr;
1802b17ce1afSJed Brown   DMCoarsenHookLink link;
180347c6ae99SBarry Smith 
180447c6ae99SBarry Smith   PetscFunctionBegin;
1805171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
180647c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
180743842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
18088cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1809644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
18100598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
1811656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
1812e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1813b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1814b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1815b17ce1afSJed Brown   }
1816b17ce1afSJed Brown   PetscFunctionReturn(0);
1817b17ce1afSJed Brown }
1818b17ce1afSJed Brown 
1819b17ce1afSJed Brown #undef __FUNCT__
1820b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1821bb9467b5SJed Brown /*@C
1822b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1823b17ce1afSJed Brown 
1824b17ce1afSJed Brown    Logically Collective
1825b17ce1afSJed Brown 
1826b17ce1afSJed Brown    Input Arguments:
1827b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1828b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1829b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
18300298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1831b17ce1afSJed Brown 
1832b17ce1afSJed Brown    Calling sequence of coarsenhook:
1833b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1834b17ce1afSJed Brown 
1835b17ce1afSJed Brown +  fine - fine level DM
1836b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1837b17ce1afSJed Brown -  ctx - optional user-defined function context
1838b17ce1afSJed Brown 
1839b17ce1afSJed Brown    Calling sequence for restricthook:
1840c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1841b17ce1afSJed Brown 
1842b17ce1afSJed Brown +  fine - fine level DM
1843b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1844c833c3b5SJed Brown .  rscale - scaling vector for restriction
1845c833c3b5SJed Brown .  inject - matrix restricting by injection
1846b17ce1afSJed Brown .  coarse - coarse level DM to update
1847b17ce1afSJed Brown -  ctx - optional user-defined function context
1848b17ce1afSJed Brown 
1849b17ce1afSJed Brown    Level: advanced
1850b17ce1afSJed Brown 
1851b17ce1afSJed Brown    Notes:
1852b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1853b17ce1afSJed Brown 
1854b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1855b17ce1afSJed Brown 
1856b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1857b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1858b17ce1afSJed Brown 
1859bb9467b5SJed Brown    This function is currently not available from Fortran.
1860bb9467b5SJed Brown 
1861c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1862b17ce1afSJed Brown @*/
1863b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1864b17ce1afSJed Brown {
1865b17ce1afSJed Brown   PetscErrorCode    ierr;
1866b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1867b17ce1afSJed Brown 
1868b17ce1afSJed Brown   PetscFunctionBegin;
1869b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
18706bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1871b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1872b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
1873b17ce1afSJed Brown   link->restricthook = restricthook;
1874b17ce1afSJed Brown   link->ctx          = ctx;
18750298fd71SBarry Smith   link->next         = NULL;
1876b17ce1afSJed Brown   *p                 = link;
1877b17ce1afSJed Brown   PetscFunctionReturn(0);
1878b17ce1afSJed Brown }
1879b17ce1afSJed Brown 
1880b17ce1afSJed Brown #undef __FUNCT__
1881b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1882b17ce1afSJed Brown /*@
1883b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1884b17ce1afSJed Brown 
1885b17ce1afSJed Brown    Collective if any hooks are
1886b17ce1afSJed Brown 
1887b17ce1afSJed Brown    Input Arguments:
1888b17ce1afSJed Brown +  fine - finer DM to use as a base
1889b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1890b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1891b17ce1afSJed Brown -  coarse - coarer DM to update
1892b17ce1afSJed Brown 
1893b17ce1afSJed Brown    Level: developer
1894b17ce1afSJed Brown 
1895b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1896b17ce1afSJed Brown @*/
1897b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1898b17ce1afSJed Brown {
1899b17ce1afSJed Brown   PetscErrorCode    ierr;
1900b17ce1afSJed Brown   DMCoarsenHookLink link;
1901b17ce1afSJed Brown 
1902b17ce1afSJed Brown   PetscFunctionBegin;
1903b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
19048865f1eaSKarl Rupp     if (link->restricthook) {
19058865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
19068865f1eaSKarl Rupp     }
1907b17ce1afSJed Brown   }
190847c6ae99SBarry Smith   PetscFunctionReturn(0);
190947c6ae99SBarry Smith }
191047c6ae99SBarry Smith 
191147c6ae99SBarry Smith #undef __FUNCT__
1912be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
1913bb9467b5SJed Brown /*@C
1914be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
19155dbd56e3SPeter Brune 
19165dbd56e3SPeter Brune    Logically Collective
19175dbd56e3SPeter Brune 
19185dbd56e3SPeter Brune    Input Arguments:
19195dbd56e3SPeter Brune +  global - global DM
1920ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
19215dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
19220298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19235dbd56e3SPeter Brune 
1924ec4806b8SPeter Brune 
1925ec4806b8SPeter Brune    Calling sequence for ddhook:
1926ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
1927ec4806b8SPeter Brune 
1928ec4806b8SPeter Brune +  global - global DM
1929ec4806b8SPeter Brune .  block  - block DM
1930ec4806b8SPeter Brune -  ctx - optional user-defined function context
1931ec4806b8SPeter Brune 
19325dbd56e3SPeter Brune    Calling sequence for restricthook:
1933ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
19345dbd56e3SPeter Brune 
19355dbd56e3SPeter Brune +  global - global DM
19365dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
19375dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
1938ec4806b8SPeter Brune .  block  - block DM
19395dbd56e3SPeter Brune -  ctx - optional user-defined function context
19405dbd56e3SPeter Brune 
19415dbd56e3SPeter Brune    Level: advanced
19425dbd56e3SPeter Brune 
19435dbd56e3SPeter Brune    Notes:
1944ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
19455dbd56e3SPeter Brune 
19465dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
19475dbd56e3SPeter Brune 
19485dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1949ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
19505dbd56e3SPeter Brune 
1951bb9467b5SJed Brown    This function is currently not available from Fortran.
1952bb9467b5SJed Brown 
19535dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19545dbd56e3SPeter Brune @*/
1955be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
19565dbd56e3SPeter Brune {
19575dbd56e3SPeter Brune   PetscErrorCode      ierr;
1958be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
19595dbd56e3SPeter Brune 
19605dbd56e3SPeter Brune   PetscFunctionBegin;
19615dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
1962be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1963be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
19645dbd56e3SPeter Brune   link->restricthook = restricthook;
1965be081cd6SPeter Brune   link->ddhook       = ddhook;
19665dbd56e3SPeter Brune   link->ctx          = ctx;
19670298fd71SBarry Smith   link->next         = NULL;
19685dbd56e3SPeter Brune   *p                 = link;
19695dbd56e3SPeter Brune   PetscFunctionReturn(0);
19705dbd56e3SPeter Brune }
19715dbd56e3SPeter Brune 
19725dbd56e3SPeter Brune #undef __FUNCT__
1973be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
19745dbd56e3SPeter Brune /*@
1975be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
19765dbd56e3SPeter Brune 
19775dbd56e3SPeter Brune    Collective if any hooks are
19785dbd56e3SPeter Brune 
19795dbd56e3SPeter Brune    Input Arguments:
19805dbd56e3SPeter Brune +  fine - finer DM to use as a base
1981be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
1982be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
19835dbd56e3SPeter Brune -  coarse - coarer DM to update
19845dbd56e3SPeter Brune 
19855dbd56e3SPeter Brune    Level: developer
19865dbd56e3SPeter Brune 
19875dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
19885dbd56e3SPeter Brune @*/
1989be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
19905dbd56e3SPeter Brune {
19915dbd56e3SPeter Brune   PetscErrorCode      ierr;
1992be081cd6SPeter Brune   DMSubDomainHookLink link;
19935dbd56e3SPeter Brune 
19945dbd56e3SPeter Brune   PetscFunctionBegin;
1995be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
19968865f1eaSKarl Rupp     if (link->restricthook) {
19978865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
19988865f1eaSKarl Rupp     }
19995dbd56e3SPeter Brune   }
20005dbd56e3SPeter Brune   PetscFunctionReturn(0);
20015dbd56e3SPeter Brune }
20025dbd56e3SPeter Brune 
20035dbd56e3SPeter Brune #undef __FUNCT__
20045fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
20055fe1f584SPeter Brune /*@
20066a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
20075fe1f584SPeter Brune 
20085fe1f584SPeter Brune     Not Collective
20095fe1f584SPeter Brune 
20105fe1f584SPeter Brune     Input Parameter:
20115fe1f584SPeter Brune .   dm - the DM object
20125fe1f584SPeter Brune 
20135fe1f584SPeter Brune     Output Parameter:
20146a7d9d85SPeter Brune .   level - number of coarsenings
20155fe1f584SPeter Brune 
20165fe1f584SPeter Brune     Level: developer
20175fe1f584SPeter Brune 
20186a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
20195fe1f584SPeter Brune 
20205fe1f584SPeter Brune @*/
20215fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
20225fe1f584SPeter Brune {
20235fe1f584SPeter Brune   PetscFunctionBegin;
20245fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20255fe1f584SPeter Brune   *level = dm->leveldown;
20265fe1f584SPeter Brune   PetscFunctionReturn(0);
20275fe1f584SPeter Brune }
20285fe1f584SPeter Brune 
20295fe1f584SPeter Brune 
20305fe1f584SPeter Brune 
20315fe1f584SPeter Brune #undef __FUNCT__
203247c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
203347c6ae99SBarry Smith /*@C
203447c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
203547c6ae99SBarry Smith 
203647c6ae99SBarry Smith     Collective on DM
203747c6ae99SBarry Smith 
203847c6ae99SBarry Smith     Input Parameter:
203947c6ae99SBarry Smith +   dm - the DM object
204047c6ae99SBarry Smith -   nlevels - the number of levels of refinement
204147c6ae99SBarry Smith 
204247c6ae99SBarry Smith     Output Parameter:
204347c6ae99SBarry Smith .   dmf - the refined DM hierarchy
204447c6ae99SBarry Smith 
204547c6ae99SBarry Smith     Level: developer
204647c6ae99SBarry Smith 
2047e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
204847c6ae99SBarry Smith 
204947c6ae99SBarry Smith @*/
20507087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
205147c6ae99SBarry Smith {
205247c6ae99SBarry Smith   PetscErrorCode ierr;
205347c6ae99SBarry Smith 
205447c6ae99SBarry Smith   PetscFunctionBegin;
2055171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2056ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
205747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
205847c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
205947c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
206047c6ae99SBarry Smith   } else if (dm->ops->refine) {
206147c6ae99SBarry Smith     PetscInt i;
206247c6ae99SBarry Smith 
2063ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
206447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2065ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
206647c6ae99SBarry Smith     }
2067ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
206847c6ae99SBarry Smith   PetscFunctionReturn(0);
206947c6ae99SBarry Smith }
207047c6ae99SBarry Smith 
207147c6ae99SBarry Smith #undef __FUNCT__
207247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
207347c6ae99SBarry Smith /*@C
207447c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
207547c6ae99SBarry Smith 
207647c6ae99SBarry Smith     Collective on DM
207747c6ae99SBarry Smith 
207847c6ae99SBarry Smith     Input Parameter:
207947c6ae99SBarry Smith +   dm - the DM object
208047c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
208147c6ae99SBarry Smith 
208247c6ae99SBarry Smith     Output Parameter:
208347c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
208447c6ae99SBarry Smith 
208547c6ae99SBarry Smith     Level: developer
208647c6ae99SBarry Smith 
2087e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
208847c6ae99SBarry Smith 
208947c6ae99SBarry Smith @*/
20907087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
209147c6ae99SBarry Smith {
209247c6ae99SBarry Smith   PetscErrorCode ierr;
209347c6ae99SBarry Smith 
209447c6ae99SBarry Smith   PetscFunctionBegin;
2095171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2096ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
209747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
209847c6ae99SBarry Smith   PetscValidPointer(dmc,3);
209947c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
210047c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
210147c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
210247c6ae99SBarry Smith     PetscInt i;
210347c6ae99SBarry Smith 
2104ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
210547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2106ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
210747c6ae99SBarry Smith     }
2108ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
210947c6ae99SBarry Smith   PetscFunctionReturn(0);
211047c6ae99SBarry Smith }
211147c6ae99SBarry Smith 
211247c6ae99SBarry Smith #undef __FUNCT__
2113e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
211447c6ae99SBarry Smith /*@
2115e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
211647c6ae99SBarry Smith    grids associated with two DMs.
211747c6ae99SBarry Smith 
211847c6ae99SBarry Smith    Collective on DM
211947c6ae99SBarry Smith 
212047c6ae99SBarry Smith    Input Parameters:
212147c6ae99SBarry Smith +  dmc - the coarse grid DM
212247c6ae99SBarry Smith -  dmf - the fine grid DM
212347c6ae99SBarry Smith 
212447c6ae99SBarry Smith    Output Parameters:
212547c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
212647c6ae99SBarry Smith 
212747c6ae99SBarry Smith    Level: intermediate
212847c6ae99SBarry Smith 
212947c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
213047c6ae99SBarry Smith 
2131e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
213247c6ae99SBarry Smith @*/
2133e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
213447c6ae99SBarry Smith {
213547c6ae99SBarry Smith   PetscErrorCode ierr;
213647c6ae99SBarry Smith 
213747c6ae99SBarry Smith   PetscFunctionBegin;
2138171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2139171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
214047c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
214147c6ae99SBarry Smith   PetscFunctionReturn(0);
214247c6ae99SBarry Smith }
214347c6ae99SBarry Smith 
214447c6ae99SBarry Smith #undef __FUNCT__
21451a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
21461a266240SBarry Smith /*@C
21471a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
21481a266240SBarry Smith 
21491a266240SBarry Smith     Not Collective
21501a266240SBarry Smith 
21511a266240SBarry Smith     Input Parameters:
21521a266240SBarry Smith +   dm - the DM object
21531a266240SBarry Smith -   destroy - the destroy function
21541a266240SBarry Smith 
21551a266240SBarry Smith     Level: intermediate
21561a266240SBarry Smith 
2157e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
21581a266240SBarry Smith 
2159f07f9ceaSJed Brown @*/
21601a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
21611a266240SBarry Smith {
21621a266240SBarry Smith   PetscFunctionBegin;
2163171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21641a266240SBarry Smith   dm->ctxdestroy = destroy;
21651a266240SBarry Smith   PetscFunctionReturn(0);
21661a266240SBarry Smith }
21671a266240SBarry Smith 
21681a266240SBarry Smith #undef __FUNCT__
21691b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2170b07ff414SBarry Smith /*@
21711b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
217247c6ae99SBarry Smith 
217347c6ae99SBarry Smith     Not Collective
217447c6ae99SBarry Smith 
217547c6ae99SBarry Smith     Input Parameters:
217647c6ae99SBarry Smith +   dm - the DM object
217747c6ae99SBarry Smith -   ctx - the user context
217847c6ae99SBarry Smith 
217947c6ae99SBarry Smith     Level: intermediate
218047c6ae99SBarry Smith 
2181e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
218247c6ae99SBarry Smith 
218347c6ae99SBarry Smith @*/
21841b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
218547c6ae99SBarry Smith {
218647c6ae99SBarry Smith   PetscFunctionBegin;
2187171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
218847c6ae99SBarry Smith   dm->ctx = ctx;
218947c6ae99SBarry Smith   PetscFunctionReturn(0);
219047c6ae99SBarry Smith }
219147c6ae99SBarry Smith 
219247c6ae99SBarry Smith #undef __FUNCT__
21931b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
219447c6ae99SBarry Smith /*@
21951b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
219647c6ae99SBarry Smith 
219747c6ae99SBarry Smith     Not Collective
219847c6ae99SBarry Smith 
219947c6ae99SBarry Smith     Input Parameter:
220047c6ae99SBarry Smith .   dm - the DM object
220147c6ae99SBarry Smith 
220247c6ae99SBarry Smith     Output Parameter:
220347c6ae99SBarry Smith .   ctx - the user context
220447c6ae99SBarry Smith 
220547c6ae99SBarry Smith     Level: intermediate
220647c6ae99SBarry Smith 
2207e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
220847c6ae99SBarry Smith 
220947c6ae99SBarry Smith @*/
22101b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
221147c6ae99SBarry Smith {
221247c6ae99SBarry Smith   PetscFunctionBegin;
2213171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22141b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
221547c6ae99SBarry Smith   PetscFunctionReturn(0);
221647c6ae99SBarry Smith }
221747c6ae99SBarry Smith 
221847c6ae99SBarry Smith #undef __FUNCT__
221908da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
222008da532bSDmitry Karpeev /*@C
222108da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
222208da532bSDmitry Karpeev 
222308da532bSDmitry Karpeev     Logically Collective on DM
222408da532bSDmitry Karpeev 
222508da532bSDmitry Karpeev     Input Parameter:
222608da532bSDmitry Karpeev +   dm - the DM object
22270298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
222808da532bSDmitry Karpeev 
222908da532bSDmitry Karpeev     Level: intermediate
223008da532bSDmitry Karpeev 
2231835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
223208da532bSDmitry Karpeev          DMSetJacobian()
223308da532bSDmitry Karpeev 
223408da532bSDmitry Karpeev @*/
223508da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
223608da532bSDmitry Karpeev {
223708da532bSDmitry Karpeev   PetscFunctionBegin;
223808da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
223908da532bSDmitry Karpeev   PetscFunctionReturn(0);
224008da532bSDmitry Karpeev }
224108da532bSDmitry Karpeev 
224208da532bSDmitry Karpeev #undef __FUNCT__
224308da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
224408da532bSDmitry Karpeev /*@
224508da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
224608da532bSDmitry Karpeev 
224708da532bSDmitry Karpeev     Not Collective
224808da532bSDmitry Karpeev 
224908da532bSDmitry Karpeev     Input Parameter:
225008da532bSDmitry Karpeev .   dm - the DM object to destroy
225108da532bSDmitry Karpeev 
225208da532bSDmitry Karpeev     Output Parameter:
225308da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
225408da532bSDmitry Karpeev 
225508da532bSDmitry Karpeev     Level: developer
225608da532bSDmitry Karpeev 
225774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
225808da532bSDmitry Karpeev 
225908da532bSDmitry Karpeev @*/
226008da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
226108da532bSDmitry Karpeev {
226208da532bSDmitry Karpeev   PetscFunctionBegin;
226308da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
226408da532bSDmitry Karpeev   PetscFunctionReturn(0);
226508da532bSDmitry Karpeev }
226608da532bSDmitry Karpeev 
226708da532bSDmitry Karpeev #undef __FUNCT__
226808da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
226908da532bSDmitry Karpeev /*@C
227008da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
227108da532bSDmitry Karpeev 
227208da532bSDmitry Karpeev     Logically Collective on DM
227308da532bSDmitry Karpeev 
227408da532bSDmitry Karpeev     Input Parameters:
227508da532bSDmitry Karpeev +   dm - the DM object to destroy
227608da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
227708da532bSDmitry Karpeev 
227808da532bSDmitry Karpeev     Output parameters:
227908da532bSDmitry Karpeev +   xl - lower bound
228008da532bSDmitry Karpeev -   xu - upper bound
228108da532bSDmitry Karpeev 
228208da532bSDmitry Karpeev     Level: intermediate
228308da532bSDmitry Karpeev 
228474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
228508da532bSDmitry Karpeev 
228608da532bSDmitry Karpeev @*/
228708da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
228808da532bSDmitry Karpeev {
228908da532bSDmitry Karpeev   PetscErrorCode ierr;
22905fd66863SKarl Rupp 
229108da532bSDmitry Karpeev   PetscFunctionBegin;
229208da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
229308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
229408da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
229508da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
22968865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
229708da532bSDmitry Karpeev   PetscFunctionReturn(0);
229808da532bSDmitry Karpeev }
229908da532bSDmitry Karpeev 
230008da532bSDmitry Karpeev #undef __FUNCT__
2301b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2302b0ae01b7SPeter Brune /*@
2303b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2304b0ae01b7SPeter Brune 
2305b0ae01b7SPeter Brune     Not Collective
2306b0ae01b7SPeter Brune 
2307b0ae01b7SPeter Brune     Input Parameter:
2308b0ae01b7SPeter Brune .   dm - the DM object
2309b0ae01b7SPeter Brune 
2310b0ae01b7SPeter Brune     Output Parameter:
2311b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2312b0ae01b7SPeter Brune 
2313b0ae01b7SPeter Brune     Level: developer
2314b0ae01b7SPeter Brune 
2315b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2316b0ae01b7SPeter Brune 
2317b0ae01b7SPeter Brune @*/
2318b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2319b0ae01b7SPeter Brune {
2320b0ae01b7SPeter Brune   PetscFunctionBegin;
2321b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2322b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2323b0ae01b7SPeter Brune }
2324b0ae01b7SPeter Brune 
2325b0ae01b7SPeter Brune #undef  __FUNCT__
232608da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2327748fac09SDmitry Karpeev /*@C
232808da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
232908da532bSDmitry Karpeev 
233008da532bSDmitry Karpeev     Collective on DM
233108da532bSDmitry Karpeev 
233208da532bSDmitry Karpeev     Input Parameter:
233308da532bSDmitry Karpeev +   dm - the DM object
23340298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
233508da532bSDmitry Karpeev 
233608da532bSDmitry Karpeev     Level: developer
233708da532bSDmitry Karpeev 
233874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
233908da532bSDmitry Karpeev 
234008da532bSDmitry Karpeev @*/
234108da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
234208da532bSDmitry Karpeev {
234308da532bSDmitry Karpeev   PetscErrorCode ierr;
23445fd66863SKarl Rupp 
234508da532bSDmitry Karpeev   PetscFunctionBegin;
234608da532bSDmitry Karpeev   if (x) {
234708da532bSDmitry Karpeev     if (!dm->x) {
234808da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
234908da532bSDmitry Karpeev     }
235008da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
23518865f1eaSKarl Rupp   } else if (dm->x) {
235208da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
235308da532bSDmitry Karpeev   }
235408da532bSDmitry Karpeev   PetscFunctionReturn(0);
235508da532bSDmitry Karpeev }
235608da532bSDmitry Karpeev 
23570298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2358264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2359264ace61SBarry Smith 
2360264ace61SBarry Smith #undef __FUNCT__
2361264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2362264ace61SBarry Smith /*@C
2363264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2364264ace61SBarry Smith 
2365264ace61SBarry Smith   Collective on DM
2366264ace61SBarry Smith 
2367264ace61SBarry Smith   Input Parameters:
2368264ace61SBarry Smith + dm     - The DM object
2369264ace61SBarry Smith - method - The name of the DM type
2370264ace61SBarry Smith 
2371264ace61SBarry Smith   Options Database Key:
2372264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2373264ace61SBarry Smith 
2374264ace61SBarry Smith   Notes:
2375e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2376264ace61SBarry Smith 
2377264ace61SBarry Smith   Level: intermediate
2378264ace61SBarry Smith 
2379264ace61SBarry Smith .keywords: DM, set, type
2380264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2381264ace61SBarry Smith @*/
238219fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2383264ace61SBarry Smith {
2384264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2385264ace61SBarry Smith   PetscBool      match;
2386264ace61SBarry Smith   PetscErrorCode ierr;
2387264ace61SBarry Smith 
2388264ace61SBarry Smith   PetscFunctionBegin;
2389264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2390251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2391264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2392264ace61SBarry Smith 
2393607a6623SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll();CHKERRQ(ierr);}
23941c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2395ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2396264ace61SBarry Smith 
2397264ace61SBarry Smith   if (dm->ops->destroy) {
2398264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
23990298fd71SBarry Smith     dm->ops->destroy = NULL;
2400264ace61SBarry Smith   }
2401264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2402264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2403264ace61SBarry Smith   PetscFunctionReturn(0);
2404264ace61SBarry Smith }
2405264ace61SBarry Smith 
2406264ace61SBarry Smith #undef __FUNCT__
2407264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2408264ace61SBarry Smith /*@C
2409264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2410264ace61SBarry Smith 
2411264ace61SBarry Smith   Not Collective
2412264ace61SBarry Smith 
2413264ace61SBarry Smith   Input Parameter:
2414264ace61SBarry Smith . dm  - The DM
2415264ace61SBarry Smith 
2416264ace61SBarry Smith   Output Parameter:
2417264ace61SBarry Smith . type - The DM type name
2418264ace61SBarry Smith 
2419264ace61SBarry Smith   Level: intermediate
2420264ace61SBarry Smith 
2421264ace61SBarry Smith .keywords: DM, get, type, name
2422264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2423264ace61SBarry Smith @*/
242419fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2425264ace61SBarry Smith {
2426264ace61SBarry Smith   PetscErrorCode ierr;
2427264ace61SBarry Smith 
2428264ace61SBarry Smith   PetscFunctionBegin;
2429264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2430264ace61SBarry Smith   PetscValidCharPointer(type,2);
2431264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2432607a6623SBarry Smith     ierr = DMRegisterAll();CHKERRQ(ierr);
2433264ace61SBarry Smith   }
2434264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2435264ace61SBarry Smith   PetscFunctionReturn(0);
2436264ace61SBarry Smith }
2437264ace61SBarry Smith 
243867a56275SMatthew G Knepley #undef __FUNCT__
243967a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
244067a56275SMatthew G Knepley /*@C
244167a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
244267a56275SMatthew G Knepley 
244367a56275SMatthew G Knepley   Collective on DM
244467a56275SMatthew G Knepley 
244567a56275SMatthew G Knepley   Input Parameters:
244667a56275SMatthew G Knepley + dm - the DM
244767a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
244867a56275SMatthew G Knepley 
244967a56275SMatthew G Knepley   Output Parameter:
245067a56275SMatthew G Knepley . M - pointer to new DM
245167a56275SMatthew G Knepley 
245267a56275SMatthew G Knepley   Notes:
245367a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
245467a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
245567a56275SMatthew G Knepley   of the input DM.
245667a56275SMatthew G Knepley 
245767a56275SMatthew G Knepley   Level: intermediate
245867a56275SMatthew G Knepley 
245967a56275SMatthew G Knepley .seealso: DMCreate()
246067a56275SMatthew G Knepley @*/
246119fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
246267a56275SMatthew G Knepley {
246367a56275SMatthew G Knepley   DM             B;
246467a56275SMatthew G Knepley   char           convname[256];
246567a56275SMatthew G Knepley   PetscBool      sametype, issame;
246667a56275SMatthew G Knepley   PetscErrorCode ierr;
246767a56275SMatthew G Knepley 
246867a56275SMatthew G Knepley   PetscFunctionBegin;
246967a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
247067a56275SMatthew G Knepley   PetscValidType(dm,1);
247167a56275SMatthew G Knepley   PetscValidPointer(M,3);
2472251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
247367a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
247467a56275SMatthew G Knepley   {
24750298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
247667a56275SMatthew G Knepley 
247767a56275SMatthew G Knepley     /*
247867a56275SMatthew G Knepley        Order of precedence:
247967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
248067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
248167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
248267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
248367a56275SMatthew G Knepley        5) Use a really basic converter.
248467a56275SMatthew G Knepley     */
248567a56275SMatthew G Knepley 
248667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
248767a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
248867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
248967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
249067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
249167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
24920005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
249367a56275SMatthew G Knepley     if (conv) goto foundconv;
249467a56275SMatthew G Knepley 
249567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
249682f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
249767a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
249867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
249967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
250067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
250167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
250267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
25030005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
250467a56275SMatthew G Knepley     if (conv) {
2505fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
250667a56275SMatthew G Knepley       goto foundconv;
250767a56275SMatthew G Knepley     }
250867a56275SMatthew G Knepley 
250967a56275SMatthew G Knepley #if 0
251067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
251167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2512fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
251367a56275SMatthew G Knepley     if (conv) goto foundconv;
251467a56275SMatthew G Knepley 
251567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
251667a56275SMatthew G Knepley     if (dm->ops->convert) {
251767a56275SMatthew G Knepley       conv = dm->ops->convert;
251867a56275SMatthew G Knepley     }
251967a56275SMatthew G Knepley     if (conv) goto foundconv;
252067a56275SMatthew G Knepley #endif
252167a56275SMatthew G Knepley 
252267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
252382f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
252467a56275SMatthew G Knepley 
252567a56275SMatthew G Knepley foundconv:
252667a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
252767a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
252867a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
252967a56275SMatthew G Knepley   }
253067a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
253167a56275SMatthew G Knepley   PetscFunctionReturn(0);
253267a56275SMatthew G Knepley }
2533264ace61SBarry Smith 
2534264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2535264ace61SBarry Smith 
2536264ace61SBarry Smith #undef __FUNCT__
2537264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2538264ace61SBarry Smith /*@C
25391c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
25401c84c290SBarry Smith 
25411c84c290SBarry Smith   Not Collective
25421c84c290SBarry Smith 
25431c84c290SBarry Smith   Input Parameters:
25441c84c290SBarry Smith + name        - The name of a new user-defined creation routine
25451c84c290SBarry Smith - create_func - The creation routine itself
25461c84c290SBarry Smith 
25471c84c290SBarry Smith   Notes:
25481c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
25491c84c290SBarry Smith 
25501c84c290SBarry Smith 
25511c84c290SBarry Smith   Sample usage:
25521c84c290SBarry Smith .vb
2553bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
25541c84c290SBarry Smith .ve
25551c84c290SBarry Smith 
25561c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
25571c84c290SBarry Smith .vb
25581c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
25591c84c290SBarry Smith     DMSetType(DM,"my_da");
25601c84c290SBarry Smith .ve
25611c84c290SBarry Smith    or at runtime via the option
25621c84c290SBarry Smith .vb
25631c84c290SBarry Smith     -da_type my_da
25641c84c290SBarry Smith .ve
2565264ace61SBarry Smith 
2566264ace61SBarry Smith   Level: advanced
25671c84c290SBarry Smith 
25681c84c290SBarry Smith .keywords: DM, register
2569bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
25701c84c290SBarry Smith 
2571264ace61SBarry Smith @*/
2572bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2573264ace61SBarry Smith {
2574264ace61SBarry Smith   PetscErrorCode ierr;
2575264ace61SBarry Smith 
2576264ace61SBarry Smith   PetscFunctionBegin;
2577a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2578264ace61SBarry Smith   PetscFunctionReturn(0);
2579264ace61SBarry Smith }
2580264ace61SBarry Smith 
2581b859378eSBarry Smith #undef __FUNCT__
2582b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2583b859378eSBarry Smith /*@C
258455849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2585b859378eSBarry Smith 
2586b859378eSBarry Smith   Collective on PetscViewer
2587b859378eSBarry Smith 
2588b859378eSBarry Smith   Input Parameters:
2589b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2590b859378eSBarry Smith            some related function before a call to DMLoad().
2591b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2592b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2593b859378eSBarry Smith 
2594b859378eSBarry Smith    Level: intermediate
2595b859378eSBarry Smith 
2596b859378eSBarry Smith   Notes:
259755849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2598b859378eSBarry Smith 
2599b859378eSBarry Smith   Notes for advanced users:
2600b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2601b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2602b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2603b859378eSBarry Smith   format is
2604b859378eSBarry Smith .vb
2605b859378eSBarry Smith      has not yet been determined
2606b859378eSBarry Smith .ve
2607b859378eSBarry Smith 
2608b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2609b859378eSBarry Smith @*/
2610b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2611b859378eSBarry Smith {
2612b859378eSBarry Smith   PetscErrorCode ierr;
261332c0f0efSBarry Smith   PetscBool      isbinary;
261455849f57SBarry Smith   PetscInt       classid;
261532c0f0efSBarry Smith   char           type[256];
2616b859378eSBarry Smith 
2617b859378eSBarry Smith   PetscFunctionBegin;
2618b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2619b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
262032c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
262132c0f0efSBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
2622b859378eSBarry Smith 
262332c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
2624ce94432eSBarry Smith   if (classid != DM_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file");
262532c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
262632c0f0efSBarry Smith   ierr = DMSetType(newdm, type);CHKERRQ(ierr);
26272d53ad75SBarry Smith   if (newdm->ops->load) {
2628b859378eSBarry Smith     ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
26292d53ad75SBarry Smith   }
2630b859378eSBarry Smith   PetscFunctionReturn(0);
2631b859378eSBarry Smith }
2632b859378eSBarry Smith 
26337da65231SMatthew G Knepley /******************************** FEM Support **********************************/
26347da65231SMatthew G Knepley 
26357da65231SMatthew G Knepley #undef __FUNCT__
26367da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
2637a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
2638a6dfd86eSKarl Rupp {
26391d47ebbbSSatish Balay   PetscInt       f;
26401b30c384SMatthew G Knepley   PetscErrorCode ierr;
26411b30c384SMatthew G Knepley 
26427da65231SMatthew G Knepley   PetscFunctionBegin;
264374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26441d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
264574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
26467da65231SMatthew G Knepley   }
26477da65231SMatthew G Knepley   PetscFunctionReturn(0);
26487da65231SMatthew G Knepley }
26497da65231SMatthew G Knepley 
26507da65231SMatthew G Knepley #undef __FUNCT__
26517da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
2652a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
2653a6dfd86eSKarl Rupp {
26541b30c384SMatthew G Knepley   PetscInt       f, g;
26557da65231SMatthew G Knepley   PetscErrorCode ierr;
26567da65231SMatthew G Knepley 
26577da65231SMatthew G Knepley   PetscFunctionBegin;
265874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26591d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
266074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
26611d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
266274778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
26637da65231SMatthew G Knepley     }
266474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
26657da65231SMatthew G Knepley   }
26667da65231SMatthew G Knepley   PetscFunctionReturn(0);
26677da65231SMatthew G Knepley }
2668e7c4fc90SDmitry Karpeev 
2669970e74d5SMatthew G Knepley #undef __FUNCT__
267088ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
267188ed4aceSMatthew G Knepley /*@
267288ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
267388ed4aceSMatthew G Knepley 
267488ed4aceSMatthew G Knepley   Input Parameter:
267588ed4aceSMatthew G Knepley . dm - The DM
267688ed4aceSMatthew G Knepley 
267788ed4aceSMatthew G Knepley   Output Parameter:
267888ed4aceSMatthew G Knepley . section - The PetscSection
267988ed4aceSMatthew G Knepley 
268088ed4aceSMatthew G Knepley   Level: intermediate
268188ed4aceSMatthew G Knepley 
268288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
268388ed4aceSMatthew G Knepley 
268488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
268588ed4aceSMatthew G Knepley @*/
26860adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
26870adebc6cSBarry Smith {
268888ed4aceSMatthew G Knepley   PetscFunctionBegin;
268988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
269088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
269188ed4aceSMatthew G Knepley   *section = dm->defaultSection;
269288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
269388ed4aceSMatthew G Knepley }
269488ed4aceSMatthew G Knepley 
269588ed4aceSMatthew G Knepley #undef __FUNCT__
269688ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
269788ed4aceSMatthew G Knepley /*@
269888ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
269988ed4aceSMatthew G Knepley 
270088ed4aceSMatthew G Knepley   Input Parameters:
270188ed4aceSMatthew G Knepley + dm - The DM
270288ed4aceSMatthew G Knepley - section - The PetscSection
270388ed4aceSMatthew G Knepley 
270488ed4aceSMatthew G Knepley   Level: intermediate
270588ed4aceSMatthew G Knepley 
270688ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
270788ed4aceSMatthew G Knepley 
270888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
270988ed4aceSMatthew G Knepley @*/
27100adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
27110adebc6cSBarry Smith {
2712af122d2aSMatthew G Knepley   PetscInt       numFields;
2713af122d2aSMatthew G Knepley   PetscInt       f;
271488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
271588ed4aceSMatthew G Knepley 
271688ed4aceSMatthew G Knepley   PetscFunctionBegin;
271788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27181d799100SJed Brown   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
27191d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
272088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
272188ed4aceSMatthew G Knepley   dm->defaultSection = section;
2722af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
2723af122d2aSMatthew G Knepley   if (numFields) {
2724af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
2725af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
2726af122d2aSMatthew G Knepley       const char *name;
2727af122d2aSMatthew G Knepley 
2728af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
2729af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
2730af122d2aSMatthew G Knepley     }
2731af122d2aSMatthew G Knepley   }
27321d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
27331d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
273488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
273588ed4aceSMatthew G Knepley }
273688ed4aceSMatthew G Knepley 
273788ed4aceSMatthew G Knepley #undef __FUNCT__
273888ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
273988ed4aceSMatthew G Knepley /*@
274088ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
274188ed4aceSMatthew G Knepley 
27428b1ab98fSJed Brown   Collective on DM
27438b1ab98fSJed Brown 
274488ed4aceSMatthew G Knepley   Input Parameter:
274588ed4aceSMatthew G Knepley . dm - The DM
274688ed4aceSMatthew G Knepley 
274788ed4aceSMatthew G Knepley   Output Parameter:
274888ed4aceSMatthew G Knepley . section - The PetscSection
274988ed4aceSMatthew G Knepley 
275088ed4aceSMatthew G Knepley   Level: intermediate
275188ed4aceSMatthew G Knepley 
275288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
275388ed4aceSMatthew G Knepley 
275488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
275588ed4aceSMatthew G Knepley @*/
27560adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
27570adebc6cSBarry Smith {
275888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
275988ed4aceSMatthew G Knepley 
276088ed4aceSMatthew G Knepley   PetscFunctionBegin;
276188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
276288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
276388ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
276482f516ccSBarry 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");
2765b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
2766ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm),dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr);
276788ed4aceSMatthew G Knepley   }
276888ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
276988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
277088ed4aceSMatthew G Knepley }
277188ed4aceSMatthew G Knepley 
277288ed4aceSMatthew G Knepley #undef __FUNCT__
2773b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
2774b21d0597SMatthew G Knepley /*@
2775b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
2776b21d0597SMatthew G Knepley 
2777b21d0597SMatthew G Knepley   Input Parameters:
2778b21d0597SMatthew G Knepley + dm - The DM
27795080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
2780b21d0597SMatthew G Knepley 
2781b21d0597SMatthew G Knepley   Level: intermediate
2782b21d0597SMatthew G Knepley 
2783b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
2784b21d0597SMatthew G Knepley 
2785b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
2786b21d0597SMatthew G Knepley @*/
27870adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
27880adebc6cSBarry Smith {
2789b21d0597SMatthew G Knepley   PetscErrorCode ierr;
2790b21d0597SMatthew G Knepley 
2791b21d0597SMatthew G Knepley   PetscFunctionBegin;
2792b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27935080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
27941d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
2795b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
2796b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
2797b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2798b21d0597SMatthew G Knepley }
2799b21d0597SMatthew G Knepley 
2800b21d0597SMatthew G Knepley #undef __FUNCT__
280188ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
280288ed4aceSMatthew G Knepley /*@
280388ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
280488ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
280588ed4aceSMatthew G Knepley 
280688ed4aceSMatthew G Knepley   Input Parameter:
280788ed4aceSMatthew G Knepley . dm - The DM
280888ed4aceSMatthew G Knepley 
280988ed4aceSMatthew G Knepley   Output Parameter:
281088ed4aceSMatthew G Knepley . sf - The PetscSF
281188ed4aceSMatthew G Knepley 
281288ed4aceSMatthew G Knepley   Level: intermediate
281388ed4aceSMatthew G Knepley 
281488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
281588ed4aceSMatthew G Knepley 
281688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
281788ed4aceSMatthew G Knepley @*/
28180adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
28190adebc6cSBarry Smith {
282088ed4aceSMatthew G Knepley   PetscInt       nroots;
282188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
282288ed4aceSMatthew G Knepley 
282388ed4aceSMatthew G Knepley   PetscFunctionBegin;
282488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
282588ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
28260298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
282788ed4aceSMatthew G Knepley   if (nroots < 0) {
282888ed4aceSMatthew G Knepley     PetscSection section, gSection;
282988ed4aceSMatthew G Knepley 
283088ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
283131ea6d37SMatthew G Knepley     if (section) {
283288ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
283388ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
283431ea6d37SMatthew G Knepley     } else {
28350298fd71SBarry Smith       *sf = NULL;
283631ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
283731ea6d37SMatthew G Knepley     }
283888ed4aceSMatthew G Knepley   }
283988ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
284088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
284188ed4aceSMatthew G Knepley }
284288ed4aceSMatthew G Knepley 
284388ed4aceSMatthew G Knepley #undef __FUNCT__
284488ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
284588ed4aceSMatthew G Knepley /*@
284688ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
284788ed4aceSMatthew G Knepley 
284888ed4aceSMatthew G Knepley   Input Parameters:
284988ed4aceSMatthew G Knepley + dm - The DM
285088ed4aceSMatthew G Knepley - sf - The PetscSF
285188ed4aceSMatthew G Knepley 
285288ed4aceSMatthew G Knepley   Level: intermediate
285388ed4aceSMatthew G Knepley 
285488ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
285588ed4aceSMatthew G Knepley 
285688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
285788ed4aceSMatthew G Knepley @*/
28580adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
28590adebc6cSBarry Smith {
286088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
286188ed4aceSMatthew G Knepley 
286288ed4aceSMatthew G Knepley   PetscFunctionBegin;
286388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
286488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
286588ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
286688ed4aceSMatthew G Knepley   dm->defaultSF = sf;
286788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
286888ed4aceSMatthew G Knepley }
286988ed4aceSMatthew G Knepley 
287088ed4aceSMatthew G Knepley #undef __FUNCT__
287188ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
287288ed4aceSMatthew G Knepley /*@C
287388ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
287488ed4aceSMatthew G Knepley   describing the data layout.
287588ed4aceSMatthew G Knepley 
287688ed4aceSMatthew G Knepley   Input Parameters:
287788ed4aceSMatthew G Knepley + dm - The DM
287888ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
287988ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
288088ed4aceSMatthew G Knepley 
288188ed4aceSMatthew G Knepley   Level: intermediate
288288ed4aceSMatthew G Knepley 
288388ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
288488ed4aceSMatthew G Knepley @*/
288588ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
288688ed4aceSMatthew G Knepley {
288782f516ccSBarry Smith   MPI_Comm       comm;
288888ed4aceSMatthew G Knepley   PetscLayout    layout;
288988ed4aceSMatthew G Knepley   const PetscInt *ranges;
289088ed4aceSMatthew G Knepley   PetscInt       *local;
289188ed4aceSMatthew G Knepley   PetscSFNode    *remote;
289288ed4aceSMatthew G Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves, l;
289388ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
289488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
289588ed4aceSMatthew G Knepley 
289688ed4aceSMatthew G Knepley   PetscFunctionBegin;
289782f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
289888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
289988ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
290088ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
290188ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
290288ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
290388ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
290488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
290588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
290688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
290788ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
290888ed4aceSMatthew G Knepley   for (p = pStart, nleaves = 0; p < pEnd; ++p) {
29096636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
291088ed4aceSMatthew G Knepley 
29116636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
29126636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
29136636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
291488ed4aceSMatthew G Knepley   }
291588ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
291688ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
291788ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
29181f588964SMatthew G Knepley     const PetscInt *cind;
29196636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
292088ed4aceSMatthew G Knepley 
292188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
292288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
292388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
292488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
292588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
29266636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
292788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
29286636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
29296636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
29306636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
2931057b4bcdSMatthew 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);
29326636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
29336636e97aSMatthew G Knepley     }
293488ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
293588ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
293688ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
293788ed4aceSMatthew G Knepley     }
293888ed4aceSMatthew G Knepley     if (gdof < 0) {
29396636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
294088ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
294188ed4aceSMatthew G Knepley 
294231d3f06eSJed Brown         ierr = PetscFindInt(offset,size,ranges,&r);CHKERRQ(ierr);
294331d3f06eSJed Brown         if (r < 0) r = -(r+2);
294488ed4aceSMatthew G Knepley         remote[l].rank  = r;
294588ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
294688ed4aceSMatthew G Knepley       }
294788ed4aceSMatthew G Knepley     } else {
29486636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
294988ed4aceSMatthew G Knepley         remote[l].rank  = rank;
295088ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
295188ed4aceSMatthew G Knepley       }
295288ed4aceSMatthew G Knepley     }
295388ed4aceSMatthew G Knepley   }
29546636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
295588ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
295688ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
295788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
295888ed4aceSMatthew G Knepley }
2959af122d2aSMatthew G Knepley 
2960af122d2aSMatthew G Knepley #undef __FUNCT__
2961b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
2962b21d0597SMatthew G Knepley /*@
2963b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
2964b21d0597SMatthew G Knepley 
2965b21d0597SMatthew G Knepley   Input Parameter:
2966b21d0597SMatthew G Knepley . dm - The DM
2967b21d0597SMatthew G Knepley 
2968b21d0597SMatthew G Knepley   Output Parameter:
2969b21d0597SMatthew G Knepley . sf - The PetscSF
2970b21d0597SMatthew G Knepley 
2971b21d0597SMatthew G Knepley   Level: intermediate
2972b21d0597SMatthew G Knepley 
2973b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
2974b21d0597SMatthew G Knepley 
2975057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
2976b21d0597SMatthew G Knepley @*/
29770adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
29780adebc6cSBarry Smith {
2979b21d0597SMatthew G Knepley   PetscFunctionBegin;
2980b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2981b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
2982b21d0597SMatthew G Knepley   *sf = dm->sf;
2983b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2984b21d0597SMatthew G Knepley }
2985b21d0597SMatthew G Knepley 
2986b21d0597SMatthew G Knepley #undef __FUNCT__
2987057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
2988057b4bcdSMatthew G Knepley /*@
2989057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
2990057b4bcdSMatthew G Knepley 
2991057b4bcdSMatthew G Knepley   Input Parameters:
2992057b4bcdSMatthew G Knepley + dm - The DM
2993057b4bcdSMatthew G Knepley - sf - The PetscSF
2994057b4bcdSMatthew G Knepley 
2995057b4bcdSMatthew G Knepley   Level: intermediate
2996057b4bcdSMatthew G Knepley 
2997057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
2998057b4bcdSMatthew G Knepley @*/
29990adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
30000adebc6cSBarry Smith {
3001057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3002057b4bcdSMatthew G Knepley 
3003057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3004057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3005057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3006057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3007057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3008057b4bcdSMatthew G Knepley   dm->sf = sf;
3009057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3010057b4bcdSMatthew G Knepley }
3011057b4bcdSMatthew G Knepley 
3012057b4bcdSMatthew G Knepley #undef __FUNCT__
3013af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3014af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3015af122d2aSMatthew G Knepley {
3016af122d2aSMatthew G Knepley   PetscFunctionBegin;
3017af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3018af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3019af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3020af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3021af122d2aSMatthew G Knepley }
3022af122d2aSMatthew G Knepley 
3023af122d2aSMatthew G Knepley #undef __FUNCT__
3024af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3025af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3026af122d2aSMatthew G Knepley {
3027af122d2aSMatthew G Knepley   PetscInt       f;
3028af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3029af122d2aSMatthew G Knepley 
3030af122d2aSMatthew G Knepley   PetscFunctionBegin;
3031af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3032af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3033af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3034af122d2aSMatthew G Knepley   }
3035af122d2aSMatthew G Knepley   ierr          = PetscFree(dm->fields);CHKERRQ(ierr);
3036af122d2aSMatthew G Knepley   dm->numFields = numFields;
3037af122d2aSMatthew G Knepley   ierr          = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3038af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
303982f516ccSBarry Smith     ierr = PetscContainerCreate(PetscObjectComm((PetscObject)dm), (PetscContainer*) &dm->fields[f]);CHKERRQ(ierr);
3040af122d2aSMatthew G Knepley   }
3041af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3042af122d2aSMatthew G Knepley }
3043af122d2aSMatthew G Knepley 
3044af122d2aSMatthew G Knepley #undef __FUNCT__
3045af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3046af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3047af122d2aSMatthew G Knepley {
3048af122d2aSMatthew G Knepley   PetscFunctionBegin;
3049af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3050af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
305182f516ccSBarry Smith   if (!dm->fields) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
305282f516ccSBarry 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);
3053af122d2aSMatthew G Knepley   *field = dm->fields[f];
3054af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3055af122d2aSMatthew G Knepley }
30566636e97aSMatthew G Knepley 
30576636e97aSMatthew G Knepley #undef __FUNCT__
30586636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
30596636e97aSMatthew G Knepley /*@
30606636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
30616636e97aSMatthew G Knepley 
30626636e97aSMatthew G Knepley   Collective on DM
30636636e97aSMatthew G Knepley 
30646636e97aSMatthew G Knepley   Input Parameters:
30656636e97aSMatthew G Knepley + dm - the DM
30666636e97aSMatthew G Knepley - c - coordinate vector
30676636e97aSMatthew G Knepley 
30686636e97aSMatthew G Knepley   Note:
30696636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
30706636e97aSMatthew G Knepley 
30716636e97aSMatthew G Knepley   Level: intermediate
30726636e97aSMatthew G Knepley 
30736636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
30746636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
30756636e97aSMatthew G Knepley @*/
30766636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
30776636e97aSMatthew G Knepley {
30786636e97aSMatthew G Knepley   PetscErrorCode ierr;
30796636e97aSMatthew G Knepley 
30806636e97aSMatthew G Knepley   PetscFunctionBegin;
30816636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30826636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
30836636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
30846636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
30856636e97aSMatthew G Knepley   dm->coordinates = c;
30866636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
30876636e97aSMatthew G Knepley   PetscFunctionReturn(0);
30886636e97aSMatthew G Knepley }
30896636e97aSMatthew G Knepley 
30906636e97aSMatthew G Knepley #undef __FUNCT__
30916636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
30926636e97aSMatthew G Knepley /*@
30936636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
30946636e97aSMatthew G Knepley 
30956636e97aSMatthew G Knepley   Collective on DM
30966636e97aSMatthew G Knepley 
30976636e97aSMatthew G Knepley    Input Parameters:
30986636e97aSMatthew G Knepley +  dm - the DM
30996636e97aSMatthew G Knepley -  c - coordinate vector
31006636e97aSMatthew G Knepley 
31016636e97aSMatthew G Knepley   Note:
31026636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
31036636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
31046636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
31056636e97aSMatthew G Knepley 
31066636e97aSMatthew G Knepley   Level: intermediate
31076636e97aSMatthew G Knepley 
31086636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31096636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
31106636e97aSMatthew G Knepley @*/
31116636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
31126636e97aSMatthew G Knepley {
31136636e97aSMatthew G Knepley   PetscErrorCode ierr;
31146636e97aSMatthew G Knepley 
31156636e97aSMatthew G Knepley   PetscFunctionBegin;
31166636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31176636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
31186636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
31196636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
31208865f1eaSKarl Rupp 
31216636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
31228865f1eaSKarl Rupp 
31236636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
31246636e97aSMatthew G Knepley   PetscFunctionReturn(0);
31256636e97aSMatthew G Knepley }
31266636e97aSMatthew G Knepley 
31276636e97aSMatthew G Knepley #undef __FUNCT__
31286636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
31296636e97aSMatthew G Knepley /*@
31306636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
31316636e97aSMatthew G Knepley 
31326636e97aSMatthew G Knepley   Not Collective
31336636e97aSMatthew G Knepley 
31346636e97aSMatthew G Knepley   Input Parameter:
31356636e97aSMatthew G Knepley . dm - the DM
31366636e97aSMatthew G Knepley 
31376636e97aSMatthew G Knepley   Output Parameter:
31386636e97aSMatthew G Knepley . c - global coordinate vector
31396636e97aSMatthew G Knepley 
31406636e97aSMatthew G Knepley   Note:
31416636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
31426636e97aSMatthew G Knepley 
31436636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
31446636e97aSMatthew G Knepley 
31456636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
31466636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
31476636e97aSMatthew G Knepley 
31486636e97aSMatthew G Knepley   Level: intermediate
31496636e97aSMatthew G Knepley 
31506636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31516636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
31526636e97aSMatthew G Knepley @*/
31536636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
31546636e97aSMatthew G Knepley {
31556636e97aSMatthew G Knepley   PetscErrorCode ierr;
31566636e97aSMatthew G Knepley 
31576636e97aSMatthew G Knepley   PetscFunctionBegin;
31586636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31596636e97aSMatthew G Knepley   PetscValidPointer(c,2);
31601f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
31610298fd71SBarry Smith     DM cdm = NULL;
31626636e97aSMatthew G Knepley 
31636636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
31646636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
31656636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
31666636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
31676636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
31686636e97aSMatthew G Knepley   }
31696636e97aSMatthew G Knepley   *c = dm->coordinates;
31706636e97aSMatthew G Knepley   PetscFunctionReturn(0);
31716636e97aSMatthew G Knepley }
31726636e97aSMatthew G Knepley 
31736636e97aSMatthew G Knepley #undef __FUNCT__
31746636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
31756636e97aSMatthew G Knepley /*@
31766636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
31776636e97aSMatthew G Knepley 
31786636e97aSMatthew G Knepley   Collective on DM
31796636e97aSMatthew G Knepley 
31806636e97aSMatthew G Knepley   Input Parameter:
31816636e97aSMatthew G Knepley . dm - the DM
31826636e97aSMatthew G Knepley 
31836636e97aSMatthew G Knepley   Output Parameter:
31846636e97aSMatthew G Knepley . c - coordinate vector
31856636e97aSMatthew G Knepley 
31866636e97aSMatthew G Knepley   Note:
31876636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
31886636e97aSMatthew G Knepley 
31896636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
31906636e97aSMatthew G Knepley 
31916636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
31926636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
31936636e97aSMatthew G Knepley 
31946636e97aSMatthew G Knepley   Level: intermediate
31956636e97aSMatthew G Knepley 
31966636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31976636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
31986636e97aSMatthew G Knepley @*/
31996636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
32006636e97aSMatthew G Knepley {
32016636e97aSMatthew G Knepley   PetscErrorCode ierr;
32026636e97aSMatthew G Knepley 
32036636e97aSMatthew G Knepley   PetscFunctionBegin;
32046636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32056636e97aSMatthew G Knepley   PetscValidPointer(c,2);
32061f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
32070298fd71SBarry Smith     DM cdm = NULL;
32086636e97aSMatthew G Knepley 
32096636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
32106636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
32116636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
32126636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
32136636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
32146636e97aSMatthew G Knepley   }
32156636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
32166636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32176636e97aSMatthew G Knepley }
32186636e97aSMatthew G Knepley 
32196636e97aSMatthew G Knepley #undef __FUNCT__
32206636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
32216636e97aSMatthew G Knepley /*@
32226636e97aSMatthew G Knepley   DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates
32236636e97aSMatthew G Knepley 
32246636e97aSMatthew G Knepley   Collective on DM
32256636e97aSMatthew G Knepley 
32266636e97aSMatthew G Knepley   Input Parameter:
32276636e97aSMatthew G Knepley . dm - the DM
32286636e97aSMatthew G Knepley 
32296636e97aSMatthew G Knepley   Output Parameter:
32306636e97aSMatthew G Knepley . cdm - coordinate DM
32316636e97aSMatthew G Knepley 
32326636e97aSMatthew G Knepley   Level: intermediate
32336636e97aSMatthew G Knepley 
32346636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
32356636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
32366636e97aSMatthew G Knepley @*/
32376636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
32386636e97aSMatthew G Knepley {
32396636e97aSMatthew G Knepley   PetscErrorCode ierr;
32406636e97aSMatthew G Knepley 
32416636e97aSMatthew G Knepley   PetscFunctionBegin;
32426636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32436636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
32446636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
324582f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
32466636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
32476636e97aSMatthew G Knepley   }
32486636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
32496636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32506636e97aSMatthew G Knepley }
3251e87bb0d3SMatthew G Knepley 
3252e87bb0d3SMatthew G Knepley #undef __FUNCT__
3253e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
3254e87bb0d3SMatthew G Knepley /*@
3255e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
3256e87bb0d3SMatthew G Knepley 
3257e87bb0d3SMatthew G Knepley   Not collective
3258e87bb0d3SMatthew G Knepley 
3259e87bb0d3SMatthew G Knepley   Input Parameters:
3260e87bb0d3SMatthew G Knepley + dm - The DM
3261e87bb0d3SMatthew G Knepley - v - The Vec of points
3262e87bb0d3SMatthew G Knepley 
326361e3bb9bSMatthew G Knepley   Output Parameter:
3264e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
3265e87bb0d3SMatthew G Knepley 
3266e87bb0d3SMatthew G Knepley   Level: developer
326761e3bb9bSMatthew G Knepley 
326861e3bb9bSMatthew G Knepley .keywords: point location, mesh
326961e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
327061e3bb9bSMatthew G Knepley @*/
3271e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
3272e87bb0d3SMatthew G Knepley {
3273735aa83eSMatthew G Knepley   PetscErrorCode ierr;
3274735aa83eSMatthew G Knepley 
3275e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
3276e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3277e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
3278e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
3279735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
3280735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
328182f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
3282e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
3283e87bb0d3SMatthew G Knepley }
3284