xref: /petsc/src/dm/interface/dm.c (revision 88ed4acec49ae3a81b39199da1871f2558231dad)
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;
53*88ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
54*88ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
55*88ed4aceSMatthew G Knepley   v->defaultSection       = PETSC_NULL;
56*88ed4aceSMatthew 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;
170b17ce1afSJed Brown   DMCoarsenHookLink link,next;
171dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
17247c6ae99SBarry Smith   PetscErrorCode ierr;
17347c6ae99SBarry Smith 
17447c6ae99SBarry Smith   PetscFunctionBegin;
1756bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
1766bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
17787e657c6SBarry Smith 
17887e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
179732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1806bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
1816bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
182732e2eb9SMatthew G Knepley   }
183dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
18471cd77b2SBarry Smith   if ((*dm)->x) {
18571cd77b2SBarry Smith     PetscObject obj;
18671cd77b2SBarry Smith     ierr = PetscObjectQuery((PetscObject)(*dm)->x,"DM",&obj);CHKERRQ(ierr);
18771cd77b2SBarry Smith     if (obj == (PetscObject)*dm) cnt++;
18871cd77b2SBarry Smith   }
189732e2eb9SMatthew G Knepley 
1906bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
191732e2eb9SMatthew G Knepley   /*
192732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
193732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
194732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
195732e2eb9SMatthew G Knepley   */
1966bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
1976bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
198732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1996bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
2006bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
201732e2eb9SMatthew G Knepley   }
202dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
203dfe15315SJed Brown     nnext = nlink->next;
204dfe15315SJed 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);
205dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
206dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
207dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
208dfe15315SJed Brown   }
209dfe15315SJed Brown   (*dm)->namedglobal = PETSC_NULL;
2101a266240SBarry Smith 
211b17ce1afSJed Brown   /* Destroy the list of hooks */
212b17ce1afSJed Brown   for (link=(*dm)->coarsenhook; link; link=next) {
213b17ce1afSJed Brown     next = link->next;
214b17ce1afSJed Brown     ierr = PetscFree(link);CHKERRQ(ierr);
215b17ce1afSJed Brown   }
216b17ce1afSJed Brown   (*dm)->coarsenhook = PETSC_NULL;
217b17ce1afSJed Brown 
2181a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
2191a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
2201a266240SBarry Smith   }
22187e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
22271cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
2234dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
2246bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
2256bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
2266bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
227073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
228a89ea682SMatthew G Knepley   ierr = PetscFree((*dm)->workArray);CHKERRQ(ierr);
229*88ed4aceSMatthew G Knepley 
230*88ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
231*88ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
232*88ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
233*88ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
234732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
2356bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
236732e2eb9SMatthew G Knepley 
2376bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
2386bf464f9SBarry Smith   ierr = PetscFree((*dm)->data);CHKERRQ(ierr);
239732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
24047c6ae99SBarry Smith   PetscFunctionReturn(0);
24147c6ae99SBarry Smith }
24247c6ae99SBarry Smith 
24347c6ae99SBarry Smith #undef __FUNCT__
244d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
245d7bf68aeSBarry Smith /*@
246d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
247d7bf68aeSBarry Smith 
248d7bf68aeSBarry Smith     Collective on DM
249d7bf68aeSBarry Smith 
250d7bf68aeSBarry Smith     Input Parameter:
251d7bf68aeSBarry Smith .   dm - the DM object to setup
252d7bf68aeSBarry Smith 
253d7bf68aeSBarry Smith     Level: developer
254d7bf68aeSBarry Smith 
255e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
256d7bf68aeSBarry Smith 
257d7bf68aeSBarry Smith @*/
2587087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
259d7bf68aeSBarry Smith {
260d7bf68aeSBarry Smith   PetscErrorCode ierr;
261d7bf68aeSBarry Smith 
262d7bf68aeSBarry Smith   PetscFunctionBegin;
263171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2648387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
265d7bf68aeSBarry Smith   if (dm->ops->setup) {
266d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
267d7bf68aeSBarry Smith   }
2688387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
269d7bf68aeSBarry Smith   PetscFunctionReturn(0);
270d7bf68aeSBarry Smith }
271d7bf68aeSBarry Smith 
272d7bf68aeSBarry Smith #undef __FUNCT__
273d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
274d7bf68aeSBarry Smith /*@
275d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
276d7bf68aeSBarry Smith 
277d7bf68aeSBarry Smith     Collective on DM
278d7bf68aeSBarry Smith 
279d7bf68aeSBarry Smith     Input Parameter:
280d7bf68aeSBarry Smith .   dm - the DM object to set options for
281d7bf68aeSBarry Smith 
282732e2eb9SMatthew G Knepley     Options Database:
283dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
284dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
285171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
286171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
287732e2eb9SMatthew G Knepley 
288d7bf68aeSBarry Smith     Level: developer
289d7bf68aeSBarry Smith 
290e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
291d7bf68aeSBarry Smith 
292d7bf68aeSBarry Smith @*/
2937087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
294d7bf68aeSBarry Smith {
29567ad5babSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg;
296d7bf68aeSBarry Smith   PetscErrorCode ierr;
297f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
298d7bf68aeSBarry Smith 
299d7bf68aeSBarry Smith   PetscFunctionBegin;
300171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3013194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
30282fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
303c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
304c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
30567ad5babSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_latex", "Output mesh in LaTeX TikZ format", "DMView", flg4, &flg4, PETSC_NULL);CHKERRQ(ierr);
306073dac72SJed 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);
307f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
308f9ba7244SBarry Smith     if (flg) {
309f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
310f9ba7244SBarry Smith     }
311521d9a4cSLisandro 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);
312073dac72SJed Brown     if (flg) {
313521d9a4cSLisandro Dalcin       ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
314073dac72SJed Brown     }
3151b89239cSHong 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);
316f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
317f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
318f9ba7244SBarry Smith     }
319f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
320f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
32182fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
32282fcb398SMatthew G Knepley   if (flg1) {
32382fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
32482fcb398SMatthew G Knepley   }
325c4721b0eSMatthew G Knepley   if (flg2) {
326c4721b0eSMatthew G Knepley     PetscViewer viewer;
327c4721b0eSMatthew G Knepley 
328c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
329c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
330c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
331c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
332c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
333c4721b0eSMatthew G Knepley   }
334c4721b0eSMatthew G Knepley   if (flg3) {
335c4721b0eSMatthew G Knepley     PetscViewer viewer;
336c4721b0eSMatthew G Knepley 
337c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
338c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
339c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
340c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
341c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
342c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
343c4721b0eSMatthew G Knepley   }
34467ad5babSMatthew G Knepley   if (flg4) {
34567ad5babSMatthew G Knepley     PetscViewer viewer;
34667ad5babSMatthew G Knepley 
34767ad5babSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
34867ad5babSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
34967ad5babSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_LATEX);CHKERRQ(ierr);
35067ad5babSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.tex");CHKERRQ(ierr);
35167ad5babSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
35267ad5babSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
35367ad5babSMatthew G Knepley   }
354d7bf68aeSBarry Smith   PetscFunctionReturn(0);
355d7bf68aeSBarry Smith }
356d7bf68aeSBarry Smith 
357d7bf68aeSBarry Smith #undef __FUNCT__
35847c6ae99SBarry Smith #define __FUNCT__ "DMView"
359fc9bc008SSatish Balay /*@C
360aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
36147c6ae99SBarry Smith 
36247c6ae99SBarry Smith     Collective on DM
36347c6ae99SBarry Smith 
36447c6ae99SBarry Smith     Input Parameter:
36547c6ae99SBarry Smith +   dm - the DM object to view
36647c6ae99SBarry Smith -   v - the viewer
36747c6ae99SBarry Smith 
36847c6ae99SBarry Smith     Level: developer
36947c6ae99SBarry Smith 
370e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
37147c6ae99SBarry Smith 
37247c6ae99SBarry Smith @*/
3737087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
37447c6ae99SBarry Smith {
37547c6ae99SBarry Smith   PetscErrorCode ierr;
37647c6ae99SBarry Smith 
37747c6ae99SBarry Smith   PetscFunctionBegin;
378171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3793014e516SBarry Smith  if (!v) {
3803014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
3813014e516SBarry Smith   }
3820c010503SBarry Smith   if (dm->ops->view) {
3830c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
38447c6ae99SBarry Smith   }
38547c6ae99SBarry Smith   PetscFunctionReturn(0);
38647c6ae99SBarry Smith }
38747c6ae99SBarry Smith 
38847c6ae99SBarry Smith #undef __FUNCT__
38947c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
39047c6ae99SBarry Smith /*@
391aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
39247c6ae99SBarry Smith 
39347c6ae99SBarry Smith     Collective on DM
39447c6ae99SBarry Smith 
39547c6ae99SBarry Smith     Input Parameter:
39647c6ae99SBarry Smith .   dm - the DM object
39747c6ae99SBarry Smith 
39847c6ae99SBarry Smith     Output Parameter:
39947c6ae99SBarry Smith .   vec - the global vector
40047c6ae99SBarry Smith 
401073dac72SJed Brown     Level: beginner
40247c6ae99SBarry Smith 
403e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
40447c6ae99SBarry Smith 
40547c6ae99SBarry Smith @*/
4067087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
40747c6ae99SBarry Smith {
40847c6ae99SBarry Smith   PetscErrorCode ierr;
40947c6ae99SBarry Smith 
41047c6ae99SBarry Smith   PetscFunctionBegin;
411171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
412*88ed4aceSMatthew G Knepley   if (dm->defaultSection) {
413*88ed4aceSMatthew G Knepley     PetscSection gSection;
414*88ed4aceSMatthew G Knepley     PetscInt     localSize;
415*88ed4aceSMatthew G Knepley 
416*88ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
417*88ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
418*88ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
419*88ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
420*88ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
421*88ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
422*88ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
423*88ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
424*88ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
425*88ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
426*88ed4aceSMatthew G Knepley   } else {
42747c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
428*88ed4aceSMatthew G Knepley   }
42947c6ae99SBarry Smith   PetscFunctionReturn(0);
43047c6ae99SBarry Smith }
43147c6ae99SBarry Smith 
43247c6ae99SBarry Smith #undef __FUNCT__
43347c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
43447c6ae99SBarry Smith /*@
435aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
43647c6ae99SBarry Smith 
43747c6ae99SBarry Smith     Not Collective
43847c6ae99SBarry Smith 
43947c6ae99SBarry Smith     Input Parameter:
44047c6ae99SBarry Smith .   dm - the DM object
44147c6ae99SBarry Smith 
44247c6ae99SBarry Smith     Output Parameter:
44347c6ae99SBarry Smith .   vec - the local vector
44447c6ae99SBarry Smith 
445073dac72SJed Brown     Level: beginner
44647c6ae99SBarry Smith 
447e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
44847c6ae99SBarry Smith 
44947c6ae99SBarry Smith @*/
4507087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
45147c6ae99SBarry Smith {
45247c6ae99SBarry Smith   PetscErrorCode ierr;
45347c6ae99SBarry Smith 
45447c6ae99SBarry Smith   PetscFunctionBegin;
455171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
456*88ed4aceSMatthew G Knepley   if (dm->defaultSection) {
457*88ed4aceSMatthew G Knepley     PetscInt localSize;
458*88ed4aceSMatthew G Knepley 
459*88ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
460*88ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
461*88ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
462*88ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
463*88ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
464*88ed4aceSMatthew G Knepley   } else {
46547c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
466*88ed4aceSMatthew G Knepley   }
46747c6ae99SBarry Smith   PetscFunctionReturn(0);
46847c6ae99SBarry Smith }
46947c6ae99SBarry Smith 
47047c6ae99SBarry Smith #undef __FUNCT__
4711411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
4721411c6eeSJed Brown /*@
4731411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
4741411c6eeSJed Brown 
4751411c6eeSJed Brown    Collective on DM
4761411c6eeSJed Brown 
4771411c6eeSJed Brown    Input Parameter:
4781411c6eeSJed Brown .  dm - the DM that provides the mapping
4791411c6eeSJed Brown 
4801411c6eeSJed Brown    Output Parameter:
4811411c6eeSJed Brown .  ltog - the mapping
4821411c6eeSJed Brown 
4831411c6eeSJed Brown    Level: intermediate
4841411c6eeSJed Brown 
4851411c6eeSJed Brown    Notes:
4861411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
4871411c6eeSJed Brown    MatSetLocalToGlobalMapping().
4881411c6eeSJed Brown 
4891411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
4901411c6eeSJed Brown @*/
4917087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
4921411c6eeSJed Brown {
4931411c6eeSJed Brown   PetscErrorCode ierr;
4941411c6eeSJed Brown 
4951411c6eeSJed Brown   PetscFunctionBegin;
4961411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4971411c6eeSJed Brown   PetscValidPointer(ltog,2);
4981411c6eeSJed Brown   if (!dm->ltogmap) {
4991411c6eeSJed Brown     if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
5001411c6eeSJed Brown     ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
5011411c6eeSJed Brown   }
5021411c6eeSJed Brown   *ltog = dm->ltogmap;
5031411c6eeSJed Brown   PetscFunctionReturn(0);
5041411c6eeSJed Brown }
5051411c6eeSJed Brown 
5061411c6eeSJed Brown #undef __FUNCT__
5071411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
5081411c6eeSJed Brown /*@
5091411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
5101411c6eeSJed Brown 
5111411c6eeSJed Brown    Collective on DM
5121411c6eeSJed Brown 
5131411c6eeSJed Brown    Input Parameter:
5141411c6eeSJed Brown .  da - the distributed array that provides the mapping
5151411c6eeSJed Brown 
5161411c6eeSJed Brown    Output Parameter:
5171411c6eeSJed Brown .  ltog - the block mapping
5181411c6eeSJed Brown 
5191411c6eeSJed Brown    Level: intermediate
5201411c6eeSJed Brown 
5211411c6eeSJed Brown    Notes:
5221411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
5231411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
5241411c6eeSJed Brown 
5251411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
5261411c6eeSJed Brown @*/
5277087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
5281411c6eeSJed Brown {
5291411c6eeSJed Brown   PetscErrorCode ierr;
5301411c6eeSJed Brown 
5311411c6eeSJed Brown   PetscFunctionBegin;
5321411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5331411c6eeSJed Brown   PetscValidPointer(ltog,2);
5341411c6eeSJed Brown   if (!dm->ltogmapb) {
5351411c6eeSJed Brown     PetscInt bs;
5361411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
5371411c6eeSJed Brown     if (bs > 1) {
5381411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
5391411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
5401411c6eeSJed Brown     } else {
5411411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
5421411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
5431411c6eeSJed Brown     }
5441411c6eeSJed Brown   }
5451411c6eeSJed Brown   *ltog = dm->ltogmapb;
5461411c6eeSJed Brown   PetscFunctionReturn(0);
5471411c6eeSJed Brown }
5481411c6eeSJed Brown 
5491411c6eeSJed Brown #undef __FUNCT__
5501411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
5511411c6eeSJed Brown /*@
5521411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
5531411c6eeSJed Brown 
5541411c6eeSJed Brown    Not Collective
5551411c6eeSJed Brown 
5561411c6eeSJed Brown    Input Parameter:
5571411c6eeSJed Brown .  dm - the DM with block structure
5581411c6eeSJed Brown 
5591411c6eeSJed Brown    Output Parameter:
5601411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
5611411c6eeSJed Brown 
5621411c6eeSJed Brown    Level: intermediate
5631411c6eeSJed Brown 
5641411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
5651411c6eeSJed Brown @*/
5667087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
5671411c6eeSJed Brown {
5681411c6eeSJed Brown   PetscFunctionBegin;
5691411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5701411c6eeSJed Brown   PetscValidPointer(bs,2);
5711411c6eeSJed 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");
5721411c6eeSJed Brown   *bs = dm->bs;
5731411c6eeSJed Brown   PetscFunctionReturn(0);
5741411c6eeSJed Brown }
5751411c6eeSJed Brown 
5761411c6eeSJed Brown #undef __FUNCT__
577e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
57847c6ae99SBarry Smith /*@
579e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
58047c6ae99SBarry Smith 
58147c6ae99SBarry Smith     Collective on DM
58247c6ae99SBarry Smith 
58347c6ae99SBarry Smith     Input Parameter:
58447c6ae99SBarry Smith +   dm1 - the DM object
58547c6ae99SBarry Smith -   dm2 - the second, finer DM object
58647c6ae99SBarry Smith 
58747c6ae99SBarry Smith     Output Parameter:
58847c6ae99SBarry Smith +  mat - the interpolation
58947c6ae99SBarry Smith -  vec - the scaling (optional)
59047c6ae99SBarry Smith 
59147c6ae99SBarry Smith     Level: developer
59247c6ae99SBarry Smith 
59385afcc9aSBarry 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
59485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
595d52bd9f3SBarry Smith 
596d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
597d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
59885afcc9aSBarry Smith 
59985afcc9aSBarry Smith 
600e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
60147c6ae99SBarry Smith 
60247c6ae99SBarry Smith @*/
603e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
60447c6ae99SBarry Smith {
60547c6ae99SBarry Smith   PetscErrorCode ierr;
60647c6ae99SBarry Smith 
60747c6ae99SBarry Smith   PetscFunctionBegin;
608171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
609171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
61025296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
61147c6ae99SBarry Smith   PetscFunctionReturn(0);
61247c6ae99SBarry Smith }
61347c6ae99SBarry Smith 
61447c6ae99SBarry Smith #undef __FUNCT__
615e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
61647c6ae99SBarry Smith /*@
617e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
61847c6ae99SBarry Smith 
61947c6ae99SBarry Smith     Collective on DM
62047c6ae99SBarry Smith 
62147c6ae99SBarry Smith     Input Parameter:
62247c6ae99SBarry Smith +   dm1 - the DM object
62347c6ae99SBarry Smith -   dm2 - the second, finer DM object
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith     Output Parameter:
62647c6ae99SBarry Smith .   ctx - the injection
62747c6ae99SBarry Smith 
62847c6ae99SBarry Smith     Level: developer
62947c6ae99SBarry Smith 
63085afcc9aSBarry 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
63185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
63285afcc9aSBarry Smith 
633e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
63447c6ae99SBarry Smith 
63547c6ae99SBarry Smith @*/
636e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
63747c6ae99SBarry Smith {
63847c6ae99SBarry Smith   PetscErrorCode ierr;
63947c6ae99SBarry Smith 
64047c6ae99SBarry Smith   PetscFunctionBegin;
641171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
642171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
64347c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
64447c6ae99SBarry Smith   PetscFunctionReturn(0);
64547c6ae99SBarry Smith }
64647c6ae99SBarry Smith 
64747c6ae99SBarry Smith #undef __FUNCT__
648e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
649d1e2c406SBarry Smith /*@C
650e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
65147c6ae99SBarry Smith 
65247c6ae99SBarry Smith     Collective on DM
65347c6ae99SBarry Smith 
65447c6ae99SBarry Smith     Input Parameter:
65547c6ae99SBarry Smith +   dm - the DM object
65647c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
65747c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
65847c6ae99SBarry Smith 
65947c6ae99SBarry Smith     Output Parameter:
66047c6ae99SBarry Smith .   coloring - the coloring
66147c6ae99SBarry Smith 
66247c6ae99SBarry Smith     Level: developer
66347c6ae99SBarry Smith 
664e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
66547c6ae99SBarry Smith 
666aab9d709SJed Brown @*/
667e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
66847c6ae99SBarry Smith {
66947c6ae99SBarry Smith   PetscErrorCode ierr;
67047c6ae99SBarry Smith 
67147c6ae99SBarry Smith   PetscFunctionBegin;
672171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
67347c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
67447c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
67547c6ae99SBarry Smith   PetscFunctionReturn(0);
67647c6ae99SBarry Smith }
67747c6ae99SBarry Smith 
67847c6ae99SBarry Smith #undef __FUNCT__
679950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
68047c6ae99SBarry Smith /*@C
681950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
68247c6ae99SBarry Smith 
68347c6ae99SBarry Smith     Collective on DM
68447c6ae99SBarry Smith 
68547c6ae99SBarry Smith     Input Parameter:
68647c6ae99SBarry Smith +   dm - the DM object
68747c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
68894013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
68947c6ae99SBarry Smith 
69047c6ae99SBarry Smith     Output Parameter:
69147c6ae99SBarry Smith .   mat - the empty Jacobian
69247c6ae99SBarry Smith 
693073dac72SJed Brown     Level: beginner
69447c6ae99SBarry Smith 
69594013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
69694013140SBarry Smith        do not need to do it yourself.
69794013140SBarry Smith 
69894013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
699aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
70094013140SBarry Smith 
70194013140SBarry 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
70294013140SBarry Smith        internally by PETSc.
70394013140SBarry Smith 
70494013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
705aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
70694013140SBarry Smith 
707e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
70847c6ae99SBarry Smith 
709aab9d709SJed Brown @*/
710950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
71147c6ae99SBarry Smith {
71247c6ae99SBarry Smith   PetscErrorCode ierr;
71347c6ae99SBarry Smith 
71447c6ae99SBarry Smith   PetscFunctionBegin;
715171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
716235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
717235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
718235683edSBarry Smith #endif
719c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
720c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
721073dac72SJed Brown   if (dm->mattype) {
72225296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
723073dac72SJed Brown   } else {
72425296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
725c7b7c8a4SJed Brown   }
72647c6ae99SBarry Smith   PetscFunctionReturn(0);
72747c6ae99SBarry Smith }
72847c6ae99SBarry Smith 
72947c6ae99SBarry Smith #undef __FUNCT__
730732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
731732e2eb9SMatthew G Knepley /*@
732950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
733732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
734732e2eb9SMatthew G Knepley 
735732e2eb9SMatthew G Knepley   Logically Collective on DMDA
736732e2eb9SMatthew G Knepley 
737732e2eb9SMatthew G Knepley   Input Parameter:
738732e2eb9SMatthew G Knepley + dm - the DM
739732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
740732e2eb9SMatthew G Knepley 
741732e2eb9SMatthew G Knepley   Level: developer
742950540a4SJed Brown .seealso DMCreateMatrix()
743732e2eb9SMatthew G Knepley @*/
744732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
745732e2eb9SMatthew G Knepley {
746732e2eb9SMatthew G Knepley   PetscFunctionBegin;
747732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
748732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
749732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
750732e2eb9SMatthew G Knepley }
751732e2eb9SMatthew G Knepley 
752732e2eb9SMatthew G Knepley #undef __FUNCT__
753a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
754a89ea682SMatthew G Knepley /*@C
755a89ea682SMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size
756a89ea682SMatthew G Knepley 
757a89ea682SMatthew G Knepley   Not Collective
758a89ea682SMatthew G Knepley 
759a89ea682SMatthew G Knepley   Input Parameters:
760a89ea682SMatthew G Knepley + dm - the DM object
761a89ea682SMatthew G Knepley - size - The minium size
762a89ea682SMatthew G Knepley 
763a89ea682SMatthew G Knepley   Output Parameter:
764a89ea682SMatthew G Knepley . array - the work array
765a89ea682SMatthew G Knepley 
766a89ea682SMatthew G Knepley   Level: developer
767a89ea682SMatthew G Knepley 
768a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
769a89ea682SMatthew G Knepley @*/
770a89ea682SMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt size,PetscScalar **array)
771a89ea682SMatthew G Knepley {
772a89ea682SMatthew G Knepley   PetscErrorCode ierr;
773a89ea682SMatthew G Knepley 
774a89ea682SMatthew G Knepley   PetscFunctionBegin;
775a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
776a89ea682SMatthew G Knepley   PetscValidPointer(array,3);
777a89ea682SMatthew G Knepley   if (size > dm->workSize) {
778a89ea682SMatthew G Knepley     dm->workSize = size;
779a89ea682SMatthew G Knepley     ierr = PetscFree(dm->workArray);CHKERRQ(ierr);
780a89ea682SMatthew G Knepley     ierr = PetscMalloc(dm->workSize * sizeof(PetscScalar), &dm->workArray);CHKERRQ(ierr);
781a89ea682SMatthew G Knepley   }
782a89ea682SMatthew G Knepley   *array = dm->workArray;
783a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
784a89ea682SMatthew G Knepley }
785a89ea682SMatthew G Knepley 
7864d343eeaSMatthew G Knepley #undef __FUNCT__
787e7c4fc90SDmitry Karpeev #define __FUNCT__ "DMCreateDecompositionDM"
788e7c4fc90SDmitry Karpeev /*@C
78901bc414fSDmitry Karpeev   DMCreateDecompositionDM - creates a DM that encapsulates a decomposition of the original DM.
790e7c4fc90SDmitry Karpeev 
791e7c4fc90SDmitry Karpeev   Not Collective
792e7c4fc90SDmitry Karpeev 
793e7c4fc90SDmitry Karpeev   Input Parameters:
794e7c4fc90SDmitry Karpeev + dm   - the DM object
795e7c4fc90SDmitry Karpeev - name - the name of the decomposition
796e7c4fc90SDmitry Karpeev 
797e7c4fc90SDmitry Karpeev   Output Parameter:
798e7c4fc90SDmitry Karpeev . ddm  - the decomposition DM (PETSC_NULL, if no such decomposition is known)
799e7c4fc90SDmitry Karpeev 
800e7c4fc90SDmitry Karpeev   Level: advanced
801e7c4fc90SDmitry Karpeev 
802e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateDecomposition()
803e7c4fc90SDmitry Karpeev @*/
804e7c4fc90SDmitry Karpeev PetscErrorCode DMCreateDecompositionDM(DM dm, const char* name, DM *ddm)
805e7c4fc90SDmitry Karpeev {
806e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
807e7c4fc90SDmitry Karpeev 
808e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
809e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
810e7c4fc90SDmitry Karpeev   PetscValidCharPointer(name,2);
811e7c4fc90SDmitry Karpeev   PetscValidPointer(ddm,3);
812e7c4fc90SDmitry Karpeev   if(!dm->ops->createdecompositiondm) {
813e7c4fc90SDmitry Karpeev     *ddm = PETSC_NULL;
814e7c4fc90SDmitry Karpeev   }
815e7c4fc90SDmitry Karpeev   else {
816e7c4fc90SDmitry Karpeev     ierr = (*dm->ops->createdecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
817e7c4fc90SDmitry Karpeev   }
818e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
819e7c4fc90SDmitry Karpeev }
820e7c4fc90SDmitry Karpeev 
821e7c4fc90SDmitry Karpeev #undef __FUNCT__
8224d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
8234f3b5142SJed Brown /*@C
8244d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
8254d343eeaSMatthew G Knepley 
8264d343eeaSMatthew G Knepley   Not collective
8274d343eeaSMatthew G Knepley 
8284d343eeaSMatthew G Knepley   Input Parameter:
8294d343eeaSMatthew G Knepley . dm - the DM object
8304d343eeaSMatthew G Knepley 
8314d343eeaSMatthew G Knepley   Output Parameters:
83221c9b008SJed Brown + numFields - The number of fields (or PETSC_NULL if not requested)
83321c9b008SJed Brown . names     - The name for each field (or PETSC_NULL if not requested)
83421c9b008SJed Brown - fields    - The global indices for each field (or PETSC_NULL if not requested)
8354d343eeaSMatthew G Knepley 
8364d343eeaSMatthew G Knepley   Level: intermediate
8374d343eeaSMatthew G Knepley 
83821c9b008SJed Brown   Notes:
83921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
84021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
84121c9b008SJed Brown   PetscFree().
84221c9b008SJed Brown 
8434d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
8444d343eeaSMatthew G Knepley @*/
84521c9b008SJed Brown PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***names, IS **fields)
8464d343eeaSMatthew G Knepley {
8474d343eeaSMatthew G Knepley   PetscErrorCode ierr;
8484d343eeaSMatthew G Knepley 
8494d343eeaSMatthew G Knepley   PetscFunctionBegin;
8504d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85169ca1f37SDmitry Karpeev   if (numFields) {
85269ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
85369ca1f37SDmitry Karpeev     *numFields = 0;
85469ca1f37SDmitry Karpeev   }
85569ca1f37SDmitry Karpeev   if (names) {
85669ca1f37SDmitry Karpeev     PetscValidPointer(names,3);
85769ca1f37SDmitry Karpeev     *names = PETSC_NULL;
85869ca1f37SDmitry Karpeev   }
85969ca1f37SDmitry Karpeev   if (fields) {
86069ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
86169ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
86269ca1f37SDmitry Karpeev   }
86369ca1f37SDmitry Karpeev   if(dm->ops->createfieldis) {
8644d343eeaSMatthew G Knepley     ierr = (*dm->ops->createfieldis)(dm, numFields, names, fields);CHKERRQ(ierr);
86569ca1f37SDmitry Karpeev   }
8664d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
8674d343eeaSMatthew G Knepley }
8684d343eeaSMatthew G Knepley 
8695fe1f584SPeter Brune 
870a89ea682SMatthew G Knepley #undef __FUNCT__
871e7c4fc90SDmitry Karpeev #define __FUNCT__ "DMCreateDecomposition"
872e7c4fc90SDmitry Karpeev /*@C
873e7c4fc90SDmitry Karpeev   DMCreateDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems:
874e7c4fc90SDmitry Karpeev                           each IS contains the global indices of the dofs of the corresponding subproblem.
875e7c4fc90SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
876e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
877e7c4fc90SDmitry Karpeev 
878e7c4fc90SDmitry Karpeev   Not collective
879e7c4fc90SDmitry Karpeev 
880e7c4fc90SDmitry Karpeev   Input Parameter:
881e7c4fc90SDmitry Karpeev . dm - the DM object
882e7c4fc90SDmitry Karpeev 
883e7c4fc90SDmitry Karpeev   Output Parameters:
884e7c4fc90SDmitry Karpeev + len       - The number of subproblems in the decomposition (or PETSC_NULL if not requested)
885e7c4fc90SDmitry Karpeev . namelist  - The name for each subproblem (or PETSC_NULL if not requested)
886e7c4fc90SDmitry Karpeev . islist    - The global indices for each subproblem (or PETSC_NULL if not requested)
887e7c4fc90SDmitry Karpeev - dmlist    - The DMs for each subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
888e7c4fc90SDmitry Karpeev 
889e7c4fc90SDmitry Karpeev   Level: intermediate
890e7c4fc90SDmitry Karpeev 
891e7c4fc90SDmitry Karpeev   Notes:
892e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
893e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
894e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
895e7c4fc90SDmitry Karpeev 
896e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
897e7c4fc90SDmitry Karpeev @*/
898e7c4fc90SDmitry Karpeev PetscErrorCode DMCreateDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
899e7c4fc90SDmitry Karpeev {
900e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
901e7c4fc90SDmitry Karpeev 
902e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
903e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
904e7c4fc90SDmitry Karpeev   if (len) {PetscValidPointer(len,2);}
905e7c4fc90SDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3);}
906e7c4fc90SDmitry Karpeev   if (islist) {PetscValidPointer(islist,4);}
907e7c4fc90SDmitry Karpeev   if (dmlist) {PetscValidPointer(dmlist,5);}
908e7c4fc90SDmitry Karpeev   if(!dm->ops->createdecomposition) {
90969ca1f37SDmitry Karpeev     ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
910e7c4fc90SDmitry Karpeev     /* By default there are no DMs associated with subproblems. */
911e7c4fc90SDmitry Karpeev     if(dmlist) *dmlist = PETSC_NULL;
912e7c4fc90SDmitry Karpeev   }
913e7c4fc90SDmitry Karpeev   else {
914e7c4fc90SDmitry Karpeev     ierr = (*dm->ops->createdecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
915e7c4fc90SDmitry Karpeev   }
916e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
917e7c4fc90SDmitry Karpeev }
918e7c4fc90SDmitry Karpeev 
919e7c4fc90SDmitry Karpeev 
920e7c4fc90SDmitry Karpeev #undef __FUNCT__
92147c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
92247c6ae99SBarry Smith /*@
92347c6ae99SBarry Smith   DMRefine - Refines a DM object
92447c6ae99SBarry Smith 
92547c6ae99SBarry Smith   Collective on DM
92647c6ae99SBarry Smith 
92747c6ae99SBarry Smith   Input Parameter:
92847c6ae99SBarry Smith + dm   - the DM object
92991d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
93047c6ae99SBarry Smith 
93147c6ae99SBarry Smith   Output Parameter:
932ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
933ae0a1c52SMatthew G Knepley 
934ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith   Level: developer
93747c6ae99SBarry Smith 
938e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
93947c6ae99SBarry Smith @*/
9407087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
94147c6ae99SBarry Smith {
94247c6ae99SBarry Smith   PetscErrorCode ierr;
94347c6ae99SBarry Smith 
94447c6ae99SBarry Smith   PetscFunctionBegin;
945732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
94647c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
9474057135bSMatthew G Knepley   if (*dmf) {
94843842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
949644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
950644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
951644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
952644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
953644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
954644e2e5bSBarry Smith     }
9558cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
956644e2e5bSBarry Smith     (*dmf)->ctx     = dm->ctx;
957656b349aSBarry Smith     (*dmf)->levelup = dm->levelup + 1;
9584057135bSMatthew G Knepley   }
95947c6ae99SBarry Smith   PetscFunctionReturn(0);
96047c6ae99SBarry Smith }
96147c6ae99SBarry Smith 
96247c6ae99SBarry Smith #undef __FUNCT__
963eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
964eb3f98d2SBarry Smith /*@
965eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
966eb3f98d2SBarry Smith 
967eb3f98d2SBarry Smith     Not Collective
968eb3f98d2SBarry Smith 
969eb3f98d2SBarry Smith     Input Parameter:
970eb3f98d2SBarry Smith .   dm - the DM object
971eb3f98d2SBarry Smith 
972eb3f98d2SBarry Smith     Output Parameter:
973eb3f98d2SBarry Smith .   level - number of refinements
974eb3f98d2SBarry Smith 
975eb3f98d2SBarry Smith     Level: developer
976eb3f98d2SBarry Smith 
9776a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
978eb3f98d2SBarry Smith 
979eb3f98d2SBarry Smith @*/
980eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
981eb3f98d2SBarry Smith {
982eb3f98d2SBarry Smith   PetscFunctionBegin;
983eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
984eb3f98d2SBarry Smith   *level = dm->levelup;
985eb3f98d2SBarry Smith   PetscFunctionReturn(0);
986eb3f98d2SBarry Smith }
987eb3f98d2SBarry Smith 
988eb3f98d2SBarry Smith #undef __FUNCT__
98947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
99047c6ae99SBarry Smith /*@
99147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
99247c6ae99SBarry Smith 
99347c6ae99SBarry Smith     Neighbor-wise Collective on DM
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith     Input Parameters:
99647c6ae99SBarry Smith +   dm - the DM object
99747c6ae99SBarry Smith .   g - the global vector
99847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
99947c6ae99SBarry Smith -   l - the local vector
100047c6ae99SBarry Smith 
100147c6ae99SBarry Smith 
100247c6ae99SBarry Smith     Level: beginner
100347c6ae99SBarry Smith 
1004e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith @*/
10077087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
100847c6ae99SBarry Smith {
100947c6ae99SBarry Smith   PetscErrorCode ierr;
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith   PetscFunctionBegin;
1012171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1013843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
101447c6ae99SBarry Smith   PetscFunctionReturn(0);
101547c6ae99SBarry Smith }
101647c6ae99SBarry Smith 
101747c6ae99SBarry Smith #undef __FUNCT__
101847c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
101947c6ae99SBarry Smith /*@
102047c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
102147c6ae99SBarry Smith 
102247c6ae99SBarry Smith     Neighbor-wise Collective on DM
102347c6ae99SBarry Smith 
102447c6ae99SBarry Smith     Input Parameters:
102547c6ae99SBarry Smith +   dm - the DM object
102647c6ae99SBarry Smith .   g - the global vector
102747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
102847c6ae99SBarry Smith -   l - the local vector
102947c6ae99SBarry Smith 
103047c6ae99SBarry Smith 
103147c6ae99SBarry Smith     Level: beginner
103247c6ae99SBarry Smith 
1033e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
103447c6ae99SBarry Smith 
103547c6ae99SBarry Smith @*/
10367087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
103747c6ae99SBarry Smith {
103847c6ae99SBarry Smith   PetscErrorCode ierr;
103947c6ae99SBarry Smith 
104047c6ae99SBarry Smith   PetscFunctionBegin;
1041171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1042843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
104347c6ae99SBarry Smith   PetscFunctionReturn(0);
104447c6ae99SBarry Smith }
104547c6ae99SBarry Smith 
104647c6ae99SBarry Smith #undef __FUNCT__
10479a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
104847c6ae99SBarry Smith /*@
10499a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
10509a42bb27SBarry Smith 
10519a42bb27SBarry Smith     Neighbor-wise Collective on DM
10529a42bb27SBarry Smith 
10539a42bb27SBarry Smith     Input Parameters:
10549a42bb27SBarry Smith +   dm - the DM object
1055f6813fd5SJed Brown .   l - the local vector
10569a42bb27SBarry 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
10579a42bb27SBarry Smith            base point.
1058f6813fd5SJed Brown - - the global vector
10599a42bb27SBarry Smith 
10609a42bb27SBarry 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
10619a42bb27SBarry 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
10629a42bb27SBarry Smith            global array to the final global array with VecAXPY().
10639a42bb27SBarry Smith 
10649a42bb27SBarry Smith     Level: beginner
10659a42bb27SBarry Smith 
1066e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
10679a42bb27SBarry Smith 
10689a42bb27SBarry Smith @*/
10697087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
10709a42bb27SBarry Smith {
10719a42bb27SBarry Smith   PetscErrorCode ierr;
10729a42bb27SBarry Smith 
10739a42bb27SBarry Smith   PetscFunctionBegin;
1074171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1075843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
10769a42bb27SBarry Smith   PetscFunctionReturn(0);
10779a42bb27SBarry Smith }
10789a42bb27SBarry Smith 
10799a42bb27SBarry Smith #undef __FUNCT__
10809a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
10819a42bb27SBarry Smith /*@
10829a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
108347c6ae99SBarry Smith 
108447c6ae99SBarry Smith     Neighbor-wise Collective on DM
108547c6ae99SBarry Smith 
108647c6ae99SBarry Smith     Input Parameters:
108747c6ae99SBarry Smith +   dm - the DM object
1088f6813fd5SJed Brown .   l - the local vector
108947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1090f6813fd5SJed Brown -   g - the global vector
109147c6ae99SBarry Smith 
109247c6ae99SBarry Smith 
109347c6ae99SBarry Smith     Level: beginner
109447c6ae99SBarry Smith 
1095e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
109647c6ae99SBarry Smith 
109747c6ae99SBarry Smith @*/
10987087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
109947c6ae99SBarry Smith {
110047c6ae99SBarry Smith   PetscErrorCode ierr;
110147c6ae99SBarry Smith 
110247c6ae99SBarry Smith   PetscFunctionBegin;
1103171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1104843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
110547c6ae99SBarry Smith   PetscFunctionReturn(0);
110647c6ae99SBarry Smith }
110747c6ae99SBarry Smith 
110847c6ae99SBarry Smith #undef __FUNCT__
110947c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
111047c6ae99SBarry Smith /*@
111147c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
111247c6ae99SBarry Smith 
111347c6ae99SBarry Smith     Collective on DM
111447c6ae99SBarry Smith 
111547c6ae99SBarry Smith     Input Parameter:
111647c6ae99SBarry Smith +   dm - the DM object
111747c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
111847c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
111947c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
112047c6ae99SBarry Smith 
112147c6ae99SBarry Smith     Level: developer
112247c6ae99SBarry Smith 
1123e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
112447c6ae99SBarry Smith          DMSetFunction()
112547c6ae99SBarry Smith 
112647c6ae99SBarry Smith @*/
11277087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
112847c6ae99SBarry Smith {
112947c6ae99SBarry Smith   PetscErrorCode ierr;
1130171400e9SBarry Smith 
113147c6ae99SBarry Smith   PetscFunctionBegin;
1132171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
113347c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
113447c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
113547c6ae99SBarry Smith   if (A != B) {
113647c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
113747c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
113847c6ae99SBarry Smith   }
113947c6ae99SBarry Smith   PetscFunctionReturn(0);
114047c6ae99SBarry Smith }
114147c6ae99SBarry Smith 
114247c6ae99SBarry Smith #undef __FUNCT__
114347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
114447c6ae99SBarry Smith /*@
114547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
114647c6ae99SBarry Smith 
114747c6ae99SBarry Smith     Collective on DM
114847c6ae99SBarry Smith 
114947c6ae99SBarry Smith     Input Parameter:
115047c6ae99SBarry Smith +   dm - the DM object
115191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
115247c6ae99SBarry Smith 
115347c6ae99SBarry Smith     Output Parameter:
115447c6ae99SBarry Smith .   dmc - the coarsened DM
115547c6ae99SBarry Smith 
115647c6ae99SBarry Smith     Level: developer
115747c6ae99SBarry Smith 
1158e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
115947c6ae99SBarry Smith 
116047c6ae99SBarry Smith @*/
11617087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
116247c6ae99SBarry Smith {
116347c6ae99SBarry Smith   PetscErrorCode ierr;
1164b17ce1afSJed Brown   DMCoarsenHookLink link;
116547c6ae99SBarry Smith 
116647c6ae99SBarry Smith   PetscFunctionBegin;
1167171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
116847c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
116943842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
117047c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
117147c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
117247c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
117347c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
117447c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
117547c6ae99SBarry Smith   }
11768cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1177644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
1178656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1179b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1180b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1181b17ce1afSJed Brown   }
1182b17ce1afSJed Brown   PetscFunctionReturn(0);
1183b17ce1afSJed Brown }
1184b17ce1afSJed Brown 
1185b17ce1afSJed Brown #undef __FUNCT__
1186b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1187b17ce1afSJed Brown /*@
1188b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1189b17ce1afSJed Brown 
1190b17ce1afSJed Brown    Logically Collective
1191b17ce1afSJed Brown 
1192b17ce1afSJed Brown    Input Arguments:
1193b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1194b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1195b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1196b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1197b17ce1afSJed Brown 
1198b17ce1afSJed Brown    Calling sequence of coarsenhook:
1199b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1200b17ce1afSJed Brown 
1201b17ce1afSJed Brown +  fine - fine level DM
1202b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1203b17ce1afSJed Brown -  ctx - optional user-defined function context
1204b17ce1afSJed Brown 
1205b17ce1afSJed Brown    Calling sequence for restricthook:
1206b17ce1afSJed Brown $    restricthook(DM fine,Mat mrestrict,Mat inject,DM coarse,void *ctx)
1207b17ce1afSJed Brown 
1208b17ce1afSJed Brown +  fine - fine level DM
1209b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1210b17ce1afSJed Brown .  inject - matrix restricting by applying the transpose of injection
1211b17ce1afSJed Brown .  coarse - coarse level DM to update
1212b17ce1afSJed Brown -  ctx - optional user-defined function context
1213b17ce1afSJed Brown 
1214b17ce1afSJed Brown    Level: advanced
1215b17ce1afSJed Brown 
1216b17ce1afSJed Brown    Notes:
1217b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1218b17ce1afSJed Brown 
1219b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1220b17ce1afSJed Brown 
1221b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1222b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1223b17ce1afSJed Brown 
1224b17ce1afSJed Brown .seealso: SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1225b17ce1afSJed Brown @*/
1226b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1227b17ce1afSJed Brown {
1228b17ce1afSJed Brown   PetscErrorCode ierr;
1229b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1230b17ce1afSJed Brown 
1231b17ce1afSJed Brown   PetscFunctionBegin;
1232b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
12336bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1234b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1235b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1236b17ce1afSJed Brown   link->restricthook = restricthook;
1237b17ce1afSJed Brown   link->ctx = ctx;
12386cab3a1bSJed Brown   link->next = PETSC_NULL;
1239b17ce1afSJed Brown   *p = link;
1240b17ce1afSJed Brown   PetscFunctionReturn(0);
1241b17ce1afSJed Brown }
1242b17ce1afSJed Brown 
1243b17ce1afSJed Brown #undef __FUNCT__
1244b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1245b17ce1afSJed Brown /*@
1246b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1247b17ce1afSJed Brown 
1248b17ce1afSJed Brown    Collective if any hooks are
1249b17ce1afSJed Brown 
1250b17ce1afSJed Brown    Input Arguments:
1251b17ce1afSJed Brown +  fine - finer DM to use as a base
1252b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1253b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1254b17ce1afSJed Brown -  coarse - coarer DM to update
1255b17ce1afSJed Brown 
1256b17ce1afSJed Brown    Level: developer
1257b17ce1afSJed Brown 
1258b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1259b17ce1afSJed Brown @*/
1260b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1261b17ce1afSJed Brown {
1262b17ce1afSJed Brown   PetscErrorCode ierr;
1263b17ce1afSJed Brown   DMCoarsenHookLink link;
1264b17ce1afSJed Brown 
1265b17ce1afSJed Brown   PetscFunctionBegin;
1266b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1267b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1268b17ce1afSJed Brown   }
126947c6ae99SBarry Smith   PetscFunctionReturn(0);
127047c6ae99SBarry Smith }
127147c6ae99SBarry Smith 
127247c6ae99SBarry Smith #undef __FUNCT__
12735fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
12745fe1f584SPeter Brune /*@
12756a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
12765fe1f584SPeter Brune 
12775fe1f584SPeter Brune     Not Collective
12785fe1f584SPeter Brune 
12795fe1f584SPeter Brune     Input Parameter:
12805fe1f584SPeter Brune .   dm - the DM object
12815fe1f584SPeter Brune 
12825fe1f584SPeter Brune     Output Parameter:
12836a7d9d85SPeter Brune .   level - number of coarsenings
12845fe1f584SPeter Brune 
12855fe1f584SPeter Brune     Level: developer
12865fe1f584SPeter Brune 
12876a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
12885fe1f584SPeter Brune 
12895fe1f584SPeter Brune @*/
12905fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
12915fe1f584SPeter Brune {
12925fe1f584SPeter Brune   PetscFunctionBegin;
12935fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12945fe1f584SPeter Brune   *level = dm->leveldown;
12955fe1f584SPeter Brune   PetscFunctionReturn(0);
12965fe1f584SPeter Brune }
12975fe1f584SPeter Brune 
12985fe1f584SPeter Brune 
12995fe1f584SPeter Brune 
13005fe1f584SPeter Brune #undef __FUNCT__
130147c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
130247c6ae99SBarry Smith /*@C
130347c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
130447c6ae99SBarry Smith 
130547c6ae99SBarry Smith     Collective on DM
130647c6ae99SBarry Smith 
130747c6ae99SBarry Smith     Input Parameter:
130847c6ae99SBarry Smith +   dm - the DM object
130947c6ae99SBarry Smith -   nlevels - the number of levels of refinement
131047c6ae99SBarry Smith 
131147c6ae99SBarry Smith     Output Parameter:
131247c6ae99SBarry Smith .   dmf - the refined DM hierarchy
131347c6ae99SBarry Smith 
131447c6ae99SBarry Smith     Level: developer
131547c6ae99SBarry Smith 
1316e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
131747c6ae99SBarry Smith 
131847c6ae99SBarry Smith @*/
13197087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
132047c6ae99SBarry Smith {
132147c6ae99SBarry Smith   PetscErrorCode ierr;
132247c6ae99SBarry Smith 
132347c6ae99SBarry Smith   PetscFunctionBegin;
1324171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
132547c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
132647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
132747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
132847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
132947c6ae99SBarry Smith   } else if (dm->ops->refine) {
133047c6ae99SBarry Smith     PetscInt i;
133147c6ae99SBarry Smith 
133247c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
133347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
133447c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
133547c6ae99SBarry Smith     }
133647c6ae99SBarry Smith   } else {
133747c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
133847c6ae99SBarry Smith   }
133947c6ae99SBarry Smith   PetscFunctionReturn(0);
134047c6ae99SBarry Smith }
134147c6ae99SBarry Smith 
134247c6ae99SBarry Smith #undef __FUNCT__
134347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
134447c6ae99SBarry Smith /*@C
134547c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
134647c6ae99SBarry Smith 
134747c6ae99SBarry Smith     Collective on DM
134847c6ae99SBarry Smith 
134947c6ae99SBarry Smith     Input Parameter:
135047c6ae99SBarry Smith +   dm - the DM object
135147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
135247c6ae99SBarry Smith 
135347c6ae99SBarry Smith     Output Parameter:
135447c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
135547c6ae99SBarry Smith 
135647c6ae99SBarry Smith     Level: developer
135747c6ae99SBarry Smith 
1358e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
135947c6ae99SBarry Smith 
136047c6ae99SBarry Smith @*/
13617087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
136247c6ae99SBarry Smith {
136347c6ae99SBarry Smith   PetscErrorCode ierr;
136447c6ae99SBarry Smith 
136547c6ae99SBarry Smith   PetscFunctionBegin;
1366171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
136747c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
136847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
136947c6ae99SBarry Smith   PetscValidPointer(dmc,3);
137047c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
137147c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
137247c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
137347c6ae99SBarry Smith     PetscInt i;
137447c6ae99SBarry Smith 
137547c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
137647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
137747c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
137847c6ae99SBarry Smith     }
137947c6ae99SBarry Smith   } else {
138047c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
138147c6ae99SBarry Smith   }
138247c6ae99SBarry Smith   PetscFunctionReturn(0);
138347c6ae99SBarry Smith }
138447c6ae99SBarry Smith 
138547c6ae99SBarry Smith #undef __FUNCT__
1386e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
138747c6ae99SBarry Smith /*@
1388e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
138947c6ae99SBarry Smith    grids associated with two DMs.
139047c6ae99SBarry Smith 
139147c6ae99SBarry Smith    Collective on DM
139247c6ae99SBarry Smith 
139347c6ae99SBarry Smith    Input Parameters:
139447c6ae99SBarry Smith +  dmc - the coarse grid DM
139547c6ae99SBarry Smith -  dmf - the fine grid DM
139647c6ae99SBarry Smith 
139747c6ae99SBarry Smith    Output Parameters:
139847c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
139947c6ae99SBarry Smith 
140047c6ae99SBarry Smith    Level: intermediate
140147c6ae99SBarry Smith 
140247c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
140347c6ae99SBarry Smith 
1404e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
140547c6ae99SBarry Smith @*/
1406e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
140747c6ae99SBarry Smith {
140847c6ae99SBarry Smith   PetscErrorCode ierr;
140947c6ae99SBarry Smith 
141047c6ae99SBarry Smith   PetscFunctionBegin;
1411171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1412171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
141347c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
141447c6ae99SBarry Smith   PetscFunctionReturn(0);
141547c6ae99SBarry Smith }
141647c6ae99SBarry Smith 
141747c6ae99SBarry Smith #undef __FUNCT__
14181a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
14191a266240SBarry Smith /*@C
14201a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
14211a266240SBarry Smith 
14221a266240SBarry Smith     Not Collective
14231a266240SBarry Smith 
14241a266240SBarry Smith     Input Parameters:
14251a266240SBarry Smith +   dm - the DM object
14261a266240SBarry Smith -   destroy - the destroy function
14271a266240SBarry Smith 
14281a266240SBarry Smith     Level: intermediate
14291a266240SBarry Smith 
1430e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
14311a266240SBarry Smith 
1432f07f9ceaSJed Brown @*/
14331a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
14341a266240SBarry Smith {
14351a266240SBarry Smith   PetscFunctionBegin;
1436171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14371a266240SBarry Smith   dm->ctxdestroy = destroy;
14381a266240SBarry Smith   PetscFunctionReturn(0);
14391a266240SBarry Smith }
14401a266240SBarry Smith 
14411a266240SBarry Smith #undef __FUNCT__
14421b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1443b07ff414SBarry Smith /*@
14441b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
144547c6ae99SBarry Smith 
144647c6ae99SBarry Smith     Not Collective
144747c6ae99SBarry Smith 
144847c6ae99SBarry Smith     Input Parameters:
144947c6ae99SBarry Smith +   dm - the DM object
145047c6ae99SBarry Smith -   ctx - the user context
145147c6ae99SBarry Smith 
145247c6ae99SBarry Smith     Level: intermediate
145347c6ae99SBarry Smith 
1454e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
145547c6ae99SBarry Smith 
145647c6ae99SBarry Smith @*/
14571b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
145847c6ae99SBarry Smith {
145947c6ae99SBarry Smith   PetscFunctionBegin;
1460171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
146147c6ae99SBarry Smith   dm->ctx = ctx;
146247c6ae99SBarry Smith   PetscFunctionReturn(0);
146347c6ae99SBarry Smith }
146447c6ae99SBarry Smith 
146547c6ae99SBarry Smith #undef __FUNCT__
14661b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
146747c6ae99SBarry Smith /*@
14681b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
146947c6ae99SBarry Smith 
147047c6ae99SBarry Smith     Not Collective
147147c6ae99SBarry Smith 
147247c6ae99SBarry Smith     Input Parameter:
147347c6ae99SBarry Smith .   dm - the DM object
147447c6ae99SBarry Smith 
147547c6ae99SBarry Smith     Output Parameter:
147647c6ae99SBarry Smith .   ctx - the user context
147747c6ae99SBarry Smith 
147847c6ae99SBarry Smith     Level: intermediate
147947c6ae99SBarry Smith 
1480e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
148147c6ae99SBarry Smith 
148247c6ae99SBarry Smith @*/
14831b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
148447c6ae99SBarry Smith {
148547c6ae99SBarry Smith   PetscFunctionBegin;
1486171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14871b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
148847c6ae99SBarry Smith   PetscFunctionReturn(0);
148947c6ae99SBarry Smith }
149047c6ae99SBarry Smith 
149147c6ae99SBarry Smith #undef __FUNCT__
149247c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
14937e833e3aSBarry Smith /*@C
149447c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
149547c6ae99SBarry Smith 
149647c6ae99SBarry Smith     Logically Collective on DM
149747c6ae99SBarry Smith 
149847c6ae99SBarry Smith     Input Parameter:
149947c6ae99SBarry Smith +   dm - the DM object to destroy
150047c6ae99SBarry Smith -   f - the function to compute the initial guess
150147c6ae99SBarry Smith 
150247c6ae99SBarry Smith     Level: intermediate
150347c6ae99SBarry Smith 
1504e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
150547c6ae99SBarry Smith 
1506f07f9ceaSJed Brown @*/
15077087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
150847c6ae99SBarry Smith {
150947c6ae99SBarry Smith   PetscFunctionBegin;
1510171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
151147c6ae99SBarry Smith   dm->ops->initialguess = f;
151247c6ae99SBarry Smith   PetscFunctionReturn(0);
151347c6ae99SBarry Smith }
151447c6ae99SBarry Smith 
151547c6ae99SBarry Smith #undef __FUNCT__
151647c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
15177e833e3aSBarry Smith /*@C
151847c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
151947c6ae99SBarry Smith 
152047c6ae99SBarry Smith     Logically Collective on DM
152147c6ae99SBarry Smith 
152247c6ae99SBarry Smith     Input Parameter:
152347c6ae99SBarry Smith +   dm - the DM object
152447c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
152547c6ae99SBarry Smith 
152647c6ae99SBarry Smith     Level: intermediate
152747c6ae99SBarry Smith 
152847c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
152947c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
153047c6ae99SBarry Smith 
1531e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
153247c6ae99SBarry Smith          DMSetJacobian()
153347c6ae99SBarry Smith 
1534f07f9ceaSJed Brown @*/
15357087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
153647c6ae99SBarry Smith {
153747c6ae99SBarry Smith   PetscFunctionBegin;
1538171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
153947c6ae99SBarry Smith   dm->ops->function = f;
154047c6ae99SBarry Smith   if (f) {
154147c6ae99SBarry Smith     dm->ops->functionj = f;
154247c6ae99SBarry Smith   }
154347c6ae99SBarry Smith   PetscFunctionReturn(0);
154447c6ae99SBarry Smith }
154547c6ae99SBarry Smith 
154647c6ae99SBarry Smith #undef __FUNCT__
154747c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
15487e833e3aSBarry Smith /*@C
154947c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
155047c6ae99SBarry Smith 
155147c6ae99SBarry Smith     Logically Collective on DM
155247c6ae99SBarry Smith 
155347c6ae99SBarry Smith     Input Parameter:
155447c6ae99SBarry Smith +   dm - the DM object to destroy
155547c6ae99SBarry Smith -   f - the function to compute the matrix entries
155647c6ae99SBarry Smith 
155747c6ae99SBarry Smith     Level: intermediate
155847c6ae99SBarry Smith 
1559e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
156047c6ae99SBarry Smith          DMSetFunction()
156147c6ae99SBarry Smith 
1562f07f9ceaSJed Brown @*/
15637087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
156447c6ae99SBarry Smith {
156547c6ae99SBarry Smith   PetscFunctionBegin;
1566171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
156747c6ae99SBarry Smith   dm->ops->jacobian = f;
156847c6ae99SBarry Smith   PetscFunctionReturn(0);
156947c6ae99SBarry Smith }
157047c6ae99SBarry Smith 
157147c6ae99SBarry Smith #undef __FUNCT__
157208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
157308da532bSDmitry Karpeev /*@C
157408da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
157508da532bSDmitry Karpeev 
157608da532bSDmitry Karpeev     Logically Collective on DM
157708da532bSDmitry Karpeev 
157808da532bSDmitry Karpeev     Input Parameter:
157908da532bSDmitry Karpeev +   dm - the DM object
158008da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
158108da532bSDmitry Karpeev 
158208da532bSDmitry Karpeev     Level: intermediate
158308da532bSDmitry Karpeev 
1584e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
158508da532bSDmitry Karpeev          DMSetJacobian()
158608da532bSDmitry Karpeev 
158708da532bSDmitry Karpeev @*/
158808da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
158908da532bSDmitry Karpeev {
159008da532bSDmitry Karpeev   PetscFunctionBegin;
159108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
159208da532bSDmitry Karpeev   PetscFunctionReturn(0);
159308da532bSDmitry Karpeev }
159408da532bSDmitry Karpeev 
159508da532bSDmitry Karpeev #undef __FUNCT__
159608da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
159708da532bSDmitry Karpeev /*@
159808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
159908da532bSDmitry Karpeev 
160008da532bSDmitry Karpeev     Not Collective
160108da532bSDmitry Karpeev 
160208da532bSDmitry Karpeev     Input Parameter:
160308da532bSDmitry Karpeev .   dm - the DM object to destroy
160408da532bSDmitry Karpeev 
160508da532bSDmitry Karpeev     Output Parameter:
160608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
160708da532bSDmitry Karpeev 
160808da532bSDmitry Karpeev     Level: developer
160908da532bSDmitry Karpeev 
1610e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
161108da532bSDmitry Karpeev 
161208da532bSDmitry Karpeev @*/
161308da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
161408da532bSDmitry Karpeev {
161508da532bSDmitry Karpeev   PetscFunctionBegin;
161608da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
161708da532bSDmitry Karpeev   PetscFunctionReturn(0);
161808da532bSDmitry Karpeev }
161908da532bSDmitry Karpeev 
162008da532bSDmitry Karpeev #undef __FUNCT__
162108da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
162208da532bSDmitry Karpeev /*@C
162308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
162408da532bSDmitry Karpeev 
162508da532bSDmitry Karpeev     Logically Collective on DM
162608da532bSDmitry Karpeev 
162708da532bSDmitry Karpeev     Input Parameters:
162808da532bSDmitry Karpeev +   dm - the DM object to destroy
162908da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
163008da532bSDmitry Karpeev 
163108da532bSDmitry Karpeev     Output parameters:
163208da532bSDmitry Karpeev +   xl - lower bound
163308da532bSDmitry Karpeev -   xu - upper bound
163408da532bSDmitry Karpeev 
163508da532bSDmitry Karpeev     Level: intermediate
163608da532bSDmitry Karpeev 
1637e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
163808da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
163908da532bSDmitry Karpeev 
164008da532bSDmitry Karpeev @*/
164108da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
164208da532bSDmitry Karpeev {
164308da532bSDmitry Karpeev   PetscErrorCode ierr;
164408da532bSDmitry Karpeev   PetscFunctionBegin;
164508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
164608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
164708da532bSDmitry Karpeev   if(dm->ops->computevariablebounds) {
164808da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
164908da532bSDmitry Karpeev   }
165008da532bSDmitry Karpeev   else {
165108da532bSDmitry Karpeev     ierr = VecSet(xl,SNES_VI_NINF); CHKERRQ(ierr);
165208da532bSDmitry Karpeev     ierr = VecSet(xu,SNES_VI_INF);  CHKERRQ(ierr);
165308da532bSDmitry Karpeev   }
165408da532bSDmitry Karpeev   PetscFunctionReturn(0);
165508da532bSDmitry Karpeev }
165608da532bSDmitry Karpeev 
165708da532bSDmitry Karpeev #undef __FUNCT__
165847c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
165947c6ae99SBarry Smith /*@
166047c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
166147c6ae99SBarry Smith 
166247c6ae99SBarry Smith     Collective on DM
166347c6ae99SBarry Smith 
166447c6ae99SBarry Smith     Input Parameter:
166547c6ae99SBarry Smith +   dm - the DM object to destroy
166647c6ae99SBarry Smith -   x - the vector to hold the initial guess values
166747c6ae99SBarry Smith 
166847c6ae99SBarry Smith     Level: developer
166947c6ae99SBarry Smith 
1670e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
167147c6ae99SBarry Smith 
167247c6ae99SBarry Smith @*/
16737087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
167447c6ae99SBarry Smith {
167547c6ae99SBarry Smith   PetscErrorCode ierr;
167647c6ae99SBarry Smith   PetscFunctionBegin;
1677171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
167847c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
167947c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
168047c6ae99SBarry Smith   PetscFunctionReturn(0);
168147c6ae99SBarry Smith }
168247c6ae99SBarry Smith 
168347c6ae99SBarry Smith #undef __FUNCT__
168447c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
168547c6ae99SBarry Smith /*@
168647c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
168747c6ae99SBarry Smith 
168847c6ae99SBarry Smith     Not Collective
168947c6ae99SBarry Smith 
169047c6ae99SBarry Smith     Input Parameter:
169147c6ae99SBarry Smith .   dm - the DM object to destroy
169247c6ae99SBarry Smith 
169347c6ae99SBarry Smith     Output Parameter:
169447c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
169547c6ae99SBarry Smith 
169647c6ae99SBarry Smith     Level: developer
169747c6ae99SBarry Smith 
1698e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
169947c6ae99SBarry Smith 
170047c6ae99SBarry Smith @*/
17017087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
170247c6ae99SBarry Smith {
170347c6ae99SBarry Smith   PetscFunctionBegin;
1704171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
170547c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
170647c6ae99SBarry Smith   PetscFunctionReturn(0);
170747c6ae99SBarry Smith }
170847c6ae99SBarry Smith 
170947c6ae99SBarry Smith #undef __FUNCT__
171047c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
171147c6ae99SBarry Smith /*@
171247c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
171347c6ae99SBarry Smith 
171447c6ae99SBarry Smith     Not Collective
171547c6ae99SBarry Smith 
171647c6ae99SBarry Smith     Input Parameter:
171747c6ae99SBarry Smith .   dm - the DM object to destroy
171847c6ae99SBarry Smith 
171947c6ae99SBarry Smith     Output Parameter:
172047c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
172147c6ae99SBarry Smith 
172247c6ae99SBarry Smith     Level: developer
172347c6ae99SBarry Smith 
1724e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
172547c6ae99SBarry Smith 
172647c6ae99SBarry Smith @*/
17277087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
172847c6ae99SBarry Smith {
172947c6ae99SBarry Smith   PetscFunctionBegin;
1730171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
173147c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
173247c6ae99SBarry Smith   PetscFunctionReturn(0);
173347c6ae99SBarry Smith }
173447c6ae99SBarry Smith 
173547c6ae99SBarry Smith #undef __FUNCT__
173647c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
173747c6ae99SBarry Smith /*@
173847c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
173947c6ae99SBarry Smith 
174047c6ae99SBarry Smith     Not Collective
174147c6ae99SBarry Smith 
174247c6ae99SBarry Smith     Input Parameter:
174347c6ae99SBarry Smith .   dm - the DM object to destroy
174447c6ae99SBarry Smith 
174547c6ae99SBarry Smith     Output Parameter:
174647c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
174747c6ae99SBarry Smith 
174847c6ae99SBarry Smith     Level: developer
174947c6ae99SBarry Smith 
1750e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
175147c6ae99SBarry Smith 
175247c6ae99SBarry Smith @*/
17537087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
175447c6ae99SBarry Smith {
175547c6ae99SBarry Smith   PetscFunctionBegin;
1756171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
175747c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
175847c6ae99SBarry Smith   PetscFunctionReturn(0);
175947c6ae99SBarry Smith }
176047c6ae99SBarry Smith 
176147c6ae99SBarry Smith #undef  __FUNCT__
176208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
1763748fac09SDmitry Karpeev /*@C
176408da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
176508da532bSDmitry Karpeev 
176608da532bSDmitry Karpeev     Collective on DM
176708da532bSDmitry Karpeev 
176808da532bSDmitry Karpeev     Input Parameter:
176908da532bSDmitry Karpeev +   dm - the DM object
1770e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
177108da532bSDmitry Karpeev 
177208da532bSDmitry Karpeev     Level: developer
177308da532bSDmitry Karpeev 
1774e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
177508da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
177608da532bSDmitry Karpeev 
177708da532bSDmitry Karpeev @*/
177808da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
177908da532bSDmitry Karpeev {
178008da532bSDmitry Karpeev   PetscErrorCode ierr;
178108da532bSDmitry Karpeev   PetscFunctionBegin;
178208da532bSDmitry Karpeev   if (x) {
178308da532bSDmitry Karpeev     if (!dm->x) {
178408da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
178508da532bSDmitry Karpeev     }
178608da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
178708da532bSDmitry Karpeev   }
178808da532bSDmitry Karpeev   else if(dm->x) {
178908da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
179008da532bSDmitry Karpeev   }
179108da532bSDmitry Karpeev   PetscFunctionReturn(0);
179208da532bSDmitry Karpeev }
179308da532bSDmitry Karpeev 
179408da532bSDmitry Karpeev 
179508da532bSDmitry Karpeev #undef __FUNCT__
179647c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
179747c6ae99SBarry Smith /*@
179847c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
179947c6ae99SBarry Smith 
180047c6ae99SBarry Smith     Collective on DM
180147c6ae99SBarry Smith 
180247c6ae99SBarry Smith     Input Parameter:
180347c6ae99SBarry Smith +   dm - the DM object to destroy
180447c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
180547c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
180647c6ae99SBarry Smith 
180747c6ae99SBarry Smith     Level: developer
180847c6ae99SBarry Smith 
1809e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
181047c6ae99SBarry Smith          DMSetJacobian()
181147c6ae99SBarry Smith 
181247c6ae99SBarry Smith @*/
18137087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
181447c6ae99SBarry Smith {
181547c6ae99SBarry Smith   PetscErrorCode ierr;
181647c6ae99SBarry Smith   PetscFunctionBegin;
1817171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
181847c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
1819644e2e5bSBarry Smith   PetscStackPush("DM user function");
182047c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
1821644e2e5bSBarry Smith   PetscStackPop;
182247c6ae99SBarry Smith   PetscFunctionReturn(0);
182347c6ae99SBarry Smith }
182447c6ae99SBarry Smith 
182547c6ae99SBarry Smith 
182608da532bSDmitry Karpeev 
182747c6ae99SBarry Smith #undef __FUNCT__
182847c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
182947c6ae99SBarry Smith /*@
183047c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
183147c6ae99SBarry Smith 
183247c6ae99SBarry Smith     Collective on DM
183347c6ae99SBarry Smith 
183447c6ae99SBarry Smith     Input Parameter:
183547c6ae99SBarry Smith +   dm - the DM object
1836cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
183747c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
183847c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
183947c6ae99SBarry Smith 
184047c6ae99SBarry Smith     Level: developer
184147c6ae99SBarry Smith 
1842e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
184347c6ae99SBarry Smith          DMSetFunction()
184447c6ae99SBarry Smith 
184547c6ae99SBarry Smith @*/
18467087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
184747c6ae99SBarry Smith {
184847c6ae99SBarry Smith   PetscErrorCode ierr;
184947c6ae99SBarry Smith 
185047c6ae99SBarry Smith   PetscFunctionBegin;
1851171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
185247c6ae99SBarry Smith   if (!dm->ops->jacobian) {
185347c6ae99SBarry Smith     ISColoring     coloring;
185447c6ae99SBarry Smith     MatFDColoring  fd;
18552c9966d7SBarry Smith     const MatType  mtype;
185647c6ae99SBarry Smith 
18572c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
18582c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
185947c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
1860fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
186147c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
18620bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
18630bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
186471cd77b2SBarry Smith 
186547c6ae99SBarry Smith     dm->fd = fd;
186647c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
18672533e041SBarry Smith 
186871cd77b2SBarry Smith     /* don't know why this is needed */
186971cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
187047c6ae99SBarry Smith   }
187147c6ae99SBarry Smith   if (!x) x = dm->x;
187247c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
1873cab2e9ccSBarry Smith 
187471cd77b2SBarry 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 */
1875649052a6SBarry Smith   if (x) {
1876cab2e9ccSBarry Smith     if (!dm->x) {
187771cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
1878cab2e9ccSBarry Smith     }
1879cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
1880649052a6SBarry Smith   }
1881a8248277SBarry Smith   if (A != B) {
1882a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1883a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1884a8248277SBarry Smith   }
188547c6ae99SBarry Smith   PetscFunctionReturn(0);
188647c6ae99SBarry Smith }
1887264ace61SBarry Smith 
1888cab2e9ccSBarry Smith 
1889264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
1890264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
1891264ace61SBarry Smith 
1892264ace61SBarry Smith #undef __FUNCT__
1893264ace61SBarry Smith #define __FUNCT__ "DMSetType"
1894264ace61SBarry Smith /*@C
1895264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
1896264ace61SBarry Smith 
1897264ace61SBarry Smith   Collective on DM
1898264ace61SBarry Smith 
1899264ace61SBarry Smith   Input Parameters:
1900264ace61SBarry Smith + dm     - The DM object
1901264ace61SBarry Smith - method - The name of the DM type
1902264ace61SBarry Smith 
1903264ace61SBarry Smith   Options Database Key:
1904264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
1905264ace61SBarry Smith 
1906264ace61SBarry Smith   Notes:
1907e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
1908264ace61SBarry Smith 
1909264ace61SBarry Smith   Level: intermediate
1910264ace61SBarry Smith 
1911264ace61SBarry Smith .keywords: DM, set, type
1912264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
1913264ace61SBarry Smith @*/
19147087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
1915264ace61SBarry Smith {
1916264ace61SBarry Smith   PetscErrorCode (*r)(DM);
1917264ace61SBarry Smith   PetscBool      match;
1918264ace61SBarry Smith   PetscErrorCode ierr;
1919264ace61SBarry Smith 
1920264ace61SBarry Smith   PetscFunctionBegin;
1921264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1922251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
1923264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
1924264ace61SBarry Smith 
1925264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
19264b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
1927264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
1928264ace61SBarry Smith 
1929264ace61SBarry Smith   if (dm->ops->destroy) {
1930264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
1931b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
1932264ace61SBarry Smith   }
1933264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
1934264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
1935264ace61SBarry Smith   PetscFunctionReturn(0);
1936264ace61SBarry Smith }
1937264ace61SBarry Smith 
1938264ace61SBarry Smith #undef __FUNCT__
1939264ace61SBarry Smith #define __FUNCT__ "DMGetType"
1940264ace61SBarry Smith /*@C
1941264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
1942264ace61SBarry Smith 
1943264ace61SBarry Smith   Not Collective
1944264ace61SBarry Smith 
1945264ace61SBarry Smith   Input Parameter:
1946264ace61SBarry Smith . dm  - The DM
1947264ace61SBarry Smith 
1948264ace61SBarry Smith   Output Parameter:
1949264ace61SBarry Smith . type - The DM type name
1950264ace61SBarry Smith 
1951264ace61SBarry Smith   Level: intermediate
1952264ace61SBarry Smith 
1953264ace61SBarry Smith .keywords: DM, get, type, name
1954264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
1955264ace61SBarry Smith @*/
19567087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
1957264ace61SBarry Smith {
1958264ace61SBarry Smith   PetscErrorCode ierr;
1959264ace61SBarry Smith 
1960264ace61SBarry Smith   PetscFunctionBegin;
1961264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1962264ace61SBarry Smith   PetscValidCharPointer(type,2);
1963264ace61SBarry Smith   if (!DMRegisterAllCalled) {
1964264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
1965264ace61SBarry Smith   }
1966264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
1967264ace61SBarry Smith   PetscFunctionReturn(0);
1968264ace61SBarry Smith }
1969264ace61SBarry Smith 
197067a56275SMatthew G Knepley #undef __FUNCT__
197167a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
197267a56275SMatthew G Knepley /*@C
197367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
197467a56275SMatthew G Knepley 
197567a56275SMatthew G Knepley   Collective on DM
197667a56275SMatthew G Knepley 
197767a56275SMatthew G Knepley   Input Parameters:
197867a56275SMatthew G Knepley + dm - the DM
197967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
198067a56275SMatthew G Knepley 
198167a56275SMatthew G Knepley   Output Parameter:
198267a56275SMatthew G Knepley . M - pointer to new DM
198367a56275SMatthew G Knepley 
198467a56275SMatthew G Knepley   Notes:
198567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
198667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
198767a56275SMatthew G Knepley   of the input DM.
198867a56275SMatthew G Knepley 
198967a56275SMatthew G Knepley   Level: intermediate
199067a56275SMatthew G Knepley 
199167a56275SMatthew G Knepley .seealso: DMCreate()
199267a56275SMatthew G Knepley @*/
199367a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
199467a56275SMatthew G Knepley {
199567a56275SMatthew G Knepley   DM             B;
199667a56275SMatthew G Knepley   char           convname[256];
199767a56275SMatthew G Knepley   PetscBool      sametype, issame;
199867a56275SMatthew G Knepley   PetscErrorCode ierr;
199967a56275SMatthew G Knepley 
200067a56275SMatthew G Knepley   PetscFunctionBegin;
200167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
200267a56275SMatthew G Knepley   PetscValidType(dm,1);
200367a56275SMatthew G Knepley   PetscValidPointer(M,3);
2004251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
200567a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
200667a56275SMatthew G Knepley   {
200767a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
200867a56275SMatthew G Knepley 
200967a56275SMatthew G Knepley     /*
201067a56275SMatthew G Knepley        Order of precedence:
201167a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
201267a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
201367a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
201467a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
201567a56275SMatthew G Knepley        5) Use a really basic converter.
201667a56275SMatthew G Knepley     */
201767a56275SMatthew G Knepley 
201867a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
201967a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
202067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
202167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
202267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
202367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
202467a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
202567a56275SMatthew G Knepley     if (conv) goto foundconv;
202667a56275SMatthew G Knepley 
202767a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
202867a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
202967a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
203067a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
203167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
203267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
203367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
203467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
203567a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
203667a56275SMatthew G Knepley     if (conv) {
2037fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
203867a56275SMatthew G Knepley       goto foundconv;
203967a56275SMatthew G Knepley     }
204067a56275SMatthew G Knepley 
204167a56275SMatthew G Knepley #if 0
204267a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
204367a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2044fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
204567a56275SMatthew G Knepley     if (conv) goto foundconv;
204667a56275SMatthew G Knepley 
204767a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
204867a56275SMatthew G Knepley     if (dm->ops->convert) {
204967a56275SMatthew G Knepley       conv = dm->ops->convert;
205067a56275SMatthew G Knepley     }
205167a56275SMatthew G Knepley     if (conv) goto foundconv;
205267a56275SMatthew G Knepley #endif
205367a56275SMatthew G Knepley 
205467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
205567a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
205667a56275SMatthew G Knepley 
205767a56275SMatthew G Knepley     foundconv:
205867a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
205967a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
206067a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
206167a56275SMatthew G Knepley   }
206267a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
206367a56275SMatthew G Knepley   PetscFunctionReturn(0);
206467a56275SMatthew G Knepley }
2065264ace61SBarry Smith 
2066264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2067264ace61SBarry Smith 
2068264ace61SBarry Smith #undef __FUNCT__
2069264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2070264ace61SBarry Smith /*@C
2071264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2072264ace61SBarry Smith 
2073264ace61SBarry Smith   Level: advanced
2074264ace61SBarry Smith @*/
20757087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2076264ace61SBarry Smith {
2077264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2078264ace61SBarry Smith   PetscErrorCode ierr;
2079264ace61SBarry Smith 
2080264ace61SBarry Smith   PetscFunctionBegin;
2081264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2082264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2083264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2084264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2085264ace61SBarry Smith   PetscFunctionReturn(0);
2086264ace61SBarry Smith }
2087264ace61SBarry Smith 
2088264ace61SBarry Smith 
2089264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2090264ace61SBarry Smith #undef __FUNCT__
2091264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2092264ace61SBarry Smith /*@C
2093264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2094264ace61SBarry Smith 
2095264ace61SBarry Smith    Not Collective
2096264ace61SBarry Smith 
2097264ace61SBarry Smith    Level: advanced
2098264ace61SBarry Smith 
2099264ace61SBarry Smith .keywords: DM, register, destroy
2100264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2101264ace61SBarry Smith @*/
21027087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2103264ace61SBarry Smith {
2104264ace61SBarry Smith   PetscErrorCode ierr;
2105264ace61SBarry Smith 
2106264ace61SBarry Smith   PetscFunctionBegin;
2107264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2108264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2109264ace61SBarry Smith   PetscFunctionReturn(0);
2110264ace61SBarry Smith }
211123f975d1SBarry Smith 
211223f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2113c6db04a5SJed Brown #include <mex.h>
211423f975d1SBarry Smith 
21153014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
211623f975d1SBarry Smith 
211723f975d1SBarry Smith #undef __FUNCT__
211823f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
211923f975d1SBarry Smith /*
212023f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
212123f975d1SBarry Smith                          DMSetFunctionMatlab().
212223f975d1SBarry Smith 
212323f975d1SBarry Smith    For linear problems x is null
212423f975d1SBarry Smith 
212523f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
212623f975d1SBarry Smith */
21277087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
212823f975d1SBarry Smith {
212923f975d1SBarry Smith   PetscErrorCode    ierr;
213023f975d1SBarry Smith   DMMatlabContext   *sctx;
213123f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
213223f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
213323f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
213423f975d1SBarry Smith 
213523f975d1SBarry Smith   PetscFunctionBegin;
213623f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
213723f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
213823f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
213923f975d1SBarry Smith 
214023f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
21411b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
214223f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
214323f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
21443014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
214523f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
214623f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
214723f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
214823f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2149b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
215023f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
215123f975d1SBarry Smith   mxDestroyArray(prhs[0]);
215223f975d1SBarry Smith   mxDestroyArray(prhs[1]);
215323f975d1SBarry Smith   mxDestroyArray(prhs[2]);
215423f975d1SBarry Smith   mxDestroyArray(prhs[3]);
215523f975d1SBarry Smith   mxDestroyArray(plhs[0]);
215623f975d1SBarry Smith   PetscFunctionReturn(0);
215723f975d1SBarry Smith }
215823f975d1SBarry Smith 
215923f975d1SBarry Smith 
216023f975d1SBarry Smith #undef __FUNCT__
216123f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
216223f975d1SBarry Smith /*
216323f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
216423f975d1SBarry Smith 
216523f975d1SBarry Smith */
21667087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
216723f975d1SBarry Smith {
216823f975d1SBarry Smith   PetscErrorCode    ierr;
216923f975d1SBarry Smith   DMMatlabContext   *sctx;
217023f975d1SBarry Smith 
217123f975d1SBarry Smith   PetscFunctionBegin;
2172171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
217323f975d1SBarry Smith   /* currently sctx is memory bleed */
21741b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
21753014e516SBarry Smith   if (!sctx) {
217623f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
21773014e516SBarry Smith   }
217823f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
21791b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
218023f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
218123f975d1SBarry Smith   PetscFunctionReturn(0);
218223f975d1SBarry Smith }
21833014e516SBarry Smith 
21843014e516SBarry Smith #undef __FUNCT__
21853014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
21863014e516SBarry Smith /*
21873014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
21883014e516SBarry Smith                          DMSetJacobianMatlab().
21893014e516SBarry Smith 
21903014e516SBarry Smith    For linear problems x is null
21913014e516SBarry Smith 
21923014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
21933014e516SBarry Smith */
21947087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
21953014e516SBarry Smith {
21963014e516SBarry Smith   PetscErrorCode    ierr;
21973014e516SBarry Smith   DMMatlabContext   *sctx;
21983014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
21993014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
22003014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
22013014e516SBarry Smith 
22023014e516SBarry Smith   PetscFunctionBegin;
22033014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22043014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
22053014e516SBarry Smith 
2206e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
22071b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
22083014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
22093014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
22103014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
22113014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
22123014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
22133014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
22143014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
22153014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
22163014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2217b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2218c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2219c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
22203014e516SBarry Smith   mxDestroyArray(prhs[0]);
22213014e516SBarry Smith   mxDestroyArray(prhs[1]);
22223014e516SBarry Smith   mxDestroyArray(prhs[2]);
22233014e516SBarry Smith   mxDestroyArray(prhs[3]);
22243014e516SBarry Smith   mxDestroyArray(prhs[4]);
22253014e516SBarry Smith   mxDestroyArray(plhs[0]);
22263014e516SBarry Smith   mxDestroyArray(plhs[1]);
22273014e516SBarry Smith   PetscFunctionReturn(0);
22283014e516SBarry Smith }
22293014e516SBarry Smith 
22303014e516SBarry Smith 
22313014e516SBarry Smith #undef __FUNCT__
22323014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
22333014e516SBarry Smith /*
22343014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
22353014e516SBarry Smith 
22363014e516SBarry Smith */
22377087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
22383014e516SBarry Smith {
22393014e516SBarry Smith   PetscErrorCode    ierr;
22403014e516SBarry Smith   DMMatlabContext   *sctx;
22413014e516SBarry Smith 
22423014e516SBarry Smith   PetscFunctionBegin;
2243171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22443014e516SBarry Smith   /* currently sctx is memory bleed */
22451b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
22463014e516SBarry Smith   if (!sctx) {
22473014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
22483014e516SBarry Smith   }
22493014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
22501b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
22513014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
22523014e516SBarry Smith   PetscFunctionReturn(0);
22533014e516SBarry Smith }
225423f975d1SBarry Smith #endif
2255b859378eSBarry Smith 
2256b859378eSBarry Smith #undef __FUNCT__
2257b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2258b859378eSBarry Smith /*@C
2259b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2260b859378eSBarry Smith   with DMView().
2261b859378eSBarry Smith 
2262b859378eSBarry Smith   Collective on PetscViewer
2263b859378eSBarry Smith 
2264b859378eSBarry Smith   Input Parameters:
2265b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2266b859378eSBarry Smith            some related function before a call to DMLoad().
2267b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2268b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2269b859378eSBarry Smith 
2270b859378eSBarry Smith    Level: intermediate
2271b859378eSBarry Smith 
2272b859378eSBarry Smith   Notes:
2273b859378eSBarry Smith   Defaults to the DM DA.
2274b859378eSBarry Smith 
2275b859378eSBarry Smith   Notes for advanced users:
2276b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2277b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2278b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2279b859378eSBarry Smith   format is
2280b859378eSBarry Smith .vb
2281b859378eSBarry Smith      has not yet been determined
2282b859378eSBarry Smith .ve
2283b859378eSBarry Smith 
2284b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2285b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2286b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2287b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2288b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2289b859378eSBarry Smith 
2290b859378eSBarry Smith   Concepts: vector^loading from file
2291b859378eSBarry Smith 
2292b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2293b859378eSBarry Smith @*/
2294b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2295b859378eSBarry Smith {
2296b859378eSBarry Smith   PetscErrorCode ierr;
2297b859378eSBarry Smith 
2298b859378eSBarry Smith   PetscFunctionBegin;
2299b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2300b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2301b859378eSBarry Smith 
2302b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2303b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2304b859378eSBarry Smith   }
2305b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2306b859378eSBarry Smith   PetscFunctionReturn(0);
2307b859378eSBarry Smith }
2308b859378eSBarry Smith 
23097da65231SMatthew G Knepley /******************************** FEM Support **********************************/
23107da65231SMatthew G Knepley 
23117da65231SMatthew G Knepley #undef __FUNCT__
23127da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
23137da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
23141d47ebbbSSatish Balay   PetscInt       f;
23151b30c384SMatthew G Knepley   PetscErrorCode ierr;
23161b30c384SMatthew G Knepley 
23177da65231SMatthew G Knepley   PetscFunctionBegin;
231874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
23191d47ebbbSSatish Balay   for(f = 0; f < len; ++f) {
232074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
23217da65231SMatthew G Knepley   }
23227da65231SMatthew G Knepley   PetscFunctionReturn(0);
23237da65231SMatthew G Knepley }
23247da65231SMatthew G Knepley 
23257da65231SMatthew G Knepley #undef __FUNCT__
23267da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
23277da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
23281b30c384SMatthew G Knepley   PetscInt       f, g;
23297da65231SMatthew G Knepley   PetscErrorCode ierr;
23307da65231SMatthew G Knepley 
23317da65231SMatthew G Knepley   PetscFunctionBegin;
233274778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
23331d47ebbbSSatish Balay   for(f = 0; f < rows; ++f) {
233474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
23351d47ebbbSSatish Balay     for(g = 0; g < cols; ++g) {
233674778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
23377da65231SMatthew G Knepley     }
233874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
23397da65231SMatthew G Knepley   }
23407da65231SMatthew G Knepley   PetscFunctionReturn(0);
23417da65231SMatthew G Knepley }
2342e7c4fc90SDmitry Karpeev 
2343970e74d5SMatthew G Knepley #undef __FUNCT__
2344970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
2345970e74d5SMatthew G Knepley /*@C
2346970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
2347970e74d5SMatthew G Knepley 
2348970e74d5SMatthew G Knepley   Not collective
2349970e74d5SMatthew G Knepley 
2350970e74d5SMatthew G Knepley   Input Parameter:
2351970e74d5SMatthew G Knepley . dm - The DM
2352970e74d5SMatthew G Knepley 
2353970e74d5SMatthew G Knepley   Output Parameter:
2354970e74d5SMatthew G Knepley . lf - The local residual function
2355970e74d5SMatthew G Knepley 
2356970e74d5SMatthew G Knepley    Calling sequence of lf:
2357970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2358970e74d5SMatthew G Knepley 
2359970e74d5SMatthew G Knepley +  snes - the SNES context
2360970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2361970e74d5SMatthew G Knepley .  f - local vector to put residual in
2362970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2363970e74d5SMatthew G Knepley 
2364970e74d5SMatthew G Knepley   Level: intermediate
2365970e74d5SMatthew G Knepley 
2366970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2367970e74d5SMatthew G Knepley @*/
2368970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
2369970e74d5SMatthew G Knepley {
2370970e74d5SMatthew G Knepley   PetscFunctionBegin;
2371970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2372970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
2373970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2374970e74d5SMatthew G Knepley }
2375970e74d5SMatthew G Knepley 
2376970e74d5SMatthew G Knepley #undef __FUNCT__
2377970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
2378970e74d5SMatthew G Knepley /*@C
2379970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
2380970e74d5SMatthew G Knepley 
2381970e74d5SMatthew G Knepley   Not collective
2382970e74d5SMatthew G Knepley 
2383970e74d5SMatthew G Knepley   Input Parameters:
2384970e74d5SMatthew G Knepley + dm - The DM
2385970e74d5SMatthew G Knepley - lf - The local residual function
2386970e74d5SMatthew G Knepley 
2387970e74d5SMatthew G Knepley    Calling sequence of lf:
2388970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2389970e74d5SMatthew G Knepley 
2390970e74d5SMatthew G Knepley +  snes - the SNES context
2391970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2392970e74d5SMatthew G Knepley .  f - local vector to put residual in
2393970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2394970e74d5SMatthew G Knepley 
2395970e74d5SMatthew G Knepley   Level: intermediate
2396970e74d5SMatthew G Knepley 
2397970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2398970e74d5SMatthew G Knepley @*/
2399970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
2400970e74d5SMatthew G Knepley {
2401970e74d5SMatthew G Knepley   PetscFunctionBegin;
2402970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2403970e74d5SMatthew G Knepley   dm->lf = lf;
2404970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2405970e74d5SMatthew G Knepley }
2406970e74d5SMatthew G Knepley 
2407970e74d5SMatthew G Knepley #undef __FUNCT__
2408970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
2409970e74d5SMatthew G Knepley /*@C
2410970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
2411970e74d5SMatthew G Knepley 
2412970e74d5SMatthew G Knepley   Not collective
2413970e74d5SMatthew G Knepley 
2414970e74d5SMatthew G Knepley   Input Parameter:
2415970e74d5SMatthew G Knepley . dm - The DM
2416970e74d5SMatthew G Knepley 
2417970e74d5SMatthew G Knepley   Output Parameter:
2418970e74d5SMatthew G Knepley . lj - The local Jacobian function
2419970e74d5SMatthew G Knepley 
2420970e74d5SMatthew G Knepley    Calling sequence of lj:
2421970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2422970e74d5SMatthew G Knepley 
2423970e74d5SMatthew G Knepley +  snes - the SNES context
2424970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2425970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2426970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2427970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2428970e74d5SMatthew G Knepley 
2429970e74d5SMatthew G Knepley   Level: intermediate
2430970e74d5SMatthew G Knepley 
2431970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2432970e74d5SMatthew G Knepley @*/
2433970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
2434970e74d5SMatthew G Knepley {
2435970e74d5SMatthew G Knepley   PetscFunctionBegin;
2436970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2437970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
2438970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2439970e74d5SMatthew G Knepley }
2440970e74d5SMatthew G Knepley 
2441970e74d5SMatthew G Knepley #undef __FUNCT__
2442970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
2443970e74d5SMatthew G Knepley /*@C
2444970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
2445970e74d5SMatthew G Knepley 
2446970e74d5SMatthew G Knepley   Not collective
2447970e74d5SMatthew G Knepley 
2448970e74d5SMatthew G Knepley   Input Parameters:
2449970e74d5SMatthew G Knepley + dm - The DM
2450970e74d5SMatthew G Knepley - lj - The local Jacobian function
2451970e74d5SMatthew G Knepley 
2452970e74d5SMatthew G Knepley    Calling sequence of lj:
2453970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2454970e74d5SMatthew G Knepley 
2455970e74d5SMatthew G Knepley +  snes - the SNES context
2456970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2457970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2458970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2459970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2460970e74d5SMatthew G Knepley 
2461970e74d5SMatthew G Knepley   Level: intermediate
2462970e74d5SMatthew G Knepley 
2463970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2464970e74d5SMatthew G Knepley @*/
2465970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
2466970e74d5SMatthew G Knepley {
2467970e74d5SMatthew G Knepley   PetscFunctionBegin;
2468970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2469970e74d5SMatthew G Knepley   dm->lj = lj;
2470970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2471970e74d5SMatthew G Knepley }
2472*88ed4aceSMatthew G Knepley 
2473*88ed4aceSMatthew G Knepley #undef __FUNCT__
2474*88ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
2475*88ed4aceSMatthew G Knepley /*@
2476*88ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
2477*88ed4aceSMatthew G Knepley 
2478*88ed4aceSMatthew G Knepley   Input Parameter:
2479*88ed4aceSMatthew G Knepley . dm - The DM
2480*88ed4aceSMatthew G Knepley 
2481*88ed4aceSMatthew G Knepley   Output Parameter:
2482*88ed4aceSMatthew G Knepley . section - The PetscSection
2483*88ed4aceSMatthew G Knepley 
2484*88ed4aceSMatthew G Knepley   Level: intermediate
2485*88ed4aceSMatthew G Knepley 
2486*88ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
2487*88ed4aceSMatthew G Knepley 
2488*88ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
2489*88ed4aceSMatthew G Knepley @*/
2490*88ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
2491*88ed4aceSMatthew G Knepley   PetscFunctionBegin;
2492*88ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2493*88ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
2494*88ed4aceSMatthew G Knepley   *section = dm->defaultSection;
2495*88ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
2496*88ed4aceSMatthew G Knepley }
2497*88ed4aceSMatthew G Knepley 
2498*88ed4aceSMatthew G Knepley #undef __FUNCT__
2499*88ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
2500*88ed4aceSMatthew G Knepley /*@
2501*88ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
2502*88ed4aceSMatthew G Knepley 
2503*88ed4aceSMatthew G Knepley   Input Parameters:
2504*88ed4aceSMatthew G Knepley + dm - The DM
2505*88ed4aceSMatthew G Knepley - section - The PetscSection
2506*88ed4aceSMatthew G Knepley 
2507*88ed4aceSMatthew G Knepley   Level: intermediate
2508*88ed4aceSMatthew G Knepley 
2509*88ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
2510*88ed4aceSMatthew G Knepley 
2511*88ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
2512*88ed4aceSMatthew G Knepley @*/
2513*88ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
2514*88ed4aceSMatthew G Knepley   PetscErrorCode ierr;
2515*88ed4aceSMatthew G Knepley 
2516*88ed4aceSMatthew G Knepley   PetscFunctionBegin;
2517*88ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2518*88ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
2519*88ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
2520*88ed4aceSMatthew G Knepley   dm->defaultSection = section;
2521*88ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
2522*88ed4aceSMatthew G Knepley }
2523*88ed4aceSMatthew G Knepley 
2524*88ed4aceSMatthew G Knepley #undef __FUNCT__
2525*88ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
2526*88ed4aceSMatthew G Knepley /*@
2527*88ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
2528*88ed4aceSMatthew G Knepley 
2529*88ed4aceSMatthew G Knepley   Input Parameter:
2530*88ed4aceSMatthew G Knepley . dm - The DM
2531*88ed4aceSMatthew G Knepley 
2532*88ed4aceSMatthew G Knepley   Output Parameter:
2533*88ed4aceSMatthew G Knepley . section - The PetscSection
2534*88ed4aceSMatthew G Knepley 
2535*88ed4aceSMatthew G Knepley   Level: intermediate
2536*88ed4aceSMatthew G Knepley 
2537*88ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
2538*88ed4aceSMatthew G Knepley 
2539*88ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
2540*88ed4aceSMatthew G Knepley @*/
2541*88ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
2542*88ed4aceSMatthew G Knepley   PetscErrorCode ierr;
2543*88ed4aceSMatthew G Knepley 
2544*88ed4aceSMatthew G Knepley   PetscFunctionBegin;
2545*88ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2546*88ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
2547*88ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
2548*88ed4aceSMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, &dm->defaultGlobalSection);CHKERRQ(ierr);
2549*88ed4aceSMatthew G Knepley   }
2550*88ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
2551*88ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
2552*88ed4aceSMatthew G Knepley }
2553*88ed4aceSMatthew G Knepley 
2554*88ed4aceSMatthew G Knepley #undef __FUNCT__
2555*88ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
2556*88ed4aceSMatthew G Knepley /*@
2557*88ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
2558*88ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
2559*88ed4aceSMatthew G Knepley 
2560*88ed4aceSMatthew G Knepley   Input Parameter:
2561*88ed4aceSMatthew G Knepley . dm - The DM
2562*88ed4aceSMatthew G Knepley 
2563*88ed4aceSMatthew G Knepley   Output Parameter:
2564*88ed4aceSMatthew G Knepley . sf - The PetscSF
2565*88ed4aceSMatthew G Knepley 
2566*88ed4aceSMatthew G Knepley   Level: intermediate
2567*88ed4aceSMatthew G Knepley 
2568*88ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
2569*88ed4aceSMatthew G Knepley 
2570*88ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
2571*88ed4aceSMatthew G Knepley @*/
2572*88ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
2573*88ed4aceSMatthew G Knepley   PetscInt       nroots;
2574*88ed4aceSMatthew G Knepley   PetscErrorCode ierr;
2575*88ed4aceSMatthew G Knepley 
2576*88ed4aceSMatthew G Knepley   PetscFunctionBegin;
2577*88ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2578*88ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
2579*88ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
2580*88ed4aceSMatthew G Knepley   if (nroots < 0) {
2581*88ed4aceSMatthew G Knepley     PetscSection section, gSection;
2582*88ed4aceSMatthew G Knepley 
2583*88ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
2584*88ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
2585*88ed4aceSMatthew G Knepley     ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
2586*88ed4aceSMatthew G Knepley   }
2587*88ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
2588*88ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
2589*88ed4aceSMatthew G Knepley }
2590*88ed4aceSMatthew G Knepley 
2591*88ed4aceSMatthew G Knepley #undef __FUNCT__
2592*88ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
2593*88ed4aceSMatthew G Knepley /*@
2594*88ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
2595*88ed4aceSMatthew G Knepley 
2596*88ed4aceSMatthew G Knepley   Input Parameters:
2597*88ed4aceSMatthew G Knepley + dm - The DM
2598*88ed4aceSMatthew G Knepley - sf - The PetscSF
2599*88ed4aceSMatthew G Knepley 
2600*88ed4aceSMatthew G Knepley   Level: intermediate
2601*88ed4aceSMatthew G Knepley 
2602*88ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
2603*88ed4aceSMatthew G Knepley 
2604*88ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
2605*88ed4aceSMatthew G Knepley @*/
2606*88ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
2607*88ed4aceSMatthew G Knepley   PetscErrorCode ierr;
2608*88ed4aceSMatthew G Knepley 
2609*88ed4aceSMatthew G Knepley   PetscFunctionBegin;
2610*88ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2611*88ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
2612*88ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
2613*88ed4aceSMatthew G Knepley   dm->defaultSF = sf;
2614*88ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
2615*88ed4aceSMatthew G Knepley }
2616*88ed4aceSMatthew G Knepley 
2617*88ed4aceSMatthew G Knepley #undef __FUNCT__
2618*88ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
2619*88ed4aceSMatthew G Knepley /*@C
2620*88ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
2621*88ed4aceSMatthew G Knepley   describing the data layout.
2622*88ed4aceSMatthew G Knepley 
2623*88ed4aceSMatthew G Knepley   Input Parameters:
2624*88ed4aceSMatthew G Knepley + dm - The DM
2625*88ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
2626*88ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
2627*88ed4aceSMatthew G Knepley 
2628*88ed4aceSMatthew G Knepley   Level: intermediate
2629*88ed4aceSMatthew G Knepley 
2630*88ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
2631*88ed4aceSMatthew G Knepley @*/
2632*88ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
2633*88ed4aceSMatthew G Knepley {
2634*88ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
2635*88ed4aceSMatthew G Knepley   PetscLayout     layout;
2636*88ed4aceSMatthew G Knepley   const PetscInt *ranges;
2637*88ed4aceSMatthew G Knepley   PetscInt       *local;
2638*88ed4aceSMatthew G Knepley   PetscSFNode    *remote;
2639*88ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
2640*88ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
2641*88ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
2642*88ed4aceSMatthew G Knepley 
2643*88ed4aceSMatthew G Knepley   PetscFunctionBegin;
2644*88ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2645*88ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
2646*88ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2647*88ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
2648*88ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
2649*88ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
2650*88ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
2651*88ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
2652*88ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
2653*88ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
2654*88ed4aceSMatthew G Knepley   for(p = pStart, nleaves = 0; p < pEnd; ++p) {
2655*88ed4aceSMatthew G Knepley     PetscInt dof, cdof;
2656*88ed4aceSMatthew G Knepley 
2657*88ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &dof);CHKERRQ(ierr);
2658*88ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &cdof);CHKERRQ(ierr);
2659*88ed4aceSMatthew G Knepley     nleaves += dof < 0 ? -(dof+1)-cdof : dof-cdof;
2660*88ed4aceSMatthew G Knepley   }
2661*88ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
2662*88ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
2663*88ed4aceSMatthew G Knepley   for(p = pStart, l = 0; p < pEnd; ++p) {
2664*88ed4aceSMatthew G Knepley     PetscInt *cind;
2665*88ed4aceSMatthew G Knepley     PetscInt  dof, gdof, cdof, dim, off, goff, d, c;
2666*88ed4aceSMatthew G Knepley 
2667*88ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
2668*88ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
2669*88ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
2670*88ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
2671*88ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
2672*88ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
2673*88ed4aceSMatthew G Knepley     dim  = dof-cdof;
2674*88ed4aceSMatthew G Knepley     for(d = 0, c = 0; d < dof; ++d) {
2675*88ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
2676*88ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
2677*88ed4aceSMatthew G Knepley     }
2678*88ed4aceSMatthew G Knepley     if (gdof < 0) {
2679*88ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
2680*88ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
2681*88ed4aceSMatthew G Knepley 
2682*88ed4aceSMatthew G Knepley         for(r = 0; r < size; ++r) {
2683*88ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
2684*88ed4aceSMatthew G Knepley         }
2685*88ed4aceSMatthew G Knepley         remote[l].rank  = r;
2686*88ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
2687*88ed4aceSMatthew G Knepley       }
2688*88ed4aceSMatthew G Knepley     } else {
2689*88ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
2690*88ed4aceSMatthew G Knepley         remote[l].rank  = rank;
2691*88ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
2692*88ed4aceSMatthew G Knepley       }
2693*88ed4aceSMatthew G Knepley     }
2694*88ed4aceSMatthew G Knepley   }
2695*88ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
2696*88ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
2697*88ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
2698*88ed4aceSMatthew G Knepley }
2699