xref: /petsc/src/dm/interface/dm.c (revision 1d47ebbb7e3d8e91004033c2c370db7c3f4e9e09)
147c6ae99SBarry Smith 
2c6db04a5SJed Brown #include <private/dmimpl.h>     /*I      "petscdm.h"     I*/
347c6ae99SBarry Smith 
4732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
567a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal;
667a56275SMatthew G Knepley 
747c6ae99SBarry Smith #undef __FUNCT__
8a4121054SBarry Smith #define __FUNCT__ "DMCreate"
9a4121054SBarry Smith /*@
10de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
11a4121054SBarry Smith 
12a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
13a4121054SBarry Smith    error when you try to use the vector.
14a4121054SBarry Smith 
15a4121054SBarry Smith   Collective on MPI_Comm
16a4121054SBarry Smith 
17a4121054SBarry Smith   Input Parameter:
18a4121054SBarry Smith . comm - The communicator for the DM object
19a4121054SBarry Smith 
20a4121054SBarry Smith   Output Parameter:
21a4121054SBarry Smith . dm - The DM object
22a4121054SBarry Smith 
23a4121054SBarry Smith   Level: beginner
24a4121054SBarry Smith 
25a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
26a4121054SBarry Smith @*/
277087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
28a4121054SBarry Smith {
29a4121054SBarry Smith   DM             v;
30a4121054SBarry Smith   PetscErrorCode ierr;
31a4121054SBarry Smith 
32a4121054SBarry Smith   PetscFunctionBegin;
331411c6eeSJed Brown   PetscValidPointer(dm,2);
341411c6eeSJed Brown   *dm = PETSC_NULL;
35a4121054SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
36b84caa0eSBarry Smith   ierr = VecInitializePackage(PETSC_NULL);CHKERRQ(ierr);
37b84caa0eSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
38a4121054SBarry Smith   ierr = DMInitializePackage(PETSC_NULL);CHKERRQ(ierr);
39a4121054SBarry Smith #endif
40a4121054SBarry Smith 
413194b578SJed Brown   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
42a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
431411c6eeSJed Brown 
44a89ea682SMatthew G Knepley   v->workSize     = 0;
45a89ea682SMatthew G Knepley   v->workArray    = PETSC_NULL;
461411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
471411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
481411c6eeSJed Brown   v->bs           = 1;
49171400e9SBarry Smith   v->coloringtype = IS_COLORING_GLOBAL;
501411c6eeSJed Brown 
511411c6eeSJed Brown   *dm = v;
52a4121054SBarry Smith   PetscFunctionReturn(0);
53a4121054SBarry Smith }
54a4121054SBarry Smith 
55a4121054SBarry Smith 
56a4121054SBarry Smith #undef __FUNCT__
579a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
589a42bb27SBarry Smith /*@C
59564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
609a42bb27SBarry Smith 
61aa219208SBarry Smith    Logically Collective on DMDA
629a42bb27SBarry Smith 
639a42bb27SBarry Smith    Input Parameter:
649a42bb27SBarry Smith +  da - initial distributed array
658154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
669a42bb27SBarry Smith 
679a42bb27SBarry Smith    Options Database:
68dd85299cSBarry Smith .   -dm_vec_type ctype
699a42bb27SBarry Smith 
709a42bb27SBarry Smith    Level: intermediate
719a42bb27SBarry Smith 
72aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
739a42bb27SBarry Smith @*/
747087cfbeSBarry Smith PetscErrorCode  DMSetVecType(DM da,const VecType ctype)
759a42bb27SBarry Smith {
769a42bb27SBarry Smith   PetscErrorCode ierr;
779a42bb27SBarry Smith 
789a42bb27SBarry Smith   PetscFunctionBegin;
799a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
809a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
819a42bb27SBarry Smith   ierr = PetscStrallocpy(ctype,&da->vectype);CHKERRQ(ierr);
829a42bb27SBarry Smith   PetscFunctionReturn(0);
839a42bb27SBarry Smith }
849a42bb27SBarry Smith 
859a42bb27SBarry Smith #undef __FUNCT__
869a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
879a42bb27SBarry Smith /*@C
889a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
89aa219208SBarry Smith    DMDA options in the database.
909a42bb27SBarry Smith 
91aa219208SBarry Smith    Logically Collective on DMDA
929a42bb27SBarry Smith 
939a42bb27SBarry Smith    Input Parameter:
94aa219208SBarry Smith +  da - the DMDA context
959a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
969a42bb27SBarry Smith 
979a42bb27SBarry Smith    Notes:
989a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
999a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
1009a42bb27SBarry Smith 
1019a42bb27SBarry Smith    Level: advanced
1029a42bb27SBarry Smith 
103aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
1049a42bb27SBarry Smith 
1059a42bb27SBarry Smith .seealso: DMSetFromOptions()
1069a42bb27SBarry Smith @*/
1077087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
1089a42bb27SBarry Smith {
1099a42bb27SBarry Smith   PetscErrorCode ierr;
1109a42bb27SBarry Smith 
1119a42bb27SBarry Smith   PetscFunctionBegin;
1129a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1139a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
1149a42bb27SBarry Smith   PetscFunctionReturn(0);
1159a42bb27SBarry Smith }
1169a42bb27SBarry Smith 
1179a42bb27SBarry Smith #undef __FUNCT__
11847c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
11947c6ae99SBarry Smith /*@
120aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
12147c6ae99SBarry Smith 
12247c6ae99SBarry Smith     Collective on DM
12347c6ae99SBarry Smith 
12447c6ae99SBarry Smith     Input Parameter:
12547c6ae99SBarry Smith .   dm - the DM object to destroy
12647c6ae99SBarry Smith 
12747c6ae99SBarry Smith     Level: developer
12847c6ae99SBarry Smith 
129e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
13047c6ae99SBarry Smith 
13147c6ae99SBarry Smith @*/
132fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
13347c6ae99SBarry Smith {
134732e2eb9SMatthew G Knepley   PetscInt       i, cnt = 0;
13547c6ae99SBarry Smith   PetscErrorCode ierr;
13647c6ae99SBarry Smith 
13747c6ae99SBarry Smith   PetscFunctionBegin;
1386bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
1396bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
14087e657c6SBarry Smith 
14187e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
142732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1436bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
1446bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
145732e2eb9SMatthew G Knepley   }
14671cd77b2SBarry Smith   if ((*dm)->x) {
14771cd77b2SBarry Smith     PetscObject obj;
14871cd77b2SBarry Smith     ierr = PetscObjectQuery((PetscObject)(*dm)->x,"DM",&obj);CHKERRQ(ierr);
14971cd77b2SBarry Smith     if (obj == (PetscObject)*dm) cnt++;
15071cd77b2SBarry Smith   }
151732e2eb9SMatthew G Knepley 
1526bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
153732e2eb9SMatthew G Knepley   /*
154732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
155732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
156732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
157732e2eb9SMatthew G Knepley   */
1586bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
1596bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
160732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1616bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
1626bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
163732e2eb9SMatthew G Knepley   }
1641a266240SBarry Smith 
1651a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
1661a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
1671a266240SBarry Smith   }
16887e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
16971cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
1704dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
1716bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
1726bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
1736bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
174073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
175a89ea682SMatthew G Knepley   ierr = PetscFree((*dm)->workArray);CHKERRQ(ierr);
176732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
1776bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
178732e2eb9SMatthew G Knepley 
1796bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
1806bf464f9SBarry Smith   ierr = PetscFree((*dm)->data);CHKERRQ(ierr);
181732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
18247c6ae99SBarry Smith   PetscFunctionReturn(0);
18347c6ae99SBarry Smith }
18447c6ae99SBarry Smith 
18547c6ae99SBarry Smith #undef __FUNCT__
186d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
187d7bf68aeSBarry Smith /*@
188d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
189d7bf68aeSBarry Smith 
190d7bf68aeSBarry Smith     Collective on DM
191d7bf68aeSBarry Smith 
192d7bf68aeSBarry Smith     Input Parameter:
193d7bf68aeSBarry Smith .   dm - the DM object to setup
194d7bf68aeSBarry Smith 
195d7bf68aeSBarry Smith     Level: developer
196d7bf68aeSBarry Smith 
197e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
198d7bf68aeSBarry Smith 
199d7bf68aeSBarry Smith @*/
2007087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
201d7bf68aeSBarry Smith {
202d7bf68aeSBarry Smith   PetscErrorCode ierr;
203d7bf68aeSBarry Smith 
204d7bf68aeSBarry Smith   PetscFunctionBegin;
205171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2068387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
207d7bf68aeSBarry Smith   if (dm->ops->setup) {
208d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
209d7bf68aeSBarry Smith   }
2108387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
211d7bf68aeSBarry Smith   PetscFunctionReturn(0);
212d7bf68aeSBarry Smith }
213d7bf68aeSBarry Smith 
214d7bf68aeSBarry Smith #undef __FUNCT__
215d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
216d7bf68aeSBarry Smith /*@
217d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
218d7bf68aeSBarry Smith 
219d7bf68aeSBarry Smith     Collective on DM
220d7bf68aeSBarry Smith 
221d7bf68aeSBarry Smith     Input Parameter:
222d7bf68aeSBarry Smith .   dm - the DM object to set options for
223d7bf68aeSBarry Smith 
224732e2eb9SMatthew G Knepley     Options Database:
225dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
226dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
227171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
228171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
229732e2eb9SMatthew G Knepley 
230d7bf68aeSBarry Smith     Level: developer
231d7bf68aeSBarry Smith 
232e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
233d7bf68aeSBarry Smith 
234d7bf68aeSBarry Smith @*/
2357087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
236d7bf68aeSBarry Smith {
237c4721b0eSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg;
238d7bf68aeSBarry Smith   PetscErrorCode ierr;
239f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
240d7bf68aeSBarry Smith 
241d7bf68aeSBarry Smith   PetscFunctionBegin;
242171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2433194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
24482fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
245c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
246c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
247073dac72SJed 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);
248f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
249f9ba7244SBarry Smith     if (flg) {
250f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
251f9ba7244SBarry Smith     }
252f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_mat_type","Matrix type","MatSetType",MatList,typeName,typeName,sizeof typeName,&flg);CHKERRQ(ierr);
253073dac72SJed Brown     if (flg) {
254073dac72SJed Brown       ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
255f9ba7244SBarry Smith       ierr = PetscStrallocpy(typeName,&dm->mattype);CHKERRQ(ierr);
256073dac72SJed Brown     }
2571b89239cSHong 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);
258f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
259f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
260f9ba7244SBarry Smith     }
261f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
262f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
26382fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
26482fcb398SMatthew G Knepley   if (flg1) {
26582fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
26682fcb398SMatthew G Knepley   }
267c4721b0eSMatthew G Knepley   if (flg2) {
268c4721b0eSMatthew G Knepley     PetscViewer viewer;
269c4721b0eSMatthew G Knepley 
270c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
271c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
272c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
273c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
274c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
275c4721b0eSMatthew G Knepley   }
276c4721b0eSMatthew G Knepley   if (flg3) {
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_VTK);CHKERRQ(ierr);
282c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
283c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
284c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
285c4721b0eSMatthew G Knepley   }
286d7bf68aeSBarry Smith   PetscFunctionReturn(0);
287d7bf68aeSBarry Smith }
288d7bf68aeSBarry Smith 
289d7bf68aeSBarry Smith #undef __FUNCT__
29047c6ae99SBarry Smith #define __FUNCT__ "DMView"
291fc9bc008SSatish Balay /*@C
292aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
29347c6ae99SBarry Smith 
29447c6ae99SBarry Smith     Collective on DM
29547c6ae99SBarry Smith 
29647c6ae99SBarry Smith     Input Parameter:
29747c6ae99SBarry Smith +   dm - the DM object to view
29847c6ae99SBarry Smith -   v - the viewer
29947c6ae99SBarry Smith 
30047c6ae99SBarry Smith     Level: developer
30147c6ae99SBarry Smith 
302e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
30347c6ae99SBarry Smith 
30447c6ae99SBarry Smith @*/
3057087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
30647c6ae99SBarry Smith {
30747c6ae99SBarry Smith   PetscErrorCode ierr;
30847c6ae99SBarry Smith 
30947c6ae99SBarry Smith   PetscFunctionBegin;
310171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3113014e516SBarry Smith  if (!v) {
3123014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
3133014e516SBarry Smith   }
3140c010503SBarry Smith   if (dm->ops->view) {
3150c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
31647c6ae99SBarry Smith   }
31747c6ae99SBarry Smith   PetscFunctionReturn(0);
31847c6ae99SBarry Smith }
31947c6ae99SBarry Smith 
32047c6ae99SBarry Smith #undef __FUNCT__
32147c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
32247c6ae99SBarry Smith /*@
323aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
32447c6ae99SBarry Smith 
32547c6ae99SBarry Smith     Collective on DM
32647c6ae99SBarry Smith 
32747c6ae99SBarry Smith     Input Parameter:
32847c6ae99SBarry Smith .   dm - the DM object
32947c6ae99SBarry Smith 
33047c6ae99SBarry Smith     Output Parameter:
33147c6ae99SBarry Smith .   vec - the global vector
33247c6ae99SBarry Smith 
333073dac72SJed Brown     Level: beginner
33447c6ae99SBarry Smith 
335e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
33647c6ae99SBarry Smith 
33747c6ae99SBarry Smith @*/
3387087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
33947c6ae99SBarry Smith {
34047c6ae99SBarry Smith   PetscErrorCode ierr;
34147c6ae99SBarry Smith 
34247c6ae99SBarry Smith   PetscFunctionBegin;
343171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34447c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
34547c6ae99SBarry Smith   PetscFunctionReturn(0);
34647c6ae99SBarry Smith }
34747c6ae99SBarry Smith 
34847c6ae99SBarry Smith #undef __FUNCT__
34947c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
35047c6ae99SBarry Smith /*@
351aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
35247c6ae99SBarry Smith 
35347c6ae99SBarry Smith     Not Collective
35447c6ae99SBarry Smith 
35547c6ae99SBarry Smith     Input Parameter:
35647c6ae99SBarry Smith .   dm - the DM object
35747c6ae99SBarry Smith 
35847c6ae99SBarry Smith     Output Parameter:
35947c6ae99SBarry Smith .   vec - the local vector
36047c6ae99SBarry Smith 
361073dac72SJed Brown     Level: beginner
36247c6ae99SBarry Smith 
363e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
36447c6ae99SBarry Smith 
36547c6ae99SBarry Smith @*/
3667087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
36747c6ae99SBarry Smith {
36847c6ae99SBarry Smith   PetscErrorCode ierr;
36947c6ae99SBarry Smith 
37047c6ae99SBarry Smith   PetscFunctionBegin;
371171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
37247c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
37347c6ae99SBarry Smith   PetscFunctionReturn(0);
37447c6ae99SBarry Smith }
37547c6ae99SBarry Smith 
37647c6ae99SBarry Smith #undef __FUNCT__
3771411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
3781411c6eeSJed Brown /*@
3791411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
3801411c6eeSJed Brown 
3811411c6eeSJed Brown    Collective on DM
3821411c6eeSJed Brown 
3831411c6eeSJed Brown    Input Parameter:
3841411c6eeSJed Brown .  dm - the DM that provides the mapping
3851411c6eeSJed Brown 
3861411c6eeSJed Brown    Output Parameter:
3871411c6eeSJed Brown .  ltog - the mapping
3881411c6eeSJed Brown 
3891411c6eeSJed Brown    Level: intermediate
3901411c6eeSJed Brown 
3911411c6eeSJed Brown    Notes:
3921411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
3931411c6eeSJed Brown    MatSetLocalToGlobalMapping().
3941411c6eeSJed Brown 
3951411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
3961411c6eeSJed Brown @*/
3977087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
3981411c6eeSJed Brown {
3991411c6eeSJed Brown   PetscErrorCode ierr;
4001411c6eeSJed Brown 
4011411c6eeSJed Brown   PetscFunctionBegin;
4021411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4031411c6eeSJed Brown   PetscValidPointer(ltog,2);
4041411c6eeSJed Brown   if (!dm->ltogmap) {
4051411c6eeSJed Brown     if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
4061411c6eeSJed Brown     ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
4071411c6eeSJed Brown   }
4081411c6eeSJed Brown   *ltog = dm->ltogmap;
4091411c6eeSJed Brown   PetscFunctionReturn(0);
4101411c6eeSJed Brown }
4111411c6eeSJed Brown 
4121411c6eeSJed Brown #undef __FUNCT__
4131411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
4141411c6eeSJed Brown /*@
4151411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
4161411c6eeSJed Brown 
4171411c6eeSJed Brown    Collective on DM
4181411c6eeSJed Brown 
4191411c6eeSJed Brown    Input Parameter:
4201411c6eeSJed Brown .  da - the distributed array that provides the mapping
4211411c6eeSJed Brown 
4221411c6eeSJed Brown    Output Parameter:
4231411c6eeSJed Brown .  ltog - the block mapping
4241411c6eeSJed Brown 
4251411c6eeSJed Brown    Level: intermediate
4261411c6eeSJed Brown 
4271411c6eeSJed Brown    Notes:
4281411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
4291411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
4301411c6eeSJed Brown 
4311411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
4321411c6eeSJed Brown @*/
4337087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
4341411c6eeSJed Brown {
4351411c6eeSJed Brown   PetscErrorCode ierr;
4361411c6eeSJed Brown 
4371411c6eeSJed Brown   PetscFunctionBegin;
4381411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4391411c6eeSJed Brown   PetscValidPointer(ltog,2);
4401411c6eeSJed Brown   if (!dm->ltogmapb) {
4411411c6eeSJed Brown     PetscInt bs;
4421411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
4431411c6eeSJed Brown     if (bs > 1) {
4441411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
4451411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
4461411c6eeSJed Brown     } else {
4471411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
4481411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
4491411c6eeSJed Brown     }
4501411c6eeSJed Brown   }
4511411c6eeSJed Brown   *ltog = dm->ltogmapb;
4521411c6eeSJed Brown   PetscFunctionReturn(0);
4531411c6eeSJed Brown }
4541411c6eeSJed Brown 
4551411c6eeSJed Brown #undef __FUNCT__
4561411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
4571411c6eeSJed Brown /*@
4581411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
4591411c6eeSJed Brown 
4601411c6eeSJed Brown    Not Collective
4611411c6eeSJed Brown 
4621411c6eeSJed Brown    Input Parameter:
4631411c6eeSJed Brown .  dm - the DM with block structure
4641411c6eeSJed Brown 
4651411c6eeSJed Brown    Output Parameter:
4661411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
4671411c6eeSJed Brown 
4681411c6eeSJed Brown    Level: intermediate
4691411c6eeSJed Brown 
4701411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
4711411c6eeSJed Brown @*/
4727087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
4731411c6eeSJed Brown {
4741411c6eeSJed Brown   PetscFunctionBegin;
4751411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4761411c6eeSJed Brown   PetscValidPointer(bs,2);
4771411c6eeSJed 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");
4781411c6eeSJed Brown   *bs = dm->bs;
4791411c6eeSJed Brown   PetscFunctionReturn(0);
4801411c6eeSJed Brown }
4811411c6eeSJed Brown 
4821411c6eeSJed Brown #undef __FUNCT__
483e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
48447c6ae99SBarry Smith /*@
485e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
48647c6ae99SBarry Smith 
48747c6ae99SBarry Smith     Collective on DM
48847c6ae99SBarry Smith 
48947c6ae99SBarry Smith     Input Parameter:
49047c6ae99SBarry Smith +   dm1 - the DM object
49147c6ae99SBarry Smith -   dm2 - the second, finer DM object
49247c6ae99SBarry Smith 
49347c6ae99SBarry Smith     Output Parameter:
49447c6ae99SBarry Smith +  mat - the interpolation
49547c6ae99SBarry Smith -  vec - the scaling (optional)
49647c6ae99SBarry Smith 
49747c6ae99SBarry Smith     Level: developer
49847c6ae99SBarry Smith 
49985afcc9aSBarry 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
50085afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
501d52bd9f3SBarry Smith 
502d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
503d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
50485afcc9aSBarry Smith 
50585afcc9aSBarry Smith 
506e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
50747c6ae99SBarry Smith 
50847c6ae99SBarry Smith @*/
509e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
51047c6ae99SBarry Smith {
51147c6ae99SBarry Smith   PetscErrorCode ierr;
51247c6ae99SBarry Smith 
51347c6ae99SBarry Smith   PetscFunctionBegin;
514171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
515171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
51625296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
51747c6ae99SBarry Smith   PetscFunctionReturn(0);
51847c6ae99SBarry Smith }
51947c6ae99SBarry Smith 
52047c6ae99SBarry Smith #undef __FUNCT__
521e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
52247c6ae99SBarry Smith /*@
523e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
52447c6ae99SBarry Smith 
52547c6ae99SBarry Smith     Collective on DM
52647c6ae99SBarry Smith 
52747c6ae99SBarry Smith     Input Parameter:
52847c6ae99SBarry Smith +   dm1 - the DM object
52947c6ae99SBarry Smith -   dm2 - the second, finer DM object
53047c6ae99SBarry Smith 
53147c6ae99SBarry Smith     Output Parameter:
53247c6ae99SBarry Smith .   ctx - the injection
53347c6ae99SBarry Smith 
53447c6ae99SBarry Smith     Level: developer
53547c6ae99SBarry Smith 
53685afcc9aSBarry 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
53785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
53885afcc9aSBarry Smith 
539e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
54047c6ae99SBarry Smith 
54147c6ae99SBarry Smith @*/
542e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
54347c6ae99SBarry Smith {
54447c6ae99SBarry Smith   PetscErrorCode ierr;
54547c6ae99SBarry Smith 
54647c6ae99SBarry Smith   PetscFunctionBegin;
547171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
548171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
54947c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
55047c6ae99SBarry Smith   PetscFunctionReturn(0);
55147c6ae99SBarry Smith }
55247c6ae99SBarry Smith 
55347c6ae99SBarry Smith #undef __FUNCT__
554e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
555d1e2c406SBarry Smith /*@C
556e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
55747c6ae99SBarry Smith 
55847c6ae99SBarry Smith     Collective on DM
55947c6ae99SBarry Smith 
56047c6ae99SBarry Smith     Input Parameter:
56147c6ae99SBarry Smith +   dm - the DM object
56247c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
56347c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
56447c6ae99SBarry Smith 
56547c6ae99SBarry Smith     Output Parameter:
56647c6ae99SBarry Smith .   coloring - the coloring
56747c6ae99SBarry Smith 
56847c6ae99SBarry Smith     Level: developer
56947c6ae99SBarry Smith 
570e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
57147c6ae99SBarry Smith 
572aab9d709SJed Brown @*/
573e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
57447c6ae99SBarry Smith {
57547c6ae99SBarry Smith   PetscErrorCode ierr;
57647c6ae99SBarry Smith 
57747c6ae99SBarry Smith   PetscFunctionBegin;
578171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57947c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
58047c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
58147c6ae99SBarry Smith   PetscFunctionReturn(0);
58247c6ae99SBarry Smith }
58347c6ae99SBarry Smith 
58447c6ae99SBarry Smith #undef __FUNCT__
585950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
58647c6ae99SBarry Smith /*@C
587950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
58847c6ae99SBarry Smith 
58947c6ae99SBarry Smith     Collective on DM
59047c6ae99SBarry Smith 
59147c6ae99SBarry Smith     Input Parameter:
59247c6ae99SBarry Smith +   dm - the DM object
59347c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
59494013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
59547c6ae99SBarry Smith 
59647c6ae99SBarry Smith     Output Parameter:
59747c6ae99SBarry Smith .   mat - the empty Jacobian
59847c6ae99SBarry Smith 
599073dac72SJed Brown     Level: beginner
60047c6ae99SBarry Smith 
60194013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
60294013140SBarry Smith        do not need to do it yourself.
60394013140SBarry Smith 
60494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
605aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
60694013140SBarry Smith 
60794013140SBarry 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
60894013140SBarry Smith        internally by PETSc.
60994013140SBarry Smith 
61094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
611aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
61294013140SBarry Smith 
613e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
61447c6ae99SBarry Smith 
615aab9d709SJed Brown @*/
616950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
61747c6ae99SBarry Smith {
61847c6ae99SBarry Smith   PetscErrorCode ierr;
61947c6ae99SBarry Smith 
62047c6ae99SBarry Smith   PetscFunctionBegin;
621171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
622235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
623235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
624235683edSBarry Smith #endif
625c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
626c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
627073dac72SJed Brown   if (dm->mattype) {
62825296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
629073dac72SJed Brown   } else {
63025296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
631c7b7c8a4SJed Brown   }
63247c6ae99SBarry Smith   PetscFunctionReturn(0);
63347c6ae99SBarry Smith }
63447c6ae99SBarry Smith 
63547c6ae99SBarry Smith #undef __FUNCT__
636732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
637732e2eb9SMatthew G Knepley /*@
638950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
639732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
640732e2eb9SMatthew G Knepley 
641732e2eb9SMatthew G Knepley   Logically Collective on DMDA
642732e2eb9SMatthew G Knepley 
643732e2eb9SMatthew G Knepley   Input Parameter:
644732e2eb9SMatthew G Knepley + dm - the DM
645732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
646732e2eb9SMatthew G Knepley 
647732e2eb9SMatthew G Knepley   Level: developer
648950540a4SJed Brown .seealso DMCreateMatrix()
649732e2eb9SMatthew G Knepley @*/
650732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
651732e2eb9SMatthew G Knepley {
652732e2eb9SMatthew G Knepley   PetscFunctionBegin;
653732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
654732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
655732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
656732e2eb9SMatthew G Knepley }
657732e2eb9SMatthew G Knepley 
658732e2eb9SMatthew G Knepley #undef __FUNCT__
659a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
660a89ea682SMatthew G Knepley /*@C
661a89ea682SMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size
662a89ea682SMatthew G Knepley 
663a89ea682SMatthew G Knepley   Not Collective
664a89ea682SMatthew G Knepley 
665a89ea682SMatthew G Knepley   Input Parameters:
666a89ea682SMatthew G Knepley + dm - the DM object
667a89ea682SMatthew G Knepley - size - The minium size
668a89ea682SMatthew G Knepley 
669a89ea682SMatthew G Knepley   Output Parameter:
670a89ea682SMatthew G Knepley . array - the work array
671a89ea682SMatthew G Knepley 
672a89ea682SMatthew G Knepley   Level: developer
673a89ea682SMatthew G Knepley 
674a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
675a89ea682SMatthew G Knepley @*/
676a89ea682SMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt size,PetscScalar **array)
677a89ea682SMatthew G Knepley {
678a89ea682SMatthew G Knepley   PetscErrorCode ierr;
679a89ea682SMatthew G Knepley 
680a89ea682SMatthew G Knepley   PetscFunctionBegin;
681a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
682a89ea682SMatthew G Knepley   PetscValidPointer(array,3);
683a89ea682SMatthew G Knepley   if (size > dm->workSize) {
684a89ea682SMatthew G Knepley     dm->workSize = size;
685a89ea682SMatthew G Knepley     ierr = PetscFree(dm->workArray);CHKERRQ(ierr);
686a89ea682SMatthew G Knepley     ierr = PetscMalloc(dm->workSize * sizeof(PetscScalar), &dm->workArray);CHKERRQ(ierr);
687a89ea682SMatthew G Knepley   }
688a89ea682SMatthew G Knepley   *array = dm->workArray;
689a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
690a89ea682SMatthew G Knepley }
691a89ea682SMatthew G Knepley 
6925fe1f584SPeter Brune 
693a89ea682SMatthew G Knepley #undef __FUNCT__
69447c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
69547c6ae99SBarry Smith /*@
69647c6ae99SBarry Smith     DMRefine - Refines a DM object
69747c6ae99SBarry Smith 
69847c6ae99SBarry Smith     Collective on DM
69947c6ae99SBarry Smith 
70047c6ae99SBarry Smith     Input Parameter:
70147c6ae99SBarry Smith +   dm - the DM object
70247c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
70347c6ae99SBarry Smith 
70447c6ae99SBarry Smith     Output Parameter:
70547c6ae99SBarry Smith .   dmf - the refined DM
70647c6ae99SBarry Smith 
70747c6ae99SBarry Smith     Level: developer
70847c6ae99SBarry Smith 
709e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
71047c6ae99SBarry Smith 
71147c6ae99SBarry Smith @*/
7127087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
71347c6ae99SBarry Smith {
71447c6ae99SBarry Smith   PetscErrorCode ierr;
71547c6ae99SBarry Smith 
71647c6ae99SBarry Smith   PetscFunctionBegin;
717732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
71847c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
7194057135bSMatthew G Knepley   if (*dmf) {
720644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
721644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
722644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
723644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
724644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
725644e2e5bSBarry Smith     }
7268cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
727644e2e5bSBarry Smith     (*dmf)->ctx     = dm->ctx;
728656b349aSBarry Smith     (*dmf)->levelup = dm->levelup + 1;
7294057135bSMatthew G Knepley   }
73047c6ae99SBarry Smith   PetscFunctionReturn(0);
73147c6ae99SBarry Smith }
73247c6ae99SBarry Smith 
73347c6ae99SBarry Smith #undef __FUNCT__
734eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
735eb3f98d2SBarry Smith /*@
736eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
737eb3f98d2SBarry Smith 
738eb3f98d2SBarry Smith     Not Collective
739eb3f98d2SBarry Smith 
740eb3f98d2SBarry Smith     Input Parameter:
741eb3f98d2SBarry Smith .   dm - the DM object
742eb3f98d2SBarry Smith 
743eb3f98d2SBarry Smith     Output Parameter:
744eb3f98d2SBarry Smith .   level - number of refinements
745eb3f98d2SBarry Smith 
746eb3f98d2SBarry Smith     Level: developer
747eb3f98d2SBarry Smith 
7486a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
749eb3f98d2SBarry Smith 
750eb3f98d2SBarry Smith @*/
751eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
752eb3f98d2SBarry Smith {
753eb3f98d2SBarry Smith   PetscFunctionBegin;
754eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
755eb3f98d2SBarry Smith   *level = dm->levelup;
756eb3f98d2SBarry Smith   PetscFunctionReturn(0);
757eb3f98d2SBarry Smith }
758eb3f98d2SBarry Smith 
759eb3f98d2SBarry Smith #undef __FUNCT__
76047c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
76147c6ae99SBarry Smith /*@
76247c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
76347c6ae99SBarry Smith 
76447c6ae99SBarry Smith     Neighbor-wise Collective on DM
76547c6ae99SBarry Smith 
76647c6ae99SBarry Smith     Input Parameters:
76747c6ae99SBarry Smith +   dm - the DM object
76847c6ae99SBarry Smith .   g - the global vector
76947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
77047c6ae99SBarry Smith -   l - the local vector
77147c6ae99SBarry Smith 
77247c6ae99SBarry Smith 
77347c6ae99SBarry Smith     Level: beginner
77447c6ae99SBarry Smith 
775e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
77647c6ae99SBarry Smith 
77747c6ae99SBarry Smith @*/
7787087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
77947c6ae99SBarry Smith {
78047c6ae99SBarry Smith   PetscErrorCode ierr;
78147c6ae99SBarry Smith 
78247c6ae99SBarry Smith   PetscFunctionBegin;
783171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
784843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
78547c6ae99SBarry Smith   PetscFunctionReturn(0);
78647c6ae99SBarry Smith }
78747c6ae99SBarry Smith 
78847c6ae99SBarry Smith #undef __FUNCT__
78947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
79047c6ae99SBarry Smith /*@
79147c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
79247c6ae99SBarry Smith 
79347c6ae99SBarry Smith     Neighbor-wise Collective on DM
79447c6ae99SBarry Smith 
79547c6ae99SBarry Smith     Input Parameters:
79647c6ae99SBarry Smith +   dm - the DM object
79747c6ae99SBarry Smith .   g - the global vector
79847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
79947c6ae99SBarry Smith -   l - the local vector
80047c6ae99SBarry Smith 
80147c6ae99SBarry Smith 
80247c6ae99SBarry Smith     Level: beginner
80347c6ae99SBarry Smith 
804e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
80547c6ae99SBarry Smith 
80647c6ae99SBarry Smith @*/
8077087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
80847c6ae99SBarry Smith {
80947c6ae99SBarry Smith   PetscErrorCode ierr;
81047c6ae99SBarry Smith 
81147c6ae99SBarry Smith   PetscFunctionBegin;
812171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
813843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
81447c6ae99SBarry Smith   PetscFunctionReturn(0);
81547c6ae99SBarry Smith }
81647c6ae99SBarry Smith 
81747c6ae99SBarry Smith #undef __FUNCT__
8189a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
81947c6ae99SBarry Smith /*@
8209a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
8219a42bb27SBarry Smith 
8229a42bb27SBarry Smith     Neighbor-wise Collective on DM
8239a42bb27SBarry Smith 
8249a42bb27SBarry Smith     Input Parameters:
8259a42bb27SBarry Smith +   dm - the DM object
826f6813fd5SJed Brown .   l - the local vector
8279a42bb27SBarry 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
8289a42bb27SBarry Smith            base point.
829f6813fd5SJed Brown - - the global vector
8309a42bb27SBarry Smith 
8319a42bb27SBarry 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
8329a42bb27SBarry 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
8339a42bb27SBarry Smith            global array to the final global array with VecAXPY().
8349a42bb27SBarry Smith 
8359a42bb27SBarry Smith     Level: beginner
8369a42bb27SBarry Smith 
837e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
8389a42bb27SBarry Smith 
8399a42bb27SBarry Smith @*/
8407087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
8419a42bb27SBarry Smith {
8429a42bb27SBarry Smith   PetscErrorCode ierr;
8439a42bb27SBarry Smith 
8449a42bb27SBarry Smith   PetscFunctionBegin;
845171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
846843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
8479a42bb27SBarry Smith   PetscFunctionReturn(0);
8489a42bb27SBarry Smith }
8499a42bb27SBarry Smith 
8509a42bb27SBarry Smith #undef __FUNCT__
8519a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
8529a42bb27SBarry Smith /*@
8539a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
85447c6ae99SBarry Smith 
85547c6ae99SBarry Smith     Neighbor-wise Collective on DM
85647c6ae99SBarry Smith 
85747c6ae99SBarry Smith     Input Parameters:
85847c6ae99SBarry Smith +   dm - the DM object
859f6813fd5SJed Brown .   l - the local vector
86047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
861f6813fd5SJed Brown -   g - the global vector
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith 
86447c6ae99SBarry Smith     Level: beginner
86547c6ae99SBarry Smith 
866e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith @*/
8697087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
87047c6ae99SBarry Smith {
87147c6ae99SBarry Smith   PetscErrorCode ierr;
87247c6ae99SBarry Smith 
87347c6ae99SBarry Smith   PetscFunctionBegin;
874171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
875843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
87647c6ae99SBarry Smith   PetscFunctionReturn(0);
87747c6ae99SBarry Smith }
87847c6ae99SBarry Smith 
87947c6ae99SBarry Smith #undef __FUNCT__
88047c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
88147c6ae99SBarry Smith /*@
88247c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
88347c6ae99SBarry Smith 
88447c6ae99SBarry Smith     Collective on DM
88547c6ae99SBarry Smith 
88647c6ae99SBarry Smith     Input Parameter:
88747c6ae99SBarry Smith +   dm - the DM object
88847c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
88947c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
89047c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
89147c6ae99SBarry Smith 
89247c6ae99SBarry Smith     Level: developer
89347c6ae99SBarry Smith 
894e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
89547c6ae99SBarry Smith          DMSetFunction()
89647c6ae99SBarry Smith 
89747c6ae99SBarry Smith @*/
8987087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
89947c6ae99SBarry Smith {
90047c6ae99SBarry Smith   PetscErrorCode ierr;
901171400e9SBarry Smith 
90247c6ae99SBarry Smith   PetscFunctionBegin;
903171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
90447c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
90547c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
90647c6ae99SBarry Smith   if (A != B) {
90747c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
90847c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
90947c6ae99SBarry Smith   }
91047c6ae99SBarry Smith   PetscFunctionReturn(0);
91147c6ae99SBarry Smith }
91247c6ae99SBarry Smith 
91347c6ae99SBarry Smith #undef __FUNCT__
91447c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
91547c6ae99SBarry Smith /*@
91647c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
91747c6ae99SBarry Smith 
91847c6ae99SBarry Smith     Collective on DM
91947c6ae99SBarry Smith 
92047c6ae99SBarry Smith     Input Parameter:
92147c6ae99SBarry Smith +   dm - the DM object
92247c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
92347c6ae99SBarry Smith 
92447c6ae99SBarry Smith     Output Parameter:
92547c6ae99SBarry Smith .   dmc - the coarsened DM
92647c6ae99SBarry Smith 
92747c6ae99SBarry Smith     Level: developer
92847c6ae99SBarry Smith 
929e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
93047c6ae99SBarry Smith 
93147c6ae99SBarry Smith @*/
9327087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
93347c6ae99SBarry Smith {
93447c6ae99SBarry Smith   PetscErrorCode ierr;
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith   PetscFunctionBegin;
937171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
93847c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
93947c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
94047c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
94147c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
94247c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
94347c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
94447c6ae99SBarry Smith   }
9458cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
946644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
947656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
94847c6ae99SBarry Smith   PetscFunctionReturn(0);
94947c6ae99SBarry Smith }
95047c6ae99SBarry Smith 
95147c6ae99SBarry Smith #undef __FUNCT__
9525fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
9535fe1f584SPeter Brune /*@
9546a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
9555fe1f584SPeter Brune 
9565fe1f584SPeter Brune     Not Collective
9575fe1f584SPeter Brune 
9585fe1f584SPeter Brune     Input Parameter:
9595fe1f584SPeter Brune .   dm - the DM object
9605fe1f584SPeter Brune 
9615fe1f584SPeter Brune     Output Parameter:
9626a7d9d85SPeter Brune .   level - number of coarsenings
9635fe1f584SPeter Brune 
9645fe1f584SPeter Brune     Level: developer
9655fe1f584SPeter Brune 
9666a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
9675fe1f584SPeter Brune 
9685fe1f584SPeter Brune @*/
9695fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
9705fe1f584SPeter Brune {
9715fe1f584SPeter Brune   PetscFunctionBegin;
9725fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9735fe1f584SPeter Brune   *level = dm->leveldown;
9745fe1f584SPeter Brune   PetscFunctionReturn(0);
9755fe1f584SPeter Brune }
9765fe1f584SPeter Brune 
9775fe1f584SPeter Brune 
9785fe1f584SPeter Brune 
9795fe1f584SPeter Brune #undef __FUNCT__
98047c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
98147c6ae99SBarry Smith /*@C
98247c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
98347c6ae99SBarry Smith 
98447c6ae99SBarry Smith     Collective on DM
98547c6ae99SBarry Smith 
98647c6ae99SBarry Smith     Input Parameter:
98747c6ae99SBarry Smith +   dm - the DM object
98847c6ae99SBarry Smith -   nlevels - the number of levels of refinement
98947c6ae99SBarry Smith 
99047c6ae99SBarry Smith     Output Parameter:
99147c6ae99SBarry Smith .   dmf - the refined DM hierarchy
99247c6ae99SBarry Smith 
99347c6ae99SBarry Smith     Level: developer
99447c6ae99SBarry Smith 
995e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
99647c6ae99SBarry Smith 
99747c6ae99SBarry Smith @*/
9987087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
99947c6ae99SBarry Smith {
100047c6ae99SBarry Smith   PetscErrorCode ierr;
100147c6ae99SBarry Smith 
100247c6ae99SBarry Smith   PetscFunctionBegin;
1003171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
100447c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
100547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
100647c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
100747c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
100847c6ae99SBarry Smith   } else if (dm->ops->refine) {
100947c6ae99SBarry Smith     PetscInt i;
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
101247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
101347c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
101447c6ae99SBarry Smith     }
101547c6ae99SBarry Smith   } else {
101647c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
101747c6ae99SBarry Smith   }
101847c6ae99SBarry Smith   PetscFunctionReturn(0);
101947c6ae99SBarry Smith }
102047c6ae99SBarry Smith 
102147c6ae99SBarry Smith #undef __FUNCT__
102247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
102347c6ae99SBarry Smith /*@C
102447c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
102547c6ae99SBarry Smith 
102647c6ae99SBarry Smith     Collective on DM
102747c6ae99SBarry Smith 
102847c6ae99SBarry Smith     Input Parameter:
102947c6ae99SBarry Smith +   dm - the DM object
103047c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
103147c6ae99SBarry Smith 
103247c6ae99SBarry Smith     Output Parameter:
103347c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
103447c6ae99SBarry Smith 
103547c6ae99SBarry Smith     Level: developer
103647c6ae99SBarry Smith 
1037e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
103847c6ae99SBarry Smith 
103947c6ae99SBarry Smith @*/
10407087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
104147c6ae99SBarry Smith {
104247c6ae99SBarry Smith   PetscErrorCode ierr;
104347c6ae99SBarry Smith 
104447c6ae99SBarry Smith   PetscFunctionBegin;
1045171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
104647c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
104747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
104847c6ae99SBarry Smith   PetscValidPointer(dmc,3);
104947c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
105047c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
105147c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
105247c6ae99SBarry Smith     PetscInt i;
105347c6ae99SBarry Smith 
105447c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
105547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
105647c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
105747c6ae99SBarry Smith     }
105847c6ae99SBarry Smith   } else {
105947c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
106047c6ae99SBarry Smith   }
106147c6ae99SBarry Smith   PetscFunctionReturn(0);
106247c6ae99SBarry Smith }
106347c6ae99SBarry Smith 
106447c6ae99SBarry Smith #undef __FUNCT__
1065e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
106647c6ae99SBarry Smith /*@
1067e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
106847c6ae99SBarry Smith    grids associated with two DMs.
106947c6ae99SBarry Smith 
107047c6ae99SBarry Smith    Collective on DM
107147c6ae99SBarry Smith 
107247c6ae99SBarry Smith    Input Parameters:
107347c6ae99SBarry Smith +  dmc - the coarse grid DM
107447c6ae99SBarry Smith -  dmf - the fine grid DM
107547c6ae99SBarry Smith 
107647c6ae99SBarry Smith    Output Parameters:
107747c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
107847c6ae99SBarry Smith 
107947c6ae99SBarry Smith    Level: intermediate
108047c6ae99SBarry Smith 
108147c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
108247c6ae99SBarry Smith 
1083e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
108447c6ae99SBarry Smith @*/
1085e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
108647c6ae99SBarry Smith {
108747c6ae99SBarry Smith   PetscErrorCode ierr;
108847c6ae99SBarry Smith 
108947c6ae99SBarry Smith   PetscFunctionBegin;
1090171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1091171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
109247c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
109347c6ae99SBarry Smith   PetscFunctionReturn(0);
109447c6ae99SBarry Smith }
109547c6ae99SBarry Smith 
109647c6ae99SBarry Smith #undef __FUNCT__
10971a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
10981a266240SBarry Smith /*@C
10991a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
11001a266240SBarry Smith 
11011a266240SBarry Smith     Not Collective
11021a266240SBarry Smith 
11031a266240SBarry Smith     Input Parameters:
11041a266240SBarry Smith +   dm - the DM object
11051a266240SBarry Smith -   destroy - the destroy function
11061a266240SBarry Smith 
11071a266240SBarry Smith     Level: intermediate
11081a266240SBarry Smith 
1109e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
11101a266240SBarry Smith 
1111f07f9ceaSJed Brown @*/
11121a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
11131a266240SBarry Smith {
11141a266240SBarry Smith   PetscFunctionBegin;
1115171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11161a266240SBarry Smith   dm->ctxdestroy = destroy;
11171a266240SBarry Smith   PetscFunctionReturn(0);
11181a266240SBarry Smith }
11191a266240SBarry Smith 
11201a266240SBarry Smith #undef __FUNCT__
11211b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1122b07ff414SBarry Smith /*@
11231b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
112447c6ae99SBarry Smith 
112547c6ae99SBarry Smith     Not Collective
112647c6ae99SBarry Smith 
112747c6ae99SBarry Smith     Input Parameters:
112847c6ae99SBarry Smith +   dm - the DM object
112947c6ae99SBarry Smith -   ctx - the user context
113047c6ae99SBarry Smith 
113147c6ae99SBarry Smith     Level: intermediate
113247c6ae99SBarry Smith 
1133e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
113447c6ae99SBarry Smith 
113547c6ae99SBarry Smith @*/
11361b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
113747c6ae99SBarry Smith {
113847c6ae99SBarry Smith   PetscFunctionBegin;
1139171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
114047c6ae99SBarry Smith   dm->ctx = ctx;
114147c6ae99SBarry Smith   PetscFunctionReturn(0);
114247c6ae99SBarry Smith }
114347c6ae99SBarry Smith 
114447c6ae99SBarry Smith #undef __FUNCT__
11451b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
114647c6ae99SBarry Smith /*@
11471b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
114847c6ae99SBarry Smith 
114947c6ae99SBarry Smith     Not Collective
115047c6ae99SBarry Smith 
115147c6ae99SBarry Smith     Input Parameter:
115247c6ae99SBarry Smith .   dm - the DM object
115347c6ae99SBarry Smith 
115447c6ae99SBarry Smith     Output Parameter:
115547c6ae99SBarry Smith .   ctx - the user context
115647c6ae99SBarry Smith 
115747c6ae99SBarry Smith     Level: intermediate
115847c6ae99SBarry Smith 
1159e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
116047c6ae99SBarry Smith 
116147c6ae99SBarry Smith @*/
11621b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
116347c6ae99SBarry Smith {
116447c6ae99SBarry Smith   PetscFunctionBegin;
1165171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11661b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
116747c6ae99SBarry Smith   PetscFunctionReturn(0);
116847c6ae99SBarry Smith }
116947c6ae99SBarry Smith 
117047c6ae99SBarry Smith #undef __FUNCT__
117147c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
11727e833e3aSBarry Smith /*@C
117347c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
117447c6ae99SBarry Smith 
117547c6ae99SBarry Smith     Logically Collective on DM
117647c6ae99SBarry Smith 
117747c6ae99SBarry Smith     Input Parameter:
117847c6ae99SBarry Smith +   dm - the DM object to destroy
117947c6ae99SBarry Smith -   f - the function to compute the initial guess
118047c6ae99SBarry Smith 
118147c6ae99SBarry Smith     Level: intermediate
118247c6ae99SBarry Smith 
1183e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
118447c6ae99SBarry Smith 
1185f07f9ceaSJed Brown @*/
11867087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
118747c6ae99SBarry Smith {
118847c6ae99SBarry Smith   PetscFunctionBegin;
1189171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
119047c6ae99SBarry Smith   dm->ops->initialguess = f;
119147c6ae99SBarry Smith   PetscFunctionReturn(0);
119247c6ae99SBarry Smith }
119347c6ae99SBarry Smith 
119447c6ae99SBarry Smith #undef __FUNCT__
119547c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
11967e833e3aSBarry Smith /*@C
119747c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
119847c6ae99SBarry Smith 
119947c6ae99SBarry Smith     Logically Collective on DM
120047c6ae99SBarry Smith 
120147c6ae99SBarry Smith     Input Parameter:
120247c6ae99SBarry Smith +   dm - the DM object
120347c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
120447c6ae99SBarry Smith 
120547c6ae99SBarry Smith     Level: intermediate
120647c6ae99SBarry Smith 
120747c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
120847c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
120947c6ae99SBarry Smith 
1210e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
121147c6ae99SBarry Smith          DMSetJacobian()
121247c6ae99SBarry Smith 
1213f07f9ceaSJed Brown @*/
12147087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
121547c6ae99SBarry Smith {
121647c6ae99SBarry Smith   PetscFunctionBegin;
1217171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
121847c6ae99SBarry Smith   dm->ops->function = f;
121947c6ae99SBarry Smith   if (f) {
122047c6ae99SBarry Smith     dm->ops->functionj = f;
122147c6ae99SBarry Smith   }
122247c6ae99SBarry Smith   PetscFunctionReturn(0);
122347c6ae99SBarry Smith }
122447c6ae99SBarry Smith 
122547c6ae99SBarry Smith #undef __FUNCT__
122647c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
12277e833e3aSBarry Smith /*@C
122847c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
122947c6ae99SBarry Smith 
123047c6ae99SBarry Smith     Logically Collective on DM
123147c6ae99SBarry Smith 
123247c6ae99SBarry Smith     Input Parameter:
123347c6ae99SBarry Smith +   dm - the DM object to destroy
123447c6ae99SBarry Smith -   f - the function to compute the matrix entries
123547c6ae99SBarry Smith 
123647c6ae99SBarry Smith     Level: intermediate
123747c6ae99SBarry Smith 
1238e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
123947c6ae99SBarry Smith          DMSetFunction()
124047c6ae99SBarry Smith 
1241f07f9ceaSJed Brown @*/
12427087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
124347c6ae99SBarry Smith {
124447c6ae99SBarry Smith   PetscFunctionBegin;
1245171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
124647c6ae99SBarry Smith   dm->ops->jacobian = f;
124747c6ae99SBarry Smith   PetscFunctionReturn(0);
124847c6ae99SBarry Smith }
124947c6ae99SBarry Smith 
125047c6ae99SBarry Smith #undef __FUNCT__
125147c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
125247c6ae99SBarry Smith /*@
125347c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
125447c6ae99SBarry Smith 
125547c6ae99SBarry Smith     Collective on DM
125647c6ae99SBarry Smith 
125747c6ae99SBarry Smith     Input Parameter:
125847c6ae99SBarry Smith +   dm - the DM object to destroy
125947c6ae99SBarry Smith -   x - the vector to hold the initial guess values
126047c6ae99SBarry Smith 
126147c6ae99SBarry Smith     Level: developer
126247c6ae99SBarry Smith 
1263e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
126447c6ae99SBarry Smith 
126547c6ae99SBarry Smith @*/
12667087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
126747c6ae99SBarry Smith {
126847c6ae99SBarry Smith   PetscErrorCode ierr;
126947c6ae99SBarry Smith   PetscFunctionBegin;
1270171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
127147c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
127247c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
127347c6ae99SBarry Smith   PetscFunctionReturn(0);
127447c6ae99SBarry Smith }
127547c6ae99SBarry Smith 
127647c6ae99SBarry Smith #undef __FUNCT__
127747c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
127847c6ae99SBarry Smith /*@
127947c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
128047c6ae99SBarry Smith 
128147c6ae99SBarry Smith     Not Collective
128247c6ae99SBarry Smith 
128347c6ae99SBarry Smith     Input Parameter:
128447c6ae99SBarry Smith .   dm - the DM object to destroy
128547c6ae99SBarry Smith 
128647c6ae99SBarry Smith     Output Parameter:
128747c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
128847c6ae99SBarry Smith 
128947c6ae99SBarry Smith     Level: developer
129047c6ae99SBarry Smith 
1291e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
129247c6ae99SBarry Smith 
129347c6ae99SBarry Smith @*/
12947087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
129547c6ae99SBarry Smith {
129647c6ae99SBarry Smith   PetscFunctionBegin;
1297171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
129847c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
129947c6ae99SBarry Smith   PetscFunctionReturn(0);
130047c6ae99SBarry Smith }
130147c6ae99SBarry Smith 
130247c6ae99SBarry Smith #undef __FUNCT__
130347c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
130447c6ae99SBarry Smith /*@
130547c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
130647c6ae99SBarry Smith 
130747c6ae99SBarry Smith     Not Collective
130847c6ae99SBarry Smith 
130947c6ae99SBarry Smith     Input Parameter:
131047c6ae99SBarry Smith .   dm - the DM object to destroy
131147c6ae99SBarry Smith 
131247c6ae99SBarry Smith     Output Parameter:
131347c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
131447c6ae99SBarry Smith 
131547c6ae99SBarry Smith     Level: developer
131647c6ae99SBarry Smith 
1317e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
131847c6ae99SBarry Smith 
131947c6ae99SBarry Smith @*/
13207087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
132147c6ae99SBarry Smith {
132247c6ae99SBarry Smith   PetscFunctionBegin;
1323171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
132447c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
132547c6ae99SBarry Smith   PetscFunctionReturn(0);
132647c6ae99SBarry Smith }
132747c6ae99SBarry Smith 
132847c6ae99SBarry Smith #undef __FUNCT__
132947c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
133047c6ae99SBarry Smith /*@
133147c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
133247c6ae99SBarry Smith 
133347c6ae99SBarry Smith     Not Collective
133447c6ae99SBarry Smith 
133547c6ae99SBarry Smith     Input Parameter:
133647c6ae99SBarry Smith .   dm - the DM object to destroy
133747c6ae99SBarry Smith 
133847c6ae99SBarry Smith     Output Parameter:
133947c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
134047c6ae99SBarry Smith 
134147c6ae99SBarry Smith     Level: developer
134247c6ae99SBarry Smith 
1343e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
134447c6ae99SBarry Smith 
134547c6ae99SBarry Smith @*/
13467087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
134747c6ae99SBarry Smith {
134847c6ae99SBarry Smith   PetscFunctionBegin;
1349171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
135047c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
135147c6ae99SBarry Smith   PetscFunctionReturn(0);
135247c6ae99SBarry Smith }
135347c6ae99SBarry Smith 
135447c6ae99SBarry Smith #undef __FUNCT__
135547c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
135647c6ae99SBarry Smith /*@
135747c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
135847c6ae99SBarry Smith 
135947c6ae99SBarry Smith     Collective on DM
136047c6ae99SBarry Smith 
136147c6ae99SBarry Smith     Input Parameter:
136247c6ae99SBarry Smith +   dm - the DM object to destroy
136347c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
136447c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
136547c6ae99SBarry Smith 
136647c6ae99SBarry Smith     Level: developer
136747c6ae99SBarry Smith 
1368e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
136947c6ae99SBarry Smith          DMSetJacobian()
137047c6ae99SBarry Smith 
137147c6ae99SBarry Smith @*/
13727087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
137347c6ae99SBarry Smith {
137447c6ae99SBarry Smith   PetscErrorCode ierr;
137547c6ae99SBarry Smith   PetscFunctionBegin;
1376171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
137747c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
1378644e2e5bSBarry Smith   PetscStackPush("DM user function");
137947c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
1380644e2e5bSBarry Smith   PetscStackPop;
138147c6ae99SBarry Smith   PetscFunctionReturn(0);
138247c6ae99SBarry Smith }
138347c6ae99SBarry Smith 
138447c6ae99SBarry Smith 
138547c6ae99SBarry Smith #undef __FUNCT__
138647c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
138747c6ae99SBarry Smith /*@
138847c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
138947c6ae99SBarry Smith 
139047c6ae99SBarry Smith     Collective on DM
139147c6ae99SBarry Smith 
139247c6ae99SBarry Smith     Input Parameter:
139347c6ae99SBarry Smith +   dm - the DM object
1394cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
139547c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
139647c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
139747c6ae99SBarry Smith 
139847c6ae99SBarry Smith     Level: developer
139947c6ae99SBarry Smith 
1400e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
140147c6ae99SBarry Smith          DMSetFunction()
140247c6ae99SBarry Smith 
140347c6ae99SBarry Smith @*/
14047087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
140547c6ae99SBarry Smith {
140647c6ae99SBarry Smith   PetscErrorCode ierr;
140747c6ae99SBarry Smith 
140847c6ae99SBarry Smith   PetscFunctionBegin;
1409171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
141047c6ae99SBarry Smith   if (!dm->ops->jacobian) {
141147c6ae99SBarry Smith     ISColoring     coloring;
141247c6ae99SBarry Smith     MatFDColoring  fd;
141347c6ae99SBarry Smith 
1414171400e9SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,MATAIJ,&coloring);CHKERRQ(ierr);
141547c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
1416fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
141747c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
14180bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
14190bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
142071cd77b2SBarry Smith 
142147c6ae99SBarry Smith     dm->fd = fd;
142247c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
14232533e041SBarry Smith 
142471cd77b2SBarry Smith     /* don't know why this is needed */
142571cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
142647c6ae99SBarry Smith   }
142747c6ae99SBarry Smith   if (!x) x = dm->x;
142847c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
1429cab2e9ccSBarry Smith 
143071cd77b2SBarry 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 */
1431649052a6SBarry Smith   if (x) {
1432cab2e9ccSBarry Smith     if (!dm->x) {
143371cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
1434cab2e9ccSBarry Smith     }
1435cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
1436649052a6SBarry Smith   }
1437a8248277SBarry Smith   if (A != B) {
1438a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1439a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1440a8248277SBarry Smith   }
144147c6ae99SBarry Smith   PetscFunctionReturn(0);
144247c6ae99SBarry Smith }
1443264ace61SBarry Smith 
1444cab2e9ccSBarry Smith 
1445264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
1446264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
1447264ace61SBarry Smith 
1448264ace61SBarry Smith #undef __FUNCT__
1449264ace61SBarry Smith #define __FUNCT__ "DMSetType"
1450264ace61SBarry Smith /*@C
1451264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
1452264ace61SBarry Smith 
1453264ace61SBarry Smith   Collective on DM
1454264ace61SBarry Smith 
1455264ace61SBarry Smith   Input Parameters:
1456264ace61SBarry Smith + dm     - The DM object
1457264ace61SBarry Smith - method - The name of the DM type
1458264ace61SBarry Smith 
1459264ace61SBarry Smith   Options Database Key:
1460264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
1461264ace61SBarry Smith 
1462264ace61SBarry Smith   Notes:
1463e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
1464264ace61SBarry Smith 
1465264ace61SBarry Smith   Level: intermediate
1466264ace61SBarry Smith 
1467264ace61SBarry Smith .keywords: DM, set, type
1468264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
1469264ace61SBarry Smith @*/
14707087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
1471264ace61SBarry Smith {
1472264ace61SBarry Smith   PetscErrorCode (*r)(DM);
1473264ace61SBarry Smith   PetscBool      match;
1474264ace61SBarry Smith   PetscErrorCode ierr;
1475264ace61SBarry Smith 
1476264ace61SBarry Smith   PetscFunctionBegin;
1477264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1478264ace61SBarry Smith   ierr = PetscTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
1479264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
1480264ace61SBarry Smith 
1481264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
14824b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
1483264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
1484264ace61SBarry Smith 
1485264ace61SBarry Smith   if (dm->ops->destroy) {
1486264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
1487264ace61SBarry Smith   }
1488264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
1489264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
1490264ace61SBarry Smith   PetscFunctionReturn(0);
1491264ace61SBarry Smith }
1492264ace61SBarry Smith 
1493264ace61SBarry Smith #undef __FUNCT__
1494264ace61SBarry Smith #define __FUNCT__ "DMGetType"
1495264ace61SBarry Smith /*@C
1496264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
1497264ace61SBarry Smith 
1498264ace61SBarry Smith   Not Collective
1499264ace61SBarry Smith 
1500264ace61SBarry Smith   Input Parameter:
1501264ace61SBarry Smith . dm  - The DM
1502264ace61SBarry Smith 
1503264ace61SBarry Smith   Output Parameter:
1504264ace61SBarry Smith . type - The DM type name
1505264ace61SBarry Smith 
1506264ace61SBarry Smith   Level: intermediate
1507264ace61SBarry Smith 
1508264ace61SBarry Smith .keywords: DM, get, type, name
1509264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
1510264ace61SBarry Smith @*/
15117087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
1512264ace61SBarry Smith {
1513264ace61SBarry Smith   PetscErrorCode ierr;
1514264ace61SBarry Smith 
1515264ace61SBarry Smith   PetscFunctionBegin;
1516264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1517264ace61SBarry Smith   PetscValidCharPointer(type,2);
1518264ace61SBarry Smith   if (!DMRegisterAllCalled) {
1519264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
1520264ace61SBarry Smith   }
1521264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
1522264ace61SBarry Smith   PetscFunctionReturn(0);
1523264ace61SBarry Smith }
1524264ace61SBarry Smith 
152567a56275SMatthew G Knepley #undef __FUNCT__
152667a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
152767a56275SMatthew G Knepley /*@C
152867a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
152967a56275SMatthew G Knepley 
153067a56275SMatthew G Knepley   Collective on DM
153167a56275SMatthew G Knepley 
153267a56275SMatthew G Knepley   Input Parameters:
153367a56275SMatthew G Knepley + dm - the DM
153467a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
153567a56275SMatthew G Knepley 
153667a56275SMatthew G Knepley   Output Parameter:
153767a56275SMatthew G Knepley . M - pointer to new DM
153867a56275SMatthew G Knepley 
153967a56275SMatthew G Knepley   Notes:
154067a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
154167a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
154267a56275SMatthew G Knepley   of the input DM.
154367a56275SMatthew G Knepley 
154467a56275SMatthew G Knepley   Level: intermediate
154567a56275SMatthew G Knepley 
154667a56275SMatthew G Knepley .seealso: DMCreate()
154767a56275SMatthew G Knepley @*/
154867a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
154967a56275SMatthew G Knepley {
155067a56275SMatthew G Knepley   DM             B;
155167a56275SMatthew G Knepley   char           convname[256];
155267a56275SMatthew G Knepley   PetscBool      sametype, issame;
155367a56275SMatthew G Knepley   PetscErrorCode ierr;
155467a56275SMatthew G Knepley 
155567a56275SMatthew G Knepley   PetscFunctionBegin;
155667a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
155767a56275SMatthew G Knepley   PetscValidType(dm,1);
155867a56275SMatthew G Knepley   PetscValidPointer(M,3);
155967a56275SMatthew G Knepley   ierr = PetscTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
156067a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
156167a56275SMatthew G Knepley   {
156267a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
156367a56275SMatthew G Knepley 
156467a56275SMatthew G Knepley     /*
156567a56275SMatthew G Knepley        Order of precedence:
156667a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
156767a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
156867a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
156967a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
157067a56275SMatthew G Knepley        5) Use a really basic converter.
157167a56275SMatthew G Knepley     */
157267a56275SMatthew G Knepley 
157367a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
157467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
157567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
157667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
157767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
157867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
157967a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
158067a56275SMatthew G Knepley     if (conv) goto foundconv;
158167a56275SMatthew G Knepley 
158267a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
158367a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
158467a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
158567a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
158667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
158767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
158867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
158967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
159067a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
159167a56275SMatthew G Knepley     if (conv) {
1592fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
159367a56275SMatthew G Knepley       goto foundconv;
159467a56275SMatthew G Knepley     }
159567a56275SMatthew G Knepley 
159667a56275SMatthew G Knepley #if 0
159767a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
159867a56275SMatthew G Knepley     conv = B->ops->convertfrom;
1599fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
160067a56275SMatthew G Knepley     if (conv) goto foundconv;
160167a56275SMatthew G Knepley 
160267a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
160367a56275SMatthew G Knepley     if (dm->ops->convert) {
160467a56275SMatthew G Knepley       conv = dm->ops->convert;
160567a56275SMatthew G Knepley     }
160667a56275SMatthew G Knepley     if (conv) goto foundconv;
160767a56275SMatthew G Knepley #endif
160867a56275SMatthew G Knepley 
160967a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
161067a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
161167a56275SMatthew G Knepley 
161267a56275SMatthew G Knepley     foundconv:
161367a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
161467a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
161567a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
161667a56275SMatthew G Knepley   }
161767a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
161867a56275SMatthew G Knepley   PetscFunctionReturn(0);
161967a56275SMatthew G Knepley }
1620264ace61SBarry Smith 
1621264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1622264ace61SBarry Smith 
1623264ace61SBarry Smith #undef __FUNCT__
1624264ace61SBarry Smith #define __FUNCT__ "DMRegister"
1625264ace61SBarry Smith /*@C
1626264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
1627264ace61SBarry Smith 
1628264ace61SBarry Smith   Level: advanced
1629264ace61SBarry Smith @*/
16307087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
1631264ace61SBarry Smith {
1632264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
1633264ace61SBarry Smith   PetscErrorCode ierr;
1634264ace61SBarry Smith 
1635264ace61SBarry Smith   PetscFunctionBegin;
1636264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
1637264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
1638264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
1639264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
1640264ace61SBarry Smith   PetscFunctionReturn(0);
1641264ace61SBarry Smith }
1642264ace61SBarry Smith 
1643264ace61SBarry Smith 
1644264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1645264ace61SBarry Smith #undef __FUNCT__
1646264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
1647264ace61SBarry Smith /*@C
1648264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
1649264ace61SBarry Smith 
1650264ace61SBarry Smith    Not Collective
1651264ace61SBarry Smith 
1652264ace61SBarry Smith    Level: advanced
1653264ace61SBarry Smith 
1654264ace61SBarry Smith .keywords: DM, register, destroy
1655264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
1656264ace61SBarry Smith @*/
16577087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
1658264ace61SBarry Smith {
1659264ace61SBarry Smith   PetscErrorCode ierr;
1660264ace61SBarry Smith 
1661264ace61SBarry Smith   PetscFunctionBegin;
1662264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
1663264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
1664264ace61SBarry Smith   PetscFunctionReturn(0);
1665264ace61SBarry Smith }
166623f975d1SBarry Smith 
166723f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
1668c6db04a5SJed Brown #include <mex.h>
166923f975d1SBarry Smith 
16703014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
167123f975d1SBarry Smith 
167223f975d1SBarry Smith #undef __FUNCT__
167323f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
167423f975d1SBarry Smith /*
167523f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
167623f975d1SBarry Smith                          DMSetFunctionMatlab().
167723f975d1SBarry Smith 
167823f975d1SBarry Smith    For linear problems x is null
167923f975d1SBarry Smith 
168023f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
168123f975d1SBarry Smith */
16827087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
168323f975d1SBarry Smith {
168423f975d1SBarry Smith   PetscErrorCode    ierr;
168523f975d1SBarry Smith   DMMatlabContext   *sctx;
168623f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
168723f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
168823f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
168923f975d1SBarry Smith 
169023f975d1SBarry Smith   PetscFunctionBegin;
169123f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
169223f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
169323f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
169423f975d1SBarry Smith 
169523f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
16961b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
169723f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
169823f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
16993014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
170023f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
170123f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
170223f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
170323f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
1704b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
170523f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
170623f975d1SBarry Smith   mxDestroyArray(prhs[0]);
170723f975d1SBarry Smith   mxDestroyArray(prhs[1]);
170823f975d1SBarry Smith   mxDestroyArray(prhs[2]);
170923f975d1SBarry Smith   mxDestroyArray(prhs[3]);
171023f975d1SBarry Smith   mxDestroyArray(plhs[0]);
171123f975d1SBarry Smith   PetscFunctionReturn(0);
171223f975d1SBarry Smith }
171323f975d1SBarry Smith 
171423f975d1SBarry Smith 
171523f975d1SBarry Smith #undef __FUNCT__
171623f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
171723f975d1SBarry Smith /*
171823f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
171923f975d1SBarry Smith 
172023f975d1SBarry Smith */
17217087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
172223f975d1SBarry Smith {
172323f975d1SBarry Smith   PetscErrorCode    ierr;
172423f975d1SBarry Smith   DMMatlabContext   *sctx;
172523f975d1SBarry Smith 
172623f975d1SBarry Smith   PetscFunctionBegin;
1727171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
172823f975d1SBarry Smith   /* currently sctx is memory bleed */
17291b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
17303014e516SBarry Smith   if (!sctx) {
173123f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
17323014e516SBarry Smith   }
173323f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
17341b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
173523f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
173623f975d1SBarry Smith   PetscFunctionReturn(0);
173723f975d1SBarry Smith }
17383014e516SBarry Smith 
17393014e516SBarry Smith #undef __FUNCT__
17403014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
17413014e516SBarry Smith /*
17423014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
17433014e516SBarry Smith                          DMSetJacobianMatlab().
17443014e516SBarry Smith 
17453014e516SBarry Smith    For linear problems x is null
17463014e516SBarry Smith 
17473014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
17483014e516SBarry Smith */
17497087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
17503014e516SBarry Smith {
17513014e516SBarry Smith   PetscErrorCode    ierr;
17523014e516SBarry Smith   DMMatlabContext   *sctx;
17533014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
17543014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
17553014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
17563014e516SBarry Smith 
17573014e516SBarry Smith   PetscFunctionBegin;
17583014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17593014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
17603014e516SBarry Smith 
1761e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
17621b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
17633014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
17643014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
17653014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
17663014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
17673014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
17683014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
17693014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
17703014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
17713014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
1772b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
1773c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
1774c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
17753014e516SBarry Smith   mxDestroyArray(prhs[0]);
17763014e516SBarry Smith   mxDestroyArray(prhs[1]);
17773014e516SBarry Smith   mxDestroyArray(prhs[2]);
17783014e516SBarry Smith   mxDestroyArray(prhs[3]);
17793014e516SBarry Smith   mxDestroyArray(prhs[4]);
17803014e516SBarry Smith   mxDestroyArray(plhs[0]);
17813014e516SBarry Smith   mxDestroyArray(plhs[1]);
17823014e516SBarry Smith   PetscFunctionReturn(0);
17833014e516SBarry Smith }
17843014e516SBarry Smith 
17853014e516SBarry Smith 
17863014e516SBarry Smith #undef __FUNCT__
17873014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
17883014e516SBarry Smith /*
17893014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
17903014e516SBarry Smith 
17913014e516SBarry Smith */
17927087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
17933014e516SBarry Smith {
17943014e516SBarry Smith   PetscErrorCode    ierr;
17953014e516SBarry Smith   DMMatlabContext   *sctx;
17963014e516SBarry Smith 
17973014e516SBarry Smith   PetscFunctionBegin;
1798171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17993014e516SBarry Smith   /* currently sctx is memory bleed */
18001b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
18013014e516SBarry Smith   if (!sctx) {
18023014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
18033014e516SBarry Smith   }
18043014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
18051b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
18063014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
18073014e516SBarry Smith   PetscFunctionReturn(0);
18083014e516SBarry Smith }
180923f975d1SBarry Smith #endif
1810b859378eSBarry Smith 
1811b859378eSBarry Smith #undef __FUNCT__
1812b859378eSBarry Smith #define __FUNCT__ "DMLoad"
1813b859378eSBarry Smith /*@C
1814b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
1815b859378eSBarry Smith   with DMView().
1816b859378eSBarry Smith 
1817b859378eSBarry Smith   Collective on PetscViewer
1818b859378eSBarry Smith 
1819b859378eSBarry Smith   Input Parameters:
1820b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
1821b859378eSBarry Smith            some related function before a call to DMLoad().
1822b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
1823b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
1824b859378eSBarry Smith 
1825b859378eSBarry Smith    Level: intermediate
1826b859378eSBarry Smith 
1827b859378eSBarry Smith   Notes:
1828b859378eSBarry Smith   Defaults to the DM DA.
1829b859378eSBarry Smith 
1830b859378eSBarry Smith   Notes for advanced users:
1831b859378eSBarry Smith   Most users should not need to know the details of the binary storage
1832b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
1833b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
1834b859378eSBarry Smith   format is
1835b859378eSBarry Smith .vb
1836b859378eSBarry Smith      has not yet been determined
1837b859378eSBarry Smith .ve
1838b859378eSBarry Smith 
1839b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
1840b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
1841b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
1842b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
1843b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
1844b859378eSBarry Smith 
1845b859378eSBarry Smith   Concepts: vector^loading from file
1846b859378eSBarry Smith 
1847b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
1848b859378eSBarry Smith @*/
1849b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
1850b859378eSBarry Smith {
1851b859378eSBarry Smith   PetscErrorCode ierr;
1852b859378eSBarry Smith 
1853b859378eSBarry Smith   PetscFunctionBegin;
1854b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
1855b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1856b859378eSBarry Smith 
1857b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
1858b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
1859b859378eSBarry Smith   }
1860b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
1861b859378eSBarry Smith   PetscFunctionReturn(0);
1862b859378eSBarry Smith }
1863b859378eSBarry Smith 
18647da65231SMatthew G Knepley /******************************** FEM Support **********************************/
18657da65231SMatthew G Knepley 
18667da65231SMatthew G Knepley #undef __FUNCT__
18677da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
18687da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
18697da65231SMatthew G Knepley   PetscErrorCode ierr;
1870*1d47ebbbSSatish Balay   PetscInt f;
18717da65231SMatthew G Knepley   PetscFunctionBegin;
18727da65231SMatthew G Knepley   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %d Element %s\n", c, name);CHKERRQ(ierr);
1873*1d47ebbbSSatish Balay   for(f = 0; f < len; ++f) {
18747da65231SMatthew G Knepley     PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", x[f]);
18757da65231SMatthew G Knepley   }
18767da65231SMatthew G Knepley   PetscFunctionReturn(0);
18777da65231SMatthew G Knepley }
18787da65231SMatthew G Knepley 
18797da65231SMatthew G Knepley #undef __FUNCT__
18807da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
18817da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
18827da65231SMatthew G Knepley   PetscErrorCode ierr;
1883*1d47ebbbSSatish Balay   int f,g;
18847da65231SMatthew G Knepley 
18857da65231SMatthew G Knepley   PetscFunctionBegin;
18867da65231SMatthew G Knepley   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %d Element %s\n", c, name);CHKERRQ(ierr);
1887*1d47ebbbSSatish Balay   for(f = 0; f < rows; ++f) {
18887da65231SMatthew G Knepley     PetscPrintf(PETSC_COMM_SELF, "  |");
1889*1d47ebbbSSatish Balay     for(g = 0; g < cols; ++g) {
18907da65231SMatthew G Knepley       PetscPrintf(PETSC_COMM_SELF, " % 9.5g", A[f*cols+g]);
18917da65231SMatthew G Knepley     }
18927da65231SMatthew G Knepley     PetscPrintf(PETSC_COMM_SELF, " |\n");
18937da65231SMatthew G Knepley   }
18947da65231SMatthew G Knepley   PetscFunctionReturn(0);
18957da65231SMatthew G Knepley }
1896