xref: /petsc/src/dm/interface/dm.c (revision 1411c6ee19d0933bd2bf6d23611b5e480183c563)
147c6ae99SBarry Smith #define PETSCDM_DLL
247c6ae99SBarry Smith 
3e1589f56SBarry Smith #include "private/dmimpl.h"     /*I      "petscdm.h"     I*/
447c6ae99SBarry Smith 
547c6ae99SBarry Smith #undef __FUNCT__
6a4121054SBarry Smith #define __FUNCT__ "DMCreate"
7a4121054SBarry Smith /*@
8a4121054SBarry Smith   DMCreate - Creates an empty vector object. The type can then be set with DMetType().
9a4121054SBarry Smith 
10a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
11a4121054SBarry Smith    error when you try to use the vector.
12a4121054SBarry Smith 
13a4121054SBarry Smith   Collective on MPI_Comm
14a4121054SBarry Smith 
15a4121054SBarry Smith   Input Parameter:
16a4121054SBarry Smith . comm - The communicator for the DM object
17a4121054SBarry Smith 
18a4121054SBarry Smith   Output Parameter:
19a4121054SBarry Smith . dm - The DM object
20a4121054SBarry Smith 
21a4121054SBarry Smith   Level: beginner
22a4121054SBarry Smith 
23a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
24a4121054SBarry Smith @*/
25*1411c6eeSJed Brown PetscErrorCode PETSCVEC_DLLEXPORT DMCreate(MPI_Comm comm,DM *dm)
26a4121054SBarry Smith {
27a4121054SBarry Smith   DM             v;
28a4121054SBarry Smith   PetscErrorCode ierr;
29a4121054SBarry Smith 
30a4121054SBarry Smith   PetscFunctionBegin;
31*1411c6eeSJed Brown   PetscValidPointer(dm,2);
32*1411c6eeSJed Brown   *dm = PETSC_NULL;
33a4121054SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
34a4121054SBarry Smith   ierr = DMInitializePackage(PETSC_NULL);CHKERRQ(ierr);
35a4121054SBarry Smith #endif
36a4121054SBarry Smith 
37a4121054SBarry Smith   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
38a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
39*1411c6eeSJed Brown 
40*1411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
41*1411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
42*1411c6eeSJed Brown   v->bs           = 1;
43*1411c6eeSJed Brown 
44*1411c6eeSJed Brown   *dm = v;
45a4121054SBarry Smith   PetscFunctionReturn(0);
46a4121054SBarry Smith }
47a4121054SBarry Smith 
48a4121054SBarry Smith 
49a4121054SBarry Smith #undef __FUNCT__
509a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
519a42bb27SBarry Smith /*@C
52564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
539a42bb27SBarry Smith 
54aa219208SBarry Smith    Logically Collective on DMDA
559a42bb27SBarry Smith 
569a42bb27SBarry Smith    Input Parameter:
579a42bb27SBarry Smith +  da - initial distributed array
589a42bb27SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUDA
599a42bb27SBarry Smith 
609a42bb27SBarry Smith    Options Database:
619a42bb27SBarry Smith .   -da_vec_type ctype
629a42bb27SBarry Smith 
639a42bb27SBarry Smith    Level: intermediate
649a42bb27SBarry Smith 
65aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
669a42bb27SBarry Smith @*/
679a42bb27SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMSetVecType(DM da,const VecType ctype)
689a42bb27SBarry Smith {
699a42bb27SBarry Smith   PetscErrorCode ierr;
709a42bb27SBarry Smith 
719a42bb27SBarry Smith   PetscFunctionBegin;
729a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
739a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
749a42bb27SBarry Smith   ierr = PetscStrallocpy(ctype,&da->vectype);CHKERRQ(ierr);
759a42bb27SBarry Smith   PetscFunctionReturn(0);
769a42bb27SBarry Smith }
779a42bb27SBarry Smith 
789a42bb27SBarry Smith #undef __FUNCT__
799a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
809a42bb27SBarry Smith /*@C
819a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
82aa219208SBarry Smith    DMDA options in the database.
839a42bb27SBarry Smith 
84aa219208SBarry Smith    Logically Collective on DMDA
859a42bb27SBarry Smith 
869a42bb27SBarry Smith    Input Parameter:
87aa219208SBarry Smith +  da - the DMDA context
889a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
899a42bb27SBarry Smith 
909a42bb27SBarry Smith    Notes:
919a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
929a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
939a42bb27SBarry Smith 
949a42bb27SBarry Smith    Level: advanced
959a42bb27SBarry Smith 
96aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
979a42bb27SBarry Smith 
989a42bb27SBarry Smith .seealso: DMSetFromOptions()
999a42bb27SBarry Smith @*/
1009a42bb27SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMSetOptionsPrefix(DM dm,const char prefix[])
1019a42bb27SBarry Smith {
1029a42bb27SBarry Smith   PetscErrorCode ierr;
1039a42bb27SBarry Smith 
1049a42bb27SBarry Smith   PetscFunctionBegin;
1059a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1069a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
1079a42bb27SBarry Smith   PetscFunctionReturn(0);
1089a42bb27SBarry Smith }
1099a42bb27SBarry Smith 
1109a42bb27SBarry Smith #undef __FUNCT__
11147c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
11247c6ae99SBarry Smith /*@
113aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
11447c6ae99SBarry Smith 
11547c6ae99SBarry Smith     Collective on DM
11647c6ae99SBarry Smith 
11747c6ae99SBarry Smith     Input Parameter:
11847c6ae99SBarry Smith .   dm - the DM object to destroy
11947c6ae99SBarry Smith 
12047c6ae99SBarry Smith     Level: developer
12147c6ae99SBarry Smith 
12247c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
12347c6ae99SBarry Smith 
12447c6ae99SBarry Smith @*/
12547c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMDestroy(DM dm)
12647c6ae99SBarry Smith {
12747c6ae99SBarry Smith   PetscErrorCode ierr;
12847c6ae99SBarry Smith 
12947c6ae99SBarry Smith   PetscFunctionBegin;
1300c010503SBarry Smith   ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
13147c6ae99SBarry Smith   PetscFunctionReturn(0);
13247c6ae99SBarry Smith }
13347c6ae99SBarry Smith 
13447c6ae99SBarry Smith #undef __FUNCT__
135d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
136d7bf68aeSBarry Smith /*@
137d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
138d7bf68aeSBarry Smith 
139d7bf68aeSBarry Smith     Collective on DM
140d7bf68aeSBarry Smith 
141d7bf68aeSBarry Smith     Input Parameter:
142d7bf68aeSBarry Smith .   dm - the DM object to setup
143d7bf68aeSBarry Smith 
144d7bf68aeSBarry Smith     Level: developer
145d7bf68aeSBarry Smith 
146d7bf68aeSBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
147d7bf68aeSBarry Smith 
148d7bf68aeSBarry Smith @*/
149d7bf68aeSBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMSetUp(DM dm)
150d7bf68aeSBarry Smith {
151d7bf68aeSBarry Smith   PetscErrorCode ierr;
152d7bf68aeSBarry Smith 
153d7bf68aeSBarry Smith   PetscFunctionBegin;
154d7bf68aeSBarry Smith   if (dm->ops->setup) {
155d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
156d7bf68aeSBarry Smith   }
157d7bf68aeSBarry Smith   PetscFunctionReturn(0);
158d7bf68aeSBarry Smith }
159d7bf68aeSBarry Smith 
160d7bf68aeSBarry Smith #undef __FUNCT__
161d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
162d7bf68aeSBarry Smith /*@
163d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
164d7bf68aeSBarry Smith 
165d7bf68aeSBarry Smith     Collective on DM
166d7bf68aeSBarry Smith 
167d7bf68aeSBarry Smith     Input Parameter:
168d7bf68aeSBarry Smith .   dm - the DM object to set options for
169d7bf68aeSBarry Smith 
170d7bf68aeSBarry Smith     Level: developer
171d7bf68aeSBarry Smith 
172d7bf68aeSBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
173d7bf68aeSBarry Smith 
174d7bf68aeSBarry Smith @*/
175d7bf68aeSBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMSetFromOptions(DM dm)
176d7bf68aeSBarry Smith {
177d7bf68aeSBarry Smith   PetscErrorCode ierr;
178d7bf68aeSBarry Smith 
179d7bf68aeSBarry Smith   PetscFunctionBegin;
180d7bf68aeSBarry Smith   if (dm->ops->setfromoptions) {
181d7bf68aeSBarry Smith     ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
182d7bf68aeSBarry Smith   }
183d7bf68aeSBarry Smith   PetscFunctionReturn(0);
184d7bf68aeSBarry Smith }
185d7bf68aeSBarry Smith 
186d7bf68aeSBarry Smith #undef __FUNCT__
18747c6ae99SBarry Smith #define __FUNCT__ "DMView"
18847c6ae99SBarry Smith /*@
189aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
19047c6ae99SBarry Smith 
19147c6ae99SBarry Smith     Collective on DM
19247c6ae99SBarry Smith 
19347c6ae99SBarry Smith     Input Parameter:
19447c6ae99SBarry Smith +   dm - the DM object to view
19547c6ae99SBarry Smith -   v - the viewer
19647c6ae99SBarry Smith 
19747c6ae99SBarry Smith     Level: developer
19847c6ae99SBarry Smith 
19947c6ae99SBarry Smith .seealso DMDestroy(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
20047c6ae99SBarry Smith 
20147c6ae99SBarry Smith @*/
20247c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMView(DM dm,PetscViewer v)
20347c6ae99SBarry Smith {
20447c6ae99SBarry Smith   PetscErrorCode ierr;
20547c6ae99SBarry Smith 
20647c6ae99SBarry Smith   PetscFunctionBegin;
2073014e516SBarry Smith  if (!v) {
2083014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
2093014e516SBarry Smith   }
2100c010503SBarry Smith   if (dm->ops->view) {
2110c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
21247c6ae99SBarry Smith   }
21347c6ae99SBarry Smith   PetscFunctionReturn(0);
21447c6ae99SBarry Smith }
21547c6ae99SBarry Smith 
21647c6ae99SBarry Smith #undef __FUNCT__
21747c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
21847c6ae99SBarry Smith /*@
219aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
22047c6ae99SBarry Smith 
22147c6ae99SBarry Smith     Collective on DM
22247c6ae99SBarry Smith 
22347c6ae99SBarry Smith     Input Parameter:
22447c6ae99SBarry Smith .   dm - the DM object
22547c6ae99SBarry Smith 
22647c6ae99SBarry Smith     Output Parameter:
22747c6ae99SBarry Smith .   vec - the global vector
22847c6ae99SBarry Smith 
22947c6ae99SBarry Smith     Level: developer
23047c6ae99SBarry Smith 
23147c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
23247c6ae99SBarry Smith 
23347c6ae99SBarry Smith @*/
23447c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMCreateGlobalVector(DM dm,Vec *vec)
23547c6ae99SBarry Smith {
23647c6ae99SBarry Smith   PetscErrorCode ierr;
23747c6ae99SBarry Smith 
23847c6ae99SBarry Smith   PetscFunctionBegin;
23947c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
24047c6ae99SBarry Smith   PetscFunctionReturn(0);
24147c6ae99SBarry Smith }
24247c6ae99SBarry Smith 
24347c6ae99SBarry Smith #undef __FUNCT__
24447c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
24547c6ae99SBarry Smith /*@
246aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
24747c6ae99SBarry Smith 
24847c6ae99SBarry Smith     Not Collective
24947c6ae99SBarry Smith 
25047c6ae99SBarry Smith     Input Parameter:
25147c6ae99SBarry Smith .   dm - the DM object
25247c6ae99SBarry Smith 
25347c6ae99SBarry Smith     Output Parameter:
25447c6ae99SBarry Smith .   vec - the local vector
25547c6ae99SBarry Smith 
25647c6ae99SBarry Smith     Level: developer
25747c6ae99SBarry Smith 
25847c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix()
25947c6ae99SBarry Smith 
26047c6ae99SBarry Smith @*/
26147c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMCreateLocalVector(DM dm,Vec *vec)
26247c6ae99SBarry Smith {
26347c6ae99SBarry Smith   PetscErrorCode ierr;
26447c6ae99SBarry Smith 
26547c6ae99SBarry Smith   PetscFunctionBegin;
26647c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
26747c6ae99SBarry Smith   PetscFunctionReturn(0);
26847c6ae99SBarry Smith }
26947c6ae99SBarry Smith 
27047c6ae99SBarry Smith #undef __FUNCT__
271*1411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
272*1411c6eeSJed Brown /*@
273*1411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
274*1411c6eeSJed Brown 
275*1411c6eeSJed Brown    Collective on DM
276*1411c6eeSJed Brown 
277*1411c6eeSJed Brown    Input Parameter:
278*1411c6eeSJed Brown .  dm - the DM that provides the mapping
279*1411c6eeSJed Brown 
280*1411c6eeSJed Brown    Output Parameter:
281*1411c6eeSJed Brown .  ltog - the mapping
282*1411c6eeSJed Brown 
283*1411c6eeSJed Brown    Level: intermediate
284*1411c6eeSJed Brown 
285*1411c6eeSJed Brown    Notes:
286*1411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
287*1411c6eeSJed Brown    MatSetLocalToGlobalMapping().
288*1411c6eeSJed Brown 
289*1411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
290*1411c6eeSJed Brown @*/
291*1411c6eeSJed Brown PetscErrorCode PETSCDM_DLLEXPORT DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
292*1411c6eeSJed Brown {
293*1411c6eeSJed Brown   PetscErrorCode ierr;
294*1411c6eeSJed Brown 
295*1411c6eeSJed Brown   PetscFunctionBegin;
296*1411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
297*1411c6eeSJed Brown   PetscValidPointer(ltog,2);
298*1411c6eeSJed Brown   if (!dm->ltogmap) {
299*1411c6eeSJed Brown     if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
300*1411c6eeSJed Brown     ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
301*1411c6eeSJed Brown   }
302*1411c6eeSJed Brown   *ltog = dm->ltogmap;
303*1411c6eeSJed Brown   PetscFunctionReturn(0);
304*1411c6eeSJed Brown }
305*1411c6eeSJed Brown 
306*1411c6eeSJed Brown #undef __FUNCT__
307*1411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
308*1411c6eeSJed Brown /*@
309*1411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
310*1411c6eeSJed Brown 
311*1411c6eeSJed Brown    Collective on DM
312*1411c6eeSJed Brown 
313*1411c6eeSJed Brown    Input Parameter:
314*1411c6eeSJed Brown .  da - the distributed array that provides the mapping
315*1411c6eeSJed Brown 
316*1411c6eeSJed Brown    Output Parameter:
317*1411c6eeSJed Brown .  ltog - the block mapping
318*1411c6eeSJed Brown 
319*1411c6eeSJed Brown    Level: intermediate
320*1411c6eeSJed Brown 
321*1411c6eeSJed Brown    Notes:
322*1411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
323*1411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
324*1411c6eeSJed Brown 
325*1411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
326*1411c6eeSJed Brown @*/
327*1411c6eeSJed Brown PetscErrorCode PETSCDM_DLLEXPORT DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
328*1411c6eeSJed Brown {
329*1411c6eeSJed Brown   PetscErrorCode ierr;
330*1411c6eeSJed Brown 
331*1411c6eeSJed Brown   PetscFunctionBegin;
332*1411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
333*1411c6eeSJed Brown   PetscValidPointer(ltog,2);
334*1411c6eeSJed Brown   if (!dm->ltogmapb) {
335*1411c6eeSJed Brown     PetscInt bs;
336*1411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
337*1411c6eeSJed Brown     if (bs > 1) {
338*1411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
339*1411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
340*1411c6eeSJed Brown     } else {
341*1411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
342*1411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
343*1411c6eeSJed Brown     }
344*1411c6eeSJed Brown   }
345*1411c6eeSJed Brown   *ltog = dm->ltogmapb;
346*1411c6eeSJed Brown   PetscFunctionReturn(0);
347*1411c6eeSJed Brown }
348*1411c6eeSJed Brown 
349*1411c6eeSJed Brown #undef __FUNCT__
350*1411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
351*1411c6eeSJed Brown /*@
352*1411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
353*1411c6eeSJed Brown 
354*1411c6eeSJed Brown    Not Collective
355*1411c6eeSJed Brown 
356*1411c6eeSJed Brown    Input Parameter:
357*1411c6eeSJed Brown .  dm - the DM with block structure
358*1411c6eeSJed Brown 
359*1411c6eeSJed Brown    Output Parameter:
360*1411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
361*1411c6eeSJed Brown 
362*1411c6eeSJed Brown    Level: intermediate
363*1411c6eeSJed Brown 
364*1411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
365*1411c6eeSJed Brown @*/
366*1411c6eeSJed Brown PetscErrorCode PETSCDM_DLLEXPORT DMGetBlockSize(DM dm,PetscInt *bs)
367*1411c6eeSJed Brown {
368*1411c6eeSJed Brown   PetscFunctionBegin;
369*1411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
370*1411c6eeSJed Brown   PetscValidPointer(bs,2);
371*1411c6eeSJed 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");
372*1411c6eeSJed Brown   *bs = dm->bs;
373*1411c6eeSJed Brown   PetscFunctionReturn(0);
374*1411c6eeSJed Brown }
375*1411c6eeSJed Brown 
376*1411c6eeSJed Brown #undef __FUNCT__
37747c6ae99SBarry Smith #define __FUNCT__ "DMGetInterpolation"
37847c6ae99SBarry Smith /*@
379aa219208SBarry Smith     DMGetInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
38047c6ae99SBarry Smith 
38147c6ae99SBarry Smith     Collective on DM
38247c6ae99SBarry Smith 
38347c6ae99SBarry Smith     Input Parameter:
38447c6ae99SBarry Smith +   dm1 - the DM object
38547c6ae99SBarry Smith -   dm2 - the second, finer DM object
38647c6ae99SBarry Smith 
38747c6ae99SBarry Smith     Output Parameter:
38847c6ae99SBarry Smith +  mat - the interpolation
38947c6ae99SBarry Smith -  vec - the scaling (optional)
39047c6ae99SBarry Smith 
39147c6ae99SBarry Smith     Level: developer
39247c6ae99SBarry Smith 
39347c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetColoring(), DMGetMatrix()
39447c6ae99SBarry Smith 
39547c6ae99SBarry Smith @*/
39647c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMGetInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
39747c6ae99SBarry Smith {
39847c6ae99SBarry Smith   PetscErrorCode ierr;
39947c6ae99SBarry Smith 
40047c6ae99SBarry Smith   PetscFunctionBegin;
40147c6ae99SBarry Smith   ierr = (*dm1->ops->getinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
40247c6ae99SBarry Smith   PetscFunctionReturn(0);
40347c6ae99SBarry Smith }
40447c6ae99SBarry Smith 
40547c6ae99SBarry Smith #undef __FUNCT__
40647c6ae99SBarry Smith #define __FUNCT__ "DMGetInjection"
40747c6ae99SBarry Smith /*@
408aa219208SBarry Smith     DMGetInjection - Gets injection matrix between two DMDA or DMComposite objects
40947c6ae99SBarry Smith 
41047c6ae99SBarry Smith     Collective on DM
41147c6ae99SBarry Smith 
41247c6ae99SBarry Smith     Input Parameter:
41347c6ae99SBarry Smith +   dm1 - the DM object
41447c6ae99SBarry Smith -   dm2 - the second, finer DM object
41547c6ae99SBarry Smith 
41647c6ae99SBarry Smith     Output Parameter:
41747c6ae99SBarry Smith .   ctx - the injection
41847c6ae99SBarry Smith 
41947c6ae99SBarry Smith     Level: developer
42047c6ae99SBarry Smith 
42147c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetColoring(), DMGetMatrix(), DMGetInterpolation()
42247c6ae99SBarry Smith 
42347c6ae99SBarry Smith @*/
42447c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMGetInjection(DM dm1,DM dm2,VecScatter *ctx)
42547c6ae99SBarry Smith {
42647c6ae99SBarry Smith   PetscErrorCode ierr;
42747c6ae99SBarry Smith 
42847c6ae99SBarry Smith   PetscFunctionBegin;
42947c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
43047c6ae99SBarry Smith   PetscFunctionReturn(0);
43147c6ae99SBarry Smith }
43247c6ae99SBarry Smith 
43347c6ae99SBarry Smith #undef __FUNCT__
43447c6ae99SBarry Smith #define __FUNCT__ "DMGetColoring"
43547c6ae99SBarry Smith /*@
436aa219208SBarry Smith     DMGetColoring - Gets coloring for a DMDA or DMComposite
43747c6ae99SBarry Smith 
43847c6ae99SBarry Smith     Collective on DM
43947c6ae99SBarry Smith 
44047c6ae99SBarry Smith     Input Parameter:
44147c6ae99SBarry Smith +   dm - the DM object
44247c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
44347c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
44447c6ae99SBarry Smith 
44547c6ae99SBarry Smith     Output Parameter:
44647c6ae99SBarry Smith .   coloring - the coloring
44747c6ae99SBarry Smith 
44847c6ae99SBarry Smith     Level: developer
44947c6ae99SBarry Smith 
45047c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetMatrix()
45147c6ae99SBarry Smith 
45247c6ae99SBarry Smith @*/
45347c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMGetColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
45447c6ae99SBarry Smith {
45547c6ae99SBarry Smith   PetscErrorCode ierr;
45647c6ae99SBarry Smith 
45747c6ae99SBarry Smith   PetscFunctionBegin;
45847c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
45947c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
46047c6ae99SBarry Smith   PetscFunctionReturn(0);
46147c6ae99SBarry Smith }
46247c6ae99SBarry Smith 
46347c6ae99SBarry Smith #undef __FUNCT__
46447c6ae99SBarry Smith #define __FUNCT__ "DMGetMatrix"
46547c6ae99SBarry Smith /*@C
466aa219208SBarry Smith     DMGetMatrix - Gets empty Jacobian for a DMDA or DMComposite
46747c6ae99SBarry Smith 
46847c6ae99SBarry Smith     Collective on DM
46947c6ae99SBarry Smith 
47047c6ae99SBarry Smith     Input Parameter:
47147c6ae99SBarry Smith +   dm - the DM object
47247c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
47394013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
47447c6ae99SBarry Smith 
47547c6ae99SBarry Smith     Output Parameter:
47647c6ae99SBarry Smith .   mat - the empty Jacobian
47747c6ae99SBarry Smith 
47847c6ae99SBarry Smith     Level: developer
47947c6ae99SBarry Smith 
48094013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
48194013140SBarry Smith        do not need to do it yourself.
48294013140SBarry Smith 
48394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
484aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
48594013140SBarry Smith 
48694013140SBarry 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
48794013140SBarry Smith        internally by PETSc.
48894013140SBarry Smith 
48994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
490aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
49194013140SBarry Smith 
49247c6ae99SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetMatrix()
49347c6ae99SBarry Smith 
49447c6ae99SBarry Smith @*/
49547c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMGetMatrix(DM dm, const MatType mtype,Mat *mat)
49647c6ae99SBarry Smith {
49747c6ae99SBarry Smith   PetscErrorCode ierr;
49847c6ae99SBarry Smith 
49947c6ae99SBarry Smith   PetscFunctionBegin;
50047c6ae99SBarry Smith   ierr = (*dm->ops->getmatrix)(dm,mtype,mat);CHKERRQ(ierr);
50147c6ae99SBarry Smith   PetscFunctionReturn(0);
50247c6ae99SBarry Smith }
50347c6ae99SBarry Smith 
50447c6ae99SBarry Smith #undef __FUNCT__
50547c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
50647c6ae99SBarry Smith /*@
50747c6ae99SBarry Smith     DMRefine - Refines a DM object
50847c6ae99SBarry Smith 
50947c6ae99SBarry Smith     Collective on DM
51047c6ae99SBarry Smith 
51147c6ae99SBarry Smith     Input Parameter:
51247c6ae99SBarry Smith +   dm - the DM object
51347c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
51447c6ae99SBarry Smith 
51547c6ae99SBarry Smith     Output Parameter:
51647c6ae99SBarry Smith .   dmf - the refined DM
51747c6ae99SBarry Smith 
51847c6ae99SBarry Smith     Level: developer
51947c6ae99SBarry Smith 
52047c6ae99SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
52147c6ae99SBarry Smith 
52247c6ae99SBarry Smith @*/
52347c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMRefine(DM dm,MPI_Comm comm,DM *dmf)
52447c6ae99SBarry Smith {
52547c6ae99SBarry Smith   PetscErrorCode ierr;
52647c6ae99SBarry Smith 
52747c6ae99SBarry Smith   PetscFunctionBegin;
52847c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
52947c6ae99SBarry Smith   PetscFunctionReturn(0);
53047c6ae99SBarry Smith }
53147c6ae99SBarry Smith 
53247c6ae99SBarry Smith #undef __FUNCT__
53347c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
53447c6ae99SBarry Smith /*@
53547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
53647c6ae99SBarry Smith 
53747c6ae99SBarry Smith     Neighbor-wise Collective on DM
53847c6ae99SBarry Smith 
53947c6ae99SBarry Smith     Input Parameters:
54047c6ae99SBarry Smith +   dm - the DM object
54147c6ae99SBarry Smith .   g - the global vector
54247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
54347c6ae99SBarry Smith -   l - the local vector
54447c6ae99SBarry Smith 
54547c6ae99SBarry Smith 
54647c6ae99SBarry Smith     Level: beginner
54747c6ae99SBarry Smith 
5489a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
54947c6ae99SBarry Smith 
55047c6ae99SBarry Smith @*/
55147c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
55247c6ae99SBarry Smith {
55347c6ae99SBarry Smith   PetscErrorCode ierr;
55447c6ae99SBarry Smith 
55547c6ae99SBarry Smith   PetscFunctionBegin;
55647c6ae99SBarry Smith   ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode,l);CHKERRQ(ierr);
55747c6ae99SBarry Smith   PetscFunctionReturn(0);
55847c6ae99SBarry Smith }
55947c6ae99SBarry Smith 
56047c6ae99SBarry Smith #undef __FUNCT__
56147c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
56247c6ae99SBarry Smith /*@
56347c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
56447c6ae99SBarry Smith 
56547c6ae99SBarry Smith     Neighbor-wise Collective on DM
56647c6ae99SBarry Smith 
56747c6ae99SBarry Smith     Input Parameters:
56847c6ae99SBarry Smith +   dm - the DM object
56947c6ae99SBarry Smith .   g - the global vector
57047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
57147c6ae99SBarry Smith -   l - the local vector
57247c6ae99SBarry Smith 
57347c6ae99SBarry Smith 
57447c6ae99SBarry Smith     Level: beginner
57547c6ae99SBarry Smith 
5769a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
57747c6ae99SBarry Smith 
57847c6ae99SBarry Smith @*/
57947c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
58047c6ae99SBarry Smith {
58147c6ae99SBarry Smith   PetscErrorCode ierr;
58247c6ae99SBarry Smith 
58347c6ae99SBarry Smith   PetscFunctionBegin;
58447c6ae99SBarry Smith   ierr = (*dm->ops->globaltolocalend)(dm,g,mode,l);CHKERRQ(ierr);
58547c6ae99SBarry Smith   PetscFunctionReturn(0);
58647c6ae99SBarry Smith }
58747c6ae99SBarry Smith 
58847c6ae99SBarry Smith #undef __FUNCT__
5899a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
59047c6ae99SBarry Smith /*@
5919a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
5929a42bb27SBarry Smith 
5939a42bb27SBarry Smith     Neighbor-wise Collective on DM
5949a42bb27SBarry Smith 
5959a42bb27SBarry Smith     Input Parameters:
5969a42bb27SBarry Smith +   dm - the DM object
597f6813fd5SJed Brown .   l - the local vector
5989a42bb27SBarry 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
5999a42bb27SBarry Smith            base point.
600f6813fd5SJed Brown - - the global vector
6019a42bb27SBarry Smith 
6029a42bb27SBarry 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
6039a42bb27SBarry 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
6049a42bb27SBarry Smith            global array to the final global array with VecAXPY().
6059a42bb27SBarry Smith 
6069a42bb27SBarry Smith     Level: beginner
6079a42bb27SBarry Smith 
6089a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
6099a42bb27SBarry Smith 
6109a42bb27SBarry Smith @*/
611f6813fd5SJed Brown PetscErrorCode PETSCDM_DLLEXPORT DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
6129a42bb27SBarry Smith {
6139a42bb27SBarry Smith   PetscErrorCode ierr;
6149a42bb27SBarry Smith 
6159a42bb27SBarry Smith   PetscFunctionBegin;
616f6813fd5SJed Brown   ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode,g);CHKERRQ(ierr);
6179a42bb27SBarry Smith   PetscFunctionReturn(0);
6189a42bb27SBarry Smith }
6199a42bb27SBarry Smith 
6209a42bb27SBarry Smith #undef __FUNCT__
6219a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
6229a42bb27SBarry Smith /*@
6239a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith     Neighbor-wise Collective on DM
62647c6ae99SBarry Smith 
62747c6ae99SBarry Smith     Input Parameters:
62847c6ae99SBarry Smith +   dm - the DM object
629f6813fd5SJed Brown .   l - the local vector
63047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
631f6813fd5SJed Brown -   g - the global vector
63247c6ae99SBarry Smith 
63347c6ae99SBarry Smith 
63447c6ae99SBarry Smith     Level: beginner
63547c6ae99SBarry Smith 
6369a42bb27SBarry Smith .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
63747c6ae99SBarry Smith 
63847c6ae99SBarry Smith @*/
639f6813fd5SJed Brown PetscErrorCode PETSCDM_DLLEXPORT DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
64047c6ae99SBarry Smith {
64147c6ae99SBarry Smith   PetscErrorCode ierr;
64247c6ae99SBarry Smith 
64347c6ae99SBarry Smith   PetscFunctionBegin;
644f6813fd5SJed Brown   ierr = (*dm->ops->localtoglobalend)(dm,l,mode,g);CHKERRQ(ierr);
64547c6ae99SBarry Smith   PetscFunctionReturn(0);
64647c6ae99SBarry Smith }
64747c6ae99SBarry Smith 
64847c6ae99SBarry Smith #undef __FUNCT__
64947c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
65047c6ae99SBarry Smith /*@
65147c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
65247c6ae99SBarry Smith 
65347c6ae99SBarry Smith     Collective on DM
65447c6ae99SBarry Smith 
65547c6ae99SBarry Smith     Input Parameter:
65647c6ae99SBarry Smith +   dm - the DM object
65747c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
65847c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
65947c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
66047c6ae99SBarry Smith 
66147c6ae99SBarry Smith     Level: developer
66247c6ae99SBarry Smith 
66347c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
66447c6ae99SBarry Smith          DMSetFunction()
66547c6ae99SBarry Smith 
66647c6ae99SBarry Smith @*/
66747c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
66847c6ae99SBarry Smith {
66947c6ae99SBarry Smith   PetscErrorCode ierr;
67047c6ae99SBarry Smith   PetscFunctionBegin;
67147c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
67247c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
67347c6ae99SBarry Smith   if (A != B) {
67447c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
67547c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
67647c6ae99SBarry Smith   }
67747c6ae99SBarry Smith   PetscFunctionReturn(0);
67847c6ae99SBarry Smith }
67947c6ae99SBarry Smith 
68047c6ae99SBarry Smith #undef __FUNCT__
68147c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
68247c6ae99SBarry Smith /*@
68347c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
68447c6ae99SBarry Smith 
68547c6ae99SBarry Smith     Collective on DM
68647c6ae99SBarry Smith 
68747c6ae99SBarry Smith     Input Parameter:
68847c6ae99SBarry Smith +   dm - the DM object
68947c6ae99SBarry Smith -   comm - the communicator to contain the new DM object (or PETSC_NULL)
69047c6ae99SBarry Smith 
69147c6ae99SBarry Smith     Output Parameter:
69247c6ae99SBarry Smith .   dmc - the coarsened DM
69347c6ae99SBarry Smith 
69447c6ae99SBarry Smith     Level: developer
69547c6ae99SBarry Smith 
69647c6ae99SBarry Smith .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
69747c6ae99SBarry Smith 
69847c6ae99SBarry Smith @*/
69947c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
70047c6ae99SBarry Smith {
70147c6ae99SBarry Smith   PetscErrorCode ierr;
70247c6ae99SBarry Smith 
70347c6ae99SBarry Smith   PetscFunctionBegin;
70447c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
70547c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
70647c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
70747c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
70847c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
70947c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
71047c6ae99SBarry Smith   }
71147c6ae99SBarry Smith   PetscFunctionReturn(0);
71247c6ae99SBarry Smith }
71347c6ae99SBarry Smith 
71447c6ae99SBarry Smith #undef __FUNCT__
71547c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
71647c6ae99SBarry Smith /*@C
71747c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
71847c6ae99SBarry Smith 
71947c6ae99SBarry Smith     Collective on DM
72047c6ae99SBarry Smith 
72147c6ae99SBarry Smith     Input Parameter:
72247c6ae99SBarry Smith +   dm - the DM object
72347c6ae99SBarry Smith -   nlevels - the number of levels of refinement
72447c6ae99SBarry Smith 
72547c6ae99SBarry Smith     Output Parameter:
72647c6ae99SBarry Smith .   dmf - the refined DM hierarchy
72747c6ae99SBarry Smith 
72847c6ae99SBarry Smith     Level: developer
72947c6ae99SBarry Smith 
73047c6ae99SBarry Smith .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
73147c6ae99SBarry Smith 
73247c6ae99SBarry Smith @*/
73347c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
73447c6ae99SBarry Smith {
73547c6ae99SBarry Smith   PetscErrorCode ierr;
73647c6ae99SBarry Smith 
73747c6ae99SBarry Smith   PetscFunctionBegin;
73847c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
73947c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
74047c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
74147c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
74247c6ae99SBarry Smith   } else if (dm->ops->refine) {
74347c6ae99SBarry Smith     PetscInt i;
74447c6ae99SBarry Smith 
74547c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
74647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
74747c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
74847c6ae99SBarry Smith     }
74947c6ae99SBarry Smith   } else {
75047c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
75147c6ae99SBarry Smith   }
75247c6ae99SBarry Smith   PetscFunctionReturn(0);
75347c6ae99SBarry Smith }
75447c6ae99SBarry Smith 
75547c6ae99SBarry Smith #undef __FUNCT__
75647c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
75747c6ae99SBarry Smith /*@C
75847c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
75947c6ae99SBarry Smith 
76047c6ae99SBarry Smith     Collective on DM
76147c6ae99SBarry Smith 
76247c6ae99SBarry Smith     Input Parameter:
76347c6ae99SBarry Smith +   dm - the DM object
76447c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
76547c6ae99SBarry Smith 
76647c6ae99SBarry Smith     Output Parameter:
76747c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
76847c6ae99SBarry Smith 
76947c6ae99SBarry Smith     Level: developer
77047c6ae99SBarry Smith 
77147c6ae99SBarry Smith .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMGetInterpolation()
77247c6ae99SBarry Smith 
77347c6ae99SBarry Smith @*/
77447c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
77547c6ae99SBarry Smith {
77647c6ae99SBarry Smith   PetscErrorCode ierr;
77747c6ae99SBarry Smith 
77847c6ae99SBarry Smith   PetscFunctionBegin;
77947c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
78047c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
78147c6ae99SBarry Smith   PetscValidPointer(dmc,3);
78247c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
78347c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
78447c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
78547c6ae99SBarry Smith     PetscInt i;
78647c6ae99SBarry Smith 
78747c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
78847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
78947c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
79047c6ae99SBarry Smith     }
79147c6ae99SBarry Smith   } else {
79247c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
79347c6ae99SBarry Smith   }
79447c6ae99SBarry Smith   PetscFunctionReturn(0);
79547c6ae99SBarry Smith }
79647c6ae99SBarry Smith 
79747c6ae99SBarry Smith #undef __FUNCT__
79847c6ae99SBarry Smith #define __FUNCT__ "DMGetAggregates"
79947c6ae99SBarry Smith /*@
80047c6ae99SBarry Smith    DMGetAggregates - Gets the aggregates that map between
80147c6ae99SBarry Smith    grids associated with two DMs.
80247c6ae99SBarry Smith 
80347c6ae99SBarry Smith    Collective on DM
80447c6ae99SBarry Smith 
80547c6ae99SBarry Smith    Input Parameters:
80647c6ae99SBarry Smith +  dmc - the coarse grid DM
80747c6ae99SBarry Smith -  dmf - the fine grid DM
80847c6ae99SBarry Smith 
80947c6ae99SBarry Smith    Output Parameters:
81047c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
81147c6ae99SBarry Smith 
81247c6ae99SBarry Smith    Level: intermediate
81347c6ae99SBarry Smith 
81447c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
81547c6ae99SBarry Smith 
81647c6ae99SBarry Smith .seealso: DMRefine(), DMGetInjection(), DMGetInterpolation()
81747c6ae99SBarry Smith @*/
81847c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMGetAggregates(DM dmc, DM dmf, Mat *rest)
81947c6ae99SBarry Smith {
82047c6ae99SBarry Smith   PetscErrorCode ierr;
82147c6ae99SBarry Smith 
82247c6ae99SBarry Smith   PetscFunctionBegin;
82347c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
82447c6ae99SBarry Smith   PetscFunctionReturn(0);
82547c6ae99SBarry Smith }
82647c6ae99SBarry Smith 
82747c6ae99SBarry Smith #undef __FUNCT__
82847c6ae99SBarry Smith #define __FUNCT__ "DMSetContext"
82947c6ae99SBarry Smith /*@
83047c6ae99SBarry Smith     DMSetContext - Set a user context into a DM object
83147c6ae99SBarry Smith 
83247c6ae99SBarry Smith     Not Collective
83347c6ae99SBarry Smith 
83447c6ae99SBarry Smith     Input Parameters:
83547c6ae99SBarry Smith +   dm - the DM object
83647c6ae99SBarry Smith -   ctx - the user context
83747c6ae99SBarry Smith 
83847c6ae99SBarry Smith     Level: intermediate
83947c6ae99SBarry Smith 
84047c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext()
84147c6ae99SBarry Smith 
84247c6ae99SBarry Smith @*/
84347c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMSetContext(DM dm,void *ctx)
84447c6ae99SBarry Smith {
84547c6ae99SBarry Smith   PetscFunctionBegin;
84647c6ae99SBarry Smith   dm->ctx = ctx;
84747c6ae99SBarry Smith   PetscFunctionReturn(0);
84847c6ae99SBarry Smith }
84947c6ae99SBarry Smith 
85047c6ae99SBarry Smith #undef __FUNCT__
85147c6ae99SBarry Smith #define __FUNCT__ "DMGetContext"
85247c6ae99SBarry Smith /*@
85347c6ae99SBarry Smith     DMGetContext - Gets a user context from a DM object
85447c6ae99SBarry Smith 
85547c6ae99SBarry Smith     Not Collective
85647c6ae99SBarry Smith 
85747c6ae99SBarry Smith     Input Parameter:
85847c6ae99SBarry Smith .   dm - the DM object
85947c6ae99SBarry Smith 
86047c6ae99SBarry Smith     Output Parameter:
86147c6ae99SBarry Smith .   ctx - the user context
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith     Level: intermediate
86447c6ae99SBarry Smith 
86547c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext()
86647c6ae99SBarry Smith 
86747c6ae99SBarry Smith @*/
86847c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMGetContext(DM dm,void **ctx)
86947c6ae99SBarry Smith {
87047c6ae99SBarry Smith   PetscFunctionBegin;
87147c6ae99SBarry Smith   *ctx = dm->ctx;
87247c6ae99SBarry Smith   PetscFunctionReturn(0);
87347c6ae99SBarry Smith }
87447c6ae99SBarry Smith 
87547c6ae99SBarry Smith #undef __FUNCT__
87647c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
87747c6ae99SBarry Smith /*@
87847c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
87947c6ae99SBarry Smith 
88047c6ae99SBarry Smith     Logically Collective on DM
88147c6ae99SBarry Smith 
88247c6ae99SBarry Smith     Input Parameter:
88347c6ae99SBarry Smith +   dm - the DM object to destroy
88447c6ae99SBarry Smith -   f - the function to compute the initial guess
88547c6ae99SBarry Smith 
88647c6ae99SBarry Smith     Level: intermediate
88747c6ae99SBarry Smith 
88847c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
88947c6ae99SBarry Smith 
89047c6ae99SBarry Smith @*/
89147c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
89247c6ae99SBarry Smith {
89347c6ae99SBarry Smith   PetscFunctionBegin;
89447c6ae99SBarry Smith   dm->ops->initialguess = f;
89547c6ae99SBarry Smith   PetscFunctionReturn(0);
89647c6ae99SBarry Smith }
89747c6ae99SBarry Smith 
89847c6ae99SBarry Smith #undef __FUNCT__
89947c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
90047c6ae99SBarry Smith /*@
90147c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
90247c6ae99SBarry Smith 
90347c6ae99SBarry Smith     Logically Collective on DM
90447c6ae99SBarry Smith 
90547c6ae99SBarry Smith     Input Parameter:
90647c6ae99SBarry Smith +   dm - the DM object
90747c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
90847c6ae99SBarry Smith 
90947c6ae99SBarry Smith     Level: intermediate
91047c6ae99SBarry Smith 
91147c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
91247c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
91347c6ae99SBarry Smith 
91447c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
91547c6ae99SBarry Smith          DMSetJacobian()
91647c6ae99SBarry Smith 
91747c6ae99SBarry Smith @*/
91847c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
91947c6ae99SBarry Smith {
92047c6ae99SBarry Smith   PetscFunctionBegin;
92147c6ae99SBarry Smith   dm->ops->function = f;
92247c6ae99SBarry Smith   if (f) {
92347c6ae99SBarry Smith     dm->ops->functionj = f;
92447c6ae99SBarry Smith   }
92547c6ae99SBarry Smith   PetscFunctionReturn(0);
92647c6ae99SBarry Smith }
92747c6ae99SBarry Smith 
92847c6ae99SBarry Smith #undef __FUNCT__
92947c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
93047c6ae99SBarry Smith /*@
93147c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
93247c6ae99SBarry Smith 
93347c6ae99SBarry Smith     Logically Collective on DM
93447c6ae99SBarry Smith 
93547c6ae99SBarry Smith     Input Parameter:
93647c6ae99SBarry Smith +   dm - the DM object to destroy
93747c6ae99SBarry Smith -   f - the function to compute the matrix entries
93847c6ae99SBarry Smith 
93947c6ae99SBarry Smith     Level: intermediate
94047c6ae99SBarry Smith 
94147c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
94247c6ae99SBarry Smith          DMSetFunction()
94347c6ae99SBarry Smith 
94447c6ae99SBarry Smith @*/
94547c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
94647c6ae99SBarry Smith {
94747c6ae99SBarry Smith   PetscFunctionBegin;
94847c6ae99SBarry Smith   dm->ops->jacobian = f;
94947c6ae99SBarry Smith   PetscFunctionReturn(0);
95047c6ae99SBarry Smith }
95147c6ae99SBarry Smith 
95247c6ae99SBarry Smith #undef __FUNCT__
95347c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
95447c6ae99SBarry Smith /*@
95547c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
95647c6ae99SBarry Smith 
95747c6ae99SBarry Smith     Collective on DM
95847c6ae99SBarry Smith 
95947c6ae99SBarry Smith     Input Parameter:
96047c6ae99SBarry Smith +   dm - the DM object to destroy
96147c6ae99SBarry Smith -   x - the vector to hold the initial guess values
96247c6ae99SBarry Smith 
96347c6ae99SBarry Smith     Level: developer
96447c6ae99SBarry Smith 
96547c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetRhs(), DMSetMat()
96647c6ae99SBarry Smith 
96747c6ae99SBarry Smith @*/
96847c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMComputeInitialGuess(DM dm,Vec x)
96947c6ae99SBarry Smith {
97047c6ae99SBarry Smith   PetscErrorCode ierr;
97147c6ae99SBarry Smith   PetscFunctionBegin;
97247c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
97347c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
97447c6ae99SBarry Smith   PetscFunctionReturn(0);
97547c6ae99SBarry Smith }
97647c6ae99SBarry Smith 
97747c6ae99SBarry Smith #undef __FUNCT__
97847c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
97947c6ae99SBarry Smith /*@
98047c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
98147c6ae99SBarry Smith 
98247c6ae99SBarry Smith     Not Collective
98347c6ae99SBarry Smith 
98447c6ae99SBarry Smith     Input Parameter:
98547c6ae99SBarry Smith .   dm - the DM object to destroy
98647c6ae99SBarry Smith 
98747c6ae99SBarry Smith     Output Parameter:
98847c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
98947c6ae99SBarry Smith 
99047c6ae99SBarry Smith     Level: developer
99147c6ae99SBarry Smith 
99247c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
99347c6ae99SBarry Smith 
99447c6ae99SBarry Smith @*/
99547c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMHasInitialGuess(DM dm,PetscBool  *flg)
99647c6ae99SBarry Smith {
99747c6ae99SBarry Smith   PetscFunctionBegin;
99847c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
99947c6ae99SBarry Smith   PetscFunctionReturn(0);
100047c6ae99SBarry Smith }
100147c6ae99SBarry Smith 
100247c6ae99SBarry Smith #undef __FUNCT__
100347c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
100447c6ae99SBarry Smith /*@
100547c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
100647c6ae99SBarry Smith 
100747c6ae99SBarry Smith     Not Collective
100847c6ae99SBarry Smith 
100947c6ae99SBarry Smith     Input Parameter:
101047c6ae99SBarry Smith .   dm - the DM object to destroy
101147c6ae99SBarry Smith 
101247c6ae99SBarry Smith     Output Parameter:
101347c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
101447c6ae99SBarry Smith 
101547c6ae99SBarry Smith     Level: developer
101647c6ae99SBarry Smith 
101747c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
101847c6ae99SBarry Smith 
101947c6ae99SBarry Smith @*/
102047c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMHasFunction(DM dm,PetscBool  *flg)
102147c6ae99SBarry Smith {
102247c6ae99SBarry Smith   PetscFunctionBegin;
102347c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
102447c6ae99SBarry Smith   PetscFunctionReturn(0);
102547c6ae99SBarry Smith }
102647c6ae99SBarry Smith 
102747c6ae99SBarry Smith #undef __FUNCT__
102847c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
102947c6ae99SBarry Smith /*@
103047c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
103147c6ae99SBarry Smith 
103247c6ae99SBarry Smith     Not Collective
103347c6ae99SBarry Smith 
103447c6ae99SBarry Smith     Input Parameter:
103547c6ae99SBarry Smith .   dm - the DM object to destroy
103647c6ae99SBarry Smith 
103747c6ae99SBarry Smith     Output Parameter:
103847c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
103947c6ae99SBarry Smith 
104047c6ae99SBarry Smith     Level: developer
104147c6ae99SBarry Smith 
104247c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetFunction(), DMSetJacobian()
104347c6ae99SBarry Smith 
104447c6ae99SBarry Smith @*/
104547c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMHasJacobian(DM dm,PetscBool  *flg)
104647c6ae99SBarry Smith {
104747c6ae99SBarry Smith   PetscFunctionBegin;
104847c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
104947c6ae99SBarry Smith   PetscFunctionReturn(0);
105047c6ae99SBarry Smith }
105147c6ae99SBarry Smith 
105247c6ae99SBarry Smith #undef __FUNCT__
105347c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
105447c6ae99SBarry Smith /*@
105547c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
105647c6ae99SBarry Smith 
105747c6ae99SBarry Smith     Collective on DM
105847c6ae99SBarry Smith 
105947c6ae99SBarry Smith     Input Parameter:
106047c6ae99SBarry Smith +   dm - the DM object to destroy
106147c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
106247c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
106347c6ae99SBarry Smith 
106447c6ae99SBarry Smith     Level: developer
106547c6ae99SBarry Smith 
106647c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
106747c6ae99SBarry Smith          DMSetJacobian()
106847c6ae99SBarry Smith 
106947c6ae99SBarry Smith @*/
107047c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMComputeFunction(DM dm,Vec x,Vec b)
107147c6ae99SBarry Smith {
107247c6ae99SBarry Smith   PetscErrorCode ierr;
107347c6ae99SBarry Smith   PetscFunctionBegin;
107447c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
107547c6ae99SBarry Smith   if (!x) x = dm->x;
107647c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
107747c6ae99SBarry Smith   PetscFunctionReturn(0);
107847c6ae99SBarry Smith }
107947c6ae99SBarry Smith 
108047c6ae99SBarry Smith 
108147c6ae99SBarry Smith #undef __FUNCT__
108247c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
108347c6ae99SBarry Smith /*@
108447c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
108547c6ae99SBarry Smith 
108647c6ae99SBarry Smith     Collective on DM
108747c6ae99SBarry Smith 
108847c6ae99SBarry Smith     Input Parameter:
108947c6ae99SBarry Smith +   dm - the DM object
109047c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
109147c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
109247c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
109347c6ae99SBarry Smith 
109447c6ae99SBarry Smith     Level: developer
109547c6ae99SBarry Smith 
109647c6ae99SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMGetInterpolation(), DMGetColoring(), DMGetMatrix(), DMGetContext(), DMSetInitialGuess(),
109747c6ae99SBarry Smith          DMSetFunction()
109847c6ae99SBarry Smith 
109947c6ae99SBarry Smith @*/
110047c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
110147c6ae99SBarry Smith {
110247c6ae99SBarry Smith   PetscErrorCode ierr;
110347c6ae99SBarry Smith 
110447c6ae99SBarry Smith   PetscFunctionBegin;
110547c6ae99SBarry Smith   if (!dm->ops->jacobian) {
110647c6ae99SBarry Smith     ISColoring    coloring;
110747c6ae99SBarry Smith     MatFDColoring fd;
110847c6ae99SBarry Smith 
110947c6ae99SBarry Smith     ierr = DMGetColoring(dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr);
111047c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
111147c6ae99SBarry Smith     ierr = ISColoringDestroy(coloring);CHKERRQ(ierr);
111247c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
111347c6ae99SBarry Smith     dm->fd = fd;
111447c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
111547c6ae99SBarry Smith 
111647c6ae99SBarry Smith     if (!dm->x) {
111747c6ae99SBarry Smith       ierr = MatGetVecs(B,&dm->x,PETSC_NULL);CHKERRQ(ierr);
111847c6ae99SBarry Smith     }
111947c6ae99SBarry Smith   }
112047c6ae99SBarry Smith   if (!x) x = dm->x;
112147c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
112247c6ae99SBarry Smith   PetscFunctionReturn(0);
112347c6ae99SBarry Smith }
1124264ace61SBarry Smith 
1125264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
1126264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
1127264ace61SBarry Smith 
1128264ace61SBarry Smith #undef __FUNCT__
1129264ace61SBarry Smith #define __FUNCT__ "DMSetType"
1130264ace61SBarry Smith /*@C
1131264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
1132264ace61SBarry Smith 
1133264ace61SBarry Smith   Collective on DM
1134264ace61SBarry Smith 
1135264ace61SBarry Smith   Input Parameters:
1136264ace61SBarry Smith + dm     - The DM object
1137264ace61SBarry Smith - method - The name of the DM type
1138264ace61SBarry Smith 
1139264ace61SBarry Smith   Options Database Key:
1140264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
1141264ace61SBarry Smith 
1142264ace61SBarry Smith   Notes:
1143e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
1144264ace61SBarry Smith 
1145264ace61SBarry Smith   Level: intermediate
1146264ace61SBarry Smith 
1147264ace61SBarry Smith .keywords: DM, set, type
1148264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
1149264ace61SBarry Smith @*/
1150264ace61SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMSetType(DM dm, const DMType method)
1151264ace61SBarry Smith {
1152264ace61SBarry Smith   PetscErrorCode (*r)(DM);
1153264ace61SBarry Smith   PetscBool      match;
1154264ace61SBarry Smith   PetscErrorCode ierr;
1155264ace61SBarry Smith 
1156264ace61SBarry Smith   PetscFunctionBegin;
1157264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1158264ace61SBarry Smith   ierr = PetscTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
1159264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
1160264ace61SBarry Smith 
1161264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
1162264ace61SBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,(void (**)(void)) &r);CHKERRQ(ierr);
1163264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
1164264ace61SBarry Smith 
1165264ace61SBarry Smith   if (dm->ops->destroy) {
1166264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
1167264ace61SBarry Smith   }
1168264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
1169264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
1170264ace61SBarry Smith   PetscFunctionReturn(0);
1171264ace61SBarry Smith }
1172264ace61SBarry Smith 
1173264ace61SBarry Smith #undef __FUNCT__
1174264ace61SBarry Smith #define __FUNCT__ "DMGetType"
1175264ace61SBarry Smith /*@C
1176264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
1177264ace61SBarry Smith 
1178264ace61SBarry Smith   Not Collective
1179264ace61SBarry Smith 
1180264ace61SBarry Smith   Input Parameter:
1181264ace61SBarry Smith . dm  - The DM
1182264ace61SBarry Smith 
1183264ace61SBarry Smith   Output Parameter:
1184264ace61SBarry Smith . type - The DM type name
1185264ace61SBarry Smith 
1186264ace61SBarry Smith   Level: intermediate
1187264ace61SBarry Smith 
1188264ace61SBarry Smith .keywords: DM, get, type, name
1189264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
1190264ace61SBarry Smith @*/
1191264ace61SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMGetType(DM dm, const DMType *type)
1192264ace61SBarry Smith {
1193264ace61SBarry Smith   PetscErrorCode ierr;
1194264ace61SBarry Smith 
1195264ace61SBarry Smith   PetscFunctionBegin;
1196264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
1197264ace61SBarry Smith   PetscValidCharPointer(type,2);
1198264ace61SBarry Smith   if (!DMRegisterAllCalled) {
1199264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
1200264ace61SBarry Smith   }
1201264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
1202264ace61SBarry Smith   PetscFunctionReturn(0);
1203264ace61SBarry Smith }
1204264ace61SBarry Smith 
1205264ace61SBarry Smith 
1206264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1207264ace61SBarry Smith 
1208264ace61SBarry Smith #undef __FUNCT__
1209264ace61SBarry Smith #define __FUNCT__ "DMRegister"
1210264ace61SBarry Smith /*@C
1211264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
1212264ace61SBarry Smith 
1213264ace61SBarry Smith   Level: advanced
1214264ace61SBarry Smith @*/
1215264ace61SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
1216264ace61SBarry Smith {
1217264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
1218264ace61SBarry Smith   PetscErrorCode ierr;
1219264ace61SBarry Smith 
1220264ace61SBarry Smith   PetscFunctionBegin;
1221264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
1222264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
1223264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
1224264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
1225264ace61SBarry Smith   PetscFunctionReturn(0);
1226264ace61SBarry Smith }
1227264ace61SBarry Smith 
1228264ace61SBarry Smith 
1229264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
1230264ace61SBarry Smith #undef __FUNCT__
1231264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
1232264ace61SBarry Smith /*@C
1233264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
1234264ace61SBarry Smith 
1235264ace61SBarry Smith    Not Collective
1236264ace61SBarry Smith 
1237264ace61SBarry Smith    Level: advanced
1238264ace61SBarry Smith 
1239264ace61SBarry Smith .keywords: DM, register, destroy
1240264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
1241264ace61SBarry Smith @*/
1242264ace61SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMRegisterDestroy(void)
1243264ace61SBarry Smith {
1244264ace61SBarry Smith   PetscErrorCode ierr;
1245264ace61SBarry Smith 
1246264ace61SBarry Smith   PetscFunctionBegin;
1247264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
1248264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
1249264ace61SBarry Smith   PetscFunctionReturn(0);
1250264ace61SBarry Smith }
125123f975d1SBarry Smith 
125223f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
125323f975d1SBarry Smith #include "mex.h"
125423f975d1SBarry Smith 
12553014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
125623f975d1SBarry Smith 
125723f975d1SBarry Smith #undef __FUNCT__
125823f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
125923f975d1SBarry Smith /*
126023f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
126123f975d1SBarry Smith                          DMSetFunctionMatlab().
126223f975d1SBarry Smith 
126323f975d1SBarry Smith    For linear problems x is null
126423f975d1SBarry Smith 
126523f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
126623f975d1SBarry Smith */
126723f975d1SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
126823f975d1SBarry Smith {
126923f975d1SBarry Smith   PetscErrorCode    ierr;
127023f975d1SBarry Smith   DMMatlabContext   *sctx;
127123f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
127223f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
127323f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
127423f975d1SBarry Smith 
127523f975d1SBarry Smith   PetscFunctionBegin;
127623f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
127723f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
127823f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
127923f975d1SBarry Smith 
128023f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
128123f975d1SBarry Smith   ierr = DMGetContext(dm,(void**)&sctx);CHKERRQ(ierr);
128223f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
128323f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
12843014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
128523f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
128623f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
128723f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
128823f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
1289b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
129023f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
129123f975d1SBarry Smith   mxDestroyArray(prhs[0]);
129223f975d1SBarry Smith   mxDestroyArray(prhs[1]);
129323f975d1SBarry Smith   mxDestroyArray(prhs[2]);
129423f975d1SBarry Smith   mxDestroyArray(prhs[3]);
129523f975d1SBarry Smith   mxDestroyArray(plhs[0]);
129623f975d1SBarry Smith   PetscFunctionReturn(0);
129723f975d1SBarry Smith }
129823f975d1SBarry Smith 
129923f975d1SBarry Smith 
130023f975d1SBarry Smith #undef __FUNCT__
130123f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
130223f975d1SBarry Smith /*
130323f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
130423f975d1SBarry Smith 
130523f975d1SBarry Smith */
130623f975d1SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMSetFunctionMatlab(DM dm,const char *func)
130723f975d1SBarry Smith {
130823f975d1SBarry Smith   PetscErrorCode    ierr;
130923f975d1SBarry Smith   DMMatlabContext   *sctx;
131023f975d1SBarry Smith 
131123f975d1SBarry Smith   PetscFunctionBegin;
131223f975d1SBarry Smith   /* currently sctx is memory bleed */
13133014e516SBarry Smith   ierr = DMGetContext(dm,(void**)&sctx);CHKERRQ(ierr);
13143014e516SBarry Smith   if (!sctx) {
131523f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
13163014e516SBarry Smith   }
131723f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
131823f975d1SBarry Smith   ierr = DMSetContext(dm,sctx);CHKERRQ(ierr);
131923f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
132023f975d1SBarry Smith   PetscFunctionReturn(0);
132123f975d1SBarry Smith }
13223014e516SBarry Smith 
13233014e516SBarry Smith #undef __FUNCT__
13243014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
13253014e516SBarry Smith /*
13263014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
13273014e516SBarry Smith                          DMSetJacobianMatlab().
13283014e516SBarry Smith 
13293014e516SBarry Smith    For linear problems x is null
13303014e516SBarry Smith 
13313014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
13323014e516SBarry Smith */
13333014e516SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
13343014e516SBarry Smith {
13353014e516SBarry Smith   PetscErrorCode    ierr;
13363014e516SBarry Smith   DMMatlabContext   *sctx;
13373014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
13383014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
13393014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
13403014e516SBarry Smith 
13413014e516SBarry Smith   PetscFunctionBegin;
13423014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13433014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
13443014e516SBarry Smith 
13453014e516SBarry Smith   /* call Matlab function in ctx with arguments x, A, and B */
13463014e516SBarry Smith   ierr = DMGetContext(dm,(void**)&sctx);CHKERRQ(ierr);
13473014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
13483014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
13493014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
13503014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
13513014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
13523014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
13533014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
13543014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
13553014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
1356b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
13573014e516SBarry Smith   *str    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
13583014e516SBarry Smith   ierr    =  mxGetScalar(plhs[1]);CHKERRQ(ierr);
13593014e516SBarry Smith   mxDestroyArray(prhs[0]);
13603014e516SBarry Smith   mxDestroyArray(prhs[1]);
13613014e516SBarry Smith   mxDestroyArray(prhs[2]);
13623014e516SBarry Smith   mxDestroyArray(prhs[3]);
13633014e516SBarry Smith   mxDestroyArray(prhs[4]);
13643014e516SBarry Smith   mxDestroyArray(plhs[0]);
13653014e516SBarry Smith   mxDestroyArray(plhs[1]);
13663014e516SBarry Smith   PetscFunctionReturn(0);
13673014e516SBarry Smith }
13683014e516SBarry Smith 
13693014e516SBarry Smith 
13703014e516SBarry Smith #undef __FUNCT__
13713014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
13723014e516SBarry Smith /*
13733014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
13743014e516SBarry Smith 
13753014e516SBarry Smith */
13763014e516SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DMSetJacobianMatlab(DM dm,const char *func)
13773014e516SBarry Smith {
13783014e516SBarry Smith   PetscErrorCode    ierr;
13793014e516SBarry Smith   DMMatlabContext   *sctx;
13803014e516SBarry Smith 
13813014e516SBarry Smith   PetscFunctionBegin;
13823014e516SBarry Smith   /* currently sctx is memory bleed */
13833014e516SBarry Smith   ierr = DMGetContext(dm,(void**)&sctx);CHKERRQ(ierr);
13843014e516SBarry Smith   if (!sctx) {
13853014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
13863014e516SBarry Smith   }
13873014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
13883014e516SBarry Smith   ierr = DMSetContext(dm,sctx);CHKERRQ(ierr);
13893014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
13903014e516SBarry Smith   PetscFunctionReturn(0);
13913014e516SBarry Smith }
139223f975d1SBarry Smith #endif
1393