xref: /petsc/src/dm/interface/dm.c (revision 16621825bf3b09a910276d7bb3d66e46182fe66f)
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__
937*16621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM"
938e7c4fc90SDmitry Karpeev /*@C
939*16621825SDmitry Karpeev   DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields.
940*16621825SDmitry Karpeev 
941*16621825SDmitry Karpeev   Not Collective
942*16621825SDmitry Karpeev 
943*16621825SDmitry Karpeev   Input Parameters:
944*16621825SDmitry Karpeev + dm   - the DM object
945*16621825SDmitry Karpeev - name - the name of the field decomposition
946*16621825SDmitry Karpeev 
947*16621825SDmitry Karpeev   Output Parameter:
948*16621825SDmitry Karpeev . ddm  - the field decomposition DM (PETSC_NULL, if no such decomposition is known)
949*16621825SDmitry Karpeev 
950*16621825SDmitry Karpeev   Level: advanced
951*16621825SDmitry Karpeev 
952*16621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
953*16621825SDmitry Karpeev @*/
954*16621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm)
955*16621825SDmitry Karpeev {
956*16621825SDmitry Karpeev   PetscErrorCode ierr;
957*16621825SDmitry Karpeev 
958*16621825SDmitry Karpeev   PetscFunctionBegin;
959*16621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
960*16621825SDmitry Karpeev   PetscValidCharPointer(name,2);
961*16621825SDmitry Karpeev   PetscValidPointer(ddm,3);
962*16621825SDmitry Karpeev   if(!dm->ops->createfielddecompositiondm) {
963*16621825SDmitry Karpeev     *ddm = PETSC_NULL;
964*16621825SDmitry Karpeev   }
965*16621825SDmitry Karpeev   else {
966*16621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
967*16621825SDmitry Karpeev   }
968*16621825SDmitry Karpeev   PetscFunctionReturn(0);
969*16621825SDmitry Karpeev }
970*16621825SDmitry Karpeev 
971*16621825SDmitry Karpeev 
972*16621825SDmitry Karpeev #undef __FUNCT__
973*16621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
974*16621825SDmitry Karpeev /*@C
975*16621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
976*16621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
977*16621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
978e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
979e7c4fc90SDmitry Karpeev 
980e7c4fc90SDmitry Karpeev   Not collective
981e7c4fc90SDmitry Karpeev 
982e7c4fc90SDmitry Karpeev   Input Parameter:
983e7c4fc90SDmitry Karpeev . dm - the DM object
984e7c4fc90SDmitry Karpeev 
985e7c4fc90SDmitry Karpeev   Output Parameters:
986*16621825SDmitry Karpeev + len       - The number of subproblems in the field decomposition (or PETSC_NULL if not requested)
987*16621825SDmitry Karpeev . namelist  - The name for each field (or PETSC_NULL if not requested)
988*16621825SDmitry Karpeev . islist    - The global indices for each field (or PETSC_NULL if not requested)
989*16621825SDmitry Karpeev - dmlist    - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
990e7c4fc90SDmitry Karpeev 
991e7c4fc90SDmitry Karpeev   Level: intermediate
992e7c4fc90SDmitry Karpeev 
993e7c4fc90SDmitry Karpeev   Notes:
994e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
995e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
996e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
997e7c4fc90SDmitry Karpeev 
998e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
999e7c4fc90SDmitry Karpeev @*/
1000*16621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1001e7c4fc90SDmitry Karpeev {
1002e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1003e7c4fc90SDmitry Karpeev 
1004e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1005e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1006e7c4fc90SDmitry Karpeev   if (len) {PetscValidPointer(len,2);}
1007e7c4fc90SDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3);}
1008e7c4fc90SDmitry Karpeev   if (islist) {PetscValidPointer(islist,4);}
1009e7c4fc90SDmitry Karpeev   if (dmlist) {PetscValidPointer(dmlist,5);}
1010*16621825SDmitry Karpeev   if(!dm->ops->createfielddecomposition) {
101169ca1f37SDmitry Karpeev     ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1012e7c4fc90SDmitry Karpeev     /* By default there are no DMs associated with subproblems. */
1013e7c4fc90SDmitry Karpeev     if(dmlist) *dmlist = PETSC_NULL;
1014e7c4fc90SDmitry Karpeev   }
1015e7c4fc90SDmitry Karpeev   else {
1016*16621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
1017*16621825SDmitry Karpeev   }
1018*16621825SDmitry Karpeev   PetscFunctionReturn(0);
1019*16621825SDmitry Karpeev }
1020*16621825SDmitry Karpeev 
1021*16621825SDmitry Karpeev #undef __FUNCT__
1022*16621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM"
1023*16621825SDmitry Karpeev /*@C
1024*16621825SDmitry Karpeev   DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains.
1025*16621825SDmitry Karpeev 
1026*16621825SDmitry Karpeev   Not Collective
1027*16621825SDmitry Karpeev 
1028*16621825SDmitry Karpeev   Input Parameters:
1029*16621825SDmitry Karpeev + dm   - the DM object
1030*16621825SDmitry Karpeev - name - the name of the subdomain decomposition
1031*16621825SDmitry Karpeev 
1032*16621825SDmitry Karpeev   Output Parameter:
1033*16621825SDmitry Karpeev . ddm  - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known)
1034*16621825SDmitry Karpeev 
1035*16621825SDmitry Karpeev   Level: advanced
1036*16621825SDmitry Karpeev 
1037*16621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
1038*16621825SDmitry Karpeev @*/
1039*16621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm)
1040*16621825SDmitry Karpeev {
1041*16621825SDmitry Karpeev   PetscErrorCode ierr;
1042*16621825SDmitry Karpeev 
1043*16621825SDmitry Karpeev   PetscFunctionBegin;
1044*16621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1045*16621825SDmitry Karpeev   PetscValidCharPointer(name,2);
1046*16621825SDmitry Karpeev   PetscValidPointer(ddm,3);
1047*16621825SDmitry Karpeev   if(!dm->ops->createdomaindecompositiondm) {
1048*16621825SDmitry Karpeev     *ddm = PETSC_NULL;
1049*16621825SDmitry Karpeev   }
1050*16621825SDmitry Karpeev   else {
1051*16621825SDmitry Karpeev     ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
1052*16621825SDmitry Karpeev   }
1053*16621825SDmitry Karpeev   PetscFunctionReturn(0);
1054*16621825SDmitry Karpeev }
1055*16621825SDmitry Karpeev 
1056*16621825SDmitry Karpeev 
1057*16621825SDmitry Karpeev #undef __FUNCT__
1058*16621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
1059*16621825SDmitry Karpeev /*@C
1060*16621825SDmitry Karpeev   DMCreateDomainDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
1061*16621825SDmitry Karpeev                           corresponding to restrictions to a family of subdomains : each IS contains the global indices
1062*16621825SDmitry Karpeev                           of the dofs of the corresponding subdomains. The optional list of DMs define the DM for each
1063*16621825SDmitry Karpeev                           subproblem.
1064*16621825SDmitry Karpeev 
1065*16621825SDmitry Karpeev   Not collective
1066*16621825SDmitry Karpeev 
1067*16621825SDmitry Karpeev   Input Parameter:
1068*16621825SDmitry Karpeev . dm - the DM object
1069*16621825SDmitry Karpeev 
1070*16621825SDmitry Karpeev   Output Parameters:
1071*16621825SDmitry Karpeev + len       - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested)
1072*16621825SDmitry Karpeev . namelist  - The name for each subdomain (or PETSC_NULL if not requested)
1073*16621825SDmitry Karpeev . islist    - The global indices for each subdomain (or PETSC_NULL if not requested)
1074*16621825SDmitry Karpeev - dmlist    - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
1075*16621825SDmitry Karpeev 
1076*16621825SDmitry Karpeev   Level: intermediate
1077*16621825SDmitry Karpeev 
1078*16621825SDmitry Karpeev   Notes:
1079*16621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1080*16621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1081*16621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1082*16621825SDmitry Karpeev 
1083*16621825SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), CreateFieldDecomposition()
1084*16621825SDmitry Karpeev @*/
1085*16621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1086*16621825SDmitry Karpeev {
1087*16621825SDmitry Karpeev   PetscErrorCode ierr;
1088*16621825SDmitry Karpeev 
1089*16621825SDmitry Karpeev   PetscFunctionBegin;
1090*16621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1091*16621825SDmitry Karpeev   if (len) {PetscValidPointer(len,2);}
1092*16621825SDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3); *namelist = PETSC_NULL;}
1093*16621825SDmitry Karpeev   if (islist) {PetscValidPointer(islist,4);     *islist   = PETSC_NULL;}
1094*16621825SDmitry Karpeev   if (dmlist) {PetscValidPointer(dmlist,5);     *dmlist   = PETSC_NULL;}
1095*16621825SDmitry Karpeev   if(dm->ops->createdomaindecomposition) {
1096*16621825SDmitry Karpeev     ierr = (*dm->ops->createdomaindecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
1097e7c4fc90SDmitry Karpeev   }
1098e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1099e7c4fc90SDmitry Karpeev }
1100e7c4fc90SDmitry Karpeev 
1101e7c4fc90SDmitry Karpeev 
1102e7c4fc90SDmitry Karpeev #undef __FUNCT__
110347c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
110447c6ae99SBarry Smith /*@
110547c6ae99SBarry Smith   DMRefine - Refines a DM object
110647c6ae99SBarry Smith 
110747c6ae99SBarry Smith   Collective on DM
110847c6ae99SBarry Smith 
110947c6ae99SBarry Smith   Input Parameter:
111047c6ae99SBarry Smith + dm   - the DM object
111191d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
111247c6ae99SBarry Smith 
111347c6ae99SBarry Smith   Output Parameter:
1114ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1115ae0a1c52SMatthew G Knepley 
1116ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
111747c6ae99SBarry Smith 
111847c6ae99SBarry Smith   Level: developer
111947c6ae99SBarry Smith 
1120e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
112147c6ae99SBarry Smith @*/
11227087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
112347c6ae99SBarry Smith {
112447c6ae99SBarry Smith   PetscErrorCode ierr;
1125c833c3b5SJed Brown   DMRefineHookLink link;
112647c6ae99SBarry Smith 
112747c6ae99SBarry Smith   PetscFunctionBegin;
1128732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
112947c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
11304057135bSMatthew G Knepley   if (*dmf) {
113143842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
1132644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
1133644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
1134644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
1135644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
1136644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
1137644e2e5bSBarry Smith     }
11388cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1139644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
11400598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1141656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
1142c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1143c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1144c833c3b5SJed Brown     }
1145c833c3b5SJed Brown   }
1146c833c3b5SJed Brown   PetscFunctionReturn(0);
1147c833c3b5SJed Brown }
1148c833c3b5SJed Brown 
1149c833c3b5SJed Brown #undef __FUNCT__
1150c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1151c833c3b5SJed Brown /*@
1152c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1153c833c3b5SJed Brown 
1154c833c3b5SJed Brown    Logically Collective
1155c833c3b5SJed Brown 
1156c833c3b5SJed Brown    Input Arguments:
1157c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1158c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1159c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1160c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1161c833c3b5SJed Brown 
1162c833c3b5SJed Brown    Calling sequence of refinehook:
1163c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1164c833c3b5SJed Brown 
1165c833c3b5SJed Brown +  coarse - coarse level DM
1166c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1167c833c3b5SJed Brown -  ctx - optional user-defined function context
1168c833c3b5SJed Brown 
1169c833c3b5SJed Brown    Calling sequence for interphook:
1170c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1171c833c3b5SJed Brown 
1172c833c3b5SJed Brown +  coarse - coarse level DM
1173c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1174c833c3b5SJed Brown .  fine - fine level DM to update
1175c833c3b5SJed Brown -  ctx - optional user-defined function context
1176c833c3b5SJed Brown 
1177c833c3b5SJed Brown    Level: advanced
1178c833c3b5SJed Brown 
1179c833c3b5SJed Brown    Notes:
1180c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1181c833c3b5SJed Brown 
1182c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1183c833c3b5SJed Brown 
1184c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1185c833c3b5SJed Brown @*/
1186c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1187c833c3b5SJed Brown {
1188c833c3b5SJed Brown   PetscErrorCode ierr;
1189c833c3b5SJed Brown   DMRefineHookLink link,*p;
1190c833c3b5SJed Brown 
1191c833c3b5SJed Brown   PetscFunctionBegin;
1192c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1193c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1194c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1195c833c3b5SJed Brown   link->refinehook = refinehook;
1196c833c3b5SJed Brown   link->interphook = interphook;
1197c833c3b5SJed Brown   link->ctx = ctx;
1198c833c3b5SJed Brown   link->next = PETSC_NULL;
1199c833c3b5SJed Brown   *p = link;
1200c833c3b5SJed Brown   PetscFunctionReturn(0);
1201c833c3b5SJed Brown }
1202c833c3b5SJed Brown 
1203c833c3b5SJed Brown #undef __FUNCT__
1204c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1205c833c3b5SJed Brown /*@
1206c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1207c833c3b5SJed Brown 
1208c833c3b5SJed Brown    Collective if any hooks are
1209c833c3b5SJed Brown 
1210c833c3b5SJed Brown    Input Arguments:
1211c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1212c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1213c833c3b5SJed Brown -  fine - finer DM to update
1214c833c3b5SJed Brown 
1215c833c3b5SJed Brown    Level: developer
1216c833c3b5SJed Brown 
1217c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1218c833c3b5SJed Brown @*/
1219c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1220c833c3b5SJed Brown {
1221c833c3b5SJed Brown   PetscErrorCode ierr;
1222c833c3b5SJed Brown   DMRefineHookLink link;
1223c833c3b5SJed Brown 
1224c833c3b5SJed Brown   PetscFunctionBegin;
1225c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1226c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
12274057135bSMatthew G Knepley   }
122847c6ae99SBarry Smith   PetscFunctionReturn(0);
122947c6ae99SBarry Smith }
123047c6ae99SBarry Smith 
123147c6ae99SBarry Smith #undef __FUNCT__
1232eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1233eb3f98d2SBarry Smith /*@
1234eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1235eb3f98d2SBarry Smith 
1236eb3f98d2SBarry Smith     Not Collective
1237eb3f98d2SBarry Smith 
1238eb3f98d2SBarry Smith     Input Parameter:
1239eb3f98d2SBarry Smith .   dm - the DM object
1240eb3f98d2SBarry Smith 
1241eb3f98d2SBarry Smith     Output Parameter:
1242eb3f98d2SBarry Smith .   level - number of refinements
1243eb3f98d2SBarry Smith 
1244eb3f98d2SBarry Smith     Level: developer
1245eb3f98d2SBarry Smith 
12466a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1247eb3f98d2SBarry Smith 
1248eb3f98d2SBarry Smith @*/
1249eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1250eb3f98d2SBarry Smith {
1251eb3f98d2SBarry Smith   PetscFunctionBegin;
1252eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1253eb3f98d2SBarry Smith   *level = dm->levelup;
1254eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1255eb3f98d2SBarry Smith }
1256eb3f98d2SBarry Smith 
1257eb3f98d2SBarry Smith #undef __FUNCT__
125847c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
125947c6ae99SBarry Smith /*@
126047c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
126147c6ae99SBarry Smith 
126247c6ae99SBarry Smith     Neighbor-wise Collective on DM
126347c6ae99SBarry Smith 
126447c6ae99SBarry Smith     Input Parameters:
126547c6ae99SBarry Smith +   dm - the DM object
126647c6ae99SBarry Smith .   g - the global vector
126747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
126847c6ae99SBarry Smith -   l - the local vector
126947c6ae99SBarry Smith 
127047c6ae99SBarry Smith 
127147c6ae99SBarry Smith     Level: beginner
127247c6ae99SBarry Smith 
1273e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
127447c6ae99SBarry Smith 
127547c6ae99SBarry Smith @*/
12767087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
127747c6ae99SBarry Smith {
12787128ae9fSMatthew G Knepley   PetscSF        sf;
127947c6ae99SBarry Smith   PetscErrorCode ierr;
128047c6ae99SBarry Smith 
128147c6ae99SBarry Smith   PetscFunctionBegin;
1282171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12837128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
12847128ae9fSMatthew G Knepley   if (sf) {
12857128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
12867128ae9fSMatthew G Knepley 
12877128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
12887128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
12897128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
12907128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
12917128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
12927128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
12937128ae9fSMatthew G Knepley   } else {
1294843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
12957128ae9fSMatthew G Knepley   }
129647c6ae99SBarry Smith   PetscFunctionReturn(0);
129747c6ae99SBarry Smith }
129847c6ae99SBarry Smith 
129947c6ae99SBarry Smith #undef __FUNCT__
130047c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
130147c6ae99SBarry Smith /*@
130247c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
130347c6ae99SBarry Smith 
130447c6ae99SBarry Smith     Neighbor-wise Collective on DM
130547c6ae99SBarry Smith 
130647c6ae99SBarry Smith     Input Parameters:
130747c6ae99SBarry Smith +   dm - the DM object
130847c6ae99SBarry Smith .   g - the global vector
130947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
131047c6ae99SBarry Smith -   l - the local vector
131147c6ae99SBarry Smith 
131247c6ae99SBarry Smith 
131347c6ae99SBarry Smith     Level: beginner
131447c6ae99SBarry Smith 
1315e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
131647c6ae99SBarry Smith 
131747c6ae99SBarry Smith @*/
13187087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
131947c6ae99SBarry Smith {
13207128ae9fSMatthew G Knepley   PetscSF        sf;
132147c6ae99SBarry Smith   PetscErrorCode ierr;
132261a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
132347c6ae99SBarry Smith 
132447c6ae99SBarry Smith   PetscFunctionBegin;
1325171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13267128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
13277128ae9fSMatthew G Knepley   if (sf) {
13287128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
13297128ae9fSMatthew G Knepley 
13307128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
13317128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
13327128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
13337128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
13347128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
13357128ae9fSMatthew G Knepley   } else {
1336843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
13377128ae9fSMatthew G Knepley   }
133847c6ae99SBarry Smith   PetscFunctionReturn(0);
133947c6ae99SBarry Smith }
134047c6ae99SBarry Smith 
134147c6ae99SBarry Smith #undef __FUNCT__
13429a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
134347c6ae99SBarry Smith /*@
13449a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
13459a42bb27SBarry Smith 
13469a42bb27SBarry Smith     Neighbor-wise Collective on DM
13479a42bb27SBarry Smith 
13489a42bb27SBarry Smith     Input Parameters:
13499a42bb27SBarry Smith +   dm - the DM object
1350f6813fd5SJed Brown .   l - the local vector
13519a42bb27SBarry 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
13529a42bb27SBarry Smith            base point.
1353f6813fd5SJed Brown - - the global vector
13549a42bb27SBarry Smith 
13559a42bb27SBarry 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
13569a42bb27SBarry 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
13579a42bb27SBarry Smith            global array to the final global array with VecAXPY().
13589a42bb27SBarry Smith 
13599a42bb27SBarry Smith     Level: beginner
13609a42bb27SBarry Smith 
1361e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
13629a42bb27SBarry Smith 
13639a42bb27SBarry Smith @*/
13647087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
13659a42bb27SBarry Smith {
13667128ae9fSMatthew G Knepley   PetscSF        sf;
13679a42bb27SBarry Smith   PetscErrorCode ierr;
13689a42bb27SBarry Smith 
13699a42bb27SBarry Smith   PetscFunctionBegin;
1370171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13717128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
13727128ae9fSMatthew G Knepley   if (sf) {
13737128ae9fSMatthew G Knepley     MPI_Op       op;
13747128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
13757128ae9fSMatthew G Knepley 
13767128ae9fSMatthew G Knepley     switch(mode) {
13777128ae9fSMatthew G Knepley     case INSERT_VALUES:
13787128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
13797128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
13807128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
13817128ae9fSMatthew G Knepley #else
13827128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
13837128ae9fSMatthew G Knepley #endif
13847128ae9fSMatthew G Knepley     case ADD_VALUES:
13857128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
13867128ae9fSMatthew G Knepley       op = MPI_SUM; break;
13877128ae9fSMatthew G Knepley   default:
13887128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
13897128ae9fSMatthew G Knepley     }
13907128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
13917128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
13927128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
13937128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
13947128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
13957128ae9fSMatthew G Knepley   } else {
1396843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
13977128ae9fSMatthew G Knepley   }
13989a42bb27SBarry Smith   PetscFunctionReturn(0);
13999a42bb27SBarry Smith }
14009a42bb27SBarry Smith 
14019a42bb27SBarry Smith #undef __FUNCT__
14029a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
14039a42bb27SBarry Smith /*@
14049a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
140547c6ae99SBarry Smith 
140647c6ae99SBarry Smith     Neighbor-wise Collective on DM
140747c6ae99SBarry Smith 
140847c6ae99SBarry Smith     Input Parameters:
140947c6ae99SBarry Smith +   dm - the DM object
1410f6813fd5SJed Brown .   l - the local vector
141147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1412f6813fd5SJed Brown -   g - the global vector
141347c6ae99SBarry Smith 
141447c6ae99SBarry Smith 
141547c6ae99SBarry Smith     Level: beginner
141647c6ae99SBarry Smith 
1417e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
141847c6ae99SBarry Smith 
141947c6ae99SBarry Smith @*/
14207087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
142147c6ae99SBarry Smith {
14227128ae9fSMatthew G Knepley   PetscSF        sf;
142347c6ae99SBarry Smith   PetscErrorCode ierr;
142447c6ae99SBarry Smith 
142547c6ae99SBarry Smith   PetscFunctionBegin;
1426171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14277128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
14287128ae9fSMatthew G Knepley   if (sf) {
14297128ae9fSMatthew G Knepley     MPI_Op       op;
14307128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
14317128ae9fSMatthew G Knepley 
14327128ae9fSMatthew G Knepley     switch(mode) {
14337128ae9fSMatthew G Knepley     case INSERT_VALUES:
14347128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
14357128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
14367128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
14377128ae9fSMatthew G Knepley #else
14387128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
14397128ae9fSMatthew G Knepley #endif
14407128ae9fSMatthew G Knepley     case ADD_VALUES:
14417128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
14427128ae9fSMatthew G Knepley       op = MPI_SUM; break;
14437128ae9fSMatthew G Knepley     default:
14447128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
14457128ae9fSMatthew G Knepley     }
14467128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
14477128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
14487128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
14497128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
14507128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
14517128ae9fSMatthew G Knepley   } else {
1452843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
14537128ae9fSMatthew G Knepley   }
145447c6ae99SBarry Smith   PetscFunctionReturn(0);
145547c6ae99SBarry Smith }
145647c6ae99SBarry Smith 
145747c6ae99SBarry Smith #undef __FUNCT__
145847c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
145947c6ae99SBarry Smith /*@
146047c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
146147c6ae99SBarry Smith 
146247c6ae99SBarry Smith     Collective on DM
146347c6ae99SBarry Smith 
146447c6ae99SBarry Smith     Input Parameter:
146547c6ae99SBarry Smith +   dm - the DM object
146647c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
146747c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
146847c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
146947c6ae99SBarry Smith 
147047c6ae99SBarry Smith     Level: developer
147147c6ae99SBarry Smith 
1472e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
147347c6ae99SBarry Smith          DMSetFunction()
147447c6ae99SBarry Smith 
147547c6ae99SBarry Smith @*/
14767087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
147747c6ae99SBarry Smith {
147847c6ae99SBarry Smith   PetscErrorCode ierr;
1479171400e9SBarry Smith 
148047c6ae99SBarry Smith   PetscFunctionBegin;
1481171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
148247c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
148347c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
148447c6ae99SBarry Smith   if (A != B) {
148547c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
148647c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
148747c6ae99SBarry Smith   }
148847c6ae99SBarry Smith   PetscFunctionReturn(0);
148947c6ae99SBarry Smith }
149047c6ae99SBarry Smith 
149147c6ae99SBarry Smith #undef __FUNCT__
149247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
149347c6ae99SBarry Smith /*@
149447c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
149547c6ae99SBarry Smith 
149647c6ae99SBarry Smith     Collective on DM
149747c6ae99SBarry Smith 
149847c6ae99SBarry Smith     Input Parameter:
149947c6ae99SBarry Smith +   dm - the DM object
150091d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
150147c6ae99SBarry Smith 
150247c6ae99SBarry Smith     Output Parameter:
150347c6ae99SBarry Smith .   dmc - the coarsened DM
150447c6ae99SBarry Smith 
150547c6ae99SBarry Smith     Level: developer
150647c6ae99SBarry Smith 
1507e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
150847c6ae99SBarry Smith 
150947c6ae99SBarry Smith @*/
15107087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
151147c6ae99SBarry Smith {
151247c6ae99SBarry Smith   PetscErrorCode ierr;
1513b17ce1afSJed Brown   DMCoarsenHookLink link;
151447c6ae99SBarry Smith 
151547c6ae99SBarry Smith   PetscFunctionBegin;
1516171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
151747c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
151843842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
151947c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
152047c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
152147c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
152247c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
152347c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
152447c6ae99SBarry Smith   }
15258cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1526644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
15270598a293SJed Brown   (*dmc)->levelup   = dm->levelup;
1528656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1529b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1530b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1531b17ce1afSJed Brown   }
1532b17ce1afSJed Brown   PetscFunctionReturn(0);
1533b17ce1afSJed Brown }
1534b17ce1afSJed Brown 
1535b17ce1afSJed Brown #undef __FUNCT__
1536b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1537b17ce1afSJed Brown /*@
1538b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1539b17ce1afSJed Brown 
1540b17ce1afSJed Brown    Logically Collective
1541b17ce1afSJed Brown 
1542b17ce1afSJed Brown    Input Arguments:
1543b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1544b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1545b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1546b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1547b17ce1afSJed Brown 
1548b17ce1afSJed Brown    Calling sequence of coarsenhook:
1549b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1550b17ce1afSJed Brown 
1551b17ce1afSJed Brown +  fine - fine level DM
1552b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1553b17ce1afSJed Brown -  ctx - optional user-defined function context
1554b17ce1afSJed Brown 
1555b17ce1afSJed Brown    Calling sequence for restricthook:
1556c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
1557b17ce1afSJed Brown 
1558b17ce1afSJed Brown +  fine - fine level DM
1559b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1560c833c3b5SJed Brown .  rscale - scaling vector for restriction
1561c833c3b5SJed Brown .  inject - matrix restricting by injection
1562b17ce1afSJed Brown .  coarse - coarse level DM to update
1563b17ce1afSJed Brown -  ctx - optional user-defined function context
1564b17ce1afSJed Brown 
1565b17ce1afSJed Brown    Level: advanced
1566b17ce1afSJed Brown 
1567b17ce1afSJed Brown    Notes:
1568b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1569b17ce1afSJed Brown 
1570b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1571b17ce1afSJed Brown 
1572b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1573b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1574b17ce1afSJed Brown 
1575c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1576b17ce1afSJed Brown @*/
1577b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1578b17ce1afSJed Brown {
1579b17ce1afSJed Brown   PetscErrorCode ierr;
1580b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1581b17ce1afSJed Brown 
1582b17ce1afSJed Brown   PetscFunctionBegin;
1583b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
15846bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1585b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1586b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1587b17ce1afSJed Brown   link->restricthook = restricthook;
1588b17ce1afSJed Brown   link->ctx = ctx;
15896cab3a1bSJed Brown   link->next = PETSC_NULL;
1590b17ce1afSJed Brown   *p = link;
1591b17ce1afSJed Brown   PetscFunctionReturn(0);
1592b17ce1afSJed Brown }
1593b17ce1afSJed Brown 
1594b17ce1afSJed Brown #undef __FUNCT__
1595b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1596b17ce1afSJed Brown /*@
1597b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1598b17ce1afSJed Brown 
1599b17ce1afSJed Brown    Collective if any hooks are
1600b17ce1afSJed Brown 
1601b17ce1afSJed Brown    Input Arguments:
1602b17ce1afSJed Brown +  fine - finer DM to use as a base
1603b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1604b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1605b17ce1afSJed Brown -  coarse - coarer DM to update
1606b17ce1afSJed Brown 
1607b17ce1afSJed Brown    Level: developer
1608b17ce1afSJed Brown 
1609b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1610b17ce1afSJed Brown @*/
1611b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1612b17ce1afSJed Brown {
1613b17ce1afSJed Brown   PetscErrorCode ierr;
1614b17ce1afSJed Brown   DMCoarsenHookLink link;
1615b17ce1afSJed Brown 
1616b17ce1afSJed Brown   PetscFunctionBegin;
1617b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1618b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1619b17ce1afSJed Brown   }
162047c6ae99SBarry Smith   PetscFunctionReturn(0);
162147c6ae99SBarry Smith }
162247c6ae99SBarry Smith 
162347c6ae99SBarry Smith #undef __FUNCT__
16245fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
16255fe1f584SPeter Brune /*@
16266a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
16275fe1f584SPeter Brune 
16285fe1f584SPeter Brune     Not Collective
16295fe1f584SPeter Brune 
16305fe1f584SPeter Brune     Input Parameter:
16315fe1f584SPeter Brune .   dm - the DM object
16325fe1f584SPeter Brune 
16335fe1f584SPeter Brune     Output Parameter:
16346a7d9d85SPeter Brune .   level - number of coarsenings
16355fe1f584SPeter Brune 
16365fe1f584SPeter Brune     Level: developer
16375fe1f584SPeter Brune 
16386a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
16395fe1f584SPeter Brune 
16405fe1f584SPeter Brune @*/
16415fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
16425fe1f584SPeter Brune {
16435fe1f584SPeter Brune   PetscFunctionBegin;
16445fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16455fe1f584SPeter Brune   *level = dm->leveldown;
16465fe1f584SPeter Brune   PetscFunctionReturn(0);
16475fe1f584SPeter Brune }
16485fe1f584SPeter Brune 
16495fe1f584SPeter Brune 
16505fe1f584SPeter Brune 
16515fe1f584SPeter Brune #undef __FUNCT__
165247c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
165347c6ae99SBarry Smith /*@C
165447c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
165547c6ae99SBarry Smith 
165647c6ae99SBarry Smith     Collective on DM
165747c6ae99SBarry Smith 
165847c6ae99SBarry Smith     Input Parameter:
165947c6ae99SBarry Smith +   dm - the DM object
166047c6ae99SBarry Smith -   nlevels - the number of levels of refinement
166147c6ae99SBarry Smith 
166247c6ae99SBarry Smith     Output Parameter:
166347c6ae99SBarry Smith .   dmf - the refined DM hierarchy
166447c6ae99SBarry Smith 
166547c6ae99SBarry Smith     Level: developer
166647c6ae99SBarry Smith 
1667e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
166847c6ae99SBarry Smith 
166947c6ae99SBarry Smith @*/
16707087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
167147c6ae99SBarry Smith {
167247c6ae99SBarry Smith   PetscErrorCode ierr;
167347c6ae99SBarry Smith 
167447c6ae99SBarry Smith   PetscFunctionBegin;
1675171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
167647c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
167747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
167847c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
167947c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
168047c6ae99SBarry Smith   } else if (dm->ops->refine) {
168147c6ae99SBarry Smith     PetscInt i;
168247c6ae99SBarry Smith 
168347c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
168447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
168547c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
168647c6ae99SBarry Smith     }
168747c6ae99SBarry Smith   } else {
168847c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
168947c6ae99SBarry Smith   }
169047c6ae99SBarry Smith   PetscFunctionReturn(0);
169147c6ae99SBarry Smith }
169247c6ae99SBarry Smith 
169347c6ae99SBarry Smith #undef __FUNCT__
169447c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
169547c6ae99SBarry Smith /*@C
169647c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
169747c6ae99SBarry Smith 
169847c6ae99SBarry Smith     Collective on DM
169947c6ae99SBarry Smith 
170047c6ae99SBarry Smith     Input Parameter:
170147c6ae99SBarry Smith +   dm - the DM object
170247c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
170347c6ae99SBarry Smith 
170447c6ae99SBarry Smith     Output Parameter:
170547c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
170647c6ae99SBarry Smith 
170747c6ae99SBarry Smith     Level: developer
170847c6ae99SBarry Smith 
1709e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
171047c6ae99SBarry Smith 
171147c6ae99SBarry Smith @*/
17127087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
171347c6ae99SBarry Smith {
171447c6ae99SBarry Smith   PetscErrorCode ierr;
171547c6ae99SBarry Smith 
171647c6ae99SBarry Smith   PetscFunctionBegin;
1717171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
171847c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
171947c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
172047c6ae99SBarry Smith   PetscValidPointer(dmc,3);
172147c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
172247c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
172347c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
172447c6ae99SBarry Smith     PetscInt i;
172547c6ae99SBarry Smith 
172647c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
172747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
172847c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
172947c6ae99SBarry Smith     }
173047c6ae99SBarry Smith   } else {
173147c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
173247c6ae99SBarry Smith   }
173347c6ae99SBarry Smith   PetscFunctionReturn(0);
173447c6ae99SBarry Smith }
173547c6ae99SBarry Smith 
173647c6ae99SBarry Smith #undef __FUNCT__
1737e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
173847c6ae99SBarry Smith /*@
1739e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
174047c6ae99SBarry Smith    grids associated with two DMs.
174147c6ae99SBarry Smith 
174247c6ae99SBarry Smith    Collective on DM
174347c6ae99SBarry Smith 
174447c6ae99SBarry Smith    Input Parameters:
174547c6ae99SBarry Smith +  dmc - the coarse grid DM
174647c6ae99SBarry Smith -  dmf - the fine grid DM
174747c6ae99SBarry Smith 
174847c6ae99SBarry Smith    Output Parameters:
174947c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
175047c6ae99SBarry Smith 
175147c6ae99SBarry Smith    Level: intermediate
175247c6ae99SBarry Smith 
175347c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
175447c6ae99SBarry Smith 
1755e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
175647c6ae99SBarry Smith @*/
1757e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
175847c6ae99SBarry Smith {
175947c6ae99SBarry Smith   PetscErrorCode ierr;
176047c6ae99SBarry Smith 
176147c6ae99SBarry Smith   PetscFunctionBegin;
1762171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1763171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
176447c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
176547c6ae99SBarry Smith   PetscFunctionReturn(0);
176647c6ae99SBarry Smith }
176747c6ae99SBarry Smith 
176847c6ae99SBarry Smith #undef __FUNCT__
17691a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
17701a266240SBarry Smith /*@C
17711a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
17721a266240SBarry Smith 
17731a266240SBarry Smith     Not Collective
17741a266240SBarry Smith 
17751a266240SBarry Smith     Input Parameters:
17761a266240SBarry Smith +   dm - the DM object
17771a266240SBarry Smith -   destroy - the destroy function
17781a266240SBarry Smith 
17791a266240SBarry Smith     Level: intermediate
17801a266240SBarry Smith 
1781e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
17821a266240SBarry Smith 
1783f07f9ceaSJed Brown @*/
17841a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
17851a266240SBarry Smith {
17861a266240SBarry Smith   PetscFunctionBegin;
1787171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17881a266240SBarry Smith   dm->ctxdestroy = destroy;
17891a266240SBarry Smith   PetscFunctionReturn(0);
17901a266240SBarry Smith }
17911a266240SBarry Smith 
17921a266240SBarry Smith #undef __FUNCT__
17931b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1794b07ff414SBarry Smith /*@
17951b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
179647c6ae99SBarry Smith 
179747c6ae99SBarry Smith     Not Collective
179847c6ae99SBarry Smith 
179947c6ae99SBarry Smith     Input Parameters:
180047c6ae99SBarry Smith +   dm - the DM object
180147c6ae99SBarry Smith -   ctx - the user context
180247c6ae99SBarry Smith 
180347c6ae99SBarry Smith     Level: intermediate
180447c6ae99SBarry Smith 
1805e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
180647c6ae99SBarry Smith 
180747c6ae99SBarry Smith @*/
18081b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
180947c6ae99SBarry Smith {
181047c6ae99SBarry Smith   PetscFunctionBegin;
1811171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
181247c6ae99SBarry Smith   dm->ctx = ctx;
181347c6ae99SBarry Smith   PetscFunctionReturn(0);
181447c6ae99SBarry Smith }
181547c6ae99SBarry Smith 
181647c6ae99SBarry Smith #undef __FUNCT__
18171b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
181847c6ae99SBarry Smith /*@
18191b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
182047c6ae99SBarry Smith 
182147c6ae99SBarry Smith     Not Collective
182247c6ae99SBarry Smith 
182347c6ae99SBarry Smith     Input Parameter:
182447c6ae99SBarry Smith .   dm - the DM object
182547c6ae99SBarry Smith 
182647c6ae99SBarry Smith     Output Parameter:
182747c6ae99SBarry Smith .   ctx - the user context
182847c6ae99SBarry Smith 
182947c6ae99SBarry Smith     Level: intermediate
183047c6ae99SBarry Smith 
1831e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
183247c6ae99SBarry Smith 
183347c6ae99SBarry Smith @*/
18341b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
183547c6ae99SBarry Smith {
183647c6ae99SBarry Smith   PetscFunctionBegin;
1837171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18381b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
183947c6ae99SBarry Smith   PetscFunctionReturn(0);
184047c6ae99SBarry Smith }
184147c6ae99SBarry Smith 
184247c6ae99SBarry Smith #undef __FUNCT__
184347c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
18447e833e3aSBarry Smith /*@C
184547c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
184647c6ae99SBarry Smith 
184747c6ae99SBarry Smith     Logically Collective on DM
184847c6ae99SBarry Smith 
184947c6ae99SBarry Smith     Input Parameter:
185047c6ae99SBarry Smith +   dm - the DM object to destroy
185147c6ae99SBarry Smith -   f - the function to compute the initial guess
185247c6ae99SBarry Smith 
185347c6ae99SBarry Smith     Level: intermediate
185447c6ae99SBarry Smith 
1855e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
185647c6ae99SBarry Smith 
1857f07f9ceaSJed Brown @*/
18587087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
185947c6ae99SBarry Smith {
186047c6ae99SBarry Smith   PetscFunctionBegin;
1861171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
186247c6ae99SBarry Smith   dm->ops->initialguess = f;
186347c6ae99SBarry Smith   PetscFunctionReturn(0);
186447c6ae99SBarry Smith }
186547c6ae99SBarry Smith 
186647c6ae99SBarry Smith #undef __FUNCT__
186747c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
18687e833e3aSBarry Smith /*@C
186947c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
187047c6ae99SBarry Smith 
187147c6ae99SBarry Smith     Logically Collective on DM
187247c6ae99SBarry Smith 
187347c6ae99SBarry Smith     Input Parameter:
187447c6ae99SBarry Smith +   dm - the DM object
187547c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
187647c6ae99SBarry Smith 
187747c6ae99SBarry Smith     Level: intermediate
187847c6ae99SBarry Smith 
187947c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
188047c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
188147c6ae99SBarry Smith 
1882e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
188347c6ae99SBarry Smith          DMSetJacobian()
188447c6ae99SBarry Smith 
1885f07f9ceaSJed Brown @*/
18867087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
188747c6ae99SBarry Smith {
188847c6ae99SBarry Smith   PetscFunctionBegin;
1889171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
189047c6ae99SBarry Smith   dm->ops->function = f;
189147c6ae99SBarry Smith   if (f) {
189247c6ae99SBarry Smith     dm->ops->functionj = f;
189347c6ae99SBarry Smith   }
189447c6ae99SBarry Smith   PetscFunctionReturn(0);
189547c6ae99SBarry Smith }
189647c6ae99SBarry Smith 
189747c6ae99SBarry Smith #undef __FUNCT__
189847c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
18997e833e3aSBarry Smith /*@C
190047c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
190147c6ae99SBarry Smith 
190247c6ae99SBarry Smith     Logically Collective on DM
190347c6ae99SBarry Smith 
190447c6ae99SBarry Smith     Input Parameter:
190547c6ae99SBarry Smith +   dm - the DM object to destroy
190647c6ae99SBarry Smith -   f - the function to compute the matrix entries
190747c6ae99SBarry Smith 
190847c6ae99SBarry Smith     Level: intermediate
190947c6ae99SBarry Smith 
1910e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
191147c6ae99SBarry Smith          DMSetFunction()
191247c6ae99SBarry Smith 
1913f07f9ceaSJed Brown @*/
19147087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
191547c6ae99SBarry Smith {
191647c6ae99SBarry Smith   PetscFunctionBegin;
1917171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
191847c6ae99SBarry Smith   dm->ops->jacobian = f;
191947c6ae99SBarry Smith   PetscFunctionReturn(0);
192047c6ae99SBarry Smith }
192147c6ae99SBarry Smith 
192247c6ae99SBarry Smith #undef __FUNCT__
192308da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
192408da532bSDmitry Karpeev /*@C
192508da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
192608da532bSDmitry Karpeev 
192708da532bSDmitry Karpeev     Logically Collective on DM
192808da532bSDmitry Karpeev 
192908da532bSDmitry Karpeev     Input Parameter:
193008da532bSDmitry Karpeev +   dm - the DM object
193108da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
193208da532bSDmitry Karpeev 
193308da532bSDmitry Karpeev     Level: intermediate
193408da532bSDmitry Karpeev 
1935e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
193608da532bSDmitry Karpeev          DMSetJacobian()
193708da532bSDmitry Karpeev 
193808da532bSDmitry Karpeev @*/
193908da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
194008da532bSDmitry Karpeev {
194108da532bSDmitry Karpeev   PetscFunctionBegin;
194208da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
194308da532bSDmitry Karpeev   PetscFunctionReturn(0);
194408da532bSDmitry Karpeev }
194508da532bSDmitry Karpeev 
194608da532bSDmitry Karpeev #undef __FUNCT__
194708da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
194808da532bSDmitry Karpeev /*@
194908da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
195008da532bSDmitry Karpeev 
195108da532bSDmitry Karpeev     Not Collective
195208da532bSDmitry Karpeev 
195308da532bSDmitry Karpeev     Input Parameter:
195408da532bSDmitry Karpeev .   dm - the DM object to destroy
195508da532bSDmitry Karpeev 
195608da532bSDmitry Karpeev     Output Parameter:
195708da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
195808da532bSDmitry Karpeev 
195908da532bSDmitry Karpeev     Level: developer
196008da532bSDmitry Karpeev 
1961e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
196208da532bSDmitry Karpeev 
196308da532bSDmitry Karpeev @*/
196408da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
196508da532bSDmitry Karpeev {
196608da532bSDmitry Karpeev   PetscFunctionBegin;
196708da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
196808da532bSDmitry Karpeev   PetscFunctionReturn(0);
196908da532bSDmitry Karpeev }
197008da532bSDmitry Karpeev 
197108da532bSDmitry Karpeev #undef __FUNCT__
197208da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
197308da532bSDmitry Karpeev /*@C
197408da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
197508da532bSDmitry Karpeev 
197608da532bSDmitry Karpeev     Logically Collective on DM
197708da532bSDmitry Karpeev 
197808da532bSDmitry Karpeev     Input Parameters:
197908da532bSDmitry Karpeev +   dm - the DM object to destroy
198008da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
198108da532bSDmitry Karpeev 
198208da532bSDmitry Karpeev     Output parameters:
198308da532bSDmitry Karpeev +   xl - lower bound
198408da532bSDmitry Karpeev -   xu - upper bound
198508da532bSDmitry Karpeev 
198608da532bSDmitry Karpeev     Level: intermediate
198708da532bSDmitry Karpeev 
1988e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
198908da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
199008da532bSDmitry Karpeev 
199108da532bSDmitry Karpeev @*/
199208da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
199308da532bSDmitry Karpeev {
199408da532bSDmitry Karpeev   PetscErrorCode ierr;
199508da532bSDmitry Karpeev   PetscFunctionBegin;
199608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
199708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
199808da532bSDmitry Karpeev   if(dm->ops->computevariablebounds) {
199908da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
200008da532bSDmitry Karpeev   }
200108da532bSDmitry Karpeev   else {
200208da532bSDmitry Karpeev     ierr = VecSet(xl,SNES_VI_NINF); CHKERRQ(ierr);
200308da532bSDmitry Karpeev     ierr = VecSet(xu,SNES_VI_INF);  CHKERRQ(ierr);
200408da532bSDmitry Karpeev   }
200508da532bSDmitry Karpeev   PetscFunctionReturn(0);
200608da532bSDmitry Karpeev }
200708da532bSDmitry Karpeev 
200808da532bSDmitry Karpeev #undef __FUNCT__
200947c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
201047c6ae99SBarry Smith /*@
201147c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
201247c6ae99SBarry Smith 
201347c6ae99SBarry Smith     Collective on DM
201447c6ae99SBarry Smith 
201547c6ae99SBarry Smith     Input Parameter:
201647c6ae99SBarry Smith +   dm - the DM object to destroy
201747c6ae99SBarry Smith -   x - the vector to hold the initial guess values
201847c6ae99SBarry Smith 
201947c6ae99SBarry Smith     Level: developer
202047c6ae99SBarry Smith 
2021e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
202247c6ae99SBarry Smith 
202347c6ae99SBarry Smith @*/
20247087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
202547c6ae99SBarry Smith {
202647c6ae99SBarry Smith   PetscErrorCode ierr;
202747c6ae99SBarry Smith   PetscFunctionBegin;
2028171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
202947c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
203047c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
203147c6ae99SBarry Smith   PetscFunctionReturn(0);
203247c6ae99SBarry Smith }
203347c6ae99SBarry Smith 
203447c6ae99SBarry Smith #undef __FUNCT__
203547c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
203647c6ae99SBarry Smith /*@
203747c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
203847c6ae99SBarry Smith 
203947c6ae99SBarry Smith     Not Collective
204047c6ae99SBarry Smith 
204147c6ae99SBarry Smith     Input Parameter:
204247c6ae99SBarry Smith .   dm - the DM object to destroy
204347c6ae99SBarry Smith 
204447c6ae99SBarry Smith     Output Parameter:
204547c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
204647c6ae99SBarry Smith 
204747c6ae99SBarry Smith     Level: developer
204847c6ae99SBarry Smith 
2049e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
205047c6ae99SBarry Smith 
205147c6ae99SBarry Smith @*/
20527087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
205347c6ae99SBarry Smith {
205447c6ae99SBarry Smith   PetscFunctionBegin;
2055171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
205647c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
205747c6ae99SBarry Smith   PetscFunctionReturn(0);
205847c6ae99SBarry Smith }
205947c6ae99SBarry Smith 
206047c6ae99SBarry Smith #undef __FUNCT__
206147c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
206247c6ae99SBarry Smith /*@
206347c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
206447c6ae99SBarry Smith 
206547c6ae99SBarry Smith     Not Collective
206647c6ae99SBarry Smith 
206747c6ae99SBarry Smith     Input Parameter:
206847c6ae99SBarry Smith .   dm - the DM object to destroy
206947c6ae99SBarry Smith 
207047c6ae99SBarry Smith     Output Parameter:
207147c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
207247c6ae99SBarry Smith 
207347c6ae99SBarry Smith     Level: developer
207447c6ae99SBarry Smith 
2075e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
207647c6ae99SBarry Smith 
207747c6ae99SBarry Smith @*/
20787087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
207947c6ae99SBarry Smith {
208047c6ae99SBarry Smith   PetscFunctionBegin;
2081171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
208247c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
208347c6ae99SBarry Smith   PetscFunctionReturn(0);
208447c6ae99SBarry Smith }
208547c6ae99SBarry Smith 
208647c6ae99SBarry Smith #undef __FUNCT__
208747c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
208847c6ae99SBarry Smith /*@
208947c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
209047c6ae99SBarry Smith 
209147c6ae99SBarry Smith     Not Collective
209247c6ae99SBarry Smith 
209347c6ae99SBarry Smith     Input Parameter:
209447c6ae99SBarry Smith .   dm - the DM object to destroy
209547c6ae99SBarry Smith 
209647c6ae99SBarry Smith     Output Parameter:
209747c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
209847c6ae99SBarry Smith 
209947c6ae99SBarry Smith     Level: developer
210047c6ae99SBarry Smith 
2101e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
210247c6ae99SBarry Smith 
210347c6ae99SBarry Smith @*/
21047087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
210547c6ae99SBarry Smith {
210647c6ae99SBarry Smith   PetscFunctionBegin;
2107171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
210847c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
210947c6ae99SBarry Smith   PetscFunctionReturn(0);
211047c6ae99SBarry Smith }
211147c6ae99SBarry Smith 
211247c6ae99SBarry Smith #undef  __FUNCT__
211308da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2114748fac09SDmitry Karpeev /*@C
211508da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
211608da532bSDmitry Karpeev 
211708da532bSDmitry Karpeev     Collective on DM
211808da532bSDmitry Karpeev 
211908da532bSDmitry Karpeev     Input Parameter:
212008da532bSDmitry Karpeev +   dm - the DM object
2121e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
212208da532bSDmitry Karpeev 
212308da532bSDmitry Karpeev     Level: developer
212408da532bSDmitry Karpeev 
2125e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
212608da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
212708da532bSDmitry Karpeev 
212808da532bSDmitry Karpeev @*/
212908da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
213008da532bSDmitry Karpeev {
213108da532bSDmitry Karpeev   PetscErrorCode ierr;
213208da532bSDmitry Karpeev   PetscFunctionBegin;
213308da532bSDmitry Karpeev   if (x) {
213408da532bSDmitry Karpeev     if (!dm->x) {
213508da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
213608da532bSDmitry Karpeev     }
213708da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
213808da532bSDmitry Karpeev   }
213908da532bSDmitry Karpeev   else if(dm->x) {
214008da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
214108da532bSDmitry Karpeev   }
214208da532bSDmitry Karpeev   PetscFunctionReturn(0);
214308da532bSDmitry Karpeev }
214408da532bSDmitry Karpeev 
214508da532bSDmitry Karpeev 
214608da532bSDmitry Karpeev #undef __FUNCT__
214747c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
214847c6ae99SBarry Smith /*@
214947c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
215047c6ae99SBarry Smith 
215147c6ae99SBarry Smith     Collective on DM
215247c6ae99SBarry Smith 
215347c6ae99SBarry Smith     Input Parameter:
215447c6ae99SBarry Smith +   dm - the DM object to destroy
215547c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
215647c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
215747c6ae99SBarry Smith 
215847c6ae99SBarry Smith     Level: developer
215947c6ae99SBarry Smith 
2160e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
216147c6ae99SBarry Smith          DMSetJacobian()
216247c6ae99SBarry Smith 
216347c6ae99SBarry Smith @*/
21647087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
216547c6ae99SBarry Smith {
216647c6ae99SBarry Smith   PetscErrorCode ierr;
216747c6ae99SBarry Smith   PetscFunctionBegin;
2168171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
216947c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
2170644e2e5bSBarry Smith   PetscStackPush("DM user function");
217147c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
2172644e2e5bSBarry Smith   PetscStackPop;
217347c6ae99SBarry Smith   PetscFunctionReturn(0);
217447c6ae99SBarry Smith }
217547c6ae99SBarry Smith 
217647c6ae99SBarry Smith 
217708da532bSDmitry Karpeev 
217847c6ae99SBarry Smith #undef __FUNCT__
217947c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
218047c6ae99SBarry Smith /*@
218147c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
218247c6ae99SBarry Smith 
218347c6ae99SBarry Smith     Collective on DM
218447c6ae99SBarry Smith 
218547c6ae99SBarry Smith     Input Parameter:
218647c6ae99SBarry Smith +   dm - the DM object
2187cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
218847c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
218947c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
219047c6ae99SBarry Smith 
219147c6ae99SBarry Smith     Level: developer
219247c6ae99SBarry Smith 
2193e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
219447c6ae99SBarry Smith          DMSetFunction()
219547c6ae99SBarry Smith 
219647c6ae99SBarry Smith @*/
21977087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
219847c6ae99SBarry Smith {
219947c6ae99SBarry Smith   PetscErrorCode ierr;
220047c6ae99SBarry Smith 
220147c6ae99SBarry Smith   PetscFunctionBegin;
2202171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
220347c6ae99SBarry Smith   if (!dm->ops->jacobian) {
220447c6ae99SBarry Smith     ISColoring     coloring;
220547c6ae99SBarry Smith     MatFDColoring  fd;
22062c9966d7SBarry Smith     const MatType  mtype;
220747c6ae99SBarry Smith 
22082c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
22092c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
221047c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
2211fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
221247c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
22130bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
22140bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
221571cd77b2SBarry Smith 
221647c6ae99SBarry Smith     dm->fd = fd;
221747c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
22182533e041SBarry Smith 
221971cd77b2SBarry Smith     /* don't know why this is needed */
222071cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
222147c6ae99SBarry Smith   }
222247c6ae99SBarry Smith   if (!x) x = dm->x;
222347c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
2224cab2e9ccSBarry Smith 
222571cd77b2SBarry 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 */
2226649052a6SBarry Smith   if (x) {
2227cab2e9ccSBarry Smith     if (!dm->x) {
222871cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
2229cab2e9ccSBarry Smith     }
2230cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
2231649052a6SBarry Smith   }
2232a8248277SBarry Smith   if (A != B) {
2233a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2234a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2235a8248277SBarry Smith   }
223647c6ae99SBarry Smith   PetscFunctionReturn(0);
223747c6ae99SBarry Smith }
2238264ace61SBarry Smith 
2239cab2e9ccSBarry Smith 
2240264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2241264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2242264ace61SBarry Smith 
2243264ace61SBarry Smith #undef __FUNCT__
2244264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2245264ace61SBarry Smith /*@C
2246264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2247264ace61SBarry Smith 
2248264ace61SBarry Smith   Collective on DM
2249264ace61SBarry Smith 
2250264ace61SBarry Smith   Input Parameters:
2251264ace61SBarry Smith + dm     - The DM object
2252264ace61SBarry Smith - method - The name of the DM type
2253264ace61SBarry Smith 
2254264ace61SBarry Smith   Options Database Key:
2255264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2256264ace61SBarry Smith 
2257264ace61SBarry Smith   Notes:
2258e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2259264ace61SBarry Smith 
2260264ace61SBarry Smith   Level: intermediate
2261264ace61SBarry Smith 
2262264ace61SBarry Smith .keywords: DM, set, type
2263264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2264264ace61SBarry Smith @*/
22657087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
2266264ace61SBarry Smith {
2267264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2268264ace61SBarry Smith   PetscBool      match;
2269264ace61SBarry Smith   PetscErrorCode ierr;
2270264ace61SBarry Smith 
2271264ace61SBarry Smith   PetscFunctionBegin;
2272264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2273251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2274264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2275264ace61SBarry Smith 
2276264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
22774b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2278264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2279264ace61SBarry Smith 
2280264ace61SBarry Smith   if (dm->ops->destroy) {
2281264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2282b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2283264ace61SBarry Smith   }
2284264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2285264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2286264ace61SBarry Smith   PetscFunctionReturn(0);
2287264ace61SBarry Smith }
2288264ace61SBarry Smith 
2289264ace61SBarry Smith #undef __FUNCT__
2290264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2291264ace61SBarry Smith /*@C
2292264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2293264ace61SBarry Smith 
2294264ace61SBarry Smith   Not Collective
2295264ace61SBarry Smith 
2296264ace61SBarry Smith   Input Parameter:
2297264ace61SBarry Smith . dm  - The DM
2298264ace61SBarry Smith 
2299264ace61SBarry Smith   Output Parameter:
2300264ace61SBarry Smith . type - The DM type name
2301264ace61SBarry Smith 
2302264ace61SBarry Smith   Level: intermediate
2303264ace61SBarry Smith 
2304264ace61SBarry Smith .keywords: DM, get, type, name
2305264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2306264ace61SBarry Smith @*/
23077087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
2308264ace61SBarry Smith {
2309264ace61SBarry Smith   PetscErrorCode ierr;
2310264ace61SBarry Smith 
2311264ace61SBarry Smith   PetscFunctionBegin;
2312264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2313264ace61SBarry Smith   PetscValidCharPointer(type,2);
2314264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2315264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2316264ace61SBarry Smith   }
2317264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2318264ace61SBarry Smith   PetscFunctionReturn(0);
2319264ace61SBarry Smith }
2320264ace61SBarry Smith 
232167a56275SMatthew G Knepley #undef __FUNCT__
232267a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
232367a56275SMatthew G Knepley /*@C
232467a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
232567a56275SMatthew G Knepley 
232667a56275SMatthew G Knepley   Collective on DM
232767a56275SMatthew G Knepley 
232867a56275SMatthew G Knepley   Input Parameters:
232967a56275SMatthew G Knepley + dm - the DM
233067a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
233167a56275SMatthew G Knepley 
233267a56275SMatthew G Knepley   Output Parameter:
233367a56275SMatthew G Knepley . M - pointer to new DM
233467a56275SMatthew G Knepley 
233567a56275SMatthew G Knepley   Notes:
233667a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
233767a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
233867a56275SMatthew G Knepley   of the input DM.
233967a56275SMatthew G Knepley 
234067a56275SMatthew G Knepley   Level: intermediate
234167a56275SMatthew G Knepley 
234267a56275SMatthew G Knepley .seealso: DMCreate()
234367a56275SMatthew G Knepley @*/
234467a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
234567a56275SMatthew G Knepley {
234667a56275SMatthew G Knepley   DM             B;
234767a56275SMatthew G Knepley   char           convname[256];
234867a56275SMatthew G Knepley   PetscBool      sametype, issame;
234967a56275SMatthew G Knepley   PetscErrorCode ierr;
235067a56275SMatthew G Knepley 
235167a56275SMatthew G Knepley   PetscFunctionBegin;
235267a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
235367a56275SMatthew G Knepley   PetscValidType(dm,1);
235467a56275SMatthew G Knepley   PetscValidPointer(M,3);
2355251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
235667a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
235767a56275SMatthew G Knepley   {
235867a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
235967a56275SMatthew G Knepley 
236067a56275SMatthew G Knepley     /*
236167a56275SMatthew G Knepley        Order of precedence:
236267a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
236367a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
236467a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
236567a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
236667a56275SMatthew G Knepley        5) Use a really basic converter.
236767a56275SMatthew G Knepley     */
236867a56275SMatthew G Knepley 
236967a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
237067a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
237167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
237267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
237367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
237467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
237567a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
237667a56275SMatthew G Knepley     if (conv) goto foundconv;
237767a56275SMatthew G Knepley 
237867a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
237967a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
238067a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
238167a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
238267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
238367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
238467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
238567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
238667a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
238767a56275SMatthew G Knepley     if (conv) {
2388fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
238967a56275SMatthew G Knepley       goto foundconv;
239067a56275SMatthew G Knepley     }
239167a56275SMatthew G Knepley 
239267a56275SMatthew G Knepley #if 0
239367a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
239467a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2395fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
239667a56275SMatthew G Knepley     if (conv) goto foundconv;
239767a56275SMatthew G Knepley 
239867a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
239967a56275SMatthew G Knepley     if (dm->ops->convert) {
240067a56275SMatthew G Knepley       conv = dm->ops->convert;
240167a56275SMatthew G Knepley     }
240267a56275SMatthew G Knepley     if (conv) goto foundconv;
240367a56275SMatthew G Knepley #endif
240467a56275SMatthew G Knepley 
240567a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
240667a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
240767a56275SMatthew G Knepley 
240867a56275SMatthew G Knepley     foundconv:
240967a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
241067a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
241167a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
241267a56275SMatthew G Knepley   }
241367a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
241467a56275SMatthew G Knepley   PetscFunctionReturn(0);
241567a56275SMatthew G Knepley }
2416264ace61SBarry Smith 
2417264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2418264ace61SBarry Smith 
2419264ace61SBarry Smith #undef __FUNCT__
2420264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2421264ace61SBarry Smith /*@C
2422264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2423264ace61SBarry Smith 
2424264ace61SBarry Smith   Level: advanced
2425264ace61SBarry Smith @*/
24267087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2427264ace61SBarry Smith {
2428264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2429264ace61SBarry Smith   PetscErrorCode ierr;
2430264ace61SBarry Smith 
2431264ace61SBarry Smith   PetscFunctionBegin;
2432264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2433264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2434264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2435264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2436264ace61SBarry Smith   PetscFunctionReturn(0);
2437264ace61SBarry Smith }
2438264ace61SBarry Smith 
2439264ace61SBarry Smith 
2440264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2441264ace61SBarry Smith #undef __FUNCT__
2442264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2443264ace61SBarry Smith /*@C
2444264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2445264ace61SBarry Smith 
2446264ace61SBarry Smith    Not Collective
2447264ace61SBarry Smith 
2448264ace61SBarry Smith    Level: advanced
2449264ace61SBarry Smith 
2450264ace61SBarry Smith .keywords: DM, register, destroy
2451264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2452264ace61SBarry Smith @*/
24537087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2454264ace61SBarry Smith {
2455264ace61SBarry Smith   PetscErrorCode ierr;
2456264ace61SBarry Smith 
2457264ace61SBarry Smith   PetscFunctionBegin;
2458264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2459264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2460264ace61SBarry Smith   PetscFunctionReturn(0);
2461264ace61SBarry Smith }
246223f975d1SBarry Smith 
246323f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2464c6db04a5SJed Brown #include <mex.h>
246523f975d1SBarry Smith 
24663014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
246723f975d1SBarry Smith 
246823f975d1SBarry Smith #undef __FUNCT__
246923f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
247023f975d1SBarry Smith /*
247123f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
247223f975d1SBarry Smith                          DMSetFunctionMatlab().
247323f975d1SBarry Smith 
247423f975d1SBarry Smith    For linear problems x is null
247523f975d1SBarry Smith 
247623f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
247723f975d1SBarry Smith */
24787087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
247923f975d1SBarry Smith {
248023f975d1SBarry Smith   PetscErrorCode    ierr;
248123f975d1SBarry Smith   DMMatlabContext   *sctx;
248223f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
248323f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
248423f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
248523f975d1SBarry Smith 
248623f975d1SBarry Smith   PetscFunctionBegin;
248723f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
248823f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
248923f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
249023f975d1SBarry Smith 
249123f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
24921b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
249323f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
249423f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
24953014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
249623f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
249723f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
249823f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
249923f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2500b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
250123f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
250223f975d1SBarry Smith   mxDestroyArray(prhs[0]);
250323f975d1SBarry Smith   mxDestroyArray(prhs[1]);
250423f975d1SBarry Smith   mxDestroyArray(prhs[2]);
250523f975d1SBarry Smith   mxDestroyArray(prhs[3]);
250623f975d1SBarry Smith   mxDestroyArray(plhs[0]);
250723f975d1SBarry Smith   PetscFunctionReturn(0);
250823f975d1SBarry Smith }
250923f975d1SBarry Smith 
251023f975d1SBarry Smith 
251123f975d1SBarry Smith #undef __FUNCT__
251223f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
251323f975d1SBarry Smith /*
251423f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
251523f975d1SBarry Smith 
251623f975d1SBarry Smith */
25177087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
251823f975d1SBarry Smith {
251923f975d1SBarry Smith   PetscErrorCode    ierr;
252023f975d1SBarry Smith   DMMatlabContext   *sctx;
252123f975d1SBarry Smith 
252223f975d1SBarry Smith   PetscFunctionBegin;
2523171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
252423f975d1SBarry Smith   /* currently sctx is memory bleed */
25251b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
25263014e516SBarry Smith   if (!sctx) {
252723f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
25283014e516SBarry Smith   }
252923f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
25301b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
253123f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
253223f975d1SBarry Smith   PetscFunctionReturn(0);
253323f975d1SBarry Smith }
25343014e516SBarry Smith 
25353014e516SBarry Smith #undef __FUNCT__
25363014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
25373014e516SBarry Smith /*
25383014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
25393014e516SBarry Smith                          DMSetJacobianMatlab().
25403014e516SBarry Smith 
25413014e516SBarry Smith    For linear problems x is null
25423014e516SBarry Smith 
25433014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
25443014e516SBarry Smith */
25457087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
25463014e516SBarry Smith {
25473014e516SBarry Smith   PetscErrorCode    ierr;
25483014e516SBarry Smith   DMMatlabContext   *sctx;
25493014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
25503014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
25513014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
25523014e516SBarry Smith 
25533014e516SBarry Smith   PetscFunctionBegin;
25543014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25553014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
25563014e516SBarry Smith 
2557e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
25581b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
25593014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
25603014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
25613014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
25623014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
25633014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
25643014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
25653014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
25663014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
25673014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2568b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2569c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2570c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
25713014e516SBarry Smith   mxDestroyArray(prhs[0]);
25723014e516SBarry Smith   mxDestroyArray(prhs[1]);
25733014e516SBarry Smith   mxDestroyArray(prhs[2]);
25743014e516SBarry Smith   mxDestroyArray(prhs[3]);
25753014e516SBarry Smith   mxDestroyArray(prhs[4]);
25763014e516SBarry Smith   mxDestroyArray(plhs[0]);
25773014e516SBarry Smith   mxDestroyArray(plhs[1]);
25783014e516SBarry Smith   PetscFunctionReturn(0);
25793014e516SBarry Smith }
25803014e516SBarry Smith 
25813014e516SBarry Smith 
25823014e516SBarry Smith #undef __FUNCT__
25833014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
25843014e516SBarry Smith /*
25853014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
25863014e516SBarry Smith 
25873014e516SBarry Smith */
25887087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
25893014e516SBarry Smith {
25903014e516SBarry Smith   PetscErrorCode    ierr;
25913014e516SBarry Smith   DMMatlabContext   *sctx;
25923014e516SBarry Smith 
25933014e516SBarry Smith   PetscFunctionBegin;
2594171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25953014e516SBarry Smith   /* currently sctx is memory bleed */
25961b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
25973014e516SBarry Smith   if (!sctx) {
25983014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
25993014e516SBarry Smith   }
26003014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
26011b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
26023014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
26033014e516SBarry Smith   PetscFunctionReturn(0);
26043014e516SBarry Smith }
260523f975d1SBarry Smith #endif
2606b859378eSBarry Smith 
2607b859378eSBarry Smith #undef __FUNCT__
2608b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2609b859378eSBarry Smith /*@C
2610b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2611b859378eSBarry Smith   with DMView().
2612b859378eSBarry Smith 
2613b859378eSBarry Smith   Collective on PetscViewer
2614b859378eSBarry Smith 
2615b859378eSBarry Smith   Input Parameters:
2616b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2617b859378eSBarry Smith            some related function before a call to DMLoad().
2618b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2619b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2620b859378eSBarry Smith 
2621b859378eSBarry Smith    Level: intermediate
2622b859378eSBarry Smith 
2623b859378eSBarry Smith   Notes:
2624b859378eSBarry Smith   Defaults to the DM DA.
2625b859378eSBarry Smith 
2626b859378eSBarry Smith   Notes for advanced users:
2627b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2628b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2629b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2630b859378eSBarry Smith   format is
2631b859378eSBarry Smith .vb
2632b859378eSBarry Smith      has not yet been determined
2633b859378eSBarry Smith .ve
2634b859378eSBarry Smith 
2635b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2636b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2637b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2638b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2639b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2640b859378eSBarry Smith 
2641b859378eSBarry Smith   Concepts: vector^loading from file
2642b859378eSBarry Smith 
2643b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2644b859378eSBarry Smith @*/
2645b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2646b859378eSBarry Smith {
2647b859378eSBarry Smith   PetscErrorCode ierr;
2648b859378eSBarry Smith 
2649b859378eSBarry Smith   PetscFunctionBegin;
2650b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2651b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2652b859378eSBarry Smith 
2653b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2654b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2655b859378eSBarry Smith   }
2656b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2657b859378eSBarry Smith   PetscFunctionReturn(0);
2658b859378eSBarry Smith }
2659b859378eSBarry Smith 
26607da65231SMatthew G Knepley /******************************** FEM Support **********************************/
26617da65231SMatthew G Knepley 
26627da65231SMatthew G Knepley #undef __FUNCT__
26637da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
26647da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
26651d47ebbbSSatish Balay   PetscInt       f;
26661b30c384SMatthew G Knepley   PetscErrorCode ierr;
26671b30c384SMatthew G Knepley 
26687da65231SMatthew G Knepley   PetscFunctionBegin;
266974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26701d47ebbbSSatish Balay   for(f = 0; f < len; ++f) {
267174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
26727da65231SMatthew G Knepley   }
26737da65231SMatthew G Knepley   PetscFunctionReturn(0);
26747da65231SMatthew G Knepley }
26757da65231SMatthew G Knepley 
26767da65231SMatthew G Knepley #undef __FUNCT__
26777da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
26787da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
26791b30c384SMatthew G Knepley   PetscInt       f, g;
26807da65231SMatthew G Knepley   PetscErrorCode ierr;
26817da65231SMatthew G Knepley 
26827da65231SMatthew G Knepley   PetscFunctionBegin;
268374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26841d47ebbbSSatish Balay   for(f = 0; f < rows; ++f) {
268574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
26861d47ebbbSSatish Balay     for(g = 0; g < cols; ++g) {
268774778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
26887da65231SMatthew G Knepley     }
268974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
26907da65231SMatthew G Knepley   }
26917da65231SMatthew G Knepley   PetscFunctionReturn(0);
26927da65231SMatthew G Knepley }
2693e7c4fc90SDmitry Karpeev 
2694970e74d5SMatthew G Knepley #undef __FUNCT__
2695970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
2696970e74d5SMatthew G Knepley /*@C
2697970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
2698970e74d5SMatthew G Knepley 
2699970e74d5SMatthew G Knepley   Not collective
2700970e74d5SMatthew G Knepley 
2701970e74d5SMatthew G Knepley   Input Parameter:
2702970e74d5SMatthew G Knepley . dm - The DM
2703970e74d5SMatthew G Knepley 
2704970e74d5SMatthew G Knepley   Output Parameter:
2705970e74d5SMatthew G Knepley . lf - The local residual function
2706970e74d5SMatthew G Knepley 
2707970e74d5SMatthew G Knepley    Calling sequence of lf:
2708970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2709970e74d5SMatthew G Knepley 
2710970e74d5SMatthew G Knepley +  snes - the SNES context
2711970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2712970e74d5SMatthew G Knepley .  f - local vector to put residual in
2713970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2714970e74d5SMatthew G Knepley 
2715970e74d5SMatthew G Knepley   Level: intermediate
2716970e74d5SMatthew G Knepley 
2717970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2718970e74d5SMatthew G Knepley @*/
2719970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
2720970e74d5SMatthew G Knepley {
2721970e74d5SMatthew G Knepley   PetscFunctionBegin;
2722970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2723970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
2724970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2725970e74d5SMatthew G Knepley }
2726970e74d5SMatthew G Knepley 
2727970e74d5SMatthew G Knepley #undef __FUNCT__
2728970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
2729970e74d5SMatthew G Knepley /*@C
2730970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
2731970e74d5SMatthew G Knepley 
2732970e74d5SMatthew G Knepley   Not collective
2733970e74d5SMatthew G Knepley 
2734970e74d5SMatthew G Knepley   Input Parameters:
2735970e74d5SMatthew G Knepley + dm - The DM
2736970e74d5SMatthew G Knepley - lf - The local residual function
2737970e74d5SMatthew G Knepley 
2738970e74d5SMatthew G Knepley    Calling sequence of lf:
2739970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2740970e74d5SMatthew G Knepley 
2741970e74d5SMatthew G Knepley +  snes - the SNES context
2742970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2743970e74d5SMatthew G Knepley .  f - local vector to put residual in
2744970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2745970e74d5SMatthew G Knepley 
2746970e74d5SMatthew G Knepley   Level: intermediate
2747970e74d5SMatthew G Knepley 
2748970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2749970e74d5SMatthew G Knepley @*/
2750970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
2751970e74d5SMatthew G Knepley {
2752970e74d5SMatthew G Knepley   PetscFunctionBegin;
2753970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2754970e74d5SMatthew G Knepley   dm->lf = lf;
2755970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2756970e74d5SMatthew G Knepley }
2757970e74d5SMatthew G Knepley 
2758970e74d5SMatthew G Knepley #undef __FUNCT__
2759970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
2760970e74d5SMatthew G Knepley /*@C
2761970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
2762970e74d5SMatthew G Knepley 
2763970e74d5SMatthew G Knepley   Not collective
2764970e74d5SMatthew G Knepley 
2765970e74d5SMatthew G Knepley   Input Parameter:
2766970e74d5SMatthew G Knepley . dm - The DM
2767970e74d5SMatthew G Knepley 
2768970e74d5SMatthew G Knepley   Output Parameter:
2769970e74d5SMatthew G Knepley . lj - The local Jacobian function
2770970e74d5SMatthew G Knepley 
2771970e74d5SMatthew G Knepley    Calling sequence of lj:
2772970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2773970e74d5SMatthew G Knepley 
2774970e74d5SMatthew G Knepley +  snes - the SNES context
2775970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2776970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2777970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2778970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2779970e74d5SMatthew G Knepley 
2780970e74d5SMatthew G Knepley   Level: intermediate
2781970e74d5SMatthew G Knepley 
2782970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2783970e74d5SMatthew G Knepley @*/
2784970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
2785970e74d5SMatthew G Knepley {
2786970e74d5SMatthew G Knepley   PetscFunctionBegin;
2787970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2788970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
2789970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2790970e74d5SMatthew G Knepley }
2791970e74d5SMatthew G Knepley 
2792970e74d5SMatthew G Knepley #undef __FUNCT__
2793970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
2794970e74d5SMatthew G Knepley /*@C
2795970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
2796970e74d5SMatthew G Knepley 
2797970e74d5SMatthew G Knepley   Not collective
2798970e74d5SMatthew G Knepley 
2799970e74d5SMatthew G Knepley   Input Parameters:
2800970e74d5SMatthew G Knepley + dm - The DM
2801970e74d5SMatthew G Knepley - lj - The local Jacobian function
2802970e74d5SMatthew G Knepley 
2803970e74d5SMatthew G Knepley    Calling sequence of lj:
2804970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2805970e74d5SMatthew G Knepley 
2806970e74d5SMatthew G Knepley +  snes - the SNES context
2807970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2808970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2809970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2810970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2811970e74d5SMatthew G Knepley 
2812970e74d5SMatthew G Knepley   Level: intermediate
2813970e74d5SMatthew G Knepley 
2814970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2815970e74d5SMatthew G Knepley @*/
2816970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
2817970e74d5SMatthew G Knepley {
2818970e74d5SMatthew G Knepley   PetscFunctionBegin;
2819970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2820970e74d5SMatthew G Knepley   dm->lj = lj;
2821970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2822970e74d5SMatthew G Knepley }
282388ed4aceSMatthew G Knepley 
282488ed4aceSMatthew G Knepley #undef __FUNCT__
282588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
282688ed4aceSMatthew G Knepley /*@
282788ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
282888ed4aceSMatthew G Knepley 
282988ed4aceSMatthew G Knepley   Input Parameter:
283088ed4aceSMatthew G Knepley . dm - The DM
283188ed4aceSMatthew G Knepley 
283288ed4aceSMatthew G Knepley   Output Parameter:
283388ed4aceSMatthew G Knepley . section - The PetscSection
283488ed4aceSMatthew G Knepley 
283588ed4aceSMatthew G Knepley   Level: intermediate
283688ed4aceSMatthew G Knepley 
283788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
283888ed4aceSMatthew G Knepley 
283988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
284088ed4aceSMatthew G Knepley @*/
284188ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
284288ed4aceSMatthew G Knepley   PetscFunctionBegin;
284388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
284488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
284588ed4aceSMatthew G Knepley   *section = dm->defaultSection;
284688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
284788ed4aceSMatthew G Knepley }
284888ed4aceSMatthew G Knepley 
284988ed4aceSMatthew G Knepley #undef __FUNCT__
285088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
285188ed4aceSMatthew G Knepley /*@
285288ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
285388ed4aceSMatthew G Knepley 
285488ed4aceSMatthew G Knepley   Input Parameters:
285588ed4aceSMatthew G Knepley + dm - The DM
285688ed4aceSMatthew G Knepley - section - The PetscSection
285788ed4aceSMatthew G Knepley 
285888ed4aceSMatthew G Knepley   Level: intermediate
285988ed4aceSMatthew G Knepley 
286088ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
286188ed4aceSMatthew G Knepley 
286288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
286388ed4aceSMatthew G Knepley @*/
286488ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
286588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
286688ed4aceSMatthew G Knepley 
286788ed4aceSMatthew G Knepley   PetscFunctionBegin;
286888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
286988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
287088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
287188ed4aceSMatthew G Knepley   dm->defaultSection = section;
287288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
287388ed4aceSMatthew G Knepley }
287488ed4aceSMatthew G Knepley 
287588ed4aceSMatthew G Knepley #undef __FUNCT__
287688ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
287788ed4aceSMatthew G Knepley /*@
287888ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
287988ed4aceSMatthew G Knepley 
288088ed4aceSMatthew G Knepley   Input Parameter:
288188ed4aceSMatthew G Knepley . dm - The DM
288288ed4aceSMatthew G Knepley 
288388ed4aceSMatthew G Knepley   Output Parameter:
288488ed4aceSMatthew G Knepley . section - The PetscSection
288588ed4aceSMatthew G Knepley 
288688ed4aceSMatthew G Knepley   Level: intermediate
288788ed4aceSMatthew G Knepley 
288888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
288988ed4aceSMatthew G Knepley 
289088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
289188ed4aceSMatthew G Knepley @*/
289288ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
289388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
289488ed4aceSMatthew G Knepley 
289588ed4aceSMatthew G Knepley   PetscFunctionBegin;
289688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
289788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
289888ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
289988ed4aceSMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, &dm->defaultGlobalSection);CHKERRQ(ierr);
290088ed4aceSMatthew G Knepley   }
290188ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
290288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
290388ed4aceSMatthew G Knepley }
290488ed4aceSMatthew G Knepley 
290588ed4aceSMatthew G Knepley #undef __FUNCT__
290688ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
290788ed4aceSMatthew G Knepley /*@
290888ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
290988ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
291088ed4aceSMatthew G Knepley 
291188ed4aceSMatthew G Knepley   Input Parameter:
291288ed4aceSMatthew G Knepley . dm - The DM
291388ed4aceSMatthew G Knepley 
291488ed4aceSMatthew G Knepley   Output Parameter:
291588ed4aceSMatthew G Knepley . sf - The PetscSF
291688ed4aceSMatthew G Knepley 
291788ed4aceSMatthew G Knepley   Level: intermediate
291888ed4aceSMatthew G Knepley 
291988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
292088ed4aceSMatthew G Knepley 
292188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
292288ed4aceSMatthew G Knepley @*/
292388ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
292488ed4aceSMatthew G Knepley   PetscInt       nroots;
292588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
292688ed4aceSMatthew G Knepley 
292788ed4aceSMatthew G Knepley   PetscFunctionBegin;
292888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
292988ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
293088ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
293188ed4aceSMatthew G Knepley   if (nroots < 0) {
293288ed4aceSMatthew G Knepley     PetscSection section, gSection;
293388ed4aceSMatthew G Knepley 
293488ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
293531ea6d37SMatthew G Knepley     if (section) {
293688ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
293788ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
293831ea6d37SMatthew G Knepley     } else {
293931ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
294031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
294131ea6d37SMatthew G Knepley     }
294288ed4aceSMatthew G Knepley   }
294388ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
294488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
294588ed4aceSMatthew G Knepley }
294688ed4aceSMatthew G Knepley 
294788ed4aceSMatthew G Knepley #undef __FUNCT__
294888ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
294988ed4aceSMatthew G Knepley /*@
295088ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
295188ed4aceSMatthew G Knepley 
295288ed4aceSMatthew G Knepley   Input Parameters:
295388ed4aceSMatthew G Knepley + dm - The DM
295488ed4aceSMatthew G Knepley - sf - The PetscSF
295588ed4aceSMatthew G Knepley 
295688ed4aceSMatthew G Knepley   Level: intermediate
295788ed4aceSMatthew G Knepley 
295888ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
295988ed4aceSMatthew G Knepley 
296088ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
296188ed4aceSMatthew G Knepley @*/
296288ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
296388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
296488ed4aceSMatthew G Knepley 
296588ed4aceSMatthew G Knepley   PetscFunctionBegin;
296688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
296788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
296888ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
296988ed4aceSMatthew G Knepley   dm->defaultSF = sf;
297088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
297188ed4aceSMatthew G Knepley }
297288ed4aceSMatthew G Knepley 
297388ed4aceSMatthew G Knepley #undef __FUNCT__
297488ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
297588ed4aceSMatthew G Knepley /*@C
297688ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
297788ed4aceSMatthew G Knepley   describing the data layout.
297888ed4aceSMatthew G Knepley 
297988ed4aceSMatthew G Knepley   Input Parameters:
298088ed4aceSMatthew G Knepley + dm - The DM
298188ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
298288ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
298388ed4aceSMatthew G Knepley 
298488ed4aceSMatthew G Knepley   Level: intermediate
298588ed4aceSMatthew G Knepley 
298688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
298788ed4aceSMatthew G Knepley @*/
298888ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
298988ed4aceSMatthew G Knepley {
299088ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
299188ed4aceSMatthew G Knepley   PetscLayout     layout;
299288ed4aceSMatthew G Knepley   const PetscInt *ranges;
299388ed4aceSMatthew G Knepley   PetscInt       *local;
299488ed4aceSMatthew G Knepley   PetscSFNode    *remote;
299588ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
299688ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
299788ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
299888ed4aceSMatthew G Knepley 
299988ed4aceSMatthew G Knepley   PetscFunctionBegin;
300088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
300188ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
300288ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
300388ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
300488ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
300588ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
300688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
300788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
300888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
300988ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
301088ed4aceSMatthew G Knepley   for(p = pStart, nleaves = 0; p < pEnd; ++p) {
301188ed4aceSMatthew G Knepley     PetscInt dof, cdof;
301288ed4aceSMatthew G Knepley 
301388ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &dof);CHKERRQ(ierr);
301488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &cdof);CHKERRQ(ierr);
301588ed4aceSMatthew G Knepley     nleaves += dof < 0 ? -(dof+1)-cdof : dof-cdof;
301688ed4aceSMatthew G Knepley   }
301788ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
301888ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
301988ed4aceSMatthew G Knepley   for(p = pStart, l = 0; p < pEnd; ++p) {
302088ed4aceSMatthew G Knepley     PetscInt *cind;
302188ed4aceSMatthew G Knepley     PetscInt  dof, gdof, cdof, dim, off, goff, d, c;
302288ed4aceSMatthew G Knepley 
302388ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
302488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
302588ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
302688ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
302788ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
302888ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
302988ed4aceSMatthew G Knepley     dim  = dof-cdof;
303088ed4aceSMatthew G Knepley     for(d = 0, c = 0; d < dof; ++d) {
303188ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
303288ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
303388ed4aceSMatthew G Knepley     }
303488ed4aceSMatthew G Knepley     if (gdof < 0) {
303588ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
303688ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
303788ed4aceSMatthew G Knepley 
303888ed4aceSMatthew G Knepley         for(r = 0; r < size; ++r) {
303988ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
304088ed4aceSMatthew G Knepley         }
304188ed4aceSMatthew G Knepley         remote[l].rank  = r;
304288ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
304388ed4aceSMatthew G Knepley       }
304488ed4aceSMatthew G Knepley     } else {
304588ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
304688ed4aceSMatthew G Knepley         remote[l].rank  = rank;
304788ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
304888ed4aceSMatthew G Knepley       }
304988ed4aceSMatthew G Knepley     }
305088ed4aceSMatthew G Knepley   }
305188ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
305288ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
305388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
305488ed4aceSMatthew G Knepley }
3055