xref: /petsc/src/dm/interface/dm.c (revision b17ce1af89b448f824bb09659a9de6779534cd8e)
147c6ae99SBarry Smith 
2c6db04a5SJed Brown #include <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 
44a89ea682SMatthew G Knepley   v->workSize     = 0;
45a89ea682SMatthew G Knepley   v->workArray    = PETSC_NULL;
461411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
471411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
481411c6eeSJed Brown   v->bs           = 1;
49171400e9SBarry Smith   v->coloringtype = IS_COLORING_GLOBAL;
501411c6eeSJed Brown 
511411c6eeSJed Brown   *dm = v;
52a4121054SBarry Smith   PetscFunctionReturn(0);
53a4121054SBarry Smith }
54a4121054SBarry Smith 
55a4121054SBarry Smith 
56a4121054SBarry Smith #undef __FUNCT__
579a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
589a42bb27SBarry Smith /*@C
59564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
609a42bb27SBarry Smith 
61aa219208SBarry Smith    Logically Collective on DMDA
629a42bb27SBarry Smith 
639a42bb27SBarry Smith    Input Parameter:
649a42bb27SBarry Smith +  da - initial distributed array
658154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
669a42bb27SBarry Smith 
679a42bb27SBarry Smith    Options Database:
68dd85299cSBarry Smith .   -dm_vec_type ctype
699a42bb27SBarry Smith 
709a42bb27SBarry Smith    Level: intermediate
719a42bb27SBarry Smith 
72aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
739a42bb27SBarry Smith @*/
747087cfbeSBarry Smith PetscErrorCode  DMSetVecType(DM da,const VecType ctype)
759a42bb27SBarry Smith {
769a42bb27SBarry Smith   PetscErrorCode ierr;
779a42bb27SBarry Smith 
789a42bb27SBarry Smith   PetscFunctionBegin;
799a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
809a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
819a42bb27SBarry Smith   ierr = PetscStrallocpy(ctype,&da->vectype);CHKERRQ(ierr);
829a42bb27SBarry Smith   PetscFunctionReturn(0);
839a42bb27SBarry Smith }
849a42bb27SBarry Smith 
859a42bb27SBarry Smith #undef __FUNCT__
869a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
879a42bb27SBarry Smith /*@C
889a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
89aa219208SBarry Smith    DMDA options in the database.
909a42bb27SBarry Smith 
91aa219208SBarry Smith    Logically Collective on DMDA
929a42bb27SBarry Smith 
939a42bb27SBarry Smith    Input Parameter:
94aa219208SBarry Smith +  da - the DMDA context
959a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
969a42bb27SBarry Smith 
979a42bb27SBarry Smith    Notes:
989a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
999a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
1009a42bb27SBarry Smith 
1019a42bb27SBarry Smith    Level: advanced
1029a42bb27SBarry Smith 
103aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
1049a42bb27SBarry Smith 
1059a42bb27SBarry Smith .seealso: DMSetFromOptions()
1069a42bb27SBarry Smith @*/
1077087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
1089a42bb27SBarry Smith {
1099a42bb27SBarry Smith   PetscErrorCode ierr;
1109a42bb27SBarry Smith 
1119a42bb27SBarry Smith   PetscFunctionBegin;
1129a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1139a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
1149a42bb27SBarry Smith   PetscFunctionReturn(0);
1159a42bb27SBarry Smith }
1169a42bb27SBarry Smith 
1179a42bb27SBarry Smith #undef __FUNCT__
11847c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
11947c6ae99SBarry Smith /*@
120aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
12147c6ae99SBarry Smith 
12247c6ae99SBarry Smith     Collective on DM
12347c6ae99SBarry Smith 
12447c6ae99SBarry Smith     Input Parameter:
12547c6ae99SBarry Smith .   dm - the DM object to destroy
12647c6ae99SBarry Smith 
12747c6ae99SBarry Smith     Level: developer
12847c6ae99SBarry Smith 
129e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
13047c6ae99SBarry Smith 
13147c6ae99SBarry Smith @*/
132fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
13347c6ae99SBarry Smith {
134732e2eb9SMatthew G Knepley   PetscInt       i, cnt = 0;
135*b17ce1afSJed Brown   DMCoarsenHookLink link,next;
13647c6ae99SBarry Smith   PetscErrorCode ierr;
13747c6ae99SBarry Smith 
13847c6ae99SBarry Smith   PetscFunctionBegin;
1396bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
1406bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
14187e657c6SBarry Smith 
14287e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
143732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1446bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
1456bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
146732e2eb9SMatthew G Knepley   }
14771cd77b2SBarry Smith   if ((*dm)->x) {
14871cd77b2SBarry Smith     PetscObject obj;
14971cd77b2SBarry Smith     ierr = PetscObjectQuery((PetscObject)(*dm)->x,"DM",&obj);CHKERRQ(ierr);
15071cd77b2SBarry Smith     if (obj == (PetscObject)*dm) cnt++;
15171cd77b2SBarry Smith   }
152732e2eb9SMatthew G Knepley 
1536bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
154732e2eb9SMatthew G Knepley   /*
155732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
156732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
157732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
158732e2eb9SMatthew G Knepley   */
1596bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
1606bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
161732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1626bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
1636bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
164732e2eb9SMatthew G Knepley   }
1651a266240SBarry Smith 
166*b17ce1afSJed Brown   /* Destroy the list of hooks */
167*b17ce1afSJed Brown   for (link=(*dm)->coarsenhook; link; link=next) {
168*b17ce1afSJed Brown     next = link->next;
169*b17ce1afSJed Brown     ierr = PetscFree(link);CHKERRQ(ierr);
170*b17ce1afSJed Brown   }
171*b17ce1afSJed Brown   (*dm)->coarsenhook = PETSC_NULL;
172*b17ce1afSJed Brown 
1731a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
1741a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
1751a266240SBarry Smith   }
17687e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
17771cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
1784dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
1796bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
1806bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
1816bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
182073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
183a89ea682SMatthew G Knepley   ierr = PetscFree((*dm)->workArray);CHKERRQ(ierr);
184732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
1856bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
186732e2eb9SMatthew G Knepley 
1876bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
1886bf464f9SBarry Smith   ierr = PetscFree((*dm)->data);CHKERRQ(ierr);
189732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
19047c6ae99SBarry Smith   PetscFunctionReturn(0);
19147c6ae99SBarry Smith }
19247c6ae99SBarry Smith 
19347c6ae99SBarry Smith #undef __FUNCT__
194d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
195d7bf68aeSBarry Smith /*@
196d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
197d7bf68aeSBarry Smith 
198d7bf68aeSBarry Smith     Collective on DM
199d7bf68aeSBarry Smith 
200d7bf68aeSBarry Smith     Input Parameter:
201d7bf68aeSBarry Smith .   dm - the DM object to setup
202d7bf68aeSBarry Smith 
203d7bf68aeSBarry Smith     Level: developer
204d7bf68aeSBarry Smith 
205e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
206d7bf68aeSBarry Smith 
207d7bf68aeSBarry Smith @*/
2087087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
209d7bf68aeSBarry Smith {
210d7bf68aeSBarry Smith   PetscErrorCode ierr;
211d7bf68aeSBarry Smith 
212d7bf68aeSBarry Smith   PetscFunctionBegin;
213171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2148387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
215d7bf68aeSBarry Smith   if (dm->ops->setup) {
216d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
217d7bf68aeSBarry Smith   }
2188387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
219d7bf68aeSBarry Smith   PetscFunctionReturn(0);
220d7bf68aeSBarry Smith }
221d7bf68aeSBarry Smith 
222d7bf68aeSBarry Smith #undef __FUNCT__
223d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
224d7bf68aeSBarry Smith /*@
225d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
226d7bf68aeSBarry Smith 
227d7bf68aeSBarry Smith     Collective on DM
228d7bf68aeSBarry Smith 
229d7bf68aeSBarry Smith     Input Parameter:
230d7bf68aeSBarry Smith .   dm - the DM object to set options for
231d7bf68aeSBarry Smith 
232732e2eb9SMatthew G Knepley     Options Database:
233dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
234dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
235171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
236171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
237732e2eb9SMatthew G Knepley 
238d7bf68aeSBarry Smith     Level: developer
239d7bf68aeSBarry Smith 
240e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
241d7bf68aeSBarry Smith 
242d7bf68aeSBarry Smith @*/
2437087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
244d7bf68aeSBarry Smith {
245c4721b0eSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg;
246d7bf68aeSBarry Smith   PetscErrorCode ierr;
247f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
248d7bf68aeSBarry Smith 
249d7bf68aeSBarry Smith   PetscFunctionBegin;
250171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2513194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
25282fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
253c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
254c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
255073dac72SJed 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);
256f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
257f9ba7244SBarry Smith     if (flg) {
258f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
259f9ba7244SBarry Smith     }
260f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_mat_type","Matrix type","MatSetType",MatList,typeName,typeName,sizeof typeName,&flg);CHKERRQ(ierr);
261073dac72SJed Brown     if (flg) {
262073dac72SJed Brown       ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
263f9ba7244SBarry Smith       ierr = PetscStrallocpy(typeName,&dm->mattype);CHKERRQ(ierr);
264073dac72SJed Brown     }
2651b89239cSHong 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);
266f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
267f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
268f9ba7244SBarry Smith     }
269f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
270f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
27182fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
27282fcb398SMatthew G Knepley   if (flg1) {
27382fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
27482fcb398SMatthew G Knepley   }
275c4721b0eSMatthew G Knepley   if (flg2) {
276c4721b0eSMatthew G Knepley     PetscViewer viewer;
277c4721b0eSMatthew G Knepley 
278c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
279c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
280c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
281c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
282c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
283c4721b0eSMatthew G Knepley   }
284c4721b0eSMatthew G Knepley   if (flg3) {
285c4721b0eSMatthew G Knepley     PetscViewer viewer;
286c4721b0eSMatthew G Knepley 
287c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
288c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
289c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
290c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
291c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
292c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
293c4721b0eSMatthew G Knepley   }
294d7bf68aeSBarry Smith   PetscFunctionReturn(0);
295d7bf68aeSBarry Smith }
296d7bf68aeSBarry Smith 
297d7bf68aeSBarry Smith #undef __FUNCT__
29847c6ae99SBarry Smith #define __FUNCT__ "DMView"
299fc9bc008SSatish Balay /*@C
300aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
30147c6ae99SBarry Smith 
30247c6ae99SBarry Smith     Collective on DM
30347c6ae99SBarry Smith 
30447c6ae99SBarry Smith     Input Parameter:
30547c6ae99SBarry Smith +   dm - the DM object to view
30647c6ae99SBarry Smith -   v - the viewer
30747c6ae99SBarry Smith 
30847c6ae99SBarry Smith     Level: developer
30947c6ae99SBarry Smith 
310e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
31147c6ae99SBarry Smith 
31247c6ae99SBarry Smith @*/
3137087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
31447c6ae99SBarry Smith {
31547c6ae99SBarry Smith   PetscErrorCode ierr;
31647c6ae99SBarry Smith 
31747c6ae99SBarry Smith   PetscFunctionBegin;
318171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3193014e516SBarry Smith  if (!v) {
3203014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
3213014e516SBarry Smith   }
3220c010503SBarry Smith   if (dm->ops->view) {
3230c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
32447c6ae99SBarry Smith   }
32547c6ae99SBarry Smith   PetscFunctionReturn(0);
32647c6ae99SBarry Smith }
32747c6ae99SBarry Smith 
32847c6ae99SBarry Smith #undef __FUNCT__
32947c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
33047c6ae99SBarry Smith /*@
331aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
33247c6ae99SBarry Smith 
33347c6ae99SBarry Smith     Collective on DM
33447c6ae99SBarry Smith 
33547c6ae99SBarry Smith     Input Parameter:
33647c6ae99SBarry Smith .   dm - the DM object
33747c6ae99SBarry Smith 
33847c6ae99SBarry Smith     Output Parameter:
33947c6ae99SBarry Smith .   vec - the global vector
34047c6ae99SBarry Smith 
341073dac72SJed Brown     Level: beginner
34247c6ae99SBarry Smith 
343e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
34447c6ae99SBarry Smith 
34547c6ae99SBarry Smith @*/
3467087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
34747c6ae99SBarry Smith {
34847c6ae99SBarry Smith   PetscErrorCode ierr;
34947c6ae99SBarry Smith 
35047c6ae99SBarry Smith   PetscFunctionBegin;
351171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35247c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
35347c6ae99SBarry Smith   PetscFunctionReturn(0);
35447c6ae99SBarry Smith }
35547c6ae99SBarry Smith 
35647c6ae99SBarry Smith #undef __FUNCT__
35747c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
35847c6ae99SBarry Smith /*@
359aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
36047c6ae99SBarry Smith 
36147c6ae99SBarry Smith     Not Collective
36247c6ae99SBarry Smith 
36347c6ae99SBarry Smith     Input Parameter:
36447c6ae99SBarry Smith .   dm - the DM object
36547c6ae99SBarry Smith 
36647c6ae99SBarry Smith     Output Parameter:
36747c6ae99SBarry Smith .   vec - the local vector
36847c6ae99SBarry Smith 
369073dac72SJed Brown     Level: beginner
37047c6ae99SBarry Smith 
371e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
37247c6ae99SBarry Smith 
37347c6ae99SBarry Smith @*/
3747087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
37547c6ae99SBarry Smith {
37647c6ae99SBarry Smith   PetscErrorCode ierr;
37747c6ae99SBarry Smith 
37847c6ae99SBarry Smith   PetscFunctionBegin;
379171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
38047c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
38147c6ae99SBarry Smith   PetscFunctionReturn(0);
38247c6ae99SBarry Smith }
38347c6ae99SBarry Smith 
38447c6ae99SBarry Smith #undef __FUNCT__
3851411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
3861411c6eeSJed Brown /*@
3871411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
3881411c6eeSJed Brown 
3891411c6eeSJed Brown    Collective on DM
3901411c6eeSJed Brown 
3911411c6eeSJed Brown    Input Parameter:
3921411c6eeSJed Brown .  dm - the DM that provides the mapping
3931411c6eeSJed Brown 
3941411c6eeSJed Brown    Output Parameter:
3951411c6eeSJed Brown .  ltog - the mapping
3961411c6eeSJed Brown 
3971411c6eeSJed Brown    Level: intermediate
3981411c6eeSJed Brown 
3991411c6eeSJed Brown    Notes:
4001411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
4011411c6eeSJed Brown    MatSetLocalToGlobalMapping().
4021411c6eeSJed Brown 
4031411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
4041411c6eeSJed Brown @*/
4057087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
4061411c6eeSJed Brown {
4071411c6eeSJed Brown   PetscErrorCode ierr;
4081411c6eeSJed Brown 
4091411c6eeSJed Brown   PetscFunctionBegin;
4101411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4111411c6eeSJed Brown   PetscValidPointer(ltog,2);
4121411c6eeSJed Brown   if (!dm->ltogmap) {
4131411c6eeSJed Brown     if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
4141411c6eeSJed Brown     ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
4151411c6eeSJed Brown   }
4161411c6eeSJed Brown   *ltog = dm->ltogmap;
4171411c6eeSJed Brown   PetscFunctionReturn(0);
4181411c6eeSJed Brown }
4191411c6eeSJed Brown 
4201411c6eeSJed Brown #undef __FUNCT__
4211411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
4221411c6eeSJed Brown /*@
4231411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
4241411c6eeSJed Brown 
4251411c6eeSJed Brown    Collective on DM
4261411c6eeSJed Brown 
4271411c6eeSJed Brown    Input Parameter:
4281411c6eeSJed Brown .  da - the distributed array that provides the mapping
4291411c6eeSJed Brown 
4301411c6eeSJed Brown    Output Parameter:
4311411c6eeSJed Brown .  ltog - the block mapping
4321411c6eeSJed Brown 
4331411c6eeSJed Brown    Level: intermediate
4341411c6eeSJed Brown 
4351411c6eeSJed Brown    Notes:
4361411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
4371411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
4381411c6eeSJed Brown 
4391411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
4401411c6eeSJed Brown @*/
4417087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
4421411c6eeSJed Brown {
4431411c6eeSJed Brown   PetscErrorCode ierr;
4441411c6eeSJed Brown 
4451411c6eeSJed Brown   PetscFunctionBegin;
4461411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4471411c6eeSJed Brown   PetscValidPointer(ltog,2);
4481411c6eeSJed Brown   if (!dm->ltogmapb) {
4491411c6eeSJed Brown     PetscInt bs;
4501411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
4511411c6eeSJed Brown     if (bs > 1) {
4521411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
4531411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
4541411c6eeSJed Brown     } else {
4551411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
4561411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
4571411c6eeSJed Brown     }
4581411c6eeSJed Brown   }
4591411c6eeSJed Brown   *ltog = dm->ltogmapb;
4601411c6eeSJed Brown   PetscFunctionReturn(0);
4611411c6eeSJed Brown }
4621411c6eeSJed Brown 
4631411c6eeSJed Brown #undef __FUNCT__
4641411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
4651411c6eeSJed Brown /*@
4661411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
4671411c6eeSJed Brown 
4681411c6eeSJed Brown    Not Collective
4691411c6eeSJed Brown 
4701411c6eeSJed Brown    Input Parameter:
4711411c6eeSJed Brown .  dm - the DM with block structure
4721411c6eeSJed Brown 
4731411c6eeSJed Brown    Output Parameter:
4741411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
4751411c6eeSJed Brown 
4761411c6eeSJed Brown    Level: intermediate
4771411c6eeSJed Brown 
4781411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
4791411c6eeSJed Brown @*/
4807087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
4811411c6eeSJed Brown {
4821411c6eeSJed Brown   PetscFunctionBegin;
4831411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4841411c6eeSJed Brown   PetscValidPointer(bs,2);
4851411c6eeSJed 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");
4861411c6eeSJed Brown   *bs = dm->bs;
4871411c6eeSJed Brown   PetscFunctionReturn(0);
4881411c6eeSJed Brown }
4891411c6eeSJed Brown 
4901411c6eeSJed Brown #undef __FUNCT__
491e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
49247c6ae99SBarry Smith /*@
493e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
49447c6ae99SBarry Smith 
49547c6ae99SBarry Smith     Collective on DM
49647c6ae99SBarry Smith 
49747c6ae99SBarry Smith     Input Parameter:
49847c6ae99SBarry Smith +   dm1 - the DM object
49947c6ae99SBarry Smith -   dm2 - the second, finer DM object
50047c6ae99SBarry Smith 
50147c6ae99SBarry Smith     Output Parameter:
50247c6ae99SBarry Smith +  mat - the interpolation
50347c6ae99SBarry Smith -  vec - the scaling (optional)
50447c6ae99SBarry Smith 
50547c6ae99SBarry Smith     Level: developer
50647c6ae99SBarry Smith 
50785afcc9aSBarry 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
50885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
509d52bd9f3SBarry Smith 
510d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
511d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
51285afcc9aSBarry Smith 
51385afcc9aSBarry Smith 
514e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
51547c6ae99SBarry Smith 
51647c6ae99SBarry Smith @*/
517e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
51847c6ae99SBarry Smith {
51947c6ae99SBarry Smith   PetscErrorCode ierr;
52047c6ae99SBarry Smith 
52147c6ae99SBarry Smith   PetscFunctionBegin;
522171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
523171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
52425296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
52547c6ae99SBarry Smith   PetscFunctionReturn(0);
52647c6ae99SBarry Smith }
52747c6ae99SBarry Smith 
52847c6ae99SBarry Smith #undef __FUNCT__
529e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
53047c6ae99SBarry Smith /*@
531e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
53247c6ae99SBarry Smith 
53347c6ae99SBarry Smith     Collective on DM
53447c6ae99SBarry Smith 
53547c6ae99SBarry Smith     Input Parameter:
53647c6ae99SBarry Smith +   dm1 - the DM object
53747c6ae99SBarry Smith -   dm2 - the second, finer DM object
53847c6ae99SBarry Smith 
53947c6ae99SBarry Smith     Output Parameter:
54047c6ae99SBarry Smith .   ctx - the injection
54147c6ae99SBarry Smith 
54247c6ae99SBarry Smith     Level: developer
54347c6ae99SBarry Smith 
54485afcc9aSBarry 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
54585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
54685afcc9aSBarry Smith 
547e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
54847c6ae99SBarry Smith 
54947c6ae99SBarry Smith @*/
550e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
55147c6ae99SBarry Smith {
55247c6ae99SBarry Smith   PetscErrorCode ierr;
55347c6ae99SBarry Smith 
55447c6ae99SBarry Smith   PetscFunctionBegin;
555171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
556171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
55747c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
55847c6ae99SBarry Smith   PetscFunctionReturn(0);
55947c6ae99SBarry Smith }
56047c6ae99SBarry Smith 
56147c6ae99SBarry Smith #undef __FUNCT__
562e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
563d1e2c406SBarry Smith /*@C
564e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
56547c6ae99SBarry Smith 
56647c6ae99SBarry Smith     Collective on DM
56747c6ae99SBarry Smith 
56847c6ae99SBarry Smith     Input Parameter:
56947c6ae99SBarry Smith +   dm - the DM object
57047c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
57147c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
57247c6ae99SBarry Smith 
57347c6ae99SBarry Smith     Output Parameter:
57447c6ae99SBarry Smith .   coloring - the coloring
57547c6ae99SBarry Smith 
57647c6ae99SBarry Smith     Level: developer
57747c6ae99SBarry Smith 
578e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
57947c6ae99SBarry Smith 
580aab9d709SJed Brown @*/
581e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
58247c6ae99SBarry Smith {
58347c6ae99SBarry Smith   PetscErrorCode ierr;
58447c6ae99SBarry Smith 
58547c6ae99SBarry Smith   PetscFunctionBegin;
586171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58747c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
58847c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
58947c6ae99SBarry Smith   PetscFunctionReturn(0);
59047c6ae99SBarry Smith }
59147c6ae99SBarry Smith 
59247c6ae99SBarry Smith #undef __FUNCT__
593950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
59447c6ae99SBarry Smith /*@C
595950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
59647c6ae99SBarry Smith 
59747c6ae99SBarry Smith     Collective on DM
59847c6ae99SBarry Smith 
59947c6ae99SBarry Smith     Input Parameter:
60047c6ae99SBarry Smith +   dm - the DM object
60147c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
60294013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
60347c6ae99SBarry Smith 
60447c6ae99SBarry Smith     Output Parameter:
60547c6ae99SBarry Smith .   mat - the empty Jacobian
60647c6ae99SBarry Smith 
607073dac72SJed Brown     Level: beginner
60847c6ae99SBarry Smith 
60994013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
61094013140SBarry Smith        do not need to do it yourself.
61194013140SBarry Smith 
61294013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
613aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
61494013140SBarry Smith 
61594013140SBarry 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
61694013140SBarry Smith        internally by PETSc.
61794013140SBarry Smith 
61894013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
619aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
62094013140SBarry Smith 
621e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
62247c6ae99SBarry Smith 
623aab9d709SJed Brown @*/
624950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
62547c6ae99SBarry Smith {
62647c6ae99SBarry Smith   PetscErrorCode ierr;
62747c6ae99SBarry Smith 
62847c6ae99SBarry Smith   PetscFunctionBegin;
629171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
630235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
631235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
632235683edSBarry Smith #endif
633c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
634c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
635073dac72SJed Brown   if (dm->mattype) {
63625296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
637073dac72SJed Brown   } else {
63825296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
639c7b7c8a4SJed Brown   }
64047c6ae99SBarry Smith   PetscFunctionReturn(0);
64147c6ae99SBarry Smith }
64247c6ae99SBarry Smith 
64347c6ae99SBarry Smith #undef __FUNCT__
644732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
645732e2eb9SMatthew G Knepley /*@
646950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
647732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
648732e2eb9SMatthew G Knepley 
649732e2eb9SMatthew G Knepley   Logically Collective on DMDA
650732e2eb9SMatthew G Knepley 
651732e2eb9SMatthew G Knepley   Input Parameter:
652732e2eb9SMatthew G Knepley + dm - the DM
653732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
654732e2eb9SMatthew G Knepley 
655732e2eb9SMatthew G Knepley   Level: developer
656950540a4SJed Brown .seealso DMCreateMatrix()
657732e2eb9SMatthew G Knepley @*/
658732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
659732e2eb9SMatthew G Knepley {
660732e2eb9SMatthew G Knepley   PetscFunctionBegin;
661732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
662732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
663732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
664732e2eb9SMatthew G Knepley }
665732e2eb9SMatthew G Knepley 
666732e2eb9SMatthew G Knepley #undef __FUNCT__
667a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
668a89ea682SMatthew G Knepley /*@C
669a89ea682SMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size
670a89ea682SMatthew G Knepley 
671a89ea682SMatthew G Knepley   Not Collective
672a89ea682SMatthew G Knepley 
673a89ea682SMatthew G Knepley   Input Parameters:
674a89ea682SMatthew G Knepley + dm - the DM object
675a89ea682SMatthew G Knepley - size - The minium size
676a89ea682SMatthew G Knepley 
677a89ea682SMatthew G Knepley   Output Parameter:
678a89ea682SMatthew G Knepley . array - the work array
679a89ea682SMatthew G Knepley 
680a89ea682SMatthew G Knepley   Level: developer
681a89ea682SMatthew G Knepley 
682a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
683a89ea682SMatthew G Knepley @*/
684a89ea682SMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt size,PetscScalar **array)
685a89ea682SMatthew G Knepley {
686a89ea682SMatthew G Knepley   PetscErrorCode ierr;
687a89ea682SMatthew G Knepley 
688a89ea682SMatthew G Knepley   PetscFunctionBegin;
689a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
690a89ea682SMatthew G Knepley   PetscValidPointer(array,3);
691a89ea682SMatthew G Knepley   if (size > dm->workSize) {
692a89ea682SMatthew G Knepley     dm->workSize = size;
693a89ea682SMatthew G Knepley     ierr = PetscFree(dm->workArray);CHKERRQ(ierr);
694a89ea682SMatthew G Knepley     ierr = PetscMalloc(dm->workSize * sizeof(PetscScalar), &dm->workArray);CHKERRQ(ierr);
695a89ea682SMatthew G Knepley   }
696a89ea682SMatthew G Knepley   *array = dm->workArray;
697a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
698a89ea682SMatthew G Knepley }
699a89ea682SMatthew G Knepley 
7005fe1f584SPeter Brune 
701a89ea682SMatthew G Knepley #undef __FUNCT__
70247c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
70347c6ae99SBarry Smith /*@
70447c6ae99SBarry Smith     DMRefine - Refines a DM object
70547c6ae99SBarry Smith 
70647c6ae99SBarry Smith     Collective on DM
70747c6ae99SBarry Smith 
70847c6ae99SBarry Smith     Input Parameter:
70947c6ae99SBarry Smith +   dm - the DM object
71047c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
71147c6ae99SBarry Smith 
71247c6ae99SBarry Smith     Output Parameter:
71347c6ae99SBarry Smith .   dmf - the refined DM
71447c6ae99SBarry Smith 
71547c6ae99SBarry Smith     Level: developer
71647c6ae99SBarry Smith 
717e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
71847c6ae99SBarry Smith 
71947c6ae99SBarry Smith @*/
7207087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
72147c6ae99SBarry Smith {
72247c6ae99SBarry Smith   PetscErrorCode ierr;
72347c6ae99SBarry Smith 
72447c6ae99SBarry Smith   PetscFunctionBegin;
725732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
72647c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
7274057135bSMatthew G Knepley   if (*dmf) {
728644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
729644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
730644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
731644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
732644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
733644e2e5bSBarry Smith     }
7348cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
735644e2e5bSBarry Smith     (*dmf)->ctx     = dm->ctx;
736656b349aSBarry Smith     (*dmf)->levelup = dm->levelup + 1;
7374057135bSMatthew G Knepley   }
73847c6ae99SBarry Smith   PetscFunctionReturn(0);
73947c6ae99SBarry Smith }
74047c6ae99SBarry Smith 
74147c6ae99SBarry Smith #undef __FUNCT__
742eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
743eb3f98d2SBarry Smith /*@
744eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
745eb3f98d2SBarry Smith 
746eb3f98d2SBarry Smith     Not Collective
747eb3f98d2SBarry Smith 
748eb3f98d2SBarry Smith     Input Parameter:
749eb3f98d2SBarry Smith .   dm - the DM object
750eb3f98d2SBarry Smith 
751eb3f98d2SBarry Smith     Output Parameter:
752eb3f98d2SBarry Smith .   level - number of refinements
753eb3f98d2SBarry Smith 
754eb3f98d2SBarry Smith     Level: developer
755eb3f98d2SBarry Smith 
7566a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
757eb3f98d2SBarry Smith 
758eb3f98d2SBarry Smith @*/
759eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
760eb3f98d2SBarry Smith {
761eb3f98d2SBarry Smith   PetscFunctionBegin;
762eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
763eb3f98d2SBarry Smith   *level = dm->levelup;
764eb3f98d2SBarry Smith   PetscFunctionReturn(0);
765eb3f98d2SBarry Smith }
766eb3f98d2SBarry Smith 
767eb3f98d2SBarry Smith #undef __FUNCT__
76847c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
76947c6ae99SBarry Smith /*@
77047c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
77147c6ae99SBarry Smith 
77247c6ae99SBarry Smith     Neighbor-wise Collective on DM
77347c6ae99SBarry Smith 
77447c6ae99SBarry Smith     Input Parameters:
77547c6ae99SBarry Smith +   dm - the DM object
77647c6ae99SBarry Smith .   g - the global vector
77747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
77847c6ae99SBarry Smith -   l - the local vector
77947c6ae99SBarry Smith 
78047c6ae99SBarry Smith 
78147c6ae99SBarry Smith     Level: beginner
78247c6ae99SBarry Smith 
783e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
78447c6ae99SBarry Smith 
78547c6ae99SBarry Smith @*/
7867087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
78747c6ae99SBarry Smith {
78847c6ae99SBarry Smith   PetscErrorCode ierr;
78947c6ae99SBarry Smith 
79047c6ae99SBarry Smith   PetscFunctionBegin;
791171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
792843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
79347c6ae99SBarry Smith   PetscFunctionReturn(0);
79447c6ae99SBarry Smith }
79547c6ae99SBarry Smith 
79647c6ae99SBarry Smith #undef __FUNCT__
79747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
79847c6ae99SBarry Smith /*@
79947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
80047c6ae99SBarry Smith 
80147c6ae99SBarry Smith     Neighbor-wise Collective on DM
80247c6ae99SBarry Smith 
80347c6ae99SBarry Smith     Input Parameters:
80447c6ae99SBarry Smith +   dm - the DM object
80547c6ae99SBarry Smith .   g - the global vector
80647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
80747c6ae99SBarry Smith -   l - the local vector
80847c6ae99SBarry Smith 
80947c6ae99SBarry Smith 
81047c6ae99SBarry Smith     Level: beginner
81147c6ae99SBarry Smith 
812e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
81347c6ae99SBarry Smith 
81447c6ae99SBarry Smith @*/
8157087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
81647c6ae99SBarry Smith {
81747c6ae99SBarry Smith   PetscErrorCode ierr;
81847c6ae99SBarry Smith 
81947c6ae99SBarry Smith   PetscFunctionBegin;
820171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
821843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
82247c6ae99SBarry Smith   PetscFunctionReturn(0);
82347c6ae99SBarry Smith }
82447c6ae99SBarry Smith 
82547c6ae99SBarry Smith #undef __FUNCT__
8269a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
82747c6ae99SBarry Smith /*@
8289a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
8299a42bb27SBarry Smith 
8309a42bb27SBarry Smith     Neighbor-wise Collective on DM
8319a42bb27SBarry Smith 
8329a42bb27SBarry Smith     Input Parameters:
8339a42bb27SBarry Smith +   dm - the DM object
834f6813fd5SJed Brown .   l - the local vector
8359a42bb27SBarry 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
8369a42bb27SBarry Smith            base point.
837f6813fd5SJed Brown - - the global vector
8389a42bb27SBarry Smith 
8399a42bb27SBarry 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
8409a42bb27SBarry 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
8419a42bb27SBarry Smith            global array to the final global array with VecAXPY().
8429a42bb27SBarry Smith 
8439a42bb27SBarry Smith     Level: beginner
8449a42bb27SBarry Smith 
845e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
8469a42bb27SBarry Smith 
8479a42bb27SBarry Smith @*/
8487087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
8499a42bb27SBarry Smith {
8509a42bb27SBarry Smith   PetscErrorCode ierr;
8519a42bb27SBarry Smith 
8529a42bb27SBarry Smith   PetscFunctionBegin;
853171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
854843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
8559a42bb27SBarry Smith   PetscFunctionReturn(0);
8569a42bb27SBarry Smith }
8579a42bb27SBarry Smith 
8589a42bb27SBarry Smith #undef __FUNCT__
8599a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
8609a42bb27SBarry Smith /*@
8619a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith     Neighbor-wise Collective on DM
86447c6ae99SBarry Smith 
86547c6ae99SBarry Smith     Input Parameters:
86647c6ae99SBarry Smith +   dm - the DM object
867f6813fd5SJed Brown .   l - the local vector
86847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
869f6813fd5SJed Brown -   g - the global vector
87047c6ae99SBarry Smith 
87147c6ae99SBarry Smith 
87247c6ae99SBarry Smith     Level: beginner
87347c6ae99SBarry Smith 
874e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith @*/
8777087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
87847c6ae99SBarry Smith {
87947c6ae99SBarry Smith   PetscErrorCode ierr;
88047c6ae99SBarry Smith 
88147c6ae99SBarry Smith   PetscFunctionBegin;
882171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
883843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
88447c6ae99SBarry Smith   PetscFunctionReturn(0);
88547c6ae99SBarry Smith }
88647c6ae99SBarry Smith 
88747c6ae99SBarry Smith #undef __FUNCT__
88847c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
88947c6ae99SBarry Smith /*@
89047c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
89147c6ae99SBarry Smith 
89247c6ae99SBarry Smith     Collective on DM
89347c6ae99SBarry Smith 
89447c6ae99SBarry Smith     Input Parameter:
89547c6ae99SBarry Smith +   dm - the DM object
89647c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
89747c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
89847c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
89947c6ae99SBarry Smith 
90047c6ae99SBarry Smith     Level: developer
90147c6ae99SBarry Smith 
902e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
90347c6ae99SBarry Smith          DMSetFunction()
90447c6ae99SBarry Smith 
90547c6ae99SBarry Smith @*/
9067087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
90747c6ae99SBarry Smith {
90847c6ae99SBarry Smith   PetscErrorCode ierr;
909171400e9SBarry Smith 
91047c6ae99SBarry Smith   PetscFunctionBegin;
911171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
91247c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
91347c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
91447c6ae99SBarry Smith   if (A != B) {
91547c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
91647c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
91747c6ae99SBarry Smith   }
91847c6ae99SBarry Smith   PetscFunctionReturn(0);
91947c6ae99SBarry Smith }
92047c6ae99SBarry Smith 
92147c6ae99SBarry Smith #undef __FUNCT__
92247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
92347c6ae99SBarry Smith /*@
92447c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
92547c6ae99SBarry Smith 
92647c6ae99SBarry Smith     Collective on DM
92747c6ae99SBarry Smith 
92847c6ae99SBarry Smith     Input Parameter:
92947c6ae99SBarry Smith +   dm - the DM object
93047c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
93147c6ae99SBarry Smith 
93247c6ae99SBarry Smith     Output Parameter:
93347c6ae99SBarry Smith .   dmc - the coarsened DM
93447c6ae99SBarry Smith 
93547c6ae99SBarry Smith     Level: developer
93647c6ae99SBarry Smith 
937e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
93847c6ae99SBarry Smith 
93947c6ae99SBarry Smith @*/
9407087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
94147c6ae99SBarry Smith {
94247c6ae99SBarry Smith   PetscErrorCode ierr;
943*b17ce1afSJed Brown   DMCoarsenHookLink link;
94447c6ae99SBarry Smith 
94547c6ae99SBarry Smith   PetscFunctionBegin;
946171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
94747c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
94847c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
94947c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
95047c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
95147c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
95247c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
95347c6ae99SBarry Smith   }
9548cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
955644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
956656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
957*b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
958*b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
959*b17ce1afSJed Brown   }
960*b17ce1afSJed Brown   PetscFunctionReturn(0);
961*b17ce1afSJed Brown }
962*b17ce1afSJed Brown 
963*b17ce1afSJed Brown #undef __FUNCT__
964*b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
965*b17ce1afSJed Brown /*@
966*b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
967*b17ce1afSJed Brown 
968*b17ce1afSJed Brown    Logically Collective
969*b17ce1afSJed Brown 
970*b17ce1afSJed Brown    Input Arguments:
971*b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
972*b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
973*b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
974*b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
975*b17ce1afSJed Brown 
976*b17ce1afSJed Brown    Calling sequence of coarsenhook:
977*b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
978*b17ce1afSJed Brown 
979*b17ce1afSJed Brown +  fine - fine level DM
980*b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
981*b17ce1afSJed Brown -  ctx - optional user-defined function context
982*b17ce1afSJed Brown 
983*b17ce1afSJed Brown    Calling sequence for restricthook:
984*b17ce1afSJed Brown $    restricthook(DM fine,Mat mrestrict,Mat inject,DM coarse,void *ctx)
985*b17ce1afSJed Brown 
986*b17ce1afSJed Brown +  fine - fine level DM
987*b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
988*b17ce1afSJed Brown .  inject - matrix restricting by applying the transpose of injection
989*b17ce1afSJed Brown .  coarse - coarse level DM to update
990*b17ce1afSJed Brown -  ctx - optional user-defined function context
991*b17ce1afSJed Brown 
992*b17ce1afSJed Brown    Level: advanced
993*b17ce1afSJed Brown 
994*b17ce1afSJed Brown    Notes:
995*b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
996*b17ce1afSJed Brown 
997*b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
998*b17ce1afSJed Brown 
999*b17ce1afSJed Brown    The
1000*b17ce1afSJed Brown 
1001*b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1002*b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1003*b17ce1afSJed Brown 
1004*b17ce1afSJed Brown .seealso: SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1005*b17ce1afSJed Brown @*/
1006*b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1007*b17ce1afSJed Brown {
1008*b17ce1afSJed Brown   PetscErrorCode ierr;
1009*b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1010*b17ce1afSJed Brown 
1011*b17ce1afSJed Brown   PetscFunctionBegin;
1012*b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
1013*b17ce1afSJed Brown   for (p=&fine->coarsenhook; *p; *p=(*p)->next) {} /* Scan to the end of the current list of hooks */
1014*b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1015*b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1016*b17ce1afSJed Brown   link->restricthook = restricthook;
1017*b17ce1afSJed Brown   link->ctx = ctx;
1018*b17ce1afSJed Brown   *p = link;
1019*b17ce1afSJed Brown   PetscFunctionReturn(0);
1020*b17ce1afSJed Brown }
1021*b17ce1afSJed Brown 
1022*b17ce1afSJed Brown #undef __FUNCT__
1023*b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1024*b17ce1afSJed Brown /*@
1025*b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1026*b17ce1afSJed Brown 
1027*b17ce1afSJed Brown    Collective if any hooks are
1028*b17ce1afSJed Brown 
1029*b17ce1afSJed Brown    Input Arguments:
1030*b17ce1afSJed Brown +  fine - finer DM to use as a base
1031*b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1032*b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1033*b17ce1afSJed Brown -  coarse - coarer DM to update
1034*b17ce1afSJed Brown 
1035*b17ce1afSJed Brown    Level: developer
1036*b17ce1afSJed Brown 
1037*b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1038*b17ce1afSJed Brown @*/
1039*b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1040*b17ce1afSJed Brown {
1041*b17ce1afSJed Brown   PetscErrorCode ierr;
1042*b17ce1afSJed Brown   DMCoarsenHookLink link;
1043*b17ce1afSJed Brown 
1044*b17ce1afSJed Brown   PetscFunctionBegin;
1045*b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1046*b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1047*b17ce1afSJed Brown   }
104847c6ae99SBarry Smith   PetscFunctionReturn(0);
104947c6ae99SBarry Smith }
105047c6ae99SBarry Smith 
105147c6ae99SBarry Smith #undef __FUNCT__
10525fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
10535fe1f584SPeter Brune /*@
10546a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
10555fe1f584SPeter Brune 
10565fe1f584SPeter Brune     Not Collective
10575fe1f584SPeter Brune 
10585fe1f584SPeter Brune     Input Parameter:
10595fe1f584SPeter Brune .   dm - the DM object
10605fe1f584SPeter Brune 
10615fe1f584SPeter Brune     Output Parameter:
10626a7d9d85SPeter Brune .   level - number of coarsenings
10635fe1f584SPeter Brune 
10645fe1f584SPeter Brune     Level: developer
10655fe1f584SPeter Brune 
10666a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
10675fe1f584SPeter Brune 
10685fe1f584SPeter Brune @*/
10695fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
10705fe1f584SPeter Brune {
10715fe1f584SPeter Brune   PetscFunctionBegin;
10725fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10735fe1f584SPeter Brune   *level = dm->leveldown;
10745fe1f584SPeter Brune   PetscFunctionReturn(0);
10755fe1f584SPeter Brune }
10765fe1f584SPeter Brune 
10775fe1f584SPeter Brune 
10785fe1f584SPeter Brune 
10795fe1f584SPeter Brune #undef __FUNCT__
108047c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
108147c6ae99SBarry Smith /*@C
108247c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
108347c6ae99SBarry Smith 
108447c6ae99SBarry Smith     Collective on DM
108547c6ae99SBarry Smith 
108647c6ae99SBarry Smith     Input Parameter:
108747c6ae99SBarry Smith +   dm - the DM object
108847c6ae99SBarry Smith -   nlevels - the number of levels of refinement
108947c6ae99SBarry Smith 
109047c6ae99SBarry Smith     Output Parameter:
109147c6ae99SBarry Smith .   dmf - the refined DM hierarchy
109247c6ae99SBarry Smith 
109347c6ae99SBarry Smith     Level: developer
109447c6ae99SBarry Smith 
1095e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
109647c6ae99SBarry Smith 
109747c6ae99SBarry Smith @*/
10987087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
109947c6ae99SBarry Smith {
110047c6ae99SBarry Smith   PetscErrorCode ierr;
110147c6ae99SBarry Smith 
110247c6ae99SBarry Smith   PetscFunctionBegin;
1103171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
110447c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
110547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
110647c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
110747c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
110847c6ae99SBarry Smith   } else if (dm->ops->refine) {
110947c6ae99SBarry Smith     PetscInt i;
111047c6ae99SBarry Smith 
111147c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
111247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
111347c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
111447c6ae99SBarry Smith     }
111547c6ae99SBarry Smith   } else {
111647c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
111747c6ae99SBarry Smith   }
111847c6ae99SBarry Smith   PetscFunctionReturn(0);
111947c6ae99SBarry Smith }
112047c6ae99SBarry Smith 
112147c6ae99SBarry Smith #undef __FUNCT__
112247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
112347c6ae99SBarry Smith /*@C
112447c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
112547c6ae99SBarry Smith 
112647c6ae99SBarry Smith     Collective on DM
112747c6ae99SBarry Smith 
112847c6ae99SBarry Smith     Input Parameter:
112947c6ae99SBarry Smith +   dm - the DM object
113047c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
113147c6ae99SBarry Smith 
113247c6ae99SBarry Smith     Output Parameter:
113347c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
113447c6ae99SBarry Smith 
113547c6ae99SBarry Smith     Level: developer
113647c6ae99SBarry Smith 
1137e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
113847c6ae99SBarry Smith 
113947c6ae99SBarry Smith @*/
11407087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
114147c6ae99SBarry Smith {
114247c6ae99SBarry Smith   PetscErrorCode ierr;
114347c6ae99SBarry Smith 
114447c6ae99SBarry Smith   PetscFunctionBegin;
1145171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
114647c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
114747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
114847c6ae99SBarry Smith   PetscValidPointer(dmc,3);
114947c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
115047c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
115147c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
115247c6ae99SBarry Smith     PetscInt i;
115347c6ae99SBarry Smith 
115447c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
115547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
115647c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
115747c6ae99SBarry Smith     }
115847c6ae99SBarry Smith   } else {
115947c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
116047c6ae99SBarry Smith   }
116147c6ae99SBarry Smith   PetscFunctionReturn(0);
116247c6ae99SBarry Smith }
116347c6ae99SBarry Smith 
116447c6ae99SBarry Smith #undef __FUNCT__
1165e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
116647c6ae99SBarry Smith /*@
1167e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
116847c6ae99SBarry Smith    grids associated with two DMs.
116947c6ae99SBarry Smith 
117047c6ae99SBarry Smith    Collective on DM
117147c6ae99SBarry Smith 
117247c6ae99SBarry Smith    Input Parameters:
117347c6ae99SBarry Smith +  dmc - the coarse grid DM
117447c6ae99SBarry Smith -  dmf - the fine grid DM
117547c6ae99SBarry Smith 
117647c6ae99SBarry Smith    Output Parameters:
117747c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
117847c6ae99SBarry Smith 
117947c6ae99SBarry Smith    Level: intermediate
118047c6ae99SBarry Smith 
118147c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
118247c6ae99SBarry Smith 
1183e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
118447c6ae99SBarry Smith @*/
1185e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
118647c6ae99SBarry Smith {
118747c6ae99SBarry Smith   PetscErrorCode ierr;
118847c6ae99SBarry Smith 
118947c6ae99SBarry Smith   PetscFunctionBegin;
1190171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1191171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
119247c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
119347c6ae99SBarry Smith   PetscFunctionReturn(0);
119447c6ae99SBarry Smith }
119547c6ae99SBarry Smith 
119647c6ae99SBarry Smith #undef __FUNCT__
11971a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
11981a266240SBarry Smith /*@C
11991a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
12001a266240SBarry Smith 
12011a266240SBarry Smith     Not Collective
12021a266240SBarry Smith 
12031a266240SBarry Smith     Input Parameters:
12041a266240SBarry Smith +   dm - the DM object
12051a266240SBarry Smith -   destroy - the destroy function
12061a266240SBarry Smith 
12071a266240SBarry Smith     Level: intermediate
12081a266240SBarry Smith 
1209e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
12101a266240SBarry Smith 
1211f07f9ceaSJed Brown @*/
12121a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
12131a266240SBarry Smith {
12141a266240SBarry Smith   PetscFunctionBegin;
1215171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12161a266240SBarry Smith   dm->ctxdestroy = destroy;
12171a266240SBarry Smith   PetscFunctionReturn(0);
12181a266240SBarry Smith }
12191a266240SBarry Smith 
12201a266240SBarry Smith #undef __FUNCT__
12211b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1222b07ff414SBarry Smith /*@
12231b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
122447c6ae99SBarry Smith 
122547c6ae99SBarry Smith     Not Collective
122647c6ae99SBarry Smith 
122747c6ae99SBarry Smith     Input Parameters:
122847c6ae99SBarry Smith +   dm - the DM object
122947c6ae99SBarry Smith -   ctx - the user context
123047c6ae99SBarry Smith 
123147c6ae99SBarry Smith     Level: intermediate
123247c6ae99SBarry Smith 
1233e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
123447c6ae99SBarry Smith 
123547c6ae99SBarry Smith @*/
12361b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
123747c6ae99SBarry Smith {
123847c6ae99SBarry Smith   PetscFunctionBegin;
1239171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
124047c6ae99SBarry Smith   dm->ctx = ctx;
124147c6ae99SBarry Smith   PetscFunctionReturn(0);
124247c6ae99SBarry Smith }
124347c6ae99SBarry Smith 
124447c6ae99SBarry Smith #undef __FUNCT__
12451b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
124647c6ae99SBarry Smith /*@
12471b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
124847c6ae99SBarry Smith 
124947c6ae99SBarry Smith     Not Collective
125047c6ae99SBarry Smith 
125147c6ae99SBarry Smith     Input Parameter:
125247c6ae99SBarry Smith .   dm - the DM object
125347c6ae99SBarry Smith 
125447c6ae99SBarry Smith     Output Parameter:
125547c6ae99SBarry Smith .   ctx - the user context
125647c6ae99SBarry Smith 
125747c6ae99SBarry Smith     Level: intermediate
125847c6ae99SBarry Smith 
1259e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
126047c6ae99SBarry Smith 
126147c6ae99SBarry Smith @*/
12621b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
126347c6ae99SBarry Smith {
126447c6ae99SBarry Smith   PetscFunctionBegin;
1265171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12661b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
126747c6ae99SBarry Smith   PetscFunctionReturn(0);
126847c6ae99SBarry Smith }
126947c6ae99SBarry Smith 
127047c6ae99SBarry Smith #undef __FUNCT__
127147c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
12727e833e3aSBarry Smith /*@C
127347c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
127447c6ae99SBarry Smith 
127547c6ae99SBarry Smith     Logically Collective on DM
127647c6ae99SBarry Smith 
127747c6ae99SBarry Smith     Input Parameter:
127847c6ae99SBarry Smith +   dm - the DM object to destroy
127947c6ae99SBarry Smith -   f - the function to compute the initial guess
128047c6ae99SBarry Smith 
128147c6ae99SBarry Smith     Level: intermediate
128247c6ae99SBarry Smith 
1283e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
128447c6ae99SBarry Smith 
1285f07f9ceaSJed Brown @*/
12867087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
128747c6ae99SBarry Smith {
128847c6ae99SBarry Smith   PetscFunctionBegin;
1289171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
129047c6ae99SBarry Smith   dm->ops->initialguess = f;
129147c6ae99SBarry Smith   PetscFunctionReturn(0);
129247c6ae99SBarry Smith }
129347c6ae99SBarry Smith 
129447c6ae99SBarry Smith #undef __FUNCT__
129547c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
12967e833e3aSBarry Smith /*@C
129747c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
129847c6ae99SBarry Smith 
129947c6ae99SBarry Smith     Logically Collective on DM
130047c6ae99SBarry Smith 
130147c6ae99SBarry Smith     Input Parameter:
130247c6ae99SBarry Smith +   dm - the DM object
130347c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
130447c6ae99SBarry Smith 
130547c6ae99SBarry Smith     Level: intermediate
130647c6ae99SBarry Smith 
130747c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
130847c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
130947c6ae99SBarry Smith 
1310e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
131147c6ae99SBarry Smith          DMSetJacobian()
131247c6ae99SBarry Smith 
1313f07f9ceaSJed Brown @*/
13147087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
131547c6ae99SBarry Smith {
131647c6ae99SBarry Smith   PetscFunctionBegin;
1317171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
131847c6ae99SBarry Smith   dm->ops->function = f;
131947c6ae99SBarry Smith   if (f) {
132047c6ae99SBarry Smith     dm->ops->functionj = f;
132147c6ae99SBarry Smith   }
132247c6ae99SBarry Smith   PetscFunctionReturn(0);
132347c6ae99SBarry Smith }
132447c6ae99SBarry Smith 
132547c6ae99SBarry Smith #undef __FUNCT__
132647c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
13277e833e3aSBarry Smith /*@C
132847c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
132947c6ae99SBarry Smith 
133047c6ae99SBarry Smith     Logically Collective on DM
133147c6ae99SBarry Smith 
133247c6ae99SBarry Smith     Input Parameter:
133347c6ae99SBarry Smith +   dm - the DM object to destroy
133447c6ae99SBarry Smith -   f - the function to compute the matrix entries
133547c6ae99SBarry Smith 
133647c6ae99SBarry Smith     Level: intermediate
133747c6ae99SBarry Smith 
1338e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
133947c6ae99SBarry Smith          DMSetFunction()
134047c6ae99SBarry Smith 
1341f07f9ceaSJed Brown @*/
13427087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
134347c6ae99SBarry Smith {
134447c6ae99SBarry Smith   PetscFunctionBegin;
1345171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
134647c6ae99SBarry Smith   dm->ops->jacobian = f;
134747c6ae99SBarry Smith   PetscFunctionReturn(0);
134847c6ae99SBarry Smith }
134947c6ae99SBarry Smith 
135047c6ae99SBarry Smith #undef __FUNCT__
135147c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
135247c6ae99SBarry Smith /*@
135347c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
135447c6ae99SBarry Smith 
135547c6ae99SBarry Smith     Collective on DM
135647c6ae99SBarry Smith 
135747c6ae99SBarry Smith     Input Parameter:
135847c6ae99SBarry Smith +   dm - the DM object to destroy
135947c6ae99SBarry Smith -   x - the vector to hold the initial guess values
136047c6ae99SBarry Smith 
136147c6ae99SBarry Smith     Level: developer
136247c6ae99SBarry Smith 
1363e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
136447c6ae99SBarry Smith 
136547c6ae99SBarry Smith @*/
13667087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
136747c6ae99SBarry Smith {
136847c6ae99SBarry Smith   PetscErrorCode ierr;
136947c6ae99SBarry Smith   PetscFunctionBegin;
1370171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
137147c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
137247c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
137347c6ae99SBarry Smith   PetscFunctionReturn(0);
137447c6ae99SBarry Smith }
137547c6ae99SBarry Smith 
137647c6ae99SBarry Smith #undef __FUNCT__
137747c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
137847c6ae99SBarry Smith /*@
137947c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
138047c6ae99SBarry Smith 
138147c6ae99SBarry Smith     Not Collective
138247c6ae99SBarry Smith 
138347c6ae99SBarry Smith     Input Parameter:
138447c6ae99SBarry Smith .   dm - the DM object to destroy
138547c6ae99SBarry Smith 
138647c6ae99SBarry Smith     Output Parameter:
138747c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
138847c6ae99SBarry Smith 
138947c6ae99SBarry Smith     Level: developer
139047c6ae99SBarry Smith 
1391e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
139247c6ae99SBarry Smith 
139347c6ae99SBarry Smith @*/
13947087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
139547c6ae99SBarry Smith {
139647c6ae99SBarry Smith   PetscFunctionBegin;
1397171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
139847c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
139947c6ae99SBarry Smith   PetscFunctionReturn(0);
140047c6ae99SBarry Smith }
140147c6ae99SBarry Smith 
140247c6ae99SBarry Smith #undef __FUNCT__
140347c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
140447c6ae99SBarry Smith /*@
140547c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
140647c6ae99SBarry Smith 
140747c6ae99SBarry Smith     Not Collective
140847c6ae99SBarry Smith 
140947c6ae99SBarry Smith     Input Parameter:
141047c6ae99SBarry Smith .   dm - the DM object to destroy
141147c6ae99SBarry Smith 
141247c6ae99SBarry Smith     Output Parameter:
141347c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
141447c6ae99SBarry Smith 
141547c6ae99SBarry Smith     Level: developer
141647c6ae99SBarry Smith 
1417e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
141847c6ae99SBarry Smith 
141947c6ae99SBarry Smith @*/
14207087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
142147c6ae99SBarry Smith {
142247c6ae99SBarry Smith   PetscFunctionBegin;
1423171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
142447c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
142547c6ae99SBarry Smith   PetscFunctionReturn(0);
142647c6ae99SBarry Smith }
142747c6ae99SBarry Smith 
142847c6ae99SBarry Smith #undef __FUNCT__
142947c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
143047c6ae99SBarry Smith /*@
143147c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
143247c6ae99SBarry Smith 
143347c6ae99SBarry Smith     Not Collective
143447c6ae99SBarry Smith 
143547c6ae99SBarry Smith     Input Parameter:
143647c6ae99SBarry Smith .   dm - the DM object to destroy
143747c6ae99SBarry Smith 
143847c6ae99SBarry Smith     Output Parameter:
143947c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
144047c6ae99SBarry Smith 
144147c6ae99SBarry Smith     Level: developer
144247c6ae99SBarry Smith 
1443e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
144447c6ae99SBarry Smith 
144547c6ae99SBarry Smith @*/
14467087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
144747c6ae99SBarry Smith {
144847c6ae99SBarry Smith   PetscFunctionBegin;
1449171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
145047c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
145147c6ae99SBarry Smith   PetscFunctionReturn(0);
145247c6ae99SBarry Smith }
145347c6ae99SBarry Smith 
145447c6ae99SBarry Smith #undef __FUNCT__
145547c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
145647c6ae99SBarry Smith /*@
145747c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
145847c6ae99SBarry Smith 
145947c6ae99SBarry Smith     Collective on DM
146047c6ae99SBarry Smith 
146147c6ae99SBarry Smith     Input Parameter:
146247c6ae99SBarry Smith +   dm - the DM object to destroy
146347c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
146447c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
146547c6ae99SBarry Smith 
146647c6ae99SBarry Smith     Level: developer
146747c6ae99SBarry Smith 
1468e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
146947c6ae99SBarry Smith          DMSetJacobian()
147047c6ae99SBarry Smith 
147147c6ae99SBarry Smith @*/
14727087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
147347c6ae99SBarry Smith {
147447c6ae99SBarry Smith   PetscErrorCode ierr;
147547c6ae99SBarry Smith   PetscFunctionBegin;
1476171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
147747c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
1478644e2e5bSBarry Smith   PetscStackPush("DM user function");
147947c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
1480644e2e5bSBarry Smith   PetscStackPop;
148147c6ae99SBarry Smith   PetscFunctionReturn(0);
148247c6ae99SBarry Smith }
148347c6ae99SBarry Smith 
148447c6ae99SBarry Smith 
148547c6ae99SBarry Smith #undef __FUNCT__
148647c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
148747c6ae99SBarry Smith /*@
148847c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
148947c6ae99SBarry Smith 
149047c6ae99SBarry Smith     Collective on DM
149147c6ae99SBarry Smith 
149247c6ae99SBarry Smith     Input Parameter:
149347c6ae99SBarry Smith +   dm - the DM object
1494cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
149547c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
149647c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
149747c6ae99SBarry Smith 
149847c6ae99SBarry Smith     Level: developer
149947c6ae99SBarry Smith 
1500e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
150147c6ae99SBarry Smith          DMSetFunction()
150247c6ae99SBarry Smith 
150347c6ae99SBarry Smith @*/
15047087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
150547c6ae99SBarry Smith {
150647c6ae99SBarry Smith   PetscErrorCode ierr;
150747c6ae99SBarry Smith 
150847c6ae99SBarry Smith   PetscFunctionBegin;
1509171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
151047c6ae99SBarry Smith   if (!dm->ops->jacobian) {
151147c6ae99SBarry Smith     ISColoring     coloring;
151247c6ae99SBarry Smith     MatFDColoring  fd;
151347c6ae99SBarry Smith 
1514171400e9SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,MATAIJ,&coloring);CHKERRQ(ierr);
151547c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
1516fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
151747c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
15180bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
15190bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
152071cd77b2SBarry Smith 
152147c6ae99SBarry Smith     dm->fd = fd;
152247c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
15232533e041SBarry Smith 
152471cd77b2SBarry Smith     /* don't know why this is needed */
152571cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
152647c6ae99SBarry Smith   }
152747c6ae99SBarry Smith   if (!x) x = dm->x;
152847c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
1529cab2e9ccSBarry Smith 
153071cd77b2SBarry 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 */
1531649052a6SBarry Smith   if (x) {
1532cab2e9ccSBarry Smith     if (!dm->x) {
153371cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
1534cab2e9ccSBarry Smith     }
1535cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
1536649052a6SBarry Smith   }
1537a8248277SBarry Smith   if (A != B) {
1538a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1539a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1540a8248277SBarry Smith   }
154147c6ae99SBarry Smith   PetscFunctionReturn(0);
154247c6ae99SBarry Smith }
1543264ace61SBarry Smith 
1544cab2e9ccSBarry Smith 
1545264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
1546264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
1547264ace61SBarry Smith 
1548264ace61SBarry Smith #undef __FUNCT__
1549264ace61SBarry Smith #define __FUNCT__ "DMSetType"
1550264ace61SBarry Smith /*@C
1551264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
1552264ace61SBarry Smith 
1553264ace61SBarry Smith   Collective on DM
1554264ace61SBarry Smith 
1555264ace61SBarry Smith   Input Parameters:
1556264ace61SBarry Smith + dm     - The DM object
1557264ace61SBarry Smith - method - The name of the DM type
1558264ace61SBarry Smith 
1559264ace61SBarry Smith   Options Database Key:
1560264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
1561264ace61SBarry Smith 
1562264ace61SBarry Smith   Notes:
1563e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
1564264ace61SBarry Smith 
1565264ace61SBarry Smith   Level: intermediate
1566264ace61SBarry Smith 
1567264ace61SBarry Smith .keywords: DM, set, type
1568264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
1569264ace61SBarry Smith @*/
15707087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
1571264ace61SBarry Smith {
1572264ace61SBarry Smith   PetscErrorCode (*r)(DM);
1573264ace61SBarry Smith   PetscBool      match;
1574264ace61SBarry Smith   PetscErrorCode ierr;
1575264ace61SBarry Smith 
1576264ace61SBarry Smith   PetscFunctionBegin;
1577264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1578264ace61SBarry Smith   ierr = PetscTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
1579264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
1580264ace61SBarry Smith 
1581264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
15824b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
1583264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
1584264ace61SBarry Smith 
1585264ace61SBarry Smith   if (dm->ops->destroy) {
1586264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
1587264ace61SBarry Smith   }
1588264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
1589264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
1590264ace61SBarry Smith   PetscFunctionReturn(0);
1591264ace61SBarry Smith }
1592264ace61SBarry Smith 
1593264ace61SBarry Smith #undef __FUNCT__
1594264ace61SBarry Smith #define __FUNCT__ "DMGetType"
1595264ace61SBarry Smith /*@C
1596264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
1597264ace61SBarry Smith 
1598264ace61SBarry Smith   Not Collective
1599264ace61SBarry Smith 
1600264ace61SBarry Smith   Input Parameter:
1601264ace61SBarry Smith . dm  - The DM
1602264ace61SBarry Smith 
1603264ace61SBarry Smith   Output Parameter:
1604264ace61SBarry Smith . type - The DM type name
1605264ace61SBarry Smith 
1606264ace61SBarry Smith   Level: intermediate
1607264ace61SBarry Smith 
1608264ace61SBarry Smith .keywords: DM, get, type, name
1609264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
1610264ace61SBarry Smith @*/
16117087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
1612264ace61SBarry Smith {
1613264ace61SBarry Smith   PetscErrorCode ierr;
1614264ace61SBarry Smith 
1615264ace61SBarry Smith   PetscFunctionBegin;
1616264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1617264ace61SBarry Smith   PetscValidCharPointer(type,2);
1618264ace61SBarry Smith   if (!DMRegisterAllCalled) {
1619264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
1620264ace61SBarry Smith   }
1621264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
1622264ace61SBarry Smith   PetscFunctionReturn(0);
1623264ace61SBarry Smith }
1624264ace61SBarry Smith 
162567a56275SMatthew G Knepley #undef __FUNCT__
162667a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
162767a56275SMatthew G Knepley /*@C
162867a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
162967a56275SMatthew G Knepley 
163067a56275SMatthew G Knepley   Collective on DM
163167a56275SMatthew G Knepley 
163267a56275SMatthew G Knepley   Input Parameters:
163367a56275SMatthew G Knepley + dm - the DM
163467a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
163567a56275SMatthew G Knepley 
163667a56275SMatthew G Knepley   Output Parameter:
163767a56275SMatthew G Knepley . M - pointer to new DM
163867a56275SMatthew G Knepley 
163967a56275SMatthew G Knepley   Notes:
164067a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
164167a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
164267a56275SMatthew G Knepley   of the input DM.
164367a56275SMatthew G Knepley 
164467a56275SMatthew G Knepley   Level: intermediate
164567a56275SMatthew G Knepley 
164667a56275SMatthew G Knepley .seealso: DMCreate()
164767a56275SMatthew G Knepley @*/
164867a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
164967a56275SMatthew G Knepley {
165067a56275SMatthew G Knepley   DM             B;
165167a56275SMatthew G Knepley   char           convname[256];
165267a56275SMatthew G Knepley   PetscBool      sametype, issame;
165367a56275SMatthew G Knepley   PetscErrorCode ierr;
165467a56275SMatthew G Knepley 
165567a56275SMatthew G Knepley   PetscFunctionBegin;
165667a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
165767a56275SMatthew G Knepley   PetscValidType(dm,1);
165867a56275SMatthew G Knepley   PetscValidPointer(M,3);
165967a56275SMatthew G Knepley   ierr = PetscTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
166067a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
166167a56275SMatthew G Knepley   {
166267a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
166367a56275SMatthew G Knepley 
166467a56275SMatthew G Knepley     /*
166567a56275SMatthew G Knepley        Order of precedence:
166667a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
166767a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
166867a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
166967a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
167067a56275SMatthew G Knepley        5) Use a really basic converter.
167167a56275SMatthew G Knepley     */
167267a56275SMatthew G Knepley 
167367a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
167467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
167567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
167667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
167767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
167867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
167967a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
168067a56275SMatthew G Knepley     if (conv) goto foundconv;
168167a56275SMatthew G Knepley 
168267a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
168367a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
168467a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
168567a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
168667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
168767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
168867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
168967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
169067a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
169167a56275SMatthew G Knepley     if (conv) {
1692fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
169367a56275SMatthew G Knepley       goto foundconv;
169467a56275SMatthew G Knepley     }
169567a56275SMatthew G Knepley 
169667a56275SMatthew G Knepley #if 0
169767a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
169867a56275SMatthew G Knepley     conv = B->ops->convertfrom;
1699fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
170067a56275SMatthew G Knepley     if (conv) goto foundconv;
170167a56275SMatthew G Knepley 
170267a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
170367a56275SMatthew G Knepley     if (dm->ops->convert) {
170467a56275SMatthew G Knepley       conv = dm->ops->convert;
170567a56275SMatthew G Knepley     }
170667a56275SMatthew G Knepley     if (conv) goto foundconv;
170767a56275SMatthew G Knepley #endif
170867a56275SMatthew G Knepley 
170967a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
171067a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
171167a56275SMatthew G Knepley 
171267a56275SMatthew G Knepley     foundconv:
171367a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
171467a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
171567a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
171667a56275SMatthew G Knepley   }
171767a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
171867a56275SMatthew G Knepley   PetscFunctionReturn(0);
171967a56275SMatthew G Knepley }
1720264ace61SBarry Smith 
1721264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1722264ace61SBarry Smith 
1723264ace61SBarry Smith #undef __FUNCT__
1724264ace61SBarry Smith #define __FUNCT__ "DMRegister"
1725264ace61SBarry Smith /*@C
1726264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
1727264ace61SBarry Smith 
1728264ace61SBarry Smith   Level: advanced
1729264ace61SBarry Smith @*/
17307087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
1731264ace61SBarry Smith {
1732264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
1733264ace61SBarry Smith   PetscErrorCode ierr;
1734264ace61SBarry Smith 
1735264ace61SBarry Smith   PetscFunctionBegin;
1736264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
1737264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
1738264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
1739264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
1740264ace61SBarry Smith   PetscFunctionReturn(0);
1741264ace61SBarry Smith }
1742264ace61SBarry Smith 
1743264ace61SBarry Smith 
1744264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1745264ace61SBarry Smith #undef __FUNCT__
1746264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
1747264ace61SBarry Smith /*@C
1748264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
1749264ace61SBarry Smith 
1750264ace61SBarry Smith    Not Collective
1751264ace61SBarry Smith 
1752264ace61SBarry Smith    Level: advanced
1753264ace61SBarry Smith 
1754264ace61SBarry Smith .keywords: DM, register, destroy
1755264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
1756264ace61SBarry Smith @*/
17577087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
1758264ace61SBarry Smith {
1759264ace61SBarry Smith   PetscErrorCode ierr;
1760264ace61SBarry Smith 
1761264ace61SBarry Smith   PetscFunctionBegin;
1762264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
1763264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
1764264ace61SBarry Smith   PetscFunctionReturn(0);
1765264ace61SBarry Smith }
176623f975d1SBarry Smith 
176723f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
1768c6db04a5SJed Brown #include <mex.h>
176923f975d1SBarry Smith 
17703014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
177123f975d1SBarry Smith 
177223f975d1SBarry Smith #undef __FUNCT__
177323f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
177423f975d1SBarry Smith /*
177523f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
177623f975d1SBarry Smith                          DMSetFunctionMatlab().
177723f975d1SBarry Smith 
177823f975d1SBarry Smith    For linear problems x is null
177923f975d1SBarry Smith 
178023f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
178123f975d1SBarry Smith */
17827087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
178323f975d1SBarry Smith {
178423f975d1SBarry Smith   PetscErrorCode    ierr;
178523f975d1SBarry Smith   DMMatlabContext   *sctx;
178623f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
178723f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
178823f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
178923f975d1SBarry Smith 
179023f975d1SBarry Smith   PetscFunctionBegin;
179123f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
179223f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
179323f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
179423f975d1SBarry Smith 
179523f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
17961b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
179723f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
179823f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
17993014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
180023f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
180123f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
180223f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
180323f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
1804b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
180523f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
180623f975d1SBarry Smith   mxDestroyArray(prhs[0]);
180723f975d1SBarry Smith   mxDestroyArray(prhs[1]);
180823f975d1SBarry Smith   mxDestroyArray(prhs[2]);
180923f975d1SBarry Smith   mxDestroyArray(prhs[3]);
181023f975d1SBarry Smith   mxDestroyArray(plhs[0]);
181123f975d1SBarry Smith   PetscFunctionReturn(0);
181223f975d1SBarry Smith }
181323f975d1SBarry Smith 
181423f975d1SBarry Smith 
181523f975d1SBarry Smith #undef __FUNCT__
181623f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
181723f975d1SBarry Smith /*
181823f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
181923f975d1SBarry Smith 
182023f975d1SBarry Smith */
18217087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
182223f975d1SBarry Smith {
182323f975d1SBarry Smith   PetscErrorCode    ierr;
182423f975d1SBarry Smith   DMMatlabContext   *sctx;
182523f975d1SBarry Smith 
182623f975d1SBarry Smith   PetscFunctionBegin;
1827171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
182823f975d1SBarry Smith   /* currently sctx is memory bleed */
18291b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
18303014e516SBarry Smith   if (!sctx) {
183123f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
18323014e516SBarry Smith   }
183323f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
18341b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
183523f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
183623f975d1SBarry Smith   PetscFunctionReturn(0);
183723f975d1SBarry Smith }
18383014e516SBarry Smith 
18393014e516SBarry Smith #undef __FUNCT__
18403014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
18413014e516SBarry Smith /*
18423014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
18433014e516SBarry Smith                          DMSetJacobianMatlab().
18443014e516SBarry Smith 
18453014e516SBarry Smith    For linear problems x is null
18463014e516SBarry Smith 
18473014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
18483014e516SBarry Smith */
18497087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
18503014e516SBarry Smith {
18513014e516SBarry Smith   PetscErrorCode    ierr;
18523014e516SBarry Smith   DMMatlabContext   *sctx;
18533014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
18543014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
18553014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
18563014e516SBarry Smith 
18573014e516SBarry Smith   PetscFunctionBegin;
18583014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18593014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
18603014e516SBarry Smith 
1861e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
18621b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
18633014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
18643014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
18653014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
18663014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
18673014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
18683014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
18693014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
18703014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
18713014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
1872b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
1873c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
1874c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
18753014e516SBarry Smith   mxDestroyArray(prhs[0]);
18763014e516SBarry Smith   mxDestroyArray(prhs[1]);
18773014e516SBarry Smith   mxDestroyArray(prhs[2]);
18783014e516SBarry Smith   mxDestroyArray(prhs[3]);
18793014e516SBarry Smith   mxDestroyArray(prhs[4]);
18803014e516SBarry Smith   mxDestroyArray(plhs[0]);
18813014e516SBarry Smith   mxDestroyArray(plhs[1]);
18823014e516SBarry Smith   PetscFunctionReturn(0);
18833014e516SBarry Smith }
18843014e516SBarry Smith 
18853014e516SBarry Smith 
18863014e516SBarry Smith #undef __FUNCT__
18873014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
18883014e516SBarry Smith /*
18893014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
18903014e516SBarry Smith 
18913014e516SBarry Smith */
18927087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
18933014e516SBarry Smith {
18943014e516SBarry Smith   PetscErrorCode    ierr;
18953014e516SBarry Smith   DMMatlabContext   *sctx;
18963014e516SBarry Smith 
18973014e516SBarry Smith   PetscFunctionBegin;
1898171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18993014e516SBarry Smith   /* currently sctx is memory bleed */
19001b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
19013014e516SBarry Smith   if (!sctx) {
19023014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
19033014e516SBarry Smith   }
19043014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
19051b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
19063014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
19073014e516SBarry Smith   PetscFunctionReturn(0);
19083014e516SBarry Smith }
190923f975d1SBarry Smith #endif
1910b859378eSBarry Smith 
1911b859378eSBarry Smith #undef __FUNCT__
1912b859378eSBarry Smith #define __FUNCT__ "DMLoad"
1913b859378eSBarry Smith /*@C
1914b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
1915b859378eSBarry Smith   with DMView().
1916b859378eSBarry Smith 
1917b859378eSBarry Smith   Collective on PetscViewer
1918b859378eSBarry Smith 
1919b859378eSBarry Smith   Input Parameters:
1920b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
1921b859378eSBarry Smith            some related function before a call to DMLoad().
1922b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
1923b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
1924b859378eSBarry Smith 
1925b859378eSBarry Smith    Level: intermediate
1926b859378eSBarry Smith 
1927b859378eSBarry Smith   Notes:
1928b859378eSBarry Smith   Defaults to the DM DA.
1929b859378eSBarry Smith 
1930b859378eSBarry Smith   Notes for advanced users:
1931b859378eSBarry Smith   Most users should not need to know the details of the binary storage
1932b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
1933b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
1934b859378eSBarry Smith   format is
1935b859378eSBarry Smith .vb
1936b859378eSBarry Smith      has not yet been determined
1937b859378eSBarry Smith .ve
1938b859378eSBarry Smith 
1939b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
1940b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
1941b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
1942b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
1943b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
1944b859378eSBarry Smith 
1945b859378eSBarry Smith   Concepts: vector^loading from file
1946b859378eSBarry Smith 
1947b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
1948b859378eSBarry Smith @*/
1949b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
1950b859378eSBarry Smith {
1951b859378eSBarry Smith   PetscErrorCode ierr;
1952b859378eSBarry Smith 
1953b859378eSBarry Smith   PetscFunctionBegin;
1954b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
1955b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1956b859378eSBarry Smith 
1957b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
1958b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
1959b859378eSBarry Smith   }
1960b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
1961b859378eSBarry Smith   PetscFunctionReturn(0);
1962b859378eSBarry Smith }
1963b859378eSBarry Smith 
19647da65231SMatthew G Knepley /******************************** FEM Support **********************************/
19657da65231SMatthew G Knepley 
19667da65231SMatthew G Knepley #undef __FUNCT__
19677da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
19687da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
19691d47ebbbSSatish Balay   PetscInt       f;
19701b30c384SMatthew G Knepley   PetscErrorCode ierr;
19711b30c384SMatthew G Knepley 
19727da65231SMatthew G Knepley   PetscFunctionBegin;
19737da65231SMatthew G Knepley   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %d Element %s\n", c, name);CHKERRQ(ierr);
19741d47ebbbSSatish Balay   for(f = 0; f < len; ++f) {
19757da65231SMatthew G Knepley     PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", x[f]);
19767da65231SMatthew G Knepley   }
19777da65231SMatthew G Knepley   PetscFunctionReturn(0);
19787da65231SMatthew G Knepley }
19797da65231SMatthew G Knepley 
19807da65231SMatthew G Knepley #undef __FUNCT__
19817da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
19827da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
19831b30c384SMatthew G Knepley   PetscInt       f, g;
19847da65231SMatthew G Knepley   PetscErrorCode ierr;
19857da65231SMatthew G Knepley 
19867da65231SMatthew G Knepley   PetscFunctionBegin;
19877da65231SMatthew G Knepley   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %d Element %s\n", c, name);CHKERRQ(ierr);
19881d47ebbbSSatish Balay   for(f = 0; f < rows; ++f) {
19897da65231SMatthew G Knepley     PetscPrintf(PETSC_COMM_SELF, "  |");
19901d47ebbbSSatish Balay     for(g = 0; g < cols; ++g) {
19917da65231SMatthew G Knepley       PetscPrintf(PETSC_COMM_SELF, " % 9.5g", A[f*cols+g]);
19927da65231SMatthew G Knepley     }
19937da65231SMatthew G Knepley     PetscPrintf(PETSC_COMM_SELF, " |\n");
19947da65231SMatthew G Knepley   }
19957da65231SMatthew G Knepley   PetscFunctionReturn(0);
19967da65231SMatthew G Knepley }
1997