xref: /petsc/src/dm/interface/dm.c (revision 25296bd559e7c54e980de28eb47cb392db6b5766)
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 
44*a89ea682SMatthew G Knepley   v->workSize     = 0;
45*a89ea682SMatthew 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);
175*a89ea682SMatthew 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 {
237073dac72SJed Brown   PetscBool      flg1 = 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);
245073dac72SJed 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);
246f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
247f9ba7244SBarry Smith     if (flg) {
248f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
249f9ba7244SBarry Smith     }
250f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_mat_type","Matrix type","MatSetType",MatList,typeName,typeName,sizeof typeName,&flg);CHKERRQ(ierr);
251073dac72SJed Brown     if (flg) {
252073dac72SJed Brown       ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
253f9ba7244SBarry Smith       ierr = PetscStrallocpy(typeName,&dm->mattype);CHKERRQ(ierr);
254073dac72SJed Brown     }
2551b89239cSHong 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);
256f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
257f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
258f9ba7244SBarry Smith     }
259f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
260f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
26182fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
26282fcb398SMatthew G Knepley   if (flg1) {
26382fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
26482fcb398SMatthew G Knepley   }
265d7bf68aeSBarry Smith   PetscFunctionReturn(0);
266d7bf68aeSBarry Smith }
267d7bf68aeSBarry Smith 
268d7bf68aeSBarry Smith #undef __FUNCT__
26947c6ae99SBarry Smith #define __FUNCT__ "DMView"
270fc9bc008SSatish Balay /*@C
271aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
27247c6ae99SBarry Smith 
27347c6ae99SBarry Smith     Collective on DM
27447c6ae99SBarry Smith 
27547c6ae99SBarry Smith     Input Parameter:
27647c6ae99SBarry Smith +   dm - the DM object to view
27747c6ae99SBarry Smith -   v - the viewer
27847c6ae99SBarry Smith 
27947c6ae99SBarry Smith     Level: developer
28047c6ae99SBarry Smith 
281e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
28247c6ae99SBarry Smith 
28347c6ae99SBarry Smith @*/
2847087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
28547c6ae99SBarry Smith {
28647c6ae99SBarry Smith   PetscErrorCode ierr;
28747c6ae99SBarry Smith 
28847c6ae99SBarry Smith   PetscFunctionBegin;
289171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2903014e516SBarry Smith  if (!v) {
2913014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
2923014e516SBarry Smith   }
2930c010503SBarry Smith   if (dm->ops->view) {
2940c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
29547c6ae99SBarry Smith   }
29647c6ae99SBarry Smith   PetscFunctionReturn(0);
29747c6ae99SBarry Smith }
29847c6ae99SBarry Smith 
29947c6ae99SBarry Smith #undef __FUNCT__
30047c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
30147c6ae99SBarry Smith /*@
302aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
30347c6ae99SBarry Smith 
30447c6ae99SBarry Smith     Collective on DM
30547c6ae99SBarry Smith 
30647c6ae99SBarry Smith     Input Parameter:
30747c6ae99SBarry Smith .   dm - the DM object
30847c6ae99SBarry Smith 
30947c6ae99SBarry Smith     Output Parameter:
31047c6ae99SBarry Smith .   vec - the global vector
31147c6ae99SBarry Smith 
312073dac72SJed Brown     Level: beginner
31347c6ae99SBarry Smith 
314e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
31547c6ae99SBarry Smith 
31647c6ae99SBarry Smith @*/
3177087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
31847c6ae99SBarry Smith {
31947c6ae99SBarry Smith   PetscErrorCode ierr;
32047c6ae99SBarry Smith 
32147c6ae99SBarry Smith   PetscFunctionBegin;
322171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32347c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
32447c6ae99SBarry Smith   PetscFunctionReturn(0);
32547c6ae99SBarry Smith }
32647c6ae99SBarry Smith 
32747c6ae99SBarry Smith #undef __FUNCT__
32847c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
32947c6ae99SBarry Smith /*@
330aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
33147c6ae99SBarry Smith 
33247c6ae99SBarry Smith     Not Collective
33347c6ae99SBarry Smith 
33447c6ae99SBarry Smith     Input Parameter:
33547c6ae99SBarry Smith .   dm - the DM object
33647c6ae99SBarry Smith 
33747c6ae99SBarry Smith     Output Parameter:
33847c6ae99SBarry Smith .   vec - the local vector
33947c6ae99SBarry Smith 
340073dac72SJed Brown     Level: beginner
34147c6ae99SBarry Smith 
342e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
34347c6ae99SBarry Smith 
34447c6ae99SBarry Smith @*/
3457087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
34647c6ae99SBarry Smith {
34747c6ae99SBarry Smith   PetscErrorCode ierr;
34847c6ae99SBarry Smith 
34947c6ae99SBarry Smith   PetscFunctionBegin;
350171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
35147c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
35247c6ae99SBarry Smith   PetscFunctionReturn(0);
35347c6ae99SBarry Smith }
35447c6ae99SBarry Smith 
35547c6ae99SBarry Smith #undef __FUNCT__
3561411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
3571411c6eeSJed Brown /*@
3581411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
3591411c6eeSJed Brown 
3601411c6eeSJed Brown    Collective on DM
3611411c6eeSJed Brown 
3621411c6eeSJed Brown    Input Parameter:
3631411c6eeSJed Brown .  dm - the DM that provides the mapping
3641411c6eeSJed Brown 
3651411c6eeSJed Brown    Output Parameter:
3661411c6eeSJed Brown .  ltog - the mapping
3671411c6eeSJed Brown 
3681411c6eeSJed Brown    Level: intermediate
3691411c6eeSJed Brown 
3701411c6eeSJed Brown    Notes:
3711411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
3721411c6eeSJed Brown    MatSetLocalToGlobalMapping().
3731411c6eeSJed Brown 
3741411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
3751411c6eeSJed Brown @*/
3767087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
3771411c6eeSJed Brown {
3781411c6eeSJed Brown   PetscErrorCode ierr;
3791411c6eeSJed Brown 
3801411c6eeSJed Brown   PetscFunctionBegin;
3811411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3821411c6eeSJed Brown   PetscValidPointer(ltog,2);
3831411c6eeSJed Brown   if (!dm->ltogmap) {
3841411c6eeSJed Brown     if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
3851411c6eeSJed Brown     ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
3861411c6eeSJed Brown   }
3871411c6eeSJed Brown   *ltog = dm->ltogmap;
3881411c6eeSJed Brown   PetscFunctionReturn(0);
3891411c6eeSJed Brown }
3901411c6eeSJed Brown 
3911411c6eeSJed Brown #undef __FUNCT__
3921411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
3931411c6eeSJed Brown /*@
3941411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
3951411c6eeSJed Brown 
3961411c6eeSJed Brown    Collective on DM
3971411c6eeSJed Brown 
3981411c6eeSJed Brown    Input Parameter:
3991411c6eeSJed Brown .  da - the distributed array that provides the mapping
4001411c6eeSJed Brown 
4011411c6eeSJed Brown    Output Parameter:
4021411c6eeSJed Brown .  ltog - the block mapping
4031411c6eeSJed Brown 
4041411c6eeSJed Brown    Level: intermediate
4051411c6eeSJed Brown 
4061411c6eeSJed Brown    Notes:
4071411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
4081411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
4091411c6eeSJed Brown 
4101411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
4111411c6eeSJed Brown @*/
4127087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
4131411c6eeSJed Brown {
4141411c6eeSJed Brown   PetscErrorCode ierr;
4151411c6eeSJed Brown 
4161411c6eeSJed Brown   PetscFunctionBegin;
4171411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4181411c6eeSJed Brown   PetscValidPointer(ltog,2);
4191411c6eeSJed Brown   if (!dm->ltogmapb) {
4201411c6eeSJed Brown     PetscInt bs;
4211411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
4221411c6eeSJed Brown     if (bs > 1) {
4231411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
4241411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
4251411c6eeSJed Brown     } else {
4261411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
4271411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
4281411c6eeSJed Brown     }
4291411c6eeSJed Brown   }
4301411c6eeSJed Brown   *ltog = dm->ltogmapb;
4311411c6eeSJed Brown   PetscFunctionReturn(0);
4321411c6eeSJed Brown }
4331411c6eeSJed Brown 
4341411c6eeSJed Brown #undef __FUNCT__
4351411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
4361411c6eeSJed Brown /*@
4371411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
4381411c6eeSJed Brown 
4391411c6eeSJed Brown    Not Collective
4401411c6eeSJed Brown 
4411411c6eeSJed Brown    Input Parameter:
4421411c6eeSJed Brown .  dm - the DM with block structure
4431411c6eeSJed Brown 
4441411c6eeSJed Brown    Output Parameter:
4451411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
4461411c6eeSJed Brown 
4471411c6eeSJed Brown    Level: intermediate
4481411c6eeSJed Brown 
4491411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
4501411c6eeSJed Brown @*/
4517087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
4521411c6eeSJed Brown {
4531411c6eeSJed Brown   PetscFunctionBegin;
4541411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4551411c6eeSJed Brown   PetscValidPointer(bs,2);
4561411c6eeSJed 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");
4571411c6eeSJed Brown   *bs = dm->bs;
4581411c6eeSJed Brown   PetscFunctionReturn(0);
4591411c6eeSJed Brown }
4601411c6eeSJed Brown 
4611411c6eeSJed Brown #undef __FUNCT__
462e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
46347c6ae99SBarry Smith /*@
464e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
46547c6ae99SBarry Smith 
46647c6ae99SBarry Smith     Collective on DM
46747c6ae99SBarry Smith 
46847c6ae99SBarry Smith     Input Parameter:
46947c6ae99SBarry Smith +   dm1 - the DM object
47047c6ae99SBarry Smith -   dm2 - the second, finer DM object
47147c6ae99SBarry Smith 
47247c6ae99SBarry Smith     Output Parameter:
47347c6ae99SBarry Smith +  mat - the interpolation
47447c6ae99SBarry Smith -  vec - the scaling (optional)
47547c6ae99SBarry Smith 
47647c6ae99SBarry Smith     Level: developer
47747c6ae99SBarry Smith 
47885afcc9aSBarry 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
47985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
480d52bd9f3SBarry Smith 
481d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
482d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
48385afcc9aSBarry Smith 
48485afcc9aSBarry Smith 
485e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
48647c6ae99SBarry Smith 
48747c6ae99SBarry Smith @*/
488e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
48947c6ae99SBarry Smith {
49047c6ae99SBarry Smith   PetscErrorCode ierr;
49147c6ae99SBarry Smith 
49247c6ae99SBarry Smith   PetscFunctionBegin;
493171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
494171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
49525296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
49647c6ae99SBarry Smith   PetscFunctionReturn(0);
49747c6ae99SBarry Smith }
49847c6ae99SBarry Smith 
49947c6ae99SBarry Smith #undef __FUNCT__
500e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
50147c6ae99SBarry Smith /*@
502e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
50347c6ae99SBarry Smith 
50447c6ae99SBarry Smith     Collective on DM
50547c6ae99SBarry Smith 
50647c6ae99SBarry Smith     Input Parameter:
50747c6ae99SBarry Smith +   dm1 - the DM object
50847c6ae99SBarry Smith -   dm2 - the second, finer DM object
50947c6ae99SBarry Smith 
51047c6ae99SBarry Smith     Output Parameter:
51147c6ae99SBarry Smith .   ctx - the injection
51247c6ae99SBarry Smith 
51347c6ae99SBarry Smith     Level: developer
51447c6ae99SBarry Smith 
51585afcc9aSBarry 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
51685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
51785afcc9aSBarry Smith 
518e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
51947c6ae99SBarry Smith 
52047c6ae99SBarry Smith @*/
521e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
52247c6ae99SBarry Smith {
52347c6ae99SBarry Smith   PetscErrorCode ierr;
52447c6ae99SBarry Smith 
52547c6ae99SBarry Smith   PetscFunctionBegin;
526171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
527171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
52847c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
52947c6ae99SBarry Smith   PetscFunctionReturn(0);
53047c6ae99SBarry Smith }
53147c6ae99SBarry Smith 
53247c6ae99SBarry Smith #undef __FUNCT__
533e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
534d1e2c406SBarry Smith /*@C
535e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
53647c6ae99SBarry Smith 
53747c6ae99SBarry Smith     Collective on DM
53847c6ae99SBarry Smith 
53947c6ae99SBarry Smith     Input Parameter:
54047c6ae99SBarry Smith +   dm - the DM object
54147c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
54247c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
54347c6ae99SBarry Smith 
54447c6ae99SBarry Smith     Output Parameter:
54547c6ae99SBarry Smith .   coloring - the coloring
54647c6ae99SBarry Smith 
54747c6ae99SBarry Smith     Level: developer
54847c6ae99SBarry Smith 
549e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
55047c6ae99SBarry Smith 
551aab9d709SJed Brown @*/
552e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
55347c6ae99SBarry Smith {
55447c6ae99SBarry Smith   PetscErrorCode ierr;
55547c6ae99SBarry Smith 
55647c6ae99SBarry Smith   PetscFunctionBegin;
557171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55847c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
55947c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
56047c6ae99SBarry Smith   PetscFunctionReturn(0);
56147c6ae99SBarry Smith }
56247c6ae99SBarry Smith 
56347c6ae99SBarry Smith #undef __FUNCT__
564950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
56547c6ae99SBarry Smith /*@C
566950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
56747c6ae99SBarry Smith 
56847c6ae99SBarry Smith     Collective on DM
56947c6ae99SBarry Smith 
57047c6ae99SBarry Smith     Input Parameter:
57147c6ae99SBarry Smith +   dm - the DM object
57247c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
57394013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
57447c6ae99SBarry Smith 
57547c6ae99SBarry Smith     Output Parameter:
57647c6ae99SBarry Smith .   mat - the empty Jacobian
57747c6ae99SBarry Smith 
578073dac72SJed Brown     Level: beginner
57947c6ae99SBarry Smith 
58094013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
58194013140SBarry Smith        do not need to do it yourself.
58294013140SBarry Smith 
58394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
584aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
58594013140SBarry Smith 
58694013140SBarry 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
58794013140SBarry Smith        internally by PETSc.
58894013140SBarry Smith 
58994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
590aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
59194013140SBarry Smith 
592e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
59347c6ae99SBarry Smith 
594aab9d709SJed Brown @*/
595950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
59647c6ae99SBarry Smith {
59747c6ae99SBarry Smith   PetscErrorCode ierr;
59847c6ae99SBarry Smith 
59947c6ae99SBarry Smith   PetscFunctionBegin;
600171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
601235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
602235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
603235683edSBarry Smith #endif
604c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
605c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
606073dac72SJed Brown   if (dm->mattype) {
60725296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
608073dac72SJed Brown   } else {
60925296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
610c7b7c8a4SJed Brown   }
61147c6ae99SBarry Smith   PetscFunctionReturn(0);
61247c6ae99SBarry Smith }
61347c6ae99SBarry Smith 
61447c6ae99SBarry Smith #undef __FUNCT__
615732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
616732e2eb9SMatthew G Knepley /*@
617950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
618732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
619732e2eb9SMatthew G Knepley 
620732e2eb9SMatthew G Knepley   Logically Collective on DMDA
621732e2eb9SMatthew G Knepley 
622732e2eb9SMatthew G Knepley   Input Parameter:
623732e2eb9SMatthew G Knepley + dm - the DM
624732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
625732e2eb9SMatthew G Knepley 
626732e2eb9SMatthew G Knepley   Level: developer
627950540a4SJed Brown .seealso DMCreateMatrix()
628732e2eb9SMatthew G Knepley @*/
629732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
630732e2eb9SMatthew G Knepley {
631732e2eb9SMatthew G Knepley   PetscFunctionBegin;
632732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
633732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
634732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
635732e2eb9SMatthew G Knepley }
636732e2eb9SMatthew G Knepley 
637732e2eb9SMatthew G Knepley #undef __FUNCT__
638*a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
639*a89ea682SMatthew G Knepley /*@C
640*a89ea682SMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size
641*a89ea682SMatthew G Knepley 
642*a89ea682SMatthew G Knepley   Not Collective
643*a89ea682SMatthew G Knepley 
644*a89ea682SMatthew G Knepley   Input Parameters:
645*a89ea682SMatthew G Knepley + dm - the DM object
646*a89ea682SMatthew G Knepley - size - The minium size
647*a89ea682SMatthew G Knepley 
648*a89ea682SMatthew G Knepley   Output Parameter:
649*a89ea682SMatthew G Knepley . array - the work array
650*a89ea682SMatthew G Knepley 
651*a89ea682SMatthew G Knepley   Level: developer
652*a89ea682SMatthew G Knepley 
653*a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
654*a89ea682SMatthew G Knepley @*/
655*a89ea682SMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt size,PetscScalar **array)
656*a89ea682SMatthew G Knepley {
657*a89ea682SMatthew G Knepley   PetscErrorCode ierr;
658*a89ea682SMatthew G Knepley 
659*a89ea682SMatthew G Knepley   PetscFunctionBegin;
660*a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
661*a89ea682SMatthew G Knepley   PetscValidPointer(array,3);
662*a89ea682SMatthew G Knepley   if (size > dm->workSize) {
663*a89ea682SMatthew G Knepley     dm->workSize = size;
664*a89ea682SMatthew G Knepley     ierr = PetscFree(dm->workArray);CHKERRQ(ierr);
665*a89ea682SMatthew G Knepley     ierr = PetscMalloc(dm->workSize * sizeof(PetscScalar), &dm->workArray);CHKERRQ(ierr);
666*a89ea682SMatthew G Knepley   }
667*a89ea682SMatthew G Knepley   *array = dm->workArray;
668*a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
669*a89ea682SMatthew G Knepley }
670*a89ea682SMatthew G Knepley 
671*a89ea682SMatthew G Knepley #undef __FUNCT__
67247c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
67347c6ae99SBarry Smith /*@
67447c6ae99SBarry Smith     DMRefine - Refines a DM object
67547c6ae99SBarry Smith 
67647c6ae99SBarry Smith     Collective on DM
67747c6ae99SBarry Smith 
67847c6ae99SBarry Smith     Input Parameter:
67947c6ae99SBarry Smith +   dm - the DM object
68047c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
68147c6ae99SBarry Smith 
68247c6ae99SBarry Smith     Output Parameter:
68347c6ae99SBarry Smith .   dmf - the refined DM
68447c6ae99SBarry Smith 
68547c6ae99SBarry Smith     Level: developer
68647c6ae99SBarry Smith 
687e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
68847c6ae99SBarry Smith 
68947c6ae99SBarry Smith @*/
6907087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
69147c6ae99SBarry Smith {
69247c6ae99SBarry Smith   PetscErrorCode ierr;
69347c6ae99SBarry Smith 
69447c6ae99SBarry Smith   PetscFunctionBegin;
695732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
69647c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
697644e2e5bSBarry Smith   (*dmf)->ops->initialguess = dm->ops->initialguess;
698644e2e5bSBarry Smith   (*dmf)->ops->function     = dm->ops->function;
699644e2e5bSBarry Smith   (*dmf)->ops->functionj    = dm->ops->functionj;
700644e2e5bSBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
701644e2e5bSBarry Smith     (*dmf)->ops->jacobian     = dm->ops->jacobian;
702644e2e5bSBarry Smith   }
7038cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
704644e2e5bSBarry Smith   (*dmf)->ctx     = dm->ctx;
705656b349aSBarry Smith   (*dmf)->levelup = dm->levelup + 1;
70647c6ae99SBarry Smith   PetscFunctionReturn(0);
70747c6ae99SBarry Smith }
70847c6ae99SBarry Smith 
70947c6ae99SBarry Smith #undef __FUNCT__
710eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
711eb3f98d2SBarry Smith /*@
712eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
713eb3f98d2SBarry Smith 
714eb3f98d2SBarry Smith     Not Collective
715eb3f98d2SBarry Smith 
716eb3f98d2SBarry Smith     Input Parameter:
717eb3f98d2SBarry Smith .   dm - the DM object
718eb3f98d2SBarry Smith 
719eb3f98d2SBarry Smith     Output Parameter:
720eb3f98d2SBarry Smith .   level - number of refinements
721eb3f98d2SBarry Smith 
722eb3f98d2SBarry Smith     Level: developer
723eb3f98d2SBarry Smith 
724e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
725eb3f98d2SBarry Smith 
726eb3f98d2SBarry Smith @*/
727eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
728eb3f98d2SBarry Smith {
729eb3f98d2SBarry Smith   PetscFunctionBegin;
730eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
731eb3f98d2SBarry Smith   *level = dm->levelup;
732eb3f98d2SBarry Smith   PetscFunctionReturn(0);
733eb3f98d2SBarry Smith }
734eb3f98d2SBarry Smith 
735eb3f98d2SBarry Smith #undef __FUNCT__
73647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
73747c6ae99SBarry Smith /*@
73847c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
73947c6ae99SBarry Smith 
74047c6ae99SBarry Smith     Neighbor-wise Collective on DM
74147c6ae99SBarry Smith 
74247c6ae99SBarry Smith     Input Parameters:
74347c6ae99SBarry Smith +   dm - the DM object
74447c6ae99SBarry Smith .   g - the global vector
74547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
74647c6ae99SBarry Smith -   l - the local vector
74747c6ae99SBarry Smith 
74847c6ae99SBarry Smith 
74947c6ae99SBarry Smith     Level: beginner
75047c6ae99SBarry Smith 
751e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
75247c6ae99SBarry Smith 
75347c6ae99SBarry Smith @*/
7547087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
75547c6ae99SBarry Smith {
75647c6ae99SBarry Smith   PetscErrorCode ierr;
75747c6ae99SBarry Smith 
75847c6ae99SBarry Smith   PetscFunctionBegin;
759171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
760843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
76147c6ae99SBarry Smith   PetscFunctionReturn(0);
76247c6ae99SBarry Smith }
76347c6ae99SBarry Smith 
76447c6ae99SBarry Smith #undef __FUNCT__
76547c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
76647c6ae99SBarry Smith /*@
76747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
76847c6ae99SBarry Smith 
76947c6ae99SBarry Smith     Neighbor-wise Collective on DM
77047c6ae99SBarry Smith 
77147c6ae99SBarry Smith     Input Parameters:
77247c6ae99SBarry Smith +   dm - the DM object
77347c6ae99SBarry Smith .   g - the global vector
77447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
77547c6ae99SBarry Smith -   l - the local vector
77647c6ae99SBarry Smith 
77747c6ae99SBarry Smith 
77847c6ae99SBarry Smith     Level: beginner
77947c6ae99SBarry Smith 
780e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
78147c6ae99SBarry Smith 
78247c6ae99SBarry Smith @*/
7837087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
78447c6ae99SBarry Smith {
78547c6ae99SBarry Smith   PetscErrorCode ierr;
78647c6ae99SBarry Smith 
78747c6ae99SBarry Smith   PetscFunctionBegin;
788171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
789843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
79047c6ae99SBarry Smith   PetscFunctionReturn(0);
79147c6ae99SBarry Smith }
79247c6ae99SBarry Smith 
79347c6ae99SBarry Smith #undef __FUNCT__
7949a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
79547c6ae99SBarry Smith /*@
7969a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
7979a42bb27SBarry Smith 
7989a42bb27SBarry Smith     Neighbor-wise Collective on DM
7999a42bb27SBarry Smith 
8009a42bb27SBarry Smith     Input Parameters:
8019a42bb27SBarry Smith +   dm - the DM object
802f6813fd5SJed Brown .   l - the local vector
8039a42bb27SBarry 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
8049a42bb27SBarry Smith            base point.
805f6813fd5SJed Brown - - the global vector
8069a42bb27SBarry Smith 
8079a42bb27SBarry 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
8089a42bb27SBarry 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
8099a42bb27SBarry Smith            global array to the final global array with VecAXPY().
8109a42bb27SBarry Smith 
8119a42bb27SBarry Smith     Level: beginner
8129a42bb27SBarry Smith 
813e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
8149a42bb27SBarry Smith 
8159a42bb27SBarry Smith @*/
8167087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
8179a42bb27SBarry Smith {
8189a42bb27SBarry Smith   PetscErrorCode ierr;
8199a42bb27SBarry Smith 
8209a42bb27SBarry Smith   PetscFunctionBegin;
821171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
822843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
8239a42bb27SBarry Smith   PetscFunctionReturn(0);
8249a42bb27SBarry Smith }
8259a42bb27SBarry Smith 
8269a42bb27SBarry Smith #undef __FUNCT__
8279a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
8289a42bb27SBarry Smith /*@
8299a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
83047c6ae99SBarry Smith 
83147c6ae99SBarry Smith     Neighbor-wise Collective on DM
83247c6ae99SBarry Smith 
83347c6ae99SBarry Smith     Input Parameters:
83447c6ae99SBarry Smith +   dm - the DM object
835f6813fd5SJed Brown .   l - the local vector
83647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
837f6813fd5SJed Brown -   g - the global vector
83847c6ae99SBarry Smith 
83947c6ae99SBarry Smith 
84047c6ae99SBarry Smith     Level: beginner
84147c6ae99SBarry Smith 
842e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
84347c6ae99SBarry Smith 
84447c6ae99SBarry Smith @*/
8457087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
84647c6ae99SBarry Smith {
84747c6ae99SBarry Smith   PetscErrorCode ierr;
84847c6ae99SBarry Smith 
84947c6ae99SBarry Smith   PetscFunctionBegin;
850171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
851843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
85247c6ae99SBarry Smith   PetscFunctionReturn(0);
85347c6ae99SBarry Smith }
85447c6ae99SBarry Smith 
85547c6ae99SBarry Smith #undef __FUNCT__
85647c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
85747c6ae99SBarry Smith /*@
85847c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
85947c6ae99SBarry Smith 
86047c6ae99SBarry Smith     Collective on DM
86147c6ae99SBarry Smith 
86247c6ae99SBarry Smith     Input Parameter:
86347c6ae99SBarry Smith +   dm - the DM object
86447c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
86547c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
86647c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith     Level: developer
86947c6ae99SBarry Smith 
870e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
87147c6ae99SBarry Smith          DMSetFunction()
87247c6ae99SBarry Smith 
87347c6ae99SBarry Smith @*/
8747087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
87547c6ae99SBarry Smith {
87647c6ae99SBarry Smith   PetscErrorCode ierr;
877171400e9SBarry Smith 
87847c6ae99SBarry Smith   PetscFunctionBegin;
879171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
88047c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
88147c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
88247c6ae99SBarry Smith   if (A != B) {
88347c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
88447c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
88547c6ae99SBarry Smith   }
88647c6ae99SBarry Smith   PetscFunctionReturn(0);
88747c6ae99SBarry Smith }
88847c6ae99SBarry Smith 
88947c6ae99SBarry Smith #undef __FUNCT__
89047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
89147c6ae99SBarry Smith /*@
89247c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
89347c6ae99SBarry Smith 
89447c6ae99SBarry Smith     Collective on DM
89547c6ae99SBarry Smith 
89647c6ae99SBarry Smith     Input Parameter:
89747c6ae99SBarry Smith +   dm - the DM object
89847c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
89947c6ae99SBarry Smith 
90047c6ae99SBarry Smith     Output Parameter:
90147c6ae99SBarry Smith .   dmc - the coarsened DM
90247c6ae99SBarry Smith 
90347c6ae99SBarry Smith     Level: developer
90447c6ae99SBarry Smith 
905e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
90647c6ae99SBarry Smith 
90747c6ae99SBarry Smith @*/
9087087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
90947c6ae99SBarry Smith {
91047c6ae99SBarry Smith   PetscErrorCode ierr;
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith   PetscFunctionBegin;
913171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
91447c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
91547c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
91647c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
91747c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
91847c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
91947c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
92047c6ae99SBarry Smith   }
9218cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
922644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
923656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
92447c6ae99SBarry Smith   PetscFunctionReturn(0);
92547c6ae99SBarry Smith }
92647c6ae99SBarry Smith 
92747c6ae99SBarry Smith #undef __FUNCT__
92847c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
92947c6ae99SBarry Smith /*@C
93047c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
93147c6ae99SBarry Smith 
93247c6ae99SBarry Smith     Collective on DM
93347c6ae99SBarry Smith 
93447c6ae99SBarry Smith     Input Parameter:
93547c6ae99SBarry Smith +   dm - the DM object
93647c6ae99SBarry Smith -   nlevels - the number of levels of refinement
93747c6ae99SBarry Smith 
93847c6ae99SBarry Smith     Output Parameter:
93947c6ae99SBarry Smith .   dmf - the refined DM hierarchy
94047c6ae99SBarry Smith 
94147c6ae99SBarry Smith     Level: developer
94247c6ae99SBarry Smith 
943e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
94447c6ae99SBarry Smith 
94547c6ae99SBarry Smith @*/
9467087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
94747c6ae99SBarry Smith {
94847c6ae99SBarry Smith   PetscErrorCode ierr;
94947c6ae99SBarry Smith 
95047c6ae99SBarry Smith   PetscFunctionBegin;
951171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
95247c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
95347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
95447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
95547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
95647c6ae99SBarry Smith   } else if (dm->ops->refine) {
95747c6ae99SBarry Smith     PetscInt i;
95847c6ae99SBarry Smith 
95947c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
96047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
96147c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
96247c6ae99SBarry Smith     }
96347c6ae99SBarry Smith   } else {
96447c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
96547c6ae99SBarry Smith   }
96647c6ae99SBarry Smith   PetscFunctionReturn(0);
96747c6ae99SBarry Smith }
96847c6ae99SBarry Smith 
96947c6ae99SBarry Smith #undef __FUNCT__
97047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
97147c6ae99SBarry Smith /*@C
97247c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
97347c6ae99SBarry Smith 
97447c6ae99SBarry Smith     Collective on DM
97547c6ae99SBarry Smith 
97647c6ae99SBarry Smith     Input Parameter:
97747c6ae99SBarry Smith +   dm - the DM object
97847c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
97947c6ae99SBarry Smith 
98047c6ae99SBarry Smith     Output Parameter:
98147c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
98247c6ae99SBarry Smith 
98347c6ae99SBarry Smith     Level: developer
98447c6ae99SBarry Smith 
985e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
98647c6ae99SBarry Smith 
98747c6ae99SBarry Smith @*/
9887087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
98947c6ae99SBarry Smith {
99047c6ae99SBarry Smith   PetscErrorCode ierr;
99147c6ae99SBarry Smith 
99247c6ae99SBarry Smith   PetscFunctionBegin;
993171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
99447c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
99547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
99647c6ae99SBarry Smith   PetscValidPointer(dmc,3);
99747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
99847c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
99947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
100047c6ae99SBarry Smith     PetscInt i;
100147c6ae99SBarry Smith 
100247c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
100347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
100447c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
100547c6ae99SBarry Smith     }
100647c6ae99SBarry Smith   } else {
100747c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
100847c6ae99SBarry Smith   }
100947c6ae99SBarry Smith   PetscFunctionReturn(0);
101047c6ae99SBarry Smith }
101147c6ae99SBarry Smith 
101247c6ae99SBarry Smith #undef __FUNCT__
1013e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
101447c6ae99SBarry Smith /*@
1015e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
101647c6ae99SBarry Smith    grids associated with two DMs.
101747c6ae99SBarry Smith 
101847c6ae99SBarry Smith    Collective on DM
101947c6ae99SBarry Smith 
102047c6ae99SBarry Smith    Input Parameters:
102147c6ae99SBarry Smith +  dmc - the coarse grid DM
102247c6ae99SBarry Smith -  dmf - the fine grid DM
102347c6ae99SBarry Smith 
102447c6ae99SBarry Smith    Output Parameters:
102547c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
102647c6ae99SBarry Smith 
102747c6ae99SBarry Smith    Level: intermediate
102847c6ae99SBarry Smith 
102947c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
103047c6ae99SBarry Smith 
1031e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
103247c6ae99SBarry Smith @*/
1033e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
103447c6ae99SBarry Smith {
103547c6ae99SBarry Smith   PetscErrorCode ierr;
103647c6ae99SBarry Smith 
103747c6ae99SBarry Smith   PetscFunctionBegin;
1038171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1039171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
104047c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
104147c6ae99SBarry Smith   PetscFunctionReturn(0);
104247c6ae99SBarry Smith }
104347c6ae99SBarry Smith 
104447c6ae99SBarry Smith #undef __FUNCT__
10451a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
10461a266240SBarry Smith /*@C
10471a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
10481a266240SBarry Smith 
10491a266240SBarry Smith     Not Collective
10501a266240SBarry Smith 
10511a266240SBarry Smith     Input Parameters:
10521a266240SBarry Smith +   dm - the DM object
10531a266240SBarry Smith -   destroy - the destroy function
10541a266240SBarry Smith 
10551a266240SBarry Smith     Level: intermediate
10561a266240SBarry Smith 
1057e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
10581a266240SBarry Smith 
1059f07f9ceaSJed Brown @*/
10601a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
10611a266240SBarry Smith {
10621a266240SBarry Smith   PetscFunctionBegin;
1063171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10641a266240SBarry Smith   dm->ctxdestroy = destroy;
10651a266240SBarry Smith   PetscFunctionReturn(0);
10661a266240SBarry Smith }
10671a266240SBarry Smith 
10681a266240SBarry Smith #undef __FUNCT__
10691b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1070b07ff414SBarry Smith /*@
10711b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
107247c6ae99SBarry Smith 
107347c6ae99SBarry Smith     Not Collective
107447c6ae99SBarry Smith 
107547c6ae99SBarry Smith     Input Parameters:
107647c6ae99SBarry Smith +   dm - the DM object
107747c6ae99SBarry Smith -   ctx - the user context
107847c6ae99SBarry Smith 
107947c6ae99SBarry Smith     Level: intermediate
108047c6ae99SBarry Smith 
1081e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
108247c6ae99SBarry Smith 
108347c6ae99SBarry Smith @*/
10841b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
108547c6ae99SBarry Smith {
108647c6ae99SBarry Smith   PetscFunctionBegin;
1087171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
108847c6ae99SBarry Smith   dm->ctx = ctx;
108947c6ae99SBarry Smith   PetscFunctionReturn(0);
109047c6ae99SBarry Smith }
109147c6ae99SBarry Smith 
109247c6ae99SBarry Smith #undef __FUNCT__
10931b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
109447c6ae99SBarry Smith /*@
10951b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
109647c6ae99SBarry Smith 
109747c6ae99SBarry Smith     Not Collective
109847c6ae99SBarry Smith 
109947c6ae99SBarry Smith     Input Parameter:
110047c6ae99SBarry Smith .   dm - the DM object
110147c6ae99SBarry Smith 
110247c6ae99SBarry Smith     Output Parameter:
110347c6ae99SBarry Smith .   ctx - the user context
110447c6ae99SBarry Smith 
110547c6ae99SBarry Smith     Level: intermediate
110647c6ae99SBarry Smith 
1107e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
110847c6ae99SBarry Smith 
110947c6ae99SBarry Smith @*/
11101b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
111147c6ae99SBarry Smith {
111247c6ae99SBarry Smith   PetscFunctionBegin;
1113171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11141b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
111547c6ae99SBarry Smith   PetscFunctionReturn(0);
111647c6ae99SBarry Smith }
111747c6ae99SBarry Smith 
111847c6ae99SBarry Smith #undef __FUNCT__
111947c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
11207e833e3aSBarry Smith /*@C
112147c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
112247c6ae99SBarry Smith 
112347c6ae99SBarry Smith     Logically Collective on DM
112447c6ae99SBarry Smith 
112547c6ae99SBarry Smith     Input Parameter:
112647c6ae99SBarry Smith +   dm - the DM object to destroy
112747c6ae99SBarry Smith -   f - the function to compute the initial guess
112847c6ae99SBarry Smith 
112947c6ae99SBarry Smith     Level: intermediate
113047c6ae99SBarry Smith 
1131e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
113247c6ae99SBarry Smith 
1133f07f9ceaSJed Brown @*/
11347087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
113547c6ae99SBarry Smith {
113647c6ae99SBarry Smith   PetscFunctionBegin;
1137171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
113847c6ae99SBarry Smith   dm->ops->initialguess = f;
113947c6ae99SBarry Smith   PetscFunctionReturn(0);
114047c6ae99SBarry Smith }
114147c6ae99SBarry Smith 
114247c6ae99SBarry Smith #undef __FUNCT__
114347c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
11447e833e3aSBarry Smith /*@C
114547c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
114647c6ae99SBarry Smith 
114747c6ae99SBarry Smith     Logically Collective on DM
114847c6ae99SBarry Smith 
114947c6ae99SBarry Smith     Input Parameter:
115047c6ae99SBarry Smith +   dm - the DM object
115147c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
115247c6ae99SBarry Smith 
115347c6ae99SBarry Smith     Level: intermediate
115447c6ae99SBarry Smith 
115547c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
115647c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
115747c6ae99SBarry Smith 
1158e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
115947c6ae99SBarry Smith          DMSetJacobian()
116047c6ae99SBarry Smith 
1161f07f9ceaSJed Brown @*/
11627087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
116347c6ae99SBarry Smith {
116447c6ae99SBarry Smith   PetscFunctionBegin;
1165171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
116647c6ae99SBarry Smith   dm->ops->function = f;
116747c6ae99SBarry Smith   if (f) {
116847c6ae99SBarry Smith     dm->ops->functionj = f;
116947c6ae99SBarry Smith   }
117047c6ae99SBarry Smith   PetscFunctionReturn(0);
117147c6ae99SBarry Smith }
117247c6ae99SBarry Smith 
117347c6ae99SBarry Smith #undef __FUNCT__
117447c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
11757e833e3aSBarry Smith /*@C
117647c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
117747c6ae99SBarry Smith 
117847c6ae99SBarry Smith     Logically Collective on DM
117947c6ae99SBarry Smith 
118047c6ae99SBarry Smith     Input Parameter:
118147c6ae99SBarry Smith +   dm - the DM object to destroy
118247c6ae99SBarry Smith -   f - the function to compute the matrix entries
118347c6ae99SBarry Smith 
118447c6ae99SBarry Smith     Level: intermediate
118547c6ae99SBarry Smith 
1186e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
118747c6ae99SBarry Smith          DMSetFunction()
118847c6ae99SBarry Smith 
1189f07f9ceaSJed Brown @*/
11907087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
119147c6ae99SBarry Smith {
119247c6ae99SBarry Smith   PetscFunctionBegin;
1193171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
119447c6ae99SBarry Smith   dm->ops->jacobian = f;
119547c6ae99SBarry Smith   PetscFunctionReturn(0);
119647c6ae99SBarry Smith }
119747c6ae99SBarry Smith 
119847c6ae99SBarry Smith #undef __FUNCT__
119947c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
120047c6ae99SBarry Smith /*@
120147c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
120247c6ae99SBarry Smith 
120347c6ae99SBarry Smith     Collective on DM
120447c6ae99SBarry Smith 
120547c6ae99SBarry Smith     Input Parameter:
120647c6ae99SBarry Smith +   dm - the DM object to destroy
120747c6ae99SBarry Smith -   x - the vector to hold the initial guess values
120847c6ae99SBarry Smith 
120947c6ae99SBarry Smith     Level: developer
121047c6ae99SBarry Smith 
1211e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
121247c6ae99SBarry Smith 
121347c6ae99SBarry Smith @*/
12147087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
121547c6ae99SBarry Smith {
121647c6ae99SBarry Smith   PetscErrorCode ierr;
121747c6ae99SBarry Smith   PetscFunctionBegin;
1218171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
121947c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
122047c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
122147c6ae99SBarry Smith   PetscFunctionReturn(0);
122247c6ae99SBarry Smith }
122347c6ae99SBarry Smith 
122447c6ae99SBarry Smith #undef __FUNCT__
122547c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
122647c6ae99SBarry Smith /*@
122747c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
122847c6ae99SBarry Smith 
122947c6ae99SBarry Smith     Not Collective
123047c6ae99SBarry Smith 
123147c6ae99SBarry Smith     Input Parameter:
123247c6ae99SBarry Smith .   dm - the DM object to destroy
123347c6ae99SBarry Smith 
123447c6ae99SBarry Smith     Output Parameter:
123547c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
123647c6ae99SBarry Smith 
123747c6ae99SBarry Smith     Level: developer
123847c6ae99SBarry Smith 
1239e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
124047c6ae99SBarry Smith 
124147c6ae99SBarry Smith @*/
12427087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
124347c6ae99SBarry Smith {
124447c6ae99SBarry Smith   PetscFunctionBegin;
1245171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
124647c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
124747c6ae99SBarry Smith   PetscFunctionReturn(0);
124847c6ae99SBarry Smith }
124947c6ae99SBarry Smith 
125047c6ae99SBarry Smith #undef __FUNCT__
125147c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
125247c6ae99SBarry Smith /*@
125347c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
125447c6ae99SBarry Smith 
125547c6ae99SBarry Smith     Not Collective
125647c6ae99SBarry Smith 
125747c6ae99SBarry Smith     Input Parameter:
125847c6ae99SBarry Smith .   dm - the DM object to destroy
125947c6ae99SBarry Smith 
126047c6ae99SBarry Smith     Output Parameter:
126147c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
126247c6ae99SBarry Smith 
126347c6ae99SBarry Smith     Level: developer
126447c6ae99SBarry Smith 
1265e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
126647c6ae99SBarry Smith 
126747c6ae99SBarry Smith @*/
12687087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
126947c6ae99SBarry Smith {
127047c6ae99SBarry Smith   PetscFunctionBegin;
1271171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
127247c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
127347c6ae99SBarry Smith   PetscFunctionReturn(0);
127447c6ae99SBarry Smith }
127547c6ae99SBarry Smith 
127647c6ae99SBarry Smith #undef __FUNCT__
127747c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
127847c6ae99SBarry Smith /*@
127947c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
128047c6ae99SBarry Smith 
128147c6ae99SBarry Smith     Not Collective
128247c6ae99SBarry Smith 
128347c6ae99SBarry Smith     Input Parameter:
128447c6ae99SBarry Smith .   dm - the DM object to destroy
128547c6ae99SBarry Smith 
128647c6ae99SBarry Smith     Output Parameter:
128747c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
128847c6ae99SBarry Smith 
128947c6ae99SBarry Smith     Level: developer
129047c6ae99SBarry Smith 
1291e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
129247c6ae99SBarry Smith 
129347c6ae99SBarry Smith @*/
12947087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
129547c6ae99SBarry Smith {
129647c6ae99SBarry Smith   PetscFunctionBegin;
1297171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
129847c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
129947c6ae99SBarry Smith   PetscFunctionReturn(0);
130047c6ae99SBarry Smith }
130147c6ae99SBarry Smith 
130247c6ae99SBarry Smith #undef __FUNCT__
130347c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
130447c6ae99SBarry Smith /*@
130547c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
130647c6ae99SBarry Smith 
130747c6ae99SBarry Smith     Collective on DM
130847c6ae99SBarry Smith 
130947c6ae99SBarry Smith     Input Parameter:
131047c6ae99SBarry Smith +   dm - the DM object to destroy
131147c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
131247c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
131347c6ae99SBarry Smith 
131447c6ae99SBarry Smith     Level: developer
131547c6ae99SBarry Smith 
1316e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
131747c6ae99SBarry Smith          DMSetJacobian()
131847c6ae99SBarry Smith 
131947c6ae99SBarry Smith @*/
13207087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
132147c6ae99SBarry Smith {
132247c6ae99SBarry Smith   PetscErrorCode ierr;
132347c6ae99SBarry Smith   PetscFunctionBegin;
1324171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
132547c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
1326644e2e5bSBarry Smith   PetscStackPush("DM user function");
132747c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
1328644e2e5bSBarry Smith   PetscStackPop;
132947c6ae99SBarry Smith   PetscFunctionReturn(0);
133047c6ae99SBarry Smith }
133147c6ae99SBarry Smith 
133247c6ae99SBarry Smith 
133347c6ae99SBarry Smith #undef __FUNCT__
133447c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
133547c6ae99SBarry Smith /*@
133647c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
133747c6ae99SBarry Smith 
133847c6ae99SBarry Smith     Collective on DM
133947c6ae99SBarry Smith 
134047c6ae99SBarry Smith     Input Parameter:
134147c6ae99SBarry Smith +   dm - the DM object
1342cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
134347c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
134447c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
134547c6ae99SBarry Smith 
134647c6ae99SBarry Smith     Level: developer
134747c6ae99SBarry Smith 
1348e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
134947c6ae99SBarry Smith          DMSetFunction()
135047c6ae99SBarry Smith 
135147c6ae99SBarry Smith @*/
13527087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
135347c6ae99SBarry Smith {
135447c6ae99SBarry Smith   PetscErrorCode ierr;
135547c6ae99SBarry Smith 
135647c6ae99SBarry Smith   PetscFunctionBegin;
1357171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
135847c6ae99SBarry Smith   if (!dm->ops->jacobian) {
135947c6ae99SBarry Smith     ISColoring     coloring;
136047c6ae99SBarry Smith     MatFDColoring  fd;
136147c6ae99SBarry Smith 
1362171400e9SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,MATAIJ,&coloring);CHKERRQ(ierr);
136347c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
1364fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
136547c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
13660bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
13670bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
136871cd77b2SBarry Smith 
136947c6ae99SBarry Smith     dm->fd = fd;
137047c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
13712533e041SBarry Smith 
137271cd77b2SBarry Smith     /* don't know why this is needed */
137371cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
137447c6ae99SBarry Smith   }
137547c6ae99SBarry Smith   if (!x) x = dm->x;
137647c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
1377cab2e9ccSBarry Smith 
137871cd77b2SBarry 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 */
1379649052a6SBarry Smith   if (x) {
1380cab2e9ccSBarry Smith     if (!dm->x) {
138171cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
1382cab2e9ccSBarry Smith     }
1383cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
1384649052a6SBarry Smith   }
1385a8248277SBarry Smith   if (A != B) {
1386a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1387a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1388a8248277SBarry Smith   }
138947c6ae99SBarry Smith   PetscFunctionReturn(0);
139047c6ae99SBarry Smith }
1391264ace61SBarry Smith 
1392cab2e9ccSBarry Smith 
1393264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
1394264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
1395264ace61SBarry Smith 
1396264ace61SBarry Smith #undef __FUNCT__
1397264ace61SBarry Smith #define __FUNCT__ "DMSetType"
1398264ace61SBarry Smith /*@C
1399264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
1400264ace61SBarry Smith 
1401264ace61SBarry Smith   Collective on DM
1402264ace61SBarry Smith 
1403264ace61SBarry Smith   Input Parameters:
1404264ace61SBarry Smith + dm     - The DM object
1405264ace61SBarry Smith - method - The name of the DM type
1406264ace61SBarry Smith 
1407264ace61SBarry Smith   Options Database Key:
1408264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
1409264ace61SBarry Smith 
1410264ace61SBarry Smith   Notes:
1411e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
1412264ace61SBarry Smith 
1413264ace61SBarry Smith   Level: intermediate
1414264ace61SBarry Smith 
1415264ace61SBarry Smith .keywords: DM, set, type
1416264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
1417264ace61SBarry Smith @*/
14187087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
1419264ace61SBarry Smith {
1420264ace61SBarry Smith   PetscErrorCode (*r)(DM);
1421264ace61SBarry Smith   PetscBool      match;
1422264ace61SBarry Smith   PetscErrorCode ierr;
1423264ace61SBarry Smith 
1424264ace61SBarry Smith   PetscFunctionBegin;
1425264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1426264ace61SBarry Smith   ierr = PetscTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
1427264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
1428264ace61SBarry Smith 
1429264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
14304b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
1431264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
1432264ace61SBarry Smith 
1433264ace61SBarry Smith   if (dm->ops->destroy) {
1434264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
1435264ace61SBarry Smith   }
1436264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
1437264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
1438264ace61SBarry Smith   PetscFunctionReturn(0);
1439264ace61SBarry Smith }
1440264ace61SBarry Smith 
1441264ace61SBarry Smith #undef __FUNCT__
1442264ace61SBarry Smith #define __FUNCT__ "DMGetType"
1443264ace61SBarry Smith /*@C
1444264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
1445264ace61SBarry Smith 
1446264ace61SBarry Smith   Not Collective
1447264ace61SBarry Smith 
1448264ace61SBarry Smith   Input Parameter:
1449264ace61SBarry Smith . dm  - The DM
1450264ace61SBarry Smith 
1451264ace61SBarry Smith   Output Parameter:
1452264ace61SBarry Smith . type - The DM type name
1453264ace61SBarry Smith 
1454264ace61SBarry Smith   Level: intermediate
1455264ace61SBarry Smith 
1456264ace61SBarry Smith .keywords: DM, get, type, name
1457264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
1458264ace61SBarry Smith @*/
14597087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
1460264ace61SBarry Smith {
1461264ace61SBarry Smith   PetscErrorCode ierr;
1462264ace61SBarry Smith 
1463264ace61SBarry Smith   PetscFunctionBegin;
1464264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1465264ace61SBarry Smith   PetscValidCharPointer(type,2);
1466264ace61SBarry Smith   if (!DMRegisterAllCalled) {
1467264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
1468264ace61SBarry Smith   }
1469264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
1470264ace61SBarry Smith   PetscFunctionReturn(0);
1471264ace61SBarry Smith }
1472264ace61SBarry Smith 
147367a56275SMatthew G Knepley #undef __FUNCT__
147467a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
147567a56275SMatthew G Knepley /*@C
147667a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
147767a56275SMatthew G Knepley 
147867a56275SMatthew G Knepley   Collective on DM
147967a56275SMatthew G Knepley 
148067a56275SMatthew G Knepley   Input Parameters:
148167a56275SMatthew G Knepley + dm - the DM
148267a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
148367a56275SMatthew G Knepley 
148467a56275SMatthew G Knepley   Output Parameter:
148567a56275SMatthew G Knepley . M - pointer to new DM
148667a56275SMatthew G Knepley 
148767a56275SMatthew G Knepley   Notes:
148867a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
148967a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
149067a56275SMatthew G Knepley   of the input DM.
149167a56275SMatthew G Knepley 
149267a56275SMatthew G Knepley   Level: intermediate
149367a56275SMatthew G Knepley 
149467a56275SMatthew G Knepley .seealso: DMCreate()
149567a56275SMatthew G Knepley @*/
149667a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
149767a56275SMatthew G Knepley {
149867a56275SMatthew G Knepley   DM             B;
149967a56275SMatthew G Knepley   char           convname[256];
150067a56275SMatthew G Knepley   PetscBool      sametype, issame;
150167a56275SMatthew G Knepley   PetscErrorCode ierr;
150267a56275SMatthew G Knepley 
150367a56275SMatthew G Knepley   PetscFunctionBegin;
150467a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
150567a56275SMatthew G Knepley   PetscValidType(dm,1);
150667a56275SMatthew G Knepley   PetscValidPointer(M,3);
150767a56275SMatthew G Knepley   ierr = PetscTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
150867a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
150967a56275SMatthew G Knepley   {
151067a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
151167a56275SMatthew G Knepley 
151267a56275SMatthew G Knepley     /*
151367a56275SMatthew G Knepley        Order of precedence:
151467a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
151567a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
151667a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
151767a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
151867a56275SMatthew G Knepley        5) Use a really basic converter.
151967a56275SMatthew G Knepley     */
152067a56275SMatthew G Knepley 
152167a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
152267a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
152367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
152467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
152567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
152667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
152767a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
152867a56275SMatthew G Knepley     if (conv) goto foundconv;
152967a56275SMatthew G Knepley 
153067a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
153167a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
153267a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
153367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
153467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
153567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
153667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
153767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
153867a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
153967a56275SMatthew G Knepley     if (conv) {
1540fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
154167a56275SMatthew G Knepley       goto foundconv;
154267a56275SMatthew G Knepley     }
154367a56275SMatthew G Knepley 
154467a56275SMatthew G Knepley #if 0
154567a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
154667a56275SMatthew G Knepley     conv = B->ops->convertfrom;
1547fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
154867a56275SMatthew G Knepley     if (conv) goto foundconv;
154967a56275SMatthew G Knepley 
155067a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
155167a56275SMatthew G Knepley     if (dm->ops->convert) {
155267a56275SMatthew G Knepley       conv = dm->ops->convert;
155367a56275SMatthew G Knepley     }
155467a56275SMatthew G Knepley     if (conv) goto foundconv;
155567a56275SMatthew G Knepley #endif
155667a56275SMatthew G Knepley 
155767a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
155867a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
155967a56275SMatthew G Knepley 
156067a56275SMatthew G Knepley     foundconv:
156167a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
156267a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
156367a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
156467a56275SMatthew G Knepley   }
156567a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
156667a56275SMatthew G Knepley   PetscFunctionReturn(0);
156767a56275SMatthew G Knepley }
1568264ace61SBarry Smith 
1569264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1570264ace61SBarry Smith 
1571264ace61SBarry Smith #undef __FUNCT__
1572264ace61SBarry Smith #define __FUNCT__ "DMRegister"
1573264ace61SBarry Smith /*@C
1574264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
1575264ace61SBarry Smith 
1576264ace61SBarry Smith   Level: advanced
1577264ace61SBarry Smith @*/
15787087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
1579264ace61SBarry Smith {
1580264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
1581264ace61SBarry Smith   PetscErrorCode ierr;
1582264ace61SBarry Smith 
1583264ace61SBarry Smith   PetscFunctionBegin;
1584264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
1585264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
1586264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
1587264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
1588264ace61SBarry Smith   PetscFunctionReturn(0);
1589264ace61SBarry Smith }
1590264ace61SBarry Smith 
1591264ace61SBarry Smith 
1592264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1593264ace61SBarry Smith #undef __FUNCT__
1594264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
1595264ace61SBarry Smith /*@C
1596264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
1597264ace61SBarry Smith 
1598264ace61SBarry Smith    Not Collective
1599264ace61SBarry Smith 
1600264ace61SBarry Smith    Level: advanced
1601264ace61SBarry Smith 
1602264ace61SBarry Smith .keywords: DM, register, destroy
1603264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
1604264ace61SBarry Smith @*/
16057087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
1606264ace61SBarry Smith {
1607264ace61SBarry Smith   PetscErrorCode ierr;
1608264ace61SBarry Smith 
1609264ace61SBarry Smith   PetscFunctionBegin;
1610264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
1611264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
1612264ace61SBarry Smith   PetscFunctionReturn(0);
1613264ace61SBarry Smith }
161423f975d1SBarry Smith 
161523f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
1616c6db04a5SJed Brown #include <mex.h>
161723f975d1SBarry Smith 
16183014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
161923f975d1SBarry Smith 
162023f975d1SBarry Smith #undef __FUNCT__
162123f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
162223f975d1SBarry Smith /*
162323f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
162423f975d1SBarry Smith                          DMSetFunctionMatlab().
162523f975d1SBarry Smith 
162623f975d1SBarry Smith    For linear problems x is null
162723f975d1SBarry Smith 
162823f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
162923f975d1SBarry Smith */
16307087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
163123f975d1SBarry Smith {
163223f975d1SBarry Smith   PetscErrorCode    ierr;
163323f975d1SBarry Smith   DMMatlabContext   *sctx;
163423f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
163523f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
163623f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
163723f975d1SBarry Smith 
163823f975d1SBarry Smith   PetscFunctionBegin;
163923f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
164023f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
164123f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
164223f975d1SBarry Smith 
164323f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
16441b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
164523f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
164623f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
16473014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
164823f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
164923f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
165023f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
165123f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
1652b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
165323f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
165423f975d1SBarry Smith   mxDestroyArray(prhs[0]);
165523f975d1SBarry Smith   mxDestroyArray(prhs[1]);
165623f975d1SBarry Smith   mxDestroyArray(prhs[2]);
165723f975d1SBarry Smith   mxDestroyArray(prhs[3]);
165823f975d1SBarry Smith   mxDestroyArray(plhs[0]);
165923f975d1SBarry Smith   PetscFunctionReturn(0);
166023f975d1SBarry Smith }
166123f975d1SBarry Smith 
166223f975d1SBarry Smith 
166323f975d1SBarry Smith #undef __FUNCT__
166423f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
166523f975d1SBarry Smith /*
166623f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
166723f975d1SBarry Smith 
166823f975d1SBarry Smith */
16697087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
167023f975d1SBarry Smith {
167123f975d1SBarry Smith   PetscErrorCode    ierr;
167223f975d1SBarry Smith   DMMatlabContext   *sctx;
167323f975d1SBarry Smith 
167423f975d1SBarry Smith   PetscFunctionBegin;
1675171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
167623f975d1SBarry Smith   /* currently sctx is memory bleed */
16771b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
16783014e516SBarry Smith   if (!sctx) {
167923f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
16803014e516SBarry Smith   }
168123f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
16821b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
168323f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
168423f975d1SBarry Smith   PetscFunctionReturn(0);
168523f975d1SBarry Smith }
16863014e516SBarry Smith 
16873014e516SBarry Smith #undef __FUNCT__
16883014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
16893014e516SBarry Smith /*
16903014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
16913014e516SBarry Smith                          DMSetJacobianMatlab().
16923014e516SBarry Smith 
16933014e516SBarry Smith    For linear problems x is null
16943014e516SBarry Smith 
16953014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
16963014e516SBarry Smith */
16977087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
16983014e516SBarry Smith {
16993014e516SBarry Smith   PetscErrorCode    ierr;
17003014e516SBarry Smith   DMMatlabContext   *sctx;
17013014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
17023014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
17033014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
17043014e516SBarry Smith 
17053014e516SBarry Smith   PetscFunctionBegin;
17063014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17073014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
17083014e516SBarry Smith 
1709e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
17101b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
17113014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
17123014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
17133014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
17143014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
17153014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
17163014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
17173014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
17183014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
17193014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
1720b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
1721c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
1722c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
17233014e516SBarry Smith   mxDestroyArray(prhs[0]);
17243014e516SBarry Smith   mxDestroyArray(prhs[1]);
17253014e516SBarry Smith   mxDestroyArray(prhs[2]);
17263014e516SBarry Smith   mxDestroyArray(prhs[3]);
17273014e516SBarry Smith   mxDestroyArray(prhs[4]);
17283014e516SBarry Smith   mxDestroyArray(plhs[0]);
17293014e516SBarry Smith   mxDestroyArray(plhs[1]);
17303014e516SBarry Smith   PetscFunctionReturn(0);
17313014e516SBarry Smith }
17323014e516SBarry Smith 
17333014e516SBarry Smith 
17343014e516SBarry Smith #undef __FUNCT__
17353014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
17363014e516SBarry Smith /*
17373014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
17383014e516SBarry Smith 
17393014e516SBarry Smith */
17407087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
17413014e516SBarry Smith {
17423014e516SBarry Smith   PetscErrorCode    ierr;
17433014e516SBarry Smith   DMMatlabContext   *sctx;
17443014e516SBarry Smith 
17453014e516SBarry Smith   PetscFunctionBegin;
1746171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17473014e516SBarry Smith   /* currently sctx is memory bleed */
17481b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
17493014e516SBarry Smith   if (!sctx) {
17503014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
17513014e516SBarry Smith   }
17523014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
17531b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
17543014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
17553014e516SBarry Smith   PetscFunctionReturn(0);
17563014e516SBarry Smith }
175723f975d1SBarry Smith #endif
1758b859378eSBarry Smith 
1759b859378eSBarry Smith #undef __FUNCT__
1760b859378eSBarry Smith #define __FUNCT__ "DMLoad"
1761b859378eSBarry Smith /*@C
1762b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
1763b859378eSBarry Smith   with DMView().
1764b859378eSBarry Smith 
1765b859378eSBarry Smith   Collective on PetscViewer
1766b859378eSBarry Smith 
1767b859378eSBarry Smith   Input Parameters:
1768b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
1769b859378eSBarry Smith            some related function before a call to DMLoad().
1770b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
1771b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
1772b859378eSBarry Smith 
1773b859378eSBarry Smith    Level: intermediate
1774b859378eSBarry Smith 
1775b859378eSBarry Smith   Notes:
1776b859378eSBarry Smith   Defaults to the DM DA.
1777b859378eSBarry Smith 
1778b859378eSBarry Smith   Notes for advanced users:
1779b859378eSBarry Smith   Most users should not need to know the details of the binary storage
1780b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
1781b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
1782b859378eSBarry Smith   format is
1783b859378eSBarry Smith .vb
1784b859378eSBarry Smith      has not yet been determined
1785b859378eSBarry Smith .ve
1786b859378eSBarry Smith 
1787b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
1788b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
1789b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
1790b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
1791b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
1792b859378eSBarry Smith 
1793b859378eSBarry Smith   Concepts: vector^loading from file
1794b859378eSBarry Smith 
1795b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
1796b859378eSBarry Smith @*/
1797b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
1798b859378eSBarry Smith {
1799b859378eSBarry Smith   PetscErrorCode ierr;
1800b859378eSBarry Smith 
1801b859378eSBarry Smith   PetscFunctionBegin;
1802b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
1803b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1804b859378eSBarry Smith 
1805b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
1806b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
1807b859378eSBarry Smith   }
1808b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
1809b859378eSBarry Smith   PetscFunctionReturn(0);
1810b859378eSBarry Smith }
1811b859378eSBarry Smith 
1812