xref: /petsc/src/dm/interface/dm.c (revision dd85299cac017dea8f8657dcbe071ecc007ecc52)
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;
491411c6eeSJed Brown 
501411c6eeSJed Brown   *dm = v;
51a4121054SBarry Smith   PetscFunctionReturn(0);
52a4121054SBarry Smith }
53a4121054SBarry Smith 
54a4121054SBarry Smith 
55a4121054SBarry Smith #undef __FUNCT__
569a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
579a42bb27SBarry Smith /*@C
58564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
599a42bb27SBarry Smith 
60aa219208SBarry Smith    Logically Collective on DMDA
619a42bb27SBarry Smith 
629a42bb27SBarry Smith    Input Parameter:
639a42bb27SBarry Smith +  da - initial distributed array
648154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
659a42bb27SBarry Smith 
669a42bb27SBarry Smith    Options Database:
67dd85299cSBarry Smith .   -dm_vec_type ctype
689a42bb27SBarry Smith 
699a42bb27SBarry Smith    Level: intermediate
709a42bb27SBarry Smith 
71aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
729a42bb27SBarry Smith @*/
737087cfbeSBarry Smith PetscErrorCode  DMSetVecType(DM da,const VecType ctype)
749a42bb27SBarry Smith {
759a42bb27SBarry Smith   PetscErrorCode ierr;
769a42bb27SBarry Smith 
779a42bb27SBarry Smith   PetscFunctionBegin;
789a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
799a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
809a42bb27SBarry Smith   ierr = PetscStrallocpy(ctype,&da->vectype);CHKERRQ(ierr);
819a42bb27SBarry Smith   PetscFunctionReturn(0);
829a42bb27SBarry Smith }
839a42bb27SBarry Smith 
849a42bb27SBarry Smith #undef __FUNCT__
859a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
869a42bb27SBarry Smith /*@C
879a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
88aa219208SBarry Smith    DMDA options in the database.
899a42bb27SBarry Smith 
90aa219208SBarry Smith    Logically Collective on DMDA
919a42bb27SBarry Smith 
929a42bb27SBarry Smith    Input Parameter:
93aa219208SBarry Smith +  da - the DMDA context
949a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
959a42bb27SBarry Smith 
969a42bb27SBarry Smith    Notes:
979a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
989a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
999a42bb27SBarry Smith 
1009a42bb27SBarry Smith    Level: advanced
1019a42bb27SBarry Smith 
102aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
1039a42bb27SBarry Smith 
1049a42bb27SBarry Smith .seealso: DMSetFromOptions()
1059a42bb27SBarry Smith @*/
1067087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
1079a42bb27SBarry Smith {
1089a42bb27SBarry Smith   PetscErrorCode ierr;
1099a42bb27SBarry Smith 
1109a42bb27SBarry Smith   PetscFunctionBegin;
1119a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1129a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
1139a42bb27SBarry Smith   PetscFunctionReturn(0);
1149a42bb27SBarry Smith }
1159a42bb27SBarry Smith 
1169a42bb27SBarry Smith #undef __FUNCT__
11747c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
11847c6ae99SBarry Smith /*@
119aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
12047c6ae99SBarry Smith 
12147c6ae99SBarry Smith     Collective on DM
12247c6ae99SBarry Smith 
12347c6ae99SBarry Smith     Input Parameter:
12447c6ae99SBarry Smith .   dm - the DM object to destroy
12547c6ae99SBarry Smith 
12647c6ae99SBarry Smith     Level: developer
12747c6ae99SBarry Smith 
128e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
12947c6ae99SBarry Smith 
13047c6ae99SBarry Smith @*/
131fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
13247c6ae99SBarry Smith {
133732e2eb9SMatthew G Knepley   PetscInt       i, cnt = 0;
13447c6ae99SBarry Smith   PetscErrorCode ierr;
13547c6ae99SBarry Smith 
13647c6ae99SBarry Smith   PetscFunctionBegin;
1376bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
1386bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
13987e657c6SBarry Smith 
14087e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
141732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1426bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
1436bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
144732e2eb9SMatthew G Knepley   }
14571cd77b2SBarry Smith   if ((*dm)->x) {
14671cd77b2SBarry Smith     PetscObject obj;
14771cd77b2SBarry Smith     ierr = PetscObjectQuery((PetscObject)(*dm)->x,"DM",&obj);CHKERRQ(ierr);
14871cd77b2SBarry Smith     if (obj == (PetscObject)*dm) cnt++;
14971cd77b2SBarry Smith   }
150732e2eb9SMatthew G Knepley 
1516bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
152732e2eb9SMatthew G Knepley   /*
153732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
154732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
155732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
156732e2eb9SMatthew G Knepley   */
1576bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
1586bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
159732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1606bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
1616bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
162732e2eb9SMatthew G Knepley   }
1631a266240SBarry Smith 
1641a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
1651a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
1661a266240SBarry Smith   }
16787e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
16871cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
1694dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
1706bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
1716bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
1726bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
173073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
174*a89ea682SMatthew G Knepley   ierr = PetscFree((*dm)->workArray);CHKERRQ(ierr);
175732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
1766bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
177732e2eb9SMatthew G Knepley 
1786bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
1796bf464f9SBarry Smith   ierr = PetscFree((*dm)->data);CHKERRQ(ierr);
180732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
18147c6ae99SBarry Smith   PetscFunctionReturn(0);
18247c6ae99SBarry Smith }
18347c6ae99SBarry Smith 
18447c6ae99SBarry Smith #undef __FUNCT__
185d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
186d7bf68aeSBarry Smith /*@
187d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
188d7bf68aeSBarry Smith 
189d7bf68aeSBarry Smith     Collective on DM
190d7bf68aeSBarry Smith 
191d7bf68aeSBarry Smith     Input Parameter:
192d7bf68aeSBarry Smith .   dm - the DM object to setup
193d7bf68aeSBarry Smith 
194d7bf68aeSBarry Smith     Level: developer
195d7bf68aeSBarry Smith 
196e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
197d7bf68aeSBarry Smith 
198d7bf68aeSBarry Smith @*/
1997087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
200d7bf68aeSBarry Smith {
201d7bf68aeSBarry Smith   PetscErrorCode ierr;
202d7bf68aeSBarry Smith 
203d7bf68aeSBarry Smith   PetscFunctionBegin;
2048387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
205d7bf68aeSBarry Smith   if (dm->ops->setup) {
206d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
207d7bf68aeSBarry Smith   }
2088387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
209d7bf68aeSBarry Smith   PetscFunctionReturn(0);
210d7bf68aeSBarry Smith }
211d7bf68aeSBarry Smith 
212d7bf68aeSBarry Smith #undef __FUNCT__
213d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
214d7bf68aeSBarry Smith /*@
215d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
216d7bf68aeSBarry Smith 
217d7bf68aeSBarry Smith     Collective on DM
218d7bf68aeSBarry Smith 
219d7bf68aeSBarry Smith     Input Parameter:
220d7bf68aeSBarry Smith .   dm - the DM object to set options for
221d7bf68aeSBarry Smith 
222732e2eb9SMatthew G Knepley     Options Database:
223dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
224dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
225dd85299cSBarry Smith -   -dm_mat_type <type>  type of matrix to create inside DM
226732e2eb9SMatthew G Knepley 
227d7bf68aeSBarry Smith     Level: developer
228d7bf68aeSBarry Smith 
229e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
230d7bf68aeSBarry Smith 
231d7bf68aeSBarry Smith @*/
2327087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
233d7bf68aeSBarry Smith {
234073dac72SJed Brown   PetscBool      flg1 = PETSC_FALSE,flg;
235d7bf68aeSBarry Smith   PetscErrorCode ierr;
236f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
237d7bf68aeSBarry Smith 
238d7bf68aeSBarry Smith   PetscFunctionBegin;
2393194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
24082fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
241073dac72SJed 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);
242f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
243f9ba7244SBarry Smith     if (flg) {
244f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
245f9ba7244SBarry Smith     }
246f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_mat_type","Matrix type","MatSetType",MatList,typeName,typeName,sizeof typeName,&flg);CHKERRQ(ierr);
247073dac72SJed Brown     if (flg) {
248073dac72SJed Brown       ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
249f9ba7244SBarry Smith       ierr = PetscStrallocpy(typeName,&dm->mattype);CHKERRQ(ierr);
250073dac72SJed Brown     }
251f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
252f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
253f9ba7244SBarry Smith     }
254f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
255f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
25682fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
25782fcb398SMatthew G Knepley   if (flg1) {
25882fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
25982fcb398SMatthew G Knepley   }
260d7bf68aeSBarry Smith   PetscFunctionReturn(0);
261d7bf68aeSBarry Smith }
262d7bf68aeSBarry Smith 
263d7bf68aeSBarry Smith #undef __FUNCT__
26447c6ae99SBarry Smith #define __FUNCT__ "DMView"
265fc9bc008SSatish Balay /*@C
266aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
26747c6ae99SBarry Smith 
26847c6ae99SBarry Smith     Collective on DM
26947c6ae99SBarry Smith 
27047c6ae99SBarry Smith     Input Parameter:
27147c6ae99SBarry Smith +   dm - the DM object to view
27247c6ae99SBarry Smith -   v - the viewer
27347c6ae99SBarry Smith 
27447c6ae99SBarry Smith     Level: developer
27547c6ae99SBarry Smith 
276e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
27747c6ae99SBarry Smith 
27847c6ae99SBarry Smith @*/
2797087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
28047c6ae99SBarry Smith {
28147c6ae99SBarry Smith   PetscErrorCode ierr;
28247c6ae99SBarry Smith 
28347c6ae99SBarry Smith   PetscFunctionBegin;
2843014e516SBarry Smith  if (!v) {
2853014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
2863014e516SBarry Smith   }
2870c010503SBarry Smith   if (dm->ops->view) {
2880c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
28947c6ae99SBarry Smith   }
29047c6ae99SBarry Smith   PetscFunctionReturn(0);
29147c6ae99SBarry Smith }
29247c6ae99SBarry Smith 
29347c6ae99SBarry Smith #undef __FUNCT__
29447c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
29547c6ae99SBarry Smith /*@
296aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
29747c6ae99SBarry Smith 
29847c6ae99SBarry Smith     Collective on DM
29947c6ae99SBarry Smith 
30047c6ae99SBarry Smith     Input Parameter:
30147c6ae99SBarry Smith .   dm - the DM object
30247c6ae99SBarry Smith 
30347c6ae99SBarry Smith     Output Parameter:
30447c6ae99SBarry Smith .   vec - the global vector
30547c6ae99SBarry Smith 
306073dac72SJed Brown     Level: beginner
30747c6ae99SBarry Smith 
308e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
30947c6ae99SBarry Smith 
31047c6ae99SBarry Smith @*/
3117087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
31247c6ae99SBarry Smith {
31347c6ae99SBarry Smith   PetscErrorCode ierr;
31447c6ae99SBarry Smith 
31547c6ae99SBarry Smith   PetscFunctionBegin;
31647c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
31747c6ae99SBarry Smith   PetscFunctionReturn(0);
31847c6ae99SBarry Smith }
31947c6ae99SBarry Smith 
32047c6ae99SBarry Smith #undef __FUNCT__
32147c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
32247c6ae99SBarry Smith /*@
323aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
32447c6ae99SBarry Smith 
32547c6ae99SBarry Smith     Not Collective
32647c6ae99SBarry Smith 
32747c6ae99SBarry Smith     Input Parameter:
32847c6ae99SBarry Smith .   dm - the DM object
32947c6ae99SBarry Smith 
33047c6ae99SBarry Smith     Output Parameter:
33147c6ae99SBarry Smith .   vec - the local vector
33247c6ae99SBarry Smith 
333073dac72SJed Brown     Level: beginner
33447c6ae99SBarry Smith 
335e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
33647c6ae99SBarry Smith 
33747c6ae99SBarry Smith @*/
3387087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
33947c6ae99SBarry Smith {
34047c6ae99SBarry Smith   PetscErrorCode ierr;
34147c6ae99SBarry Smith 
34247c6ae99SBarry Smith   PetscFunctionBegin;
34347c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
34447c6ae99SBarry Smith   PetscFunctionReturn(0);
34547c6ae99SBarry Smith }
34647c6ae99SBarry Smith 
34747c6ae99SBarry Smith #undef __FUNCT__
3481411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
3491411c6eeSJed Brown /*@
3501411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
3511411c6eeSJed Brown 
3521411c6eeSJed Brown    Collective on DM
3531411c6eeSJed Brown 
3541411c6eeSJed Brown    Input Parameter:
3551411c6eeSJed Brown .  dm - the DM that provides the mapping
3561411c6eeSJed Brown 
3571411c6eeSJed Brown    Output Parameter:
3581411c6eeSJed Brown .  ltog - the mapping
3591411c6eeSJed Brown 
3601411c6eeSJed Brown    Level: intermediate
3611411c6eeSJed Brown 
3621411c6eeSJed Brown    Notes:
3631411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
3641411c6eeSJed Brown    MatSetLocalToGlobalMapping().
3651411c6eeSJed Brown 
3661411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
3671411c6eeSJed Brown @*/
3687087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
3691411c6eeSJed Brown {
3701411c6eeSJed Brown   PetscErrorCode ierr;
3711411c6eeSJed Brown 
3721411c6eeSJed Brown   PetscFunctionBegin;
3731411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3741411c6eeSJed Brown   PetscValidPointer(ltog,2);
3751411c6eeSJed Brown   if (!dm->ltogmap) {
3761411c6eeSJed Brown     if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
3771411c6eeSJed Brown     ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
3781411c6eeSJed Brown   }
3791411c6eeSJed Brown   *ltog = dm->ltogmap;
3801411c6eeSJed Brown   PetscFunctionReturn(0);
3811411c6eeSJed Brown }
3821411c6eeSJed Brown 
3831411c6eeSJed Brown #undef __FUNCT__
3841411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
3851411c6eeSJed Brown /*@
3861411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
3871411c6eeSJed Brown 
3881411c6eeSJed Brown    Collective on DM
3891411c6eeSJed Brown 
3901411c6eeSJed Brown    Input Parameter:
3911411c6eeSJed Brown .  da - the distributed array that provides the mapping
3921411c6eeSJed Brown 
3931411c6eeSJed Brown    Output Parameter:
3941411c6eeSJed Brown .  ltog - the block mapping
3951411c6eeSJed Brown 
3961411c6eeSJed Brown    Level: intermediate
3971411c6eeSJed Brown 
3981411c6eeSJed Brown    Notes:
3991411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
4001411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
4011411c6eeSJed Brown 
4021411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
4031411c6eeSJed Brown @*/
4047087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
4051411c6eeSJed Brown {
4061411c6eeSJed Brown   PetscErrorCode ierr;
4071411c6eeSJed Brown 
4081411c6eeSJed Brown   PetscFunctionBegin;
4091411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4101411c6eeSJed Brown   PetscValidPointer(ltog,2);
4111411c6eeSJed Brown   if (!dm->ltogmapb) {
4121411c6eeSJed Brown     PetscInt bs;
4131411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
4141411c6eeSJed Brown     if (bs > 1) {
4151411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
4161411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
4171411c6eeSJed Brown     } else {
4181411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
4191411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
4201411c6eeSJed Brown     }
4211411c6eeSJed Brown   }
4221411c6eeSJed Brown   *ltog = dm->ltogmapb;
4231411c6eeSJed Brown   PetscFunctionReturn(0);
4241411c6eeSJed Brown }
4251411c6eeSJed Brown 
4261411c6eeSJed Brown #undef __FUNCT__
4271411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
4281411c6eeSJed Brown /*@
4291411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
4301411c6eeSJed Brown 
4311411c6eeSJed Brown    Not Collective
4321411c6eeSJed Brown 
4331411c6eeSJed Brown    Input Parameter:
4341411c6eeSJed Brown .  dm - the DM with block structure
4351411c6eeSJed Brown 
4361411c6eeSJed Brown    Output Parameter:
4371411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
4381411c6eeSJed Brown 
4391411c6eeSJed Brown    Level: intermediate
4401411c6eeSJed Brown 
4411411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
4421411c6eeSJed Brown @*/
4437087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
4441411c6eeSJed Brown {
4451411c6eeSJed Brown   PetscFunctionBegin;
4461411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4471411c6eeSJed Brown   PetscValidPointer(bs,2);
4481411c6eeSJed 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");
4491411c6eeSJed Brown   *bs = dm->bs;
4501411c6eeSJed Brown   PetscFunctionReturn(0);
4511411c6eeSJed Brown }
4521411c6eeSJed Brown 
4531411c6eeSJed Brown #undef __FUNCT__
454e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
45547c6ae99SBarry Smith /*@
456e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
45747c6ae99SBarry Smith 
45847c6ae99SBarry Smith     Collective on DM
45947c6ae99SBarry Smith 
46047c6ae99SBarry Smith     Input Parameter:
46147c6ae99SBarry Smith +   dm1 - the DM object
46247c6ae99SBarry Smith -   dm2 - the second, finer DM object
46347c6ae99SBarry Smith 
46447c6ae99SBarry Smith     Output Parameter:
46547c6ae99SBarry Smith +  mat - the interpolation
46647c6ae99SBarry Smith -  vec - the scaling (optional)
46747c6ae99SBarry Smith 
46847c6ae99SBarry Smith     Level: developer
46947c6ae99SBarry Smith 
47085afcc9aSBarry 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
47185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
472d52bd9f3SBarry Smith 
473d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
474d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
47585afcc9aSBarry Smith 
47685afcc9aSBarry Smith 
477e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
47847c6ae99SBarry Smith 
47947c6ae99SBarry Smith @*/
480e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
48147c6ae99SBarry Smith {
48247c6ae99SBarry Smith   PetscErrorCode ierr;
48347c6ae99SBarry Smith 
48447c6ae99SBarry Smith   PetscFunctionBegin;
48547c6ae99SBarry Smith   ierr = (*dm1->ops->getinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
48647c6ae99SBarry Smith   PetscFunctionReturn(0);
48747c6ae99SBarry Smith }
48847c6ae99SBarry Smith 
48947c6ae99SBarry Smith #undef __FUNCT__
490e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
49147c6ae99SBarry Smith /*@
492e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
49347c6ae99SBarry Smith 
49447c6ae99SBarry Smith     Collective on DM
49547c6ae99SBarry Smith 
49647c6ae99SBarry Smith     Input Parameter:
49747c6ae99SBarry Smith +   dm1 - the DM object
49847c6ae99SBarry Smith -   dm2 - the second, finer DM object
49947c6ae99SBarry Smith 
50047c6ae99SBarry Smith     Output Parameter:
50147c6ae99SBarry Smith .   ctx - the injection
50247c6ae99SBarry Smith 
50347c6ae99SBarry Smith     Level: developer
50447c6ae99SBarry Smith 
50585afcc9aSBarry 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
50685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
50785afcc9aSBarry Smith 
508e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
50947c6ae99SBarry Smith 
51047c6ae99SBarry Smith @*/
511e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
51247c6ae99SBarry Smith {
51347c6ae99SBarry Smith   PetscErrorCode ierr;
51447c6ae99SBarry Smith 
51547c6ae99SBarry Smith   PetscFunctionBegin;
51647c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
51747c6ae99SBarry Smith   PetscFunctionReturn(0);
51847c6ae99SBarry Smith }
51947c6ae99SBarry Smith 
52047c6ae99SBarry Smith #undef __FUNCT__
521e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
522d1e2c406SBarry Smith /*@C
523e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
52447c6ae99SBarry Smith 
52547c6ae99SBarry Smith     Collective on DM
52647c6ae99SBarry Smith 
52747c6ae99SBarry Smith     Input Parameter:
52847c6ae99SBarry Smith +   dm - the DM object
52947c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
53047c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
53147c6ae99SBarry Smith 
53247c6ae99SBarry Smith     Output Parameter:
53347c6ae99SBarry Smith .   coloring - the coloring
53447c6ae99SBarry Smith 
53547c6ae99SBarry Smith     Level: developer
53647c6ae99SBarry Smith 
537e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
53847c6ae99SBarry Smith 
539aab9d709SJed Brown @*/
540e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
54147c6ae99SBarry Smith {
54247c6ae99SBarry Smith   PetscErrorCode ierr;
54347c6ae99SBarry Smith 
54447c6ae99SBarry Smith   PetscFunctionBegin;
54547c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
54647c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
54747c6ae99SBarry Smith   PetscFunctionReturn(0);
54847c6ae99SBarry Smith }
54947c6ae99SBarry Smith 
55047c6ae99SBarry Smith #undef __FUNCT__
551950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
55247c6ae99SBarry Smith /*@C
553950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
55447c6ae99SBarry Smith 
55547c6ae99SBarry Smith     Collective on DM
55647c6ae99SBarry Smith 
55747c6ae99SBarry Smith     Input Parameter:
55847c6ae99SBarry Smith +   dm - the DM object
55947c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
56094013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
56147c6ae99SBarry Smith 
56247c6ae99SBarry Smith     Output Parameter:
56347c6ae99SBarry Smith .   mat - the empty Jacobian
56447c6ae99SBarry Smith 
565073dac72SJed Brown     Level: beginner
56647c6ae99SBarry Smith 
56794013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
56894013140SBarry Smith        do not need to do it yourself.
56994013140SBarry Smith 
57094013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
571aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
57294013140SBarry Smith 
57394013140SBarry 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
57494013140SBarry Smith        internally by PETSc.
57594013140SBarry Smith 
57694013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
577aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
57894013140SBarry Smith 
579e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
58047c6ae99SBarry Smith 
581aab9d709SJed Brown @*/
582950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
58347c6ae99SBarry Smith {
58447c6ae99SBarry Smith   PetscErrorCode ierr;
58547c6ae99SBarry Smith 
58647c6ae99SBarry Smith   PetscFunctionBegin;
587235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
588235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
589235683edSBarry Smith #endif
590c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
591c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
592073dac72SJed Brown   if (dm->mattype) {
593073dac72SJed Brown     ierr = (*dm->ops->getmatrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
594073dac72SJed Brown   } else {
595073dac72SJed Brown     ierr = (*dm->ops->getmatrix)(dm,mtype,mat);CHKERRQ(ierr);
596c7b7c8a4SJed Brown   }
59747c6ae99SBarry Smith   PetscFunctionReturn(0);
59847c6ae99SBarry Smith }
59947c6ae99SBarry Smith 
60047c6ae99SBarry Smith #undef __FUNCT__
601732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
602732e2eb9SMatthew G Knepley /*@
603950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
604732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
605732e2eb9SMatthew G Knepley 
606732e2eb9SMatthew G Knepley   Logically Collective on DMDA
607732e2eb9SMatthew G Knepley 
608732e2eb9SMatthew G Knepley   Input Parameter:
609732e2eb9SMatthew G Knepley + dm - the DM
610732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
611732e2eb9SMatthew G Knepley 
612732e2eb9SMatthew G Knepley   Level: developer
613950540a4SJed Brown .seealso DMCreateMatrix()
614732e2eb9SMatthew G Knepley @*/
615732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
616732e2eb9SMatthew G Knepley {
617732e2eb9SMatthew G Knepley   PetscFunctionBegin;
618732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
619732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
620732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
621732e2eb9SMatthew G Knepley }
622732e2eb9SMatthew G Knepley 
623732e2eb9SMatthew G Knepley #undef __FUNCT__
624*a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
625*a89ea682SMatthew G Knepley /*@C
626*a89ea682SMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size
627*a89ea682SMatthew G Knepley 
628*a89ea682SMatthew G Knepley   Not Collective
629*a89ea682SMatthew G Knepley 
630*a89ea682SMatthew G Knepley   Input Parameters:
631*a89ea682SMatthew G Knepley + dm - the DM object
632*a89ea682SMatthew G Knepley - size - The minium size
633*a89ea682SMatthew G Knepley 
634*a89ea682SMatthew G Knepley   Output Parameter:
635*a89ea682SMatthew G Knepley . array - the work array
636*a89ea682SMatthew G Knepley 
637*a89ea682SMatthew G Knepley   Level: developer
638*a89ea682SMatthew G Knepley 
639*a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
640*a89ea682SMatthew G Knepley @*/
641*a89ea682SMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt size,PetscScalar **array)
642*a89ea682SMatthew G Knepley {
643*a89ea682SMatthew G Knepley   PetscErrorCode ierr;
644*a89ea682SMatthew G Knepley 
645*a89ea682SMatthew G Knepley   PetscFunctionBegin;
646*a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
647*a89ea682SMatthew G Knepley   PetscValidPointer(array,3);
648*a89ea682SMatthew G Knepley   if (size > dm->workSize) {
649*a89ea682SMatthew G Knepley     dm->workSize = size;
650*a89ea682SMatthew G Knepley     ierr = PetscFree(dm->workArray);CHKERRQ(ierr);
651*a89ea682SMatthew G Knepley     ierr = PetscMalloc(dm->workSize * sizeof(PetscScalar), &dm->workArray);CHKERRQ(ierr);
652*a89ea682SMatthew G Knepley   }
653*a89ea682SMatthew G Knepley   *array = dm->workArray;
654*a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
655*a89ea682SMatthew G Knepley }
656*a89ea682SMatthew G Knepley 
657*a89ea682SMatthew G Knepley #undef __FUNCT__
65847c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
65947c6ae99SBarry Smith /*@
66047c6ae99SBarry Smith     DMRefine - Refines a DM object
66147c6ae99SBarry Smith 
66247c6ae99SBarry Smith     Collective on DM
66347c6ae99SBarry Smith 
66447c6ae99SBarry Smith     Input Parameter:
66547c6ae99SBarry Smith +   dm - the DM object
66647c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
66747c6ae99SBarry Smith 
66847c6ae99SBarry Smith     Output Parameter:
66947c6ae99SBarry Smith .   dmf - the refined DM
67047c6ae99SBarry Smith 
67147c6ae99SBarry Smith     Level: developer
67247c6ae99SBarry Smith 
673e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
67447c6ae99SBarry Smith 
67547c6ae99SBarry Smith @*/
6767087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
67747c6ae99SBarry Smith {
67847c6ae99SBarry Smith   PetscErrorCode ierr;
67947c6ae99SBarry Smith 
68047c6ae99SBarry Smith   PetscFunctionBegin;
681732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
68247c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
683644e2e5bSBarry Smith   (*dmf)->ops->initialguess = dm->ops->initialguess;
684644e2e5bSBarry Smith   (*dmf)->ops->function     = dm->ops->function;
685644e2e5bSBarry Smith   (*dmf)->ops->functionj    = dm->ops->functionj;
686644e2e5bSBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
687644e2e5bSBarry Smith     (*dmf)->ops->jacobian     = dm->ops->jacobian;
688644e2e5bSBarry Smith   }
6898cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
690644e2e5bSBarry Smith   (*dmf)->ctx     = dm->ctx;
691656b349aSBarry Smith   (*dmf)->levelup = dm->levelup + 1;
69247c6ae99SBarry Smith   PetscFunctionReturn(0);
69347c6ae99SBarry Smith }
69447c6ae99SBarry Smith 
69547c6ae99SBarry Smith #undef __FUNCT__
696eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
697eb3f98d2SBarry Smith /*@
698eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
699eb3f98d2SBarry Smith 
700eb3f98d2SBarry Smith     Not Collective
701eb3f98d2SBarry Smith 
702eb3f98d2SBarry Smith     Input Parameter:
703eb3f98d2SBarry Smith .   dm - the DM object
704eb3f98d2SBarry Smith 
705eb3f98d2SBarry Smith     Output Parameter:
706eb3f98d2SBarry Smith .   level - number of refinements
707eb3f98d2SBarry Smith 
708eb3f98d2SBarry Smith     Level: developer
709eb3f98d2SBarry Smith 
710e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
711eb3f98d2SBarry Smith 
712eb3f98d2SBarry Smith @*/
713eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
714eb3f98d2SBarry Smith {
715eb3f98d2SBarry Smith   PetscFunctionBegin;
716eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
717eb3f98d2SBarry Smith   *level = dm->levelup;
718eb3f98d2SBarry Smith   PetscFunctionReturn(0);
719eb3f98d2SBarry Smith }
720eb3f98d2SBarry Smith 
721eb3f98d2SBarry Smith #undef __FUNCT__
72247c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
72347c6ae99SBarry Smith /*@
72447c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
72547c6ae99SBarry Smith 
72647c6ae99SBarry Smith     Neighbor-wise Collective on DM
72747c6ae99SBarry Smith 
72847c6ae99SBarry Smith     Input Parameters:
72947c6ae99SBarry Smith +   dm - the DM object
73047c6ae99SBarry Smith .   g - the global vector
73147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
73247c6ae99SBarry Smith -   l - the local vector
73347c6ae99SBarry Smith 
73447c6ae99SBarry Smith 
73547c6ae99SBarry Smith     Level: beginner
73647c6ae99SBarry Smith 
737e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
73847c6ae99SBarry Smith 
73947c6ae99SBarry Smith @*/
7407087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
74147c6ae99SBarry Smith {
74247c6ae99SBarry Smith   PetscErrorCode ierr;
74347c6ae99SBarry Smith 
74447c6ae99SBarry Smith   PetscFunctionBegin;
745843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
74647c6ae99SBarry Smith   PetscFunctionReturn(0);
74747c6ae99SBarry Smith }
74847c6ae99SBarry Smith 
74947c6ae99SBarry Smith #undef __FUNCT__
75047c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
75147c6ae99SBarry Smith /*@
75247c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
75347c6ae99SBarry Smith 
75447c6ae99SBarry Smith     Neighbor-wise Collective on DM
75547c6ae99SBarry Smith 
75647c6ae99SBarry Smith     Input Parameters:
75747c6ae99SBarry Smith +   dm - the DM object
75847c6ae99SBarry Smith .   g - the global vector
75947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
76047c6ae99SBarry Smith -   l - the local vector
76147c6ae99SBarry Smith 
76247c6ae99SBarry Smith 
76347c6ae99SBarry Smith     Level: beginner
76447c6ae99SBarry Smith 
765e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
76647c6ae99SBarry Smith 
76747c6ae99SBarry Smith @*/
7687087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
76947c6ae99SBarry Smith {
77047c6ae99SBarry Smith   PetscErrorCode ierr;
77147c6ae99SBarry Smith 
77247c6ae99SBarry Smith   PetscFunctionBegin;
773843c4018SMatthew G Knepley   ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
77447c6ae99SBarry Smith   PetscFunctionReturn(0);
77547c6ae99SBarry Smith }
77647c6ae99SBarry Smith 
77747c6ae99SBarry Smith #undef __FUNCT__
7789a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
77947c6ae99SBarry Smith /*@
7809a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
7819a42bb27SBarry Smith 
7829a42bb27SBarry Smith     Neighbor-wise Collective on DM
7839a42bb27SBarry Smith 
7849a42bb27SBarry Smith     Input Parameters:
7859a42bb27SBarry Smith +   dm - the DM object
786f6813fd5SJed Brown .   l - the local vector
7879a42bb27SBarry 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
7889a42bb27SBarry Smith            base point.
789f6813fd5SJed Brown - - the global vector
7909a42bb27SBarry Smith 
7919a42bb27SBarry 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
7929a42bb27SBarry 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
7939a42bb27SBarry Smith            global array to the final global array with VecAXPY().
7949a42bb27SBarry Smith 
7959a42bb27SBarry Smith     Level: beginner
7969a42bb27SBarry Smith 
797e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
7989a42bb27SBarry Smith 
7999a42bb27SBarry Smith @*/
8007087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
8019a42bb27SBarry Smith {
8029a42bb27SBarry Smith   PetscErrorCode ierr;
8039a42bb27SBarry Smith 
8049a42bb27SBarry Smith   PetscFunctionBegin;
805843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
8069a42bb27SBarry Smith   PetscFunctionReturn(0);
8079a42bb27SBarry Smith }
8089a42bb27SBarry Smith 
8099a42bb27SBarry Smith #undef __FUNCT__
8109a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
8119a42bb27SBarry Smith /*@
8129a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
81347c6ae99SBarry Smith 
81447c6ae99SBarry Smith     Neighbor-wise Collective on DM
81547c6ae99SBarry Smith 
81647c6ae99SBarry Smith     Input Parameters:
81747c6ae99SBarry Smith +   dm - the DM object
818f6813fd5SJed Brown .   l - the local vector
81947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
820f6813fd5SJed Brown -   g - the global vector
82147c6ae99SBarry Smith 
82247c6ae99SBarry Smith 
82347c6ae99SBarry Smith     Level: beginner
82447c6ae99SBarry Smith 
825e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
82647c6ae99SBarry Smith 
82747c6ae99SBarry Smith @*/
8287087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
82947c6ae99SBarry Smith {
83047c6ae99SBarry Smith   PetscErrorCode ierr;
83147c6ae99SBarry Smith 
83247c6ae99SBarry Smith   PetscFunctionBegin;
833843c4018SMatthew G Knepley   ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
83447c6ae99SBarry Smith   PetscFunctionReturn(0);
83547c6ae99SBarry Smith }
83647c6ae99SBarry Smith 
83747c6ae99SBarry Smith #undef __FUNCT__
83847c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
83947c6ae99SBarry Smith /*@
84047c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
84147c6ae99SBarry Smith 
84247c6ae99SBarry Smith     Collective on DM
84347c6ae99SBarry Smith 
84447c6ae99SBarry Smith     Input Parameter:
84547c6ae99SBarry Smith +   dm - the DM object
84647c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
84747c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
84847c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
84947c6ae99SBarry Smith 
85047c6ae99SBarry Smith     Level: developer
85147c6ae99SBarry Smith 
852e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
85347c6ae99SBarry Smith          DMSetFunction()
85447c6ae99SBarry Smith 
85547c6ae99SBarry Smith @*/
8567087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
85747c6ae99SBarry Smith {
85847c6ae99SBarry Smith   PetscErrorCode ierr;
85947c6ae99SBarry Smith   PetscFunctionBegin;
86047c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
86147c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
86247c6ae99SBarry Smith   if (A != B) {
86347c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
86447c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
86547c6ae99SBarry Smith   }
86647c6ae99SBarry Smith   PetscFunctionReturn(0);
86747c6ae99SBarry Smith }
86847c6ae99SBarry Smith 
86947c6ae99SBarry Smith #undef __FUNCT__
87047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
87147c6ae99SBarry Smith /*@
87247c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
87347c6ae99SBarry Smith 
87447c6ae99SBarry Smith     Collective on DM
87547c6ae99SBarry Smith 
87647c6ae99SBarry Smith     Input Parameter:
87747c6ae99SBarry Smith +   dm - the DM object
87847c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
87947c6ae99SBarry Smith 
88047c6ae99SBarry Smith     Output Parameter:
88147c6ae99SBarry Smith .   dmc - the coarsened DM
88247c6ae99SBarry Smith 
88347c6ae99SBarry Smith     Level: developer
88447c6ae99SBarry Smith 
885e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
88647c6ae99SBarry Smith 
88747c6ae99SBarry Smith @*/
8887087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
88947c6ae99SBarry Smith {
89047c6ae99SBarry Smith   PetscErrorCode ierr;
89147c6ae99SBarry Smith 
89247c6ae99SBarry Smith   PetscFunctionBegin;
89347c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
89447c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
89547c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
89647c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
89747c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
89847c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
89947c6ae99SBarry Smith   }
9008cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
901644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
902656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
90347c6ae99SBarry Smith   PetscFunctionReturn(0);
90447c6ae99SBarry Smith }
90547c6ae99SBarry Smith 
90647c6ae99SBarry Smith #undef __FUNCT__
90747c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
90847c6ae99SBarry Smith /*@C
90947c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
91047c6ae99SBarry Smith 
91147c6ae99SBarry Smith     Collective on DM
91247c6ae99SBarry Smith 
91347c6ae99SBarry Smith     Input Parameter:
91447c6ae99SBarry Smith +   dm - the DM object
91547c6ae99SBarry Smith -   nlevels - the number of levels of refinement
91647c6ae99SBarry Smith 
91747c6ae99SBarry Smith     Output Parameter:
91847c6ae99SBarry Smith .   dmf - the refined DM hierarchy
91947c6ae99SBarry Smith 
92047c6ae99SBarry Smith     Level: developer
92147c6ae99SBarry Smith 
922e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
92347c6ae99SBarry Smith 
92447c6ae99SBarry Smith @*/
9257087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
92647c6ae99SBarry Smith {
92747c6ae99SBarry Smith   PetscErrorCode ierr;
92847c6ae99SBarry Smith 
92947c6ae99SBarry Smith   PetscFunctionBegin;
93047c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
93147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
93247c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
93347c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
93447c6ae99SBarry Smith   } else if (dm->ops->refine) {
93547c6ae99SBarry Smith     PetscInt i;
93647c6ae99SBarry Smith 
93747c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
93847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
93947c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
94047c6ae99SBarry Smith     }
94147c6ae99SBarry Smith   } else {
94247c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
94347c6ae99SBarry Smith   }
94447c6ae99SBarry Smith   PetscFunctionReturn(0);
94547c6ae99SBarry Smith }
94647c6ae99SBarry Smith 
94747c6ae99SBarry Smith #undef __FUNCT__
94847c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
94947c6ae99SBarry Smith /*@C
95047c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
95147c6ae99SBarry Smith 
95247c6ae99SBarry Smith     Collective on DM
95347c6ae99SBarry Smith 
95447c6ae99SBarry Smith     Input Parameter:
95547c6ae99SBarry Smith +   dm - the DM object
95647c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
95747c6ae99SBarry Smith 
95847c6ae99SBarry Smith     Output Parameter:
95947c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
96047c6ae99SBarry Smith 
96147c6ae99SBarry Smith     Level: developer
96247c6ae99SBarry Smith 
963e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
96447c6ae99SBarry Smith 
96547c6ae99SBarry Smith @*/
9667087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
96747c6ae99SBarry Smith {
96847c6ae99SBarry Smith   PetscErrorCode ierr;
96947c6ae99SBarry Smith 
97047c6ae99SBarry Smith   PetscFunctionBegin;
97147c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
97247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
97347c6ae99SBarry Smith   PetscValidPointer(dmc,3);
97447c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
97547c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
97647c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
97747c6ae99SBarry Smith     PetscInt i;
97847c6ae99SBarry Smith 
97947c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
98047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
98147c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
98247c6ae99SBarry Smith     }
98347c6ae99SBarry Smith   } else {
98447c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
98547c6ae99SBarry Smith   }
98647c6ae99SBarry Smith   PetscFunctionReturn(0);
98747c6ae99SBarry Smith }
98847c6ae99SBarry Smith 
98947c6ae99SBarry Smith #undef __FUNCT__
990e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
99147c6ae99SBarry Smith /*@
992e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
99347c6ae99SBarry Smith    grids associated with two DMs.
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith    Collective on DM
99647c6ae99SBarry Smith 
99747c6ae99SBarry Smith    Input Parameters:
99847c6ae99SBarry Smith +  dmc - the coarse grid DM
99947c6ae99SBarry Smith -  dmf - the fine grid DM
100047c6ae99SBarry Smith 
100147c6ae99SBarry Smith    Output Parameters:
100247c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
100347c6ae99SBarry Smith 
100447c6ae99SBarry Smith    Level: intermediate
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
100747c6ae99SBarry Smith 
1008e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
100947c6ae99SBarry Smith @*/
1010e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
101147c6ae99SBarry Smith {
101247c6ae99SBarry Smith   PetscErrorCode ierr;
101347c6ae99SBarry Smith 
101447c6ae99SBarry Smith   PetscFunctionBegin;
101547c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
101647c6ae99SBarry Smith   PetscFunctionReturn(0);
101747c6ae99SBarry Smith }
101847c6ae99SBarry Smith 
101947c6ae99SBarry Smith #undef __FUNCT__
10201a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
10211a266240SBarry Smith /*@C
10221a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
10231a266240SBarry Smith 
10241a266240SBarry Smith     Not Collective
10251a266240SBarry Smith 
10261a266240SBarry Smith     Input Parameters:
10271a266240SBarry Smith +   dm - the DM object
10281a266240SBarry Smith -   destroy - the destroy function
10291a266240SBarry Smith 
10301a266240SBarry Smith     Level: intermediate
10311a266240SBarry Smith 
1032e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
10331a266240SBarry Smith 
1034f07f9ceaSJed Brown @*/
10351a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
10361a266240SBarry Smith {
10371a266240SBarry Smith   PetscFunctionBegin;
10381a266240SBarry Smith   dm->ctxdestroy = destroy;
10391a266240SBarry Smith   PetscFunctionReturn(0);
10401a266240SBarry Smith }
10411a266240SBarry Smith 
10421a266240SBarry Smith #undef __FUNCT__
10431b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1044b07ff414SBarry Smith /*@
10451b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
104647c6ae99SBarry Smith 
104747c6ae99SBarry Smith     Not Collective
104847c6ae99SBarry Smith 
104947c6ae99SBarry Smith     Input Parameters:
105047c6ae99SBarry Smith +   dm - the DM object
105147c6ae99SBarry Smith -   ctx - the user context
105247c6ae99SBarry Smith 
105347c6ae99SBarry Smith     Level: intermediate
105447c6ae99SBarry Smith 
1055e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
105647c6ae99SBarry Smith 
105747c6ae99SBarry Smith @*/
10581b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
105947c6ae99SBarry Smith {
106047c6ae99SBarry Smith   PetscFunctionBegin;
106147c6ae99SBarry Smith   dm->ctx = ctx;
106247c6ae99SBarry Smith   PetscFunctionReturn(0);
106347c6ae99SBarry Smith }
106447c6ae99SBarry Smith 
106547c6ae99SBarry Smith #undef __FUNCT__
10661b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
106747c6ae99SBarry Smith /*@
10681b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
106947c6ae99SBarry Smith 
107047c6ae99SBarry Smith     Not Collective
107147c6ae99SBarry Smith 
107247c6ae99SBarry Smith     Input Parameter:
107347c6ae99SBarry Smith .   dm - the DM object
107447c6ae99SBarry Smith 
107547c6ae99SBarry Smith     Output Parameter:
107647c6ae99SBarry Smith .   ctx - the user context
107747c6ae99SBarry Smith 
107847c6ae99SBarry Smith     Level: intermediate
107947c6ae99SBarry Smith 
1080e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
108147c6ae99SBarry Smith 
108247c6ae99SBarry Smith @*/
10831b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
108447c6ae99SBarry Smith {
108547c6ae99SBarry Smith   PetscFunctionBegin;
10861b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
108747c6ae99SBarry Smith   PetscFunctionReturn(0);
108847c6ae99SBarry Smith }
108947c6ae99SBarry Smith 
109047c6ae99SBarry Smith #undef __FUNCT__
109147c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
10927e833e3aSBarry Smith /*@C
109347c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
109447c6ae99SBarry Smith 
109547c6ae99SBarry Smith     Logically Collective on DM
109647c6ae99SBarry Smith 
109747c6ae99SBarry Smith     Input Parameter:
109847c6ae99SBarry Smith +   dm - the DM object to destroy
109947c6ae99SBarry Smith -   f - the function to compute the initial guess
110047c6ae99SBarry Smith 
110147c6ae99SBarry Smith     Level: intermediate
110247c6ae99SBarry Smith 
1103e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
110447c6ae99SBarry Smith 
1105f07f9ceaSJed Brown @*/
11067087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
110747c6ae99SBarry Smith {
110847c6ae99SBarry Smith   PetscFunctionBegin;
110947c6ae99SBarry Smith   dm->ops->initialguess = f;
111047c6ae99SBarry Smith   PetscFunctionReturn(0);
111147c6ae99SBarry Smith }
111247c6ae99SBarry Smith 
111347c6ae99SBarry Smith #undef __FUNCT__
111447c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
11157e833e3aSBarry Smith /*@C
111647c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
111747c6ae99SBarry Smith 
111847c6ae99SBarry Smith     Logically Collective on DM
111947c6ae99SBarry Smith 
112047c6ae99SBarry Smith     Input Parameter:
112147c6ae99SBarry Smith +   dm - the DM object
112247c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
112347c6ae99SBarry Smith 
112447c6ae99SBarry Smith     Level: intermediate
112547c6ae99SBarry Smith 
112647c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
112747c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
112847c6ae99SBarry Smith 
1129e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
113047c6ae99SBarry Smith          DMSetJacobian()
113147c6ae99SBarry Smith 
1132f07f9ceaSJed Brown @*/
11337087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
113447c6ae99SBarry Smith {
113547c6ae99SBarry Smith   PetscFunctionBegin;
113647c6ae99SBarry Smith   dm->ops->function = f;
113747c6ae99SBarry Smith   if (f) {
113847c6ae99SBarry Smith     dm->ops->functionj = f;
113947c6ae99SBarry Smith   }
114047c6ae99SBarry Smith   PetscFunctionReturn(0);
114147c6ae99SBarry Smith }
114247c6ae99SBarry Smith 
114347c6ae99SBarry Smith #undef __FUNCT__
114447c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
11457e833e3aSBarry Smith /*@C
114647c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
114747c6ae99SBarry Smith 
114847c6ae99SBarry Smith     Logically Collective on DM
114947c6ae99SBarry Smith 
115047c6ae99SBarry Smith     Input Parameter:
115147c6ae99SBarry Smith +   dm - the DM object to destroy
115247c6ae99SBarry Smith -   f - the function to compute the matrix entries
115347c6ae99SBarry Smith 
115447c6ae99SBarry Smith     Level: intermediate
115547c6ae99SBarry Smith 
1156e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
115747c6ae99SBarry Smith          DMSetFunction()
115847c6ae99SBarry Smith 
1159f07f9ceaSJed Brown @*/
11607087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
116147c6ae99SBarry Smith {
116247c6ae99SBarry Smith   PetscFunctionBegin;
116347c6ae99SBarry Smith   dm->ops->jacobian = f;
116447c6ae99SBarry Smith   PetscFunctionReturn(0);
116547c6ae99SBarry Smith }
116647c6ae99SBarry Smith 
116747c6ae99SBarry Smith #undef __FUNCT__
116847c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
116947c6ae99SBarry Smith /*@
117047c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
117147c6ae99SBarry Smith 
117247c6ae99SBarry Smith     Collective on DM
117347c6ae99SBarry Smith 
117447c6ae99SBarry Smith     Input Parameter:
117547c6ae99SBarry Smith +   dm - the DM object to destroy
117647c6ae99SBarry Smith -   x - the vector to hold the initial guess values
117747c6ae99SBarry Smith 
117847c6ae99SBarry Smith     Level: developer
117947c6ae99SBarry Smith 
1180e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
118147c6ae99SBarry Smith 
118247c6ae99SBarry Smith @*/
11837087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
118447c6ae99SBarry Smith {
118547c6ae99SBarry Smith   PetscErrorCode ierr;
118647c6ae99SBarry Smith   PetscFunctionBegin;
118747c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
118847c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
118947c6ae99SBarry Smith   PetscFunctionReturn(0);
119047c6ae99SBarry Smith }
119147c6ae99SBarry Smith 
119247c6ae99SBarry Smith #undef __FUNCT__
119347c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
119447c6ae99SBarry Smith /*@
119547c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
119647c6ae99SBarry Smith 
119747c6ae99SBarry Smith     Not Collective
119847c6ae99SBarry Smith 
119947c6ae99SBarry Smith     Input Parameter:
120047c6ae99SBarry Smith .   dm - the DM object to destroy
120147c6ae99SBarry Smith 
120247c6ae99SBarry Smith     Output Parameter:
120347c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
120447c6ae99SBarry Smith 
120547c6ae99SBarry Smith     Level: developer
120647c6ae99SBarry Smith 
1207e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
120847c6ae99SBarry Smith 
120947c6ae99SBarry Smith @*/
12107087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
121147c6ae99SBarry Smith {
121247c6ae99SBarry Smith   PetscFunctionBegin;
121347c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
121447c6ae99SBarry Smith   PetscFunctionReturn(0);
121547c6ae99SBarry Smith }
121647c6ae99SBarry Smith 
121747c6ae99SBarry Smith #undef __FUNCT__
121847c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
121947c6ae99SBarry Smith /*@
122047c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
122147c6ae99SBarry Smith 
122247c6ae99SBarry Smith     Not Collective
122347c6ae99SBarry Smith 
122447c6ae99SBarry Smith     Input Parameter:
122547c6ae99SBarry Smith .   dm - the DM object to destroy
122647c6ae99SBarry Smith 
122747c6ae99SBarry Smith     Output Parameter:
122847c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
122947c6ae99SBarry Smith 
123047c6ae99SBarry Smith     Level: developer
123147c6ae99SBarry Smith 
1232e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
123347c6ae99SBarry Smith 
123447c6ae99SBarry Smith @*/
12357087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
123647c6ae99SBarry Smith {
123747c6ae99SBarry Smith   PetscFunctionBegin;
123847c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
123947c6ae99SBarry Smith   PetscFunctionReturn(0);
124047c6ae99SBarry Smith }
124147c6ae99SBarry Smith 
124247c6ae99SBarry Smith #undef __FUNCT__
124347c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
124447c6ae99SBarry Smith /*@
124547c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
124647c6ae99SBarry Smith 
124747c6ae99SBarry Smith     Not Collective
124847c6ae99SBarry Smith 
124947c6ae99SBarry Smith     Input Parameter:
125047c6ae99SBarry Smith .   dm - the DM object to destroy
125147c6ae99SBarry Smith 
125247c6ae99SBarry Smith     Output Parameter:
125347c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
125447c6ae99SBarry Smith 
125547c6ae99SBarry Smith     Level: developer
125647c6ae99SBarry Smith 
1257e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
125847c6ae99SBarry Smith 
125947c6ae99SBarry Smith @*/
12607087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
126147c6ae99SBarry Smith {
126247c6ae99SBarry Smith   PetscFunctionBegin;
126347c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
126447c6ae99SBarry Smith   PetscFunctionReturn(0);
126547c6ae99SBarry Smith }
126647c6ae99SBarry Smith 
126747c6ae99SBarry Smith #undef __FUNCT__
126847c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
126947c6ae99SBarry Smith /*@
127047c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
127147c6ae99SBarry Smith 
127247c6ae99SBarry Smith     Collective on DM
127347c6ae99SBarry Smith 
127447c6ae99SBarry Smith     Input Parameter:
127547c6ae99SBarry Smith +   dm - the DM object to destroy
127647c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
127747c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
127847c6ae99SBarry Smith 
127947c6ae99SBarry Smith     Level: developer
128047c6ae99SBarry Smith 
1281e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
128247c6ae99SBarry Smith          DMSetJacobian()
128347c6ae99SBarry Smith 
128447c6ae99SBarry Smith @*/
12857087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
128647c6ae99SBarry Smith {
128747c6ae99SBarry Smith   PetscErrorCode ierr;
128847c6ae99SBarry Smith   PetscFunctionBegin;
128947c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
1290644e2e5bSBarry Smith   PetscStackPush("DM user function");
129147c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
1292644e2e5bSBarry Smith   PetscStackPop;
129347c6ae99SBarry Smith   PetscFunctionReturn(0);
129447c6ae99SBarry Smith }
129547c6ae99SBarry Smith 
129647c6ae99SBarry Smith 
129747c6ae99SBarry Smith #undef __FUNCT__
129847c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
129947c6ae99SBarry Smith /*@
130047c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
130147c6ae99SBarry Smith 
130247c6ae99SBarry Smith     Collective on DM
130347c6ae99SBarry Smith 
130447c6ae99SBarry Smith     Input Parameter:
130547c6ae99SBarry Smith +   dm - the DM object
1306cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
130747c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
130847c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
130947c6ae99SBarry Smith 
131047c6ae99SBarry Smith     Level: developer
131147c6ae99SBarry Smith 
1312e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
131347c6ae99SBarry Smith          DMSetFunction()
131447c6ae99SBarry Smith 
131547c6ae99SBarry Smith @*/
13167087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
131747c6ae99SBarry Smith {
131847c6ae99SBarry Smith   PetscErrorCode ierr;
131947c6ae99SBarry Smith 
132047c6ae99SBarry Smith   PetscFunctionBegin;
132147c6ae99SBarry Smith   if (!dm->ops->jacobian) {
132247c6ae99SBarry Smith     ISColoring     coloring;
132347c6ae99SBarry Smith     MatFDColoring  fd;
132447c6ae99SBarry Smith 
1325e727c939SJed Brown     ierr = DMCreateColoring(dm,IS_COLORING_GLOBAL,MATAIJ,&coloring);CHKERRQ(ierr);
132647c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
1327fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
132847c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
13290bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
13300bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
133171cd77b2SBarry Smith 
133247c6ae99SBarry Smith     dm->fd = fd;
133347c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
13342533e041SBarry Smith 
133571cd77b2SBarry Smith     /* don't know why this is needed */
133671cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
133747c6ae99SBarry Smith   }
133847c6ae99SBarry Smith   if (!x) x = dm->x;
133947c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
1340cab2e9ccSBarry Smith 
134171cd77b2SBarry 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 */
1342649052a6SBarry Smith   if (x) {
1343cab2e9ccSBarry Smith     if (!dm->x) {
134471cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
1345cab2e9ccSBarry Smith     }
1346cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
1347649052a6SBarry Smith   }
1348a8248277SBarry Smith   if (A != B) {
1349a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1350a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1351a8248277SBarry Smith   }
135247c6ae99SBarry Smith   PetscFunctionReturn(0);
135347c6ae99SBarry Smith }
1354264ace61SBarry Smith 
1355cab2e9ccSBarry Smith 
1356264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
1357264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
1358264ace61SBarry Smith 
1359264ace61SBarry Smith #undef __FUNCT__
1360264ace61SBarry Smith #define __FUNCT__ "DMSetType"
1361264ace61SBarry Smith /*@C
1362264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
1363264ace61SBarry Smith 
1364264ace61SBarry Smith   Collective on DM
1365264ace61SBarry Smith 
1366264ace61SBarry Smith   Input Parameters:
1367264ace61SBarry Smith + dm     - The DM object
1368264ace61SBarry Smith - method - The name of the DM type
1369264ace61SBarry Smith 
1370264ace61SBarry Smith   Options Database Key:
1371264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
1372264ace61SBarry Smith 
1373264ace61SBarry Smith   Notes:
1374e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
1375264ace61SBarry Smith 
1376264ace61SBarry Smith   Level: intermediate
1377264ace61SBarry Smith 
1378264ace61SBarry Smith .keywords: DM, set, type
1379264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
1380264ace61SBarry Smith @*/
13817087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
1382264ace61SBarry Smith {
1383264ace61SBarry Smith   PetscErrorCode (*r)(DM);
1384264ace61SBarry Smith   PetscBool      match;
1385264ace61SBarry Smith   PetscErrorCode ierr;
1386264ace61SBarry Smith 
1387264ace61SBarry Smith   PetscFunctionBegin;
1388264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1389264ace61SBarry Smith   ierr = PetscTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
1390264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
1391264ace61SBarry Smith 
1392264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
13934b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
1394264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
1395264ace61SBarry Smith 
1396264ace61SBarry Smith   if (dm->ops->destroy) {
1397264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
1398264ace61SBarry Smith   }
1399264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
1400264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
1401264ace61SBarry Smith   PetscFunctionReturn(0);
1402264ace61SBarry Smith }
1403264ace61SBarry Smith 
1404264ace61SBarry Smith #undef __FUNCT__
1405264ace61SBarry Smith #define __FUNCT__ "DMGetType"
1406264ace61SBarry Smith /*@C
1407264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
1408264ace61SBarry Smith 
1409264ace61SBarry Smith   Not Collective
1410264ace61SBarry Smith 
1411264ace61SBarry Smith   Input Parameter:
1412264ace61SBarry Smith . dm  - The DM
1413264ace61SBarry Smith 
1414264ace61SBarry Smith   Output Parameter:
1415264ace61SBarry Smith . type - The DM type name
1416264ace61SBarry Smith 
1417264ace61SBarry Smith   Level: intermediate
1418264ace61SBarry Smith 
1419264ace61SBarry Smith .keywords: DM, get, type, name
1420264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
1421264ace61SBarry Smith @*/
14227087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
1423264ace61SBarry Smith {
1424264ace61SBarry Smith   PetscErrorCode ierr;
1425264ace61SBarry Smith 
1426264ace61SBarry Smith   PetscFunctionBegin;
1427264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1428264ace61SBarry Smith   PetscValidCharPointer(type,2);
1429264ace61SBarry Smith   if (!DMRegisterAllCalled) {
1430264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
1431264ace61SBarry Smith   }
1432264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
1433264ace61SBarry Smith   PetscFunctionReturn(0);
1434264ace61SBarry Smith }
1435264ace61SBarry Smith 
143667a56275SMatthew G Knepley #undef __FUNCT__
143767a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
143867a56275SMatthew G Knepley /*@C
143967a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
144067a56275SMatthew G Knepley 
144167a56275SMatthew G Knepley   Collective on DM
144267a56275SMatthew G Knepley 
144367a56275SMatthew G Knepley   Input Parameters:
144467a56275SMatthew G Knepley + dm - the DM
144567a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
144667a56275SMatthew G Knepley 
144767a56275SMatthew G Knepley   Output Parameter:
144867a56275SMatthew G Knepley . M - pointer to new DM
144967a56275SMatthew G Knepley 
145067a56275SMatthew G Knepley   Notes:
145167a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
145267a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
145367a56275SMatthew G Knepley   of the input DM.
145467a56275SMatthew G Knepley 
145567a56275SMatthew G Knepley   Level: intermediate
145667a56275SMatthew G Knepley 
145767a56275SMatthew G Knepley .seealso: DMCreate()
145867a56275SMatthew G Knepley @*/
145967a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
146067a56275SMatthew G Knepley {
146167a56275SMatthew G Knepley   DM             B;
146267a56275SMatthew G Knepley   char           convname[256];
146367a56275SMatthew G Knepley   PetscBool      sametype, issame;
146467a56275SMatthew G Knepley   PetscErrorCode ierr;
146567a56275SMatthew G Knepley 
146667a56275SMatthew G Knepley   PetscFunctionBegin;
146767a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
146867a56275SMatthew G Knepley   PetscValidType(dm,1);
146967a56275SMatthew G Knepley   PetscValidPointer(M,3);
147067a56275SMatthew G Knepley   ierr = PetscTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
147167a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
147267a56275SMatthew G Knepley   {
147367a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
147467a56275SMatthew G Knepley 
147567a56275SMatthew G Knepley     /*
147667a56275SMatthew G Knepley        Order of precedence:
147767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
147867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
147967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
148067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
148167a56275SMatthew G Knepley        5) Use a really basic converter.
148267a56275SMatthew G Knepley     */
148367a56275SMatthew G Knepley 
148467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
148567a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
148667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
148767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
148867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
148967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
149067a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
149167a56275SMatthew G Knepley     if (conv) goto foundconv;
149267a56275SMatthew G Knepley 
149367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
149467a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
149567a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
149667a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
149767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
149867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
149967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
150067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
150167a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
150267a56275SMatthew G Knepley     if (conv) {
1503fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
150467a56275SMatthew G Knepley       goto foundconv;
150567a56275SMatthew G Knepley     }
150667a56275SMatthew G Knepley 
150767a56275SMatthew G Knepley #if 0
150867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
150967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
1510fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
151167a56275SMatthew G Knepley     if (conv) goto foundconv;
151267a56275SMatthew G Knepley 
151367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
151467a56275SMatthew G Knepley     if (dm->ops->convert) {
151567a56275SMatthew G Knepley       conv = dm->ops->convert;
151667a56275SMatthew G Knepley     }
151767a56275SMatthew G Knepley     if (conv) goto foundconv;
151867a56275SMatthew G Knepley #endif
151967a56275SMatthew G Knepley 
152067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
152167a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
152267a56275SMatthew G Knepley 
152367a56275SMatthew G Knepley     foundconv:
152467a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
152567a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
152667a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
152767a56275SMatthew G Knepley   }
152867a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
152967a56275SMatthew G Knepley   PetscFunctionReturn(0);
153067a56275SMatthew G Knepley }
1531264ace61SBarry Smith 
1532264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1533264ace61SBarry Smith 
1534264ace61SBarry Smith #undef __FUNCT__
1535264ace61SBarry Smith #define __FUNCT__ "DMRegister"
1536264ace61SBarry Smith /*@C
1537264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
1538264ace61SBarry Smith 
1539264ace61SBarry Smith   Level: advanced
1540264ace61SBarry Smith @*/
15417087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
1542264ace61SBarry Smith {
1543264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
1544264ace61SBarry Smith   PetscErrorCode ierr;
1545264ace61SBarry Smith 
1546264ace61SBarry Smith   PetscFunctionBegin;
1547264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
1548264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
1549264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
1550264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
1551264ace61SBarry Smith   PetscFunctionReturn(0);
1552264ace61SBarry Smith }
1553264ace61SBarry Smith 
1554264ace61SBarry Smith 
1555264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1556264ace61SBarry Smith #undef __FUNCT__
1557264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
1558264ace61SBarry Smith /*@C
1559264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
1560264ace61SBarry Smith 
1561264ace61SBarry Smith    Not Collective
1562264ace61SBarry Smith 
1563264ace61SBarry Smith    Level: advanced
1564264ace61SBarry Smith 
1565264ace61SBarry Smith .keywords: DM, register, destroy
1566264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
1567264ace61SBarry Smith @*/
15687087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
1569264ace61SBarry Smith {
1570264ace61SBarry Smith   PetscErrorCode ierr;
1571264ace61SBarry Smith 
1572264ace61SBarry Smith   PetscFunctionBegin;
1573264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
1574264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
1575264ace61SBarry Smith   PetscFunctionReturn(0);
1576264ace61SBarry Smith }
157723f975d1SBarry Smith 
157823f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
1579c6db04a5SJed Brown #include <mex.h>
158023f975d1SBarry Smith 
15813014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
158223f975d1SBarry Smith 
158323f975d1SBarry Smith #undef __FUNCT__
158423f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
158523f975d1SBarry Smith /*
158623f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
158723f975d1SBarry Smith                          DMSetFunctionMatlab().
158823f975d1SBarry Smith 
158923f975d1SBarry Smith    For linear problems x is null
159023f975d1SBarry Smith 
159123f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
159223f975d1SBarry Smith */
15937087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
159423f975d1SBarry Smith {
159523f975d1SBarry Smith   PetscErrorCode    ierr;
159623f975d1SBarry Smith   DMMatlabContext   *sctx;
159723f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
159823f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
159923f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
160023f975d1SBarry Smith 
160123f975d1SBarry Smith   PetscFunctionBegin;
160223f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
160323f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
160423f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
160523f975d1SBarry Smith 
160623f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
16071b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
160823f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
160923f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
16103014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
161123f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
161223f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
161323f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
161423f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
1615b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
161623f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
161723f975d1SBarry Smith   mxDestroyArray(prhs[0]);
161823f975d1SBarry Smith   mxDestroyArray(prhs[1]);
161923f975d1SBarry Smith   mxDestroyArray(prhs[2]);
162023f975d1SBarry Smith   mxDestroyArray(prhs[3]);
162123f975d1SBarry Smith   mxDestroyArray(plhs[0]);
162223f975d1SBarry Smith   PetscFunctionReturn(0);
162323f975d1SBarry Smith }
162423f975d1SBarry Smith 
162523f975d1SBarry Smith 
162623f975d1SBarry Smith #undef __FUNCT__
162723f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
162823f975d1SBarry Smith /*
162923f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
163023f975d1SBarry Smith 
163123f975d1SBarry Smith */
16327087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
163323f975d1SBarry Smith {
163423f975d1SBarry Smith   PetscErrorCode    ierr;
163523f975d1SBarry Smith   DMMatlabContext   *sctx;
163623f975d1SBarry Smith 
163723f975d1SBarry Smith   PetscFunctionBegin;
163823f975d1SBarry Smith   /* currently sctx is memory bleed */
16391b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
16403014e516SBarry Smith   if (!sctx) {
164123f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
16423014e516SBarry Smith   }
164323f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
16441b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
164523f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
164623f975d1SBarry Smith   PetscFunctionReturn(0);
164723f975d1SBarry Smith }
16483014e516SBarry Smith 
16493014e516SBarry Smith #undef __FUNCT__
16503014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
16513014e516SBarry Smith /*
16523014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
16533014e516SBarry Smith                          DMSetJacobianMatlab().
16543014e516SBarry Smith 
16553014e516SBarry Smith    For linear problems x is null
16563014e516SBarry Smith 
16573014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
16583014e516SBarry Smith */
16597087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
16603014e516SBarry Smith {
16613014e516SBarry Smith   PetscErrorCode    ierr;
16623014e516SBarry Smith   DMMatlabContext   *sctx;
16633014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
16643014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
16653014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
16663014e516SBarry Smith 
16673014e516SBarry Smith   PetscFunctionBegin;
16683014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16693014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
16703014e516SBarry Smith 
1671e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
16721b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
16733014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
16743014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
16753014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
16763014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
16773014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
16783014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
16793014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
16803014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
16813014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
1682b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
1683c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
1684c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
16853014e516SBarry Smith   mxDestroyArray(prhs[0]);
16863014e516SBarry Smith   mxDestroyArray(prhs[1]);
16873014e516SBarry Smith   mxDestroyArray(prhs[2]);
16883014e516SBarry Smith   mxDestroyArray(prhs[3]);
16893014e516SBarry Smith   mxDestroyArray(prhs[4]);
16903014e516SBarry Smith   mxDestroyArray(plhs[0]);
16913014e516SBarry Smith   mxDestroyArray(plhs[1]);
16923014e516SBarry Smith   PetscFunctionReturn(0);
16933014e516SBarry Smith }
16943014e516SBarry Smith 
16953014e516SBarry Smith 
16963014e516SBarry Smith #undef __FUNCT__
16973014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
16983014e516SBarry Smith /*
16993014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
17003014e516SBarry Smith 
17013014e516SBarry Smith */
17027087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
17033014e516SBarry Smith {
17043014e516SBarry Smith   PetscErrorCode    ierr;
17053014e516SBarry Smith   DMMatlabContext   *sctx;
17063014e516SBarry Smith 
17073014e516SBarry Smith   PetscFunctionBegin;
17083014e516SBarry Smith   /* currently sctx is memory bleed */
17091b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
17103014e516SBarry Smith   if (!sctx) {
17113014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
17123014e516SBarry Smith   }
17133014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
17141b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
17153014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
17163014e516SBarry Smith   PetscFunctionReturn(0);
17173014e516SBarry Smith }
171823f975d1SBarry Smith #endif
1719b859378eSBarry Smith 
1720b859378eSBarry Smith #undef __FUNCT__
1721b859378eSBarry Smith #define __FUNCT__ "DMLoad"
1722b859378eSBarry Smith /*@C
1723b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
1724b859378eSBarry Smith   with DMView().
1725b859378eSBarry Smith 
1726b859378eSBarry Smith   Collective on PetscViewer
1727b859378eSBarry Smith 
1728b859378eSBarry Smith   Input Parameters:
1729b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
1730b859378eSBarry Smith            some related function before a call to DMLoad().
1731b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
1732b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
1733b859378eSBarry Smith 
1734b859378eSBarry Smith    Level: intermediate
1735b859378eSBarry Smith 
1736b859378eSBarry Smith   Notes:
1737b859378eSBarry Smith   Defaults to the DM DA.
1738b859378eSBarry Smith 
1739b859378eSBarry Smith   Notes for advanced users:
1740b859378eSBarry Smith   Most users should not need to know the details of the binary storage
1741b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
1742b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
1743b859378eSBarry Smith   format is
1744b859378eSBarry Smith .vb
1745b859378eSBarry Smith      has not yet been determined
1746b859378eSBarry Smith .ve
1747b859378eSBarry Smith 
1748b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
1749b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
1750b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
1751b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
1752b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
1753b859378eSBarry Smith 
1754b859378eSBarry Smith   Concepts: vector^loading from file
1755b859378eSBarry Smith 
1756b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
1757b859378eSBarry Smith @*/
1758b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
1759b859378eSBarry Smith {
1760b859378eSBarry Smith   PetscErrorCode ierr;
1761b859378eSBarry Smith 
1762b859378eSBarry Smith   PetscFunctionBegin;
1763b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
1764b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1765b859378eSBarry Smith 
1766b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
1767b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
1768b859378eSBarry Smith   }
1769b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
1770b859378eSBarry Smith   PetscFunctionReturn(0);
1771b859378eSBarry Smith }
1772b859378eSBarry Smith 
1773