xref: /petsc/src/dm/interface/dm.c (revision 31ea6d37be99c212be42918c421494b4a6f1f448)
108da532bSDmitry Karpeev #include <petscsnes.h>
2b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
347c6ae99SBarry Smith 
4732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
567a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal;
667a56275SMatthew G Knepley 
747c6ae99SBarry Smith #undef __FUNCT__
8a4121054SBarry Smith #define __FUNCT__ "DMCreate"
9a4121054SBarry Smith /*@
10de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
11a4121054SBarry Smith 
12a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
13a4121054SBarry Smith    error when you try to use the vector.
14a4121054SBarry Smith 
15a4121054SBarry Smith   Collective on MPI_Comm
16a4121054SBarry Smith 
17a4121054SBarry Smith   Input Parameter:
18a4121054SBarry Smith . comm - The communicator for the DM object
19a4121054SBarry Smith 
20a4121054SBarry Smith   Output Parameter:
21a4121054SBarry Smith . dm - The DM object
22a4121054SBarry Smith 
23a4121054SBarry Smith   Level: beginner
24a4121054SBarry Smith 
25a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
26a4121054SBarry Smith @*/
277087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
28a4121054SBarry Smith {
29a4121054SBarry Smith   DM             v;
30a4121054SBarry Smith   PetscErrorCode ierr;
31a4121054SBarry Smith 
32a4121054SBarry Smith   PetscFunctionBegin;
331411c6eeSJed Brown   PetscValidPointer(dm,2);
341411c6eeSJed Brown   *dm = PETSC_NULL;
35a4121054SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
36b84caa0eSBarry Smith   ierr = VecInitializePackage(PETSC_NULL);CHKERRQ(ierr);
37b84caa0eSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
38a4121054SBarry Smith   ierr = DMInitializePackage(PETSC_NULL);CHKERRQ(ierr);
39a4121054SBarry Smith #endif
40a4121054SBarry Smith 
413194b578SJed Brown   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
42a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
431411c6eeSJed Brown 
44e7c4fc90SDmitry Karpeev 
45a89ea682SMatthew G Knepley   v->workSize     = 0;
46a89ea682SMatthew G Knepley   v->workArray    = PETSC_NULL;
471411c6eeSJed Brown   v->ltogmap      = PETSC_NULL;
481411c6eeSJed Brown   v->ltogmapb     = PETSC_NULL;
491411c6eeSJed Brown   v->bs           = 1;
50171400e9SBarry Smith   v->coloringtype = IS_COLORING_GLOBAL;
51970e74d5SMatthew G Knepley   v->lf           = PETSC_NULL;
52970e74d5SMatthew G Knepley   v->lj           = PETSC_NULL;
5388ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5488ed4aceSMatthew G Knepley   ierr = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
5588ed4aceSMatthew G Knepley   v->defaultSection       = PETSC_NULL;
5688ed4aceSMatthew G Knepley   v->defaultGlobalSection = PETSC_NULL;
571411c6eeSJed Brown 
581411c6eeSJed Brown   *dm = v;
59a4121054SBarry Smith   PetscFunctionReturn(0);
60a4121054SBarry Smith }
61a4121054SBarry Smith 
62a4121054SBarry Smith 
63a4121054SBarry Smith #undef __FUNCT__
649a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
659a42bb27SBarry Smith /*@C
66564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
679a42bb27SBarry Smith 
68aa219208SBarry Smith    Logically Collective on DMDA
699a42bb27SBarry Smith 
709a42bb27SBarry Smith    Input Parameter:
719a42bb27SBarry Smith +  da - initial distributed array
728154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
739a42bb27SBarry Smith 
749a42bb27SBarry Smith    Options Database:
75dd85299cSBarry Smith .   -dm_vec_type ctype
769a42bb27SBarry Smith 
779a42bb27SBarry Smith    Level: intermediate
789a42bb27SBarry Smith 
79aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
809a42bb27SBarry Smith @*/
817087cfbeSBarry Smith PetscErrorCode  DMSetVecType(DM da,const VecType ctype)
829a42bb27SBarry Smith {
839a42bb27SBarry Smith   PetscErrorCode ierr;
849a42bb27SBarry Smith 
859a42bb27SBarry Smith   PetscFunctionBegin;
869a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
879a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
889a42bb27SBarry Smith   ierr = PetscStrallocpy(ctype,&da->vectype);CHKERRQ(ierr);
899a42bb27SBarry Smith   PetscFunctionReturn(0);
909a42bb27SBarry Smith }
919a42bb27SBarry Smith 
929a42bb27SBarry Smith #undef __FUNCT__
93521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
94521d9a4cSLisandro Dalcin /*@C
95521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
96521d9a4cSLisandro Dalcin 
97521d9a4cSLisandro Dalcin    Logically Collective on DM
98521d9a4cSLisandro Dalcin 
99521d9a4cSLisandro Dalcin    Input Parameter:
100521d9a4cSLisandro Dalcin +  dm - the DM context
101521d9a4cSLisandro Dalcin .  ctype - the matrix type
102521d9a4cSLisandro Dalcin 
103521d9a4cSLisandro Dalcin    Options Database:
104521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
105521d9a4cSLisandro Dalcin 
106521d9a4cSLisandro Dalcin    Level: intermediate
107521d9a4cSLisandro Dalcin 
108521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType
109521d9a4cSLisandro Dalcin @*/
110521d9a4cSLisandro Dalcin PetscErrorCode  DMSetMatType(DM dm,const MatType ctype)
111521d9a4cSLisandro Dalcin {
112521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
113521d9a4cSLisandro Dalcin   PetscFunctionBegin;
114521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
115521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
116521d9a4cSLisandro Dalcin   ierr = PetscStrallocpy(ctype,&dm->mattype);CHKERRQ(ierr);
117521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
118521d9a4cSLisandro Dalcin }
119521d9a4cSLisandro Dalcin 
120521d9a4cSLisandro Dalcin #undef __FUNCT__
1219a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
1229a42bb27SBarry Smith /*@C
1239a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
124aa219208SBarry Smith    DMDA options in the database.
1259a42bb27SBarry Smith 
126aa219208SBarry Smith    Logically Collective on DMDA
1279a42bb27SBarry Smith 
1289a42bb27SBarry Smith    Input Parameter:
129aa219208SBarry Smith +  da - the DMDA context
1309a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
1319a42bb27SBarry Smith 
1329a42bb27SBarry Smith    Notes:
1339a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
1349a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
1359a42bb27SBarry Smith 
1369a42bb27SBarry Smith    Level: advanced
1379a42bb27SBarry Smith 
138aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
1399a42bb27SBarry Smith 
1409a42bb27SBarry Smith .seealso: DMSetFromOptions()
1419a42bb27SBarry Smith @*/
1427087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
1439a42bb27SBarry Smith {
1449a42bb27SBarry Smith   PetscErrorCode ierr;
1459a42bb27SBarry Smith 
1469a42bb27SBarry Smith   PetscFunctionBegin;
1479a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1489a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
1499a42bb27SBarry Smith   PetscFunctionReturn(0);
1509a42bb27SBarry Smith }
1519a42bb27SBarry Smith 
1529a42bb27SBarry Smith #undef __FUNCT__
15347c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
15447c6ae99SBarry Smith /*@
155aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
15647c6ae99SBarry Smith 
15747c6ae99SBarry Smith     Collective on DM
15847c6ae99SBarry Smith 
15947c6ae99SBarry Smith     Input Parameter:
16047c6ae99SBarry Smith .   dm - the DM object to destroy
16147c6ae99SBarry Smith 
16247c6ae99SBarry Smith     Level: developer
16347c6ae99SBarry Smith 
164e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
16547c6ae99SBarry Smith 
16647c6ae99SBarry Smith @*/
167fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
16847c6ae99SBarry Smith {
169732e2eb9SMatthew G Knepley   PetscInt       i, cnt = 0;
170b17ce1afSJed Brown   DMCoarsenHookLink link,next;
171dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
17247c6ae99SBarry Smith   PetscErrorCode ierr;
17347c6ae99SBarry Smith 
17447c6ae99SBarry Smith   PetscFunctionBegin;
1756bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
1766bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
17787e657c6SBarry Smith 
17887e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
179732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1806bf464f9SBarry Smith     if ((*dm)->localin[i])  {cnt++;}
1816bf464f9SBarry Smith     if ((*dm)->globalin[i]) {cnt++;}
182732e2eb9SMatthew G Knepley   }
183dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
18471cd77b2SBarry Smith   if ((*dm)->x) {
18571cd77b2SBarry Smith     PetscObject obj;
18671cd77b2SBarry Smith     ierr = PetscObjectQuery((PetscObject)(*dm)->x,"DM",&obj);CHKERRQ(ierr);
18771cd77b2SBarry Smith     if (obj == (PetscObject)*dm) cnt++;
18871cd77b2SBarry Smith   }
189732e2eb9SMatthew G Knepley 
1906bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
191732e2eb9SMatthew G Knepley   /*
192732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
193732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
194732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
195732e2eb9SMatthew G Knepley   */
1966bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
1976bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
198732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
1996bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
2006bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
201732e2eb9SMatthew G Knepley   }
202dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */
203dfe15315SJed Brown     nnext = nlink->next;
204dfe15315SJed Brown     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
205dfe15315SJed Brown     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
206dfe15315SJed Brown     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
207dfe15315SJed Brown     ierr = PetscFree(nlink);CHKERRQ(ierr);
208dfe15315SJed Brown   }
209dfe15315SJed Brown   (*dm)->namedglobal = PETSC_NULL;
2101a266240SBarry Smith 
211b17ce1afSJed Brown   /* Destroy the list of hooks */
212b17ce1afSJed Brown   for (link=(*dm)->coarsenhook; link; link=next) {
213b17ce1afSJed Brown     next = link->next;
214b17ce1afSJed Brown     ierr = PetscFree(link);CHKERRQ(ierr);
215b17ce1afSJed Brown   }
216b17ce1afSJed Brown   (*dm)->coarsenhook = PETSC_NULL;
217b17ce1afSJed Brown 
2181a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
2191a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
2201a266240SBarry Smith   }
22187e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
22271cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
2234dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
2246bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
2256bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr);
2266bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
227073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
228a89ea682SMatthew G Knepley   ierr = PetscFree((*dm)->workArray);CHKERRQ(ierr);
22988ed4aceSMatthew G Knepley 
23088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
23188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
23288ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
23388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
234732e2eb9SMatthew G Knepley   /* if memory was published with AMS then destroy it */
2356bf464f9SBarry Smith   ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr);
236732e2eb9SMatthew G Knepley 
2376bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
2386bf464f9SBarry Smith   ierr = PetscFree((*dm)->data);CHKERRQ(ierr);
239732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
24047c6ae99SBarry Smith   PetscFunctionReturn(0);
24147c6ae99SBarry Smith }
24247c6ae99SBarry Smith 
24347c6ae99SBarry Smith #undef __FUNCT__
244d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
245d7bf68aeSBarry Smith /*@
246d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
247d7bf68aeSBarry Smith 
248d7bf68aeSBarry Smith     Collective on DM
249d7bf68aeSBarry Smith 
250d7bf68aeSBarry Smith     Input Parameter:
251d7bf68aeSBarry Smith .   dm - the DM object to setup
252d7bf68aeSBarry Smith 
253d7bf68aeSBarry Smith     Level: developer
254d7bf68aeSBarry Smith 
255e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
256d7bf68aeSBarry Smith 
257d7bf68aeSBarry Smith @*/
2587087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
259d7bf68aeSBarry Smith {
260d7bf68aeSBarry Smith   PetscErrorCode ierr;
261d7bf68aeSBarry Smith 
262d7bf68aeSBarry Smith   PetscFunctionBegin;
263171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2648387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
265d7bf68aeSBarry Smith   if (dm->ops->setup) {
266d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
267d7bf68aeSBarry Smith   }
2688387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
269d7bf68aeSBarry Smith   PetscFunctionReturn(0);
270d7bf68aeSBarry Smith }
271d7bf68aeSBarry Smith 
272d7bf68aeSBarry Smith #undef __FUNCT__
273d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
274d7bf68aeSBarry Smith /*@
275d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
276d7bf68aeSBarry Smith 
277d7bf68aeSBarry Smith     Collective on DM
278d7bf68aeSBarry Smith 
279d7bf68aeSBarry Smith     Input Parameter:
280d7bf68aeSBarry Smith .   dm - the DM object to set options for
281d7bf68aeSBarry Smith 
282732e2eb9SMatthew G Knepley     Options Database:
283dd85299cSBarry Smith +   -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
284dd85299cSBarry Smith .   -dm_vec_type <type>  type of vector to create inside DM
285171400e9SBarry Smith .   -dm_mat_type <type>  type of matrix to create inside DM
286171400e9SBarry Smith -   -dm_coloring_type <global or ghosted>
287732e2eb9SMatthew G Knepley 
288d7bf68aeSBarry Smith     Level: developer
289d7bf68aeSBarry Smith 
290e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
291d7bf68aeSBarry Smith 
292d7bf68aeSBarry Smith @*/
2937087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
294d7bf68aeSBarry Smith {
29567ad5babSMatthew G Knepley   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flg4 = PETSC_FALSE,flg;
296d7bf68aeSBarry Smith   PetscErrorCode ierr;
297f9ba7244SBarry Smith   char           typeName[256] = MATAIJ;
298d7bf68aeSBarry Smith 
299d7bf68aeSBarry Smith   PetscFunctionBegin;
300171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3013194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
30282fcb398SMatthew G Knepley     ierr = PetscOptionsBool("-dm_view", "Information on DM", "DMView", flg1, &flg1, PETSC_NULL);CHKERRQ(ierr);
303c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_detail", "Exhaustive mesh description", "DMView", flg2, &flg2, PETSC_NULL);CHKERRQ(ierr);
304c4721b0eSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_vtk", "Output mesh in VTK format", "DMView", flg3, &flg3, PETSC_NULL);CHKERRQ(ierr);
30567ad5babSMatthew G Knepley     ierr = PetscOptionsBool("-dm_view_latex", "Output mesh in LaTeX TikZ format", "DMView", flg4, &flg4, PETSC_NULL);CHKERRQ(ierr);
306073dac72SJed Brown     ierr = PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,PETSC_NULL);CHKERRQ(ierr);
307f9ba7244SBarry Smith     ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
308f9ba7244SBarry Smith     if (flg) {
309f9ba7244SBarry Smith       ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
310f9ba7244SBarry Smith     }
311521d9a4cSLisandro Dalcin     ierr = PetscOptionsList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype?dm->mattype:typeName,typeName,sizeof typeName,&flg);CHKERRQ(ierr);
312073dac72SJed Brown     if (flg) {
313521d9a4cSLisandro Dalcin       ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
314073dac72SJed Brown     }
3151b89239cSHong Zhang     ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,PETSC_NULL);CHKERRQ(ierr);
316f9ba7244SBarry Smith     if (dm->ops->setfromoptions) {
317f9ba7244SBarry Smith       ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr);
318f9ba7244SBarry Smith     }
319f9ba7244SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
320f9ba7244SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
32182fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
32282fcb398SMatthew G Knepley   if (flg1) {
32382fcb398SMatthew G Knepley     ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
32482fcb398SMatthew G Knepley   }
325c4721b0eSMatthew G Knepley   if (flg2) {
326c4721b0eSMatthew G Knepley     PetscViewer viewer;
327c4721b0eSMatthew G Knepley 
328c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
329c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
330c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
331c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
332c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
333c4721b0eSMatthew G Knepley   }
334c4721b0eSMatthew G Knepley   if (flg3) {
335c4721b0eSMatthew G Knepley     PetscViewer viewer;
336c4721b0eSMatthew G Knepley 
337c4721b0eSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
338c4721b0eSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
339c4721b0eSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr);
340c4721b0eSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.vtk");CHKERRQ(ierr);
341c4721b0eSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
342c4721b0eSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
343c4721b0eSMatthew G Knepley   }
34467ad5babSMatthew G Knepley   if (flg4) {
34567ad5babSMatthew G Knepley     PetscViewer viewer;
34667ad5babSMatthew G Knepley 
34767ad5babSMatthew G Knepley     ierr = PetscViewerCreate(((PetscObject) dm)->comm, &viewer);CHKERRQ(ierr);
34867ad5babSMatthew G Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
34967ad5babSMatthew G Knepley     ierr = PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_LATEX);CHKERRQ(ierr);
35067ad5babSMatthew G Knepley     ierr = PetscViewerFileSetName(viewer, "mesh.tex");CHKERRQ(ierr);
35167ad5babSMatthew G Knepley     ierr = DMView(dm, viewer);CHKERRQ(ierr);
35267ad5babSMatthew G Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
35367ad5babSMatthew G Knepley   }
354d7bf68aeSBarry Smith   PetscFunctionReturn(0);
355d7bf68aeSBarry Smith }
356d7bf68aeSBarry Smith 
357d7bf68aeSBarry Smith #undef __FUNCT__
35847c6ae99SBarry Smith #define __FUNCT__ "DMView"
359fc9bc008SSatish Balay /*@C
360aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
36147c6ae99SBarry Smith 
36247c6ae99SBarry Smith     Collective on DM
36347c6ae99SBarry Smith 
36447c6ae99SBarry Smith     Input Parameter:
36547c6ae99SBarry Smith +   dm - the DM object to view
36647c6ae99SBarry Smith -   v - the viewer
36747c6ae99SBarry Smith 
36847c6ae99SBarry Smith     Level: developer
36947c6ae99SBarry Smith 
370e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
37147c6ae99SBarry Smith 
37247c6ae99SBarry Smith @*/
3737087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
37447c6ae99SBarry Smith {
37547c6ae99SBarry Smith   PetscErrorCode ierr;
37647c6ae99SBarry Smith 
37747c6ae99SBarry Smith   PetscFunctionBegin;
378171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3793014e516SBarry Smith  if (!v) {
3803014e516SBarry Smith     ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr);
3813014e516SBarry Smith   }
3820c010503SBarry Smith   if (dm->ops->view) {
3830c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
38447c6ae99SBarry Smith   }
38547c6ae99SBarry Smith   PetscFunctionReturn(0);
38647c6ae99SBarry Smith }
38747c6ae99SBarry Smith 
38847c6ae99SBarry Smith #undef __FUNCT__
38947c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
39047c6ae99SBarry Smith /*@
391aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
39247c6ae99SBarry Smith 
39347c6ae99SBarry Smith     Collective on DM
39447c6ae99SBarry Smith 
39547c6ae99SBarry Smith     Input Parameter:
39647c6ae99SBarry Smith .   dm - the DM object
39747c6ae99SBarry Smith 
39847c6ae99SBarry Smith     Output Parameter:
39947c6ae99SBarry Smith .   vec - the global vector
40047c6ae99SBarry Smith 
401073dac72SJed Brown     Level: beginner
40247c6ae99SBarry Smith 
403e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
40447c6ae99SBarry Smith 
40547c6ae99SBarry Smith @*/
4067087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
40747c6ae99SBarry Smith {
40847c6ae99SBarry Smith   PetscErrorCode ierr;
40947c6ae99SBarry Smith 
41047c6ae99SBarry Smith   PetscFunctionBegin;
411171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41288ed4aceSMatthew G Knepley   if (dm->defaultSection) {
41388ed4aceSMatthew G Knepley     PetscSection gSection;
41488ed4aceSMatthew G Knepley     PetscInt     localSize;
41588ed4aceSMatthew G Knepley 
41688ed4aceSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
41788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstrainedStorageSize(dm->defaultGlobalSection, &localSize);CHKERRQ(ierr);
41888ed4aceSMatthew G Knepley     ierr = VecCreate(((PetscObject) dm)->comm, vec);CHKERRQ(ierr);
41988ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
42088ed4aceSMatthew G Knepley     /* ierr = VecSetType(*vec, dm->vectype);CHKERRQ(ierr); */
42188ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
42288ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
42388ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
42488ed4aceSMatthew G Knepley     /* ierr = VecSetLocalToGlobalMappingBlock(*vec, dm->ltogmapb);CHKERRQ(ierr); */
42588ed4aceSMatthew G Knepley     /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
42688ed4aceSMatthew G Knepley   } else {
42747c6ae99SBarry Smith     ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
42888ed4aceSMatthew G Knepley   }
42947c6ae99SBarry Smith   PetscFunctionReturn(0);
43047c6ae99SBarry Smith }
43147c6ae99SBarry Smith 
43247c6ae99SBarry Smith #undef __FUNCT__
43347c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
43447c6ae99SBarry Smith /*@
435aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
43647c6ae99SBarry Smith 
43747c6ae99SBarry Smith     Not Collective
43847c6ae99SBarry Smith 
43947c6ae99SBarry Smith     Input Parameter:
44047c6ae99SBarry Smith .   dm - the DM object
44147c6ae99SBarry Smith 
44247c6ae99SBarry Smith     Output Parameter:
44347c6ae99SBarry Smith .   vec - the local vector
44447c6ae99SBarry Smith 
445073dac72SJed Brown     Level: beginner
44647c6ae99SBarry Smith 
447e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
44847c6ae99SBarry Smith 
44947c6ae99SBarry Smith @*/
4507087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
45147c6ae99SBarry Smith {
45247c6ae99SBarry Smith   PetscErrorCode ierr;
45347c6ae99SBarry Smith 
45447c6ae99SBarry Smith   PetscFunctionBegin;
455171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45688ed4aceSMatthew G Knepley   if (dm->defaultSection) {
45788ed4aceSMatthew G Knepley     PetscInt localSize;
45888ed4aceSMatthew G Knepley 
45988ed4aceSMatthew G Knepley     ierr = PetscSectionGetStorageSize(dm->defaultSection, &localSize);CHKERRQ(ierr);
46088ed4aceSMatthew G Knepley     ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
46188ed4aceSMatthew G Knepley     ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
46288ed4aceSMatthew G Knepley     ierr = VecSetFromOptions(*vec);CHKERRQ(ierr);
46388ed4aceSMatthew G Knepley     ierr = PetscObjectCompose((PetscObject) *vec, "DM", (PetscObject) dm);CHKERRQ(ierr);
46488ed4aceSMatthew G Knepley   } else {
46547c6ae99SBarry Smith     ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
46688ed4aceSMatthew G Knepley   }
46747c6ae99SBarry Smith   PetscFunctionReturn(0);
46847c6ae99SBarry Smith }
46947c6ae99SBarry Smith 
47047c6ae99SBarry Smith #undef __FUNCT__
4711411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
4721411c6eeSJed Brown /*@
4731411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
4741411c6eeSJed Brown 
4751411c6eeSJed Brown    Collective on DM
4761411c6eeSJed Brown 
4771411c6eeSJed Brown    Input Parameter:
4781411c6eeSJed Brown .  dm - the DM that provides the mapping
4791411c6eeSJed Brown 
4801411c6eeSJed Brown    Output Parameter:
4811411c6eeSJed Brown .  ltog - the mapping
4821411c6eeSJed Brown 
4831411c6eeSJed Brown    Level: intermediate
4841411c6eeSJed Brown 
4851411c6eeSJed Brown    Notes:
4861411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
4871411c6eeSJed Brown    MatSetLocalToGlobalMapping().
4881411c6eeSJed Brown 
4891411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock()
4901411c6eeSJed Brown @*/
4917087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
4921411c6eeSJed Brown {
4931411c6eeSJed Brown   PetscErrorCode ierr;
4941411c6eeSJed Brown 
4951411c6eeSJed Brown   PetscFunctionBegin;
4961411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4971411c6eeSJed Brown   PetscValidPointer(ltog,2);
4981411c6eeSJed Brown   if (!dm->ltogmap) {
4991411c6eeSJed Brown     if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
5001411c6eeSJed Brown     ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr);
5011411c6eeSJed Brown   }
5021411c6eeSJed Brown   *ltog = dm->ltogmap;
5031411c6eeSJed Brown   PetscFunctionReturn(0);
5041411c6eeSJed Brown }
5051411c6eeSJed Brown 
5061411c6eeSJed Brown #undef __FUNCT__
5071411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock"
5081411c6eeSJed Brown /*@
5091411c6eeSJed Brown    DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM.
5101411c6eeSJed Brown 
5111411c6eeSJed Brown    Collective on DM
5121411c6eeSJed Brown 
5131411c6eeSJed Brown    Input Parameter:
5141411c6eeSJed Brown .  da - the distributed array that provides the mapping
5151411c6eeSJed Brown 
5161411c6eeSJed Brown    Output Parameter:
5171411c6eeSJed Brown .  ltog - the block mapping
5181411c6eeSJed Brown 
5191411c6eeSJed Brown    Level: intermediate
5201411c6eeSJed Brown 
5211411c6eeSJed Brown    Notes:
5221411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMappingBlock() or
5231411c6eeSJed Brown    MatSetLocalToGlobalMappingBlock().
5241411c6eeSJed Brown 
5251411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize()
5261411c6eeSJed Brown @*/
5277087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog)
5281411c6eeSJed Brown {
5291411c6eeSJed Brown   PetscErrorCode ierr;
5301411c6eeSJed Brown 
5311411c6eeSJed Brown   PetscFunctionBegin;
5321411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5331411c6eeSJed Brown   PetscValidPointer(ltog,2);
5341411c6eeSJed Brown   if (!dm->ltogmapb) {
5351411c6eeSJed Brown     PetscInt bs;
5361411c6eeSJed Brown     ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr);
5371411c6eeSJed Brown     if (bs > 1) {
5381411c6eeSJed Brown       if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock");
5391411c6eeSJed Brown       ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr);
5401411c6eeSJed Brown     } else {
5411411c6eeSJed Brown       ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr);
5421411c6eeSJed Brown       ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr);
5431411c6eeSJed Brown     }
5441411c6eeSJed Brown   }
5451411c6eeSJed Brown   *ltog = dm->ltogmapb;
5461411c6eeSJed Brown   PetscFunctionReturn(0);
5471411c6eeSJed Brown }
5481411c6eeSJed Brown 
5491411c6eeSJed Brown #undef __FUNCT__
5501411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
5511411c6eeSJed Brown /*@
5521411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
5531411c6eeSJed Brown 
5541411c6eeSJed Brown    Not Collective
5551411c6eeSJed Brown 
5561411c6eeSJed Brown    Input Parameter:
5571411c6eeSJed Brown .  dm - the DM with block structure
5581411c6eeSJed Brown 
5591411c6eeSJed Brown    Output Parameter:
5601411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
5611411c6eeSJed Brown 
5621411c6eeSJed Brown    Level: intermediate
5631411c6eeSJed Brown 
5641411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock()
5651411c6eeSJed Brown @*/
5667087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
5671411c6eeSJed Brown {
5681411c6eeSJed Brown   PetscFunctionBegin;
5691411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5701411c6eeSJed Brown   PetscValidPointer(bs,2);
5711411c6eeSJed 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");
5721411c6eeSJed Brown   *bs = dm->bs;
5731411c6eeSJed Brown   PetscFunctionReturn(0);
5741411c6eeSJed Brown }
5751411c6eeSJed Brown 
5761411c6eeSJed Brown #undef __FUNCT__
577e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
57847c6ae99SBarry Smith /*@
579e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
58047c6ae99SBarry Smith 
58147c6ae99SBarry Smith     Collective on DM
58247c6ae99SBarry Smith 
58347c6ae99SBarry Smith     Input Parameter:
58447c6ae99SBarry Smith +   dm1 - the DM object
58547c6ae99SBarry Smith -   dm2 - the second, finer DM object
58647c6ae99SBarry Smith 
58747c6ae99SBarry Smith     Output Parameter:
58847c6ae99SBarry Smith +  mat - the interpolation
58947c6ae99SBarry Smith -  vec - the scaling (optional)
59047c6ae99SBarry Smith 
59147c6ae99SBarry Smith     Level: developer
59247c6ae99SBarry Smith 
59385afcc9aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
59485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
595d52bd9f3SBarry Smith 
596d52bd9f3SBarry Smith         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMDAGetCoordinateDA()) to interpolate the mesh coordinate vectors
597d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
59885afcc9aSBarry Smith 
59985afcc9aSBarry Smith 
600e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
60147c6ae99SBarry Smith 
60247c6ae99SBarry Smith @*/
603e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
60447c6ae99SBarry Smith {
60547c6ae99SBarry Smith   PetscErrorCode ierr;
60647c6ae99SBarry Smith 
60747c6ae99SBarry Smith   PetscFunctionBegin;
608171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
609171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
61025296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
61147c6ae99SBarry Smith   PetscFunctionReturn(0);
61247c6ae99SBarry Smith }
61347c6ae99SBarry Smith 
61447c6ae99SBarry Smith #undef __FUNCT__
615e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
61647c6ae99SBarry Smith /*@
617e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
61847c6ae99SBarry Smith 
61947c6ae99SBarry Smith     Collective on DM
62047c6ae99SBarry Smith 
62147c6ae99SBarry Smith     Input Parameter:
62247c6ae99SBarry Smith +   dm1 - the DM object
62347c6ae99SBarry Smith -   dm2 - the second, finer DM object
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith     Output Parameter:
62647c6ae99SBarry Smith .   ctx - the injection
62747c6ae99SBarry Smith 
62847c6ae99SBarry Smith     Level: developer
62947c6ae99SBarry Smith 
63085afcc9aSBarry Smith    Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
63185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
63285afcc9aSBarry Smith 
633e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
63447c6ae99SBarry Smith 
63547c6ae99SBarry Smith @*/
636e727c939SJed Brown PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx)
63747c6ae99SBarry Smith {
63847c6ae99SBarry Smith   PetscErrorCode ierr;
63947c6ae99SBarry Smith 
64047c6ae99SBarry Smith   PetscFunctionBegin;
641171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
642171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
64347c6ae99SBarry Smith   ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr);
64447c6ae99SBarry Smith   PetscFunctionReturn(0);
64547c6ae99SBarry Smith }
64647c6ae99SBarry Smith 
64747c6ae99SBarry Smith #undef __FUNCT__
648e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
649d1e2c406SBarry Smith /*@C
650e727c939SJed Brown     DMCreateColoring - Gets coloring for a DMDA or DMComposite
65147c6ae99SBarry Smith 
65247c6ae99SBarry Smith     Collective on DM
65347c6ae99SBarry Smith 
65447c6ae99SBarry Smith     Input Parameter:
65547c6ae99SBarry Smith +   dm - the DM object
65647c6ae99SBarry Smith .   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
65747c6ae99SBarry Smith -   matype - either MATAIJ or MATBAIJ
65847c6ae99SBarry Smith 
65947c6ae99SBarry Smith     Output Parameter:
66047c6ae99SBarry Smith .   coloring - the coloring
66147c6ae99SBarry Smith 
66247c6ae99SBarry Smith     Level: developer
66347c6ae99SBarry Smith 
664e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix()
66547c6ae99SBarry Smith 
666aab9d709SJed Brown @*/
667e727c939SJed Brown PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,const MatType mtype,ISColoring *coloring)
66847c6ae99SBarry Smith {
66947c6ae99SBarry Smith   PetscErrorCode ierr;
67047c6ae99SBarry Smith 
67147c6ae99SBarry Smith   PetscFunctionBegin;
672171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
67347c6ae99SBarry Smith   if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet");
67447c6ae99SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr);
67547c6ae99SBarry Smith   PetscFunctionReturn(0);
67647c6ae99SBarry Smith }
67747c6ae99SBarry Smith 
67847c6ae99SBarry Smith #undef __FUNCT__
679950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
68047c6ae99SBarry Smith /*@C
681950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
68247c6ae99SBarry Smith 
68347c6ae99SBarry Smith     Collective on DM
68447c6ae99SBarry Smith 
68547c6ae99SBarry Smith     Input Parameter:
68647c6ae99SBarry Smith +   dm - the DM object
68747c6ae99SBarry Smith -   mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or
68894013140SBarry Smith             any type which inherits from one of these (such as MATAIJ)
68947c6ae99SBarry Smith 
69047c6ae99SBarry Smith     Output Parameter:
69147c6ae99SBarry Smith .   mat - the empty Jacobian
69247c6ae99SBarry Smith 
693073dac72SJed Brown     Level: beginner
69447c6ae99SBarry Smith 
69594013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
69694013140SBarry Smith        do not need to do it yourself.
69794013140SBarry Smith 
69894013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
699aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
70094013140SBarry Smith 
70194013140SBarry 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
70294013140SBarry Smith        internally by PETSc.
70394013140SBarry Smith 
70494013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
705aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
70694013140SBarry Smith 
707e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
70847c6ae99SBarry Smith 
709aab9d709SJed Brown @*/
710950540a4SJed Brown PetscErrorCode  DMCreateMatrix(DM dm,const MatType mtype,Mat *mat)
71147c6ae99SBarry Smith {
71247c6ae99SBarry Smith   PetscErrorCode ierr;
71347c6ae99SBarry Smith 
71447c6ae99SBarry Smith   PetscFunctionBegin;
715171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
716235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
717235683edSBarry Smith   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
718235683edSBarry Smith #endif
719c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
720c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
721073dac72SJed Brown   if (dm->mattype) {
72225296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr);
723073dac72SJed Brown   } else {
72425296bd5SBarry Smith     ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr);
725c7b7c8a4SJed Brown   }
72647c6ae99SBarry Smith   PetscFunctionReturn(0);
72747c6ae99SBarry Smith }
72847c6ae99SBarry Smith 
72947c6ae99SBarry Smith #undef __FUNCT__
730732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
731732e2eb9SMatthew G Knepley /*@
732950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
733732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
734732e2eb9SMatthew G Knepley 
735732e2eb9SMatthew G Knepley   Logically Collective on DMDA
736732e2eb9SMatthew G Knepley 
737732e2eb9SMatthew G Knepley   Input Parameter:
738732e2eb9SMatthew G Knepley + dm - the DM
739732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
740732e2eb9SMatthew G Knepley 
741732e2eb9SMatthew G Knepley   Level: developer
742950540a4SJed Brown .seealso DMCreateMatrix()
743732e2eb9SMatthew G Knepley @*/
744732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
745732e2eb9SMatthew G Knepley {
746732e2eb9SMatthew G Knepley   PetscFunctionBegin;
747732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
748732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
749732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
750732e2eb9SMatthew G Knepley }
751732e2eb9SMatthew G Knepley 
752732e2eb9SMatthew G Knepley #undef __FUNCT__
753a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
754a89ea682SMatthew G Knepley /*@C
755a89ea682SMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size
756a89ea682SMatthew G Knepley 
757a89ea682SMatthew G Knepley   Not Collective
758a89ea682SMatthew G Knepley 
759a89ea682SMatthew G Knepley   Input Parameters:
760a89ea682SMatthew G Knepley + dm - the DM object
761a89ea682SMatthew G Knepley - size - The minium size
762a89ea682SMatthew G Knepley 
763a89ea682SMatthew G Knepley   Output Parameter:
764a89ea682SMatthew G Knepley . array - the work array
765a89ea682SMatthew G Knepley 
766a89ea682SMatthew G Knepley   Level: developer
767a89ea682SMatthew G Knepley 
768a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
769a89ea682SMatthew G Knepley @*/
770a89ea682SMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt size,PetscScalar **array)
771a89ea682SMatthew G Knepley {
772a89ea682SMatthew G Knepley   PetscErrorCode ierr;
773a89ea682SMatthew G Knepley 
774a89ea682SMatthew G Knepley   PetscFunctionBegin;
775a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
776a89ea682SMatthew G Knepley   PetscValidPointer(array,3);
777a89ea682SMatthew G Knepley   if (size > dm->workSize) {
778a89ea682SMatthew G Knepley     dm->workSize = size;
779a89ea682SMatthew G Knepley     ierr = PetscFree(dm->workArray);CHKERRQ(ierr);
780a89ea682SMatthew G Knepley     ierr = PetscMalloc(dm->workSize * sizeof(PetscScalar), &dm->workArray);CHKERRQ(ierr);
781a89ea682SMatthew G Knepley   }
782a89ea682SMatthew G Knepley   *array = dm->workArray;
783a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
784a89ea682SMatthew G Knepley }
785a89ea682SMatthew G Knepley 
7864d343eeaSMatthew G Knepley #undef __FUNCT__
787e7c4fc90SDmitry Karpeev #define __FUNCT__ "DMCreateDecompositionDM"
788e7c4fc90SDmitry Karpeev /*@C
78901bc414fSDmitry Karpeev   DMCreateDecompositionDM - creates a DM that encapsulates a decomposition of the original DM.
790e7c4fc90SDmitry Karpeev 
791e7c4fc90SDmitry Karpeev   Not Collective
792e7c4fc90SDmitry Karpeev 
793e7c4fc90SDmitry Karpeev   Input Parameters:
794e7c4fc90SDmitry Karpeev + dm   - the DM object
795e7c4fc90SDmitry Karpeev - name - the name of the decomposition
796e7c4fc90SDmitry Karpeev 
797e7c4fc90SDmitry Karpeev   Output Parameter:
798e7c4fc90SDmitry Karpeev . ddm  - the decomposition DM (PETSC_NULL, if no such decomposition is known)
799e7c4fc90SDmitry Karpeev 
800e7c4fc90SDmitry Karpeev   Level: advanced
801e7c4fc90SDmitry Karpeev 
802e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateDecomposition()
803e7c4fc90SDmitry Karpeev @*/
804e7c4fc90SDmitry Karpeev PetscErrorCode DMCreateDecompositionDM(DM dm, const char* name, DM *ddm)
805e7c4fc90SDmitry Karpeev {
806e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
807e7c4fc90SDmitry Karpeev 
808e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
809e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
810e7c4fc90SDmitry Karpeev   PetscValidCharPointer(name,2);
811e7c4fc90SDmitry Karpeev   PetscValidPointer(ddm,3);
812e7c4fc90SDmitry Karpeev   if(!dm->ops->createdecompositiondm) {
813e7c4fc90SDmitry Karpeev     *ddm = PETSC_NULL;
814e7c4fc90SDmitry Karpeev   }
815e7c4fc90SDmitry Karpeev   else {
816e7c4fc90SDmitry Karpeev     ierr = (*dm->ops->createdecompositiondm)(dm,name,ddm); CHKERRQ(ierr);
817e7c4fc90SDmitry Karpeev   }
818e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
819e7c4fc90SDmitry Karpeev }
820e7c4fc90SDmitry Karpeev 
821e7c4fc90SDmitry Karpeev #undef __FUNCT__
8224d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
8234f3b5142SJed Brown /*@C
8244d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
8254d343eeaSMatthew G Knepley 
8264d343eeaSMatthew G Knepley   Not collective
8274d343eeaSMatthew G Knepley 
8284d343eeaSMatthew G Knepley   Input Parameter:
8294d343eeaSMatthew G Knepley . dm - the DM object
8304d343eeaSMatthew G Knepley 
8314d343eeaSMatthew G Knepley   Output Parameters:
83221c9b008SJed Brown + numFields - The number of fields (or PETSC_NULL if not requested)
83321c9b008SJed Brown . names     - The name for each field (or PETSC_NULL if not requested)
83421c9b008SJed Brown - fields    - The global indices for each field (or PETSC_NULL if not requested)
8354d343eeaSMatthew G Knepley 
8364d343eeaSMatthew G Knepley   Level: intermediate
8374d343eeaSMatthew G Knepley 
83821c9b008SJed Brown   Notes:
83921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
84021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
84121c9b008SJed Brown   PetscFree().
84221c9b008SJed Brown 
8434d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
8444d343eeaSMatthew G Knepley @*/
84521c9b008SJed Brown PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***names, IS **fields)
8464d343eeaSMatthew G Knepley {
8474d343eeaSMatthew G Knepley   PetscErrorCode ierr;
8484d343eeaSMatthew G Knepley 
8494d343eeaSMatthew G Knepley   PetscFunctionBegin;
8504d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85169ca1f37SDmitry Karpeev   if (numFields) {
85269ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
85369ca1f37SDmitry Karpeev     *numFields = 0;
85469ca1f37SDmitry Karpeev   }
85569ca1f37SDmitry Karpeev   if (names) {
85669ca1f37SDmitry Karpeev     PetscValidPointer(names,3);
85769ca1f37SDmitry Karpeev     *names = PETSC_NULL;
85869ca1f37SDmitry Karpeev   }
85969ca1f37SDmitry Karpeev   if (fields) {
86069ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
86169ca1f37SDmitry Karpeev     *fields = PETSC_NULL;
86269ca1f37SDmitry Karpeev   }
86369ca1f37SDmitry Karpeev   if(dm->ops->createfieldis) {
8644d343eeaSMatthew G Knepley     ierr = (*dm->ops->createfieldis)(dm, numFields, names, fields);CHKERRQ(ierr);
86569ca1f37SDmitry Karpeev   }
8664d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
8674d343eeaSMatthew G Knepley }
8684d343eeaSMatthew G Knepley 
8695fe1f584SPeter Brune 
870a89ea682SMatthew G Knepley #undef __FUNCT__
871e7c4fc90SDmitry Karpeev #define __FUNCT__ "DMCreateDecomposition"
872e7c4fc90SDmitry Karpeev /*@C
873e7c4fc90SDmitry Karpeev   DMCreateDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems:
874e7c4fc90SDmitry Karpeev                           each IS contains the global indices of the dofs of the corresponding subproblem.
875e7c4fc90SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
876e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
877e7c4fc90SDmitry Karpeev 
878e7c4fc90SDmitry Karpeev   Not collective
879e7c4fc90SDmitry Karpeev 
880e7c4fc90SDmitry Karpeev   Input Parameter:
881e7c4fc90SDmitry Karpeev . dm - the DM object
882e7c4fc90SDmitry Karpeev 
883e7c4fc90SDmitry Karpeev   Output Parameters:
884e7c4fc90SDmitry Karpeev + len       - The number of subproblems in the decomposition (or PETSC_NULL if not requested)
885e7c4fc90SDmitry Karpeev . namelist  - The name for each subproblem (or PETSC_NULL if not requested)
886e7c4fc90SDmitry Karpeev . islist    - The global indices for each subproblem (or PETSC_NULL if not requested)
887e7c4fc90SDmitry Karpeev - dmlist    - The DMs for each subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined)
888e7c4fc90SDmitry Karpeev 
889e7c4fc90SDmitry Karpeev   Level: intermediate
890e7c4fc90SDmitry Karpeev 
891e7c4fc90SDmitry Karpeev   Notes:
892e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
893e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
894e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
895e7c4fc90SDmitry Karpeev 
896e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
897e7c4fc90SDmitry Karpeev @*/
898e7c4fc90SDmitry Karpeev PetscErrorCode DMCreateDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
899e7c4fc90SDmitry Karpeev {
900e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
901e7c4fc90SDmitry Karpeev 
902e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
903e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
904e7c4fc90SDmitry Karpeev   if (len) {PetscValidPointer(len,2);}
905e7c4fc90SDmitry Karpeev   if (namelist) {PetscValidPointer(namelist,3);}
906e7c4fc90SDmitry Karpeev   if (islist) {PetscValidPointer(islist,4);}
907e7c4fc90SDmitry Karpeev   if (dmlist) {PetscValidPointer(dmlist,5);}
908e7c4fc90SDmitry Karpeev   if(!dm->ops->createdecomposition) {
90969ca1f37SDmitry Karpeev     ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
910e7c4fc90SDmitry Karpeev     /* By default there are no DMs associated with subproblems. */
911e7c4fc90SDmitry Karpeev     if(dmlist) *dmlist = PETSC_NULL;
912e7c4fc90SDmitry Karpeev   }
913e7c4fc90SDmitry Karpeev   else {
914e7c4fc90SDmitry Karpeev     ierr = (*dm->ops->createdecomposition)(dm,len,namelist,islist,dmlist); CHKERRQ(ierr);
915e7c4fc90SDmitry Karpeev   }
916e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
917e7c4fc90SDmitry Karpeev }
918e7c4fc90SDmitry Karpeev 
919e7c4fc90SDmitry Karpeev 
920e7c4fc90SDmitry Karpeev #undef __FUNCT__
92147c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
92247c6ae99SBarry Smith /*@
92347c6ae99SBarry Smith   DMRefine - Refines a DM object
92447c6ae99SBarry Smith 
92547c6ae99SBarry Smith   Collective on DM
92647c6ae99SBarry Smith 
92747c6ae99SBarry Smith   Input Parameter:
92847c6ae99SBarry Smith + dm   - the DM object
92991d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
93047c6ae99SBarry Smith 
93147c6ae99SBarry Smith   Output Parameter:
932ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL
933ae0a1c52SMatthew G Knepley 
934ae0a1c52SMatthew G Knepley   Note: If no refinement was done, the return value is PETSC_NULL
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith   Level: developer
93747c6ae99SBarry Smith 
938e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
93947c6ae99SBarry Smith @*/
9407087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
94147c6ae99SBarry Smith {
94247c6ae99SBarry Smith   PetscErrorCode ierr;
94347c6ae99SBarry Smith 
94447c6ae99SBarry Smith   PetscFunctionBegin;
945732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
94647c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
9474057135bSMatthew G Knepley   if (*dmf) {
94843842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
949644e2e5bSBarry Smith     (*dmf)->ops->initialguess = dm->ops->initialguess;
950644e2e5bSBarry Smith     (*dmf)->ops->function     = dm->ops->function;
951644e2e5bSBarry Smith     (*dmf)->ops->functionj    = dm->ops->functionj;
952644e2e5bSBarry Smith     if (dm->ops->jacobian != DMComputeJacobianDefault) {
953644e2e5bSBarry Smith       (*dmf)->ops->jacobian     = dm->ops->jacobian;
954644e2e5bSBarry Smith     }
9558cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
956644e2e5bSBarry Smith     (*dmf)->ctx     = dm->ctx;
957656b349aSBarry Smith     (*dmf)->levelup = dm->levelup + 1;
9584057135bSMatthew G Knepley   }
95947c6ae99SBarry Smith   PetscFunctionReturn(0);
96047c6ae99SBarry Smith }
96147c6ae99SBarry Smith 
96247c6ae99SBarry Smith #undef __FUNCT__
963eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
964eb3f98d2SBarry Smith /*@
965eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
966eb3f98d2SBarry Smith 
967eb3f98d2SBarry Smith     Not Collective
968eb3f98d2SBarry Smith 
969eb3f98d2SBarry Smith     Input Parameter:
970eb3f98d2SBarry Smith .   dm - the DM object
971eb3f98d2SBarry Smith 
972eb3f98d2SBarry Smith     Output Parameter:
973eb3f98d2SBarry Smith .   level - number of refinements
974eb3f98d2SBarry Smith 
975eb3f98d2SBarry Smith     Level: developer
976eb3f98d2SBarry Smith 
9776a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
978eb3f98d2SBarry Smith 
979eb3f98d2SBarry Smith @*/
980eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
981eb3f98d2SBarry Smith {
982eb3f98d2SBarry Smith   PetscFunctionBegin;
983eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
984eb3f98d2SBarry Smith   *level = dm->levelup;
985eb3f98d2SBarry Smith   PetscFunctionReturn(0);
986eb3f98d2SBarry Smith }
987eb3f98d2SBarry Smith 
988eb3f98d2SBarry Smith #undef __FUNCT__
98947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
99047c6ae99SBarry Smith /*@
99147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
99247c6ae99SBarry Smith 
99347c6ae99SBarry Smith     Neighbor-wise Collective on DM
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith     Input Parameters:
99647c6ae99SBarry Smith +   dm - the DM object
99747c6ae99SBarry Smith .   g - the global vector
99847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
99947c6ae99SBarry Smith -   l - the local vector
100047c6ae99SBarry Smith 
100147c6ae99SBarry Smith 
100247c6ae99SBarry Smith     Level: beginner
100347c6ae99SBarry Smith 
1004e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith @*/
10077087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
100847c6ae99SBarry Smith {
10097128ae9fSMatthew G Knepley   PetscSF        sf;
101047c6ae99SBarry Smith   PetscErrorCode ierr;
101147c6ae99SBarry Smith 
101247c6ae99SBarry Smith   PetscFunctionBegin;
1013171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10147128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
10157128ae9fSMatthew G Knepley   if (sf) {
10167128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
10177128ae9fSMatthew G Knepley 
10187128ae9fSMatthew G Knepley     if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
10197128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
10207128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
10217128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
10227128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
10237128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
10247128ae9fSMatthew G Knepley   } else {
1025843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
10267128ae9fSMatthew G Knepley   }
102747c6ae99SBarry Smith   PetscFunctionReturn(0);
102847c6ae99SBarry Smith }
102947c6ae99SBarry Smith 
103047c6ae99SBarry Smith #undef __FUNCT__
103147c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
103247c6ae99SBarry Smith /*@
103347c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
103447c6ae99SBarry Smith 
103547c6ae99SBarry Smith     Neighbor-wise Collective on DM
103647c6ae99SBarry Smith 
103747c6ae99SBarry Smith     Input Parameters:
103847c6ae99SBarry Smith +   dm - the DM object
103947c6ae99SBarry Smith .   g - the global vector
104047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
104147c6ae99SBarry Smith -   l - the local vector
104247c6ae99SBarry Smith 
104347c6ae99SBarry Smith 
104447c6ae99SBarry Smith     Level: beginner
104547c6ae99SBarry Smith 
1046e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
104747c6ae99SBarry Smith 
104847c6ae99SBarry Smith @*/
10497087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
105047c6ae99SBarry Smith {
10517128ae9fSMatthew G Knepley   PetscSF        sf;
105247c6ae99SBarry Smith   PetscErrorCode ierr;
105347c6ae99SBarry Smith 
105447c6ae99SBarry Smith   PetscFunctionBegin;
1055171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10567128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
10577128ae9fSMatthew G Knepley   if (sf) {
10587128ae9fSMatthew G Knepley   if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
10597128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
10607128ae9fSMatthew G Knepley 
10617128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
10627128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
10637128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
10647128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
10657128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
10667128ae9fSMatthew G Knepley   } else {
1067843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
10687128ae9fSMatthew G Knepley   }
106947c6ae99SBarry Smith   PetscFunctionReturn(0);
107047c6ae99SBarry Smith }
107147c6ae99SBarry Smith 
107247c6ae99SBarry Smith #undef __FUNCT__
10739a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
107447c6ae99SBarry Smith /*@
10759a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
10769a42bb27SBarry Smith 
10779a42bb27SBarry Smith     Neighbor-wise Collective on DM
10789a42bb27SBarry Smith 
10799a42bb27SBarry Smith     Input Parameters:
10809a42bb27SBarry Smith +   dm - the DM object
1081f6813fd5SJed Brown .   l - the local vector
10829a42bb27SBarry 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
10839a42bb27SBarry Smith            base point.
1084f6813fd5SJed Brown - - the global vector
10859a42bb27SBarry Smith 
10869a42bb27SBarry 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
10879a42bb27SBarry 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
10889a42bb27SBarry Smith            global array to the final global array with VecAXPY().
10899a42bb27SBarry Smith 
10909a42bb27SBarry Smith     Level: beginner
10919a42bb27SBarry Smith 
1092e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
10939a42bb27SBarry Smith 
10949a42bb27SBarry Smith @*/
10957087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
10969a42bb27SBarry Smith {
10977128ae9fSMatthew G Knepley   PetscSF        sf;
10989a42bb27SBarry Smith   PetscErrorCode ierr;
10999a42bb27SBarry Smith 
11009a42bb27SBarry Smith   PetscFunctionBegin;
1101171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11027128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
11037128ae9fSMatthew G Knepley   if (sf) {
11047128ae9fSMatthew G Knepley     MPI_Op       op;
11057128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
11067128ae9fSMatthew G Knepley 
11077128ae9fSMatthew G Knepley     switch(mode) {
11087128ae9fSMatthew G Knepley     case INSERT_VALUES:
11097128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
11107128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
11117128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
11127128ae9fSMatthew G Knepley #else
11137128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
11147128ae9fSMatthew G Knepley #endif
11157128ae9fSMatthew G Knepley     case ADD_VALUES:
11167128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
11177128ae9fSMatthew G Knepley       op = MPI_SUM; break;
11187128ae9fSMatthew G Knepley   default:
11197128ae9fSMatthew G Knepley     SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
11207128ae9fSMatthew G Knepley     }
11217128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
11227128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
11237128ae9fSMatthew G Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
11247128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
11257128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
11267128ae9fSMatthew G Knepley   } else {
1127843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
11287128ae9fSMatthew G Knepley   }
11299a42bb27SBarry Smith   PetscFunctionReturn(0);
11309a42bb27SBarry Smith }
11319a42bb27SBarry Smith 
11329a42bb27SBarry Smith #undef __FUNCT__
11339a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
11349a42bb27SBarry Smith /*@
11359a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
113647c6ae99SBarry Smith 
113747c6ae99SBarry Smith     Neighbor-wise Collective on DM
113847c6ae99SBarry Smith 
113947c6ae99SBarry Smith     Input Parameters:
114047c6ae99SBarry Smith +   dm - the DM object
1141f6813fd5SJed Brown .   l - the local vector
114247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1143f6813fd5SJed Brown -   g - the global vector
114447c6ae99SBarry Smith 
114547c6ae99SBarry Smith 
114647c6ae99SBarry Smith     Level: beginner
114747c6ae99SBarry Smith 
1148e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
114947c6ae99SBarry Smith 
115047c6ae99SBarry Smith @*/
11517087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
115247c6ae99SBarry Smith {
11537128ae9fSMatthew G Knepley   PetscSF        sf;
115447c6ae99SBarry Smith   PetscErrorCode ierr;
115547c6ae99SBarry Smith 
115647c6ae99SBarry Smith   PetscFunctionBegin;
1157171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11587128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
11597128ae9fSMatthew G Knepley   if (sf) {
11607128ae9fSMatthew G Knepley     MPI_Op       op;
11617128ae9fSMatthew G Knepley     PetscScalar *lArray, *gArray;
11627128ae9fSMatthew G Knepley 
11637128ae9fSMatthew G Knepley     switch(mode) {
11647128ae9fSMatthew G Knepley     case INSERT_VALUES:
11657128ae9fSMatthew G Knepley     case INSERT_ALL_VALUES:
11667128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE)
11677128ae9fSMatthew G Knepley       op = MPI_REPLACE; break;
11687128ae9fSMatthew G Knepley #else
11697128ae9fSMatthew G Knepley       SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation");
11707128ae9fSMatthew G Knepley #endif
11717128ae9fSMatthew G Knepley     case ADD_VALUES:
11727128ae9fSMatthew G Knepley     case ADD_ALL_VALUES:
11737128ae9fSMatthew G Knepley       op = MPI_SUM; break;
11747128ae9fSMatthew G Knepley     default:
11757128ae9fSMatthew G Knepley       SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
11767128ae9fSMatthew G Knepley     }
11777128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
11787128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
11797128ae9fSMatthew G Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr);
11807128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
11817128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
11827128ae9fSMatthew G Knepley   } else {
1183843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
11847128ae9fSMatthew G Knepley   }
118547c6ae99SBarry Smith   PetscFunctionReturn(0);
118647c6ae99SBarry Smith }
118747c6ae99SBarry Smith 
118847c6ae99SBarry Smith #undef __FUNCT__
118947c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobianDefault"
119047c6ae99SBarry Smith /*@
119147c6ae99SBarry Smith     DMComputeJacobianDefault - computes the Jacobian using the DMComputeFunction() if Jacobian computer is not provided
119247c6ae99SBarry Smith 
119347c6ae99SBarry Smith     Collective on DM
119447c6ae99SBarry Smith 
119547c6ae99SBarry Smith     Input Parameter:
119647c6ae99SBarry Smith +   dm - the DM object
119747c6ae99SBarry Smith .   x - location to compute Jacobian at; may be ignored for linear problems
119847c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
119947c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
120047c6ae99SBarry Smith 
120147c6ae99SBarry Smith     Level: developer
120247c6ae99SBarry Smith 
1203e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
120447c6ae99SBarry Smith          DMSetFunction()
120547c6ae99SBarry Smith 
120647c6ae99SBarry Smith @*/
12077087cfbeSBarry Smith PetscErrorCode  DMComputeJacobianDefault(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
120847c6ae99SBarry Smith {
120947c6ae99SBarry Smith   PetscErrorCode ierr;
1210171400e9SBarry Smith 
121147c6ae99SBarry Smith   PetscFunctionBegin;
1212171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
121347c6ae99SBarry Smith   *stflag = SAME_NONZERO_PATTERN;
121447c6ae99SBarry Smith   ierr  = MatFDColoringApply(B,dm->fd,x,stflag,dm);CHKERRQ(ierr);
121547c6ae99SBarry Smith   if (A != B) {
121647c6ae99SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
121747c6ae99SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
121847c6ae99SBarry Smith   }
121947c6ae99SBarry Smith   PetscFunctionReturn(0);
122047c6ae99SBarry Smith }
122147c6ae99SBarry Smith 
122247c6ae99SBarry Smith #undef __FUNCT__
122347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
122447c6ae99SBarry Smith /*@
122547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
122647c6ae99SBarry Smith 
122747c6ae99SBarry Smith     Collective on DM
122847c6ae99SBarry Smith 
122947c6ae99SBarry Smith     Input Parameter:
123047c6ae99SBarry Smith +   dm - the DM object
123191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
123247c6ae99SBarry Smith 
123347c6ae99SBarry Smith     Output Parameter:
123447c6ae99SBarry Smith .   dmc - the coarsened DM
123547c6ae99SBarry Smith 
123647c6ae99SBarry Smith     Level: developer
123747c6ae99SBarry Smith 
1238e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
123947c6ae99SBarry Smith 
124047c6ae99SBarry Smith @*/
12417087cfbeSBarry Smith PetscErrorCode  DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
124247c6ae99SBarry Smith {
124347c6ae99SBarry Smith   PetscErrorCode ierr;
1244b17ce1afSJed Brown   DMCoarsenHookLink link;
124547c6ae99SBarry Smith 
124647c6ae99SBarry Smith   PetscFunctionBegin;
1247171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
124847c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
124943842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
125047c6ae99SBarry Smith   (*dmc)->ops->initialguess = dm->ops->initialguess;
125147c6ae99SBarry Smith   (*dmc)->ops->function     = dm->ops->function;
125247c6ae99SBarry Smith   (*dmc)->ops->functionj    = dm->ops->functionj;
125347c6ae99SBarry Smith   if (dm->ops->jacobian != DMComputeJacobianDefault) {
125447c6ae99SBarry Smith     (*dmc)->ops->jacobian     = dm->ops->jacobian;
125547c6ae99SBarry Smith   }
12568cd211a4SJed Brown   ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
1257644e2e5bSBarry Smith   (*dmc)->ctx       = dm->ctx;
1258656b349aSBarry Smith   (*dmc)->leveldown = dm->leveldown + 1;
1259b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
1260b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
1261b17ce1afSJed Brown   }
1262b17ce1afSJed Brown   PetscFunctionReturn(0);
1263b17ce1afSJed Brown }
1264b17ce1afSJed Brown 
1265b17ce1afSJed Brown #undef __FUNCT__
1266b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
1267b17ce1afSJed Brown /*@
1268b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
1269b17ce1afSJed Brown 
1270b17ce1afSJed Brown    Logically Collective
1271b17ce1afSJed Brown 
1272b17ce1afSJed Brown    Input Arguments:
1273b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
1274b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
1275b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
1276b17ce1afSJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL)
1277b17ce1afSJed Brown 
1278b17ce1afSJed Brown    Calling sequence of coarsenhook:
1279b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
1280b17ce1afSJed Brown 
1281b17ce1afSJed Brown +  fine - fine level DM
1282b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
1283b17ce1afSJed Brown -  ctx - optional user-defined function context
1284b17ce1afSJed Brown 
1285b17ce1afSJed Brown    Calling sequence for restricthook:
1286b17ce1afSJed Brown $    restricthook(DM fine,Mat mrestrict,Mat inject,DM coarse,void *ctx)
1287b17ce1afSJed Brown 
1288b17ce1afSJed Brown +  fine - fine level DM
1289b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
1290b17ce1afSJed Brown .  inject - matrix restricting by applying the transpose of injection
1291b17ce1afSJed Brown .  coarse - coarse level DM to update
1292b17ce1afSJed Brown -  ctx - optional user-defined function context
1293b17ce1afSJed Brown 
1294b17ce1afSJed Brown    Level: advanced
1295b17ce1afSJed Brown 
1296b17ce1afSJed Brown    Notes:
1297b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
1298b17ce1afSJed Brown 
1299b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1300b17ce1afSJed Brown 
1301b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
1302b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
1303b17ce1afSJed Brown 
1304b17ce1afSJed Brown .seealso: SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1305b17ce1afSJed Brown @*/
1306b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
1307b17ce1afSJed Brown {
1308b17ce1afSJed Brown   PetscErrorCode ierr;
1309b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
1310b17ce1afSJed Brown 
1311b17ce1afSJed Brown   PetscFunctionBegin;
1312b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
13136bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1314b17ce1afSJed Brown   ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
1315b17ce1afSJed Brown   link->coarsenhook = coarsenhook;
1316b17ce1afSJed Brown   link->restricthook = restricthook;
1317b17ce1afSJed Brown   link->ctx = ctx;
13186cab3a1bSJed Brown   link->next = PETSC_NULL;
1319b17ce1afSJed Brown   *p = link;
1320b17ce1afSJed Brown   PetscFunctionReturn(0);
1321b17ce1afSJed Brown }
1322b17ce1afSJed Brown 
1323b17ce1afSJed Brown #undef __FUNCT__
1324b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
1325b17ce1afSJed Brown /*@
1326b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
1327b17ce1afSJed Brown 
1328b17ce1afSJed Brown    Collective if any hooks are
1329b17ce1afSJed Brown 
1330b17ce1afSJed Brown    Input Arguments:
1331b17ce1afSJed Brown +  fine - finer DM to use as a base
1332b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
1333b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
1334b17ce1afSJed Brown -  coarse - coarer DM to update
1335b17ce1afSJed Brown 
1336b17ce1afSJed Brown    Level: developer
1337b17ce1afSJed Brown 
1338b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
1339b17ce1afSJed Brown @*/
1340b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
1341b17ce1afSJed Brown {
1342b17ce1afSJed Brown   PetscErrorCode ierr;
1343b17ce1afSJed Brown   DMCoarsenHookLink link;
1344b17ce1afSJed Brown 
1345b17ce1afSJed Brown   PetscFunctionBegin;
1346b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
1347b17ce1afSJed Brown     if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);}
1348b17ce1afSJed Brown   }
134947c6ae99SBarry Smith   PetscFunctionReturn(0);
135047c6ae99SBarry Smith }
135147c6ae99SBarry Smith 
135247c6ae99SBarry Smith #undef __FUNCT__
13535fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
13545fe1f584SPeter Brune /*@
13556a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
13565fe1f584SPeter Brune 
13575fe1f584SPeter Brune     Not Collective
13585fe1f584SPeter Brune 
13595fe1f584SPeter Brune     Input Parameter:
13605fe1f584SPeter Brune .   dm - the DM object
13615fe1f584SPeter Brune 
13625fe1f584SPeter Brune     Output Parameter:
13636a7d9d85SPeter Brune .   level - number of coarsenings
13645fe1f584SPeter Brune 
13655fe1f584SPeter Brune     Level: developer
13665fe1f584SPeter Brune 
13676a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
13685fe1f584SPeter Brune 
13695fe1f584SPeter Brune @*/
13705fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
13715fe1f584SPeter Brune {
13725fe1f584SPeter Brune   PetscFunctionBegin;
13735fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13745fe1f584SPeter Brune   *level = dm->leveldown;
13755fe1f584SPeter Brune   PetscFunctionReturn(0);
13765fe1f584SPeter Brune }
13775fe1f584SPeter Brune 
13785fe1f584SPeter Brune 
13795fe1f584SPeter Brune 
13805fe1f584SPeter Brune #undef __FUNCT__
138147c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
138247c6ae99SBarry Smith /*@C
138347c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
138447c6ae99SBarry Smith 
138547c6ae99SBarry Smith     Collective on DM
138647c6ae99SBarry Smith 
138747c6ae99SBarry Smith     Input Parameter:
138847c6ae99SBarry Smith +   dm - the DM object
138947c6ae99SBarry Smith -   nlevels - the number of levels of refinement
139047c6ae99SBarry Smith 
139147c6ae99SBarry Smith     Output Parameter:
139247c6ae99SBarry Smith .   dmf - the refined DM hierarchy
139347c6ae99SBarry Smith 
139447c6ae99SBarry Smith     Level: developer
139547c6ae99SBarry Smith 
1396e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
139747c6ae99SBarry Smith 
139847c6ae99SBarry Smith @*/
13997087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
140047c6ae99SBarry Smith {
140147c6ae99SBarry Smith   PetscErrorCode ierr;
140247c6ae99SBarry Smith 
140347c6ae99SBarry Smith   PetscFunctionBegin;
1404171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
140547c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
140647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
140747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
140847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
140947c6ae99SBarry Smith   } else if (dm->ops->refine) {
141047c6ae99SBarry Smith     PetscInt i;
141147c6ae99SBarry Smith 
141247c6ae99SBarry Smith     ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr);
141347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
141447c6ae99SBarry Smith       ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr);
141547c6ae99SBarry Smith     }
141647c6ae99SBarry Smith   } else {
141747c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
141847c6ae99SBarry Smith   }
141947c6ae99SBarry Smith   PetscFunctionReturn(0);
142047c6ae99SBarry Smith }
142147c6ae99SBarry Smith 
142247c6ae99SBarry Smith #undef __FUNCT__
142347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
142447c6ae99SBarry Smith /*@C
142547c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
142647c6ae99SBarry Smith 
142747c6ae99SBarry Smith     Collective on DM
142847c6ae99SBarry Smith 
142947c6ae99SBarry Smith     Input Parameter:
143047c6ae99SBarry Smith +   dm - the DM object
143147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
143247c6ae99SBarry Smith 
143347c6ae99SBarry Smith     Output Parameter:
143447c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
143547c6ae99SBarry Smith 
143647c6ae99SBarry Smith     Level: developer
143747c6ae99SBarry Smith 
1438e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
143947c6ae99SBarry Smith 
144047c6ae99SBarry Smith @*/
14417087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
144247c6ae99SBarry Smith {
144347c6ae99SBarry Smith   PetscErrorCode ierr;
144447c6ae99SBarry Smith 
144547c6ae99SBarry Smith   PetscFunctionBegin;
1446171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
144747c6ae99SBarry Smith   if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
144847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
144947c6ae99SBarry Smith   PetscValidPointer(dmc,3);
145047c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
145147c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
145247c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
145347c6ae99SBarry Smith     PetscInt i;
145447c6ae99SBarry Smith 
145547c6ae99SBarry Smith     ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr);
145647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
145747c6ae99SBarry Smith       ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr);
145847c6ae99SBarry Smith     }
145947c6ae99SBarry Smith   } else {
146047c6ae99SBarry Smith     SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
146147c6ae99SBarry Smith   }
146247c6ae99SBarry Smith   PetscFunctionReturn(0);
146347c6ae99SBarry Smith }
146447c6ae99SBarry Smith 
146547c6ae99SBarry Smith #undef __FUNCT__
1466e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
146747c6ae99SBarry Smith /*@
1468e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
146947c6ae99SBarry Smith    grids associated with two DMs.
147047c6ae99SBarry Smith 
147147c6ae99SBarry Smith    Collective on DM
147247c6ae99SBarry Smith 
147347c6ae99SBarry Smith    Input Parameters:
147447c6ae99SBarry Smith +  dmc - the coarse grid DM
147547c6ae99SBarry Smith -  dmf - the fine grid DM
147647c6ae99SBarry Smith 
147747c6ae99SBarry Smith    Output Parameters:
147847c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
147947c6ae99SBarry Smith 
148047c6ae99SBarry Smith    Level: intermediate
148147c6ae99SBarry Smith 
148247c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
148347c6ae99SBarry Smith 
1484e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
148547c6ae99SBarry Smith @*/
1486e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
148747c6ae99SBarry Smith {
148847c6ae99SBarry Smith   PetscErrorCode ierr;
148947c6ae99SBarry Smith 
149047c6ae99SBarry Smith   PetscFunctionBegin;
1491171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1492171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
149347c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
149447c6ae99SBarry Smith   PetscFunctionReturn(0);
149547c6ae99SBarry Smith }
149647c6ae99SBarry Smith 
149747c6ae99SBarry Smith #undef __FUNCT__
14981a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
14991a266240SBarry Smith /*@C
15001a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
15011a266240SBarry Smith 
15021a266240SBarry Smith     Not Collective
15031a266240SBarry Smith 
15041a266240SBarry Smith     Input Parameters:
15051a266240SBarry Smith +   dm - the DM object
15061a266240SBarry Smith -   destroy - the destroy function
15071a266240SBarry Smith 
15081a266240SBarry Smith     Level: intermediate
15091a266240SBarry Smith 
1510e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
15111a266240SBarry Smith 
1512f07f9ceaSJed Brown @*/
15131a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
15141a266240SBarry Smith {
15151a266240SBarry Smith   PetscFunctionBegin;
1516171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15171a266240SBarry Smith   dm->ctxdestroy = destroy;
15181a266240SBarry Smith   PetscFunctionReturn(0);
15191a266240SBarry Smith }
15201a266240SBarry Smith 
15211a266240SBarry Smith #undef __FUNCT__
15221b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
1523b07ff414SBarry Smith /*@
15241b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
152547c6ae99SBarry Smith 
152647c6ae99SBarry Smith     Not Collective
152747c6ae99SBarry Smith 
152847c6ae99SBarry Smith     Input Parameters:
152947c6ae99SBarry Smith +   dm - the DM object
153047c6ae99SBarry Smith -   ctx - the user context
153147c6ae99SBarry Smith 
153247c6ae99SBarry Smith     Level: intermediate
153347c6ae99SBarry Smith 
1534e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
153547c6ae99SBarry Smith 
153647c6ae99SBarry Smith @*/
15371b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
153847c6ae99SBarry Smith {
153947c6ae99SBarry Smith   PetscFunctionBegin;
1540171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
154147c6ae99SBarry Smith   dm->ctx = ctx;
154247c6ae99SBarry Smith   PetscFunctionReturn(0);
154347c6ae99SBarry Smith }
154447c6ae99SBarry Smith 
154547c6ae99SBarry Smith #undef __FUNCT__
15461b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
154747c6ae99SBarry Smith /*@
15481b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
154947c6ae99SBarry Smith 
155047c6ae99SBarry Smith     Not Collective
155147c6ae99SBarry Smith 
155247c6ae99SBarry Smith     Input Parameter:
155347c6ae99SBarry Smith .   dm - the DM object
155447c6ae99SBarry Smith 
155547c6ae99SBarry Smith     Output Parameter:
155647c6ae99SBarry Smith .   ctx - the user context
155747c6ae99SBarry Smith 
155847c6ae99SBarry Smith     Level: intermediate
155947c6ae99SBarry Smith 
1560e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
156147c6ae99SBarry Smith 
156247c6ae99SBarry Smith @*/
15631b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
156447c6ae99SBarry Smith {
156547c6ae99SBarry Smith   PetscFunctionBegin;
1566171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15671b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
156847c6ae99SBarry Smith   PetscFunctionReturn(0);
156947c6ae99SBarry Smith }
157047c6ae99SBarry Smith 
157147c6ae99SBarry Smith #undef __FUNCT__
157247c6ae99SBarry Smith #define __FUNCT__ "DMSetInitialGuess"
15737e833e3aSBarry Smith /*@C
157447c6ae99SBarry Smith     DMSetInitialGuess - sets a function to compute an initial guess vector entries for the solvers
157547c6ae99SBarry Smith 
157647c6ae99SBarry Smith     Logically Collective on DM
157747c6ae99SBarry Smith 
157847c6ae99SBarry Smith     Input Parameter:
157947c6ae99SBarry Smith +   dm - the DM object to destroy
158047c6ae99SBarry Smith -   f - the function to compute the initial guess
158147c6ae99SBarry Smith 
158247c6ae99SBarry Smith     Level: intermediate
158347c6ae99SBarry Smith 
1584e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
158547c6ae99SBarry Smith 
1586f07f9ceaSJed Brown @*/
15877087cfbeSBarry Smith PetscErrorCode  DMSetInitialGuess(DM dm,PetscErrorCode (*f)(DM,Vec))
158847c6ae99SBarry Smith {
158947c6ae99SBarry Smith   PetscFunctionBegin;
1590171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
159147c6ae99SBarry Smith   dm->ops->initialguess = f;
159247c6ae99SBarry Smith   PetscFunctionReturn(0);
159347c6ae99SBarry Smith }
159447c6ae99SBarry Smith 
159547c6ae99SBarry Smith #undef __FUNCT__
159647c6ae99SBarry Smith #define __FUNCT__ "DMSetFunction"
15977e833e3aSBarry Smith /*@C
159847c6ae99SBarry Smith     DMSetFunction - sets a function to compute the right hand side vector entries for the KSP solver or nonlinear function for SNES
159947c6ae99SBarry Smith 
160047c6ae99SBarry Smith     Logically Collective on DM
160147c6ae99SBarry Smith 
160247c6ae99SBarry Smith     Input Parameter:
160347c6ae99SBarry Smith +   dm - the DM object
160447c6ae99SBarry Smith -   f - the function to compute (use PETSC_NULL to cancel a previous function that was set)
160547c6ae99SBarry Smith 
160647c6ae99SBarry Smith     Level: intermediate
160747c6ae99SBarry Smith 
160847c6ae99SBarry Smith     Notes: This sets both the function for function evaluations and the function used to compute Jacobians via finite differences if no Jacobian
160947c6ae99SBarry Smith            computer is provided with DMSetJacobian(). Canceling cancels the function, but not the function used to compute the Jacobian.
161047c6ae99SBarry Smith 
1611e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
161247c6ae99SBarry Smith          DMSetJacobian()
161347c6ae99SBarry Smith 
1614f07f9ceaSJed Brown @*/
16157087cfbeSBarry Smith PetscErrorCode  DMSetFunction(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
161647c6ae99SBarry Smith {
161747c6ae99SBarry Smith   PetscFunctionBegin;
1618171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
161947c6ae99SBarry Smith   dm->ops->function = f;
162047c6ae99SBarry Smith   if (f) {
162147c6ae99SBarry Smith     dm->ops->functionj = f;
162247c6ae99SBarry Smith   }
162347c6ae99SBarry Smith   PetscFunctionReturn(0);
162447c6ae99SBarry Smith }
162547c6ae99SBarry Smith 
162647c6ae99SBarry Smith #undef __FUNCT__
162747c6ae99SBarry Smith #define __FUNCT__ "DMSetJacobian"
16287e833e3aSBarry Smith /*@C
162947c6ae99SBarry Smith     DMSetJacobian - sets a function to compute the matrix entries for the KSP solver or Jacobian for SNES
163047c6ae99SBarry Smith 
163147c6ae99SBarry Smith     Logically Collective on DM
163247c6ae99SBarry Smith 
163347c6ae99SBarry Smith     Input Parameter:
163447c6ae99SBarry Smith +   dm - the DM object to destroy
163547c6ae99SBarry Smith -   f - the function to compute the matrix entries
163647c6ae99SBarry Smith 
163747c6ae99SBarry Smith     Level: intermediate
163847c6ae99SBarry Smith 
1639e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
164047c6ae99SBarry Smith          DMSetFunction()
164147c6ae99SBarry Smith 
1642f07f9ceaSJed Brown @*/
16437087cfbeSBarry Smith PetscErrorCode  DMSetJacobian(DM dm,PetscErrorCode (*f)(DM,Vec,Mat,Mat,MatStructure*))
164447c6ae99SBarry Smith {
164547c6ae99SBarry Smith   PetscFunctionBegin;
1646171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
164747c6ae99SBarry Smith   dm->ops->jacobian = f;
164847c6ae99SBarry Smith   PetscFunctionReturn(0);
164947c6ae99SBarry Smith }
165047c6ae99SBarry Smith 
165147c6ae99SBarry Smith #undef __FUNCT__
165208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
165308da532bSDmitry Karpeev /*@C
165408da532bSDmitry Karpeev     DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI.
165508da532bSDmitry Karpeev 
165608da532bSDmitry Karpeev     Logically Collective on DM
165708da532bSDmitry Karpeev 
165808da532bSDmitry Karpeev     Input Parameter:
165908da532bSDmitry Karpeev +   dm - the DM object
166008da532bSDmitry Karpeev -   f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set)
166108da532bSDmitry Karpeev 
166208da532bSDmitry Karpeev     Level: intermediate
166308da532bSDmitry Karpeev 
1664e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
166508da532bSDmitry Karpeev          DMSetJacobian()
166608da532bSDmitry Karpeev 
166708da532bSDmitry Karpeev @*/
166808da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
166908da532bSDmitry Karpeev {
167008da532bSDmitry Karpeev   PetscFunctionBegin;
167108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
167208da532bSDmitry Karpeev   PetscFunctionReturn(0);
167308da532bSDmitry Karpeev }
167408da532bSDmitry Karpeev 
167508da532bSDmitry Karpeev #undef __FUNCT__
167608da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
167708da532bSDmitry Karpeev /*@
167808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
167908da532bSDmitry Karpeev 
168008da532bSDmitry Karpeev     Not Collective
168108da532bSDmitry Karpeev 
168208da532bSDmitry Karpeev     Input Parameter:
168308da532bSDmitry Karpeev .   dm - the DM object to destroy
168408da532bSDmitry Karpeev 
168508da532bSDmitry Karpeev     Output Parameter:
168608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
168708da532bSDmitry Karpeev 
168808da532bSDmitry Karpeev     Level: developer
168908da532bSDmitry Karpeev 
1690e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
169108da532bSDmitry Karpeev 
169208da532bSDmitry Karpeev @*/
169308da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
169408da532bSDmitry Karpeev {
169508da532bSDmitry Karpeev   PetscFunctionBegin;
169608da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
169708da532bSDmitry Karpeev   PetscFunctionReturn(0);
169808da532bSDmitry Karpeev }
169908da532bSDmitry Karpeev 
170008da532bSDmitry Karpeev #undef __FUNCT__
170108da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
170208da532bSDmitry Karpeev /*@C
170308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
170408da532bSDmitry Karpeev 
170508da532bSDmitry Karpeev     Logically Collective on DM
170608da532bSDmitry Karpeev 
170708da532bSDmitry Karpeev     Input Parameters:
170808da532bSDmitry Karpeev +   dm - the DM object to destroy
170908da532bSDmitry Karpeev -   x  - current solution at which the bounds are computed
171008da532bSDmitry Karpeev 
171108da532bSDmitry Karpeev     Output parameters:
171208da532bSDmitry Karpeev +   xl - lower bound
171308da532bSDmitry Karpeev -   xu - upper bound
171408da532bSDmitry Karpeev 
171508da532bSDmitry Karpeev     Level: intermediate
171608da532bSDmitry Karpeev 
1717e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
171808da532bSDmitry Karpeev          DMSetFunction(), DMSetVariableBounds()
171908da532bSDmitry Karpeev 
172008da532bSDmitry Karpeev @*/
172108da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
172208da532bSDmitry Karpeev {
172308da532bSDmitry Karpeev   PetscErrorCode ierr;
172408da532bSDmitry Karpeev   PetscFunctionBegin;
172508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
172608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
172708da532bSDmitry Karpeev   if(dm->ops->computevariablebounds) {
172808da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu); CHKERRQ(ierr);
172908da532bSDmitry Karpeev   }
173008da532bSDmitry Karpeev   else {
173108da532bSDmitry Karpeev     ierr = VecSet(xl,SNES_VI_NINF); CHKERRQ(ierr);
173208da532bSDmitry Karpeev     ierr = VecSet(xu,SNES_VI_INF);  CHKERRQ(ierr);
173308da532bSDmitry Karpeev   }
173408da532bSDmitry Karpeev   PetscFunctionReturn(0);
173508da532bSDmitry Karpeev }
173608da532bSDmitry Karpeev 
173708da532bSDmitry Karpeev #undef __FUNCT__
173847c6ae99SBarry Smith #define __FUNCT__ "DMComputeInitialGuess"
173947c6ae99SBarry Smith /*@
174047c6ae99SBarry Smith     DMComputeInitialGuess - computes an initial guess vector entries for the KSP solvers
174147c6ae99SBarry Smith 
174247c6ae99SBarry Smith     Collective on DM
174347c6ae99SBarry Smith 
174447c6ae99SBarry Smith     Input Parameter:
174547c6ae99SBarry Smith +   dm - the DM object to destroy
174647c6ae99SBarry Smith -   x - the vector to hold the initial guess values
174747c6ae99SBarry Smith 
174847c6ae99SBarry Smith     Level: developer
174947c6ae99SBarry Smith 
1750e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetRhs(), DMSetMat()
175147c6ae99SBarry Smith 
175247c6ae99SBarry Smith @*/
17537087cfbeSBarry Smith PetscErrorCode  DMComputeInitialGuess(DM dm,Vec x)
175447c6ae99SBarry Smith {
175547c6ae99SBarry Smith   PetscErrorCode ierr;
175647c6ae99SBarry Smith   PetscFunctionBegin;
1757171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
175847c6ae99SBarry Smith   if (!dm->ops->initialguess) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetInitialGuess()");
175947c6ae99SBarry Smith   ierr = (*dm->ops->initialguess)(dm,x);CHKERRQ(ierr);
176047c6ae99SBarry Smith   PetscFunctionReturn(0);
176147c6ae99SBarry Smith }
176247c6ae99SBarry Smith 
176347c6ae99SBarry Smith #undef __FUNCT__
176447c6ae99SBarry Smith #define __FUNCT__ "DMHasInitialGuess"
176547c6ae99SBarry Smith /*@
176647c6ae99SBarry Smith     DMHasInitialGuess - does the DM object have an initial guess function
176747c6ae99SBarry Smith 
176847c6ae99SBarry Smith     Not Collective
176947c6ae99SBarry Smith 
177047c6ae99SBarry Smith     Input Parameter:
177147c6ae99SBarry Smith .   dm - the DM object to destroy
177247c6ae99SBarry Smith 
177347c6ae99SBarry Smith     Output Parameter:
177447c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
177547c6ae99SBarry Smith 
177647c6ae99SBarry Smith     Level: developer
177747c6ae99SBarry Smith 
1778e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
177947c6ae99SBarry Smith 
178047c6ae99SBarry Smith @*/
17817087cfbeSBarry Smith PetscErrorCode  DMHasInitialGuess(DM dm,PetscBool  *flg)
178247c6ae99SBarry Smith {
178347c6ae99SBarry Smith   PetscFunctionBegin;
1784171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
178547c6ae99SBarry Smith   *flg =  (dm->ops->initialguess) ? PETSC_TRUE : PETSC_FALSE;
178647c6ae99SBarry Smith   PetscFunctionReturn(0);
178747c6ae99SBarry Smith }
178847c6ae99SBarry Smith 
178947c6ae99SBarry Smith #undef __FUNCT__
179047c6ae99SBarry Smith #define __FUNCT__ "DMHasFunction"
179147c6ae99SBarry Smith /*@
179247c6ae99SBarry Smith     DMHasFunction - does the DM object have a function
179347c6ae99SBarry Smith 
179447c6ae99SBarry Smith     Not Collective
179547c6ae99SBarry Smith 
179647c6ae99SBarry Smith     Input Parameter:
179747c6ae99SBarry Smith .   dm - the DM object to destroy
179847c6ae99SBarry Smith 
179947c6ae99SBarry Smith     Output Parameter:
180047c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
180147c6ae99SBarry Smith 
180247c6ae99SBarry Smith     Level: developer
180347c6ae99SBarry Smith 
1804e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
180547c6ae99SBarry Smith 
180647c6ae99SBarry Smith @*/
18077087cfbeSBarry Smith PetscErrorCode  DMHasFunction(DM dm,PetscBool  *flg)
180847c6ae99SBarry Smith {
180947c6ae99SBarry Smith   PetscFunctionBegin;
1810171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
181147c6ae99SBarry Smith   *flg =  (dm->ops->function) ? PETSC_TRUE : PETSC_FALSE;
181247c6ae99SBarry Smith   PetscFunctionReturn(0);
181347c6ae99SBarry Smith }
181447c6ae99SBarry Smith 
181547c6ae99SBarry Smith #undef __FUNCT__
181647c6ae99SBarry Smith #define __FUNCT__ "DMHasJacobian"
181747c6ae99SBarry Smith /*@
181847c6ae99SBarry Smith     DMHasJacobian - does the DM object have a matrix function
181947c6ae99SBarry Smith 
182047c6ae99SBarry Smith     Not Collective
182147c6ae99SBarry Smith 
182247c6ae99SBarry Smith     Input Parameter:
182347c6ae99SBarry Smith .   dm - the DM object to destroy
182447c6ae99SBarry Smith 
182547c6ae99SBarry Smith     Output Parameter:
182647c6ae99SBarry Smith .   flg - PETSC_TRUE if function exists
182747c6ae99SBarry Smith 
182847c6ae99SBarry Smith     Level: developer
182947c6ae99SBarry Smith 
1830e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetFunction(), DMSetJacobian()
183147c6ae99SBarry Smith 
183247c6ae99SBarry Smith @*/
18337087cfbeSBarry Smith PetscErrorCode  DMHasJacobian(DM dm,PetscBool  *flg)
183447c6ae99SBarry Smith {
183547c6ae99SBarry Smith   PetscFunctionBegin;
1836171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
183747c6ae99SBarry Smith   *flg =  (dm->ops->jacobian) ? PETSC_TRUE : PETSC_FALSE;
183847c6ae99SBarry Smith   PetscFunctionReturn(0);
183947c6ae99SBarry Smith }
184047c6ae99SBarry Smith 
184147c6ae99SBarry Smith #undef  __FUNCT__
184208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
1843748fac09SDmitry Karpeev /*@C
184408da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
184508da532bSDmitry Karpeev 
184608da532bSDmitry Karpeev     Collective on DM
184708da532bSDmitry Karpeev 
184808da532bSDmitry Karpeev     Input Parameter:
184908da532bSDmitry Karpeev +   dm - the DM object
1850e88d7f4bSDmitry Karpeev -   x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems.
185108da532bSDmitry Karpeev 
185208da532bSDmitry Karpeev     Level: developer
185308da532bSDmitry Karpeev 
1854e88d7f4bSDmitry Karpeev .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
185508da532bSDmitry Karpeev          DMSetFunction(), DMSetJacobian(), DMSetVariableBounds()
185608da532bSDmitry Karpeev 
185708da532bSDmitry Karpeev @*/
185808da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
185908da532bSDmitry Karpeev {
186008da532bSDmitry Karpeev   PetscErrorCode ierr;
186108da532bSDmitry Karpeev   PetscFunctionBegin;
186208da532bSDmitry Karpeev   if (x) {
186308da532bSDmitry Karpeev     if (!dm->x) {
186408da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
186508da532bSDmitry Karpeev     }
186608da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
186708da532bSDmitry Karpeev   }
186808da532bSDmitry Karpeev   else if(dm->x) {
186908da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);  CHKERRQ(ierr);
187008da532bSDmitry Karpeev   }
187108da532bSDmitry Karpeev   PetscFunctionReturn(0);
187208da532bSDmitry Karpeev }
187308da532bSDmitry Karpeev 
187408da532bSDmitry Karpeev 
187508da532bSDmitry Karpeev #undef __FUNCT__
187647c6ae99SBarry Smith #define __FUNCT__ "DMComputeFunction"
187747c6ae99SBarry Smith /*@
187847c6ae99SBarry Smith     DMComputeFunction - computes the right hand side vector entries for the KSP solver or nonlinear function for SNES
187947c6ae99SBarry Smith 
188047c6ae99SBarry Smith     Collective on DM
188147c6ae99SBarry Smith 
188247c6ae99SBarry Smith     Input Parameter:
188347c6ae99SBarry Smith +   dm - the DM object to destroy
188447c6ae99SBarry Smith .   x - the location where the function is evaluationed, may be ignored for linear problems
188547c6ae99SBarry Smith -   b - the vector to hold the right hand side entries
188647c6ae99SBarry Smith 
188747c6ae99SBarry Smith     Level: developer
188847c6ae99SBarry Smith 
1889e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
189047c6ae99SBarry Smith          DMSetJacobian()
189147c6ae99SBarry Smith 
189247c6ae99SBarry Smith @*/
18937087cfbeSBarry Smith PetscErrorCode  DMComputeFunction(DM dm,Vec x,Vec b)
189447c6ae99SBarry Smith {
189547c6ae99SBarry Smith   PetscErrorCode ierr;
189647c6ae99SBarry Smith   PetscFunctionBegin;
1897171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
189847c6ae99SBarry Smith   if (!dm->ops->function) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"Need to provide function with DMSetFunction()");
1899644e2e5bSBarry Smith   PetscStackPush("DM user function");
190047c6ae99SBarry Smith   ierr = (*dm->ops->function)(dm,x,b);CHKERRQ(ierr);
1901644e2e5bSBarry Smith   PetscStackPop;
190247c6ae99SBarry Smith   PetscFunctionReturn(0);
190347c6ae99SBarry Smith }
190447c6ae99SBarry Smith 
190547c6ae99SBarry Smith 
190608da532bSDmitry Karpeev 
190747c6ae99SBarry Smith #undef __FUNCT__
190847c6ae99SBarry Smith #define __FUNCT__ "DMComputeJacobian"
190947c6ae99SBarry Smith /*@
191047c6ae99SBarry Smith     DMComputeJacobian - compute the matrix entries for the solver
191147c6ae99SBarry Smith 
191247c6ae99SBarry Smith     Collective on DM
191347c6ae99SBarry Smith 
191447c6ae99SBarry Smith     Input Parameter:
191547c6ae99SBarry Smith +   dm - the DM object
1916cab2e9ccSBarry Smith .   x - location to compute Jacobian at; will be PETSC_NULL for linear problems, for nonlinear problems if not provided then pulled from DM
191747c6ae99SBarry Smith .   A - matrix that defines the operator for the linear solve
191847c6ae99SBarry Smith -   B - the matrix used to construct the preconditioner
191947c6ae99SBarry Smith 
192047c6ae99SBarry Smith     Level: developer
192147c6ae99SBarry Smith 
1922e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), DMSetInitialGuess(),
192347c6ae99SBarry Smith          DMSetFunction()
192447c6ae99SBarry Smith 
192547c6ae99SBarry Smith @*/
19267087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian(DM dm,Vec x,Mat A,Mat B,MatStructure *stflag)
192747c6ae99SBarry Smith {
192847c6ae99SBarry Smith   PetscErrorCode ierr;
192947c6ae99SBarry Smith 
193047c6ae99SBarry Smith   PetscFunctionBegin;
1931171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
193247c6ae99SBarry Smith   if (!dm->ops->jacobian) {
193347c6ae99SBarry Smith     ISColoring     coloring;
193447c6ae99SBarry Smith     MatFDColoring  fd;
19352c9966d7SBarry Smith     const MatType  mtype;
193647c6ae99SBarry Smith 
19372c9966d7SBarry Smith     ierr = PetscObjectGetType((PetscObject)B,&mtype);CHKERRQ(ierr);
19382c9966d7SBarry Smith     ierr = DMCreateColoring(dm,dm->coloringtype,mtype,&coloring);CHKERRQ(ierr);
193947c6ae99SBarry Smith     ierr = MatFDColoringCreate(B,coloring,&fd);CHKERRQ(ierr);
1940fcfd50ebSBarry Smith     ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr);
194147c6ae99SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))dm->ops->functionj,dm);CHKERRQ(ierr);
19420bdded8aSJed Brown     ierr = PetscObjectSetOptionsPrefix((PetscObject)fd,((PetscObject)dm)->prefix);CHKERRQ(ierr);
19430bdded8aSJed Brown     ierr = MatFDColoringSetFromOptions(fd);CHKERRQ(ierr);
194471cd77b2SBarry Smith 
194547c6ae99SBarry Smith     dm->fd = fd;
194647c6ae99SBarry Smith     dm->ops->jacobian = DMComputeJacobianDefault;
19472533e041SBarry Smith 
194871cd77b2SBarry Smith     /* don't know why this is needed */
194971cd77b2SBarry Smith     ierr = PetscObjectDereference((PetscObject)dm);CHKERRQ(ierr);
195047c6ae99SBarry Smith   }
195147c6ae99SBarry Smith   if (!x) x = dm->x;
195247c6ae99SBarry Smith   ierr = (*dm->ops->jacobian)(dm,x,A,B,stflag);CHKERRQ(ierr);
1953cab2e9ccSBarry Smith 
195471cd77b2SBarry Smith   /* if matrix depends on x; i.e. nonlinear problem, keep copy of input vector since needed by multigrid methods to generate coarse grid matrices */
1955649052a6SBarry Smith   if (x) {
1956cab2e9ccSBarry Smith     if (!dm->x) {
195771cd77b2SBarry Smith       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
1958cab2e9ccSBarry Smith     }
1959cab2e9ccSBarry Smith     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
1960649052a6SBarry Smith   }
1961a8248277SBarry Smith   if (A != B) {
1962a8248277SBarry Smith     ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1963a8248277SBarry Smith     ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1964a8248277SBarry Smith   }
196547c6ae99SBarry Smith   PetscFunctionReturn(0);
196647c6ae99SBarry Smith }
1967264ace61SBarry Smith 
1968cab2e9ccSBarry Smith 
1969264ace61SBarry Smith PetscFList DMList                       = PETSC_NULL;
1970264ace61SBarry Smith PetscBool  DMRegisterAllCalled          = PETSC_FALSE;
1971264ace61SBarry Smith 
1972264ace61SBarry Smith #undef __FUNCT__
1973264ace61SBarry Smith #define __FUNCT__ "DMSetType"
1974264ace61SBarry Smith /*@C
1975264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
1976264ace61SBarry Smith 
1977264ace61SBarry Smith   Collective on DM
1978264ace61SBarry Smith 
1979264ace61SBarry Smith   Input Parameters:
1980264ace61SBarry Smith + dm     - The DM object
1981264ace61SBarry Smith - method - The name of the DM type
1982264ace61SBarry Smith 
1983264ace61SBarry Smith   Options Database Key:
1984264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
1985264ace61SBarry Smith 
1986264ace61SBarry Smith   Notes:
1987e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
1988264ace61SBarry Smith 
1989264ace61SBarry Smith   Level: intermediate
1990264ace61SBarry Smith 
1991264ace61SBarry Smith .keywords: DM, set, type
1992264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
1993264ace61SBarry Smith @*/
19947087cfbeSBarry Smith PetscErrorCode  DMSetType(DM dm, const DMType method)
1995264ace61SBarry Smith {
1996264ace61SBarry Smith   PetscErrorCode (*r)(DM);
1997264ace61SBarry Smith   PetscBool      match;
1998264ace61SBarry Smith   PetscErrorCode ierr;
1999264ace61SBarry Smith 
2000264ace61SBarry Smith   PetscFunctionBegin;
2001264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2002251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2003264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2004264ace61SBarry Smith 
2005264ace61SBarry Smith   if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
20064b91b6eaSBarry Smith   ierr = PetscFListFind(DMList, ((PetscObject)dm)->comm, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2007264ace61SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2008264ace61SBarry Smith 
2009264ace61SBarry Smith   if (dm->ops->destroy) {
2010264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
2011b5c23020SJed Brown     dm->ops->destroy = PETSC_NULL;
2012264ace61SBarry Smith   }
2013264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2014264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2015264ace61SBarry Smith   PetscFunctionReturn(0);
2016264ace61SBarry Smith }
2017264ace61SBarry Smith 
2018264ace61SBarry Smith #undef __FUNCT__
2019264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2020264ace61SBarry Smith /*@C
2021264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2022264ace61SBarry Smith 
2023264ace61SBarry Smith   Not Collective
2024264ace61SBarry Smith 
2025264ace61SBarry Smith   Input Parameter:
2026264ace61SBarry Smith . dm  - The DM
2027264ace61SBarry Smith 
2028264ace61SBarry Smith   Output Parameter:
2029264ace61SBarry Smith . type - The DM type name
2030264ace61SBarry Smith 
2031264ace61SBarry Smith   Level: intermediate
2032264ace61SBarry Smith 
2033264ace61SBarry Smith .keywords: DM, get, type, name
2034264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2035264ace61SBarry Smith @*/
20367087cfbeSBarry Smith PetscErrorCode  DMGetType(DM dm, const DMType *type)
2037264ace61SBarry Smith {
2038264ace61SBarry Smith   PetscErrorCode ierr;
2039264ace61SBarry Smith 
2040264ace61SBarry Smith   PetscFunctionBegin;
2041264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2042264ace61SBarry Smith   PetscValidCharPointer(type,2);
2043264ace61SBarry Smith   if (!DMRegisterAllCalled) {
2044264ace61SBarry Smith     ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2045264ace61SBarry Smith   }
2046264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2047264ace61SBarry Smith   PetscFunctionReturn(0);
2048264ace61SBarry Smith }
2049264ace61SBarry Smith 
205067a56275SMatthew G Knepley #undef __FUNCT__
205167a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
205267a56275SMatthew G Knepley /*@C
205367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
205467a56275SMatthew G Knepley 
205567a56275SMatthew G Knepley   Collective on DM
205667a56275SMatthew G Knepley 
205767a56275SMatthew G Knepley   Input Parameters:
205867a56275SMatthew G Knepley + dm - the DM
205967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
206067a56275SMatthew G Knepley 
206167a56275SMatthew G Knepley   Output Parameter:
206267a56275SMatthew G Knepley . M - pointer to new DM
206367a56275SMatthew G Knepley 
206467a56275SMatthew G Knepley   Notes:
206567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
206667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
206767a56275SMatthew G Knepley   of the input DM.
206867a56275SMatthew G Knepley 
206967a56275SMatthew G Knepley   Level: intermediate
207067a56275SMatthew G Knepley 
207167a56275SMatthew G Knepley .seealso: DMCreate()
207267a56275SMatthew G Knepley @*/
207367a56275SMatthew G Knepley PetscErrorCode DMConvert(DM dm, const DMType newtype, DM *M)
207467a56275SMatthew G Knepley {
207567a56275SMatthew G Knepley   DM             B;
207667a56275SMatthew G Knepley   char           convname[256];
207767a56275SMatthew G Knepley   PetscBool      sametype, issame;
207867a56275SMatthew G Knepley   PetscErrorCode ierr;
207967a56275SMatthew G Knepley 
208067a56275SMatthew G Knepley   PetscFunctionBegin;
208167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
208267a56275SMatthew G Knepley   PetscValidType(dm,1);
208367a56275SMatthew G Knepley   PetscValidPointer(M,3);
2084251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
208567a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
208667a56275SMatthew G Knepley   {
208767a56275SMatthew G Knepley     PetscErrorCode (*conv)(DM, const DMType, DM *) = PETSC_NULL;
208867a56275SMatthew G Knepley 
208967a56275SMatthew G Knepley     /*
209067a56275SMatthew G Knepley        Order of precedence:
209167a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
209267a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
209367a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
209467a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
209567a56275SMatthew G Knepley        5) Use a really basic converter.
209667a56275SMatthew G Knepley     */
209767a56275SMatthew G Knepley 
209867a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
209967a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
210067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
210167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
210267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
210367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
210467a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr);
210567a56275SMatthew G Knepley     if (conv) goto foundconv;
210667a56275SMatthew G Knepley 
210767a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
210867a56275SMatthew G Knepley     ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr);
210967a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
211067a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
211167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
211267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
211367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
211467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
211567a56275SMatthew G Knepley     ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr);
211667a56275SMatthew G Knepley     if (conv) {
2117fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
211867a56275SMatthew G Knepley       goto foundconv;
211967a56275SMatthew G Knepley     }
212067a56275SMatthew G Knepley 
212167a56275SMatthew G Knepley #if 0
212267a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
212367a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2124fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
212567a56275SMatthew G Knepley     if (conv) goto foundconv;
212667a56275SMatthew G Knepley 
212767a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
212867a56275SMatthew G Knepley     if (dm->ops->convert) {
212967a56275SMatthew G Knepley       conv = dm->ops->convert;
213067a56275SMatthew G Knepley     }
213167a56275SMatthew G Knepley     if (conv) goto foundconv;
213267a56275SMatthew G Knepley #endif
213367a56275SMatthew G Knepley 
213467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
213567a56275SMatthew G Knepley     SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
213667a56275SMatthew G Knepley 
213767a56275SMatthew G Knepley     foundconv:
213867a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
213967a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
214067a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
214167a56275SMatthew G Knepley   }
214267a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
214367a56275SMatthew G Knepley   PetscFunctionReturn(0);
214467a56275SMatthew G Knepley }
2145264ace61SBarry Smith 
2146264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2147264ace61SBarry Smith 
2148264ace61SBarry Smith #undef __FUNCT__
2149264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2150264ace61SBarry Smith /*@C
2151264ace61SBarry Smith   DMRegister - See DMRegisterDynamic()
2152264ace61SBarry Smith 
2153264ace61SBarry Smith   Level: advanced
2154264ace61SBarry Smith @*/
21557087cfbeSBarry Smith PetscErrorCode  DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM))
2156264ace61SBarry Smith {
2157264ace61SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2158264ace61SBarry Smith   PetscErrorCode ierr;
2159264ace61SBarry Smith 
2160264ace61SBarry Smith   PetscFunctionBegin;
2161264ace61SBarry Smith   ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr);
2162264ace61SBarry Smith   ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr);
2163264ace61SBarry Smith   ierr = PetscStrcat(fullname, name);CHKERRQ(ierr);
2164264ace61SBarry Smith   ierr = PetscFListAdd(&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr);
2165264ace61SBarry Smith   PetscFunctionReturn(0);
2166264ace61SBarry Smith }
2167264ace61SBarry Smith 
2168264ace61SBarry Smith 
2169264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2170264ace61SBarry Smith #undef __FUNCT__
2171264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy"
2172264ace61SBarry Smith /*@C
2173264ace61SBarry Smith    DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic().
2174264ace61SBarry Smith 
2175264ace61SBarry Smith    Not Collective
2176264ace61SBarry Smith 
2177264ace61SBarry Smith    Level: advanced
2178264ace61SBarry Smith 
2179264ace61SBarry Smith .keywords: DM, register, destroy
2180264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic()
2181264ace61SBarry Smith @*/
21827087cfbeSBarry Smith PetscErrorCode  DMRegisterDestroy(void)
2183264ace61SBarry Smith {
2184264ace61SBarry Smith   PetscErrorCode ierr;
2185264ace61SBarry Smith 
2186264ace61SBarry Smith   PetscFunctionBegin;
2187264ace61SBarry Smith   ierr = PetscFListDestroy(&DMList);CHKERRQ(ierr);
2188264ace61SBarry Smith   DMRegisterAllCalled = PETSC_FALSE;
2189264ace61SBarry Smith   PetscFunctionReturn(0);
2190264ace61SBarry Smith }
219123f975d1SBarry Smith 
219223f975d1SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
2193c6db04a5SJed Brown #include <mex.h>
219423f975d1SBarry Smith 
21953014e516SBarry Smith typedef struct {char *funcname; char *jacname; mxArray *ctx;} DMMatlabContext;
219623f975d1SBarry Smith 
219723f975d1SBarry Smith #undef __FUNCT__
219823f975d1SBarry Smith #define __FUNCT__ "DMComputeFunction_Matlab"
219923f975d1SBarry Smith /*
220023f975d1SBarry Smith    DMComputeFunction_Matlab - Calls the function that has been set with
220123f975d1SBarry Smith                          DMSetFunctionMatlab().
220223f975d1SBarry Smith 
220323f975d1SBarry Smith    For linear problems x is null
220423f975d1SBarry Smith 
220523f975d1SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
220623f975d1SBarry Smith */
22077087cfbeSBarry Smith PetscErrorCode  DMComputeFunction_Matlab(DM dm,Vec x,Vec y)
220823f975d1SBarry Smith {
220923f975d1SBarry Smith   PetscErrorCode    ierr;
221023f975d1SBarry Smith   DMMatlabContext   *sctx;
221123f975d1SBarry Smith   int               nlhs = 1,nrhs = 4;
221223f975d1SBarry Smith   mxArray	    *plhs[1],*prhs[4];
221323f975d1SBarry Smith   long long int     lx = 0,ly = 0,ls = 0;
221423f975d1SBarry Smith 
221523f975d1SBarry Smith   PetscFunctionBegin;
221623f975d1SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
221723f975d1SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
221823f975d1SBarry Smith   PetscCheckSameComm(dm,1,y,3);
221923f975d1SBarry Smith 
222023f975d1SBarry Smith   /* call Matlab function in ctx with arguments x and y */
22211b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
222223f975d1SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
222323f975d1SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
22243014e516SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(y));CHKERRQ(ierr);
222523f975d1SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
222623f975d1SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
222723f975d1SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)ly);
222823f975d1SBarry Smith   prhs[3] =  mxCreateString(sctx->funcname);
2229b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeFunctionInternal");CHKERRQ(ierr);
223023f975d1SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
223123f975d1SBarry Smith   mxDestroyArray(prhs[0]);
223223f975d1SBarry Smith   mxDestroyArray(prhs[1]);
223323f975d1SBarry Smith   mxDestroyArray(prhs[2]);
223423f975d1SBarry Smith   mxDestroyArray(prhs[3]);
223523f975d1SBarry Smith   mxDestroyArray(plhs[0]);
223623f975d1SBarry Smith   PetscFunctionReturn(0);
223723f975d1SBarry Smith }
223823f975d1SBarry Smith 
223923f975d1SBarry Smith 
224023f975d1SBarry Smith #undef __FUNCT__
224123f975d1SBarry Smith #define __FUNCT__ "DMSetFunctionMatlab"
224223f975d1SBarry Smith /*
224323f975d1SBarry Smith    DMSetFunctionMatlab - Sets the function evaluation routine
224423f975d1SBarry Smith 
224523f975d1SBarry Smith */
22467087cfbeSBarry Smith PetscErrorCode  DMSetFunctionMatlab(DM dm,const char *func)
224723f975d1SBarry Smith {
224823f975d1SBarry Smith   PetscErrorCode    ierr;
224923f975d1SBarry Smith   DMMatlabContext   *sctx;
225023f975d1SBarry Smith 
225123f975d1SBarry Smith   PetscFunctionBegin;
2252171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
225323f975d1SBarry Smith   /* currently sctx is memory bleed */
22541b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
22553014e516SBarry Smith   if (!sctx) {
225623f975d1SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
22573014e516SBarry Smith   }
225823f975d1SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
22591b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
226023f975d1SBarry Smith   ierr = DMSetFunction(dm,DMComputeFunction_Matlab);CHKERRQ(ierr);
226123f975d1SBarry Smith   PetscFunctionReturn(0);
226223f975d1SBarry Smith }
22633014e516SBarry Smith 
22643014e516SBarry Smith #undef __FUNCT__
22653014e516SBarry Smith #define __FUNCT__ "DMComputeJacobian_Matlab"
22663014e516SBarry Smith /*
22673014e516SBarry Smith    DMComputeJacobian_Matlab - Calls the function that has been set with
22683014e516SBarry Smith                          DMSetJacobianMatlab().
22693014e516SBarry Smith 
22703014e516SBarry Smith    For linear problems x is null
22713014e516SBarry Smith 
22723014e516SBarry Smith .seealso: DMSetFunction(), DMGetFunction()
22733014e516SBarry Smith */
22747087cfbeSBarry Smith PetscErrorCode  DMComputeJacobian_Matlab(DM dm,Vec x,Mat A,Mat B,MatStructure *str)
22753014e516SBarry Smith {
22763014e516SBarry Smith   PetscErrorCode    ierr;
22773014e516SBarry Smith   DMMatlabContext   *sctx;
22783014e516SBarry Smith   int               nlhs = 2,nrhs = 5;
22793014e516SBarry Smith   mxArray	    *plhs[2],*prhs[5];
22803014e516SBarry Smith   long long int     lx = 0,lA = 0,lB = 0,ls = 0;
22813014e516SBarry Smith 
22823014e516SBarry Smith   PetscFunctionBegin;
22833014e516SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22843014e516SBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
22853014e516SBarry Smith 
2286e3c5b3baSBarry Smith   /* call MATLAB function in ctx with arguments x, A, and B */
22871b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
22883014e516SBarry Smith   ierr = PetscMemcpy(&ls,&dm,sizeof(dm));CHKERRQ(ierr);
22893014e516SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
22903014e516SBarry Smith   ierr = PetscMemcpy(&lA,&A,sizeof(A));CHKERRQ(ierr);
22913014e516SBarry Smith   ierr = PetscMemcpy(&lB,&B,sizeof(B));CHKERRQ(ierr);
22923014e516SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
22933014e516SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)lx);
22943014e516SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lA);
22953014e516SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lB);
22963014e516SBarry Smith   prhs[4] =  mxCreateString(sctx->jacname);
2297b807a863SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscDMComputeJacobianInternal");CHKERRQ(ierr);
2298c980e822SBarry Smith   *str    =  (MatStructure) mxGetScalar(plhs[0]);
2299c088a8dcSBarry Smith   ierr    =  (PetscInt) mxGetScalar(plhs[1]);CHKERRQ(ierr);
23003014e516SBarry Smith   mxDestroyArray(prhs[0]);
23013014e516SBarry Smith   mxDestroyArray(prhs[1]);
23023014e516SBarry Smith   mxDestroyArray(prhs[2]);
23033014e516SBarry Smith   mxDestroyArray(prhs[3]);
23043014e516SBarry Smith   mxDestroyArray(prhs[4]);
23053014e516SBarry Smith   mxDestroyArray(plhs[0]);
23063014e516SBarry Smith   mxDestroyArray(plhs[1]);
23073014e516SBarry Smith   PetscFunctionReturn(0);
23083014e516SBarry Smith }
23093014e516SBarry Smith 
23103014e516SBarry Smith 
23113014e516SBarry Smith #undef __FUNCT__
23123014e516SBarry Smith #define __FUNCT__ "DMSetJacobianMatlab"
23133014e516SBarry Smith /*
23143014e516SBarry Smith    DMSetJacobianMatlab - Sets the Jacobian function evaluation routine
23153014e516SBarry Smith 
23163014e516SBarry Smith */
23177087cfbeSBarry Smith PetscErrorCode  DMSetJacobianMatlab(DM dm,const char *func)
23183014e516SBarry Smith {
23193014e516SBarry Smith   PetscErrorCode    ierr;
23203014e516SBarry Smith   DMMatlabContext   *sctx;
23213014e516SBarry Smith 
23223014e516SBarry Smith   PetscFunctionBegin;
2323171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23243014e516SBarry Smith   /* currently sctx is memory bleed */
23251b2093e4SBarry Smith   ierr = DMGetApplicationContext(dm,&sctx);CHKERRQ(ierr);
23263014e516SBarry Smith   if (!sctx) {
23273014e516SBarry Smith     ierr = PetscMalloc(sizeof(DMMatlabContext),&sctx);CHKERRQ(ierr);
23283014e516SBarry Smith   }
23293014e516SBarry Smith   ierr = PetscStrallocpy(func,&sctx->jacname);CHKERRQ(ierr);
23301b2093e4SBarry Smith   ierr = DMSetApplicationContext(dm,sctx);CHKERRQ(ierr);
23313014e516SBarry Smith   ierr = DMSetJacobian(dm,DMComputeJacobian_Matlab);CHKERRQ(ierr);
23323014e516SBarry Smith   PetscFunctionReturn(0);
23333014e516SBarry Smith }
233423f975d1SBarry Smith #endif
2335b859378eSBarry Smith 
2336b859378eSBarry Smith #undef __FUNCT__
2337b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2338b859378eSBarry Smith /*@C
2339b859378eSBarry Smith   DMLoad - Loads a DM that has been stored in binary or HDF5 format
2340b859378eSBarry Smith   with DMView().
2341b859378eSBarry Smith 
2342b859378eSBarry Smith   Collective on PetscViewer
2343b859378eSBarry Smith 
2344b859378eSBarry Smith   Input Parameters:
2345b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2346b859378eSBarry Smith            some related function before a call to DMLoad().
2347b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2348b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2349b859378eSBarry Smith 
2350b859378eSBarry Smith    Level: intermediate
2351b859378eSBarry Smith 
2352b859378eSBarry Smith   Notes:
2353b859378eSBarry Smith   Defaults to the DM DA.
2354b859378eSBarry Smith 
2355b859378eSBarry Smith   Notes for advanced users:
2356b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2357b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2358b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2359b859378eSBarry Smith   format is
2360b859378eSBarry Smith .vb
2361b859378eSBarry Smith      has not yet been determined
2362b859378eSBarry Smith .ve
2363b859378eSBarry Smith 
2364b859378eSBarry Smith    In addition, PETSc automatically does the byte swapping for
2365b859378eSBarry Smith machines that store the bytes reversed, e.g.  DEC alpha, freebsd,
2366b859378eSBarry Smith linux, Windows and the paragon; thus if you write your own binary
2367b859378eSBarry Smith read/write routines you have to swap the bytes; see PetscBinaryRead()
2368b859378eSBarry Smith and PetscBinaryWrite() to see how this may be done.
2369b859378eSBarry Smith 
2370b859378eSBarry Smith   Concepts: vector^loading from file
2371b859378eSBarry Smith 
2372b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2373b859378eSBarry Smith @*/
2374b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2375b859378eSBarry Smith {
2376b859378eSBarry Smith   PetscErrorCode ierr;
2377b859378eSBarry Smith 
2378b859378eSBarry Smith   PetscFunctionBegin;
2379b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2380b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2381b859378eSBarry Smith 
2382b859378eSBarry Smith   if (!((PetscObject)newdm)->type_name) {
2383b859378eSBarry Smith     ierr = DMSetType(newdm, DMDA);CHKERRQ(ierr);
2384b859378eSBarry Smith   }
2385b859378eSBarry Smith   ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);
2386b859378eSBarry Smith   PetscFunctionReturn(0);
2387b859378eSBarry Smith }
2388b859378eSBarry Smith 
23897da65231SMatthew G Knepley /******************************** FEM Support **********************************/
23907da65231SMatthew G Knepley 
23917da65231SMatthew G Knepley #undef __FUNCT__
23927da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
23937da65231SMatthew G Knepley PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) {
23941d47ebbbSSatish Balay   PetscInt       f;
23951b30c384SMatthew G Knepley   PetscErrorCode ierr;
23961b30c384SMatthew G Knepley 
23977da65231SMatthew G Knepley   PetscFunctionBegin;
239874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
23991d47ebbbSSatish Balay   for(f = 0; f < len; ++f) {
240074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr);
24017da65231SMatthew G Knepley   }
24027da65231SMatthew G Knepley   PetscFunctionReturn(0);
24037da65231SMatthew G Knepley }
24047da65231SMatthew G Knepley 
24057da65231SMatthew G Knepley #undef __FUNCT__
24067da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
24077da65231SMatthew G Knepley PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) {
24081b30c384SMatthew G Knepley   PetscInt       f, g;
24097da65231SMatthew G Knepley   PetscErrorCode ierr;
24107da65231SMatthew G Knepley 
24117da65231SMatthew G Knepley   PetscFunctionBegin;
241274778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
24131d47ebbbSSatish Balay   for(f = 0; f < rows; ++f) {
241474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
24151d47ebbbSSatish Balay     for(g = 0; g < cols; ++g) {
241674778d6cSJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
24177da65231SMatthew G Knepley     }
241874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
24197da65231SMatthew G Knepley   }
24207da65231SMatthew G Knepley   PetscFunctionReturn(0);
24217da65231SMatthew G Knepley }
2422e7c4fc90SDmitry Karpeev 
2423970e74d5SMatthew G Knepley #undef __FUNCT__
2424970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalFunction"
2425970e74d5SMatthew G Knepley /*@C
2426970e74d5SMatthew G Knepley   DMGetLocalFunction - Get the local residual function from this DM
2427970e74d5SMatthew G Knepley 
2428970e74d5SMatthew G Knepley   Not collective
2429970e74d5SMatthew G Knepley 
2430970e74d5SMatthew G Knepley   Input Parameter:
2431970e74d5SMatthew G Knepley . dm - The DM
2432970e74d5SMatthew G Knepley 
2433970e74d5SMatthew G Knepley   Output Parameter:
2434970e74d5SMatthew G Knepley . lf - The local residual function
2435970e74d5SMatthew G Knepley 
2436970e74d5SMatthew G Knepley    Calling sequence of lf:
2437970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2438970e74d5SMatthew G Knepley 
2439970e74d5SMatthew G Knepley +  snes - the SNES context
2440970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2441970e74d5SMatthew G Knepley .  f - local vector to put residual in
2442970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2443970e74d5SMatthew G Knepley 
2444970e74d5SMatthew G Knepley   Level: intermediate
2445970e74d5SMatthew G Knepley 
2446970e74d5SMatthew G Knepley .seealso DMSetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2447970e74d5SMatthew G Knepley @*/
2448970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalFunction(DM dm, PetscErrorCode (**lf)(DM, Vec, Vec, void *))
2449970e74d5SMatthew G Knepley {
2450970e74d5SMatthew G Knepley   PetscFunctionBegin;
2451970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2452970e74d5SMatthew G Knepley   if (lf) *lf = dm->lf;
2453970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2454970e74d5SMatthew G Knepley }
2455970e74d5SMatthew G Knepley 
2456970e74d5SMatthew G Knepley #undef __FUNCT__
2457970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalFunction"
2458970e74d5SMatthew G Knepley /*@C
2459970e74d5SMatthew G Knepley   DMSetLocalFunction - Set the local residual function from this DM
2460970e74d5SMatthew G Knepley 
2461970e74d5SMatthew G Knepley   Not collective
2462970e74d5SMatthew G Knepley 
2463970e74d5SMatthew G Knepley   Input Parameters:
2464970e74d5SMatthew G Knepley + dm - The DM
2465970e74d5SMatthew G Knepley - lf - The local residual function
2466970e74d5SMatthew G Knepley 
2467970e74d5SMatthew G Knepley    Calling sequence of lf:
2468970e74d5SMatthew G Knepley $    lf (SNES snes, Vec x, Vec f, void *ctx);
2469970e74d5SMatthew G Knepley 
2470970e74d5SMatthew G Knepley +  snes - the SNES context
2471970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2472970e74d5SMatthew G Knepley .  f - local vector to put residual in
2473970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2474970e74d5SMatthew G Knepley 
2475970e74d5SMatthew G Knepley   Level: intermediate
2476970e74d5SMatthew G Knepley 
2477970e74d5SMatthew G Knepley .seealso DMGetLocalFunction(), DMGetLocalJacobian(), DMSetLocalJacobian()
2478970e74d5SMatthew G Knepley @*/
2479970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalFunction(DM dm, PetscErrorCode (*lf)(DM, Vec, Vec, void *))
2480970e74d5SMatthew G Knepley {
2481970e74d5SMatthew G Knepley   PetscFunctionBegin;
2482970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2483970e74d5SMatthew G Knepley   dm->lf = lf;
2484970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2485970e74d5SMatthew G Knepley }
2486970e74d5SMatthew G Knepley 
2487970e74d5SMatthew G Knepley #undef __FUNCT__
2488970e74d5SMatthew G Knepley #define __FUNCT__ "DMGetLocalJacobian"
2489970e74d5SMatthew G Knepley /*@C
2490970e74d5SMatthew G Knepley   DMGetLocalJacobian - Get the local Jacobian function from this DM
2491970e74d5SMatthew G Knepley 
2492970e74d5SMatthew G Knepley   Not collective
2493970e74d5SMatthew G Knepley 
2494970e74d5SMatthew G Knepley   Input Parameter:
2495970e74d5SMatthew G Knepley . dm - The DM
2496970e74d5SMatthew G Knepley 
2497970e74d5SMatthew G Knepley   Output Parameter:
2498970e74d5SMatthew G Knepley . lj - The local Jacobian function
2499970e74d5SMatthew G Knepley 
2500970e74d5SMatthew G Knepley    Calling sequence of lj:
2501970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2502970e74d5SMatthew G Knepley 
2503970e74d5SMatthew G Knepley +  snes - the SNES context
2504970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2505970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2506970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2507970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2508970e74d5SMatthew G Knepley 
2509970e74d5SMatthew G Knepley   Level: intermediate
2510970e74d5SMatthew G Knepley 
2511970e74d5SMatthew G Knepley .seealso DMSetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2512970e74d5SMatthew G Knepley @*/
2513970e74d5SMatthew G Knepley PetscErrorCode DMGetLocalJacobian(DM dm, PetscErrorCode (**lj)(DM, Vec, Mat, Mat, void *))
2514970e74d5SMatthew G Knepley {
2515970e74d5SMatthew G Knepley   PetscFunctionBegin;
2516970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2517970e74d5SMatthew G Knepley   if (lj) *lj = dm->lj;
2518970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2519970e74d5SMatthew G Knepley }
2520970e74d5SMatthew G Knepley 
2521970e74d5SMatthew G Knepley #undef __FUNCT__
2522970e74d5SMatthew G Knepley #define __FUNCT__ "DMSetLocalJacobian"
2523970e74d5SMatthew G Knepley /*@C
2524970e74d5SMatthew G Knepley   DMSetLocalJacobian - Set the local Jacobian function from this DM
2525970e74d5SMatthew G Knepley 
2526970e74d5SMatthew G Knepley   Not collective
2527970e74d5SMatthew G Knepley 
2528970e74d5SMatthew G Knepley   Input Parameters:
2529970e74d5SMatthew G Knepley + dm - The DM
2530970e74d5SMatthew G Knepley - lj - The local Jacobian function
2531970e74d5SMatthew G Knepley 
2532970e74d5SMatthew G Knepley    Calling sequence of lj:
2533970e74d5SMatthew G Knepley $    lj (SNES snes, Vec x, Mat J, Mat M, void *ctx);
2534970e74d5SMatthew G Knepley 
2535970e74d5SMatthew G Knepley +  snes - the SNES context
2536970e74d5SMatthew G Knepley .  x - local vector with the state at which to evaluate residual
2537970e74d5SMatthew G Knepley .  J - matrix to put Jacobian in
2538970e74d5SMatthew G Knepley .  M - matrix to use for defining Jacobian preconditioner
2539970e74d5SMatthew G Knepley -  ctx - optional user-defined function context
2540970e74d5SMatthew G Knepley 
2541970e74d5SMatthew G Knepley   Level: intermediate
2542970e74d5SMatthew G Knepley 
2543970e74d5SMatthew G Knepley .seealso DMGetLocalJacobian(), DMGetLocalFunction(), DMSetLocalFunction()
2544970e74d5SMatthew G Knepley @*/
2545970e74d5SMatthew G Knepley PetscErrorCode DMSetLocalJacobian(DM dm, PetscErrorCode (*lj)(DM, Vec, Mat,  Mat, void *))
2546970e74d5SMatthew G Knepley {
2547970e74d5SMatthew G Knepley   PetscFunctionBegin;
2548970e74d5SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2549970e74d5SMatthew G Knepley   dm->lj = lj;
2550970e74d5SMatthew G Knepley   PetscFunctionReturn(0);
2551970e74d5SMatthew G Knepley }
255288ed4aceSMatthew G Knepley 
255388ed4aceSMatthew G Knepley #undef __FUNCT__
255488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
255588ed4aceSMatthew G Knepley /*@
255688ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
255788ed4aceSMatthew G Knepley 
255888ed4aceSMatthew G Knepley   Input Parameter:
255988ed4aceSMatthew G Knepley . dm - The DM
256088ed4aceSMatthew G Knepley 
256188ed4aceSMatthew G Knepley   Output Parameter:
256288ed4aceSMatthew G Knepley . section - The PetscSection
256388ed4aceSMatthew G Knepley 
256488ed4aceSMatthew G Knepley   Level: intermediate
256588ed4aceSMatthew G Knepley 
256688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
256788ed4aceSMatthew G Knepley 
256888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
256988ed4aceSMatthew G Knepley @*/
257088ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) {
257188ed4aceSMatthew G Knepley   PetscFunctionBegin;
257288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
257388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
257488ed4aceSMatthew G Knepley   *section = dm->defaultSection;
257588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
257688ed4aceSMatthew G Knepley }
257788ed4aceSMatthew G Knepley 
257888ed4aceSMatthew G Knepley #undef __FUNCT__
257988ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
258088ed4aceSMatthew G Knepley /*@
258188ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
258288ed4aceSMatthew G Knepley 
258388ed4aceSMatthew G Knepley   Input Parameters:
258488ed4aceSMatthew G Knepley + dm - The DM
258588ed4aceSMatthew G Knepley - section - The PetscSection
258688ed4aceSMatthew G Knepley 
258788ed4aceSMatthew G Knepley   Level: intermediate
258888ed4aceSMatthew G Knepley 
258988ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
259088ed4aceSMatthew G Knepley 
259188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
259288ed4aceSMatthew G Knepley @*/
259388ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) {
259488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
259588ed4aceSMatthew G Knepley 
259688ed4aceSMatthew G Knepley   PetscFunctionBegin;
259788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
259888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
259988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
260088ed4aceSMatthew G Knepley   dm->defaultSection = section;
260188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
260288ed4aceSMatthew G Knepley }
260388ed4aceSMatthew G Knepley 
260488ed4aceSMatthew G Knepley #undef __FUNCT__
260588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
260688ed4aceSMatthew G Knepley /*@
260788ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
260888ed4aceSMatthew G Knepley 
260988ed4aceSMatthew G Knepley   Input Parameter:
261088ed4aceSMatthew G Knepley . dm - The DM
261188ed4aceSMatthew G Knepley 
261288ed4aceSMatthew G Knepley   Output Parameter:
261388ed4aceSMatthew G Knepley . section - The PetscSection
261488ed4aceSMatthew G Knepley 
261588ed4aceSMatthew G Knepley   Level: intermediate
261688ed4aceSMatthew G Knepley 
261788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
261888ed4aceSMatthew G Knepley 
261988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
262088ed4aceSMatthew G Knepley @*/
262188ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) {
262288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
262388ed4aceSMatthew G Knepley 
262488ed4aceSMatthew G Knepley   PetscFunctionBegin;
262588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
262688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
262788ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
262888ed4aceSMatthew G Knepley     ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, &dm->defaultGlobalSection);CHKERRQ(ierr);
262988ed4aceSMatthew G Knepley   }
263088ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
263188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
263288ed4aceSMatthew G Knepley }
263388ed4aceSMatthew G Knepley 
263488ed4aceSMatthew G Knepley #undef __FUNCT__
263588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
263688ed4aceSMatthew G Knepley /*@
263788ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
263888ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
263988ed4aceSMatthew G Knepley 
264088ed4aceSMatthew G Knepley   Input Parameter:
264188ed4aceSMatthew G Knepley . dm - The DM
264288ed4aceSMatthew G Knepley 
264388ed4aceSMatthew G Knepley   Output Parameter:
264488ed4aceSMatthew G Knepley . sf - The PetscSF
264588ed4aceSMatthew G Knepley 
264688ed4aceSMatthew G Knepley   Level: intermediate
264788ed4aceSMatthew G Knepley 
264888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
264988ed4aceSMatthew G Knepley 
265088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
265188ed4aceSMatthew G Knepley @*/
265288ed4aceSMatthew G Knepley PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) {
265388ed4aceSMatthew G Knepley   PetscInt       nroots;
265488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
265588ed4aceSMatthew G Knepley 
265688ed4aceSMatthew G Knepley   PetscFunctionBegin;
265788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
265888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
265988ed4aceSMatthew G Knepley   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr);
266088ed4aceSMatthew G Knepley   if (nroots < 0) {
266188ed4aceSMatthew G Knepley     PetscSection section, gSection;
266288ed4aceSMatthew G Knepley 
266388ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
2664*31ea6d37SMatthew G Knepley     if (section) {
266588ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
266688ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
2667*31ea6d37SMatthew G Knepley     } else {
2668*31ea6d37SMatthew G Knepley       *sf = PETSC_NULL;
2669*31ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
2670*31ea6d37SMatthew G Knepley     }
267188ed4aceSMatthew G Knepley   }
267288ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
267388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
267488ed4aceSMatthew G Knepley }
267588ed4aceSMatthew G Knepley 
267688ed4aceSMatthew G Knepley #undef __FUNCT__
267788ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
267888ed4aceSMatthew G Knepley /*@
267988ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
268088ed4aceSMatthew G Knepley 
268188ed4aceSMatthew G Knepley   Input Parameters:
268288ed4aceSMatthew G Knepley + dm - The DM
268388ed4aceSMatthew G Knepley - sf - The PetscSF
268488ed4aceSMatthew G Knepley 
268588ed4aceSMatthew G Knepley   Level: intermediate
268688ed4aceSMatthew G Knepley 
268788ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
268888ed4aceSMatthew G Knepley 
268988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
269088ed4aceSMatthew G Knepley @*/
269188ed4aceSMatthew G Knepley PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) {
269288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
269388ed4aceSMatthew G Knepley 
269488ed4aceSMatthew G Knepley   PetscFunctionBegin;
269588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
269688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
269788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
269888ed4aceSMatthew G Knepley   dm->defaultSF = sf;
269988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
270088ed4aceSMatthew G Knepley }
270188ed4aceSMatthew G Knepley 
270288ed4aceSMatthew G Knepley #undef __FUNCT__
270388ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
270488ed4aceSMatthew G Knepley /*@C
270588ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
270688ed4aceSMatthew G Knepley   describing the data layout.
270788ed4aceSMatthew G Knepley 
270888ed4aceSMatthew G Knepley   Input Parameters:
270988ed4aceSMatthew G Knepley + dm - The DM
271088ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
271188ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
271288ed4aceSMatthew G Knepley 
271388ed4aceSMatthew G Knepley   Level: intermediate
271488ed4aceSMatthew G Knepley 
271588ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
271688ed4aceSMatthew G Knepley @*/
271788ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
271888ed4aceSMatthew G Knepley {
271988ed4aceSMatthew G Knepley   MPI_Comm        comm = ((PetscObject) dm)->comm;
272088ed4aceSMatthew G Knepley   PetscLayout     layout;
272188ed4aceSMatthew G Knepley   const PetscInt *ranges;
272288ed4aceSMatthew G Knepley   PetscInt       *local;
272388ed4aceSMatthew G Knepley   PetscSFNode    *remote;
272488ed4aceSMatthew G Knepley   PetscInt        pStart, pEnd, p, nroots, nleaves, l;
272588ed4aceSMatthew G Knepley   PetscMPIInt     size, rank;
272688ed4aceSMatthew G Knepley   PetscErrorCode  ierr;
272788ed4aceSMatthew G Knepley 
272888ed4aceSMatthew G Knepley   PetscFunctionBegin;
272988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
273088ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
273188ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
273288ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
273388ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
273488ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
273588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
273688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
273788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
273888ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
273988ed4aceSMatthew G Knepley   for(p = pStart, nleaves = 0; p < pEnd; ++p) {
274088ed4aceSMatthew G Knepley     PetscInt dof, cdof;
274188ed4aceSMatthew G Knepley 
274288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &dof);CHKERRQ(ierr);
274388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &cdof);CHKERRQ(ierr);
274488ed4aceSMatthew G Knepley     nleaves += dof < 0 ? -(dof+1)-cdof : dof-cdof;
274588ed4aceSMatthew G Knepley   }
274688ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr);
274788ed4aceSMatthew G Knepley   ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr);
274888ed4aceSMatthew G Knepley   for(p = pStart, l = 0; p < pEnd; ++p) {
274988ed4aceSMatthew G Knepley     PetscInt *cind;
275088ed4aceSMatthew G Knepley     PetscInt  dof, gdof, cdof, dim, off, goff, d, c;
275188ed4aceSMatthew G Knepley 
275288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
275388ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
275488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
275588ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
275688ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
275788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
275888ed4aceSMatthew G Knepley     dim  = dof-cdof;
275988ed4aceSMatthew G Knepley     for(d = 0, c = 0; d < dof; ++d) {
276088ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
276188ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
276288ed4aceSMatthew G Knepley     }
276388ed4aceSMatthew G Knepley     if (gdof < 0) {
276488ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
276588ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
276688ed4aceSMatthew G Knepley 
276788ed4aceSMatthew G Knepley         for(r = 0; r < size; ++r) {
276888ed4aceSMatthew G Knepley           if ((offset >= ranges[r]) && (offset < ranges[r+1])) break;
276988ed4aceSMatthew G Knepley         }
277088ed4aceSMatthew G Knepley         remote[l].rank  = r;
277188ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
277288ed4aceSMatthew G Knepley       }
277388ed4aceSMatthew G Knepley     } else {
277488ed4aceSMatthew G Knepley       for(d = 0; d < dim; ++d, ++l) {
277588ed4aceSMatthew G Knepley         remote[l].rank  = rank;
277688ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
277788ed4aceSMatthew G Knepley       }
277888ed4aceSMatthew G Knepley     }
277988ed4aceSMatthew G Knepley   }
278088ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
278188ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
278288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
278388ed4aceSMatthew G Knepley }
2784