xref: /petsc/src/dm/interface/dm.c (revision 7da65231c26e71e92fa4ef0818bddc6fe70c366a)
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);
719644e2e5bSBarry Smith   (*dmf)->ops->initialguess = dm->ops->initialguess;
720644e2e5bSBarry Smith   (*dmf)->ops->function     = dm->ops->function;
721644e2e5bSBarry Smith   (*dmf)->ops->functionj    = dm->ops->functionj;
722644e2e5bSBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
723644e2e5bSBarry Smith     (*dmf)->ops->jacobian     = dm->ops->jacobian;
724644e2e5bSBarry Smith   }
7258cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
726644e2e5bSBarry Smith   (*dmf)->ctx     = dm->ctx;
727656b349aSBarry Smith   (*dmf)->levelup = dm->levelup + 1;
72847c6ae99SBarry Smith   PetscFunctionReturn(0);
72947c6ae99SBarry Smith }
73047c6ae99SBarry Smith 
73147c6ae99SBarry Smith #undef __FUNCT__
732eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
733eb3f98d2SBarry Smith /*@
734eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
735eb3f98d2SBarry Smith 
736eb3f98d2SBarry Smith     Not Collective
737eb3f98d2SBarry Smith 
738eb3f98d2SBarry Smith     Input Parameter:
739eb3f98d2SBarry Smith .   dm - the DM object
740eb3f98d2SBarry Smith 
741eb3f98d2SBarry Smith     Output Parameter:
742eb3f98d2SBarry Smith .   level - number of refinements
743eb3f98d2SBarry Smith 
744eb3f98d2SBarry Smith     Level: developer
745eb3f98d2SBarry Smith 
7466a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
747eb3f98d2SBarry Smith 
748eb3f98d2SBarry Smith @*/
749eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
750eb3f98d2SBarry Smith {
751eb3f98d2SBarry Smith   PetscFunctionBegin;
752eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
753eb3f98d2SBarry Smith   *level = dm->levelup;
754eb3f98d2SBarry Smith   PetscFunctionReturn(0);
755eb3f98d2SBarry Smith }
756eb3f98d2SBarry Smith 
757eb3f98d2SBarry Smith #undef __FUNCT__
75847c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
75947c6ae99SBarry Smith /*@
76047c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
76147c6ae99SBarry Smith 
76247c6ae99SBarry Smith     Neighbor-wise Collective on DM
76347c6ae99SBarry Smith 
76447c6ae99SBarry Smith     Input Parameters:
76547c6ae99SBarry Smith +   dm - the DM object
76647c6ae99SBarry Smith .   g - the global vector
76747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
76847c6ae99SBarry Smith -   l - the local vector
76947c6ae99SBarry Smith 
77047c6ae99SBarry Smith 
77147c6ae99SBarry Smith     Level: beginner
77247c6ae99SBarry Smith 
773e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
77447c6ae99SBarry Smith 
77547c6ae99SBarry Smith @*/
7767087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
77747c6ae99SBarry Smith {
77847c6ae99SBarry Smith   PetscErrorCode ierr;
77947c6ae99SBarry Smith 
78047c6ae99SBarry Smith   PetscFunctionBegin;
781171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
782843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
78347c6ae99SBarry Smith   PetscFunctionReturn(0);
78447c6ae99SBarry Smith }
78547c6ae99SBarry Smith 
78647c6ae99SBarry Smith #undef __FUNCT__
78747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
78847c6ae99SBarry Smith /*@
78947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
79047c6ae99SBarry Smith 
79147c6ae99SBarry Smith     Neighbor-wise Collective on DM
79247c6ae99SBarry Smith 
79347c6ae99SBarry Smith     Input Parameters:
79447c6ae99SBarry Smith +   dm - the DM object
79547c6ae99SBarry Smith .   g - the global vector
79647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
79747c6ae99SBarry Smith -   l - the local vector
79847c6ae99SBarry Smith 
79947c6ae99SBarry Smith 
80047c6ae99SBarry Smith     Level: beginner
80147c6ae99SBarry Smith 
802e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
80347c6ae99SBarry Smith 
80447c6ae99SBarry Smith @*/
8057087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
80647c6ae99SBarry Smith {
80747c6ae99SBarry Smith   PetscErrorCode ierr;
80847c6ae99SBarry Smith 
80947c6ae99SBarry Smith   PetscFunctionBegin;
810171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
811843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
81247c6ae99SBarry Smith   PetscFunctionReturn(0);
81347c6ae99SBarry Smith }
81447c6ae99SBarry Smith 
81547c6ae99SBarry Smith #undef __FUNCT__
8169a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
81747c6ae99SBarry Smith /*@
8189a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
8199a42bb27SBarry Smith 
8209a42bb27SBarry Smith     Neighbor-wise Collective on DM
8219a42bb27SBarry Smith 
8229a42bb27SBarry Smith     Input Parameters:
8239a42bb27SBarry Smith +   dm - the DM object
824f6813fd5SJed Brown .   l - the local vector
8259a42bb27SBarry 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
8269a42bb27SBarry Smith            base point.
827f6813fd5SJed Brown - - the global vector
8289a42bb27SBarry Smith 
8299a42bb27SBarry 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
8309a42bb27SBarry 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
8319a42bb27SBarry Smith            global array to the final global array with VecAXPY().
8329a42bb27SBarry Smith 
8339a42bb27SBarry Smith     Level: beginner
8349a42bb27SBarry Smith 
835e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
8369a42bb27SBarry Smith 
8379a42bb27SBarry Smith @*/
8387087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
8399a42bb27SBarry Smith {
8409a42bb27SBarry Smith   PetscErrorCode ierr;
8419a42bb27SBarry Smith 
8429a42bb27SBarry Smith   PetscFunctionBegin;
843171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
844843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
8459a42bb27SBarry Smith   PetscFunctionReturn(0);
8469a42bb27SBarry Smith }
8479a42bb27SBarry Smith 
8489a42bb27SBarry Smith #undef __FUNCT__
8499a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
8509a42bb27SBarry Smith /*@
8519a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
85247c6ae99SBarry Smith 
85347c6ae99SBarry Smith     Neighbor-wise Collective on DM
85447c6ae99SBarry Smith 
85547c6ae99SBarry Smith     Input Parameters:
85647c6ae99SBarry Smith +   dm - the DM object
857f6813fd5SJed Brown .   l - the local vector
85847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
859f6813fd5SJed Brown -   g - the global vector
86047c6ae99SBarry Smith 
86147c6ae99SBarry Smith 
86247c6ae99SBarry Smith     Level: beginner
86347c6ae99SBarry Smith 
864e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
86547c6ae99SBarry Smith 
86647c6ae99SBarry Smith @*/
8677087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
86847c6ae99SBarry Smith {
86947c6ae99SBarry Smith   PetscErrorCode ierr;
87047c6ae99SBarry Smith 
87147c6ae99SBarry Smith   PetscFunctionBegin;
872171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
873843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
87447c6ae99SBarry Smith   PetscFunctionReturn(0);
87547c6ae99SBarry Smith }
87647c6ae99SBarry Smith 
87747c6ae99SBarry Smith #undef __FUNCT__
87847c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
87947c6ae99SBarry Smith /*@
88047c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
88147c6ae99SBarry Smith 
88247c6ae99SBarry Smith     Collective on DM
88347c6ae99SBarry Smith 
88447c6ae99SBarry Smith     Input Parameter:
88547c6ae99SBarry Smith +   dm - the DM object
88647c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
88747c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
88847c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
88947c6ae99SBarry Smith 
89047c6ae99SBarry Smith     Level: developer
89147c6ae99SBarry Smith 
892e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
89347c6ae99SBarry Smith          DMSetFunction()
89447c6ae99SBarry Smith 
89547c6ae99SBarry Smith @*/
8967087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
89747c6ae99SBarry Smith {
89847c6ae99SBarry Smith   PetscErrorCode ierr;
899171400e9SBarry Smith 
90047c6ae99SBarry Smith   PetscFunctionBegin;
901171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
90247c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
90347c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
90447c6ae99SBarry Smith   if (A != B) {
90547c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
90647c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
90747c6ae99SBarry Smith   }
90847c6ae99SBarry Smith   PetscFunctionReturn(0);
90947c6ae99SBarry Smith }
91047c6ae99SBarry Smith 
91147c6ae99SBarry Smith #undef __FUNCT__
91247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
91347c6ae99SBarry Smith /*@
91447c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
91547c6ae99SBarry Smith 
91647c6ae99SBarry Smith     Collective on DM
91747c6ae99SBarry Smith 
91847c6ae99SBarry Smith     Input Parameter:
91947c6ae99SBarry Smith +   dm - the DM object
92047c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
92147c6ae99SBarry Smith 
92247c6ae99SBarry Smith     Output Parameter:
92347c6ae99SBarry Smith .   dmc - the coarsened DM
92447c6ae99SBarry Smith 
92547c6ae99SBarry Smith     Level: developer
92647c6ae99SBarry Smith 
927e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
92847c6ae99SBarry Smith 
92947c6ae99SBarry Smith @*/
9307087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
93147c6ae99SBarry Smith {
93247c6ae99SBarry Smith   PetscErrorCode ierr;
93347c6ae99SBarry Smith 
93447c6ae99SBarry Smith   PetscFunctionBegin;
935171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
93647c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
93747c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
93847c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
93947c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
94047c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
94147c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
94247c6ae99SBarry Smith   }
9438cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
944644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
945656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
94647c6ae99SBarry Smith   PetscFunctionReturn(0);
94747c6ae99SBarry Smith }
94847c6ae99SBarry Smith 
94947c6ae99SBarry Smith #undef __FUNCT__
9505fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
9515fe1f584SPeter Brune /*@
9526a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
9535fe1f584SPeter Brune 
9545fe1f584SPeter Brune     Not Collective
9555fe1f584SPeter Brune 
9565fe1f584SPeter Brune     Input Parameter:
9575fe1f584SPeter Brune .   dm - the DM object
9585fe1f584SPeter Brune 
9595fe1f584SPeter Brune     Output Parameter:
9606a7d9d85SPeter Brune .   level - number of coarsenings
9615fe1f584SPeter Brune 
9625fe1f584SPeter Brune     Level: developer
9635fe1f584SPeter Brune 
9646a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
9655fe1f584SPeter Brune 
9665fe1f584SPeter Brune @*/
9675fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
9685fe1f584SPeter Brune {
9695fe1f584SPeter Brune   PetscFunctionBegin;
9705fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9715fe1f584SPeter Brune   *level = dm->leveldown;
9725fe1f584SPeter Brune   PetscFunctionReturn(0);
9735fe1f584SPeter Brune }
9745fe1f584SPeter Brune 
9755fe1f584SPeter Brune 
9765fe1f584SPeter Brune 
9775fe1f584SPeter Brune #undef __FUNCT__
97847c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
97947c6ae99SBarry Smith /*@C
98047c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
98147c6ae99SBarry Smith 
98247c6ae99SBarry Smith     Collective on DM
98347c6ae99SBarry Smith 
98447c6ae99SBarry Smith     Input Parameter:
98547c6ae99SBarry Smith +   dm - the DM object
98647c6ae99SBarry Smith -   nlevels - the number of levels of refinement
98747c6ae99SBarry Smith 
98847c6ae99SBarry Smith     Output Parameter:
98947c6ae99SBarry Smith .   dmf - the refined DM hierarchy
99047c6ae99SBarry Smith 
99147c6ae99SBarry Smith     Level: developer
99247c6ae99SBarry Smith 
993e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith @*/
9967087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
99747c6ae99SBarry Smith {
99847c6ae99SBarry Smith   PetscErrorCode ierr;
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith   PetscFunctionBegin;
1001171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
100247c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
100347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
100447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
100547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
100647c6ae99SBarry Smith   } else if (dm->ops->refine) {
100747c6ae99SBarry Smith     PetscInt i;
100847c6ae99SBarry Smith 
100947c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
101047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
101147c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
101247c6ae99SBarry Smith     }
101347c6ae99SBarry Smith   } else {
101447c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
101547c6ae99SBarry Smith   }
101647c6ae99SBarry Smith   PetscFunctionReturn(0);
101747c6ae99SBarry Smith }
101847c6ae99SBarry Smith 
101947c6ae99SBarry Smith #undef __FUNCT__
102047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
102147c6ae99SBarry Smith /*@C
102247c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
102347c6ae99SBarry Smith 
102447c6ae99SBarry Smith     Collective on DM
102547c6ae99SBarry Smith 
102647c6ae99SBarry Smith     Input Parameter:
102747c6ae99SBarry Smith +   dm - the DM object
102847c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
102947c6ae99SBarry Smith 
103047c6ae99SBarry Smith     Output Parameter:
103147c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
103247c6ae99SBarry Smith 
103347c6ae99SBarry Smith     Level: developer
103447c6ae99SBarry Smith 
1035e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
103647c6ae99SBarry Smith 
103747c6ae99SBarry Smith @*/
10387087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
103947c6ae99SBarry Smith {
104047c6ae99SBarry Smith   PetscErrorCode ierr;
104147c6ae99SBarry Smith 
104247c6ae99SBarry Smith   PetscFunctionBegin;
1043171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
104447c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
104547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
104647c6ae99SBarry Smith   PetscValidPointer(dmc,3);
104747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
104847c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
104947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
105047c6ae99SBarry Smith     PetscInt i;
105147c6ae99SBarry Smith 
105247c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
105347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
105447c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
105547c6ae99SBarry Smith     }
105647c6ae99SBarry Smith   } else {
105747c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
105847c6ae99SBarry Smith   }
105947c6ae99SBarry Smith   PetscFunctionReturn(0);
106047c6ae99SBarry Smith }
106147c6ae99SBarry Smith 
106247c6ae99SBarry Smith #undef __FUNCT__
1063e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
106447c6ae99SBarry Smith /*@
1065e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
106647c6ae99SBarry Smith    grids associated with two DMs.
106747c6ae99SBarry Smith 
106847c6ae99SBarry Smith    Collective on DM
106947c6ae99SBarry Smith 
107047c6ae99SBarry Smith    Input Parameters:
107147c6ae99SBarry Smith +  dmc - the coarse grid DM
107247c6ae99SBarry Smith -  dmf - the fine grid DM
107347c6ae99SBarry Smith 
107447c6ae99SBarry Smith    Output Parameters:
107547c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
107647c6ae99SBarry Smith 
107747c6ae99SBarry Smith    Level: intermediate
107847c6ae99SBarry Smith 
107947c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
108047c6ae99SBarry Smith 
1081e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
108247c6ae99SBarry Smith @*/
1083e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
108447c6ae99SBarry Smith {
108547c6ae99SBarry Smith   PetscErrorCode ierr;
108647c6ae99SBarry Smith 
108747c6ae99SBarry Smith   PetscFunctionBegin;
1088171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1089171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
109047c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
109147c6ae99SBarry Smith   PetscFunctionReturn(0);
109247c6ae99SBarry Smith }
109347c6ae99SBarry Smith 
109447c6ae99SBarry Smith #undef __FUNCT__
10951a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
10961a266240SBarry Smith /*@C
10971a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
10981a266240SBarry Smith 
10991a266240SBarry Smith     Not Collective
11001a266240SBarry Smith 
11011a266240SBarry Smith     Input Parameters:
11021a266240SBarry Smith +   dm - the DM object
11031a266240SBarry Smith -   destroy - the destroy function
11041a266240SBarry Smith 
11051a266240SBarry Smith     Level: intermediate
11061a266240SBarry Smith 
1107e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
11081a266240SBarry Smith 
1109f07f9ceaSJed Brown @*/
11101a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
11111a266240SBarry Smith {
11121a266240SBarry Smith   PetscFunctionBegin;
1113171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11141a266240SBarry Smith   dm->ctxdestroy = destroy;
11151a266240SBarry Smith   PetscFunctionReturn(0);
11161a266240SBarry Smith }
11171a266240SBarry Smith 
11181a266240SBarry Smith #undef __FUNCT__
11191b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1120b07ff414SBarry Smith /*@
11211b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
112247c6ae99SBarry Smith 
112347c6ae99SBarry Smith     Not Collective
112447c6ae99SBarry Smith 
112547c6ae99SBarry Smith     Input Parameters:
112647c6ae99SBarry Smith +   dm - the DM object
112747c6ae99SBarry Smith -   ctx - the user context
112847c6ae99SBarry Smith 
112947c6ae99SBarry Smith     Level: intermediate
113047c6ae99SBarry Smith 
1131e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
113247c6ae99SBarry Smith 
113347c6ae99SBarry Smith @*/
11341b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
113547c6ae99SBarry Smith {
113647c6ae99SBarry Smith   PetscFunctionBegin;
1137171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
113847c6ae99SBarry Smith   dm->ctx = ctx;
113947c6ae99SBarry Smith   PetscFunctionReturn(0);
114047c6ae99SBarry Smith }
114147c6ae99SBarry Smith 
114247c6ae99SBarry Smith #undef __FUNCT__
11431b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
114447c6ae99SBarry Smith /*@
11451b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
114647c6ae99SBarry Smith 
114747c6ae99SBarry Smith     Not Collective
114847c6ae99SBarry Smith 
114947c6ae99SBarry Smith     Input Parameter:
115047c6ae99SBarry Smith .   dm - the DM object
115147c6ae99SBarry Smith 
115247c6ae99SBarry Smith     Output Parameter:
115347c6ae99SBarry Smith .   ctx - the user context
115447c6ae99SBarry Smith 
115547c6ae99SBarry Smith     Level: intermediate
115647c6ae99SBarry Smith 
1157e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
115847c6ae99SBarry Smith 
115947c6ae99SBarry Smith @*/
11601b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
116147c6ae99SBarry Smith {
116247c6ae99SBarry Smith   PetscFunctionBegin;
1163171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11641b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
116547c6ae99SBarry Smith   PetscFunctionReturn(0);
116647c6ae99SBarry Smith }
116747c6ae99SBarry Smith 
116847c6ae99SBarry Smith #undef __FUNCT__
116947c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
11707e833e3aSBarry Smith /*@C
117147c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
117247c6ae99SBarry Smith 
117347c6ae99SBarry Smith     Logically Collective on DM
117447c6ae99SBarry Smith 
117547c6ae99SBarry Smith     Input Parameter:
117647c6ae99SBarry Smith +   dm - the DM object to destroy
117747c6ae99SBarry Smith -   f - the function to compute the initial guess
117847c6ae99SBarry Smith 
117947c6ae99SBarry Smith     Level: intermediate
118047c6ae99SBarry Smith 
1181e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
118247c6ae99SBarry Smith 
1183f07f9ceaSJed Brown @*/
11847087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
118547c6ae99SBarry Smith {
118647c6ae99SBarry Smith   PetscFunctionBegin;
1187171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
118847c6ae99SBarry Smith   dm->ops->initialguess = f;
118947c6ae99SBarry Smith   PetscFunctionReturn(0);
119047c6ae99SBarry Smith }
119147c6ae99SBarry Smith 
119247c6ae99SBarry Smith #undef __FUNCT__
119347c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
11947e833e3aSBarry Smith /*@C
119547c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
119647c6ae99SBarry Smith 
119747c6ae99SBarry Smith     Logically Collective on DM
119847c6ae99SBarry Smith 
119947c6ae99SBarry Smith     Input Parameter:
120047c6ae99SBarry Smith +   dm - the DM object
120147c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
120247c6ae99SBarry Smith 
120347c6ae99SBarry Smith     Level: intermediate
120447c6ae99SBarry Smith 
120547c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
120647c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
120747c6ae99SBarry Smith 
1208e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
120947c6ae99SBarry Smith          DMSetJacobian()
121047c6ae99SBarry Smith 
1211f07f9ceaSJed Brown @*/
12127087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
121347c6ae99SBarry Smith {
121447c6ae99SBarry Smith   PetscFunctionBegin;
1215171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
121647c6ae99SBarry Smith   dm->ops->function = f;
121747c6ae99SBarry Smith   if (f) {
121847c6ae99SBarry Smith     dm->ops->functionj = f;
121947c6ae99SBarry Smith   }
122047c6ae99SBarry Smith   PetscFunctionReturn(0);
122147c6ae99SBarry Smith }
122247c6ae99SBarry Smith 
122347c6ae99SBarry Smith #undef __FUNCT__
122447c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
12257e833e3aSBarry Smith /*@C
122647c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
122747c6ae99SBarry Smith 
122847c6ae99SBarry Smith     Logically Collective on DM
122947c6ae99SBarry Smith 
123047c6ae99SBarry Smith     Input Parameter:
123147c6ae99SBarry Smith +   dm - the DM object to destroy
123247c6ae99SBarry Smith -   f - the function to compute the matrix entries
123347c6ae99SBarry Smith 
123447c6ae99SBarry Smith     Level: intermediate
123547c6ae99SBarry Smith 
1236e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
123747c6ae99SBarry Smith          DMSetFunction()
123847c6ae99SBarry Smith 
1239f07f9ceaSJed Brown @*/
12407087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
124147c6ae99SBarry Smith {
124247c6ae99SBarry Smith   PetscFunctionBegin;
1243171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
124447c6ae99SBarry Smith   dm->ops->jacobian = f;
124547c6ae99SBarry Smith   PetscFunctionReturn(0);
124647c6ae99SBarry Smith }
124747c6ae99SBarry Smith 
124847c6ae99SBarry Smith #undef __FUNCT__
124947c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
125047c6ae99SBarry Smith /*@
125147c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
125247c6ae99SBarry Smith 
125347c6ae99SBarry Smith     Collective on DM
125447c6ae99SBarry Smith 
125547c6ae99SBarry Smith     Input Parameter:
125647c6ae99SBarry Smith +   dm - the DM object to destroy
125747c6ae99SBarry Smith -   x - the vector to hold the initial guess values
125847c6ae99SBarry Smith 
125947c6ae99SBarry Smith     Level: developer
126047c6ae99SBarry Smith 
1261e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
126247c6ae99SBarry Smith 
126347c6ae99SBarry Smith @*/
12647087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
126547c6ae99SBarry Smith {
126647c6ae99SBarry Smith   PetscErrorCode ierr;
126747c6ae99SBarry Smith   PetscFunctionBegin;
1268171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
126947c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
127047c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
127147c6ae99SBarry Smith   PetscFunctionReturn(0);
127247c6ae99SBarry Smith }
127347c6ae99SBarry Smith 
127447c6ae99SBarry Smith #undef __FUNCT__
127547c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
127647c6ae99SBarry Smith /*@
127747c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
127847c6ae99SBarry Smith 
127947c6ae99SBarry Smith     Not Collective
128047c6ae99SBarry Smith 
128147c6ae99SBarry Smith     Input Parameter:
128247c6ae99SBarry Smith .   dm - the DM object to destroy
128347c6ae99SBarry Smith 
128447c6ae99SBarry Smith     Output Parameter:
128547c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
128647c6ae99SBarry Smith 
128747c6ae99SBarry Smith     Level: developer
128847c6ae99SBarry Smith 
1289e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
129047c6ae99SBarry Smith 
129147c6ae99SBarry Smith @*/
12927087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
129347c6ae99SBarry Smith {
129447c6ae99SBarry Smith   PetscFunctionBegin;
1295171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
129647c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
129747c6ae99SBarry Smith   PetscFunctionReturn(0);
129847c6ae99SBarry Smith }
129947c6ae99SBarry Smith 
130047c6ae99SBarry Smith #undef __FUNCT__
130147c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
130247c6ae99SBarry Smith /*@
130347c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
130447c6ae99SBarry Smith 
130547c6ae99SBarry Smith     Not Collective
130647c6ae99SBarry Smith 
130747c6ae99SBarry Smith     Input Parameter:
130847c6ae99SBarry Smith .   dm - the DM object to destroy
130947c6ae99SBarry Smith 
131047c6ae99SBarry Smith     Output Parameter:
131147c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
131247c6ae99SBarry Smith 
131347c6ae99SBarry Smith     Level: developer
131447c6ae99SBarry Smith 
1315e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
131647c6ae99SBarry Smith 
131747c6ae99SBarry Smith @*/
13187087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
131947c6ae99SBarry Smith {
132047c6ae99SBarry Smith   PetscFunctionBegin;
1321171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
132247c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
132347c6ae99SBarry Smith   PetscFunctionReturn(0);
132447c6ae99SBarry Smith }
132547c6ae99SBarry Smith 
132647c6ae99SBarry Smith #undef __FUNCT__
132747c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
132847c6ae99SBarry Smith /*@
132947c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
133047c6ae99SBarry Smith 
133147c6ae99SBarry Smith     Not Collective
133247c6ae99SBarry Smith 
133347c6ae99SBarry Smith     Input Parameter:
133447c6ae99SBarry Smith .   dm - the DM object to destroy
133547c6ae99SBarry Smith 
133647c6ae99SBarry Smith     Output Parameter:
133747c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
133847c6ae99SBarry Smith 
133947c6ae99SBarry Smith     Level: developer
134047c6ae99SBarry Smith 
1341e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
134247c6ae99SBarry Smith 
134347c6ae99SBarry Smith @*/
13447087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
134547c6ae99SBarry Smith {
134647c6ae99SBarry Smith   PetscFunctionBegin;
1347171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
134847c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
134947c6ae99SBarry Smith   PetscFunctionReturn(0);
135047c6ae99SBarry Smith }
135147c6ae99SBarry Smith 
135247c6ae99SBarry Smith #undef __FUNCT__
135347c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
135447c6ae99SBarry Smith /*@
135547c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
135647c6ae99SBarry Smith 
135747c6ae99SBarry Smith     Collective on DM
135847c6ae99SBarry Smith 
135947c6ae99SBarry Smith     Input Parameter:
136047c6ae99SBarry Smith +   dm - the DM object to destroy
136147c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
136247c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
136347c6ae99SBarry Smith 
136447c6ae99SBarry Smith     Level: developer
136547c6ae99SBarry Smith 
1366e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
136747c6ae99SBarry Smith          DMSetJacobian()
136847c6ae99SBarry Smith 
136947c6ae99SBarry Smith @*/
13707087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
137147c6ae99SBarry Smith {
137247c6ae99SBarry Smith   PetscErrorCode ierr;
137347c6ae99SBarry Smith   PetscFunctionBegin;
1374171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
137547c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
1376644e2e5bSBarry Smith   PetscStackPush("DM user function");
137747c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
1378644e2e5bSBarry Smith   PetscStackPop;
137947c6ae99SBarry Smith   PetscFunctionReturn(0);
138047c6ae99SBarry Smith }
138147c6ae99SBarry Smith 
138247c6ae99SBarry Smith 
138347c6ae99SBarry Smith #undef __FUNCT__
138447c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
138547c6ae99SBarry Smith /*@
138647c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
138747c6ae99SBarry Smith 
138847c6ae99SBarry Smith     Collective on DM
138947c6ae99SBarry Smith 
139047c6ae99SBarry Smith     Input Parameter:
139147c6ae99SBarry Smith +   dm - the DM object
1392cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
139347c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
139447c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
139547c6ae99SBarry Smith 
139647c6ae99SBarry Smith     Level: developer
139747c6ae99SBarry Smith 
1398e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
139947c6ae99SBarry Smith          DMSetFunction()
140047c6ae99SBarry Smith 
140147c6ae99SBarry Smith @*/
14027087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
140347c6ae99SBarry Smith {
140447c6ae99SBarry Smith   PetscErrorCode ierr;
140547c6ae99SBarry Smith 
140647c6ae99SBarry Smith   PetscFunctionBegin;
1407171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
140847c6ae99SBarry Smith   if (!dm->ops->jacobian) {
140947c6ae99SBarry Smith     ISColoring     coloring;
141047c6ae99SBarry Smith     MatFDColoring  fd;
141147c6ae99SBarry Smith 
1412171400e9SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,MATAIJ,&coloring);CHKERRQ(ierr);
141347c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
1414fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
141547c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
14160bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
14170bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
141871cd77b2SBarry Smith 
141947c6ae99SBarry Smith     dm->fd = fd;
142047c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
14212533e041SBarry Smith 
142271cd77b2SBarry Smith     /* don't know why this is needed */
142371cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
142447c6ae99SBarry Smith   }
142547c6ae99SBarry Smith   if (!x) x = dm->x;
142647c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
1427cab2e9ccSBarry Smith 
142871cd77b2SBarry 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 */
1429649052a6SBarry Smith   if (x) {
1430cab2e9ccSBarry Smith     if (!dm->x) {
143171cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
1432cab2e9ccSBarry Smith     }
1433cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
1434649052a6SBarry Smith   }
1435a8248277SBarry Smith   if (A != B) {
1436a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1437a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1438a8248277SBarry Smith   }
143947c6ae99SBarry Smith   PetscFunctionReturn(0);
144047c6ae99SBarry Smith }
1441264ace61SBarry Smith 
1442cab2e9ccSBarry Smith 
1443264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
1444264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
1445264ace61SBarry Smith 
1446264ace61SBarry Smith #undef __FUNCT__
1447264ace61SBarry Smith #define __FUNCT__ "DMSetType"
1448264ace61SBarry Smith /*@C
1449264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
1450264ace61SBarry Smith 
1451264ace61SBarry Smith   Collective on DM
1452264ace61SBarry Smith 
1453264ace61SBarry Smith   Input Parameters:
1454264ace61SBarry Smith + dm     - The DM object
1455264ace61SBarry Smith - method - The name of the DM type
1456264ace61SBarry Smith 
1457264ace61SBarry Smith   Options Database Key:
1458264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
1459264ace61SBarry Smith 
1460264ace61SBarry Smith   Notes:
1461e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
1462264ace61SBarry Smith 
1463264ace61SBarry Smith   Level: intermediate
1464264ace61SBarry Smith 
1465264ace61SBarry Smith .keywords: DM, set, type
1466264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
1467264ace61SBarry Smith @*/
14687087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
1469264ace61SBarry Smith {
1470264ace61SBarry Smith   PetscErrorCode (*r)(DM);
1471264ace61SBarry Smith   PetscBool      match;
1472264ace61SBarry Smith   PetscErrorCode ierr;
1473264ace61SBarry Smith 
1474264ace61SBarry Smith   PetscFunctionBegin;
1475264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1476264ace61SBarry Smith   ierr = PetscTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
1477264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
1478264ace61SBarry Smith 
1479264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
14804b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
1481264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
1482264ace61SBarry Smith 
1483264ace61SBarry Smith   if (dm->ops->destroy) {
1484264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
1485264ace61SBarry Smith   }
1486264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
1487264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
1488264ace61SBarry Smith   PetscFunctionReturn(0);
1489264ace61SBarry Smith }
1490264ace61SBarry Smith 
1491264ace61SBarry Smith #undef __FUNCT__
1492264ace61SBarry Smith #define __FUNCT__ "DMGetType"
1493264ace61SBarry Smith /*@C
1494264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
1495264ace61SBarry Smith 
1496264ace61SBarry Smith   Not Collective
1497264ace61SBarry Smith 
1498264ace61SBarry Smith   Input Parameter:
1499264ace61SBarry Smith . dm  - The DM
1500264ace61SBarry Smith 
1501264ace61SBarry Smith   Output Parameter:
1502264ace61SBarry Smith . type - The DM type name
1503264ace61SBarry Smith 
1504264ace61SBarry Smith   Level: intermediate
1505264ace61SBarry Smith 
1506264ace61SBarry Smith .keywords: DM, get, type, name
1507264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
1508264ace61SBarry Smith @*/
15097087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
1510264ace61SBarry Smith {
1511264ace61SBarry Smith   PetscErrorCode ierr;
1512264ace61SBarry Smith 
1513264ace61SBarry Smith   PetscFunctionBegin;
1514264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1515264ace61SBarry Smith   PetscValidCharPointer(type,2);
1516264ace61SBarry Smith   if (!DMRegisterAllCalled) {
1517264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
1518264ace61SBarry Smith   }
1519264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
1520264ace61SBarry Smith   PetscFunctionReturn(0);
1521264ace61SBarry Smith }
1522264ace61SBarry Smith 
152367a56275SMatthew G Knepley #undef __FUNCT__
152467a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
152567a56275SMatthew G Knepley /*@C
152667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
152767a56275SMatthew G Knepley 
152867a56275SMatthew G Knepley   Collective on DM
152967a56275SMatthew G Knepley 
153067a56275SMatthew G Knepley   Input Parameters:
153167a56275SMatthew G Knepley + dm - the DM
153267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
153367a56275SMatthew G Knepley 
153467a56275SMatthew G Knepley   Output Parameter:
153567a56275SMatthew G Knepley . M - pointer to new DM
153667a56275SMatthew G Knepley 
153767a56275SMatthew G Knepley   Notes:
153867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
153967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
154067a56275SMatthew G Knepley   of the input DM.
154167a56275SMatthew G Knepley 
154267a56275SMatthew G Knepley   Level: intermediate
154367a56275SMatthew G Knepley 
154467a56275SMatthew G Knepley .seealso: DMCreate()
154567a56275SMatthew G Knepley @*/
154667a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
154767a56275SMatthew G Knepley {
154867a56275SMatthew G Knepley   DM             B;
154967a56275SMatthew G Knepley   char           convname[256];
155067a56275SMatthew G Knepley   PetscBool      sametype, issame;
155167a56275SMatthew G Knepley   PetscErrorCode ierr;
155267a56275SMatthew G Knepley 
155367a56275SMatthew G Knepley   PetscFunctionBegin;
155467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
155567a56275SMatthew G Knepley   PetscValidType(dm,1);
155667a56275SMatthew G Knepley   PetscValidPointer(M,3);
155767a56275SMatthew G Knepley   ierr = PetscTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
155867a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
155967a56275SMatthew G Knepley   {
156067a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
156167a56275SMatthew G Knepley 
156267a56275SMatthew G Knepley     /*
156367a56275SMatthew G Knepley        Order of precedence:
156467a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
156567a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
156667a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
156767a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
156867a56275SMatthew G Knepley        5) Use a really basic converter.
156967a56275SMatthew G Knepley     */
157067a56275SMatthew G Knepley 
157167a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
157267a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
157367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
157467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
157567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
157667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
157767a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
157867a56275SMatthew G Knepley     if (conv) goto foundconv;
157967a56275SMatthew G Knepley 
158067a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
158167a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
158267a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
158367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
158467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
158567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
158667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
158767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
158867a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
158967a56275SMatthew G Knepley     if (conv) {
1590fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
159167a56275SMatthew G Knepley       goto foundconv;
159267a56275SMatthew G Knepley     }
159367a56275SMatthew G Knepley 
159467a56275SMatthew G Knepley #if 0
159567a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
159667a56275SMatthew G Knepley     conv = B->ops->convertfrom;
1597fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
159867a56275SMatthew G Knepley     if (conv) goto foundconv;
159967a56275SMatthew G Knepley 
160067a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
160167a56275SMatthew G Knepley     if (dm->ops->convert) {
160267a56275SMatthew G Knepley       conv = dm->ops->convert;
160367a56275SMatthew G Knepley     }
160467a56275SMatthew G Knepley     if (conv) goto foundconv;
160567a56275SMatthew G Knepley #endif
160667a56275SMatthew G Knepley 
160767a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
160867a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
160967a56275SMatthew G Knepley 
161067a56275SMatthew G Knepley     foundconv:
161167a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
161267a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
161367a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
161467a56275SMatthew G Knepley   }
161567a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
161667a56275SMatthew G Knepley   PetscFunctionReturn(0);
161767a56275SMatthew G Knepley }
1618264ace61SBarry Smith 
1619264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1620264ace61SBarry Smith 
1621264ace61SBarry Smith #undef __FUNCT__
1622264ace61SBarry Smith #define __FUNCT__ "DMRegister"
1623264ace61SBarry Smith /*@C
1624264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
1625264ace61SBarry Smith 
1626264ace61SBarry Smith   Level: advanced
1627264ace61SBarry Smith @*/
16287087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
1629264ace61SBarry Smith {
1630264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
1631264ace61SBarry Smith   PetscErrorCode ierr;
1632264ace61SBarry Smith 
1633264ace61SBarry Smith   PetscFunctionBegin;
1634264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
1635264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
1636264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
1637264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
1638264ace61SBarry Smith   PetscFunctionReturn(0);
1639264ace61SBarry Smith }
1640264ace61SBarry Smith 
1641264ace61SBarry Smith 
1642264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1643264ace61SBarry Smith #undef __FUNCT__
1644264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
1645264ace61SBarry Smith /*@C
1646264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
1647264ace61SBarry Smith 
1648264ace61SBarry Smith    Not Collective
1649264ace61SBarry Smith 
1650264ace61SBarry Smith    Level: advanced
1651264ace61SBarry Smith 
1652264ace61SBarry Smith .keywords: DM, register, destroy
1653264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
1654264ace61SBarry Smith @*/
16557087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
1656264ace61SBarry Smith {
1657264ace61SBarry Smith   PetscErrorCode ierr;
1658264ace61SBarry Smith 
1659264ace61SBarry Smith   PetscFunctionBegin;
1660264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
1661264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
1662264ace61SBarry Smith   PetscFunctionReturn(0);
1663264ace61SBarry Smith }
166423f975d1SBarry Smith 
166523f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
1666c6db04a5SJed Brown #include <mex.h>
166723f975d1SBarry Smith 
16683014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
166923f975d1SBarry Smith 
167023f975d1SBarry Smith #undef __FUNCT__
167123f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
167223f975d1SBarry Smith /*
167323f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
167423f975d1SBarry Smith                          DMSetFunctionMatlab().
167523f975d1SBarry Smith 
167623f975d1SBarry Smith    For linear problems x is null
167723f975d1SBarry Smith 
167823f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
167923f975d1SBarry Smith */
16807087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
168123f975d1SBarry Smith {
168223f975d1SBarry Smith   PetscErrorCode    ierr;
168323f975d1SBarry Smith   DMMatlabContext   *sctx;
168423f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
168523f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
168623f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
168723f975d1SBarry Smith 
168823f975d1SBarry Smith   PetscFunctionBegin;
168923f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
169023f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
169123f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
169223f975d1SBarry Smith 
169323f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
16941b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
169523f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
169623f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
16973014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
169823f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
169923f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
170023f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
170123f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
1702b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
170323f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
170423f975d1SBarry Smith   mxDestroyArray(prhs[0]);
170523f975d1SBarry Smith   mxDestroyArray(prhs[1]);
170623f975d1SBarry Smith   mxDestroyArray(prhs[2]);
170723f975d1SBarry Smith   mxDestroyArray(prhs[3]);
170823f975d1SBarry Smith   mxDestroyArray(plhs[0]);
170923f975d1SBarry Smith   PetscFunctionReturn(0);
171023f975d1SBarry Smith }
171123f975d1SBarry Smith 
171223f975d1SBarry Smith 
171323f975d1SBarry Smith #undef __FUNCT__
171423f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
171523f975d1SBarry Smith /*
171623f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
171723f975d1SBarry Smith 
171823f975d1SBarry Smith */
17197087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
172023f975d1SBarry Smith {
172123f975d1SBarry Smith   PetscErrorCode    ierr;
172223f975d1SBarry Smith   DMMatlabContext   *sctx;
172323f975d1SBarry Smith 
172423f975d1SBarry Smith   PetscFunctionBegin;
1725171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
172623f975d1SBarry Smith   /* currently sctx is memory bleed */
17271b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
17283014e516SBarry Smith   if (!sctx) {
172923f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
17303014e516SBarry Smith   }
173123f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
17321b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
173323f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
173423f975d1SBarry Smith   PetscFunctionReturn(0);
173523f975d1SBarry Smith }
17363014e516SBarry Smith 
17373014e516SBarry Smith #undef __FUNCT__
17383014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
17393014e516SBarry Smith /*
17403014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
17413014e516SBarry Smith                          DMSetJacobianMatlab().
17423014e516SBarry Smith 
17433014e516SBarry Smith    For linear problems x is null
17443014e516SBarry Smith 
17453014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
17463014e516SBarry Smith */
17477087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
17483014e516SBarry Smith {
17493014e516SBarry Smith   PetscErrorCode    ierr;
17503014e516SBarry Smith   DMMatlabContext   *sctx;
17513014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
17523014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
17533014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
17543014e516SBarry Smith 
17553014e516SBarry Smith   PetscFunctionBegin;
17563014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17573014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
17583014e516SBarry Smith 
1759e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
17601b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
17613014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
17623014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
17633014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
17643014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
17653014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
17663014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
17673014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
17683014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
17693014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
1770b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
1771c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
1772c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
17733014e516SBarry Smith   mxDestroyArray(prhs[0]);
17743014e516SBarry Smith   mxDestroyArray(prhs[1]);
17753014e516SBarry Smith   mxDestroyArray(prhs[2]);
17763014e516SBarry Smith   mxDestroyArray(prhs[3]);
17773014e516SBarry Smith   mxDestroyArray(prhs[4]);
17783014e516SBarry Smith   mxDestroyArray(plhs[0]);
17793014e516SBarry Smith   mxDestroyArray(plhs[1]);
17803014e516SBarry Smith   PetscFunctionReturn(0);
17813014e516SBarry Smith }
17823014e516SBarry Smith 
17833014e516SBarry Smith 
17843014e516SBarry Smith #undef __FUNCT__
17853014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
17863014e516SBarry Smith /*
17873014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
17883014e516SBarry Smith 
17893014e516SBarry Smith */
17907087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
17913014e516SBarry Smith {
17923014e516SBarry Smith   PetscErrorCode    ierr;
17933014e516SBarry Smith   DMMatlabContext   *sctx;
17943014e516SBarry Smith 
17953014e516SBarry Smith   PetscFunctionBegin;
1796171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17973014e516SBarry Smith   /* currently sctx is memory bleed */
17981b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
17993014e516SBarry Smith   if (!sctx) {
18003014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
18013014e516SBarry Smith   }
18023014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
18031b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
18043014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
18053014e516SBarry Smith   PetscFunctionReturn(0);
18063014e516SBarry Smith }
180723f975d1SBarry Smith #endif
1808b859378eSBarry Smith 
1809b859378eSBarry Smith #undef __FUNCT__
1810b859378eSBarry Smith #define __FUNCT__ "DMLoad"
1811b859378eSBarry Smith /*@C
1812b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
1813b859378eSBarry Smith   with DMView().
1814b859378eSBarry Smith 
1815b859378eSBarry Smith   Collective on PetscViewer
1816b859378eSBarry Smith 
1817b859378eSBarry Smith   Input Parameters:
1818b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
1819b859378eSBarry Smith            some related function before a call to DMLoad().
1820b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
1821b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
1822b859378eSBarry Smith 
1823b859378eSBarry Smith    Level: intermediate
1824b859378eSBarry Smith 
1825b859378eSBarry Smith   Notes:
1826b859378eSBarry Smith   Defaults to the DM DA.
1827b859378eSBarry Smith 
1828b859378eSBarry Smith   Notes for advanced users:
1829b859378eSBarry Smith   Most users should not need to know the details of the binary storage
1830b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
1831b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
1832b859378eSBarry Smith   format is
1833b859378eSBarry Smith .vb
1834b859378eSBarry Smith      has not yet been determined
1835b859378eSBarry Smith .ve
1836b859378eSBarry Smith 
1837b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
1838b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
1839b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
1840b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
1841b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
1842b859378eSBarry Smith 
1843b859378eSBarry Smith   Concepts: vector^loading from file
1844b859378eSBarry Smith 
1845b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
1846b859378eSBarry Smith @*/
1847b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
1848b859378eSBarry Smith {
1849b859378eSBarry Smith   PetscErrorCode ierr;
1850b859378eSBarry Smith 
1851b859378eSBarry Smith   PetscFunctionBegin;
1852b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
1853b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1854b859378eSBarry Smith 
1855b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
1856b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
1857b859378eSBarry Smith   }
1858b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
1859b859378eSBarry Smith   PetscFunctionReturn(0);
1860b859378eSBarry Smith }
1861b859378eSBarry Smith 
1862*7da65231SMatthew G Knepley /******************************** FEM Support **********************************/
1863*7da65231SMatthew G Knepley 
1864*7da65231SMatthew G Knepley #undef __FUNCT__
1865*7da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
1866*7da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
1867*7da65231SMatthew G Knepley   PetscErrorCode ierr;
1868*7da65231SMatthew G Knepley 
1869*7da65231SMatthew G Knepley   PetscFunctionBegin;
1870*7da65231SMatthew G Knepley   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %d Element %s\n", c, name);CHKERRQ(ierr);
1871*7da65231SMatthew G Knepley   for(PetscInt f = 0; f < len; ++f) {
1872*7da65231SMatthew G Knepley     PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", x[f]);
1873*7da65231SMatthew G Knepley   }
1874*7da65231SMatthew G Knepley   PetscFunctionReturn(0);
1875*7da65231SMatthew G Knepley }
1876*7da65231SMatthew G Knepley 
1877*7da65231SMatthew G Knepley #undef __FUNCT__
1878*7da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
1879*7da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
1880*7da65231SMatthew G Knepley   PetscErrorCode ierr;
1881*7da65231SMatthew G Knepley 
1882*7da65231SMatthew G Knepley   PetscFunctionBegin;
1883*7da65231SMatthew G Knepley   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %d Element %s\n", c, name);CHKERRQ(ierr);
1884*7da65231SMatthew G Knepley   for(int f = 0; f < rows; ++f) {
1885*7da65231SMatthew G Knepley     PetscPrintf(PETSC_COMM_SELF, "  |");
1886*7da65231SMatthew G Knepley     for(int g = 0; g < cols; ++g) {
1887*7da65231SMatthew G Knepley       PetscPrintf(PETSC_COMM_SELF, " % 9.5g", A[f*cols+g]);
1888*7da65231SMatthew G Knepley     }
1889*7da65231SMatthew G Knepley     PetscPrintf(PETSC_COMM_SELF, " |\n");
1890*7da65231SMatthew G Knepley   }
1891*7da65231SMatthew G Knepley   PetscFunctionReturn(0);
1892*7da65231SMatthew G Knepley }
1893