xref: /petsc/src/dm/interface/dm.c (revision a201590f66d78a0ee8e1bf4e6f9fcce39f32a809)
108da532bSDmitry Karpeev #include <petscsnes.h>
2b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
347c6ae99SBarry Smith 
4732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
567a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal;
667a56275SMatthew G Knepley 
747c6ae99SBarry Smith #undef __FUNCT__
8a4121054SBarry Smith #define __FUNCT__ "DMCreate"
9a4121054SBarry Smith /*@
10de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
11a4121054SBarry Smith 
12a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
13a4121054SBarry Smith    error when you try to use the vector.
14a4121054SBarry Smith 
15a4121054SBarry Smith   Collective on MPI_Comm
16a4121054SBarry Smith 
17a4121054SBarry Smith   Input Parameter:
18a4121054SBarry Smith . comm - The communicator for the DM object
19a4121054SBarry Smith 
20a4121054SBarry Smith   Output Parameter:
21a4121054SBarry Smith . dm - The DM object
22a4121054SBarry Smith 
23a4121054SBarry Smith   Level: beginner
24a4121054SBarry Smith 
25a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
26a4121054SBarry Smith @*/
277087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
28a4121054SBarry Smith {
29a4121054SBarry Smith   DM             v;
30a4121054SBarry Smith   PetscErrorCode ierr;
31a4121054SBarry Smith 
32a4121054SBarry Smith   PetscFunctionBegin;
331411c6eeSJed Brown   PetscValidPointer(dm,2);
341411c6eeSJed Brown   *dm = PETSC_NULL;
35a4121054SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
36b84caa0eSBarry Smith   ierr = VecInitializePackage(PETSC_NULL);CHKERRQ(ierr);
37b84caa0eSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
38a4121054SBarry Smith   ierr = DMInitializePackage(PETSC_NULL);CHKERRQ(ierr);
39a4121054SBarry Smith #endif
40a4121054SBarry Smith 
413194b578SJed Brown   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
42a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
431411c6eeSJed Brown 
44e7c4fc90SDmitry Karpeev 
45a89ea682SMatthew G Knepley   v->workSize     = 0;
46a89ea682SMatthew G Knepley   v->workArray    = PETSC_NULL;
471411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
481411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
491411c6eeSJed Brown   v->bs           = 1;
50171400e9SBarry Smith   v->coloringtype = IS_COLORING_GLOBAL;
51970e74d5SMatthew G Knepley   v->lf           = PETSC_NULL;
52970e74d5SMatthew G Knepley   v->lj           = PETSC_NULL;
5388ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5488ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
5588ed4aceSMatthew G Knepley   v->defaultSection       = PETSC_NULL;
5688ed4aceSMatthew G Knepley   v->defaultGlobalSection = PETSC_NULL;
571411c6eeSJed Brown 
581411c6eeSJed Brown   *dm = v;
59a4121054SBarry Smith   PetscFunctionReturn(0);
60a4121054SBarry Smith }
61a4121054SBarry Smith 
62a4121054SBarry Smith 
63a4121054SBarry Smith #undef __FUNCT__
649a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
659a42bb27SBarry Smith /*@C
66564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
679a42bb27SBarry Smith 
68aa219208SBarry Smith    Logically Collective on DMDA
699a42bb27SBarry Smith 
709a42bb27SBarry Smith    Input Parameter:
719a42bb27SBarry Smith +  da - initial distributed array
728154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
739a42bb27SBarry Smith 
749a42bb27SBarry Smith    Options Database:
75dd85299cSBarry Smith .   -dm_vec_type ctype
769a42bb27SBarry Smith 
779a42bb27SBarry Smith    Level: intermediate
789a42bb27SBarry Smith 
79aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
809a42bb27SBarry Smith @*/
817087cfbeSBarry Smith PetscErrorCode  DMSetVecType(DM da,const VecType ctype)
829a42bb27SBarry Smith {
839a42bb27SBarry Smith   PetscErrorCode ierr;
849a42bb27SBarry Smith 
859a42bb27SBarry Smith   PetscFunctionBegin;
869a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
879a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
889a42bb27SBarry Smith   ierr = PetscStrallocpy(ctype,&da->vectype);CHKERRQ(ierr);
899a42bb27SBarry Smith   PetscFunctionReturn(0);
909a42bb27SBarry Smith }
919a42bb27SBarry Smith 
929a42bb27SBarry Smith #undef __FUNCT__
93521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
94521d9a4cSLisandro Dalcin /*@C
95521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
96521d9a4cSLisandro Dalcin 
97521d9a4cSLisandro Dalcin    Logically Collective on DM
98521d9a4cSLisandro Dalcin 
99521d9a4cSLisandro Dalcin    Input Parameter:
100521d9a4cSLisandro Dalcin +  dm - the DM context
101521d9a4cSLisandro Dalcin .  ctype - the matrix type
102521d9a4cSLisandro Dalcin 
103521d9a4cSLisandro Dalcin    Options Database:
104521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
105521d9a4cSLisandro Dalcin 
106521d9a4cSLisandro Dalcin    Level: intermediate
107521d9a4cSLisandro Dalcin 
108521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType
109521d9a4cSLisandro Dalcin @*/
110521d9a4cSLisandro Dalcin PetscErrorCode  DMSetMatType(DM dm,const MatType ctype)
111521d9a4cSLisandro Dalcin {
112521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
113521d9a4cSLisandro Dalcin   PetscFunctionBegin;
114521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
115521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
116521d9a4cSLisandro Dalcin   ierr = PetscStrallocpy(ctype,&dm->mattype);CHKERRQ(ierr);
117521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
118521d9a4cSLisandro Dalcin }
119521d9a4cSLisandro Dalcin 
120521d9a4cSLisandro Dalcin #undef __FUNCT__
1219a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
1229a42bb27SBarry Smith /*@C
1239a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
124aa219208SBarry Smith    DMDA options in the database.
1259a42bb27SBarry Smith 
126aa219208SBarry Smith    Logically Collective on DMDA
1279a42bb27SBarry Smith 
1289a42bb27SBarry Smith    Input Parameter:
129aa219208SBarry Smith +  da - the DMDA context
1309a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
1319a42bb27SBarry Smith 
1329a42bb27SBarry Smith    Notes:
1339a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1349a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
1359a42bb27SBarry Smith 
1369a42bb27SBarry Smith    Level: advanced
1379a42bb27SBarry Smith 
138aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
1399a42bb27SBarry Smith 
1409a42bb27SBarry Smith .seealso: DMSetFromOptions()
1419a42bb27SBarry Smith @*/
1427087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
1439a42bb27SBarry Smith {
1449a42bb27SBarry Smith   PetscErrorCode ierr;
1459a42bb27SBarry Smith 
1469a42bb27SBarry Smith   PetscFunctionBegin;
1479a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1489a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
1499a42bb27SBarry Smith   PetscFunctionReturn(0);
1509a42bb27SBarry Smith }
1519a42bb27SBarry Smith 
1529a42bb27SBarry Smith #undef __FUNCT__
15347c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
15447c6ae99SBarry Smith /*@
155aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
15647c6ae99SBarry Smith 
15747c6ae99SBarry Smith     Collective on DM
15847c6ae99SBarry Smith 
15947c6ae99SBarry Smith     Input Parameter:
16047c6ae99SBarry Smith .   dm - the DM object to destroy
16147c6ae99SBarry Smith 
16247c6ae99SBarry Smith     Level: developer
16347c6ae99SBarry Smith 
164e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
16547c6ae99SBarry Smith 
16647c6ae99SBarry Smith @*/
167fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
16847c6ae99SBarry Smith {
169732e2eb9SMatthew G Knepley   PetscInt       i, cnt = 0;
170dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
17147c6ae99SBarry Smith   PetscErrorCode ierr;
17247c6ae99SBarry Smith 
17347c6ae99SBarry Smith   PetscFunctionBegin;
1746bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
1756bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
17687e657c6SBarry Smith 
17787e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
178732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1796bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
1806bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
181732e2eb9SMatthew G Knepley   }
182dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
18371cd77b2SBarry Smith   if ((*dm)->x) {
18471cd77b2SBarry Smith     PetscObject obj;
18571cd77b2SBarry Smith     ierr = PetscObjectQuery((PetscObject)(*dm)->x,"DM",&obj);CHKERRQ(ierr);
18671cd77b2SBarry Smith     if (obj == (PetscObject)*dm) cnt++;
18771cd77b2SBarry Smith   }
188732e2eb9SMatthew G Knepley 
1896bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
190732e2eb9SMatthew G Knepley   /*
191732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
192732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
193732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
194732e2eb9SMatthew G Knepley   */
1956bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
1966bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
197732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1986bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
1996bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
200732e2eb9SMatthew G Knepley   }
201dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
202dfe15315SJed Brown     nnext = nlink->next;
203dfe15315SJed Brown     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
204dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
205dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
206dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
207dfe15315SJed Brown   }
208dfe15315SJed Brown   (*dm)->namedglobal = PETSC_NULL;
2091a266240SBarry Smith 
210b17ce1afSJed Brown   /* Destroy the list of hooks */
211c833c3b5SJed Brown   {
212c833c3b5SJed Brown     DMCoarsenHookLink link,next;
213b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
214b17ce1afSJed Brown       next = link->next;
215b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
216b17ce1afSJed Brown     }
217b17ce1afSJed Brown     (*dm)->coarsenhook = PETSC_NULL;
218c833c3b5SJed Brown   }
219c833c3b5SJed Brown   {
220c833c3b5SJed Brown     DMRefineHookLink link,next;
221c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
222c833c3b5SJed Brown       next = link->next;
223c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
224c833c3b5SJed Brown     }
225c833c3b5SJed Brown     (*dm)->refinehook = PETSC_NULL;
226c833c3b5SJed Brown   }
227b17ce1afSJed Brown 
2281a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
2291a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
2301a266240SBarry Smith   }
23187e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
23271cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
2334dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
2346bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
2356bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
2366bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
237073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
238a89ea682SMatthew G Knepley   ierr = PetscFree((*dm)->workArray);CHKERRQ(ierr);
23988ed4aceSMatthew G Knepley 
24088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
24188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
24288ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
24388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
244732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
2456bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
246732e2eb9SMatthew G Knepley 
2476bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
2486bf464f9SBarry Smith   ierr = PetscFree((*dm)->data);CHKERRQ(ierr);
249732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
25047c6ae99SBarry Smith   PetscFunctionReturn(0);
25147c6ae99SBarry Smith }
25247c6ae99SBarry Smith 
25347c6ae99SBarry Smith #undef __FUNCT__
254d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
255d7bf68aeSBarry Smith /*@
256d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
257d7bf68aeSBarry Smith 
258d7bf68aeSBarry Smith     Collective on DM
259d7bf68aeSBarry Smith 
260d7bf68aeSBarry Smith     Input Parameter:
261d7bf68aeSBarry Smith .   dm - the DM object to setup
262d7bf68aeSBarry Smith 
263d7bf68aeSBarry Smith     Level: developer
264d7bf68aeSBarry Smith 
265e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
266d7bf68aeSBarry Smith 
267d7bf68aeSBarry Smith @*/
2687087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
269d7bf68aeSBarry Smith {
270d7bf68aeSBarry Smith   PetscErrorCode ierr;
271d7bf68aeSBarry Smith 
272d7bf68aeSBarry Smith   PetscFunctionBegin;
273171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2748387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
275d7bf68aeSBarry Smith   if (dm->ops->setup) {
276d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
277d7bf68aeSBarry Smith   }
2788387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
279d7bf68aeSBarry Smith   PetscFunctionReturn(0);
280d7bf68aeSBarry Smith }
281d7bf68aeSBarry Smith 
282d7bf68aeSBarry Smith #undef __FUNCT__
283d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
284d7bf68aeSBarry Smith /*@
285d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
286d7bf68aeSBarry Smith 
287d7bf68aeSBarry Smith     Collective on DM
288d7bf68aeSBarry Smith 
289d7bf68aeSBarry Smith     Input Parameter:
290d7bf68aeSBarry Smith .   dm - the DM object to set options for
291d7bf68aeSBarry Smith 
292732e2eb9SMatthew G Knepley     Options Database:
293dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
294dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
295171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
296171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
297732e2eb9SMatthew G Knepley 
298d7bf68aeSBarry Smith     Level: developer
299d7bf68aeSBarry Smith 
300e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
301d7bf68aeSBarry Smith 
302d7bf68aeSBarry Smith @*/
3037087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
304d7bf68aeSBarry Smith {
30567ad5babSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg;
306d7bf68aeSBarry Smith   PetscErrorCode ierr;
307f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
308d7bf68aeSBarry Smith 
309d7bf68aeSBarry Smith   PetscFunctionBegin;
310171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3113194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
31282fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
313c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
314c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
31567ad5babSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_latex", "Output mesh in LaTeX TikZ format", "DMView", flg4, &flg4, PETSC_NULL);CHKERRQ(ierr);
316073dac72SJed Brown     ierr = PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,PETSC_NULL);CHKERRQ(ierr);
317f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
318f9ba7244SBarry Smith     if (flg) {
319f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
320f9ba7244SBarry Smith     }
321521d9a4cSLisandro Dalcin     ierr = PetscOptionsList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype?dm->mattype:typeName,typeName,sizeof typeName,&flg);CHKERRQ(ierr);
322073dac72SJed Brown     if (flg) {
323521d9a4cSLisandro Dalcin       ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
324073dac72SJed Brown     }
3251b89239cSHong Zhang     ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,PETSC_NULL);CHKERRQ(ierr);
326f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
327f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
328f9ba7244SBarry Smith     }
329f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
330f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
33182fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
33282fcb398SMatthew G Knepley   if (flg1) {
33382fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
33482fcb398SMatthew G Knepley   }
335c4721b0eSMatthew G Knepley   if (flg2) {
336c4721b0eSMatthew G Knepley     PetscViewer viewer;
337c4721b0eSMatthew G Knepley 
338c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
339c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
340c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
341c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
342c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
343c4721b0eSMatthew G Knepley   }
344c4721b0eSMatthew G Knepley   if (flg3) {
345c4721b0eSMatthew G Knepley     PetscViewer viewer;
346c4721b0eSMatthew G Knepley 
347c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
348c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
349c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
350c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
351c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
352c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
353c4721b0eSMatthew G Knepley   }
35467ad5babSMatthew G Knepley   if (flg4) {
35567ad5babSMatthew G Knepley     PetscViewer viewer;
35667ad5babSMatthew G Knepley 
35767ad5babSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
35867ad5babSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
35967ad5babSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_LATEX);CHKERRQ(ierr);
36067ad5babSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.tex");CHKERRQ(ierr);
36167ad5babSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
36267ad5babSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
36367ad5babSMatthew G Knepley   }
364d7bf68aeSBarry Smith   PetscFunctionReturn(0);
365d7bf68aeSBarry Smith }
366d7bf68aeSBarry Smith 
367d7bf68aeSBarry Smith #undef __FUNCT__
36847c6ae99SBarry Smith #define __FUNCT__ "DMView"
369fc9bc008SSatish Balay /*@C
370aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
37147c6ae99SBarry Smith 
37247c6ae99SBarry Smith     Collective on DM
37347c6ae99SBarry Smith 
37447c6ae99SBarry Smith     Input Parameter:
37547c6ae99SBarry Smith +   dm - the DM object to view
37647c6ae99SBarry Smith -   v - the viewer
37747c6ae99SBarry Smith 
37847c6ae99SBarry Smith     Level: developer
37947c6ae99SBarry Smith 
380e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
38147c6ae99SBarry Smith 
38247c6ae99SBarry Smith @*/
3837087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
38447c6ae99SBarry Smith {
38547c6ae99SBarry Smith   PetscErrorCode ierr;
38647c6ae99SBarry Smith 
38747c6ae99SBarry Smith   PetscFunctionBegin;
388171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3893014e516SBarry Smith  if (!v) {
3903014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
3913014e516SBarry Smith   }
3920c010503SBarry Smith   if (dm->ops->view) {
3930c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
39447c6ae99SBarry Smith   }
39547c6ae99SBarry Smith   PetscFunctionReturn(0);
39647c6ae99SBarry Smith }
39747c6ae99SBarry Smith 
39847c6ae99SBarry Smith #undef __FUNCT__
39947c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
40047c6ae99SBarry Smith /*@
401aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
40247c6ae99SBarry Smith 
40347c6ae99SBarry Smith     Collective on DM
40447c6ae99SBarry Smith 
40547c6ae99SBarry Smith     Input Parameter:
40647c6ae99SBarry Smith .   dm - the DM object
40747c6ae99SBarry Smith 
40847c6ae99SBarry Smith     Output Parameter:
40947c6ae99SBarry Smith .   vec - the global vector
41047c6ae99SBarry Smith 
411073dac72SJed Brown     Level: beginner
41247c6ae99SBarry Smith 
413e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
41447c6ae99SBarry Smith 
41547c6ae99SBarry Smith @*/
4167087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
41747c6ae99SBarry Smith {
41847c6ae99SBarry Smith   PetscErrorCode ierr;
41947c6ae99SBarry Smith 
42047c6ae99SBarry Smith   PetscFunctionBegin;
421171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42288ed4aceSMatthew G Knepley   if (dm->defaultSection) {
42388ed4aceSMatthew G Knepley     PetscSection gSection;
42488ed4aceSMatthew G Knepley     PetscInt     localSize;
42588ed4aceSMatthew G Knepley 
42688ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
42788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
42888ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
42988ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
43088ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
43188ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
43288ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
43388ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
43488ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
43588ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
43688ed4aceSMatthew G Knepley   } else {
43747c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
43888ed4aceSMatthew G Knepley   }
43947c6ae99SBarry Smith   PetscFunctionReturn(0);
44047c6ae99SBarry Smith }
44147c6ae99SBarry Smith 
44247c6ae99SBarry Smith #undef __FUNCT__
44347c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
44447c6ae99SBarry Smith /*@
445aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
44647c6ae99SBarry Smith 
44747c6ae99SBarry Smith     Not Collective
44847c6ae99SBarry Smith 
44947c6ae99SBarry Smith     Input Parameter:
45047c6ae99SBarry Smith .   dm - the DM object
45147c6ae99SBarry Smith 
45247c6ae99SBarry Smith     Output Parameter:
45347c6ae99SBarry Smith .   vec - the local vector
45447c6ae99SBarry Smith 
455073dac72SJed Brown     Level: beginner
45647c6ae99SBarry Smith 
457e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
45847c6ae99SBarry Smith 
45947c6ae99SBarry Smith @*/
4607087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
46147c6ae99SBarry Smith {
46247c6ae99SBarry Smith   PetscErrorCode ierr;
46347c6ae99SBarry Smith 
46447c6ae99SBarry Smith   PetscFunctionBegin;
465171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
46688ed4aceSMatthew G Knepley   if (dm->defaultSection) {
46788ed4aceSMatthew G Knepley     PetscInt localSize;
46888ed4aceSMatthew G Knepley 
46988ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
47088ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
47188ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
47288ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
47388ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
47488ed4aceSMatthew G Knepley   } else {
47547c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
47688ed4aceSMatthew G Knepley   }
47747c6ae99SBarry Smith   PetscFunctionReturn(0);
47847c6ae99SBarry Smith }
47947c6ae99SBarry Smith 
48047c6ae99SBarry Smith #undef __FUNCT__
4811411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
4821411c6eeSJed Brown /*@
4831411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
4841411c6eeSJed Brown 
4851411c6eeSJed Brown    Collective on DM
4861411c6eeSJed Brown 
4871411c6eeSJed Brown    Input Parameter:
4881411c6eeSJed Brown .  dm - the DM that provides the mapping
4891411c6eeSJed Brown 
4901411c6eeSJed Brown    Output Parameter:
4911411c6eeSJed Brown .  ltog - the mapping
4921411c6eeSJed Brown 
4931411c6eeSJed Brown    Level: intermediate
4941411c6eeSJed Brown 
4951411c6eeSJed Brown    Notes:
4961411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
4971411c6eeSJed Brown    MatSetLocalToGlobalMapping().
4981411c6eeSJed Brown 
4991411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
5001411c6eeSJed Brown @*/
5017087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
5021411c6eeSJed Brown {
5031411c6eeSJed Brown   PetscErrorCode ierr;
5041411c6eeSJed Brown 
5051411c6eeSJed Brown   PetscFunctionBegin;
5061411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5071411c6eeSJed Brown   PetscValidPointer(ltog,2);
5081411c6eeSJed Brown   if (!dm->ltogmap) {
50937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
51037d0c07bSMatthew G Knepley 
51137d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
51237d0c07bSMatthew G Knepley     if (section) {
51337d0c07bSMatthew G Knepley       PetscInt      *ltog;
51437d0c07bSMatthew G Knepley       PetscInt       pStart, pEnd, size, p, l;
51537d0c07bSMatthew G Knepley 
51637d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
51737d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
51837d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
51937d0c07bSMatthew G Knepley       ierr = PetscMalloc(size * sizeof(PetscInt), &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
52037d0c07bSMatthew G Knepley       for(p = pStart, l = 0; p < pEnd; ++p) {
52137d0c07bSMatthew G Knepley         PetscInt dof, off, c;
52237d0c07bSMatthew G Knepley 
52337d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
52437d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
52537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
52637d0c07bSMatthew G Knepley         for(c = 0; c < dof; ++c, ++l) {
52737d0c07bSMatthew G Knepley           ltog[l] = off+c;
52837d0c07bSMatthew G Knepley         }
52937d0c07bSMatthew G Knepley       }
53037d0c07bSMatthew G Knepley       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
53137d0c07bSMatthew G Knepley       ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr);
53237d0c07bSMatthew G Knepley     } else {
5331411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
5341411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
5351411c6eeSJed Brown     }
53637d0c07bSMatthew G Knepley   }
5371411c6eeSJed Brown   *ltog = dm->ltogmap;
5381411c6eeSJed Brown   PetscFunctionReturn(0);
5391411c6eeSJed Brown }
5401411c6eeSJed Brown 
5411411c6eeSJed Brown #undef __FUNCT__
5421411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
5431411c6eeSJed Brown /*@
5441411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
5451411c6eeSJed Brown 
5461411c6eeSJed Brown    Collective on DM
5471411c6eeSJed Brown 
5481411c6eeSJed Brown    Input Parameter:
5491411c6eeSJed Brown .  da - the distributed array that provides the mapping
5501411c6eeSJed Brown 
5511411c6eeSJed Brown    Output Parameter:
5521411c6eeSJed Brown .  ltog - the block mapping
5531411c6eeSJed Brown 
5541411c6eeSJed Brown    Level: intermediate
5551411c6eeSJed Brown 
5561411c6eeSJed Brown    Notes:
5571411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
5581411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
5591411c6eeSJed Brown 
5601411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
5611411c6eeSJed Brown @*/
5627087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
5631411c6eeSJed Brown {
5641411c6eeSJed Brown   PetscErrorCode ierr;
5651411c6eeSJed Brown 
5661411c6eeSJed Brown   PetscFunctionBegin;
5671411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5681411c6eeSJed Brown   PetscValidPointer(ltog,2);
5691411c6eeSJed Brown   if (!dm->ltogmapb) {
5701411c6eeSJed Brown     PetscInt bs;
5711411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
5721411c6eeSJed Brown     if (bs > 1) {
5731411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
5741411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
5751411c6eeSJed Brown     } else {
5761411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
5771411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
5781411c6eeSJed Brown     }
5791411c6eeSJed Brown   }
5801411c6eeSJed Brown   *ltog = dm->ltogmapb;
5811411c6eeSJed Brown   PetscFunctionReturn(0);
5821411c6eeSJed Brown }
5831411c6eeSJed Brown 
5841411c6eeSJed Brown #undef __FUNCT__
5851411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
5861411c6eeSJed Brown /*@
5871411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
5881411c6eeSJed Brown 
5891411c6eeSJed Brown    Not Collective
5901411c6eeSJed Brown 
5911411c6eeSJed Brown    Input Parameter:
5921411c6eeSJed Brown .  dm - the DM with block structure
5931411c6eeSJed Brown 
5941411c6eeSJed Brown    Output Parameter:
5951411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
5961411c6eeSJed Brown 
5971411c6eeSJed Brown    Level: intermediate
5981411c6eeSJed Brown 
5991411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
6001411c6eeSJed Brown @*/
6017087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
6021411c6eeSJed Brown {
6031411c6eeSJed Brown   PetscFunctionBegin;
6041411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6051411c6eeSJed Brown   PetscValidPointer(bs,2);
6061411c6eeSJed Brown   if (dm->bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
6071411c6eeSJed Brown   *bs = dm->bs;
6081411c6eeSJed Brown   PetscFunctionReturn(0);
6091411c6eeSJed Brown }
6101411c6eeSJed Brown 
6111411c6eeSJed Brown #undef __FUNCT__
612e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
61347c6ae99SBarry Smith /*@
614e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
61547c6ae99SBarry Smith 
61647c6ae99SBarry Smith     Collective on DM
61747c6ae99SBarry Smith 
61847c6ae99SBarry Smith     Input Parameter:
61947c6ae99SBarry Smith +   dm1 - the DM object
62047c6ae99SBarry Smith -   dm2 - the second, finer DM object
62147c6ae99SBarry Smith 
62247c6ae99SBarry Smith     Output Parameter:
62347c6ae99SBarry Smith +  mat - the interpolation
62447c6ae99SBarry Smith -  vec - the scaling (optional)
62547c6ae99SBarry Smith 
62647c6ae99SBarry Smith     Level: developer
62747c6ae99SBarry Smith 
62885afcc9aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
62985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
630d52bd9f3SBarry Smith 
631d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
632d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
63385afcc9aSBarry Smith 
63485afcc9aSBarry Smith 
635e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
63647c6ae99SBarry Smith 
63747c6ae99SBarry Smith @*/
638e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
63947c6ae99SBarry Smith {
64047c6ae99SBarry Smith   PetscErrorCode ierr;
64147c6ae99SBarry Smith 
64247c6ae99SBarry Smith   PetscFunctionBegin;
643171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
644171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
64525296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
64647c6ae99SBarry Smith   PetscFunctionReturn(0);
64747c6ae99SBarry Smith }
64847c6ae99SBarry Smith 
64947c6ae99SBarry Smith #undef __FUNCT__
650e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
65147c6ae99SBarry Smith /*@
652e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
65347c6ae99SBarry Smith 
65447c6ae99SBarry Smith     Collective on DM
65547c6ae99SBarry Smith 
65647c6ae99SBarry Smith     Input Parameter:
65747c6ae99SBarry Smith +   dm1 - the DM object
65847c6ae99SBarry Smith -   dm2 - the second, finer DM object
65947c6ae99SBarry Smith 
66047c6ae99SBarry Smith     Output Parameter:
66147c6ae99SBarry Smith .   ctx - the injection
66247c6ae99SBarry Smith 
66347c6ae99SBarry Smith     Level: developer
66447c6ae99SBarry Smith 
66585afcc9aSBarry Smith    Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
66685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
66785afcc9aSBarry Smith 
668e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
66947c6ae99SBarry Smith 
67047c6ae99SBarry Smith @*/
671e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
67247c6ae99SBarry Smith {
67347c6ae99SBarry Smith   PetscErrorCode ierr;
67447c6ae99SBarry Smith 
67547c6ae99SBarry Smith   PetscFunctionBegin;
676171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
677171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
67847c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
67947c6ae99SBarry Smith   PetscFunctionReturn(0);
68047c6ae99SBarry Smith }
68147c6ae99SBarry Smith 
68247c6ae99SBarry Smith #undef __FUNCT__
683e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
684d1e2c406SBarry Smith /*@C
685e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
68647c6ae99SBarry Smith 
68747c6ae99SBarry Smith     Collective on DM
68847c6ae99SBarry Smith 
68947c6ae99SBarry Smith     Input Parameter:
69047c6ae99SBarry Smith +   dm - the DM object
69147c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
69247c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
69347c6ae99SBarry Smith 
69447c6ae99SBarry Smith     Output Parameter:
69547c6ae99SBarry Smith .   coloring - the coloring
69647c6ae99SBarry Smith 
69747c6ae99SBarry Smith     Level: developer
69847c6ae99SBarry Smith 
699e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
70047c6ae99SBarry Smith 
701aab9d709SJed Brown @*/
702e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
70347c6ae99SBarry Smith {
70447c6ae99SBarry Smith   PetscErrorCode ierr;
70547c6ae99SBarry Smith 
70647c6ae99SBarry Smith   PetscFunctionBegin;
707171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
70847c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
70947c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
71047c6ae99SBarry Smith   PetscFunctionReturn(0);
71147c6ae99SBarry Smith }
71247c6ae99SBarry Smith 
71347c6ae99SBarry Smith #undef __FUNCT__
714950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
71547c6ae99SBarry Smith /*@C
716950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
71747c6ae99SBarry Smith 
71847c6ae99SBarry Smith     Collective on DM
71947c6ae99SBarry Smith 
72047c6ae99SBarry Smith     Input Parameter:
72147c6ae99SBarry Smith +   dm - the DM object
72247c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
72394013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
72447c6ae99SBarry Smith 
72547c6ae99SBarry Smith     Output Parameter:
72647c6ae99SBarry Smith .   mat - the empty Jacobian
72747c6ae99SBarry Smith 
728073dac72SJed Brown     Level: beginner
72947c6ae99SBarry Smith 
73094013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
73194013140SBarry Smith        do not need to do it yourself.
73294013140SBarry Smith 
73394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
734aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
73594013140SBarry Smith 
73694013140SBarry Smith        For structured grid problems, when you call MatView() on this matrix it is displayed using the global natural ordering, NOT in the ordering used
73794013140SBarry Smith        internally by PETSc.
73894013140SBarry Smith 
73994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
740aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
74194013140SBarry Smith 
742e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
74347c6ae99SBarry Smith 
744aab9d709SJed Brown @*/
745950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
74647c6ae99SBarry Smith {
74747c6ae99SBarry Smith   PetscErrorCode ierr;
74847c6ae99SBarry Smith 
74947c6ae99SBarry Smith   PetscFunctionBegin;
750171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
751235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
752235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
753235683edSBarry Smith #endif
754c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
755c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
756073dac72SJed Brown   if (dm->mattype) {
75725296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
758073dac72SJed Brown   } else {
75925296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
760c7b7c8a4SJed Brown   }
76147c6ae99SBarry Smith   PetscFunctionReturn(0);
76247c6ae99SBarry Smith }
76347c6ae99SBarry Smith 
76447c6ae99SBarry Smith #undef __FUNCT__
765732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
766732e2eb9SMatthew G Knepley /*@
767950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
768732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
769732e2eb9SMatthew G Knepley 
770732e2eb9SMatthew G Knepley   Logically Collective on DMDA
771732e2eb9SMatthew G Knepley 
772732e2eb9SMatthew G Knepley   Input Parameter:
773732e2eb9SMatthew G Knepley + dm - the DM
774732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
775732e2eb9SMatthew G Knepley 
776732e2eb9SMatthew G Knepley   Level: developer
777950540a4SJed Brown .seealso DMCreateMatrix()
778732e2eb9SMatthew G Knepley @*/
779732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
780732e2eb9SMatthew G Knepley {
781732e2eb9SMatthew G Knepley   PetscFunctionBegin;
782732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
783732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
784732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
785732e2eb9SMatthew G Knepley }
786732e2eb9SMatthew G Knepley 
787732e2eb9SMatthew G Knepley #undef __FUNCT__
788a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
789a89ea682SMatthew G Knepley /*@C
790a89ea682SMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size
791a89ea682SMatthew G Knepley 
792a89ea682SMatthew G Knepley   Not Collective
793a89ea682SMatthew G Knepley 
794a89ea682SMatthew G Knepley   Input Parameters:
795a89ea682SMatthew G Knepley + dm - the DM object
796a89ea682SMatthew G Knepley - size - The minium size
797a89ea682SMatthew G Knepley 
798a89ea682SMatthew G Knepley   Output Parameter:
799a89ea682SMatthew G Knepley . array - the work array
800a89ea682SMatthew G Knepley 
801a89ea682SMatthew G Knepley   Level: developer
802a89ea682SMatthew G Knepley 
803a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
804a89ea682SMatthew G Knepley @*/
805a89ea682SMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt size,PetscScalar **array)
806a89ea682SMatthew G Knepley {
807a89ea682SMatthew G Knepley   PetscErrorCode ierr;
808a89ea682SMatthew G Knepley 
809a89ea682SMatthew G Knepley   PetscFunctionBegin;
810a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
811a89ea682SMatthew G Knepley   PetscValidPointer(array,3);
812a89ea682SMatthew G Knepley   if (size > dm->workSize) {
813a89ea682SMatthew G Knepley     dm->workSize = size;
814a89ea682SMatthew G Knepley     ierr = PetscFree(dm->workArray);CHKERRQ(ierr);
815a89ea682SMatthew G Knepley     ierr = PetscMalloc(dm->workSize * sizeof(PetscScalar), &dm->workArray);CHKERRQ(ierr);
816a89ea682SMatthew G Knepley   }
817a89ea682SMatthew G Knepley   *array = dm->workArray;
818a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
819a89ea682SMatthew G Knepley }
820a89ea682SMatthew G Knepley 
821e7c4fc90SDmitry Karpeev 
822e7c4fc90SDmitry Karpeev #undef __FUNCT__
8234d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
8244f3b5142SJed Brown /*@C
8254d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
8264d343eeaSMatthew G Knepley 
8274d343eeaSMatthew G Knepley   Not collective
8284d343eeaSMatthew G Knepley 
8294d343eeaSMatthew G Knepley   Input Parameter:
8304d343eeaSMatthew G Knepley . dm - the DM object
8314d343eeaSMatthew G Knepley 
8324d343eeaSMatthew G Knepley   Output Parameters:
83321c9b008SJed Brown + numFields  - The number of fields (or PETSC_NULL if not requested)
83437d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested)
83521c9b008SJed Brown - fields     - The global indices for each field (or PETSC_NULL if not requested)
8364d343eeaSMatthew G Knepley 
8374d343eeaSMatthew G Knepley   Level: intermediate
8384d343eeaSMatthew G Knepley 
83921c9b008SJed Brown   Notes:
84021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
84121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
84221c9b008SJed Brown   PetscFree().
84321c9b008SJed Brown 
8444d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
8454d343eeaSMatthew G Knepley @*/
84637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
8474d343eeaSMatthew G Knepley {
84837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
8494d343eeaSMatthew G Knepley   PetscErrorCode ierr;
8504d343eeaSMatthew G Knepley 
8514d343eeaSMatthew G Knepley   PetscFunctionBegin;
8524d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85369ca1f37SDmitry Karpeev   if (numFields) {
85469ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
85569ca1f37SDmitry Karpeev     *numFields = 0;
85669ca1f37SDmitry Karpeev   }
85737d0c07bSMatthew G Knepley   if (fieldNames) {
85837d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
85937d0c07bSMatthew G Knepley     *fieldNames = PETSC_NULL;
86069ca1f37SDmitry Karpeev   }
86169ca1f37SDmitry Karpeev   if (fields) {
86269ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
86369ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
86469ca1f37SDmitry Karpeev   }
86537d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
86637d0c07bSMatthew G Knepley   if (section) {
86737d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
86837d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
86937d0c07bSMatthew G Knepley 
87037d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
87137d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
87237d0c07bSMatthew G Knepley     ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr);
87337d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
87437d0c07bSMatthew G Knepley     for(f = 0; f < nF; ++f) {
87537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
87637d0c07bSMatthew G Knepley     }
87737d0c07bSMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
87837d0c07bSMatthew G Knepley       PetscInt gdof;
87937d0c07bSMatthew G Knepley 
88037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
88137d0c07bSMatthew G Knepley       if (gdof > 0) {
88237d0c07bSMatthew G Knepley         for(f = 0; f < nF; ++f) {
88337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
88437d0c07bSMatthew G Knepley 
88537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
88637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
88737d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
88837d0c07bSMatthew G Knepley         }
88937d0c07bSMatthew G Knepley       }
89037d0c07bSMatthew G Knepley     }
89137d0c07bSMatthew G Knepley     for(f = 0; f < nF; ++f) {
89237d0c07bSMatthew G Knepley       ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr);
89337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
89437d0c07bSMatthew G Knepley     }
89537d0c07bSMatthew G Knepley     for(p = pStart; p < pEnd; ++p) {
89637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
89737d0c07bSMatthew G Knepley 
89837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
89937d0c07bSMatthew G Knepley       if (gdof > 0) {
90037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
90137d0c07bSMatthew G Knepley         for(f = 0; f < nF; ++f) {
90237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
90337d0c07bSMatthew G Knepley 
90437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
90537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
90637d0c07bSMatthew G Knepley           for(fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
90737d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
90837d0c07bSMatthew G Knepley           }
90937d0c07bSMatthew G Knepley         }
91037d0c07bSMatthew G Knepley       }
91137d0c07bSMatthew G Knepley     }
91237d0c07bSMatthew G Knepley     if (numFields) {*numFields = nF;}
91337d0c07bSMatthew G Knepley     if (fieldNames) {
91437d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr);
91537d0c07bSMatthew G Knepley       for(f = 0; f < nF; ++f) {
91637d0c07bSMatthew G Knepley         const char *fieldName;
91737d0c07bSMatthew G Knepley 
91837d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
91937d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr);
92037d0c07bSMatthew G Knepley       }
92137d0c07bSMatthew G Knepley     }
92237d0c07bSMatthew G Knepley     if (fields) {
92337d0c07bSMatthew G Knepley       ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr);
92437d0c07bSMatthew G Knepley       for(f = 0; f < nF; ++f) {
92537d0c07bSMatthew G Knepley         ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
92637d0c07bSMatthew G Knepley       }
92737d0c07bSMatthew G Knepley     }
92837d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
92937d0c07bSMatthew G Knepley   } else {
93037d0c07bSMatthew G Knepley     if(dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);}
93169ca1f37SDmitry Karpeev   }
9324d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
9334d343eeaSMatthew G Knepley }
9344d343eeaSMatthew G Knepley 
9355fe1f584SPeter Brune 
936a89ea682SMatthew G Knepley #undef __FUNCT__
93716621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM"
938e7c4fc90SDmitry Karpeev /*@C
93916621825SDmitry Karpeev   DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields.
94016621825SDmitry Karpeev 
94116621825SDmitry Karpeev   Not Collective
94216621825SDmitry Karpeev 
94316621825SDmitry Karpeev   Input Parameters:
94416621825SDmitry Karpeev + dm   - the DM object
94516621825SDmitry Karpeev - name - the name of the field decomposition
94616621825SDmitry Karpeev 
94716621825SDmitry Karpeev   Output Parameter:
94816621825SDmitry Karpeev . ddm  - the field decomposition DM (PETSC_NULL, if no such decomposition is known)
94916621825SDmitry Karpeev 
95016621825SDmitry Karpeev   Level: advanced
95116621825SDmitry Karpeev 
95216621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
95316621825SDmitry Karpeev @*/
95416621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm)
95516621825SDmitry Karpeev {
95616621825SDmitry Karpeev   PetscErrorCode ierr;
95716621825SDmitry Karpeev 
95816621825SDmitry Karpeev   PetscFunctionBegin;
95916621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
96016621825SDmitry Karpeev   PetscValidCharPointer(name,2);
96116621825SDmitry Karpeev   PetscValidPointer(ddm,3);
96216621825SDmitry Karpeev   *ddm = PETSC_NULL;
963731c8d9eSDmitry Karpeev   if(dm->ops->createfielddecompositiondm) {
96416621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
96516621825SDmitry Karpeev   }
96616621825SDmitry Karpeev   PetscFunctionReturn(0);
96716621825SDmitry Karpeev }
96816621825SDmitry Karpeev 
96916621825SDmitry Karpeev 
97016621825SDmitry Karpeev #undef __FUNCT__
97116621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
97216621825SDmitry Karpeev /*@C
97316621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
97416621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
97516621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
976e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
977e7c4fc90SDmitry Karpeev 
978e7c4fc90SDmitry Karpeev   Not collective
979e7c4fc90SDmitry Karpeev 
980e7c4fc90SDmitry Karpeev   Input Parameter:
981e7c4fc90SDmitry Karpeev . dm - the DM object
982e7c4fc90SDmitry Karpeev 
983e7c4fc90SDmitry Karpeev   Output Parameters:
98416621825SDmitry Karpeev + len       - The number of subproblems in the field decomposition (or PETSC_NULL if not requested)
98516621825SDmitry Karpeev . namelist  - The name for each field (or PETSC_NULL if not requested)
98616621825SDmitry Karpeev . islist    - The global indices for each field (or PETSC_NULL if not requested)
98716621825SDmitry Karpeev - dmlist    - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
988e7c4fc90SDmitry Karpeev 
989e7c4fc90SDmitry Karpeev   Level: intermediate
990e7c4fc90SDmitry Karpeev 
991e7c4fc90SDmitry Karpeev   Notes:
992e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
993e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
994e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
995e7c4fc90SDmitry Karpeev 
996e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
997e7c4fc90SDmitry Karpeev @*/
99816621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
999e7c4fc90SDmitry Karpeev {
1000e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1001e7c4fc90SDmitry Karpeev 
1002e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1003e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1004731c8d9eSDmitry Karpeev   if (len)      {PetscValidPointer(len,2);      *len      = 0;}
1005731c8d9eSDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3); *namelist = 0;}
1006731c8d9eSDmitry Karpeev   if (islist)   {PetscValidPointer(islist,4);   *islist   = 0;}
1007731c8d9eSDmitry Karpeev   if (dmlist)   {PetscValidPointer(dmlist,5);   *dmlist   = 0;}
100816621825SDmitry Karpeev   if(!dm->ops->createfielddecomposition) {
100969ca1f37SDmitry Karpeev     ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1010e7c4fc90SDmitry Karpeev     /* By default there are no DMs associated with subproblems. */
1011e7c4fc90SDmitry Karpeev     if(dmlist) *dmlist = PETSC_NULL;
1012e7c4fc90SDmitry Karpeev   }
1013e7c4fc90SDmitry Karpeev   else {
101416621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
101516621825SDmitry Karpeev   }
101616621825SDmitry Karpeev   PetscFunctionReturn(0);
101716621825SDmitry Karpeev }
101816621825SDmitry Karpeev 
101916621825SDmitry Karpeev #undef __FUNCT__
102016621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM"
102116621825SDmitry Karpeev /*@C
102216621825SDmitry Karpeev   DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains.
102316621825SDmitry Karpeev 
102416621825SDmitry Karpeev   Not Collective
102516621825SDmitry Karpeev 
102616621825SDmitry Karpeev   Input Parameters:
102716621825SDmitry Karpeev + dm   - the DM object
102816621825SDmitry Karpeev - name - the name of the subdomain decomposition
102916621825SDmitry Karpeev 
103016621825SDmitry Karpeev   Output Parameter:
103116621825SDmitry Karpeev . ddm  - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known)
103216621825SDmitry Karpeev 
103316621825SDmitry Karpeev   Level: advanced
103416621825SDmitry Karpeev 
103516621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM()
103616621825SDmitry Karpeev @*/
103716621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm)
103816621825SDmitry Karpeev {
103916621825SDmitry Karpeev   PetscErrorCode ierr;
104016621825SDmitry Karpeev 
104116621825SDmitry Karpeev   PetscFunctionBegin;
104216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
104316621825SDmitry Karpeev   PetscValidCharPointer(name,2);
104416621825SDmitry Karpeev   PetscValidPointer(ddm,3);
104516621825SDmitry Karpeev   *ddm = PETSC_NULL;
1046731c8d9eSDmitry Karpeev   if(dm->ops->createdomaindecompositiondm) {
104716621825SDmitry Karpeev     ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
104816621825SDmitry Karpeev   }
104916621825SDmitry Karpeev   PetscFunctionReturn(0);
105016621825SDmitry Karpeev }
105116621825SDmitry Karpeev 
105216621825SDmitry Karpeev 
105316621825SDmitry Karpeev #undef __FUNCT__
105416621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
105516621825SDmitry Karpeev /*@C
10568d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
10578d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
10588d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
10598d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
10608d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
106116621825SDmitry Karpeev 
106216621825SDmitry Karpeev   Not collective
106316621825SDmitry Karpeev 
106416621825SDmitry Karpeev   Input Parameter:
106516621825SDmitry Karpeev . dm - the DM object
106616621825SDmitry Karpeev 
106716621825SDmitry Karpeev   Output Parameters:
106816621825SDmitry Karpeev + len         - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested)
106916621825SDmitry Karpeev . namelist    - The name for each subdomain (or PETSC_NULL if not requested)
10708d4ac253SDmitry Karpeev . innerislist - The global indices for each inner subdomain (or PETSC_NULL, if not requested)
10718d4ac253SDmitry Karpeev . outerislist - The global indices for each outer subdomain (or PETSC_NULL, if not requested)
107216621825SDmitry Karpeev - dmlist      - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
107316621825SDmitry Karpeev 
107416621825SDmitry Karpeev   Level: intermediate
107516621825SDmitry Karpeev 
107616621825SDmitry Karpeev   Notes:
107716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
107816621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
107916621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
108016621825SDmitry Karpeev 
10818d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
108216621825SDmitry Karpeev @*/
10838d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
108416621825SDmitry Karpeev {
108516621825SDmitry Karpeev   PetscErrorCode ierr;
108616621825SDmitry Karpeev 
108716621825SDmitry Karpeev   PetscFunctionBegin;
108816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1089731c8d9eSDmitry Karpeev   if (len)           {PetscValidPointer(len,2);            *len         = PETSC_NULL;}
109016621825SDmitry Karpeev   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = PETSC_NULL;}
10918d4ac253SDmitry Karpeev   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = PETSC_NULL;}
10928d4ac253SDmitry Karpeev   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = PETSC_NULL;}
10938d4ac253SDmitry Karpeev   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = PETSC_NULL;}
109416621825SDmitry Karpeev   if(dm->ops->createdomaindecomposition) {
10958d4ac253SDmitry Karpeev     ierr = (*dm->ops->createdomaindecomposition)(dm,len,namelist,innerislist,outerislist,dmlist); CHKERRQ(ierr);
1096e7c4fc90SDmitry Karpeev   }
1097e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1098e7c4fc90SDmitry Karpeev }
1099e7c4fc90SDmitry Karpeev 
1100731c8d9eSDmitry Karpeev #undef __FUNCT__
110147c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
110247c6ae99SBarry Smith /*@
110347c6ae99SBarry Smith   DMRefine - Refines a DM object
110447c6ae99SBarry Smith 
110547c6ae99SBarry Smith   Collective on DM
110647c6ae99SBarry Smith 
110747c6ae99SBarry Smith   Input Parameter:
110847c6ae99SBarry Smith + dm   - the DM object
110991d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
111047c6ae99SBarry Smith 
111147c6ae99SBarry Smith   Output Parameter:
1112ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
1113ae0a1c52SMatthew G Knepley 
1114ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
111547c6ae99SBarry Smith 
111647c6ae99SBarry Smith   Level: developer
111747c6ae99SBarry Smith 
1118e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
111947c6ae99SBarry Smith @*/
11207087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
112147c6ae99SBarry Smith {
112247c6ae99SBarry Smith   PetscErrorCode ierr;
1123c833c3b5SJed Brown   DMRefineHookLink link;
112447c6ae99SBarry Smith 
112547c6ae99SBarry Smith   PetscFunctionBegin;
1126732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
112747c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
11284057135bSMatthew G Knepley   if (*dmf) {
112943842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
1130644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
1131644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
1132644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
1133644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
1134644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
1135644e2e5bSBarry Smith     }
11368cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1137644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
11380598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1139656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
1140e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1141c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
1142c833c3b5SJed Brown       if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);}
1143c833c3b5SJed Brown     }
1144c833c3b5SJed Brown   }
1145c833c3b5SJed Brown   PetscFunctionReturn(0);
1146c833c3b5SJed Brown }
1147c833c3b5SJed Brown 
1148c833c3b5SJed Brown #undef __FUNCT__
1149c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1150c833c3b5SJed Brown /*@
1151c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1152c833c3b5SJed Brown 
1153c833c3b5SJed Brown    Logically Collective
1154c833c3b5SJed Brown 
1155c833c3b5SJed Brown    Input Arguments:
1156c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1157c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1158c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
1159c833c3b5SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1160c833c3b5SJed Brown 
1161c833c3b5SJed Brown    Calling sequence of refinehook:
1162c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1163c833c3b5SJed Brown 
1164c833c3b5SJed Brown +  coarse - coarse level DM
1165c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1166c833c3b5SJed Brown -  ctx - optional user-defined function context
1167c833c3b5SJed Brown 
1168c833c3b5SJed Brown    Calling sequence for interphook:
1169c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1170c833c3b5SJed Brown 
1171c833c3b5SJed Brown +  coarse - coarse level DM
1172c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1173c833c3b5SJed Brown .  fine - fine level DM to update
1174c833c3b5SJed Brown -  ctx - optional user-defined function context
1175c833c3b5SJed Brown 
1176c833c3b5SJed Brown    Level: advanced
1177c833c3b5SJed Brown 
1178c833c3b5SJed Brown    Notes:
1179c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1180c833c3b5SJed Brown 
1181c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1182c833c3b5SJed Brown 
1183c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1184c833c3b5SJed Brown @*/
1185c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1186c833c3b5SJed Brown {
1187c833c3b5SJed Brown   PetscErrorCode ierr;
1188c833c3b5SJed Brown   DMRefineHookLink link,*p;
1189c833c3b5SJed Brown 
1190c833c3b5SJed Brown   PetscFunctionBegin;
1191c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1192c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1193c833c3b5SJed Brown   ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1194c833c3b5SJed Brown   link->refinehook = refinehook;
1195c833c3b5SJed Brown   link->interphook = interphook;
1196c833c3b5SJed Brown   link->ctx = ctx;
1197c833c3b5SJed Brown   link->next = PETSC_NULL;
1198c833c3b5SJed Brown   *p = link;
1199c833c3b5SJed Brown   PetscFunctionReturn(0);
1200c833c3b5SJed Brown }
1201c833c3b5SJed Brown 
1202c833c3b5SJed Brown #undef __FUNCT__
1203c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1204c833c3b5SJed Brown /*@
1205c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1206c833c3b5SJed Brown 
1207c833c3b5SJed Brown    Collective if any hooks are
1208c833c3b5SJed Brown 
1209c833c3b5SJed Brown    Input Arguments:
1210c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1211c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1212c833c3b5SJed Brown -  fine - finer DM to update
1213c833c3b5SJed Brown 
1214c833c3b5SJed Brown    Level: developer
1215c833c3b5SJed Brown 
1216c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1217c833c3b5SJed Brown @*/
1218c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1219c833c3b5SJed Brown {
1220c833c3b5SJed Brown   PetscErrorCode ierr;
1221c833c3b5SJed Brown   DMRefineHookLink link;
1222c833c3b5SJed Brown 
1223c833c3b5SJed Brown   PetscFunctionBegin;
1224c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
1225c833c3b5SJed Brown     if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);}
12264057135bSMatthew G Knepley   }
122747c6ae99SBarry Smith   PetscFunctionReturn(0);
122847c6ae99SBarry Smith }
122947c6ae99SBarry Smith 
123047c6ae99SBarry Smith #undef __FUNCT__
1231eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1232eb3f98d2SBarry Smith /*@
1233eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1234eb3f98d2SBarry Smith 
1235eb3f98d2SBarry Smith     Not Collective
1236eb3f98d2SBarry Smith 
1237eb3f98d2SBarry Smith     Input Parameter:
1238eb3f98d2SBarry Smith .   dm - the DM object
1239eb3f98d2SBarry Smith 
1240eb3f98d2SBarry Smith     Output Parameter:
1241eb3f98d2SBarry Smith .   level - number of refinements
1242eb3f98d2SBarry Smith 
1243eb3f98d2SBarry Smith     Level: developer
1244eb3f98d2SBarry Smith 
12456a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1246eb3f98d2SBarry Smith 
1247eb3f98d2SBarry Smith @*/
1248eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1249eb3f98d2SBarry Smith {
1250eb3f98d2SBarry Smith   PetscFunctionBegin;
1251eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1252eb3f98d2SBarry Smith   *level = dm->levelup;
1253eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1254eb3f98d2SBarry Smith }
1255eb3f98d2SBarry Smith 
1256eb3f98d2SBarry Smith #undef __FUNCT__
125747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
125847c6ae99SBarry Smith /*@
125947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
126047c6ae99SBarry Smith 
126147c6ae99SBarry Smith     Neighbor-wise Collective on DM
126247c6ae99SBarry Smith 
126347c6ae99SBarry Smith     Input Parameters:
126447c6ae99SBarry Smith +   dm - the DM object
126547c6ae99SBarry Smith .   g - the global vector
126647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
126747c6ae99SBarry Smith -   l - the local vector
126847c6ae99SBarry Smith 
126947c6ae99SBarry Smith 
127047c6ae99SBarry Smith     Level: beginner
127147c6ae99SBarry Smith 
1272e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
127347c6ae99SBarry Smith 
127447c6ae99SBarry Smith @*/
12757087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
127647c6ae99SBarry Smith {
12777128ae9fSMatthew G Knepley   PetscSF        sf;
127847c6ae99SBarry Smith   PetscErrorCode ierr;
127947c6ae99SBarry Smith 
128047c6ae99SBarry Smith   PetscFunctionBegin;
1281171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12827128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
12837128ae9fSMatthew G Knepley   if (sf) {
12847128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
12857128ae9fSMatthew G Knepley 
12867128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
12877128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
12887128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
12897128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
12907128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
12917128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
12927128ae9fSMatthew G Knepley   } else {
1293843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
12947128ae9fSMatthew G Knepley   }
129547c6ae99SBarry Smith   PetscFunctionReturn(0);
129647c6ae99SBarry Smith }
129747c6ae99SBarry Smith 
129847c6ae99SBarry Smith #undef __FUNCT__
129947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
130047c6ae99SBarry Smith /*@
130147c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
130247c6ae99SBarry Smith 
130347c6ae99SBarry Smith     Neighbor-wise Collective on DM
130447c6ae99SBarry Smith 
130547c6ae99SBarry Smith     Input Parameters:
130647c6ae99SBarry Smith +   dm - the DM object
130747c6ae99SBarry Smith .   g - the global vector
130847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
130947c6ae99SBarry Smith -   l - the local vector
131047c6ae99SBarry Smith 
131147c6ae99SBarry Smith 
131247c6ae99SBarry Smith     Level: beginner
131347c6ae99SBarry Smith 
1314e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
131547c6ae99SBarry Smith 
131647c6ae99SBarry Smith @*/
13177087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
131847c6ae99SBarry Smith {
13197128ae9fSMatthew G Knepley   PetscSF        sf;
132047c6ae99SBarry Smith   PetscErrorCode ierr;
132161a3c1faSSatish Balay   PetscScalar    *lArray, *gArray;
132247c6ae99SBarry Smith 
132347c6ae99SBarry Smith   PetscFunctionBegin;
1324171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13257128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
13267128ae9fSMatthew G Knepley   if (sf) {
13277128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
13287128ae9fSMatthew G Knepley 
13297128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
13307128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
13317128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
13327128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
13337128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
13347128ae9fSMatthew G Knepley   } else {
1335843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
13367128ae9fSMatthew G Knepley   }
133747c6ae99SBarry Smith   PetscFunctionReturn(0);
133847c6ae99SBarry Smith }
133947c6ae99SBarry Smith 
134047c6ae99SBarry Smith #undef __FUNCT__
13419a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
134247c6ae99SBarry Smith /*@
13439a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
13449a42bb27SBarry Smith 
13459a42bb27SBarry Smith     Neighbor-wise Collective on DM
13469a42bb27SBarry Smith 
13479a42bb27SBarry Smith     Input Parameters:
13489a42bb27SBarry Smith +   dm - the DM object
1349f6813fd5SJed Brown .   l - the local vector
13509a42bb27SBarry 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
13519a42bb27SBarry Smith            base point.
1352f6813fd5SJed Brown - - the global vector
13539a42bb27SBarry Smith 
13549a42bb27SBarry 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
13559a42bb27SBarry 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
13569a42bb27SBarry Smith            global array to the final global array with VecAXPY().
13579a42bb27SBarry Smith 
13589a42bb27SBarry Smith     Level: beginner
13599a42bb27SBarry Smith 
1360e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
13619a42bb27SBarry Smith 
13629a42bb27SBarry Smith @*/
13637087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
13649a42bb27SBarry Smith {
13657128ae9fSMatthew G Knepley   PetscSF        sf;
13669a42bb27SBarry Smith   PetscErrorCode ierr;
13679a42bb27SBarry Smith 
13689a42bb27SBarry Smith   PetscFunctionBegin;
1369171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13707128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
13717128ae9fSMatthew G Knepley   if (sf) {
13727128ae9fSMatthew G Knepley     MPI_Op       op;
13737128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
13747128ae9fSMatthew G Knepley 
13757128ae9fSMatthew G Knepley     switch(mode) {
13767128ae9fSMatthew G Knepley     case INSERT_VALUES:
13777128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
13787128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
13797128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
13807128ae9fSMatthew G Knepley #else
13817128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
13827128ae9fSMatthew G Knepley #endif
13837128ae9fSMatthew G Knepley     case ADD_VALUES:
13847128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
13857128ae9fSMatthew G Knepley       op = MPI_SUM; break;
13867128ae9fSMatthew G Knepley   default:
13877128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
13887128ae9fSMatthew G Knepley     }
13897128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
13907128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
13917128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
13927128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
13937128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
13947128ae9fSMatthew G Knepley   } else {
1395843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
13967128ae9fSMatthew G Knepley   }
13979a42bb27SBarry Smith   PetscFunctionReturn(0);
13989a42bb27SBarry Smith }
13999a42bb27SBarry Smith 
14009a42bb27SBarry Smith #undef __FUNCT__
14019a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
14029a42bb27SBarry Smith /*@
14039a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
140447c6ae99SBarry Smith 
140547c6ae99SBarry Smith     Neighbor-wise Collective on DM
140647c6ae99SBarry Smith 
140747c6ae99SBarry Smith     Input Parameters:
140847c6ae99SBarry Smith +   dm - the DM object
1409f6813fd5SJed Brown .   l - the local vector
141047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1411f6813fd5SJed Brown -   g - the global vector
141247c6ae99SBarry Smith 
141347c6ae99SBarry Smith 
141447c6ae99SBarry Smith     Level: beginner
141547c6ae99SBarry Smith 
1416e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
141747c6ae99SBarry Smith 
141847c6ae99SBarry Smith @*/
14197087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
142047c6ae99SBarry Smith {
14217128ae9fSMatthew G Knepley   PetscSF        sf;
142247c6ae99SBarry Smith   PetscErrorCode ierr;
142347c6ae99SBarry Smith 
142447c6ae99SBarry Smith   PetscFunctionBegin;
1425171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14267128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
14277128ae9fSMatthew G Knepley   if (sf) {
14287128ae9fSMatthew G Knepley     MPI_Op       op;
14297128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
14307128ae9fSMatthew G Knepley 
14317128ae9fSMatthew G Knepley     switch(mode) {
14327128ae9fSMatthew G Knepley     case INSERT_VALUES:
14337128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
14347128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
14357128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
14367128ae9fSMatthew G Knepley #else
14377128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
14387128ae9fSMatthew G Knepley #endif
14397128ae9fSMatthew G Knepley     case ADD_VALUES:
14407128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
14417128ae9fSMatthew G Knepley       op = MPI_SUM; break;
14427128ae9fSMatthew G Knepley     default:
14437128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
14447128ae9fSMatthew G Knepley     }
14457128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
14467128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
14477128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
14487128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
14497128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
14507128ae9fSMatthew G Knepley   } else {
1451843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
14527128ae9fSMatthew G Knepley   }
145347c6ae99SBarry Smith   PetscFunctionReturn(0);
145447c6ae99SBarry Smith }
145547c6ae99SBarry Smith 
145647c6ae99SBarry Smith #undef __FUNCT__
145747c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
145847c6ae99SBarry Smith /*@
145947c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
146047c6ae99SBarry Smith 
146147c6ae99SBarry Smith     Collective on DM
146247c6ae99SBarry Smith 
146347c6ae99SBarry Smith     Input Parameter:
146447c6ae99SBarry Smith +   dm - the DM object
146547c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
146647c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
146747c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
146847c6ae99SBarry Smith 
146947c6ae99SBarry Smith     Level: developer
147047c6ae99SBarry Smith 
1471e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
147247c6ae99SBarry Smith          DMSetFunction()
147347c6ae99SBarry Smith 
147447c6ae99SBarry Smith @*/
14757087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
147647c6ae99SBarry Smith {
147747c6ae99SBarry Smith   PetscErrorCode ierr;
1478171400e9SBarry Smith 
147947c6ae99SBarry Smith   PetscFunctionBegin;
1480171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
148147c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
148247c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
148347c6ae99SBarry Smith   if (A != B) {
148447c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
148547c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
148647c6ae99SBarry Smith   }
148747c6ae99SBarry Smith   PetscFunctionReturn(0);
148847c6ae99SBarry Smith }
148947c6ae99SBarry Smith 
149047c6ae99SBarry Smith #undef __FUNCT__
149147c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
149247c6ae99SBarry Smith /*@
149347c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
149447c6ae99SBarry Smith 
149547c6ae99SBarry Smith     Collective on DM
149647c6ae99SBarry Smith 
149747c6ae99SBarry Smith     Input Parameter:
149847c6ae99SBarry Smith +   dm - the DM object
149991d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
150047c6ae99SBarry Smith 
150147c6ae99SBarry Smith     Output Parameter:
150247c6ae99SBarry Smith .   dmc - the coarsened DM
150347c6ae99SBarry Smith 
150447c6ae99SBarry Smith     Level: developer
150547c6ae99SBarry Smith 
1506e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
150747c6ae99SBarry Smith 
150847c6ae99SBarry Smith @*/
15097087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
151047c6ae99SBarry Smith {
151147c6ae99SBarry Smith   PetscErrorCode ierr;
1512b17ce1afSJed Brown   DMCoarsenHookLink link;
151347c6ae99SBarry Smith 
151447c6ae99SBarry Smith   PetscFunctionBegin;
1515171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
151647c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
151743842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
151847c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
151947c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
152047c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
152147c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
152247c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
152347c6ae99SBarry Smith   }
15248cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1525644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
15260598a293SJed Brown   (*dmc)->levelup   = dm->levelup;
1527656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1528e4b4b23bSJed Brown   ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
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   }
2001*a201590fSDmitry Karpeev   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
200208da532bSDmitry Karpeev   PetscFunctionReturn(0);
200308da532bSDmitry Karpeev }
200408da532bSDmitry Karpeev 
200508da532bSDmitry Karpeev #undef __FUNCT__
200647c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
200747c6ae99SBarry Smith /*@
200847c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
200947c6ae99SBarry Smith 
201047c6ae99SBarry Smith     Collective on DM
201147c6ae99SBarry Smith 
201247c6ae99SBarry Smith     Input Parameter:
201347c6ae99SBarry Smith +   dm - the DM object to destroy
201447c6ae99SBarry Smith -   x - the vector to hold the initial guess values
201547c6ae99SBarry Smith 
201647c6ae99SBarry Smith     Level: developer
201747c6ae99SBarry Smith 
2018e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
201947c6ae99SBarry Smith 
202047c6ae99SBarry Smith @*/
20217087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
202247c6ae99SBarry Smith {
202347c6ae99SBarry Smith   PetscErrorCode ierr;
202447c6ae99SBarry Smith   PetscFunctionBegin;
2025171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
202647c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
202747c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
202847c6ae99SBarry Smith   PetscFunctionReturn(0);
202947c6ae99SBarry Smith }
203047c6ae99SBarry Smith 
203147c6ae99SBarry Smith #undef __FUNCT__
203247c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
203347c6ae99SBarry Smith /*@
203447c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
203547c6ae99SBarry Smith 
203647c6ae99SBarry Smith     Not Collective
203747c6ae99SBarry Smith 
203847c6ae99SBarry Smith     Input Parameter:
203947c6ae99SBarry Smith .   dm - the DM object to destroy
204047c6ae99SBarry Smith 
204147c6ae99SBarry Smith     Output Parameter:
204247c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
204347c6ae99SBarry Smith 
204447c6ae99SBarry Smith     Level: developer
204547c6ae99SBarry Smith 
2046e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
204747c6ae99SBarry Smith 
204847c6ae99SBarry Smith @*/
20497087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
205047c6ae99SBarry Smith {
205147c6ae99SBarry Smith   PetscFunctionBegin;
2052171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
205347c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
205447c6ae99SBarry Smith   PetscFunctionReturn(0);
205547c6ae99SBarry Smith }
205647c6ae99SBarry Smith 
205747c6ae99SBarry Smith #undef __FUNCT__
205847c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
205947c6ae99SBarry Smith /*@
206047c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
206147c6ae99SBarry Smith 
206247c6ae99SBarry Smith     Not Collective
206347c6ae99SBarry Smith 
206447c6ae99SBarry Smith     Input Parameter:
206547c6ae99SBarry Smith .   dm - the DM object to destroy
206647c6ae99SBarry Smith 
206747c6ae99SBarry Smith     Output Parameter:
206847c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
206947c6ae99SBarry Smith 
207047c6ae99SBarry Smith     Level: developer
207147c6ae99SBarry Smith 
2072e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
207347c6ae99SBarry Smith 
207447c6ae99SBarry Smith @*/
20757087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
207647c6ae99SBarry Smith {
207747c6ae99SBarry Smith   PetscFunctionBegin;
2078171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
207947c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
208047c6ae99SBarry Smith   PetscFunctionReturn(0);
208147c6ae99SBarry Smith }
208247c6ae99SBarry Smith 
208347c6ae99SBarry Smith #undef __FUNCT__
208447c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
208547c6ae99SBarry Smith /*@
208647c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
208747c6ae99SBarry Smith 
208847c6ae99SBarry Smith     Not Collective
208947c6ae99SBarry Smith 
209047c6ae99SBarry Smith     Input Parameter:
209147c6ae99SBarry Smith .   dm - the DM object to destroy
209247c6ae99SBarry Smith 
209347c6ae99SBarry Smith     Output Parameter:
209447c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
209547c6ae99SBarry Smith 
209647c6ae99SBarry Smith     Level: developer
209747c6ae99SBarry Smith 
2098e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
209947c6ae99SBarry Smith 
210047c6ae99SBarry Smith @*/
21017087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
210247c6ae99SBarry Smith {
210347c6ae99SBarry Smith   PetscFunctionBegin;
2104171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
210547c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
210647c6ae99SBarry Smith   PetscFunctionReturn(0);
210747c6ae99SBarry Smith }
210847c6ae99SBarry Smith 
210947c6ae99SBarry Smith #undef  __FUNCT__
211008da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2111748fac09SDmitry Karpeev /*@C
211208da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
211308da532bSDmitry Karpeev 
211408da532bSDmitry Karpeev     Collective on DM
211508da532bSDmitry Karpeev 
211608da532bSDmitry Karpeev     Input Parameter:
211708da532bSDmitry Karpeev +   dm - the DM object
2118e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
211908da532bSDmitry Karpeev 
212008da532bSDmitry Karpeev     Level: developer
212108da532bSDmitry Karpeev 
2122e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
212308da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
212408da532bSDmitry Karpeev 
212508da532bSDmitry Karpeev @*/
212608da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
212708da532bSDmitry Karpeev {
212808da532bSDmitry Karpeev   PetscErrorCode ierr;
212908da532bSDmitry Karpeev   PetscFunctionBegin;
213008da532bSDmitry Karpeev   if (x) {
213108da532bSDmitry Karpeev     if (!dm->x) {
213208da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
213308da532bSDmitry Karpeev     }
213408da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
213508da532bSDmitry Karpeev   }
213608da532bSDmitry Karpeev   else if(dm->x) {
213708da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
213808da532bSDmitry Karpeev   }
213908da532bSDmitry Karpeev   PetscFunctionReturn(0);
214008da532bSDmitry Karpeev }
214108da532bSDmitry Karpeev 
214208da532bSDmitry Karpeev 
214308da532bSDmitry Karpeev #undef __FUNCT__
214447c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
214547c6ae99SBarry Smith /*@
214647c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
214747c6ae99SBarry Smith 
214847c6ae99SBarry Smith     Collective on DM
214947c6ae99SBarry Smith 
215047c6ae99SBarry Smith     Input Parameter:
215147c6ae99SBarry Smith +   dm - the DM object to destroy
215247c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
215347c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
215447c6ae99SBarry Smith 
215547c6ae99SBarry Smith     Level: developer
215647c6ae99SBarry Smith 
2157e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
215847c6ae99SBarry Smith          DMSetJacobian()
215947c6ae99SBarry Smith 
216047c6ae99SBarry Smith @*/
21617087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
216247c6ae99SBarry Smith {
216347c6ae99SBarry Smith   PetscErrorCode ierr;
216447c6ae99SBarry Smith   PetscFunctionBegin;
2165171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
216647c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
2167644e2e5bSBarry Smith   PetscStackPush("DM user function");
216847c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
2169644e2e5bSBarry Smith   PetscStackPop;
217047c6ae99SBarry Smith   PetscFunctionReturn(0);
217147c6ae99SBarry Smith }
217247c6ae99SBarry Smith 
217347c6ae99SBarry Smith 
217408da532bSDmitry Karpeev 
217547c6ae99SBarry Smith #undef __FUNCT__
217647c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
217747c6ae99SBarry Smith /*@
217847c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
217947c6ae99SBarry Smith 
218047c6ae99SBarry Smith     Collective on DM
218147c6ae99SBarry Smith 
218247c6ae99SBarry Smith     Input Parameter:
218347c6ae99SBarry Smith +   dm - the DM object
2184cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
218547c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
218647c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
218747c6ae99SBarry Smith 
218847c6ae99SBarry Smith     Level: developer
218947c6ae99SBarry Smith 
2190e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
219147c6ae99SBarry Smith          DMSetFunction()
219247c6ae99SBarry Smith 
219347c6ae99SBarry Smith @*/
21947087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
219547c6ae99SBarry Smith {
219647c6ae99SBarry Smith   PetscErrorCode ierr;
219747c6ae99SBarry Smith 
219847c6ae99SBarry Smith   PetscFunctionBegin;
2199171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
220047c6ae99SBarry Smith   if (!dm->ops->jacobian) {
220147c6ae99SBarry Smith     ISColoring     coloring;
220247c6ae99SBarry Smith     MatFDColoring  fd;
22032c9966d7SBarry Smith     const MatType  mtype;
220447c6ae99SBarry Smith 
22052c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
22062c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
220747c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
2208fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
220947c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
22100bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
22110bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
221271cd77b2SBarry Smith 
221347c6ae99SBarry Smith     dm->fd = fd;
221447c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
22152533e041SBarry Smith 
221671cd77b2SBarry Smith     /* don't know why this is needed */
221771cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
221847c6ae99SBarry Smith   }
221947c6ae99SBarry Smith   if (!x) x = dm->x;
222047c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
2221cab2e9ccSBarry Smith 
222271cd77b2SBarry 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 */
2223649052a6SBarry Smith   if (x) {
2224cab2e9ccSBarry Smith     if (!dm->x) {
222571cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
2226cab2e9ccSBarry Smith     }
2227cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
2228649052a6SBarry Smith   }
2229a8248277SBarry Smith   if (A != B) {
2230a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2231a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2232a8248277SBarry Smith   }
223347c6ae99SBarry Smith   PetscFunctionReturn(0);
223447c6ae99SBarry Smith }
2235264ace61SBarry Smith 
2236cab2e9ccSBarry Smith 
2237264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
2238264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
2239264ace61SBarry Smith 
2240264ace61SBarry Smith #undef __FUNCT__
2241264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2242264ace61SBarry Smith /*@C
2243264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2244264ace61SBarry Smith 
2245264ace61SBarry Smith   Collective on DM
2246264ace61SBarry Smith 
2247264ace61SBarry Smith   Input Parameters:
2248264ace61SBarry Smith + dm     - The DM object
2249264ace61SBarry Smith - method - The name of the DM type
2250264ace61SBarry Smith 
2251264ace61SBarry Smith   Options Database Key:
2252264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2253264ace61SBarry Smith 
2254264ace61SBarry Smith   Notes:
2255e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2256264ace61SBarry Smith 
2257264ace61SBarry Smith   Level: intermediate
2258264ace61SBarry Smith 
2259264ace61SBarry Smith .keywords: DM, set, type
2260264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2261264ace61SBarry Smith @*/
22627087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
2263264ace61SBarry Smith {
2264264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2265264ace61SBarry Smith   PetscBool      match;
2266264ace61SBarry Smith   PetscErrorCode ierr;
2267264ace61SBarry Smith 
2268264ace61SBarry Smith   PetscFunctionBegin;
2269264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2270251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2271264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2272264ace61SBarry Smith 
2273264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
22744b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2275264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2276264ace61SBarry Smith 
2277264ace61SBarry Smith   if (dm->ops->destroy) {
2278264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2279b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2280264ace61SBarry Smith   }
2281264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2282264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2283264ace61SBarry Smith   PetscFunctionReturn(0);
2284264ace61SBarry Smith }
2285264ace61SBarry Smith 
2286264ace61SBarry Smith #undef __FUNCT__
2287264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2288264ace61SBarry Smith /*@C
2289264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2290264ace61SBarry Smith 
2291264ace61SBarry Smith   Not Collective
2292264ace61SBarry Smith 
2293264ace61SBarry Smith   Input Parameter:
2294264ace61SBarry Smith . dm  - The DM
2295264ace61SBarry Smith 
2296264ace61SBarry Smith   Output Parameter:
2297264ace61SBarry Smith . type - The DM type name
2298264ace61SBarry Smith 
2299264ace61SBarry Smith   Level: intermediate
2300264ace61SBarry Smith 
2301264ace61SBarry Smith .keywords: DM, get, type, name
2302264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2303264ace61SBarry Smith @*/
23047087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
2305264ace61SBarry Smith {
2306264ace61SBarry Smith   PetscErrorCode ierr;
2307264ace61SBarry Smith 
2308264ace61SBarry Smith   PetscFunctionBegin;
2309264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2310264ace61SBarry Smith   PetscValidCharPointer(type,2);
2311264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2312264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2313264ace61SBarry Smith   }
2314264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2315264ace61SBarry Smith   PetscFunctionReturn(0);
2316264ace61SBarry Smith }
2317264ace61SBarry Smith 
231867a56275SMatthew G Knepley #undef __FUNCT__
231967a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
232067a56275SMatthew G Knepley /*@C
232167a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
232267a56275SMatthew G Knepley 
232367a56275SMatthew G Knepley   Collective on DM
232467a56275SMatthew G Knepley 
232567a56275SMatthew G Knepley   Input Parameters:
232667a56275SMatthew G Knepley + dm - the DM
232767a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
232867a56275SMatthew G Knepley 
232967a56275SMatthew G Knepley   Output Parameter:
233067a56275SMatthew G Knepley . M - pointer to new DM
233167a56275SMatthew G Knepley 
233267a56275SMatthew G Knepley   Notes:
233367a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
233467a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
233567a56275SMatthew G Knepley   of the input DM.
233667a56275SMatthew G Knepley 
233767a56275SMatthew G Knepley   Level: intermediate
233867a56275SMatthew G Knepley 
233967a56275SMatthew G Knepley .seealso: DMCreate()
234067a56275SMatthew G Knepley @*/
234167a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
234267a56275SMatthew G Knepley {
234367a56275SMatthew G Knepley   DM             B;
234467a56275SMatthew G Knepley   char           convname[256];
234567a56275SMatthew G Knepley   PetscBool      sametype, issame;
234667a56275SMatthew G Knepley   PetscErrorCode ierr;
234767a56275SMatthew G Knepley 
234867a56275SMatthew G Knepley   PetscFunctionBegin;
234967a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
235067a56275SMatthew G Knepley   PetscValidType(dm,1);
235167a56275SMatthew G Knepley   PetscValidPointer(M,3);
2352251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
235367a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
235467a56275SMatthew G Knepley   {
235567a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
235667a56275SMatthew G Knepley 
235767a56275SMatthew G Knepley     /*
235867a56275SMatthew G Knepley        Order of precedence:
235967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
236067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
236167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
236267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
236367a56275SMatthew G Knepley        5) Use a really basic converter.
236467a56275SMatthew G Knepley     */
236567a56275SMatthew G Knepley 
236667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
236767a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
236867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
236967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
237067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
237167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
237267a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
237367a56275SMatthew G Knepley     if (conv) goto foundconv;
237467a56275SMatthew G Knepley 
237567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
237667a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
237767a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
237867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
237967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
238067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
238167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
238267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
238367a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
238467a56275SMatthew G Knepley     if (conv) {
2385fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
238667a56275SMatthew G Knepley       goto foundconv;
238767a56275SMatthew G Knepley     }
238867a56275SMatthew G Knepley 
238967a56275SMatthew G Knepley #if 0
239067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
239167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2392fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
239367a56275SMatthew G Knepley     if (conv) goto foundconv;
239467a56275SMatthew G Knepley 
239567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
239667a56275SMatthew G Knepley     if (dm->ops->convert) {
239767a56275SMatthew G Knepley       conv = dm->ops->convert;
239867a56275SMatthew G Knepley     }
239967a56275SMatthew G Knepley     if (conv) goto foundconv;
240067a56275SMatthew G Knepley #endif
240167a56275SMatthew G Knepley 
240267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
240367a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
240467a56275SMatthew G Knepley 
240567a56275SMatthew G Knepley     foundconv:
240667a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
240767a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
240867a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
240967a56275SMatthew G Knepley   }
241067a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
241167a56275SMatthew G Knepley   PetscFunctionReturn(0);
241267a56275SMatthew G Knepley }
2413264ace61SBarry Smith 
2414264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2415264ace61SBarry Smith 
2416264ace61SBarry Smith #undef __FUNCT__
2417264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2418264ace61SBarry Smith /*@C
2419264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2420264ace61SBarry Smith 
2421264ace61SBarry Smith   Level: advanced
2422264ace61SBarry Smith @*/
24237087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2424264ace61SBarry Smith {
2425264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2426264ace61SBarry Smith   PetscErrorCode ierr;
2427264ace61SBarry Smith 
2428264ace61SBarry Smith   PetscFunctionBegin;
2429264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2430264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2431264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2432264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2433264ace61SBarry Smith   PetscFunctionReturn(0);
2434264ace61SBarry Smith }
2435264ace61SBarry Smith 
2436264ace61SBarry Smith 
2437264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2438264ace61SBarry Smith #undef __FUNCT__
2439264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2440264ace61SBarry Smith /*@C
2441264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2442264ace61SBarry Smith 
2443264ace61SBarry Smith    Not Collective
2444264ace61SBarry Smith 
2445264ace61SBarry Smith    Level: advanced
2446264ace61SBarry Smith 
2447264ace61SBarry Smith .keywords: DM, register, destroy
2448264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2449264ace61SBarry Smith @*/
24507087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2451264ace61SBarry Smith {
2452264ace61SBarry Smith   PetscErrorCode ierr;
2453264ace61SBarry Smith 
2454264ace61SBarry Smith   PetscFunctionBegin;
2455264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2456264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2457264ace61SBarry Smith   PetscFunctionReturn(0);
2458264ace61SBarry Smith }
245923f975d1SBarry Smith 
246023f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2461c6db04a5SJed Brown #include <mex.h>
246223f975d1SBarry Smith 
24633014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
246423f975d1SBarry Smith 
246523f975d1SBarry Smith #undef __FUNCT__
246623f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
246723f975d1SBarry Smith /*
246823f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
246923f975d1SBarry Smith                          DMSetFunctionMatlab().
247023f975d1SBarry Smith 
247123f975d1SBarry Smith    For linear problems x is null
247223f975d1SBarry Smith 
247323f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
247423f975d1SBarry Smith */
24757087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
247623f975d1SBarry Smith {
247723f975d1SBarry Smith   PetscErrorCode    ierr;
247823f975d1SBarry Smith   DMMatlabContext   *sctx;
247923f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
248023f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
248123f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
248223f975d1SBarry Smith 
248323f975d1SBarry Smith   PetscFunctionBegin;
248423f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
248523f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
248623f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
248723f975d1SBarry Smith 
248823f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
24891b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
249023f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
249123f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
24923014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
249323f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
249423f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
249523f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
249623f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2497b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
249823f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
249923f975d1SBarry Smith   mxDestroyArray(prhs[0]);
250023f975d1SBarry Smith   mxDestroyArray(prhs[1]);
250123f975d1SBarry Smith   mxDestroyArray(prhs[2]);
250223f975d1SBarry Smith   mxDestroyArray(prhs[3]);
250323f975d1SBarry Smith   mxDestroyArray(plhs[0]);
250423f975d1SBarry Smith   PetscFunctionReturn(0);
250523f975d1SBarry Smith }
250623f975d1SBarry Smith 
250723f975d1SBarry Smith 
250823f975d1SBarry Smith #undef __FUNCT__
250923f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
251023f975d1SBarry Smith /*
251123f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
251223f975d1SBarry Smith 
251323f975d1SBarry Smith */
25147087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
251523f975d1SBarry Smith {
251623f975d1SBarry Smith   PetscErrorCode    ierr;
251723f975d1SBarry Smith   DMMatlabContext   *sctx;
251823f975d1SBarry Smith 
251923f975d1SBarry Smith   PetscFunctionBegin;
2520171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
252123f975d1SBarry Smith   /* currently sctx is memory bleed */
25221b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
25233014e516SBarry Smith   if (!sctx) {
252423f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
25253014e516SBarry Smith   }
252623f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
25271b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
252823f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
252923f975d1SBarry Smith   PetscFunctionReturn(0);
253023f975d1SBarry Smith }
25313014e516SBarry Smith 
25323014e516SBarry Smith #undef __FUNCT__
25333014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
25343014e516SBarry Smith /*
25353014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
25363014e516SBarry Smith                          DMSetJacobianMatlab().
25373014e516SBarry Smith 
25383014e516SBarry Smith    For linear problems x is null
25393014e516SBarry Smith 
25403014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
25413014e516SBarry Smith */
25427087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
25433014e516SBarry Smith {
25443014e516SBarry Smith   PetscErrorCode    ierr;
25453014e516SBarry Smith   DMMatlabContext   *sctx;
25463014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
25473014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
25483014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
25493014e516SBarry Smith 
25503014e516SBarry Smith   PetscFunctionBegin;
25513014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25523014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
25533014e516SBarry Smith 
2554e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
25551b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
25563014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
25573014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
25583014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
25593014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
25603014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
25613014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
25623014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
25633014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
25643014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2565b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2566c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2567c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
25683014e516SBarry Smith   mxDestroyArray(prhs[0]);
25693014e516SBarry Smith   mxDestroyArray(prhs[1]);
25703014e516SBarry Smith   mxDestroyArray(prhs[2]);
25713014e516SBarry Smith   mxDestroyArray(prhs[3]);
25723014e516SBarry Smith   mxDestroyArray(prhs[4]);
25733014e516SBarry Smith   mxDestroyArray(plhs[0]);
25743014e516SBarry Smith   mxDestroyArray(plhs[1]);
25753014e516SBarry Smith   PetscFunctionReturn(0);
25763014e516SBarry Smith }
25773014e516SBarry Smith 
25783014e516SBarry Smith 
25793014e516SBarry Smith #undef __FUNCT__
25803014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
25813014e516SBarry Smith /*
25823014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
25833014e516SBarry Smith 
25843014e516SBarry Smith */
25857087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
25863014e516SBarry Smith {
25873014e516SBarry Smith   PetscErrorCode    ierr;
25883014e516SBarry Smith   DMMatlabContext   *sctx;
25893014e516SBarry Smith 
25903014e516SBarry Smith   PetscFunctionBegin;
2591171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25923014e516SBarry Smith   /* currently sctx is memory bleed */
25931b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
25943014e516SBarry Smith   if (!sctx) {
25953014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
25963014e516SBarry Smith   }
25973014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
25981b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
25993014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
26003014e516SBarry Smith   PetscFunctionReturn(0);
26013014e516SBarry Smith }
260223f975d1SBarry Smith #endif
2603b859378eSBarry Smith 
2604b859378eSBarry Smith #undef __FUNCT__
2605b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2606b859378eSBarry Smith /*@C
2607b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2608b859378eSBarry Smith   with DMView().
2609b859378eSBarry Smith 
2610b859378eSBarry Smith   Collective on PetscViewer
2611b859378eSBarry Smith 
2612b859378eSBarry Smith   Input Parameters:
2613b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2614b859378eSBarry Smith            some related function before a call to DMLoad().
2615b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2616b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2617b859378eSBarry Smith 
2618b859378eSBarry Smith    Level: intermediate
2619b859378eSBarry Smith 
2620b859378eSBarry Smith   Notes:
2621b859378eSBarry Smith   Defaults to the DM DA.
2622b859378eSBarry Smith 
2623b859378eSBarry Smith   Notes for advanced users:
2624b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2625b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2626b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2627b859378eSBarry Smith   format is
2628b859378eSBarry Smith .vb
2629b859378eSBarry Smith      has not yet been determined
2630b859378eSBarry Smith .ve
2631b859378eSBarry Smith 
2632b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2633b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2634b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2635b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2636b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2637b859378eSBarry Smith 
2638b859378eSBarry Smith   Concepts: vector^loading from file
2639b859378eSBarry Smith 
2640b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2641b859378eSBarry Smith @*/
2642b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2643b859378eSBarry Smith {
2644b859378eSBarry Smith   PetscErrorCode ierr;
2645b859378eSBarry Smith 
2646b859378eSBarry Smith   PetscFunctionBegin;
2647b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2648b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2649b859378eSBarry Smith 
2650b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2651b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2652b859378eSBarry Smith   }
2653b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2654b859378eSBarry Smith   PetscFunctionReturn(0);
2655b859378eSBarry Smith }
2656b859378eSBarry Smith 
26577da65231SMatthew G Knepley /******************************** FEM Support **********************************/
26587da65231SMatthew G Knepley 
26597da65231SMatthew G Knepley #undef __FUNCT__
26607da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
26617da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
26621d47ebbbSSatish Balay   PetscInt       f;
26631b30c384SMatthew G Knepley   PetscErrorCode ierr;
26641b30c384SMatthew G Knepley 
26657da65231SMatthew G Knepley   PetscFunctionBegin;
266674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26671d47ebbbSSatish Balay   for(f = 0; f < len; ++f) {
266874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
26697da65231SMatthew G Knepley   }
26707da65231SMatthew G Knepley   PetscFunctionReturn(0);
26717da65231SMatthew G Knepley }
26727da65231SMatthew G Knepley 
26737da65231SMatthew G Knepley #undef __FUNCT__
26747da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
26757da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
26761b30c384SMatthew G Knepley   PetscInt       f, g;
26777da65231SMatthew G Knepley   PetscErrorCode ierr;
26787da65231SMatthew G Knepley 
26797da65231SMatthew G Knepley   PetscFunctionBegin;
268074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
26811d47ebbbSSatish Balay   for(f = 0; f < rows; ++f) {
268274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
26831d47ebbbSSatish Balay     for(g = 0; g < cols; ++g) {
268474778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
26857da65231SMatthew G Knepley     }
268674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
26877da65231SMatthew G Knepley   }
26887da65231SMatthew G Knepley   PetscFunctionReturn(0);
26897da65231SMatthew G Knepley }
2690e7c4fc90SDmitry Karpeev 
2691970e74d5SMatthew G Knepley #undef __FUNCT__
2692970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
2693970e74d5SMatthew G Knepley /*@C
2694970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
2695970e74d5SMatthew G Knepley 
2696970e74d5SMatthew G Knepley   Not collective
2697970e74d5SMatthew G Knepley 
2698970e74d5SMatthew G Knepley   Input Parameter:
2699970e74d5SMatthew G Knepley . dm - The DM
2700970e74d5SMatthew G Knepley 
2701970e74d5SMatthew G Knepley   Output Parameter:
2702970e74d5SMatthew G Knepley . lf - The local residual function
2703970e74d5SMatthew G Knepley 
2704970e74d5SMatthew G Knepley    Calling sequence of lf:
2705970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2706970e74d5SMatthew G Knepley 
2707970e74d5SMatthew G Knepley +  snes - the SNES context
2708970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2709970e74d5SMatthew G Knepley .  f - local vector to put residual in
2710970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2711970e74d5SMatthew G Knepley 
2712970e74d5SMatthew G Knepley   Level: intermediate
2713970e74d5SMatthew G Knepley 
2714970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2715970e74d5SMatthew G Knepley @*/
2716970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
2717970e74d5SMatthew G Knepley {
2718970e74d5SMatthew G Knepley   PetscFunctionBegin;
2719970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2720970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
2721970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2722970e74d5SMatthew G Knepley }
2723970e74d5SMatthew G Knepley 
2724970e74d5SMatthew G Knepley #undef __FUNCT__
2725970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
2726970e74d5SMatthew G Knepley /*@C
2727970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
2728970e74d5SMatthew G Knepley 
2729970e74d5SMatthew G Knepley   Not collective
2730970e74d5SMatthew G Knepley 
2731970e74d5SMatthew G Knepley   Input Parameters:
2732970e74d5SMatthew G Knepley + dm - The DM
2733970e74d5SMatthew G Knepley - lf - The local residual function
2734970e74d5SMatthew G Knepley 
2735970e74d5SMatthew G Knepley    Calling sequence of lf:
2736970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2737970e74d5SMatthew G Knepley 
2738970e74d5SMatthew G Knepley +  snes - the SNES context
2739970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2740970e74d5SMatthew G Knepley .  f - local vector to put residual in
2741970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2742970e74d5SMatthew G Knepley 
2743970e74d5SMatthew G Knepley   Level: intermediate
2744970e74d5SMatthew G Knepley 
2745970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2746970e74d5SMatthew G Knepley @*/
2747970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
2748970e74d5SMatthew G Knepley {
2749970e74d5SMatthew G Knepley   PetscFunctionBegin;
2750970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2751970e74d5SMatthew G Knepley   dm->lf = lf;
2752970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2753970e74d5SMatthew G Knepley }
2754970e74d5SMatthew G Knepley 
2755970e74d5SMatthew G Knepley #undef __FUNCT__
2756970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
2757970e74d5SMatthew G Knepley /*@C
2758970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
2759970e74d5SMatthew G Knepley 
2760970e74d5SMatthew G Knepley   Not collective
2761970e74d5SMatthew G Knepley 
2762970e74d5SMatthew G Knepley   Input Parameter:
2763970e74d5SMatthew G Knepley . dm - The DM
2764970e74d5SMatthew G Knepley 
2765970e74d5SMatthew G Knepley   Output Parameter:
2766970e74d5SMatthew G Knepley . lj - The local Jacobian function
2767970e74d5SMatthew G Knepley 
2768970e74d5SMatthew G Knepley    Calling sequence of lj:
2769970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2770970e74d5SMatthew G Knepley 
2771970e74d5SMatthew G Knepley +  snes - the SNES context
2772970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2773970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2774970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2775970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2776970e74d5SMatthew G Knepley 
2777970e74d5SMatthew G Knepley   Level: intermediate
2778970e74d5SMatthew G Knepley 
2779970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2780970e74d5SMatthew G Knepley @*/
2781970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
2782970e74d5SMatthew G Knepley {
2783970e74d5SMatthew G Knepley   PetscFunctionBegin;
2784970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2785970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
2786970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2787970e74d5SMatthew G Knepley }
2788970e74d5SMatthew G Knepley 
2789970e74d5SMatthew G Knepley #undef __FUNCT__
2790970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
2791970e74d5SMatthew G Knepley /*@C
2792970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
2793970e74d5SMatthew G Knepley 
2794970e74d5SMatthew G Knepley   Not collective
2795970e74d5SMatthew G Knepley 
2796970e74d5SMatthew G Knepley   Input Parameters:
2797970e74d5SMatthew G Knepley + dm - The DM
2798970e74d5SMatthew G Knepley - lj - The local Jacobian function
2799970e74d5SMatthew G Knepley 
2800970e74d5SMatthew G Knepley    Calling sequence of lj:
2801970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2802970e74d5SMatthew G Knepley 
2803970e74d5SMatthew G Knepley +  snes - the SNES context
2804970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2805970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2806970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2807970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2808970e74d5SMatthew G Knepley 
2809970e74d5SMatthew G Knepley   Level: intermediate
2810970e74d5SMatthew G Knepley 
2811970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2812970e74d5SMatthew G Knepley @*/
2813970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
2814970e74d5SMatthew G Knepley {
2815970e74d5SMatthew G Knepley   PetscFunctionBegin;
2816970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2817970e74d5SMatthew G Knepley   dm->lj = lj;
2818970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2819970e74d5SMatthew G Knepley }
282088ed4aceSMatthew G Knepley 
282188ed4aceSMatthew G Knepley #undef __FUNCT__
282288ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
282388ed4aceSMatthew G Knepley /*@
282488ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
282588ed4aceSMatthew G Knepley 
282688ed4aceSMatthew G Knepley   Input Parameter:
282788ed4aceSMatthew G Knepley . dm - The DM
282888ed4aceSMatthew G Knepley 
282988ed4aceSMatthew G Knepley   Output Parameter:
283088ed4aceSMatthew G Knepley . section - The PetscSection
283188ed4aceSMatthew G Knepley 
283288ed4aceSMatthew G Knepley   Level: intermediate
283388ed4aceSMatthew G Knepley 
283488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
283588ed4aceSMatthew G Knepley 
283688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
283788ed4aceSMatthew G Knepley @*/
283888ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
283988ed4aceSMatthew G Knepley   PetscFunctionBegin;
284088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
284188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
284288ed4aceSMatthew G Knepley   *section = dm->defaultSection;
284388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
284488ed4aceSMatthew G Knepley }
284588ed4aceSMatthew G Knepley 
284688ed4aceSMatthew G Knepley #undef __FUNCT__
284788ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
284888ed4aceSMatthew G Knepley /*@
284988ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
285088ed4aceSMatthew G Knepley 
285188ed4aceSMatthew G Knepley   Input Parameters:
285288ed4aceSMatthew G Knepley + dm - The DM
285388ed4aceSMatthew G Knepley - section - The PetscSection
285488ed4aceSMatthew G Knepley 
285588ed4aceSMatthew G Knepley   Level: intermediate
285688ed4aceSMatthew G Knepley 
285788ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
285888ed4aceSMatthew G Knepley 
285988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
286088ed4aceSMatthew G Knepley @*/
286188ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
286288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
286388ed4aceSMatthew G Knepley 
286488ed4aceSMatthew G Knepley   PetscFunctionBegin;
286588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
286688ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
286788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
286888ed4aceSMatthew G Knepley   dm->defaultSection = section;
286988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
287088ed4aceSMatthew G Knepley }
287188ed4aceSMatthew G Knepley 
287288ed4aceSMatthew G Knepley #undef __FUNCT__
287388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
287488ed4aceSMatthew G Knepley /*@
287588ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
287688ed4aceSMatthew G Knepley 
287788ed4aceSMatthew G Knepley   Input Parameter:
287888ed4aceSMatthew G Knepley . dm - The DM
287988ed4aceSMatthew G Knepley 
288088ed4aceSMatthew G Knepley   Output Parameter:
288188ed4aceSMatthew G Knepley . section - The PetscSection
288288ed4aceSMatthew G Knepley 
288388ed4aceSMatthew G Knepley   Level: intermediate
288488ed4aceSMatthew G Knepley 
288588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
288688ed4aceSMatthew G Knepley 
288788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
288888ed4aceSMatthew G Knepley @*/
288988ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
289088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
289188ed4aceSMatthew G Knepley 
289288ed4aceSMatthew G Knepley   PetscFunctionBegin;
289388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
289488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
289588ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
289688ed4aceSMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, &dm->defaultGlobalSection);CHKERRQ(ierr);
289788ed4aceSMatthew G Knepley   }
289888ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
289988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
290088ed4aceSMatthew G Knepley }
290188ed4aceSMatthew G Knepley 
290288ed4aceSMatthew G Knepley #undef __FUNCT__
290388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
290488ed4aceSMatthew G Knepley /*@
290588ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
290688ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
290788ed4aceSMatthew G Knepley 
290888ed4aceSMatthew G Knepley   Input Parameter:
290988ed4aceSMatthew G Knepley . dm - The DM
291088ed4aceSMatthew G Knepley 
291188ed4aceSMatthew G Knepley   Output Parameter:
291288ed4aceSMatthew G Knepley . sf - The PetscSF
291388ed4aceSMatthew G Knepley 
291488ed4aceSMatthew G Knepley   Level: intermediate
291588ed4aceSMatthew G Knepley 
291688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
291788ed4aceSMatthew G Knepley 
291888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
291988ed4aceSMatthew G Knepley @*/
292088ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
292188ed4aceSMatthew G Knepley   PetscInt       nroots;
292288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
292388ed4aceSMatthew G Knepley 
292488ed4aceSMatthew G Knepley   PetscFunctionBegin;
292588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
292688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
292788ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
292888ed4aceSMatthew G Knepley   if (nroots < 0) {
292988ed4aceSMatthew G Knepley     PetscSection section, gSection;
293088ed4aceSMatthew G Knepley 
293188ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
293231ea6d37SMatthew G Knepley     if (section) {
293388ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
293488ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
293531ea6d37SMatthew G Knepley     } else {
293631ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
293731ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
293831ea6d37SMatthew G Knepley     }
293988ed4aceSMatthew G Knepley   }
294088ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
294188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
294288ed4aceSMatthew G Knepley }
294388ed4aceSMatthew G Knepley 
294488ed4aceSMatthew G Knepley #undef __FUNCT__
294588ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
294688ed4aceSMatthew G Knepley /*@
294788ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
294888ed4aceSMatthew G Knepley 
294988ed4aceSMatthew G Knepley   Input Parameters:
295088ed4aceSMatthew G Knepley + dm - The DM
295188ed4aceSMatthew G Knepley - sf - The PetscSF
295288ed4aceSMatthew G Knepley 
295388ed4aceSMatthew G Knepley   Level: intermediate
295488ed4aceSMatthew G Knepley 
295588ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
295688ed4aceSMatthew G Knepley 
295788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
295888ed4aceSMatthew G Knepley @*/
295988ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
296088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
296188ed4aceSMatthew G Knepley 
296288ed4aceSMatthew G Knepley   PetscFunctionBegin;
296388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
296488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
296588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
296688ed4aceSMatthew G Knepley   dm->defaultSF = sf;
296788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
296888ed4aceSMatthew G Knepley }
296988ed4aceSMatthew G Knepley 
297088ed4aceSMatthew G Knepley #undef __FUNCT__
297188ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
297288ed4aceSMatthew G Knepley /*@C
297388ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
297488ed4aceSMatthew G Knepley   describing the data layout.
297588ed4aceSMatthew G Knepley 
297688ed4aceSMatthew G Knepley   Input Parameters:
297788ed4aceSMatthew G Knepley + dm - The DM
297888ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
297988ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
298088ed4aceSMatthew G Knepley 
298188ed4aceSMatthew G Knepley   Level: intermediate
298288ed4aceSMatthew G Knepley 
298388ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
298488ed4aceSMatthew G Knepley @*/
298588ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
298688ed4aceSMatthew G Knepley {
298788ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
298888ed4aceSMatthew G Knepley   PetscLayout     layout;
298988ed4aceSMatthew G Knepley   const PetscInt *ranges;
299088ed4aceSMatthew G Knepley   PetscInt       *local;
299188ed4aceSMatthew G Knepley   PetscSFNode    *remote;
299288ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
299388ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
299488ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
299588ed4aceSMatthew G Knepley 
299688ed4aceSMatthew G Knepley   PetscFunctionBegin;
299788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
299888ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
299988ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
300088ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
300188ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
300288ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
300388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
300488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
300588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
300688ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
300788ed4aceSMatthew G Knepley   for(p = pStart, nleaves = 0; p < pEnd; ++p) {
300888ed4aceSMatthew G Knepley     PetscInt dof, cdof;
300988ed4aceSMatthew G Knepley 
301088ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &dof);CHKERRQ(ierr);
301188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &cdof);CHKERRQ(ierr);
301288ed4aceSMatthew G Knepley     nleaves += dof < 0 ? -(dof+1)-cdof : dof-cdof;
301388ed4aceSMatthew G Knepley   }
301488ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
301588ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
301688ed4aceSMatthew G Knepley   for(p = pStart, l = 0; p < pEnd; ++p) {
301788ed4aceSMatthew G Knepley     PetscInt *cind;
301888ed4aceSMatthew G Knepley     PetscInt  dof, gdof, cdof, dim, off, goff, d, c;
301988ed4aceSMatthew G Knepley 
302088ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
302188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
302288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
302388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
302488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
302588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
302688ed4aceSMatthew G Knepley     dim  = dof-cdof;
302788ed4aceSMatthew G Knepley     for(d = 0, c = 0; d < dof; ++d) {
302888ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
302988ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
303088ed4aceSMatthew G Knepley     }
303188ed4aceSMatthew G Knepley     if (gdof < 0) {
303288ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
303388ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
303488ed4aceSMatthew G Knepley 
303588ed4aceSMatthew G Knepley         for(r = 0; r < size; ++r) {
303688ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
303788ed4aceSMatthew G Knepley         }
303888ed4aceSMatthew G Knepley         remote[l].rank  = r;
303988ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
304088ed4aceSMatthew G Knepley       }
304188ed4aceSMatthew G Knepley     } else {
304288ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
304388ed4aceSMatthew G Knepley         remote[l].rank  = rank;
304488ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
304588ed4aceSMatthew G Knepley       }
304688ed4aceSMatthew G Knepley     }
304788ed4aceSMatthew G Knepley   }
304888ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
304988ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
305088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
305188ed4aceSMatthew G Knepley }
3052