xref: /petsc/src/dm/interface/dm.c (revision 8d4ac253bb04af478db01159355c1e9bd2e17b98)
108da532bSDmitry Karpeev #include <petscsnes.h>
2b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
347c6ae99SBarry Smith 
4732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
567a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal;
667a56275SMatthew G Knepley 
747c6ae99SBarry Smith #undef __FUNCT__
8a4121054SBarry Smith #define __FUNCT__ "DMCreate"
9a4121054SBarry Smith /*@
10de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
11a4121054SBarry Smith 
12a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
13a4121054SBarry Smith    error when you try to use the vector.
14a4121054SBarry Smith 
15a4121054SBarry Smith   Collective on MPI_Comm
16a4121054SBarry Smith 
17a4121054SBarry Smith   Input Parameter:
18a4121054SBarry Smith . comm - The communicator for the DM object
19a4121054SBarry Smith 
20a4121054SBarry Smith   Output Parameter:
21a4121054SBarry Smith . dm - The DM object
22a4121054SBarry Smith 
23a4121054SBarry Smith   Level: beginner
24a4121054SBarry Smith 
25a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
26a4121054SBarry Smith @*/
277087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
28a4121054SBarry Smith {
29a4121054SBarry Smith   DM             v;
30a4121054SBarry Smith   PetscErrorCode ierr;
31a4121054SBarry Smith 
32a4121054SBarry Smith   PetscFunctionBegin;
331411c6eeSJed Brown   PetscValidPointer(dm,2);
341411c6eeSJed Brown   *dm = PETSC_NULL;
35a4121054SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
36b84caa0eSBarry Smith   ierr = VecInitializePackage(PETSC_NULL);CHKERRQ(ierr);
37b84caa0eSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
38a4121054SBarry Smith   ierr = DMInitializePackage(PETSC_NULL);CHKERRQ(ierr);
39a4121054SBarry Smith #endif
40a4121054SBarry Smith 
413194b578SJed Brown   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
42a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
431411c6eeSJed Brown 
44e7c4fc90SDmitry Karpeev 
45a89ea682SMatthew G Knepley   v->workSize     = 0;
46a89ea682SMatthew G Knepley   v->workArray    = PETSC_NULL;
471411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
481411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
491411c6eeSJed Brown   v->bs           = 1;
50171400e9SBarry Smith   v->coloringtype = IS_COLORING_GLOBAL;
51970e74d5SMatthew G Knepley   v->lf           = PETSC_NULL;
52970e74d5SMatthew G Knepley   v->lj           = PETSC_NULL;
5388ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5488ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
5588ed4aceSMatthew G Knepley   v->defaultSection       = PETSC_NULL;
5688ed4aceSMatthew G Knepley   v->defaultGlobalSection = PETSC_NULL;
571411c6eeSJed Brown 
581411c6eeSJed Brown   *dm = v;
59a4121054SBarry Smith   PetscFunctionReturn(0);
60a4121054SBarry Smith }
61a4121054SBarry Smith 
62a4121054SBarry Smith 
63a4121054SBarry Smith #undef __FUNCT__
649a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
659a42bb27SBarry Smith /*@C
66564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
679a42bb27SBarry Smith 
68aa219208SBarry Smith    Logically Collective on DMDA
699a42bb27SBarry Smith 
709a42bb27SBarry Smith    Input Parameter:
719a42bb27SBarry Smith +  da - initial distributed array
728154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
739a42bb27SBarry Smith 
749a42bb27SBarry Smith    Options Database:
75dd85299cSBarry Smith .   -dm_vec_type ctype
769a42bb27SBarry Smith 
779a42bb27SBarry Smith    Level: intermediate
789a42bb27SBarry Smith 
79aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
809a42bb27SBarry Smith @*/
817087cfbeSBarry Smith PetscErrorCode  DMSetVecType(DM da,const VecType ctype)
829a42bb27SBarry Smith {
839a42bb27SBarry Smith   PetscErrorCode ierr;
849a42bb27SBarry Smith 
859a42bb27SBarry Smith   PetscFunctionBegin;
869a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
879a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
889a42bb27SBarry Smith   ierr = PetscStrallocpy(ctype,&da->vectype);CHKERRQ(ierr);
899a42bb27SBarry Smith   PetscFunctionReturn(0);
909a42bb27SBarry Smith }
919a42bb27SBarry Smith 
929a42bb27SBarry Smith #undef __FUNCT__
93521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
94521d9a4cSLisandro Dalcin /*@C
95521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
96521d9a4cSLisandro Dalcin 
97521d9a4cSLisandro Dalcin    Logically Collective on DM
98521d9a4cSLisandro Dalcin 
99521d9a4cSLisandro Dalcin    Input Parameter:
100521d9a4cSLisandro Dalcin +  dm - the DM context
101521d9a4cSLisandro Dalcin .  ctype - the matrix type
102521d9a4cSLisandro Dalcin 
103521d9a4cSLisandro Dalcin    Options Database:
104521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
105521d9a4cSLisandro Dalcin 
106521d9a4cSLisandro Dalcin    Level: intermediate
107521d9a4cSLisandro Dalcin 
108521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType
109521d9a4cSLisandro Dalcin @*/
110521d9a4cSLisandro Dalcin PetscErrorCode  DMSetMatType(DM dm,const MatType ctype)
111521d9a4cSLisandro Dalcin {
112521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
113521d9a4cSLisandro Dalcin   PetscFunctionBegin;
114521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
115521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
116521d9a4cSLisandro Dalcin   ierr = PetscStrallocpy(ctype,&dm->mattype);CHKERRQ(ierr);
117521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
118521d9a4cSLisandro Dalcin }
119521d9a4cSLisandro Dalcin 
120521d9a4cSLisandro Dalcin #undef __FUNCT__
1219a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
1229a42bb27SBarry Smith /*@C
1239a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
124aa219208SBarry Smith    DMDA options in the database.
1259a42bb27SBarry Smith 
126aa219208SBarry Smith    Logically Collective on DMDA
1279a42bb27SBarry Smith 
1289a42bb27SBarry Smith    Input Parameter:
129aa219208SBarry Smith +  da - the DMDA context
1309a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
1319a42bb27SBarry Smith 
1329a42bb27SBarry Smith    Notes:
1339a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1349a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
1359a42bb27SBarry Smith 
1369a42bb27SBarry Smith    Level: advanced
1379a42bb27SBarry Smith 
138aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
1399a42bb27SBarry Smith 
1409a42bb27SBarry Smith .seealso: DMSetFromOptions()
1419a42bb27SBarry Smith @*/
1427087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
1439a42bb27SBarry Smith {
1449a42bb27SBarry Smith   PetscErrorCode ierr;
1459a42bb27SBarry Smith 
1469a42bb27SBarry Smith   PetscFunctionBegin;
1479a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1489a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
1499a42bb27SBarry Smith   PetscFunctionReturn(0);
1509a42bb27SBarry Smith }
1519a42bb27SBarry Smith 
1529a42bb27SBarry Smith #undef __FUNCT__
15347c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
15447c6ae99SBarry Smith /*@
155aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
15647c6ae99SBarry Smith 
15747c6ae99SBarry Smith     Collective on DM
15847c6ae99SBarry Smith 
15947c6ae99SBarry Smith     Input Parameter:
16047c6ae99SBarry Smith .   dm - the DM object to destroy
16147c6ae99SBarry Smith 
16247c6ae99SBarry Smith     Level: developer
16347c6ae99SBarry Smith 
164e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
16547c6ae99SBarry Smith 
16647c6ae99SBarry Smith @*/
167fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
16847c6ae99SBarry Smith {
169732e2eb9SMatthew G Knepley   PetscInt       i, cnt = 0;
170dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
17147c6ae99SBarry Smith   PetscErrorCode ierr;
17247c6ae99SBarry Smith 
17347c6ae99SBarry Smith   PetscFunctionBegin;
1746bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
1756bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
17687e657c6SBarry Smith 
17787e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
178732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1796bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
1806bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
181732e2eb9SMatthew G Knepley   }
182dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
18371cd77b2SBarry Smith   if ((*dm)->x) {
18471cd77b2SBarry Smith     PetscObject obj;
18571cd77b2SBarry Smith     ierr = PetscObjectQuery((PetscObject)(*dm)->x,"DM",&obj);CHKERRQ(ierr);
18671cd77b2SBarry Smith     if (obj == (PetscObject)*dm) cnt++;
18771cd77b2SBarry Smith   }
188732e2eb9SMatthew G Knepley 
1896bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
190732e2eb9SMatthew G Knepley   /*
191732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
192732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
193732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
194732e2eb9SMatthew G Knepley   */
1956bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
1966bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
197732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1986bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
1996bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
200732e2eb9SMatthew G Knepley   }
201dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
202dfe15315SJed Brown     nnext = nlink->next;
203dfe15315SJed Brown     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
204dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
205dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
206dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
207dfe15315SJed Brown   }
208dfe15315SJed Brown   (*dm)->namedglobal = PETSC_NULL;
2091a266240SBarry Smith 
210b17ce1afSJed Brown   /* Destroy the list of hooks */
211c833c3b5SJed Brown   {
212c833c3b5SJed Brown     DMCoarsenHookLink link,next;
213b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
214b17ce1afSJed Brown       next = link->next;
215b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
216b17ce1afSJed Brown     }
217b17ce1afSJed Brown     (*dm)->coarsenhook = PETSC_NULL;
218c833c3b5SJed Brown   }
219c833c3b5SJed Brown   {
220c833c3b5SJed Brown     DMRefineHookLink link,next;
221c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
222c833c3b5SJed Brown       next = link->next;
223c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
224c833c3b5SJed Brown     }
225c833c3b5SJed Brown     (*dm)->refinehook = PETSC_NULL;
226c833c3b5SJed Brown   }
227b17ce1afSJed Brown 
2281a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
2291a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
2301a266240SBarry Smith   }
23187e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
23271cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
2334dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
2346bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
2356bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
2366bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
237073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
238a89ea682SMatthew G Knepley   ierr = PetscFree((*dm)->workArray);CHKERRQ(ierr);
23988ed4aceSMatthew G Knepley 
24088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
24188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
24288ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
24388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
244732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
2456bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
246732e2eb9SMatthew G Knepley 
2476bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
2486bf464f9SBarry Smith   ierr = PetscFree((*dm)->data);CHKERRQ(ierr);
249732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
25047c6ae99SBarry Smith   PetscFunctionReturn(0);
25147c6ae99SBarry Smith }
25247c6ae99SBarry Smith 
25347c6ae99SBarry Smith #undef __FUNCT__
254d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
255d7bf68aeSBarry Smith /*@
256d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
257d7bf68aeSBarry Smith 
258d7bf68aeSBarry Smith     Collective on DM
259d7bf68aeSBarry Smith 
260d7bf68aeSBarry Smith     Input Parameter:
261d7bf68aeSBarry Smith .   dm - the DM object to setup
262d7bf68aeSBarry Smith 
263d7bf68aeSBarry Smith     Level: developer
264d7bf68aeSBarry Smith 
265e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
266d7bf68aeSBarry Smith 
267d7bf68aeSBarry Smith @*/
2687087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
269d7bf68aeSBarry Smith {
270d7bf68aeSBarry Smith   PetscErrorCode ierr;
271d7bf68aeSBarry Smith 
272d7bf68aeSBarry Smith   PetscFunctionBegin;
273171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2748387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
275d7bf68aeSBarry Smith   if (dm->ops->setup) {
276d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
277d7bf68aeSBarry Smith   }
2788387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
279d7bf68aeSBarry Smith   PetscFunctionReturn(0);
280d7bf68aeSBarry Smith }
281d7bf68aeSBarry Smith 
282d7bf68aeSBarry Smith #undef __FUNCT__
283d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
284d7bf68aeSBarry Smith /*@
285d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
286d7bf68aeSBarry Smith 
287d7bf68aeSBarry Smith     Collective on DM
288d7bf68aeSBarry Smith 
289d7bf68aeSBarry Smith     Input Parameter:
290d7bf68aeSBarry Smith .   dm - the DM object to set options for
291d7bf68aeSBarry Smith 
292732e2eb9SMatthew G Knepley     Options Database:
293dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
294dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
295171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
296171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
297732e2eb9SMatthew G Knepley 
298d7bf68aeSBarry Smith     Level: developer
299d7bf68aeSBarry Smith 
300e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
301d7bf68aeSBarry Smith 
302d7bf68aeSBarry Smith @*/
3037087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
304d7bf68aeSBarry Smith {
30567ad5babSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg;
306d7bf68aeSBarry Smith   PetscErrorCode ierr;
307f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
308d7bf68aeSBarry Smith 
309d7bf68aeSBarry Smith   PetscFunctionBegin;
310171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3113194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
31282fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
313c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
314c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
31567ad5babSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_latex", "Output mesh in LaTeX TikZ format", "DMView", flg4, &flg4, PETSC_NULL);CHKERRQ(ierr);
316073dac72SJed Brown     ierr = PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,PETSC_NULL);CHKERRQ(ierr);
317f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
318f9ba7244SBarry Smith     if (flg) {
319f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
320f9ba7244SBarry Smith     }
321521d9a4cSLisandro Dalcin     ierr = PetscOptionsList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype?dm->mattype:typeName,typeName,sizeof typeName,&flg);CHKERRQ(ierr);
322073dac72SJed Brown     if (flg) {
323521d9a4cSLisandro Dalcin       ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
324073dac72SJed Brown     }
3251b89239cSHong Zhang     ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,PETSC_NULL);CHKERRQ(ierr);
326f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
327f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
328f9ba7244SBarry Smith     }
329f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
330f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
33182fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
33282fcb398SMatthew G Knepley   if (flg1) {
33382fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
33482fcb398SMatthew G Knepley   }
335c4721b0eSMatthew G Knepley   if (flg2) {
336c4721b0eSMatthew G Knepley     PetscViewer viewer;
337c4721b0eSMatthew G Knepley 
338c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
339c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
340c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
341c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
342c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
343c4721b0eSMatthew G Knepley   }
344c4721b0eSMatthew G Knepley   if (flg3) {
345c4721b0eSMatthew G Knepley     PetscViewer viewer;
346c4721b0eSMatthew G Knepley 
347c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
348c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
349c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
350c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
351c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
352c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
353c4721b0eSMatthew G Knepley   }
35467ad5babSMatthew G Knepley   if (flg4) {
35567ad5babSMatthew G Knepley     PetscViewer viewer;
35667ad5babSMatthew G Knepley 
35767ad5babSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
35867ad5babSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
35967ad5babSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_LATEX);CHKERRQ(ierr);
36067ad5babSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.tex");CHKERRQ(ierr);
36167ad5babSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
36267ad5babSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
36367ad5babSMatthew G Knepley   }
364d7bf68aeSBarry Smith   PetscFunctionReturn(0);
365d7bf68aeSBarry Smith }
366d7bf68aeSBarry Smith 
367d7bf68aeSBarry Smith #undef __FUNCT__
36847c6ae99SBarry Smith #define __FUNCT__ "DMView"
369fc9bc008SSatish Balay /*@C
370aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
37147c6ae99SBarry Smith 
37247c6ae99SBarry Smith     Collective on DM
37347c6ae99SBarry Smith 
37447c6ae99SBarry Smith     Input Parameter:
37547c6ae99SBarry Smith +   dm - the DM object to view
37647c6ae99SBarry Smith -   v - the viewer
37747c6ae99SBarry Smith 
37847c6ae99SBarry Smith     Level: developer
37947c6ae99SBarry Smith 
380e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
38147c6ae99SBarry Smith 
38247c6ae99SBarry Smith @*/
3837087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
38447c6ae99SBarry Smith {
38547c6ae99SBarry Smith   PetscErrorCode ierr;
38647c6ae99SBarry Smith 
38747c6ae99SBarry Smith   PetscFunctionBegin;
388171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3893014e516SBarry Smith  if (!v) {
3903014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
3913014e516SBarry Smith   }
3920c010503SBarry Smith   if (dm->ops->view) {
3930c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
39447c6ae99SBarry Smith   }
39547c6ae99SBarry Smith   PetscFunctionReturn(0);
39647c6ae99SBarry Smith }
39747c6ae99SBarry Smith 
39847c6ae99SBarry Smith #undef __FUNCT__
39947c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
40047c6ae99SBarry Smith /*@
401aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
40247c6ae99SBarry Smith 
40347c6ae99SBarry Smith     Collective on DM
40447c6ae99SBarry Smith 
40547c6ae99SBarry Smith     Input Parameter:
40647c6ae99SBarry Smith .   dm - the DM object
40747c6ae99SBarry Smith 
40847c6ae99SBarry Smith     Output Parameter:
40947c6ae99SBarry Smith .   vec - the global vector
41047c6ae99SBarry Smith 
411073dac72SJed Brown     Level: beginner
41247c6ae99SBarry Smith 
413e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
41447c6ae99SBarry Smith 
41547c6ae99SBarry Smith @*/
4167087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
41747c6ae99SBarry Smith {
41847c6ae99SBarry Smith   PetscErrorCode ierr;
41947c6ae99SBarry Smith 
42047c6ae99SBarry Smith   PetscFunctionBegin;
421171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42288ed4aceSMatthew G Knepley   if (dm->defaultSection) {
42388ed4aceSMatthew G Knepley     PetscSection gSection;
42488ed4aceSMatthew G Knepley     PetscInt     localSize;
42588ed4aceSMatthew G Knepley 
42688ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
42788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
42888ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
42988ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
43088ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
43188ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
43288ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
43388ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
43488ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
43588ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
43688ed4aceSMatthew G Knepley   } else {
43747c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
43888ed4aceSMatthew G Knepley   }
43947c6ae99SBarry Smith   PetscFunctionReturn(0);
44047c6ae99SBarry Smith }
44147c6ae99SBarry Smith 
44247c6ae99SBarry Smith #undef __FUNCT__
44347c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
44447c6ae99SBarry Smith /*@
445aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
44647c6ae99SBarry Smith 
44747c6ae99SBarry Smith     Not Collective
44847c6ae99SBarry Smith 
44947c6ae99SBarry Smith     Input Parameter:
45047c6ae99SBarry Smith .   dm - the DM object
45147c6ae99SBarry Smith 
45247c6ae99SBarry Smith     Output Parameter:
45347c6ae99SBarry Smith .   vec - the local vector
45447c6ae99SBarry Smith 
455073dac72SJed Brown     Level: beginner
45647c6ae99SBarry Smith 
457e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
45847c6ae99SBarry Smith 
45947c6ae99SBarry Smith @*/
4607087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
46147c6ae99SBarry Smith {
46247c6ae99SBarry Smith   PetscErrorCode ierr;
46347c6ae99SBarry Smith 
46447c6ae99SBarry Smith   PetscFunctionBegin;
465171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
46688ed4aceSMatthew G Knepley   if (dm->defaultSection) {
46788ed4aceSMatthew G Knepley     PetscInt localSize;
46888ed4aceSMatthew G Knepley 
46988ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
47088ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
47188ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
47288ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
47388ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
47488ed4aceSMatthew G Knepley   } else {
47547c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
47688ed4aceSMatthew G Knepley   }
47747c6ae99SBarry Smith   PetscFunctionReturn(0);
47847c6ae99SBarry Smith }
47947c6ae99SBarry Smith 
48047c6ae99SBarry Smith #undef __FUNCT__
4811411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
4821411c6eeSJed Brown /*@
4831411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
4841411c6eeSJed Brown 
4851411c6eeSJed Brown    Collective on DM
4861411c6eeSJed Brown 
4871411c6eeSJed Brown    Input Parameter:
4881411c6eeSJed Brown .  dm - the DM that provides the mapping
4891411c6eeSJed Brown 
4901411c6eeSJed Brown    Output Parameter:
4911411c6eeSJed Brown .  ltog - the mapping
4921411c6eeSJed Brown 
4931411c6eeSJed Brown    Level: intermediate
4941411c6eeSJed Brown 
4951411c6eeSJed Brown    Notes:
4961411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
4971411c6eeSJed Brown    MatSetLocalToGlobalMapping().
4981411c6eeSJed Brown 
4991411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
5001411c6eeSJed Brown @*/
5017087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
5021411c6eeSJed Brown {
5031411c6eeSJed Brown   PetscErrorCode ierr;
5041411c6eeSJed Brown 
5051411c6eeSJed Brown   PetscFunctionBegin;
5061411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5071411c6eeSJed Brown   PetscValidPointer(ltog,2);
5081411c6eeSJed Brown   if (!dm->ltogmap) {
50937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
51037d0c07bSMatthew G Knepley 
51137d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
51237d0c07bSMatthew G Knepley     if (section) {
51337d0c07bSMatthew G Knepley       PetscInt      *ltog;
51437d0c07bSMatthew G Knepley       PetscInt       pStart, pEnd, size, p, l;
51537d0c07bSMatthew G Knepley 
51637d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
51737d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
51837d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
51937d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
52037d0c07bSMatthew G Knepley       for(p = pStart, l = 0; p < pEnd; ++p) {
52137d0c07bSMatthew G Knepley         PetscInt dof, off, c;
52237d0c07bSMatthew G Knepley 
52337d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
52437d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
52537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
52637d0c07bSMatthew G Knepley         for(c = 0; c < dof; ++c, ++l) {
52737d0c07bSMatthew G Knepley           ltog[l] = off+c;
52837d0c07bSMatthew G Knepley         }
52937d0c07bSMatthew G Knepley       }
53037d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
53137d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
53237d0c07bSMatthew G Knepley     } else {
5331411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
5341411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
5351411c6eeSJed Brown     }
53637d0c07bSMatthew G Knepley   }
5371411c6eeSJed Brown   *ltog = dm->ltogmap;
5381411c6eeSJed Brown   PetscFunctionReturn(0);
5391411c6eeSJed Brown }
5401411c6eeSJed Brown 
5411411c6eeSJed Brown #undef __FUNCT__
5421411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
5431411c6eeSJed Brown /*@
5441411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
5451411c6eeSJed Brown 
5461411c6eeSJed Brown    Collective on DM
5471411c6eeSJed Brown 
5481411c6eeSJed Brown    Input Parameter:
5491411c6eeSJed Brown .  da - the distributed array that provides the mapping
5501411c6eeSJed Brown 
5511411c6eeSJed Brown    Output Parameter:
5521411c6eeSJed Brown .  ltog - the block mapping
5531411c6eeSJed Brown 
5541411c6eeSJed Brown    Level: intermediate
5551411c6eeSJed Brown 
5561411c6eeSJed Brown    Notes:
5571411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
5581411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
5591411c6eeSJed Brown 
5601411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
5611411c6eeSJed Brown @*/
5627087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
5631411c6eeSJed Brown {
5641411c6eeSJed Brown   PetscErrorCode ierr;
5651411c6eeSJed Brown 
5661411c6eeSJed Brown   PetscFunctionBegin;
5671411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5681411c6eeSJed Brown   PetscValidPointer(ltog,2);
5691411c6eeSJed Brown   if (!dm->ltogmapb) {
5701411c6eeSJed Brown     PetscInt bs;
5711411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
5721411c6eeSJed Brown     if (bs > 1) {
5731411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
5741411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
5751411c6eeSJed Brown     } else {
5761411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
5771411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
5781411c6eeSJed Brown     }
5791411c6eeSJed Brown   }
5801411c6eeSJed Brown   *ltog = dm->ltogmapb;
5811411c6eeSJed Brown   PetscFunctionReturn(0);
5821411c6eeSJed Brown }
5831411c6eeSJed Brown 
5841411c6eeSJed Brown #undef __FUNCT__
5851411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
5861411c6eeSJed Brown /*@
5871411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
5881411c6eeSJed Brown 
5891411c6eeSJed Brown    Not Collective
5901411c6eeSJed Brown 
5911411c6eeSJed Brown    Input Parameter:
5921411c6eeSJed Brown .  dm - the DM with block structure
5931411c6eeSJed Brown 
5941411c6eeSJed Brown    Output Parameter:
5951411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
5961411c6eeSJed Brown 
5971411c6eeSJed Brown    Level: intermediate
5981411c6eeSJed Brown 
5991411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
6001411c6eeSJed Brown @*/
6017087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
6021411c6eeSJed Brown {
6031411c6eeSJed Brown   PetscFunctionBegin;
6041411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6051411c6eeSJed Brown   PetscValidPointer(bs,2);
6061411c6eeSJed Brown   if (dm->bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
6071411c6eeSJed Brown   *bs = dm->bs;
6081411c6eeSJed Brown   PetscFunctionReturn(0);
6091411c6eeSJed Brown }
6101411c6eeSJed Brown 
6111411c6eeSJed Brown #undef __FUNCT__
612e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
61347c6ae99SBarry Smith /*@
614e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
61547c6ae99SBarry Smith 
61647c6ae99SBarry Smith     Collective on DM
61747c6ae99SBarry Smith 
61847c6ae99SBarry Smith     Input Parameter:
61947c6ae99SBarry Smith +   dm1 - the DM object
62047c6ae99SBarry Smith -   dm2 - the second, finer DM object
62147c6ae99SBarry Smith 
62247c6ae99SBarry Smith     Output Parameter:
62347c6ae99SBarry Smith +  mat - the interpolation
62447c6ae99SBarry Smith -  vec - the scaling (optional)
62547c6ae99SBarry Smith 
62647c6ae99SBarry Smith     Level: developer
62747c6ae99SBarry Smith 
62885afcc9aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
62985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
630d52bd9f3SBarry Smith 
631d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
632d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
63385afcc9aSBarry Smith 
63485afcc9aSBarry Smith 
635e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
63647c6ae99SBarry Smith 
63747c6ae99SBarry Smith @*/
638e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
63947c6ae99SBarry Smith {
64047c6ae99SBarry Smith   PetscErrorCode ierr;
64147c6ae99SBarry Smith 
64247c6ae99SBarry Smith   PetscFunctionBegin;
643171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
644171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
64525296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
64647c6ae99SBarry Smith   PetscFunctionReturn(0);
64747c6ae99SBarry Smith }
64847c6ae99SBarry Smith 
64947c6ae99SBarry Smith #undef __FUNCT__
650e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
65147c6ae99SBarry Smith /*@
652e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
65347c6ae99SBarry Smith 
65447c6ae99SBarry Smith     Collective on DM
65547c6ae99SBarry Smith 
65647c6ae99SBarry Smith     Input Parameter:
65747c6ae99SBarry Smith +   dm1 - the DM object
65847c6ae99SBarry Smith -   dm2 - the second, finer DM object
65947c6ae99SBarry Smith 
66047c6ae99SBarry Smith     Output Parameter:
66147c6ae99SBarry Smith .   ctx - the injection
66247c6ae99SBarry Smith 
66347c6ae99SBarry Smith     Level: developer
66447c6ae99SBarry Smith 
66585afcc9aSBarry Smith    Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
66685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
66785afcc9aSBarry Smith 
668e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
66947c6ae99SBarry Smith 
67047c6ae99SBarry Smith @*/
671e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
67247c6ae99SBarry Smith {
67347c6ae99SBarry Smith   PetscErrorCode ierr;
67447c6ae99SBarry Smith 
67547c6ae99SBarry Smith   PetscFunctionBegin;
676171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
677171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
67847c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
67947c6ae99SBarry Smith   PetscFunctionReturn(0);
68047c6ae99SBarry Smith }
68147c6ae99SBarry Smith 
68247c6ae99SBarry Smith #undef __FUNCT__
683e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
684d1e2c406SBarry Smith /*@C
685e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
68647c6ae99SBarry Smith 
68747c6ae99SBarry Smith     Collective on DM
68847c6ae99SBarry Smith 
68947c6ae99SBarry Smith     Input Parameter:
69047c6ae99SBarry Smith +   dm - the DM object
69147c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
69247c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
69347c6ae99SBarry Smith 
69447c6ae99SBarry Smith     Output Parameter:
69547c6ae99SBarry Smith .   coloring - the coloring
69647c6ae99SBarry Smith 
69747c6ae99SBarry Smith     Level: developer
69847c6ae99SBarry Smith 
699e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
70047c6ae99SBarry Smith 
701aab9d709SJed Brown @*/
702e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
70347c6ae99SBarry Smith {
70447c6ae99SBarry Smith   PetscErrorCode ierr;
70547c6ae99SBarry Smith 
70647c6ae99SBarry Smith   PetscFunctionBegin;
707171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
70847c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
70947c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
71047c6ae99SBarry Smith   PetscFunctionReturn(0);
71147c6ae99SBarry Smith }
71247c6ae99SBarry Smith 
71347c6ae99SBarry Smith #undef __FUNCT__
714950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
71547c6ae99SBarry Smith /*@C
716950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
71747c6ae99SBarry Smith 
71847c6ae99SBarry Smith     Collective on DM
71947c6ae99SBarry Smith 
72047c6ae99SBarry Smith     Input Parameter:
72147c6ae99SBarry Smith +   dm - the DM object
72247c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
72394013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
72447c6ae99SBarry Smith 
72547c6ae99SBarry Smith     Output Parameter:
72647c6ae99SBarry Smith .   mat - the empty Jacobian
72747c6ae99SBarry Smith 
728073dac72SJed Brown     Level: beginner
72947c6ae99SBarry Smith 
73094013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
73194013140SBarry Smith        do not need to do it yourself.
73294013140SBarry Smith 
73394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
734aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
73594013140SBarry Smith 
73694013140SBarry Smith        For structured grid problems, when you call MatView() on this matrix it is displayed using the global natural ordering, NOT in the ordering used
73794013140SBarry Smith        internally by PETSc.
73894013140SBarry Smith 
73994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
740aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
74194013140SBarry Smith 
742e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
74347c6ae99SBarry Smith 
744aab9d709SJed Brown @*/
745950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
74647c6ae99SBarry Smith {
74747c6ae99SBarry Smith   PetscErrorCode ierr;
74847c6ae99SBarry Smith 
74947c6ae99SBarry Smith   PetscFunctionBegin;
750171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
751235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
752235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
753235683edSBarry Smith #endif
754c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
755c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
756073dac72SJed Brown   if (dm->mattype) {
75725296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
758073dac72SJed Brown   } else {
75925296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
760c7b7c8a4SJed Brown   }
76147c6ae99SBarry Smith   PetscFunctionReturn(0);
76247c6ae99SBarry Smith }
76347c6ae99SBarry Smith 
76447c6ae99SBarry Smith #undef __FUNCT__
765732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
766732e2eb9SMatthew G Knepley /*@
767950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
768732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
769732e2eb9SMatthew G Knepley 
770732e2eb9SMatthew G Knepley   Logically Collective on DMDA
771732e2eb9SMatthew G Knepley 
772732e2eb9SMatthew G Knepley   Input Parameter:
773732e2eb9SMatthew G Knepley + dm - the DM
774732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
775732e2eb9SMatthew G Knepley 
776732e2eb9SMatthew G Knepley   Level: developer
777950540a4SJed Brown .seealso DMCreateMatrix()
778732e2eb9SMatthew G Knepley @*/
779732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
780732e2eb9SMatthew G Knepley {
781732e2eb9SMatthew G Knepley   PetscFunctionBegin;
782732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
783732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
784732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
785732e2eb9SMatthew G Knepley }
786732e2eb9SMatthew G Knepley 
787732e2eb9SMatthew G Knepley #undef __FUNCT__
788a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
789a89ea682SMatthew G Knepley /*@C
790a89ea682SMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size
791a89ea682SMatthew G Knepley 
792a89ea682SMatthew G Knepley   Not Collective
793a89ea682SMatthew G Knepley 
794a89ea682SMatthew G Knepley   Input Parameters:
795a89ea682SMatthew G Knepley + dm - the DM object
796a89ea682SMatthew G Knepley - size - The minium size
797a89ea682SMatthew G Knepley 
798a89ea682SMatthew G Knepley   Output Parameter:
799a89ea682SMatthew G Knepley . array - the work array
800a89ea682SMatthew G Knepley 
801a89ea682SMatthew G Knepley   Level: developer
802a89ea682SMatthew G Knepley 
803a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
804a89ea682SMatthew G Knepley @*/
805a89ea682SMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt size,PetscScalar **array)
806a89ea682SMatthew G Knepley {
807a89ea682SMatthew G Knepley   PetscErrorCode ierr;
808a89ea682SMatthew G Knepley 
809a89ea682SMatthew G Knepley   PetscFunctionBegin;
810a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
811a89ea682SMatthew G Knepley   PetscValidPointer(array,3);
812a89ea682SMatthew G Knepley   if (size > dm->workSize) {
813a89ea682SMatthew G Knepley     dm->workSize = size;
814a89ea682SMatthew G Knepley     ierr = PetscFree(dm->workArray);CHKERRQ(ierr);
815a89ea682SMatthew G Knepley     ierr = PetscMalloc(dm->workSize * sizeof(PetscScalar), &dm->workArray);CHKERRQ(ierr);
816a89ea682SMatthew G Knepley   }
817a89ea682SMatthew G Knepley   *array = dm->workArray;
818a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
819a89ea682SMatthew G Knepley }
820a89ea682SMatthew G Knepley 
821e7c4fc90SDmitry Karpeev 
822e7c4fc90SDmitry Karpeev #undef __FUNCT__
8234d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
8244f3b5142SJed Brown /*@C
8254d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
8264d343eeaSMatthew G Knepley 
8274d343eeaSMatthew G Knepley   Not collective
8284d343eeaSMatthew G Knepley 
8294d343eeaSMatthew G Knepley   Input Parameter:
8304d343eeaSMatthew G Knepley . dm - the DM object
8314d343eeaSMatthew G Knepley 
8324d343eeaSMatthew G Knepley   Output Parameters:
83321c9b008SJed Brown + numFields  - The number of fields (or PETSC_NULL if not requested)
83437d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested)
83521c9b008SJed Brown - fields     - The global indices for each field (or PETSC_NULL if not requested)
8364d343eeaSMatthew G Knepley 
8374d343eeaSMatthew G Knepley   Level: intermediate
8384d343eeaSMatthew G Knepley 
83921c9b008SJed Brown   Notes:
84021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
84121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
84221c9b008SJed Brown   PetscFree().
84321c9b008SJed Brown 
8444d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
8454d343eeaSMatthew G Knepley @*/
84637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
8474d343eeaSMatthew G Knepley {
84837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
8494d343eeaSMatthew G Knepley   PetscErrorCode ierr;
8504d343eeaSMatthew G Knepley 
8514d343eeaSMatthew G Knepley   PetscFunctionBegin;
8524d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85369ca1f37SDmitry Karpeev   if (numFields) {
85469ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
85569ca1f37SDmitry Karpeev     *numFields = 0;
85669ca1f37SDmitry Karpeev   }
85737d0c07bSMatthew G Knepley   if (fieldNames) {
85837d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
85937d0c07bSMatthew G Knepley     *fieldNames = PETSC_NULL;
86069ca1f37SDmitry Karpeev   }
86169ca1f37SDmitry Karpeev   if (fields) {
86269ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
86369ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
86469ca1f37SDmitry Karpeev   }
86537d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
86637d0c07bSMatthew G Knepley   if (section) {
86737d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
86837d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
86937d0c07bSMatthew G Knepley 
87037d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
87137d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
87237d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr);
87337d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
87437d0c07bSMatthew G Knepley     for(f = 0; f < nF; ++f) {
87537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
87637d0c07bSMatthew G Knepley     }
87737d0c07bSMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
87837d0c07bSMatthew G Knepley       PetscInt gdof;
87937d0c07bSMatthew G Knepley 
88037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
88137d0c07bSMatthew G Knepley       if (gdof > 0) {
88237d0c07bSMatthew G Knepley         for(f = 0; f < nF; ++f) {
88337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
88437d0c07bSMatthew G Knepley 
88537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
88637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
88737d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
88837d0c07bSMatthew G Knepley         }
88937d0c07bSMatthew G Knepley       }
89037d0c07bSMatthew G Knepley     }
89137d0c07bSMatthew G Knepley     for(f = 0; f < nF; ++f) {
89237d0c07bSMatthew G Knepley       ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
89337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
89437d0c07bSMatthew G Knepley     }
89537d0c07bSMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
89637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
89737d0c07bSMatthew G Knepley 
89837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
89937d0c07bSMatthew G Knepley       if (gdof > 0) {
90037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
90137d0c07bSMatthew G Knepley         for(f = 0; f < nF; ++f) {
90237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
90337d0c07bSMatthew G Knepley 
90437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
90537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
90637d0c07bSMatthew G Knepley           for(fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
90737d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
90837d0c07bSMatthew G Knepley           }
90937d0c07bSMatthew G Knepley         }
91037d0c07bSMatthew G Knepley       }
91137d0c07bSMatthew G Knepley     }
91237d0c07bSMatthew G Knepley     if (numFields) {*numFields = nF;}
91337d0c07bSMatthew G Knepley     if (fieldNames) {
91437d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr);
91537d0c07bSMatthew G Knepley       for(f = 0; f < nF; ++f) {
91637d0c07bSMatthew G Knepley         const char *fieldName;
91737d0c07bSMatthew G Knepley 
91837d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
91937d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr);
92037d0c07bSMatthew G Knepley       }
92137d0c07bSMatthew G Knepley     }
92237d0c07bSMatthew G Knepley     if (fields) {
92337d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
92437d0c07bSMatthew G Knepley       for(f = 0; f < nF; ++f) {
92537d0c07bSMatthew G Knepley         ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
92637d0c07bSMatthew G Knepley       }
92737d0c07bSMatthew G Knepley     }
92837d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
92937d0c07bSMatthew G Knepley   } else {
93037d0c07bSMatthew G Knepley     if(dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);}
93169ca1f37SDmitry Karpeev   }
9324d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
9334d343eeaSMatthew G Knepley }
9344d343eeaSMatthew G Knepley 
9355fe1f584SPeter Brune 
936a89ea682SMatthew G Knepley #undef __FUNCT__
93716621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM"
938e7c4fc90SDmitry Karpeev /*@C
93916621825SDmitry Karpeev   DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields.
94016621825SDmitry Karpeev 
94116621825SDmitry Karpeev   Not Collective
94216621825SDmitry Karpeev 
94316621825SDmitry Karpeev   Input Parameters:
94416621825SDmitry Karpeev + dm   - the DM object
94516621825SDmitry Karpeev - name - the name of the field decomposition
94616621825SDmitry Karpeev 
94716621825SDmitry Karpeev   Output Parameter:
94816621825SDmitry Karpeev . ddm  - the field decomposition DM (PETSC_NULL, if no such decomposition is known)
94916621825SDmitry Karpeev 
95016621825SDmitry Karpeev   Level: advanced
95116621825SDmitry Karpeev 
95216621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
95316621825SDmitry Karpeev @*/
95416621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm)
95516621825SDmitry Karpeev {
95616621825SDmitry Karpeev   PetscErrorCode ierr;
95716621825SDmitry Karpeev 
95816621825SDmitry Karpeev   PetscFunctionBegin;
95916621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
96016621825SDmitry Karpeev   PetscValidCharPointer(name,2);
96116621825SDmitry Karpeev   PetscValidPointer(ddm,3);
96216621825SDmitry Karpeev   *ddm = PETSC_NULL;
963731c8d9eSDmitry Karpeev   if(dm->ops->createfielddecompositiondm) {
96416621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
96516621825SDmitry Karpeev   }
96616621825SDmitry Karpeev   PetscFunctionReturn(0);
96716621825SDmitry Karpeev }
96816621825SDmitry Karpeev 
96916621825SDmitry Karpeev 
97016621825SDmitry Karpeev #undef __FUNCT__
97116621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
97216621825SDmitry Karpeev /*@C
97316621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
97416621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
97516621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
976e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
977e7c4fc90SDmitry Karpeev 
978e7c4fc90SDmitry Karpeev   Not collective
979e7c4fc90SDmitry Karpeev 
980e7c4fc90SDmitry Karpeev   Input Parameter:
981e7c4fc90SDmitry Karpeev . dm - the DM object
982e7c4fc90SDmitry Karpeev 
983e7c4fc90SDmitry Karpeev   Output Parameters:
98416621825SDmitry Karpeev + len       - The number of subproblems in the field decomposition (or PETSC_NULL if not requested)
98516621825SDmitry Karpeev . namelist  - The name for each field (or PETSC_NULL if not requested)
98616621825SDmitry Karpeev . islist    - The global indices for each field (or PETSC_NULL if not requested)
98716621825SDmitry Karpeev - dmlist    - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
988e7c4fc90SDmitry Karpeev 
989e7c4fc90SDmitry Karpeev   Level: intermediate
990e7c4fc90SDmitry Karpeev 
991e7c4fc90SDmitry Karpeev   Notes:
992e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
993e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
994e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
995e7c4fc90SDmitry Karpeev 
996e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
997e7c4fc90SDmitry Karpeev @*/
99816621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
999e7c4fc90SDmitry Karpeev {
1000e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1001e7c4fc90SDmitry Karpeev 
1002e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1003e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1004731c8d9eSDmitry Karpeev   if (len)      {PetscValidPointer(len,2);      *len      = 0;}
1005731c8d9eSDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3); *namelist = 0;}
1006731c8d9eSDmitry Karpeev   if (islist)   {PetscValidPointer(islist,4);   *islist   = 0;}
1007731c8d9eSDmitry Karpeev   if (dmlist)   {PetscValidPointer(dmlist,5);   *dmlist   = 0;}
100816621825SDmitry Karpeev   if(!dm->ops->createfielddecomposition) {
100969ca1f37SDmitry Karpeev     ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1010e7c4fc90SDmitry Karpeev     /* By default there are no DMs associated with subproblems. */
1011e7c4fc90SDmitry Karpeev     if(dmlist) *dmlist = PETSC_NULL;
1012e7c4fc90SDmitry Karpeev   }
1013e7c4fc90SDmitry Karpeev   else {
101416621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
101516621825SDmitry Karpeev   }
101616621825SDmitry Karpeev   PetscFunctionReturn(0);
101716621825SDmitry Karpeev }
101816621825SDmitry Karpeev 
101916621825SDmitry Karpeev #undef __FUNCT__
102016621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM"
102116621825SDmitry Karpeev /*@C
102216621825SDmitry Karpeev   DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains.
102316621825SDmitry Karpeev 
102416621825SDmitry Karpeev   Not Collective
102516621825SDmitry Karpeev 
102616621825SDmitry Karpeev   Input Parameters:
102716621825SDmitry Karpeev + dm   - the DM object
102816621825SDmitry Karpeev - name - the name of the subdomain decomposition
102916621825SDmitry Karpeev 
103016621825SDmitry Karpeev   Output Parameter:
103116621825SDmitry Karpeev . ddm  - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known)
103216621825SDmitry Karpeev 
103316621825SDmitry Karpeev   Level: advanced
103416621825SDmitry Karpeev 
103516621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
103616621825SDmitry Karpeev @*/
103716621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm)
103816621825SDmitry Karpeev {
103916621825SDmitry Karpeev   PetscErrorCode ierr;
104016621825SDmitry Karpeev 
104116621825SDmitry Karpeev   PetscFunctionBegin;
104216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
104316621825SDmitry Karpeev   PetscValidCharPointer(name,2);
104416621825SDmitry Karpeev   PetscValidPointer(ddm,3);
104516621825SDmitry Karpeev   *ddm = PETSC_NULL;
1046731c8d9eSDmitry Karpeev   if(dm->ops->createdomaindecompositiondm) {
104716621825SDmitry Karpeev     ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
104816621825SDmitry Karpeev   }
104916621825SDmitry Karpeev   PetscFunctionReturn(0);
105016621825SDmitry Karpeev }
105116621825SDmitry Karpeev 
105216621825SDmitry Karpeev 
105316621825SDmitry Karpeev #undef __FUNCT__
105416621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
105516621825SDmitry Karpeev /*@C
1056*8d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
1057*8d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
1058*8d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
1059*8d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
1060*8d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
106116621825SDmitry Karpeev 
106216621825SDmitry Karpeev   Not collective
106316621825SDmitry Karpeev 
106416621825SDmitry Karpeev   Input Parameter:
106516621825SDmitry Karpeev . dm - the DM object
106616621825SDmitry Karpeev 
106716621825SDmitry Karpeev   Output Parameters:
106816621825SDmitry Karpeev + len         - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested)
106916621825SDmitry Karpeev . namelist    - The name for each subdomain (or PETSC_NULL if not requested)
1070*8d4ac253SDmitry Karpeev . innerislist - The global indices for each inner subdomain (or PETSC_NULL, if not requested)
1071*8d4ac253SDmitry Karpeev . outerislist - The global indices for each outer subdomain (or PETSC_NULL, if not requested)
107216621825SDmitry Karpeev - dmlist      - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
107316621825SDmitry Karpeev 
107416621825SDmitry Karpeev   Level: intermediate
107516621825SDmitry Karpeev 
107616621825SDmitry Karpeev   Notes:
107716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
107816621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
107916621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
108016621825SDmitry Karpeev 
1081*8d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
108216621825SDmitry Karpeev @*/
1083*8d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
108416621825SDmitry Karpeev {
108516621825SDmitry Karpeev   PetscErrorCode ierr;
108616621825SDmitry Karpeev 
108716621825SDmitry Karpeev   PetscFunctionBegin;
108816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1089731c8d9eSDmitry Karpeev   if (len)           {PetscValidPointer(len,2);            *len         = PETSC_NULL;}
109016621825SDmitry Karpeev   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = PETSC_NULL;}
1091*8d4ac253SDmitry Karpeev   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = PETSC_NULL;}
1092*8d4ac253SDmitry Karpeev   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = PETSC_NULL;}
1093*8d4ac253SDmitry Karpeev   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = PETSC_NULL;}
109416621825SDmitry Karpeev   if(dm->ops->createdomaindecomposition) {
1095*8d4ac253SDmitry Karpeev     ierr = (*dm->ops->createdomaindecomposition)(dm,len,namelist,innerislist,outerislist,dmlist); CHKERRQ(ierr);
1096e7c4fc90SDmitry Karpeev   }
1097e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1098e7c4fc90SDmitry Karpeev }
1099e7c4fc90SDmitry Karpeev 
1100731c8d9eSDmitry Karpeev #undef __FUNCT__
110147c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
110247c6ae99SBarry Smith /*@
110347c6ae99SBarry Smith   DMRefine - Refines a DM object
110447c6ae99SBarry Smith 
110547c6ae99SBarry Smith   Collective on DM
110647c6ae99SBarry Smith 
110747c6ae99SBarry Smith   Input Parameter:
110847c6ae99SBarry Smith + dm   - the DM object
110991d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
111047c6ae99SBarry Smith 
111147c6ae99SBarry Smith   Output Parameter:
1112ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1113ae0a1c52SMatthew G Knepley 
1114ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
111547c6ae99SBarry Smith 
111647c6ae99SBarry Smith   Level: developer
111747c6ae99SBarry Smith 
1118e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
111947c6ae99SBarry Smith @*/
11207087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
112147c6ae99SBarry Smith {
112247c6ae99SBarry Smith   PetscErrorCode ierr;
1123c833c3b5SJed Brown   DMRefineHookLink link;
112447c6ae99SBarry Smith 
112547c6ae99SBarry Smith   PetscFunctionBegin;
1126732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
112747c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
11284057135bSMatthew G Knepley   if (*dmf) {
112943842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
1130644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
1131644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
1132644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
1133644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
1134644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
1135644e2e5bSBarry Smith     }
11368cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1137644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
11380598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1139656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
1140c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1141c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1142c833c3b5SJed Brown     }
1143c833c3b5SJed Brown   }
1144c833c3b5SJed Brown   PetscFunctionReturn(0);
1145c833c3b5SJed Brown }
1146c833c3b5SJed Brown 
1147c833c3b5SJed Brown #undef __FUNCT__
1148c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1149c833c3b5SJed Brown /*@
1150c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1151c833c3b5SJed Brown 
1152c833c3b5SJed Brown    Logically Collective
1153c833c3b5SJed Brown 
1154c833c3b5SJed Brown    Input Arguments:
1155c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1156c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1157c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1158c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1159c833c3b5SJed Brown 
1160c833c3b5SJed Brown    Calling sequence of refinehook:
1161c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1162c833c3b5SJed Brown 
1163c833c3b5SJed Brown +  coarse - coarse level DM
1164c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1165c833c3b5SJed Brown -  ctx - optional user-defined function context
1166c833c3b5SJed Brown 
1167c833c3b5SJed Brown    Calling sequence for interphook:
1168c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1169c833c3b5SJed Brown 
1170c833c3b5SJed Brown +  coarse - coarse level DM
1171c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1172c833c3b5SJed Brown .  fine - fine level DM to update
1173c833c3b5SJed Brown -  ctx - optional user-defined function context
1174c833c3b5SJed Brown 
1175c833c3b5SJed Brown    Level: advanced
1176c833c3b5SJed Brown 
1177c833c3b5SJed Brown    Notes:
1178c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1179c833c3b5SJed Brown 
1180c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1181c833c3b5SJed Brown 
1182c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1183c833c3b5SJed Brown @*/
1184c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1185c833c3b5SJed Brown {
1186c833c3b5SJed Brown   PetscErrorCode ierr;
1187c833c3b5SJed Brown   DMRefineHookLink link,*p;
1188c833c3b5SJed Brown 
1189c833c3b5SJed Brown   PetscFunctionBegin;
1190c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1191c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1192c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1193c833c3b5SJed Brown   link->refinehook = refinehook;
1194c833c3b5SJed Brown   link->interphook = interphook;
1195c833c3b5SJed Brown   link->ctx = ctx;
1196c833c3b5SJed Brown   link->next = PETSC_NULL;
1197c833c3b5SJed Brown   *p = link;
1198c833c3b5SJed Brown   PetscFunctionReturn(0);
1199c833c3b5SJed Brown }
1200c833c3b5SJed Brown 
1201c833c3b5SJed Brown #undef __FUNCT__
1202c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1203c833c3b5SJed Brown /*@
1204c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1205c833c3b5SJed Brown 
1206c833c3b5SJed Brown    Collective if any hooks are
1207c833c3b5SJed Brown 
1208c833c3b5SJed Brown    Input Arguments:
1209c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1210c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1211c833c3b5SJed Brown -  fine - finer DM to update
1212c833c3b5SJed Brown 
1213c833c3b5SJed Brown    Level: developer
1214c833c3b5SJed Brown 
1215c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1216c833c3b5SJed Brown @*/
1217c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1218c833c3b5SJed Brown {
1219c833c3b5SJed Brown   PetscErrorCode ierr;
1220c833c3b5SJed Brown   DMRefineHookLink link;
1221c833c3b5SJed Brown 
1222c833c3b5SJed Brown   PetscFunctionBegin;
1223c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1224c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
12254057135bSMatthew G Knepley   }
122647c6ae99SBarry Smith   PetscFunctionReturn(0);
122747c6ae99SBarry Smith }
122847c6ae99SBarry Smith 
122947c6ae99SBarry Smith #undef __FUNCT__
1230eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1231eb3f98d2SBarry Smith /*@
1232eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1233eb3f98d2SBarry Smith 
1234eb3f98d2SBarry Smith     Not Collective
1235eb3f98d2SBarry Smith 
1236eb3f98d2SBarry Smith     Input Parameter:
1237eb3f98d2SBarry Smith .   dm - the DM object
1238eb3f98d2SBarry Smith 
1239eb3f98d2SBarry Smith     Output Parameter:
1240eb3f98d2SBarry Smith .   level - number of refinements
1241eb3f98d2SBarry Smith 
1242eb3f98d2SBarry Smith     Level: developer
1243eb3f98d2SBarry Smith 
12446a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1245eb3f98d2SBarry Smith 
1246eb3f98d2SBarry Smith @*/
1247eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1248eb3f98d2SBarry Smith {
1249eb3f98d2SBarry Smith   PetscFunctionBegin;
1250eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1251eb3f98d2SBarry Smith   *level = dm->levelup;
1252eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1253eb3f98d2SBarry Smith }
1254eb3f98d2SBarry Smith 
1255eb3f98d2SBarry Smith #undef __FUNCT__
125647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
125747c6ae99SBarry Smith /*@
125847c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
125947c6ae99SBarry Smith 
126047c6ae99SBarry Smith     Neighbor-wise Collective on DM
126147c6ae99SBarry Smith 
126247c6ae99SBarry Smith     Input Parameters:
126347c6ae99SBarry Smith +   dm - the DM object
126447c6ae99SBarry Smith .   g - the global vector
126547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
126647c6ae99SBarry Smith -   l - the local vector
126747c6ae99SBarry Smith 
126847c6ae99SBarry Smith 
126947c6ae99SBarry Smith     Level: beginner
127047c6ae99SBarry Smith 
1271e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
127247c6ae99SBarry Smith 
127347c6ae99SBarry Smith @*/
12747087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
127547c6ae99SBarry Smith {
12767128ae9fSMatthew G Knepley   PetscSF        sf;
127747c6ae99SBarry Smith   PetscErrorCode ierr;
127847c6ae99SBarry Smith 
127947c6ae99SBarry Smith   PetscFunctionBegin;
1280171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12817128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
12827128ae9fSMatthew G Knepley   if (sf) {
12837128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
12847128ae9fSMatthew G Knepley 
12857128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
12867128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
12877128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
12887128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
12897128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
12907128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
12917128ae9fSMatthew G Knepley   } else {
1292843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
12937128ae9fSMatthew G Knepley   }
129447c6ae99SBarry Smith   PetscFunctionReturn(0);
129547c6ae99SBarry Smith }
129647c6ae99SBarry Smith 
129747c6ae99SBarry Smith #undef __FUNCT__
129847c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
129947c6ae99SBarry Smith /*@
130047c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
130147c6ae99SBarry Smith 
130247c6ae99SBarry Smith     Neighbor-wise Collective on DM
130347c6ae99SBarry Smith 
130447c6ae99SBarry Smith     Input Parameters:
130547c6ae99SBarry Smith +   dm - the DM object
130647c6ae99SBarry Smith .   g - the global vector
130747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
130847c6ae99SBarry Smith -   l - the local vector
130947c6ae99SBarry Smith 
131047c6ae99SBarry Smith 
131147c6ae99SBarry Smith     Level: beginner
131247c6ae99SBarry Smith 
1313e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
131447c6ae99SBarry Smith 
131547c6ae99SBarry Smith @*/
13167087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
131747c6ae99SBarry Smith {
13187128ae9fSMatthew G Knepley   PetscSF        sf;
131947c6ae99SBarry Smith   PetscErrorCode ierr;
132061a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
132147c6ae99SBarry Smith 
132247c6ae99SBarry Smith   PetscFunctionBegin;
1323171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13247128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
13257128ae9fSMatthew G Knepley   if (sf) {
13267128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
13277128ae9fSMatthew G Knepley 
13287128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
13297128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
13307128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
13317128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
13327128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
13337128ae9fSMatthew G Knepley   } else {
1334843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
13357128ae9fSMatthew G Knepley   }
133647c6ae99SBarry Smith   PetscFunctionReturn(0);
133747c6ae99SBarry Smith }
133847c6ae99SBarry Smith 
133947c6ae99SBarry Smith #undef __FUNCT__
13409a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
134147c6ae99SBarry Smith /*@
13429a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
13439a42bb27SBarry Smith 
13449a42bb27SBarry Smith     Neighbor-wise Collective on DM
13459a42bb27SBarry Smith 
13469a42bb27SBarry Smith     Input Parameters:
13479a42bb27SBarry Smith +   dm - the DM object
1348f6813fd5SJed Brown .   l - the local vector
13499a42bb27SBarry 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
13509a42bb27SBarry Smith            base point.
1351f6813fd5SJed Brown - - the global vector
13529a42bb27SBarry Smith 
13539a42bb27SBarry 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
13549a42bb27SBarry 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
13559a42bb27SBarry Smith            global array to the final global array with VecAXPY().
13569a42bb27SBarry Smith 
13579a42bb27SBarry Smith     Level: beginner
13589a42bb27SBarry Smith 
1359e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
13609a42bb27SBarry Smith 
13619a42bb27SBarry Smith @*/
13627087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
13639a42bb27SBarry Smith {
13647128ae9fSMatthew G Knepley   PetscSF        sf;
13659a42bb27SBarry Smith   PetscErrorCode ierr;
13669a42bb27SBarry Smith 
13679a42bb27SBarry Smith   PetscFunctionBegin;
1368171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13697128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
13707128ae9fSMatthew G Knepley   if (sf) {
13717128ae9fSMatthew G Knepley     MPI_Op       op;
13727128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
13737128ae9fSMatthew G Knepley 
13747128ae9fSMatthew G Knepley     switch(mode) {
13757128ae9fSMatthew G Knepley     case INSERT_VALUES:
13767128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
13777128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
13787128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
13797128ae9fSMatthew G Knepley #else
13807128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
13817128ae9fSMatthew G Knepley #endif
13827128ae9fSMatthew G Knepley     case ADD_VALUES:
13837128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
13847128ae9fSMatthew G Knepley       op = MPI_SUM; break;
13857128ae9fSMatthew G Knepley   default:
13867128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
13877128ae9fSMatthew G Knepley     }
13887128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
13897128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
13907128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
13917128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
13927128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
13937128ae9fSMatthew G Knepley   } else {
1394843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
13957128ae9fSMatthew G Knepley   }
13969a42bb27SBarry Smith   PetscFunctionReturn(0);
13979a42bb27SBarry Smith }
13989a42bb27SBarry Smith 
13999a42bb27SBarry Smith #undef __FUNCT__
14009a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
14019a42bb27SBarry Smith /*@
14029a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
140347c6ae99SBarry Smith 
140447c6ae99SBarry Smith     Neighbor-wise Collective on DM
140547c6ae99SBarry Smith 
140647c6ae99SBarry Smith     Input Parameters:
140747c6ae99SBarry Smith +   dm - the DM object
1408f6813fd5SJed Brown .   l - the local vector
140947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1410f6813fd5SJed Brown -   g - the global vector
141147c6ae99SBarry Smith 
141247c6ae99SBarry Smith 
141347c6ae99SBarry Smith     Level: beginner
141447c6ae99SBarry Smith 
1415e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
141647c6ae99SBarry Smith 
141747c6ae99SBarry Smith @*/
14187087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
141947c6ae99SBarry Smith {
14207128ae9fSMatthew G Knepley   PetscSF        sf;
142147c6ae99SBarry Smith   PetscErrorCode ierr;
142247c6ae99SBarry Smith 
142347c6ae99SBarry Smith   PetscFunctionBegin;
1424171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14257128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
14267128ae9fSMatthew G Knepley   if (sf) {
14277128ae9fSMatthew G Knepley     MPI_Op       op;
14287128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
14297128ae9fSMatthew G Knepley 
14307128ae9fSMatthew G Knepley     switch(mode) {
14317128ae9fSMatthew G Knepley     case INSERT_VALUES:
14327128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
14337128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
14347128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
14357128ae9fSMatthew G Knepley #else
14367128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
14377128ae9fSMatthew G Knepley #endif
14387128ae9fSMatthew G Knepley     case ADD_VALUES:
14397128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
14407128ae9fSMatthew G Knepley       op = MPI_SUM; break;
14417128ae9fSMatthew G Knepley     default:
14427128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
14437128ae9fSMatthew G Knepley     }
14447128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
14457128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
14467128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
14477128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
14487128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
14497128ae9fSMatthew G Knepley   } else {
1450843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
14517128ae9fSMatthew G Knepley   }
145247c6ae99SBarry Smith   PetscFunctionReturn(0);
145347c6ae99SBarry Smith }
145447c6ae99SBarry Smith 
145547c6ae99SBarry Smith #undef __FUNCT__
145647c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
145747c6ae99SBarry Smith /*@
145847c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
145947c6ae99SBarry Smith 
146047c6ae99SBarry Smith     Collective on DM
146147c6ae99SBarry Smith 
146247c6ae99SBarry Smith     Input Parameter:
146347c6ae99SBarry Smith +   dm - the DM object
146447c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
146547c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
146647c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
146747c6ae99SBarry Smith 
146847c6ae99SBarry Smith     Level: developer
146947c6ae99SBarry Smith 
1470e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
147147c6ae99SBarry Smith          DMSetFunction()
147247c6ae99SBarry Smith 
147347c6ae99SBarry Smith @*/
14747087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
147547c6ae99SBarry Smith {
147647c6ae99SBarry Smith   PetscErrorCode ierr;
1477171400e9SBarry Smith 
147847c6ae99SBarry Smith   PetscFunctionBegin;
1479171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
148047c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
148147c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
148247c6ae99SBarry Smith   if (A != B) {
148347c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
148447c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
148547c6ae99SBarry Smith   }
148647c6ae99SBarry Smith   PetscFunctionReturn(0);
148747c6ae99SBarry Smith }
148847c6ae99SBarry Smith 
148947c6ae99SBarry Smith #undef __FUNCT__
149047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
149147c6ae99SBarry Smith /*@
149247c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
149347c6ae99SBarry Smith 
149447c6ae99SBarry Smith     Collective on DM
149547c6ae99SBarry Smith 
149647c6ae99SBarry Smith     Input Parameter:
149747c6ae99SBarry Smith +   dm - the DM object
149891d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
149947c6ae99SBarry Smith 
150047c6ae99SBarry Smith     Output Parameter:
150147c6ae99SBarry Smith .   dmc - the coarsened DM
150247c6ae99SBarry Smith 
150347c6ae99SBarry Smith     Level: developer
150447c6ae99SBarry Smith 
1505e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
150647c6ae99SBarry Smith 
150747c6ae99SBarry Smith @*/
15087087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
150947c6ae99SBarry Smith {
151047c6ae99SBarry Smith   PetscErrorCode ierr;
1511b17ce1afSJed Brown   DMCoarsenHookLink link;
151247c6ae99SBarry Smith 
151347c6ae99SBarry Smith   PetscFunctionBegin;
1514171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
151547c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
151643842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
151747c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
151847c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
151947c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
152047c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
152147c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
152247c6ae99SBarry Smith   }
15238cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1524644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
15250598a293SJed Brown   (*dmc)->levelup   = dm->levelup;
1526656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1527b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1528b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1529b17ce1afSJed Brown   }
1530b17ce1afSJed Brown   PetscFunctionReturn(0);
1531b17ce1afSJed Brown }
1532b17ce1afSJed Brown 
1533b17ce1afSJed Brown #undef __FUNCT__
1534b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1535b17ce1afSJed Brown /*@
1536b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1537b17ce1afSJed Brown 
1538b17ce1afSJed Brown    Logically Collective
1539b17ce1afSJed Brown 
1540b17ce1afSJed Brown    Input Arguments:
1541b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1542b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1543b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1544b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1545b17ce1afSJed Brown 
1546b17ce1afSJed Brown    Calling sequence of coarsenhook:
1547b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1548b17ce1afSJed Brown 
1549b17ce1afSJed Brown +  fine - fine level DM
1550b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1551b17ce1afSJed Brown -  ctx - optional user-defined function context
1552b17ce1afSJed Brown 
1553b17ce1afSJed Brown    Calling sequence for restricthook:
1554c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1555b17ce1afSJed Brown 
1556b17ce1afSJed Brown +  fine - fine level DM
1557b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1558c833c3b5SJed Brown .  rscale - scaling vector for restriction
1559c833c3b5SJed Brown .  inject - matrix restricting by injection
1560b17ce1afSJed Brown .  coarse - coarse level DM to update
1561b17ce1afSJed Brown -  ctx - optional user-defined function context
1562b17ce1afSJed Brown 
1563b17ce1afSJed Brown    Level: advanced
1564b17ce1afSJed Brown 
1565b17ce1afSJed Brown    Notes:
1566b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1567b17ce1afSJed Brown 
1568b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1569b17ce1afSJed Brown 
1570b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1571b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1572b17ce1afSJed Brown 
1573c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1574b17ce1afSJed Brown @*/
1575b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1576b17ce1afSJed Brown {
1577b17ce1afSJed Brown   PetscErrorCode ierr;
1578b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1579b17ce1afSJed Brown 
1580b17ce1afSJed Brown   PetscFunctionBegin;
1581b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
15826bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1583b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1584b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1585b17ce1afSJed Brown   link->restricthook = restricthook;
1586b17ce1afSJed Brown   link->ctx = ctx;
15876cab3a1bSJed Brown   link->next = PETSC_NULL;
1588b17ce1afSJed Brown   *p = link;
1589b17ce1afSJed Brown   PetscFunctionReturn(0);
1590b17ce1afSJed Brown }
1591b17ce1afSJed Brown 
1592b17ce1afSJed Brown #undef __FUNCT__
1593b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1594b17ce1afSJed Brown /*@
1595b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1596b17ce1afSJed Brown 
1597b17ce1afSJed Brown    Collective if any hooks are
1598b17ce1afSJed Brown 
1599b17ce1afSJed Brown    Input Arguments:
1600b17ce1afSJed Brown +  fine - finer DM to use as a base
1601b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1602b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1603b17ce1afSJed Brown -  coarse - coarer DM to update
1604b17ce1afSJed Brown 
1605b17ce1afSJed Brown    Level: developer
1606b17ce1afSJed Brown 
1607b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1608b17ce1afSJed Brown @*/
1609b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1610b17ce1afSJed Brown {
1611b17ce1afSJed Brown   PetscErrorCode ierr;
1612b17ce1afSJed Brown   DMCoarsenHookLink link;
1613b17ce1afSJed Brown 
1614b17ce1afSJed Brown   PetscFunctionBegin;
1615b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1616b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1617b17ce1afSJed Brown   }
161847c6ae99SBarry Smith   PetscFunctionReturn(0);
161947c6ae99SBarry Smith }
162047c6ae99SBarry Smith 
162147c6ae99SBarry Smith #undef __FUNCT__
16225fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
16235fe1f584SPeter Brune /*@
16246a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
16255fe1f584SPeter Brune 
16265fe1f584SPeter Brune     Not Collective
16275fe1f584SPeter Brune 
16285fe1f584SPeter Brune     Input Parameter:
16295fe1f584SPeter Brune .   dm - the DM object
16305fe1f584SPeter Brune 
16315fe1f584SPeter Brune     Output Parameter:
16326a7d9d85SPeter Brune .   level - number of coarsenings
16335fe1f584SPeter Brune 
16345fe1f584SPeter Brune     Level: developer
16355fe1f584SPeter Brune 
16366a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
16375fe1f584SPeter Brune 
16385fe1f584SPeter Brune @*/
16395fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
16405fe1f584SPeter Brune {
16415fe1f584SPeter Brune   PetscFunctionBegin;
16425fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16435fe1f584SPeter Brune   *level = dm->leveldown;
16445fe1f584SPeter Brune   PetscFunctionReturn(0);
16455fe1f584SPeter Brune }
16465fe1f584SPeter Brune 
16475fe1f584SPeter Brune 
16485fe1f584SPeter Brune 
16495fe1f584SPeter Brune #undef __FUNCT__
165047c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
165147c6ae99SBarry Smith /*@C
165247c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
165347c6ae99SBarry Smith 
165447c6ae99SBarry Smith     Collective on DM
165547c6ae99SBarry Smith 
165647c6ae99SBarry Smith     Input Parameter:
165747c6ae99SBarry Smith +   dm - the DM object
165847c6ae99SBarry Smith -   nlevels - the number of levels of refinement
165947c6ae99SBarry Smith 
166047c6ae99SBarry Smith     Output Parameter:
166147c6ae99SBarry Smith .   dmf - the refined DM hierarchy
166247c6ae99SBarry Smith 
166347c6ae99SBarry Smith     Level: developer
166447c6ae99SBarry Smith 
1665e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
166647c6ae99SBarry Smith 
166747c6ae99SBarry Smith @*/
16687087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
166947c6ae99SBarry Smith {
167047c6ae99SBarry Smith   PetscErrorCode ierr;
167147c6ae99SBarry Smith 
167247c6ae99SBarry Smith   PetscFunctionBegin;
1673171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
167447c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
167547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
167647c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
167747c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
167847c6ae99SBarry Smith   } else if (dm->ops->refine) {
167947c6ae99SBarry Smith     PetscInt i;
168047c6ae99SBarry Smith 
168147c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
168247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
168347c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
168447c6ae99SBarry Smith     }
168547c6ae99SBarry Smith   } else {
168647c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
168747c6ae99SBarry Smith   }
168847c6ae99SBarry Smith   PetscFunctionReturn(0);
168947c6ae99SBarry Smith }
169047c6ae99SBarry Smith 
169147c6ae99SBarry Smith #undef __FUNCT__
169247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
169347c6ae99SBarry Smith /*@C
169447c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
169547c6ae99SBarry Smith 
169647c6ae99SBarry Smith     Collective on DM
169747c6ae99SBarry Smith 
169847c6ae99SBarry Smith     Input Parameter:
169947c6ae99SBarry Smith +   dm - the DM object
170047c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
170147c6ae99SBarry Smith 
170247c6ae99SBarry Smith     Output Parameter:
170347c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
170447c6ae99SBarry Smith 
170547c6ae99SBarry Smith     Level: developer
170647c6ae99SBarry Smith 
1707e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
170847c6ae99SBarry Smith 
170947c6ae99SBarry Smith @*/
17107087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
171147c6ae99SBarry Smith {
171247c6ae99SBarry Smith   PetscErrorCode ierr;
171347c6ae99SBarry Smith 
171447c6ae99SBarry Smith   PetscFunctionBegin;
1715171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
171647c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
171747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
171847c6ae99SBarry Smith   PetscValidPointer(dmc,3);
171947c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
172047c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
172147c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
172247c6ae99SBarry Smith     PetscInt i;
172347c6ae99SBarry Smith 
172447c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
172547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
172647c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
172747c6ae99SBarry Smith     }
172847c6ae99SBarry Smith   } else {
172947c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
173047c6ae99SBarry Smith   }
173147c6ae99SBarry Smith   PetscFunctionReturn(0);
173247c6ae99SBarry Smith }
173347c6ae99SBarry Smith 
173447c6ae99SBarry Smith #undef __FUNCT__
1735e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
173647c6ae99SBarry Smith /*@
1737e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
173847c6ae99SBarry Smith    grids associated with two DMs.
173947c6ae99SBarry Smith 
174047c6ae99SBarry Smith    Collective on DM
174147c6ae99SBarry Smith 
174247c6ae99SBarry Smith    Input Parameters:
174347c6ae99SBarry Smith +  dmc - the coarse grid DM
174447c6ae99SBarry Smith -  dmf - the fine grid DM
174547c6ae99SBarry Smith 
174647c6ae99SBarry Smith    Output Parameters:
174747c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
174847c6ae99SBarry Smith 
174947c6ae99SBarry Smith    Level: intermediate
175047c6ae99SBarry Smith 
175147c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
175247c6ae99SBarry Smith 
1753e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
175447c6ae99SBarry Smith @*/
1755e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
175647c6ae99SBarry Smith {
175747c6ae99SBarry Smith   PetscErrorCode ierr;
175847c6ae99SBarry Smith 
175947c6ae99SBarry Smith   PetscFunctionBegin;
1760171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1761171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
176247c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
176347c6ae99SBarry Smith   PetscFunctionReturn(0);
176447c6ae99SBarry Smith }
176547c6ae99SBarry Smith 
176647c6ae99SBarry Smith #undef __FUNCT__
17671a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
17681a266240SBarry Smith /*@C
17691a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
17701a266240SBarry Smith 
17711a266240SBarry Smith     Not Collective
17721a266240SBarry Smith 
17731a266240SBarry Smith     Input Parameters:
17741a266240SBarry Smith +   dm - the DM object
17751a266240SBarry Smith -   destroy - the destroy function
17761a266240SBarry Smith 
17771a266240SBarry Smith     Level: intermediate
17781a266240SBarry Smith 
1779e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
17801a266240SBarry Smith 
1781f07f9ceaSJed Brown @*/
17821a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
17831a266240SBarry Smith {
17841a266240SBarry Smith   PetscFunctionBegin;
1785171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17861a266240SBarry Smith   dm->ctxdestroy = destroy;
17871a266240SBarry Smith   PetscFunctionReturn(0);
17881a266240SBarry Smith }
17891a266240SBarry Smith 
17901a266240SBarry Smith #undef __FUNCT__
17911b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1792b07ff414SBarry Smith /*@
17931b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
179447c6ae99SBarry Smith 
179547c6ae99SBarry Smith     Not Collective
179647c6ae99SBarry Smith 
179747c6ae99SBarry Smith     Input Parameters:
179847c6ae99SBarry Smith +   dm - the DM object
179947c6ae99SBarry Smith -   ctx - the user context
180047c6ae99SBarry Smith 
180147c6ae99SBarry Smith     Level: intermediate
180247c6ae99SBarry Smith 
1803e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
180447c6ae99SBarry Smith 
180547c6ae99SBarry Smith @*/
18061b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
180747c6ae99SBarry Smith {
180847c6ae99SBarry Smith   PetscFunctionBegin;
1809171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
181047c6ae99SBarry Smith   dm->ctx = ctx;
181147c6ae99SBarry Smith   PetscFunctionReturn(0);
181247c6ae99SBarry Smith }
181347c6ae99SBarry Smith 
181447c6ae99SBarry Smith #undef __FUNCT__
18151b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
181647c6ae99SBarry Smith /*@
18171b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
181847c6ae99SBarry Smith 
181947c6ae99SBarry Smith     Not Collective
182047c6ae99SBarry Smith 
182147c6ae99SBarry Smith     Input Parameter:
182247c6ae99SBarry Smith .   dm - the DM object
182347c6ae99SBarry Smith 
182447c6ae99SBarry Smith     Output Parameter:
182547c6ae99SBarry Smith .   ctx - the user context
182647c6ae99SBarry Smith 
182747c6ae99SBarry Smith     Level: intermediate
182847c6ae99SBarry Smith 
1829e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
183047c6ae99SBarry Smith 
183147c6ae99SBarry Smith @*/
18321b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
183347c6ae99SBarry Smith {
183447c6ae99SBarry Smith   PetscFunctionBegin;
1835171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18361b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
183747c6ae99SBarry Smith   PetscFunctionReturn(0);
183847c6ae99SBarry Smith }
183947c6ae99SBarry Smith 
184047c6ae99SBarry Smith #undef __FUNCT__
184147c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
18427e833e3aSBarry Smith /*@C
184347c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
184447c6ae99SBarry Smith 
184547c6ae99SBarry Smith     Logically Collective on DM
184647c6ae99SBarry Smith 
184747c6ae99SBarry Smith     Input Parameter:
184847c6ae99SBarry Smith +   dm - the DM object to destroy
184947c6ae99SBarry Smith -   f - the function to compute the initial guess
185047c6ae99SBarry Smith 
185147c6ae99SBarry Smith     Level: intermediate
185247c6ae99SBarry Smith 
1853e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
185447c6ae99SBarry Smith 
1855f07f9ceaSJed Brown @*/
18567087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
185747c6ae99SBarry Smith {
185847c6ae99SBarry Smith   PetscFunctionBegin;
1859171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
186047c6ae99SBarry Smith   dm->ops->initialguess = f;
186147c6ae99SBarry Smith   PetscFunctionReturn(0);
186247c6ae99SBarry Smith }
186347c6ae99SBarry Smith 
186447c6ae99SBarry Smith #undef __FUNCT__
186547c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
18667e833e3aSBarry Smith /*@C
186747c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
186847c6ae99SBarry Smith 
186947c6ae99SBarry Smith     Logically Collective on DM
187047c6ae99SBarry Smith 
187147c6ae99SBarry Smith     Input Parameter:
187247c6ae99SBarry Smith +   dm - the DM object
187347c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
187447c6ae99SBarry Smith 
187547c6ae99SBarry Smith     Level: intermediate
187647c6ae99SBarry Smith 
187747c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
187847c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
187947c6ae99SBarry Smith 
1880e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
188147c6ae99SBarry Smith          DMSetJacobian()
188247c6ae99SBarry Smith 
1883f07f9ceaSJed Brown @*/
18847087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
188547c6ae99SBarry Smith {
188647c6ae99SBarry Smith   PetscFunctionBegin;
1887171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
188847c6ae99SBarry Smith   dm->ops->function = f;
188947c6ae99SBarry Smith   if (f) {
189047c6ae99SBarry Smith     dm->ops->functionj = f;
189147c6ae99SBarry Smith   }
189247c6ae99SBarry Smith   PetscFunctionReturn(0);
189347c6ae99SBarry Smith }
189447c6ae99SBarry Smith 
189547c6ae99SBarry Smith #undef __FUNCT__
189647c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
18977e833e3aSBarry Smith /*@C
189847c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
189947c6ae99SBarry Smith 
190047c6ae99SBarry Smith     Logically Collective on DM
190147c6ae99SBarry Smith 
190247c6ae99SBarry Smith     Input Parameter:
190347c6ae99SBarry Smith +   dm - the DM object to destroy
190447c6ae99SBarry Smith -   f - the function to compute the matrix entries
190547c6ae99SBarry Smith 
190647c6ae99SBarry Smith     Level: intermediate
190747c6ae99SBarry Smith 
1908e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
190947c6ae99SBarry Smith          DMSetFunction()
191047c6ae99SBarry Smith 
1911f07f9ceaSJed Brown @*/
19127087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
191347c6ae99SBarry Smith {
191447c6ae99SBarry Smith   PetscFunctionBegin;
1915171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
191647c6ae99SBarry Smith   dm->ops->jacobian = f;
191747c6ae99SBarry Smith   PetscFunctionReturn(0);
191847c6ae99SBarry Smith }
191947c6ae99SBarry Smith 
192047c6ae99SBarry Smith #undef __FUNCT__
192108da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
192208da532bSDmitry Karpeev /*@C
192308da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
192408da532bSDmitry Karpeev 
192508da532bSDmitry Karpeev     Logically Collective on DM
192608da532bSDmitry Karpeev 
192708da532bSDmitry Karpeev     Input Parameter:
192808da532bSDmitry Karpeev +   dm - the DM object
192908da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
193008da532bSDmitry Karpeev 
193108da532bSDmitry Karpeev     Level: intermediate
193208da532bSDmitry Karpeev 
1933e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
193408da532bSDmitry Karpeev          DMSetJacobian()
193508da532bSDmitry Karpeev 
193608da532bSDmitry Karpeev @*/
193708da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
193808da532bSDmitry Karpeev {
193908da532bSDmitry Karpeev   PetscFunctionBegin;
194008da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
194108da532bSDmitry Karpeev   PetscFunctionReturn(0);
194208da532bSDmitry Karpeev }
194308da532bSDmitry Karpeev 
194408da532bSDmitry Karpeev #undef __FUNCT__
194508da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
194608da532bSDmitry Karpeev /*@
194708da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
194808da532bSDmitry Karpeev 
194908da532bSDmitry Karpeev     Not Collective
195008da532bSDmitry Karpeev 
195108da532bSDmitry Karpeev     Input Parameter:
195208da532bSDmitry Karpeev .   dm - the DM object to destroy
195308da532bSDmitry Karpeev 
195408da532bSDmitry Karpeev     Output Parameter:
195508da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
195608da532bSDmitry Karpeev 
195708da532bSDmitry Karpeev     Level: developer
195808da532bSDmitry Karpeev 
1959e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
196008da532bSDmitry Karpeev 
196108da532bSDmitry Karpeev @*/
196208da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
196308da532bSDmitry Karpeev {
196408da532bSDmitry Karpeev   PetscFunctionBegin;
196508da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
196608da532bSDmitry Karpeev   PetscFunctionReturn(0);
196708da532bSDmitry Karpeev }
196808da532bSDmitry Karpeev 
196908da532bSDmitry Karpeev #undef __FUNCT__
197008da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
197108da532bSDmitry Karpeev /*@C
197208da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
197308da532bSDmitry Karpeev 
197408da532bSDmitry Karpeev     Logically Collective on DM
197508da532bSDmitry Karpeev 
197608da532bSDmitry Karpeev     Input Parameters:
197708da532bSDmitry Karpeev +   dm - the DM object to destroy
197808da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
197908da532bSDmitry Karpeev 
198008da532bSDmitry Karpeev     Output parameters:
198108da532bSDmitry Karpeev +   xl - lower bound
198208da532bSDmitry Karpeev -   xu - upper bound
198308da532bSDmitry Karpeev 
198408da532bSDmitry Karpeev     Level: intermediate
198508da532bSDmitry Karpeev 
1986e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
198708da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
198808da532bSDmitry Karpeev 
198908da532bSDmitry Karpeev @*/
199008da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
199108da532bSDmitry Karpeev {
199208da532bSDmitry Karpeev   PetscErrorCode ierr;
199308da532bSDmitry Karpeev   PetscFunctionBegin;
199408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
199508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
199608da532bSDmitry Karpeev   if(dm->ops->computevariablebounds) {
199708da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
199808da532bSDmitry Karpeev   }
199908da532bSDmitry Karpeev   else {
200008da532bSDmitry Karpeev     ierr = VecSet(xl,SNES_VI_NINF); CHKERRQ(ierr);
200108da532bSDmitry Karpeev     ierr = VecSet(xu,SNES_VI_INF);  CHKERRQ(ierr);
200208da532bSDmitry Karpeev   }
200308da532bSDmitry Karpeev   PetscFunctionReturn(0);
200408da532bSDmitry Karpeev }
200508da532bSDmitry Karpeev 
200608da532bSDmitry Karpeev #undef __FUNCT__
200747c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
200847c6ae99SBarry Smith /*@
200947c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
201047c6ae99SBarry Smith 
201147c6ae99SBarry Smith     Collective on DM
201247c6ae99SBarry Smith 
201347c6ae99SBarry Smith     Input Parameter:
201447c6ae99SBarry Smith +   dm - the DM object to destroy
201547c6ae99SBarry Smith -   x - the vector to hold the initial guess values
201647c6ae99SBarry Smith 
201747c6ae99SBarry Smith     Level: developer
201847c6ae99SBarry Smith 
2019e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
202047c6ae99SBarry Smith 
202147c6ae99SBarry Smith @*/
20227087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
202347c6ae99SBarry Smith {
202447c6ae99SBarry Smith   PetscErrorCode ierr;
202547c6ae99SBarry Smith   PetscFunctionBegin;
2026171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
202747c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
202847c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
202947c6ae99SBarry Smith   PetscFunctionReturn(0);
203047c6ae99SBarry Smith }
203147c6ae99SBarry Smith 
203247c6ae99SBarry Smith #undef __FUNCT__
203347c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
203447c6ae99SBarry Smith /*@
203547c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
203647c6ae99SBarry Smith 
203747c6ae99SBarry Smith     Not Collective
203847c6ae99SBarry Smith 
203947c6ae99SBarry Smith     Input Parameter:
204047c6ae99SBarry Smith .   dm - the DM object to destroy
204147c6ae99SBarry Smith 
204247c6ae99SBarry Smith     Output Parameter:
204347c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
204447c6ae99SBarry Smith 
204547c6ae99SBarry Smith     Level: developer
204647c6ae99SBarry Smith 
2047e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
204847c6ae99SBarry Smith 
204947c6ae99SBarry Smith @*/
20507087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
205147c6ae99SBarry Smith {
205247c6ae99SBarry Smith   PetscFunctionBegin;
2053171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
205447c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
205547c6ae99SBarry Smith   PetscFunctionReturn(0);
205647c6ae99SBarry Smith }
205747c6ae99SBarry Smith 
205847c6ae99SBarry Smith #undef __FUNCT__
205947c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
206047c6ae99SBarry Smith /*@
206147c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
206247c6ae99SBarry Smith 
206347c6ae99SBarry Smith     Not Collective
206447c6ae99SBarry Smith 
206547c6ae99SBarry Smith     Input Parameter:
206647c6ae99SBarry Smith .   dm - the DM object to destroy
206747c6ae99SBarry Smith 
206847c6ae99SBarry Smith     Output Parameter:
206947c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
207047c6ae99SBarry Smith 
207147c6ae99SBarry Smith     Level: developer
207247c6ae99SBarry Smith 
2073e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
207447c6ae99SBarry Smith 
207547c6ae99SBarry Smith @*/
20767087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
207747c6ae99SBarry Smith {
207847c6ae99SBarry Smith   PetscFunctionBegin;
2079171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
208047c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
208147c6ae99SBarry Smith   PetscFunctionReturn(0);
208247c6ae99SBarry Smith }
208347c6ae99SBarry Smith 
208447c6ae99SBarry Smith #undef __FUNCT__
208547c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
208647c6ae99SBarry Smith /*@
208747c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
208847c6ae99SBarry Smith 
208947c6ae99SBarry Smith     Not Collective
209047c6ae99SBarry Smith 
209147c6ae99SBarry Smith     Input Parameter:
209247c6ae99SBarry Smith .   dm - the DM object to destroy
209347c6ae99SBarry Smith 
209447c6ae99SBarry Smith     Output Parameter:
209547c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
209647c6ae99SBarry Smith 
209747c6ae99SBarry Smith     Level: developer
209847c6ae99SBarry Smith 
2099e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
210047c6ae99SBarry Smith 
210147c6ae99SBarry Smith @*/
21027087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
210347c6ae99SBarry Smith {
210447c6ae99SBarry Smith   PetscFunctionBegin;
2105171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
210647c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
210747c6ae99SBarry Smith   PetscFunctionReturn(0);
210847c6ae99SBarry Smith }
210947c6ae99SBarry Smith 
211047c6ae99SBarry Smith #undef  __FUNCT__
211108da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2112748fac09SDmitry Karpeev /*@C
211308da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
211408da532bSDmitry Karpeev 
211508da532bSDmitry Karpeev     Collective on DM
211608da532bSDmitry Karpeev 
211708da532bSDmitry Karpeev     Input Parameter:
211808da532bSDmitry Karpeev +   dm - the DM object
2119e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
212008da532bSDmitry Karpeev 
212108da532bSDmitry Karpeev     Level: developer
212208da532bSDmitry Karpeev 
2123e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
212408da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
212508da532bSDmitry Karpeev 
212608da532bSDmitry Karpeev @*/
212708da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
212808da532bSDmitry Karpeev {
212908da532bSDmitry Karpeev   PetscErrorCode ierr;
213008da532bSDmitry Karpeev   PetscFunctionBegin;
213108da532bSDmitry Karpeev   if (x) {
213208da532bSDmitry Karpeev     if (!dm->x) {
213308da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
213408da532bSDmitry Karpeev     }
213508da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
213608da532bSDmitry Karpeev   }
213708da532bSDmitry Karpeev   else if(dm->x) {
213808da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
213908da532bSDmitry Karpeev   }
214008da532bSDmitry Karpeev   PetscFunctionReturn(0);
214108da532bSDmitry Karpeev }
214208da532bSDmitry Karpeev 
214308da532bSDmitry Karpeev 
214408da532bSDmitry Karpeev #undef __FUNCT__
214547c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
214647c6ae99SBarry Smith /*@
214747c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
214847c6ae99SBarry Smith 
214947c6ae99SBarry Smith     Collective on DM
215047c6ae99SBarry Smith 
215147c6ae99SBarry Smith     Input Parameter:
215247c6ae99SBarry Smith +   dm - the DM object to destroy
215347c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
215447c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
215547c6ae99SBarry Smith 
215647c6ae99SBarry Smith     Level: developer
215747c6ae99SBarry Smith 
2158e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
215947c6ae99SBarry Smith          DMSetJacobian()
216047c6ae99SBarry Smith 
216147c6ae99SBarry Smith @*/
21627087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
216347c6ae99SBarry Smith {
216447c6ae99SBarry Smith   PetscErrorCode ierr;
216547c6ae99SBarry Smith   PetscFunctionBegin;
2166171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
216747c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
2168644e2e5bSBarry Smith   PetscStackPush("DM user function");
216947c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
2170644e2e5bSBarry Smith   PetscStackPop;
217147c6ae99SBarry Smith   PetscFunctionReturn(0);
217247c6ae99SBarry Smith }
217347c6ae99SBarry Smith 
217447c6ae99SBarry Smith 
217508da532bSDmitry Karpeev 
217647c6ae99SBarry Smith #undef __FUNCT__
217747c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
217847c6ae99SBarry Smith /*@
217947c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
218047c6ae99SBarry Smith 
218147c6ae99SBarry Smith     Collective on DM
218247c6ae99SBarry Smith 
218347c6ae99SBarry Smith     Input Parameter:
218447c6ae99SBarry Smith +   dm - the DM object
2185cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
218647c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
218747c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
218847c6ae99SBarry Smith 
218947c6ae99SBarry Smith     Level: developer
219047c6ae99SBarry Smith 
2191e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
219247c6ae99SBarry Smith          DMSetFunction()
219347c6ae99SBarry Smith 
219447c6ae99SBarry Smith @*/
21957087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
219647c6ae99SBarry Smith {
219747c6ae99SBarry Smith   PetscErrorCode ierr;
219847c6ae99SBarry Smith 
219947c6ae99SBarry Smith   PetscFunctionBegin;
2200171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
220147c6ae99SBarry Smith   if (!dm->ops->jacobian) {
220247c6ae99SBarry Smith     ISColoring     coloring;
220347c6ae99SBarry Smith     MatFDColoring  fd;
22042c9966d7SBarry Smith     const MatType  mtype;
220547c6ae99SBarry Smith 
22062c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
22072c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
220847c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
2209fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
221047c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
22110bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
22120bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
221371cd77b2SBarry Smith 
221447c6ae99SBarry Smith     dm->fd = fd;
221547c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
22162533e041SBarry Smith 
221771cd77b2SBarry Smith     /* don't know why this is needed */
221871cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
221947c6ae99SBarry Smith   }
222047c6ae99SBarry Smith   if (!x) x = dm->x;
222147c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
2222cab2e9ccSBarry Smith 
222371cd77b2SBarry 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 */
2224649052a6SBarry Smith   if (x) {
2225cab2e9ccSBarry Smith     if (!dm->x) {
222671cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
2227cab2e9ccSBarry Smith     }
2228cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
2229649052a6SBarry Smith   }
2230a8248277SBarry Smith   if (A != B) {
2231a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2232a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2233a8248277SBarry Smith   }
223447c6ae99SBarry Smith   PetscFunctionReturn(0);
223547c6ae99SBarry Smith }
2236264ace61SBarry Smith 
2237cab2e9ccSBarry Smith 
2238264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2239264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2240264ace61SBarry Smith 
2241264ace61SBarry Smith #undef __FUNCT__
2242264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2243264ace61SBarry Smith /*@C
2244264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2245264ace61SBarry Smith 
2246264ace61SBarry Smith   Collective on DM
2247264ace61SBarry Smith 
2248264ace61SBarry Smith   Input Parameters:
2249264ace61SBarry Smith + dm     - The DM object
2250264ace61SBarry Smith - method - The name of the DM type
2251264ace61SBarry Smith 
2252264ace61SBarry Smith   Options Database Key:
2253264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2254264ace61SBarry Smith 
2255264ace61SBarry Smith   Notes:
2256e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2257264ace61SBarry Smith 
2258264ace61SBarry Smith   Level: intermediate
2259264ace61SBarry Smith 
2260264ace61SBarry Smith .keywords: DM, set, type
2261264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2262264ace61SBarry Smith @*/
22637087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
2264264ace61SBarry Smith {
2265264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2266264ace61SBarry Smith   PetscBool      match;
2267264ace61SBarry Smith   PetscErrorCode ierr;
2268264ace61SBarry Smith 
2269264ace61SBarry Smith   PetscFunctionBegin;
2270264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2271251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2272264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2273264ace61SBarry Smith 
2274264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
22754b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2276264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2277264ace61SBarry Smith 
2278264ace61SBarry Smith   if (dm->ops->destroy) {
2279264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2280b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2281264ace61SBarry Smith   }
2282264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2283264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2284264ace61SBarry Smith   PetscFunctionReturn(0);
2285264ace61SBarry Smith }
2286264ace61SBarry Smith 
2287264ace61SBarry Smith #undef __FUNCT__
2288264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2289264ace61SBarry Smith /*@C
2290264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2291264ace61SBarry Smith 
2292264ace61SBarry Smith   Not Collective
2293264ace61SBarry Smith 
2294264ace61SBarry Smith   Input Parameter:
2295264ace61SBarry Smith . dm  - The DM
2296264ace61SBarry Smith 
2297264ace61SBarry Smith   Output Parameter:
2298264ace61SBarry Smith . type - The DM type name
2299264ace61SBarry Smith 
2300264ace61SBarry Smith   Level: intermediate
2301264ace61SBarry Smith 
2302264ace61SBarry Smith .keywords: DM, get, type, name
2303264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2304264ace61SBarry Smith @*/
23057087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
2306264ace61SBarry Smith {
2307264ace61SBarry Smith   PetscErrorCode ierr;
2308264ace61SBarry Smith 
2309264ace61SBarry Smith   PetscFunctionBegin;
2310264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2311264ace61SBarry Smith   PetscValidCharPointer(type,2);
2312264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2313264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2314264ace61SBarry Smith   }
2315264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2316264ace61SBarry Smith   PetscFunctionReturn(0);
2317264ace61SBarry Smith }
2318264ace61SBarry Smith 
231967a56275SMatthew G Knepley #undef __FUNCT__
232067a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
232167a56275SMatthew G Knepley /*@C
232267a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
232367a56275SMatthew G Knepley 
232467a56275SMatthew G Knepley   Collective on DM
232567a56275SMatthew G Knepley 
232667a56275SMatthew G Knepley   Input Parameters:
232767a56275SMatthew G Knepley + dm - the DM
232867a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
232967a56275SMatthew G Knepley 
233067a56275SMatthew G Knepley   Output Parameter:
233167a56275SMatthew G Knepley . M - pointer to new DM
233267a56275SMatthew G Knepley 
233367a56275SMatthew G Knepley   Notes:
233467a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
233567a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
233667a56275SMatthew G Knepley   of the input DM.
233767a56275SMatthew G Knepley 
233867a56275SMatthew G Knepley   Level: intermediate
233967a56275SMatthew G Knepley 
234067a56275SMatthew G Knepley .seealso: DMCreate()
234167a56275SMatthew G Knepley @*/
234267a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
234367a56275SMatthew G Knepley {
234467a56275SMatthew G Knepley   DM             B;
234567a56275SMatthew G Knepley   char           convname[256];
234667a56275SMatthew G Knepley   PetscBool      sametype, issame;
234767a56275SMatthew G Knepley   PetscErrorCode ierr;
234867a56275SMatthew G Knepley 
234967a56275SMatthew G Knepley   PetscFunctionBegin;
235067a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
235167a56275SMatthew G Knepley   PetscValidType(dm,1);
235267a56275SMatthew G Knepley   PetscValidPointer(M,3);
2353251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
235467a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
235567a56275SMatthew G Knepley   {
235667a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
235767a56275SMatthew G Knepley 
235867a56275SMatthew G Knepley     /*
235967a56275SMatthew G Knepley        Order of precedence:
236067a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
236167a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
236267a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
236367a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
236467a56275SMatthew G Knepley        5) Use a really basic converter.
236567a56275SMatthew G Knepley     */
236667a56275SMatthew G Knepley 
236767a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
236867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
236967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
237067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
237167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
237267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
237367a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
237467a56275SMatthew G Knepley     if (conv) goto foundconv;
237567a56275SMatthew G Knepley 
237667a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
237767a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
237867a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
237967a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
238067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
238167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
238267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
238367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
238467a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
238567a56275SMatthew G Knepley     if (conv) {
2386fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
238767a56275SMatthew G Knepley       goto foundconv;
238867a56275SMatthew G Knepley     }
238967a56275SMatthew G Knepley 
239067a56275SMatthew G Knepley #if 0
239167a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
239267a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2393fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
239467a56275SMatthew G Knepley     if (conv) goto foundconv;
239567a56275SMatthew G Knepley 
239667a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
239767a56275SMatthew G Knepley     if (dm->ops->convert) {
239867a56275SMatthew G Knepley       conv = dm->ops->convert;
239967a56275SMatthew G Knepley     }
240067a56275SMatthew G Knepley     if (conv) goto foundconv;
240167a56275SMatthew G Knepley #endif
240267a56275SMatthew G Knepley 
240367a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
240467a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
240567a56275SMatthew G Knepley 
240667a56275SMatthew G Knepley     foundconv:
240767a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
240867a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
240967a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
241067a56275SMatthew G Knepley   }
241167a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
241267a56275SMatthew G Knepley   PetscFunctionReturn(0);
241367a56275SMatthew G Knepley }
2414264ace61SBarry Smith 
2415264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2416264ace61SBarry Smith 
2417264ace61SBarry Smith #undef __FUNCT__
2418264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2419264ace61SBarry Smith /*@C
2420264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2421264ace61SBarry Smith 
2422264ace61SBarry Smith   Level: advanced
2423264ace61SBarry Smith @*/
24247087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2425264ace61SBarry Smith {
2426264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2427264ace61SBarry Smith   PetscErrorCode ierr;
2428264ace61SBarry Smith 
2429264ace61SBarry Smith   PetscFunctionBegin;
2430264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2431264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2432264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2433264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2434264ace61SBarry Smith   PetscFunctionReturn(0);
2435264ace61SBarry Smith }
2436264ace61SBarry Smith 
2437264ace61SBarry Smith 
2438264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2439264ace61SBarry Smith #undef __FUNCT__
2440264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2441264ace61SBarry Smith /*@C
2442264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2443264ace61SBarry Smith 
2444264ace61SBarry Smith    Not Collective
2445264ace61SBarry Smith 
2446264ace61SBarry Smith    Level: advanced
2447264ace61SBarry Smith 
2448264ace61SBarry Smith .keywords: DM, register, destroy
2449264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2450264ace61SBarry Smith @*/
24517087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2452264ace61SBarry Smith {
2453264ace61SBarry Smith   PetscErrorCode ierr;
2454264ace61SBarry Smith 
2455264ace61SBarry Smith   PetscFunctionBegin;
2456264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2457264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2458264ace61SBarry Smith   PetscFunctionReturn(0);
2459264ace61SBarry Smith }
246023f975d1SBarry Smith 
246123f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2462c6db04a5SJed Brown #include <mex.h>
246323f975d1SBarry Smith 
24643014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
246523f975d1SBarry Smith 
246623f975d1SBarry Smith #undef __FUNCT__
246723f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
246823f975d1SBarry Smith /*
246923f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
247023f975d1SBarry Smith                          DMSetFunctionMatlab().
247123f975d1SBarry Smith 
247223f975d1SBarry Smith    For linear problems x is null
247323f975d1SBarry Smith 
247423f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
247523f975d1SBarry Smith */
24767087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
247723f975d1SBarry Smith {
247823f975d1SBarry Smith   PetscErrorCode    ierr;
247923f975d1SBarry Smith   DMMatlabContext   *sctx;
248023f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
248123f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
248223f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
248323f975d1SBarry Smith 
248423f975d1SBarry Smith   PetscFunctionBegin;
248523f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
248623f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
248723f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
248823f975d1SBarry Smith 
248923f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
24901b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
249123f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
249223f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
24933014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
249423f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
249523f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
249623f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
249723f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2498b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
249923f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
250023f975d1SBarry Smith   mxDestroyArray(prhs[0]);
250123f975d1SBarry Smith   mxDestroyArray(prhs[1]);
250223f975d1SBarry Smith   mxDestroyArray(prhs[2]);
250323f975d1SBarry Smith   mxDestroyArray(prhs[3]);
250423f975d1SBarry Smith   mxDestroyArray(plhs[0]);
250523f975d1SBarry Smith   PetscFunctionReturn(0);
250623f975d1SBarry Smith }
250723f975d1SBarry Smith 
250823f975d1SBarry Smith 
250923f975d1SBarry Smith #undef __FUNCT__
251023f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
251123f975d1SBarry Smith /*
251223f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
251323f975d1SBarry Smith 
251423f975d1SBarry Smith */
25157087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
251623f975d1SBarry Smith {
251723f975d1SBarry Smith   PetscErrorCode    ierr;
251823f975d1SBarry Smith   DMMatlabContext   *sctx;
251923f975d1SBarry Smith 
252023f975d1SBarry Smith   PetscFunctionBegin;
2521171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
252223f975d1SBarry Smith   /* currently sctx is memory bleed */
25231b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
25243014e516SBarry Smith   if (!sctx) {
252523f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
25263014e516SBarry Smith   }
252723f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
25281b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
252923f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
253023f975d1SBarry Smith   PetscFunctionReturn(0);
253123f975d1SBarry Smith }
25323014e516SBarry Smith 
25333014e516SBarry Smith #undef __FUNCT__
25343014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
25353014e516SBarry Smith /*
25363014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
25373014e516SBarry Smith                          DMSetJacobianMatlab().
25383014e516SBarry Smith 
25393014e516SBarry Smith    For linear problems x is null
25403014e516SBarry Smith 
25413014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
25423014e516SBarry Smith */
25437087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
25443014e516SBarry Smith {
25453014e516SBarry Smith   PetscErrorCode    ierr;
25463014e516SBarry Smith   DMMatlabContext   *sctx;
25473014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
25483014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
25493014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
25503014e516SBarry Smith 
25513014e516SBarry Smith   PetscFunctionBegin;
25523014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25533014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
25543014e516SBarry Smith 
2555e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
25561b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
25573014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
25583014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
25593014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
25603014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
25613014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
25623014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
25633014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
25643014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
25653014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2566b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2567c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2568c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
25693014e516SBarry Smith   mxDestroyArray(prhs[0]);
25703014e516SBarry Smith   mxDestroyArray(prhs[1]);
25713014e516SBarry Smith   mxDestroyArray(prhs[2]);
25723014e516SBarry Smith   mxDestroyArray(prhs[3]);
25733014e516SBarry Smith   mxDestroyArray(prhs[4]);
25743014e516SBarry Smith   mxDestroyArray(plhs[0]);
25753014e516SBarry Smith   mxDestroyArray(plhs[1]);
25763014e516SBarry Smith   PetscFunctionReturn(0);
25773014e516SBarry Smith }
25783014e516SBarry Smith 
25793014e516SBarry Smith 
25803014e516SBarry Smith #undef __FUNCT__
25813014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
25823014e516SBarry Smith /*
25833014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
25843014e516SBarry Smith 
25853014e516SBarry Smith */
25867087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
25873014e516SBarry Smith {
25883014e516SBarry Smith   PetscErrorCode    ierr;
25893014e516SBarry Smith   DMMatlabContext   *sctx;
25903014e516SBarry Smith 
25913014e516SBarry Smith   PetscFunctionBegin;
2592171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25933014e516SBarry Smith   /* currently sctx is memory bleed */
25941b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
25953014e516SBarry Smith   if (!sctx) {
25963014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
25973014e516SBarry Smith   }
25983014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
25991b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
26003014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
26013014e516SBarry Smith   PetscFunctionReturn(0);
26023014e516SBarry Smith }
260323f975d1SBarry Smith #endif
2604b859378eSBarry Smith 
2605b859378eSBarry Smith #undef __FUNCT__
2606b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2607b859378eSBarry Smith /*@C
2608b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2609b859378eSBarry Smith   with DMView().
2610b859378eSBarry Smith 
2611b859378eSBarry Smith   Collective on PetscViewer
2612b859378eSBarry Smith 
2613b859378eSBarry Smith   Input Parameters:
2614b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2615b859378eSBarry Smith            some related function before a call to DMLoad().
2616b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2617b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2618b859378eSBarry Smith 
2619b859378eSBarry Smith    Level: intermediate
2620b859378eSBarry Smith 
2621b859378eSBarry Smith   Notes:
2622b859378eSBarry Smith   Defaults to the DM DA.
2623b859378eSBarry Smith 
2624b859378eSBarry Smith   Notes for advanced users:
2625b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2626b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2627b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2628b859378eSBarry Smith   format is
2629b859378eSBarry Smith .vb
2630b859378eSBarry Smith      has not yet been determined
2631b859378eSBarry Smith .ve
2632b859378eSBarry Smith 
2633b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2634b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2635b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2636b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2637b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2638b859378eSBarry Smith 
2639b859378eSBarry Smith   Concepts: vector^loading from file
2640b859378eSBarry Smith 
2641b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2642b859378eSBarry Smith @*/
2643b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2644b859378eSBarry Smith {
2645b859378eSBarry Smith   PetscErrorCode ierr;
2646b859378eSBarry Smith 
2647b859378eSBarry Smith   PetscFunctionBegin;
2648b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2649b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2650b859378eSBarry Smith 
2651b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2652b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2653b859378eSBarry Smith   }
2654b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2655b859378eSBarry Smith   PetscFunctionReturn(0);
2656b859378eSBarry Smith }
2657b859378eSBarry Smith 
26587da65231SMatthew G Knepley /******************************** FEM Support **********************************/
26597da65231SMatthew G Knepley 
26607da65231SMatthew G Knepley #undef __FUNCT__
26617da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
26627da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
26631d47ebbbSSatish Balay   PetscInt       f;
26641b30c384SMatthew G Knepley   PetscErrorCode ierr;
26651b30c384SMatthew G Knepley 
26667da65231SMatthew G Knepley   PetscFunctionBegin;
266774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26681d47ebbbSSatish Balay   for(f = 0; f < len; ++f) {
266974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
26707da65231SMatthew G Knepley   }
26717da65231SMatthew G Knepley   PetscFunctionReturn(0);
26727da65231SMatthew G Knepley }
26737da65231SMatthew G Knepley 
26747da65231SMatthew G Knepley #undef __FUNCT__
26757da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
26767da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
26771b30c384SMatthew G Knepley   PetscInt       f, g;
26787da65231SMatthew G Knepley   PetscErrorCode ierr;
26797da65231SMatthew G Knepley 
26807da65231SMatthew G Knepley   PetscFunctionBegin;
268174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26821d47ebbbSSatish Balay   for(f = 0; f < rows; ++f) {
268374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
26841d47ebbbSSatish Balay     for(g = 0; g < cols; ++g) {
268574778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
26867da65231SMatthew G Knepley     }
268774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
26887da65231SMatthew G Knepley   }
26897da65231SMatthew G Knepley   PetscFunctionReturn(0);
26907da65231SMatthew G Knepley }
2691e7c4fc90SDmitry Karpeev 
2692970e74d5SMatthew G Knepley #undef __FUNCT__
2693970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
2694970e74d5SMatthew G Knepley /*@C
2695970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
2696970e74d5SMatthew G Knepley 
2697970e74d5SMatthew G Knepley   Not collective
2698970e74d5SMatthew G Knepley 
2699970e74d5SMatthew G Knepley   Input Parameter:
2700970e74d5SMatthew G Knepley . dm - The DM
2701970e74d5SMatthew G Knepley 
2702970e74d5SMatthew G Knepley   Output Parameter:
2703970e74d5SMatthew G Knepley . lf - The local residual function
2704970e74d5SMatthew G Knepley 
2705970e74d5SMatthew G Knepley    Calling sequence of lf:
2706970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2707970e74d5SMatthew G Knepley 
2708970e74d5SMatthew G Knepley +  snes - the SNES context
2709970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2710970e74d5SMatthew G Knepley .  f - local vector to put residual in
2711970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2712970e74d5SMatthew G Knepley 
2713970e74d5SMatthew G Knepley   Level: intermediate
2714970e74d5SMatthew G Knepley 
2715970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2716970e74d5SMatthew G Knepley @*/
2717970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
2718970e74d5SMatthew G Knepley {
2719970e74d5SMatthew G Knepley   PetscFunctionBegin;
2720970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2721970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
2722970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2723970e74d5SMatthew G Knepley }
2724970e74d5SMatthew G Knepley 
2725970e74d5SMatthew G Knepley #undef __FUNCT__
2726970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
2727970e74d5SMatthew G Knepley /*@C
2728970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
2729970e74d5SMatthew G Knepley 
2730970e74d5SMatthew G Knepley   Not collective
2731970e74d5SMatthew G Knepley 
2732970e74d5SMatthew G Knepley   Input Parameters:
2733970e74d5SMatthew G Knepley + dm - The DM
2734970e74d5SMatthew G Knepley - lf - The local residual function
2735970e74d5SMatthew G Knepley 
2736970e74d5SMatthew G Knepley    Calling sequence of lf:
2737970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2738970e74d5SMatthew G Knepley 
2739970e74d5SMatthew G Knepley +  snes - the SNES context
2740970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2741970e74d5SMatthew G Knepley .  f - local vector to put residual in
2742970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2743970e74d5SMatthew G Knepley 
2744970e74d5SMatthew G Knepley   Level: intermediate
2745970e74d5SMatthew G Knepley 
2746970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2747970e74d5SMatthew G Knepley @*/
2748970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
2749970e74d5SMatthew G Knepley {
2750970e74d5SMatthew G Knepley   PetscFunctionBegin;
2751970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2752970e74d5SMatthew G Knepley   dm->lf = lf;
2753970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2754970e74d5SMatthew G Knepley }
2755970e74d5SMatthew G Knepley 
2756970e74d5SMatthew G Knepley #undef __FUNCT__
2757970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
2758970e74d5SMatthew G Knepley /*@C
2759970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
2760970e74d5SMatthew G Knepley 
2761970e74d5SMatthew G Knepley   Not collective
2762970e74d5SMatthew G Knepley 
2763970e74d5SMatthew G Knepley   Input Parameter:
2764970e74d5SMatthew G Knepley . dm - The DM
2765970e74d5SMatthew G Knepley 
2766970e74d5SMatthew G Knepley   Output Parameter:
2767970e74d5SMatthew G Knepley . lj - The local Jacobian function
2768970e74d5SMatthew G Knepley 
2769970e74d5SMatthew G Knepley    Calling sequence of lj:
2770970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2771970e74d5SMatthew G Knepley 
2772970e74d5SMatthew G Knepley +  snes - the SNES context
2773970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2774970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2775970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2776970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2777970e74d5SMatthew G Knepley 
2778970e74d5SMatthew G Knepley   Level: intermediate
2779970e74d5SMatthew G Knepley 
2780970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2781970e74d5SMatthew G Knepley @*/
2782970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
2783970e74d5SMatthew G Knepley {
2784970e74d5SMatthew G Knepley   PetscFunctionBegin;
2785970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2786970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
2787970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2788970e74d5SMatthew G Knepley }
2789970e74d5SMatthew G Knepley 
2790970e74d5SMatthew G Knepley #undef __FUNCT__
2791970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
2792970e74d5SMatthew G Knepley /*@C
2793970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
2794970e74d5SMatthew G Knepley 
2795970e74d5SMatthew G Knepley   Not collective
2796970e74d5SMatthew G Knepley 
2797970e74d5SMatthew G Knepley   Input Parameters:
2798970e74d5SMatthew G Knepley + dm - The DM
2799970e74d5SMatthew G Knepley - lj - The local Jacobian function
2800970e74d5SMatthew G Knepley 
2801970e74d5SMatthew G Knepley    Calling sequence of lj:
2802970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2803970e74d5SMatthew G Knepley 
2804970e74d5SMatthew G Knepley +  snes - the SNES context
2805970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2806970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2807970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2808970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2809970e74d5SMatthew G Knepley 
2810970e74d5SMatthew G Knepley   Level: intermediate
2811970e74d5SMatthew G Knepley 
2812970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2813970e74d5SMatthew G Knepley @*/
2814970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
2815970e74d5SMatthew G Knepley {
2816970e74d5SMatthew G Knepley   PetscFunctionBegin;
2817970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2818970e74d5SMatthew G Knepley   dm->lj = lj;
2819970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2820970e74d5SMatthew G Knepley }
282188ed4aceSMatthew G Knepley 
282288ed4aceSMatthew G Knepley #undef __FUNCT__
282388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
282488ed4aceSMatthew G Knepley /*@
282588ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
282688ed4aceSMatthew G Knepley 
282788ed4aceSMatthew G Knepley   Input Parameter:
282888ed4aceSMatthew G Knepley . dm - The DM
282988ed4aceSMatthew G Knepley 
283088ed4aceSMatthew G Knepley   Output Parameter:
283188ed4aceSMatthew G Knepley . section - The PetscSection
283288ed4aceSMatthew G Knepley 
283388ed4aceSMatthew G Knepley   Level: intermediate
283488ed4aceSMatthew G Knepley 
283588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
283688ed4aceSMatthew G Knepley 
283788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
283888ed4aceSMatthew G Knepley @*/
283988ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
284088ed4aceSMatthew G Knepley   PetscFunctionBegin;
284188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
284288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
284388ed4aceSMatthew G Knepley   *section = dm->defaultSection;
284488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
284588ed4aceSMatthew G Knepley }
284688ed4aceSMatthew G Knepley 
284788ed4aceSMatthew G Knepley #undef __FUNCT__
284888ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
284988ed4aceSMatthew G Knepley /*@
285088ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
285188ed4aceSMatthew G Knepley 
285288ed4aceSMatthew G Knepley   Input Parameters:
285388ed4aceSMatthew G Knepley + dm - The DM
285488ed4aceSMatthew G Knepley - section - The PetscSection
285588ed4aceSMatthew G Knepley 
285688ed4aceSMatthew G Knepley   Level: intermediate
285788ed4aceSMatthew G Knepley 
285888ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
285988ed4aceSMatthew G Knepley 
286088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
286188ed4aceSMatthew G Knepley @*/
286288ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
286388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
286488ed4aceSMatthew G Knepley 
286588ed4aceSMatthew G Knepley   PetscFunctionBegin;
286688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
286788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
286888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
286988ed4aceSMatthew G Knepley   dm->defaultSection = section;
287088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
287188ed4aceSMatthew G Knepley }
287288ed4aceSMatthew G Knepley 
287388ed4aceSMatthew G Knepley #undef __FUNCT__
287488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
287588ed4aceSMatthew G Knepley /*@
287688ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
287788ed4aceSMatthew G Knepley 
287888ed4aceSMatthew G Knepley   Input Parameter:
287988ed4aceSMatthew G Knepley . dm - The DM
288088ed4aceSMatthew G Knepley 
288188ed4aceSMatthew G Knepley   Output Parameter:
288288ed4aceSMatthew G Knepley . section - The PetscSection
288388ed4aceSMatthew G Knepley 
288488ed4aceSMatthew G Knepley   Level: intermediate
288588ed4aceSMatthew G Knepley 
288688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
288788ed4aceSMatthew G Knepley 
288888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
288988ed4aceSMatthew G Knepley @*/
289088ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
289188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
289288ed4aceSMatthew G Knepley 
289388ed4aceSMatthew G Knepley   PetscFunctionBegin;
289488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
289588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
289688ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
289788ed4aceSMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, &dm->defaultGlobalSection);CHKERRQ(ierr);
289888ed4aceSMatthew G Knepley   }
289988ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
290088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
290188ed4aceSMatthew G Knepley }
290288ed4aceSMatthew G Knepley 
290388ed4aceSMatthew G Knepley #undef __FUNCT__
290488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
290588ed4aceSMatthew G Knepley /*@
290688ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
290788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
290888ed4aceSMatthew G Knepley 
290988ed4aceSMatthew G Knepley   Input Parameter:
291088ed4aceSMatthew G Knepley . dm - The DM
291188ed4aceSMatthew G Knepley 
291288ed4aceSMatthew G Knepley   Output Parameter:
291388ed4aceSMatthew G Knepley . sf - The PetscSF
291488ed4aceSMatthew G Knepley 
291588ed4aceSMatthew G Knepley   Level: intermediate
291688ed4aceSMatthew G Knepley 
291788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
291888ed4aceSMatthew G Knepley 
291988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
292088ed4aceSMatthew G Knepley @*/
292188ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
292288ed4aceSMatthew G Knepley   PetscInt       nroots;
292388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
292488ed4aceSMatthew G Knepley 
292588ed4aceSMatthew G Knepley   PetscFunctionBegin;
292688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
292788ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
292888ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
292988ed4aceSMatthew G Knepley   if (nroots < 0) {
293088ed4aceSMatthew G Knepley     PetscSection section, gSection;
293188ed4aceSMatthew G Knepley 
293288ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
293331ea6d37SMatthew G Knepley     if (section) {
293488ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
293588ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
293631ea6d37SMatthew G Knepley     } else {
293731ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
293831ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
293931ea6d37SMatthew G Knepley     }
294088ed4aceSMatthew G Knepley   }
294188ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
294288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
294388ed4aceSMatthew G Knepley }
294488ed4aceSMatthew G Knepley 
294588ed4aceSMatthew G Knepley #undef __FUNCT__
294688ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
294788ed4aceSMatthew G Knepley /*@
294888ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
294988ed4aceSMatthew G Knepley 
295088ed4aceSMatthew G Knepley   Input Parameters:
295188ed4aceSMatthew G Knepley + dm - The DM
295288ed4aceSMatthew G Knepley - sf - The PetscSF
295388ed4aceSMatthew G Knepley 
295488ed4aceSMatthew G Knepley   Level: intermediate
295588ed4aceSMatthew G Knepley 
295688ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
295788ed4aceSMatthew G Knepley 
295888ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
295988ed4aceSMatthew G Knepley @*/
296088ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
296188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
296288ed4aceSMatthew G Knepley 
296388ed4aceSMatthew G Knepley   PetscFunctionBegin;
296488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
296588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
296688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
296788ed4aceSMatthew G Knepley   dm->defaultSF = sf;
296888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
296988ed4aceSMatthew G Knepley }
297088ed4aceSMatthew G Knepley 
297188ed4aceSMatthew G Knepley #undef __FUNCT__
297288ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
297388ed4aceSMatthew G Knepley /*@C
297488ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
297588ed4aceSMatthew G Knepley   describing the data layout.
297688ed4aceSMatthew G Knepley 
297788ed4aceSMatthew G Knepley   Input Parameters:
297888ed4aceSMatthew G Knepley + dm - The DM
297988ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
298088ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
298188ed4aceSMatthew G Knepley 
298288ed4aceSMatthew G Knepley   Level: intermediate
298388ed4aceSMatthew G Knepley 
298488ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
298588ed4aceSMatthew G Knepley @*/
298688ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
298788ed4aceSMatthew G Knepley {
298888ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
298988ed4aceSMatthew G Knepley   PetscLayout     layout;
299088ed4aceSMatthew G Knepley   const PetscInt *ranges;
299188ed4aceSMatthew G Knepley   PetscInt       *local;
299288ed4aceSMatthew G Knepley   PetscSFNode    *remote;
299388ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
299488ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
299588ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
299688ed4aceSMatthew G Knepley 
299788ed4aceSMatthew G Knepley   PetscFunctionBegin;
299888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
299988ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
300088ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
300188ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
300288ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
300388ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
300488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
300588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
300688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
300788ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
300888ed4aceSMatthew G Knepley   for(p = pStart, nleaves = 0; p < pEnd; ++p) {
300988ed4aceSMatthew G Knepley     PetscInt dof, cdof;
301088ed4aceSMatthew G Knepley 
301188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &dof);CHKERRQ(ierr);
301288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &cdof);CHKERRQ(ierr);
301388ed4aceSMatthew G Knepley     nleaves += dof < 0 ? -(dof+1)-cdof : dof-cdof;
301488ed4aceSMatthew G Knepley   }
301588ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
301688ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
301788ed4aceSMatthew G Knepley   for(p = pStart, l = 0; p < pEnd; ++p) {
301888ed4aceSMatthew G Knepley     PetscInt *cind;
301988ed4aceSMatthew G Knepley     PetscInt  dof, gdof, cdof, dim, off, goff, d, c;
302088ed4aceSMatthew G Knepley 
302188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
302288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
302388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
302488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
302588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
302688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
302788ed4aceSMatthew G Knepley     dim  = dof-cdof;
302888ed4aceSMatthew G Knepley     for(d = 0, c = 0; d < dof; ++d) {
302988ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
303088ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
303188ed4aceSMatthew G Knepley     }
303288ed4aceSMatthew G Knepley     if (gdof < 0) {
303388ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
303488ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
303588ed4aceSMatthew G Knepley 
303688ed4aceSMatthew G Knepley         for(r = 0; r < size; ++r) {
303788ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
303888ed4aceSMatthew G Knepley         }
303988ed4aceSMatthew G Knepley         remote[l].rank  = r;
304088ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
304188ed4aceSMatthew G Knepley       }
304288ed4aceSMatthew G Knepley     } else {
304388ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
304488ed4aceSMatthew G Knepley         remote[l].rank  = rank;
304588ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
304688ed4aceSMatthew G Knepley       }
304788ed4aceSMatthew G Knepley     }
304888ed4aceSMatthew G Knepley   }
304988ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
305088ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
305188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
305288ed4aceSMatthew G Knepley }
3053