xref: /petsc/src/dm/interface/dm.c (revision f07f9cea22d9c7235d5f7763ed877c96b52b91c4)
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 /*@
10a4121054SBarry Smith   DMCreate - Creates an empty vector object. The type can then be set with DMetType().
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
36a4121054SBarry Smith   ierr = DMInitializePackage(PETSC_NULL);CHKERRQ(ierr);
37a4121054SBarry Smith #endif
38a4121054SBarry Smith 
393194b578SJed Brown   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
40a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
411411c6eeSJed Brown 
421411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
431411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
441411c6eeSJed Brown   v->bs           = 1;
451411c6eeSJed Brown 
461411c6eeSJed Brown   *dm = v;
47a4121054SBarry Smith   PetscFunctionReturn(0);
48a4121054SBarry Smith }
49a4121054SBarry Smith 
50a4121054SBarry Smith 
51a4121054SBarry Smith #undef __FUNCT__
529a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
539a42bb27SBarry Smith /*@C
54564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
559a42bb27SBarry Smith 
56aa219208SBarry Smith    Logically Collective on DMDA
579a42bb27SBarry Smith 
589a42bb27SBarry Smith    Input Parameter:
599a42bb27SBarry Smith +  da - initial distributed array
608154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
619a42bb27SBarry Smith 
629a42bb27SBarry Smith    Options Database:
639a42bb27SBarry Smith .   -da_vec_type ctype
649a42bb27SBarry Smith 
659a42bb27SBarry Smith    Level: intermediate
669a42bb27SBarry Smith 
67aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
689a42bb27SBarry Smith @*/
697087cfbeSBarry Smith PetscErrorCode  DMSetVecType(DM da,const VecType ctype)
709a42bb27SBarry Smith {
719a42bb27SBarry Smith   PetscErrorCode ierr;
729a42bb27SBarry Smith 
739a42bb27SBarry Smith   PetscFunctionBegin;
749a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
759a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
769a42bb27SBarry Smith   ierr = PetscStrallocpy(ctype,&da->vectype);CHKERRQ(ierr);
779a42bb27SBarry Smith   PetscFunctionReturn(0);
789a42bb27SBarry Smith }
799a42bb27SBarry Smith 
809a42bb27SBarry Smith #undef __FUNCT__
819a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
829a42bb27SBarry Smith /*@C
839a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
84aa219208SBarry Smith    DMDA options in the database.
859a42bb27SBarry Smith 
86aa219208SBarry Smith    Logically Collective on DMDA
879a42bb27SBarry Smith 
889a42bb27SBarry Smith    Input Parameter:
89aa219208SBarry Smith +  da - the DMDA context
909a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
919a42bb27SBarry Smith 
929a42bb27SBarry Smith    Notes:
939a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
949a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
959a42bb27SBarry Smith 
969a42bb27SBarry Smith    Level: advanced
979a42bb27SBarry Smith 
98aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
999a42bb27SBarry Smith 
1009a42bb27SBarry Smith .seealso: DMSetFromOptions()
1019a42bb27SBarry Smith @*/
1027087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
1039a42bb27SBarry Smith {
1049a42bb27SBarry Smith   PetscErrorCode ierr;
1059a42bb27SBarry Smith 
1069a42bb27SBarry Smith   PetscFunctionBegin;
1079a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1089a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
1099a42bb27SBarry Smith   PetscFunctionReturn(0);
1109a42bb27SBarry Smith }
1119a42bb27SBarry Smith 
1129a42bb27SBarry Smith #undef __FUNCT__
11347c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
11447c6ae99SBarry Smith /*@
115aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
11647c6ae99SBarry Smith 
11747c6ae99SBarry Smith     Collective on DM
11847c6ae99SBarry Smith 
11947c6ae99SBarry Smith     Input Parameter:
12047c6ae99SBarry Smith .   dm - the DM object to destroy
12147c6ae99SBarry Smith 
12247c6ae99SBarry Smith     Level: developer
12347c6ae99SBarry Smith 
12447c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
12547c6ae99SBarry Smith 
12647c6ae99SBarry Smith @*/
127fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
12847c6ae99SBarry Smith {
129732e2eb9SMatthew G Knepley   PetscInt       i, cnt = 0;
13047c6ae99SBarry Smith   PetscErrorCode ierr;
13147c6ae99SBarry Smith 
13247c6ae99SBarry Smith   PetscFunctionBegin;
1336bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
1346bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
13587e657c6SBarry Smith 
13687e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
137732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1386bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
1396bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
140732e2eb9SMatthew G Knepley   }
14171cd77b2SBarry Smith   if ((*dm)->x) {
14271cd77b2SBarry Smith     PetscObject obj;
14371cd77b2SBarry Smith     ierr = PetscObjectQuery((PetscObject)(*dm)->x,"DM",&obj);CHKERRQ(ierr);
14471cd77b2SBarry Smith     if (obj == (PetscObject)*dm) cnt++;
14571cd77b2SBarry Smith   }
146732e2eb9SMatthew G Knepley 
1476bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
148732e2eb9SMatthew G Knepley   /*
149732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
150732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
151732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
152732e2eb9SMatthew G Knepley   */
1536bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
1546bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
155732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1566bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
1576bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
158732e2eb9SMatthew G Knepley   }
1591a266240SBarry Smith 
1601a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
1611a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
1621a266240SBarry Smith   }
16387e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
16471cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
1654dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
1666bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
1676bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
1686bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
169073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
170732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
1716bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
172732e2eb9SMatthew G Knepley 
1736bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
1746bf464f9SBarry Smith   ierr = PetscFree((*dm)->data);CHKERRQ(ierr);
175732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
17647c6ae99SBarry Smith   PetscFunctionReturn(0);
17747c6ae99SBarry Smith }
17847c6ae99SBarry Smith 
17947c6ae99SBarry Smith #undef __FUNCT__
180d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
181d7bf68aeSBarry Smith /*@
182d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
183d7bf68aeSBarry Smith 
184d7bf68aeSBarry Smith     Collective on DM
185d7bf68aeSBarry Smith 
186d7bf68aeSBarry Smith     Input Parameter:
187d7bf68aeSBarry Smith .   dm - the DM object to setup
188d7bf68aeSBarry Smith 
189d7bf68aeSBarry Smith     Level: developer
190d7bf68aeSBarry Smith 
191d7bf68aeSBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
192d7bf68aeSBarry Smith 
193d7bf68aeSBarry Smith @*/
1947087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
195d7bf68aeSBarry Smith {
196d7bf68aeSBarry Smith   PetscErrorCode ierr;
197d7bf68aeSBarry Smith 
198d7bf68aeSBarry Smith   PetscFunctionBegin;
1998387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
200d7bf68aeSBarry Smith   if (dm->ops->setup) {
201d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
202d7bf68aeSBarry Smith   }
2038387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
204d7bf68aeSBarry Smith   PetscFunctionReturn(0);
205d7bf68aeSBarry Smith }
206d7bf68aeSBarry Smith 
207d7bf68aeSBarry Smith #undef __FUNCT__
208d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
209d7bf68aeSBarry Smith /*@
210d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
211d7bf68aeSBarry Smith 
212d7bf68aeSBarry Smith     Collective on DM
213d7bf68aeSBarry Smith 
214d7bf68aeSBarry Smith     Input Parameter:
215d7bf68aeSBarry Smith .   dm - the DM object to set options for
216d7bf68aeSBarry Smith 
217732e2eb9SMatthew G Knepley     Options Database:
218732e2eb9SMatthew G Knepley .   -dm_preallocate_only: Only preallocate the matrix for DMGetMatrix(), but do not fill it with zeros
219732e2eb9SMatthew G Knepley 
220d7bf68aeSBarry Smith     Level: developer
221d7bf68aeSBarry Smith 
222d7bf68aeSBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
223d7bf68aeSBarry Smith 
224d7bf68aeSBarry Smith @*/
2257087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
226d7bf68aeSBarry Smith {
227073dac72SJed Brown   PetscBool      flg1 = PETSC_FALSE,flg;
228d7bf68aeSBarry Smith   PetscErrorCode ierr;
229073dac72SJed Brown   char           mtype[256] = MATAIJ;
230d7bf68aeSBarry Smith 
231d7bf68aeSBarry Smith   PetscFunctionBegin;
232d7bf68aeSBarry Smith   if (dm->ops->setfromoptions) {
233d7bf68aeSBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
234d7bf68aeSBarry Smith   }
2353194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
23682fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
237073dac72SJed 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);
238073dac72SJed Brown     ierr = PetscOptionsList("-dm_mat_type","Matrix type","MatSetType",MatList,mtype,mtype,sizeof mtype,&flg);CHKERRQ(ierr);
239073dac72SJed Brown     if (flg) {
240073dac72SJed Brown       ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
241073dac72SJed Brown       ierr = PetscStrallocpy(mtype,&dm->mattype);CHKERRQ(ierr);
242073dac72SJed Brown     }
24382fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
24482fcb398SMatthew G Knepley   if (flg1) {
24582fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
24682fcb398SMatthew G Knepley   }
247d7bf68aeSBarry Smith   PetscFunctionReturn(0);
248d7bf68aeSBarry Smith }
249d7bf68aeSBarry Smith 
250d7bf68aeSBarry Smith #undef __FUNCT__
25147c6ae99SBarry Smith #define __FUNCT__ "DMView"
252fc9bc008SSatish Balay /*@C
253aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
25447c6ae99SBarry Smith 
25547c6ae99SBarry Smith     Collective on DM
25647c6ae99SBarry Smith 
25747c6ae99SBarry Smith     Input Parameter:
25847c6ae99SBarry Smith +   dm - the DM object to view
25947c6ae99SBarry Smith -   v - the viewer
26047c6ae99SBarry Smith 
26147c6ae99SBarry Smith     Level: developer
26247c6ae99SBarry Smith 
26347c6ae99SBarry Smith .seealso DMDestroy(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
26447c6ae99SBarry Smith 
26547c6ae99SBarry Smith @*/
2667087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
26747c6ae99SBarry Smith {
26847c6ae99SBarry Smith   PetscErrorCode ierr;
26947c6ae99SBarry Smith 
27047c6ae99SBarry Smith   PetscFunctionBegin;
2713014e516SBarry Smith  if (!v) {
2723014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
2733014e516SBarry Smith   }
2740c010503SBarry Smith   if (dm->ops->view) {
2750c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
27647c6ae99SBarry Smith   }
27747c6ae99SBarry Smith   PetscFunctionReturn(0);
27847c6ae99SBarry Smith }
27947c6ae99SBarry Smith 
28047c6ae99SBarry Smith #undef __FUNCT__
28147c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
28247c6ae99SBarry Smith /*@
283aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
28447c6ae99SBarry Smith 
28547c6ae99SBarry Smith     Collective on DM
28647c6ae99SBarry Smith 
28747c6ae99SBarry Smith     Input Parameter:
28847c6ae99SBarry Smith .   dm - the DM object
28947c6ae99SBarry Smith 
29047c6ae99SBarry Smith     Output Parameter:
29147c6ae99SBarry Smith .   vec - the global vector
29247c6ae99SBarry Smith 
293073dac72SJed Brown     Level: beginner
29447c6ae99SBarry Smith 
29547c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
29647c6ae99SBarry Smith 
29747c6ae99SBarry Smith @*/
2987087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
29947c6ae99SBarry Smith {
30047c6ae99SBarry Smith   PetscErrorCode ierr;
30147c6ae99SBarry Smith 
30247c6ae99SBarry Smith   PetscFunctionBegin;
30347c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
30447c6ae99SBarry Smith   PetscFunctionReturn(0);
30547c6ae99SBarry Smith }
30647c6ae99SBarry Smith 
30747c6ae99SBarry Smith #undef __FUNCT__
30847c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
30947c6ae99SBarry Smith /*@
310aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
31147c6ae99SBarry Smith 
31247c6ae99SBarry Smith     Not Collective
31347c6ae99SBarry Smith 
31447c6ae99SBarry Smith     Input Parameter:
31547c6ae99SBarry Smith .   dm - the DM object
31647c6ae99SBarry Smith 
31747c6ae99SBarry Smith     Output Parameter:
31847c6ae99SBarry Smith .   vec - the local vector
31947c6ae99SBarry Smith 
320073dac72SJed Brown     Level: beginner
32147c6ae99SBarry Smith 
32247c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
32347c6ae99SBarry Smith 
32447c6ae99SBarry Smith @*/
3257087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
32647c6ae99SBarry Smith {
32747c6ae99SBarry Smith   PetscErrorCode ierr;
32847c6ae99SBarry Smith 
32947c6ae99SBarry Smith   PetscFunctionBegin;
33047c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
33147c6ae99SBarry Smith   PetscFunctionReturn(0);
33247c6ae99SBarry Smith }
33347c6ae99SBarry Smith 
33447c6ae99SBarry Smith #undef __FUNCT__
3351411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
3361411c6eeSJed Brown /*@
3371411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
3381411c6eeSJed Brown 
3391411c6eeSJed Brown    Collective on DM
3401411c6eeSJed Brown 
3411411c6eeSJed Brown    Input Parameter:
3421411c6eeSJed Brown .  dm - the DM that provides the mapping
3431411c6eeSJed Brown 
3441411c6eeSJed Brown    Output Parameter:
3451411c6eeSJed Brown .  ltog - the mapping
3461411c6eeSJed Brown 
3471411c6eeSJed Brown    Level: intermediate
3481411c6eeSJed Brown 
3491411c6eeSJed Brown    Notes:
3501411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
3511411c6eeSJed Brown    MatSetLocalToGlobalMapping().
3521411c6eeSJed Brown 
3531411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
3541411c6eeSJed Brown @*/
3557087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
3561411c6eeSJed Brown {
3571411c6eeSJed Brown   PetscErrorCode ierr;
3581411c6eeSJed Brown 
3591411c6eeSJed Brown   PetscFunctionBegin;
3601411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3611411c6eeSJed Brown   PetscValidPointer(ltog,2);
3621411c6eeSJed Brown   if (!dm->ltogmap) {
3631411c6eeSJed Brown     if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
3641411c6eeSJed Brown     ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
3651411c6eeSJed Brown   }
3661411c6eeSJed Brown   *ltog = dm->ltogmap;
3671411c6eeSJed Brown   PetscFunctionReturn(0);
3681411c6eeSJed Brown }
3691411c6eeSJed Brown 
3701411c6eeSJed Brown #undef __FUNCT__
3711411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
3721411c6eeSJed Brown /*@
3731411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
3741411c6eeSJed Brown 
3751411c6eeSJed Brown    Collective on DM
3761411c6eeSJed Brown 
3771411c6eeSJed Brown    Input Parameter:
3781411c6eeSJed Brown .  da - the distributed array that provides the mapping
3791411c6eeSJed Brown 
3801411c6eeSJed Brown    Output Parameter:
3811411c6eeSJed Brown .  ltog - the block mapping
3821411c6eeSJed Brown 
3831411c6eeSJed Brown    Level: intermediate
3841411c6eeSJed Brown 
3851411c6eeSJed Brown    Notes:
3861411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
3871411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
3881411c6eeSJed Brown 
3891411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
3901411c6eeSJed Brown @*/
3917087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
3921411c6eeSJed Brown {
3931411c6eeSJed Brown   PetscErrorCode ierr;
3941411c6eeSJed Brown 
3951411c6eeSJed Brown   PetscFunctionBegin;
3961411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3971411c6eeSJed Brown   PetscValidPointer(ltog,2);
3981411c6eeSJed Brown   if (!dm->ltogmapb) {
3991411c6eeSJed Brown     PetscInt bs;
4001411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
4011411c6eeSJed Brown     if (bs > 1) {
4021411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
4031411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
4041411c6eeSJed Brown     } else {
4051411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
4061411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
4071411c6eeSJed Brown     }
4081411c6eeSJed Brown   }
4091411c6eeSJed Brown   *ltog = dm->ltogmapb;
4101411c6eeSJed Brown   PetscFunctionReturn(0);
4111411c6eeSJed Brown }
4121411c6eeSJed Brown 
4131411c6eeSJed Brown #undef __FUNCT__
4141411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
4151411c6eeSJed Brown /*@
4161411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
4171411c6eeSJed Brown 
4181411c6eeSJed Brown    Not Collective
4191411c6eeSJed Brown 
4201411c6eeSJed Brown    Input Parameter:
4211411c6eeSJed Brown .  dm - the DM with block structure
4221411c6eeSJed Brown 
4231411c6eeSJed Brown    Output Parameter:
4241411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
4251411c6eeSJed Brown 
4261411c6eeSJed Brown    Level: intermediate
4271411c6eeSJed Brown 
4281411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
4291411c6eeSJed Brown @*/
4307087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
4311411c6eeSJed Brown {
4321411c6eeSJed Brown   PetscFunctionBegin;
4331411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4341411c6eeSJed Brown   PetscValidPointer(bs,2);
4351411c6eeSJed 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");
4361411c6eeSJed Brown   *bs = dm->bs;
4371411c6eeSJed Brown   PetscFunctionReturn(0);
4381411c6eeSJed Brown }
4391411c6eeSJed Brown 
4401411c6eeSJed Brown #undef __FUNCT__
44147c6ae99SBarry Smith #define __FUNCT__ "DMGetInterpolation"
44247c6ae99SBarry Smith /*@
443aa219208SBarry Smith     DMGetInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
44447c6ae99SBarry Smith 
44547c6ae99SBarry Smith     Collective on DM
44647c6ae99SBarry Smith 
44747c6ae99SBarry Smith     Input Parameter:
44847c6ae99SBarry Smith +   dm1 - the DM object
44947c6ae99SBarry Smith -   dm2 - the second, finer DM object
45047c6ae99SBarry Smith 
45147c6ae99SBarry Smith     Output Parameter:
45247c6ae99SBarry Smith +  mat - the interpolation
45347c6ae99SBarry Smith -  vec - the scaling (optional)
45447c6ae99SBarry Smith 
45547c6ae99SBarry Smith     Level: developer
45647c6ae99SBarry Smith 
45785afcc9aSBarry 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
45885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
459d52bd9f3SBarry Smith 
460d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
461d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
46285afcc9aSBarry Smith 
46385afcc9aSBarry Smith 
46485afcc9aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetColoring(), DMGetMatrix(), DMRefine(), DMCoarsen()
46547c6ae99SBarry Smith 
46647c6ae99SBarry Smith @*/
4677087cfbeSBarry Smith PetscErrorCode  DMGetInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
46847c6ae99SBarry Smith {
46947c6ae99SBarry Smith   PetscErrorCode ierr;
47047c6ae99SBarry Smith 
47147c6ae99SBarry Smith   PetscFunctionBegin;
47247c6ae99SBarry Smith   ierr = (*dm1->ops->getinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
47347c6ae99SBarry Smith   PetscFunctionReturn(0);
47447c6ae99SBarry Smith }
47547c6ae99SBarry Smith 
47647c6ae99SBarry Smith #undef __FUNCT__
47747c6ae99SBarry Smith #define __FUNCT__ "DMGetInjection"
47847c6ae99SBarry Smith /*@
479aa219208SBarry Smith     DMGetInjection - Gets injection matrix between two DMDA or DMComposite objects
48047c6ae99SBarry Smith 
48147c6ae99SBarry Smith     Collective on DM
48247c6ae99SBarry Smith 
48347c6ae99SBarry Smith     Input Parameter:
48447c6ae99SBarry Smith +   dm1 - the DM object
48547c6ae99SBarry Smith -   dm2 - the second, finer DM object
48647c6ae99SBarry Smith 
48747c6ae99SBarry Smith     Output Parameter:
48847c6ae99SBarry Smith .   ctx - the injection
48947c6ae99SBarry Smith 
49047c6ae99SBarry Smith     Level: developer
49147c6ae99SBarry Smith 
49285afcc9aSBarry 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
49385afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
49485afcc9aSBarry Smith 
49547c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetColoring(), DMGetMatrix(), DMGetInterpolation()
49647c6ae99SBarry Smith 
49747c6ae99SBarry Smith @*/
4987087cfbeSBarry Smith PetscErrorCode  DMGetInjection(DM dm1,DM dm2,VecScatter *ctx)
49947c6ae99SBarry Smith {
50047c6ae99SBarry Smith   PetscErrorCode ierr;
50147c6ae99SBarry Smith 
50247c6ae99SBarry Smith   PetscFunctionBegin;
50347c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
50447c6ae99SBarry Smith   PetscFunctionReturn(0);
50547c6ae99SBarry Smith }
50647c6ae99SBarry Smith 
50747c6ae99SBarry Smith #undef __FUNCT__
50847c6ae99SBarry Smith #define __FUNCT__ "DMGetColoring"
509d1e2c406SBarry Smith /*@C
510aa219208SBarry Smith     DMGetColoring - Gets coloring for a DMDA or DMComposite
51147c6ae99SBarry Smith 
51247c6ae99SBarry Smith     Collective on DM
51347c6ae99SBarry Smith 
51447c6ae99SBarry Smith     Input Parameter:
51547c6ae99SBarry Smith +   dm - the DM object
51647c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
51747c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
51847c6ae99SBarry Smith 
51947c6ae99SBarry Smith     Output Parameter:
52047c6ae99SBarry Smith .   coloring - the coloring
52147c6ae99SBarry Smith 
52247c6ae99SBarry Smith     Level: developer
52347c6ae99SBarry Smith 
52447c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetMatrix()
52547c6ae99SBarry Smith 
526aab9d709SJed Brown @*/
5277087cfbeSBarry Smith PetscErrorCode  DMGetColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
52847c6ae99SBarry Smith {
52947c6ae99SBarry Smith   PetscErrorCode ierr;
53047c6ae99SBarry Smith 
53147c6ae99SBarry Smith   PetscFunctionBegin;
53247c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
53347c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
53447c6ae99SBarry Smith   PetscFunctionReturn(0);
53547c6ae99SBarry Smith }
53647c6ae99SBarry Smith 
53747c6ae99SBarry Smith #undef __FUNCT__
53847c6ae99SBarry Smith #define __FUNCT__ "DMGetMatrix"
53947c6ae99SBarry Smith /*@C
540aa219208SBarry Smith     DMGetMatrix - Gets empty Jacobian for a DMDA or DMComposite
54147c6ae99SBarry Smith 
54247c6ae99SBarry Smith     Collective on DM
54347c6ae99SBarry Smith 
54447c6ae99SBarry Smith     Input Parameter:
54547c6ae99SBarry Smith +   dm - the DM object
54647c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
54794013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
54847c6ae99SBarry Smith 
54947c6ae99SBarry Smith     Output Parameter:
55047c6ae99SBarry Smith .   mat - the empty Jacobian
55147c6ae99SBarry Smith 
552073dac72SJed Brown     Level: beginner
55347c6ae99SBarry Smith 
55494013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
55594013140SBarry Smith        do not need to do it yourself.
55694013140SBarry Smith 
55794013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
558aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
55994013140SBarry Smith 
56094013140SBarry 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
56194013140SBarry Smith        internally by PETSc.
56294013140SBarry Smith 
56394013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
564aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
56594013140SBarry Smith 
566e00f7332SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
56747c6ae99SBarry Smith 
568aab9d709SJed Brown @*/
5697087cfbeSBarry Smith PetscErrorCode  DMGetMatrix(DM dm,const MatType mtype,Mat *mat)
57047c6ae99SBarry Smith {
57147c6ae99SBarry Smith   PetscErrorCode ierr;
57247c6ae99SBarry Smith 
57347c6ae99SBarry Smith   PetscFunctionBegin;
574235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
575235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
576235683edSBarry Smith #endif
577c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
578c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
579073dac72SJed Brown   if (dm->mattype) {
580073dac72SJed Brown     ierr = (*dm->ops->getmatrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
581073dac72SJed Brown   } else {
582073dac72SJed Brown     ierr = (*dm->ops->getmatrix)(dm,mtype,mat);CHKERRQ(ierr);
583c7b7c8a4SJed Brown   }
58447c6ae99SBarry Smith   PetscFunctionReturn(0);
58547c6ae99SBarry Smith }
58647c6ae99SBarry Smith 
58747c6ae99SBarry Smith #undef __FUNCT__
588732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
589732e2eb9SMatthew G Knepley /*@
590732e2eb9SMatthew G Knepley   DMSetMatrixPreallocateOnly - When DMGetMatrix() is called the matrix will be properly
591732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
592732e2eb9SMatthew G Knepley 
593732e2eb9SMatthew G Knepley   Logically Collective on DMDA
594732e2eb9SMatthew G Knepley 
595732e2eb9SMatthew G Knepley   Input Parameter:
596732e2eb9SMatthew G Knepley + dm - the DM
597732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
598732e2eb9SMatthew G Knepley 
599732e2eb9SMatthew G Knepley   Level: developer
600732e2eb9SMatthew G Knepley .seealso DMGetMatrix()
601732e2eb9SMatthew G Knepley @*/
602732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
603732e2eb9SMatthew G Knepley {
604732e2eb9SMatthew G Knepley   PetscFunctionBegin;
605732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
606732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
607732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
608732e2eb9SMatthew G Knepley }
609732e2eb9SMatthew G Knepley 
610732e2eb9SMatthew G Knepley #undef __FUNCT__
61147c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
61247c6ae99SBarry Smith /*@
61347c6ae99SBarry Smith     DMRefine - Refines a DM object
61447c6ae99SBarry Smith 
61547c6ae99SBarry Smith     Collective on DM
61647c6ae99SBarry Smith 
61747c6ae99SBarry Smith     Input Parameter:
61847c6ae99SBarry Smith +   dm - the DM object
61947c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
62047c6ae99SBarry Smith 
62147c6ae99SBarry Smith     Output Parameter:
62247c6ae99SBarry Smith .   dmf - the refined DM
62347c6ae99SBarry Smith 
62447c6ae99SBarry Smith     Level: developer
62547c6ae99SBarry Smith 
62647c6ae99SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
62747c6ae99SBarry Smith 
62847c6ae99SBarry Smith @*/
6297087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
63047c6ae99SBarry Smith {
63147c6ae99SBarry Smith   PetscErrorCode ierr;
63247c6ae99SBarry Smith 
63347c6ae99SBarry Smith   PetscFunctionBegin;
634732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63547c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
636644e2e5bSBarry Smith   (*dmf)->ops->initialguess = dm->ops->initialguess;
637644e2e5bSBarry Smith   (*dmf)->ops->function     = dm->ops->function;
638644e2e5bSBarry Smith   (*dmf)->ops->functionj    = dm->ops->functionj;
639644e2e5bSBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
640644e2e5bSBarry Smith     (*dmf)->ops->jacobian     = dm->ops->jacobian;
641644e2e5bSBarry Smith   }
642644e2e5bSBarry Smith   (*dmf)->ctx     = dm->ctx;
643656b349aSBarry Smith   (*dmf)->levelup = dm->levelup + 1;
64447c6ae99SBarry Smith   PetscFunctionReturn(0);
64547c6ae99SBarry Smith }
64647c6ae99SBarry Smith 
64747c6ae99SBarry Smith #undef __FUNCT__
648eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
649eb3f98d2SBarry Smith /*@
650eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
651eb3f98d2SBarry Smith 
652eb3f98d2SBarry Smith     Not Collective
653eb3f98d2SBarry Smith 
654eb3f98d2SBarry Smith     Input Parameter:
655eb3f98d2SBarry Smith .   dm - the DM object
656eb3f98d2SBarry Smith 
657eb3f98d2SBarry Smith     Output Parameter:
658eb3f98d2SBarry Smith .   level - number of refinements
659eb3f98d2SBarry Smith 
660eb3f98d2SBarry Smith     Level: developer
661eb3f98d2SBarry Smith 
662eb3f98d2SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
663eb3f98d2SBarry Smith 
664eb3f98d2SBarry Smith @*/
665eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
666eb3f98d2SBarry Smith {
667eb3f98d2SBarry Smith   PetscFunctionBegin;
668eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
669eb3f98d2SBarry Smith   *level = dm->levelup;
670eb3f98d2SBarry Smith   PetscFunctionReturn(0);
671eb3f98d2SBarry Smith }
672eb3f98d2SBarry Smith 
673eb3f98d2SBarry Smith #undef __FUNCT__
67447c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
67547c6ae99SBarry Smith /*@
67647c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
67747c6ae99SBarry Smith 
67847c6ae99SBarry Smith     Neighbor-wise Collective on DM
67947c6ae99SBarry Smith 
68047c6ae99SBarry Smith     Input Parameters:
68147c6ae99SBarry Smith +   dm - the DM object
68247c6ae99SBarry Smith .   g - the global vector
68347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
68447c6ae99SBarry Smith -   l - the local vector
68547c6ae99SBarry Smith 
68647c6ae99SBarry Smith 
68747c6ae99SBarry Smith     Level: beginner
68847c6ae99SBarry Smith 
6899a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
69047c6ae99SBarry Smith 
69147c6ae99SBarry Smith @*/
6927087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
69347c6ae99SBarry Smith {
69447c6ae99SBarry Smith   PetscErrorCode ierr;
69547c6ae99SBarry Smith 
69647c6ae99SBarry Smith   PetscFunctionBegin;
69747c6ae99SBarry Smith   ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode,l);CHKERRQ(ierr);
69847c6ae99SBarry Smith   PetscFunctionReturn(0);
69947c6ae99SBarry Smith }
70047c6ae99SBarry Smith 
70147c6ae99SBarry Smith #undef __FUNCT__
70247c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
70347c6ae99SBarry Smith /*@
70447c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
70547c6ae99SBarry Smith 
70647c6ae99SBarry Smith     Neighbor-wise Collective on DM
70747c6ae99SBarry Smith 
70847c6ae99SBarry Smith     Input Parameters:
70947c6ae99SBarry Smith +   dm - the DM object
71047c6ae99SBarry Smith .   g - the global vector
71147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
71247c6ae99SBarry Smith -   l - the local vector
71347c6ae99SBarry Smith 
71447c6ae99SBarry Smith 
71547c6ae99SBarry Smith     Level: beginner
71647c6ae99SBarry Smith 
7179a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
71847c6ae99SBarry Smith 
71947c6ae99SBarry Smith @*/
7207087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
72147c6ae99SBarry Smith {
72247c6ae99SBarry Smith   PetscErrorCode ierr;
72347c6ae99SBarry Smith 
72447c6ae99SBarry Smith   PetscFunctionBegin;
72547c6ae99SBarry Smith   ierr = (*dm->ops->globaltolocalend)(dm,g,mode,l);CHKERRQ(ierr);
72647c6ae99SBarry Smith   PetscFunctionReturn(0);
72747c6ae99SBarry Smith }
72847c6ae99SBarry Smith 
72947c6ae99SBarry Smith #undef __FUNCT__
7309a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
73147c6ae99SBarry Smith /*@
7329a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
7339a42bb27SBarry Smith 
7349a42bb27SBarry Smith     Neighbor-wise Collective on DM
7359a42bb27SBarry Smith 
7369a42bb27SBarry Smith     Input Parameters:
7379a42bb27SBarry Smith +   dm - the DM object
738f6813fd5SJed Brown .   l - the local vector
7399a42bb27SBarry 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
7409a42bb27SBarry Smith            base point.
741f6813fd5SJed Brown - - the global vector
7429a42bb27SBarry Smith 
7439a42bb27SBarry 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
7449a42bb27SBarry 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
7459a42bb27SBarry Smith            global array to the final global array with VecAXPY().
7469a42bb27SBarry Smith 
7479a42bb27SBarry Smith     Level: beginner
7489a42bb27SBarry Smith 
7499a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
7509a42bb27SBarry Smith 
7519a42bb27SBarry Smith @*/
7527087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
7539a42bb27SBarry Smith {
7549a42bb27SBarry Smith   PetscErrorCode ierr;
7559a42bb27SBarry Smith 
7569a42bb27SBarry Smith   PetscFunctionBegin;
757f6813fd5SJed Brown   ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode,g);CHKERRQ(ierr);
7589a42bb27SBarry Smith   PetscFunctionReturn(0);
7599a42bb27SBarry Smith }
7609a42bb27SBarry Smith 
7619a42bb27SBarry Smith #undef __FUNCT__
7629a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
7639a42bb27SBarry Smith /*@
7649a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
76547c6ae99SBarry Smith 
76647c6ae99SBarry Smith     Neighbor-wise Collective on DM
76747c6ae99SBarry Smith 
76847c6ae99SBarry Smith     Input Parameters:
76947c6ae99SBarry Smith +   dm - the DM object
770f6813fd5SJed Brown .   l - the local vector
77147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
772f6813fd5SJed Brown -   g - the global vector
77347c6ae99SBarry Smith 
77447c6ae99SBarry Smith 
77547c6ae99SBarry Smith     Level: beginner
77647c6ae99SBarry Smith 
7779a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
77847c6ae99SBarry Smith 
77947c6ae99SBarry Smith @*/
7807087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
78147c6ae99SBarry Smith {
78247c6ae99SBarry Smith   PetscErrorCode ierr;
78347c6ae99SBarry Smith 
78447c6ae99SBarry Smith   PetscFunctionBegin;
785f6813fd5SJed Brown   ierr = (*dm->ops->localtoglobalend)(dm,l,mode,g);CHKERRQ(ierr);
78647c6ae99SBarry Smith   PetscFunctionReturn(0);
78747c6ae99SBarry Smith }
78847c6ae99SBarry Smith 
78947c6ae99SBarry Smith #undef __FUNCT__
79047c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
79147c6ae99SBarry Smith /*@
79247c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
79347c6ae99SBarry Smith 
79447c6ae99SBarry Smith     Collective on DM
79547c6ae99SBarry Smith 
79647c6ae99SBarry Smith     Input Parameter:
79747c6ae99SBarry Smith +   dm - the DM object
79847c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
79947c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
80047c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
80147c6ae99SBarry Smith 
80247c6ae99SBarry Smith     Level: developer
80347c6ae99SBarry Smith 
8041b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
80547c6ae99SBarry Smith          DMSetFunction()
80647c6ae99SBarry Smith 
80747c6ae99SBarry Smith @*/
8087087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
80947c6ae99SBarry Smith {
81047c6ae99SBarry Smith   PetscErrorCode ierr;
81147c6ae99SBarry Smith   PetscFunctionBegin;
81247c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
81347c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
81447c6ae99SBarry Smith   if (A != B) {
81547c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
81647c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
81747c6ae99SBarry Smith   }
81847c6ae99SBarry Smith   PetscFunctionReturn(0);
81947c6ae99SBarry Smith }
82047c6ae99SBarry Smith 
82147c6ae99SBarry Smith #undef __FUNCT__
82247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
82347c6ae99SBarry Smith /*@
82447c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
82547c6ae99SBarry Smith 
82647c6ae99SBarry Smith     Collective on DM
82747c6ae99SBarry Smith 
82847c6ae99SBarry Smith     Input Parameter:
82947c6ae99SBarry Smith +   dm - the DM object
83047c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
83147c6ae99SBarry Smith 
83247c6ae99SBarry Smith     Output Parameter:
83347c6ae99SBarry Smith .   dmc - the coarsened DM
83447c6ae99SBarry Smith 
83547c6ae99SBarry Smith     Level: developer
83647c6ae99SBarry Smith 
83747c6ae99SBarry Smith .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
83847c6ae99SBarry Smith 
83947c6ae99SBarry Smith @*/
8407087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
84147c6ae99SBarry Smith {
84247c6ae99SBarry Smith   PetscErrorCode ierr;
84347c6ae99SBarry Smith 
84447c6ae99SBarry Smith   PetscFunctionBegin;
84547c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
84647c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
84747c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
84847c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
84947c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
85047c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
85147c6ae99SBarry Smith   }
852644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
853656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
85447c6ae99SBarry Smith   PetscFunctionReturn(0);
85547c6ae99SBarry Smith }
85647c6ae99SBarry Smith 
85747c6ae99SBarry Smith #undef __FUNCT__
85847c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
85947c6ae99SBarry Smith /*@C
86047c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
86147c6ae99SBarry Smith 
86247c6ae99SBarry Smith     Collective on DM
86347c6ae99SBarry Smith 
86447c6ae99SBarry Smith     Input Parameter:
86547c6ae99SBarry Smith +   dm - the DM object
86647c6ae99SBarry Smith -   nlevels - the number of levels of refinement
86747c6ae99SBarry Smith 
86847c6ae99SBarry Smith     Output Parameter:
86947c6ae99SBarry Smith .   dmf - the refined DM hierarchy
87047c6ae99SBarry Smith 
87147c6ae99SBarry Smith     Level: developer
87247c6ae99SBarry Smith 
87347c6ae99SBarry Smith .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
87447c6ae99SBarry Smith 
87547c6ae99SBarry Smith @*/
8767087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
87747c6ae99SBarry Smith {
87847c6ae99SBarry Smith   PetscErrorCode ierr;
87947c6ae99SBarry Smith 
88047c6ae99SBarry Smith   PetscFunctionBegin;
88147c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
88247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
88347c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
88447c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
88547c6ae99SBarry Smith   } else if (dm->ops->refine) {
88647c6ae99SBarry Smith     PetscInt i;
88747c6ae99SBarry Smith 
88847c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
88947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
89047c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
89147c6ae99SBarry Smith     }
89247c6ae99SBarry Smith   } else {
89347c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
89447c6ae99SBarry Smith   }
89547c6ae99SBarry Smith   PetscFunctionReturn(0);
89647c6ae99SBarry Smith }
89747c6ae99SBarry Smith 
89847c6ae99SBarry Smith #undef __FUNCT__
89947c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
90047c6ae99SBarry Smith /*@C
90147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
90247c6ae99SBarry Smith 
90347c6ae99SBarry Smith     Collective on DM
90447c6ae99SBarry Smith 
90547c6ae99SBarry Smith     Input Parameter:
90647c6ae99SBarry Smith +   dm - the DM object
90747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
90847c6ae99SBarry Smith 
90947c6ae99SBarry Smith     Output Parameter:
91047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith     Level: developer
91347c6ae99SBarry Smith 
91447c6ae99SBarry Smith .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
91547c6ae99SBarry Smith 
91647c6ae99SBarry Smith @*/
9177087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
91847c6ae99SBarry Smith {
91947c6ae99SBarry Smith   PetscErrorCode ierr;
92047c6ae99SBarry Smith 
92147c6ae99SBarry Smith   PetscFunctionBegin;
92247c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
92347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
92447c6ae99SBarry Smith   PetscValidPointer(dmc,3);
92547c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
92647c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
92747c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
92847c6ae99SBarry Smith     PetscInt i;
92947c6ae99SBarry Smith 
93047c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
93147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
93247c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
93347c6ae99SBarry Smith     }
93447c6ae99SBarry Smith   } else {
93547c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
93647c6ae99SBarry Smith   }
93747c6ae99SBarry Smith   PetscFunctionReturn(0);
93847c6ae99SBarry Smith }
93947c6ae99SBarry Smith 
94047c6ae99SBarry Smith #undef __FUNCT__
94147c6ae99SBarry Smith #define __FUNCT__ "DMGetAggregates"
94247c6ae99SBarry Smith /*@
94347c6ae99SBarry Smith    DMGetAggregates - Gets the aggregates that map between
94447c6ae99SBarry Smith    grids associated with two DMs.
94547c6ae99SBarry Smith 
94647c6ae99SBarry Smith    Collective on DM
94747c6ae99SBarry Smith 
94847c6ae99SBarry Smith    Input Parameters:
94947c6ae99SBarry Smith +  dmc - the coarse grid DM
95047c6ae99SBarry Smith -  dmf - the fine grid DM
95147c6ae99SBarry Smith 
95247c6ae99SBarry Smith    Output Parameters:
95347c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
95447c6ae99SBarry Smith 
95547c6ae99SBarry Smith    Level: intermediate
95647c6ae99SBarry Smith 
95747c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
95847c6ae99SBarry Smith 
95947c6ae99SBarry Smith .seealso: DMRefine(), DMGetInjection(), DMGetInterpolation()
96047c6ae99SBarry Smith @*/
9617087cfbeSBarry Smith PetscErrorCode  DMGetAggregates(DM dmc, DM dmf, Mat *rest)
96247c6ae99SBarry Smith {
96347c6ae99SBarry Smith   PetscErrorCode ierr;
96447c6ae99SBarry Smith 
96547c6ae99SBarry Smith   PetscFunctionBegin;
96647c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
96747c6ae99SBarry Smith   PetscFunctionReturn(0);
96847c6ae99SBarry Smith }
96947c6ae99SBarry Smith 
97047c6ae99SBarry Smith #undef __FUNCT__
9711a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
9721a266240SBarry Smith /*@C
9731a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
9741a266240SBarry Smith 
9751a266240SBarry Smith     Not Collective
9761a266240SBarry Smith 
9771a266240SBarry Smith     Input Parameters:
9781a266240SBarry Smith +   dm - the DM object
9791a266240SBarry Smith -   destroy - the destroy function
9801a266240SBarry Smith 
9811a266240SBarry Smith     Level: intermediate
9821a266240SBarry Smith 
9831a266240SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext()
9841a266240SBarry Smith 
985*f07f9ceaSJed Brown @*/
9861a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
9871a266240SBarry Smith {
9881a266240SBarry Smith   PetscFunctionBegin;
9891a266240SBarry Smith   dm->ctxdestroy = destroy;
9901a266240SBarry Smith   PetscFunctionReturn(0);
9911a266240SBarry Smith }
9921a266240SBarry Smith 
9931a266240SBarry Smith #undef __FUNCT__
9941b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
995b07ff414SBarry Smith /*@
9961b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
99747c6ae99SBarry Smith 
99847c6ae99SBarry Smith     Not Collective
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith     Input Parameters:
100147c6ae99SBarry Smith +   dm - the DM object
100247c6ae99SBarry Smith -   ctx - the user context
100347c6ae99SBarry Smith 
100447c6ae99SBarry Smith     Level: intermediate
100547c6ae99SBarry Smith 
10061b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext()
100747c6ae99SBarry Smith 
100847c6ae99SBarry Smith @*/
10091b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
101047c6ae99SBarry Smith {
101147c6ae99SBarry Smith   PetscFunctionBegin;
101247c6ae99SBarry Smith   dm->ctx = ctx;
101347c6ae99SBarry Smith   PetscFunctionReturn(0);
101447c6ae99SBarry Smith }
101547c6ae99SBarry Smith 
101647c6ae99SBarry Smith #undef __FUNCT__
10171b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
101847c6ae99SBarry Smith /*@
10191b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
102047c6ae99SBarry Smith 
102147c6ae99SBarry Smith     Not Collective
102247c6ae99SBarry Smith 
102347c6ae99SBarry Smith     Input Parameter:
102447c6ae99SBarry Smith .   dm - the DM object
102547c6ae99SBarry Smith 
102647c6ae99SBarry Smith     Output Parameter:
102747c6ae99SBarry Smith .   ctx - the user context
102847c6ae99SBarry Smith 
102947c6ae99SBarry Smith     Level: intermediate
103047c6ae99SBarry Smith 
10311b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext()
103247c6ae99SBarry Smith 
103347c6ae99SBarry Smith @*/
10341b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
103547c6ae99SBarry Smith {
103647c6ae99SBarry Smith   PetscFunctionBegin;
10371b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
103847c6ae99SBarry Smith   PetscFunctionReturn(0);
103947c6ae99SBarry Smith }
104047c6ae99SBarry Smith 
104147c6ae99SBarry Smith #undef __FUNCT__
104247c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
10437e833e3aSBarry Smith /*@C
104447c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
104547c6ae99SBarry Smith 
104647c6ae99SBarry Smith     Logically Collective on DM
104747c6ae99SBarry Smith 
104847c6ae99SBarry Smith     Input Parameter:
104947c6ae99SBarry Smith +   dm - the DM object to destroy
105047c6ae99SBarry Smith -   f - the function to compute the initial guess
105147c6ae99SBarry Smith 
105247c6ae99SBarry Smith     Level: intermediate
105347c6ae99SBarry Smith 
10541b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
105547c6ae99SBarry Smith 
1056*f07f9ceaSJed Brown @*/
10577087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
105847c6ae99SBarry Smith {
105947c6ae99SBarry Smith   PetscFunctionBegin;
106047c6ae99SBarry Smith   dm->ops->initialguess = f;
106147c6ae99SBarry Smith   PetscFunctionReturn(0);
106247c6ae99SBarry Smith }
106347c6ae99SBarry Smith 
106447c6ae99SBarry Smith #undef __FUNCT__
106547c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
10667e833e3aSBarry Smith /*@C
106747c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
106847c6ae99SBarry Smith 
106947c6ae99SBarry Smith     Logically Collective on DM
107047c6ae99SBarry Smith 
107147c6ae99SBarry Smith     Input Parameter:
107247c6ae99SBarry Smith +   dm - the DM object
107347c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
107447c6ae99SBarry Smith 
107547c6ae99SBarry Smith     Level: intermediate
107647c6ae99SBarry Smith 
107747c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
107847c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
107947c6ae99SBarry Smith 
10801b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
108147c6ae99SBarry Smith          DMSetJacobian()
108247c6ae99SBarry Smith 
1083*f07f9ceaSJed Brown @*/
10847087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
108547c6ae99SBarry Smith {
108647c6ae99SBarry Smith   PetscFunctionBegin;
108747c6ae99SBarry Smith   dm->ops->function = f;
108847c6ae99SBarry Smith   if (f) {
108947c6ae99SBarry Smith     dm->ops->functionj = f;
109047c6ae99SBarry Smith   }
109147c6ae99SBarry Smith   PetscFunctionReturn(0);
109247c6ae99SBarry Smith }
109347c6ae99SBarry Smith 
109447c6ae99SBarry Smith #undef __FUNCT__
109547c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
10967e833e3aSBarry Smith /*@C
109747c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
109847c6ae99SBarry Smith 
109947c6ae99SBarry Smith     Logically Collective on DM
110047c6ae99SBarry Smith 
110147c6ae99SBarry Smith     Input Parameter:
110247c6ae99SBarry Smith +   dm - the DM object to destroy
110347c6ae99SBarry Smith -   f - the function to compute the matrix entries
110447c6ae99SBarry Smith 
110547c6ae99SBarry Smith     Level: intermediate
110647c6ae99SBarry Smith 
11071b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
110847c6ae99SBarry Smith          DMSetFunction()
110947c6ae99SBarry Smith 
1110*f07f9ceaSJed Brown @*/
11117087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
111247c6ae99SBarry Smith {
111347c6ae99SBarry Smith   PetscFunctionBegin;
111447c6ae99SBarry Smith   dm->ops->jacobian = f;
111547c6ae99SBarry Smith   PetscFunctionReturn(0);
111647c6ae99SBarry Smith }
111747c6ae99SBarry Smith 
111847c6ae99SBarry Smith #undef __FUNCT__
111947c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
112047c6ae99SBarry Smith /*@
112147c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
112247c6ae99SBarry Smith 
112347c6ae99SBarry Smith     Collective on DM
112447c6ae99SBarry Smith 
112547c6ae99SBarry Smith     Input Parameter:
112647c6ae99SBarry Smith +   dm - the DM object to destroy
112747c6ae99SBarry Smith -   x - the vector to hold the initial guess values
112847c6ae99SBarry Smith 
112947c6ae99SBarry Smith     Level: developer
113047c6ae99SBarry Smith 
11311b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
113247c6ae99SBarry Smith 
113347c6ae99SBarry Smith @*/
11347087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
113547c6ae99SBarry Smith {
113647c6ae99SBarry Smith   PetscErrorCode ierr;
113747c6ae99SBarry Smith   PetscFunctionBegin;
113847c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
113947c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
114047c6ae99SBarry Smith   PetscFunctionReturn(0);
114147c6ae99SBarry Smith }
114247c6ae99SBarry Smith 
114347c6ae99SBarry Smith #undef __FUNCT__
114447c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
114547c6ae99SBarry Smith /*@
114647c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
114747c6ae99SBarry Smith 
114847c6ae99SBarry Smith     Not Collective
114947c6ae99SBarry Smith 
115047c6ae99SBarry Smith     Input Parameter:
115147c6ae99SBarry Smith .   dm - the DM object to destroy
115247c6ae99SBarry Smith 
115347c6ae99SBarry Smith     Output Parameter:
115447c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
115547c6ae99SBarry Smith 
115647c6ae99SBarry Smith     Level: developer
115747c6ae99SBarry Smith 
11581b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
115947c6ae99SBarry Smith 
116047c6ae99SBarry Smith @*/
11617087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
116247c6ae99SBarry Smith {
116347c6ae99SBarry Smith   PetscFunctionBegin;
116447c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
116547c6ae99SBarry Smith   PetscFunctionReturn(0);
116647c6ae99SBarry Smith }
116747c6ae99SBarry Smith 
116847c6ae99SBarry Smith #undef __FUNCT__
116947c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
117047c6ae99SBarry Smith /*@
117147c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
117247c6ae99SBarry Smith 
117347c6ae99SBarry Smith     Not Collective
117447c6ae99SBarry Smith 
117547c6ae99SBarry Smith     Input Parameter:
117647c6ae99SBarry Smith .   dm - the DM object to destroy
117747c6ae99SBarry Smith 
117847c6ae99SBarry Smith     Output Parameter:
117947c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
118047c6ae99SBarry Smith 
118147c6ae99SBarry Smith     Level: developer
118247c6ae99SBarry Smith 
11831b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
118447c6ae99SBarry Smith 
118547c6ae99SBarry Smith @*/
11867087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
118747c6ae99SBarry Smith {
118847c6ae99SBarry Smith   PetscFunctionBegin;
118947c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
119047c6ae99SBarry Smith   PetscFunctionReturn(0);
119147c6ae99SBarry Smith }
119247c6ae99SBarry Smith 
119347c6ae99SBarry Smith #undef __FUNCT__
119447c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
119547c6ae99SBarry Smith /*@
119647c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
119747c6ae99SBarry Smith 
119847c6ae99SBarry Smith     Not Collective
119947c6ae99SBarry Smith 
120047c6ae99SBarry Smith     Input Parameter:
120147c6ae99SBarry Smith .   dm - the DM object to destroy
120247c6ae99SBarry Smith 
120347c6ae99SBarry Smith     Output Parameter:
120447c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
120547c6ae99SBarry Smith 
120647c6ae99SBarry Smith     Level: developer
120747c6ae99SBarry Smith 
12081b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
120947c6ae99SBarry Smith 
121047c6ae99SBarry Smith @*/
12117087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
121247c6ae99SBarry Smith {
121347c6ae99SBarry Smith   PetscFunctionBegin;
121447c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
121547c6ae99SBarry Smith   PetscFunctionReturn(0);
121647c6ae99SBarry Smith }
121747c6ae99SBarry Smith 
121847c6ae99SBarry Smith #undef __FUNCT__
121947c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
122047c6ae99SBarry Smith /*@
122147c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
122247c6ae99SBarry Smith 
122347c6ae99SBarry Smith     Collective on DM
122447c6ae99SBarry Smith 
122547c6ae99SBarry Smith     Input Parameter:
122647c6ae99SBarry Smith +   dm - the DM object to destroy
122747c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
122847c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
122947c6ae99SBarry Smith 
123047c6ae99SBarry Smith     Level: developer
123147c6ae99SBarry Smith 
12321b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
123347c6ae99SBarry Smith          DMSetJacobian()
123447c6ae99SBarry Smith 
123547c6ae99SBarry Smith @*/
12367087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
123747c6ae99SBarry Smith {
123847c6ae99SBarry Smith   PetscErrorCode ierr;
123947c6ae99SBarry Smith   PetscFunctionBegin;
124047c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
1241644e2e5bSBarry Smith   PetscStackPush("DM user function");
124247c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
1243644e2e5bSBarry Smith   PetscStackPop;
124447c6ae99SBarry Smith   PetscFunctionReturn(0);
124547c6ae99SBarry Smith }
124647c6ae99SBarry Smith 
124747c6ae99SBarry Smith 
124847c6ae99SBarry Smith #undef __FUNCT__
124947c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
125047c6ae99SBarry Smith /*@
125147c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
125247c6ae99SBarry Smith 
125347c6ae99SBarry Smith     Collective on DM
125447c6ae99SBarry Smith 
125547c6ae99SBarry Smith     Input Parameter:
125647c6ae99SBarry Smith +   dm - the DM object
1257cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
125847c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
125947c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
126047c6ae99SBarry Smith 
126147c6ae99SBarry Smith     Level: developer
126247c6ae99SBarry Smith 
12631b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
126447c6ae99SBarry Smith          DMSetFunction()
126547c6ae99SBarry Smith 
126647c6ae99SBarry Smith @*/
12677087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
126847c6ae99SBarry Smith {
126947c6ae99SBarry Smith   PetscErrorCode ierr;
127047c6ae99SBarry Smith 
127147c6ae99SBarry Smith   PetscFunctionBegin;
127247c6ae99SBarry Smith   if (!dm->ops->jacobian) {
127347c6ae99SBarry Smith     ISColoring     coloring;
127447c6ae99SBarry Smith     MatFDColoring  fd;
127547c6ae99SBarry Smith 
12762533e041SBarry Smith     ierr = DMGetColoring(dm,IS_COLORING_GLOBAL,MATAIJ,&coloring);CHKERRQ(ierr);
127747c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
1278fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
127947c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
12800bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
12810bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
128271cd77b2SBarry Smith 
128347c6ae99SBarry Smith     dm->fd = fd;
128447c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
12852533e041SBarry Smith 
128671cd77b2SBarry Smith     /* don't know why this is needed */
128771cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
128847c6ae99SBarry Smith   }
128947c6ae99SBarry Smith   if (!x) x = dm->x;
129047c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
1291cab2e9ccSBarry Smith 
129271cd77b2SBarry 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 */
1293649052a6SBarry Smith   if (x) {
1294cab2e9ccSBarry Smith     if (!dm->x) {
129571cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
1296cab2e9ccSBarry Smith     }
1297cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
1298649052a6SBarry Smith   }
1299a8248277SBarry Smith   if (A != B) {
1300a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1301a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1302a8248277SBarry Smith   }
130347c6ae99SBarry Smith   PetscFunctionReturn(0);
130447c6ae99SBarry Smith }
1305264ace61SBarry Smith 
1306cab2e9ccSBarry Smith 
1307264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
1308264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
1309264ace61SBarry Smith 
1310264ace61SBarry Smith #undef __FUNCT__
1311264ace61SBarry Smith #define __FUNCT__ "DMSetType"
1312264ace61SBarry Smith /*@C
1313264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
1314264ace61SBarry Smith 
1315264ace61SBarry Smith   Collective on DM
1316264ace61SBarry Smith 
1317264ace61SBarry Smith   Input Parameters:
1318264ace61SBarry Smith + dm     - The DM object
1319264ace61SBarry Smith - method - The name of the DM type
1320264ace61SBarry Smith 
1321264ace61SBarry Smith   Options Database Key:
1322264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
1323264ace61SBarry Smith 
1324264ace61SBarry Smith   Notes:
1325e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
1326264ace61SBarry Smith 
1327264ace61SBarry Smith   Level: intermediate
1328264ace61SBarry Smith 
1329264ace61SBarry Smith .keywords: DM, set, type
1330264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
1331264ace61SBarry Smith @*/
13327087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
1333264ace61SBarry Smith {
1334264ace61SBarry Smith   PetscErrorCode (*r)(DM);
1335264ace61SBarry Smith   PetscBool      match;
1336264ace61SBarry Smith   PetscErrorCode ierr;
1337264ace61SBarry Smith 
1338264ace61SBarry Smith   PetscFunctionBegin;
1339264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1340264ace61SBarry Smith   ierr = PetscTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
1341264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
1342264ace61SBarry Smith 
1343264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
13444b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
1345264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
1346264ace61SBarry Smith 
1347264ace61SBarry Smith   if (dm->ops->destroy) {
1348264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
1349264ace61SBarry Smith   }
1350264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
1351264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
1352264ace61SBarry Smith   PetscFunctionReturn(0);
1353264ace61SBarry Smith }
1354264ace61SBarry Smith 
1355264ace61SBarry Smith #undef __FUNCT__
1356264ace61SBarry Smith #define __FUNCT__ "DMGetType"
1357264ace61SBarry Smith /*@C
1358264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
1359264ace61SBarry Smith 
1360264ace61SBarry Smith   Not Collective
1361264ace61SBarry Smith 
1362264ace61SBarry Smith   Input Parameter:
1363264ace61SBarry Smith . dm  - The DM
1364264ace61SBarry Smith 
1365264ace61SBarry Smith   Output Parameter:
1366264ace61SBarry Smith . type - The DM type name
1367264ace61SBarry Smith 
1368264ace61SBarry Smith   Level: intermediate
1369264ace61SBarry Smith 
1370264ace61SBarry Smith .keywords: DM, get, type, name
1371264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
1372264ace61SBarry Smith @*/
13737087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
1374264ace61SBarry Smith {
1375264ace61SBarry Smith   PetscErrorCode ierr;
1376264ace61SBarry Smith 
1377264ace61SBarry Smith   PetscFunctionBegin;
1378264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1379264ace61SBarry Smith   PetscValidCharPointer(type,2);
1380264ace61SBarry Smith   if (!DMRegisterAllCalled) {
1381264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
1382264ace61SBarry Smith   }
1383264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
1384264ace61SBarry Smith   PetscFunctionReturn(0);
1385264ace61SBarry Smith }
1386264ace61SBarry Smith 
138767a56275SMatthew G Knepley #undef __FUNCT__
138867a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
138967a56275SMatthew G Knepley /*@C
139067a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
139167a56275SMatthew G Knepley 
139267a56275SMatthew G Knepley   Collective on DM
139367a56275SMatthew G Knepley 
139467a56275SMatthew G Knepley   Input Parameters:
139567a56275SMatthew G Knepley + dm - the DM
139667a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
139767a56275SMatthew G Knepley 
139867a56275SMatthew G Knepley   Output Parameter:
139967a56275SMatthew G Knepley . M - pointer to new DM
140067a56275SMatthew G Knepley 
140167a56275SMatthew G Knepley   Notes:
140267a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
140367a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
140467a56275SMatthew G Knepley   of the input DM.
140567a56275SMatthew G Knepley 
140667a56275SMatthew G Knepley   Level: intermediate
140767a56275SMatthew G Knepley 
140867a56275SMatthew G Knepley .seealso: DMCreate()
140967a56275SMatthew G Knepley @*/
141067a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
141167a56275SMatthew G Knepley {
141267a56275SMatthew G Knepley   DM             B;
141367a56275SMatthew G Knepley   char           convname[256];
141467a56275SMatthew G Knepley   PetscBool      sametype, issame;
141567a56275SMatthew G Knepley   PetscErrorCode ierr;
141667a56275SMatthew G Knepley 
141767a56275SMatthew G Knepley   PetscFunctionBegin;
141867a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
141967a56275SMatthew G Knepley   PetscValidType(dm,1);
142067a56275SMatthew G Knepley   PetscValidPointer(M,3);
142167a56275SMatthew G Knepley   ierr = PetscTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
142267a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
142367a56275SMatthew G Knepley   {
142467a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
142567a56275SMatthew G Knepley 
142667a56275SMatthew G Knepley     /*
142767a56275SMatthew G Knepley        Order of precedence:
142867a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
142967a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
143067a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
143167a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
143267a56275SMatthew G Knepley        5) Use a really basic converter.
143367a56275SMatthew G Knepley     */
143467a56275SMatthew G Knepley 
143567a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
143667a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
143767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
143867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
143967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
144067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
144167a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
144267a56275SMatthew G Knepley     if (conv) goto foundconv;
144367a56275SMatthew G Knepley 
144467a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
144567a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
144667a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
144767a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
144867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
144967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
145067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
145167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
145267a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
145367a56275SMatthew G Knepley     if (conv) {
1454fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
145567a56275SMatthew G Knepley       goto foundconv;
145667a56275SMatthew G Knepley     }
145767a56275SMatthew G Knepley 
145867a56275SMatthew G Knepley #if 0
145967a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
146067a56275SMatthew G Knepley     conv = B->ops->convertfrom;
1461fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
146267a56275SMatthew G Knepley     if (conv) goto foundconv;
146367a56275SMatthew G Knepley 
146467a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
146567a56275SMatthew G Knepley     if (dm->ops->convert) {
146667a56275SMatthew G Knepley       conv = dm->ops->convert;
146767a56275SMatthew G Knepley     }
146867a56275SMatthew G Knepley     if (conv) goto foundconv;
146967a56275SMatthew G Knepley #endif
147067a56275SMatthew G Knepley 
147167a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
147267a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
147367a56275SMatthew G Knepley 
147467a56275SMatthew G Knepley     foundconv:
147567a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
147667a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
147767a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
147867a56275SMatthew G Knepley   }
147967a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
148067a56275SMatthew G Knepley   PetscFunctionReturn(0);
148167a56275SMatthew G Knepley }
1482264ace61SBarry Smith 
1483264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1484264ace61SBarry Smith 
1485264ace61SBarry Smith #undef __FUNCT__
1486264ace61SBarry Smith #define __FUNCT__ "DMRegister"
1487264ace61SBarry Smith /*@C
1488264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
1489264ace61SBarry Smith 
1490264ace61SBarry Smith   Level: advanced
1491264ace61SBarry Smith @*/
14927087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
1493264ace61SBarry Smith {
1494264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
1495264ace61SBarry Smith   PetscErrorCode ierr;
1496264ace61SBarry Smith 
1497264ace61SBarry Smith   PetscFunctionBegin;
1498264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
1499264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
1500264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
1501264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
1502264ace61SBarry Smith   PetscFunctionReturn(0);
1503264ace61SBarry Smith }
1504264ace61SBarry Smith 
1505264ace61SBarry Smith 
1506264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1507264ace61SBarry Smith #undef __FUNCT__
1508264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
1509264ace61SBarry Smith /*@C
1510264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
1511264ace61SBarry Smith 
1512264ace61SBarry Smith    Not Collective
1513264ace61SBarry Smith 
1514264ace61SBarry Smith    Level: advanced
1515264ace61SBarry Smith 
1516264ace61SBarry Smith .keywords: DM, register, destroy
1517264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
1518264ace61SBarry Smith @*/
15197087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
1520264ace61SBarry Smith {
1521264ace61SBarry Smith   PetscErrorCode ierr;
1522264ace61SBarry Smith 
1523264ace61SBarry Smith   PetscFunctionBegin;
1524264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
1525264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
1526264ace61SBarry Smith   PetscFunctionReturn(0);
1527264ace61SBarry Smith }
152823f975d1SBarry Smith 
152923f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
1530c6db04a5SJed Brown #include <mex.h>
153123f975d1SBarry Smith 
15323014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
153323f975d1SBarry Smith 
153423f975d1SBarry Smith #undef __FUNCT__
153523f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
153623f975d1SBarry Smith /*
153723f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
153823f975d1SBarry Smith                          DMSetFunctionMatlab().
153923f975d1SBarry Smith 
154023f975d1SBarry Smith    For linear problems x is null
154123f975d1SBarry Smith 
154223f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
154323f975d1SBarry Smith */
15447087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
154523f975d1SBarry Smith {
154623f975d1SBarry Smith   PetscErrorCode    ierr;
154723f975d1SBarry Smith   DMMatlabContext   *sctx;
154823f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
154923f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
155023f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
155123f975d1SBarry Smith 
155223f975d1SBarry Smith   PetscFunctionBegin;
155323f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
155423f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
155523f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
155623f975d1SBarry Smith 
155723f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
15581b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
155923f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
156023f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
15613014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
156223f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
156323f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
156423f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
156523f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
1566b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
156723f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
156823f975d1SBarry Smith   mxDestroyArray(prhs[0]);
156923f975d1SBarry Smith   mxDestroyArray(prhs[1]);
157023f975d1SBarry Smith   mxDestroyArray(prhs[2]);
157123f975d1SBarry Smith   mxDestroyArray(prhs[3]);
157223f975d1SBarry Smith   mxDestroyArray(plhs[0]);
157323f975d1SBarry Smith   PetscFunctionReturn(0);
157423f975d1SBarry Smith }
157523f975d1SBarry Smith 
157623f975d1SBarry Smith 
157723f975d1SBarry Smith #undef __FUNCT__
157823f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
157923f975d1SBarry Smith /*
158023f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
158123f975d1SBarry Smith 
158223f975d1SBarry Smith */
15837087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
158423f975d1SBarry Smith {
158523f975d1SBarry Smith   PetscErrorCode    ierr;
158623f975d1SBarry Smith   DMMatlabContext   *sctx;
158723f975d1SBarry Smith 
158823f975d1SBarry Smith   PetscFunctionBegin;
158923f975d1SBarry Smith   /* currently sctx is memory bleed */
15901b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
15913014e516SBarry Smith   if (!sctx) {
159223f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
15933014e516SBarry Smith   }
159423f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
15951b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
159623f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
159723f975d1SBarry Smith   PetscFunctionReturn(0);
159823f975d1SBarry Smith }
15993014e516SBarry Smith 
16003014e516SBarry Smith #undef __FUNCT__
16013014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
16023014e516SBarry Smith /*
16033014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
16043014e516SBarry Smith                          DMSetJacobianMatlab().
16053014e516SBarry Smith 
16063014e516SBarry Smith    For linear problems x is null
16073014e516SBarry Smith 
16083014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
16093014e516SBarry Smith */
16107087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
16113014e516SBarry Smith {
16123014e516SBarry Smith   PetscErrorCode    ierr;
16133014e516SBarry Smith   DMMatlabContext   *sctx;
16143014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
16153014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
16163014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
16173014e516SBarry Smith 
16183014e516SBarry Smith   PetscFunctionBegin;
16193014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16203014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
16213014e516SBarry Smith 
1622e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
16231b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
16243014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
16253014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
16263014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
16273014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
16283014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
16293014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
16303014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
16313014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
16323014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
1633b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
1634c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
1635c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
16363014e516SBarry Smith   mxDestroyArray(prhs[0]);
16373014e516SBarry Smith   mxDestroyArray(prhs[1]);
16383014e516SBarry Smith   mxDestroyArray(prhs[2]);
16393014e516SBarry Smith   mxDestroyArray(prhs[3]);
16403014e516SBarry Smith   mxDestroyArray(prhs[4]);
16413014e516SBarry Smith   mxDestroyArray(plhs[0]);
16423014e516SBarry Smith   mxDestroyArray(plhs[1]);
16433014e516SBarry Smith   PetscFunctionReturn(0);
16443014e516SBarry Smith }
16453014e516SBarry Smith 
16463014e516SBarry Smith 
16473014e516SBarry Smith #undef __FUNCT__
16483014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
16493014e516SBarry Smith /*
16503014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
16513014e516SBarry Smith 
16523014e516SBarry Smith */
16537087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
16543014e516SBarry Smith {
16553014e516SBarry Smith   PetscErrorCode    ierr;
16563014e516SBarry Smith   DMMatlabContext   *sctx;
16573014e516SBarry Smith 
16583014e516SBarry Smith   PetscFunctionBegin;
16593014e516SBarry Smith   /* currently sctx is memory bleed */
16601b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
16613014e516SBarry Smith   if (!sctx) {
16623014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
16633014e516SBarry Smith   }
16643014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
16651b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
16663014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
16673014e516SBarry Smith   PetscFunctionReturn(0);
16683014e516SBarry Smith }
166923f975d1SBarry Smith #endif
1670b859378eSBarry Smith 
1671b859378eSBarry Smith #undef __FUNCT__
1672b859378eSBarry Smith #define __FUNCT__ "DMLoad"
1673b859378eSBarry Smith /*@C
1674b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
1675b859378eSBarry Smith   with DMView().
1676b859378eSBarry Smith 
1677b859378eSBarry Smith   Collective on PetscViewer
1678b859378eSBarry Smith 
1679b859378eSBarry Smith   Input Parameters:
1680b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
1681b859378eSBarry Smith            some related function before a call to DMLoad().
1682b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
1683b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
1684b859378eSBarry Smith 
1685b859378eSBarry Smith    Level: intermediate
1686b859378eSBarry Smith 
1687b859378eSBarry Smith   Notes:
1688b859378eSBarry Smith   Defaults to the DM DA.
1689b859378eSBarry Smith 
1690b859378eSBarry Smith   Notes for advanced users:
1691b859378eSBarry Smith   Most users should not need to know the details of the binary storage
1692b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
1693b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
1694b859378eSBarry Smith   format is
1695b859378eSBarry Smith .vb
1696b859378eSBarry Smith      has not yet been determined
1697b859378eSBarry Smith .ve
1698b859378eSBarry Smith 
1699b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
1700b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
1701b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
1702b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
1703b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
1704b859378eSBarry Smith 
1705b859378eSBarry Smith   Concepts: vector^loading from file
1706b859378eSBarry Smith 
1707b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
1708b859378eSBarry Smith @*/
1709b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
1710b859378eSBarry Smith {
1711b859378eSBarry Smith   PetscErrorCode ierr;
1712b859378eSBarry Smith 
1713b859378eSBarry Smith   PetscFunctionBegin;
1714b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
1715b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1716b859378eSBarry Smith 
1717b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
1718b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
1719b859378eSBarry Smith   }
1720b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
1721b859378eSBarry Smith   PetscFunctionReturn(0);
1722b859378eSBarry Smith }
1723b859378eSBarry Smith 
1724