xref: /petsc/src/dm/interface/dm.c (revision 67a56275af7c3199211047633f41c135972aeb47)
147c6ae99SBarry Smith 
2c6db04a5SJed Brown #include <private/dmimpl.h>     /*I      "petscdm.h"     I*/
347c6ae99SBarry Smith 
4*67a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal;
5*67a56275SMatthew G Knepley 
647c6ae99SBarry Smith #undef __FUNCT__
7a4121054SBarry Smith #define __FUNCT__ "DMCreate"
8a4121054SBarry Smith /*@
9a4121054SBarry Smith   DMCreate - Creates an empty vector object. The type can then be set with DMetType().
10a4121054SBarry Smith 
11a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
12a4121054SBarry Smith    error when you try to use the vector.
13a4121054SBarry Smith 
14a4121054SBarry Smith   Collective on MPI_Comm
15a4121054SBarry Smith 
16a4121054SBarry Smith   Input Parameter:
17a4121054SBarry Smith . comm - The communicator for the DM object
18a4121054SBarry Smith 
19a4121054SBarry Smith   Output Parameter:
20a4121054SBarry Smith . dm - The DM object
21a4121054SBarry Smith 
22a4121054SBarry Smith   Level: beginner
23a4121054SBarry Smith 
24a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
25a4121054SBarry Smith @*/
267087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
27a4121054SBarry Smith {
28a4121054SBarry Smith   DM             v;
29a4121054SBarry Smith   PetscErrorCode ierr;
30a4121054SBarry Smith 
31a4121054SBarry Smith   PetscFunctionBegin;
321411c6eeSJed Brown   PetscValidPointer(dm,2);
331411c6eeSJed Brown   *dm = PETSC_NULL;
34a4121054SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
35a4121054SBarry Smith   ierr = DMInitializePackage(PETSC_NULL);CHKERRQ(ierr);
36a4121054SBarry Smith #endif
37a4121054SBarry Smith 
38a4121054SBarry Smith   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
39a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
401411c6eeSJed Brown 
411411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
421411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
431411c6eeSJed Brown   v->bs           = 1;
441411c6eeSJed Brown 
451411c6eeSJed Brown   *dm = v;
46a4121054SBarry Smith   PetscFunctionReturn(0);
47a4121054SBarry Smith }
48a4121054SBarry Smith 
49a4121054SBarry Smith 
50a4121054SBarry Smith #undef __FUNCT__
519a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
529a42bb27SBarry Smith /*@C
53564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
549a42bb27SBarry Smith 
55aa219208SBarry Smith    Logically Collective on DMDA
569a42bb27SBarry Smith 
579a42bb27SBarry Smith    Input Parameter:
589a42bb27SBarry Smith +  da - initial distributed array
598154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
609a42bb27SBarry Smith 
619a42bb27SBarry Smith    Options Database:
629a42bb27SBarry Smith .   -da_vec_type ctype
639a42bb27SBarry Smith 
649a42bb27SBarry Smith    Level: intermediate
659a42bb27SBarry Smith 
66aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
679a42bb27SBarry Smith @*/
687087cfbeSBarry Smith PetscErrorCode  DMSetVecType(DM da,const VecType ctype)
699a42bb27SBarry Smith {
709a42bb27SBarry Smith   PetscErrorCode ierr;
719a42bb27SBarry Smith 
729a42bb27SBarry Smith   PetscFunctionBegin;
739a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
749a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
759a42bb27SBarry Smith   ierr = PetscStrallocpy(ctype,&da->vectype);CHKERRQ(ierr);
769a42bb27SBarry Smith   PetscFunctionReturn(0);
779a42bb27SBarry Smith }
789a42bb27SBarry Smith 
799a42bb27SBarry Smith #undef __FUNCT__
809a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
819a42bb27SBarry Smith /*@C
829a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
83aa219208SBarry Smith    DMDA options in the database.
849a42bb27SBarry Smith 
85aa219208SBarry Smith    Logically Collective on DMDA
869a42bb27SBarry Smith 
879a42bb27SBarry Smith    Input Parameter:
88aa219208SBarry Smith +  da - the DMDA context
899a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
909a42bb27SBarry Smith 
919a42bb27SBarry Smith    Notes:
929a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
939a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
949a42bb27SBarry Smith 
959a42bb27SBarry Smith    Level: advanced
969a42bb27SBarry Smith 
97aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
989a42bb27SBarry Smith 
999a42bb27SBarry Smith .seealso: DMSetFromOptions()
1009a42bb27SBarry Smith @*/
1017087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
1029a42bb27SBarry Smith {
1039a42bb27SBarry Smith   PetscErrorCode ierr;
1049a42bb27SBarry Smith 
1059a42bb27SBarry Smith   PetscFunctionBegin;
1069a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1079a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
1089a42bb27SBarry Smith   PetscFunctionReturn(0);
1099a42bb27SBarry Smith }
1109a42bb27SBarry Smith 
1119a42bb27SBarry Smith #undef __FUNCT__
11247c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
11347c6ae99SBarry Smith /*@
114aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
11547c6ae99SBarry Smith 
11647c6ae99SBarry Smith     Collective on DM
11747c6ae99SBarry Smith 
11847c6ae99SBarry Smith     Input Parameter:
11947c6ae99SBarry Smith .   dm - the DM object to destroy
12047c6ae99SBarry Smith 
12147c6ae99SBarry Smith     Level: developer
12247c6ae99SBarry Smith 
12347c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
12447c6ae99SBarry Smith 
12547c6ae99SBarry Smith @*/
1267087cfbeSBarry Smith PetscErrorCode  DMDestroy(DM dm)
12747c6ae99SBarry Smith {
12847c6ae99SBarry Smith   PetscErrorCode ierr;
12947c6ae99SBarry Smith 
13047c6ae99SBarry Smith   PetscFunctionBegin;
1310c010503SBarry Smith   ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
13247c6ae99SBarry Smith   PetscFunctionReturn(0);
13347c6ae99SBarry Smith }
13447c6ae99SBarry Smith 
13547c6ae99SBarry Smith #undef __FUNCT__
136d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
137d7bf68aeSBarry Smith /*@
138d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
139d7bf68aeSBarry Smith 
140d7bf68aeSBarry Smith     Collective on DM
141d7bf68aeSBarry Smith 
142d7bf68aeSBarry Smith     Input Parameter:
143d7bf68aeSBarry Smith .   dm - the DM object to setup
144d7bf68aeSBarry Smith 
145d7bf68aeSBarry Smith     Level: developer
146d7bf68aeSBarry Smith 
147d7bf68aeSBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
148d7bf68aeSBarry Smith 
149d7bf68aeSBarry Smith @*/
1507087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
151d7bf68aeSBarry Smith {
152d7bf68aeSBarry Smith   PetscErrorCode ierr;
153d7bf68aeSBarry Smith 
154d7bf68aeSBarry Smith   PetscFunctionBegin;
155d7bf68aeSBarry Smith   if (dm->ops->setup) {
156d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
157d7bf68aeSBarry Smith   }
158d7bf68aeSBarry Smith   PetscFunctionReturn(0);
159d7bf68aeSBarry Smith }
160d7bf68aeSBarry Smith 
161d7bf68aeSBarry Smith #undef __FUNCT__
162d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
163d7bf68aeSBarry Smith /*@
164d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
165d7bf68aeSBarry Smith 
166d7bf68aeSBarry Smith     Collective on DM
167d7bf68aeSBarry Smith 
168d7bf68aeSBarry Smith     Input Parameter:
169d7bf68aeSBarry Smith .   dm - the DM object to set options for
170d7bf68aeSBarry Smith 
171d7bf68aeSBarry Smith     Level: developer
172d7bf68aeSBarry Smith 
173d7bf68aeSBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
174d7bf68aeSBarry Smith 
175d7bf68aeSBarry Smith @*/
1767087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
177d7bf68aeSBarry Smith {
178d7bf68aeSBarry Smith   PetscErrorCode ierr;
179d7bf68aeSBarry Smith 
180d7bf68aeSBarry Smith   PetscFunctionBegin;
181d7bf68aeSBarry Smith   if (dm->ops->setfromoptions) {
182d7bf68aeSBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
183d7bf68aeSBarry Smith   }
184d7bf68aeSBarry Smith   PetscFunctionReturn(0);
185d7bf68aeSBarry Smith }
186d7bf68aeSBarry Smith 
187d7bf68aeSBarry Smith #undef __FUNCT__
18847c6ae99SBarry Smith #define __FUNCT__ "DMView"
189fc9bc008SSatish Balay /*@C
190aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
19147c6ae99SBarry Smith 
19247c6ae99SBarry Smith     Collective on DM
19347c6ae99SBarry Smith 
19447c6ae99SBarry Smith     Input Parameter:
19547c6ae99SBarry Smith +   dm - the DM object to view
19647c6ae99SBarry Smith -   v - the viewer
19747c6ae99SBarry Smith 
19847c6ae99SBarry Smith     Level: developer
19947c6ae99SBarry Smith 
20047c6ae99SBarry Smith .seealso DMDestroy(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
20147c6ae99SBarry Smith 
20247c6ae99SBarry Smith @*/
2037087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
20447c6ae99SBarry Smith {
20547c6ae99SBarry Smith   PetscErrorCode ierr;
20647c6ae99SBarry Smith 
20747c6ae99SBarry Smith   PetscFunctionBegin;
2083014e516SBarry Smith  if (!v) {
2093014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
2103014e516SBarry Smith   }
2110c010503SBarry Smith   if (dm->ops->view) {
2120c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
21347c6ae99SBarry Smith   }
21447c6ae99SBarry Smith   PetscFunctionReturn(0);
21547c6ae99SBarry Smith }
21647c6ae99SBarry Smith 
21747c6ae99SBarry Smith #undef __FUNCT__
21847c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
21947c6ae99SBarry Smith /*@
220aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
22147c6ae99SBarry Smith 
22247c6ae99SBarry Smith     Collective on DM
22347c6ae99SBarry Smith 
22447c6ae99SBarry Smith     Input Parameter:
22547c6ae99SBarry Smith .   dm - the DM object
22647c6ae99SBarry Smith 
22747c6ae99SBarry Smith     Output Parameter:
22847c6ae99SBarry Smith .   vec - the global vector
22947c6ae99SBarry Smith 
23047c6ae99SBarry Smith     Level: developer
23147c6ae99SBarry Smith 
23247c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
23347c6ae99SBarry Smith 
23447c6ae99SBarry Smith @*/
2357087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
23647c6ae99SBarry Smith {
23747c6ae99SBarry Smith   PetscErrorCode ierr;
23847c6ae99SBarry Smith 
23947c6ae99SBarry Smith   PetscFunctionBegin;
24047c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
24147c6ae99SBarry Smith   PetscFunctionReturn(0);
24247c6ae99SBarry Smith }
24347c6ae99SBarry Smith 
24447c6ae99SBarry Smith #undef __FUNCT__
24547c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
24647c6ae99SBarry Smith /*@
247aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
24847c6ae99SBarry Smith 
24947c6ae99SBarry Smith     Not Collective
25047c6ae99SBarry Smith 
25147c6ae99SBarry Smith     Input Parameter:
25247c6ae99SBarry Smith .   dm - the DM object
25347c6ae99SBarry Smith 
25447c6ae99SBarry Smith     Output Parameter:
25547c6ae99SBarry Smith .   vec - the local vector
25647c6ae99SBarry Smith 
25747c6ae99SBarry Smith     Level: developer
25847c6ae99SBarry Smith 
25947c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
26047c6ae99SBarry Smith 
26147c6ae99SBarry Smith @*/
2627087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
26347c6ae99SBarry Smith {
26447c6ae99SBarry Smith   PetscErrorCode ierr;
26547c6ae99SBarry Smith 
26647c6ae99SBarry Smith   PetscFunctionBegin;
26747c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
26847c6ae99SBarry Smith   PetscFunctionReturn(0);
26947c6ae99SBarry Smith }
27047c6ae99SBarry Smith 
27147c6ae99SBarry Smith #undef __FUNCT__
2721411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
2731411c6eeSJed Brown /*@
2741411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
2751411c6eeSJed Brown 
2761411c6eeSJed Brown    Collective on DM
2771411c6eeSJed Brown 
2781411c6eeSJed Brown    Input Parameter:
2791411c6eeSJed Brown .  dm - the DM that provides the mapping
2801411c6eeSJed Brown 
2811411c6eeSJed Brown    Output Parameter:
2821411c6eeSJed Brown .  ltog - the mapping
2831411c6eeSJed Brown 
2841411c6eeSJed Brown    Level: intermediate
2851411c6eeSJed Brown 
2861411c6eeSJed Brown    Notes:
2871411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
2881411c6eeSJed Brown    MatSetLocalToGlobalMapping().
2891411c6eeSJed Brown 
2901411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
2911411c6eeSJed Brown @*/
2927087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
2931411c6eeSJed Brown {
2941411c6eeSJed Brown   PetscErrorCode ierr;
2951411c6eeSJed Brown 
2961411c6eeSJed Brown   PetscFunctionBegin;
2971411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2981411c6eeSJed Brown   PetscValidPointer(ltog,2);
2991411c6eeSJed Brown   if (!dm->ltogmap) {
3001411c6eeSJed Brown     if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
3011411c6eeSJed Brown     ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
3021411c6eeSJed Brown   }
3031411c6eeSJed Brown   *ltog = dm->ltogmap;
3041411c6eeSJed Brown   PetscFunctionReturn(0);
3051411c6eeSJed Brown }
3061411c6eeSJed Brown 
3071411c6eeSJed Brown #undef __FUNCT__
3081411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
3091411c6eeSJed Brown /*@
3101411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
3111411c6eeSJed Brown 
3121411c6eeSJed Brown    Collective on DM
3131411c6eeSJed Brown 
3141411c6eeSJed Brown    Input Parameter:
3151411c6eeSJed Brown .  da - the distributed array that provides the mapping
3161411c6eeSJed Brown 
3171411c6eeSJed Brown    Output Parameter:
3181411c6eeSJed Brown .  ltog - the block mapping
3191411c6eeSJed Brown 
3201411c6eeSJed Brown    Level: intermediate
3211411c6eeSJed Brown 
3221411c6eeSJed Brown    Notes:
3231411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
3241411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
3251411c6eeSJed Brown 
3261411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
3271411c6eeSJed Brown @*/
3287087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
3291411c6eeSJed Brown {
3301411c6eeSJed Brown   PetscErrorCode ierr;
3311411c6eeSJed Brown 
3321411c6eeSJed Brown   PetscFunctionBegin;
3331411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3341411c6eeSJed Brown   PetscValidPointer(ltog,2);
3351411c6eeSJed Brown   if (!dm->ltogmapb) {
3361411c6eeSJed Brown     PetscInt bs;
3371411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
3381411c6eeSJed Brown     if (bs > 1) {
3391411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
3401411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
3411411c6eeSJed Brown     } else {
3421411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
3431411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
3441411c6eeSJed Brown     }
3451411c6eeSJed Brown   }
3461411c6eeSJed Brown   *ltog = dm->ltogmapb;
3471411c6eeSJed Brown   PetscFunctionReturn(0);
3481411c6eeSJed Brown }
3491411c6eeSJed Brown 
3501411c6eeSJed Brown #undef __FUNCT__
3511411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
3521411c6eeSJed Brown /*@
3531411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
3541411c6eeSJed Brown 
3551411c6eeSJed Brown    Not Collective
3561411c6eeSJed Brown 
3571411c6eeSJed Brown    Input Parameter:
3581411c6eeSJed Brown .  dm - the DM with block structure
3591411c6eeSJed Brown 
3601411c6eeSJed Brown    Output Parameter:
3611411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
3621411c6eeSJed Brown 
3631411c6eeSJed Brown    Level: intermediate
3641411c6eeSJed Brown 
3651411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
3661411c6eeSJed Brown @*/
3677087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
3681411c6eeSJed Brown {
3691411c6eeSJed Brown   PetscFunctionBegin;
3701411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3711411c6eeSJed Brown   PetscValidPointer(bs,2);
3721411c6eeSJed 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");
3731411c6eeSJed Brown   *bs = dm->bs;
3741411c6eeSJed Brown   PetscFunctionReturn(0);
3751411c6eeSJed Brown }
3761411c6eeSJed Brown 
3771411c6eeSJed Brown #undef __FUNCT__
37847c6ae99SBarry Smith #define __FUNCT__ "DMGetInterpolation"
37947c6ae99SBarry Smith /*@
380aa219208SBarry Smith     DMGetInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
38147c6ae99SBarry Smith 
38247c6ae99SBarry Smith     Collective on DM
38347c6ae99SBarry Smith 
38447c6ae99SBarry Smith     Input Parameter:
38547c6ae99SBarry Smith +   dm1 - the DM object
38647c6ae99SBarry Smith -   dm2 - the second, finer DM object
38747c6ae99SBarry Smith 
38847c6ae99SBarry Smith     Output Parameter:
38947c6ae99SBarry Smith +  mat - the interpolation
39047c6ae99SBarry Smith -  vec - the scaling (optional)
39147c6ae99SBarry Smith 
39247c6ae99SBarry Smith     Level: developer
39347c6ae99SBarry Smith 
39447c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetColoring(), DMGetMatrix()
39547c6ae99SBarry Smith 
39647c6ae99SBarry Smith @*/
3977087cfbeSBarry Smith PetscErrorCode  DMGetInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
39847c6ae99SBarry Smith {
39947c6ae99SBarry Smith   PetscErrorCode ierr;
40047c6ae99SBarry Smith 
40147c6ae99SBarry Smith   PetscFunctionBegin;
40247c6ae99SBarry Smith   ierr = (*dm1->ops->getinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
40347c6ae99SBarry Smith   PetscFunctionReturn(0);
40447c6ae99SBarry Smith }
40547c6ae99SBarry Smith 
40647c6ae99SBarry Smith #undef __FUNCT__
40747c6ae99SBarry Smith #define __FUNCT__ "DMGetInjection"
40847c6ae99SBarry Smith /*@
409aa219208SBarry Smith     DMGetInjection - Gets injection matrix between two DMDA or DMComposite objects
41047c6ae99SBarry Smith 
41147c6ae99SBarry Smith     Collective on DM
41247c6ae99SBarry Smith 
41347c6ae99SBarry Smith     Input Parameter:
41447c6ae99SBarry Smith +   dm1 - the DM object
41547c6ae99SBarry Smith -   dm2 - the second, finer DM object
41647c6ae99SBarry Smith 
41747c6ae99SBarry Smith     Output Parameter:
41847c6ae99SBarry Smith .   ctx - the injection
41947c6ae99SBarry Smith 
42047c6ae99SBarry Smith     Level: developer
42147c6ae99SBarry Smith 
42247c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetColoring(), DMGetMatrix(), DMGetInterpolation()
42347c6ae99SBarry Smith 
42447c6ae99SBarry Smith @*/
4257087cfbeSBarry Smith PetscErrorCode  DMGetInjection(DM dm1,DM dm2,VecScatter *ctx)
42647c6ae99SBarry Smith {
42747c6ae99SBarry Smith   PetscErrorCode ierr;
42847c6ae99SBarry Smith 
42947c6ae99SBarry Smith   PetscFunctionBegin;
43047c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
43147c6ae99SBarry Smith   PetscFunctionReturn(0);
43247c6ae99SBarry Smith }
43347c6ae99SBarry Smith 
43447c6ae99SBarry Smith #undef __FUNCT__
43547c6ae99SBarry Smith #define __FUNCT__ "DMGetColoring"
43647c6ae99SBarry Smith /*@
437aa219208SBarry Smith     DMGetColoring - Gets coloring for a DMDA or DMComposite
43847c6ae99SBarry Smith 
43947c6ae99SBarry Smith     Collective on DM
44047c6ae99SBarry Smith 
44147c6ae99SBarry Smith     Input Parameter:
44247c6ae99SBarry Smith +   dm - the DM object
44347c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
44447c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
44547c6ae99SBarry Smith 
44647c6ae99SBarry Smith     Output Parameter:
44747c6ae99SBarry Smith .   coloring - the coloring
44847c6ae99SBarry Smith 
44947c6ae99SBarry Smith     Level: developer
45047c6ae99SBarry Smith 
45147c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetMatrix()
45247c6ae99SBarry Smith 
45347c6ae99SBarry Smith @*/
4547087cfbeSBarry Smith PetscErrorCode  DMGetColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
45547c6ae99SBarry Smith {
45647c6ae99SBarry Smith   PetscErrorCode ierr;
45747c6ae99SBarry Smith 
45847c6ae99SBarry Smith   PetscFunctionBegin;
45947c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
46047c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
46147c6ae99SBarry Smith   PetscFunctionReturn(0);
46247c6ae99SBarry Smith }
46347c6ae99SBarry Smith 
46447c6ae99SBarry Smith #undef __FUNCT__
46547c6ae99SBarry Smith #define __FUNCT__ "DMGetMatrix"
46647c6ae99SBarry Smith /*@C
467aa219208SBarry Smith     DMGetMatrix - Gets empty Jacobian for a DMDA or DMComposite
46847c6ae99SBarry Smith 
46947c6ae99SBarry Smith     Collective on DM
47047c6ae99SBarry Smith 
47147c6ae99SBarry Smith     Input Parameter:
47247c6ae99SBarry Smith +   dm - the DM object
47347c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
47494013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
47547c6ae99SBarry Smith 
47647c6ae99SBarry Smith     Output Parameter:
47747c6ae99SBarry Smith .   mat - the empty Jacobian
47847c6ae99SBarry Smith 
47947c6ae99SBarry Smith     Level: developer
48047c6ae99SBarry Smith 
48194013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
48294013140SBarry Smith        do not need to do it yourself.
48394013140SBarry Smith 
48494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
485aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
48694013140SBarry Smith 
48794013140SBarry 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
48894013140SBarry Smith        internally by PETSc.
48994013140SBarry Smith 
49094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
491aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
49294013140SBarry Smith 
49347c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetMatrix()
49447c6ae99SBarry Smith 
49547c6ae99SBarry Smith @*/
4967087cfbeSBarry Smith PetscErrorCode  DMGetMatrix(DM dm,const MatType mtype,Mat *mat)
49747c6ae99SBarry Smith {
49847c6ae99SBarry Smith   PetscErrorCode ierr;
499c7b7c8a4SJed Brown   char           ttype[256];
500c7b7c8a4SJed Brown   PetscBool      flg;
50147c6ae99SBarry Smith 
50247c6ae99SBarry Smith   PetscFunctionBegin;
503c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
504c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
505c7b7c8a4SJed Brown   ierr = PetscStrncpy(ttype,mtype,sizeof ttype);CHKERRQ(ierr);
506c7b7c8a4SJed Brown   ttype[sizeof ttype-1] = 0;
507c7b7c8a4SJed Brown   ierr = PetscOptionsBegin(((PetscObject)dm)->comm,((PetscObject)dm)->prefix,"DM options","Mat");CHKERRQ(ierr);
508c7b7c8a4SJed Brown   ierr = PetscOptionsList("-dm_mat_type","Matrix type","MatSetType",MatList,ttype,ttype,sizeof ttype,&flg);CHKERRQ(ierr);
509c7b7c8a4SJed Brown   ierr = PetscOptionsEnd();
510c7b7c8a4SJed Brown   if (flg || mtype) {
511c7b7c8a4SJed Brown     ierr = (*dm->ops->getmatrix)(dm,ttype,mat);CHKERRQ(ierr);
512c7b7c8a4SJed Brown   } else {                      /* Let the implementation decide */
513c7b7c8a4SJed Brown     ierr = (*dm->ops->getmatrix)(dm,PETSC_NULL,mat);CHKERRQ(ierr);
514c7b7c8a4SJed Brown   }
51547c6ae99SBarry Smith   PetscFunctionReturn(0);
51647c6ae99SBarry Smith }
51747c6ae99SBarry Smith 
51847c6ae99SBarry Smith #undef __FUNCT__
51947c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
52047c6ae99SBarry Smith /*@
52147c6ae99SBarry Smith     DMRefine - Refines a DM object
52247c6ae99SBarry Smith 
52347c6ae99SBarry Smith     Collective on DM
52447c6ae99SBarry Smith 
52547c6ae99SBarry Smith     Input Parameter:
52647c6ae99SBarry Smith +   dm - the DM object
52747c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
52847c6ae99SBarry Smith 
52947c6ae99SBarry Smith     Output Parameter:
53047c6ae99SBarry Smith .   dmf - the refined DM
53147c6ae99SBarry Smith 
53247c6ae99SBarry Smith     Level: developer
53347c6ae99SBarry Smith 
53447c6ae99SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
53547c6ae99SBarry Smith 
53647c6ae99SBarry Smith @*/
5377087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
53847c6ae99SBarry Smith {
53947c6ae99SBarry Smith   PetscErrorCode ierr;
54047c6ae99SBarry Smith 
54147c6ae99SBarry Smith   PetscFunctionBegin;
54247c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
54347c6ae99SBarry Smith   PetscFunctionReturn(0);
54447c6ae99SBarry Smith }
54547c6ae99SBarry Smith 
54647c6ae99SBarry Smith #undef __FUNCT__
54747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
54847c6ae99SBarry Smith /*@
54947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
55047c6ae99SBarry Smith 
55147c6ae99SBarry Smith     Neighbor-wise Collective on DM
55247c6ae99SBarry Smith 
55347c6ae99SBarry Smith     Input Parameters:
55447c6ae99SBarry Smith +   dm - the DM object
55547c6ae99SBarry Smith .   g - the global vector
55647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
55747c6ae99SBarry Smith -   l - the local vector
55847c6ae99SBarry Smith 
55947c6ae99SBarry Smith 
56047c6ae99SBarry Smith     Level: beginner
56147c6ae99SBarry Smith 
5629a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
56347c6ae99SBarry Smith 
56447c6ae99SBarry Smith @*/
5657087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
56647c6ae99SBarry Smith {
56747c6ae99SBarry Smith   PetscErrorCode ierr;
56847c6ae99SBarry Smith 
56947c6ae99SBarry Smith   PetscFunctionBegin;
57047c6ae99SBarry Smith   ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode,l);CHKERRQ(ierr);
57147c6ae99SBarry Smith   PetscFunctionReturn(0);
57247c6ae99SBarry Smith }
57347c6ae99SBarry Smith 
57447c6ae99SBarry Smith #undef __FUNCT__
57547c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
57647c6ae99SBarry Smith /*@
57747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
57847c6ae99SBarry Smith 
57947c6ae99SBarry Smith     Neighbor-wise Collective on DM
58047c6ae99SBarry Smith 
58147c6ae99SBarry Smith     Input Parameters:
58247c6ae99SBarry Smith +   dm - the DM object
58347c6ae99SBarry Smith .   g - the global vector
58447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
58547c6ae99SBarry Smith -   l - the local vector
58647c6ae99SBarry Smith 
58747c6ae99SBarry Smith 
58847c6ae99SBarry Smith     Level: beginner
58947c6ae99SBarry Smith 
5909a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
59147c6ae99SBarry Smith 
59247c6ae99SBarry Smith @*/
5937087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
59447c6ae99SBarry Smith {
59547c6ae99SBarry Smith   PetscErrorCode ierr;
59647c6ae99SBarry Smith 
59747c6ae99SBarry Smith   PetscFunctionBegin;
59847c6ae99SBarry Smith   ierr = (*dm->ops->globaltolocalend)(dm,g,mode,l);CHKERRQ(ierr);
59947c6ae99SBarry Smith   PetscFunctionReturn(0);
60047c6ae99SBarry Smith }
60147c6ae99SBarry Smith 
60247c6ae99SBarry Smith #undef __FUNCT__
6039a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
60447c6ae99SBarry Smith /*@
6059a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
6069a42bb27SBarry Smith 
6079a42bb27SBarry Smith     Neighbor-wise Collective on DM
6089a42bb27SBarry Smith 
6099a42bb27SBarry Smith     Input Parameters:
6109a42bb27SBarry Smith +   dm - the DM object
611f6813fd5SJed Brown .   l - the local vector
6129a42bb27SBarry 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
6139a42bb27SBarry Smith            base point.
614f6813fd5SJed Brown - - the global vector
6159a42bb27SBarry Smith 
6169a42bb27SBarry 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
6179a42bb27SBarry 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
6189a42bb27SBarry Smith            global array to the final global array with VecAXPY().
6199a42bb27SBarry Smith 
6209a42bb27SBarry Smith     Level: beginner
6219a42bb27SBarry Smith 
6229a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
6239a42bb27SBarry Smith 
6249a42bb27SBarry Smith @*/
6257087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
6269a42bb27SBarry Smith {
6279a42bb27SBarry Smith   PetscErrorCode ierr;
6289a42bb27SBarry Smith 
6299a42bb27SBarry Smith   PetscFunctionBegin;
630f6813fd5SJed Brown   ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode,g);CHKERRQ(ierr);
6319a42bb27SBarry Smith   PetscFunctionReturn(0);
6329a42bb27SBarry Smith }
6339a42bb27SBarry Smith 
6349a42bb27SBarry Smith #undef __FUNCT__
6359a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
6369a42bb27SBarry Smith /*@
6379a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
63847c6ae99SBarry Smith 
63947c6ae99SBarry Smith     Neighbor-wise Collective on DM
64047c6ae99SBarry Smith 
64147c6ae99SBarry Smith     Input Parameters:
64247c6ae99SBarry Smith +   dm - the DM object
643f6813fd5SJed Brown .   l - the local vector
64447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
645f6813fd5SJed Brown -   g - the global vector
64647c6ae99SBarry Smith 
64747c6ae99SBarry Smith 
64847c6ae99SBarry Smith     Level: beginner
64947c6ae99SBarry Smith 
6509a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
65147c6ae99SBarry Smith 
65247c6ae99SBarry Smith @*/
6537087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
65447c6ae99SBarry Smith {
65547c6ae99SBarry Smith   PetscErrorCode ierr;
65647c6ae99SBarry Smith 
65747c6ae99SBarry Smith   PetscFunctionBegin;
658f6813fd5SJed Brown   ierr = (*dm->ops->localtoglobalend)(dm,l,mode,g);CHKERRQ(ierr);
65947c6ae99SBarry Smith   PetscFunctionReturn(0);
66047c6ae99SBarry Smith }
66147c6ae99SBarry Smith 
66247c6ae99SBarry Smith #undef __FUNCT__
66347c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
66447c6ae99SBarry Smith /*@
66547c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
66647c6ae99SBarry Smith 
66747c6ae99SBarry Smith     Collective on DM
66847c6ae99SBarry Smith 
66947c6ae99SBarry Smith     Input Parameter:
67047c6ae99SBarry Smith +   dm - the DM object
67147c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
67247c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
67347c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
67447c6ae99SBarry Smith 
67547c6ae99SBarry Smith     Level: developer
67647c6ae99SBarry Smith 
67747c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
67847c6ae99SBarry Smith          DMSetFunction()
67947c6ae99SBarry Smith 
68047c6ae99SBarry Smith @*/
6817087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
68247c6ae99SBarry Smith {
68347c6ae99SBarry Smith   PetscErrorCode ierr;
68447c6ae99SBarry Smith   PetscFunctionBegin;
68547c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
68647c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
68747c6ae99SBarry Smith   if (A != B) {
68847c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
68947c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
69047c6ae99SBarry Smith   }
69147c6ae99SBarry Smith   PetscFunctionReturn(0);
69247c6ae99SBarry Smith }
69347c6ae99SBarry Smith 
69447c6ae99SBarry Smith #undef __FUNCT__
69547c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
69647c6ae99SBarry Smith /*@
69747c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
69847c6ae99SBarry Smith 
69947c6ae99SBarry Smith     Collective on DM
70047c6ae99SBarry Smith 
70147c6ae99SBarry Smith     Input Parameter:
70247c6ae99SBarry Smith +   dm - the DM object
70347c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
70447c6ae99SBarry Smith 
70547c6ae99SBarry Smith     Output Parameter:
70647c6ae99SBarry Smith .   dmc - the coarsened DM
70747c6ae99SBarry Smith 
70847c6ae99SBarry Smith     Level: developer
70947c6ae99SBarry Smith 
71047c6ae99SBarry Smith .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
71147c6ae99SBarry Smith 
71247c6ae99SBarry Smith @*/
7137087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
71447c6ae99SBarry Smith {
71547c6ae99SBarry Smith   PetscErrorCode ierr;
71647c6ae99SBarry Smith 
71747c6ae99SBarry Smith   PetscFunctionBegin;
71847c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
71947c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
72047c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
72147c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
72247c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
72347c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
72447c6ae99SBarry Smith   }
72547c6ae99SBarry Smith   PetscFunctionReturn(0);
72647c6ae99SBarry Smith }
72747c6ae99SBarry Smith 
72847c6ae99SBarry Smith #undef __FUNCT__
72947c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
73047c6ae99SBarry Smith /*@C
73147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
73247c6ae99SBarry Smith 
73347c6ae99SBarry Smith     Collective on DM
73447c6ae99SBarry Smith 
73547c6ae99SBarry Smith     Input Parameter:
73647c6ae99SBarry Smith +   dm - the DM object
73747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
73847c6ae99SBarry Smith 
73947c6ae99SBarry Smith     Output Parameter:
74047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
74147c6ae99SBarry Smith 
74247c6ae99SBarry Smith     Level: developer
74347c6ae99SBarry Smith 
74447c6ae99SBarry Smith .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
74547c6ae99SBarry Smith 
74647c6ae99SBarry Smith @*/
7477087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
74847c6ae99SBarry Smith {
74947c6ae99SBarry Smith   PetscErrorCode ierr;
75047c6ae99SBarry Smith 
75147c6ae99SBarry Smith   PetscFunctionBegin;
75247c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
75347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
75447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
75547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
75647c6ae99SBarry Smith   } else if (dm->ops->refine) {
75747c6ae99SBarry Smith     PetscInt i;
75847c6ae99SBarry Smith 
75947c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
76047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
76147c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
76247c6ae99SBarry Smith     }
76347c6ae99SBarry Smith   } else {
76447c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
76547c6ae99SBarry Smith   }
76647c6ae99SBarry Smith   PetscFunctionReturn(0);
76747c6ae99SBarry Smith }
76847c6ae99SBarry Smith 
76947c6ae99SBarry Smith #undef __FUNCT__
77047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
77147c6ae99SBarry Smith /*@C
77247c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
77347c6ae99SBarry Smith 
77447c6ae99SBarry Smith     Collective on DM
77547c6ae99SBarry Smith 
77647c6ae99SBarry Smith     Input Parameter:
77747c6ae99SBarry Smith +   dm - the DM object
77847c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
77947c6ae99SBarry Smith 
78047c6ae99SBarry Smith     Output Parameter:
78147c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
78247c6ae99SBarry Smith 
78347c6ae99SBarry Smith     Level: developer
78447c6ae99SBarry Smith 
78547c6ae99SBarry Smith .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
78647c6ae99SBarry Smith 
78747c6ae99SBarry Smith @*/
7887087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
78947c6ae99SBarry Smith {
79047c6ae99SBarry Smith   PetscErrorCode ierr;
79147c6ae99SBarry Smith 
79247c6ae99SBarry Smith   PetscFunctionBegin;
79347c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
79447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
79547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
79647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
79747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
79847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
79947c6ae99SBarry Smith     PetscInt i;
80047c6ae99SBarry Smith 
80147c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
80247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
80347c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
80447c6ae99SBarry Smith     }
80547c6ae99SBarry Smith   } else {
80647c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
80747c6ae99SBarry Smith   }
80847c6ae99SBarry Smith   PetscFunctionReturn(0);
80947c6ae99SBarry Smith }
81047c6ae99SBarry Smith 
81147c6ae99SBarry Smith #undef __FUNCT__
81247c6ae99SBarry Smith #define __FUNCT__ "DMGetAggregates"
81347c6ae99SBarry Smith /*@
81447c6ae99SBarry Smith    DMGetAggregates - Gets the aggregates that map between
81547c6ae99SBarry Smith    grids associated with two DMs.
81647c6ae99SBarry Smith 
81747c6ae99SBarry Smith    Collective on DM
81847c6ae99SBarry Smith 
81947c6ae99SBarry Smith    Input Parameters:
82047c6ae99SBarry Smith +  dmc - the coarse grid DM
82147c6ae99SBarry Smith -  dmf - the fine grid DM
82247c6ae99SBarry Smith 
82347c6ae99SBarry Smith    Output Parameters:
82447c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
82547c6ae99SBarry Smith 
82647c6ae99SBarry Smith    Level: intermediate
82747c6ae99SBarry Smith 
82847c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
82947c6ae99SBarry Smith 
83047c6ae99SBarry Smith .seealso: DMRefine(), DMGetInjection(), DMGetInterpolation()
83147c6ae99SBarry Smith @*/
8327087cfbeSBarry Smith PetscErrorCode  DMGetAggregates(DM dmc, DM dmf, Mat *rest)
83347c6ae99SBarry Smith {
83447c6ae99SBarry Smith   PetscErrorCode ierr;
83547c6ae99SBarry Smith 
83647c6ae99SBarry Smith   PetscFunctionBegin;
83747c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
83847c6ae99SBarry Smith   PetscFunctionReturn(0);
83947c6ae99SBarry Smith }
84047c6ae99SBarry Smith 
84147c6ae99SBarry Smith #undef __FUNCT__
84247c6ae99SBarry Smith #define __FUNCT__ "DMSetContext"
84347c6ae99SBarry Smith /*@
84447c6ae99SBarry Smith     DMSetContext - Set a user context into a DM object
84547c6ae99SBarry Smith 
84647c6ae99SBarry Smith     Not Collective
84747c6ae99SBarry Smith 
84847c6ae99SBarry Smith     Input Parameters:
84947c6ae99SBarry Smith +   dm - the DM object
85047c6ae99SBarry Smith -   ctx - the user context
85147c6ae99SBarry Smith 
85247c6ae99SBarry Smith     Level: intermediate
85347c6ae99SBarry Smith 
85447c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext()
85547c6ae99SBarry Smith 
85647c6ae99SBarry Smith @*/
8577087cfbeSBarry Smith PetscErrorCode  DMSetContext(DM dm,void *ctx)
85847c6ae99SBarry Smith {
85947c6ae99SBarry Smith   PetscFunctionBegin;
86047c6ae99SBarry Smith   dm->ctx = ctx;
86147c6ae99SBarry Smith   PetscFunctionReturn(0);
86247c6ae99SBarry Smith }
86347c6ae99SBarry Smith 
86447c6ae99SBarry Smith #undef __FUNCT__
86547c6ae99SBarry Smith #define __FUNCT__ "DMGetContext"
86647c6ae99SBarry Smith /*@
86747c6ae99SBarry Smith     DMGetContext - Gets a user context from a DM object
86847c6ae99SBarry Smith 
86947c6ae99SBarry Smith     Not Collective
87047c6ae99SBarry Smith 
87147c6ae99SBarry Smith     Input Parameter:
87247c6ae99SBarry Smith .   dm - the DM object
87347c6ae99SBarry Smith 
87447c6ae99SBarry Smith     Output Parameter:
87547c6ae99SBarry Smith .   ctx - the user context
87647c6ae99SBarry Smith 
87747c6ae99SBarry Smith     Level: intermediate
87847c6ae99SBarry Smith 
87947c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext()
88047c6ae99SBarry Smith 
88147c6ae99SBarry Smith @*/
8827087cfbeSBarry Smith PetscErrorCode  DMGetContext(DM dm,void **ctx)
88347c6ae99SBarry Smith {
88447c6ae99SBarry Smith   PetscFunctionBegin;
88547c6ae99SBarry Smith   *ctx = dm->ctx;
88647c6ae99SBarry Smith   PetscFunctionReturn(0);
88747c6ae99SBarry Smith }
88847c6ae99SBarry Smith 
88947c6ae99SBarry Smith #undef __FUNCT__
89047c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
89147c6ae99SBarry Smith /*@
89247c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
89347c6ae99SBarry Smith 
89447c6ae99SBarry Smith     Logically Collective on DM
89547c6ae99SBarry Smith 
89647c6ae99SBarry Smith     Input Parameter:
89747c6ae99SBarry Smith +   dm - the DM object to destroy
89847c6ae99SBarry Smith -   f - the function to compute the initial guess
89947c6ae99SBarry Smith 
90047c6ae99SBarry Smith     Level: intermediate
90147c6ae99SBarry Smith 
90247c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
90347c6ae99SBarry Smith 
90447c6ae99SBarry Smith @*/
9057087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
90647c6ae99SBarry Smith {
90747c6ae99SBarry Smith   PetscFunctionBegin;
90847c6ae99SBarry Smith   dm->ops->initialguess = f;
90947c6ae99SBarry Smith   PetscFunctionReturn(0);
91047c6ae99SBarry Smith }
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith #undef __FUNCT__
91347c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
91447c6ae99SBarry Smith /*@
91547c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
91647c6ae99SBarry Smith 
91747c6ae99SBarry Smith     Logically Collective on DM
91847c6ae99SBarry Smith 
91947c6ae99SBarry Smith     Input Parameter:
92047c6ae99SBarry Smith +   dm - the DM object
92147c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
92247c6ae99SBarry Smith 
92347c6ae99SBarry Smith     Level: intermediate
92447c6ae99SBarry Smith 
92547c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
92647c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
92747c6ae99SBarry Smith 
92847c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
92947c6ae99SBarry Smith          DMSetJacobian()
93047c6ae99SBarry Smith 
93147c6ae99SBarry Smith @*/
9327087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
93347c6ae99SBarry Smith {
93447c6ae99SBarry Smith   PetscFunctionBegin;
93547c6ae99SBarry Smith   dm->ops->function = f;
93647c6ae99SBarry Smith   if (f) {
93747c6ae99SBarry Smith     dm->ops->functionj = f;
93847c6ae99SBarry Smith   }
93947c6ae99SBarry Smith   PetscFunctionReturn(0);
94047c6ae99SBarry Smith }
94147c6ae99SBarry Smith 
94247c6ae99SBarry Smith #undef __FUNCT__
94347c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
94447c6ae99SBarry Smith /*@
94547c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
94647c6ae99SBarry Smith 
94747c6ae99SBarry Smith     Logically Collective on DM
94847c6ae99SBarry Smith 
94947c6ae99SBarry Smith     Input Parameter:
95047c6ae99SBarry Smith +   dm - the DM object to destroy
95147c6ae99SBarry Smith -   f - the function to compute the matrix entries
95247c6ae99SBarry Smith 
95347c6ae99SBarry Smith     Level: intermediate
95447c6ae99SBarry Smith 
95547c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
95647c6ae99SBarry Smith          DMSetFunction()
95747c6ae99SBarry Smith 
95847c6ae99SBarry Smith @*/
9597087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
96047c6ae99SBarry Smith {
96147c6ae99SBarry Smith   PetscFunctionBegin;
96247c6ae99SBarry Smith   dm->ops->jacobian = f;
96347c6ae99SBarry Smith   PetscFunctionReturn(0);
96447c6ae99SBarry Smith }
96547c6ae99SBarry Smith 
96647c6ae99SBarry Smith #undef __FUNCT__
96747c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
96847c6ae99SBarry Smith /*@
96947c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
97047c6ae99SBarry Smith 
97147c6ae99SBarry Smith     Collective on DM
97247c6ae99SBarry Smith 
97347c6ae99SBarry Smith     Input Parameter:
97447c6ae99SBarry Smith +   dm - the DM object to destroy
97547c6ae99SBarry Smith -   x - the vector to hold the initial guess values
97647c6ae99SBarry Smith 
97747c6ae99SBarry Smith     Level: developer
97847c6ae99SBarry Smith 
97947c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetRhs(), DMSetMat()
98047c6ae99SBarry Smith 
98147c6ae99SBarry Smith @*/
9827087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
98347c6ae99SBarry Smith {
98447c6ae99SBarry Smith   PetscErrorCode ierr;
98547c6ae99SBarry Smith   PetscFunctionBegin;
98647c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
98747c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
98847c6ae99SBarry Smith   PetscFunctionReturn(0);
98947c6ae99SBarry Smith }
99047c6ae99SBarry Smith 
99147c6ae99SBarry Smith #undef __FUNCT__
99247c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
99347c6ae99SBarry Smith /*@
99447c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
99547c6ae99SBarry Smith 
99647c6ae99SBarry Smith     Not Collective
99747c6ae99SBarry Smith 
99847c6ae99SBarry Smith     Input Parameter:
99947c6ae99SBarry Smith .   dm - the DM object to destroy
100047c6ae99SBarry Smith 
100147c6ae99SBarry Smith     Output Parameter:
100247c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
100347c6ae99SBarry Smith 
100447c6ae99SBarry Smith     Level: developer
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
100747c6ae99SBarry Smith 
100847c6ae99SBarry Smith @*/
10097087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
101047c6ae99SBarry Smith {
101147c6ae99SBarry Smith   PetscFunctionBegin;
101247c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
101347c6ae99SBarry Smith   PetscFunctionReturn(0);
101447c6ae99SBarry Smith }
101547c6ae99SBarry Smith 
101647c6ae99SBarry Smith #undef __FUNCT__
101747c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
101847c6ae99SBarry Smith /*@
101947c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
102047c6ae99SBarry Smith 
102147c6ae99SBarry Smith     Not Collective
102247c6ae99SBarry Smith 
102347c6ae99SBarry Smith     Input Parameter:
102447c6ae99SBarry Smith .   dm - the DM object to destroy
102547c6ae99SBarry Smith 
102647c6ae99SBarry Smith     Output Parameter:
102747c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
102847c6ae99SBarry Smith 
102947c6ae99SBarry Smith     Level: developer
103047c6ae99SBarry Smith 
103147c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
103247c6ae99SBarry Smith 
103347c6ae99SBarry Smith @*/
10347087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
103547c6ae99SBarry Smith {
103647c6ae99SBarry Smith   PetscFunctionBegin;
103747c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
103847c6ae99SBarry Smith   PetscFunctionReturn(0);
103947c6ae99SBarry Smith }
104047c6ae99SBarry Smith 
104147c6ae99SBarry Smith #undef __FUNCT__
104247c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
104347c6ae99SBarry Smith /*@
104447c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
104547c6ae99SBarry Smith 
104647c6ae99SBarry Smith     Not Collective
104747c6ae99SBarry Smith 
104847c6ae99SBarry Smith     Input Parameter:
104947c6ae99SBarry Smith .   dm - the DM object to destroy
105047c6ae99SBarry Smith 
105147c6ae99SBarry Smith     Output Parameter:
105247c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
105347c6ae99SBarry Smith 
105447c6ae99SBarry Smith     Level: developer
105547c6ae99SBarry Smith 
105647c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
105747c6ae99SBarry Smith 
105847c6ae99SBarry Smith @*/
10597087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
106047c6ae99SBarry Smith {
106147c6ae99SBarry Smith   PetscFunctionBegin;
106247c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
106347c6ae99SBarry Smith   PetscFunctionReturn(0);
106447c6ae99SBarry Smith }
106547c6ae99SBarry Smith 
106647c6ae99SBarry Smith #undef __FUNCT__
106747c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
106847c6ae99SBarry Smith /*@
106947c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
107047c6ae99SBarry Smith 
107147c6ae99SBarry Smith     Collective on DM
107247c6ae99SBarry Smith 
107347c6ae99SBarry Smith     Input Parameter:
107447c6ae99SBarry Smith +   dm - the DM object to destroy
107547c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
107647c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
107747c6ae99SBarry Smith 
107847c6ae99SBarry Smith     Level: developer
107947c6ae99SBarry Smith 
108047c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
108147c6ae99SBarry Smith          DMSetJacobian()
108247c6ae99SBarry Smith 
108347c6ae99SBarry Smith @*/
10847087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
108547c6ae99SBarry Smith {
108647c6ae99SBarry Smith   PetscErrorCode ierr;
108747c6ae99SBarry Smith   PetscFunctionBegin;
108847c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
108947c6ae99SBarry Smith   if (!x) x = dm->x;
109047c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
109147c6ae99SBarry Smith   PetscFunctionReturn(0);
109247c6ae99SBarry Smith }
109347c6ae99SBarry Smith 
109447c6ae99SBarry Smith 
109547c6ae99SBarry Smith #undef __FUNCT__
109647c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
109747c6ae99SBarry Smith /*@
109847c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
109947c6ae99SBarry Smith 
110047c6ae99SBarry Smith     Collective on DM
110147c6ae99SBarry Smith 
110247c6ae99SBarry Smith     Input Parameter:
110347c6ae99SBarry Smith +   dm - the DM object
110447c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
110547c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
110647c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
110747c6ae99SBarry Smith 
110847c6ae99SBarry Smith     Level: developer
110947c6ae99SBarry Smith 
111047c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
111147c6ae99SBarry Smith          DMSetFunction()
111247c6ae99SBarry Smith 
111347c6ae99SBarry Smith @*/
11147087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
111547c6ae99SBarry Smith {
111647c6ae99SBarry Smith   PetscErrorCode ierr;
111747c6ae99SBarry Smith 
111847c6ae99SBarry Smith   PetscFunctionBegin;
111947c6ae99SBarry Smith   if (!dm->ops->jacobian) {
112047c6ae99SBarry Smith     ISColoring    coloring;
112147c6ae99SBarry Smith     MatFDColoring fd;
112247c6ae99SBarry Smith 
112347c6ae99SBarry Smith     ierr = DMGetColoring(dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr);
112447c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
112547c6ae99SBarry Smith     ierr = ISColoringDestroy(coloring);CHKERRQ(ierr);
112647c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
112747c6ae99SBarry Smith     dm->fd = fd;
112847c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
112947c6ae99SBarry Smith 
113047c6ae99SBarry Smith     if (!dm->x) {
113147c6ae99SBarry Smith       ierr = MatGetVecs(B,&dm->x,PETSC_NULL);CHKERRQ(ierr);
113247c6ae99SBarry Smith     }
113347c6ae99SBarry Smith   }
113447c6ae99SBarry Smith   if (!x) x = dm->x;
113547c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
113647c6ae99SBarry Smith   PetscFunctionReturn(0);
113747c6ae99SBarry Smith }
1138264ace61SBarry Smith 
1139264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
1140264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
1141264ace61SBarry Smith 
1142264ace61SBarry Smith #undef __FUNCT__
1143264ace61SBarry Smith #define __FUNCT__ "DMSetType"
1144264ace61SBarry Smith /*@C
1145264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
1146264ace61SBarry Smith 
1147264ace61SBarry Smith   Collective on DM
1148264ace61SBarry Smith 
1149264ace61SBarry Smith   Input Parameters:
1150264ace61SBarry Smith + dm     - The DM object
1151264ace61SBarry Smith - method - The name of the DM type
1152264ace61SBarry Smith 
1153264ace61SBarry Smith   Options Database Key:
1154264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
1155264ace61SBarry Smith 
1156264ace61SBarry Smith   Notes:
1157e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
1158264ace61SBarry Smith 
1159264ace61SBarry Smith   Level: intermediate
1160264ace61SBarry Smith 
1161264ace61SBarry Smith .keywords: DM, set, type
1162264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
1163264ace61SBarry Smith @*/
11647087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
1165264ace61SBarry Smith {
1166264ace61SBarry Smith   PetscErrorCode (*r)(DM);
1167264ace61SBarry Smith   PetscBool      match;
1168264ace61SBarry Smith   PetscErrorCode ierr;
1169264ace61SBarry Smith 
1170264ace61SBarry Smith   PetscFunctionBegin;
1171264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1172264ace61SBarry Smith   ierr = PetscTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
1173264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
1174264ace61SBarry Smith 
1175264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
1176264ace61SBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,(void (**)(void)) &r);CHKERRQ(ierr);
1177264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
1178264ace61SBarry Smith 
1179264ace61SBarry Smith   if (dm->ops->destroy) {
1180264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
1181264ace61SBarry Smith   }
1182264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
1183264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
1184264ace61SBarry Smith   PetscFunctionReturn(0);
1185264ace61SBarry Smith }
1186264ace61SBarry Smith 
1187264ace61SBarry Smith #undef __FUNCT__
1188264ace61SBarry Smith #define __FUNCT__ "DMGetType"
1189264ace61SBarry Smith /*@C
1190264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
1191264ace61SBarry Smith 
1192264ace61SBarry Smith   Not Collective
1193264ace61SBarry Smith 
1194264ace61SBarry Smith   Input Parameter:
1195264ace61SBarry Smith . dm  - The DM
1196264ace61SBarry Smith 
1197264ace61SBarry Smith   Output Parameter:
1198264ace61SBarry Smith . type - The DM type name
1199264ace61SBarry Smith 
1200264ace61SBarry Smith   Level: intermediate
1201264ace61SBarry Smith 
1202264ace61SBarry Smith .keywords: DM, get, type, name
1203264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
1204264ace61SBarry Smith @*/
12057087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
1206264ace61SBarry Smith {
1207264ace61SBarry Smith   PetscErrorCode ierr;
1208264ace61SBarry Smith 
1209264ace61SBarry Smith   PetscFunctionBegin;
1210264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1211264ace61SBarry Smith   PetscValidCharPointer(type,2);
1212264ace61SBarry Smith   if (!DMRegisterAllCalled) {
1213264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
1214264ace61SBarry Smith   }
1215264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
1216264ace61SBarry Smith   PetscFunctionReturn(0);
1217264ace61SBarry Smith }
1218264ace61SBarry Smith 
1219*67a56275SMatthew G Knepley #undef __FUNCT__
1220*67a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
1221*67a56275SMatthew G Knepley /*@C
1222*67a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
1223*67a56275SMatthew G Knepley 
1224*67a56275SMatthew G Knepley   Collective on DM
1225*67a56275SMatthew G Knepley 
1226*67a56275SMatthew G Knepley   Input Parameters:
1227*67a56275SMatthew G Knepley + dm - the DM
1228*67a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
1229*67a56275SMatthew G Knepley 
1230*67a56275SMatthew G Knepley   Output Parameter:
1231*67a56275SMatthew G Knepley . M - pointer to new DM
1232*67a56275SMatthew G Knepley 
1233*67a56275SMatthew G Knepley   Notes:
1234*67a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
1235*67a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
1236*67a56275SMatthew G Knepley   of the input DM.
1237*67a56275SMatthew G Knepley 
1238*67a56275SMatthew G Knepley   Level: intermediate
1239*67a56275SMatthew G Knepley 
1240*67a56275SMatthew G Knepley .seealso: DMCreate()
1241*67a56275SMatthew G Knepley @*/
1242*67a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
1243*67a56275SMatthew G Knepley {
1244*67a56275SMatthew G Knepley   DM             B;
1245*67a56275SMatthew G Knepley   char           convname[256];
1246*67a56275SMatthew G Knepley   PetscBool      sametype, issame;
1247*67a56275SMatthew G Knepley   PetscErrorCode ierr;
1248*67a56275SMatthew G Knepley 
1249*67a56275SMatthew G Knepley   PetscFunctionBegin;
1250*67a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1251*67a56275SMatthew G Knepley   PetscValidType(dm,1);
1252*67a56275SMatthew G Knepley   PetscValidPointer(M,3);
1253*67a56275SMatthew G Knepley   ierr = PetscTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
1254*67a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
1255*67a56275SMatthew G Knepley   {
1256*67a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
1257*67a56275SMatthew G Knepley 
1258*67a56275SMatthew G Knepley     /*
1259*67a56275SMatthew G Knepley        Order of precedence:
1260*67a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
1261*67a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
1262*67a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
1263*67a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
1264*67a56275SMatthew G Knepley        5) Use a really basic converter.
1265*67a56275SMatthew G Knepley     */
1266*67a56275SMatthew G Knepley 
1267*67a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
1268*67a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
1269*67a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
1270*67a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
1271*67a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
1272*67a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
1273*67a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
1274*67a56275SMatthew G Knepley     if (conv) goto foundconv;
1275*67a56275SMatthew G Knepley 
1276*67a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
1277*67a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
1278*67a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
1279*67a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
1280*67a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
1281*67a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
1282*67a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
1283*67a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
1284*67a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
1285*67a56275SMatthew G Knepley     if (conv) {
1286*67a56275SMatthew G Knepley       ierr = DMDestroy(B);CHKERRQ(ierr);
1287*67a56275SMatthew G Knepley       goto foundconv;
1288*67a56275SMatthew G Knepley     }
1289*67a56275SMatthew G Knepley 
1290*67a56275SMatthew G Knepley #if 0
1291*67a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
1292*67a56275SMatthew G Knepley     conv = B->ops->convertfrom;
1293*67a56275SMatthew G Knepley     ierr = DMDestroy(B);CHKERRQ(ierr);
1294*67a56275SMatthew G Knepley     if (conv) goto foundconv;
1295*67a56275SMatthew G Knepley 
1296*67a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
1297*67a56275SMatthew G Knepley     if (dm->ops->convert) {
1298*67a56275SMatthew G Knepley       conv = dm->ops->convert;
1299*67a56275SMatthew G Knepley     }
1300*67a56275SMatthew G Knepley     if (conv) goto foundconv;
1301*67a56275SMatthew G Knepley #endif
1302*67a56275SMatthew G Knepley 
1303*67a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
1304*67a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
1305*67a56275SMatthew G Knepley 
1306*67a56275SMatthew G Knepley     foundconv:
1307*67a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
1308*67a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
1309*67a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
1310*67a56275SMatthew G Knepley   }
1311*67a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
1312*67a56275SMatthew G Knepley   PetscFunctionReturn(0);
1313*67a56275SMatthew G Knepley }
1314264ace61SBarry Smith 
1315264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1316264ace61SBarry Smith 
1317264ace61SBarry Smith #undef __FUNCT__
1318264ace61SBarry Smith #define __FUNCT__ "DMRegister"
1319264ace61SBarry Smith /*@C
1320264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
1321264ace61SBarry Smith 
1322264ace61SBarry Smith   Level: advanced
1323264ace61SBarry Smith @*/
13247087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
1325264ace61SBarry Smith {
1326264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
1327264ace61SBarry Smith   PetscErrorCode ierr;
1328264ace61SBarry Smith 
1329264ace61SBarry Smith   PetscFunctionBegin;
1330264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
1331264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
1332264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
1333264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
1334264ace61SBarry Smith   PetscFunctionReturn(0);
1335264ace61SBarry Smith }
1336264ace61SBarry Smith 
1337264ace61SBarry Smith 
1338264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1339264ace61SBarry Smith #undef __FUNCT__
1340264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
1341264ace61SBarry Smith /*@C
1342264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
1343264ace61SBarry Smith 
1344264ace61SBarry Smith    Not Collective
1345264ace61SBarry Smith 
1346264ace61SBarry Smith    Level: advanced
1347264ace61SBarry Smith 
1348264ace61SBarry Smith .keywords: DM, register, destroy
1349264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
1350264ace61SBarry Smith @*/
13517087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
1352264ace61SBarry Smith {
1353264ace61SBarry Smith   PetscErrorCode ierr;
1354264ace61SBarry Smith 
1355264ace61SBarry Smith   PetscFunctionBegin;
1356264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
1357264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
1358264ace61SBarry Smith   PetscFunctionReturn(0);
1359264ace61SBarry Smith }
136023f975d1SBarry Smith 
136123f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
1362c6db04a5SJed Brown #include <mex.h>
136323f975d1SBarry Smith 
13643014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
136523f975d1SBarry Smith 
136623f975d1SBarry Smith #undef __FUNCT__
136723f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
136823f975d1SBarry Smith /*
136923f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
137023f975d1SBarry Smith                          DMSetFunctionMatlab().
137123f975d1SBarry Smith 
137223f975d1SBarry Smith    For linear problems x is null
137323f975d1SBarry Smith 
137423f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
137523f975d1SBarry Smith */
13767087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
137723f975d1SBarry Smith {
137823f975d1SBarry Smith   PetscErrorCode    ierr;
137923f975d1SBarry Smith   DMMatlabContext   *sctx;
138023f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
138123f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
138223f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
138323f975d1SBarry Smith 
138423f975d1SBarry Smith   PetscFunctionBegin;
138523f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
138623f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
138723f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
138823f975d1SBarry Smith 
138923f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
139023f975d1SBarry Smith   ierr = DMGetContext(dm,(void**)&sctx);CHKERRQ(ierr);
139123f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
139223f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
13933014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
139423f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
139523f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
139623f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
139723f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
1398b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
139923f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
140023f975d1SBarry Smith   mxDestroyArray(prhs[0]);
140123f975d1SBarry Smith   mxDestroyArray(prhs[1]);
140223f975d1SBarry Smith   mxDestroyArray(prhs[2]);
140323f975d1SBarry Smith   mxDestroyArray(prhs[3]);
140423f975d1SBarry Smith   mxDestroyArray(plhs[0]);
140523f975d1SBarry Smith   PetscFunctionReturn(0);
140623f975d1SBarry Smith }
140723f975d1SBarry Smith 
140823f975d1SBarry Smith 
140923f975d1SBarry Smith #undef __FUNCT__
141023f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
141123f975d1SBarry Smith /*
141223f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
141323f975d1SBarry Smith 
141423f975d1SBarry Smith */
14157087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
141623f975d1SBarry Smith {
141723f975d1SBarry Smith   PetscErrorCode    ierr;
141823f975d1SBarry Smith   DMMatlabContext   *sctx;
141923f975d1SBarry Smith 
142023f975d1SBarry Smith   PetscFunctionBegin;
142123f975d1SBarry Smith   /* currently sctx is memory bleed */
14223014e516SBarry Smith   ierr = DMGetContext(dm,(void**)&sctx);CHKERRQ(ierr);
14233014e516SBarry Smith   if (!sctx) {
142423f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
14253014e516SBarry Smith   }
142623f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
142723f975d1SBarry Smith   ierr = DMSetContext(dm,sctx);CHKERRQ(ierr);
142823f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
142923f975d1SBarry Smith   PetscFunctionReturn(0);
143023f975d1SBarry Smith }
14313014e516SBarry Smith 
14323014e516SBarry Smith #undef __FUNCT__
14333014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
14343014e516SBarry Smith /*
14353014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
14363014e516SBarry Smith                          DMSetJacobianMatlab().
14373014e516SBarry Smith 
14383014e516SBarry Smith    For linear problems x is null
14393014e516SBarry Smith 
14403014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
14413014e516SBarry Smith */
14427087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
14433014e516SBarry Smith {
14443014e516SBarry Smith   PetscErrorCode    ierr;
14453014e516SBarry Smith   DMMatlabContext   *sctx;
14463014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
14473014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
14483014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
14493014e516SBarry Smith 
14503014e516SBarry Smith   PetscFunctionBegin;
14513014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14523014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
14533014e516SBarry Smith 
1454e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
14553014e516SBarry Smith   ierr = DMGetContext(dm,(void**)&sctx);CHKERRQ(ierr);
14563014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
14573014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
14583014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
14593014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
14603014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
14613014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
14623014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
14633014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
14643014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
1465b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
1466c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
1467c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
14683014e516SBarry Smith   mxDestroyArray(prhs[0]);
14693014e516SBarry Smith   mxDestroyArray(prhs[1]);
14703014e516SBarry Smith   mxDestroyArray(prhs[2]);
14713014e516SBarry Smith   mxDestroyArray(prhs[3]);
14723014e516SBarry Smith   mxDestroyArray(prhs[4]);
14733014e516SBarry Smith   mxDestroyArray(plhs[0]);
14743014e516SBarry Smith   mxDestroyArray(plhs[1]);
14753014e516SBarry Smith   PetscFunctionReturn(0);
14763014e516SBarry Smith }
14773014e516SBarry Smith 
14783014e516SBarry Smith 
14793014e516SBarry Smith #undef __FUNCT__
14803014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
14813014e516SBarry Smith /*
14823014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
14833014e516SBarry Smith 
14843014e516SBarry Smith */
14857087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
14863014e516SBarry Smith {
14873014e516SBarry Smith   PetscErrorCode    ierr;
14883014e516SBarry Smith   DMMatlabContext   *sctx;
14893014e516SBarry Smith 
14903014e516SBarry Smith   PetscFunctionBegin;
14913014e516SBarry Smith   /* currently sctx is memory bleed */
14923014e516SBarry Smith   ierr = DMGetContext(dm,(void**)&sctx);CHKERRQ(ierr);
14933014e516SBarry Smith   if (!sctx) {
14943014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
14953014e516SBarry Smith   }
14963014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
14973014e516SBarry Smith   ierr = DMSetContext(dm,sctx);CHKERRQ(ierr);
14983014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
14993014e516SBarry Smith   PetscFunctionReturn(0);
15003014e516SBarry Smith }
150123f975d1SBarry Smith #endif
1502