xref: /petsc/src/dm/interface/dm.c (revision 8387afaaaaa6e58d1dd183609f0f268898497364)
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 
39a4121054SBarry Smith   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "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   }
15987e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
16071cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
1614dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
1626bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
1636bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
1646bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
165732e2eb9SMatthew G Knepley 
166732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
1676bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
168732e2eb9SMatthew G Knepley 
1696bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
1706bf464f9SBarry Smith   ierr = PetscFree((*dm)->data);CHKERRQ(ierr);
171732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
17247c6ae99SBarry Smith   PetscFunctionReturn(0);
17347c6ae99SBarry Smith }
17447c6ae99SBarry Smith 
17547c6ae99SBarry Smith #undef __FUNCT__
176d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
177d7bf68aeSBarry Smith /*@
178d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
179d7bf68aeSBarry Smith 
180d7bf68aeSBarry Smith     Collective on DM
181d7bf68aeSBarry Smith 
182d7bf68aeSBarry Smith     Input Parameter:
183d7bf68aeSBarry Smith .   dm - the DM object to setup
184d7bf68aeSBarry Smith 
185d7bf68aeSBarry Smith     Level: developer
186d7bf68aeSBarry Smith 
187d7bf68aeSBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
188d7bf68aeSBarry Smith 
189d7bf68aeSBarry Smith @*/
1907087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
191d7bf68aeSBarry Smith {
192d7bf68aeSBarry Smith   PetscErrorCode ierr;
193d7bf68aeSBarry Smith 
194d7bf68aeSBarry Smith   PetscFunctionBegin;
195*8387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
196d7bf68aeSBarry Smith   if (dm->ops->setup) {
197d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
198d7bf68aeSBarry Smith   }
199*8387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
200d7bf68aeSBarry Smith   PetscFunctionReturn(0);
201d7bf68aeSBarry Smith }
202d7bf68aeSBarry Smith 
203d7bf68aeSBarry Smith #undef __FUNCT__
204d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
205d7bf68aeSBarry Smith /*@
206d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
207d7bf68aeSBarry Smith 
208d7bf68aeSBarry Smith     Collective on DM
209d7bf68aeSBarry Smith 
210d7bf68aeSBarry Smith     Input Parameter:
211d7bf68aeSBarry Smith .   dm - the DM object to set options for
212d7bf68aeSBarry Smith 
213732e2eb9SMatthew G Knepley     Options Database:
214732e2eb9SMatthew G Knepley .   -dm_preallocate_only: Only preallocate the matrix for DMGetMatrix(), but do not fill it with zeros
215732e2eb9SMatthew G Knepley 
216d7bf68aeSBarry Smith     Level: developer
217d7bf68aeSBarry Smith 
218d7bf68aeSBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
219d7bf68aeSBarry Smith 
220d7bf68aeSBarry Smith @*/
2217087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
222d7bf68aeSBarry Smith {
223d7bf68aeSBarry Smith   PetscErrorCode ierr;
224d7bf68aeSBarry Smith 
225d7bf68aeSBarry Smith   PetscFunctionBegin;
226732e2eb9SMatthew G Knepley   ierr = PetscOptionsGetBool(((PetscObject) dm)->prefix, "-dm_preallocate_only", &dm->prealloc_only, PETSC_NULL);CHKERRQ(ierr);
227d7bf68aeSBarry Smith   if (dm->ops->setfromoptions) {
228d7bf68aeSBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
229d7bf68aeSBarry Smith   }
230d7bf68aeSBarry Smith   PetscFunctionReturn(0);
231d7bf68aeSBarry Smith }
232d7bf68aeSBarry Smith 
233d7bf68aeSBarry Smith #undef __FUNCT__
23447c6ae99SBarry Smith #define __FUNCT__ "DMView"
235fc9bc008SSatish Balay /*@C
236aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
23747c6ae99SBarry Smith 
23847c6ae99SBarry Smith     Collective on DM
23947c6ae99SBarry Smith 
24047c6ae99SBarry Smith     Input Parameter:
24147c6ae99SBarry Smith +   dm - the DM object to view
24247c6ae99SBarry Smith -   v - the viewer
24347c6ae99SBarry Smith 
24447c6ae99SBarry Smith     Level: developer
24547c6ae99SBarry Smith 
24647c6ae99SBarry Smith .seealso DMDestroy(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
24747c6ae99SBarry Smith 
24847c6ae99SBarry Smith @*/
2497087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
25047c6ae99SBarry Smith {
25147c6ae99SBarry Smith   PetscErrorCode ierr;
25247c6ae99SBarry Smith 
25347c6ae99SBarry Smith   PetscFunctionBegin;
2543014e516SBarry Smith  if (!v) {
2553014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
2563014e516SBarry Smith   }
2570c010503SBarry Smith   if (dm->ops->view) {
2580c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
25947c6ae99SBarry Smith   }
26047c6ae99SBarry Smith   PetscFunctionReturn(0);
26147c6ae99SBarry Smith }
26247c6ae99SBarry Smith 
26347c6ae99SBarry Smith #undef __FUNCT__
26447c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
26547c6ae99SBarry Smith /*@
266aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
26747c6ae99SBarry Smith 
26847c6ae99SBarry Smith     Collective on DM
26947c6ae99SBarry Smith 
27047c6ae99SBarry Smith     Input Parameter:
27147c6ae99SBarry Smith .   dm - the DM object
27247c6ae99SBarry Smith 
27347c6ae99SBarry Smith     Output Parameter:
27447c6ae99SBarry Smith .   vec - the global vector
27547c6ae99SBarry Smith 
27647c6ae99SBarry Smith     Level: developer
27747c6ae99SBarry Smith 
27847c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
27947c6ae99SBarry Smith 
28047c6ae99SBarry Smith @*/
2817087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
28247c6ae99SBarry Smith {
28347c6ae99SBarry Smith   PetscErrorCode ierr;
28447c6ae99SBarry Smith 
28547c6ae99SBarry Smith   PetscFunctionBegin;
28647c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
28747c6ae99SBarry Smith   PetscFunctionReturn(0);
28847c6ae99SBarry Smith }
28947c6ae99SBarry Smith 
29047c6ae99SBarry Smith #undef __FUNCT__
29147c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
29247c6ae99SBarry Smith /*@
293aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
29447c6ae99SBarry Smith 
29547c6ae99SBarry Smith     Not Collective
29647c6ae99SBarry Smith 
29747c6ae99SBarry Smith     Input Parameter:
29847c6ae99SBarry Smith .   dm - the DM object
29947c6ae99SBarry Smith 
30047c6ae99SBarry Smith     Output Parameter:
30147c6ae99SBarry Smith .   vec - the local vector
30247c6ae99SBarry Smith 
30347c6ae99SBarry Smith     Level: developer
30447c6ae99SBarry Smith 
30547c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
30647c6ae99SBarry Smith 
30747c6ae99SBarry Smith @*/
3087087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
30947c6ae99SBarry Smith {
31047c6ae99SBarry Smith   PetscErrorCode ierr;
31147c6ae99SBarry Smith 
31247c6ae99SBarry Smith   PetscFunctionBegin;
31347c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
31447c6ae99SBarry Smith   PetscFunctionReturn(0);
31547c6ae99SBarry Smith }
31647c6ae99SBarry Smith 
31747c6ae99SBarry Smith #undef __FUNCT__
3181411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
3191411c6eeSJed Brown /*@
3201411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
3211411c6eeSJed Brown 
3221411c6eeSJed Brown    Collective on DM
3231411c6eeSJed Brown 
3241411c6eeSJed Brown    Input Parameter:
3251411c6eeSJed Brown .  dm - the DM that provides the mapping
3261411c6eeSJed Brown 
3271411c6eeSJed Brown    Output Parameter:
3281411c6eeSJed Brown .  ltog - the mapping
3291411c6eeSJed Brown 
3301411c6eeSJed Brown    Level: intermediate
3311411c6eeSJed Brown 
3321411c6eeSJed Brown    Notes:
3331411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
3341411c6eeSJed Brown    MatSetLocalToGlobalMapping().
3351411c6eeSJed Brown 
3361411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
3371411c6eeSJed Brown @*/
3387087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
3391411c6eeSJed Brown {
3401411c6eeSJed Brown   PetscErrorCode ierr;
3411411c6eeSJed Brown 
3421411c6eeSJed Brown   PetscFunctionBegin;
3431411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3441411c6eeSJed Brown   PetscValidPointer(ltog,2);
3451411c6eeSJed Brown   if (!dm->ltogmap) {
3461411c6eeSJed Brown     if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
3471411c6eeSJed Brown     ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
3481411c6eeSJed Brown   }
3491411c6eeSJed Brown   *ltog = dm->ltogmap;
3501411c6eeSJed Brown   PetscFunctionReturn(0);
3511411c6eeSJed Brown }
3521411c6eeSJed Brown 
3531411c6eeSJed Brown #undef __FUNCT__
3541411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
3551411c6eeSJed Brown /*@
3561411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
3571411c6eeSJed Brown 
3581411c6eeSJed Brown    Collective on DM
3591411c6eeSJed Brown 
3601411c6eeSJed Brown    Input Parameter:
3611411c6eeSJed Brown .  da - the distributed array that provides the mapping
3621411c6eeSJed Brown 
3631411c6eeSJed Brown    Output Parameter:
3641411c6eeSJed Brown .  ltog - the block mapping
3651411c6eeSJed Brown 
3661411c6eeSJed Brown    Level: intermediate
3671411c6eeSJed Brown 
3681411c6eeSJed Brown    Notes:
3691411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
3701411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
3711411c6eeSJed Brown 
3721411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
3731411c6eeSJed Brown @*/
3747087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
3751411c6eeSJed Brown {
3761411c6eeSJed Brown   PetscErrorCode ierr;
3771411c6eeSJed Brown 
3781411c6eeSJed Brown   PetscFunctionBegin;
3791411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3801411c6eeSJed Brown   PetscValidPointer(ltog,2);
3811411c6eeSJed Brown   if (!dm->ltogmapb) {
3821411c6eeSJed Brown     PetscInt bs;
3831411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
3841411c6eeSJed Brown     if (bs > 1) {
3851411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
3861411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
3871411c6eeSJed Brown     } else {
3881411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
3891411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
3901411c6eeSJed Brown     }
3911411c6eeSJed Brown   }
3921411c6eeSJed Brown   *ltog = dm->ltogmapb;
3931411c6eeSJed Brown   PetscFunctionReturn(0);
3941411c6eeSJed Brown }
3951411c6eeSJed Brown 
3961411c6eeSJed Brown #undef __FUNCT__
3971411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
3981411c6eeSJed Brown /*@
3991411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
4001411c6eeSJed Brown 
4011411c6eeSJed Brown    Not Collective
4021411c6eeSJed Brown 
4031411c6eeSJed Brown    Input Parameter:
4041411c6eeSJed Brown .  dm - the DM with block structure
4051411c6eeSJed Brown 
4061411c6eeSJed Brown    Output Parameter:
4071411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
4081411c6eeSJed Brown 
4091411c6eeSJed Brown    Level: intermediate
4101411c6eeSJed Brown 
4111411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
4121411c6eeSJed Brown @*/
4137087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
4141411c6eeSJed Brown {
4151411c6eeSJed Brown   PetscFunctionBegin;
4161411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4171411c6eeSJed Brown   PetscValidPointer(bs,2);
4181411c6eeSJed 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");
4191411c6eeSJed Brown   *bs = dm->bs;
4201411c6eeSJed Brown   PetscFunctionReturn(0);
4211411c6eeSJed Brown }
4221411c6eeSJed Brown 
4231411c6eeSJed Brown #undef __FUNCT__
42447c6ae99SBarry Smith #define __FUNCT__ "DMGetInterpolation"
42547c6ae99SBarry Smith /*@
426aa219208SBarry Smith     DMGetInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
42747c6ae99SBarry Smith 
42847c6ae99SBarry Smith     Collective on DM
42947c6ae99SBarry Smith 
43047c6ae99SBarry Smith     Input Parameter:
43147c6ae99SBarry Smith +   dm1 - the DM object
43247c6ae99SBarry Smith -   dm2 - the second, finer DM object
43347c6ae99SBarry Smith 
43447c6ae99SBarry Smith     Output Parameter:
43547c6ae99SBarry Smith +  mat - the interpolation
43647c6ae99SBarry Smith -  vec - the scaling (optional)
43747c6ae99SBarry Smith 
43847c6ae99SBarry Smith     Level: developer
43947c6ae99SBarry Smith 
44047c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetColoring(), DMGetMatrix()
44147c6ae99SBarry Smith 
44247c6ae99SBarry Smith @*/
4437087cfbeSBarry Smith PetscErrorCode  DMGetInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
44447c6ae99SBarry Smith {
44547c6ae99SBarry Smith   PetscErrorCode ierr;
44647c6ae99SBarry Smith 
44747c6ae99SBarry Smith   PetscFunctionBegin;
44847c6ae99SBarry Smith   ierr = (*dm1->ops->getinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
44947c6ae99SBarry Smith   PetscFunctionReturn(0);
45047c6ae99SBarry Smith }
45147c6ae99SBarry Smith 
45247c6ae99SBarry Smith #undef __FUNCT__
45347c6ae99SBarry Smith #define __FUNCT__ "DMGetInjection"
45447c6ae99SBarry Smith /*@
455aa219208SBarry Smith     DMGetInjection - Gets injection matrix between two DMDA or DMComposite objects
45647c6ae99SBarry Smith 
45747c6ae99SBarry Smith     Collective on DM
45847c6ae99SBarry Smith 
45947c6ae99SBarry Smith     Input Parameter:
46047c6ae99SBarry Smith +   dm1 - the DM object
46147c6ae99SBarry Smith -   dm2 - the second, finer DM object
46247c6ae99SBarry Smith 
46347c6ae99SBarry Smith     Output Parameter:
46447c6ae99SBarry Smith .   ctx - the injection
46547c6ae99SBarry Smith 
46647c6ae99SBarry Smith     Level: developer
46747c6ae99SBarry Smith 
46847c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetColoring(), DMGetMatrix(), DMGetInterpolation()
46947c6ae99SBarry Smith 
47047c6ae99SBarry Smith @*/
4717087cfbeSBarry Smith PetscErrorCode  DMGetInjection(DM dm1,DM dm2,VecScatter *ctx)
47247c6ae99SBarry Smith {
47347c6ae99SBarry Smith   PetscErrorCode ierr;
47447c6ae99SBarry Smith 
47547c6ae99SBarry Smith   PetscFunctionBegin;
47647c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
47747c6ae99SBarry Smith   PetscFunctionReturn(0);
47847c6ae99SBarry Smith }
47947c6ae99SBarry Smith 
48047c6ae99SBarry Smith #undef __FUNCT__
48147c6ae99SBarry Smith #define __FUNCT__ "DMGetColoring"
482d1e2c406SBarry Smith /*@C
483aa219208SBarry Smith     DMGetColoring - Gets coloring for a DMDA or DMComposite
48447c6ae99SBarry Smith 
48547c6ae99SBarry Smith     Collective on DM
48647c6ae99SBarry Smith 
48747c6ae99SBarry Smith     Input Parameter:
48847c6ae99SBarry Smith +   dm - the DM object
48947c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
49047c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
49147c6ae99SBarry Smith 
49247c6ae99SBarry Smith     Output Parameter:
49347c6ae99SBarry Smith .   coloring - the coloring
49447c6ae99SBarry Smith 
49547c6ae99SBarry Smith     Level: developer
49647c6ae99SBarry Smith 
49747c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetMatrix()
49847c6ae99SBarry Smith 
499aab9d709SJed Brown @*/
5007087cfbeSBarry Smith PetscErrorCode  DMGetColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
50147c6ae99SBarry Smith {
50247c6ae99SBarry Smith   PetscErrorCode ierr;
50347c6ae99SBarry Smith 
50447c6ae99SBarry Smith   PetscFunctionBegin;
50547c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
50647c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
50747c6ae99SBarry Smith   PetscFunctionReturn(0);
50847c6ae99SBarry Smith }
50947c6ae99SBarry Smith 
51047c6ae99SBarry Smith #undef __FUNCT__
51147c6ae99SBarry Smith #define __FUNCT__ "DMGetMatrix"
51247c6ae99SBarry Smith /*@C
513aa219208SBarry Smith     DMGetMatrix - Gets empty Jacobian for a DMDA or DMComposite
51447c6ae99SBarry Smith 
51547c6ae99SBarry Smith     Collective on DM
51647c6ae99SBarry Smith 
51747c6ae99SBarry Smith     Input Parameter:
51847c6ae99SBarry Smith +   dm - the DM object
51947c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
52094013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
52147c6ae99SBarry Smith 
52247c6ae99SBarry Smith     Output Parameter:
52347c6ae99SBarry Smith .   mat - the empty Jacobian
52447c6ae99SBarry Smith 
52547c6ae99SBarry Smith     Level: developer
52647c6ae99SBarry Smith 
52794013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
52894013140SBarry Smith        do not need to do it yourself.
52994013140SBarry Smith 
53094013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
531aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
53294013140SBarry Smith 
53394013140SBarry 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
53494013140SBarry Smith        internally by PETSc.
53594013140SBarry Smith 
53694013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
537aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
53894013140SBarry Smith 
539e00f7332SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
54047c6ae99SBarry Smith 
541aab9d709SJed Brown @*/
5427087cfbeSBarry Smith PetscErrorCode  DMGetMatrix(DM dm,const MatType mtype,Mat *mat)
54347c6ae99SBarry Smith {
54447c6ae99SBarry Smith   PetscErrorCode ierr;
545c7b7c8a4SJed Brown   char           ttype[256];
546c7b7c8a4SJed Brown   PetscBool      flg;
54747c6ae99SBarry Smith 
54847c6ae99SBarry Smith   PetscFunctionBegin;
549c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
550c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
551c7b7c8a4SJed Brown   ierr = PetscStrncpy(ttype,mtype,sizeof ttype);CHKERRQ(ierr);
552c7b7c8a4SJed Brown   ttype[sizeof ttype-1] = 0;
553c7b7c8a4SJed Brown   ierr = PetscOptionsBegin(((PetscObject)dm)->comm,((PetscObject)dm)->prefix,"DM options","Mat");CHKERRQ(ierr);
554c7b7c8a4SJed Brown   ierr = PetscOptionsList("-dm_mat_type","Matrix type","MatSetType",MatList,ttype,ttype,sizeof ttype,&flg);CHKERRQ(ierr);
555c7b7c8a4SJed Brown   ierr = PetscOptionsEnd();
556c7b7c8a4SJed Brown   if (flg || mtype) {
557c7b7c8a4SJed Brown     ierr = (*dm->ops->getmatrix)(dm,ttype,mat);CHKERRQ(ierr);
558c7b7c8a4SJed Brown   } else {                      /* Let the implementation decide */
559c7b7c8a4SJed Brown     ierr = (*dm->ops->getmatrix)(dm,PETSC_NULL,mat);CHKERRQ(ierr);
560c7b7c8a4SJed Brown   }
56147c6ae99SBarry Smith   PetscFunctionReturn(0);
56247c6ae99SBarry Smith }
56347c6ae99SBarry Smith 
56447c6ae99SBarry Smith #undef __FUNCT__
565732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
566732e2eb9SMatthew G Knepley /*@
567732e2eb9SMatthew G Knepley   DMSetMatrixPreallocateOnly - When DMGetMatrix() is called the matrix will be properly
568732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
569732e2eb9SMatthew G Knepley 
570732e2eb9SMatthew G Knepley   Logically Collective on DMDA
571732e2eb9SMatthew G Knepley 
572732e2eb9SMatthew G Knepley   Input Parameter:
573732e2eb9SMatthew G Knepley + dm - the DM
574732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
575732e2eb9SMatthew G Knepley 
576732e2eb9SMatthew G Knepley   Level: developer
577732e2eb9SMatthew G Knepley .seealso DMGetMatrix()
578732e2eb9SMatthew G Knepley @*/
579732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
580732e2eb9SMatthew G Knepley {
581732e2eb9SMatthew G Knepley   PetscFunctionBegin;
582732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
583732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
584732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
585732e2eb9SMatthew G Knepley }
586732e2eb9SMatthew G Knepley 
587732e2eb9SMatthew G Knepley #undef __FUNCT__
58847c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
58947c6ae99SBarry Smith /*@
59047c6ae99SBarry Smith     DMRefine - Refines a DM object
59147c6ae99SBarry Smith 
59247c6ae99SBarry Smith     Collective on DM
59347c6ae99SBarry Smith 
59447c6ae99SBarry Smith     Input Parameter:
59547c6ae99SBarry Smith +   dm - the DM object
59647c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
59747c6ae99SBarry Smith 
59847c6ae99SBarry Smith     Output Parameter:
59947c6ae99SBarry Smith .   dmf - the refined DM
60047c6ae99SBarry Smith 
60147c6ae99SBarry Smith     Level: developer
60247c6ae99SBarry Smith 
60347c6ae99SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
60447c6ae99SBarry Smith 
60547c6ae99SBarry Smith @*/
6067087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
60747c6ae99SBarry Smith {
60847c6ae99SBarry Smith   PetscErrorCode ierr;
60947c6ae99SBarry Smith 
61047c6ae99SBarry Smith   PetscFunctionBegin;
611732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
61247c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
613644e2e5bSBarry Smith   (*dmf)->ops->initialguess = dm->ops->initialguess;
614644e2e5bSBarry Smith   (*dmf)->ops->function     = dm->ops->function;
615644e2e5bSBarry Smith   (*dmf)->ops->functionj    = dm->ops->functionj;
616644e2e5bSBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
617644e2e5bSBarry Smith     (*dmf)->ops->jacobian     = dm->ops->jacobian;
618644e2e5bSBarry Smith   }
619644e2e5bSBarry Smith   (*dmf)->ctx     = dm->ctx;
620656b349aSBarry Smith   (*dmf)->levelup = dm->levelup + 1;
62147c6ae99SBarry Smith   PetscFunctionReturn(0);
62247c6ae99SBarry Smith }
62347c6ae99SBarry Smith 
62447c6ae99SBarry Smith #undef __FUNCT__
625eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
626eb3f98d2SBarry Smith /*@
627eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
628eb3f98d2SBarry Smith 
629eb3f98d2SBarry Smith     Not Collective
630eb3f98d2SBarry Smith 
631eb3f98d2SBarry Smith     Input Parameter:
632eb3f98d2SBarry Smith .   dm - the DM object
633eb3f98d2SBarry Smith 
634eb3f98d2SBarry Smith     Output Parameter:
635eb3f98d2SBarry Smith .   level - number of refinements
636eb3f98d2SBarry Smith 
637eb3f98d2SBarry Smith     Level: developer
638eb3f98d2SBarry Smith 
639eb3f98d2SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
640eb3f98d2SBarry Smith 
641eb3f98d2SBarry Smith @*/
642eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
643eb3f98d2SBarry Smith {
644eb3f98d2SBarry Smith   PetscFunctionBegin;
645eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
646eb3f98d2SBarry Smith   *level = dm->levelup;
647eb3f98d2SBarry Smith   PetscFunctionReturn(0);
648eb3f98d2SBarry Smith }
649eb3f98d2SBarry Smith 
650eb3f98d2SBarry Smith #undef __FUNCT__
65147c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
65247c6ae99SBarry Smith /*@
65347c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
65447c6ae99SBarry Smith 
65547c6ae99SBarry Smith     Neighbor-wise Collective on DM
65647c6ae99SBarry Smith 
65747c6ae99SBarry Smith     Input Parameters:
65847c6ae99SBarry Smith +   dm - the DM object
65947c6ae99SBarry Smith .   g - the global vector
66047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
66147c6ae99SBarry Smith -   l - the local vector
66247c6ae99SBarry Smith 
66347c6ae99SBarry Smith 
66447c6ae99SBarry Smith     Level: beginner
66547c6ae99SBarry Smith 
6669a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
66747c6ae99SBarry Smith 
66847c6ae99SBarry Smith @*/
6697087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
67047c6ae99SBarry Smith {
67147c6ae99SBarry Smith   PetscErrorCode ierr;
67247c6ae99SBarry Smith 
67347c6ae99SBarry Smith   PetscFunctionBegin;
67447c6ae99SBarry Smith   ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode,l);CHKERRQ(ierr);
67547c6ae99SBarry Smith   PetscFunctionReturn(0);
67647c6ae99SBarry Smith }
67747c6ae99SBarry Smith 
67847c6ae99SBarry Smith #undef __FUNCT__
67947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
68047c6ae99SBarry Smith /*@
68147c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
68247c6ae99SBarry Smith 
68347c6ae99SBarry Smith     Neighbor-wise Collective on DM
68447c6ae99SBarry Smith 
68547c6ae99SBarry Smith     Input Parameters:
68647c6ae99SBarry Smith +   dm - the DM object
68747c6ae99SBarry Smith .   g - the global vector
68847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
68947c6ae99SBarry Smith -   l - the local vector
69047c6ae99SBarry Smith 
69147c6ae99SBarry Smith 
69247c6ae99SBarry Smith     Level: beginner
69347c6ae99SBarry Smith 
6949a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
69547c6ae99SBarry Smith 
69647c6ae99SBarry Smith @*/
6977087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
69847c6ae99SBarry Smith {
69947c6ae99SBarry Smith   PetscErrorCode ierr;
70047c6ae99SBarry Smith 
70147c6ae99SBarry Smith   PetscFunctionBegin;
70247c6ae99SBarry Smith   ierr = (*dm->ops->globaltolocalend)(dm,g,mode,l);CHKERRQ(ierr);
70347c6ae99SBarry Smith   PetscFunctionReturn(0);
70447c6ae99SBarry Smith }
70547c6ae99SBarry Smith 
70647c6ae99SBarry Smith #undef __FUNCT__
7079a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
70847c6ae99SBarry Smith /*@
7099a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
7109a42bb27SBarry Smith 
7119a42bb27SBarry Smith     Neighbor-wise Collective on DM
7129a42bb27SBarry Smith 
7139a42bb27SBarry Smith     Input Parameters:
7149a42bb27SBarry Smith +   dm - the DM object
715f6813fd5SJed Brown .   l - the local vector
7169a42bb27SBarry 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
7179a42bb27SBarry Smith            base point.
718f6813fd5SJed Brown - - the global vector
7199a42bb27SBarry Smith 
7209a42bb27SBarry 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
7219a42bb27SBarry 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
7229a42bb27SBarry Smith            global array to the final global array with VecAXPY().
7239a42bb27SBarry Smith 
7249a42bb27SBarry Smith     Level: beginner
7259a42bb27SBarry Smith 
7269a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
7279a42bb27SBarry Smith 
7289a42bb27SBarry Smith @*/
7297087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
7309a42bb27SBarry Smith {
7319a42bb27SBarry Smith   PetscErrorCode ierr;
7329a42bb27SBarry Smith 
7339a42bb27SBarry Smith   PetscFunctionBegin;
734f6813fd5SJed Brown   ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode,g);CHKERRQ(ierr);
7359a42bb27SBarry Smith   PetscFunctionReturn(0);
7369a42bb27SBarry Smith }
7379a42bb27SBarry Smith 
7389a42bb27SBarry Smith #undef __FUNCT__
7399a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
7409a42bb27SBarry Smith /*@
7419a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
74247c6ae99SBarry Smith 
74347c6ae99SBarry Smith     Neighbor-wise Collective on DM
74447c6ae99SBarry Smith 
74547c6ae99SBarry Smith     Input Parameters:
74647c6ae99SBarry Smith +   dm - the DM object
747f6813fd5SJed Brown .   l - the local vector
74847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
749f6813fd5SJed Brown -   g - the global vector
75047c6ae99SBarry Smith 
75147c6ae99SBarry Smith 
75247c6ae99SBarry Smith     Level: beginner
75347c6ae99SBarry Smith 
7549a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
75547c6ae99SBarry Smith 
75647c6ae99SBarry Smith @*/
7577087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
75847c6ae99SBarry Smith {
75947c6ae99SBarry Smith   PetscErrorCode ierr;
76047c6ae99SBarry Smith 
76147c6ae99SBarry Smith   PetscFunctionBegin;
762f6813fd5SJed Brown   ierr = (*dm->ops->localtoglobalend)(dm,l,mode,g);CHKERRQ(ierr);
76347c6ae99SBarry Smith   PetscFunctionReturn(0);
76447c6ae99SBarry Smith }
76547c6ae99SBarry Smith 
76647c6ae99SBarry Smith #undef __FUNCT__
76747c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
76847c6ae99SBarry Smith /*@
76947c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
77047c6ae99SBarry Smith 
77147c6ae99SBarry Smith     Collective on DM
77247c6ae99SBarry Smith 
77347c6ae99SBarry Smith     Input Parameter:
77447c6ae99SBarry Smith +   dm - the DM object
77547c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
77647c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
77747c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
77847c6ae99SBarry Smith 
77947c6ae99SBarry Smith     Level: developer
78047c6ae99SBarry Smith 
7811b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
78247c6ae99SBarry Smith          DMSetFunction()
78347c6ae99SBarry Smith 
78447c6ae99SBarry Smith @*/
7857087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
78647c6ae99SBarry Smith {
78747c6ae99SBarry Smith   PetscErrorCode ierr;
78847c6ae99SBarry Smith   PetscFunctionBegin;
78947c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
79047c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
79147c6ae99SBarry Smith   if (A != B) {
79247c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
79347c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
79447c6ae99SBarry Smith   }
79547c6ae99SBarry Smith   PetscFunctionReturn(0);
79647c6ae99SBarry Smith }
79747c6ae99SBarry Smith 
79847c6ae99SBarry Smith #undef __FUNCT__
79947c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
80047c6ae99SBarry Smith /*@
80147c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
80247c6ae99SBarry Smith 
80347c6ae99SBarry Smith     Collective on DM
80447c6ae99SBarry Smith 
80547c6ae99SBarry Smith     Input Parameter:
80647c6ae99SBarry Smith +   dm - the DM object
80747c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
80847c6ae99SBarry Smith 
80947c6ae99SBarry Smith     Output Parameter:
81047c6ae99SBarry Smith .   dmc - the coarsened DM
81147c6ae99SBarry Smith 
81247c6ae99SBarry Smith     Level: developer
81347c6ae99SBarry Smith 
81447c6ae99SBarry Smith .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
81547c6ae99SBarry Smith 
81647c6ae99SBarry Smith @*/
8177087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
81847c6ae99SBarry Smith {
81947c6ae99SBarry Smith   PetscErrorCode ierr;
82047c6ae99SBarry Smith 
82147c6ae99SBarry Smith   PetscFunctionBegin;
82247c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
82347c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
82447c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
82547c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
82647c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
82747c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
82847c6ae99SBarry Smith   }
829644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
830656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
83147c6ae99SBarry Smith   PetscFunctionReturn(0);
83247c6ae99SBarry Smith }
83347c6ae99SBarry Smith 
83447c6ae99SBarry Smith #undef __FUNCT__
83547c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
83647c6ae99SBarry Smith /*@C
83747c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
83847c6ae99SBarry Smith 
83947c6ae99SBarry Smith     Collective on DM
84047c6ae99SBarry Smith 
84147c6ae99SBarry Smith     Input Parameter:
84247c6ae99SBarry Smith +   dm - the DM object
84347c6ae99SBarry Smith -   nlevels - the number of levels of refinement
84447c6ae99SBarry Smith 
84547c6ae99SBarry Smith     Output Parameter:
84647c6ae99SBarry Smith .   dmf - the refined DM hierarchy
84747c6ae99SBarry Smith 
84847c6ae99SBarry Smith     Level: developer
84947c6ae99SBarry Smith 
85047c6ae99SBarry Smith .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
85147c6ae99SBarry Smith 
85247c6ae99SBarry Smith @*/
8537087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
85447c6ae99SBarry Smith {
85547c6ae99SBarry Smith   PetscErrorCode ierr;
85647c6ae99SBarry Smith 
85747c6ae99SBarry Smith   PetscFunctionBegin;
85847c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
85947c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
86047c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
86147c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
86247c6ae99SBarry Smith   } else if (dm->ops->refine) {
86347c6ae99SBarry Smith     PetscInt i;
86447c6ae99SBarry Smith 
86547c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
86647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
86747c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
86847c6ae99SBarry Smith     }
86947c6ae99SBarry Smith   } else {
87047c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
87147c6ae99SBarry Smith   }
87247c6ae99SBarry Smith   PetscFunctionReturn(0);
87347c6ae99SBarry Smith }
87447c6ae99SBarry Smith 
87547c6ae99SBarry Smith #undef __FUNCT__
87647c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
87747c6ae99SBarry Smith /*@C
87847c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
87947c6ae99SBarry Smith 
88047c6ae99SBarry Smith     Collective on DM
88147c6ae99SBarry Smith 
88247c6ae99SBarry Smith     Input Parameter:
88347c6ae99SBarry Smith +   dm - the DM object
88447c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
88547c6ae99SBarry Smith 
88647c6ae99SBarry Smith     Output Parameter:
88747c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
88847c6ae99SBarry Smith 
88947c6ae99SBarry Smith     Level: developer
89047c6ae99SBarry Smith 
89147c6ae99SBarry Smith .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
89247c6ae99SBarry Smith 
89347c6ae99SBarry Smith @*/
8947087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
89547c6ae99SBarry Smith {
89647c6ae99SBarry Smith   PetscErrorCode ierr;
89747c6ae99SBarry Smith 
89847c6ae99SBarry Smith   PetscFunctionBegin;
89947c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
90047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
90147c6ae99SBarry Smith   PetscValidPointer(dmc,3);
90247c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
90347c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
90447c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
90547c6ae99SBarry Smith     PetscInt i;
90647c6ae99SBarry Smith 
90747c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
90847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
90947c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
91047c6ae99SBarry Smith     }
91147c6ae99SBarry Smith   } else {
91247c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
91347c6ae99SBarry Smith   }
91447c6ae99SBarry Smith   PetscFunctionReturn(0);
91547c6ae99SBarry Smith }
91647c6ae99SBarry Smith 
91747c6ae99SBarry Smith #undef __FUNCT__
91847c6ae99SBarry Smith #define __FUNCT__ "DMGetAggregates"
91947c6ae99SBarry Smith /*@
92047c6ae99SBarry Smith    DMGetAggregates - Gets the aggregates that map between
92147c6ae99SBarry Smith    grids associated with two DMs.
92247c6ae99SBarry Smith 
92347c6ae99SBarry Smith    Collective on DM
92447c6ae99SBarry Smith 
92547c6ae99SBarry Smith    Input Parameters:
92647c6ae99SBarry Smith +  dmc - the coarse grid DM
92747c6ae99SBarry Smith -  dmf - the fine grid DM
92847c6ae99SBarry Smith 
92947c6ae99SBarry Smith    Output Parameters:
93047c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
93147c6ae99SBarry Smith 
93247c6ae99SBarry Smith    Level: intermediate
93347c6ae99SBarry Smith 
93447c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith .seealso: DMRefine(), DMGetInjection(), DMGetInterpolation()
93747c6ae99SBarry Smith @*/
9387087cfbeSBarry Smith PetscErrorCode  DMGetAggregates(DM dmc, DM dmf, Mat *rest)
93947c6ae99SBarry Smith {
94047c6ae99SBarry Smith   PetscErrorCode ierr;
94147c6ae99SBarry Smith 
94247c6ae99SBarry Smith   PetscFunctionBegin;
94347c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
94447c6ae99SBarry Smith   PetscFunctionReturn(0);
94547c6ae99SBarry Smith }
94647c6ae99SBarry Smith 
94747c6ae99SBarry Smith #undef __FUNCT__
9481b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
949b07ff414SBarry Smith /*@
9501b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
95147c6ae99SBarry Smith 
95247c6ae99SBarry Smith     Not Collective
95347c6ae99SBarry Smith 
95447c6ae99SBarry Smith     Input Parameters:
95547c6ae99SBarry Smith +   dm - the DM object
95647c6ae99SBarry Smith -   ctx - the user context
95747c6ae99SBarry Smith 
95847c6ae99SBarry Smith     Level: intermediate
95947c6ae99SBarry Smith 
9601b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext()
96147c6ae99SBarry Smith 
96247c6ae99SBarry Smith @*/
9631b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
96447c6ae99SBarry Smith {
96547c6ae99SBarry Smith   PetscFunctionBegin;
96647c6ae99SBarry Smith   dm->ctx = ctx;
96747c6ae99SBarry Smith   PetscFunctionReturn(0);
96847c6ae99SBarry Smith }
96947c6ae99SBarry Smith 
97047c6ae99SBarry Smith #undef __FUNCT__
9711b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
97247c6ae99SBarry Smith /*@
9731b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
97447c6ae99SBarry Smith 
97547c6ae99SBarry Smith     Not Collective
97647c6ae99SBarry Smith 
97747c6ae99SBarry Smith     Input Parameter:
97847c6ae99SBarry Smith .   dm - the DM object
97947c6ae99SBarry Smith 
98047c6ae99SBarry Smith     Output Parameter:
98147c6ae99SBarry Smith .   ctx - the user context
98247c6ae99SBarry Smith 
98347c6ae99SBarry Smith     Level: intermediate
98447c6ae99SBarry Smith 
9851b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext()
98647c6ae99SBarry Smith 
98747c6ae99SBarry Smith @*/
9881b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
98947c6ae99SBarry Smith {
99047c6ae99SBarry Smith   PetscFunctionBegin;
9911b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
99247c6ae99SBarry Smith   PetscFunctionReturn(0);
99347c6ae99SBarry Smith }
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith #undef __FUNCT__
99647c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
99747c6ae99SBarry Smith /*@
99847c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith     Logically Collective on DM
100147c6ae99SBarry Smith 
100247c6ae99SBarry Smith     Input Parameter:
100347c6ae99SBarry Smith +   dm - the DM object to destroy
100447c6ae99SBarry Smith -   f - the function to compute the initial guess
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith     Level: intermediate
100747c6ae99SBarry Smith 
10081b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
100947c6ae99SBarry Smith 
101047c6ae99SBarry Smith @*/
10117087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
101247c6ae99SBarry Smith {
101347c6ae99SBarry Smith   PetscFunctionBegin;
101447c6ae99SBarry Smith   dm->ops->initialguess = f;
101547c6ae99SBarry Smith   PetscFunctionReturn(0);
101647c6ae99SBarry Smith }
101747c6ae99SBarry Smith 
101847c6ae99SBarry Smith #undef __FUNCT__
101947c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
102047c6ae99SBarry Smith /*@
102147c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
102247c6ae99SBarry Smith 
102347c6ae99SBarry Smith     Logically Collective on DM
102447c6ae99SBarry Smith 
102547c6ae99SBarry Smith     Input Parameter:
102647c6ae99SBarry Smith +   dm - the DM object
102747c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
102847c6ae99SBarry Smith 
102947c6ae99SBarry Smith     Level: intermediate
103047c6ae99SBarry Smith 
103147c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
103247c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
103347c6ae99SBarry Smith 
10341b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
103547c6ae99SBarry Smith          DMSetJacobian()
103647c6ae99SBarry Smith 
103747c6ae99SBarry Smith @*/
10387087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
103947c6ae99SBarry Smith {
104047c6ae99SBarry Smith   PetscFunctionBegin;
104147c6ae99SBarry Smith   dm->ops->function = f;
104247c6ae99SBarry Smith   if (f) {
104347c6ae99SBarry Smith     dm->ops->functionj = f;
104447c6ae99SBarry Smith   }
104547c6ae99SBarry Smith   PetscFunctionReturn(0);
104647c6ae99SBarry Smith }
104747c6ae99SBarry Smith 
104847c6ae99SBarry Smith #undef __FUNCT__
104947c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
105047c6ae99SBarry Smith /*@
105147c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
105247c6ae99SBarry Smith 
105347c6ae99SBarry Smith     Logically Collective on DM
105447c6ae99SBarry Smith 
105547c6ae99SBarry Smith     Input Parameter:
105647c6ae99SBarry Smith +   dm - the DM object to destroy
105747c6ae99SBarry Smith -   f - the function to compute the matrix entries
105847c6ae99SBarry Smith 
105947c6ae99SBarry Smith     Level: intermediate
106047c6ae99SBarry Smith 
10611b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
106247c6ae99SBarry Smith          DMSetFunction()
106347c6ae99SBarry Smith 
106447c6ae99SBarry Smith @*/
10657087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
106647c6ae99SBarry Smith {
106747c6ae99SBarry Smith   PetscFunctionBegin;
106847c6ae99SBarry Smith   dm->ops->jacobian = f;
106947c6ae99SBarry Smith   PetscFunctionReturn(0);
107047c6ae99SBarry Smith }
107147c6ae99SBarry Smith 
107247c6ae99SBarry Smith #undef __FUNCT__
107347c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
107447c6ae99SBarry Smith /*@
107547c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
107647c6ae99SBarry Smith 
107747c6ae99SBarry Smith     Collective on DM
107847c6ae99SBarry Smith 
107947c6ae99SBarry Smith     Input Parameter:
108047c6ae99SBarry Smith +   dm - the DM object to destroy
108147c6ae99SBarry Smith -   x - the vector to hold the initial guess values
108247c6ae99SBarry Smith 
108347c6ae99SBarry Smith     Level: developer
108447c6ae99SBarry Smith 
10851b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
108647c6ae99SBarry Smith 
108747c6ae99SBarry Smith @*/
10887087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
108947c6ae99SBarry Smith {
109047c6ae99SBarry Smith   PetscErrorCode ierr;
109147c6ae99SBarry Smith   PetscFunctionBegin;
109247c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
109347c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
109447c6ae99SBarry Smith   PetscFunctionReturn(0);
109547c6ae99SBarry Smith }
109647c6ae99SBarry Smith 
109747c6ae99SBarry Smith #undef __FUNCT__
109847c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
109947c6ae99SBarry Smith /*@
110047c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
110147c6ae99SBarry Smith 
110247c6ae99SBarry Smith     Not Collective
110347c6ae99SBarry Smith 
110447c6ae99SBarry Smith     Input Parameter:
110547c6ae99SBarry Smith .   dm - the DM object to destroy
110647c6ae99SBarry Smith 
110747c6ae99SBarry Smith     Output Parameter:
110847c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
110947c6ae99SBarry Smith 
111047c6ae99SBarry Smith     Level: developer
111147c6ae99SBarry Smith 
11121b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
111347c6ae99SBarry Smith 
111447c6ae99SBarry Smith @*/
11157087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
111647c6ae99SBarry Smith {
111747c6ae99SBarry Smith   PetscFunctionBegin;
111847c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
111947c6ae99SBarry Smith   PetscFunctionReturn(0);
112047c6ae99SBarry Smith }
112147c6ae99SBarry Smith 
112247c6ae99SBarry Smith #undef __FUNCT__
112347c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
112447c6ae99SBarry Smith /*@
112547c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
112647c6ae99SBarry Smith 
112747c6ae99SBarry Smith     Not Collective
112847c6ae99SBarry Smith 
112947c6ae99SBarry Smith     Input Parameter:
113047c6ae99SBarry Smith .   dm - the DM object to destroy
113147c6ae99SBarry Smith 
113247c6ae99SBarry Smith     Output Parameter:
113347c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
113447c6ae99SBarry Smith 
113547c6ae99SBarry Smith     Level: developer
113647c6ae99SBarry Smith 
11371b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
113847c6ae99SBarry Smith 
113947c6ae99SBarry Smith @*/
11407087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
114147c6ae99SBarry Smith {
114247c6ae99SBarry Smith   PetscFunctionBegin;
114347c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
114447c6ae99SBarry Smith   PetscFunctionReturn(0);
114547c6ae99SBarry Smith }
114647c6ae99SBarry Smith 
114747c6ae99SBarry Smith #undef __FUNCT__
114847c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
114947c6ae99SBarry Smith /*@
115047c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
115147c6ae99SBarry Smith 
115247c6ae99SBarry Smith     Not Collective
115347c6ae99SBarry Smith 
115447c6ae99SBarry Smith     Input Parameter:
115547c6ae99SBarry Smith .   dm - the DM object to destroy
115647c6ae99SBarry Smith 
115747c6ae99SBarry Smith     Output Parameter:
115847c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
115947c6ae99SBarry Smith 
116047c6ae99SBarry Smith     Level: developer
116147c6ae99SBarry Smith 
11621b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
116347c6ae99SBarry Smith 
116447c6ae99SBarry Smith @*/
11657087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
116647c6ae99SBarry Smith {
116747c6ae99SBarry Smith   PetscFunctionBegin;
116847c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
116947c6ae99SBarry Smith   PetscFunctionReturn(0);
117047c6ae99SBarry Smith }
117147c6ae99SBarry Smith 
117247c6ae99SBarry Smith #undef __FUNCT__
117347c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
117447c6ae99SBarry Smith /*@
117547c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
117647c6ae99SBarry Smith 
117747c6ae99SBarry Smith     Collective on DM
117847c6ae99SBarry Smith 
117947c6ae99SBarry Smith     Input Parameter:
118047c6ae99SBarry Smith +   dm - the DM object to destroy
118147c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
118247c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
118347c6ae99SBarry Smith 
118447c6ae99SBarry Smith     Level: developer
118547c6ae99SBarry Smith 
11861b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
118747c6ae99SBarry Smith          DMSetJacobian()
118847c6ae99SBarry Smith 
118947c6ae99SBarry Smith @*/
11907087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
119147c6ae99SBarry Smith {
119247c6ae99SBarry Smith   PetscErrorCode ierr;
119347c6ae99SBarry Smith   PetscFunctionBegin;
119447c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
1195644e2e5bSBarry Smith   PetscStackPush("DM user function");
119647c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
1197644e2e5bSBarry Smith   PetscStackPop;
119847c6ae99SBarry Smith   PetscFunctionReturn(0);
119947c6ae99SBarry Smith }
120047c6ae99SBarry Smith 
120147c6ae99SBarry Smith 
120247c6ae99SBarry Smith #undef __FUNCT__
120347c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
120447c6ae99SBarry Smith /*@
120547c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
120647c6ae99SBarry Smith 
120747c6ae99SBarry Smith     Collective on DM
120847c6ae99SBarry Smith 
120947c6ae99SBarry Smith     Input Parameter:
121047c6ae99SBarry Smith +   dm - the DM object
1211cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
121247c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
121347c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
121447c6ae99SBarry Smith 
121547c6ae99SBarry Smith     Level: developer
121647c6ae99SBarry Smith 
12171b2093e4SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
121847c6ae99SBarry Smith          DMSetFunction()
121947c6ae99SBarry Smith 
122047c6ae99SBarry Smith @*/
12217087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
122247c6ae99SBarry Smith {
122347c6ae99SBarry Smith   PetscErrorCode ierr;
122447c6ae99SBarry Smith 
122547c6ae99SBarry Smith   PetscFunctionBegin;
122647c6ae99SBarry Smith   if (!dm->ops->jacobian) {
122747c6ae99SBarry Smith     ISColoring     coloring;
122847c6ae99SBarry Smith     MatFDColoring  fd;
122947c6ae99SBarry Smith 
12302533e041SBarry Smith     ierr = DMGetColoring(dm,IS_COLORING_GLOBAL,MATAIJ,&coloring);CHKERRQ(ierr);
123147c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
1232fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
123347c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
123471cd77b2SBarry Smith 
123547c6ae99SBarry Smith     dm->fd = fd;
123647c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
12372533e041SBarry Smith 
123871cd77b2SBarry Smith     /* don't know why this is needed */
123971cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
124047c6ae99SBarry Smith   }
124147c6ae99SBarry Smith   if (!x) x = dm->x;
124247c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
1243cab2e9ccSBarry Smith 
124471cd77b2SBarry 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 */
1245649052a6SBarry Smith   if (x) {
1246cab2e9ccSBarry Smith     if (!dm->x) {
124771cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
1248cab2e9ccSBarry Smith     }
1249cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
1250649052a6SBarry Smith   }
1251a8248277SBarry Smith   if (A != B) {
1252a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1253a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1254a8248277SBarry Smith   }
125547c6ae99SBarry Smith   PetscFunctionReturn(0);
125647c6ae99SBarry Smith }
1257264ace61SBarry Smith 
1258cab2e9ccSBarry Smith 
1259264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
1260264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
1261264ace61SBarry Smith 
1262264ace61SBarry Smith #undef __FUNCT__
1263264ace61SBarry Smith #define __FUNCT__ "DMSetType"
1264264ace61SBarry Smith /*@C
1265264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
1266264ace61SBarry Smith 
1267264ace61SBarry Smith   Collective on DM
1268264ace61SBarry Smith 
1269264ace61SBarry Smith   Input Parameters:
1270264ace61SBarry Smith + dm     - The DM object
1271264ace61SBarry Smith - method - The name of the DM type
1272264ace61SBarry Smith 
1273264ace61SBarry Smith   Options Database Key:
1274264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
1275264ace61SBarry Smith 
1276264ace61SBarry Smith   Notes:
1277e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
1278264ace61SBarry Smith 
1279264ace61SBarry Smith   Level: intermediate
1280264ace61SBarry Smith 
1281264ace61SBarry Smith .keywords: DM, set, type
1282264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
1283264ace61SBarry Smith @*/
12847087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
1285264ace61SBarry Smith {
1286264ace61SBarry Smith   PetscErrorCode (*r)(DM);
1287264ace61SBarry Smith   PetscBool      match;
1288264ace61SBarry Smith   PetscErrorCode ierr;
1289264ace61SBarry Smith 
1290264ace61SBarry Smith   PetscFunctionBegin;
1291264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1292264ace61SBarry Smith   ierr = PetscTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
1293264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
1294264ace61SBarry Smith 
1295264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
12964b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
1297264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
1298264ace61SBarry Smith 
1299264ace61SBarry Smith   if (dm->ops->destroy) {
1300264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
1301264ace61SBarry Smith   }
1302264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
1303264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
1304264ace61SBarry Smith   PetscFunctionReturn(0);
1305264ace61SBarry Smith }
1306264ace61SBarry Smith 
1307264ace61SBarry Smith #undef __FUNCT__
1308264ace61SBarry Smith #define __FUNCT__ "DMGetType"
1309264ace61SBarry Smith /*@C
1310264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
1311264ace61SBarry Smith 
1312264ace61SBarry Smith   Not Collective
1313264ace61SBarry Smith 
1314264ace61SBarry Smith   Input Parameter:
1315264ace61SBarry Smith . dm  - The DM
1316264ace61SBarry Smith 
1317264ace61SBarry Smith   Output Parameter:
1318264ace61SBarry Smith . type - The DM type name
1319264ace61SBarry Smith 
1320264ace61SBarry Smith   Level: intermediate
1321264ace61SBarry Smith 
1322264ace61SBarry Smith .keywords: DM, get, type, name
1323264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
1324264ace61SBarry Smith @*/
13257087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
1326264ace61SBarry Smith {
1327264ace61SBarry Smith   PetscErrorCode ierr;
1328264ace61SBarry Smith 
1329264ace61SBarry Smith   PetscFunctionBegin;
1330264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1331264ace61SBarry Smith   PetscValidCharPointer(type,2);
1332264ace61SBarry Smith   if (!DMRegisterAllCalled) {
1333264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
1334264ace61SBarry Smith   }
1335264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
1336264ace61SBarry Smith   PetscFunctionReturn(0);
1337264ace61SBarry Smith }
1338264ace61SBarry Smith 
133967a56275SMatthew G Knepley #undef __FUNCT__
134067a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
134167a56275SMatthew G Knepley /*@C
134267a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
134367a56275SMatthew G Knepley 
134467a56275SMatthew G Knepley   Collective on DM
134567a56275SMatthew G Knepley 
134667a56275SMatthew G Knepley   Input Parameters:
134767a56275SMatthew G Knepley + dm - the DM
134867a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
134967a56275SMatthew G Knepley 
135067a56275SMatthew G Knepley   Output Parameter:
135167a56275SMatthew G Knepley . M - pointer to new DM
135267a56275SMatthew G Knepley 
135367a56275SMatthew G Knepley   Notes:
135467a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
135567a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
135667a56275SMatthew G Knepley   of the input DM.
135767a56275SMatthew G Knepley 
135867a56275SMatthew G Knepley   Level: intermediate
135967a56275SMatthew G Knepley 
136067a56275SMatthew G Knepley .seealso: DMCreate()
136167a56275SMatthew G Knepley @*/
136267a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
136367a56275SMatthew G Knepley {
136467a56275SMatthew G Knepley   DM             B;
136567a56275SMatthew G Knepley   char           convname[256];
136667a56275SMatthew G Knepley   PetscBool      sametype, issame;
136767a56275SMatthew G Knepley   PetscErrorCode ierr;
136867a56275SMatthew G Knepley 
136967a56275SMatthew G Knepley   PetscFunctionBegin;
137067a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
137167a56275SMatthew G Knepley   PetscValidType(dm,1);
137267a56275SMatthew G Knepley   PetscValidPointer(M,3);
137367a56275SMatthew G Knepley   ierr = PetscTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
137467a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
137567a56275SMatthew G Knepley   {
137667a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
137767a56275SMatthew G Knepley 
137867a56275SMatthew G Knepley     /*
137967a56275SMatthew G Knepley        Order of precedence:
138067a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
138167a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
138267a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
138367a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
138467a56275SMatthew G Knepley        5) Use a really basic converter.
138567a56275SMatthew G Knepley     */
138667a56275SMatthew G Knepley 
138767a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
138867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
138967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
139067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
139167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
139267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
139367a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
139467a56275SMatthew G Knepley     if (conv) goto foundconv;
139567a56275SMatthew G Knepley 
139667a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
139767a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
139867a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
139967a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
140067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
140167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
140267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
140367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
140467a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
140567a56275SMatthew G Knepley     if (conv) {
1406fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
140767a56275SMatthew G Knepley       goto foundconv;
140867a56275SMatthew G Knepley     }
140967a56275SMatthew G Knepley 
141067a56275SMatthew G Knepley #if 0
141167a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
141267a56275SMatthew G Knepley     conv = B->ops->convertfrom;
1413fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
141467a56275SMatthew G Knepley     if (conv) goto foundconv;
141567a56275SMatthew G Knepley 
141667a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
141767a56275SMatthew G Knepley     if (dm->ops->convert) {
141867a56275SMatthew G Knepley       conv = dm->ops->convert;
141967a56275SMatthew G Knepley     }
142067a56275SMatthew G Knepley     if (conv) goto foundconv;
142167a56275SMatthew G Knepley #endif
142267a56275SMatthew G Knepley 
142367a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
142467a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
142567a56275SMatthew G Knepley 
142667a56275SMatthew G Knepley     foundconv:
142767a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
142867a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
142967a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
143067a56275SMatthew G Knepley   }
143167a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
143267a56275SMatthew G Knepley   PetscFunctionReturn(0);
143367a56275SMatthew G Knepley }
1434264ace61SBarry Smith 
1435264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1436264ace61SBarry Smith 
1437264ace61SBarry Smith #undef __FUNCT__
1438264ace61SBarry Smith #define __FUNCT__ "DMRegister"
1439264ace61SBarry Smith /*@C
1440264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
1441264ace61SBarry Smith 
1442264ace61SBarry Smith   Level: advanced
1443264ace61SBarry Smith @*/
14447087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
1445264ace61SBarry Smith {
1446264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
1447264ace61SBarry Smith   PetscErrorCode ierr;
1448264ace61SBarry Smith 
1449264ace61SBarry Smith   PetscFunctionBegin;
1450264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
1451264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
1452264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
1453264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
1454264ace61SBarry Smith   PetscFunctionReturn(0);
1455264ace61SBarry Smith }
1456264ace61SBarry Smith 
1457264ace61SBarry Smith 
1458264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1459264ace61SBarry Smith #undef __FUNCT__
1460264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
1461264ace61SBarry Smith /*@C
1462264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
1463264ace61SBarry Smith 
1464264ace61SBarry Smith    Not Collective
1465264ace61SBarry Smith 
1466264ace61SBarry Smith    Level: advanced
1467264ace61SBarry Smith 
1468264ace61SBarry Smith .keywords: DM, register, destroy
1469264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
1470264ace61SBarry Smith @*/
14717087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
1472264ace61SBarry Smith {
1473264ace61SBarry Smith   PetscErrorCode ierr;
1474264ace61SBarry Smith 
1475264ace61SBarry Smith   PetscFunctionBegin;
1476264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
1477264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
1478264ace61SBarry Smith   PetscFunctionReturn(0);
1479264ace61SBarry Smith }
148023f975d1SBarry Smith 
148123f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
1482c6db04a5SJed Brown #include <mex.h>
148323f975d1SBarry Smith 
14843014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
148523f975d1SBarry Smith 
148623f975d1SBarry Smith #undef __FUNCT__
148723f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
148823f975d1SBarry Smith /*
148923f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
149023f975d1SBarry Smith                          DMSetFunctionMatlab().
149123f975d1SBarry Smith 
149223f975d1SBarry Smith    For linear problems x is null
149323f975d1SBarry Smith 
149423f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
149523f975d1SBarry Smith */
14967087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
149723f975d1SBarry Smith {
149823f975d1SBarry Smith   PetscErrorCode    ierr;
149923f975d1SBarry Smith   DMMatlabContext   *sctx;
150023f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
150123f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
150223f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
150323f975d1SBarry Smith 
150423f975d1SBarry Smith   PetscFunctionBegin;
150523f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
150623f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
150723f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
150823f975d1SBarry Smith 
150923f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
15101b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
151123f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
151223f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
15133014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
151423f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
151523f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
151623f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
151723f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
1518b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
151923f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
152023f975d1SBarry Smith   mxDestroyArray(prhs[0]);
152123f975d1SBarry Smith   mxDestroyArray(prhs[1]);
152223f975d1SBarry Smith   mxDestroyArray(prhs[2]);
152323f975d1SBarry Smith   mxDestroyArray(prhs[3]);
152423f975d1SBarry Smith   mxDestroyArray(plhs[0]);
152523f975d1SBarry Smith   PetscFunctionReturn(0);
152623f975d1SBarry Smith }
152723f975d1SBarry Smith 
152823f975d1SBarry Smith 
152923f975d1SBarry Smith #undef __FUNCT__
153023f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
153123f975d1SBarry Smith /*
153223f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
153323f975d1SBarry Smith 
153423f975d1SBarry Smith */
15357087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
153623f975d1SBarry Smith {
153723f975d1SBarry Smith   PetscErrorCode    ierr;
153823f975d1SBarry Smith   DMMatlabContext   *sctx;
153923f975d1SBarry Smith 
154023f975d1SBarry Smith   PetscFunctionBegin;
154123f975d1SBarry Smith   /* currently sctx is memory bleed */
15421b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
15433014e516SBarry Smith   if (!sctx) {
154423f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
15453014e516SBarry Smith   }
154623f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
15471b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
154823f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
154923f975d1SBarry Smith   PetscFunctionReturn(0);
155023f975d1SBarry Smith }
15513014e516SBarry Smith 
15523014e516SBarry Smith #undef __FUNCT__
15533014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
15543014e516SBarry Smith /*
15553014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
15563014e516SBarry Smith                          DMSetJacobianMatlab().
15573014e516SBarry Smith 
15583014e516SBarry Smith    For linear problems x is null
15593014e516SBarry Smith 
15603014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
15613014e516SBarry Smith */
15627087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
15633014e516SBarry Smith {
15643014e516SBarry Smith   PetscErrorCode    ierr;
15653014e516SBarry Smith   DMMatlabContext   *sctx;
15663014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
15673014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
15683014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
15693014e516SBarry Smith 
15703014e516SBarry Smith   PetscFunctionBegin;
15713014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15723014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
15733014e516SBarry Smith 
1574e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
15751b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
15763014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
15773014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
15783014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
15793014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
15803014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
15813014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
15823014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
15833014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
15843014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
1585b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
1586c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
1587c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
15883014e516SBarry Smith   mxDestroyArray(prhs[0]);
15893014e516SBarry Smith   mxDestroyArray(prhs[1]);
15903014e516SBarry Smith   mxDestroyArray(prhs[2]);
15913014e516SBarry Smith   mxDestroyArray(prhs[3]);
15923014e516SBarry Smith   mxDestroyArray(prhs[4]);
15933014e516SBarry Smith   mxDestroyArray(plhs[0]);
15943014e516SBarry Smith   mxDestroyArray(plhs[1]);
15953014e516SBarry Smith   PetscFunctionReturn(0);
15963014e516SBarry Smith }
15973014e516SBarry Smith 
15983014e516SBarry Smith 
15993014e516SBarry Smith #undef __FUNCT__
16003014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
16013014e516SBarry Smith /*
16023014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
16033014e516SBarry Smith 
16043014e516SBarry Smith */
16057087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
16063014e516SBarry Smith {
16073014e516SBarry Smith   PetscErrorCode    ierr;
16083014e516SBarry Smith   DMMatlabContext   *sctx;
16093014e516SBarry Smith 
16103014e516SBarry Smith   PetscFunctionBegin;
16113014e516SBarry Smith   /* currently sctx is memory bleed */
16121b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
16133014e516SBarry Smith   if (!sctx) {
16143014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
16153014e516SBarry Smith   }
16163014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
16171b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
16183014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
16193014e516SBarry Smith   PetscFunctionReturn(0);
16203014e516SBarry Smith }
162123f975d1SBarry Smith #endif
1622