xref: /petsc/src/dm/interface/dm.c (revision 732e2eb99afef1b984167973f126bbed28c96039)
147c6ae99SBarry Smith 
2c6db04a5SJed Brown #include <private/dmimpl.h>     /*I      "petscdm.h"     I*/
347c6ae99SBarry Smith 
4*732e2eb9SMatthew 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 @*/
1277087cfbeSBarry Smith PetscErrorCode  DMDestroy(DM dm)
12847c6ae99SBarry Smith {
129*732e2eb9SMatthew G Knepley   PetscInt       i, cnt = 0;
13047c6ae99SBarry Smith   PetscErrorCode ierr;
13147c6ae99SBarry Smith 
13247c6ae99SBarry Smith   PetscFunctionBegin;
133*732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
134*732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
135*732e2eb9SMatthew G Knepley     if (dm->localin[i])  {cnt++;}
136*732e2eb9SMatthew G Knepley     if (dm->globalin[i]) {cnt++;}
137*732e2eb9SMatthew G Knepley   }
138*732e2eb9SMatthew G Knepley 
139*732e2eb9SMatthew G Knepley   if (--((PetscObject)dm)->refct - cnt > 0) PetscFunctionReturn(0);
140*732e2eb9SMatthew G Knepley   /*
141*732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
142*732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
143*732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
144*732e2eb9SMatthew G Knepley   */
145*732e2eb9SMatthew G Knepley   if (((PetscObject)dm)->refct < 0) PetscFunctionReturn(0);
146*732e2eb9SMatthew G Knepley   ((PetscObject) dm)->refct = 0;
147*732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
148*732e2eb9SMatthew G Knepley     if (dm->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
149*732e2eb9SMatthew G Knepley     if (dm->localin[i]) {ierr = VecDestroy(dm->localin[i]);CHKERRQ(ierr);}
150*732e2eb9SMatthew G Knepley     if (dm->globalout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a global vector obtained with DMGetGlobalVector()");
151*732e2eb9SMatthew G Knepley     if (dm->globalin[i]) {ierr = VecDestroy(dm->globalin[i]);CHKERRQ(ierr);}
152*732e2eb9SMatthew G Knepley   }
153*732e2eb9SMatthew G Knepley   if (dm->ltogmap)  {ierr = ISLocalToGlobalMappingDestroy(dm->ltogmap);CHKERRQ(ierr);}
154*732e2eb9SMatthew G Knepley   if (dm->ltogmapb) {ierr = ISLocalToGlobalMappingDestroy(dm->ltogmapb);CHKERRQ(ierr);}
155*732e2eb9SMatthew G Knepley 
156*732e2eb9SMatthew G Knepley   if (dm->vectype)  {ierr = PetscFree(dm->vectype);CHKERRQ(ierr);}
157*732e2eb9SMatthew G Knepley 
158*732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
159*732e2eb9SMatthew G Knepley   ierr = PetscObjectDepublish(dm);CHKERRQ(ierr);
160*732e2eb9SMatthew G Knepley 
1610c010503SBarry Smith   ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
162*732e2eb9SMatthew G Knepley   ierr = PetscFree(dm->data);CHKERRQ(ierr);
163*732e2eb9SMatthew G Knepley 
164*732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
16547c6ae99SBarry Smith   PetscFunctionReturn(0);
16647c6ae99SBarry Smith }
16747c6ae99SBarry Smith 
16847c6ae99SBarry Smith #undef __FUNCT__
169d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
170d7bf68aeSBarry Smith /*@
171d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
172d7bf68aeSBarry Smith 
173d7bf68aeSBarry Smith     Collective on DM
174d7bf68aeSBarry Smith 
175d7bf68aeSBarry Smith     Input Parameter:
176d7bf68aeSBarry Smith .   dm - the DM object to setup
177d7bf68aeSBarry Smith 
178d7bf68aeSBarry Smith     Level: developer
179d7bf68aeSBarry Smith 
180d7bf68aeSBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
181d7bf68aeSBarry Smith 
182d7bf68aeSBarry Smith @*/
1837087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
184d7bf68aeSBarry Smith {
185d7bf68aeSBarry Smith   PetscErrorCode ierr;
186d7bf68aeSBarry Smith 
187d7bf68aeSBarry Smith   PetscFunctionBegin;
188d7bf68aeSBarry Smith   if (dm->ops->setup) {
189d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
190d7bf68aeSBarry Smith   }
191d7bf68aeSBarry Smith   PetscFunctionReturn(0);
192d7bf68aeSBarry Smith }
193d7bf68aeSBarry Smith 
194d7bf68aeSBarry Smith #undef __FUNCT__
195d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
196d7bf68aeSBarry Smith /*@
197d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
198d7bf68aeSBarry Smith 
199d7bf68aeSBarry Smith     Collective on DM
200d7bf68aeSBarry Smith 
201d7bf68aeSBarry Smith     Input Parameter:
202d7bf68aeSBarry Smith .   dm - the DM object to set options for
203d7bf68aeSBarry Smith 
204*732e2eb9SMatthew G Knepley     Options Database:
205*732e2eb9SMatthew G Knepley .   -dm_preallocate_only: Only preallocate the matrix for DMGetMatrix(), but do not fill it with zeros
206*732e2eb9SMatthew G Knepley 
207d7bf68aeSBarry Smith     Level: developer
208d7bf68aeSBarry Smith 
209d7bf68aeSBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
210d7bf68aeSBarry Smith 
211d7bf68aeSBarry Smith @*/
2127087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
213d7bf68aeSBarry Smith {
214d7bf68aeSBarry Smith   PetscErrorCode ierr;
215d7bf68aeSBarry Smith 
216d7bf68aeSBarry Smith   PetscFunctionBegin;
217*732e2eb9SMatthew G Knepley   ierr = PetscOptionsGetBool(((PetscObject) dm)->prefix, "-dm_preallocate_only", &dm->prealloc_only, PETSC_NULL);CHKERRQ(ierr);
218d7bf68aeSBarry Smith   if (dm->ops->setfromoptions) {
219d7bf68aeSBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
220d7bf68aeSBarry Smith   }
221d7bf68aeSBarry Smith   PetscFunctionReturn(0);
222d7bf68aeSBarry Smith }
223d7bf68aeSBarry Smith 
224d7bf68aeSBarry Smith #undef __FUNCT__
22547c6ae99SBarry Smith #define __FUNCT__ "DMView"
226fc9bc008SSatish Balay /*@C
227aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
22847c6ae99SBarry Smith 
22947c6ae99SBarry Smith     Collective on DM
23047c6ae99SBarry Smith 
23147c6ae99SBarry Smith     Input Parameter:
23247c6ae99SBarry Smith +   dm - the DM object to view
23347c6ae99SBarry Smith -   v - the viewer
23447c6ae99SBarry Smith 
23547c6ae99SBarry Smith     Level: developer
23647c6ae99SBarry Smith 
23747c6ae99SBarry Smith .seealso DMDestroy(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
23847c6ae99SBarry Smith 
23947c6ae99SBarry Smith @*/
2407087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
24147c6ae99SBarry Smith {
24247c6ae99SBarry Smith   PetscErrorCode ierr;
24347c6ae99SBarry Smith 
24447c6ae99SBarry Smith   PetscFunctionBegin;
2453014e516SBarry Smith  if (!v) {
2463014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
2473014e516SBarry Smith   }
2480c010503SBarry Smith   if (dm->ops->view) {
2490c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
25047c6ae99SBarry Smith   }
25147c6ae99SBarry Smith   PetscFunctionReturn(0);
25247c6ae99SBarry Smith }
25347c6ae99SBarry Smith 
25447c6ae99SBarry Smith #undef __FUNCT__
25547c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
25647c6ae99SBarry Smith /*@
257aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
25847c6ae99SBarry Smith 
25947c6ae99SBarry Smith     Collective on DM
26047c6ae99SBarry Smith 
26147c6ae99SBarry Smith     Input Parameter:
26247c6ae99SBarry Smith .   dm - the DM object
26347c6ae99SBarry Smith 
26447c6ae99SBarry Smith     Output Parameter:
26547c6ae99SBarry Smith .   vec - the global vector
26647c6ae99SBarry Smith 
26747c6ae99SBarry Smith     Level: developer
26847c6ae99SBarry Smith 
26947c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
27047c6ae99SBarry Smith 
27147c6ae99SBarry Smith @*/
2727087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
27347c6ae99SBarry Smith {
27447c6ae99SBarry Smith   PetscErrorCode ierr;
27547c6ae99SBarry Smith 
27647c6ae99SBarry Smith   PetscFunctionBegin;
27747c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
27847c6ae99SBarry Smith   PetscFunctionReturn(0);
27947c6ae99SBarry Smith }
28047c6ae99SBarry Smith 
28147c6ae99SBarry Smith #undef __FUNCT__
28247c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
28347c6ae99SBarry Smith /*@
284aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
28547c6ae99SBarry Smith 
28647c6ae99SBarry Smith     Not Collective
28747c6ae99SBarry Smith 
28847c6ae99SBarry Smith     Input Parameter:
28947c6ae99SBarry Smith .   dm - the DM object
29047c6ae99SBarry Smith 
29147c6ae99SBarry Smith     Output Parameter:
29247c6ae99SBarry Smith .   vec - the local vector
29347c6ae99SBarry Smith 
29447c6ae99SBarry Smith     Level: developer
29547c6ae99SBarry Smith 
29647c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
29747c6ae99SBarry Smith 
29847c6ae99SBarry Smith @*/
2997087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
30047c6ae99SBarry Smith {
30147c6ae99SBarry Smith   PetscErrorCode ierr;
30247c6ae99SBarry Smith 
30347c6ae99SBarry Smith   PetscFunctionBegin;
30447c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
30547c6ae99SBarry Smith   PetscFunctionReturn(0);
30647c6ae99SBarry Smith }
30747c6ae99SBarry Smith 
30847c6ae99SBarry Smith #undef __FUNCT__
3091411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
3101411c6eeSJed Brown /*@
3111411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
3121411c6eeSJed Brown 
3131411c6eeSJed Brown    Collective on DM
3141411c6eeSJed Brown 
3151411c6eeSJed Brown    Input Parameter:
3161411c6eeSJed Brown .  dm - the DM that provides the mapping
3171411c6eeSJed Brown 
3181411c6eeSJed Brown    Output Parameter:
3191411c6eeSJed Brown .  ltog - the mapping
3201411c6eeSJed Brown 
3211411c6eeSJed Brown    Level: intermediate
3221411c6eeSJed Brown 
3231411c6eeSJed Brown    Notes:
3241411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
3251411c6eeSJed Brown    MatSetLocalToGlobalMapping().
3261411c6eeSJed Brown 
3271411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
3281411c6eeSJed Brown @*/
3297087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
3301411c6eeSJed Brown {
3311411c6eeSJed Brown   PetscErrorCode ierr;
3321411c6eeSJed Brown 
3331411c6eeSJed Brown   PetscFunctionBegin;
3341411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3351411c6eeSJed Brown   PetscValidPointer(ltog,2);
3361411c6eeSJed Brown   if (!dm->ltogmap) {
3371411c6eeSJed Brown     if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
3381411c6eeSJed Brown     ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
3391411c6eeSJed Brown   }
3401411c6eeSJed Brown   *ltog = dm->ltogmap;
3411411c6eeSJed Brown   PetscFunctionReturn(0);
3421411c6eeSJed Brown }
3431411c6eeSJed Brown 
3441411c6eeSJed Brown #undef __FUNCT__
3451411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
3461411c6eeSJed Brown /*@
3471411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
3481411c6eeSJed Brown 
3491411c6eeSJed Brown    Collective on DM
3501411c6eeSJed Brown 
3511411c6eeSJed Brown    Input Parameter:
3521411c6eeSJed Brown .  da - the distributed array that provides the mapping
3531411c6eeSJed Brown 
3541411c6eeSJed Brown    Output Parameter:
3551411c6eeSJed Brown .  ltog - the block mapping
3561411c6eeSJed Brown 
3571411c6eeSJed Brown    Level: intermediate
3581411c6eeSJed Brown 
3591411c6eeSJed Brown    Notes:
3601411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
3611411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
3621411c6eeSJed Brown 
3631411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
3641411c6eeSJed Brown @*/
3657087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
3661411c6eeSJed Brown {
3671411c6eeSJed Brown   PetscErrorCode ierr;
3681411c6eeSJed Brown 
3691411c6eeSJed Brown   PetscFunctionBegin;
3701411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3711411c6eeSJed Brown   PetscValidPointer(ltog,2);
3721411c6eeSJed Brown   if (!dm->ltogmapb) {
3731411c6eeSJed Brown     PetscInt bs;
3741411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
3751411c6eeSJed Brown     if (bs > 1) {
3761411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
3771411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
3781411c6eeSJed Brown     } else {
3791411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
3801411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
3811411c6eeSJed Brown     }
3821411c6eeSJed Brown   }
3831411c6eeSJed Brown   *ltog = dm->ltogmapb;
3841411c6eeSJed Brown   PetscFunctionReturn(0);
3851411c6eeSJed Brown }
3861411c6eeSJed Brown 
3871411c6eeSJed Brown #undef __FUNCT__
3881411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
3891411c6eeSJed Brown /*@
3901411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
3911411c6eeSJed Brown 
3921411c6eeSJed Brown    Not Collective
3931411c6eeSJed Brown 
3941411c6eeSJed Brown    Input Parameter:
3951411c6eeSJed Brown .  dm - the DM with block structure
3961411c6eeSJed Brown 
3971411c6eeSJed Brown    Output Parameter:
3981411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
3991411c6eeSJed Brown 
4001411c6eeSJed Brown    Level: intermediate
4011411c6eeSJed Brown 
4021411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
4031411c6eeSJed Brown @*/
4047087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
4051411c6eeSJed Brown {
4061411c6eeSJed Brown   PetscFunctionBegin;
4071411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4081411c6eeSJed Brown   PetscValidPointer(bs,2);
4091411c6eeSJed 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");
4101411c6eeSJed Brown   *bs = dm->bs;
4111411c6eeSJed Brown   PetscFunctionReturn(0);
4121411c6eeSJed Brown }
4131411c6eeSJed Brown 
4141411c6eeSJed Brown #undef __FUNCT__
41547c6ae99SBarry Smith #define __FUNCT__ "DMGetInterpolation"
41647c6ae99SBarry Smith /*@
417aa219208SBarry Smith     DMGetInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
41847c6ae99SBarry Smith 
41947c6ae99SBarry Smith     Collective on DM
42047c6ae99SBarry Smith 
42147c6ae99SBarry Smith     Input Parameter:
42247c6ae99SBarry Smith +   dm1 - the DM object
42347c6ae99SBarry Smith -   dm2 - the second, finer DM object
42447c6ae99SBarry Smith 
42547c6ae99SBarry Smith     Output Parameter:
42647c6ae99SBarry Smith +  mat - the interpolation
42747c6ae99SBarry Smith -  vec - the scaling (optional)
42847c6ae99SBarry Smith 
42947c6ae99SBarry Smith     Level: developer
43047c6ae99SBarry Smith 
43147c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetColoring(), DMGetMatrix()
43247c6ae99SBarry Smith 
43347c6ae99SBarry Smith @*/
4347087cfbeSBarry Smith PetscErrorCode  DMGetInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
43547c6ae99SBarry Smith {
43647c6ae99SBarry Smith   PetscErrorCode ierr;
43747c6ae99SBarry Smith 
43847c6ae99SBarry Smith   PetscFunctionBegin;
43947c6ae99SBarry Smith   ierr = (*dm1->ops->getinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
44047c6ae99SBarry Smith   PetscFunctionReturn(0);
44147c6ae99SBarry Smith }
44247c6ae99SBarry Smith 
44347c6ae99SBarry Smith #undef __FUNCT__
44447c6ae99SBarry Smith #define __FUNCT__ "DMGetInjection"
44547c6ae99SBarry Smith /*@
446aa219208SBarry Smith     DMGetInjection - Gets injection matrix between two DMDA or DMComposite objects
44747c6ae99SBarry Smith 
44847c6ae99SBarry Smith     Collective on DM
44947c6ae99SBarry Smith 
45047c6ae99SBarry Smith     Input Parameter:
45147c6ae99SBarry Smith +   dm1 - the DM object
45247c6ae99SBarry Smith -   dm2 - the second, finer DM object
45347c6ae99SBarry Smith 
45447c6ae99SBarry Smith     Output Parameter:
45547c6ae99SBarry Smith .   ctx - the injection
45647c6ae99SBarry Smith 
45747c6ae99SBarry Smith     Level: developer
45847c6ae99SBarry Smith 
45947c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetColoring(), DMGetMatrix(), DMGetInterpolation()
46047c6ae99SBarry Smith 
46147c6ae99SBarry Smith @*/
4627087cfbeSBarry Smith PetscErrorCode  DMGetInjection(DM dm1,DM dm2,VecScatter *ctx)
46347c6ae99SBarry Smith {
46447c6ae99SBarry Smith   PetscErrorCode ierr;
46547c6ae99SBarry Smith 
46647c6ae99SBarry Smith   PetscFunctionBegin;
46747c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
46847c6ae99SBarry Smith   PetscFunctionReturn(0);
46947c6ae99SBarry Smith }
47047c6ae99SBarry Smith 
47147c6ae99SBarry Smith #undef __FUNCT__
47247c6ae99SBarry Smith #define __FUNCT__ "DMGetColoring"
47347c6ae99SBarry Smith /*@
474aa219208SBarry Smith     DMGetColoring - Gets coloring for a DMDA or DMComposite
47547c6ae99SBarry Smith 
47647c6ae99SBarry Smith     Collective on DM
47747c6ae99SBarry Smith 
47847c6ae99SBarry Smith     Input Parameter:
47947c6ae99SBarry Smith +   dm - the DM object
48047c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
48147c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
48247c6ae99SBarry Smith 
48347c6ae99SBarry Smith     Output Parameter:
48447c6ae99SBarry Smith .   coloring - the coloring
48547c6ae99SBarry Smith 
48647c6ae99SBarry Smith     Level: developer
48747c6ae99SBarry Smith 
48847c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetMatrix()
48947c6ae99SBarry Smith 
49047c6ae99SBarry Smith @*/
4917087cfbeSBarry Smith PetscErrorCode  DMGetColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
49247c6ae99SBarry Smith {
49347c6ae99SBarry Smith   PetscErrorCode ierr;
49447c6ae99SBarry Smith 
49547c6ae99SBarry Smith   PetscFunctionBegin;
49647c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
49747c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
49847c6ae99SBarry Smith   PetscFunctionReturn(0);
49947c6ae99SBarry Smith }
50047c6ae99SBarry Smith 
50147c6ae99SBarry Smith #undef __FUNCT__
50247c6ae99SBarry Smith #define __FUNCT__ "DMGetMatrix"
50347c6ae99SBarry Smith /*@C
504aa219208SBarry Smith     DMGetMatrix - Gets empty Jacobian for a DMDA or DMComposite
50547c6ae99SBarry Smith 
50647c6ae99SBarry Smith     Collective on DM
50747c6ae99SBarry Smith 
50847c6ae99SBarry Smith     Input Parameter:
50947c6ae99SBarry Smith +   dm - the DM object
51047c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
51194013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
51247c6ae99SBarry Smith 
51347c6ae99SBarry Smith     Output Parameter:
51447c6ae99SBarry Smith .   mat - the empty Jacobian
51547c6ae99SBarry Smith 
51647c6ae99SBarry Smith     Level: developer
51747c6ae99SBarry Smith 
51894013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
51994013140SBarry Smith        do not need to do it yourself.
52094013140SBarry Smith 
52194013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
522aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
52394013140SBarry Smith 
52494013140SBarry 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
52594013140SBarry Smith        internally by PETSc.
52694013140SBarry Smith 
52794013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
528aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
52994013140SBarry Smith 
53047c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetMatrix()
53147c6ae99SBarry Smith 
53247c6ae99SBarry Smith @*/
5337087cfbeSBarry Smith PetscErrorCode  DMGetMatrix(DM dm,const MatType mtype,Mat *mat)
53447c6ae99SBarry Smith {
53547c6ae99SBarry Smith   PetscErrorCode ierr;
536c7b7c8a4SJed Brown   char           ttype[256];
537c7b7c8a4SJed Brown   PetscBool      flg;
53847c6ae99SBarry Smith 
53947c6ae99SBarry Smith   PetscFunctionBegin;
540c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
541c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
542c7b7c8a4SJed Brown   ierr = PetscStrncpy(ttype,mtype,sizeof ttype);CHKERRQ(ierr);
543c7b7c8a4SJed Brown   ttype[sizeof ttype-1] = 0;
544c7b7c8a4SJed Brown   ierr = PetscOptionsBegin(((PetscObject)dm)->comm,((PetscObject)dm)->prefix,"DM options","Mat");CHKERRQ(ierr);
545c7b7c8a4SJed Brown   ierr = PetscOptionsList("-dm_mat_type","Matrix type","MatSetType",MatList,ttype,ttype,sizeof ttype,&flg);CHKERRQ(ierr);
546c7b7c8a4SJed Brown   ierr = PetscOptionsEnd();
547c7b7c8a4SJed Brown   if (flg || mtype) {
548c7b7c8a4SJed Brown     ierr = (*dm->ops->getmatrix)(dm,ttype,mat);CHKERRQ(ierr);
549c7b7c8a4SJed Brown   } else {                      /* Let the implementation decide */
550c7b7c8a4SJed Brown     ierr = (*dm->ops->getmatrix)(dm,PETSC_NULL,mat);CHKERRQ(ierr);
551c7b7c8a4SJed Brown   }
55247c6ae99SBarry Smith   PetscFunctionReturn(0);
55347c6ae99SBarry Smith }
55447c6ae99SBarry Smith 
55547c6ae99SBarry Smith #undef __FUNCT__
556*732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
557*732e2eb9SMatthew G Knepley /*@
558*732e2eb9SMatthew G Knepley   DMSetMatrixPreallocateOnly - When DMGetMatrix() is called the matrix will be properly
559*732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
560*732e2eb9SMatthew G Knepley 
561*732e2eb9SMatthew G Knepley   Logically Collective on DMDA
562*732e2eb9SMatthew G Knepley 
563*732e2eb9SMatthew G Knepley   Input Parameter:
564*732e2eb9SMatthew G Knepley + dm - the DM
565*732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
566*732e2eb9SMatthew G Knepley 
567*732e2eb9SMatthew G Knepley   Level: developer
568*732e2eb9SMatthew G Knepley .seealso DMGetMatrix()
569*732e2eb9SMatthew G Knepley @*/
570*732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
571*732e2eb9SMatthew G Knepley {
572*732e2eb9SMatthew G Knepley   PetscFunctionBegin;
573*732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
574*732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
575*732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
576*732e2eb9SMatthew G Knepley }
577*732e2eb9SMatthew G Knepley 
578*732e2eb9SMatthew G Knepley #undef __FUNCT__
57947c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
58047c6ae99SBarry Smith /*@
58147c6ae99SBarry Smith     DMRefine - Refines a DM object
58247c6ae99SBarry Smith 
58347c6ae99SBarry Smith     Collective on DM
58447c6ae99SBarry Smith 
58547c6ae99SBarry Smith     Input Parameter:
58647c6ae99SBarry Smith +   dm - the DM object
58747c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
58847c6ae99SBarry Smith 
58947c6ae99SBarry Smith     Output Parameter:
59047c6ae99SBarry Smith .   dmf - the refined DM
59147c6ae99SBarry Smith 
59247c6ae99SBarry Smith     Level: developer
59347c6ae99SBarry Smith 
59447c6ae99SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
59547c6ae99SBarry Smith 
59647c6ae99SBarry Smith @*/
5977087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
59847c6ae99SBarry Smith {
59947c6ae99SBarry Smith   PetscErrorCode ierr;
60047c6ae99SBarry Smith 
60147c6ae99SBarry Smith   PetscFunctionBegin;
602*732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60347c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
60447c6ae99SBarry Smith   PetscFunctionReturn(0);
60547c6ae99SBarry Smith }
60647c6ae99SBarry Smith 
60747c6ae99SBarry Smith #undef __FUNCT__
60847c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
60947c6ae99SBarry Smith /*@
61047c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
61147c6ae99SBarry Smith 
61247c6ae99SBarry Smith     Neighbor-wise Collective on DM
61347c6ae99SBarry Smith 
61447c6ae99SBarry Smith     Input Parameters:
61547c6ae99SBarry Smith +   dm - the DM object
61647c6ae99SBarry Smith .   g - the global vector
61747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
61847c6ae99SBarry Smith -   l - the local vector
61947c6ae99SBarry Smith 
62047c6ae99SBarry Smith 
62147c6ae99SBarry Smith     Level: beginner
62247c6ae99SBarry Smith 
6239a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith @*/
6267087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
62747c6ae99SBarry Smith {
62847c6ae99SBarry Smith   PetscErrorCode ierr;
62947c6ae99SBarry Smith 
63047c6ae99SBarry Smith   PetscFunctionBegin;
63147c6ae99SBarry Smith   ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode,l);CHKERRQ(ierr);
63247c6ae99SBarry Smith   PetscFunctionReturn(0);
63347c6ae99SBarry Smith }
63447c6ae99SBarry Smith 
63547c6ae99SBarry Smith #undef __FUNCT__
63647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
63747c6ae99SBarry Smith /*@
63847c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
63947c6ae99SBarry Smith 
64047c6ae99SBarry Smith     Neighbor-wise Collective on DM
64147c6ae99SBarry Smith 
64247c6ae99SBarry Smith     Input Parameters:
64347c6ae99SBarry Smith +   dm - the DM object
64447c6ae99SBarry Smith .   g - the global vector
64547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
64647c6ae99SBarry Smith -   l - the local vector
64747c6ae99SBarry Smith 
64847c6ae99SBarry Smith 
64947c6ae99SBarry Smith     Level: beginner
65047c6ae99SBarry Smith 
6519a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
65247c6ae99SBarry Smith 
65347c6ae99SBarry Smith @*/
6547087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
65547c6ae99SBarry Smith {
65647c6ae99SBarry Smith   PetscErrorCode ierr;
65747c6ae99SBarry Smith 
65847c6ae99SBarry Smith   PetscFunctionBegin;
65947c6ae99SBarry Smith   ierr = (*dm->ops->globaltolocalend)(dm,g,mode,l);CHKERRQ(ierr);
66047c6ae99SBarry Smith   PetscFunctionReturn(0);
66147c6ae99SBarry Smith }
66247c6ae99SBarry Smith 
66347c6ae99SBarry Smith #undef __FUNCT__
6649a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
66547c6ae99SBarry Smith /*@
6669a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
6679a42bb27SBarry Smith 
6689a42bb27SBarry Smith     Neighbor-wise Collective on DM
6699a42bb27SBarry Smith 
6709a42bb27SBarry Smith     Input Parameters:
6719a42bb27SBarry Smith +   dm - the DM object
672f6813fd5SJed Brown .   l - the local vector
6739a42bb27SBarry 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
6749a42bb27SBarry Smith            base point.
675f6813fd5SJed Brown - - the global vector
6769a42bb27SBarry Smith 
6779a42bb27SBarry 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
6789a42bb27SBarry 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
6799a42bb27SBarry Smith            global array to the final global array with VecAXPY().
6809a42bb27SBarry Smith 
6819a42bb27SBarry Smith     Level: beginner
6829a42bb27SBarry Smith 
6839a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
6849a42bb27SBarry Smith 
6859a42bb27SBarry Smith @*/
6867087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
6879a42bb27SBarry Smith {
6889a42bb27SBarry Smith   PetscErrorCode ierr;
6899a42bb27SBarry Smith 
6909a42bb27SBarry Smith   PetscFunctionBegin;
691f6813fd5SJed Brown   ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode,g);CHKERRQ(ierr);
6929a42bb27SBarry Smith   PetscFunctionReturn(0);
6939a42bb27SBarry Smith }
6949a42bb27SBarry Smith 
6959a42bb27SBarry Smith #undef __FUNCT__
6969a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
6979a42bb27SBarry Smith /*@
6989a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
69947c6ae99SBarry Smith 
70047c6ae99SBarry Smith     Neighbor-wise Collective on DM
70147c6ae99SBarry Smith 
70247c6ae99SBarry Smith     Input Parameters:
70347c6ae99SBarry Smith +   dm - the DM object
704f6813fd5SJed Brown .   l - the local vector
70547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
706f6813fd5SJed Brown -   g - the global vector
70747c6ae99SBarry Smith 
70847c6ae99SBarry Smith 
70947c6ae99SBarry Smith     Level: beginner
71047c6ae99SBarry Smith 
7119a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
71247c6ae99SBarry Smith 
71347c6ae99SBarry Smith @*/
7147087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
71547c6ae99SBarry Smith {
71647c6ae99SBarry Smith   PetscErrorCode ierr;
71747c6ae99SBarry Smith 
71847c6ae99SBarry Smith   PetscFunctionBegin;
719f6813fd5SJed Brown   ierr = (*dm->ops->localtoglobalend)(dm,l,mode,g);CHKERRQ(ierr);
72047c6ae99SBarry Smith   PetscFunctionReturn(0);
72147c6ae99SBarry Smith }
72247c6ae99SBarry Smith 
72347c6ae99SBarry Smith #undef __FUNCT__
72447c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
72547c6ae99SBarry Smith /*@
72647c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
72747c6ae99SBarry Smith 
72847c6ae99SBarry Smith     Collective on DM
72947c6ae99SBarry Smith 
73047c6ae99SBarry Smith     Input Parameter:
73147c6ae99SBarry Smith +   dm - the DM object
73247c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
73347c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
73447c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
73547c6ae99SBarry Smith 
73647c6ae99SBarry Smith     Level: developer
73747c6ae99SBarry Smith 
73847c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
73947c6ae99SBarry Smith          DMSetFunction()
74047c6ae99SBarry Smith 
74147c6ae99SBarry Smith @*/
7427087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
74347c6ae99SBarry Smith {
74447c6ae99SBarry Smith   PetscErrorCode ierr;
74547c6ae99SBarry Smith   PetscFunctionBegin;
74647c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
74747c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
74847c6ae99SBarry Smith   if (A != B) {
74947c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
75047c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
75147c6ae99SBarry Smith   }
75247c6ae99SBarry Smith   PetscFunctionReturn(0);
75347c6ae99SBarry Smith }
75447c6ae99SBarry Smith 
75547c6ae99SBarry Smith #undef __FUNCT__
75647c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
75747c6ae99SBarry Smith /*@
75847c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
75947c6ae99SBarry Smith 
76047c6ae99SBarry Smith     Collective on DM
76147c6ae99SBarry Smith 
76247c6ae99SBarry Smith     Input Parameter:
76347c6ae99SBarry Smith +   dm - the DM object
76447c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
76547c6ae99SBarry Smith 
76647c6ae99SBarry Smith     Output Parameter:
76747c6ae99SBarry Smith .   dmc - the coarsened DM
76847c6ae99SBarry Smith 
76947c6ae99SBarry Smith     Level: developer
77047c6ae99SBarry Smith 
77147c6ae99SBarry Smith .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
77247c6ae99SBarry Smith 
77347c6ae99SBarry Smith @*/
7747087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
77547c6ae99SBarry Smith {
77647c6ae99SBarry Smith   PetscErrorCode ierr;
77747c6ae99SBarry Smith 
77847c6ae99SBarry Smith   PetscFunctionBegin;
77947c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
78047c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
78147c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
78247c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
78347c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
78447c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
78547c6ae99SBarry Smith   }
78647c6ae99SBarry Smith   PetscFunctionReturn(0);
78747c6ae99SBarry Smith }
78847c6ae99SBarry Smith 
78947c6ae99SBarry Smith #undef __FUNCT__
79047c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
79147c6ae99SBarry Smith /*@C
79247c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
79347c6ae99SBarry Smith 
79447c6ae99SBarry Smith     Collective on DM
79547c6ae99SBarry Smith 
79647c6ae99SBarry Smith     Input Parameter:
79747c6ae99SBarry Smith +   dm - the DM object
79847c6ae99SBarry Smith -   nlevels - the number of levels of refinement
79947c6ae99SBarry Smith 
80047c6ae99SBarry Smith     Output Parameter:
80147c6ae99SBarry Smith .   dmf - the refined DM hierarchy
80247c6ae99SBarry Smith 
80347c6ae99SBarry Smith     Level: developer
80447c6ae99SBarry Smith 
80547c6ae99SBarry Smith .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
80647c6ae99SBarry Smith 
80747c6ae99SBarry Smith @*/
8087087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
80947c6ae99SBarry Smith {
81047c6ae99SBarry Smith   PetscErrorCode ierr;
81147c6ae99SBarry Smith 
81247c6ae99SBarry Smith   PetscFunctionBegin;
81347c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
81447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
81547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
81647c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
81747c6ae99SBarry Smith   } else if (dm->ops->refine) {
81847c6ae99SBarry Smith     PetscInt i;
81947c6ae99SBarry Smith 
82047c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
82147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
82247c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
82347c6ae99SBarry Smith     }
82447c6ae99SBarry Smith   } else {
82547c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
82647c6ae99SBarry Smith   }
82747c6ae99SBarry Smith   PetscFunctionReturn(0);
82847c6ae99SBarry Smith }
82947c6ae99SBarry Smith 
83047c6ae99SBarry Smith #undef __FUNCT__
83147c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
83247c6ae99SBarry Smith /*@C
83347c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
83447c6ae99SBarry Smith 
83547c6ae99SBarry Smith     Collective on DM
83647c6ae99SBarry Smith 
83747c6ae99SBarry Smith     Input Parameter:
83847c6ae99SBarry Smith +   dm - the DM object
83947c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
84047c6ae99SBarry Smith 
84147c6ae99SBarry Smith     Output Parameter:
84247c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
84347c6ae99SBarry Smith 
84447c6ae99SBarry Smith     Level: developer
84547c6ae99SBarry Smith 
84647c6ae99SBarry Smith .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
84747c6ae99SBarry Smith 
84847c6ae99SBarry Smith @*/
8497087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
85047c6ae99SBarry Smith {
85147c6ae99SBarry Smith   PetscErrorCode ierr;
85247c6ae99SBarry Smith 
85347c6ae99SBarry Smith   PetscFunctionBegin;
85447c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
85547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
85647c6ae99SBarry Smith   PetscValidPointer(dmc,3);
85747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
85847c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
85947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
86047c6ae99SBarry Smith     PetscInt i;
86147c6ae99SBarry Smith 
86247c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
86347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
86447c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
86547c6ae99SBarry Smith     }
86647c6ae99SBarry Smith   } else {
86747c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
86847c6ae99SBarry Smith   }
86947c6ae99SBarry Smith   PetscFunctionReturn(0);
87047c6ae99SBarry Smith }
87147c6ae99SBarry Smith 
87247c6ae99SBarry Smith #undef __FUNCT__
87347c6ae99SBarry Smith #define __FUNCT__ "DMGetAggregates"
87447c6ae99SBarry Smith /*@
87547c6ae99SBarry Smith    DMGetAggregates - Gets the aggregates that map between
87647c6ae99SBarry Smith    grids associated with two DMs.
87747c6ae99SBarry Smith 
87847c6ae99SBarry Smith    Collective on DM
87947c6ae99SBarry Smith 
88047c6ae99SBarry Smith    Input Parameters:
88147c6ae99SBarry Smith +  dmc - the coarse grid DM
88247c6ae99SBarry Smith -  dmf - the fine grid DM
88347c6ae99SBarry Smith 
88447c6ae99SBarry Smith    Output Parameters:
88547c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
88647c6ae99SBarry Smith 
88747c6ae99SBarry Smith    Level: intermediate
88847c6ae99SBarry Smith 
88947c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
89047c6ae99SBarry Smith 
89147c6ae99SBarry Smith .seealso: DMRefine(), DMGetInjection(), DMGetInterpolation()
89247c6ae99SBarry Smith @*/
8937087cfbeSBarry Smith PetscErrorCode  DMGetAggregates(DM dmc, DM dmf, Mat *rest)
89447c6ae99SBarry Smith {
89547c6ae99SBarry Smith   PetscErrorCode ierr;
89647c6ae99SBarry Smith 
89747c6ae99SBarry Smith   PetscFunctionBegin;
89847c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
89947c6ae99SBarry Smith   PetscFunctionReturn(0);
90047c6ae99SBarry Smith }
90147c6ae99SBarry Smith 
90247c6ae99SBarry Smith #undef __FUNCT__
90347c6ae99SBarry Smith #define __FUNCT__ "DMSetContext"
90447c6ae99SBarry Smith /*@
90547c6ae99SBarry Smith     DMSetContext - Set a user context into a DM object
90647c6ae99SBarry Smith 
90747c6ae99SBarry Smith     Not Collective
90847c6ae99SBarry Smith 
90947c6ae99SBarry Smith     Input Parameters:
91047c6ae99SBarry Smith +   dm - the DM object
91147c6ae99SBarry Smith -   ctx - the user context
91247c6ae99SBarry Smith 
91347c6ae99SBarry Smith     Level: intermediate
91447c6ae99SBarry Smith 
91547c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext()
91647c6ae99SBarry Smith 
91747c6ae99SBarry Smith @*/
9187087cfbeSBarry Smith PetscErrorCode  DMSetContext(DM dm,void *ctx)
91947c6ae99SBarry Smith {
92047c6ae99SBarry Smith   PetscFunctionBegin;
92147c6ae99SBarry Smith   dm->ctx = ctx;
92247c6ae99SBarry Smith   PetscFunctionReturn(0);
92347c6ae99SBarry Smith }
92447c6ae99SBarry Smith 
92547c6ae99SBarry Smith #undef __FUNCT__
92647c6ae99SBarry Smith #define __FUNCT__ "DMGetContext"
92747c6ae99SBarry Smith /*@
92847c6ae99SBarry Smith     DMGetContext - Gets a user context from a DM object
92947c6ae99SBarry Smith 
93047c6ae99SBarry Smith     Not Collective
93147c6ae99SBarry Smith 
93247c6ae99SBarry Smith     Input Parameter:
93347c6ae99SBarry Smith .   dm - the DM object
93447c6ae99SBarry Smith 
93547c6ae99SBarry Smith     Output Parameter:
93647c6ae99SBarry Smith .   ctx - the user context
93747c6ae99SBarry Smith 
93847c6ae99SBarry Smith     Level: intermediate
93947c6ae99SBarry Smith 
94047c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext()
94147c6ae99SBarry Smith 
94247c6ae99SBarry Smith @*/
9437087cfbeSBarry Smith PetscErrorCode  DMGetContext(DM dm,void **ctx)
94447c6ae99SBarry Smith {
94547c6ae99SBarry Smith   PetscFunctionBegin;
94647c6ae99SBarry Smith   *ctx = dm->ctx;
94747c6ae99SBarry Smith   PetscFunctionReturn(0);
94847c6ae99SBarry Smith }
94947c6ae99SBarry Smith 
95047c6ae99SBarry Smith #undef __FUNCT__
95147c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
95247c6ae99SBarry Smith /*@
95347c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
95447c6ae99SBarry Smith 
95547c6ae99SBarry Smith     Logically Collective on DM
95647c6ae99SBarry Smith 
95747c6ae99SBarry Smith     Input Parameter:
95847c6ae99SBarry Smith +   dm - the DM object to destroy
95947c6ae99SBarry Smith -   f - the function to compute the initial guess
96047c6ae99SBarry Smith 
96147c6ae99SBarry Smith     Level: intermediate
96247c6ae99SBarry Smith 
96347c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
96447c6ae99SBarry Smith 
96547c6ae99SBarry Smith @*/
9667087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
96747c6ae99SBarry Smith {
96847c6ae99SBarry Smith   PetscFunctionBegin;
96947c6ae99SBarry Smith   dm->ops->initialguess = f;
97047c6ae99SBarry Smith   PetscFunctionReturn(0);
97147c6ae99SBarry Smith }
97247c6ae99SBarry Smith 
97347c6ae99SBarry Smith #undef __FUNCT__
97447c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
97547c6ae99SBarry Smith /*@
97647c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
97747c6ae99SBarry Smith 
97847c6ae99SBarry Smith     Logically Collective on DM
97947c6ae99SBarry Smith 
98047c6ae99SBarry Smith     Input Parameter:
98147c6ae99SBarry Smith +   dm - the DM object
98247c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
98347c6ae99SBarry Smith 
98447c6ae99SBarry Smith     Level: intermediate
98547c6ae99SBarry Smith 
98647c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
98747c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
98847c6ae99SBarry Smith 
98947c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
99047c6ae99SBarry Smith          DMSetJacobian()
99147c6ae99SBarry Smith 
99247c6ae99SBarry Smith @*/
9937087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
99447c6ae99SBarry Smith {
99547c6ae99SBarry Smith   PetscFunctionBegin;
99647c6ae99SBarry Smith   dm->ops->function = f;
99747c6ae99SBarry Smith   if (f) {
99847c6ae99SBarry Smith     dm->ops->functionj = f;
99947c6ae99SBarry Smith   }
100047c6ae99SBarry Smith   PetscFunctionReturn(0);
100147c6ae99SBarry Smith }
100247c6ae99SBarry Smith 
100347c6ae99SBarry Smith #undef __FUNCT__
100447c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
100547c6ae99SBarry Smith /*@
100647c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
100747c6ae99SBarry Smith 
100847c6ae99SBarry Smith     Logically Collective on DM
100947c6ae99SBarry Smith 
101047c6ae99SBarry Smith     Input Parameter:
101147c6ae99SBarry Smith +   dm - the DM object to destroy
101247c6ae99SBarry Smith -   f - the function to compute the matrix entries
101347c6ae99SBarry Smith 
101447c6ae99SBarry Smith     Level: intermediate
101547c6ae99SBarry Smith 
101647c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
101747c6ae99SBarry Smith          DMSetFunction()
101847c6ae99SBarry Smith 
101947c6ae99SBarry Smith @*/
10207087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
102147c6ae99SBarry Smith {
102247c6ae99SBarry Smith   PetscFunctionBegin;
102347c6ae99SBarry Smith   dm->ops->jacobian = f;
102447c6ae99SBarry Smith   PetscFunctionReturn(0);
102547c6ae99SBarry Smith }
102647c6ae99SBarry Smith 
102747c6ae99SBarry Smith #undef __FUNCT__
102847c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
102947c6ae99SBarry Smith /*@
103047c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
103147c6ae99SBarry Smith 
103247c6ae99SBarry Smith     Collective on DM
103347c6ae99SBarry Smith 
103447c6ae99SBarry Smith     Input Parameter:
103547c6ae99SBarry Smith +   dm - the DM object to destroy
103647c6ae99SBarry Smith -   x - the vector to hold the initial guess values
103747c6ae99SBarry Smith 
103847c6ae99SBarry Smith     Level: developer
103947c6ae99SBarry Smith 
104047c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetRhs(), DMSetMat()
104147c6ae99SBarry Smith 
104247c6ae99SBarry Smith @*/
10437087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
104447c6ae99SBarry Smith {
104547c6ae99SBarry Smith   PetscErrorCode ierr;
104647c6ae99SBarry Smith   PetscFunctionBegin;
104747c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
104847c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
104947c6ae99SBarry Smith   PetscFunctionReturn(0);
105047c6ae99SBarry Smith }
105147c6ae99SBarry Smith 
105247c6ae99SBarry Smith #undef __FUNCT__
105347c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
105447c6ae99SBarry Smith /*@
105547c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
105647c6ae99SBarry Smith 
105747c6ae99SBarry Smith     Not Collective
105847c6ae99SBarry Smith 
105947c6ae99SBarry Smith     Input Parameter:
106047c6ae99SBarry Smith .   dm - the DM object to destroy
106147c6ae99SBarry Smith 
106247c6ae99SBarry Smith     Output Parameter:
106347c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
106447c6ae99SBarry Smith 
106547c6ae99SBarry Smith     Level: developer
106647c6ae99SBarry Smith 
106747c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
106847c6ae99SBarry Smith 
106947c6ae99SBarry Smith @*/
10707087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
107147c6ae99SBarry Smith {
107247c6ae99SBarry Smith   PetscFunctionBegin;
107347c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
107447c6ae99SBarry Smith   PetscFunctionReturn(0);
107547c6ae99SBarry Smith }
107647c6ae99SBarry Smith 
107747c6ae99SBarry Smith #undef __FUNCT__
107847c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
107947c6ae99SBarry Smith /*@
108047c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
108147c6ae99SBarry Smith 
108247c6ae99SBarry Smith     Not Collective
108347c6ae99SBarry Smith 
108447c6ae99SBarry Smith     Input Parameter:
108547c6ae99SBarry Smith .   dm - the DM object to destroy
108647c6ae99SBarry Smith 
108747c6ae99SBarry Smith     Output Parameter:
108847c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
108947c6ae99SBarry Smith 
109047c6ae99SBarry Smith     Level: developer
109147c6ae99SBarry Smith 
109247c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
109347c6ae99SBarry Smith 
109447c6ae99SBarry Smith @*/
10957087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
109647c6ae99SBarry Smith {
109747c6ae99SBarry Smith   PetscFunctionBegin;
109847c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
109947c6ae99SBarry Smith   PetscFunctionReturn(0);
110047c6ae99SBarry Smith }
110147c6ae99SBarry Smith 
110247c6ae99SBarry Smith #undef __FUNCT__
110347c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
110447c6ae99SBarry Smith /*@
110547c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
110647c6ae99SBarry Smith 
110747c6ae99SBarry Smith     Not Collective
110847c6ae99SBarry Smith 
110947c6ae99SBarry Smith     Input Parameter:
111047c6ae99SBarry Smith .   dm - the DM object to destroy
111147c6ae99SBarry Smith 
111247c6ae99SBarry Smith     Output Parameter:
111347c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
111447c6ae99SBarry Smith 
111547c6ae99SBarry Smith     Level: developer
111647c6ae99SBarry Smith 
111747c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
111847c6ae99SBarry Smith 
111947c6ae99SBarry Smith @*/
11207087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
112147c6ae99SBarry Smith {
112247c6ae99SBarry Smith   PetscFunctionBegin;
112347c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
112447c6ae99SBarry Smith   PetscFunctionReturn(0);
112547c6ae99SBarry Smith }
112647c6ae99SBarry Smith 
112747c6ae99SBarry Smith #undef __FUNCT__
112847c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
112947c6ae99SBarry Smith /*@
113047c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
113147c6ae99SBarry Smith 
113247c6ae99SBarry Smith     Collective on DM
113347c6ae99SBarry Smith 
113447c6ae99SBarry Smith     Input Parameter:
113547c6ae99SBarry Smith +   dm - the DM object to destroy
113647c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
113747c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
113847c6ae99SBarry Smith 
113947c6ae99SBarry Smith     Level: developer
114047c6ae99SBarry Smith 
114147c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
114247c6ae99SBarry Smith          DMSetJacobian()
114347c6ae99SBarry Smith 
114447c6ae99SBarry Smith @*/
11457087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
114647c6ae99SBarry Smith {
114747c6ae99SBarry Smith   PetscErrorCode ierr;
114847c6ae99SBarry Smith   PetscFunctionBegin;
114947c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
115047c6ae99SBarry Smith   if (!x) x = dm->x;
115147c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
115247c6ae99SBarry Smith   PetscFunctionReturn(0);
115347c6ae99SBarry Smith }
115447c6ae99SBarry Smith 
115547c6ae99SBarry Smith 
115647c6ae99SBarry Smith #undef __FUNCT__
115747c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
115847c6ae99SBarry Smith /*@
115947c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
116047c6ae99SBarry Smith 
116147c6ae99SBarry Smith     Collective on DM
116247c6ae99SBarry Smith 
116347c6ae99SBarry Smith     Input Parameter:
116447c6ae99SBarry Smith +   dm - the DM object
116547c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
116647c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
116747c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
116847c6ae99SBarry Smith 
116947c6ae99SBarry Smith     Level: developer
117047c6ae99SBarry Smith 
117147c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
117247c6ae99SBarry Smith          DMSetFunction()
117347c6ae99SBarry Smith 
117447c6ae99SBarry Smith @*/
11757087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
117647c6ae99SBarry Smith {
117747c6ae99SBarry Smith   PetscErrorCode ierr;
117847c6ae99SBarry Smith 
117947c6ae99SBarry Smith   PetscFunctionBegin;
118047c6ae99SBarry Smith   if (!dm->ops->jacobian) {
118147c6ae99SBarry Smith     ISColoring    coloring;
118247c6ae99SBarry Smith     MatFDColoring fd;
118347c6ae99SBarry Smith 
118447c6ae99SBarry Smith     ierr = DMGetColoring(dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr);
118547c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
118647c6ae99SBarry Smith     ierr = ISColoringDestroy(coloring);CHKERRQ(ierr);
118747c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
118847c6ae99SBarry Smith     dm->fd = fd;
118947c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
119047c6ae99SBarry Smith 
119147c6ae99SBarry Smith     if (!dm->x) {
119247c6ae99SBarry Smith       ierr = MatGetVecs(B,&dm->x,PETSC_NULL);CHKERRQ(ierr);
119347c6ae99SBarry Smith     }
119447c6ae99SBarry Smith   }
119547c6ae99SBarry Smith   if (!x) x = dm->x;
119647c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
119747c6ae99SBarry Smith   PetscFunctionReturn(0);
119847c6ae99SBarry Smith }
1199264ace61SBarry Smith 
1200264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
1201264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
1202264ace61SBarry Smith 
1203264ace61SBarry Smith #undef __FUNCT__
1204264ace61SBarry Smith #define __FUNCT__ "DMSetType"
1205264ace61SBarry Smith /*@C
1206264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
1207264ace61SBarry Smith 
1208264ace61SBarry Smith   Collective on DM
1209264ace61SBarry Smith 
1210264ace61SBarry Smith   Input Parameters:
1211264ace61SBarry Smith + dm     - The DM object
1212264ace61SBarry Smith - method - The name of the DM type
1213264ace61SBarry Smith 
1214264ace61SBarry Smith   Options Database Key:
1215264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
1216264ace61SBarry Smith 
1217264ace61SBarry Smith   Notes:
1218e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
1219264ace61SBarry Smith 
1220264ace61SBarry Smith   Level: intermediate
1221264ace61SBarry Smith 
1222264ace61SBarry Smith .keywords: DM, set, type
1223264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
1224264ace61SBarry Smith @*/
12257087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
1226264ace61SBarry Smith {
1227264ace61SBarry Smith   PetscErrorCode (*r)(DM);
1228264ace61SBarry Smith   PetscBool      match;
1229264ace61SBarry Smith   PetscErrorCode ierr;
1230264ace61SBarry Smith 
1231264ace61SBarry Smith   PetscFunctionBegin;
1232264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1233264ace61SBarry Smith   ierr = PetscTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
1234264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
1235264ace61SBarry Smith 
1236264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
1237264ace61SBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,(void (**)(void)) &r);CHKERRQ(ierr);
1238264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
1239264ace61SBarry Smith 
1240264ace61SBarry Smith   if (dm->ops->destroy) {
1241264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
1242264ace61SBarry Smith   }
1243264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
1244264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
1245264ace61SBarry Smith   PetscFunctionReturn(0);
1246264ace61SBarry Smith }
1247264ace61SBarry Smith 
1248264ace61SBarry Smith #undef __FUNCT__
1249264ace61SBarry Smith #define __FUNCT__ "DMGetType"
1250264ace61SBarry Smith /*@C
1251264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
1252264ace61SBarry Smith 
1253264ace61SBarry Smith   Not Collective
1254264ace61SBarry Smith 
1255264ace61SBarry Smith   Input Parameter:
1256264ace61SBarry Smith . dm  - The DM
1257264ace61SBarry Smith 
1258264ace61SBarry Smith   Output Parameter:
1259264ace61SBarry Smith . type - The DM type name
1260264ace61SBarry Smith 
1261264ace61SBarry Smith   Level: intermediate
1262264ace61SBarry Smith 
1263264ace61SBarry Smith .keywords: DM, get, type, name
1264264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
1265264ace61SBarry Smith @*/
12667087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
1267264ace61SBarry Smith {
1268264ace61SBarry Smith   PetscErrorCode ierr;
1269264ace61SBarry Smith 
1270264ace61SBarry Smith   PetscFunctionBegin;
1271264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1272264ace61SBarry Smith   PetscValidCharPointer(type,2);
1273264ace61SBarry Smith   if (!DMRegisterAllCalled) {
1274264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
1275264ace61SBarry Smith   }
1276264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
1277264ace61SBarry Smith   PetscFunctionReturn(0);
1278264ace61SBarry Smith }
1279264ace61SBarry Smith 
128067a56275SMatthew G Knepley #undef __FUNCT__
128167a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
128267a56275SMatthew G Knepley /*@C
128367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
128467a56275SMatthew G Knepley 
128567a56275SMatthew G Knepley   Collective on DM
128667a56275SMatthew G Knepley 
128767a56275SMatthew G Knepley   Input Parameters:
128867a56275SMatthew G Knepley + dm - the DM
128967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
129067a56275SMatthew G Knepley 
129167a56275SMatthew G Knepley   Output Parameter:
129267a56275SMatthew G Knepley . M - pointer to new DM
129367a56275SMatthew G Knepley 
129467a56275SMatthew G Knepley   Notes:
129567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
129667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
129767a56275SMatthew G Knepley   of the input DM.
129867a56275SMatthew G Knepley 
129967a56275SMatthew G Knepley   Level: intermediate
130067a56275SMatthew G Knepley 
130167a56275SMatthew G Knepley .seealso: DMCreate()
130267a56275SMatthew G Knepley @*/
130367a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
130467a56275SMatthew G Knepley {
130567a56275SMatthew G Knepley   DM             B;
130667a56275SMatthew G Knepley   char           convname[256];
130767a56275SMatthew G Knepley   PetscBool      sametype, issame;
130867a56275SMatthew G Knepley   PetscErrorCode ierr;
130967a56275SMatthew G Knepley 
131067a56275SMatthew G Knepley   PetscFunctionBegin;
131167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
131267a56275SMatthew G Knepley   PetscValidType(dm,1);
131367a56275SMatthew G Knepley   PetscValidPointer(M,3);
131467a56275SMatthew G Knepley   ierr = PetscTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
131567a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
131667a56275SMatthew G Knepley   {
131767a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
131867a56275SMatthew G Knepley 
131967a56275SMatthew G Knepley     /*
132067a56275SMatthew G Knepley        Order of precedence:
132167a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
132267a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
132367a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
132467a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
132567a56275SMatthew G Knepley        5) Use a really basic converter.
132667a56275SMatthew G Knepley     */
132767a56275SMatthew G Knepley 
132867a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
132967a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
133067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
133167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
133267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
133367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
133467a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
133567a56275SMatthew G Knepley     if (conv) goto foundconv;
133667a56275SMatthew G Knepley 
133767a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
133867a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
133967a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
134067a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
134167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
134267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
134367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
134467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
134567a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
134667a56275SMatthew G Knepley     if (conv) {
134767a56275SMatthew G Knepley       ierr = DMDestroy(B);CHKERRQ(ierr);
134867a56275SMatthew G Knepley       goto foundconv;
134967a56275SMatthew G Knepley     }
135067a56275SMatthew G Knepley 
135167a56275SMatthew G Knepley #if 0
135267a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
135367a56275SMatthew G Knepley     conv = B->ops->convertfrom;
135467a56275SMatthew G Knepley     ierr = DMDestroy(B);CHKERRQ(ierr);
135567a56275SMatthew G Knepley     if (conv) goto foundconv;
135667a56275SMatthew G Knepley 
135767a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
135867a56275SMatthew G Knepley     if (dm->ops->convert) {
135967a56275SMatthew G Knepley       conv = dm->ops->convert;
136067a56275SMatthew G Knepley     }
136167a56275SMatthew G Knepley     if (conv) goto foundconv;
136267a56275SMatthew G Knepley #endif
136367a56275SMatthew G Knepley 
136467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
136567a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
136667a56275SMatthew G Knepley 
136767a56275SMatthew G Knepley     foundconv:
136867a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
136967a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
137067a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
137167a56275SMatthew G Knepley   }
137267a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
137367a56275SMatthew G Knepley   PetscFunctionReturn(0);
137467a56275SMatthew G Knepley }
1375264ace61SBarry Smith 
1376264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1377264ace61SBarry Smith 
1378264ace61SBarry Smith #undef __FUNCT__
1379264ace61SBarry Smith #define __FUNCT__ "DMRegister"
1380264ace61SBarry Smith /*@C
1381264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
1382264ace61SBarry Smith 
1383264ace61SBarry Smith   Level: advanced
1384264ace61SBarry Smith @*/
13857087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
1386264ace61SBarry Smith {
1387264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
1388264ace61SBarry Smith   PetscErrorCode ierr;
1389264ace61SBarry Smith 
1390264ace61SBarry Smith   PetscFunctionBegin;
1391264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
1392264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
1393264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
1394264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
1395264ace61SBarry Smith   PetscFunctionReturn(0);
1396264ace61SBarry Smith }
1397264ace61SBarry Smith 
1398264ace61SBarry Smith 
1399264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1400264ace61SBarry Smith #undef __FUNCT__
1401264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
1402264ace61SBarry Smith /*@C
1403264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
1404264ace61SBarry Smith 
1405264ace61SBarry Smith    Not Collective
1406264ace61SBarry Smith 
1407264ace61SBarry Smith    Level: advanced
1408264ace61SBarry Smith 
1409264ace61SBarry Smith .keywords: DM, register, destroy
1410264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
1411264ace61SBarry Smith @*/
14127087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
1413264ace61SBarry Smith {
1414264ace61SBarry Smith   PetscErrorCode ierr;
1415264ace61SBarry Smith 
1416264ace61SBarry Smith   PetscFunctionBegin;
1417264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
1418264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
1419264ace61SBarry Smith   PetscFunctionReturn(0);
1420264ace61SBarry Smith }
142123f975d1SBarry Smith 
142223f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
1423c6db04a5SJed Brown #include <mex.h>
142423f975d1SBarry Smith 
14253014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
142623f975d1SBarry Smith 
142723f975d1SBarry Smith #undef __FUNCT__
142823f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
142923f975d1SBarry Smith /*
143023f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
143123f975d1SBarry Smith                          DMSetFunctionMatlab().
143223f975d1SBarry Smith 
143323f975d1SBarry Smith    For linear problems x is null
143423f975d1SBarry Smith 
143523f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
143623f975d1SBarry Smith */
14377087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
143823f975d1SBarry Smith {
143923f975d1SBarry Smith   PetscErrorCode    ierr;
144023f975d1SBarry Smith   DMMatlabContext   *sctx;
144123f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
144223f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
144323f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
144423f975d1SBarry Smith 
144523f975d1SBarry Smith   PetscFunctionBegin;
144623f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
144723f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
144823f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
144923f975d1SBarry Smith 
145023f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
145123f975d1SBarry Smith   ierr = DMGetContext(dm,(void**)&sctx);CHKERRQ(ierr);
145223f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
145323f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
14543014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
145523f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
145623f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
145723f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
145823f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
1459b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
146023f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
146123f975d1SBarry Smith   mxDestroyArray(prhs[0]);
146223f975d1SBarry Smith   mxDestroyArray(prhs[1]);
146323f975d1SBarry Smith   mxDestroyArray(prhs[2]);
146423f975d1SBarry Smith   mxDestroyArray(prhs[3]);
146523f975d1SBarry Smith   mxDestroyArray(plhs[0]);
146623f975d1SBarry Smith   PetscFunctionReturn(0);
146723f975d1SBarry Smith }
146823f975d1SBarry Smith 
146923f975d1SBarry Smith 
147023f975d1SBarry Smith #undef __FUNCT__
147123f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
147223f975d1SBarry Smith /*
147323f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
147423f975d1SBarry Smith 
147523f975d1SBarry Smith */
14767087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
147723f975d1SBarry Smith {
147823f975d1SBarry Smith   PetscErrorCode    ierr;
147923f975d1SBarry Smith   DMMatlabContext   *sctx;
148023f975d1SBarry Smith 
148123f975d1SBarry Smith   PetscFunctionBegin;
148223f975d1SBarry Smith   /* currently sctx is memory bleed */
14833014e516SBarry Smith   ierr = DMGetContext(dm,(void**)&sctx);CHKERRQ(ierr);
14843014e516SBarry Smith   if (!sctx) {
148523f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
14863014e516SBarry Smith   }
148723f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
148823f975d1SBarry Smith   ierr = DMSetContext(dm,sctx);CHKERRQ(ierr);
148923f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
149023f975d1SBarry Smith   PetscFunctionReturn(0);
149123f975d1SBarry Smith }
14923014e516SBarry Smith 
14933014e516SBarry Smith #undef __FUNCT__
14943014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
14953014e516SBarry Smith /*
14963014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
14973014e516SBarry Smith                          DMSetJacobianMatlab().
14983014e516SBarry Smith 
14993014e516SBarry Smith    For linear problems x is null
15003014e516SBarry Smith 
15013014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
15023014e516SBarry Smith */
15037087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
15043014e516SBarry Smith {
15053014e516SBarry Smith   PetscErrorCode    ierr;
15063014e516SBarry Smith   DMMatlabContext   *sctx;
15073014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
15083014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
15093014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
15103014e516SBarry Smith 
15113014e516SBarry Smith   PetscFunctionBegin;
15123014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15133014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
15143014e516SBarry Smith 
1515e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
15163014e516SBarry Smith   ierr = DMGetContext(dm,(void**)&sctx);CHKERRQ(ierr);
15173014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
15183014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
15193014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
15203014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
15213014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
15223014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
15233014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
15243014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
15253014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
1526b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
1527c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
1528c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
15293014e516SBarry Smith   mxDestroyArray(prhs[0]);
15303014e516SBarry Smith   mxDestroyArray(prhs[1]);
15313014e516SBarry Smith   mxDestroyArray(prhs[2]);
15323014e516SBarry Smith   mxDestroyArray(prhs[3]);
15333014e516SBarry Smith   mxDestroyArray(prhs[4]);
15343014e516SBarry Smith   mxDestroyArray(plhs[0]);
15353014e516SBarry Smith   mxDestroyArray(plhs[1]);
15363014e516SBarry Smith   PetscFunctionReturn(0);
15373014e516SBarry Smith }
15383014e516SBarry Smith 
15393014e516SBarry Smith 
15403014e516SBarry Smith #undef __FUNCT__
15413014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
15423014e516SBarry Smith /*
15433014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
15443014e516SBarry Smith 
15453014e516SBarry Smith */
15467087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
15473014e516SBarry Smith {
15483014e516SBarry Smith   PetscErrorCode    ierr;
15493014e516SBarry Smith   DMMatlabContext   *sctx;
15503014e516SBarry Smith 
15513014e516SBarry Smith   PetscFunctionBegin;
15523014e516SBarry Smith   /* currently sctx is memory bleed */
15533014e516SBarry Smith   ierr = DMGetContext(dm,(void**)&sctx);CHKERRQ(ierr);
15543014e516SBarry Smith   if (!sctx) {
15553014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
15563014e516SBarry Smith   }
15573014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
15583014e516SBarry Smith   ierr = DMSetContext(dm,sctx);CHKERRQ(ierr);
15593014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
15603014e516SBarry Smith   PetscFunctionReturn(0);
15613014e516SBarry Smith }
156223f975d1SBarry Smith #endif
1563