xref: /petsc/src/dm/interface/dm.c (revision c833c3b5c5435e5e23b2df48a1390f46f7cf2c78)
108da532bSDmitry Karpeev #include <petscsnes.h>
2b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
347c6ae99SBarry Smith 
4732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
567a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal;
667a56275SMatthew G Knepley 
747c6ae99SBarry Smith #undef __FUNCT__
8a4121054SBarry Smith #define __FUNCT__ "DMCreate"
9a4121054SBarry Smith /*@
10de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
11a4121054SBarry Smith 
12a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
13a4121054SBarry Smith    error when you try to use the vector.
14a4121054SBarry Smith 
15a4121054SBarry Smith   Collective on MPI_Comm
16a4121054SBarry Smith 
17a4121054SBarry Smith   Input Parameter:
18a4121054SBarry Smith . comm - The communicator for the DM object
19a4121054SBarry Smith 
20a4121054SBarry Smith   Output Parameter:
21a4121054SBarry Smith . dm - The DM object
22a4121054SBarry Smith 
23a4121054SBarry Smith   Level: beginner
24a4121054SBarry Smith 
25a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
26a4121054SBarry Smith @*/
277087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
28a4121054SBarry Smith {
29a4121054SBarry Smith   DM             v;
30a4121054SBarry Smith   PetscErrorCode ierr;
31a4121054SBarry Smith 
32a4121054SBarry Smith   PetscFunctionBegin;
331411c6eeSJed Brown   PetscValidPointer(dm,2);
341411c6eeSJed Brown   *dm = PETSC_NULL;
35a4121054SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
36b84caa0eSBarry Smith   ierr = VecInitializePackage(PETSC_NULL);CHKERRQ(ierr);
37b84caa0eSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
38a4121054SBarry Smith   ierr = DMInitializePackage(PETSC_NULL);CHKERRQ(ierr);
39a4121054SBarry Smith #endif
40a4121054SBarry Smith 
413194b578SJed Brown   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
42a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
431411c6eeSJed Brown 
44e7c4fc90SDmitry Karpeev 
45a89ea682SMatthew G Knepley   v->workSize     = 0;
46a89ea682SMatthew G Knepley   v->workArray    = PETSC_NULL;
471411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
481411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
491411c6eeSJed Brown   v->bs           = 1;
50171400e9SBarry Smith   v->coloringtype = IS_COLORING_GLOBAL;
51970e74d5SMatthew G Knepley   v->lf           = PETSC_NULL;
52970e74d5SMatthew G Knepley   v->lj           = PETSC_NULL;
5388ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5488ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
5588ed4aceSMatthew G Knepley   v->defaultSection       = PETSC_NULL;
5688ed4aceSMatthew G Knepley   v->defaultGlobalSection = PETSC_NULL;
571411c6eeSJed Brown 
581411c6eeSJed Brown   *dm = v;
59a4121054SBarry Smith   PetscFunctionReturn(0);
60a4121054SBarry Smith }
61a4121054SBarry Smith 
62a4121054SBarry Smith 
63a4121054SBarry Smith #undef __FUNCT__
649a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
659a42bb27SBarry Smith /*@C
66564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
679a42bb27SBarry Smith 
68aa219208SBarry Smith    Logically Collective on DMDA
699a42bb27SBarry Smith 
709a42bb27SBarry Smith    Input Parameter:
719a42bb27SBarry Smith +  da - initial distributed array
728154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
739a42bb27SBarry Smith 
749a42bb27SBarry Smith    Options Database:
75dd85299cSBarry Smith .   -dm_vec_type ctype
769a42bb27SBarry Smith 
779a42bb27SBarry Smith    Level: intermediate
789a42bb27SBarry Smith 
79aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
809a42bb27SBarry Smith @*/
817087cfbeSBarry Smith PetscErrorCode  DMSetVecType(DM da,const VecType ctype)
829a42bb27SBarry Smith {
839a42bb27SBarry Smith   PetscErrorCode ierr;
849a42bb27SBarry Smith 
859a42bb27SBarry Smith   PetscFunctionBegin;
869a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
879a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
889a42bb27SBarry Smith   ierr = PetscStrallocpy(ctype,&da->vectype);CHKERRQ(ierr);
899a42bb27SBarry Smith   PetscFunctionReturn(0);
909a42bb27SBarry Smith }
919a42bb27SBarry Smith 
929a42bb27SBarry Smith #undef __FUNCT__
93521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
94521d9a4cSLisandro Dalcin /*@C
95521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
96521d9a4cSLisandro Dalcin 
97521d9a4cSLisandro Dalcin    Logically Collective on DM
98521d9a4cSLisandro Dalcin 
99521d9a4cSLisandro Dalcin    Input Parameter:
100521d9a4cSLisandro Dalcin +  dm - the DM context
101521d9a4cSLisandro Dalcin .  ctype - the matrix type
102521d9a4cSLisandro Dalcin 
103521d9a4cSLisandro Dalcin    Options Database:
104521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
105521d9a4cSLisandro Dalcin 
106521d9a4cSLisandro Dalcin    Level: intermediate
107521d9a4cSLisandro Dalcin 
108521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType
109521d9a4cSLisandro Dalcin @*/
110521d9a4cSLisandro Dalcin PetscErrorCode  DMSetMatType(DM dm,const MatType ctype)
111521d9a4cSLisandro Dalcin {
112521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
113521d9a4cSLisandro Dalcin   PetscFunctionBegin;
114521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
115521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
116521d9a4cSLisandro Dalcin   ierr = PetscStrallocpy(ctype,&dm->mattype);CHKERRQ(ierr);
117521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
118521d9a4cSLisandro Dalcin }
119521d9a4cSLisandro Dalcin 
120521d9a4cSLisandro Dalcin #undef __FUNCT__
1219a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
1229a42bb27SBarry Smith /*@C
1239a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
124aa219208SBarry Smith    DMDA options in the database.
1259a42bb27SBarry Smith 
126aa219208SBarry Smith    Logically Collective on DMDA
1279a42bb27SBarry Smith 
1289a42bb27SBarry Smith    Input Parameter:
129aa219208SBarry Smith +  da - the DMDA context
1309a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
1319a42bb27SBarry Smith 
1329a42bb27SBarry Smith    Notes:
1339a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1349a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
1359a42bb27SBarry Smith 
1369a42bb27SBarry Smith    Level: advanced
1379a42bb27SBarry Smith 
138aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
1399a42bb27SBarry Smith 
1409a42bb27SBarry Smith .seealso: DMSetFromOptions()
1419a42bb27SBarry Smith @*/
1427087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
1439a42bb27SBarry Smith {
1449a42bb27SBarry Smith   PetscErrorCode ierr;
1459a42bb27SBarry Smith 
1469a42bb27SBarry Smith   PetscFunctionBegin;
1479a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1489a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
1499a42bb27SBarry Smith   PetscFunctionReturn(0);
1509a42bb27SBarry Smith }
1519a42bb27SBarry Smith 
1529a42bb27SBarry Smith #undef __FUNCT__
15347c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
15447c6ae99SBarry Smith /*@
155aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
15647c6ae99SBarry Smith 
15747c6ae99SBarry Smith     Collective on DM
15847c6ae99SBarry Smith 
15947c6ae99SBarry Smith     Input Parameter:
16047c6ae99SBarry Smith .   dm - the DM object to destroy
16147c6ae99SBarry Smith 
16247c6ae99SBarry Smith     Level: developer
16347c6ae99SBarry Smith 
164e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
16547c6ae99SBarry Smith 
16647c6ae99SBarry Smith @*/
167fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
16847c6ae99SBarry Smith {
169732e2eb9SMatthew G Knepley   PetscInt       i, cnt = 0;
170dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
17147c6ae99SBarry Smith   PetscErrorCode ierr;
17247c6ae99SBarry Smith 
17347c6ae99SBarry Smith   PetscFunctionBegin;
1746bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
1756bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
17687e657c6SBarry Smith 
17787e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
178732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1796bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
1806bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
181732e2eb9SMatthew G Knepley   }
182dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
18371cd77b2SBarry Smith   if ((*dm)->x) {
18471cd77b2SBarry Smith     PetscObject obj;
18571cd77b2SBarry Smith     ierr = PetscObjectQuery((PetscObject)(*dm)->x,"DM",&obj);CHKERRQ(ierr);
18671cd77b2SBarry Smith     if (obj == (PetscObject)*dm) cnt++;
18771cd77b2SBarry Smith   }
188732e2eb9SMatthew G Knepley 
1896bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
190732e2eb9SMatthew G Knepley   /*
191732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
192732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
193732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
194732e2eb9SMatthew G Knepley   */
1956bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
1966bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
197732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1986bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
1996bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
200732e2eb9SMatthew G Knepley   }
201dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
202dfe15315SJed Brown     nnext = nlink->next;
203dfe15315SJed 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);
204dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
205dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
206dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
207dfe15315SJed Brown   }
208dfe15315SJed Brown   (*dm)->namedglobal = PETSC_NULL;
2091a266240SBarry Smith 
210b17ce1afSJed Brown   /* Destroy the list of hooks */
211*c833c3b5SJed Brown   {
212*c833c3b5SJed Brown     DMCoarsenHookLink link,next;
213b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
214b17ce1afSJed Brown       next = link->next;
215b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
216b17ce1afSJed Brown     }
217b17ce1afSJed Brown     (*dm)->coarsenhook = PETSC_NULL;
218*c833c3b5SJed Brown   }
219*c833c3b5SJed Brown   {
220*c833c3b5SJed Brown     DMRefineHookLink link,next;
221*c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
222*c833c3b5SJed Brown       next = link->next;
223*c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
224*c833c3b5SJed Brown     }
225*c833c3b5SJed Brown     (*dm)->refinehook = PETSC_NULL;
226*c833c3b5SJed Brown   }
227b17ce1afSJed Brown 
2281a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
2291a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
2301a266240SBarry Smith   }
23187e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
23271cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
2334dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
2346bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
2356bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
2366bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
237073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
238a89ea682SMatthew G Knepley   ierr = PetscFree((*dm)->workArray);CHKERRQ(ierr);
23988ed4aceSMatthew G Knepley 
24088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
24188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
24288ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
24388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
244732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
2456bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
246732e2eb9SMatthew G Knepley 
2476bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
2486bf464f9SBarry Smith   ierr = PetscFree((*dm)->data);CHKERRQ(ierr);
249732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
25047c6ae99SBarry Smith   PetscFunctionReturn(0);
25147c6ae99SBarry Smith }
25247c6ae99SBarry Smith 
25347c6ae99SBarry Smith #undef __FUNCT__
254d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
255d7bf68aeSBarry Smith /*@
256d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
257d7bf68aeSBarry Smith 
258d7bf68aeSBarry Smith     Collective on DM
259d7bf68aeSBarry Smith 
260d7bf68aeSBarry Smith     Input Parameter:
261d7bf68aeSBarry Smith .   dm - the DM object to setup
262d7bf68aeSBarry Smith 
263d7bf68aeSBarry Smith     Level: developer
264d7bf68aeSBarry Smith 
265e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
266d7bf68aeSBarry Smith 
267d7bf68aeSBarry Smith @*/
2687087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
269d7bf68aeSBarry Smith {
270d7bf68aeSBarry Smith   PetscErrorCode ierr;
271d7bf68aeSBarry Smith 
272d7bf68aeSBarry Smith   PetscFunctionBegin;
273171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2748387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
275d7bf68aeSBarry Smith   if (dm->ops->setup) {
276d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
277d7bf68aeSBarry Smith   }
2788387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
279d7bf68aeSBarry Smith   PetscFunctionReturn(0);
280d7bf68aeSBarry Smith }
281d7bf68aeSBarry Smith 
282d7bf68aeSBarry Smith #undef __FUNCT__
283d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
284d7bf68aeSBarry Smith /*@
285d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
286d7bf68aeSBarry Smith 
287d7bf68aeSBarry Smith     Collective on DM
288d7bf68aeSBarry Smith 
289d7bf68aeSBarry Smith     Input Parameter:
290d7bf68aeSBarry Smith .   dm - the DM object to set options for
291d7bf68aeSBarry Smith 
292732e2eb9SMatthew G Knepley     Options Database:
293dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
294dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
295171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
296171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
297732e2eb9SMatthew G Knepley 
298d7bf68aeSBarry Smith     Level: developer
299d7bf68aeSBarry Smith 
300e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
301d7bf68aeSBarry Smith 
302d7bf68aeSBarry Smith @*/
3037087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
304d7bf68aeSBarry Smith {
30567ad5babSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg;
306d7bf68aeSBarry Smith   PetscErrorCode ierr;
307f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
308d7bf68aeSBarry Smith 
309d7bf68aeSBarry Smith   PetscFunctionBegin;
310171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3113194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
31282fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
313c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
314c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
31567ad5babSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_latex", "Output mesh in LaTeX TikZ format", "DMView", flg4, &flg4, PETSC_NULL);CHKERRQ(ierr);
316073dac72SJed 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);
317f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
318f9ba7244SBarry Smith     if (flg) {
319f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
320f9ba7244SBarry Smith     }
321521d9a4cSLisandro Dalcin     ierr = PetscOptionsList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype?dm->mattype:typeName,typeName,sizeof typeName,&flg);CHKERRQ(ierr);
322073dac72SJed Brown     if (flg) {
323521d9a4cSLisandro Dalcin       ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
324073dac72SJed Brown     }
3251b89239cSHong 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);
326f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
327f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
328f9ba7244SBarry Smith     }
329f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
330f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
33182fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
33282fcb398SMatthew G Knepley   if (flg1) {
33382fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
33482fcb398SMatthew G Knepley   }
335c4721b0eSMatthew G Knepley   if (flg2) {
336c4721b0eSMatthew G Knepley     PetscViewer viewer;
337c4721b0eSMatthew G Knepley 
338c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
339c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
340c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
341c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
342c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
343c4721b0eSMatthew G Knepley   }
344c4721b0eSMatthew G Knepley   if (flg3) {
345c4721b0eSMatthew G Knepley     PetscViewer viewer;
346c4721b0eSMatthew G Knepley 
347c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
348c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
349c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
350c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
351c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
352c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
353c4721b0eSMatthew G Knepley   }
35467ad5babSMatthew G Knepley   if (flg4) {
35567ad5babSMatthew G Knepley     PetscViewer viewer;
35667ad5babSMatthew G Knepley 
35767ad5babSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
35867ad5babSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
35967ad5babSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_LATEX);CHKERRQ(ierr);
36067ad5babSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.tex");CHKERRQ(ierr);
36167ad5babSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
36267ad5babSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
36367ad5babSMatthew G Knepley   }
364d7bf68aeSBarry Smith   PetscFunctionReturn(0);
365d7bf68aeSBarry Smith }
366d7bf68aeSBarry Smith 
367d7bf68aeSBarry Smith #undef __FUNCT__
36847c6ae99SBarry Smith #define __FUNCT__ "DMView"
369fc9bc008SSatish Balay /*@C
370aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
37147c6ae99SBarry Smith 
37247c6ae99SBarry Smith     Collective on DM
37347c6ae99SBarry Smith 
37447c6ae99SBarry Smith     Input Parameter:
37547c6ae99SBarry Smith +   dm - the DM object to view
37647c6ae99SBarry Smith -   v - the viewer
37747c6ae99SBarry Smith 
37847c6ae99SBarry Smith     Level: developer
37947c6ae99SBarry Smith 
380e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
38147c6ae99SBarry Smith 
38247c6ae99SBarry Smith @*/
3837087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
38447c6ae99SBarry Smith {
38547c6ae99SBarry Smith   PetscErrorCode ierr;
38647c6ae99SBarry Smith 
38747c6ae99SBarry Smith   PetscFunctionBegin;
388171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3893014e516SBarry Smith  if (!v) {
3903014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
3913014e516SBarry Smith   }
3920c010503SBarry Smith   if (dm->ops->view) {
3930c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
39447c6ae99SBarry Smith   }
39547c6ae99SBarry Smith   PetscFunctionReturn(0);
39647c6ae99SBarry Smith }
39747c6ae99SBarry Smith 
39847c6ae99SBarry Smith #undef __FUNCT__
39947c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
40047c6ae99SBarry Smith /*@
401aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
40247c6ae99SBarry Smith 
40347c6ae99SBarry Smith     Collective on DM
40447c6ae99SBarry Smith 
40547c6ae99SBarry Smith     Input Parameter:
40647c6ae99SBarry Smith .   dm - the DM object
40747c6ae99SBarry Smith 
40847c6ae99SBarry Smith     Output Parameter:
40947c6ae99SBarry Smith .   vec - the global vector
41047c6ae99SBarry Smith 
411073dac72SJed Brown     Level: beginner
41247c6ae99SBarry Smith 
413e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
41447c6ae99SBarry Smith 
41547c6ae99SBarry Smith @*/
4167087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
41747c6ae99SBarry Smith {
41847c6ae99SBarry Smith   PetscErrorCode ierr;
41947c6ae99SBarry Smith 
42047c6ae99SBarry Smith   PetscFunctionBegin;
421171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42288ed4aceSMatthew G Knepley   if (dm->defaultSection) {
42388ed4aceSMatthew G Knepley     PetscSection gSection;
42488ed4aceSMatthew G Knepley     PetscInt     localSize;
42588ed4aceSMatthew G Knepley 
42688ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
42788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
42888ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
42988ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
43088ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
43188ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
43288ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
43388ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
43488ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
43588ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
43688ed4aceSMatthew G Knepley   } else {
43747c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
43888ed4aceSMatthew G Knepley   }
43947c6ae99SBarry Smith   PetscFunctionReturn(0);
44047c6ae99SBarry Smith }
44147c6ae99SBarry Smith 
44247c6ae99SBarry Smith #undef __FUNCT__
44347c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
44447c6ae99SBarry Smith /*@
445aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
44647c6ae99SBarry Smith 
44747c6ae99SBarry Smith     Not Collective
44847c6ae99SBarry Smith 
44947c6ae99SBarry Smith     Input Parameter:
45047c6ae99SBarry Smith .   dm - the DM object
45147c6ae99SBarry Smith 
45247c6ae99SBarry Smith     Output Parameter:
45347c6ae99SBarry Smith .   vec - the local vector
45447c6ae99SBarry Smith 
455073dac72SJed Brown     Level: beginner
45647c6ae99SBarry Smith 
457e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
45847c6ae99SBarry Smith 
45947c6ae99SBarry Smith @*/
4607087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
46147c6ae99SBarry Smith {
46247c6ae99SBarry Smith   PetscErrorCode ierr;
46347c6ae99SBarry Smith 
46447c6ae99SBarry Smith   PetscFunctionBegin;
465171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
46688ed4aceSMatthew G Knepley   if (dm->defaultSection) {
46788ed4aceSMatthew G Knepley     PetscInt localSize;
46888ed4aceSMatthew G Knepley 
46988ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
47088ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
47188ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
47288ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
47388ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
47488ed4aceSMatthew G Knepley   } else {
47547c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
47688ed4aceSMatthew G Knepley   }
47747c6ae99SBarry Smith   PetscFunctionReturn(0);
47847c6ae99SBarry Smith }
47947c6ae99SBarry Smith 
48047c6ae99SBarry Smith #undef __FUNCT__
4811411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
4821411c6eeSJed Brown /*@
4831411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
4841411c6eeSJed Brown 
4851411c6eeSJed Brown    Collective on DM
4861411c6eeSJed Brown 
4871411c6eeSJed Brown    Input Parameter:
4881411c6eeSJed Brown .  dm - the DM that provides the mapping
4891411c6eeSJed Brown 
4901411c6eeSJed Brown    Output Parameter:
4911411c6eeSJed Brown .  ltog - the mapping
4921411c6eeSJed Brown 
4931411c6eeSJed Brown    Level: intermediate
4941411c6eeSJed Brown 
4951411c6eeSJed Brown    Notes:
4961411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
4971411c6eeSJed Brown    MatSetLocalToGlobalMapping().
4981411c6eeSJed Brown 
4991411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
5001411c6eeSJed Brown @*/
5017087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
5021411c6eeSJed Brown {
5031411c6eeSJed Brown   PetscErrorCode ierr;
5041411c6eeSJed Brown 
5051411c6eeSJed Brown   PetscFunctionBegin;
5061411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5071411c6eeSJed Brown   PetscValidPointer(ltog,2);
5081411c6eeSJed Brown   if (!dm->ltogmap) {
50937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
51037d0c07bSMatthew G Knepley 
51137d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
51237d0c07bSMatthew G Knepley     if (section) {
51337d0c07bSMatthew G Knepley       PetscInt      *ltog;
51437d0c07bSMatthew G Knepley       PetscInt       pStart, pEnd, size, p, l;
51537d0c07bSMatthew G Knepley 
51637d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
51737d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
51837d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
51937d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
52037d0c07bSMatthew G Knepley       for(p = pStart, l = 0; p < pEnd; ++p) {
52137d0c07bSMatthew G Knepley         PetscInt dof, off, c;
52237d0c07bSMatthew G Knepley 
52337d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
52437d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
52537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
52637d0c07bSMatthew G Knepley         for(c = 0; c < dof; ++c, ++l) {
52737d0c07bSMatthew G Knepley           ltog[l] = off+c;
52837d0c07bSMatthew G Knepley         }
52937d0c07bSMatthew G Knepley       }
53037d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
53137d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
53237d0c07bSMatthew G Knepley     } else {
5331411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
5341411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
5351411c6eeSJed Brown     }
53637d0c07bSMatthew G Knepley   }
5371411c6eeSJed Brown   *ltog = dm->ltogmap;
5381411c6eeSJed Brown   PetscFunctionReturn(0);
5391411c6eeSJed Brown }
5401411c6eeSJed Brown 
5411411c6eeSJed Brown #undef __FUNCT__
5421411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
5431411c6eeSJed Brown /*@
5441411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
5451411c6eeSJed Brown 
5461411c6eeSJed Brown    Collective on DM
5471411c6eeSJed Brown 
5481411c6eeSJed Brown    Input Parameter:
5491411c6eeSJed Brown .  da - the distributed array that provides the mapping
5501411c6eeSJed Brown 
5511411c6eeSJed Brown    Output Parameter:
5521411c6eeSJed Brown .  ltog - the block mapping
5531411c6eeSJed Brown 
5541411c6eeSJed Brown    Level: intermediate
5551411c6eeSJed Brown 
5561411c6eeSJed Brown    Notes:
5571411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
5581411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
5591411c6eeSJed Brown 
5601411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
5611411c6eeSJed Brown @*/
5627087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
5631411c6eeSJed Brown {
5641411c6eeSJed Brown   PetscErrorCode ierr;
5651411c6eeSJed Brown 
5661411c6eeSJed Brown   PetscFunctionBegin;
5671411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5681411c6eeSJed Brown   PetscValidPointer(ltog,2);
5691411c6eeSJed Brown   if (!dm->ltogmapb) {
5701411c6eeSJed Brown     PetscInt bs;
5711411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
5721411c6eeSJed Brown     if (bs > 1) {
5731411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
5741411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
5751411c6eeSJed Brown     } else {
5761411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
5771411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
5781411c6eeSJed Brown     }
5791411c6eeSJed Brown   }
5801411c6eeSJed Brown   *ltog = dm->ltogmapb;
5811411c6eeSJed Brown   PetscFunctionReturn(0);
5821411c6eeSJed Brown }
5831411c6eeSJed Brown 
5841411c6eeSJed Brown #undef __FUNCT__
5851411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
5861411c6eeSJed Brown /*@
5871411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
5881411c6eeSJed Brown 
5891411c6eeSJed Brown    Not Collective
5901411c6eeSJed Brown 
5911411c6eeSJed Brown    Input Parameter:
5921411c6eeSJed Brown .  dm - the DM with block structure
5931411c6eeSJed Brown 
5941411c6eeSJed Brown    Output Parameter:
5951411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
5961411c6eeSJed Brown 
5971411c6eeSJed Brown    Level: intermediate
5981411c6eeSJed Brown 
5991411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
6001411c6eeSJed Brown @*/
6017087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
6021411c6eeSJed Brown {
6031411c6eeSJed Brown   PetscFunctionBegin;
6041411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6051411c6eeSJed Brown   PetscValidPointer(bs,2);
6061411c6eeSJed 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");
6071411c6eeSJed Brown   *bs = dm->bs;
6081411c6eeSJed Brown   PetscFunctionReturn(0);
6091411c6eeSJed Brown }
6101411c6eeSJed Brown 
6111411c6eeSJed Brown #undef __FUNCT__
612e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
61347c6ae99SBarry Smith /*@
614e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
61547c6ae99SBarry Smith 
61647c6ae99SBarry Smith     Collective on DM
61747c6ae99SBarry Smith 
61847c6ae99SBarry Smith     Input Parameter:
61947c6ae99SBarry Smith +   dm1 - the DM object
62047c6ae99SBarry Smith -   dm2 - the second, finer DM object
62147c6ae99SBarry Smith 
62247c6ae99SBarry Smith     Output Parameter:
62347c6ae99SBarry Smith +  mat - the interpolation
62447c6ae99SBarry Smith -  vec - the scaling (optional)
62547c6ae99SBarry Smith 
62647c6ae99SBarry Smith     Level: developer
62747c6ae99SBarry Smith 
62885afcc9aSBarry 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
62985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
630d52bd9f3SBarry Smith 
631d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
632d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
63385afcc9aSBarry Smith 
63485afcc9aSBarry Smith 
635e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
63647c6ae99SBarry Smith 
63747c6ae99SBarry Smith @*/
638e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
63947c6ae99SBarry Smith {
64047c6ae99SBarry Smith   PetscErrorCode ierr;
64147c6ae99SBarry Smith 
64247c6ae99SBarry Smith   PetscFunctionBegin;
643171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
644171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
64525296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
64647c6ae99SBarry Smith   PetscFunctionReturn(0);
64747c6ae99SBarry Smith }
64847c6ae99SBarry Smith 
64947c6ae99SBarry Smith #undef __FUNCT__
650e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
65147c6ae99SBarry Smith /*@
652e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
65347c6ae99SBarry Smith 
65447c6ae99SBarry Smith     Collective on DM
65547c6ae99SBarry Smith 
65647c6ae99SBarry Smith     Input Parameter:
65747c6ae99SBarry Smith +   dm1 - the DM object
65847c6ae99SBarry Smith -   dm2 - the second, finer DM object
65947c6ae99SBarry Smith 
66047c6ae99SBarry Smith     Output Parameter:
66147c6ae99SBarry Smith .   ctx - the injection
66247c6ae99SBarry Smith 
66347c6ae99SBarry Smith     Level: developer
66447c6ae99SBarry Smith 
66585afcc9aSBarry 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
66685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
66785afcc9aSBarry Smith 
668e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
66947c6ae99SBarry Smith 
67047c6ae99SBarry Smith @*/
671e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
67247c6ae99SBarry Smith {
67347c6ae99SBarry Smith   PetscErrorCode ierr;
67447c6ae99SBarry Smith 
67547c6ae99SBarry Smith   PetscFunctionBegin;
676171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
677171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
67847c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
67947c6ae99SBarry Smith   PetscFunctionReturn(0);
68047c6ae99SBarry Smith }
68147c6ae99SBarry Smith 
68247c6ae99SBarry Smith #undef __FUNCT__
683e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
684d1e2c406SBarry Smith /*@C
685e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
68647c6ae99SBarry Smith 
68747c6ae99SBarry Smith     Collective on DM
68847c6ae99SBarry Smith 
68947c6ae99SBarry Smith     Input Parameter:
69047c6ae99SBarry Smith +   dm - the DM object
69147c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
69247c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
69347c6ae99SBarry Smith 
69447c6ae99SBarry Smith     Output Parameter:
69547c6ae99SBarry Smith .   coloring - the coloring
69647c6ae99SBarry Smith 
69747c6ae99SBarry Smith     Level: developer
69847c6ae99SBarry Smith 
699e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
70047c6ae99SBarry Smith 
701aab9d709SJed Brown @*/
702e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
70347c6ae99SBarry Smith {
70447c6ae99SBarry Smith   PetscErrorCode ierr;
70547c6ae99SBarry Smith 
70647c6ae99SBarry Smith   PetscFunctionBegin;
707171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
70847c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
70947c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
71047c6ae99SBarry Smith   PetscFunctionReturn(0);
71147c6ae99SBarry Smith }
71247c6ae99SBarry Smith 
71347c6ae99SBarry Smith #undef __FUNCT__
714950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
71547c6ae99SBarry Smith /*@C
716950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
71747c6ae99SBarry Smith 
71847c6ae99SBarry Smith     Collective on DM
71947c6ae99SBarry Smith 
72047c6ae99SBarry Smith     Input Parameter:
72147c6ae99SBarry Smith +   dm - the DM object
72247c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
72394013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
72447c6ae99SBarry Smith 
72547c6ae99SBarry Smith     Output Parameter:
72647c6ae99SBarry Smith .   mat - the empty Jacobian
72747c6ae99SBarry Smith 
728073dac72SJed Brown     Level: beginner
72947c6ae99SBarry Smith 
73094013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
73194013140SBarry Smith        do not need to do it yourself.
73294013140SBarry Smith 
73394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
734aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
73594013140SBarry Smith 
73694013140SBarry 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
73794013140SBarry Smith        internally by PETSc.
73894013140SBarry Smith 
73994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
740aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
74194013140SBarry Smith 
742e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
74347c6ae99SBarry Smith 
744aab9d709SJed Brown @*/
745950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
74647c6ae99SBarry Smith {
74747c6ae99SBarry Smith   PetscErrorCode ierr;
74847c6ae99SBarry Smith 
74947c6ae99SBarry Smith   PetscFunctionBegin;
750171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
751235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
752235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
753235683edSBarry Smith #endif
754c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
755c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
756073dac72SJed Brown   if (dm->mattype) {
75725296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
758073dac72SJed Brown   } else {
75925296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
760c7b7c8a4SJed Brown   }
76147c6ae99SBarry Smith   PetscFunctionReturn(0);
76247c6ae99SBarry Smith }
76347c6ae99SBarry Smith 
76447c6ae99SBarry Smith #undef __FUNCT__
765732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
766732e2eb9SMatthew G Knepley /*@
767950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
768732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
769732e2eb9SMatthew G Knepley 
770732e2eb9SMatthew G Knepley   Logically Collective on DMDA
771732e2eb9SMatthew G Knepley 
772732e2eb9SMatthew G Knepley   Input Parameter:
773732e2eb9SMatthew G Knepley + dm - the DM
774732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
775732e2eb9SMatthew G Knepley 
776732e2eb9SMatthew G Knepley   Level: developer
777950540a4SJed Brown .seealso DMCreateMatrix()
778732e2eb9SMatthew G Knepley @*/
779732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
780732e2eb9SMatthew G Knepley {
781732e2eb9SMatthew G Knepley   PetscFunctionBegin;
782732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
783732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
784732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
785732e2eb9SMatthew G Knepley }
786732e2eb9SMatthew G Knepley 
787732e2eb9SMatthew G Knepley #undef __FUNCT__
788a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
789a89ea682SMatthew G Knepley /*@C
790a89ea682SMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size
791a89ea682SMatthew G Knepley 
792a89ea682SMatthew G Knepley   Not Collective
793a89ea682SMatthew G Knepley 
794a89ea682SMatthew G Knepley   Input Parameters:
795a89ea682SMatthew G Knepley + dm - the DM object
796a89ea682SMatthew G Knepley - size - The minium size
797a89ea682SMatthew G Knepley 
798a89ea682SMatthew G Knepley   Output Parameter:
799a89ea682SMatthew G Knepley . array - the work array
800a89ea682SMatthew G Knepley 
801a89ea682SMatthew G Knepley   Level: developer
802a89ea682SMatthew G Knepley 
803a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
804a89ea682SMatthew G Knepley @*/
805a89ea682SMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt size,PetscScalar **array)
806a89ea682SMatthew G Knepley {
807a89ea682SMatthew G Knepley   PetscErrorCode ierr;
808a89ea682SMatthew G Knepley 
809a89ea682SMatthew G Knepley   PetscFunctionBegin;
810a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
811a89ea682SMatthew G Knepley   PetscValidPointer(array,3);
812a89ea682SMatthew G Knepley   if (size > dm->workSize) {
813a89ea682SMatthew G Knepley     dm->workSize = size;
814a89ea682SMatthew G Knepley     ierr = PetscFree(dm->workArray);CHKERRQ(ierr);
815a89ea682SMatthew G Knepley     ierr = PetscMalloc(dm->workSize * sizeof(PetscScalar), &dm->workArray);CHKERRQ(ierr);
816a89ea682SMatthew G Knepley   }
817a89ea682SMatthew G Knepley   *array = dm->workArray;
818a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
819a89ea682SMatthew G Knepley }
820a89ea682SMatthew G Knepley 
8214d343eeaSMatthew G Knepley #undef __FUNCT__
822e7c4fc90SDmitry Karpeev #define __FUNCT__ "DMCreateDecompositionDM"
823e7c4fc90SDmitry Karpeev /*@C
82401bc414fSDmitry Karpeev   DMCreateDecompositionDM - creates a DM that encapsulates a decomposition of the original DM.
825e7c4fc90SDmitry Karpeev 
826e7c4fc90SDmitry Karpeev   Not Collective
827e7c4fc90SDmitry Karpeev 
828e7c4fc90SDmitry Karpeev   Input Parameters:
829e7c4fc90SDmitry Karpeev + dm   - the DM object
830e7c4fc90SDmitry Karpeev - name - the name of the decomposition
831e7c4fc90SDmitry Karpeev 
832e7c4fc90SDmitry Karpeev   Output Parameter:
833e7c4fc90SDmitry Karpeev . ddm  - the decomposition DM (PETSC_NULL, if no such decomposition is known)
834e7c4fc90SDmitry Karpeev 
835e7c4fc90SDmitry Karpeev   Level: advanced
836e7c4fc90SDmitry Karpeev 
837e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateDecomposition()
838e7c4fc90SDmitry Karpeev @*/
839e7c4fc90SDmitry Karpeev PetscErrorCode DMCreateDecompositionDM(DM dm, const char* name, DM *ddm)
840e7c4fc90SDmitry Karpeev {
841e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
842e7c4fc90SDmitry Karpeev 
843e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
844e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
845e7c4fc90SDmitry Karpeev   PetscValidCharPointer(name,2);
846e7c4fc90SDmitry Karpeev   PetscValidPointer(ddm,3);
847e7c4fc90SDmitry Karpeev   if(!dm->ops->createdecompositiondm) {
848e7c4fc90SDmitry Karpeev     *ddm = PETSC_NULL;
849e7c4fc90SDmitry Karpeev   }
850e7c4fc90SDmitry Karpeev   else {
851e7c4fc90SDmitry Karpeev     ierr = (*dm->ops->createdecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
852e7c4fc90SDmitry Karpeev   }
853e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
854e7c4fc90SDmitry Karpeev }
855e7c4fc90SDmitry Karpeev 
856e7c4fc90SDmitry Karpeev #undef __FUNCT__
8574d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
8584f3b5142SJed Brown /*@C
8594d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
8604d343eeaSMatthew G Knepley 
8614d343eeaSMatthew G Knepley   Not collective
8624d343eeaSMatthew G Knepley 
8634d343eeaSMatthew G Knepley   Input Parameter:
8644d343eeaSMatthew G Knepley . dm - the DM object
8654d343eeaSMatthew G Knepley 
8664d343eeaSMatthew G Knepley   Output Parameters:
86721c9b008SJed Brown + numFields  - The number of fields (or PETSC_NULL if not requested)
86837d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested)
86921c9b008SJed Brown - fields     - The global indices for each field (or PETSC_NULL if not requested)
8704d343eeaSMatthew G Knepley 
8714d343eeaSMatthew G Knepley   Level: intermediate
8724d343eeaSMatthew G Knepley 
87321c9b008SJed Brown   Notes:
87421c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
87521c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
87621c9b008SJed Brown   PetscFree().
87721c9b008SJed Brown 
8784d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
8794d343eeaSMatthew G Knepley @*/
88037d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
8814d343eeaSMatthew G Knepley {
88237d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
8834d343eeaSMatthew G Knepley   PetscErrorCode ierr;
8844d343eeaSMatthew G Knepley 
8854d343eeaSMatthew G Knepley   PetscFunctionBegin;
8864d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
88769ca1f37SDmitry Karpeev   if (numFields) {
88869ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
88969ca1f37SDmitry Karpeev     *numFields = 0;
89069ca1f37SDmitry Karpeev   }
89137d0c07bSMatthew G Knepley   if (fieldNames) {
89237d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
89337d0c07bSMatthew G Knepley     *fieldNames = PETSC_NULL;
89469ca1f37SDmitry Karpeev   }
89569ca1f37SDmitry Karpeev   if (fields) {
89669ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
89769ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
89869ca1f37SDmitry Karpeev   }
89937d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
90037d0c07bSMatthew G Knepley   if (section) {
90137d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
90237d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
90337d0c07bSMatthew G Knepley 
90437d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
90537d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
90637d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr);
90737d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
90837d0c07bSMatthew G Knepley     for(f = 0; f < nF; ++f) {
90937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
91037d0c07bSMatthew G Knepley     }
91137d0c07bSMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
91237d0c07bSMatthew G Knepley       PetscInt gdof;
91337d0c07bSMatthew G Knepley 
91437d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
91537d0c07bSMatthew G Knepley       if (gdof > 0) {
91637d0c07bSMatthew G Knepley         for(f = 0; f < nF; ++f) {
91737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
91837d0c07bSMatthew G Knepley 
91937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
92037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
92137d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
92237d0c07bSMatthew G Knepley         }
92337d0c07bSMatthew G Knepley       }
92437d0c07bSMatthew G Knepley     }
92537d0c07bSMatthew G Knepley     for(f = 0; f < nF; ++f) {
92637d0c07bSMatthew G Knepley       ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
92737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
92837d0c07bSMatthew G Knepley     }
92937d0c07bSMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
93037d0c07bSMatthew G Knepley       PetscInt gdof, goff;
93137d0c07bSMatthew G Knepley 
93237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
93337d0c07bSMatthew G Knepley       if (gdof > 0) {
93437d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
93537d0c07bSMatthew G Knepley         for(f = 0; f < nF; ++f) {
93637d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
93737d0c07bSMatthew G Knepley 
93837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
93937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
94037d0c07bSMatthew G Knepley           for(fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
94137d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
94237d0c07bSMatthew G Knepley           }
94337d0c07bSMatthew G Knepley         }
94437d0c07bSMatthew G Knepley       }
94537d0c07bSMatthew G Knepley     }
94637d0c07bSMatthew G Knepley     if (numFields) {*numFields = nF;}
94737d0c07bSMatthew G Knepley     if (fieldNames) {
94837d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr);
94937d0c07bSMatthew G Knepley       for(f = 0; f < nF; ++f) {
95037d0c07bSMatthew G Knepley         const char *fieldName;
95137d0c07bSMatthew G Knepley 
95237d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
95337d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr);
95437d0c07bSMatthew G Knepley       }
95537d0c07bSMatthew G Knepley     }
95637d0c07bSMatthew G Knepley     if (fields) {
95737d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
95837d0c07bSMatthew G Knepley       for(f = 0; f < nF; ++f) {
95937d0c07bSMatthew G Knepley         ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
96037d0c07bSMatthew G Knepley       }
96137d0c07bSMatthew G Knepley     }
96237d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
96337d0c07bSMatthew G Knepley   } else {
96437d0c07bSMatthew G Knepley     if(dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);}
96569ca1f37SDmitry Karpeev   }
9664d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
9674d343eeaSMatthew G Knepley }
9684d343eeaSMatthew G Knepley 
9695fe1f584SPeter Brune 
970a89ea682SMatthew G Knepley #undef __FUNCT__
971e7c4fc90SDmitry Karpeev #define __FUNCT__ "DMCreateDecomposition"
972e7c4fc90SDmitry Karpeev /*@C
973e7c4fc90SDmitry Karpeev   DMCreateDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems:
974e7c4fc90SDmitry Karpeev                           each IS contains the global indices of the dofs of the corresponding subproblem.
975e7c4fc90SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
976e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
977e7c4fc90SDmitry Karpeev 
978e7c4fc90SDmitry Karpeev   Not collective
979e7c4fc90SDmitry Karpeev 
980e7c4fc90SDmitry Karpeev   Input Parameter:
981e7c4fc90SDmitry Karpeev . dm - the DM object
982e7c4fc90SDmitry Karpeev 
983e7c4fc90SDmitry Karpeev   Output Parameters:
984e7c4fc90SDmitry Karpeev + len       - The number of subproblems in the decomposition (or PETSC_NULL if not requested)
985e7c4fc90SDmitry Karpeev . namelist  - The name for each subproblem (or PETSC_NULL if not requested)
986e7c4fc90SDmitry Karpeev . islist    - The global indices for each subproblem (or PETSC_NULL if not requested)
987e7c4fc90SDmitry Karpeev - dmlist    - The DMs for each subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
988e7c4fc90SDmitry Karpeev 
989e7c4fc90SDmitry Karpeev   Level: intermediate
990e7c4fc90SDmitry Karpeev 
991e7c4fc90SDmitry Karpeev   Notes:
992e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
993e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
994e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
995e7c4fc90SDmitry Karpeev 
996e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
997e7c4fc90SDmitry Karpeev @*/
998e7c4fc90SDmitry Karpeev PetscErrorCode DMCreateDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
999e7c4fc90SDmitry Karpeev {
1000e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1001e7c4fc90SDmitry Karpeev 
1002e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1003e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1004e7c4fc90SDmitry Karpeev   if (len) {PetscValidPointer(len,2);}
1005e7c4fc90SDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3);}
1006e7c4fc90SDmitry Karpeev   if (islist) {PetscValidPointer(islist,4);}
1007e7c4fc90SDmitry Karpeev   if (dmlist) {PetscValidPointer(dmlist,5);}
1008e7c4fc90SDmitry Karpeev   if(!dm->ops->createdecomposition) {
100969ca1f37SDmitry Karpeev     ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1010e7c4fc90SDmitry Karpeev     /* By default there are no DMs associated with subproblems. */
1011e7c4fc90SDmitry Karpeev     if(dmlist) *dmlist = PETSC_NULL;
1012e7c4fc90SDmitry Karpeev   }
1013e7c4fc90SDmitry Karpeev   else {
1014e7c4fc90SDmitry Karpeev     ierr = (*dm->ops->createdecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
1015e7c4fc90SDmitry Karpeev   }
1016e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1017e7c4fc90SDmitry Karpeev }
1018e7c4fc90SDmitry Karpeev 
1019e7c4fc90SDmitry Karpeev 
1020e7c4fc90SDmitry Karpeev #undef __FUNCT__
102147c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
102247c6ae99SBarry Smith /*@
102347c6ae99SBarry Smith   DMRefine - Refines a DM object
102447c6ae99SBarry Smith 
102547c6ae99SBarry Smith   Collective on DM
102647c6ae99SBarry Smith 
102747c6ae99SBarry Smith   Input Parameter:
102847c6ae99SBarry Smith + dm   - the DM object
102991d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
103047c6ae99SBarry Smith 
103147c6ae99SBarry Smith   Output Parameter:
1032ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1033ae0a1c52SMatthew G Knepley 
1034ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
103547c6ae99SBarry Smith 
103647c6ae99SBarry Smith   Level: developer
103747c6ae99SBarry Smith 
1038e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
103947c6ae99SBarry Smith @*/
10407087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
104147c6ae99SBarry Smith {
104247c6ae99SBarry Smith   PetscErrorCode ierr;
1043*c833c3b5SJed Brown   DMRefineHookLink link;
104447c6ae99SBarry Smith 
104547c6ae99SBarry Smith   PetscFunctionBegin;
1046732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
104747c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
10484057135bSMatthew G Knepley   if (*dmf) {
104943842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
1050644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
1051644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
1052644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
1053644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
1054644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
1055644e2e5bSBarry Smith     }
10568cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1057644e2e5bSBarry Smith     (*dmf)->ctx     = dm->ctx;
1058656b349aSBarry Smith     (*dmf)->levelup = dm->levelup + 1;
1059*c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1060*c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1061*c833c3b5SJed Brown     }
1062*c833c3b5SJed Brown   }
1063*c833c3b5SJed Brown   PetscFunctionReturn(0);
1064*c833c3b5SJed Brown }
1065*c833c3b5SJed Brown 
1066*c833c3b5SJed Brown #undef __FUNCT__
1067*c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1068*c833c3b5SJed Brown /*@
1069*c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1070*c833c3b5SJed Brown 
1071*c833c3b5SJed Brown    Logically Collective
1072*c833c3b5SJed Brown 
1073*c833c3b5SJed Brown    Input Arguments:
1074*c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1075*c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1076*c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1077*c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1078*c833c3b5SJed Brown 
1079*c833c3b5SJed Brown    Calling sequence of refinehook:
1080*c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1081*c833c3b5SJed Brown 
1082*c833c3b5SJed Brown +  coarse - coarse level DM
1083*c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1084*c833c3b5SJed Brown -  ctx - optional user-defined function context
1085*c833c3b5SJed Brown 
1086*c833c3b5SJed Brown    Calling sequence for interphook:
1087*c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1088*c833c3b5SJed Brown 
1089*c833c3b5SJed Brown +  coarse - coarse level DM
1090*c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1091*c833c3b5SJed Brown .  fine - fine level DM to update
1092*c833c3b5SJed Brown -  ctx - optional user-defined function context
1093*c833c3b5SJed Brown 
1094*c833c3b5SJed Brown    Level: advanced
1095*c833c3b5SJed Brown 
1096*c833c3b5SJed Brown    Notes:
1097*c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1098*c833c3b5SJed Brown 
1099*c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1100*c833c3b5SJed Brown 
1101*c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1102*c833c3b5SJed Brown @*/
1103*c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1104*c833c3b5SJed Brown {
1105*c833c3b5SJed Brown   PetscErrorCode ierr;
1106*c833c3b5SJed Brown   DMRefineHookLink link,*p;
1107*c833c3b5SJed Brown 
1108*c833c3b5SJed Brown   PetscFunctionBegin;
1109*c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1110*c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1111*c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1112*c833c3b5SJed Brown   link->refinehook = refinehook;
1113*c833c3b5SJed Brown   link->interphook = interphook;
1114*c833c3b5SJed Brown   link->ctx = ctx;
1115*c833c3b5SJed Brown   link->next = PETSC_NULL;
1116*c833c3b5SJed Brown   *p = link;
1117*c833c3b5SJed Brown   PetscFunctionReturn(0);
1118*c833c3b5SJed Brown }
1119*c833c3b5SJed Brown 
1120*c833c3b5SJed Brown #undef __FUNCT__
1121*c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1122*c833c3b5SJed Brown /*@
1123*c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1124*c833c3b5SJed Brown 
1125*c833c3b5SJed Brown    Collective if any hooks are
1126*c833c3b5SJed Brown 
1127*c833c3b5SJed Brown    Input Arguments:
1128*c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1129*c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1130*c833c3b5SJed Brown -  fine - finer DM to update
1131*c833c3b5SJed Brown 
1132*c833c3b5SJed Brown    Level: developer
1133*c833c3b5SJed Brown 
1134*c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1135*c833c3b5SJed Brown @*/
1136*c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1137*c833c3b5SJed Brown {
1138*c833c3b5SJed Brown   PetscErrorCode ierr;
1139*c833c3b5SJed Brown   DMRefineHookLink link;
1140*c833c3b5SJed Brown 
1141*c833c3b5SJed Brown   PetscFunctionBegin;
1142*c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1143*c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
11444057135bSMatthew G Knepley   }
114547c6ae99SBarry Smith   PetscFunctionReturn(0);
114647c6ae99SBarry Smith }
114747c6ae99SBarry Smith 
114847c6ae99SBarry Smith #undef __FUNCT__
1149eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1150eb3f98d2SBarry Smith /*@
1151eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1152eb3f98d2SBarry Smith 
1153eb3f98d2SBarry Smith     Not Collective
1154eb3f98d2SBarry Smith 
1155eb3f98d2SBarry Smith     Input Parameter:
1156eb3f98d2SBarry Smith .   dm - the DM object
1157eb3f98d2SBarry Smith 
1158eb3f98d2SBarry Smith     Output Parameter:
1159eb3f98d2SBarry Smith .   level - number of refinements
1160eb3f98d2SBarry Smith 
1161eb3f98d2SBarry Smith     Level: developer
1162eb3f98d2SBarry Smith 
11636a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1164eb3f98d2SBarry Smith 
1165eb3f98d2SBarry Smith @*/
1166eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1167eb3f98d2SBarry Smith {
1168eb3f98d2SBarry Smith   PetscFunctionBegin;
1169eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1170eb3f98d2SBarry Smith   *level = dm->levelup;
1171eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1172eb3f98d2SBarry Smith }
1173eb3f98d2SBarry Smith 
1174eb3f98d2SBarry Smith #undef __FUNCT__
117547c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
117647c6ae99SBarry Smith /*@
117747c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
117847c6ae99SBarry Smith 
117947c6ae99SBarry Smith     Neighbor-wise Collective on DM
118047c6ae99SBarry Smith 
118147c6ae99SBarry Smith     Input Parameters:
118247c6ae99SBarry Smith +   dm - the DM object
118347c6ae99SBarry Smith .   g - the global vector
118447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
118547c6ae99SBarry Smith -   l - the local vector
118647c6ae99SBarry Smith 
118747c6ae99SBarry Smith 
118847c6ae99SBarry Smith     Level: beginner
118947c6ae99SBarry Smith 
1190e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
119147c6ae99SBarry Smith 
119247c6ae99SBarry Smith @*/
11937087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
119447c6ae99SBarry Smith {
11957128ae9fSMatthew G Knepley   PetscSF        sf;
119647c6ae99SBarry Smith   PetscErrorCode ierr;
119747c6ae99SBarry Smith 
119847c6ae99SBarry Smith   PetscFunctionBegin;
1199171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12007128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
12017128ae9fSMatthew G Knepley   if (sf) {
12027128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
12037128ae9fSMatthew G Knepley 
12047128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
12057128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
12067128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
12077128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
12087128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
12097128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
12107128ae9fSMatthew G Knepley   } else {
1211843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
12127128ae9fSMatthew G Knepley   }
121347c6ae99SBarry Smith   PetscFunctionReturn(0);
121447c6ae99SBarry Smith }
121547c6ae99SBarry Smith 
121647c6ae99SBarry Smith #undef __FUNCT__
121747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
121847c6ae99SBarry Smith /*@
121947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
122047c6ae99SBarry Smith 
122147c6ae99SBarry Smith     Neighbor-wise Collective on DM
122247c6ae99SBarry Smith 
122347c6ae99SBarry Smith     Input Parameters:
122447c6ae99SBarry Smith +   dm - the DM object
122547c6ae99SBarry Smith .   g - the global vector
122647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
122747c6ae99SBarry Smith -   l - the local vector
122847c6ae99SBarry Smith 
122947c6ae99SBarry Smith 
123047c6ae99SBarry Smith     Level: beginner
123147c6ae99SBarry Smith 
1232e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
123347c6ae99SBarry Smith 
123447c6ae99SBarry Smith @*/
12357087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
123647c6ae99SBarry Smith {
12377128ae9fSMatthew G Knepley   PetscSF        sf;
123847c6ae99SBarry Smith   PetscErrorCode ierr;
123961a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
124047c6ae99SBarry Smith 
124147c6ae99SBarry Smith   PetscFunctionBegin;
1242171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12437128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
12447128ae9fSMatthew G Knepley   if (sf) {
12457128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
12467128ae9fSMatthew G Knepley 
12477128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
12487128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
12497128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
12507128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
12517128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
12527128ae9fSMatthew G Knepley   } else {
1253843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
12547128ae9fSMatthew G Knepley   }
125547c6ae99SBarry Smith   PetscFunctionReturn(0);
125647c6ae99SBarry Smith }
125747c6ae99SBarry Smith 
125847c6ae99SBarry Smith #undef __FUNCT__
12599a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
126047c6ae99SBarry Smith /*@
12619a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
12629a42bb27SBarry Smith 
12639a42bb27SBarry Smith     Neighbor-wise Collective on DM
12649a42bb27SBarry Smith 
12659a42bb27SBarry Smith     Input Parameters:
12669a42bb27SBarry Smith +   dm - the DM object
1267f6813fd5SJed Brown .   l - the local vector
12689a42bb27SBarry 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
12699a42bb27SBarry Smith            base point.
1270f6813fd5SJed Brown - - the global vector
12719a42bb27SBarry Smith 
12729a42bb27SBarry 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
12739a42bb27SBarry 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
12749a42bb27SBarry Smith            global array to the final global array with VecAXPY().
12759a42bb27SBarry Smith 
12769a42bb27SBarry Smith     Level: beginner
12779a42bb27SBarry Smith 
1278e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
12799a42bb27SBarry Smith 
12809a42bb27SBarry Smith @*/
12817087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
12829a42bb27SBarry Smith {
12837128ae9fSMatthew G Knepley   PetscSF        sf;
12849a42bb27SBarry Smith   PetscErrorCode ierr;
12859a42bb27SBarry Smith 
12869a42bb27SBarry Smith   PetscFunctionBegin;
1287171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12887128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
12897128ae9fSMatthew G Knepley   if (sf) {
12907128ae9fSMatthew G Knepley     MPI_Op       op;
12917128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
12927128ae9fSMatthew G Knepley 
12937128ae9fSMatthew G Knepley     switch(mode) {
12947128ae9fSMatthew G Knepley     case INSERT_VALUES:
12957128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
12967128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
12977128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
12987128ae9fSMatthew G Knepley #else
12997128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
13007128ae9fSMatthew G Knepley #endif
13017128ae9fSMatthew G Knepley     case ADD_VALUES:
13027128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
13037128ae9fSMatthew G Knepley       op = MPI_SUM; break;
13047128ae9fSMatthew G Knepley   default:
13057128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
13067128ae9fSMatthew G Knepley     }
13077128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
13087128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
13097128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
13107128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
13117128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
13127128ae9fSMatthew G Knepley   } else {
1313843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
13147128ae9fSMatthew G Knepley   }
13159a42bb27SBarry Smith   PetscFunctionReturn(0);
13169a42bb27SBarry Smith }
13179a42bb27SBarry Smith 
13189a42bb27SBarry Smith #undef __FUNCT__
13199a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
13209a42bb27SBarry Smith /*@
13219a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
132247c6ae99SBarry Smith 
132347c6ae99SBarry Smith     Neighbor-wise Collective on DM
132447c6ae99SBarry Smith 
132547c6ae99SBarry Smith     Input Parameters:
132647c6ae99SBarry Smith +   dm - the DM object
1327f6813fd5SJed Brown .   l - the local vector
132847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1329f6813fd5SJed Brown -   g - the global vector
133047c6ae99SBarry Smith 
133147c6ae99SBarry Smith 
133247c6ae99SBarry Smith     Level: beginner
133347c6ae99SBarry Smith 
1334e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
133547c6ae99SBarry Smith 
133647c6ae99SBarry Smith @*/
13377087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
133847c6ae99SBarry Smith {
13397128ae9fSMatthew G Knepley   PetscSF        sf;
134047c6ae99SBarry Smith   PetscErrorCode ierr;
134147c6ae99SBarry Smith 
134247c6ae99SBarry Smith   PetscFunctionBegin;
1343171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13447128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
13457128ae9fSMatthew G Knepley   if (sf) {
13467128ae9fSMatthew G Knepley     MPI_Op       op;
13477128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
13487128ae9fSMatthew G Knepley 
13497128ae9fSMatthew G Knepley     switch(mode) {
13507128ae9fSMatthew G Knepley     case INSERT_VALUES:
13517128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
13527128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
13537128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
13547128ae9fSMatthew G Knepley #else
13557128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
13567128ae9fSMatthew G Knepley #endif
13577128ae9fSMatthew G Knepley     case ADD_VALUES:
13587128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
13597128ae9fSMatthew G Knepley       op = MPI_SUM; break;
13607128ae9fSMatthew G Knepley     default:
13617128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
13627128ae9fSMatthew G Knepley     }
13637128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
13647128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
13657128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
13667128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
13677128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
13687128ae9fSMatthew G Knepley   } else {
1369843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
13707128ae9fSMatthew G Knepley   }
137147c6ae99SBarry Smith   PetscFunctionReturn(0);
137247c6ae99SBarry Smith }
137347c6ae99SBarry Smith 
137447c6ae99SBarry Smith #undef __FUNCT__
137547c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
137647c6ae99SBarry Smith /*@
137747c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
137847c6ae99SBarry Smith 
137947c6ae99SBarry Smith     Collective on DM
138047c6ae99SBarry Smith 
138147c6ae99SBarry Smith     Input Parameter:
138247c6ae99SBarry Smith +   dm - the DM object
138347c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
138447c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
138547c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
138647c6ae99SBarry Smith 
138747c6ae99SBarry Smith     Level: developer
138847c6ae99SBarry Smith 
1389e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
139047c6ae99SBarry Smith          DMSetFunction()
139147c6ae99SBarry Smith 
139247c6ae99SBarry Smith @*/
13937087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
139447c6ae99SBarry Smith {
139547c6ae99SBarry Smith   PetscErrorCode ierr;
1396171400e9SBarry Smith 
139747c6ae99SBarry Smith   PetscFunctionBegin;
1398171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
139947c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
140047c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
140147c6ae99SBarry Smith   if (A != B) {
140247c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
140347c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
140447c6ae99SBarry Smith   }
140547c6ae99SBarry Smith   PetscFunctionReturn(0);
140647c6ae99SBarry Smith }
140747c6ae99SBarry Smith 
140847c6ae99SBarry Smith #undef __FUNCT__
140947c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
141047c6ae99SBarry Smith /*@
141147c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
141247c6ae99SBarry Smith 
141347c6ae99SBarry Smith     Collective on DM
141447c6ae99SBarry Smith 
141547c6ae99SBarry Smith     Input Parameter:
141647c6ae99SBarry Smith +   dm - the DM object
141791d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
141847c6ae99SBarry Smith 
141947c6ae99SBarry Smith     Output Parameter:
142047c6ae99SBarry Smith .   dmc - the coarsened DM
142147c6ae99SBarry Smith 
142247c6ae99SBarry Smith     Level: developer
142347c6ae99SBarry Smith 
1424e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
142547c6ae99SBarry Smith 
142647c6ae99SBarry Smith @*/
14277087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
142847c6ae99SBarry Smith {
142947c6ae99SBarry Smith   PetscErrorCode ierr;
1430b17ce1afSJed Brown   DMCoarsenHookLink link;
143147c6ae99SBarry Smith 
143247c6ae99SBarry Smith   PetscFunctionBegin;
1433171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
143447c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
143543842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
143647c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
143747c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
143847c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
143947c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
144047c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
144147c6ae99SBarry Smith   }
14428cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1443644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
1444656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1445b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1446b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1447b17ce1afSJed Brown   }
1448b17ce1afSJed Brown   PetscFunctionReturn(0);
1449b17ce1afSJed Brown }
1450b17ce1afSJed Brown 
1451b17ce1afSJed Brown #undef __FUNCT__
1452b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1453b17ce1afSJed Brown /*@
1454b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1455b17ce1afSJed Brown 
1456b17ce1afSJed Brown    Logically Collective
1457b17ce1afSJed Brown 
1458b17ce1afSJed Brown    Input Arguments:
1459b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1460b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1461b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1462b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1463b17ce1afSJed Brown 
1464b17ce1afSJed Brown    Calling sequence of coarsenhook:
1465b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1466b17ce1afSJed Brown 
1467b17ce1afSJed Brown +  fine - fine level DM
1468b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1469b17ce1afSJed Brown -  ctx - optional user-defined function context
1470b17ce1afSJed Brown 
1471b17ce1afSJed Brown    Calling sequence for restricthook:
1472*c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1473b17ce1afSJed Brown 
1474b17ce1afSJed Brown +  fine - fine level DM
1475b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1476*c833c3b5SJed Brown .  rscale - scaling vector for restriction
1477*c833c3b5SJed Brown .  inject - matrix restricting by injection
1478b17ce1afSJed Brown .  coarse - coarse level DM to update
1479b17ce1afSJed Brown -  ctx - optional user-defined function context
1480b17ce1afSJed Brown 
1481b17ce1afSJed Brown    Level: advanced
1482b17ce1afSJed Brown 
1483b17ce1afSJed Brown    Notes:
1484b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1485b17ce1afSJed Brown 
1486b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1487b17ce1afSJed Brown 
1488b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1489b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1490b17ce1afSJed Brown 
1491*c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1492b17ce1afSJed Brown @*/
1493b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1494b17ce1afSJed Brown {
1495b17ce1afSJed Brown   PetscErrorCode ierr;
1496b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1497b17ce1afSJed Brown 
1498b17ce1afSJed Brown   PetscFunctionBegin;
1499b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
15006bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1501b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1502b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1503b17ce1afSJed Brown   link->restricthook = restricthook;
1504b17ce1afSJed Brown   link->ctx = ctx;
15056cab3a1bSJed Brown   link->next = PETSC_NULL;
1506b17ce1afSJed Brown   *p = link;
1507b17ce1afSJed Brown   PetscFunctionReturn(0);
1508b17ce1afSJed Brown }
1509b17ce1afSJed Brown 
1510b17ce1afSJed Brown #undef __FUNCT__
1511b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1512b17ce1afSJed Brown /*@
1513b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1514b17ce1afSJed Brown 
1515b17ce1afSJed Brown    Collective if any hooks are
1516b17ce1afSJed Brown 
1517b17ce1afSJed Brown    Input Arguments:
1518b17ce1afSJed Brown +  fine - finer DM to use as a base
1519b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1520b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1521b17ce1afSJed Brown -  coarse - coarer DM to update
1522b17ce1afSJed Brown 
1523b17ce1afSJed Brown    Level: developer
1524b17ce1afSJed Brown 
1525b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1526b17ce1afSJed Brown @*/
1527b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1528b17ce1afSJed Brown {
1529b17ce1afSJed Brown   PetscErrorCode ierr;
1530b17ce1afSJed Brown   DMCoarsenHookLink link;
1531b17ce1afSJed Brown 
1532b17ce1afSJed Brown   PetscFunctionBegin;
1533b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1534b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1535b17ce1afSJed Brown   }
153647c6ae99SBarry Smith   PetscFunctionReturn(0);
153747c6ae99SBarry Smith }
153847c6ae99SBarry Smith 
153947c6ae99SBarry Smith #undef __FUNCT__
15405fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
15415fe1f584SPeter Brune /*@
15426a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
15435fe1f584SPeter Brune 
15445fe1f584SPeter Brune     Not Collective
15455fe1f584SPeter Brune 
15465fe1f584SPeter Brune     Input Parameter:
15475fe1f584SPeter Brune .   dm - the DM object
15485fe1f584SPeter Brune 
15495fe1f584SPeter Brune     Output Parameter:
15506a7d9d85SPeter Brune .   level - number of coarsenings
15515fe1f584SPeter Brune 
15525fe1f584SPeter Brune     Level: developer
15535fe1f584SPeter Brune 
15546a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
15555fe1f584SPeter Brune 
15565fe1f584SPeter Brune @*/
15575fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
15585fe1f584SPeter Brune {
15595fe1f584SPeter Brune   PetscFunctionBegin;
15605fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15615fe1f584SPeter Brune   *level = dm->leveldown;
15625fe1f584SPeter Brune   PetscFunctionReturn(0);
15635fe1f584SPeter Brune }
15645fe1f584SPeter Brune 
15655fe1f584SPeter Brune 
15665fe1f584SPeter Brune 
15675fe1f584SPeter Brune #undef __FUNCT__
156847c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
156947c6ae99SBarry Smith /*@C
157047c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
157147c6ae99SBarry Smith 
157247c6ae99SBarry Smith     Collective on DM
157347c6ae99SBarry Smith 
157447c6ae99SBarry Smith     Input Parameter:
157547c6ae99SBarry Smith +   dm - the DM object
157647c6ae99SBarry Smith -   nlevels - the number of levels of refinement
157747c6ae99SBarry Smith 
157847c6ae99SBarry Smith     Output Parameter:
157947c6ae99SBarry Smith .   dmf - the refined DM hierarchy
158047c6ae99SBarry Smith 
158147c6ae99SBarry Smith     Level: developer
158247c6ae99SBarry Smith 
1583e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
158447c6ae99SBarry Smith 
158547c6ae99SBarry Smith @*/
15867087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
158747c6ae99SBarry Smith {
158847c6ae99SBarry Smith   PetscErrorCode ierr;
158947c6ae99SBarry Smith 
159047c6ae99SBarry Smith   PetscFunctionBegin;
1591171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
159247c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
159347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
159447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
159547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
159647c6ae99SBarry Smith   } else if (dm->ops->refine) {
159747c6ae99SBarry Smith     PetscInt i;
159847c6ae99SBarry Smith 
159947c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
160047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
160147c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
160247c6ae99SBarry Smith     }
160347c6ae99SBarry Smith   } else {
160447c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
160547c6ae99SBarry Smith   }
160647c6ae99SBarry Smith   PetscFunctionReturn(0);
160747c6ae99SBarry Smith }
160847c6ae99SBarry Smith 
160947c6ae99SBarry Smith #undef __FUNCT__
161047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
161147c6ae99SBarry Smith /*@C
161247c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
161347c6ae99SBarry Smith 
161447c6ae99SBarry Smith     Collective on DM
161547c6ae99SBarry Smith 
161647c6ae99SBarry Smith     Input Parameter:
161747c6ae99SBarry Smith +   dm - the DM object
161847c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
161947c6ae99SBarry Smith 
162047c6ae99SBarry Smith     Output Parameter:
162147c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
162247c6ae99SBarry Smith 
162347c6ae99SBarry Smith     Level: developer
162447c6ae99SBarry Smith 
1625e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
162647c6ae99SBarry Smith 
162747c6ae99SBarry Smith @*/
16287087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
162947c6ae99SBarry Smith {
163047c6ae99SBarry Smith   PetscErrorCode ierr;
163147c6ae99SBarry Smith 
163247c6ae99SBarry Smith   PetscFunctionBegin;
1633171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
163447c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
163547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
163647c6ae99SBarry Smith   PetscValidPointer(dmc,3);
163747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
163847c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
163947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
164047c6ae99SBarry Smith     PetscInt i;
164147c6ae99SBarry Smith 
164247c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
164347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
164447c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
164547c6ae99SBarry Smith     }
164647c6ae99SBarry Smith   } else {
164747c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
164847c6ae99SBarry Smith   }
164947c6ae99SBarry Smith   PetscFunctionReturn(0);
165047c6ae99SBarry Smith }
165147c6ae99SBarry Smith 
165247c6ae99SBarry Smith #undef __FUNCT__
1653e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
165447c6ae99SBarry Smith /*@
1655e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
165647c6ae99SBarry Smith    grids associated with two DMs.
165747c6ae99SBarry Smith 
165847c6ae99SBarry Smith    Collective on DM
165947c6ae99SBarry Smith 
166047c6ae99SBarry Smith    Input Parameters:
166147c6ae99SBarry Smith +  dmc - the coarse grid DM
166247c6ae99SBarry Smith -  dmf - the fine grid DM
166347c6ae99SBarry Smith 
166447c6ae99SBarry Smith    Output Parameters:
166547c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
166647c6ae99SBarry Smith 
166747c6ae99SBarry Smith    Level: intermediate
166847c6ae99SBarry Smith 
166947c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
167047c6ae99SBarry Smith 
1671e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
167247c6ae99SBarry Smith @*/
1673e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
167447c6ae99SBarry Smith {
167547c6ae99SBarry Smith   PetscErrorCode ierr;
167647c6ae99SBarry Smith 
167747c6ae99SBarry Smith   PetscFunctionBegin;
1678171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1679171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
168047c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
168147c6ae99SBarry Smith   PetscFunctionReturn(0);
168247c6ae99SBarry Smith }
168347c6ae99SBarry Smith 
168447c6ae99SBarry Smith #undef __FUNCT__
16851a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
16861a266240SBarry Smith /*@C
16871a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
16881a266240SBarry Smith 
16891a266240SBarry Smith     Not Collective
16901a266240SBarry Smith 
16911a266240SBarry Smith     Input Parameters:
16921a266240SBarry Smith +   dm - the DM object
16931a266240SBarry Smith -   destroy - the destroy function
16941a266240SBarry Smith 
16951a266240SBarry Smith     Level: intermediate
16961a266240SBarry Smith 
1697e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
16981a266240SBarry Smith 
1699f07f9ceaSJed Brown @*/
17001a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
17011a266240SBarry Smith {
17021a266240SBarry Smith   PetscFunctionBegin;
1703171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17041a266240SBarry Smith   dm->ctxdestroy = destroy;
17051a266240SBarry Smith   PetscFunctionReturn(0);
17061a266240SBarry Smith }
17071a266240SBarry Smith 
17081a266240SBarry Smith #undef __FUNCT__
17091b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1710b07ff414SBarry Smith /*@
17111b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
171247c6ae99SBarry Smith 
171347c6ae99SBarry Smith     Not Collective
171447c6ae99SBarry Smith 
171547c6ae99SBarry Smith     Input Parameters:
171647c6ae99SBarry Smith +   dm - the DM object
171747c6ae99SBarry Smith -   ctx - the user context
171847c6ae99SBarry Smith 
171947c6ae99SBarry Smith     Level: intermediate
172047c6ae99SBarry Smith 
1721e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
172247c6ae99SBarry Smith 
172347c6ae99SBarry Smith @*/
17241b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
172547c6ae99SBarry Smith {
172647c6ae99SBarry Smith   PetscFunctionBegin;
1727171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
172847c6ae99SBarry Smith   dm->ctx = ctx;
172947c6ae99SBarry Smith   PetscFunctionReturn(0);
173047c6ae99SBarry Smith }
173147c6ae99SBarry Smith 
173247c6ae99SBarry Smith #undef __FUNCT__
17331b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
173447c6ae99SBarry Smith /*@
17351b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
173647c6ae99SBarry Smith 
173747c6ae99SBarry Smith     Not Collective
173847c6ae99SBarry Smith 
173947c6ae99SBarry Smith     Input Parameter:
174047c6ae99SBarry Smith .   dm - the DM object
174147c6ae99SBarry Smith 
174247c6ae99SBarry Smith     Output Parameter:
174347c6ae99SBarry Smith .   ctx - the user context
174447c6ae99SBarry Smith 
174547c6ae99SBarry Smith     Level: intermediate
174647c6ae99SBarry Smith 
1747e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
174847c6ae99SBarry Smith 
174947c6ae99SBarry Smith @*/
17501b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
175147c6ae99SBarry Smith {
175247c6ae99SBarry Smith   PetscFunctionBegin;
1753171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17541b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
175547c6ae99SBarry Smith   PetscFunctionReturn(0);
175647c6ae99SBarry Smith }
175747c6ae99SBarry Smith 
175847c6ae99SBarry Smith #undef __FUNCT__
175947c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
17607e833e3aSBarry Smith /*@C
176147c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
176247c6ae99SBarry Smith 
176347c6ae99SBarry Smith     Logically Collective on DM
176447c6ae99SBarry Smith 
176547c6ae99SBarry Smith     Input Parameter:
176647c6ae99SBarry Smith +   dm - the DM object to destroy
176747c6ae99SBarry Smith -   f - the function to compute the initial guess
176847c6ae99SBarry Smith 
176947c6ae99SBarry Smith     Level: intermediate
177047c6ae99SBarry Smith 
1771e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
177247c6ae99SBarry Smith 
1773f07f9ceaSJed Brown @*/
17747087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
177547c6ae99SBarry Smith {
177647c6ae99SBarry Smith   PetscFunctionBegin;
1777171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
177847c6ae99SBarry Smith   dm->ops->initialguess = f;
177947c6ae99SBarry Smith   PetscFunctionReturn(0);
178047c6ae99SBarry Smith }
178147c6ae99SBarry Smith 
178247c6ae99SBarry Smith #undef __FUNCT__
178347c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
17847e833e3aSBarry Smith /*@C
178547c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
178647c6ae99SBarry Smith 
178747c6ae99SBarry Smith     Logically Collective on DM
178847c6ae99SBarry Smith 
178947c6ae99SBarry Smith     Input Parameter:
179047c6ae99SBarry Smith +   dm - the DM object
179147c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
179247c6ae99SBarry Smith 
179347c6ae99SBarry Smith     Level: intermediate
179447c6ae99SBarry Smith 
179547c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
179647c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
179747c6ae99SBarry Smith 
1798e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
179947c6ae99SBarry Smith          DMSetJacobian()
180047c6ae99SBarry Smith 
1801f07f9ceaSJed Brown @*/
18027087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
180347c6ae99SBarry Smith {
180447c6ae99SBarry Smith   PetscFunctionBegin;
1805171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
180647c6ae99SBarry Smith   dm->ops->function = f;
180747c6ae99SBarry Smith   if (f) {
180847c6ae99SBarry Smith     dm->ops->functionj = f;
180947c6ae99SBarry Smith   }
181047c6ae99SBarry Smith   PetscFunctionReturn(0);
181147c6ae99SBarry Smith }
181247c6ae99SBarry Smith 
181347c6ae99SBarry Smith #undef __FUNCT__
181447c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
18157e833e3aSBarry Smith /*@C
181647c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
181747c6ae99SBarry Smith 
181847c6ae99SBarry Smith     Logically Collective on DM
181947c6ae99SBarry Smith 
182047c6ae99SBarry Smith     Input Parameter:
182147c6ae99SBarry Smith +   dm - the DM object to destroy
182247c6ae99SBarry Smith -   f - the function to compute the matrix entries
182347c6ae99SBarry Smith 
182447c6ae99SBarry Smith     Level: intermediate
182547c6ae99SBarry Smith 
1826e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
182747c6ae99SBarry Smith          DMSetFunction()
182847c6ae99SBarry Smith 
1829f07f9ceaSJed Brown @*/
18307087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
183147c6ae99SBarry Smith {
183247c6ae99SBarry Smith   PetscFunctionBegin;
1833171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
183447c6ae99SBarry Smith   dm->ops->jacobian = f;
183547c6ae99SBarry Smith   PetscFunctionReturn(0);
183647c6ae99SBarry Smith }
183747c6ae99SBarry Smith 
183847c6ae99SBarry Smith #undef __FUNCT__
183908da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
184008da532bSDmitry Karpeev /*@C
184108da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
184208da532bSDmitry Karpeev 
184308da532bSDmitry Karpeev     Logically Collective on DM
184408da532bSDmitry Karpeev 
184508da532bSDmitry Karpeev     Input Parameter:
184608da532bSDmitry Karpeev +   dm - the DM object
184708da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
184808da532bSDmitry Karpeev 
184908da532bSDmitry Karpeev     Level: intermediate
185008da532bSDmitry Karpeev 
1851e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
185208da532bSDmitry Karpeev          DMSetJacobian()
185308da532bSDmitry Karpeev 
185408da532bSDmitry Karpeev @*/
185508da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
185608da532bSDmitry Karpeev {
185708da532bSDmitry Karpeev   PetscFunctionBegin;
185808da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
185908da532bSDmitry Karpeev   PetscFunctionReturn(0);
186008da532bSDmitry Karpeev }
186108da532bSDmitry Karpeev 
186208da532bSDmitry Karpeev #undef __FUNCT__
186308da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
186408da532bSDmitry Karpeev /*@
186508da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
186608da532bSDmitry Karpeev 
186708da532bSDmitry Karpeev     Not Collective
186808da532bSDmitry Karpeev 
186908da532bSDmitry Karpeev     Input Parameter:
187008da532bSDmitry Karpeev .   dm - the DM object to destroy
187108da532bSDmitry Karpeev 
187208da532bSDmitry Karpeev     Output Parameter:
187308da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
187408da532bSDmitry Karpeev 
187508da532bSDmitry Karpeev     Level: developer
187608da532bSDmitry Karpeev 
1877e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
187808da532bSDmitry Karpeev 
187908da532bSDmitry Karpeev @*/
188008da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
188108da532bSDmitry Karpeev {
188208da532bSDmitry Karpeev   PetscFunctionBegin;
188308da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
188408da532bSDmitry Karpeev   PetscFunctionReturn(0);
188508da532bSDmitry Karpeev }
188608da532bSDmitry Karpeev 
188708da532bSDmitry Karpeev #undef __FUNCT__
188808da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
188908da532bSDmitry Karpeev /*@C
189008da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
189108da532bSDmitry Karpeev 
189208da532bSDmitry Karpeev     Logically Collective on DM
189308da532bSDmitry Karpeev 
189408da532bSDmitry Karpeev     Input Parameters:
189508da532bSDmitry Karpeev +   dm - the DM object to destroy
189608da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
189708da532bSDmitry Karpeev 
189808da532bSDmitry Karpeev     Output parameters:
189908da532bSDmitry Karpeev +   xl - lower bound
190008da532bSDmitry Karpeev -   xu - upper bound
190108da532bSDmitry Karpeev 
190208da532bSDmitry Karpeev     Level: intermediate
190308da532bSDmitry Karpeev 
1904e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
190508da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
190608da532bSDmitry Karpeev 
190708da532bSDmitry Karpeev @*/
190808da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
190908da532bSDmitry Karpeev {
191008da532bSDmitry Karpeev   PetscErrorCode ierr;
191108da532bSDmitry Karpeev   PetscFunctionBegin;
191208da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
191308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
191408da532bSDmitry Karpeev   if(dm->ops->computevariablebounds) {
191508da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
191608da532bSDmitry Karpeev   }
191708da532bSDmitry Karpeev   else {
191808da532bSDmitry Karpeev     ierr = VecSet(xl,SNES_VI_NINF); CHKERRQ(ierr);
191908da532bSDmitry Karpeev     ierr = VecSet(xu,SNES_VI_INF);  CHKERRQ(ierr);
192008da532bSDmitry Karpeev   }
192108da532bSDmitry Karpeev   PetscFunctionReturn(0);
192208da532bSDmitry Karpeev }
192308da532bSDmitry Karpeev 
192408da532bSDmitry Karpeev #undef __FUNCT__
192547c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
192647c6ae99SBarry Smith /*@
192747c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
192847c6ae99SBarry Smith 
192947c6ae99SBarry Smith     Collective on DM
193047c6ae99SBarry Smith 
193147c6ae99SBarry Smith     Input Parameter:
193247c6ae99SBarry Smith +   dm - the DM object to destroy
193347c6ae99SBarry Smith -   x - the vector to hold the initial guess values
193447c6ae99SBarry Smith 
193547c6ae99SBarry Smith     Level: developer
193647c6ae99SBarry Smith 
1937e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
193847c6ae99SBarry Smith 
193947c6ae99SBarry Smith @*/
19407087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
194147c6ae99SBarry Smith {
194247c6ae99SBarry Smith   PetscErrorCode ierr;
194347c6ae99SBarry Smith   PetscFunctionBegin;
1944171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
194547c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
194647c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
194747c6ae99SBarry Smith   PetscFunctionReturn(0);
194847c6ae99SBarry Smith }
194947c6ae99SBarry Smith 
195047c6ae99SBarry Smith #undef __FUNCT__
195147c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
195247c6ae99SBarry Smith /*@
195347c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
195447c6ae99SBarry Smith 
195547c6ae99SBarry Smith     Not Collective
195647c6ae99SBarry Smith 
195747c6ae99SBarry Smith     Input Parameter:
195847c6ae99SBarry Smith .   dm - the DM object to destroy
195947c6ae99SBarry Smith 
196047c6ae99SBarry Smith     Output Parameter:
196147c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
196247c6ae99SBarry Smith 
196347c6ae99SBarry Smith     Level: developer
196447c6ae99SBarry Smith 
1965e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
196647c6ae99SBarry Smith 
196747c6ae99SBarry Smith @*/
19687087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
196947c6ae99SBarry Smith {
197047c6ae99SBarry Smith   PetscFunctionBegin;
1971171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
197247c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
197347c6ae99SBarry Smith   PetscFunctionReturn(0);
197447c6ae99SBarry Smith }
197547c6ae99SBarry Smith 
197647c6ae99SBarry Smith #undef __FUNCT__
197747c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
197847c6ae99SBarry Smith /*@
197947c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
198047c6ae99SBarry Smith 
198147c6ae99SBarry Smith     Not Collective
198247c6ae99SBarry Smith 
198347c6ae99SBarry Smith     Input Parameter:
198447c6ae99SBarry Smith .   dm - the DM object to destroy
198547c6ae99SBarry Smith 
198647c6ae99SBarry Smith     Output Parameter:
198747c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
198847c6ae99SBarry Smith 
198947c6ae99SBarry Smith     Level: developer
199047c6ae99SBarry Smith 
1991e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
199247c6ae99SBarry Smith 
199347c6ae99SBarry Smith @*/
19947087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
199547c6ae99SBarry Smith {
199647c6ae99SBarry Smith   PetscFunctionBegin;
1997171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
199847c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
199947c6ae99SBarry Smith   PetscFunctionReturn(0);
200047c6ae99SBarry Smith }
200147c6ae99SBarry Smith 
200247c6ae99SBarry Smith #undef __FUNCT__
200347c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
200447c6ae99SBarry Smith /*@
200547c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
200647c6ae99SBarry Smith 
200747c6ae99SBarry Smith     Not Collective
200847c6ae99SBarry Smith 
200947c6ae99SBarry Smith     Input Parameter:
201047c6ae99SBarry Smith .   dm - the DM object to destroy
201147c6ae99SBarry Smith 
201247c6ae99SBarry Smith     Output Parameter:
201347c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
201447c6ae99SBarry Smith 
201547c6ae99SBarry Smith     Level: developer
201647c6ae99SBarry Smith 
2017e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
201847c6ae99SBarry Smith 
201947c6ae99SBarry Smith @*/
20207087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
202147c6ae99SBarry Smith {
202247c6ae99SBarry Smith   PetscFunctionBegin;
2023171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
202447c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
202547c6ae99SBarry Smith   PetscFunctionReturn(0);
202647c6ae99SBarry Smith }
202747c6ae99SBarry Smith 
202847c6ae99SBarry Smith #undef  __FUNCT__
202908da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2030748fac09SDmitry Karpeev /*@C
203108da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
203208da532bSDmitry Karpeev 
203308da532bSDmitry Karpeev     Collective on DM
203408da532bSDmitry Karpeev 
203508da532bSDmitry Karpeev     Input Parameter:
203608da532bSDmitry Karpeev +   dm - the DM object
2037e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
203808da532bSDmitry Karpeev 
203908da532bSDmitry Karpeev     Level: developer
204008da532bSDmitry Karpeev 
2041e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
204208da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
204308da532bSDmitry Karpeev 
204408da532bSDmitry Karpeev @*/
204508da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
204608da532bSDmitry Karpeev {
204708da532bSDmitry Karpeev   PetscErrorCode ierr;
204808da532bSDmitry Karpeev   PetscFunctionBegin;
204908da532bSDmitry Karpeev   if (x) {
205008da532bSDmitry Karpeev     if (!dm->x) {
205108da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
205208da532bSDmitry Karpeev     }
205308da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
205408da532bSDmitry Karpeev   }
205508da532bSDmitry Karpeev   else if(dm->x) {
205608da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
205708da532bSDmitry Karpeev   }
205808da532bSDmitry Karpeev   PetscFunctionReturn(0);
205908da532bSDmitry Karpeev }
206008da532bSDmitry Karpeev 
206108da532bSDmitry Karpeev 
206208da532bSDmitry Karpeev #undef __FUNCT__
206347c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
206447c6ae99SBarry Smith /*@
206547c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
206647c6ae99SBarry Smith 
206747c6ae99SBarry Smith     Collective on DM
206847c6ae99SBarry Smith 
206947c6ae99SBarry Smith     Input Parameter:
207047c6ae99SBarry Smith +   dm - the DM object to destroy
207147c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
207247c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
207347c6ae99SBarry Smith 
207447c6ae99SBarry Smith     Level: developer
207547c6ae99SBarry Smith 
2076e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
207747c6ae99SBarry Smith          DMSetJacobian()
207847c6ae99SBarry Smith 
207947c6ae99SBarry Smith @*/
20807087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
208147c6ae99SBarry Smith {
208247c6ae99SBarry Smith   PetscErrorCode ierr;
208347c6ae99SBarry Smith   PetscFunctionBegin;
2084171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
208547c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
2086644e2e5bSBarry Smith   PetscStackPush("DM user function");
208747c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
2088644e2e5bSBarry Smith   PetscStackPop;
208947c6ae99SBarry Smith   PetscFunctionReturn(0);
209047c6ae99SBarry Smith }
209147c6ae99SBarry Smith 
209247c6ae99SBarry Smith 
209308da532bSDmitry Karpeev 
209447c6ae99SBarry Smith #undef __FUNCT__
209547c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
209647c6ae99SBarry Smith /*@
209747c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
209847c6ae99SBarry Smith 
209947c6ae99SBarry Smith     Collective on DM
210047c6ae99SBarry Smith 
210147c6ae99SBarry Smith     Input Parameter:
210247c6ae99SBarry Smith +   dm - the DM object
2103cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
210447c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
210547c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
210647c6ae99SBarry Smith 
210747c6ae99SBarry Smith     Level: developer
210847c6ae99SBarry Smith 
2109e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
211047c6ae99SBarry Smith          DMSetFunction()
211147c6ae99SBarry Smith 
211247c6ae99SBarry Smith @*/
21137087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
211447c6ae99SBarry Smith {
211547c6ae99SBarry Smith   PetscErrorCode ierr;
211647c6ae99SBarry Smith 
211747c6ae99SBarry Smith   PetscFunctionBegin;
2118171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
211947c6ae99SBarry Smith   if (!dm->ops->jacobian) {
212047c6ae99SBarry Smith     ISColoring     coloring;
212147c6ae99SBarry Smith     MatFDColoring  fd;
21222c9966d7SBarry Smith     const MatType  mtype;
212347c6ae99SBarry Smith 
21242c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
21252c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
212647c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
2127fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
212847c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
21290bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
21300bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
213171cd77b2SBarry Smith 
213247c6ae99SBarry Smith     dm->fd = fd;
213347c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
21342533e041SBarry Smith 
213571cd77b2SBarry Smith     /* don't know why this is needed */
213671cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
213747c6ae99SBarry Smith   }
213847c6ae99SBarry Smith   if (!x) x = dm->x;
213947c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
2140cab2e9ccSBarry Smith 
214171cd77b2SBarry Smith   /* if matrix depends on x; i.e. nonlinear problem, keep copy of input vector since needed by multigrid methods to generate coarse grid matrices */
2142649052a6SBarry Smith   if (x) {
2143cab2e9ccSBarry Smith     if (!dm->x) {
214471cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
2145cab2e9ccSBarry Smith     }
2146cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
2147649052a6SBarry Smith   }
2148a8248277SBarry Smith   if (A != B) {
2149a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2150a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2151a8248277SBarry Smith   }
215247c6ae99SBarry Smith   PetscFunctionReturn(0);
215347c6ae99SBarry Smith }
2154264ace61SBarry Smith 
2155cab2e9ccSBarry Smith 
2156264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2157264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2158264ace61SBarry Smith 
2159264ace61SBarry Smith #undef __FUNCT__
2160264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2161264ace61SBarry Smith /*@C
2162264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2163264ace61SBarry Smith 
2164264ace61SBarry Smith   Collective on DM
2165264ace61SBarry Smith 
2166264ace61SBarry Smith   Input Parameters:
2167264ace61SBarry Smith + dm     - The DM object
2168264ace61SBarry Smith - method - The name of the DM type
2169264ace61SBarry Smith 
2170264ace61SBarry Smith   Options Database Key:
2171264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2172264ace61SBarry Smith 
2173264ace61SBarry Smith   Notes:
2174e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2175264ace61SBarry Smith 
2176264ace61SBarry Smith   Level: intermediate
2177264ace61SBarry Smith 
2178264ace61SBarry Smith .keywords: DM, set, type
2179264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2180264ace61SBarry Smith @*/
21817087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
2182264ace61SBarry Smith {
2183264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2184264ace61SBarry Smith   PetscBool      match;
2185264ace61SBarry Smith   PetscErrorCode ierr;
2186264ace61SBarry Smith 
2187264ace61SBarry Smith   PetscFunctionBegin;
2188264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2189251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2190264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2191264ace61SBarry Smith 
2192264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
21934b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2194264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2195264ace61SBarry Smith 
2196264ace61SBarry Smith   if (dm->ops->destroy) {
2197264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2198b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2199264ace61SBarry Smith   }
2200264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2201264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2202264ace61SBarry Smith   PetscFunctionReturn(0);
2203264ace61SBarry Smith }
2204264ace61SBarry Smith 
2205264ace61SBarry Smith #undef __FUNCT__
2206264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2207264ace61SBarry Smith /*@C
2208264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2209264ace61SBarry Smith 
2210264ace61SBarry Smith   Not Collective
2211264ace61SBarry Smith 
2212264ace61SBarry Smith   Input Parameter:
2213264ace61SBarry Smith . dm  - The DM
2214264ace61SBarry Smith 
2215264ace61SBarry Smith   Output Parameter:
2216264ace61SBarry Smith . type - The DM type name
2217264ace61SBarry Smith 
2218264ace61SBarry Smith   Level: intermediate
2219264ace61SBarry Smith 
2220264ace61SBarry Smith .keywords: DM, get, type, name
2221264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2222264ace61SBarry Smith @*/
22237087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
2224264ace61SBarry Smith {
2225264ace61SBarry Smith   PetscErrorCode ierr;
2226264ace61SBarry Smith 
2227264ace61SBarry Smith   PetscFunctionBegin;
2228264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2229264ace61SBarry Smith   PetscValidCharPointer(type,2);
2230264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2231264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2232264ace61SBarry Smith   }
2233264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2234264ace61SBarry Smith   PetscFunctionReturn(0);
2235264ace61SBarry Smith }
2236264ace61SBarry Smith 
223767a56275SMatthew G Knepley #undef __FUNCT__
223867a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
223967a56275SMatthew G Knepley /*@C
224067a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
224167a56275SMatthew G Knepley 
224267a56275SMatthew G Knepley   Collective on DM
224367a56275SMatthew G Knepley 
224467a56275SMatthew G Knepley   Input Parameters:
224567a56275SMatthew G Knepley + dm - the DM
224667a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
224767a56275SMatthew G Knepley 
224867a56275SMatthew G Knepley   Output Parameter:
224967a56275SMatthew G Knepley . M - pointer to new DM
225067a56275SMatthew G Knepley 
225167a56275SMatthew G Knepley   Notes:
225267a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
225367a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
225467a56275SMatthew G Knepley   of the input DM.
225567a56275SMatthew G Knepley 
225667a56275SMatthew G Knepley   Level: intermediate
225767a56275SMatthew G Knepley 
225867a56275SMatthew G Knepley .seealso: DMCreate()
225967a56275SMatthew G Knepley @*/
226067a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
226167a56275SMatthew G Knepley {
226267a56275SMatthew G Knepley   DM             B;
226367a56275SMatthew G Knepley   char           convname[256];
226467a56275SMatthew G Knepley   PetscBool      sametype, issame;
226567a56275SMatthew G Knepley   PetscErrorCode ierr;
226667a56275SMatthew G Knepley 
226767a56275SMatthew G Knepley   PetscFunctionBegin;
226867a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
226967a56275SMatthew G Knepley   PetscValidType(dm,1);
227067a56275SMatthew G Knepley   PetscValidPointer(M,3);
2271251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
227267a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
227367a56275SMatthew G Knepley   {
227467a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
227567a56275SMatthew G Knepley 
227667a56275SMatthew G Knepley     /*
227767a56275SMatthew G Knepley        Order of precedence:
227867a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
227967a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
228067a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
228167a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
228267a56275SMatthew G Knepley        5) Use a really basic converter.
228367a56275SMatthew G Knepley     */
228467a56275SMatthew G Knepley 
228567a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
228667a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
228767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
228867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
228967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
229067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
229167a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
229267a56275SMatthew G Knepley     if (conv) goto foundconv;
229367a56275SMatthew G Knepley 
229467a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
229567a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
229667a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
229767a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
229867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
229967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
230067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
230167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
230267a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
230367a56275SMatthew G Knepley     if (conv) {
2304fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
230567a56275SMatthew G Knepley       goto foundconv;
230667a56275SMatthew G Knepley     }
230767a56275SMatthew G Knepley 
230867a56275SMatthew G Knepley #if 0
230967a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
231067a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2311fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
231267a56275SMatthew G Knepley     if (conv) goto foundconv;
231367a56275SMatthew G Knepley 
231467a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
231567a56275SMatthew G Knepley     if (dm->ops->convert) {
231667a56275SMatthew G Knepley       conv = dm->ops->convert;
231767a56275SMatthew G Knepley     }
231867a56275SMatthew G Knepley     if (conv) goto foundconv;
231967a56275SMatthew G Knepley #endif
232067a56275SMatthew G Knepley 
232167a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
232267a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
232367a56275SMatthew G Knepley 
232467a56275SMatthew G Knepley     foundconv:
232567a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
232667a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
232767a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
232867a56275SMatthew G Knepley   }
232967a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
233067a56275SMatthew G Knepley   PetscFunctionReturn(0);
233167a56275SMatthew G Knepley }
2332264ace61SBarry Smith 
2333264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2334264ace61SBarry Smith 
2335264ace61SBarry Smith #undef __FUNCT__
2336264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2337264ace61SBarry Smith /*@C
2338264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2339264ace61SBarry Smith 
2340264ace61SBarry Smith   Level: advanced
2341264ace61SBarry Smith @*/
23427087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2343264ace61SBarry Smith {
2344264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2345264ace61SBarry Smith   PetscErrorCode ierr;
2346264ace61SBarry Smith 
2347264ace61SBarry Smith   PetscFunctionBegin;
2348264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2349264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2350264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2351264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2352264ace61SBarry Smith   PetscFunctionReturn(0);
2353264ace61SBarry Smith }
2354264ace61SBarry Smith 
2355264ace61SBarry Smith 
2356264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2357264ace61SBarry Smith #undef __FUNCT__
2358264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2359264ace61SBarry Smith /*@C
2360264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2361264ace61SBarry Smith 
2362264ace61SBarry Smith    Not Collective
2363264ace61SBarry Smith 
2364264ace61SBarry Smith    Level: advanced
2365264ace61SBarry Smith 
2366264ace61SBarry Smith .keywords: DM, register, destroy
2367264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2368264ace61SBarry Smith @*/
23697087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2370264ace61SBarry Smith {
2371264ace61SBarry Smith   PetscErrorCode ierr;
2372264ace61SBarry Smith 
2373264ace61SBarry Smith   PetscFunctionBegin;
2374264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2375264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2376264ace61SBarry Smith   PetscFunctionReturn(0);
2377264ace61SBarry Smith }
237823f975d1SBarry Smith 
237923f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2380c6db04a5SJed Brown #include <mex.h>
238123f975d1SBarry Smith 
23823014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
238323f975d1SBarry Smith 
238423f975d1SBarry Smith #undef __FUNCT__
238523f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
238623f975d1SBarry Smith /*
238723f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
238823f975d1SBarry Smith                          DMSetFunctionMatlab().
238923f975d1SBarry Smith 
239023f975d1SBarry Smith    For linear problems x is null
239123f975d1SBarry Smith 
239223f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
239323f975d1SBarry Smith */
23947087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
239523f975d1SBarry Smith {
239623f975d1SBarry Smith   PetscErrorCode    ierr;
239723f975d1SBarry Smith   DMMatlabContext   *sctx;
239823f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
239923f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
240023f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
240123f975d1SBarry Smith 
240223f975d1SBarry Smith   PetscFunctionBegin;
240323f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
240423f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
240523f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
240623f975d1SBarry Smith 
240723f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
24081b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
240923f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
241023f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
24113014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
241223f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
241323f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
241423f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
241523f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2416b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
241723f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
241823f975d1SBarry Smith   mxDestroyArray(prhs[0]);
241923f975d1SBarry Smith   mxDestroyArray(prhs[1]);
242023f975d1SBarry Smith   mxDestroyArray(prhs[2]);
242123f975d1SBarry Smith   mxDestroyArray(prhs[3]);
242223f975d1SBarry Smith   mxDestroyArray(plhs[0]);
242323f975d1SBarry Smith   PetscFunctionReturn(0);
242423f975d1SBarry Smith }
242523f975d1SBarry Smith 
242623f975d1SBarry Smith 
242723f975d1SBarry Smith #undef __FUNCT__
242823f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
242923f975d1SBarry Smith /*
243023f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
243123f975d1SBarry Smith 
243223f975d1SBarry Smith */
24337087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
243423f975d1SBarry Smith {
243523f975d1SBarry Smith   PetscErrorCode    ierr;
243623f975d1SBarry Smith   DMMatlabContext   *sctx;
243723f975d1SBarry Smith 
243823f975d1SBarry Smith   PetscFunctionBegin;
2439171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
244023f975d1SBarry Smith   /* currently sctx is memory bleed */
24411b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
24423014e516SBarry Smith   if (!sctx) {
244323f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
24443014e516SBarry Smith   }
244523f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
24461b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
244723f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
244823f975d1SBarry Smith   PetscFunctionReturn(0);
244923f975d1SBarry Smith }
24503014e516SBarry Smith 
24513014e516SBarry Smith #undef __FUNCT__
24523014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
24533014e516SBarry Smith /*
24543014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
24553014e516SBarry Smith                          DMSetJacobianMatlab().
24563014e516SBarry Smith 
24573014e516SBarry Smith    For linear problems x is null
24583014e516SBarry Smith 
24593014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
24603014e516SBarry Smith */
24617087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
24623014e516SBarry Smith {
24633014e516SBarry Smith   PetscErrorCode    ierr;
24643014e516SBarry Smith   DMMatlabContext   *sctx;
24653014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
24663014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
24673014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
24683014e516SBarry Smith 
24693014e516SBarry Smith   PetscFunctionBegin;
24703014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24713014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
24723014e516SBarry Smith 
2473e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
24741b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
24753014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
24763014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
24773014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
24783014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
24793014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
24803014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
24813014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
24823014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
24833014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2484b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2485c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2486c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
24873014e516SBarry Smith   mxDestroyArray(prhs[0]);
24883014e516SBarry Smith   mxDestroyArray(prhs[1]);
24893014e516SBarry Smith   mxDestroyArray(prhs[2]);
24903014e516SBarry Smith   mxDestroyArray(prhs[3]);
24913014e516SBarry Smith   mxDestroyArray(prhs[4]);
24923014e516SBarry Smith   mxDestroyArray(plhs[0]);
24933014e516SBarry Smith   mxDestroyArray(plhs[1]);
24943014e516SBarry Smith   PetscFunctionReturn(0);
24953014e516SBarry Smith }
24963014e516SBarry Smith 
24973014e516SBarry Smith 
24983014e516SBarry Smith #undef __FUNCT__
24993014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
25003014e516SBarry Smith /*
25013014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
25023014e516SBarry Smith 
25033014e516SBarry Smith */
25047087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
25053014e516SBarry Smith {
25063014e516SBarry Smith   PetscErrorCode    ierr;
25073014e516SBarry Smith   DMMatlabContext   *sctx;
25083014e516SBarry Smith 
25093014e516SBarry Smith   PetscFunctionBegin;
2510171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25113014e516SBarry Smith   /* currently sctx is memory bleed */
25121b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
25133014e516SBarry Smith   if (!sctx) {
25143014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
25153014e516SBarry Smith   }
25163014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
25171b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
25183014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
25193014e516SBarry Smith   PetscFunctionReturn(0);
25203014e516SBarry Smith }
252123f975d1SBarry Smith #endif
2522b859378eSBarry Smith 
2523b859378eSBarry Smith #undef __FUNCT__
2524b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2525b859378eSBarry Smith /*@C
2526b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2527b859378eSBarry Smith   with DMView().
2528b859378eSBarry Smith 
2529b859378eSBarry Smith   Collective on PetscViewer
2530b859378eSBarry Smith 
2531b859378eSBarry Smith   Input Parameters:
2532b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2533b859378eSBarry Smith            some related function before a call to DMLoad().
2534b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2535b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2536b859378eSBarry Smith 
2537b859378eSBarry Smith    Level: intermediate
2538b859378eSBarry Smith 
2539b859378eSBarry Smith   Notes:
2540b859378eSBarry Smith   Defaults to the DM DA.
2541b859378eSBarry Smith 
2542b859378eSBarry Smith   Notes for advanced users:
2543b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2544b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2545b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2546b859378eSBarry Smith   format is
2547b859378eSBarry Smith .vb
2548b859378eSBarry Smith      has not yet been determined
2549b859378eSBarry Smith .ve
2550b859378eSBarry Smith 
2551b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2552b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2553b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2554b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2555b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2556b859378eSBarry Smith 
2557b859378eSBarry Smith   Concepts: vector^loading from file
2558b859378eSBarry Smith 
2559b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2560b859378eSBarry Smith @*/
2561b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2562b859378eSBarry Smith {
2563b859378eSBarry Smith   PetscErrorCode ierr;
2564b859378eSBarry Smith 
2565b859378eSBarry Smith   PetscFunctionBegin;
2566b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2567b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2568b859378eSBarry Smith 
2569b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2570b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2571b859378eSBarry Smith   }
2572b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2573b859378eSBarry Smith   PetscFunctionReturn(0);
2574b859378eSBarry Smith }
2575b859378eSBarry Smith 
25767da65231SMatthew G Knepley /******************************** FEM Support **********************************/
25777da65231SMatthew G Knepley 
25787da65231SMatthew G Knepley #undef __FUNCT__
25797da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
25807da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
25811d47ebbbSSatish Balay   PetscInt       f;
25821b30c384SMatthew G Knepley   PetscErrorCode ierr;
25831b30c384SMatthew G Knepley 
25847da65231SMatthew G Knepley   PetscFunctionBegin;
258574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
25861d47ebbbSSatish Balay   for(f = 0; f < len; ++f) {
258774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
25887da65231SMatthew G Knepley   }
25897da65231SMatthew G Knepley   PetscFunctionReturn(0);
25907da65231SMatthew G Knepley }
25917da65231SMatthew G Knepley 
25927da65231SMatthew G Knepley #undef __FUNCT__
25937da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
25947da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
25951b30c384SMatthew G Knepley   PetscInt       f, g;
25967da65231SMatthew G Knepley   PetscErrorCode ierr;
25977da65231SMatthew G Knepley 
25987da65231SMatthew G Knepley   PetscFunctionBegin;
259974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26001d47ebbbSSatish Balay   for(f = 0; f < rows; ++f) {
260174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
26021d47ebbbSSatish Balay     for(g = 0; g < cols; ++g) {
260374778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
26047da65231SMatthew G Knepley     }
260574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
26067da65231SMatthew G Knepley   }
26077da65231SMatthew G Knepley   PetscFunctionReturn(0);
26087da65231SMatthew G Knepley }
2609e7c4fc90SDmitry Karpeev 
2610970e74d5SMatthew G Knepley #undef __FUNCT__
2611970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
2612970e74d5SMatthew G Knepley /*@C
2613970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
2614970e74d5SMatthew G Knepley 
2615970e74d5SMatthew G Knepley   Not collective
2616970e74d5SMatthew G Knepley 
2617970e74d5SMatthew G Knepley   Input Parameter:
2618970e74d5SMatthew G Knepley . dm - The DM
2619970e74d5SMatthew G Knepley 
2620970e74d5SMatthew G Knepley   Output Parameter:
2621970e74d5SMatthew G Knepley . lf - The local residual function
2622970e74d5SMatthew G Knepley 
2623970e74d5SMatthew G Knepley    Calling sequence of lf:
2624970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2625970e74d5SMatthew G Knepley 
2626970e74d5SMatthew G Knepley +  snes - the SNES context
2627970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2628970e74d5SMatthew G Knepley .  f - local vector to put residual in
2629970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2630970e74d5SMatthew G Knepley 
2631970e74d5SMatthew G Knepley   Level: intermediate
2632970e74d5SMatthew G Knepley 
2633970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2634970e74d5SMatthew G Knepley @*/
2635970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
2636970e74d5SMatthew G Knepley {
2637970e74d5SMatthew G Knepley   PetscFunctionBegin;
2638970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2639970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
2640970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2641970e74d5SMatthew G Knepley }
2642970e74d5SMatthew G Knepley 
2643970e74d5SMatthew G Knepley #undef __FUNCT__
2644970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
2645970e74d5SMatthew G Knepley /*@C
2646970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
2647970e74d5SMatthew G Knepley 
2648970e74d5SMatthew G Knepley   Not collective
2649970e74d5SMatthew G Knepley 
2650970e74d5SMatthew G Knepley   Input Parameters:
2651970e74d5SMatthew G Knepley + dm - The DM
2652970e74d5SMatthew G Knepley - lf - The local residual function
2653970e74d5SMatthew G Knepley 
2654970e74d5SMatthew G Knepley    Calling sequence of lf:
2655970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2656970e74d5SMatthew G Knepley 
2657970e74d5SMatthew G Knepley +  snes - the SNES context
2658970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2659970e74d5SMatthew G Knepley .  f - local vector to put residual in
2660970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2661970e74d5SMatthew G Knepley 
2662970e74d5SMatthew G Knepley   Level: intermediate
2663970e74d5SMatthew G Knepley 
2664970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2665970e74d5SMatthew G Knepley @*/
2666970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
2667970e74d5SMatthew G Knepley {
2668970e74d5SMatthew G Knepley   PetscFunctionBegin;
2669970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2670970e74d5SMatthew G Knepley   dm->lf = lf;
2671970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2672970e74d5SMatthew G Knepley }
2673970e74d5SMatthew G Knepley 
2674970e74d5SMatthew G Knepley #undef __FUNCT__
2675970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
2676970e74d5SMatthew G Knepley /*@C
2677970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
2678970e74d5SMatthew G Knepley 
2679970e74d5SMatthew G Knepley   Not collective
2680970e74d5SMatthew G Knepley 
2681970e74d5SMatthew G Knepley   Input Parameter:
2682970e74d5SMatthew G Knepley . dm - The DM
2683970e74d5SMatthew G Knepley 
2684970e74d5SMatthew G Knepley   Output Parameter:
2685970e74d5SMatthew G Knepley . lj - The local Jacobian function
2686970e74d5SMatthew G Knepley 
2687970e74d5SMatthew G Knepley    Calling sequence of lj:
2688970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2689970e74d5SMatthew G Knepley 
2690970e74d5SMatthew G Knepley +  snes - the SNES context
2691970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2692970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2693970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2694970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2695970e74d5SMatthew G Knepley 
2696970e74d5SMatthew G Knepley   Level: intermediate
2697970e74d5SMatthew G Knepley 
2698970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2699970e74d5SMatthew G Knepley @*/
2700970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
2701970e74d5SMatthew G Knepley {
2702970e74d5SMatthew G Knepley   PetscFunctionBegin;
2703970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2704970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
2705970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2706970e74d5SMatthew G Knepley }
2707970e74d5SMatthew G Knepley 
2708970e74d5SMatthew G Knepley #undef __FUNCT__
2709970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
2710970e74d5SMatthew G Knepley /*@C
2711970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
2712970e74d5SMatthew G Knepley 
2713970e74d5SMatthew G Knepley   Not collective
2714970e74d5SMatthew G Knepley 
2715970e74d5SMatthew G Knepley   Input Parameters:
2716970e74d5SMatthew G Knepley + dm - The DM
2717970e74d5SMatthew G Knepley - lj - The local Jacobian function
2718970e74d5SMatthew G Knepley 
2719970e74d5SMatthew G Knepley    Calling sequence of lj:
2720970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2721970e74d5SMatthew G Knepley 
2722970e74d5SMatthew G Knepley +  snes - the SNES context
2723970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2724970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2725970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2726970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2727970e74d5SMatthew G Knepley 
2728970e74d5SMatthew G Knepley   Level: intermediate
2729970e74d5SMatthew G Knepley 
2730970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2731970e74d5SMatthew G Knepley @*/
2732970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
2733970e74d5SMatthew G Knepley {
2734970e74d5SMatthew G Knepley   PetscFunctionBegin;
2735970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2736970e74d5SMatthew G Knepley   dm->lj = lj;
2737970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2738970e74d5SMatthew G Knepley }
273988ed4aceSMatthew G Knepley 
274088ed4aceSMatthew G Knepley #undef __FUNCT__
274188ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
274288ed4aceSMatthew G Knepley /*@
274388ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
274488ed4aceSMatthew G Knepley 
274588ed4aceSMatthew G Knepley   Input Parameter:
274688ed4aceSMatthew G Knepley . dm - The DM
274788ed4aceSMatthew G Knepley 
274888ed4aceSMatthew G Knepley   Output Parameter:
274988ed4aceSMatthew G Knepley . section - The PetscSection
275088ed4aceSMatthew G Knepley 
275188ed4aceSMatthew G Knepley   Level: intermediate
275288ed4aceSMatthew G Knepley 
275388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
275488ed4aceSMatthew G Knepley 
275588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
275688ed4aceSMatthew G Knepley @*/
275788ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
275888ed4aceSMatthew G Knepley   PetscFunctionBegin;
275988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
276088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
276188ed4aceSMatthew G Knepley   *section = dm->defaultSection;
276288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
276388ed4aceSMatthew G Knepley }
276488ed4aceSMatthew G Knepley 
276588ed4aceSMatthew G Knepley #undef __FUNCT__
276688ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
276788ed4aceSMatthew G Knepley /*@
276888ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
276988ed4aceSMatthew G Knepley 
277088ed4aceSMatthew G Knepley   Input Parameters:
277188ed4aceSMatthew G Knepley + dm - The DM
277288ed4aceSMatthew G Knepley - section - The PetscSection
277388ed4aceSMatthew G Knepley 
277488ed4aceSMatthew G Knepley   Level: intermediate
277588ed4aceSMatthew G Knepley 
277688ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
277788ed4aceSMatthew G Knepley 
277888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
277988ed4aceSMatthew G Knepley @*/
278088ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
278188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
278288ed4aceSMatthew G Knepley 
278388ed4aceSMatthew G Knepley   PetscFunctionBegin;
278488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
278588ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
278688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
278788ed4aceSMatthew G Knepley   dm->defaultSection = section;
278888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
278988ed4aceSMatthew G Knepley }
279088ed4aceSMatthew G Knepley 
279188ed4aceSMatthew G Knepley #undef __FUNCT__
279288ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
279388ed4aceSMatthew G Knepley /*@
279488ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
279588ed4aceSMatthew G Knepley 
279688ed4aceSMatthew G Knepley   Input Parameter:
279788ed4aceSMatthew G Knepley . dm - The DM
279888ed4aceSMatthew G Knepley 
279988ed4aceSMatthew G Knepley   Output Parameter:
280088ed4aceSMatthew G Knepley . section - The PetscSection
280188ed4aceSMatthew G Knepley 
280288ed4aceSMatthew G Knepley   Level: intermediate
280388ed4aceSMatthew G Knepley 
280488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
280588ed4aceSMatthew G Knepley 
280688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
280788ed4aceSMatthew G Knepley @*/
280888ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
280988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
281088ed4aceSMatthew G Knepley 
281188ed4aceSMatthew G Knepley   PetscFunctionBegin;
281288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
281388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
281488ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
281588ed4aceSMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, &dm->defaultGlobalSection);CHKERRQ(ierr);
281688ed4aceSMatthew G Knepley   }
281788ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
281888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
281988ed4aceSMatthew G Knepley }
282088ed4aceSMatthew G Knepley 
282188ed4aceSMatthew G Knepley #undef __FUNCT__
282288ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
282388ed4aceSMatthew G Knepley /*@
282488ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
282588ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
282688ed4aceSMatthew G Knepley 
282788ed4aceSMatthew G Knepley   Input Parameter:
282888ed4aceSMatthew G Knepley . dm - The DM
282988ed4aceSMatthew G Knepley 
283088ed4aceSMatthew G Knepley   Output Parameter:
283188ed4aceSMatthew G Knepley . sf - The PetscSF
283288ed4aceSMatthew G Knepley 
283388ed4aceSMatthew G Knepley   Level: intermediate
283488ed4aceSMatthew G Knepley 
283588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
283688ed4aceSMatthew G Knepley 
283788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
283888ed4aceSMatthew G Knepley @*/
283988ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
284088ed4aceSMatthew G Knepley   PetscInt       nroots;
284188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
284288ed4aceSMatthew G Knepley 
284388ed4aceSMatthew G Knepley   PetscFunctionBegin;
284488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
284588ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
284688ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
284788ed4aceSMatthew G Knepley   if (nroots < 0) {
284888ed4aceSMatthew G Knepley     PetscSection section, gSection;
284988ed4aceSMatthew G Knepley 
285088ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
285131ea6d37SMatthew G Knepley     if (section) {
285288ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
285388ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
285431ea6d37SMatthew G Knepley     } else {
285531ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
285631ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
285731ea6d37SMatthew G Knepley     }
285888ed4aceSMatthew G Knepley   }
285988ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
286088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
286188ed4aceSMatthew G Knepley }
286288ed4aceSMatthew G Knepley 
286388ed4aceSMatthew G Knepley #undef __FUNCT__
286488ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
286588ed4aceSMatthew G Knepley /*@
286688ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
286788ed4aceSMatthew G Knepley 
286888ed4aceSMatthew G Knepley   Input Parameters:
286988ed4aceSMatthew G Knepley + dm - The DM
287088ed4aceSMatthew G Knepley - sf - The PetscSF
287188ed4aceSMatthew G Knepley 
287288ed4aceSMatthew G Knepley   Level: intermediate
287388ed4aceSMatthew G Knepley 
287488ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
287588ed4aceSMatthew G Knepley 
287688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
287788ed4aceSMatthew G Knepley @*/
287888ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
287988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
288088ed4aceSMatthew G Knepley 
288188ed4aceSMatthew G Knepley   PetscFunctionBegin;
288288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
288388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
288488ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
288588ed4aceSMatthew G Knepley   dm->defaultSF = sf;
288688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
288788ed4aceSMatthew G Knepley }
288888ed4aceSMatthew G Knepley 
288988ed4aceSMatthew G Knepley #undef __FUNCT__
289088ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
289188ed4aceSMatthew G Knepley /*@C
289288ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
289388ed4aceSMatthew G Knepley   describing the data layout.
289488ed4aceSMatthew G Knepley 
289588ed4aceSMatthew G Knepley   Input Parameters:
289688ed4aceSMatthew G Knepley + dm - The DM
289788ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
289888ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
289988ed4aceSMatthew G Knepley 
290088ed4aceSMatthew G Knepley   Level: intermediate
290188ed4aceSMatthew G Knepley 
290288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
290388ed4aceSMatthew G Knepley @*/
290488ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
290588ed4aceSMatthew G Knepley {
290688ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
290788ed4aceSMatthew G Knepley   PetscLayout     layout;
290888ed4aceSMatthew G Knepley   const PetscInt *ranges;
290988ed4aceSMatthew G Knepley   PetscInt       *local;
291088ed4aceSMatthew G Knepley   PetscSFNode    *remote;
291188ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
291288ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
291388ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
291488ed4aceSMatthew G Knepley 
291588ed4aceSMatthew G Knepley   PetscFunctionBegin;
291688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
291788ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
291888ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
291988ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
292088ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
292188ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
292288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
292388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
292488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
292588ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
292688ed4aceSMatthew G Knepley   for(p = pStart, nleaves = 0; p < pEnd; ++p) {
292788ed4aceSMatthew G Knepley     PetscInt dof, cdof;
292888ed4aceSMatthew G Knepley 
292988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &dof);CHKERRQ(ierr);
293088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &cdof);CHKERRQ(ierr);
293188ed4aceSMatthew G Knepley     nleaves += dof < 0 ? -(dof+1)-cdof : dof-cdof;
293288ed4aceSMatthew G Knepley   }
293388ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
293488ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
293588ed4aceSMatthew G Knepley   for(p = pStart, l = 0; p < pEnd; ++p) {
293688ed4aceSMatthew G Knepley     PetscInt *cind;
293788ed4aceSMatthew G Knepley     PetscInt  dof, gdof, cdof, dim, off, goff, d, c;
293888ed4aceSMatthew G Knepley 
293988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
294088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
294188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
294288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
294388ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
294488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
294588ed4aceSMatthew G Knepley     dim  = dof-cdof;
294688ed4aceSMatthew G Knepley     for(d = 0, c = 0; d < dof; ++d) {
294788ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
294888ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
294988ed4aceSMatthew G Knepley     }
295088ed4aceSMatthew G Knepley     if (gdof < 0) {
295188ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
295288ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
295388ed4aceSMatthew G Knepley 
295488ed4aceSMatthew G Knepley         for(r = 0; r < size; ++r) {
295588ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
295688ed4aceSMatthew G Knepley         }
295788ed4aceSMatthew G Knepley         remote[l].rank  = r;
295888ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
295988ed4aceSMatthew G Knepley       }
296088ed4aceSMatthew G Knepley     } else {
296188ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
296288ed4aceSMatthew G Knepley         remote[l].rank  = rank;
296388ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
296488ed4aceSMatthew G Knepley       }
296588ed4aceSMatthew G Knepley     }
296688ed4aceSMatthew G Knepley   }
296788ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
296888ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
296988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
297088ed4aceSMatthew G Knepley }
2971