xref: /petsc/src/dm/interface/dm.c (revision 8cbdbec6f8317ddf7886f91eb9c6bd083b543c50)
1b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
247c6ae99SBarry Smith 
3732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
467a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal;
567a56275SMatthew G Knepley 
647c6ae99SBarry Smith #undef __FUNCT__
7ca266f36SBarry Smith #define __FUNCT__ "DMViewFromOptions"
8ca266f36SBarry Smith /*
9ca266f36SBarry Smith   Processes command line options to determine if/how a dm
10ca266f36SBarry Smith   is to be viewed.
11ca266f36SBarry Smith 
12ca266f36SBarry Smith */
13ca266f36SBarry Smith PetscErrorCode  DMViewFromOptions(DM dm,const char optionname[])
14ca266f36SBarry Smith {
15ca266f36SBarry Smith   PetscErrorCode    ierr;
16ca266f36SBarry Smith   PetscBool         flg;
17ca266f36SBarry Smith   PetscViewer       viewer;
18cffb1e40SBarry Smith   PetscViewerFormat format;
19ca266f36SBarry Smith 
20ca266f36SBarry Smith   PetscFunctionBegin;
21cffb1e40SBarry Smith   ierr = PetscOptionsGetViewer(((PetscObject)dm)->comm,((PetscObject)dm)->prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr);
22ca266f36SBarry Smith   if (flg) {
23cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
24ca266f36SBarry Smith     ierr = DMView(dm,viewer);CHKERRQ(ierr);
25cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
26cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
27ca266f36SBarry Smith   }
28ca266f36SBarry Smith   PetscFunctionReturn(0);
29ca266f36SBarry Smith }
30ca266f36SBarry Smith 
31ca266f36SBarry Smith 
32ca266f36SBarry Smith #undef __FUNCT__
33a4121054SBarry Smith #define __FUNCT__ "DMCreate"
34a4121054SBarry Smith /*@
35de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
36a4121054SBarry Smith 
37a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
38a4121054SBarry Smith    error when you try to use the vector.
39a4121054SBarry Smith 
40a4121054SBarry Smith   Collective on MPI_Comm
41a4121054SBarry Smith 
42a4121054SBarry Smith   Input Parameter:
43a4121054SBarry Smith . comm - The communicator for the DM object
44a4121054SBarry Smith 
45a4121054SBarry Smith   Output Parameter:
46a4121054SBarry Smith . dm - The DM object
47a4121054SBarry Smith 
48a4121054SBarry Smith   Level: beginner
49a4121054SBarry Smith 
50a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
51a4121054SBarry Smith @*/
527087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
53a4121054SBarry Smith {
54a4121054SBarry Smith   DM             v;
55a4121054SBarry Smith   PetscErrorCode ierr;
56a4121054SBarry Smith 
57a4121054SBarry Smith   PetscFunctionBegin;
581411c6eeSJed Brown   PetscValidPointer(dm,2);
591411c6eeSJed Brown   *dm = PETSC_NULL;
60a4121054SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
61b84caa0eSBarry Smith   ierr = VecInitializePackage(PETSC_NULL);CHKERRQ(ierr);
62b84caa0eSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
63a4121054SBarry Smith   ierr = DMInitializePackage(PETSC_NULL);CHKERRQ(ierr);
64a4121054SBarry Smith #endif
65a4121054SBarry Smith 
663194b578SJed Brown   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
67a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
681411c6eeSJed Brown 
69e7c4fc90SDmitry Karpeev 
701411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
711411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
721411c6eeSJed Brown   v->bs           = 1;
73171400e9SBarry Smith   v->coloringtype = IS_COLORING_GLOBAL;
7488ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
7588ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
7688ed4aceSMatthew G Knepley   v->defaultSection       = PETSC_NULL;
7788ed4aceSMatthew G Knepley   v->defaultGlobalSection = PETSC_NULL;
78435a35e8SMatthew G Knepley   {
79435a35e8SMatthew G Knepley     PetscInt i;
80435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
81435a35e8SMatthew G Knepley       v->nullspaceConstructors[i] = PETSC_NULL;
82435a35e8SMatthew G Knepley     }
83435a35e8SMatthew G Knepley   }
84af122d2aSMatthew G Knepley   v->numFields = 0;
85af122d2aSMatthew G Knepley   v->fields    = PETSC_NULL;
861411c6eeSJed Brown 
871411c6eeSJed Brown   *dm = v;
88a4121054SBarry Smith   PetscFunctionReturn(0);
89a4121054SBarry Smith }
90a4121054SBarry Smith 
91a4121054SBarry Smith #undef __FUNCT__
929a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
939a42bb27SBarry Smith /*@C
94564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
959a42bb27SBarry Smith 
96aa219208SBarry Smith    Logically Collective on DMDA
979a42bb27SBarry Smith 
989a42bb27SBarry Smith    Input Parameter:
999a42bb27SBarry Smith +  da - initial distributed array
1008154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1019a42bb27SBarry Smith 
1029a42bb27SBarry Smith    Options Database:
103dd85299cSBarry Smith .   -dm_vec_type ctype
1049a42bb27SBarry Smith 
1059a42bb27SBarry Smith    Level: intermediate
1069a42bb27SBarry Smith 
107aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
1089a42bb27SBarry Smith @*/
10919fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1109a42bb27SBarry Smith {
1119a42bb27SBarry Smith   PetscErrorCode ierr;
1129a42bb27SBarry Smith 
1139a42bb27SBarry Smith   PetscFunctionBegin;
1149a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1159a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
11619fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1179a42bb27SBarry Smith   PetscFunctionReturn(0);
1189a42bb27SBarry Smith }
1199a42bb27SBarry Smith 
1209a42bb27SBarry Smith #undef __FUNCT__
1215f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1225f1ad066SMatthew G Knepley /*@
12334f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1245f1ad066SMatthew G Knepley 
1255f1ad066SMatthew G Knepley   Not collective
1265f1ad066SMatthew G Knepley 
1275f1ad066SMatthew G Knepley   Input Parameter:
1285f1ad066SMatthew G Knepley . v - The Vec
1295f1ad066SMatthew G Knepley 
1305f1ad066SMatthew G Knepley   Output Parameter:
1315f1ad066SMatthew G Knepley . dm - The DM
1325f1ad066SMatthew G Knepley 
1335f1ad066SMatthew G Knepley   Level: intermediate
1345f1ad066SMatthew G Knepley 
1355f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1365f1ad066SMatthew G Knepley @*/
1375f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
1385f1ad066SMatthew G Knepley {
1395f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1405f1ad066SMatthew G Knepley 
1415f1ad066SMatthew G Knepley   PetscFunctionBegin;
1425f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1435f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
1445f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject *) dm);CHKERRQ(ierr);
1455f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
1465f1ad066SMatthew G Knepley }
1475f1ad066SMatthew G Knepley 
1485f1ad066SMatthew G Knepley #undef __FUNCT__
1495f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
1505f1ad066SMatthew G Knepley /*@
1515f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
1525f1ad066SMatthew G Knepley 
1535f1ad066SMatthew G Knepley   Not collective
1545f1ad066SMatthew G Knepley 
1555f1ad066SMatthew G Knepley   Input Parameters:
1565f1ad066SMatthew G Knepley + v - The Vec
1575f1ad066SMatthew G Knepley - dm - The DM
1585f1ad066SMatthew G Knepley 
1595f1ad066SMatthew G Knepley   Level: intermediate
1605f1ad066SMatthew G Knepley 
1615f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1625f1ad066SMatthew G Knepley @*/
1635f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
1645f1ad066SMatthew G Knepley {
1655f1ad066SMatthew G Knepley   PetscErrorCode ierr;
1665f1ad066SMatthew G Knepley 
1675f1ad066SMatthew G Knepley   PetscFunctionBegin;
1685f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
1695f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
1705f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
1715f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
1725f1ad066SMatthew G Knepley }
1735f1ad066SMatthew G Knepley 
1745f1ad066SMatthew G Knepley #undef __FUNCT__
175521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
176521d9a4cSLisandro Dalcin /*@C
177521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
178521d9a4cSLisandro Dalcin 
179521d9a4cSLisandro Dalcin    Logically Collective on DM
180521d9a4cSLisandro Dalcin 
181521d9a4cSLisandro Dalcin    Input Parameter:
182521d9a4cSLisandro Dalcin +  dm - the DM context
183521d9a4cSLisandro Dalcin .  ctype - the matrix type
184521d9a4cSLisandro Dalcin 
185521d9a4cSLisandro Dalcin    Options Database:
186521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
187521d9a4cSLisandro Dalcin 
188521d9a4cSLisandro Dalcin    Level: intermediate
189521d9a4cSLisandro Dalcin 
190521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType
191521d9a4cSLisandro Dalcin @*/
19219fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
193521d9a4cSLisandro Dalcin {
194521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
195521d9a4cSLisandro Dalcin   PetscFunctionBegin;
196521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
197521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
19819fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
199521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
200521d9a4cSLisandro Dalcin }
201521d9a4cSLisandro Dalcin 
202521d9a4cSLisandro Dalcin #undef __FUNCT__
203c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
204c688c046SMatthew G Knepley /*@
20534f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
206c688c046SMatthew G Knepley 
207c688c046SMatthew G Knepley   Not collective
208c688c046SMatthew G Knepley 
209c688c046SMatthew G Knepley   Input Parameter:
210c688c046SMatthew G Knepley . A - The Mat
211c688c046SMatthew G Knepley 
212c688c046SMatthew G Knepley   Output Parameter:
213c688c046SMatthew G Knepley . dm - The DM
214c688c046SMatthew G Knepley 
215c688c046SMatthew G Knepley   Level: intermediate
216c688c046SMatthew G Knepley 
217c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
218c688c046SMatthew G Knepley @*/
219c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
220c688c046SMatthew G Knepley {
221c688c046SMatthew G Knepley   PetscErrorCode ierr;
222c688c046SMatthew G Knepley 
223c688c046SMatthew G Knepley   PetscFunctionBegin;
224c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
225c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
226c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject *) dm);CHKERRQ(ierr);
227c688c046SMatthew G Knepley   PetscFunctionReturn(0);
228c688c046SMatthew G Knepley }
229c688c046SMatthew G Knepley 
230c688c046SMatthew G Knepley #undef __FUNCT__
231c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
232c688c046SMatthew G Knepley /*@
233c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
234c688c046SMatthew G Knepley 
235c688c046SMatthew G Knepley   Not collective
236c688c046SMatthew G Knepley 
237c688c046SMatthew G Knepley   Input Parameters:
238c688c046SMatthew G Knepley + A - The Mat
239c688c046SMatthew G Knepley - dm - The DM
240c688c046SMatthew G Knepley 
241c688c046SMatthew G Knepley   Level: intermediate
242c688c046SMatthew G Knepley 
243c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
244c688c046SMatthew G Knepley @*/
245c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
246c688c046SMatthew G Knepley {
247c688c046SMatthew G Knepley   PetscErrorCode ierr;
248c688c046SMatthew G Knepley 
249c688c046SMatthew G Knepley   PetscFunctionBegin;
250c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
251a85f731bSMatthew G Knepley   if (dm) {PetscValidHeaderSpecific(dm,DM_CLASSID,2);}
252c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
253c688c046SMatthew G Knepley   PetscFunctionReturn(0);
254c688c046SMatthew G Knepley }
255c688c046SMatthew G Knepley 
256c688c046SMatthew G Knepley #undef __FUNCT__
2579a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
2589a42bb27SBarry Smith /*@C
2599a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
260aa219208SBarry Smith    DMDA options in the database.
2619a42bb27SBarry Smith 
262aa219208SBarry Smith    Logically Collective on DMDA
2639a42bb27SBarry Smith 
2649a42bb27SBarry Smith    Input Parameter:
265aa219208SBarry Smith +  da - the DMDA context
2669a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
2679a42bb27SBarry Smith 
2689a42bb27SBarry Smith    Notes:
2699a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2709a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
2719a42bb27SBarry Smith 
2729a42bb27SBarry Smith    Level: advanced
2739a42bb27SBarry Smith 
274aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
2759a42bb27SBarry Smith 
2769a42bb27SBarry Smith .seealso: DMSetFromOptions()
2779a42bb27SBarry Smith @*/
2787087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
2799a42bb27SBarry Smith {
2809a42bb27SBarry Smith   PetscErrorCode ierr;
2819a42bb27SBarry Smith 
2829a42bb27SBarry Smith   PetscFunctionBegin;
2839a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2849a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
2859a42bb27SBarry Smith   PetscFunctionReturn(0);
2869a42bb27SBarry Smith }
2879a42bb27SBarry Smith 
2889a42bb27SBarry Smith #undef __FUNCT__
28947c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
29047c6ae99SBarry Smith /*@
291aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
29247c6ae99SBarry Smith 
29347c6ae99SBarry Smith     Collective on DM
29447c6ae99SBarry Smith 
29547c6ae99SBarry Smith     Input Parameter:
29647c6ae99SBarry Smith .   dm - the DM object to destroy
29747c6ae99SBarry Smith 
29847c6ae99SBarry Smith     Level: developer
29947c6ae99SBarry Smith 
300e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
30147c6ae99SBarry Smith 
30247c6ae99SBarry Smith @*/
303fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
30447c6ae99SBarry Smith {
305af122d2aSMatthew G Knepley   PetscInt       i, cnt = 0, f;
306dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
30747c6ae99SBarry Smith   PetscErrorCode ierr;
30847c6ae99SBarry Smith 
30947c6ae99SBarry Smith   PetscFunctionBegin;
3106bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
3116bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
31287e657c6SBarry Smith 
31387e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
314732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
3156bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
3166bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
317732e2eb9SMatthew G Knepley   }
318dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
31971cd77b2SBarry Smith   if ((*dm)->x) {
320c688c046SMatthew G Knepley     DM obj;
321c688c046SMatthew G Knepley     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
322c688c046SMatthew G Knepley     if (obj == *dm) cnt++;
32371cd77b2SBarry Smith   }
324732e2eb9SMatthew G Knepley 
3256bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
326732e2eb9SMatthew G Knepley   /*
327732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
328732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
329732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
330732e2eb9SMatthew G Knepley   */
3316bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
3326bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
333732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
3346bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
3356bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
336732e2eb9SMatthew G Knepley   }
337dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
338dfe15315SJed Brown     nnext = nlink->next;
339dfe15315SJed 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);
340dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
341dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
342dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
343dfe15315SJed Brown   }
344dfe15315SJed Brown   (*dm)->namedglobal = PETSC_NULL;
3451a266240SBarry Smith 
346b17ce1afSJed Brown   /* Destroy the list of hooks */
347c833c3b5SJed Brown   {
348c833c3b5SJed Brown     DMCoarsenHookLink link,next;
349b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
350b17ce1afSJed Brown       next = link->next;
351b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
352b17ce1afSJed Brown     }
353b17ce1afSJed Brown     (*dm)->coarsenhook = PETSC_NULL;
354c833c3b5SJed Brown   }
355c833c3b5SJed Brown   {
356c833c3b5SJed Brown     DMRefineHookLink link,next;
357c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
358c833c3b5SJed Brown       next = link->next;
359c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
360c833c3b5SJed Brown     }
361c833c3b5SJed Brown     (*dm)->refinehook = PETSC_NULL;
362c833c3b5SJed Brown   }
363be081cd6SPeter Brune   {
364be081cd6SPeter Brune     DMSubDomainHookLink link,next;
365be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
366be081cd6SPeter Brune       next = link->next;
367be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
368be081cd6SPeter Brune     }
369be081cd6SPeter Brune     (*dm)->subdomainhook = PETSC_NULL;
370be081cd6SPeter Brune   }
371baf369e7SPeter Brune   {
372baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
373baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
374baf369e7SPeter Brune       next = link->next;
375baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
376baf369e7SPeter Brune     }
377baf369e7SPeter Brune     (*dm)->gtolhook = PETSC_NULL;
378baf369e7SPeter Brune   }
379aa1993deSMatthew G Knepley   /* Destroy the work arrays */
380aa1993deSMatthew G Knepley   {
381aa1993deSMatthew G Knepley     DMWorkLink link,next;
382aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
383aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
384aa1993deSMatthew G Knepley       next = link->next;
385aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
386aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
387aa1993deSMatthew G Knepley     }
388aa1993deSMatthew G Knepley     (*dm)->workin = PETSC_NULL;
389aa1993deSMatthew G Knepley   }
390b17ce1afSJed Brown 
39152536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
39252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
39352536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
39452536dc3SBarry Smith 
3951a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
3961a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
3971a266240SBarry Smith   }
39887e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
39971cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
4004dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
4016bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
4026bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
4036bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
404073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
40588ed4aceSMatthew G Knepley 
40688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
40788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
4088b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
40988ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
41088ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
411af122d2aSMatthew G Knepley 
4126636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
4136636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
4146636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
4156636e97aSMatthew G Knepley 
416af122d2aSMatthew G Knepley   for (f = 0; f < (*dm)->numFields; ++f) {
417af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr);
418af122d2aSMatthew G Knepley   }
419af122d2aSMatthew G Knepley   ierr = PetscFree((*dm)->fields);CHKERRQ(ierr);
420732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
4216bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
422732e2eb9SMatthew G Knepley 
4236bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
424435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
425732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
42647c6ae99SBarry Smith   PetscFunctionReturn(0);
42747c6ae99SBarry Smith }
42847c6ae99SBarry Smith 
42947c6ae99SBarry Smith #undef __FUNCT__
430d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
431d7bf68aeSBarry Smith /*@
432d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
433d7bf68aeSBarry Smith 
434d7bf68aeSBarry Smith     Collective on DM
435d7bf68aeSBarry Smith 
436d7bf68aeSBarry Smith     Input Parameter:
437d7bf68aeSBarry Smith .   dm - the DM object to setup
438d7bf68aeSBarry Smith 
439d7bf68aeSBarry Smith     Level: developer
440d7bf68aeSBarry Smith 
441e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
442d7bf68aeSBarry Smith 
443d7bf68aeSBarry Smith @*/
4447087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
445d7bf68aeSBarry Smith {
446d7bf68aeSBarry Smith   PetscErrorCode ierr;
447d7bf68aeSBarry Smith 
448d7bf68aeSBarry Smith   PetscFunctionBegin;
449171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4508387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
451d7bf68aeSBarry Smith   if (dm->ops->setup) {
452d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
453d7bf68aeSBarry Smith   }
4548387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
455d7bf68aeSBarry Smith   PetscFunctionReturn(0);
456d7bf68aeSBarry Smith }
457d7bf68aeSBarry Smith 
458d7bf68aeSBarry Smith #undef __FUNCT__
459d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
460d7bf68aeSBarry Smith /*@
461d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
462d7bf68aeSBarry Smith 
463d7bf68aeSBarry Smith     Collective on DM
464d7bf68aeSBarry Smith 
465d7bf68aeSBarry Smith     Input Parameter:
466d7bf68aeSBarry Smith .   dm - the DM object to set options for
467d7bf68aeSBarry Smith 
468732e2eb9SMatthew G Knepley     Options Database:
469dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
470dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
471171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
472171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
473732e2eb9SMatthew G Knepley 
474d7bf68aeSBarry Smith     Level: developer
475d7bf68aeSBarry Smith 
476e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
477d7bf68aeSBarry Smith 
478d7bf68aeSBarry Smith @*/
4797087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
480d7bf68aeSBarry Smith {
481f55f929bSMatthew G Knepley   char           typeName[256] = MATAIJ;
482ca266f36SBarry Smith   PetscBool      flg;
483d7bf68aeSBarry Smith   PetscErrorCode ierr;
484d7bf68aeSBarry Smith 
485d7bf68aeSBarry Smith   PetscFunctionBegin;
486171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4873194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
488073dac72SJed Brown     ierr = PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,PETSC_NULL);CHKERRQ(ierr);
489f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
490f9ba7244SBarry Smith     if (flg) {
491f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
492f9ba7244SBarry Smith     }
4938caf3d72SBarry 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);
494073dac72SJed Brown     if (flg) {
495521d9a4cSLisandro Dalcin       ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
496073dac72SJed Brown     }
4971b89239cSHong Zhang     ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,PETSC_NULL);CHKERRQ(ierr);
498f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
499f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
500f9ba7244SBarry Smith     }
501f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
502f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
50382fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
504ca266f36SBarry Smith   ierr = DMViewFromOptions(dm,"-dm_view");CHKERRQ(ierr);
505d7bf68aeSBarry Smith   PetscFunctionReturn(0);
506d7bf68aeSBarry Smith }
507d7bf68aeSBarry Smith 
508d7bf68aeSBarry Smith #undef __FUNCT__
50947c6ae99SBarry Smith #define __FUNCT__ "DMView"
510fc9bc008SSatish Balay /*@C
511aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
51247c6ae99SBarry Smith 
51347c6ae99SBarry Smith     Collective on DM
51447c6ae99SBarry Smith 
51547c6ae99SBarry Smith     Input Parameter:
51647c6ae99SBarry Smith +   dm - the DM object to view
51747c6ae99SBarry Smith -   v - the viewer
51847c6ae99SBarry Smith 
51947c6ae99SBarry Smith     Level: developer
52047c6ae99SBarry Smith 
521e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
52247c6ae99SBarry Smith 
52347c6ae99SBarry Smith @*/
5247087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
52547c6ae99SBarry Smith {
52647c6ae99SBarry Smith   PetscErrorCode ierr;
52732c0f0efSBarry Smith   PetscBool      isbinary;
52847c6ae99SBarry Smith 
52947c6ae99SBarry Smith   PetscFunctionBegin;
530171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5313014e516SBarry Smith   if (!v) {
5323014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
5333014e516SBarry Smith   }
53432c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
53532c0f0efSBarry Smith   if (isbinary) {
53655849f57SBarry Smith     PetscInt         classid = DM_FILE_CLASSID;
53732c0f0efSBarry Smith     char             type[256];
53832c0f0efSBarry Smith 
53932c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
54032c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
54132c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
54232c0f0efSBarry Smith   }
5430c010503SBarry Smith   if (dm->ops->view) {
5440c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
54547c6ae99SBarry Smith   }
54647c6ae99SBarry Smith   PetscFunctionReturn(0);
54747c6ae99SBarry Smith }
54847c6ae99SBarry Smith 
549ba654b55SMatthew G Knepley PETSC_EXTERN PetscErrorCode VecView_Complex_Local(Vec, PetscViewer);
550ba654b55SMatthew G Knepley PETSC_EXTERN PetscErrorCode VecView_Complex(Vec, PetscViewer);
551ba654b55SMatthew G Knepley 
55247c6ae99SBarry Smith #undef __FUNCT__
55347c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
55447c6ae99SBarry Smith /*@
555aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
55647c6ae99SBarry Smith 
55747c6ae99SBarry Smith     Collective on DM
55847c6ae99SBarry Smith 
55947c6ae99SBarry Smith     Input Parameter:
56047c6ae99SBarry Smith .   dm - the DM object
56147c6ae99SBarry Smith 
56247c6ae99SBarry Smith     Output Parameter:
56347c6ae99SBarry Smith .   vec - the global vector
56447c6ae99SBarry Smith 
565073dac72SJed Brown     Level: beginner
56647c6ae99SBarry Smith 
567e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
56847c6ae99SBarry Smith 
56947c6ae99SBarry Smith @*/
5707087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
57147c6ae99SBarry Smith {
57247c6ae99SBarry Smith   PetscErrorCode ierr;
57347c6ae99SBarry Smith 
57447c6ae99SBarry Smith   PetscFunctionBegin;
575171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57688ed4aceSMatthew G Knepley   if (dm->defaultSection) {
57788ed4aceSMatthew G Knepley     PetscSection gSection;
5781f588964SMatthew G Knepley     PetscInt     localSize, blockSize = -1, pStart, pEnd, p;
57988ed4aceSMatthew G Knepley 
58088ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
5811f588964SMatthew G Knepley     ierr = PetscSectionGetChart(dm->defaultSection, &pStart, &pEnd);CHKERRQ(ierr);
5821f588964SMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
5831f588964SMatthew G Knepley       PetscInt dof, cdof;
5841f588964SMatthew G Knepley 
5851f588964SMatthew G Knepley       ierr = PetscSectionGetDof(dm->defaultSection, p, &dof);CHKERRQ(ierr);
5861f588964SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(dm->defaultSection, p, &cdof);CHKERRQ(ierr);
5871f588964SMatthew G Knepley       if ((blockSize < 0) && (dof > 0)) blockSize = dof-cdof;
5881f588964SMatthew G Knepley       if ((dof > 0) && (dof-cdof != blockSize)) {
5891f588964SMatthew G Knepley         blockSize = 1;
5901f588964SMatthew G Knepley         break;
5911f588964SMatthew G Knepley       }
5921f588964SMatthew G Knepley     }
59388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
5941f588964SMatthew G Knepley     if (localSize%blockSize) SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONG, "Mismatch between blocksize %d and local storage size %d", blockSize, localSize);
59588ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
59688ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
5971f588964SMatthew G Knepley     ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr);
59888ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
59988ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
600c688c046SMatthew G Knepley     ierr = VecSetDM(*vec, dm);CHKERRQ(ierr);
60188ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
60288ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
60388ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
604ba654b55SMatthew G Knepley     ierr = VecSetOperation(*vec, VECOP_VIEW, (void(*)(void)) VecView_Complex);CHKERRQ(ierr);
60588ed4aceSMatthew G Knepley   } else {
60647c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
60788ed4aceSMatthew G Knepley   }
60847c6ae99SBarry Smith   PetscFunctionReturn(0);
60947c6ae99SBarry Smith }
61047c6ae99SBarry Smith 
61147c6ae99SBarry Smith #undef __FUNCT__
61247c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
61347c6ae99SBarry Smith /*@
614aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
61547c6ae99SBarry Smith 
61647c6ae99SBarry Smith     Not Collective
61747c6ae99SBarry Smith 
61847c6ae99SBarry Smith     Input Parameter:
61947c6ae99SBarry Smith .   dm - the DM object
62047c6ae99SBarry Smith 
62147c6ae99SBarry Smith     Output Parameter:
62247c6ae99SBarry Smith .   vec - the local vector
62347c6ae99SBarry Smith 
624073dac72SJed Brown     Level: beginner
62547c6ae99SBarry Smith 
626e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
62747c6ae99SBarry Smith 
62847c6ae99SBarry Smith @*/
6297087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
63047c6ae99SBarry Smith {
63147c6ae99SBarry Smith   PetscErrorCode ierr;
63247c6ae99SBarry Smith 
63347c6ae99SBarry Smith   PetscFunctionBegin;
634171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63588ed4aceSMatthew G Knepley   if (dm->defaultSection) {
6361f588964SMatthew G Knepley     PetscInt localSize, blockSize = -1, pStart, pEnd, p;
63788ed4aceSMatthew G Knepley 
6381f588964SMatthew G Knepley     ierr = PetscSectionGetChart(dm->defaultSection, &pStart, &pEnd);CHKERRQ(ierr);
6391f588964SMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
6401f588964SMatthew G Knepley       PetscInt dof;
6411f588964SMatthew G Knepley 
6421f588964SMatthew G Knepley       ierr = PetscSectionGetDof(dm->defaultSection, p, &dof);CHKERRQ(ierr);
6431f588964SMatthew G Knepley       if ((blockSize < 0) && (dof > 0)) blockSize = dof;
6441f588964SMatthew G Knepley       if ((dof > 0) && (dof != blockSize)) {
6451f588964SMatthew G Knepley         blockSize = 1;
6461f588964SMatthew G Knepley         break;
6471f588964SMatthew G Knepley       }
6481f588964SMatthew G Knepley     }
64988ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
65088ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
65188ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
6521f588964SMatthew G Knepley     ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr);
65388ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
654c688c046SMatthew G Knepley     ierr = VecSetDM(*vec, dm);CHKERRQ(ierr);
655ba654b55SMatthew G Knepley     ierr = VecSetOperation(*vec, VECOP_VIEW, (void(*)(void)) VecView_Complex_Local);CHKERRQ(ierr);
65688ed4aceSMatthew G Knepley   } else {
65747c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
65888ed4aceSMatthew G Knepley   }
65947c6ae99SBarry Smith   PetscFunctionReturn(0);
66047c6ae99SBarry Smith }
66147c6ae99SBarry Smith 
66247c6ae99SBarry Smith #undef __FUNCT__
6631411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
6641411c6eeSJed Brown /*@
6651411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
6661411c6eeSJed Brown 
6671411c6eeSJed Brown    Collective on DM
6681411c6eeSJed Brown 
6691411c6eeSJed Brown    Input Parameter:
6701411c6eeSJed Brown .  dm - the DM that provides the mapping
6711411c6eeSJed Brown 
6721411c6eeSJed Brown    Output Parameter:
6731411c6eeSJed Brown .  ltog - the mapping
6741411c6eeSJed Brown 
6751411c6eeSJed Brown    Level: intermediate
6761411c6eeSJed Brown 
6771411c6eeSJed Brown    Notes:
6781411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
6791411c6eeSJed Brown    MatSetLocalToGlobalMapping().
6801411c6eeSJed Brown 
6811411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
6821411c6eeSJed Brown @*/
6837087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
6841411c6eeSJed Brown {
6851411c6eeSJed Brown   PetscErrorCode ierr;
6861411c6eeSJed Brown 
6871411c6eeSJed Brown   PetscFunctionBegin;
6881411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6891411c6eeSJed Brown   PetscValidPointer(ltog,2);
6901411c6eeSJed Brown   if (!dm->ltogmap) {
69137d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
69237d0c07bSMatthew G Knepley 
69337d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
69437d0c07bSMatthew G Knepley     if (section) {
69537d0c07bSMatthew G Knepley       PetscInt      *ltog;
69637d0c07bSMatthew G Knepley       PetscInt       pStart, pEnd, size, p, l;
69737d0c07bSMatthew G Knepley 
69837d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
69937d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
70037d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
70137d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
70237d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
70337d0c07bSMatthew G Knepley         PetscInt dof, off, c;
70437d0c07bSMatthew G Knepley 
70537d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
70637d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
70737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
70837d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
70937d0c07bSMatthew G Knepley           ltog[l] = off+c;
71037d0c07bSMatthew G Knepley         }
71137d0c07bSMatthew G Knepley       }
71237d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
71337d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
71437d0c07bSMatthew G Knepley     } else {
7151411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
7161411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
7171411c6eeSJed Brown     }
71837d0c07bSMatthew G Knepley   }
7191411c6eeSJed Brown   *ltog = dm->ltogmap;
7201411c6eeSJed Brown   PetscFunctionReturn(0);
7211411c6eeSJed Brown }
7221411c6eeSJed Brown 
7231411c6eeSJed Brown #undef __FUNCT__
7241411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
7251411c6eeSJed Brown /*@
7261411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
7271411c6eeSJed Brown 
7281411c6eeSJed Brown    Collective on DM
7291411c6eeSJed Brown 
7301411c6eeSJed Brown    Input Parameter:
7311411c6eeSJed Brown .  da - the distributed array that provides the mapping
7321411c6eeSJed Brown 
7331411c6eeSJed Brown    Output Parameter:
7341411c6eeSJed Brown .  ltog - the block mapping
7351411c6eeSJed Brown 
7361411c6eeSJed Brown    Level: intermediate
7371411c6eeSJed Brown 
7381411c6eeSJed Brown    Notes:
7391411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
7401411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
7411411c6eeSJed Brown 
7421411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
7431411c6eeSJed Brown @*/
7447087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
7451411c6eeSJed Brown {
7461411c6eeSJed Brown   PetscErrorCode ierr;
7471411c6eeSJed Brown 
7481411c6eeSJed Brown   PetscFunctionBegin;
7491411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7501411c6eeSJed Brown   PetscValidPointer(ltog,2);
7511411c6eeSJed Brown   if (!dm->ltogmapb) {
7521411c6eeSJed Brown     PetscInt bs;
7531411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
7541411c6eeSJed Brown     if (bs > 1) {
7551411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
7561411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
7571411c6eeSJed Brown     } else {
7581411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
7591411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
7601411c6eeSJed Brown     }
7611411c6eeSJed Brown   }
7621411c6eeSJed Brown   *ltog = dm->ltogmapb;
7631411c6eeSJed Brown   PetscFunctionReturn(0);
7641411c6eeSJed Brown }
7651411c6eeSJed Brown 
7661411c6eeSJed Brown #undef __FUNCT__
7671411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7681411c6eeSJed Brown /*@
7691411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7701411c6eeSJed Brown 
7711411c6eeSJed Brown    Not Collective
7721411c6eeSJed Brown 
7731411c6eeSJed Brown    Input Parameter:
7741411c6eeSJed Brown .  dm - the DM with block structure
7751411c6eeSJed Brown 
7761411c6eeSJed Brown    Output Parameter:
7771411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7781411c6eeSJed Brown 
7791411c6eeSJed Brown    Level: intermediate
7801411c6eeSJed Brown 
7811411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
7821411c6eeSJed Brown @*/
7837087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7841411c6eeSJed Brown {
7851411c6eeSJed Brown   PetscFunctionBegin;
7861411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7871411c6eeSJed Brown   PetscValidPointer(bs,2);
7881411c6eeSJed 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");
7891411c6eeSJed Brown   *bs = dm->bs;
7901411c6eeSJed Brown   PetscFunctionReturn(0);
7911411c6eeSJed Brown }
7921411c6eeSJed Brown 
7931411c6eeSJed Brown #undef __FUNCT__
794e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
79547c6ae99SBarry Smith /*@
796e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
79747c6ae99SBarry Smith 
79847c6ae99SBarry Smith     Collective on DM
79947c6ae99SBarry Smith 
80047c6ae99SBarry Smith     Input Parameter:
80147c6ae99SBarry Smith +   dm1 - the DM object
80247c6ae99SBarry Smith -   dm2 - the second, finer DM object
80347c6ae99SBarry Smith 
80447c6ae99SBarry Smith     Output Parameter:
80547c6ae99SBarry Smith +  mat - the interpolation
80647c6ae99SBarry Smith -  vec - the scaling (optional)
80747c6ae99SBarry Smith 
80847c6ae99SBarry Smith     Level: developer
80947c6ae99SBarry Smith 
81085afcc9aSBarry 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
81185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
812d52bd9f3SBarry Smith 
8131f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
814d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
81585afcc9aSBarry Smith 
81685afcc9aSBarry Smith 
817e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
81847c6ae99SBarry Smith 
81947c6ae99SBarry Smith @*/
820e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
82147c6ae99SBarry Smith {
82247c6ae99SBarry Smith   PetscErrorCode ierr;
82347c6ae99SBarry Smith 
82447c6ae99SBarry Smith   PetscFunctionBegin;
825171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
826171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
82725296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
82847c6ae99SBarry Smith   PetscFunctionReturn(0);
82947c6ae99SBarry Smith }
83047c6ae99SBarry Smith 
83147c6ae99SBarry Smith #undef __FUNCT__
832e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
83347c6ae99SBarry Smith /*@
834e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
83547c6ae99SBarry Smith 
83647c6ae99SBarry Smith     Collective on DM
83747c6ae99SBarry Smith 
83847c6ae99SBarry Smith     Input Parameter:
83947c6ae99SBarry Smith +   dm1 - the DM object
84047c6ae99SBarry Smith -   dm2 - the second, finer DM object
84147c6ae99SBarry Smith 
84247c6ae99SBarry Smith     Output Parameter:
84347c6ae99SBarry Smith .   ctx - the injection
84447c6ae99SBarry Smith 
84547c6ae99SBarry Smith     Level: developer
84647c6ae99SBarry Smith 
84785afcc9aSBarry 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
84885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
84985afcc9aSBarry Smith 
850e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
85147c6ae99SBarry Smith 
85247c6ae99SBarry Smith @*/
853e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
85447c6ae99SBarry Smith {
85547c6ae99SBarry Smith   PetscErrorCode ierr;
85647c6ae99SBarry Smith 
85747c6ae99SBarry Smith   PetscFunctionBegin;
858171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
859171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
86047c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
86147c6ae99SBarry Smith   PetscFunctionReturn(0);
86247c6ae99SBarry Smith }
86347c6ae99SBarry Smith 
86447c6ae99SBarry Smith #undef __FUNCT__
865e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
866d1e2c406SBarry Smith /*@C
867e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
86847c6ae99SBarry Smith 
86947c6ae99SBarry Smith     Collective on DM
87047c6ae99SBarry Smith 
87147c6ae99SBarry Smith     Input Parameter:
87247c6ae99SBarry Smith +   dm - the DM object
87347c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
87447c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith     Output Parameter:
87747c6ae99SBarry Smith .   coloring - the coloring
87847c6ae99SBarry Smith 
87947c6ae99SBarry Smith     Level: developer
88047c6ae99SBarry Smith 
881e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
88247c6ae99SBarry Smith 
883aab9d709SJed Brown @*/
88419fd82e9SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,MatType mtype,ISColoring *coloring)
88547c6ae99SBarry Smith {
88647c6ae99SBarry Smith   PetscErrorCode ierr;
88747c6ae99SBarry Smith 
88847c6ae99SBarry Smith   PetscFunctionBegin;
889171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
89047c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
89147c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
89247c6ae99SBarry Smith   PetscFunctionReturn(0);
89347c6ae99SBarry Smith }
89447c6ae99SBarry Smith 
89547c6ae99SBarry Smith #undef __FUNCT__
896950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
89747c6ae99SBarry Smith /*@C
898950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
89947c6ae99SBarry Smith 
90047c6ae99SBarry Smith     Collective on DM
90147c6ae99SBarry Smith 
90247c6ae99SBarry Smith     Input Parameter:
90347c6ae99SBarry Smith +   dm - the DM object
90447c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
90594013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
90647c6ae99SBarry Smith 
90747c6ae99SBarry Smith     Output Parameter:
90847c6ae99SBarry Smith .   mat - the empty Jacobian
90947c6ae99SBarry Smith 
910073dac72SJed Brown     Level: beginner
91147c6ae99SBarry Smith 
91294013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
91394013140SBarry Smith        do not need to do it yourself.
91494013140SBarry Smith 
91594013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
916aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
91794013140SBarry Smith 
91894013140SBarry 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
91994013140SBarry Smith        internally by PETSc.
92094013140SBarry Smith 
92194013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
922aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
92394013140SBarry Smith 
924e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
92547c6ae99SBarry Smith 
926aab9d709SJed Brown @*/
92719fd82e9SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,MatType mtype,Mat *mat)
92847c6ae99SBarry Smith {
92947c6ae99SBarry Smith   PetscErrorCode ierr;
93047c6ae99SBarry Smith 
93147c6ae99SBarry Smith   PetscFunctionBegin;
932171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
933235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
934235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
935235683edSBarry Smith #endif
936c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
937c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
938073dac72SJed Brown   if (dm->mattype) {
93925296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
940073dac72SJed Brown   } else {
94125296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
942c7b7c8a4SJed Brown   }
94347c6ae99SBarry Smith   PetscFunctionReturn(0);
94447c6ae99SBarry Smith }
94547c6ae99SBarry Smith 
94647c6ae99SBarry Smith #undef __FUNCT__
947732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
948732e2eb9SMatthew G Knepley /*@
949950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
950732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
951732e2eb9SMatthew G Knepley 
952732e2eb9SMatthew G Knepley   Logically Collective on DMDA
953732e2eb9SMatthew G Knepley 
954732e2eb9SMatthew G Knepley   Input Parameter:
955732e2eb9SMatthew G Knepley + dm - the DM
956732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
957732e2eb9SMatthew G Knepley 
958732e2eb9SMatthew G Knepley   Level: developer
959950540a4SJed Brown .seealso DMCreateMatrix()
960732e2eb9SMatthew G Knepley @*/
961732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
962732e2eb9SMatthew G Knepley {
963732e2eb9SMatthew G Knepley   PetscFunctionBegin;
964732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
965732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
966732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
967732e2eb9SMatthew G Knepley }
968732e2eb9SMatthew G Knepley 
969732e2eb9SMatthew G Knepley #undef __FUNCT__
970a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
971a89ea682SMatthew G Knepley /*@C
972aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
973a89ea682SMatthew G Knepley 
974a89ea682SMatthew G Knepley   Not Collective
975a89ea682SMatthew G Knepley 
976a89ea682SMatthew G Knepley   Input Parameters:
977a89ea682SMatthew G Knepley + dm - the DM object
978aa1993deSMatthew G Knepley . count - The minium size
979aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
980a89ea682SMatthew G Knepley 
981a89ea682SMatthew G Knepley   Output Parameter:
982a89ea682SMatthew G Knepley . array - the work array
983a89ea682SMatthew G Knepley 
984a89ea682SMatthew G Knepley   Level: developer
985a89ea682SMatthew G Knepley 
986a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
987a89ea682SMatthew G Knepley @*/
988aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
989a89ea682SMatthew G Knepley {
990a89ea682SMatthew G Knepley   PetscErrorCode ierr;
991aa1993deSMatthew G Knepley   DMWorkLink link;
992aa1993deSMatthew G Knepley   size_t size;
993a89ea682SMatthew G Knepley 
994a89ea682SMatthew G Knepley   PetscFunctionBegin;
995a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
996aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
997aa1993deSMatthew G Knepley   if (dm->workin) {
998aa1993deSMatthew G Knepley     link = dm->workin;
999aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1000aa1993deSMatthew G Knepley   } else {
1001aa1993deSMatthew G Knepley     ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr);
1002a89ea682SMatthew G Knepley   }
1003aa1993deSMatthew G Knepley   ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr);
1004aa1993deSMatthew G Knepley   if (size*count > link->bytes) {
1005aa1993deSMatthew G Knepley     ierr = PetscFree(link->mem);CHKERRQ(ierr);
1006aa1993deSMatthew G Knepley     ierr = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr);
1007aa1993deSMatthew G Knepley     link->bytes = size*count;
1008aa1993deSMatthew G Knepley   }
1009aa1993deSMatthew G Knepley   link->next = dm->workout;
1010aa1993deSMatthew G Knepley   dm->workout = link;
1011aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1012a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1013a89ea682SMatthew G Knepley }
1014a89ea682SMatthew G Knepley 
1015aa1993deSMatthew G Knepley #undef __FUNCT__
1016aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1017aa1993deSMatthew G Knepley /*@C
1018aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1019aa1993deSMatthew G Knepley 
1020aa1993deSMatthew G Knepley   Not Collective
1021aa1993deSMatthew G Knepley 
1022aa1993deSMatthew G Knepley   Input Parameters:
1023aa1993deSMatthew G Knepley + dm - the DM object
1024aa1993deSMatthew G Knepley . count - The minium size
1025aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1026aa1993deSMatthew G Knepley 
1027aa1993deSMatthew G Knepley   Output Parameter:
1028aa1993deSMatthew G Knepley . array - the work array
1029aa1993deSMatthew G Knepley 
1030aa1993deSMatthew G Knepley   Level: developer
1031aa1993deSMatthew G Knepley 
1032aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1033aa1993deSMatthew G Knepley @*/
1034aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1035aa1993deSMatthew G Knepley {
1036aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1037aa1993deSMatthew G Knepley 
1038aa1993deSMatthew G Knepley   PetscFunctionBegin;
1039aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1040aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1041aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1042aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1043aa1993deSMatthew G Knepley       *p = link->next;
1044aa1993deSMatthew G Knepley       link->next = dm->workin;
1045aa1993deSMatthew G Knepley       dm->workin = link;
1046aa1993deSMatthew G Knepley       *(void**)mem = PETSC_NULL;
1047aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1048aa1993deSMatthew G Knepley     }
1049aa1993deSMatthew G Knepley   }
1050aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1051aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1052aa1993deSMatthew G Knepley }
1053e7c4fc90SDmitry Karpeev 
1054e7c4fc90SDmitry Karpeev #undef __FUNCT__
1055435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1056435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1057435a35e8SMatthew G Knepley {
1058435a35e8SMatthew G Knepley   PetscFunctionBegin;
1059435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1060435a35e8SMatthew G Knepley   if (field >= 10) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1061435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1062435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1063435a35e8SMatthew G Knepley }
1064435a35e8SMatthew G Knepley 
1065435a35e8SMatthew G Knepley #undef __FUNCT__
10664d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10674f3b5142SJed Brown /*@C
10684d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10694d343eeaSMatthew G Knepley 
10704d343eeaSMatthew G Knepley   Not collective
10714d343eeaSMatthew G Knepley 
10724d343eeaSMatthew G Knepley   Input Parameter:
10734d343eeaSMatthew G Knepley . dm - the DM object
10744d343eeaSMatthew G Knepley 
10754d343eeaSMatthew G Knepley   Output Parameters:
107621c9b008SJed Brown + numFields  - The number of fields (or PETSC_NULL if not requested)
107737d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested)
107821c9b008SJed Brown - fields     - The global indices for each field (or PETSC_NULL if not requested)
10794d343eeaSMatthew G Knepley 
10804d343eeaSMatthew G Knepley   Level: intermediate
10814d343eeaSMatthew G Knepley 
108221c9b008SJed Brown   Notes:
108321c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
108421c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
108521c9b008SJed Brown   PetscFree().
108621c9b008SJed Brown 
10874d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10884d343eeaSMatthew G Knepley @*/
108937d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10904d343eeaSMatthew G Knepley {
109137d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10924d343eeaSMatthew G Knepley   PetscErrorCode ierr;
10934d343eeaSMatthew G Knepley 
10944d343eeaSMatthew G Knepley   PetscFunctionBegin;
10954d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
109669ca1f37SDmitry Karpeev   if (numFields) {
109769ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
109869ca1f37SDmitry Karpeev     *numFields = 0;
109969ca1f37SDmitry Karpeev   }
110037d0c07bSMatthew G Knepley   if (fieldNames) {
110137d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
110237d0c07bSMatthew G Knepley     *fieldNames = PETSC_NULL;
110369ca1f37SDmitry Karpeev   }
110469ca1f37SDmitry Karpeev   if (fields) {
110569ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
110669ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
110769ca1f37SDmitry Karpeev   }
110837d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
110937d0c07bSMatthew G Knepley   if (section) {
111037d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
111137d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
111237d0c07bSMatthew G Knepley 
111337d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
111437d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
111537d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr);
111637d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
111737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
111837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
111937d0c07bSMatthew G Knepley     }
112037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
112137d0c07bSMatthew G Knepley       PetscInt gdof;
112237d0c07bSMatthew G Knepley 
112337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
112437d0c07bSMatthew G Knepley       if (gdof > 0) {
112537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
112637d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
112737d0c07bSMatthew G Knepley 
112837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
112937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
113037d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
113137d0c07bSMatthew G Knepley         }
113237d0c07bSMatthew G Knepley       }
113337d0c07bSMatthew G Knepley     }
113437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
113537d0c07bSMatthew G Knepley       ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
113637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
113737d0c07bSMatthew G Knepley     }
113837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
113937d0c07bSMatthew G Knepley       PetscInt gdof, goff;
114037d0c07bSMatthew G Knepley 
114137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
114237d0c07bSMatthew G Knepley       if (gdof > 0) {
114337d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
114437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
114537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
114637d0c07bSMatthew G Knepley 
114737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
114837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
114937d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
115037d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
115137d0c07bSMatthew G Knepley           }
115237d0c07bSMatthew G Knepley         }
115337d0c07bSMatthew G Knepley       }
115437d0c07bSMatthew G Knepley     }
115537d0c07bSMatthew G Knepley     if (numFields) {*numFields = nF;}
115637d0c07bSMatthew G Knepley     if (fieldNames) {
115737d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr);
115837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
115937d0c07bSMatthew G Knepley         const char *fieldName;
116037d0c07bSMatthew G Knepley 
116137d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
116237d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr);
116337d0c07bSMatthew G Knepley       }
116437d0c07bSMatthew G Knepley     }
116537d0c07bSMatthew G Knepley     if (fields) {
116637d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
116737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
116837d0c07bSMatthew G Knepley         ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
116937d0c07bSMatthew G Knepley       }
117037d0c07bSMatthew G Knepley     }
117137d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
117237d0c07bSMatthew G Knepley   } else {
117337d0c07bSMatthew G Knepley     if (dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);}
117469ca1f37SDmitry Karpeev   }
11754d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11764d343eeaSMatthew G Knepley }
11774d343eeaSMatthew G Knepley 
1178a89ea682SMatthew G Knepley #undef __FUNCT__
117916621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM"
1180e7c4fc90SDmitry Karpeev /*@C
118116621825SDmitry Karpeev   DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields.
118216621825SDmitry Karpeev 
118316621825SDmitry Karpeev   Not Collective
118416621825SDmitry Karpeev 
118516621825SDmitry Karpeev   Input Parameters:
118616621825SDmitry Karpeev + dm   - the DM object
118716621825SDmitry Karpeev - name - the name of the field decomposition
118816621825SDmitry Karpeev 
118916621825SDmitry Karpeev   Output Parameter:
119016621825SDmitry Karpeev . ddm  - the field decomposition DM (PETSC_NULL, if no such decomposition is known)
119116621825SDmitry Karpeev 
119216621825SDmitry Karpeev   Level: advanced
119316621825SDmitry Karpeev 
119416621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
119516621825SDmitry Karpeev @*/
119616621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm)
119716621825SDmitry Karpeev {
119816621825SDmitry Karpeev   PetscErrorCode ierr;
119916621825SDmitry Karpeev 
120016621825SDmitry Karpeev   PetscFunctionBegin;
120116621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
120216621825SDmitry Karpeev   PetscValidCharPointer(name,2);
120316621825SDmitry Karpeev   PetscValidPointer(ddm,3);
120416621825SDmitry Karpeev   *ddm = PETSC_NULL;
1205731c8d9eSDmitry Karpeev   if (dm->ops->createfielddecompositiondm) {
120616621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm);CHKERRQ(ierr);
120716621825SDmitry Karpeev   }
120816621825SDmitry Karpeev   PetscFunctionReturn(0);
120916621825SDmitry Karpeev }
121016621825SDmitry Karpeev 
121116621825SDmitry Karpeev #undef __FUNCT__
121216621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
121316621825SDmitry Karpeev /*@C
121416621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
121516621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
121616621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1217e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1218e7c4fc90SDmitry Karpeev 
1219e7c4fc90SDmitry Karpeev   Not collective
1220e7c4fc90SDmitry Karpeev 
1221e7c4fc90SDmitry Karpeev   Input Parameter:
1222e7c4fc90SDmitry Karpeev . dm - the DM object
1223e7c4fc90SDmitry Karpeev 
1224e7c4fc90SDmitry Karpeev   Output Parameters:
122516621825SDmitry Karpeev + len       - The number of subproblems in the field decomposition (or PETSC_NULL if not requested)
122616621825SDmitry Karpeev . namelist  - The name for each field (or PETSC_NULL if not requested)
122716621825SDmitry Karpeev . islist    - The global indices for each field (or PETSC_NULL if not requested)
122816621825SDmitry Karpeev - dmlist    - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
1229e7c4fc90SDmitry Karpeev 
1230e7c4fc90SDmitry Karpeev   Level: intermediate
1231e7c4fc90SDmitry Karpeev 
1232e7c4fc90SDmitry Karpeev   Notes:
1233e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1234e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1235e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1236e7c4fc90SDmitry Karpeev 
1237e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1238e7c4fc90SDmitry Karpeev @*/
123916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1240e7c4fc90SDmitry Karpeev {
1241e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1242e7c4fc90SDmitry Karpeev 
1243e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1244e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1245731c8d9eSDmitry Karpeev   if (len)      {PetscValidPointer(len,2);      *len      = 0;}
1246731c8d9eSDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3); *namelist = 0;}
1247731c8d9eSDmitry Karpeev   if (islist)   {PetscValidPointer(islist,4);   *islist   = 0;}
1248731c8d9eSDmitry Karpeev   if (dmlist)   {PetscValidPointer(dmlist,5);   *dmlist   = 0;}
124916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1250435a35e8SMatthew G Knepley     PetscSection section;
1251435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1252435a35e8SMatthew G Knepley 
1253435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1254435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1255435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1256435a35e8SMatthew G Knepley       *len = numFields;
1257435a35e8SMatthew G Knepley       ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr);
1258435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1259435a35e8SMatthew G Knepley         const char *fieldName;
1260435a35e8SMatthew G Knepley 
1261435a35e8SMatthew G Knepley         ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr);
1262435a35e8SMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1263435a35e8SMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*namelist)[f]);CHKERRQ(ierr);
1264435a35e8SMatthew G Knepley       }
1265435a35e8SMatthew G Knepley     } else {
126669ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1267e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
1268e7c4fc90SDmitry Karpeev       if (dmlist) *dmlist = PETSC_NULL;
1269e7c4fc90SDmitry Karpeev     }
1270435a35e8SMatthew G Knepley   }
1271e7c4fc90SDmitry Karpeev   else {
127216621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
127316621825SDmitry Karpeev   }
127416621825SDmitry Karpeev   PetscFunctionReturn(0);
127516621825SDmitry Karpeev }
127616621825SDmitry Karpeev 
127716621825SDmitry Karpeev #undef __FUNCT__
1278435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1279435a35e8SMatthew G Knepley /*@C
1280435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1281435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1282435a35e8SMatthew G Knepley 
1283435a35e8SMatthew G Knepley   Not collective
1284435a35e8SMatthew G Knepley 
1285435a35e8SMatthew G Knepley   Input Parameters:
1286435a35e8SMatthew G Knepley + dm - the DM object
1287435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
1288435a35e8SMatthew G Knepley - len       - The number of subproblems in the decomposition (or PETSC_NULL if not requested)
1289435a35e8SMatthew G Knepley 
1290435a35e8SMatthew G Knepley   Output Parameters:
1291435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1292435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1293435a35e8SMatthew G Knepley 
1294435a35e8SMatthew G Knepley   Level: intermediate
1295435a35e8SMatthew G Knepley 
1296435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1297435a35e8SMatthew G Knepley @*/
1298435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1299435a35e8SMatthew G Knepley {
1300435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1301435a35e8SMatthew G Knepley 
1302435a35e8SMatthew G Knepley   PetscFunctionBegin;
1303435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1304435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
1305435a35e8SMatthew G Knepley   if (is) {PetscValidPointer(is,4);}
1306435a35e8SMatthew G Knepley   if (subdm) {PetscValidPointer(subdm,5);}
1307435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1308435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1309435a35e8SMatthew G Knepley   } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1310435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1311435a35e8SMatthew G Knepley }
1312435a35e8SMatthew G Knepley 
1313435a35e8SMatthew G Knepley #undef __FUNCT__
131416621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM"
131516621825SDmitry Karpeev /*@C
131616621825SDmitry Karpeev   DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains.
131716621825SDmitry Karpeev 
131816621825SDmitry Karpeev   Not Collective
131916621825SDmitry Karpeev 
132016621825SDmitry Karpeev   Input Parameters:
132116621825SDmitry Karpeev + dm   - the DM object
132216621825SDmitry Karpeev - name - the name of the subdomain decomposition
132316621825SDmitry Karpeev 
132416621825SDmitry Karpeev   Output Parameter:
132516621825SDmitry Karpeev . ddm  - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known)
132616621825SDmitry Karpeev 
132716621825SDmitry Karpeev   Level: advanced
132816621825SDmitry Karpeev 
132916621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
133016621825SDmitry Karpeev @*/
133116621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm)
133216621825SDmitry Karpeev {
133316621825SDmitry Karpeev   PetscErrorCode ierr;
133416621825SDmitry Karpeev 
133516621825SDmitry Karpeev   PetscFunctionBegin;
133616621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
133716621825SDmitry Karpeev   PetscValidCharPointer(name,2);
133816621825SDmitry Karpeev   PetscValidPointer(ddm,3);
133916621825SDmitry Karpeev   *ddm = PETSC_NULL;
1340731c8d9eSDmitry Karpeev   if (dm->ops->createdomaindecompositiondm) {
134116621825SDmitry Karpeev     ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm);CHKERRQ(ierr);
134216621825SDmitry Karpeev   }
134316621825SDmitry Karpeev   PetscFunctionReturn(0);
134416621825SDmitry Karpeev }
134516621825SDmitry Karpeev 
134616621825SDmitry Karpeev #undef __FUNCT__
134716621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
134816621825SDmitry Karpeev /*@C
13498d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13508d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13518d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13528d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13538d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
135416621825SDmitry Karpeev 
135516621825SDmitry Karpeev   Not collective
135616621825SDmitry Karpeev 
135716621825SDmitry Karpeev   Input Parameter:
135816621825SDmitry Karpeev . dm - the DM object
135916621825SDmitry Karpeev 
136016621825SDmitry Karpeev   Output Parameters:
136116621825SDmitry Karpeev + len         - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested)
136216621825SDmitry Karpeev . namelist    - The name for each subdomain (or PETSC_NULL if not requested)
13638d4ac253SDmitry Karpeev . innerislist - The global indices for each inner subdomain (or PETSC_NULL, if not requested)
13648d4ac253SDmitry Karpeev . outerislist - The global indices for each outer subdomain (or PETSC_NULL, if not requested)
136516621825SDmitry Karpeev - dmlist      - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
136616621825SDmitry Karpeev 
136716621825SDmitry Karpeev   Level: intermediate
136816621825SDmitry Karpeev 
136916621825SDmitry Karpeev   Notes:
137016621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
137116621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
137216621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
137316621825SDmitry Karpeev 
13748d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
137516621825SDmitry Karpeev @*/
13768d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
137716621825SDmitry Karpeev {
137816621825SDmitry Karpeev   PetscErrorCode ierr;
1379be081cd6SPeter Brune   DMSubDomainHookLink link;
1380be081cd6SPeter Brune   PetscInt            i,l;
138116621825SDmitry Karpeev 
138216621825SDmitry Karpeev   PetscFunctionBegin;
138316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1384731c8d9eSDmitry Karpeev   if (len)           {PetscValidPointer(len,2);            *len         = PETSC_NULL;}
138516621825SDmitry Karpeev   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = PETSC_NULL;}
13868d4ac253SDmitry Karpeev   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = PETSC_NULL;}
13878d4ac253SDmitry Karpeev   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = PETSC_NULL;}
13888d4ac253SDmitry Karpeev   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = PETSC_NULL;}
138916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1390be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
1391*8cbdbec6SBarry Smith   } else SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DMCreateDomainDecomposition not supported for this DM type!");CHKERRQ(ierr);
1392be081cd6SPeter Brune   if (len) *len = l;
1393be081cd6SPeter Brune   for (i = 0; i < l; i++) {
1394be081cd6SPeter Brune     for (link=dm->subdomainhook; link; link=link->next) {
1395be081cd6SPeter Brune       if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1396be081cd6SPeter Brune     }
1397d425c654SPeter Brune     (*dmlist)[i]->ctx = dm->ctx;
1398e7c4fc90SDmitry Karpeev   }
1399e30e807fSPeter Brune   PetscFunctionReturn(0);
1400e30e807fSPeter Brune }
1401e30e807fSPeter Brune 
1402e30e807fSPeter Brune 
1403e30e807fSPeter Brune #undef __FUNCT__
1404e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1405e30e807fSPeter Brune /*@C
1406e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1407e30e807fSPeter Brune 
1408e30e807fSPeter Brune   Not collective
1409e30e807fSPeter Brune 
1410e30e807fSPeter Brune   Input Parameters:
1411e30e807fSPeter Brune + dm - the DM object
1412e30e807fSPeter Brune . n  - the number of subdomain scatters
1413e30e807fSPeter Brune - subdms - the local subdomains
1414e30e807fSPeter Brune 
1415e30e807fSPeter Brune   Output Parameters:
1416e30e807fSPeter Brune + n     - the number of scatters returned
1417e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1418e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1419e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1420e30e807fSPeter Brune 
1421e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1422e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1423e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1424e30e807fSPeter Brune   solution and residual data.
1425e30e807fSPeter Brune 
1426e30e807fSPeter Brune   Level: developer
1427e30e807fSPeter Brune 
1428e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1429e30e807fSPeter Brune @*/
1430e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1431e30e807fSPeter Brune {
1432e30e807fSPeter Brune   PetscErrorCode ierr;
1433e30e807fSPeter Brune 
1434e30e807fSPeter Brune   PetscFunctionBegin;
1435e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1436e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1437e30e807fSPeter Brune   PetscValidPointer(iscat,4);
1438e30e807fSPeter Brune   PetscValidPointer(oscat,5);
1439e30e807fSPeter Brune   PetscValidPointer(gscat,6);
1440e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1441e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
1442e30e807fSPeter Brune   } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1443e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1444e7c4fc90SDmitry Karpeev }
1445e7c4fc90SDmitry Karpeev 
1446731c8d9eSDmitry Karpeev #undef __FUNCT__
144747c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
144847c6ae99SBarry Smith /*@
144947c6ae99SBarry Smith   DMRefine - Refines a DM object
145047c6ae99SBarry Smith 
145147c6ae99SBarry Smith   Collective on DM
145247c6ae99SBarry Smith 
145347c6ae99SBarry Smith   Input Parameter:
145447c6ae99SBarry Smith + dm   - the DM object
145591d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
145647c6ae99SBarry Smith 
145747c6ae99SBarry Smith   Output Parameter:
1458ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1459ae0a1c52SMatthew G Knepley 
1460ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
146147c6ae99SBarry Smith 
146247c6ae99SBarry Smith   Level: developer
146347c6ae99SBarry Smith 
1464e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
146547c6ae99SBarry Smith @*/
14667087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
146747c6ae99SBarry Smith {
146847c6ae99SBarry Smith   PetscErrorCode ierr;
1469c833c3b5SJed Brown   DMRefineHookLink link;
147047c6ae99SBarry Smith 
147147c6ae99SBarry Smith   PetscFunctionBegin;
1472732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
147347c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14744057135bSMatthew G Knepley   if (*dmf) {
147543842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
14768cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1477644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14780598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1479656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
1480e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1481c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1482c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1483c833c3b5SJed Brown     }
1484c833c3b5SJed Brown   }
1485c833c3b5SJed Brown   PetscFunctionReturn(0);
1486c833c3b5SJed Brown }
1487c833c3b5SJed Brown 
1488c833c3b5SJed Brown #undef __FUNCT__
1489c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1490c833c3b5SJed Brown /*@
1491c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1492c833c3b5SJed Brown 
1493c833c3b5SJed Brown    Logically Collective
1494c833c3b5SJed Brown 
1495c833c3b5SJed Brown    Input Arguments:
1496c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1497c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1498c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1499c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1500c833c3b5SJed Brown 
1501c833c3b5SJed Brown    Calling sequence of refinehook:
1502c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1503c833c3b5SJed Brown 
1504c833c3b5SJed Brown +  coarse - coarse level DM
1505c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1506c833c3b5SJed Brown -  ctx - optional user-defined function context
1507c833c3b5SJed Brown 
1508c833c3b5SJed Brown    Calling sequence for interphook:
1509c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1510c833c3b5SJed Brown 
1511c833c3b5SJed Brown +  coarse - coarse level DM
1512c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1513c833c3b5SJed Brown .  fine - fine level DM to update
1514c833c3b5SJed Brown -  ctx - optional user-defined function context
1515c833c3b5SJed Brown 
1516c833c3b5SJed Brown    Level: advanced
1517c833c3b5SJed Brown 
1518c833c3b5SJed Brown    Notes:
1519c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1520c833c3b5SJed Brown 
1521c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1522c833c3b5SJed Brown 
1523c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1524c833c3b5SJed Brown @*/
1525c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1526c833c3b5SJed Brown {
1527c833c3b5SJed Brown   PetscErrorCode ierr;
1528c833c3b5SJed Brown   DMRefineHookLink link,*p;
1529c833c3b5SJed Brown 
1530c833c3b5SJed Brown   PetscFunctionBegin;
1531c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1532c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1533c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1534c833c3b5SJed Brown   link->refinehook = refinehook;
1535c833c3b5SJed Brown   link->interphook = interphook;
1536c833c3b5SJed Brown   link->ctx = ctx;
1537c833c3b5SJed Brown   link->next = PETSC_NULL;
1538c833c3b5SJed Brown   *p = link;
1539c833c3b5SJed Brown   PetscFunctionReturn(0);
1540c833c3b5SJed Brown }
1541c833c3b5SJed Brown 
1542c833c3b5SJed Brown #undef __FUNCT__
1543c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1544c833c3b5SJed Brown /*@
1545c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1546c833c3b5SJed Brown 
1547c833c3b5SJed Brown    Collective if any hooks are
1548c833c3b5SJed Brown 
1549c833c3b5SJed Brown    Input Arguments:
1550c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1551c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1552c833c3b5SJed Brown -  fine - finer DM to update
1553c833c3b5SJed Brown 
1554c833c3b5SJed Brown    Level: developer
1555c833c3b5SJed Brown 
1556c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1557c833c3b5SJed Brown @*/
1558c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1559c833c3b5SJed Brown {
1560c833c3b5SJed Brown   PetscErrorCode ierr;
1561c833c3b5SJed Brown   DMRefineHookLink link;
1562c833c3b5SJed Brown 
1563c833c3b5SJed Brown   PetscFunctionBegin;
1564c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1565c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
15664057135bSMatthew G Knepley   }
156747c6ae99SBarry Smith   PetscFunctionReturn(0);
156847c6ae99SBarry Smith }
156947c6ae99SBarry Smith 
157047c6ae99SBarry Smith #undef __FUNCT__
1571eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1572eb3f98d2SBarry Smith /*@
1573eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1574eb3f98d2SBarry Smith 
1575eb3f98d2SBarry Smith     Not Collective
1576eb3f98d2SBarry Smith 
1577eb3f98d2SBarry Smith     Input Parameter:
1578eb3f98d2SBarry Smith .   dm - the DM object
1579eb3f98d2SBarry Smith 
1580eb3f98d2SBarry Smith     Output Parameter:
1581eb3f98d2SBarry Smith .   level - number of refinements
1582eb3f98d2SBarry Smith 
1583eb3f98d2SBarry Smith     Level: developer
1584eb3f98d2SBarry Smith 
15856a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1586eb3f98d2SBarry Smith 
1587eb3f98d2SBarry Smith @*/
1588eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1589eb3f98d2SBarry Smith {
1590eb3f98d2SBarry Smith   PetscFunctionBegin;
1591eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1592eb3f98d2SBarry Smith   *level = dm->levelup;
1593eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1594eb3f98d2SBarry Smith }
1595eb3f98d2SBarry Smith 
1596eb3f98d2SBarry Smith #undef __FUNCT__
1597baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1598baf369e7SPeter Brune /*@
1599baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1600baf369e7SPeter Brune 
1601baf369e7SPeter Brune    Logically Collective
1602baf369e7SPeter Brune 
1603baf369e7SPeter Brune    Input Arguments:
1604baf369e7SPeter Brune +  dm - the DM
1605baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1606baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
1607baf369e7SPeter Brune -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1608baf369e7SPeter Brune 
1609baf369e7SPeter Brune    Calling sequence for beginhook:
1610baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1611baf369e7SPeter Brune 
1612baf369e7SPeter Brune +  dm - global DM
1613baf369e7SPeter Brune .  g - global vector
1614baf369e7SPeter Brune .  mode - mode
1615baf369e7SPeter Brune .  l - local vector
1616baf369e7SPeter Brune -  ctx - optional user-defined function context
1617baf369e7SPeter Brune 
1618baf369e7SPeter Brune 
1619baf369e7SPeter Brune    Calling sequence for endhook:
1620baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1621baf369e7SPeter Brune 
1622baf369e7SPeter Brune +  global - global DM
1623baf369e7SPeter Brune -  ctx - optional user-defined function context
1624baf369e7SPeter Brune 
1625baf369e7SPeter Brune    Level: advanced
1626baf369e7SPeter Brune 
1627baf369e7SPeter Brune    Notes:
1628baf369e7SPeter Brune    This function is only needed if auxiliary data needs to be set up on coarse grids.
1629baf369e7SPeter Brune 
1630baf369e7SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
1631baf369e7SPeter Brune 
1632baf369e7SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1633baf369e7SPeter Brune    extract the finest level information from its context (instead of from the SNES).
1634baf369e7SPeter Brune 
1635baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1636baf369e7SPeter Brune @*/
1637baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1638baf369e7SPeter Brune {
1639baf369e7SPeter Brune   PetscErrorCode ierr;
1640baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1641baf369e7SPeter Brune 
1642baf369e7SPeter Brune   PetscFunctionBegin;
1643baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1644baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1645baf369e7SPeter Brune   ierr = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1646baf369e7SPeter Brune   link->beginhook = beginhook;
1647baf369e7SPeter Brune   link->endhook = endhook;
1648baf369e7SPeter Brune   link->ctx = ctx;
1649baf369e7SPeter Brune   link->next = PETSC_NULL;
1650baf369e7SPeter Brune   *p = link;
1651baf369e7SPeter Brune   PetscFunctionReturn(0);
1652baf369e7SPeter Brune }
1653baf369e7SPeter Brune 
1654baf369e7SPeter Brune #undef __FUNCT__
165547c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
165647c6ae99SBarry Smith /*@
165747c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
165847c6ae99SBarry Smith 
165947c6ae99SBarry Smith     Neighbor-wise Collective on DM
166047c6ae99SBarry Smith 
166147c6ae99SBarry Smith     Input Parameters:
166247c6ae99SBarry Smith +   dm - the DM object
166347c6ae99SBarry Smith .   g - the global vector
166447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
166547c6ae99SBarry Smith -   l - the local vector
166647c6ae99SBarry Smith 
166747c6ae99SBarry Smith 
166847c6ae99SBarry Smith     Level: beginner
166947c6ae99SBarry Smith 
1670e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
167147c6ae99SBarry Smith 
167247c6ae99SBarry Smith @*/
16737087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
167447c6ae99SBarry Smith {
16757128ae9fSMatthew G Knepley   PetscSF        sf;
167647c6ae99SBarry Smith   PetscErrorCode ierr;
1677baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
167847c6ae99SBarry Smith 
167947c6ae99SBarry Smith   PetscFunctionBegin;
1680171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1681baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1682baf369e7SPeter Brune     if (link->beginhook) {ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1683baf369e7SPeter Brune   }
16847128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16857128ae9fSMatthew G Knepley   if (sf) {
16867128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
16877128ae9fSMatthew G Knepley 
16887128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16897128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
16907128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
16917128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16927128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
16937128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
16947128ae9fSMatthew G Knepley   } else {
1695843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16967128ae9fSMatthew G Knepley   }
169747c6ae99SBarry Smith   PetscFunctionReturn(0);
169847c6ae99SBarry Smith }
169947c6ae99SBarry Smith 
170047c6ae99SBarry Smith #undef __FUNCT__
170147c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
170247c6ae99SBarry Smith /*@
170347c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
170447c6ae99SBarry Smith 
170547c6ae99SBarry Smith     Neighbor-wise Collective on DM
170647c6ae99SBarry Smith 
170747c6ae99SBarry Smith     Input Parameters:
170847c6ae99SBarry Smith +   dm - the DM object
170947c6ae99SBarry Smith .   g - the global vector
171047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
171147c6ae99SBarry Smith -   l - the local vector
171247c6ae99SBarry Smith 
171347c6ae99SBarry Smith 
171447c6ae99SBarry Smith     Level: beginner
171547c6ae99SBarry Smith 
1716e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
171747c6ae99SBarry Smith 
171847c6ae99SBarry Smith @*/
17197087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
172047c6ae99SBarry Smith {
17217128ae9fSMatthew G Knepley   PetscSF        sf;
172247c6ae99SBarry Smith   PetscErrorCode ierr;
172361a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
1724baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
172547c6ae99SBarry Smith 
172647c6ae99SBarry Smith   PetscFunctionBegin;
1727171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17287128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17297128ae9fSMatthew G Knepley   if (sf) {
17307128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17317128ae9fSMatthew G Knepley 
17327128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17337128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17347128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17357128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17367128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
17377128ae9fSMatthew G Knepley   } else {
1738843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17397128ae9fSMatthew G Knepley   }
1740baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1741baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1742baf369e7SPeter Brune   }
174347c6ae99SBarry Smith   PetscFunctionReturn(0);
174447c6ae99SBarry Smith }
174547c6ae99SBarry Smith 
174647c6ae99SBarry Smith #undef __FUNCT__
17479a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
174847c6ae99SBarry Smith /*@
17499a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
17509a42bb27SBarry Smith 
17519a42bb27SBarry Smith     Neighbor-wise Collective on DM
17529a42bb27SBarry Smith 
17539a42bb27SBarry Smith     Input Parameters:
17549a42bb27SBarry Smith +   dm - the DM object
1755f6813fd5SJed Brown .   l - the local vector
17569a42bb27SBarry 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
17579a42bb27SBarry Smith            base point.
1758f6813fd5SJed Brown - - the global vector
17599a42bb27SBarry Smith 
17609a42bb27SBarry 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
17619a42bb27SBarry 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
17629a42bb27SBarry Smith            global array to the final global array with VecAXPY().
17639a42bb27SBarry Smith 
17649a42bb27SBarry Smith     Level: beginner
17659a42bb27SBarry Smith 
1766e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
17679a42bb27SBarry Smith 
17689a42bb27SBarry Smith @*/
17697087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
17709a42bb27SBarry Smith {
17717128ae9fSMatthew G Knepley   PetscSF        sf;
17729a42bb27SBarry Smith   PetscErrorCode ierr;
17739a42bb27SBarry Smith 
17749a42bb27SBarry Smith   PetscFunctionBegin;
1775171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17767128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17777128ae9fSMatthew G Knepley   if (sf) {
17787128ae9fSMatthew G Knepley     MPI_Op       op;
17797128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
17807128ae9fSMatthew G Knepley 
17817128ae9fSMatthew G Knepley     switch(mode) {
17827128ae9fSMatthew G Knepley     case INSERT_VALUES:
17837128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
17847128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
17857128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
17867128ae9fSMatthew G Knepley #else
17877128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
17887128ae9fSMatthew G Knepley #endif
17897128ae9fSMatthew G Knepley     case ADD_VALUES:
17907128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
17917128ae9fSMatthew G Knepley       op = MPI_SUM; break;
17927128ae9fSMatthew G Knepley   default:
17937128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17947128ae9fSMatthew G Knepley     }
17957128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
17967128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
17977128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
17987128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
17997128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
18007128ae9fSMatthew G Knepley   } else {
1801843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
18027128ae9fSMatthew G Knepley   }
18039a42bb27SBarry Smith   PetscFunctionReturn(0);
18049a42bb27SBarry Smith }
18059a42bb27SBarry Smith 
18069a42bb27SBarry Smith #undef __FUNCT__
18079a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
18089a42bb27SBarry Smith /*@
18099a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
181047c6ae99SBarry Smith 
181147c6ae99SBarry Smith     Neighbor-wise Collective on DM
181247c6ae99SBarry Smith 
181347c6ae99SBarry Smith     Input Parameters:
181447c6ae99SBarry Smith +   dm - the DM object
1815f6813fd5SJed Brown .   l - the local vector
181647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1817f6813fd5SJed Brown -   g - the global vector
181847c6ae99SBarry Smith 
181947c6ae99SBarry Smith 
182047c6ae99SBarry Smith     Level: beginner
182147c6ae99SBarry Smith 
1822e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
182347c6ae99SBarry Smith 
182447c6ae99SBarry Smith @*/
18257087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
182647c6ae99SBarry Smith {
18277128ae9fSMatthew G Knepley   PetscSF        sf;
182847c6ae99SBarry Smith   PetscErrorCode ierr;
182947c6ae99SBarry Smith 
183047c6ae99SBarry Smith   PetscFunctionBegin;
1831171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18327128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18337128ae9fSMatthew G Knepley   if (sf) {
18347128ae9fSMatthew G Knepley     MPI_Op       op;
18357128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
18367128ae9fSMatthew G Knepley 
18377128ae9fSMatthew G Knepley     switch(mode) {
18387128ae9fSMatthew G Knepley     case INSERT_VALUES:
18397128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
18407128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
18417128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
18427128ae9fSMatthew G Knepley #else
18437128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
18447128ae9fSMatthew G Knepley #endif
18457128ae9fSMatthew G Knepley     case ADD_VALUES:
18467128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
18477128ae9fSMatthew G Knepley       op = MPI_SUM; break;
18487128ae9fSMatthew G Knepley     default:
18497128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18507128ae9fSMatthew G Knepley     }
18517128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
18527128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
18537128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
18547128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
18557128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
18567128ae9fSMatthew G Knepley   } else {
1857843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
18587128ae9fSMatthew G Knepley   }
185947c6ae99SBarry Smith   PetscFunctionReturn(0);
186047c6ae99SBarry Smith }
186147c6ae99SBarry Smith 
186247c6ae99SBarry Smith #undef __FUNCT__
186347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
186447c6ae99SBarry Smith /*@
186547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
186647c6ae99SBarry Smith 
186747c6ae99SBarry Smith     Collective on DM
186847c6ae99SBarry Smith 
186947c6ae99SBarry Smith     Input Parameter:
187047c6ae99SBarry Smith +   dm - the DM object
187191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
187247c6ae99SBarry Smith 
187347c6ae99SBarry Smith     Output Parameter:
187447c6ae99SBarry Smith .   dmc - the coarsened DM
187547c6ae99SBarry Smith 
187647c6ae99SBarry Smith     Level: developer
187747c6ae99SBarry Smith 
1878e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
187947c6ae99SBarry Smith 
188047c6ae99SBarry Smith @*/
18817087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
188247c6ae99SBarry Smith {
188347c6ae99SBarry Smith   PetscErrorCode ierr;
1884b17ce1afSJed Brown   DMCoarsenHookLink link;
188547c6ae99SBarry Smith 
188647c6ae99SBarry Smith   PetscFunctionBegin;
1887171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
188847c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
188943842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
18908cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1891644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
18920598a293SJed Brown   (*dmc)->levelup   = dm->levelup;
1893656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1894e4b4b23bSJed Brown   ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
1895b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1896b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1897b17ce1afSJed Brown   }
1898b17ce1afSJed Brown   PetscFunctionReturn(0);
1899b17ce1afSJed Brown }
1900b17ce1afSJed Brown 
1901b17ce1afSJed Brown #undef __FUNCT__
1902b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1903b17ce1afSJed Brown /*@
1904b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1905b17ce1afSJed Brown 
1906b17ce1afSJed Brown    Logically Collective
1907b17ce1afSJed Brown 
1908b17ce1afSJed Brown    Input Arguments:
1909b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1910b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1911b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1912b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1913b17ce1afSJed Brown 
1914b17ce1afSJed Brown    Calling sequence of coarsenhook:
1915b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1916b17ce1afSJed Brown 
1917b17ce1afSJed Brown +  fine - fine level DM
1918b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1919b17ce1afSJed Brown -  ctx - optional user-defined function context
1920b17ce1afSJed Brown 
1921b17ce1afSJed Brown    Calling sequence for restricthook:
1922c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1923b17ce1afSJed Brown 
1924b17ce1afSJed Brown +  fine - fine level DM
1925b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1926c833c3b5SJed Brown .  rscale - scaling vector for restriction
1927c833c3b5SJed Brown .  inject - matrix restricting by injection
1928b17ce1afSJed Brown .  coarse - coarse level DM to update
1929b17ce1afSJed Brown -  ctx - optional user-defined function context
1930b17ce1afSJed Brown 
1931b17ce1afSJed Brown    Level: advanced
1932b17ce1afSJed Brown 
1933b17ce1afSJed Brown    Notes:
1934b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1935b17ce1afSJed Brown 
1936b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1937b17ce1afSJed Brown 
1938b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1939b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1940b17ce1afSJed Brown 
1941c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1942b17ce1afSJed Brown @*/
1943b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1944b17ce1afSJed Brown {
1945b17ce1afSJed Brown   PetscErrorCode ierr;
1946b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1947b17ce1afSJed Brown 
1948b17ce1afSJed Brown   PetscFunctionBegin;
1949b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
19506bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1951b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1952b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1953b17ce1afSJed Brown   link->restricthook = restricthook;
1954b17ce1afSJed Brown   link->ctx = ctx;
19556cab3a1bSJed Brown   link->next = PETSC_NULL;
1956b17ce1afSJed Brown   *p = link;
1957b17ce1afSJed Brown   PetscFunctionReturn(0);
1958b17ce1afSJed Brown }
1959b17ce1afSJed Brown 
1960b17ce1afSJed Brown #undef __FUNCT__
1961b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1962b17ce1afSJed Brown /*@
1963b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1964b17ce1afSJed Brown 
1965b17ce1afSJed Brown    Collective if any hooks are
1966b17ce1afSJed Brown 
1967b17ce1afSJed Brown    Input Arguments:
1968b17ce1afSJed Brown +  fine - finer DM to use as a base
1969b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1970b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1971b17ce1afSJed Brown -  coarse - coarer DM to update
1972b17ce1afSJed Brown 
1973b17ce1afSJed Brown    Level: developer
1974b17ce1afSJed Brown 
1975b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1976b17ce1afSJed Brown @*/
1977b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1978b17ce1afSJed Brown {
1979b17ce1afSJed Brown   PetscErrorCode ierr;
1980b17ce1afSJed Brown   DMCoarsenHookLink link;
1981b17ce1afSJed Brown 
1982b17ce1afSJed Brown   PetscFunctionBegin;
1983b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1984b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1985b17ce1afSJed Brown   }
198647c6ae99SBarry Smith   PetscFunctionReturn(0);
198747c6ae99SBarry Smith }
198847c6ae99SBarry Smith 
198947c6ae99SBarry Smith #undef __FUNCT__
1990be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
19915dbd56e3SPeter Brune /*@
1992be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
19935dbd56e3SPeter Brune 
19945dbd56e3SPeter Brune    Logically Collective
19955dbd56e3SPeter Brune 
19965dbd56e3SPeter Brune    Input Arguments:
19975dbd56e3SPeter Brune +  global - global DM
1998be081cd6SPeter Brune .
19995dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
20005dbd56e3SPeter Brune -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
20015dbd56e3SPeter Brune 
20025dbd56e3SPeter Brune    Calling sequence for restricthook:
20035dbd56e3SPeter Brune $    restricthook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
20045dbd56e3SPeter Brune 
20055dbd56e3SPeter Brune +  global - global DM
20065dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
20075dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
20085dbd56e3SPeter Brune .  block  - block DM (may just be a shell if the global DM is passed in correctly)
20095dbd56e3SPeter Brune -  ctx - optional user-defined function context
20105dbd56e3SPeter Brune 
20115dbd56e3SPeter Brune    Level: advanced
20125dbd56e3SPeter Brune 
20135dbd56e3SPeter Brune    Notes:
20145dbd56e3SPeter Brune    This function is only needed if auxiliary data needs to be set up on coarse grids.
20155dbd56e3SPeter Brune 
20165dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
20175dbd56e3SPeter Brune 
20185dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
20195dbd56e3SPeter Brune    extract the finest level information from its context (instead of from the SNES).
20205dbd56e3SPeter Brune 
20215dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
20225dbd56e3SPeter Brune @*/
2023be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
20245dbd56e3SPeter Brune {
20255dbd56e3SPeter Brune   PetscErrorCode ierr;
2026be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
20275dbd56e3SPeter Brune 
20285dbd56e3SPeter Brune   PetscFunctionBegin;
20295dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2030be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2031be081cd6SPeter Brune   ierr = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
20325dbd56e3SPeter Brune   link->restricthook = restricthook;
2033be081cd6SPeter Brune   link->ddhook = ddhook;
20345dbd56e3SPeter Brune   link->ctx = ctx;
20355dbd56e3SPeter Brune   link->next = PETSC_NULL;
20365dbd56e3SPeter Brune   *p = link;
20375dbd56e3SPeter Brune   PetscFunctionReturn(0);
20385dbd56e3SPeter Brune }
20395dbd56e3SPeter Brune 
20405dbd56e3SPeter Brune #undef __FUNCT__
2041be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
20425dbd56e3SPeter Brune /*@
2043be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
20445dbd56e3SPeter Brune 
20455dbd56e3SPeter Brune    Collective if any hooks are
20465dbd56e3SPeter Brune 
20475dbd56e3SPeter Brune    Input Arguments:
20485dbd56e3SPeter Brune +  fine - finer DM to use as a base
2049be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2050be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
20515dbd56e3SPeter Brune -  coarse - coarer DM to update
20525dbd56e3SPeter Brune 
20535dbd56e3SPeter Brune    Level: developer
20545dbd56e3SPeter Brune 
20555dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
20565dbd56e3SPeter Brune @*/
2057be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
20585dbd56e3SPeter Brune {
20595dbd56e3SPeter Brune   PetscErrorCode ierr;
2060be081cd6SPeter Brune   DMSubDomainHookLink link;
20615dbd56e3SPeter Brune 
20625dbd56e3SPeter Brune   PetscFunctionBegin;
2063be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
2064be081cd6SPeter Brune     if (link->restricthook) {ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);}
20655dbd56e3SPeter Brune   }
20665dbd56e3SPeter Brune   PetscFunctionReturn(0);
20675dbd56e3SPeter Brune }
20685dbd56e3SPeter Brune 
20695dbd56e3SPeter Brune #undef __FUNCT__
20705fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
20715fe1f584SPeter Brune /*@
20726a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
20735fe1f584SPeter Brune 
20745fe1f584SPeter Brune     Not Collective
20755fe1f584SPeter Brune 
20765fe1f584SPeter Brune     Input Parameter:
20775fe1f584SPeter Brune .   dm - the DM object
20785fe1f584SPeter Brune 
20795fe1f584SPeter Brune     Output Parameter:
20806a7d9d85SPeter Brune .   level - number of coarsenings
20815fe1f584SPeter Brune 
20825fe1f584SPeter Brune     Level: developer
20835fe1f584SPeter Brune 
20846a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
20855fe1f584SPeter Brune 
20865fe1f584SPeter Brune @*/
20875fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
20885fe1f584SPeter Brune {
20895fe1f584SPeter Brune   PetscFunctionBegin;
20905fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20915fe1f584SPeter Brune   *level = dm->leveldown;
20925fe1f584SPeter Brune   PetscFunctionReturn(0);
20935fe1f584SPeter Brune }
20945fe1f584SPeter Brune 
20955fe1f584SPeter Brune 
20965fe1f584SPeter Brune 
20975fe1f584SPeter Brune #undef __FUNCT__
209847c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
209947c6ae99SBarry Smith /*@C
210047c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
210147c6ae99SBarry Smith 
210247c6ae99SBarry Smith     Collective on DM
210347c6ae99SBarry Smith 
210447c6ae99SBarry Smith     Input Parameter:
210547c6ae99SBarry Smith +   dm - the DM object
210647c6ae99SBarry Smith -   nlevels - the number of levels of refinement
210747c6ae99SBarry Smith 
210847c6ae99SBarry Smith     Output Parameter:
210947c6ae99SBarry Smith .   dmf - the refined DM hierarchy
211047c6ae99SBarry Smith 
211147c6ae99SBarry Smith     Level: developer
211247c6ae99SBarry Smith 
2113e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
211447c6ae99SBarry Smith 
211547c6ae99SBarry Smith @*/
21167087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
211747c6ae99SBarry Smith {
211847c6ae99SBarry Smith   PetscErrorCode ierr;
211947c6ae99SBarry Smith 
212047c6ae99SBarry Smith   PetscFunctionBegin;
2121171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
212247c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
212347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
212447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
212547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
212647c6ae99SBarry Smith   } else if (dm->ops->refine) {
212747c6ae99SBarry Smith     PetscInt i;
212847c6ae99SBarry Smith 
212947c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
213047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
213147c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
213247c6ae99SBarry Smith     }
2133*8cbdbec6SBarry Smith   } else SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
213447c6ae99SBarry Smith   PetscFunctionReturn(0);
213547c6ae99SBarry Smith }
213647c6ae99SBarry Smith 
213747c6ae99SBarry Smith #undef __FUNCT__
213847c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
213947c6ae99SBarry Smith /*@C
214047c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
214147c6ae99SBarry Smith 
214247c6ae99SBarry Smith     Collective on DM
214347c6ae99SBarry Smith 
214447c6ae99SBarry Smith     Input Parameter:
214547c6ae99SBarry Smith +   dm - the DM object
214647c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
214747c6ae99SBarry Smith 
214847c6ae99SBarry Smith     Output Parameter:
214947c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
215047c6ae99SBarry Smith 
215147c6ae99SBarry Smith     Level: developer
215247c6ae99SBarry Smith 
2153e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
215447c6ae99SBarry Smith 
215547c6ae99SBarry Smith @*/
21567087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
215747c6ae99SBarry Smith {
215847c6ae99SBarry Smith   PetscErrorCode ierr;
215947c6ae99SBarry Smith 
216047c6ae99SBarry Smith   PetscFunctionBegin;
2161171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
216247c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
216347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
216447c6ae99SBarry Smith   PetscValidPointer(dmc,3);
216547c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
216647c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
216747c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
216847c6ae99SBarry Smith     PetscInt i;
216947c6ae99SBarry Smith 
217047c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
217147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
217247c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
217347c6ae99SBarry Smith     }
2174*8cbdbec6SBarry Smith   } else SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
217547c6ae99SBarry Smith   PetscFunctionReturn(0);
217647c6ae99SBarry Smith }
217747c6ae99SBarry Smith 
217847c6ae99SBarry Smith #undef __FUNCT__
2179e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
218047c6ae99SBarry Smith /*@
2181e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
218247c6ae99SBarry Smith    grids associated with two DMs.
218347c6ae99SBarry Smith 
218447c6ae99SBarry Smith    Collective on DM
218547c6ae99SBarry Smith 
218647c6ae99SBarry Smith    Input Parameters:
218747c6ae99SBarry Smith +  dmc - the coarse grid DM
218847c6ae99SBarry Smith -  dmf - the fine grid DM
218947c6ae99SBarry Smith 
219047c6ae99SBarry Smith    Output Parameters:
219147c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
219247c6ae99SBarry Smith 
219347c6ae99SBarry Smith    Level: intermediate
219447c6ae99SBarry Smith 
219547c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
219647c6ae99SBarry Smith 
2197e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
219847c6ae99SBarry Smith @*/
2199e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
220047c6ae99SBarry Smith {
220147c6ae99SBarry Smith   PetscErrorCode ierr;
220247c6ae99SBarry Smith 
220347c6ae99SBarry Smith   PetscFunctionBegin;
2204171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2205171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
220647c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
220747c6ae99SBarry Smith   PetscFunctionReturn(0);
220847c6ae99SBarry Smith }
220947c6ae99SBarry Smith 
221047c6ae99SBarry Smith #undef __FUNCT__
22111a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
22121a266240SBarry Smith /*@C
22131a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
22141a266240SBarry Smith 
22151a266240SBarry Smith     Not Collective
22161a266240SBarry Smith 
22171a266240SBarry Smith     Input Parameters:
22181a266240SBarry Smith +   dm - the DM object
22191a266240SBarry Smith -   destroy - the destroy function
22201a266240SBarry Smith 
22211a266240SBarry Smith     Level: intermediate
22221a266240SBarry Smith 
2223e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
22241a266240SBarry Smith 
2225f07f9ceaSJed Brown @*/
22261a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
22271a266240SBarry Smith {
22281a266240SBarry Smith   PetscFunctionBegin;
2229171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22301a266240SBarry Smith   dm->ctxdestroy = destroy;
22311a266240SBarry Smith   PetscFunctionReturn(0);
22321a266240SBarry Smith }
22331a266240SBarry Smith 
22341a266240SBarry Smith #undef __FUNCT__
22351b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2236b07ff414SBarry Smith /*@
22371b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
223847c6ae99SBarry Smith 
223947c6ae99SBarry Smith     Not Collective
224047c6ae99SBarry Smith 
224147c6ae99SBarry Smith     Input Parameters:
224247c6ae99SBarry Smith +   dm - the DM object
224347c6ae99SBarry Smith -   ctx - the user context
224447c6ae99SBarry Smith 
224547c6ae99SBarry Smith     Level: intermediate
224647c6ae99SBarry Smith 
2247e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
224847c6ae99SBarry Smith 
224947c6ae99SBarry Smith @*/
22501b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
225147c6ae99SBarry Smith {
225247c6ae99SBarry Smith   PetscFunctionBegin;
2253171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
225447c6ae99SBarry Smith   dm->ctx = ctx;
225547c6ae99SBarry Smith   PetscFunctionReturn(0);
225647c6ae99SBarry Smith }
225747c6ae99SBarry Smith 
225847c6ae99SBarry Smith #undef __FUNCT__
22591b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
226047c6ae99SBarry Smith /*@
22611b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
226247c6ae99SBarry Smith 
226347c6ae99SBarry Smith     Not Collective
226447c6ae99SBarry Smith 
226547c6ae99SBarry Smith     Input Parameter:
226647c6ae99SBarry Smith .   dm - the DM object
226747c6ae99SBarry Smith 
226847c6ae99SBarry Smith     Output Parameter:
226947c6ae99SBarry Smith .   ctx - the user context
227047c6ae99SBarry Smith 
227147c6ae99SBarry Smith     Level: intermediate
227247c6ae99SBarry Smith 
2273e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
227447c6ae99SBarry Smith 
227547c6ae99SBarry Smith @*/
22761b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
227747c6ae99SBarry Smith {
227847c6ae99SBarry Smith   PetscFunctionBegin;
2279171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22801b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
228147c6ae99SBarry Smith   PetscFunctionReturn(0);
228247c6ae99SBarry Smith }
228347c6ae99SBarry Smith 
228447c6ae99SBarry Smith #undef __FUNCT__
228508da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
228608da532bSDmitry Karpeev /*@C
228708da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
228808da532bSDmitry Karpeev 
228908da532bSDmitry Karpeev     Logically Collective on DM
229008da532bSDmitry Karpeev 
229108da532bSDmitry Karpeev     Input Parameter:
229208da532bSDmitry Karpeev +   dm - the DM object
229308da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
229408da532bSDmitry Karpeev 
229508da532bSDmitry Karpeev     Level: intermediate
229608da532bSDmitry Karpeev 
2297835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
229808da532bSDmitry Karpeev          DMSetJacobian()
229908da532bSDmitry Karpeev 
230008da532bSDmitry Karpeev @*/
230108da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
230208da532bSDmitry Karpeev {
230308da532bSDmitry Karpeev   PetscFunctionBegin;
230408da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
230508da532bSDmitry Karpeev   PetscFunctionReturn(0);
230608da532bSDmitry Karpeev }
230708da532bSDmitry Karpeev 
230808da532bSDmitry Karpeev #undef __FUNCT__
230908da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
231008da532bSDmitry Karpeev /*@
231108da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
231208da532bSDmitry Karpeev 
231308da532bSDmitry Karpeev     Not Collective
231408da532bSDmitry Karpeev 
231508da532bSDmitry Karpeev     Input Parameter:
231608da532bSDmitry Karpeev .   dm - the DM object to destroy
231708da532bSDmitry Karpeev 
231808da532bSDmitry Karpeev     Output Parameter:
231908da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
232008da532bSDmitry Karpeev 
232108da532bSDmitry Karpeev     Level: developer
232208da532bSDmitry Karpeev 
232374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
232408da532bSDmitry Karpeev 
232508da532bSDmitry Karpeev @*/
232608da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
232708da532bSDmitry Karpeev {
232808da532bSDmitry Karpeev   PetscFunctionBegin;
232908da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
233008da532bSDmitry Karpeev   PetscFunctionReturn(0);
233108da532bSDmitry Karpeev }
233208da532bSDmitry Karpeev 
233308da532bSDmitry Karpeev #undef __FUNCT__
233408da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
233508da532bSDmitry Karpeev /*@C
233608da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
233708da532bSDmitry Karpeev 
233808da532bSDmitry Karpeev     Logically Collective on DM
233908da532bSDmitry Karpeev 
234008da532bSDmitry Karpeev     Input Parameters:
234108da532bSDmitry Karpeev +   dm - the DM object to destroy
234208da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
234308da532bSDmitry Karpeev 
234408da532bSDmitry Karpeev     Output parameters:
234508da532bSDmitry Karpeev +   xl - lower bound
234608da532bSDmitry Karpeev -   xu - upper bound
234708da532bSDmitry Karpeev 
234808da532bSDmitry Karpeev     Level: intermediate
234908da532bSDmitry Karpeev 
235074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
235108da532bSDmitry Karpeev 
235208da532bSDmitry Karpeev @*/
235308da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
235408da532bSDmitry Karpeev {
235508da532bSDmitry Karpeev   PetscErrorCode ierr;
235608da532bSDmitry Karpeev   PetscFunctionBegin;
235708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
235808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
235908da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
236008da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
236108da532bSDmitry Karpeev   }
2362a201590fSDmitry Karpeev   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
236308da532bSDmitry Karpeev   PetscFunctionReturn(0);
236408da532bSDmitry Karpeev }
236508da532bSDmitry Karpeev 
236608da532bSDmitry Karpeev #undef __FUNCT__
2367b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2368b0ae01b7SPeter Brune /*@
2369b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2370b0ae01b7SPeter Brune 
2371b0ae01b7SPeter Brune     Not Collective
2372b0ae01b7SPeter Brune 
2373b0ae01b7SPeter Brune     Input Parameter:
2374b0ae01b7SPeter Brune .   dm - the DM object
2375b0ae01b7SPeter Brune 
2376b0ae01b7SPeter Brune     Output Parameter:
2377b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2378b0ae01b7SPeter Brune 
2379b0ae01b7SPeter Brune     Level: developer
2380b0ae01b7SPeter Brune 
2381b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2382b0ae01b7SPeter Brune 
2383b0ae01b7SPeter Brune @*/
2384b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2385b0ae01b7SPeter Brune {
2386b0ae01b7SPeter Brune   PetscFunctionBegin;
2387b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2388b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2389b0ae01b7SPeter Brune }
2390b0ae01b7SPeter Brune 
2391b0ae01b7SPeter Brune #undef  __FUNCT__
239208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2393748fac09SDmitry Karpeev /*@C
239408da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
239508da532bSDmitry Karpeev 
239608da532bSDmitry Karpeev     Collective on DM
239708da532bSDmitry Karpeev 
239808da532bSDmitry Karpeev     Input Parameter:
239908da532bSDmitry Karpeev +   dm - the DM object
2400e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
240108da532bSDmitry Karpeev 
240208da532bSDmitry Karpeev     Level: developer
240308da532bSDmitry Karpeev 
240474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
240508da532bSDmitry Karpeev 
240608da532bSDmitry Karpeev @*/
240708da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
240808da532bSDmitry Karpeev {
240908da532bSDmitry Karpeev   PetscErrorCode ierr;
241008da532bSDmitry Karpeev   PetscFunctionBegin;
241108da532bSDmitry Karpeev   if (x) {
241208da532bSDmitry Karpeev     if (!dm->x) {
241308da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
241408da532bSDmitry Karpeev     }
241508da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
241608da532bSDmitry Karpeev   }
241708da532bSDmitry Karpeev   else if (dm->x) {
241808da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
241908da532bSDmitry Karpeev   }
242008da532bSDmitry Karpeev   PetscFunctionReturn(0);
242108da532bSDmitry Karpeev }
242208da532bSDmitry Karpeev 
2423264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2424264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2425264ace61SBarry Smith 
2426264ace61SBarry Smith #undef __FUNCT__
2427264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2428264ace61SBarry Smith /*@C
2429264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2430264ace61SBarry Smith 
2431264ace61SBarry Smith   Collective on DM
2432264ace61SBarry Smith 
2433264ace61SBarry Smith   Input Parameters:
2434264ace61SBarry Smith + dm     - The DM object
2435264ace61SBarry Smith - method - The name of the DM type
2436264ace61SBarry Smith 
2437264ace61SBarry Smith   Options Database Key:
2438264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2439264ace61SBarry Smith 
2440264ace61SBarry Smith   Notes:
2441e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2442264ace61SBarry Smith 
2443264ace61SBarry Smith   Level: intermediate
2444264ace61SBarry Smith 
2445264ace61SBarry Smith .keywords: DM, set, type
2446264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2447264ace61SBarry Smith @*/
244819fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2449264ace61SBarry Smith {
2450264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2451264ace61SBarry Smith   PetscBool      match;
2452264ace61SBarry Smith   PetscErrorCode ierr;
2453264ace61SBarry Smith 
2454264ace61SBarry Smith   PetscFunctionBegin;
2455264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2456251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2457264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2458264ace61SBarry Smith 
2459264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
24604b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
246132c0f0efSBarry Smith   if (!r) SETERRQ1(((PetscObject)dm)->comm,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2462264ace61SBarry Smith 
2463264ace61SBarry Smith   if (dm->ops->destroy) {
2464264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2465b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2466264ace61SBarry Smith   }
2467264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2468264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2469264ace61SBarry Smith   PetscFunctionReturn(0);
2470264ace61SBarry Smith }
2471264ace61SBarry Smith 
2472264ace61SBarry Smith #undef __FUNCT__
2473264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2474264ace61SBarry Smith /*@C
2475264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2476264ace61SBarry Smith 
2477264ace61SBarry Smith   Not Collective
2478264ace61SBarry Smith 
2479264ace61SBarry Smith   Input Parameter:
2480264ace61SBarry Smith . dm  - The DM
2481264ace61SBarry Smith 
2482264ace61SBarry Smith   Output Parameter:
2483264ace61SBarry Smith . type - The DM type name
2484264ace61SBarry Smith 
2485264ace61SBarry Smith   Level: intermediate
2486264ace61SBarry Smith 
2487264ace61SBarry Smith .keywords: DM, get, type, name
2488264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2489264ace61SBarry Smith @*/
249019fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2491264ace61SBarry Smith {
2492264ace61SBarry Smith   PetscErrorCode ierr;
2493264ace61SBarry Smith 
2494264ace61SBarry Smith   PetscFunctionBegin;
2495264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2496264ace61SBarry Smith   PetscValidCharPointer(type,2);
2497264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2498264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2499264ace61SBarry Smith   }
2500264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2501264ace61SBarry Smith   PetscFunctionReturn(0);
2502264ace61SBarry Smith }
2503264ace61SBarry Smith 
250467a56275SMatthew G Knepley #undef __FUNCT__
250567a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
250667a56275SMatthew G Knepley /*@C
250767a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
250867a56275SMatthew G Knepley 
250967a56275SMatthew G Knepley   Collective on DM
251067a56275SMatthew G Knepley 
251167a56275SMatthew G Knepley   Input Parameters:
251267a56275SMatthew G Knepley + dm - the DM
251367a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
251467a56275SMatthew G Knepley 
251567a56275SMatthew G Knepley   Output Parameter:
251667a56275SMatthew G Knepley . M - pointer to new DM
251767a56275SMatthew G Knepley 
251867a56275SMatthew G Knepley   Notes:
251967a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
252067a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
252167a56275SMatthew G Knepley   of the input DM.
252267a56275SMatthew G Knepley 
252367a56275SMatthew G Knepley   Level: intermediate
252467a56275SMatthew G Knepley 
252567a56275SMatthew G Knepley .seealso: DMCreate()
252667a56275SMatthew G Knepley @*/
252719fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
252867a56275SMatthew G Knepley {
252967a56275SMatthew G Knepley   DM             B;
253067a56275SMatthew G Knepley   char           convname[256];
253167a56275SMatthew G Knepley   PetscBool      sametype, issame;
253267a56275SMatthew G Knepley   PetscErrorCode ierr;
253367a56275SMatthew G Knepley 
253467a56275SMatthew G Knepley   PetscFunctionBegin;
253567a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
253667a56275SMatthew G Knepley   PetscValidType(dm,1);
253767a56275SMatthew G Knepley   PetscValidPointer(M,3);
2538251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
253967a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
254067a56275SMatthew G Knepley   {
254119fd82e9SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = PETSC_NULL;
254267a56275SMatthew G Knepley 
254367a56275SMatthew G Knepley     /*
254467a56275SMatthew G Knepley        Order of precedence:
254567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
254667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
254767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
254867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
254967a56275SMatthew G Knepley        5) Use a really basic converter.
255067a56275SMatthew G Knepley     */
255167a56275SMatthew G Knepley 
255267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
255367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
255467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
255567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
255667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
255767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
255867a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
255967a56275SMatthew G Knepley     if (conv) goto foundconv;
256067a56275SMatthew G Knepley 
256167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
256267a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
256367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
256467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
256567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
256667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
256767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
256867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
256967a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
257067a56275SMatthew G Knepley     if (conv) {
2571fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
257267a56275SMatthew G Knepley       goto foundconv;
257367a56275SMatthew G Knepley     }
257467a56275SMatthew G Knepley 
257567a56275SMatthew G Knepley #if 0
257667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
257767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2578fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
257967a56275SMatthew G Knepley     if (conv) goto foundconv;
258067a56275SMatthew G Knepley 
258167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
258267a56275SMatthew G Knepley     if (dm->ops->convert) {
258367a56275SMatthew G Knepley       conv = dm->ops->convert;
258467a56275SMatthew G Knepley     }
258567a56275SMatthew G Knepley     if (conv) goto foundconv;
258667a56275SMatthew G Knepley #endif
258767a56275SMatthew G Knepley 
258867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
258967a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
259067a56275SMatthew G Knepley 
259167a56275SMatthew G Knepley     foundconv:
259267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
259367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
259467a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
259567a56275SMatthew G Knepley   }
259667a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
259767a56275SMatthew G Knepley   PetscFunctionReturn(0);
259867a56275SMatthew G Knepley }
2599264ace61SBarry Smith 
2600264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2601264ace61SBarry Smith 
2602264ace61SBarry Smith #undef __FUNCT__
2603264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2604264ace61SBarry Smith /*@C
2605264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2606264ace61SBarry Smith 
2607264ace61SBarry Smith   Level: advanced
2608264ace61SBarry Smith @*/
26097087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2610264ace61SBarry Smith {
2611264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2612264ace61SBarry Smith   PetscErrorCode ierr;
2613264ace61SBarry Smith 
2614264ace61SBarry Smith   PetscFunctionBegin;
2615264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2616264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2617264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2618264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2619264ace61SBarry Smith   PetscFunctionReturn(0);
2620264ace61SBarry Smith }
2621264ace61SBarry Smith 
2622264ace61SBarry Smith 
2623264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2624264ace61SBarry Smith #undef __FUNCT__
2625264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2626264ace61SBarry Smith /*@C
2627264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2628264ace61SBarry Smith 
2629264ace61SBarry Smith    Not Collective
2630264ace61SBarry Smith 
2631264ace61SBarry Smith    Level: advanced
2632264ace61SBarry Smith 
2633264ace61SBarry Smith .keywords: DM, register, destroy
2634264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2635264ace61SBarry Smith @*/
26367087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2637264ace61SBarry Smith {
2638264ace61SBarry Smith   PetscErrorCode ierr;
2639264ace61SBarry Smith 
2640264ace61SBarry Smith   PetscFunctionBegin;
2641264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2642264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2643264ace61SBarry Smith   PetscFunctionReturn(0);
2644264ace61SBarry Smith }
264523f975d1SBarry Smith 
2646b859378eSBarry Smith #undef __FUNCT__
2647b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2648b859378eSBarry Smith /*@C
264955849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2650b859378eSBarry Smith 
2651b859378eSBarry Smith   Collective on PetscViewer
2652b859378eSBarry Smith 
2653b859378eSBarry Smith   Input Parameters:
2654b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2655b859378eSBarry Smith            some related function before a call to DMLoad().
2656b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2657b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2658b859378eSBarry Smith 
2659b859378eSBarry Smith    Level: intermediate
2660b859378eSBarry Smith 
2661b859378eSBarry Smith   Notes:
266255849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2663b859378eSBarry Smith 
2664b859378eSBarry Smith   Notes for advanced users:
2665b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2666b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2667b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2668b859378eSBarry Smith   format is
2669b859378eSBarry Smith .vb
2670b859378eSBarry Smith      has not yet been determined
2671b859378eSBarry Smith .ve
2672b859378eSBarry Smith 
2673b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2674b859378eSBarry Smith @*/
2675b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2676b859378eSBarry Smith {
2677b859378eSBarry Smith   PetscErrorCode ierr;
267832c0f0efSBarry Smith   PetscBool      isbinary;
267955849f57SBarry Smith   PetscInt       classid;
268032c0f0efSBarry Smith   char           type[256];
2681b859378eSBarry Smith 
2682b859378eSBarry Smith   PetscFunctionBegin;
2683b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2684b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
268532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
268632c0f0efSBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
2687b859378eSBarry Smith 
268832c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
268932c0f0efSBarry Smith   if (classid != DM_FILE_CLASSID) SETERRQ(((PetscObject)newdm)->comm,PETSC_ERR_ARG_WRONG,"Not DM next in file");
269032c0f0efSBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
269132c0f0efSBarry Smith   ierr = DMSetType(newdm, type);CHKERRQ(ierr);
26922d53ad75SBarry Smith   if (newdm->ops->load) {
2693b859378eSBarry Smith     ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
26942d53ad75SBarry Smith   }
2695b859378eSBarry Smith   PetscFunctionReturn(0);
2696b859378eSBarry Smith }
2697b859378eSBarry Smith 
26987da65231SMatthew G Knepley /******************************** FEM Support **********************************/
26997da65231SMatthew G Knepley 
27007da65231SMatthew G Knepley #undef __FUNCT__
27017da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
27027da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
27031d47ebbbSSatish Balay   PetscInt       f;
27041b30c384SMatthew G Knepley   PetscErrorCode ierr;
27051b30c384SMatthew G Knepley 
27067da65231SMatthew G Knepley   PetscFunctionBegin;
270774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27081d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
270974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
27107da65231SMatthew G Knepley   }
27117da65231SMatthew G Knepley   PetscFunctionReturn(0);
27127da65231SMatthew G Knepley }
27137da65231SMatthew G Knepley 
27147da65231SMatthew G Knepley #undef __FUNCT__
27157da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
27167da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
27171b30c384SMatthew G Knepley   PetscInt       f, g;
27187da65231SMatthew G Knepley   PetscErrorCode ierr;
27197da65231SMatthew G Knepley 
27207da65231SMatthew G Knepley   PetscFunctionBegin;
272174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
27221d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
272374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
27241d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
272574778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
27267da65231SMatthew G Knepley     }
272774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
27287da65231SMatthew G Knepley   }
27297da65231SMatthew G Knepley   PetscFunctionReturn(0);
27307da65231SMatthew G Knepley }
2731e7c4fc90SDmitry Karpeev 
2732970e74d5SMatthew G Knepley #undef __FUNCT__
273388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
273488ed4aceSMatthew G Knepley /*@
273588ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
273688ed4aceSMatthew G Knepley 
273788ed4aceSMatthew G Knepley   Input Parameter:
273888ed4aceSMatthew G Knepley . dm - The DM
273988ed4aceSMatthew G Knepley 
274088ed4aceSMatthew G Knepley   Output Parameter:
274188ed4aceSMatthew G Knepley . section - The PetscSection
274288ed4aceSMatthew G Knepley 
274388ed4aceSMatthew G Knepley   Level: intermediate
274488ed4aceSMatthew G Knepley 
274588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
274688ed4aceSMatthew G Knepley 
274788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
274888ed4aceSMatthew G Knepley @*/
274988ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
275088ed4aceSMatthew G Knepley   PetscFunctionBegin;
275188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
275288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
275388ed4aceSMatthew G Knepley   *section = dm->defaultSection;
275488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
275588ed4aceSMatthew G Knepley }
275688ed4aceSMatthew G Knepley 
275788ed4aceSMatthew G Knepley #undef __FUNCT__
275888ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
275988ed4aceSMatthew G Knepley /*@
276088ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
276188ed4aceSMatthew G Knepley 
276288ed4aceSMatthew G Knepley   Input Parameters:
276388ed4aceSMatthew G Knepley + dm - The DM
276488ed4aceSMatthew G Knepley - section - The PetscSection
276588ed4aceSMatthew G Knepley 
276688ed4aceSMatthew G Knepley   Level: intermediate
276788ed4aceSMatthew G Knepley 
276888ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
276988ed4aceSMatthew G Knepley 
277088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
277188ed4aceSMatthew G Knepley @*/
277288ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
2773af122d2aSMatthew G Knepley   PetscInt       numFields;
2774af122d2aSMatthew G Knepley   PetscInt       f;
277588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
277688ed4aceSMatthew G Knepley 
277788ed4aceSMatthew G Knepley   PetscFunctionBegin;
277888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
277988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
278088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
278188ed4aceSMatthew G Knepley   dm->defaultSection = section;
2782af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
2783af122d2aSMatthew G Knepley   if (numFields) {
2784af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
2785af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
2786af122d2aSMatthew G Knepley       const char *name;
2787af122d2aSMatthew G Knepley 
2788af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
2789af122d2aSMatthew G Knepley       ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr);
2790af122d2aSMatthew G Knepley     }
2791af122d2aSMatthew G Knepley   }
279288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
279388ed4aceSMatthew G Knepley }
279488ed4aceSMatthew G Knepley 
279588ed4aceSMatthew G Knepley #undef __FUNCT__
279688ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
279788ed4aceSMatthew G Knepley /*@
279888ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
279988ed4aceSMatthew G Knepley 
28008b1ab98fSJed Brown   Collective on DM
28018b1ab98fSJed Brown 
280288ed4aceSMatthew G Knepley   Input Parameter:
280388ed4aceSMatthew G Knepley . dm - The DM
280488ed4aceSMatthew G Knepley 
280588ed4aceSMatthew G Knepley   Output Parameter:
280688ed4aceSMatthew G Knepley . section - The PetscSection
280788ed4aceSMatthew G Knepley 
280888ed4aceSMatthew G Knepley   Level: intermediate
280988ed4aceSMatthew G Knepley 
281088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
281188ed4aceSMatthew G Knepley 
281288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
281388ed4aceSMatthew G Knepley @*/
281488ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
281588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
281688ed4aceSMatthew G Knepley 
281788ed4aceSMatthew G Knepley   PetscFunctionBegin;
281888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
281988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
282088ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
282140e8a239SMatthew G Knepley     if (!dm->defaultSection || !dm->sf) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection and PetscSF in order to create a global PetscSection");
2822b21d0597SMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
28238b1ab98fSJed Brown     ierr = PetscSectionGetValueLayout(((PetscObject)dm)->comm,dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr);
282488ed4aceSMatthew G Knepley   }
282588ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
282688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
282788ed4aceSMatthew G Knepley }
282888ed4aceSMatthew G Knepley 
282988ed4aceSMatthew G Knepley #undef __FUNCT__
2830b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
2831b21d0597SMatthew G Knepley /*@
2832b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
2833b21d0597SMatthew G Knepley 
2834b21d0597SMatthew G Knepley   Input Parameters:
2835b21d0597SMatthew G Knepley + dm - The DM
2836b21d0597SMatthew G Knepley - section - The PetscSection
2837b21d0597SMatthew G Knepley 
2838b21d0597SMatthew G Knepley   Level: intermediate
2839b21d0597SMatthew G Knepley 
2840b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
2841b21d0597SMatthew G Knepley 
2842b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
2843b21d0597SMatthew G Knepley @*/
2844b21d0597SMatthew G Knepley PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section) {
2845b21d0597SMatthew G Knepley   PetscErrorCode ierr;
2846b21d0597SMatthew G Knepley 
2847b21d0597SMatthew G Knepley   PetscFunctionBegin;
2848b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2849b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
2850b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
2851b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
2852b21d0597SMatthew G Knepley }
2853b21d0597SMatthew G Knepley 
2854b21d0597SMatthew G Knepley #undef __FUNCT__
285588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
285688ed4aceSMatthew G Knepley /*@
285788ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
285888ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
285988ed4aceSMatthew G Knepley 
286088ed4aceSMatthew G Knepley   Input Parameter:
286188ed4aceSMatthew G Knepley . dm - The DM
286288ed4aceSMatthew G Knepley 
286388ed4aceSMatthew G Knepley   Output Parameter:
286488ed4aceSMatthew G Knepley . sf - The PetscSF
286588ed4aceSMatthew G Knepley 
286688ed4aceSMatthew G Knepley   Level: intermediate
286788ed4aceSMatthew G Knepley 
286888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
286988ed4aceSMatthew G Knepley 
287088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
287188ed4aceSMatthew G Knepley @*/
287288ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
287388ed4aceSMatthew G Knepley   PetscInt       nroots;
287488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
287588ed4aceSMatthew G Knepley 
287688ed4aceSMatthew G Knepley   PetscFunctionBegin;
287788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
287888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
287988ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
288088ed4aceSMatthew G Knepley   if (nroots < 0) {
288188ed4aceSMatthew G Knepley     PetscSection section, gSection;
288288ed4aceSMatthew G Knepley 
288388ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
288431ea6d37SMatthew G Knepley     if (section) {
288588ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
288688ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
288731ea6d37SMatthew G Knepley     } else {
288831ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
288931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
289031ea6d37SMatthew G Knepley     }
289188ed4aceSMatthew G Knepley   }
289288ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
289388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
289488ed4aceSMatthew G Knepley }
289588ed4aceSMatthew G Knepley 
289688ed4aceSMatthew G Knepley #undef __FUNCT__
289788ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
289888ed4aceSMatthew G Knepley /*@
289988ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
290088ed4aceSMatthew G Knepley 
290188ed4aceSMatthew G Knepley   Input Parameters:
290288ed4aceSMatthew G Knepley + dm - The DM
290388ed4aceSMatthew G Knepley - sf - The PetscSF
290488ed4aceSMatthew G Knepley 
290588ed4aceSMatthew G Knepley   Level: intermediate
290688ed4aceSMatthew G Knepley 
290788ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
290888ed4aceSMatthew G Knepley 
290988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
291088ed4aceSMatthew G Knepley @*/
291188ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
291288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
291388ed4aceSMatthew G Knepley 
291488ed4aceSMatthew G Knepley   PetscFunctionBegin;
291588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
291688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
291788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
291888ed4aceSMatthew G Knepley   dm->defaultSF = sf;
291988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
292088ed4aceSMatthew G Knepley }
292188ed4aceSMatthew G Knepley 
292288ed4aceSMatthew G Knepley #undef __FUNCT__
292388ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
292488ed4aceSMatthew G Knepley /*@C
292588ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
292688ed4aceSMatthew G Knepley   describing the data layout.
292788ed4aceSMatthew G Knepley 
292888ed4aceSMatthew G Knepley   Input Parameters:
292988ed4aceSMatthew G Knepley + dm - The DM
293088ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
293188ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
293288ed4aceSMatthew G Knepley 
293388ed4aceSMatthew G Knepley   Level: intermediate
293488ed4aceSMatthew G Knepley 
293588ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
293688ed4aceSMatthew G Knepley @*/
293788ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
293888ed4aceSMatthew G Knepley {
293988ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
294088ed4aceSMatthew G Knepley   PetscLayout     layout;
294188ed4aceSMatthew G Knepley   const PetscInt *ranges;
294288ed4aceSMatthew G Knepley   PetscInt       *local;
294388ed4aceSMatthew G Knepley   PetscSFNode    *remote;
294488ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
294588ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
294688ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
294788ed4aceSMatthew G Knepley 
294888ed4aceSMatthew G Knepley   PetscFunctionBegin;
294988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
295088ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
295188ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
295288ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
295388ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
295488ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
295588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
295688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
295788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
295888ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
295988ed4aceSMatthew G Knepley   for (p = pStart, nleaves = 0; p < pEnd; ++p) {
29606636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
296188ed4aceSMatthew G Knepley 
29626636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
29636636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
29646636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
296588ed4aceSMatthew G Knepley   }
296688ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
296788ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
296888ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
29691f588964SMatthew G Knepley     const PetscInt *cind;
29706636e97aSMatthew G Knepley     PetscInt        dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
297188ed4aceSMatthew G Knepley 
297288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
297388ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
297488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
297588ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
297688ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
29776636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
297888ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
29796636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
29806636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
29816636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
2982057b4bcdSMatthew 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);
29836636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
29846636e97aSMatthew G Knepley     }
298588ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
298688ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
298788ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
298888ed4aceSMatthew G Knepley     }
298988ed4aceSMatthew G Knepley     if (gdof < 0) {
29906636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
299188ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
299288ed4aceSMatthew G Knepley 
299331d3f06eSJed Brown         ierr = PetscFindInt(offset,size,ranges,&r);CHKERRQ(ierr);
299431d3f06eSJed Brown         if (r < 0) r = -(r+2);
299588ed4aceSMatthew G Knepley         remote[l].rank  = r;
299688ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
299788ed4aceSMatthew G Knepley       }
299888ed4aceSMatthew G Knepley     } else {
29996636e97aSMatthew G Knepley       for(d = 0; d < gsize; ++d, ++l) {
300088ed4aceSMatthew G Knepley         remote[l].rank  = rank;
300188ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
300288ed4aceSMatthew G Knepley       }
300388ed4aceSMatthew G Knepley     }
300488ed4aceSMatthew G Knepley   }
30056636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
300688ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
300788ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
300888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
300988ed4aceSMatthew G Knepley }
3010af122d2aSMatthew G Knepley 
3011af122d2aSMatthew G Knepley #undef __FUNCT__
3012b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3013b21d0597SMatthew G Knepley /*@
3014b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3015b21d0597SMatthew G Knepley 
3016b21d0597SMatthew G Knepley   Input Parameter:
3017b21d0597SMatthew G Knepley . dm - The DM
3018b21d0597SMatthew G Knepley 
3019b21d0597SMatthew G Knepley   Output Parameter:
3020b21d0597SMatthew G Knepley . sf - The PetscSF
3021b21d0597SMatthew G Knepley 
3022b21d0597SMatthew G Knepley   Level: intermediate
3023b21d0597SMatthew G Knepley 
3024b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3025b21d0597SMatthew G Knepley 
3026057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3027b21d0597SMatthew G Knepley @*/
3028b21d0597SMatthew G Knepley PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) {
3029b21d0597SMatthew G Knepley   PetscFunctionBegin;
3030b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3031b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3032b21d0597SMatthew G Knepley   *sf = dm->sf;
3033b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3034b21d0597SMatthew G Knepley }
3035b21d0597SMatthew G Knepley 
3036b21d0597SMatthew G Knepley #undef __FUNCT__
3037057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3038057b4bcdSMatthew G Knepley /*@
3039057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3040057b4bcdSMatthew G Knepley 
3041057b4bcdSMatthew G Knepley   Input Parameters:
3042057b4bcdSMatthew G Knepley + dm - The DM
3043057b4bcdSMatthew G Knepley - sf - The PetscSF
3044057b4bcdSMatthew G Knepley 
3045057b4bcdSMatthew G Knepley   Level: intermediate
3046057b4bcdSMatthew G Knepley 
3047057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3048057b4bcdSMatthew G Knepley @*/
3049057b4bcdSMatthew G Knepley PetscErrorCode DMSetPointSF(DM dm, PetscSF sf) {
3050057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3051057b4bcdSMatthew G Knepley 
3052057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3053057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3054057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3055057b4bcdSMatthew G Knepley   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3056057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3057057b4bcdSMatthew G Knepley   dm->sf = sf;
3058057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3059057b4bcdSMatthew G Knepley }
3060057b4bcdSMatthew G Knepley 
3061057b4bcdSMatthew G Knepley #undef __FUNCT__
3062af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields"
3063af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
3064af122d2aSMatthew G Knepley {
3065af122d2aSMatthew G Knepley   PetscFunctionBegin;
3066af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3067af122d2aSMatthew G Knepley   PetscValidPointer(numFields, 2);
3068af122d2aSMatthew G Knepley   *numFields = dm->numFields;
3069af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3070af122d2aSMatthew G Knepley }
3071af122d2aSMatthew G Knepley 
3072af122d2aSMatthew G Knepley #undef __FUNCT__
3073af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3074af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3075af122d2aSMatthew G Knepley {
3076af122d2aSMatthew G Knepley   PetscInt       f;
3077af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3078af122d2aSMatthew G Knepley 
3079af122d2aSMatthew G Knepley   PetscFunctionBegin;
3080af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3081af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3082af122d2aSMatthew G Knepley     ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr);
3083af122d2aSMatthew G Knepley   }
3084af122d2aSMatthew G Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
3085af122d2aSMatthew G Knepley   dm->numFields = numFields;
3086af122d2aSMatthew G Knepley   ierr = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr);
3087af122d2aSMatthew G Knepley   for (f = 0; f < dm->numFields; ++f) {
3088af122d2aSMatthew G Knepley     ierr = PetscContainerCreate(((PetscObject) dm)->comm, (PetscContainer *) &dm->fields[f]);CHKERRQ(ierr);
3089af122d2aSMatthew G Knepley   }
3090af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3091af122d2aSMatthew G Knepley }
3092af122d2aSMatthew G Knepley 
3093af122d2aSMatthew G Knepley #undef __FUNCT__
3094af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3095af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3096af122d2aSMatthew G Knepley {
3097af122d2aSMatthew G Knepley   PetscFunctionBegin;
3098af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3099af122d2aSMatthew G Knepley   PetscValidPointer(field, 2);
3100af122d2aSMatthew G Knepley   if (!dm->fields) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()");
3101af122d2aSMatthew G Knepley   if ((f < 0) || (f >= dm->numFields)) SETERRQ3(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Field %d should be in [%d,%d)", f, 0, dm->numFields);
3102af122d2aSMatthew G Knepley   *field = dm->fields[f];
3103af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3104af122d2aSMatthew G Knepley }
31056636e97aSMatthew G Knepley 
31066636e97aSMatthew G Knepley #undef __FUNCT__
31076636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
31086636e97aSMatthew G Knepley /*@
31096636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
31106636e97aSMatthew G Knepley 
31116636e97aSMatthew G Knepley   Collective on DM
31126636e97aSMatthew G Knepley 
31136636e97aSMatthew G Knepley   Input Parameters:
31146636e97aSMatthew G Knepley + dm - the DM
31156636e97aSMatthew G Knepley - c - coordinate vector
31166636e97aSMatthew G Knepley 
31176636e97aSMatthew G Knepley   Note:
31186636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
31196636e97aSMatthew G Knepley 
31206636e97aSMatthew G Knepley   Level: intermediate
31216636e97aSMatthew G Knepley 
31226636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31236636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
31246636e97aSMatthew G Knepley @*/
31256636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
31266636e97aSMatthew G Knepley {
31276636e97aSMatthew G Knepley   PetscErrorCode ierr;
31286636e97aSMatthew G Knepley 
31296636e97aSMatthew G Knepley   PetscFunctionBegin;
31306636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31316636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
31326636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
31336636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
31346636e97aSMatthew G Knepley   dm->coordinates = c;
31356636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
31366636e97aSMatthew G Knepley   PetscFunctionReturn(0);
31376636e97aSMatthew G Knepley }
31386636e97aSMatthew G Knepley 
31396636e97aSMatthew G Knepley #undef __FUNCT__
31406636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
31416636e97aSMatthew G Knepley /*@
31426636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
31436636e97aSMatthew G Knepley 
31446636e97aSMatthew G Knepley   Collective on DM
31456636e97aSMatthew G Knepley 
31466636e97aSMatthew G Knepley    Input Parameters:
31476636e97aSMatthew G Knepley +  dm - the DM
31486636e97aSMatthew G Knepley -  c - coordinate vector
31496636e97aSMatthew G Knepley 
31506636e97aSMatthew G Knepley   Note:
31516636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
31526636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
31536636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
31546636e97aSMatthew G Knepley 
31556636e97aSMatthew G Knepley   Level: intermediate
31566636e97aSMatthew G Knepley 
31576636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31586636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
31596636e97aSMatthew G Knepley @*/
31606636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
31616636e97aSMatthew G Knepley {
31626636e97aSMatthew G Knepley   PetscErrorCode ierr;
31636636e97aSMatthew G Knepley 
31646636e97aSMatthew G Knepley   PetscFunctionBegin;
31656636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31666636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
31676636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
31686636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
31696636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
31706636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
31716636e97aSMatthew G Knepley   PetscFunctionReturn(0);
31726636e97aSMatthew G Knepley }
31736636e97aSMatthew G Knepley 
31746636e97aSMatthew G Knepley #undef __FUNCT__
31756636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
31766636e97aSMatthew G Knepley /*@
31776636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
31786636e97aSMatthew G Knepley 
31796636e97aSMatthew G Knepley   Not Collective
31806636e97aSMatthew G Knepley 
31816636e97aSMatthew G Knepley   Input Parameter:
31826636e97aSMatthew G Knepley . dm - the DM
31836636e97aSMatthew G Knepley 
31846636e97aSMatthew G Knepley   Output Parameter:
31856636e97aSMatthew G Knepley . c - global coordinate vector
31866636e97aSMatthew G Knepley 
31876636e97aSMatthew G Knepley   Note:
31886636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
31896636e97aSMatthew G Knepley 
31906636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
31916636e97aSMatthew G Knepley 
31926636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
31936636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
31946636e97aSMatthew G Knepley 
31956636e97aSMatthew G Knepley   Level: intermediate
31966636e97aSMatthew G Knepley 
31976636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
31986636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
31996636e97aSMatthew G Knepley @*/
32006636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
32016636e97aSMatthew G Knepley {
32026636e97aSMatthew G Knepley   PetscErrorCode ierr;
32036636e97aSMatthew G Knepley 
32046636e97aSMatthew G Knepley   PetscFunctionBegin;
32056636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32066636e97aSMatthew G Knepley   PetscValidPointer(c,2);
32071f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
320887743a01SMatthew G Knepley     DM cdm = PETSC_NULL;
32096636e97aSMatthew G Knepley 
32106636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
32116636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
32126636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
32136636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
32146636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
32156636e97aSMatthew G Knepley   }
32166636e97aSMatthew G Knepley   *c = dm->coordinates;
32176636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32186636e97aSMatthew G Knepley }
32196636e97aSMatthew G Knepley 
32206636e97aSMatthew G Knepley #undef __FUNCT__
32216636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
32226636e97aSMatthew G Knepley /*@
32236636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
32246636e97aSMatthew G Knepley 
32256636e97aSMatthew G Knepley   Collective on DM
32266636e97aSMatthew G Knepley 
32276636e97aSMatthew G Knepley   Input Parameter:
32286636e97aSMatthew G Knepley . dm - the DM
32296636e97aSMatthew G Knepley 
32306636e97aSMatthew G Knepley   Output Parameter:
32316636e97aSMatthew G Knepley . c - coordinate vector
32326636e97aSMatthew G Knepley 
32336636e97aSMatthew G Knepley   Note:
32346636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
32356636e97aSMatthew G Knepley 
32366636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
32376636e97aSMatthew G Knepley 
32386636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
32396636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
32406636e97aSMatthew G Knepley 
32416636e97aSMatthew G Knepley   Level: intermediate
32426636e97aSMatthew G Knepley 
32436636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
32446636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
32456636e97aSMatthew G Knepley @*/
32466636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
32476636e97aSMatthew G Knepley {
32486636e97aSMatthew G Knepley   PetscErrorCode ierr;
32496636e97aSMatthew G Knepley 
32506636e97aSMatthew G Knepley   PetscFunctionBegin;
32516636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32526636e97aSMatthew G Knepley   PetscValidPointer(c,2);
32531f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
325487743a01SMatthew G Knepley     DM cdm = PETSC_NULL;
32556636e97aSMatthew G Knepley 
32566636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
32576636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
32586636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
32596636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
32606636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
32616636e97aSMatthew G Knepley   }
32626636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
32636636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32646636e97aSMatthew G Knepley }
32656636e97aSMatthew G Knepley 
32666636e97aSMatthew G Knepley #undef __FUNCT__
32676636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
32686636e97aSMatthew G Knepley /*@
32696636e97aSMatthew G Knepley   DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates
32706636e97aSMatthew G Knepley 
32716636e97aSMatthew G Knepley   Collective on DM
32726636e97aSMatthew G Knepley 
32736636e97aSMatthew G Knepley   Input Parameter:
32746636e97aSMatthew G Knepley . dm - the DM
32756636e97aSMatthew G Knepley 
32766636e97aSMatthew G Knepley   Output Parameter:
32776636e97aSMatthew G Knepley . cdm - coordinate DM
32786636e97aSMatthew G Knepley 
32796636e97aSMatthew G Knepley   Level: intermediate
32806636e97aSMatthew G Knepley 
32816636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
32826636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
32836636e97aSMatthew G Knepley @*/
32846636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
32856636e97aSMatthew G Knepley {
32866636e97aSMatthew G Knepley   PetscErrorCode ierr;
32876636e97aSMatthew G Knepley 
32886636e97aSMatthew G Knepley   PetscFunctionBegin;
32896636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32906636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
32916636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
32926636e97aSMatthew G Knepley     if (!dm->ops->createcoordinatedm) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "Unable to create coordinates for this DM");
32936636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
32946636e97aSMatthew G Knepley   }
32956636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
32966636e97aSMatthew G Knepley   PetscFunctionReturn(0);
32976636e97aSMatthew G Knepley }
3298