xref: /petsc/src/dm/interface/dm.c (revision 6bfea28cbd1c77ef039dbe1ceada5b47f7aade2f)
108da532bSDmitry Karpeev #include <petscsnes.h>
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;
135b17ce1afSJed 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 
166b17ce1afSJed Brown   /* Destroy the list of hooks */
167b17ce1afSJed Brown   for (link=(*dm)->coarsenhook; link; link=next) {
168b17ce1afSJed Brown     next = link->next;
169b17ce1afSJed Brown     ierr = PetscFree(link);CHKERRQ(ierr);
170b17ce1afSJed Brown   }
171b17ce1afSJed Brown   (*dm)->coarsenhook = PETSC_NULL;
172b17ce1afSJed 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 {
24567ad5babSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = 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);
25567ad5babSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_latex", "Output mesh in LaTeX TikZ format", "DMView", flg4, &flg4, PETSC_NULL);CHKERRQ(ierr);
256073dac72SJed 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);
257f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
258f9ba7244SBarry Smith     if (flg) {
259f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
260f9ba7244SBarry Smith     }
261f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_mat_type","Matrix type","MatSetType",MatList,typeName,typeName,sizeof typeName,&flg);CHKERRQ(ierr);
262073dac72SJed Brown     if (flg) {
263073dac72SJed Brown       ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
264f9ba7244SBarry Smith       ierr = PetscStrallocpy(typeName,&dm->mattype);CHKERRQ(ierr);
265073dac72SJed Brown     }
2661b89239cSHong 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);
267f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
268f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
269f9ba7244SBarry Smith     }
270f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
271f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
27282fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
27382fcb398SMatthew G Knepley   if (flg1) {
27482fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
27582fcb398SMatthew G Knepley   }
276c4721b0eSMatthew G Knepley   if (flg2) {
277c4721b0eSMatthew G Knepley     PetscViewer viewer;
278c4721b0eSMatthew G Knepley 
279c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
280c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
281c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
282c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
283c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
284c4721b0eSMatthew G Knepley   }
285c4721b0eSMatthew G Knepley   if (flg3) {
286c4721b0eSMatthew G Knepley     PetscViewer viewer;
287c4721b0eSMatthew G Knepley 
288c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
289c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
290c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
291c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
292c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
293c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
294c4721b0eSMatthew G Knepley   }
29567ad5babSMatthew G Knepley   if (flg4) {
29667ad5babSMatthew G Knepley     PetscViewer viewer;
29767ad5babSMatthew G Knepley 
29867ad5babSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
29967ad5babSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
30067ad5babSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_LATEX);CHKERRQ(ierr);
30167ad5babSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.tex");CHKERRQ(ierr);
30267ad5babSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
30367ad5babSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
30467ad5babSMatthew G Knepley   }
305d7bf68aeSBarry Smith   PetscFunctionReturn(0);
306d7bf68aeSBarry Smith }
307d7bf68aeSBarry Smith 
308d7bf68aeSBarry Smith #undef __FUNCT__
30947c6ae99SBarry Smith #define __FUNCT__ "DMView"
310fc9bc008SSatish Balay /*@C
311aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
31247c6ae99SBarry Smith 
31347c6ae99SBarry Smith     Collective on DM
31447c6ae99SBarry Smith 
31547c6ae99SBarry Smith     Input Parameter:
31647c6ae99SBarry Smith +   dm - the DM object to view
31747c6ae99SBarry Smith -   v - the viewer
31847c6ae99SBarry Smith 
31947c6ae99SBarry Smith     Level: developer
32047c6ae99SBarry Smith 
321e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
32247c6ae99SBarry Smith 
32347c6ae99SBarry Smith @*/
3247087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
32547c6ae99SBarry Smith {
32647c6ae99SBarry Smith   PetscErrorCode ierr;
32747c6ae99SBarry Smith 
32847c6ae99SBarry Smith   PetscFunctionBegin;
329171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3303014e516SBarry Smith  if (!v) {
3313014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
3323014e516SBarry Smith   }
3330c010503SBarry Smith   if (dm->ops->view) {
3340c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
33547c6ae99SBarry Smith   }
33647c6ae99SBarry Smith   PetscFunctionReturn(0);
33747c6ae99SBarry Smith }
33847c6ae99SBarry Smith 
33947c6ae99SBarry Smith #undef __FUNCT__
34047c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
34147c6ae99SBarry Smith /*@
342aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
34347c6ae99SBarry Smith 
34447c6ae99SBarry Smith     Collective on DM
34547c6ae99SBarry Smith 
34647c6ae99SBarry Smith     Input Parameter:
34747c6ae99SBarry Smith .   dm - the DM object
34847c6ae99SBarry Smith 
34947c6ae99SBarry Smith     Output Parameter:
35047c6ae99SBarry Smith .   vec - the global vector
35147c6ae99SBarry Smith 
352073dac72SJed Brown     Level: beginner
35347c6ae99SBarry Smith 
354e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
35547c6ae99SBarry Smith 
35647c6ae99SBarry Smith @*/
3577087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
35847c6ae99SBarry Smith {
35947c6ae99SBarry Smith   PetscErrorCode ierr;
36047c6ae99SBarry Smith 
36147c6ae99SBarry Smith   PetscFunctionBegin;
362171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
36347c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
36447c6ae99SBarry Smith   PetscFunctionReturn(0);
36547c6ae99SBarry Smith }
36647c6ae99SBarry Smith 
36747c6ae99SBarry Smith #undef __FUNCT__
36847c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
36947c6ae99SBarry Smith /*@
370aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
37147c6ae99SBarry Smith 
37247c6ae99SBarry Smith     Not Collective
37347c6ae99SBarry Smith 
37447c6ae99SBarry Smith     Input Parameter:
37547c6ae99SBarry Smith .   dm - the DM object
37647c6ae99SBarry Smith 
37747c6ae99SBarry Smith     Output Parameter:
37847c6ae99SBarry Smith .   vec - the local vector
37947c6ae99SBarry Smith 
380073dac72SJed Brown     Level: beginner
38147c6ae99SBarry Smith 
382e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
38347c6ae99SBarry Smith 
38447c6ae99SBarry Smith @*/
3857087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
38647c6ae99SBarry Smith {
38747c6ae99SBarry Smith   PetscErrorCode ierr;
38847c6ae99SBarry Smith 
38947c6ae99SBarry Smith   PetscFunctionBegin;
390171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39147c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
39247c6ae99SBarry Smith   PetscFunctionReturn(0);
39347c6ae99SBarry Smith }
39447c6ae99SBarry Smith 
39547c6ae99SBarry Smith #undef __FUNCT__
3961411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
3971411c6eeSJed Brown /*@
3981411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
3991411c6eeSJed Brown 
4001411c6eeSJed Brown    Collective on DM
4011411c6eeSJed Brown 
4021411c6eeSJed Brown    Input Parameter:
4031411c6eeSJed Brown .  dm - the DM that provides the mapping
4041411c6eeSJed Brown 
4051411c6eeSJed Brown    Output Parameter:
4061411c6eeSJed Brown .  ltog - the mapping
4071411c6eeSJed Brown 
4081411c6eeSJed Brown    Level: intermediate
4091411c6eeSJed Brown 
4101411c6eeSJed Brown    Notes:
4111411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
4121411c6eeSJed Brown    MatSetLocalToGlobalMapping().
4131411c6eeSJed Brown 
4141411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
4151411c6eeSJed Brown @*/
4167087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
4171411c6eeSJed Brown {
4181411c6eeSJed Brown   PetscErrorCode ierr;
4191411c6eeSJed Brown 
4201411c6eeSJed Brown   PetscFunctionBegin;
4211411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4221411c6eeSJed Brown   PetscValidPointer(ltog,2);
4231411c6eeSJed Brown   if (!dm->ltogmap) {
4241411c6eeSJed Brown     if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
4251411c6eeSJed Brown     ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
4261411c6eeSJed Brown   }
4271411c6eeSJed Brown   *ltog = dm->ltogmap;
4281411c6eeSJed Brown   PetscFunctionReturn(0);
4291411c6eeSJed Brown }
4301411c6eeSJed Brown 
4311411c6eeSJed Brown #undef __FUNCT__
4321411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
4331411c6eeSJed Brown /*@
4341411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
4351411c6eeSJed Brown 
4361411c6eeSJed Brown    Collective on DM
4371411c6eeSJed Brown 
4381411c6eeSJed Brown    Input Parameter:
4391411c6eeSJed Brown .  da - the distributed array that provides the mapping
4401411c6eeSJed Brown 
4411411c6eeSJed Brown    Output Parameter:
4421411c6eeSJed Brown .  ltog - the block mapping
4431411c6eeSJed Brown 
4441411c6eeSJed Brown    Level: intermediate
4451411c6eeSJed Brown 
4461411c6eeSJed Brown    Notes:
4471411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
4481411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
4491411c6eeSJed Brown 
4501411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
4511411c6eeSJed Brown @*/
4527087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
4531411c6eeSJed Brown {
4541411c6eeSJed Brown   PetscErrorCode ierr;
4551411c6eeSJed Brown 
4561411c6eeSJed Brown   PetscFunctionBegin;
4571411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4581411c6eeSJed Brown   PetscValidPointer(ltog,2);
4591411c6eeSJed Brown   if (!dm->ltogmapb) {
4601411c6eeSJed Brown     PetscInt bs;
4611411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
4621411c6eeSJed Brown     if (bs > 1) {
4631411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
4641411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
4651411c6eeSJed Brown     } else {
4661411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
4671411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
4681411c6eeSJed Brown     }
4691411c6eeSJed Brown   }
4701411c6eeSJed Brown   *ltog = dm->ltogmapb;
4711411c6eeSJed Brown   PetscFunctionReturn(0);
4721411c6eeSJed Brown }
4731411c6eeSJed Brown 
4741411c6eeSJed Brown #undef __FUNCT__
4751411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
4761411c6eeSJed Brown /*@
4771411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
4781411c6eeSJed Brown 
4791411c6eeSJed Brown    Not Collective
4801411c6eeSJed Brown 
4811411c6eeSJed Brown    Input Parameter:
4821411c6eeSJed Brown .  dm - the DM with block structure
4831411c6eeSJed Brown 
4841411c6eeSJed Brown    Output Parameter:
4851411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
4861411c6eeSJed Brown 
4871411c6eeSJed Brown    Level: intermediate
4881411c6eeSJed Brown 
4891411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
4901411c6eeSJed Brown @*/
4917087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
4921411c6eeSJed Brown {
4931411c6eeSJed Brown   PetscFunctionBegin;
4941411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4951411c6eeSJed Brown   PetscValidPointer(bs,2);
4961411c6eeSJed 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");
4971411c6eeSJed Brown   *bs = dm->bs;
4981411c6eeSJed Brown   PetscFunctionReturn(0);
4991411c6eeSJed Brown }
5001411c6eeSJed Brown 
5011411c6eeSJed Brown #undef __FUNCT__
502e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
50347c6ae99SBarry Smith /*@
504e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
50547c6ae99SBarry Smith 
50647c6ae99SBarry Smith     Collective on DM
50747c6ae99SBarry Smith 
50847c6ae99SBarry Smith     Input Parameter:
50947c6ae99SBarry Smith +   dm1 - the DM object
51047c6ae99SBarry Smith -   dm2 - the second, finer DM object
51147c6ae99SBarry Smith 
51247c6ae99SBarry Smith     Output Parameter:
51347c6ae99SBarry Smith +  mat - the interpolation
51447c6ae99SBarry Smith -  vec - the scaling (optional)
51547c6ae99SBarry Smith 
51647c6ae99SBarry Smith     Level: developer
51747c6ae99SBarry Smith 
51885afcc9aSBarry 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
51985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
520d52bd9f3SBarry Smith 
521d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
522d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
52385afcc9aSBarry Smith 
52485afcc9aSBarry Smith 
525e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
52647c6ae99SBarry Smith 
52747c6ae99SBarry Smith @*/
528e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
52947c6ae99SBarry Smith {
53047c6ae99SBarry Smith   PetscErrorCode ierr;
53147c6ae99SBarry Smith 
53247c6ae99SBarry Smith   PetscFunctionBegin;
533171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
534171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
53525296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
53647c6ae99SBarry Smith   PetscFunctionReturn(0);
53747c6ae99SBarry Smith }
53847c6ae99SBarry Smith 
53947c6ae99SBarry Smith #undef __FUNCT__
540e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
54147c6ae99SBarry Smith /*@
542e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
54347c6ae99SBarry Smith 
54447c6ae99SBarry Smith     Collective on DM
54547c6ae99SBarry Smith 
54647c6ae99SBarry Smith     Input Parameter:
54747c6ae99SBarry Smith +   dm1 - the DM object
54847c6ae99SBarry Smith -   dm2 - the second, finer DM object
54947c6ae99SBarry Smith 
55047c6ae99SBarry Smith     Output Parameter:
55147c6ae99SBarry Smith .   ctx - the injection
55247c6ae99SBarry Smith 
55347c6ae99SBarry Smith     Level: developer
55447c6ae99SBarry Smith 
55585afcc9aSBarry 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
55685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
55785afcc9aSBarry Smith 
558e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
55947c6ae99SBarry Smith 
56047c6ae99SBarry Smith @*/
561e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
56247c6ae99SBarry Smith {
56347c6ae99SBarry Smith   PetscErrorCode ierr;
56447c6ae99SBarry Smith 
56547c6ae99SBarry Smith   PetscFunctionBegin;
566171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
567171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
56847c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
56947c6ae99SBarry Smith   PetscFunctionReturn(0);
57047c6ae99SBarry Smith }
57147c6ae99SBarry Smith 
57247c6ae99SBarry Smith #undef __FUNCT__
573e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
574d1e2c406SBarry Smith /*@C
575e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
57647c6ae99SBarry Smith 
57747c6ae99SBarry Smith     Collective on DM
57847c6ae99SBarry Smith 
57947c6ae99SBarry Smith     Input Parameter:
58047c6ae99SBarry Smith +   dm - the DM object
58147c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
58247c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
58347c6ae99SBarry Smith 
58447c6ae99SBarry Smith     Output Parameter:
58547c6ae99SBarry Smith .   coloring - the coloring
58647c6ae99SBarry Smith 
58747c6ae99SBarry Smith     Level: developer
58847c6ae99SBarry Smith 
589e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
59047c6ae99SBarry Smith 
591aab9d709SJed Brown @*/
592e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
59347c6ae99SBarry Smith {
59447c6ae99SBarry Smith   PetscErrorCode ierr;
59547c6ae99SBarry Smith 
59647c6ae99SBarry Smith   PetscFunctionBegin;
597171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59847c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
59947c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
60047c6ae99SBarry Smith   PetscFunctionReturn(0);
60147c6ae99SBarry Smith }
60247c6ae99SBarry Smith 
60347c6ae99SBarry Smith #undef __FUNCT__
604950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
60547c6ae99SBarry Smith /*@C
606950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
60747c6ae99SBarry Smith 
60847c6ae99SBarry Smith     Collective on DM
60947c6ae99SBarry Smith 
61047c6ae99SBarry Smith     Input Parameter:
61147c6ae99SBarry Smith +   dm - the DM object
61247c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
61394013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
61447c6ae99SBarry Smith 
61547c6ae99SBarry Smith     Output Parameter:
61647c6ae99SBarry Smith .   mat - the empty Jacobian
61747c6ae99SBarry Smith 
618073dac72SJed Brown     Level: beginner
61947c6ae99SBarry Smith 
62094013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
62194013140SBarry Smith        do not need to do it yourself.
62294013140SBarry Smith 
62394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
624aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
62594013140SBarry Smith 
62694013140SBarry 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
62794013140SBarry Smith        internally by PETSc.
62894013140SBarry Smith 
62994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
630aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
63194013140SBarry Smith 
632e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
63347c6ae99SBarry Smith 
634aab9d709SJed Brown @*/
635950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
63647c6ae99SBarry Smith {
63747c6ae99SBarry Smith   PetscErrorCode ierr;
63847c6ae99SBarry Smith 
63947c6ae99SBarry Smith   PetscFunctionBegin;
640171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
641235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
642235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
643235683edSBarry Smith #endif
644c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
645c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
646073dac72SJed Brown   if (dm->mattype) {
64725296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
648073dac72SJed Brown   } else {
64925296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
650c7b7c8a4SJed Brown   }
65147c6ae99SBarry Smith   PetscFunctionReturn(0);
65247c6ae99SBarry Smith }
65347c6ae99SBarry Smith 
65447c6ae99SBarry Smith #undef __FUNCT__
655732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
656732e2eb9SMatthew G Knepley /*@
657950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
658732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
659732e2eb9SMatthew G Knepley 
660732e2eb9SMatthew G Knepley   Logically Collective on DMDA
661732e2eb9SMatthew G Knepley 
662732e2eb9SMatthew G Knepley   Input Parameter:
663732e2eb9SMatthew G Knepley + dm - the DM
664732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
665732e2eb9SMatthew G Knepley 
666732e2eb9SMatthew G Knepley   Level: developer
667950540a4SJed Brown .seealso DMCreateMatrix()
668732e2eb9SMatthew G Knepley @*/
669732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
670732e2eb9SMatthew G Knepley {
671732e2eb9SMatthew G Knepley   PetscFunctionBegin;
672732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
673732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
674732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
675732e2eb9SMatthew G Knepley }
676732e2eb9SMatthew G Knepley 
677732e2eb9SMatthew G Knepley #undef __FUNCT__
678a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
679a89ea682SMatthew G Knepley /*@C
680a89ea682SMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size
681a89ea682SMatthew G Knepley 
682a89ea682SMatthew G Knepley   Not Collective
683a89ea682SMatthew G Knepley 
684a89ea682SMatthew G Knepley   Input Parameters:
685a89ea682SMatthew G Knepley + dm - the DM object
686a89ea682SMatthew G Knepley - size - The minium size
687a89ea682SMatthew G Knepley 
688a89ea682SMatthew G Knepley   Output Parameter:
689a89ea682SMatthew G Knepley . array - the work array
690a89ea682SMatthew G Knepley 
691a89ea682SMatthew G Knepley   Level: developer
692a89ea682SMatthew G Knepley 
693a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
694a89ea682SMatthew G Knepley @*/
695a89ea682SMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt size,PetscScalar **array)
696a89ea682SMatthew G Knepley {
697a89ea682SMatthew G Knepley   PetscErrorCode ierr;
698a89ea682SMatthew G Knepley 
699a89ea682SMatthew G Knepley   PetscFunctionBegin;
700a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
701a89ea682SMatthew G Knepley   PetscValidPointer(array,3);
702a89ea682SMatthew G Knepley   if (size > dm->workSize) {
703a89ea682SMatthew G Knepley     dm->workSize = size;
704a89ea682SMatthew G Knepley     ierr = PetscFree(dm->workArray);CHKERRQ(ierr);
705a89ea682SMatthew G Knepley     ierr = PetscMalloc(dm->workSize * sizeof(PetscScalar), &dm->workArray);CHKERRQ(ierr);
706a89ea682SMatthew G Knepley   }
707a89ea682SMatthew G Knepley   *array = dm->workArray;
708a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
709a89ea682SMatthew G Knepley }
710a89ea682SMatthew G Knepley 
7114d343eeaSMatthew G Knepley #undef __FUNCT__
7124d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
7134f3b5142SJed Brown /*@C
7144d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
7154d343eeaSMatthew G Knepley 
7164d343eeaSMatthew G Knepley   Not collective
7174d343eeaSMatthew G Knepley 
7184d343eeaSMatthew G Knepley   Input Parameter:
7194d343eeaSMatthew G Knepley . dm - the DM object
7204d343eeaSMatthew G Knepley 
7214d343eeaSMatthew G Knepley   Output Parameters:
72221c9b008SJed Brown + numFields - The number of fields (or PETSC_NULL if not requested)
72321c9b008SJed Brown . names     - The name for each field (or PETSC_NULL if not requested)
72421c9b008SJed Brown - fields    - The global indices for each field (or PETSC_NULL if not requested)
7254d343eeaSMatthew G Knepley 
7264d343eeaSMatthew G Knepley   Level: intermediate
7274d343eeaSMatthew G Knepley 
72821c9b008SJed Brown   Notes:
72921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
73021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
73121c9b008SJed Brown   PetscFree().
73221c9b008SJed Brown 
7334d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
7344d343eeaSMatthew G Knepley @*/
73521c9b008SJed Brown PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***names, IS **fields)
7364d343eeaSMatthew G Knepley {
7374d343eeaSMatthew G Knepley   PetscErrorCode ierr;
7384d343eeaSMatthew G Knepley 
7394d343eeaSMatthew G Knepley   PetscFunctionBegin;
7404d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7414d343eeaSMatthew G Knepley   if (numFields) {PetscValidPointer(numFields,2);}
7424d343eeaSMatthew G Knepley   if (names) {PetscValidPointer(names,3);}
7434d343eeaSMatthew G Knepley   if (fields) {PetscValidPointer(fields,4);}
7444d343eeaSMatthew G Knepley   ierr = (*dm->ops->createfieldis)(dm, numFields, names, fields);CHKERRQ(ierr);
7454d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
7464d343eeaSMatthew G Knepley }
7474d343eeaSMatthew G Knepley 
7485fe1f584SPeter Brune 
749a89ea682SMatthew G Knepley #undef __FUNCT__
75047c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
75147c6ae99SBarry Smith /*@
75247c6ae99SBarry Smith     DMRefine - Refines a DM object
75347c6ae99SBarry Smith 
75447c6ae99SBarry Smith     Collective on DM
75547c6ae99SBarry Smith 
75647c6ae99SBarry Smith     Input Parameter:
75747c6ae99SBarry Smith +   dm - the DM object
75847c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
75947c6ae99SBarry Smith 
76047c6ae99SBarry Smith     Output Parameter:
76147c6ae99SBarry Smith .   dmf - the refined DM
76247c6ae99SBarry Smith 
76347c6ae99SBarry Smith     Level: developer
76447c6ae99SBarry Smith 
765e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
76647c6ae99SBarry Smith 
76747c6ae99SBarry Smith @*/
7687087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
76947c6ae99SBarry Smith {
77047c6ae99SBarry Smith   PetscErrorCode ierr;
77147c6ae99SBarry Smith 
77247c6ae99SBarry Smith   PetscFunctionBegin;
773732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77447c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
7754057135bSMatthew G Knepley   if (*dmf) {
77643842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
777644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
778644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
779644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
780644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
781644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
782644e2e5bSBarry Smith     }
7838cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
784644e2e5bSBarry Smith     (*dmf)->ctx     = dm->ctx;
785656b349aSBarry Smith     (*dmf)->levelup = dm->levelup + 1;
7864057135bSMatthew G Knepley   }
78747c6ae99SBarry Smith   PetscFunctionReturn(0);
78847c6ae99SBarry Smith }
78947c6ae99SBarry Smith 
79047c6ae99SBarry Smith #undef __FUNCT__
791eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
792eb3f98d2SBarry Smith /*@
793eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
794eb3f98d2SBarry Smith 
795eb3f98d2SBarry Smith     Not Collective
796eb3f98d2SBarry Smith 
797eb3f98d2SBarry Smith     Input Parameter:
798eb3f98d2SBarry Smith .   dm - the DM object
799eb3f98d2SBarry Smith 
800eb3f98d2SBarry Smith     Output Parameter:
801eb3f98d2SBarry Smith .   level - number of refinements
802eb3f98d2SBarry Smith 
803eb3f98d2SBarry Smith     Level: developer
804eb3f98d2SBarry Smith 
8056a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
806eb3f98d2SBarry Smith 
807eb3f98d2SBarry Smith @*/
808eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
809eb3f98d2SBarry Smith {
810eb3f98d2SBarry Smith   PetscFunctionBegin;
811eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
812eb3f98d2SBarry Smith   *level = dm->levelup;
813eb3f98d2SBarry Smith   PetscFunctionReturn(0);
814eb3f98d2SBarry Smith }
815eb3f98d2SBarry Smith 
816eb3f98d2SBarry Smith #undef __FUNCT__
81747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
81847c6ae99SBarry Smith /*@
81947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
82047c6ae99SBarry Smith 
82147c6ae99SBarry Smith     Neighbor-wise Collective on DM
82247c6ae99SBarry Smith 
82347c6ae99SBarry Smith     Input Parameters:
82447c6ae99SBarry Smith +   dm - the DM object
82547c6ae99SBarry Smith .   g - the global vector
82647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
82747c6ae99SBarry Smith -   l - the local vector
82847c6ae99SBarry Smith 
82947c6ae99SBarry Smith 
83047c6ae99SBarry Smith     Level: beginner
83147c6ae99SBarry Smith 
832e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
83347c6ae99SBarry Smith 
83447c6ae99SBarry Smith @*/
8357087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
83647c6ae99SBarry Smith {
83747c6ae99SBarry Smith   PetscErrorCode ierr;
83847c6ae99SBarry Smith 
83947c6ae99SBarry Smith   PetscFunctionBegin;
840171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
841843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
84247c6ae99SBarry Smith   PetscFunctionReturn(0);
84347c6ae99SBarry Smith }
84447c6ae99SBarry Smith 
84547c6ae99SBarry Smith #undef __FUNCT__
84647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
84747c6ae99SBarry Smith /*@
84847c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
84947c6ae99SBarry Smith 
85047c6ae99SBarry Smith     Neighbor-wise Collective on DM
85147c6ae99SBarry Smith 
85247c6ae99SBarry Smith     Input Parameters:
85347c6ae99SBarry Smith +   dm - the DM object
85447c6ae99SBarry Smith .   g - the global vector
85547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
85647c6ae99SBarry Smith -   l - the local vector
85747c6ae99SBarry Smith 
85847c6ae99SBarry Smith 
85947c6ae99SBarry Smith     Level: beginner
86047c6ae99SBarry Smith 
861e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith @*/
8647087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
86547c6ae99SBarry Smith {
86647c6ae99SBarry Smith   PetscErrorCode ierr;
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith   PetscFunctionBegin;
869171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
870843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
87147c6ae99SBarry Smith   PetscFunctionReturn(0);
87247c6ae99SBarry Smith }
87347c6ae99SBarry Smith 
87447c6ae99SBarry Smith #undef __FUNCT__
8759a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
87647c6ae99SBarry Smith /*@
8779a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
8789a42bb27SBarry Smith 
8799a42bb27SBarry Smith     Neighbor-wise Collective on DM
8809a42bb27SBarry Smith 
8819a42bb27SBarry Smith     Input Parameters:
8829a42bb27SBarry Smith +   dm - the DM object
883f6813fd5SJed Brown .   l - the local vector
8849a42bb27SBarry 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
8859a42bb27SBarry Smith            base point.
886f6813fd5SJed Brown - - the global vector
8879a42bb27SBarry Smith 
8889a42bb27SBarry 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
8899a42bb27SBarry 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
8909a42bb27SBarry Smith            global array to the final global array with VecAXPY().
8919a42bb27SBarry Smith 
8929a42bb27SBarry Smith     Level: beginner
8939a42bb27SBarry Smith 
894e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
8959a42bb27SBarry Smith 
8969a42bb27SBarry Smith @*/
8977087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
8989a42bb27SBarry Smith {
8999a42bb27SBarry Smith   PetscErrorCode ierr;
9009a42bb27SBarry Smith 
9019a42bb27SBarry Smith   PetscFunctionBegin;
902171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
903843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
9049a42bb27SBarry Smith   PetscFunctionReturn(0);
9059a42bb27SBarry Smith }
9069a42bb27SBarry Smith 
9079a42bb27SBarry Smith #undef __FUNCT__
9089a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
9099a42bb27SBarry Smith /*@
9109a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith     Neighbor-wise Collective on DM
91347c6ae99SBarry Smith 
91447c6ae99SBarry Smith     Input Parameters:
91547c6ae99SBarry Smith +   dm - the DM object
916f6813fd5SJed Brown .   l - the local vector
91747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
918f6813fd5SJed Brown -   g - the global vector
91947c6ae99SBarry Smith 
92047c6ae99SBarry Smith 
92147c6ae99SBarry Smith     Level: beginner
92247c6ae99SBarry Smith 
923e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
92447c6ae99SBarry Smith 
92547c6ae99SBarry Smith @*/
9267087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
92747c6ae99SBarry Smith {
92847c6ae99SBarry Smith   PetscErrorCode ierr;
92947c6ae99SBarry Smith 
93047c6ae99SBarry Smith   PetscFunctionBegin;
931171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
932843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
93347c6ae99SBarry Smith   PetscFunctionReturn(0);
93447c6ae99SBarry Smith }
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith #undef __FUNCT__
93747c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
93847c6ae99SBarry Smith /*@
93947c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
94047c6ae99SBarry Smith 
94147c6ae99SBarry Smith     Collective on DM
94247c6ae99SBarry Smith 
94347c6ae99SBarry Smith     Input Parameter:
94447c6ae99SBarry Smith +   dm - the DM object
94547c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
94647c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
94747c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
94847c6ae99SBarry Smith 
94947c6ae99SBarry Smith     Level: developer
95047c6ae99SBarry Smith 
951e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
95247c6ae99SBarry Smith          DMSetFunction()
95347c6ae99SBarry Smith 
95447c6ae99SBarry Smith @*/
9557087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
95647c6ae99SBarry Smith {
95747c6ae99SBarry Smith   PetscErrorCode ierr;
958171400e9SBarry Smith 
95947c6ae99SBarry Smith   PetscFunctionBegin;
960171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
96147c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
96247c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
96347c6ae99SBarry Smith   if (A != B) {
96447c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
96547c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
96647c6ae99SBarry Smith   }
96747c6ae99SBarry Smith   PetscFunctionReturn(0);
96847c6ae99SBarry Smith }
96947c6ae99SBarry Smith 
97047c6ae99SBarry Smith #undef __FUNCT__
97147c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
97247c6ae99SBarry Smith /*@
97347c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
97447c6ae99SBarry Smith 
97547c6ae99SBarry Smith     Collective on DM
97647c6ae99SBarry Smith 
97747c6ae99SBarry Smith     Input Parameter:
97847c6ae99SBarry Smith +   dm - the DM object
97947c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
98047c6ae99SBarry Smith 
98147c6ae99SBarry Smith     Output Parameter:
98247c6ae99SBarry Smith .   dmc - the coarsened DM
98347c6ae99SBarry Smith 
98447c6ae99SBarry Smith     Level: developer
98547c6ae99SBarry Smith 
986e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
98747c6ae99SBarry Smith 
98847c6ae99SBarry Smith @*/
9897087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
99047c6ae99SBarry Smith {
99147c6ae99SBarry Smith   PetscErrorCode ierr;
992b17ce1afSJed Brown   DMCoarsenHookLink link;
99347c6ae99SBarry Smith 
99447c6ae99SBarry Smith   PetscFunctionBegin;
995171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
99647c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
99743842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
99847c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
99947c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
100047c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
100147c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
100247c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
100347c6ae99SBarry Smith   }
10048cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1005644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
1006656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1007b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1008b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1009b17ce1afSJed Brown   }
1010b17ce1afSJed Brown   PetscFunctionReturn(0);
1011b17ce1afSJed Brown }
1012b17ce1afSJed Brown 
1013b17ce1afSJed Brown #undef __FUNCT__
1014b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1015b17ce1afSJed Brown /*@
1016b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1017b17ce1afSJed Brown 
1018b17ce1afSJed Brown    Logically Collective
1019b17ce1afSJed Brown 
1020b17ce1afSJed Brown    Input Arguments:
1021b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1022b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1023b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1024b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1025b17ce1afSJed Brown 
1026b17ce1afSJed Brown    Calling sequence of coarsenhook:
1027b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1028b17ce1afSJed Brown 
1029b17ce1afSJed Brown +  fine - fine level DM
1030b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1031b17ce1afSJed Brown -  ctx - optional user-defined function context
1032b17ce1afSJed Brown 
1033b17ce1afSJed Brown    Calling sequence for restricthook:
1034b17ce1afSJed Brown $    restricthook(DM fine,Mat mrestrict,Mat inject,DM coarse,void *ctx)
1035b17ce1afSJed Brown 
1036b17ce1afSJed Brown +  fine - fine level DM
1037b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1038b17ce1afSJed Brown .  inject - matrix restricting by applying the transpose of injection
1039b17ce1afSJed Brown .  coarse - coarse level DM to update
1040b17ce1afSJed Brown -  ctx - optional user-defined function context
1041b17ce1afSJed Brown 
1042b17ce1afSJed Brown    Level: advanced
1043b17ce1afSJed Brown 
1044b17ce1afSJed Brown    Notes:
1045b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1046b17ce1afSJed Brown 
1047b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1048b17ce1afSJed Brown 
1049b17ce1afSJed Brown    The
1050b17ce1afSJed Brown 
1051b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1052b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1053b17ce1afSJed Brown 
1054b17ce1afSJed Brown .seealso: SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1055b17ce1afSJed Brown @*/
1056b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1057b17ce1afSJed Brown {
1058b17ce1afSJed Brown   PetscErrorCode ierr;
1059b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1060b17ce1afSJed Brown 
1061b17ce1afSJed Brown   PetscFunctionBegin;
1062b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
1063*6bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1064b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1065b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1066b17ce1afSJed Brown   link->restricthook = restricthook;
1067b17ce1afSJed Brown   link->ctx = ctx;
10686cab3a1bSJed Brown   link->next = PETSC_NULL;
1069b17ce1afSJed Brown   *p = link;
1070b17ce1afSJed Brown   PetscFunctionReturn(0);
1071b17ce1afSJed Brown }
1072b17ce1afSJed Brown 
1073b17ce1afSJed Brown #undef __FUNCT__
1074b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1075b17ce1afSJed Brown /*@
1076b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1077b17ce1afSJed Brown 
1078b17ce1afSJed Brown    Collective if any hooks are
1079b17ce1afSJed Brown 
1080b17ce1afSJed Brown    Input Arguments:
1081b17ce1afSJed Brown +  fine - finer DM to use as a base
1082b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1083b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1084b17ce1afSJed Brown -  coarse - coarer DM to update
1085b17ce1afSJed Brown 
1086b17ce1afSJed Brown    Level: developer
1087b17ce1afSJed Brown 
1088b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1089b17ce1afSJed Brown @*/
1090b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1091b17ce1afSJed Brown {
1092b17ce1afSJed Brown   PetscErrorCode ierr;
1093b17ce1afSJed Brown   DMCoarsenHookLink link;
1094b17ce1afSJed Brown 
1095b17ce1afSJed Brown   PetscFunctionBegin;
1096b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1097b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1098b17ce1afSJed Brown   }
109947c6ae99SBarry Smith   PetscFunctionReturn(0);
110047c6ae99SBarry Smith }
110147c6ae99SBarry Smith 
110247c6ae99SBarry Smith #undef __FUNCT__
11035fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
11045fe1f584SPeter Brune /*@
11056a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
11065fe1f584SPeter Brune 
11075fe1f584SPeter Brune     Not Collective
11085fe1f584SPeter Brune 
11095fe1f584SPeter Brune     Input Parameter:
11105fe1f584SPeter Brune .   dm - the DM object
11115fe1f584SPeter Brune 
11125fe1f584SPeter Brune     Output Parameter:
11136a7d9d85SPeter Brune .   level - number of coarsenings
11145fe1f584SPeter Brune 
11155fe1f584SPeter Brune     Level: developer
11165fe1f584SPeter Brune 
11176a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
11185fe1f584SPeter Brune 
11195fe1f584SPeter Brune @*/
11205fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
11215fe1f584SPeter Brune {
11225fe1f584SPeter Brune   PetscFunctionBegin;
11235fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11245fe1f584SPeter Brune   *level = dm->leveldown;
11255fe1f584SPeter Brune   PetscFunctionReturn(0);
11265fe1f584SPeter Brune }
11275fe1f584SPeter Brune 
11285fe1f584SPeter Brune 
11295fe1f584SPeter Brune 
11305fe1f584SPeter Brune #undef __FUNCT__
113147c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
113247c6ae99SBarry Smith /*@C
113347c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
113447c6ae99SBarry Smith 
113547c6ae99SBarry Smith     Collective on DM
113647c6ae99SBarry Smith 
113747c6ae99SBarry Smith     Input Parameter:
113847c6ae99SBarry Smith +   dm - the DM object
113947c6ae99SBarry Smith -   nlevels - the number of levels of refinement
114047c6ae99SBarry Smith 
114147c6ae99SBarry Smith     Output Parameter:
114247c6ae99SBarry Smith .   dmf - the refined DM hierarchy
114347c6ae99SBarry Smith 
114447c6ae99SBarry Smith     Level: developer
114547c6ae99SBarry Smith 
1146e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
114747c6ae99SBarry Smith 
114847c6ae99SBarry Smith @*/
11497087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
115047c6ae99SBarry Smith {
115147c6ae99SBarry Smith   PetscErrorCode ierr;
115247c6ae99SBarry Smith 
115347c6ae99SBarry Smith   PetscFunctionBegin;
1154171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
115547c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
115647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
115747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
115847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
115947c6ae99SBarry Smith   } else if (dm->ops->refine) {
116047c6ae99SBarry Smith     PetscInt i;
116147c6ae99SBarry Smith 
116247c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
116347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
116447c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
116547c6ae99SBarry Smith     }
116647c6ae99SBarry Smith   } else {
116747c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
116847c6ae99SBarry Smith   }
116947c6ae99SBarry Smith   PetscFunctionReturn(0);
117047c6ae99SBarry Smith }
117147c6ae99SBarry Smith 
117247c6ae99SBarry Smith #undef __FUNCT__
117347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
117447c6ae99SBarry Smith /*@C
117547c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
117647c6ae99SBarry Smith 
117747c6ae99SBarry Smith     Collective on DM
117847c6ae99SBarry Smith 
117947c6ae99SBarry Smith     Input Parameter:
118047c6ae99SBarry Smith +   dm - the DM object
118147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
118247c6ae99SBarry Smith 
118347c6ae99SBarry Smith     Output Parameter:
118447c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
118547c6ae99SBarry Smith 
118647c6ae99SBarry Smith     Level: developer
118747c6ae99SBarry Smith 
1188e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
118947c6ae99SBarry Smith 
119047c6ae99SBarry Smith @*/
11917087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
119247c6ae99SBarry Smith {
119347c6ae99SBarry Smith   PetscErrorCode ierr;
119447c6ae99SBarry Smith 
119547c6ae99SBarry Smith   PetscFunctionBegin;
1196171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
119747c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
119847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
119947c6ae99SBarry Smith   PetscValidPointer(dmc,3);
120047c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
120147c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
120247c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
120347c6ae99SBarry Smith     PetscInt i;
120447c6ae99SBarry Smith 
120547c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
120647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
120747c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
120847c6ae99SBarry Smith     }
120947c6ae99SBarry Smith   } else {
121047c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
121147c6ae99SBarry Smith   }
121247c6ae99SBarry Smith   PetscFunctionReturn(0);
121347c6ae99SBarry Smith }
121447c6ae99SBarry Smith 
121547c6ae99SBarry Smith #undef __FUNCT__
1216e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
121747c6ae99SBarry Smith /*@
1218e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
121947c6ae99SBarry Smith    grids associated with two DMs.
122047c6ae99SBarry Smith 
122147c6ae99SBarry Smith    Collective on DM
122247c6ae99SBarry Smith 
122347c6ae99SBarry Smith    Input Parameters:
122447c6ae99SBarry Smith +  dmc - the coarse grid DM
122547c6ae99SBarry Smith -  dmf - the fine grid DM
122647c6ae99SBarry Smith 
122747c6ae99SBarry Smith    Output Parameters:
122847c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
122947c6ae99SBarry Smith 
123047c6ae99SBarry Smith    Level: intermediate
123147c6ae99SBarry Smith 
123247c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
123347c6ae99SBarry Smith 
1234e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
123547c6ae99SBarry Smith @*/
1236e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
123747c6ae99SBarry Smith {
123847c6ae99SBarry Smith   PetscErrorCode ierr;
123947c6ae99SBarry Smith 
124047c6ae99SBarry Smith   PetscFunctionBegin;
1241171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1242171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
124347c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
124447c6ae99SBarry Smith   PetscFunctionReturn(0);
124547c6ae99SBarry Smith }
124647c6ae99SBarry Smith 
124747c6ae99SBarry Smith #undef __FUNCT__
12481a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
12491a266240SBarry Smith /*@C
12501a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
12511a266240SBarry Smith 
12521a266240SBarry Smith     Not Collective
12531a266240SBarry Smith 
12541a266240SBarry Smith     Input Parameters:
12551a266240SBarry Smith +   dm - the DM object
12561a266240SBarry Smith -   destroy - the destroy function
12571a266240SBarry Smith 
12581a266240SBarry Smith     Level: intermediate
12591a266240SBarry Smith 
1260e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
12611a266240SBarry Smith 
1262f07f9ceaSJed Brown @*/
12631a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
12641a266240SBarry Smith {
12651a266240SBarry Smith   PetscFunctionBegin;
1266171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12671a266240SBarry Smith   dm->ctxdestroy = destroy;
12681a266240SBarry Smith   PetscFunctionReturn(0);
12691a266240SBarry Smith }
12701a266240SBarry Smith 
12711a266240SBarry Smith #undef __FUNCT__
12721b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1273b07ff414SBarry Smith /*@
12741b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
127547c6ae99SBarry Smith 
127647c6ae99SBarry Smith     Not Collective
127747c6ae99SBarry Smith 
127847c6ae99SBarry Smith     Input Parameters:
127947c6ae99SBarry Smith +   dm - the DM object
128047c6ae99SBarry Smith -   ctx - the user context
128147c6ae99SBarry Smith 
128247c6ae99SBarry Smith     Level: intermediate
128347c6ae99SBarry Smith 
1284e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
128547c6ae99SBarry Smith 
128647c6ae99SBarry Smith @*/
12871b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
128847c6ae99SBarry Smith {
128947c6ae99SBarry Smith   PetscFunctionBegin;
1290171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
129147c6ae99SBarry Smith   dm->ctx = ctx;
129247c6ae99SBarry Smith   PetscFunctionReturn(0);
129347c6ae99SBarry Smith }
129447c6ae99SBarry Smith 
129547c6ae99SBarry Smith #undef __FUNCT__
12961b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
129747c6ae99SBarry Smith /*@
12981b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
129947c6ae99SBarry Smith 
130047c6ae99SBarry Smith     Not Collective
130147c6ae99SBarry Smith 
130247c6ae99SBarry Smith     Input Parameter:
130347c6ae99SBarry Smith .   dm - the DM object
130447c6ae99SBarry Smith 
130547c6ae99SBarry Smith     Output Parameter:
130647c6ae99SBarry Smith .   ctx - the user context
130747c6ae99SBarry Smith 
130847c6ae99SBarry Smith     Level: intermediate
130947c6ae99SBarry Smith 
1310e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
131147c6ae99SBarry Smith 
131247c6ae99SBarry Smith @*/
13131b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
131447c6ae99SBarry Smith {
131547c6ae99SBarry Smith   PetscFunctionBegin;
1316171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13171b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
131847c6ae99SBarry Smith   PetscFunctionReturn(0);
131947c6ae99SBarry Smith }
132047c6ae99SBarry Smith 
132147c6ae99SBarry Smith #undef __FUNCT__
132247c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
13237e833e3aSBarry Smith /*@C
132447c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
132547c6ae99SBarry Smith 
132647c6ae99SBarry Smith     Logically Collective on DM
132747c6ae99SBarry Smith 
132847c6ae99SBarry Smith     Input Parameter:
132947c6ae99SBarry Smith +   dm - the DM object to destroy
133047c6ae99SBarry Smith -   f - the function to compute the initial guess
133147c6ae99SBarry Smith 
133247c6ae99SBarry Smith     Level: intermediate
133347c6ae99SBarry Smith 
1334e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
133547c6ae99SBarry Smith 
1336f07f9ceaSJed Brown @*/
13377087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
133847c6ae99SBarry Smith {
133947c6ae99SBarry Smith   PetscFunctionBegin;
1340171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
134147c6ae99SBarry Smith   dm->ops->initialguess = f;
134247c6ae99SBarry Smith   PetscFunctionReturn(0);
134347c6ae99SBarry Smith }
134447c6ae99SBarry Smith 
134547c6ae99SBarry Smith #undef __FUNCT__
134647c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
13477e833e3aSBarry Smith /*@C
134847c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
134947c6ae99SBarry Smith 
135047c6ae99SBarry Smith     Logically Collective on DM
135147c6ae99SBarry Smith 
135247c6ae99SBarry Smith     Input Parameter:
135347c6ae99SBarry Smith +   dm - the DM object
135447c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
135547c6ae99SBarry Smith 
135647c6ae99SBarry Smith     Level: intermediate
135747c6ae99SBarry Smith 
135847c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
135947c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
136047c6ae99SBarry Smith 
1361e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
136247c6ae99SBarry Smith          DMSetJacobian()
136347c6ae99SBarry Smith 
1364f07f9ceaSJed Brown @*/
13657087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
136647c6ae99SBarry Smith {
136747c6ae99SBarry Smith   PetscFunctionBegin;
1368171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
136947c6ae99SBarry Smith   dm->ops->function = f;
137047c6ae99SBarry Smith   if (f) {
137147c6ae99SBarry Smith     dm->ops->functionj = f;
137247c6ae99SBarry Smith   }
137347c6ae99SBarry Smith   PetscFunctionReturn(0);
137447c6ae99SBarry Smith }
137547c6ae99SBarry Smith 
137647c6ae99SBarry Smith #undef __FUNCT__
137747c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
13787e833e3aSBarry Smith /*@C
137947c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
138047c6ae99SBarry Smith 
138147c6ae99SBarry Smith     Logically Collective on DM
138247c6ae99SBarry Smith 
138347c6ae99SBarry Smith     Input Parameter:
138447c6ae99SBarry Smith +   dm - the DM object to destroy
138547c6ae99SBarry Smith -   f - the function to compute the matrix entries
138647c6ae99SBarry Smith 
138747c6ae99SBarry Smith     Level: intermediate
138847c6ae99SBarry Smith 
1389e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
139047c6ae99SBarry Smith          DMSetFunction()
139147c6ae99SBarry Smith 
1392f07f9ceaSJed Brown @*/
13937087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
139447c6ae99SBarry Smith {
139547c6ae99SBarry Smith   PetscFunctionBegin;
1396171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
139747c6ae99SBarry Smith   dm->ops->jacobian = f;
139847c6ae99SBarry Smith   PetscFunctionReturn(0);
139947c6ae99SBarry Smith }
140047c6ae99SBarry Smith 
140147c6ae99SBarry Smith #undef __FUNCT__
140208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
140308da532bSDmitry Karpeev /*@C
140408da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
140508da532bSDmitry Karpeev 
140608da532bSDmitry Karpeev     Logically Collective on DM
140708da532bSDmitry Karpeev 
140808da532bSDmitry Karpeev     Input Parameter:
140908da532bSDmitry Karpeev +   dm - the DM object
141008da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
141108da532bSDmitry Karpeev 
141208da532bSDmitry Karpeev     Level: intermediate
141308da532bSDmitry Karpeev 
1414e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
141508da532bSDmitry Karpeev          DMSetJacobian()
141608da532bSDmitry Karpeev 
141708da532bSDmitry Karpeev @*/
141808da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
141908da532bSDmitry Karpeev {
142008da532bSDmitry Karpeev   PetscFunctionBegin;
142108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
142208da532bSDmitry Karpeev   PetscFunctionReturn(0);
142308da532bSDmitry Karpeev }
142408da532bSDmitry Karpeev 
142508da532bSDmitry Karpeev #undef __FUNCT__
142608da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
142708da532bSDmitry Karpeev /*@
142808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
142908da532bSDmitry Karpeev 
143008da532bSDmitry Karpeev     Not Collective
143108da532bSDmitry Karpeev 
143208da532bSDmitry Karpeev     Input Parameter:
143308da532bSDmitry Karpeev .   dm - the DM object to destroy
143408da532bSDmitry Karpeev 
143508da532bSDmitry Karpeev     Output Parameter:
143608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
143708da532bSDmitry Karpeev 
143808da532bSDmitry Karpeev     Level: developer
143908da532bSDmitry Karpeev 
1440e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
144108da532bSDmitry Karpeev 
144208da532bSDmitry Karpeev @*/
144308da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
144408da532bSDmitry Karpeev {
144508da532bSDmitry Karpeev   PetscFunctionBegin;
144608da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
144708da532bSDmitry Karpeev   PetscFunctionReturn(0);
144808da532bSDmitry Karpeev }
144908da532bSDmitry Karpeev 
145008da532bSDmitry Karpeev #undef __FUNCT__
145108da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
145208da532bSDmitry Karpeev /*@C
145308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
145408da532bSDmitry Karpeev 
145508da532bSDmitry Karpeev     Logically Collective on DM
145608da532bSDmitry Karpeev 
145708da532bSDmitry Karpeev     Input Parameters:
145808da532bSDmitry Karpeev +   dm - the DM object to destroy
145908da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
146008da532bSDmitry Karpeev 
146108da532bSDmitry Karpeev     Output parameters:
146208da532bSDmitry Karpeev +   xl - lower bound
146308da532bSDmitry Karpeev -   xu - upper bound
146408da532bSDmitry Karpeev 
146508da532bSDmitry Karpeev     Level: intermediate
146608da532bSDmitry Karpeev 
1467e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
146808da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
146908da532bSDmitry Karpeev 
147008da532bSDmitry Karpeev @*/
147108da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
147208da532bSDmitry Karpeev {
147308da532bSDmitry Karpeev   PetscErrorCode ierr;
147408da532bSDmitry Karpeev   PetscFunctionBegin;
147508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
147608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
147708da532bSDmitry Karpeev   if(dm->ops->computevariablebounds) {
147808da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
147908da532bSDmitry Karpeev   }
148008da532bSDmitry Karpeev   else {
148108da532bSDmitry Karpeev     ierr = VecSet(xl,SNES_VI_NINF); CHKERRQ(ierr);
148208da532bSDmitry Karpeev     ierr = VecSet(xu,SNES_VI_INF);  CHKERRQ(ierr);
148308da532bSDmitry Karpeev   }
148408da532bSDmitry Karpeev   PetscFunctionReturn(0);
148508da532bSDmitry Karpeev }
148608da532bSDmitry Karpeev 
148708da532bSDmitry Karpeev #undef __FUNCT__
148847c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
148947c6ae99SBarry Smith /*@
149047c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
149147c6ae99SBarry Smith 
149247c6ae99SBarry Smith     Collective on DM
149347c6ae99SBarry Smith 
149447c6ae99SBarry Smith     Input Parameter:
149547c6ae99SBarry Smith +   dm - the DM object to destroy
149647c6ae99SBarry Smith -   x - the vector to hold the initial guess values
149747c6ae99SBarry Smith 
149847c6ae99SBarry Smith     Level: developer
149947c6ae99SBarry Smith 
1500e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
150147c6ae99SBarry Smith 
150247c6ae99SBarry Smith @*/
15037087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
150447c6ae99SBarry Smith {
150547c6ae99SBarry Smith   PetscErrorCode ierr;
150647c6ae99SBarry Smith   PetscFunctionBegin;
1507171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
150847c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
150947c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
151047c6ae99SBarry Smith   PetscFunctionReturn(0);
151147c6ae99SBarry Smith }
151247c6ae99SBarry Smith 
151347c6ae99SBarry Smith #undef __FUNCT__
151447c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
151547c6ae99SBarry Smith /*@
151647c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
151747c6ae99SBarry Smith 
151847c6ae99SBarry Smith     Not Collective
151947c6ae99SBarry Smith 
152047c6ae99SBarry Smith     Input Parameter:
152147c6ae99SBarry Smith .   dm - the DM object to destroy
152247c6ae99SBarry Smith 
152347c6ae99SBarry Smith     Output Parameter:
152447c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
152547c6ae99SBarry Smith 
152647c6ae99SBarry Smith     Level: developer
152747c6ae99SBarry Smith 
1528e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
152947c6ae99SBarry Smith 
153047c6ae99SBarry Smith @*/
15317087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
153247c6ae99SBarry Smith {
153347c6ae99SBarry Smith   PetscFunctionBegin;
1534171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
153547c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
153647c6ae99SBarry Smith   PetscFunctionReturn(0);
153747c6ae99SBarry Smith }
153847c6ae99SBarry Smith 
153947c6ae99SBarry Smith #undef __FUNCT__
154047c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
154147c6ae99SBarry Smith /*@
154247c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
154347c6ae99SBarry Smith 
154447c6ae99SBarry Smith     Not Collective
154547c6ae99SBarry Smith 
154647c6ae99SBarry Smith     Input Parameter:
154747c6ae99SBarry Smith .   dm - the DM object to destroy
154847c6ae99SBarry Smith 
154947c6ae99SBarry Smith     Output Parameter:
155047c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
155147c6ae99SBarry Smith 
155247c6ae99SBarry Smith     Level: developer
155347c6ae99SBarry Smith 
1554e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
155547c6ae99SBarry Smith 
155647c6ae99SBarry Smith @*/
15577087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
155847c6ae99SBarry Smith {
155947c6ae99SBarry Smith   PetscFunctionBegin;
1560171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
156147c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
156247c6ae99SBarry Smith   PetscFunctionReturn(0);
156347c6ae99SBarry Smith }
156447c6ae99SBarry Smith 
156547c6ae99SBarry Smith #undef __FUNCT__
156647c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
156747c6ae99SBarry Smith /*@
156847c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
156947c6ae99SBarry Smith 
157047c6ae99SBarry Smith     Not Collective
157147c6ae99SBarry Smith 
157247c6ae99SBarry Smith     Input Parameter:
157347c6ae99SBarry Smith .   dm - the DM object to destroy
157447c6ae99SBarry Smith 
157547c6ae99SBarry Smith     Output Parameter:
157647c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
157747c6ae99SBarry Smith 
157847c6ae99SBarry Smith     Level: developer
157947c6ae99SBarry Smith 
1580e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
158147c6ae99SBarry Smith 
158247c6ae99SBarry Smith @*/
15837087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
158447c6ae99SBarry Smith {
158547c6ae99SBarry Smith   PetscFunctionBegin;
1586171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
158747c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
158847c6ae99SBarry Smith   PetscFunctionReturn(0);
158947c6ae99SBarry Smith }
159047c6ae99SBarry Smith 
159147c6ae99SBarry Smith #undef  __FUNCT__
159208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
1593748fac09SDmitry Karpeev /*@C
159408da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
159508da532bSDmitry Karpeev 
159608da532bSDmitry Karpeev     Collective on DM
159708da532bSDmitry Karpeev 
159808da532bSDmitry Karpeev     Input Parameter:
159908da532bSDmitry Karpeev +   dm - the DM object
1600e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
160108da532bSDmitry Karpeev 
160208da532bSDmitry Karpeev     Level: developer
160308da532bSDmitry Karpeev 
1604e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
160508da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
160608da532bSDmitry Karpeev 
160708da532bSDmitry Karpeev @*/
160808da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
160908da532bSDmitry Karpeev {
161008da532bSDmitry Karpeev   PetscErrorCode ierr;
161108da532bSDmitry Karpeev   PetscFunctionBegin;
161208da532bSDmitry Karpeev   if (x) {
161308da532bSDmitry Karpeev     if (!dm->x) {
161408da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
161508da532bSDmitry Karpeev     }
161608da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
161708da532bSDmitry Karpeev   }
161808da532bSDmitry Karpeev   else if(dm->x) {
161908da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
162008da532bSDmitry Karpeev   }
162108da532bSDmitry Karpeev   PetscFunctionReturn(0);
162208da532bSDmitry Karpeev }
162308da532bSDmitry Karpeev 
162408da532bSDmitry Karpeev 
162508da532bSDmitry Karpeev #undef __FUNCT__
162647c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
162747c6ae99SBarry Smith /*@
162847c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
162947c6ae99SBarry Smith 
163047c6ae99SBarry Smith     Collective on DM
163147c6ae99SBarry Smith 
163247c6ae99SBarry Smith     Input Parameter:
163347c6ae99SBarry Smith +   dm - the DM object to destroy
163447c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
163547c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
163647c6ae99SBarry Smith 
163747c6ae99SBarry Smith     Level: developer
163847c6ae99SBarry Smith 
1639e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
164047c6ae99SBarry Smith          DMSetJacobian()
164147c6ae99SBarry Smith 
164247c6ae99SBarry Smith @*/
16437087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
164447c6ae99SBarry Smith {
164547c6ae99SBarry Smith   PetscErrorCode ierr;
164647c6ae99SBarry Smith   PetscFunctionBegin;
1647171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
164847c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
1649644e2e5bSBarry Smith   PetscStackPush("DM user function");
165047c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
1651644e2e5bSBarry Smith   PetscStackPop;
165247c6ae99SBarry Smith   PetscFunctionReturn(0);
165347c6ae99SBarry Smith }
165447c6ae99SBarry Smith 
165547c6ae99SBarry Smith 
165608da532bSDmitry Karpeev 
165747c6ae99SBarry Smith #undef __FUNCT__
165847c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
165947c6ae99SBarry Smith /*@
166047c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
166147c6ae99SBarry Smith 
166247c6ae99SBarry Smith     Collective on DM
166347c6ae99SBarry Smith 
166447c6ae99SBarry Smith     Input Parameter:
166547c6ae99SBarry Smith +   dm - the DM object
1666cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
166747c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
166847c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
166947c6ae99SBarry Smith 
167047c6ae99SBarry Smith     Level: developer
167147c6ae99SBarry Smith 
1672e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
167347c6ae99SBarry Smith          DMSetFunction()
167447c6ae99SBarry Smith 
167547c6ae99SBarry Smith @*/
16767087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
167747c6ae99SBarry Smith {
167847c6ae99SBarry Smith   PetscErrorCode ierr;
167947c6ae99SBarry Smith 
168047c6ae99SBarry Smith   PetscFunctionBegin;
1681171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
168247c6ae99SBarry Smith   if (!dm->ops->jacobian) {
168347c6ae99SBarry Smith     ISColoring     coloring;
168447c6ae99SBarry Smith     MatFDColoring  fd;
168547c6ae99SBarry Smith 
1686171400e9SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,MATAIJ,&coloring);CHKERRQ(ierr);
168747c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
1688fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
168947c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
16900bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
16910bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
169271cd77b2SBarry Smith 
169347c6ae99SBarry Smith     dm->fd = fd;
169447c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
16952533e041SBarry Smith 
169671cd77b2SBarry Smith     /* don't know why this is needed */
169771cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
169847c6ae99SBarry Smith   }
169947c6ae99SBarry Smith   if (!x) x = dm->x;
170047c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
1701cab2e9ccSBarry Smith 
170271cd77b2SBarry 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 */
1703649052a6SBarry Smith   if (x) {
1704cab2e9ccSBarry Smith     if (!dm->x) {
170571cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
1706cab2e9ccSBarry Smith     }
1707cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
1708649052a6SBarry Smith   }
1709a8248277SBarry Smith   if (A != B) {
1710a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1711a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1712a8248277SBarry Smith   }
171347c6ae99SBarry Smith   PetscFunctionReturn(0);
171447c6ae99SBarry Smith }
1715264ace61SBarry Smith 
1716cab2e9ccSBarry Smith 
1717264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
1718264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
1719264ace61SBarry Smith 
1720264ace61SBarry Smith #undef __FUNCT__
1721264ace61SBarry Smith #define __FUNCT__ "DMSetType"
1722264ace61SBarry Smith /*@C
1723264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
1724264ace61SBarry Smith 
1725264ace61SBarry Smith   Collective on DM
1726264ace61SBarry Smith 
1727264ace61SBarry Smith   Input Parameters:
1728264ace61SBarry Smith + dm     - The DM object
1729264ace61SBarry Smith - method - The name of the DM type
1730264ace61SBarry Smith 
1731264ace61SBarry Smith   Options Database Key:
1732264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
1733264ace61SBarry Smith 
1734264ace61SBarry Smith   Notes:
1735e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
1736264ace61SBarry Smith 
1737264ace61SBarry Smith   Level: intermediate
1738264ace61SBarry Smith 
1739264ace61SBarry Smith .keywords: DM, set, type
1740264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
1741264ace61SBarry Smith @*/
17427087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
1743264ace61SBarry Smith {
1744264ace61SBarry Smith   PetscErrorCode (*r)(DM);
1745264ace61SBarry Smith   PetscBool      match;
1746264ace61SBarry Smith   PetscErrorCode ierr;
1747264ace61SBarry Smith 
1748264ace61SBarry Smith   PetscFunctionBegin;
1749264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1750264ace61SBarry Smith   ierr = PetscTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
1751264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
1752264ace61SBarry Smith 
1753264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
17544b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
1755264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
1756264ace61SBarry Smith 
1757264ace61SBarry Smith   if (dm->ops->destroy) {
1758264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
1759b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
1760264ace61SBarry Smith   }
1761264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
1762264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
1763264ace61SBarry Smith   PetscFunctionReturn(0);
1764264ace61SBarry Smith }
1765264ace61SBarry Smith 
1766264ace61SBarry Smith #undef __FUNCT__
1767264ace61SBarry Smith #define __FUNCT__ "DMGetType"
1768264ace61SBarry Smith /*@C
1769264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
1770264ace61SBarry Smith 
1771264ace61SBarry Smith   Not Collective
1772264ace61SBarry Smith 
1773264ace61SBarry Smith   Input Parameter:
1774264ace61SBarry Smith . dm  - The DM
1775264ace61SBarry Smith 
1776264ace61SBarry Smith   Output Parameter:
1777264ace61SBarry Smith . type - The DM type name
1778264ace61SBarry Smith 
1779264ace61SBarry Smith   Level: intermediate
1780264ace61SBarry Smith 
1781264ace61SBarry Smith .keywords: DM, get, type, name
1782264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
1783264ace61SBarry Smith @*/
17847087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
1785264ace61SBarry Smith {
1786264ace61SBarry Smith   PetscErrorCode ierr;
1787264ace61SBarry Smith 
1788264ace61SBarry Smith   PetscFunctionBegin;
1789264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1790264ace61SBarry Smith   PetscValidCharPointer(type,2);
1791264ace61SBarry Smith   if (!DMRegisterAllCalled) {
1792264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
1793264ace61SBarry Smith   }
1794264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
1795264ace61SBarry Smith   PetscFunctionReturn(0);
1796264ace61SBarry Smith }
1797264ace61SBarry Smith 
179867a56275SMatthew G Knepley #undef __FUNCT__
179967a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
180067a56275SMatthew G Knepley /*@C
180167a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
180267a56275SMatthew G Knepley 
180367a56275SMatthew G Knepley   Collective on DM
180467a56275SMatthew G Knepley 
180567a56275SMatthew G Knepley   Input Parameters:
180667a56275SMatthew G Knepley + dm - the DM
180767a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
180867a56275SMatthew G Knepley 
180967a56275SMatthew G Knepley   Output Parameter:
181067a56275SMatthew G Knepley . M - pointer to new DM
181167a56275SMatthew G Knepley 
181267a56275SMatthew G Knepley   Notes:
181367a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
181467a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
181567a56275SMatthew G Knepley   of the input DM.
181667a56275SMatthew G Knepley 
181767a56275SMatthew G Knepley   Level: intermediate
181867a56275SMatthew G Knepley 
181967a56275SMatthew G Knepley .seealso: DMCreate()
182067a56275SMatthew G Knepley @*/
182167a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
182267a56275SMatthew G Knepley {
182367a56275SMatthew G Knepley   DM             B;
182467a56275SMatthew G Knepley   char           convname[256];
182567a56275SMatthew G Knepley   PetscBool      sametype, issame;
182667a56275SMatthew G Knepley   PetscErrorCode ierr;
182767a56275SMatthew G Knepley 
182867a56275SMatthew G Knepley   PetscFunctionBegin;
182967a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
183067a56275SMatthew G Knepley   PetscValidType(dm,1);
183167a56275SMatthew G Knepley   PetscValidPointer(M,3);
183267a56275SMatthew G Knepley   ierr = PetscTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
183367a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
183467a56275SMatthew G Knepley   {
183567a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
183667a56275SMatthew G Knepley 
183767a56275SMatthew G Knepley     /*
183867a56275SMatthew G Knepley        Order of precedence:
183967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
184067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
184167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
184267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
184367a56275SMatthew G Knepley        5) Use a really basic converter.
184467a56275SMatthew G Knepley     */
184567a56275SMatthew G Knepley 
184667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
184767a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
184867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
184967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
185067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
185167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
185267a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
185367a56275SMatthew G Knepley     if (conv) goto foundconv;
185467a56275SMatthew G Knepley 
185567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
185667a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
185767a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
185867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
185967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
186067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
186167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
186267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
186367a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
186467a56275SMatthew G Knepley     if (conv) {
1865fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
186667a56275SMatthew G Knepley       goto foundconv;
186767a56275SMatthew G Knepley     }
186867a56275SMatthew G Knepley 
186967a56275SMatthew G Knepley #if 0
187067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
187167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
1872fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
187367a56275SMatthew G Knepley     if (conv) goto foundconv;
187467a56275SMatthew G Knepley 
187567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
187667a56275SMatthew G Knepley     if (dm->ops->convert) {
187767a56275SMatthew G Knepley       conv = dm->ops->convert;
187867a56275SMatthew G Knepley     }
187967a56275SMatthew G Knepley     if (conv) goto foundconv;
188067a56275SMatthew G Knepley #endif
188167a56275SMatthew G Knepley 
188267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
188367a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
188467a56275SMatthew G Knepley 
188567a56275SMatthew G Knepley     foundconv:
188667a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
188767a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
188867a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
188967a56275SMatthew G Knepley   }
189067a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
189167a56275SMatthew G Knepley   PetscFunctionReturn(0);
189267a56275SMatthew G Knepley }
1893264ace61SBarry Smith 
1894264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1895264ace61SBarry Smith 
1896264ace61SBarry Smith #undef __FUNCT__
1897264ace61SBarry Smith #define __FUNCT__ "DMRegister"
1898264ace61SBarry Smith /*@C
1899264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
1900264ace61SBarry Smith 
1901264ace61SBarry Smith   Level: advanced
1902264ace61SBarry Smith @*/
19037087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
1904264ace61SBarry Smith {
1905264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
1906264ace61SBarry Smith   PetscErrorCode ierr;
1907264ace61SBarry Smith 
1908264ace61SBarry Smith   PetscFunctionBegin;
1909264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
1910264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
1911264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
1912264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
1913264ace61SBarry Smith   PetscFunctionReturn(0);
1914264ace61SBarry Smith }
1915264ace61SBarry Smith 
1916264ace61SBarry Smith 
1917264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1918264ace61SBarry Smith #undef __FUNCT__
1919264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
1920264ace61SBarry Smith /*@C
1921264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
1922264ace61SBarry Smith 
1923264ace61SBarry Smith    Not Collective
1924264ace61SBarry Smith 
1925264ace61SBarry Smith    Level: advanced
1926264ace61SBarry Smith 
1927264ace61SBarry Smith .keywords: DM, register, destroy
1928264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
1929264ace61SBarry Smith @*/
19307087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
1931264ace61SBarry Smith {
1932264ace61SBarry Smith   PetscErrorCode ierr;
1933264ace61SBarry Smith 
1934264ace61SBarry Smith   PetscFunctionBegin;
1935264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
1936264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
1937264ace61SBarry Smith   PetscFunctionReturn(0);
1938264ace61SBarry Smith }
193923f975d1SBarry Smith 
194023f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
1941c6db04a5SJed Brown #include <mex.h>
194223f975d1SBarry Smith 
19433014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
194423f975d1SBarry Smith 
194523f975d1SBarry Smith #undef __FUNCT__
194623f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
194723f975d1SBarry Smith /*
194823f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
194923f975d1SBarry Smith                          DMSetFunctionMatlab().
195023f975d1SBarry Smith 
195123f975d1SBarry Smith    For linear problems x is null
195223f975d1SBarry Smith 
195323f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
195423f975d1SBarry Smith */
19557087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
195623f975d1SBarry Smith {
195723f975d1SBarry Smith   PetscErrorCode    ierr;
195823f975d1SBarry Smith   DMMatlabContext   *sctx;
195923f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
196023f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
196123f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
196223f975d1SBarry Smith 
196323f975d1SBarry Smith   PetscFunctionBegin;
196423f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
196523f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
196623f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
196723f975d1SBarry Smith 
196823f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
19691b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
197023f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
197123f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
19723014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
197323f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
197423f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
197523f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
197623f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
1977b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
197823f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
197923f975d1SBarry Smith   mxDestroyArray(prhs[0]);
198023f975d1SBarry Smith   mxDestroyArray(prhs[1]);
198123f975d1SBarry Smith   mxDestroyArray(prhs[2]);
198223f975d1SBarry Smith   mxDestroyArray(prhs[3]);
198323f975d1SBarry Smith   mxDestroyArray(plhs[0]);
198423f975d1SBarry Smith   PetscFunctionReturn(0);
198523f975d1SBarry Smith }
198623f975d1SBarry Smith 
198723f975d1SBarry Smith 
198823f975d1SBarry Smith #undef __FUNCT__
198923f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
199023f975d1SBarry Smith /*
199123f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
199223f975d1SBarry Smith 
199323f975d1SBarry Smith */
19947087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
199523f975d1SBarry Smith {
199623f975d1SBarry Smith   PetscErrorCode    ierr;
199723f975d1SBarry Smith   DMMatlabContext   *sctx;
199823f975d1SBarry Smith 
199923f975d1SBarry Smith   PetscFunctionBegin;
2000171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
200123f975d1SBarry Smith   /* currently sctx is memory bleed */
20021b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
20033014e516SBarry Smith   if (!sctx) {
200423f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
20053014e516SBarry Smith   }
200623f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
20071b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
200823f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
200923f975d1SBarry Smith   PetscFunctionReturn(0);
201023f975d1SBarry Smith }
20113014e516SBarry Smith 
20123014e516SBarry Smith #undef __FUNCT__
20133014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
20143014e516SBarry Smith /*
20153014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
20163014e516SBarry Smith                          DMSetJacobianMatlab().
20173014e516SBarry Smith 
20183014e516SBarry Smith    For linear problems x is null
20193014e516SBarry Smith 
20203014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
20213014e516SBarry Smith */
20227087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
20233014e516SBarry Smith {
20243014e516SBarry Smith   PetscErrorCode    ierr;
20253014e516SBarry Smith   DMMatlabContext   *sctx;
20263014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
20273014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
20283014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
20293014e516SBarry Smith 
20303014e516SBarry Smith   PetscFunctionBegin;
20313014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20323014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
20333014e516SBarry Smith 
2034e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
20351b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
20363014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
20373014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
20383014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
20393014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
20403014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
20413014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
20423014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
20433014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
20443014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2045b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2046c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2047c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
20483014e516SBarry Smith   mxDestroyArray(prhs[0]);
20493014e516SBarry Smith   mxDestroyArray(prhs[1]);
20503014e516SBarry Smith   mxDestroyArray(prhs[2]);
20513014e516SBarry Smith   mxDestroyArray(prhs[3]);
20523014e516SBarry Smith   mxDestroyArray(prhs[4]);
20533014e516SBarry Smith   mxDestroyArray(plhs[0]);
20543014e516SBarry Smith   mxDestroyArray(plhs[1]);
20553014e516SBarry Smith   PetscFunctionReturn(0);
20563014e516SBarry Smith }
20573014e516SBarry Smith 
20583014e516SBarry Smith 
20593014e516SBarry Smith #undef __FUNCT__
20603014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
20613014e516SBarry Smith /*
20623014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
20633014e516SBarry Smith 
20643014e516SBarry Smith */
20657087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
20663014e516SBarry Smith {
20673014e516SBarry Smith   PetscErrorCode    ierr;
20683014e516SBarry Smith   DMMatlabContext   *sctx;
20693014e516SBarry Smith 
20703014e516SBarry Smith   PetscFunctionBegin;
2071171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20723014e516SBarry Smith   /* currently sctx is memory bleed */
20731b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
20743014e516SBarry Smith   if (!sctx) {
20753014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
20763014e516SBarry Smith   }
20773014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
20781b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
20793014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
20803014e516SBarry Smith   PetscFunctionReturn(0);
20813014e516SBarry Smith }
208223f975d1SBarry Smith #endif
2083b859378eSBarry Smith 
2084b859378eSBarry Smith #undef __FUNCT__
2085b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2086b859378eSBarry Smith /*@C
2087b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2088b859378eSBarry Smith   with DMView().
2089b859378eSBarry Smith 
2090b859378eSBarry Smith   Collective on PetscViewer
2091b859378eSBarry Smith 
2092b859378eSBarry Smith   Input Parameters:
2093b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2094b859378eSBarry Smith            some related function before a call to DMLoad().
2095b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2096b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2097b859378eSBarry Smith 
2098b859378eSBarry Smith    Level: intermediate
2099b859378eSBarry Smith 
2100b859378eSBarry Smith   Notes:
2101b859378eSBarry Smith   Defaults to the DM DA.
2102b859378eSBarry Smith 
2103b859378eSBarry Smith   Notes for advanced users:
2104b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2105b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2106b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2107b859378eSBarry Smith   format is
2108b859378eSBarry Smith .vb
2109b859378eSBarry Smith      has not yet been determined
2110b859378eSBarry Smith .ve
2111b859378eSBarry Smith 
2112b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2113b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2114b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2115b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2116b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2117b859378eSBarry Smith 
2118b859378eSBarry Smith   Concepts: vector^loading from file
2119b859378eSBarry Smith 
2120b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2121b859378eSBarry Smith @*/
2122b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2123b859378eSBarry Smith {
2124b859378eSBarry Smith   PetscErrorCode ierr;
2125b859378eSBarry Smith 
2126b859378eSBarry Smith   PetscFunctionBegin;
2127b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2128b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2129b859378eSBarry Smith 
2130b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2131b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2132b859378eSBarry Smith   }
2133b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2134b859378eSBarry Smith   PetscFunctionReturn(0);
2135b859378eSBarry Smith }
2136b859378eSBarry Smith 
21377da65231SMatthew G Knepley /******************************** FEM Support **********************************/
21387da65231SMatthew G Knepley 
21397da65231SMatthew G Knepley #undef __FUNCT__
21407da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
21417da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
21421d47ebbbSSatish Balay   PetscInt       f;
21431b30c384SMatthew G Knepley   PetscErrorCode ierr;
21441b30c384SMatthew G Knepley 
21457da65231SMatthew G Knepley   PetscFunctionBegin;
214674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
21471d47ebbbSSatish Balay   for(f = 0; f < len; ++f) {
214874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
21497da65231SMatthew G Knepley   }
21507da65231SMatthew G Knepley   PetscFunctionReturn(0);
21517da65231SMatthew G Knepley }
21527da65231SMatthew G Knepley 
21537da65231SMatthew G Knepley #undef __FUNCT__
21547da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
21557da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
21561b30c384SMatthew G Knepley   PetscInt       f, g;
21577da65231SMatthew G Knepley   PetscErrorCode ierr;
21587da65231SMatthew G Knepley 
21597da65231SMatthew G Knepley   PetscFunctionBegin;
216074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
21611d47ebbbSSatish Balay   for(f = 0; f < rows; ++f) {
216274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
21631d47ebbbSSatish Balay     for(g = 0; g < cols; ++g) {
216474778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
21657da65231SMatthew G Knepley     }
216674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
21677da65231SMatthew G Knepley   }
21687da65231SMatthew G Knepley   PetscFunctionReturn(0);
21697da65231SMatthew G Knepley }
2170