xref: /petsc/src/dm/interface/dm.c (revision 0598a293f3c5197504a0872cfb556bfe5420706c)
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 */
211c833c3b5SJed Brown   {
212c833c3b5SJed 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;
218c833c3b5SJed Brown   }
219c833c3b5SJed Brown   {
220c833c3b5SJed Brown     DMRefineHookLink link,next;
221c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
222c833c3b5SJed Brown       next = link->next;
223c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
224c833c3b5SJed Brown     }
225c833c3b5SJed Brown     (*dm)->refinehook = PETSC_NULL;
226c833c3b5SJed 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;
1043c833c3b5SJed 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;
1058*0598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1059656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
1060c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1061c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1062c833c3b5SJed Brown     }
1063c833c3b5SJed Brown   }
1064c833c3b5SJed Brown   PetscFunctionReturn(0);
1065c833c3b5SJed Brown }
1066c833c3b5SJed Brown 
1067c833c3b5SJed Brown #undef __FUNCT__
1068c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1069c833c3b5SJed Brown /*@
1070c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1071c833c3b5SJed Brown 
1072c833c3b5SJed Brown    Logically Collective
1073c833c3b5SJed Brown 
1074c833c3b5SJed Brown    Input Arguments:
1075c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1076c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1077c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1078c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1079c833c3b5SJed Brown 
1080c833c3b5SJed Brown    Calling sequence of refinehook:
1081c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1082c833c3b5SJed Brown 
1083c833c3b5SJed Brown +  coarse - coarse level DM
1084c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1085c833c3b5SJed Brown -  ctx - optional user-defined function context
1086c833c3b5SJed Brown 
1087c833c3b5SJed Brown    Calling sequence for interphook:
1088c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1089c833c3b5SJed Brown 
1090c833c3b5SJed Brown +  coarse - coarse level DM
1091c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1092c833c3b5SJed Brown .  fine - fine level DM to update
1093c833c3b5SJed Brown -  ctx - optional user-defined function context
1094c833c3b5SJed Brown 
1095c833c3b5SJed Brown    Level: advanced
1096c833c3b5SJed Brown 
1097c833c3b5SJed Brown    Notes:
1098c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1099c833c3b5SJed Brown 
1100c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1101c833c3b5SJed Brown 
1102c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1103c833c3b5SJed Brown @*/
1104c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1105c833c3b5SJed Brown {
1106c833c3b5SJed Brown   PetscErrorCode ierr;
1107c833c3b5SJed Brown   DMRefineHookLink link,*p;
1108c833c3b5SJed Brown 
1109c833c3b5SJed Brown   PetscFunctionBegin;
1110c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1111c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1112c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1113c833c3b5SJed Brown   link->refinehook = refinehook;
1114c833c3b5SJed Brown   link->interphook = interphook;
1115c833c3b5SJed Brown   link->ctx = ctx;
1116c833c3b5SJed Brown   link->next = PETSC_NULL;
1117c833c3b5SJed Brown   *p = link;
1118c833c3b5SJed Brown   PetscFunctionReturn(0);
1119c833c3b5SJed Brown }
1120c833c3b5SJed Brown 
1121c833c3b5SJed Brown #undef __FUNCT__
1122c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1123c833c3b5SJed Brown /*@
1124c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1125c833c3b5SJed Brown 
1126c833c3b5SJed Brown    Collective if any hooks are
1127c833c3b5SJed Brown 
1128c833c3b5SJed Brown    Input Arguments:
1129c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1130c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1131c833c3b5SJed Brown -  fine - finer DM to update
1132c833c3b5SJed Brown 
1133c833c3b5SJed Brown    Level: developer
1134c833c3b5SJed Brown 
1135c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1136c833c3b5SJed Brown @*/
1137c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1138c833c3b5SJed Brown {
1139c833c3b5SJed Brown   PetscErrorCode ierr;
1140c833c3b5SJed Brown   DMRefineHookLink link;
1141c833c3b5SJed Brown 
1142c833c3b5SJed Brown   PetscFunctionBegin;
1143c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1144c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
11454057135bSMatthew G Knepley   }
114647c6ae99SBarry Smith   PetscFunctionReturn(0);
114747c6ae99SBarry Smith }
114847c6ae99SBarry Smith 
114947c6ae99SBarry Smith #undef __FUNCT__
1150eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1151eb3f98d2SBarry Smith /*@
1152eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1153eb3f98d2SBarry Smith 
1154eb3f98d2SBarry Smith     Not Collective
1155eb3f98d2SBarry Smith 
1156eb3f98d2SBarry Smith     Input Parameter:
1157eb3f98d2SBarry Smith .   dm - the DM object
1158eb3f98d2SBarry Smith 
1159eb3f98d2SBarry Smith     Output Parameter:
1160eb3f98d2SBarry Smith .   level - number of refinements
1161eb3f98d2SBarry Smith 
1162eb3f98d2SBarry Smith     Level: developer
1163eb3f98d2SBarry Smith 
11646a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1165eb3f98d2SBarry Smith 
1166eb3f98d2SBarry Smith @*/
1167eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1168eb3f98d2SBarry Smith {
1169eb3f98d2SBarry Smith   PetscFunctionBegin;
1170eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1171eb3f98d2SBarry Smith   *level = dm->levelup;
1172eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1173eb3f98d2SBarry Smith }
1174eb3f98d2SBarry Smith 
1175eb3f98d2SBarry Smith #undef __FUNCT__
117647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
117747c6ae99SBarry Smith /*@
117847c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
117947c6ae99SBarry Smith 
118047c6ae99SBarry Smith     Neighbor-wise Collective on DM
118147c6ae99SBarry Smith 
118247c6ae99SBarry Smith     Input Parameters:
118347c6ae99SBarry Smith +   dm - the DM object
118447c6ae99SBarry Smith .   g - the global vector
118547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
118647c6ae99SBarry Smith -   l - the local vector
118747c6ae99SBarry Smith 
118847c6ae99SBarry Smith 
118947c6ae99SBarry Smith     Level: beginner
119047c6ae99SBarry Smith 
1191e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
119247c6ae99SBarry Smith 
119347c6ae99SBarry Smith @*/
11947087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
119547c6ae99SBarry Smith {
11967128ae9fSMatthew G Knepley   PetscSF        sf;
119747c6ae99SBarry Smith   PetscErrorCode ierr;
119847c6ae99SBarry Smith 
119947c6ae99SBarry Smith   PetscFunctionBegin;
1200171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12017128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
12027128ae9fSMatthew G Knepley   if (sf) {
12037128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
12047128ae9fSMatthew G Knepley 
12057128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
12067128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
12077128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
12087128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
12097128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
12107128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
12117128ae9fSMatthew G Knepley   } else {
1212843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
12137128ae9fSMatthew G Knepley   }
121447c6ae99SBarry Smith   PetscFunctionReturn(0);
121547c6ae99SBarry Smith }
121647c6ae99SBarry Smith 
121747c6ae99SBarry Smith #undef __FUNCT__
121847c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
121947c6ae99SBarry Smith /*@
122047c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
122147c6ae99SBarry Smith 
122247c6ae99SBarry Smith     Neighbor-wise Collective on DM
122347c6ae99SBarry Smith 
122447c6ae99SBarry Smith     Input Parameters:
122547c6ae99SBarry Smith +   dm - the DM object
122647c6ae99SBarry Smith .   g - the global vector
122747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
122847c6ae99SBarry Smith -   l - the local vector
122947c6ae99SBarry Smith 
123047c6ae99SBarry Smith 
123147c6ae99SBarry Smith     Level: beginner
123247c6ae99SBarry Smith 
1233e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
123447c6ae99SBarry Smith 
123547c6ae99SBarry Smith @*/
12367087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
123747c6ae99SBarry Smith {
12387128ae9fSMatthew G Knepley   PetscSF        sf;
123947c6ae99SBarry Smith   PetscErrorCode ierr;
124061a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
124147c6ae99SBarry Smith 
124247c6ae99SBarry Smith   PetscFunctionBegin;
1243171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12447128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
12457128ae9fSMatthew G Knepley   if (sf) {
12467128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
12477128ae9fSMatthew G Knepley 
12487128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
12497128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
12507128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
12517128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
12527128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
12537128ae9fSMatthew G Knepley   } else {
1254843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
12557128ae9fSMatthew G Knepley   }
125647c6ae99SBarry Smith   PetscFunctionReturn(0);
125747c6ae99SBarry Smith }
125847c6ae99SBarry Smith 
125947c6ae99SBarry Smith #undef __FUNCT__
12609a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
126147c6ae99SBarry Smith /*@
12629a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
12639a42bb27SBarry Smith 
12649a42bb27SBarry Smith     Neighbor-wise Collective on DM
12659a42bb27SBarry Smith 
12669a42bb27SBarry Smith     Input Parameters:
12679a42bb27SBarry Smith +   dm - the DM object
1268f6813fd5SJed Brown .   l - the local vector
12699a42bb27SBarry 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
12709a42bb27SBarry Smith            base point.
1271f6813fd5SJed Brown - - the global vector
12729a42bb27SBarry Smith 
12739a42bb27SBarry 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
12749a42bb27SBarry 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
12759a42bb27SBarry Smith            global array to the final global array with VecAXPY().
12769a42bb27SBarry Smith 
12779a42bb27SBarry Smith     Level: beginner
12789a42bb27SBarry Smith 
1279e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
12809a42bb27SBarry Smith 
12819a42bb27SBarry Smith @*/
12827087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
12839a42bb27SBarry Smith {
12847128ae9fSMatthew G Knepley   PetscSF        sf;
12859a42bb27SBarry Smith   PetscErrorCode ierr;
12869a42bb27SBarry Smith 
12879a42bb27SBarry Smith   PetscFunctionBegin;
1288171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12897128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
12907128ae9fSMatthew G Knepley   if (sf) {
12917128ae9fSMatthew G Knepley     MPI_Op       op;
12927128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
12937128ae9fSMatthew G Knepley 
12947128ae9fSMatthew G Knepley     switch(mode) {
12957128ae9fSMatthew G Knepley     case INSERT_VALUES:
12967128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
12977128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
12987128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
12997128ae9fSMatthew G Knepley #else
13007128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
13017128ae9fSMatthew G Knepley #endif
13027128ae9fSMatthew G Knepley     case ADD_VALUES:
13037128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
13047128ae9fSMatthew G Knepley       op = MPI_SUM; break;
13057128ae9fSMatthew G Knepley   default:
13067128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
13077128ae9fSMatthew G Knepley     }
13087128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
13097128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
13107128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
13117128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
13127128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
13137128ae9fSMatthew G Knepley   } else {
1314843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
13157128ae9fSMatthew G Knepley   }
13169a42bb27SBarry Smith   PetscFunctionReturn(0);
13179a42bb27SBarry Smith }
13189a42bb27SBarry Smith 
13199a42bb27SBarry Smith #undef __FUNCT__
13209a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
13219a42bb27SBarry Smith /*@
13229a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
132347c6ae99SBarry Smith 
132447c6ae99SBarry Smith     Neighbor-wise Collective on DM
132547c6ae99SBarry Smith 
132647c6ae99SBarry Smith     Input Parameters:
132747c6ae99SBarry Smith +   dm - the DM object
1328f6813fd5SJed Brown .   l - the local vector
132947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1330f6813fd5SJed Brown -   g - the global vector
133147c6ae99SBarry Smith 
133247c6ae99SBarry Smith 
133347c6ae99SBarry Smith     Level: beginner
133447c6ae99SBarry Smith 
1335e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
133647c6ae99SBarry Smith 
133747c6ae99SBarry Smith @*/
13387087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
133947c6ae99SBarry Smith {
13407128ae9fSMatthew G Knepley   PetscSF        sf;
134147c6ae99SBarry Smith   PetscErrorCode ierr;
134247c6ae99SBarry Smith 
134347c6ae99SBarry Smith   PetscFunctionBegin;
1344171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13457128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
13467128ae9fSMatthew G Knepley   if (sf) {
13477128ae9fSMatthew G Knepley     MPI_Op       op;
13487128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
13497128ae9fSMatthew G Knepley 
13507128ae9fSMatthew G Knepley     switch(mode) {
13517128ae9fSMatthew G Knepley     case INSERT_VALUES:
13527128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
13537128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
13547128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
13557128ae9fSMatthew G Knepley #else
13567128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
13577128ae9fSMatthew G Knepley #endif
13587128ae9fSMatthew G Knepley     case ADD_VALUES:
13597128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
13607128ae9fSMatthew G Knepley       op = MPI_SUM; break;
13617128ae9fSMatthew G Knepley     default:
13627128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
13637128ae9fSMatthew G Knepley     }
13647128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
13657128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
13667128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
13677128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
13687128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
13697128ae9fSMatthew G Knepley   } else {
1370843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
13717128ae9fSMatthew G Knepley   }
137247c6ae99SBarry Smith   PetscFunctionReturn(0);
137347c6ae99SBarry Smith }
137447c6ae99SBarry Smith 
137547c6ae99SBarry Smith #undef __FUNCT__
137647c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
137747c6ae99SBarry Smith /*@
137847c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
137947c6ae99SBarry Smith 
138047c6ae99SBarry Smith     Collective on DM
138147c6ae99SBarry Smith 
138247c6ae99SBarry Smith     Input Parameter:
138347c6ae99SBarry Smith +   dm - the DM object
138447c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
138547c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
138647c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
138747c6ae99SBarry Smith 
138847c6ae99SBarry Smith     Level: developer
138947c6ae99SBarry Smith 
1390e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
139147c6ae99SBarry Smith          DMSetFunction()
139247c6ae99SBarry Smith 
139347c6ae99SBarry Smith @*/
13947087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
139547c6ae99SBarry Smith {
139647c6ae99SBarry Smith   PetscErrorCode ierr;
1397171400e9SBarry Smith 
139847c6ae99SBarry Smith   PetscFunctionBegin;
1399171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
140047c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
140147c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
140247c6ae99SBarry Smith   if (A != B) {
140347c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
140447c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
140547c6ae99SBarry Smith   }
140647c6ae99SBarry Smith   PetscFunctionReturn(0);
140747c6ae99SBarry Smith }
140847c6ae99SBarry Smith 
140947c6ae99SBarry Smith #undef __FUNCT__
141047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
141147c6ae99SBarry Smith /*@
141247c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
141347c6ae99SBarry Smith 
141447c6ae99SBarry Smith     Collective on DM
141547c6ae99SBarry Smith 
141647c6ae99SBarry Smith     Input Parameter:
141747c6ae99SBarry Smith +   dm - the DM object
141891d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
141947c6ae99SBarry Smith 
142047c6ae99SBarry Smith     Output Parameter:
142147c6ae99SBarry Smith .   dmc - the coarsened DM
142247c6ae99SBarry Smith 
142347c6ae99SBarry Smith     Level: developer
142447c6ae99SBarry Smith 
1425e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
142647c6ae99SBarry Smith 
142747c6ae99SBarry Smith @*/
14287087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
142947c6ae99SBarry Smith {
143047c6ae99SBarry Smith   PetscErrorCode ierr;
1431b17ce1afSJed Brown   DMCoarsenHookLink link;
143247c6ae99SBarry Smith 
143347c6ae99SBarry Smith   PetscFunctionBegin;
1434171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
143547c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
143643842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
143747c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
143847c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
143947c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
144047c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
144147c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
144247c6ae99SBarry Smith   }
14438cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1444644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
1445*0598a293SJed Brown   (*dmc)->levelup   = dm->levelup;
1446656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1447b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1448b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1449b17ce1afSJed Brown   }
1450b17ce1afSJed Brown   PetscFunctionReturn(0);
1451b17ce1afSJed Brown }
1452b17ce1afSJed Brown 
1453b17ce1afSJed Brown #undef __FUNCT__
1454b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1455b17ce1afSJed Brown /*@
1456b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1457b17ce1afSJed Brown 
1458b17ce1afSJed Brown    Logically Collective
1459b17ce1afSJed Brown 
1460b17ce1afSJed Brown    Input Arguments:
1461b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1462b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1463b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1464b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1465b17ce1afSJed Brown 
1466b17ce1afSJed Brown    Calling sequence of coarsenhook:
1467b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1468b17ce1afSJed Brown 
1469b17ce1afSJed Brown +  fine - fine level DM
1470b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1471b17ce1afSJed Brown -  ctx - optional user-defined function context
1472b17ce1afSJed Brown 
1473b17ce1afSJed Brown    Calling sequence for restricthook:
1474c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1475b17ce1afSJed Brown 
1476b17ce1afSJed Brown +  fine - fine level DM
1477b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1478c833c3b5SJed Brown .  rscale - scaling vector for restriction
1479c833c3b5SJed Brown .  inject - matrix restricting by injection
1480b17ce1afSJed Brown .  coarse - coarse level DM to update
1481b17ce1afSJed Brown -  ctx - optional user-defined function context
1482b17ce1afSJed Brown 
1483b17ce1afSJed Brown    Level: advanced
1484b17ce1afSJed Brown 
1485b17ce1afSJed Brown    Notes:
1486b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1487b17ce1afSJed Brown 
1488b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1489b17ce1afSJed Brown 
1490b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1491b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1492b17ce1afSJed Brown 
1493c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1494b17ce1afSJed Brown @*/
1495b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1496b17ce1afSJed Brown {
1497b17ce1afSJed Brown   PetscErrorCode ierr;
1498b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1499b17ce1afSJed Brown 
1500b17ce1afSJed Brown   PetscFunctionBegin;
1501b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
15026bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1503b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1504b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1505b17ce1afSJed Brown   link->restricthook = restricthook;
1506b17ce1afSJed Brown   link->ctx = ctx;
15076cab3a1bSJed Brown   link->next = PETSC_NULL;
1508b17ce1afSJed Brown   *p = link;
1509b17ce1afSJed Brown   PetscFunctionReturn(0);
1510b17ce1afSJed Brown }
1511b17ce1afSJed Brown 
1512b17ce1afSJed Brown #undef __FUNCT__
1513b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1514b17ce1afSJed Brown /*@
1515b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1516b17ce1afSJed Brown 
1517b17ce1afSJed Brown    Collective if any hooks are
1518b17ce1afSJed Brown 
1519b17ce1afSJed Brown    Input Arguments:
1520b17ce1afSJed Brown +  fine - finer DM to use as a base
1521b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1522b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1523b17ce1afSJed Brown -  coarse - coarer DM to update
1524b17ce1afSJed Brown 
1525b17ce1afSJed Brown    Level: developer
1526b17ce1afSJed Brown 
1527b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1528b17ce1afSJed Brown @*/
1529b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1530b17ce1afSJed Brown {
1531b17ce1afSJed Brown   PetscErrorCode ierr;
1532b17ce1afSJed Brown   DMCoarsenHookLink link;
1533b17ce1afSJed Brown 
1534b17ce1afSJed Brown   PetscFunctionBegin;
1535b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1536b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1537b17ce1afSJed Brown   }
153847c6ae99SBarry Smith   PetscFunctionReturn(0);
153947c6ae99SBarry Smith }
154047c6ae99SBarry Smith 
154147c6ae99SBarry Smith #undef __FUNCT__
15425fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
15435fe1f584SPeter Brune /*@
15446a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
15455fe1f584SPeter Brune 
15465fe1f584SPeter Brune     Not Collective
15475fe1f584SPeter Brune 
15485fe1f584SPeter Brune     Input Parameter:
15495fe1f584SPeter Brune .   dm - the DM object
15505fe1f584SPeter Brune 
15515fe1f584SPeter Brune     Output Parameter:
15526a7d9d85SPeter Brune .   level - number of coarsenings
15535fe1f584SPeter Brune 
15545fe1f584SPeter Brune     Level: developer
15555fe1f584SPeter Brune 
15566a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
15575fe1f584SPeter Brune 
15585fe1f584SPeter Brune @*/
15595fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
15605fe1f584SPeter Brune {
15615fe1f584SPeter Brune   PetscFunctionBegin;
15625fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15635fe1f584SPeter Brune   *level = dm->leveldown;
15645fe1f584SPeter Brune   PetscFunctionReturn(0);
15655fe1f584SPeter Brune }
15665fe1f584SPeter Brune 
15675fe1f584SPeter Brune 
15685fe1f584SPeter Brune 
15695fe1f584SPeter Brune #undef __FUNCT__
157047c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
157147c6ae99SBarry Smith /*@C
157247c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
157347c6ae99SBarry Smith 
157447c6ae99SBarry Smith     Collective on DM
157547c6ae99SBarry Smith 
157647c6ae99SBarry Smith     Input Parameter:
157747c6ae99SBarry Smith +   dm - the DM object
157847c6ae99SBarry Smith -   nlevels - the number of levels of refinement
157947c6ae99SBarry Smith 
158047c6ae99SBarry Smith     Output Parameter:
158147c6ae99SBarry Smith .   dmf - the refined DM hierarchy
158247c6ae99SBarry Smith 
158347c6ae99SBarry Smith     Level: developer
158447c6ae99SBarry Smith 
1585e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
158647c6ae99SBarry Smith 
158747c6ae99SBarry Smith @*/
15887087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
158947c6ae99SBarry Smith {
159047c6ae99SBarry Smith   PetscErrorCode ierr;
159147c6ae99SBarry Smith 
159247c6ae99SBarry Smith   PetscFunctionBegin;
1593171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
159447c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
159547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
159647c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
159747c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
159847c6ae99SBarry Smith   } else if (dm->ops->refine) {
159947c6ae99SBarry Smith     PetscInt i;
160047c6ae99SBarry Smith 
160147c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
160247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
160347c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
160447c6ae99SBarry Smith     }
160547c6ae99SBarry Smith   } else {
160647c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
160747c6ae99SBarry Smith   }
160847c6ae99SBarry Smith   PetscFunctionReturn(0);
160947c6ae99SBarry Smith }
161047c6ae99SBarry Smith 
161147c6ae99SBarry Smith #undef __FUNCT__
161247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
161347c6ae99SBarry Smith /*@C
161447c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
161547c6ae99SBarry Smith 
161647c6ae99SBarry Smith     Collective on DM
161747c6ae99SBarry Smith 
161847c6ae99SBarry Smith     Input Parameter:
161947c6ae99SBarry Smith +   dm - the DM object
162047c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
162147c6ae99SBarry Smith 
162247c6ae99SBarry Smith     Output Parameter:
162347c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
162447c6ae99SBarry Smith 
162547c6ae99SBarry Smith     Level: developer
162647c6ae99SBarry Smith 
1627e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
162847c6ae99SBarry Smith 
162947c6ae99SBarry Smith @*/
16307087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
163147c6ae99SBarry Smith {
163247c6ae99SBarry Smith   PetscErrorCode ierr;
163347c6ae99SBarry Smith 
163447c6ae99SBarry Smith   PetscFunctionBegin;
1635171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
163647c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
163747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
163847c6ae99SBarry Smith   PetscValidPointer(dmc,3);
163947c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
164047c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
164147c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
164247c6ae99SBarry Smith     PetscInt i;
164347c6ae99SBarry Smith 
164447c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
164547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
164647c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
164747c6ae99SBarry Smith     }
164847c6ae99SBarry Smith   } else {
164947c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
165047c6ae99SBarry Smith   }
165147c6ae99SBarry Smith   PetscFunctionReturn(0);
165247c6ae99SBarry Smith }
165347c6ae99SBarry Smith 
165447c6ae99SBarry Smith #undef __FUNCT__
1655e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
165647c6ae99SBarry Smith /*@
1657e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
165847c6ae99SBarry Smith    grids associated with two DMs.
165947c6ae99SBarry Smith 
166047c6ae99SBarry Smith    Collective on DM
166147c6ae99SBarry Smith 
166247c6ae99SBarry Smith    Input Parameters:
166347c6ae99SBarry Smith +  dmc - the coarse grid DM
166447c6ae99SBarry Smith -  dmf - the fine grid DM
166547c6ae99SBarry Smith 
166647c6ae99SBarry Smith    Output Parameters:
166747c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
166847c6ae99SBarry Smith 
166947c6ae99SBarry Smith    Level: intermediate
167047c6ae99SBarry Smith 
167147c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
167247c6ae99SBarry Smith 
1673e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
167447c6ae99SBarry Smith @*/
1675e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
167647c6ae99SBarry Smith {
167747c6ae99SBarry Smith   PetscErrorCode ierr;
167847c6ae99SBarry Smith 
167947c6ae99SBarry Smith   PetscFunctionBegin;
1680171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1681171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
168247c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
168347c6ae99SBarry Smith   PetscFunctionReturn(0);
168447c6ae99SBarry Smith }
168547c6ae99SBarry Smith 
168647c6ae99SBarry Smith #undef __FUNCT__
16871a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
16881a266240SBarry Smith /*@C
16891a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
16901a266240SBarry Smith 
16911a266240SBarry Smith     Not Collective
16921a266240SBarry Smith 
16931a266240SBarry Smith     Input Parameters:
16941a266240SBarry Smith +   dm - the DM object
16951a266240SBarry Smith -   destroy - the destroy function
16961a266240SBarry Smith 
16971a266240SBarry Smith     Level: intermediate
16981a266240SBarry Smith 
1699e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
17001a266240SBarry Smith 
1701f07f9ceaSJed Brown @*/
17021a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
17031a266240SBarry Smith {
17041a266240SBarry Smith   PetscFunctionBegin;
1705171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17061a266240SBarry Smith   dm->ctxdestroy = destroy;
17071a266240SBarry Smith   PetscFunctionReturn(0);
17081a266240SBarry Smith }
17091a266240SBarry Smith 
17101a266240SBarry Smith #undef __FUNCT__
17111b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1712b07ff414SBarry Smith /*@
17131b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
171447c6ae99SBarry Smith 
171547c6ae99SBarry Smith     Not Collective
171647c6ae99SBarry Smith 
171747c6ae99SBarry Smith     Input Parameters:
171847c6ae99SBarry Smith +   dm - the DM object
171947c6ae99SBarry Smith -   ctx - the user context
172047c6ae99SBarry Smith 
172147c6ae99SBarry Smith     Level: intermediate
172247c6ae99SBarry Smith 
1723e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
172447c6ae99SBarry Smith 
172547c6ae99SBarry Smith @*/
17261b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
172747c6ae99SBarry Smith {
172847c6ae99SBarry Smith   PetscFunctionBegin;
1729171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
173047c6ae99SBarry Smith   dm->ctx = ctx;
173147c6ae99SBarry Smith   PetscFunctionReturn(0);
173247c6ae99SBarry Smith }
173347c6ae99SBarry Smith 
173447c6ae99SBarry Smith #undef __FUNCT__
17351b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
173647c6ae99SBarry Smith /*@
17371b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
173847c6ae99SBarry Smith 
173947c6ae99SBarry Smith     Not Collective
174047c6ae99SBarry Smith 
174147c6ae99SBarry Smith     Input Parameter:
174247c6ae99SBarry Smith .   dm - the DM object
174347c6ae99SBarry Smith 
174447c6ae99SBarry Smith     Output Parameter:
174547c6ae99SBarry Smith .   ctx - the user context
174647c6ae99SBarry Smith 
174747c6ae99SBarry Smith     Level: intermediate
174847c6ae99SBarry Smith 
1749e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
175047c6ae99SBarry Smith 
175147c6ae99SBarry Smith @*/
17521b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
175347c6ae99SBarry Smith {
175447c6ae99SBarry Smith   PetscFunctionBegin;
1755171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17561b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
175747c6ae99SBarry Smith   PetscFunctionReturn(0);
175847c6ae99SBarry Smith }
175947c6ae99SBarry Smith 
176047c6ae99SBarry Smith #undef __FUNCT__
176147c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
17627e833e3aSBarry Smith /*@C
176347c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
176447c6ae99SBarry Smith 
176547c6ae99SBarry Smith     Logically Collective on DM
176647c6ae99SBarry Smith 
176747c6ae99SBarry Smith     Input Parameter:
176847c6ae99SBarry Smith +   dm - the DM object to destroy
176947c6ae99SBarry Smith -   f - the function to compute the initial guess
177047c6ae99SBarry Smith 
177147c6ae99SBarry Smith     Level: intermediate
177247c6ae99SBarry Smith 
1773e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
177447c6ae99SBarry Smith 
1775f07f9ceaSJed Brown @*/
17767087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
177747c6ae99SBarry Smith {
177847c6ae99SBarry Smith   PetscFunctionBegin;
1779171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
178047c6ae99SBarry Smith   dm->ops->initialguess = f;
178147c6ae99SBarry Smith   PetscFunctionReturn(0);
178247c6ae99SBarry Smith }
178347c6ae99SBarry Smith 
178447c6ae99SBarry Smith #undef __FUNCT__
178547c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
17867e833e3aSBarry Smith /*@C
178747c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
178847c6ae99SBarry Smith 
178947c6ae99SBarry Smith     Logically Collective on DM
179047c6ae99SBarry Smith 
179147c6ae99SBarry Smith     Input Parameter:
179247c6ae99SBarry Smith +   dm - the DM object
179347c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
179447c6ae99SBarry Smith 
179547c6ae99SBarry Smith     Level: intermediate
179647c6ae99SBarry Smith 
179747c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
179847c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
179947c6ae99SBarry Smith 
1800e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
180147c6ae99SBarry Smith          DMSetJacobian()
180247c6ae99SBarry Smith 
1803f07f9ceaSJed Brown @*/
18047087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
180547c6ae99SBarry Smith {
180647c6ae99SBarry Smith   PetscFunctionBegin;
1807171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
180847c6ae99SBarry Smith   dm->ops->function = f;
180947c6ae99SBarry Smith   if (f) {
181047c6ae99SBarry Smith     dm->ops->functionj = f;
181147c6ae99SBarry Smith   }
181247c6ae99SBarry Smith   PetscFunctionReturn(0);
181347c6ae99SBarry Smith }
181447c6ae99SBarry Smith 
181547c6ae99SBarry Smith #undef __FUNCT__
181647c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
18177e833e3aSBarry Smith /*@C
181847c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
181947c6ae99SBarry Smith 
182047c6ae99SBarry Smith     Logically Collective on DM
182147c6ae99SBarry Smith 
182247c6ae99SBarry Smith     Input Parameter:
182347c6ae99SBarry Smith +   dm - the DM object to destroy
182447c6ae99SBarry Smith -   f - the function to compute the matrix entries
182547c6ae99SBarry Smith 
182647c6ae99SBarry Smith     Level: intermediate
182747c6ae99SBarry Smith 
1828e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
182947c6ae99SBarry Smith          DMSetFunction()
183047c6ae99SBarry Smith 
1831f07f9ceaSJed Brown @*/
18327087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
183347c6ae99SBarry Smith {
183447c6ae99SBarry Smith   PetscFunctionBegin;
1835171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
183647c6ae99SBarry Smith   dm->ops->jacobian = f;
183747c6ae99SBarry Smith   PetscFunctionReturn(0);
183847c6ae99SBarry Smith }
183947c6ae99SBarry Smith 
184047c6ae99SBarry Smith #undef __FUNCT__
184108da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
184208da532bSDmitry Karpeev /*@C
184308da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
184408da532bSDmitry Karpeev 
184508da532bSDmitry Karpeev     Logically Collective on DM
184608da532bSDmitry Karpeev 
184708da532bSDmitry Karpeev     Input Parameter:
184808da532bSDmitry Karpeev +   dm - the DM object
184908da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
185008da532bSDmitry Karpeev 
185108da532bSDmitry Karpeev     Level: intermediate
185208da532bSDmitry Karpeev 
1853e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
185408da532bSDmitry Karpeev          DMSetJacobian()
185508da532bSDmitry Karpeev 
185608da532bSDmitry Karpeev @*/
185708da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
185808da532bSDmitry Karpeev {
185908da532bSDmitry Karpeev   PetscFunctionBegin;
186008da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
186108da532bSDmitry Karpeev   PetscFunctionReturn(0);
186208da532bSDmitry Karpeev }
186308da532bSDmitry Karpeev 
186408da532bSDmitry Karpeev #undef __FUNCT__
186508da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
186608da532bSDmitry Karpeev /*@
186708da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
186808da532bSDmitry Karpeev 
186908da532bSDmitry Karpeev     Not Collective
187008da532bSDmitry Karpeev 
187108da532bSDmitry Karpeev     Input Parameter:
187208da532bSDmitry Karpeev .   dm - the DM object to destroy
187308da532bSDmitry Karpeev 
187408da532bSDmitry Karpeev     Output Parameter:
187508da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
187608da532bSDmitry Karpeev 
187708da532bSDmitry Karpeev     Level: developer
187808da532bSDmitry Karpeev 
1879e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
188008da532bSDmitry Karpeev 
188108da532bSDmitry Karpeev @*/
188208da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
188308da532bSDmitry Karpeev {
188408da532bSDmitry Karpeev   PetscFunctionBegin;
188508da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
188608da532bSDmitry Karpeev   PetscFunctionReturn(0);
188708da532bSDmitry Karpeev }
188808da532bSDmitry Karpeev 
188908da532bSDmitry Karpeev #undef __FUNCT__
189008da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
189108da532bSDmitry Karpeev /*@C
189208da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
189308da532bSDmitry Karpeev 
189408da532bSDmitry Karpeev     Logically Collective on DM
189508da532bSDmitry Karpeev 
189608da532bSDmitry Karpeev     Input Parameters:
189708da532bSDmitry Karpeev +   dm - the DM object to destroy
189808da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
189908da532bSDmitry Karpeev 
190008da532bSDmitry Karpeev     Output parameters:
190108da532bSDmitry Karpeev +   xl - lower bound
190208da532bSDmitry Karpeev -   xu - upper bound
190308da532bSDmitry Karpeev 
190408da532bSDmitry Karpeev     Level: intermediate
190508da532bSDmitry Karpeev 
1906e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
190708da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
190808da532bSDmitry Karpeev 
190908da532bSDmitry Karpeev @*/
191008da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
191108da532bSDmitry Karpeev {
191208da532bSDmitry Karpeev   PetscErrorCode ierr;
191308da532bSDmitry Karpeev   PetscFunctionBegin;
191408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
191508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
191608da532bSDmitry Karpeev   if(dm->ops->computevariablebounds) {
191708da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
191808da532bSDmitry Karpeev   }
191908da532bSDmitry Karpeev   else {
192008da532bSDmitry Karpeev     ierr = VecSet(xl,SNES_VI_NINF); CHKERRQ(ierr);
192108da532bSDmitry Karpeev     ierr = VecSet(xu,SNES_VI_INF);  CHKERRQ(ierr);
192208da532bSDmitry Karpeev   }
192308da532bSDmitry Karpeev   PetscFunctionReturn(0);
192408da532bSDmitry Karpeev }
192508da532bSDmitry Karpeev 
192608da532bSDmitry Karpeev #undef __FUNCT__
192747c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
192847c6ae99SBarry Smith /*@
192947c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
193047c6ae99SBarry Smith 
193147c6ae99SBarry Smith     Collective on DM
193247c6ae99SBarry Smith 
193347c6ae99SBarry Smith     Input Parameter:
193447c6ae99SBarry Smith +   dm - the DM object to destroy
193547c6ae99SBarry Smith -   x - the vector to hold the initial guess values
193647c6ae99SBarry Smith 
193747c6ae99SBarry Smith     Level: developer
193847c6ae99SBarry Smith 
1939e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
194047c6ae99SBarry Smith 
194147c6ae99SBarry Smith @*/
19427087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
194347c6ae99SBarry Smith {
194447c6ae99SBarry Smith   PetscErrorCode ierr;
194547c6ae99SBarry Smith   PetscFunctionBegin;
1946171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
194747c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
194847c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
194947c6ae99SBarry Smith   PetscFunctionReturn(0);
195047c6ae99SBarry Smith }
195147c6ae99SBarry Smith 
195247c6ae99SBarry Smith #undef __FUNCT__
195347c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
195447c6ae99SBarry Smith /*@
195547c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
195647c6ae99SBarry Smith 
195747c6ae99SBarry Smith     Not Collective
195847c6ae99SBarry Smith 
195947c6ae99SBarry Smith     Input Parameter:
196047c6ae99SBarry Smith .   dm - the DM object to destroy
196147c6ae99SBarry Smith 
196247c6ae99SBarry Smith     Output Parameter:
196347c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
196447c6ae99SBarry Smith 
196547c6ae99SBarry Smith     Level: developer
196647c6ae99SBarry Smith 
1967e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
196847c6ae99SBarry Smith 
196947c6ae99SBarry Smith @*/
19707087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
197147c6ae99SBarry Smith {
197247c6ae99SBarry Smith   PetscFunctionBegin;
1973171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
197447c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
197547c6ae99SBarry Smith   PetscFunctionReturn(0);
197647c6ae99SBarry Smith }
197747c6ae99SBarry Smith 
197847c6ae99SBarry Smith #undef __FUNCT__
197947c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
198047c6ae99SBarry Smith /*@
198147c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
198247c6ae99SBarry Smith 
198347c6ae99SBarry Smith     Not Collective
198447c6ae99SBarry Smith 
198547c6ae99SBarry Smith     Input Parameter:
198647c6ae99SBarry Smith .   dm - the DM object to destroy
198747c6ae99SBarry Smith 
198847c6ae99SBarry Smith     Output Parameter:
198947c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
199047c6ae99SBarry Smith 
199147c6ae99SBarry Smith     Level: developer
199247c6ae99SBarry Smith 
1993e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
199447c6ae99SBarry Smith 
199547c6ae99SBarry Smith @*/
19967087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
199747c6ae99SBarry Smith {
199847c6ae99SBarry Smith   PetscFunctionBegin;
1999171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
200047c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
200147c6ae99SBarry Smith   PetscFunctionReturn(0);
200247c6ae99SBarry Smith }
200347c6ae99SBarry Smith 
200447c6ae99SBarry Smith #undef __FUNCT__
200547c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
200647c6ae99SBarry Smith /*@
200747c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
200847c6ae99SBarry Smith 
200947c6ae99SBarry Smith     Not Collective
201047c6ae99SBarry Smith 
201147c6ae99SBarry Smith     Input Parameter:
201247c6ae99SBarry Smith .   dm - the DM object to destroy
201347c6ae99SBarry Smith 
201447c6ae99SBarry Smith     Output Parameter:
201547c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
201647c6ae99SBarry Smith 
201747c6ae99SBarry Smith     Level: developer
201847c6ae99SBarry Smith 
2019e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
202047c6ae99SBarry Smith 
202147c6ae99SBarry Smith @*/
20227087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
202347c6ae99SBarry Smith {
202447c6ae99SBarry Smith   PetscFunctionBegin;
2025171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
202647c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
202747c6ae99SBarry Smith   PetscFunctionReturn(0);
202847c6ae99SBarry Smith }
202947c6ae99SBarry Smith 
203047c6ae99SBarry Smith #undef  __FUNCT__
203108da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2032748fac09SDmitry Karpeev /*@C
203308da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
203408da532bSDmitry Karpeev 
203508da532bSDmitry Karpeev     Collective on DM
203608da532bSDmitry Karpeev 
203708da532bSDmitry Karpeev     Input Parameter:
203808da532bSDmitry Karpeev +   dm - the DM object
2039e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
204008da532bSDmitry Karpeev 
204108da532bSDmitry Karpeev     Level: developer
204208da532bSDmitry Karpeev 
2043e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
204408da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
204508da532bSDmitry Karpeev 
204608da532bSDmitry Karpeev @*/
204708da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
204808da532bSDmitry Karpeev {
204908da532bSDmitry Karpeev   PetscErrorCode ierr;
205008da532bSDmitry Karpeev   PetscFunctionBegin;
205108da532bSDmitry Karpeev   if (x) {
205208da532bSDmitry Karpeev     if (!dm->x) {
205308da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
205408da532bSDmitry Karpeev     }
205508da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
205608da532bSDmitry Karpeev   }
205708da532bSDmitry Karpeev   else if(dm->x) {
205808da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
205908da532bSDmitry Karpeev   }
206008da532bSDmitry Karpeev   PetscFunctionReturn(0);
206108da532bSDmitry Karpeev }
206208da532bSDmitry Karpeev 
206308da532bSDmitry Karpeev 
206408da532bSDmitry Karpeev #undef __FUNCT__
206547c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
206647c6ae99SBarry Smith /*@
206747c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
206847c6ae99SBarry Smith 
206947c6ae99SBarry Smith     Collective on DM
207047c6ae99SBarry Smith 
207147c6ae99SBarry Smith     Input Parameter:
207247c6ae99SBarry Smith +   dm - the DM object to destroy
207347c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
207447c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
207547c6ae99SBarry Smith 
207647c6ae99SBarry Smith     Level: developer
207747c6ae99SBarry Smith 
2078e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
207947c6ae99SBarry Smith          DMSetJacobian()
208047c6ae99SBarry Smith 
208147c6ae99SBarry Smith @*/
20827087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
208347c6ae99SBarry Smith {
208447c6ae99SBarry Smith   PetscErrorCode ierr;
208547c6ae99SBarry Smith   PetscFunctionBegin;
2086171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
208747c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
2088644e2e5bSBarry Smith   PetscStackPush("DM user function");
208947c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
2090644e2e5bSBarry Smith   PetscStackPop;
209147c6ae99SBarry Smith   PetscFunctionReturn(0);
209247c6ae99SBarry Smith }
209347c6ae99SBarry Smith 
209447c6ae99SBarry Smith 
209508da532bSDmitry Karpeev 
209647c6ae99SBarry Smith #undef __FUNCT__
209747c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
209847c6ae99SBarry Smith /*@
209947c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
210047c6ae99SBarry Smith 
210147c6ae99SBarry Smith     Collective on DM
210247c6ae99SBarry Smith 
210347c6ae99SBarry Smith     Input Parameter:
210447c6ae99SBarry Smith +   dm - the DM object
2105cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
210647c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
210747c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
210847c6ae99SBarry Smith 
210947c6ae99SBarry Smith     Level: developer
211047c6ae99SBarry Smith 
2111e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
211247c6ae99SBarry Smith          DMSetFunction()
211347c6ae99SBarry Smith 
211447c6ae99SBarry Smith @*/
21157087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
211647c6ae99SBarry Smith {
211747c6ae99SBarry Smith   PetscErrorCode ierr;
211847c6ae99SBarry Smith 
211947c6ae99SBarry Smith   PetscFunctionBegin;
2120171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
212147c6ae99SBarry Smith   if (!dm->ops->jacobian) {
212247c6ae99SBarry Smith     ISColoring     coloring;
212347c6ae99SBarry Smith     MatFDColoring  fd;
21242c9966d7SBarry Smith     const MatType  mtype;
212547c6ae99SBarry Smith 
21262c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
21272c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
212847c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
2129fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
213047c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
21310bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
21320bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
213371cd77b2SBarry Smith 
213447c6ae99SBarry Smith     dm->fd = fd;
213547c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
21362533e041SBarry Smith 
213771cd77b2SBarry Smith     /* don't know why this is needed */
213871cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
213947c6ae99SBarry Smith   }
214047c6ae99SBarry Smith   if (!x) x = dm->x;
214147c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
2142cab2e9ccSBarry Smith 
214371cd77b2SBarry 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 */
2144649052a6SBarry Smith   if (x) {
2145cab2e9ccSBarry Smith     if (!dm->x) {
214671cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
2147cab2e9ccSBarry Smith     }
2148cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
2149649052a6SBarry Smith   }
2150a8248277SBarry Smith   if (A != B) {
2151a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2152a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2153a8248277SBarry Smith   }
215447c6ae99SBarry Smith   PetscFunctionReturn(0);
215547c6ae99SBarry Smith }
2156264ace61SBarry Smith 
2157cab2e9ccSBarry Smith 
2158264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2159264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2160264ace61SBarry Smith 
2161264ace61SBarry Smith #undef __FUNCT__
2162264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2163264ace61SBarry Smith /*@C
2164264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2165264ace61SBarry Smith 
2166264ace61SBarry Smith   Collective on DM
2167264ace61SBarry Smith 
2168264ace61SBarry Smith   Input Parameters:
2169264ace61SBarry Smith + dm     - The DM object
2170264ace61SBarry Smith - method - The name of the DM type
2171264ace61SBarry Smith 
2172264ace61SBarry Smith   Options Database Key:
2173264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2174264ace61SBarry Smith 
2175264ace61SBarry Smith   Notes:
2176e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2177264ace61SBarry Smith 
2178264ace61SBarry Smith   Level: intermediate
2179264ace61SBarry Smith 
2180264ace61SBarry Smith .keywords: DM, set, type
2181264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2182264ace61SBarry Smith @*/
21837087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
2184264ace61SBarry Smith {
2185264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2186264ace61SBarry Smith   PetscBool      match;
2187264ace61SBarry Smith   PetscErrorCode ierr;
2188264ace61SBarry Smith 
2189264ace61SBarry Smith   PetscFunctionBegin;
2190264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2191251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2192264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2193264ace61SBarry Smith 
2194264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
21954b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2196264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2197264ace61SBarry Smith 
2198264ace61SBarry Smith   if (dm->ops->destroy) {
2199264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2200b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2201264ace61SBarry Smith   }
2202264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2203264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2204264ace61SBarry Smith   PetscFunctionReturn(0);
2205264ace61SBarry Smith }
2206264ace61SBarry Smith 
2207264ace61SBarry Smith #undef __FUNCT__
2208264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2209264ace61SBarry Smith /*@C
2210264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2211264ace61SBarry Smith 
2212264ace61SBarry Smith   Not Collective
2213264ace61SBarry Smith 
2214264ace61SBarry Smith   Input Parameter:
2215264ace61SBarry Smith . dm  - The DM
2216264ace61SBarry Smith 
2217264ace61SBarry Smith   Output Parameter:
2218264ace61SBarry Smith . type - The DM type name
2219264ace61SBarry Smith 
2220264ace61SBarry Smith   Level: intermediate
2221264ace61SBarry Smith 
2222264ace61SBarry Smith .keywords: DM, get, type, name
2223264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2224264ace61SBarry Smith @*/
22257087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
2226264ace61SBarry Smith {
2227264ace61SBarry Smith   PetscErrorCode ierr;
2228264ace61SBarry Smith 
2229264ace61SBarry Smith   PetscFunctionBegin;
2230264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2231264ace61SBarry Smith   PetscValidCharPointer(type,2);
2232264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2233264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2234264ace61SBarry Smith   }
2235264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2236264ace61SBarry Smith   PetscFunctionReturn(0);
2237264ace61SBarry Smith }
2238264ace61SBarry Smith 
223967a56275SMatthew G Knepley #undef __FUNCT__
224067a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
224167a56275SMatthew G Knepley /*@C
224267a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
224367a56275SMatthew G Knepley 
224467a56275SMatthew G Knepley   Collective on DM
224567a56275SMatthew G Knepley 
224667a56275SMatthew G Knepley   Input Parameters:
224767a56275SMatthew G Knepley + dm - the DM
224867a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
224967a56275SMatthew G Knepley 
225067a56275SMatthew G Knepley   Output Parameter:
225167a56275SMatthew G Knepley . M - pointer to new DM
225267a56275SMatthew G Knepley 
225367a56275SMatthew G Knepley   Notes:
225467a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
225567a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
225667a56275SMatthew G Knepley   of the input DM.
225767a56275SMatthew G Knepley 
225867a56275SMatthew G Knepley   Level: intermediate
225967a56275SMatthew G Knepley 
226067a56275SMatthew G Knepley .seealso: DMCreate()
226167a56275SMatthew G Knepley @*/
226267a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
226367a56275SMatthew G Knepley {
226467a56275SMatthew G Knepley   DM             B;
226567a56275SMatthew G Knepley   char           convname[256];
226667a56275SMatthew G Knepley   PetscBool      sametype, issame;
226767a56275SMatthew G Knepley   PetscErrorCode ierr;
226867a56275SMatthew G Knepley 
226967a56275SMatthew G Knepley   PetscFunctionBegin;
227067a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
227167a56275SMatthew G Knepley   PetscValidType(dm,1);
227267a56275SMatthew G Knepley   PetscValidPointer(M,3);
2273251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
227467a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
227567a56275SMatthew G Knepley   {
227667a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
227767a56275SMatthew G Knepley 
227867a56275SMatthew G Knepley     /*
227967a56275SMatthew G Knepley        Order of precedence:
228067a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
228167a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
228267a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
228367a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
228467a56275SMatthew G Knepley        5) Use a really basic converter.
228567a56275SMatthew G Knepley     */
228667a56275SMatthew G Knepley 
228767a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
228867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
228967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
229067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
229167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
229267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
229367a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
229467a56275SMatthew G Knepley     if (conv) goto foundconv;
229567a56275SMatthew G Knepley 
229667a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
229767a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
229867a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
229967a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
230067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
230167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
230267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
230367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
230467a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
230567a56275SMatthew G Knepley     if (conv) {
2306fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
230767a56275SMatthew G Knepley       goto foundconv;
230867a56275SMatthew G Knepley     }
230967a56275SMatthew G Knepley 
231067a56275SMatthew G Knepley #if 0
231167a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
231267a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2313fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
231467a56275SMatthew G Knepley     if (conv) goto foundconv;
231567a56275SMatthew G Knepley 
231667a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
231767a56275SMatthew G Knepley     if (dm->ops->convert) {
231867a56275SMatthew G Knepley       conv = dm->ops->convert;
231967a56275SMatthew G Knepley     }
232067a56275SMatthew G Knepley     if (conv) goto foundconv;
232167a56275SMatthew G Knepley #endif
232267a56275SMatthew G Knepley 
232367a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
232467a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
232567a56275SMatthew G Knepley 
232667a56275SMatthew G Knepley     foundconv:
232767a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
232867a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
232967a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
233067a56275SMatthew G Knepley   }
233167a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
233267a56275SMatthew G Knepley   PetscFunctionReturn(0);
233367a56275SMatthew G Knepley }
2334264ace61SBarry Smith 
2335264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2336264ace61SBarry Smith 
2337264ace61SBarry Smith #undef __FUNCT__
2338264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2339264ace61SBarry Smith /*@C
2340264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2341264ace61SBarry Smith 
2342264ace61SBarry Smith   Level: advanced
2343264ace61SBarry Smith @*/
23447087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2345264ace61SBarry Smith {
2346264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2347264ace61SBarry Smith   PetscErrorCode ierr;
2348264ace61SBarry Smith 
2349264ace61SBarry Smith   PetscFunctionBegin;
2350264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2351264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2352264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2353264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2354264ace61SBarry Smith   PetscFunctionReturn(0);
2355264ace61SBarry Smith }
2356264ace61SBarry Smith 
2357264ace61SBarry Smith 
2358264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2359264ace61SBarry Smith #undef __FUNCT__
2360264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2361264ace61SBarry Smith /*@C
2362264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2363264ace61SBarry Smith 
2364264ace61SBarry Smith    Not Collective
2365264ace61SBarry Smith 
2366264ace61SBarry Smith    Level: advanced
2367264ace61SBarry Smith 
2368264ace61SBarry Smith .keywords: DM, register, destroy
2369264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2370264ace61SBarry Smith @*/
23717087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2372264ace61SBarry Smith {
2373264ace61SBarry Smith   PetscErrorCode ierr;
2374264ace61SBarry Smith 
2375264ace61SBarry Smith   PetscFunctionBegin;
2376264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2377264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2378264ace61SBarry Smith   PetscFunctionReturn(0);
2379264ace61SBarry Smith }
238023f975d1SBarry Smith 
238123f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2382c6db04a5SJed Brown #include <mex.h>
238323f975d1SBarry Smith 
23843014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
238523f975d1SBarry Smith 
238623f975d1SBarry Smith #undef __FUNCT__
238723f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
238823f975d1SBarry Smith /*
238923f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
239023f975d1SBarry Smith                          DMSetFunctionMatlab().
239123f975d1SBarry Smith 
239223f975d1SBarry Smith    For linear problems x is null
239323f975d1SBarry Smith 
239423f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
239523f975d1SBarry Smith */
23967087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
239723f975d1SBarry Smith {
239823f975d1SBarry Smith   PetscErrorCode    ierr;
239923f975d1SBarry Smith   DMMatlabContext   *sctx;
240023f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
240123f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
240223f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
240323f975d1SBarry Smith 
240423f975d1SBarry Smith   PetscFunctionBegin;
240523f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
240623f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
240723f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
240823f975d1SBarry Smith 
240923f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
24101b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
241123f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
241223f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
24133014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
241423f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
241523f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
241623f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
241723f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2418b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
241923f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
242023f975d1SBarry Smith   mxDestroyArray(prhs[0]);
242123f975d1SBarry Smith   mxDestroyArray(prhs[1]);
242223f975d1SBarry Smith   mxDestroyArray(prhs[2]);
242323f975d1SBarry Smith   mxDestroyArray(prhs[3]);
242423f975d1SBarry Smith   mxDestroyArray(plhs[0]);
242523f975d1SBarry Smith   PetscFunctionReturn(0);
242623f975d1SBarry Smith }
242723f975d1SBarry Smith 
242823f975d1SBarry Smith 
242923f975d1SBarry Smith #undef __FUNCT__
243023f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
243123f975d1SBarry Smith /*
243223f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
243323f975d1SBarry Smith 
243423f975d1SBarry Smith */
24357087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
243623f975d1SBarry Smith {
243723f975d1SBarry Smith   PetscErrorCode    ierr;
243823f975d1SBarry Smith   DMMatlabContext   *sctx;
243923f975d1SBarry Smith 
244023f975d1SBarry Smith   PetscFunctionBegin;
2441171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
244223f975d1SBarry Smith   /* currently sctx is memory bleed */
24431b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
24443014e516SBarry Smith   if (!sctx) {
244523f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
24463014e516SBarry Smith   }
244723f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
24481b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
244923f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
245023f975d1SBarry Smith   PetscFunctionReturn(0);
245123f975d1SBarry Smith }
24523014e516SBarry Smith 
24533014e516SBarry Smith #undef __FUNCT__
24543014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
24553014e516SBarry Smith /*
24563014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
24573014e516SBarry Smith                          DMSetJacobianMatlab().
24583014e516SBarry Smith 
24593014e516SBarry Smith    For linear problems x is null
24603014e516SBarry Smith 
24613014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
24623014e516SBarry Smith */
24637087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
24643014e516SBarry Smith {
24653014e516SBarry Smith   PetscErrorCode    ierr;
24663014e516SBarry Smith   DMMatlabContext   *sctx;
24673014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
24683014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
24693014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
24703014e516SBarry Smith 
24713014e516SBarry Smith   PetscFunctionBegin;
24723014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24733014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
24743014e516SBarry Smith 
2475e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
24761b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
24773014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
24783014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
24793014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
24803014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
24813014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
24823014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
24833014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
24843014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
24853014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2486b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2487c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2488c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
24893014e516SBarry Smith   mxDestroyArray(prhs[0]);
24903014e516SBarry Smith   mxDestroyArray(prhs[1]);
24913014e516SBarry Smith   mxDestroyArray(prhs[2]);
24923014e516SBarry Smith   mxDestroyArray(prhs[3]);
24933014e516SBarry Smith   mxDestroyArray(prhs[4]);
24943014e516SBarry Smith   mxDestroyArray(plhs[0]);
24953014e516SBarry Smith   mxDestroyArray(plhs[1]);
24963014e516SBarry Smith   PetscFunctionReturn(0);
24973014e516SBarry Smith }
24983014e516SBarry Smith 
24993014e516SBarry Smith 
25003014e516SBarry Smith #undef __FUNCT__
25013014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
25023014e516SBarry Smith /*
25033014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
25043014e516SBarry Smith 
25053014e516SBarry Smith */
25067087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
25073014e516SBarry Smith {
25083014e516SBarry Smith   PetscErrorCode    ierr;
25093014e516SBarry Smith   DMMatlabContext   *sctx;
25103014e516SBarry Smith 
25113014e516SBarry Smith   PetscFunctionBegin;
2512171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25133014e516SBarry Smith   /* currently sctx is memory bleed */
25141b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
25153014e516SBarry Smith   if (!sctx) {
25163014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
25173014e516SBarry Smith   }
25183014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
25191b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
25203014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
25213014e516SBarry Smith   PetscFunctionReturn(0);
25223014e516SBarry Smith }
252323f975d1SBarry Smith #endif
2524b859378eSBarry Smith 
2525b859378eSBarry Smith #undef __FUNCT__
2526b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2527b859378eSBarry Smith /*@C
2528b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2529b859378eSBarry Smith   with DMView().
2530b859378eSBarry Smith 
2531b859378eSBarry Smith   Collective on PetscViewer
2532b859378eSBarry Smith 
2533b859378eSBarry Smith   Input Parameters:
2534b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2535b859378eSBarry Smith            some related function before a call to DMLoad().
2536b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2537b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2538b859378eSBarry Smith 
2539b859378eSBarry Smith    Level: intermediate
2540b859378eSBarry Smith 
2541b859378eSBarry Smith   Notes:
2542b859378eSBarry Smith   Defaults to the DM DA.
2543b859378eSBarry Smith 
2544b859378eSBarry Smith   Notes for advanced users:
2545b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2546b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2547b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2548b859378eSBarry Smith   format is
2549b859378eSBarry Smith .vb
2550b859378eSBarry Smith      has not yet been determined
2551b859378eSBarry Smith .ve
2552b859378eSBarry Smith 
2553b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2554b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2555b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2556b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2557b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2558b859378eSBarry Smith 
2559b859378eSBarry Smith   Concepts: vector^loading from file
2560b859378eSBarry Smith 
2561b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2562b859378eSBarry Smith @*/
2563b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2564b859378eSBarry Smith {
2565b859378eSBarry Smith   PetscErrorCode ierr;
2566b859378eSBarry Smith 
2567b859378eSBarry Smith   PetscFunctionBegin;
2568b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2569b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2570b859378eSBarry Smith 
2571b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2572b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2573b859378eSBarry Smith   }
2574b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2575b859378eSBarry Smith   PetscFunctionReturn(0);
2576b859378eSBarry Smith }
2577b859378eSBarry Smith 
25787da65231SMatthew G Knepley /******************************** FEM Support **********************************/
25797da65231SMatthew G Knepley 
25807da65231SMatthew G Knepley #undef __FUNCT__
25817da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
25827da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
25831d47ebbbSSatish Balay   PetscInt       f;
25841b30c384SMatthew G Knepley   PetscErrorCode ierr;
25851b30c384SMatthew G Knepley 
25867da65231SMatthew G Knepley   PetscFunctionBegin;
258774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
25881d47ebbbSSatish Balay   for(f = 0; f < len; ++f) {
258974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
25907da65231SMatthew G Knepley   }
25917da65231SMatthew G Knepley   PetscFunctionReturn(0);
25927da65231SMatthew G Knepley }
25937da65231SMatthew G Knepley 
25947da65231SMatthew G Knepley #undef __FUNCT__
25957da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
25967da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
25971b30c384SMatthew G Knepley   PetscInt       f, g;
25987da65231SMatthew G Knepley   PetscErrorCode ierr;
25997da65231SMatthew G Knepley 
26007da65231SMatthew G Knepley   PetscFunctionBegin;
260174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26021d47ebbbSSatish Balay   for(f = 0; f < rows; ++f) {
260374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
26041d47ebbbSSatish Balay     for(g = 0; g < cols; ++g) {
260574778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
26067da65231SMatthew G Knepley     }
260774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
26087da65231SMatthew G Knepley   }
26097da65231SMatthew G Knepley   PetscFunctionReturn(0);
26107da65231SMatthew G Knepley }
2611e7c4fc90SDmitry Karpeev 
2612970e74d5SMatthew G Knepley #undef __FUNCT__
2613970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
2614970e74d5SMatthew G Knepley /*@C
2615970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
2616970e74d5SMatthew G Knepley 
2617970e74d5SMatthew G Knepley   Not collective
2618970e74d5SMatthew G Knepley 
2619970e74d5SMatthew G Knepley   Input Parameter:
2620970e74d5SMatthew G Knepley . dm - The DM
2621970e74d5SMatthew G Knepley 
2622970e74d5SMatthew G Knepley   Output Parameter:
2623970e74d5SMatthew G Knepley . lf - The local residual function
2624970e74d5SMatthew G Knepley 
2625970e74d5SMatthew G Knepley    Calling sequence of lf:
2626970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2627970e74d5SMatthew G Knepley 
2628970e74d5SMatthew G Knepley +  snes - the SNES context
2629970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2630970e74d5SMatthew G Knepley .  f - local vector to put residual in
2631970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2632970e74d5SMatthew G Knepley 
2633970e74d5SMatthew G Knepley   Level: intermediate
2634970e74d5SMatthew G Knepley 
2635970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2636970e74d5SMatthew G Knepley @*/
2637970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
2638970e74d5SMatthew G Knepley {
2639970e74d5SMatthew G Knepley   PetscFunctionBegin;
2640970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2641970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
2642970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2643970e74d5SMatthew G Knepley }
2644970e74d5SMatthew G Knepley 
2645970e74d5SMatthew G Knepley #undef __FUNCT__
2646970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
2647970e74d5SMatthew G Knepley /*@C
2648970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
2649970e74d5SMatthew G Knepley 
2650970e74d5SMatthew G Knepley   Not collective
2651970e74d5SMatthew G Knepley 
2652970e74d5SMatthew G Knepley   Input Parameters:
2653970e74d5SMatthew G Knepley + dm - The DM
2654970e74d5SMatthew G Knepley - lf - The local residual function
2655970e74d5SMatthew G Knepley 
2656970e74d5SMatthew G Knepley    Calling sequence of lf:
2657970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2658970e74d5SMatthew G Knepley 
2659970e74d5SMatthew G Knepley +  snes - the SNES context
2660970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2661970e74d5SMatthew G Knepley .  f - local vector to put residual in
2662970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2663970e74d5SMatthew G Knepley 
2664970e74d5SMatthew G Knepley   Level: intermediate
2665970e74d5SMatthew G Knepley 
2666970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2667970e74d5SMatthew G Knepley @*/
2668970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
2669970e74d5SMatthew G Knepley {
2670970e74d5SMatthew G Knepley   PetscFunctionBegin;
2671970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2672970e74d5SMatthew G Knepley   dm->lf = lf;
2673970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2674970e74d5SMatthew G Knepley }
2675970e74d5SMatthew G Knepley 
2676970e74d5SMatthew G Knepley #undef __FUNCT__
2677970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
2678970e74d5SMatthew G Knepley /*@C
2679970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
2680970e74d5SMatthew G Knepley 
2681970e74d5SMatthew G Knepley   Not collective
2682970e74d5SMatthew G Knepley 
2683970e74d5SMatthew G Knepley   Input Parameter:
2684970e74d5SMatthew G Knepley . dm - The DM
2685970e74d5SMatthew G Knepley 
2686970e74d5SMatthew G Knepley   Output Parameter:
2687970e74d5SMatthew G Knepley . lj - The local Jacobian function
2688970e74d5SMatthew G Knepley 
2689970e74d5SMatthew G Knepley    Calling sequence of lj:
2690970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2691970e74d5SMatthew G Knepley 
2692970e74d5SMatthew G Knepley +  snes - the SNES context
2693970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2694970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2695970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2696970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2697970e74d5SMatthew G Knepley 
2698970e74d5SMatthew G Knepley   Level: intermediate
2699970e74d5SMatthew G Knepley 
2700970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2701970e74d5SMatthew G Knepley @*/
2702970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
2703970e74d5SMatthew G Knepley {
2704970e74d5SMatthew G Knepley   PetscFunctionBegin;
2705970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2706970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
2707970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2708970e74d5SMatthew G Knepley }
2709970e74d5SMatthew G Knepley 
2710970e74d5SMatthew G Knepley #undef __FUNCT__
2711970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
2712970e74d5SMatthew G Knepley /*@C
2713970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
2714970e74d5SMatthew G Knepley 
2715970e74d5SMatthew G Knepley   Not collective
2716970e74d5SMatthew G Knepley 
2717970e74d5SMatthew G Knepley   Input Parameters:
2718970e74d5SMatthew G Knepley + dm - The DM
2719970e74d5SMatthew G Knepley - lj - The local Jacobian function
2720970e74d5SMatthew G Knepley 
2721970e74d5SMatthew G Knepley    Calling sequence of lj:
2722970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2723970e74d5SMatthew G Knepley 
2724970e74d5SMatthew G Knepley +  snes - the SNES context
2725970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2726970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2727970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2728970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2729970e74d5SMatthew G Knepley 
2730970e74d5SMatthew G Knepley   Level: intermediate
2731970e74d5SMatthew G Knepley 
2732970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2733970e74d5SMatthew G Knepley @*/
2734970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
2735970e74d5SMatthew G Knepley {
2736970e74d5SMatthew G Knepley   PetscFunctionBegin;
2737970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2738970e74d5SMatthew G Knepley   dm->lj = lj;
2739970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2740970e74d5SMatthew G Knepley }
274188ed4aceSMatthew G Knepley 
274288ed4aceSMatthew G Knepley #undef __FUNCT__
274388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
274488ed4aceSMatthew G Knepley /*@
274588ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
274688ed4aceSMatthew G Knepley 
274788ed4aceSMatthew G Knepley   Input Parameter:
274888ed4aceSMatthew G Knepley . dm - The DM
274988ed4aceSMatthew G Knepley 
275088ed4aceSMatthew G Knepley   Output Parameter:
275188ed4aceSMatthew G Knepley . section - The PetscSection
275288ed4aceSMatthew G Knepley 
275388ed4aceSMatthew G Knepley   Level: intermediate
275488ed4aceSMatthew G Knepley 
275588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
275688ed4aceSMatthew G Knepley 
275788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
275888ed4aceSMatthew G Knepley @*/
275988ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
276088ed4aceSMatthew G Knepley   PetscFunctionBegin;
276188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
276288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
276388ed4aceSMatthew G Knepley   *section = dm->defaultSection;
276488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
276588ed4aceSMatthew G Knepley }
276688ed4aceSMatthew G Knepley 
276788ed4aceSMatthew G Knepley #undef __FUNCT__
276888ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
276988ed4aceSMatthew G Knepley /*@
277088ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
277188ed4aceSMatthew G Knepley 
277288ed4aceSMatthew G Knepley   Input Parameters:
277388ed4aceSMatthew G Knepley + dm - The DM
277488ed4aceSMatthew G Knepley - section - The PetscSection
277588ed4aceSMatthew G Knepley 
277688ed4aceSMatthew G Knepley   Level: intermediate
277788ed4aceSMatthew G Knepley 
277888ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
277988ed4aceSMatthew G Knepley 
278088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
278188ed4aceSMatthew G Knepley @*/
278288ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
278388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
278488ed4aceSMatthew G Knepley 
278588ed4aceSMatthew G Knepley   PetscFunctionBegin;
278688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
278788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
278888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
278988ed4aceSMatthew G Knepley   dm->defaultSection = section;
279088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
279188ed4aceSMatthew G Knepley }
279288ed4aceSMatthew G Knepley 
279388ed4aceSMatthew G Knepley #undef __FUNCT__
279488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
279588ed4aceSMatthew G Knepley /*@
279688ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
279788ed4aceSMatthew G Knepley 
279888ed4aceSMatthew G Knepley   Input Parameter:
279988ed4aceSMatthew G Knepley . dm - The DM
280088ed4aceSMatthew G Knepley 
280188ed4aceSMatthew G Knepley   Output Parameter:
280288ed4aceSMatthew G Knepley . section - The PetscSection
280388ed4aceSMatthew G Knepley 
280488ed4aceSMatthew G Knepley   Level: intermediate
280588ed4aceSMatthew G Knepley 
280688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
280788ed4aceSMatthew G Knepley 
280888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
280988ed4aceSMatthew G Knepley @*/
281088ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
281188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
281288ed4aceSMatthew G Knepley 
281388ed4aceSMatthew G Knepley   PetscFunctionBegin;
281488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
281588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
281688ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
281788ed4aceSMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, &dm->defaultGlobalSection);CHKERRQ(ierr);
281888ed4aceSMatthew G Knepley   }
281988ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
282088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
282188ed4aceSMatthew G Knepley }
282288ed4aceSMatthew G Knepley 
282388ed4aceSMatthew G Knepley #undef __FUNCT__
282488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
282588ed4aceSMatthew G Knepley /*@
282688ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
282788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
282888ed4aceSMatthew G Knepley 
282988ed4aceSMatthew G Knepley   Input Parameter:
283088ed4aceSMatthew G Knepley . dm - The DM
283188ed4aceSMatthew G Knepley 
283288ed4aceSMatthew G Knepley   Output Parameter:
283388ed4aceSMatthew G Knepley . sf - The PetscSF
283488ed4aceSMatthew G Knepley 
283588ed4aceSMatthew G Knepley   Level: intermediate
283688ed4aceSMatthew G Knepley 
283788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
283888ed4aceSMatthew G Knepley 
283988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
284088ed4aceSMatthew G Knepley @*/
284188ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
284288ed4aceSMatthew G Knepley   PetscInt       nroots;
284388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
284488ed4aceSMatthew G Knepley 
284588ed4aceSMatthew G Knepley   PetscFunctionBegin;
284688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
284788ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
284888ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
284988ed4aceSMatthew G Knepley   if (nroots < 0) {
285088ed4aceSMatthew G Knepley     PetscSection section, gSection;
285188ed4aceSMatthew G Knepley 
285288ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
285331ea6d37SMatthew G Knepley     if (section) {
285488ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
285588ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
285631ea6d37SMatthew G Knepley     } else {
285731ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
285831ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
285931ea6d37SMatthew G Knepley     }
286088ed4aceSMatthew G Knepley   }
286188ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
286288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
286388ed4aceSMatthew G Knepley }
286488ed4aceSMatthew G Knepley 
286588ed4aceSMatthew G Knepley #undef __FUNCT__
286688ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
286788ed4aceSMatthew G Knepley /*@
286888ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
286988ed4aceSMatthew G Knepley 
287088ed4aceSMatthew G Knepley   Input Parameters:
287188ed4aceSMatthew G Knepley + dm - The DM
287288ed4aceSMatthew G Knepley - sf - The PetscSF
287388ed4aceSMatthew G Knepley 
287488ed4aceSMatthew G Knepley   Level: intermediate
287588ed4aceSMatthew G Knepley 
287688ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
287788ed4aceSMatthew G Knepley 
287888ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
287988ed4aceSMatthew G Knepley @*/
288088ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
288188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
288288ed4aceSMatthew G Knepley 
288388ed4aceSMatthew G Knepley   PetscFunctionBegin;
288488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
288588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
288688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
288788ed4aceSMatthew G Knepley   dm->defaultSF = sf;
288888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
288988ed4aceSMatthew G Knepley }
289088ed4aceSMatthew G Knepley 
289188ed4aceSMatthew G Knepley #undef __FUNCT__
289288ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
289388ed4aceSMatthew G Knepley /*@C
289488ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
289588ed4aceSMatthew G Knepley   describing the data layout.
289688ed4aceSMatthew G Knepley 
289788ed4aceSMatthew G Knepley   Input Parameters:
289888ed4aceSMatthew G Knepley + dm - The DM
289988ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
290088ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
290188ed4aceSMatthew G Knepley 
290288ed4aceSMatthew G Knepley   Level: intermediate
290388ed4aceSMatthew G Knepley 
290488ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
290588ed4aceSMatthew G Knepley @*/
290688ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
290788ed4aceSMatthew G Knepley {
290888ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
290988ed4aceSMatthew G Knepley   PetscLayout     layout;
291088ed4aceSMatthew G Knepley   const PetscInt *ranges;
291188ed4aceSMatthew G Knepley   PetscInt       *local;
291288ed4aceSMatthew G Knepley   PetscSFNode    *remote;
291388ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
291488ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
291588ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
291688ed4aceSMatthew G Knepley 
291788ed4aceSMatthew G Knepley   PetscFunctionBegin;
291888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
291988ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
292088ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
292188ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
292288ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
292388ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
292488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
292588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
292688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
292788ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
292888ed4aceSMatthew G Knepley   for(p = pStart, nleaves = 0; p < pEnd; ++p) {
292988ed4aceSMatthew G Knepley     PetscInt dof, cdof;
293088ed4aceSMatthew G Knepley 
293188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &dof);CHKERRQ(ierr);
293288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &cdof);CHKERRQ(ierr);
293388ed4aceSMatthew G Knepley     nleaves += dof < 0 ? -(dof+1)-cdof : dof-cdof;
293488ed4aceSMatthew G Knepley   }
293588ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
293688ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
293788ed4aceSMatthew G Knepley   for(p = pStart, l = 0; p < pEnd; ++p) {
293888ed4aceSMatthew G Knepley     PetscInt *cind;
293988ed4aceSMatthew G Knepley     PetscInt  dof, gdof, cdof, dim, off, goff, d, c;
294088ed4aceSMatthew G Knepley 
294188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
294288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
294388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
294488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
294588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
294688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
294788ed4aceSMatthew G Knepley     dim  = dof-cdof;
294888ed4aceSMatthew G Knepley     for(d = 0, c = 0; d < dof; ++d) {
294988ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
295088ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
295188ed4aceSMatthew G Knepley     }
295288ed4aceSMatthew G Knepley     if (gdof < 0) {
295388ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
295488ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
295588ed4aceSMatthew G Knepley 
295688ed4aceSMatthew G Knepley         for(r = 0; r < size; ++r) {
295788ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
295888ed4aceSMatthew G Knepley         }
295988ed4aceSMatthew G Knepley         remote[l].rank  = r;
296088ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
296188ed4aceSMatthew G Knepley       }
296288ed4aceSMatthew G Knepley     } else {
296388ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
296488ed4aceSMatthew G Knepley         remote[l].rank  = rank;
296588ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
296688ed4aceSMatthew G Knepley       }
296788ed4aceSMatthew G Knepley     }
296888ed4aceSMatthew G Knepley   }
296988ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
297088ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
297188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
297288ed4aceSMatthew G Knepley }
2973